Skip to content

Instantly share code, notes, and snippets.

@ljmartin
Last active August 16, 2024 03:52
Show Gist options
  • Save ljmartin/3b9fe9aaa536def3219657f0910e7098 to your computer and use it in GitHub Desktop.
Save ljmartin/3b9fe9aaa536def3219657f0910e7098 to your computer and use it in GitHub Desktop.
#https://github.com/rdkit/rdkit/discussions/6135
from rdkit import Chem
from rdkit.Chem import AllChem
def mol_to_bits(mol, radius=0):
bi = {}
fp = AllChem.GetMorganFingerprint(mol, radius, bitInfo=bi,useFeatures=False)
bits = np.zeros(mol.GetNumAtoms(),dtype=int)
for key, value in bi.items():
for v in value:
if v[1]==radius:
bits[v[0]] = key
return bits
m = Chem.MolFromSmiles('C([O-])=O')
print('0th neighbourhood bits:', mol_to_bits(m, radius=1))
substruc = Chem.MolFromSmarts('[C](-O)=O')
print(m.GetSubstructMatches(substruc))
#for radius=1, also set bonds to be symmetrical
bond = m.GetBondBetweenAtoms(0, 1)
bond.SetBondType(Chem.BondType.ONEANDAHALF)
bond = m.GetBondBetweenAtoms(0, 2)
bond.SetBondType(Chem.BondType.ONEANDAHALF)
m.GetAtomWithIdx(1).SetFormalCharge(-1)
m.GetAtomWithIdx(2).SetFormalCharge(-1)
#note, two oxygens are equal now.
mol_to_bits(m, radius=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment