partridgejiang / Kekule.js

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

Is it possible to get connectedObjs of Atom #277

Closed agrodet closed 2 years ago

agrodet commented 2 years ago

Hello, and thank you again for this repository. I have a simple question. Using Bond.connectedObjs, we can know which Atom are connected to a specific Bond. I would like to do the reverse: from a selected Atom, I would like to know the Bond it is connected to. Of course, by doing some algorithmic gymnastics, it is not so hard to go through all Bond to find the ones connected to the currently selected Atom. However, it is not very practical if one wants to "walk" the paths from the selected Atom. Is there any more direct way to go from Atom to Bond?

partridgejiang commented 2 years ago

Hi @agrodet, you may use the atom.linkedConnectors property to retrieve all connectors linking to this atom. Aside from normal bonds, other types of connector may be included in this property too. If only bonds should be considered, the getLinkedBonds method can be used here:

let bonds = atom.getLinkedBonds();   // retrieve all bonds
let covalenceBonds = atom.getLinkedBonds(Kekule.BondType.COVALENT);  // retrieve only covalent bonds
agrodet commented 2 years ago

I see! I didn't find it in tutorials, and was looking for properties. Should have checked the methods as well 😄

Thank you very much! Awesome library.