partridgejiang / cheminfo-to-web

An experimental project to recompile native chemoinformatics libs into JavaScript.
https://partridgejiang.github.io/cheminfo-to-web/
MIT License
13 stars 3 forks source link

Is it possible to generate 3D coordinates? #5

Open nixondevph opened 3 years ago

nixondevph commented 3 years ago

My code doesn't seem to work

function func(){
    var conv = new OpenBabel.ObConversionWrapper();
    try
    {
        var inData = "C(Cl)(=O)CCC(=O)Cl";
        conv.setInFormat('', 'smi'); 
        var mol = new OpenBabel.OBMol(); 
        conv.readString(mol, inData);
        conv.setOutFormat('', 'mol'); 

        // my code
        var gen3d = new OpenBabel.OBOp().FindType("Gen3D");
        gen3d.Do(mol);

        var outData = conv.writeString(mol, false);
        console.log(outData);
    }
    finally
    {
        conv.delete();
    }
}
partridgejiang commented 3 years ago

Hi @DineshOng, class OBOp is not exported in current JavaScript compilation of OpenBabel, so the codes above should not work at present. I'll check it and try to include OBOp in the future.

partridgejiang commented 3 years ago

@DineshOng, please check the compilation base on OpenBabel3 at OpenBabel3/OpenBabel-js/bin/. In this new compilation, OBOp is exported and the code above should be work now.

nixondevph commented 3 years ago

@DineshOng, please check the compilation base on OpenBabel3 at OpenBabel3/OpenBabel-js/bin/. In this new compilation, OBOp is exported and the code above should be work now.

Hello @partridgejiang, do you have a demo of the new OpenBabel3?

partridgejiang commented 3 years ago

Hi @DineshOng, the OpenBabel demo page has been updated, please check it on https://partridgejiang.github.io/cheminfo-to-web/demos/items/OpenBabel/openBabelDemo.html, in which a gen3D operation demo has been added. Generally, if a 3D structure need to be generated from a SMILES string, the following codes can be used:

var smiles= 'C(Cl)(=O)CCC(=O)Cl';
var conv = new OpenBabel.ObConversionWrapper();
conv.setInFormat('', 'smi');
var mol = new OpenBabel.OBMol();
conv.readString(mol, smiles);
var gen3d = OpenBabel.OBOp.FindType('Gen3D');  // FindType is actually a factory constructor, no need to use "new" here
if (!gen3d.Do(mol, ''))
  console.error('Generate 3D failed');
else
{
  conv.setOutFormat('', 'mol');
  var outputData = conv.writeString(mol, false);
  console.log(outputData);
}
conv.delete();
mol.delete();

By the way, please use the latest compiliation including openbabel.js, openbabel.wasm and openbabel.data.

nixondevph commented 3 years ago

How to instantiate OpenBabel?

partridgejiang commented 3 years ago

Hi @DineshOng, please check the overview section of the OpenBabel demo (https://partridgejiang.github.io/cheminfo-to-web/demos/items/OpenBabel/openBabelDemo.html), the OpenBabel module can be created with calling to function OpenBabelModule(), and a callback function can be used to acknowledge the ready state of OpenBabel module:

var OpenBabel = OpenBabelModule();
OpenBabel.onRuntimeInitialized = function()
{
  // Now the OpenBabel module is ready
  var conv = new OpenBabel.ObConversionWrapper();
  // ...
}