partridgejiang / Kekule.js

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

Redrawing or recalculating all atom coordinates to be preferred bond angles #286

Closed Luckp1089 closed 9 months ago

Luckp1089 commented 1 year ago

Hi again!

When I modify a molecule by adding atoms to it, sometimes the bond angles come out looking a little strange. I was wondering if there was a way I could append Nodes and Bonds, and then recalculate the whole molecule so that it "redraws" it correctly? for reference, this is what my drawing looks like:

image

and here is a snippet of code that creates that drawing:

image

I am having trouble assigning the correct coordinates and was hoping for an easy way to just append Nodes and Connectors, and then redraw the whole molecule (this would be especially helpful when trying to draw rings as well):

image

I know that my current code assigns the coordinates but I was hoping for a way to populate "dummy" coordinates and have the drawer make the molecule look good. I have tried using mol.clean(); and mol.recalcCoords();, but these do not seem to change the structure of the molecule.

Thanks!

partridgejiang commented 1 year ago

Hi @Luckp1089, currently you have to utilize the extra OpenBabel wasm module to enable the 2D layout of molecule. The following code may help your cause:

Kekule.OpenBabel.enable(function(error){   // load the extra OpenBabel.wasm module
  if (!error)
  {
    // now the module has been succesfully loaded
    const SiAtom = new Kekule.Atom('Si_Atom').setSymbol('C');
    // ...
    mol.appendNode(SiAtom);
    // ...
    // code to build molecule, but the coordinate settings can be omitted

    // generate the 2D coordiates of atoms  
    const calculator = Kekule.Calculator.generateStructure(mol, Kekule.Calculator.Services.GEN2D, {modifySource: true},
      function(generatedMol) {
        console.log('Layout done');        
      }
    );    
  }
});
Luckp1089 commented 9 months ago

This worked well, closing this issue. Thanks!!