ketcher.getMolfile() - Export Molecule in Molfile Format

Q

How to export the molecule structure in Molfile format from the Ketcher with the ketcher.getMolfile() method?

✍: FYIcenter.com

A

To export the molecule structure in Molfile format from the Ketcher, you can use the getMolfile() method provided by the Ketcher API as shown in below:

1. Load the Ketcher editor in an "iframe" HTML element:

<iframe id=frmKetcher src="/ketcher/index.html"
  style="width: 680px; height: 400px;"></iframe>

2. Create a "button" HTML element with a "click" event handler.

<p><button onclick="exportMolfile();">Export Molecule in Molfile</button></p>

3. Access the "ketcher" object from the "iframe" element in the "click" event handler directly, since the "iframe" element is already loaded:

  frm = document.getElementById("frmKetcher");
  ketcher = frm.contentWindow.ketcher;

4. Call the ketcher.getMolfile() method to export the molecule structure from the Ketcher editor. Note that this method returns a "promise" object, which represents a child thread to perform the task. We need to add two handlers through the promise.then() method to catch the result and error.

The following HTML document gives you an example of how to export the molecule structure in Molfile format from the Ketcher:

<html>
<!-- export-molecule-as-Molfile.html Copyright (c) FYIcenter.com. -->
<head>
<title>Export Molecule Structure in Molfile Format</title>
</head>
<body>
<p>Create a molecule structure, then click the export button below the editor:</p>
<iframe id=frmKetcher src="/ketcher/index.html"
  style="width: 680px; height: 400px;"></iframe>

<p><button onclick="exportMolfile();">Export Molecule in Molfile</button></p>
<pre id=output style="background-color: #ddd;">
</pre>
<script>
function exportMolfile() {
  frm = document.getElementById("frmKetcher");
  ketcher = frm.contentWindow.ketcher;
  promise = ketcher.getMolfile();
  promise.then(
    function(value) {
      document.getElementById("output").innerHTML = value;
    },
    function(error) {
      document.getElementById("output").innerHTML = error.toString();
    }
  );
}
</script>
</body>
</html>

5. Put the above HTML document on your local Web server and open it in your browser, you will see the Ketcher editor loaded and ready to use.

6. Draw an aspirin molecule structure in the editor.

7. Click "Export Molecule in Molfile" button, you will see the molecule structure represented in Molfile format displayed:

ketcher.getMolfile() - Export Molecule in Molfile Format
ketcher.getMolfile() - Export Molecule in Molfile Format

 

ketcher.getRxn() - Export Reaction in RXN Format

ketcher.setMolecule() - Import Molecule into Ketcher

Ketcher JavaScript API

⇑⇑ Ketcher - Chemical Structure Editor

2024-03-17, 652🔥, 0💬