Skip to content

Instantly share code, notes, and snippets.

@ljmartin
Created October 22, 2021 02:18
Show Gist options
  • Save ljmartin/cd2438eb6178d362c280dd24aa97336c to your computer and use it in GitHub Desktop.
Save ljmartin/cd2438eb6178d362c280dd24aa97336c to your computer and use it in GitHub Desktop.
from rdkit.Chem.Draw import rdMolDraw2D
from IPython.display import SVG
from rdkit import Chem
from rdkit.Chem import Draw
mol = Chem.MolFromSmiles(Chem.MolToSmiles(l))
beez = mol_to_bits(mol)
def mol_to_bits(mol):
"""Convert the atoms in a molecule into bit IDs.
This uses the morgan fingerprint with a depth of zero -
i.e. it only considers 1-atom fragments. Each fragment gets
a unique ID following the Morgan algorithm.
"""
bi = {}
fp = AllChem.GetMorganFingerprint(mol, 0, bitInfo=bi,useFeatures=True)
bits = np.zeros(mol.GetNumAtoms(),dtype=int)
for key, value in bi.items():
for v in value:
bits[v[0]] = key
return bits
d = rdMolDraw2D.MolDraw2DSVG(500, 500) # or MolDraw2DSVG to get SVGs
for i in range(mol.GetNumAtoms()):
mol.GetAtomWithIdx(i).SetProp('atomNote', str(int(beez[i])))
d.DrawMolecule(mol)
d.FinishDrawing()
SVG(d.GetDrawingText())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment