partridgejiang / Kekule.js

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

Support for lone pairs in a molecule? #40

Open AaronLlanos opened 6 years ago

AaronLlanos commented 6 years ago

Messing with the charge button, it gives me the option to add radicals, but I can't draw out lone pairs. Other libraries have similar functionality to add multiple lone pairs to an atom. Similar to how Kekule bonds work, with evenly spacing out lone pairs and radicals around an atom. Is this an intended feature for Kekule? Or would I have to build something out like that myself? I don't mind either approach but some beginning steps on how to attack the problem might help me out.

Thanks!

partridgejiang commented 6 years ago

Actually the radical buttons are designed to set the radical orders of an atom (following the MDL MOL strategy) instead of to add multiple lone pairs. I'll investigate that request and may implement it soon.

AaronLlanos commented 6 years ago

@partridgejiang thank you. Please let me know. I don't mind trying to help out when/where I can!

ragnerrok commented 6 years ago

@partridgejiang Any updates towards having lone pairs? I also don't mind trying to help out! ^_^

AaronLlanos commented 6 years ago

@partridgejiang would it also work if we took advantage of custom elements that kekule already has enabled? For example using a custom element (L) to represent lone pairs?

AaronLlanos commented 6 years ago

Or also (LL) for lone pairs and (L) for radicals that you could set custom coordinates on?

partridgejiang commented 6 years ago

Hi @AaronLlanos, I am still working on it. In our current design, the lone pairs in editor are implemented as attached markers to atoms (and other types of nodes). After the full implementation, not only the lone pairs but also the charge, radical markers, and even text/image notes can be attached and can be movable and changeable (with font, size, etc.).

partridgejiang commented 6 years ago

@AaronLlanos & @ragnerrok, now the lone pair support has been added to the composer, you can download the latest js files in dist directory of editorEnhancement branch to try it, :). We also add the ability to move positions of lone pairs and charge markers, just as any other objects in editor. Defaultly, the distance between marker and the atom is restrained during moving, but you can press Alt key to break such a constraint. If you find any bug, please do let me know, thanks.

AaronLlanos commented 6 years ago

@partridgejiang it looks awesome! I have tried with the setup of using kekule.min.js and the kekule.css on a basic html file. I have a couple notes:

partridgejiang commented 6 years ago

@AaronLlanos Forgot to say, if you select the lone pair first (surrounding with a blue rectangle mark) then move it, distance will not be retained too. To start a constraint moving, just mouse down directly on a deselected lone pair and then move the mouse. The second question, MOL data format does not support recording of lone pairs (but does support charge and radical). So to save it, you have to switch to KCJ or KCX.

AaronLlanos commented 6 years ago

Ah perfect. I have been testing this out over the last few days and noticed that if I compare two structures with different number of lone pairs, say CH2 with one loan pair vs CH2 with 4 lone pairs, they are in fact returned as the same structure. I was wondering if this was an intentional update, or if it was a overlook?

Thank you so much for your continued support and implementing this so quickly!

partridgejiang commented 6 years ago

@AaronLlanos , now an additional option for lone pairs is enabled in structure comparison, e.g.:

var isSame = molecule1.isSameStructureWith(molecule2, {'lonePair': true});

Just download the files in dist directory of editorEnhancement branch for testing, :).

AaronLlanos commented 6 years ago

@partridgejiang thank you so much for that! I think the functionality is awesome. Follow up questions I had are:

  1. How hard would it also be to add support for single electrons (not the radicals) for more advanced lewis dot structure support?
  2. Are there plans to merge this into master?
partridgejiang commented 6 years ago

Hi @AaronLlanos,

  1. Actually, the single electron can also be drawn in editor at present. Just add a electron pair, select it, open the object inspector, then change electronCount property from 2 to 1.
  2. The branch will surely be merged into master in the future, just need more tests and efficiency enhancement.