partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
247 stars 61 forks source link

Save molecules in Inchi format #306

Closed joeolu14 closed 1 year ago

joeolu14 commented 1 year ago

Is there a way to saveFormatData in Inchi format?

This what I tried but did not work

 const generateMoleCules = async () => {
    const composer = Kekule.Widget.getWidgetById("composer");
    const mols = composer.exportObjs(Kekule.Molecule);
    const msg = "Molecule count: " + mols.length + "\n ";
    const m = [];
    for (let i = 0, l = mols.length; i < l; ++i) {
      const mol = mols[i];
      m.push(Kekule.IO.saveFormatData(mol, "smi"));
      const abc = Kekule.IO.saveFormatData(mol, "mol");

      const ccc = Kekule.InChI.molDataToInChI(mol);

      console.log(ccc);

      setMole(m); 
    }
  };
partridgejiang commented 1 year ago

Hi @joeolu14, since InChI is actually a external wasm module, you need to manual enable the InChI functions before using them, e.g.:

Kekule.InChI.enable( error => {
  if (!error) {
    let inchi = Kekule.IO.saveFormatData(mol, 'inchi');
    console.log(inchi);
  }
});
joeolu14 commented 1 year ago

@partridgejiang I tried the above and got this error: image

This is what the code looks like:

  const generateMoleCules = async () => {
    const composer = Kekule.Widget.getWidgetById("composer");
    const mols = composer.exportObjs(Kekule.Molecule);
    const msg = "Molecule count: " + mols.length + "\n ";
    const m = [];
    for (let i = 0, l = mols.length; i < l; ++i) {
      const mol = mols[i];
      m.push(Kekule.IO.saveFormatData(mol, "smi"));
      Kekule.InChI.enable((error: any) => {
        if (!error) {
          let inchi = Kekule.IO.saveFormatData(mol, "inchi");
          console.log(inchi);
        }
      });
      setMole(m);
    }
  };
partridgejiang commented 1 year ago

Hi @joeolu14, The error displayed ( Unexpected token '<' ) seems caused by the web server? Is content of inchi.wasm properly returned by server? Perhaps you could switch to the network panel of developer tools in web browser to check which files and contents are transfered when Kekule.InChI.enable() being called.

joeolu14 commented 1 year ago

@partridgejiang I saw the error, I had to copy the libs to my static folder to avoid the error, but the issue now is, the inchi is not being created and does not look like anything inside the enable block gets read. This is what my code looks like

  const generateMoleCules = async () => {
    const composer = Kekule.Widget.getWidgetById("composer");
    const mols = composer.exportObjs(Kekule.Molecule);
    const msg = "Molecule count: " + mols.length + "\n ";
    const m = [];
    for (let i = 0, l = mols.length; i < l; ++i) {
      const mol = mols[i];

      m.push(Kekule.IO.saveFormatData(mol, "mol"));

      Kekule.InChI.enable((error: any) => {
        if (error) {
          console.log(error);
        }
        if (!error) {
          let inchi = Kekule.IO.saveFormatData(mol, "inchi");
          console.log(inchi, "inchi");
        }
      });

      setMole(m);
    }
  };

is there anything to do to get this working?

partridgejiang commented 1 year ago

Hi @joeolu14, you may check the status of Kekule InChI module in the console of the web development tool to find out whether the module is properly loaded and initialized:

Kekule.InChI.isScriptLoaded()    // should returns true if the wasm is loaded
Kekule.InChI.getInChIScriptUrl()   // should returns the path to the inchi.js

If the Kekule.InChI.getInChIScriptUrl() is different from the actual location of inchi.js (and inchi.wasm), you need to manually set it before calling Kekule.InChI.enable():

Kekule.environment.setEnvVar('inchi.scriptSrc', '/the/real/path/to/inchi.js');
// ...
Kekule.InChI.enable(...);
joeolu14 commented 1 year ago

@partridgejiang when I checked isScriptLoaded() it returns false. the getInchiScriptUrl() is pointed to where I have the inchi.js and the wasm. I feel Kekule.InChI.enable is not being called or executed.

from the image below the inchi.js script is loaded and read image

partridgejiang commented 1 year ago

@joeolu14, would you please provide a simple demo file of you app here? So that I may debug it in my environment? Thanks.