partridgejiang / Kekule.js

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

Single molecule drawn gets split into two different molecules (in mol and smi formats) #82

Closed GerardBCN closed 6 years ago

GerardBCN commented 6 years ago

Hi Patridge, first of all thanks for your great work in this plugin. It's becoming very useful for our own chemistry app collection (www.playmolecule.org).

I am facing a problem when drawing a specific subset of molecules, specially when I am trying to link couple of rings together such as in the following case:

image

I then retrieve the smiles or mol with the following code:

var mols = composer.getChemObj();  
var smi = Kekule.IO.saveFormatData(mols, 'smi');

The molecule retrieved (in smiles) is the following:

C1CCCC1.c2(C)ccccc2

What bothers me is that most of the programs in the back-end (rdkit, htmd) for molecule reading will interpret the string as two different molecules, probably caused by the dot (".") in the smiles string. The same happens when using mol format (presumably because missing connectivity between the rings, haven't tested it).

Is this the intended behavior? Maybe I am missing the point on how to use the widget. The intuitive behavior for me is to draw a ring, then click on "Bond tool", click on the atom of the ring I want a bond attached to, then click on "Ring structures", select my ring and click on the extreme of the recently added bond. This however, results in two different molecules when I was expecting one.

Thanks for your help.

partridgejiang commented 6 years ago

@GerardBCN Hi Gerard, the dot is really the delimiter of two molecules in SMILES. Perhaps your code retrieves the SMILES string before the mouseup/pointerup event when adding the new ring? Actually, when user insert new structure to the editor (and move it around), the structure is handled as a separated molecule by default. When the user interaction is finished (e.g., mouseup), the new inserted molecule may be merged into the existing one. So the safe way is to save SMILES after the user action. Another approach is to turn of the merge preview option in editor:

composer.getEditorConfigs().getInteractionConfigs().setEnableMergePreview(false);

This method works without any modifications of your code, but may have a small impact on performance .

GerardBCN commented 6 years ago

Thanks a lot for your help. The one-liner fix worked. :)