Skip to content

Instantly share code, notes, and snippets.

@nickvandewiele
Last active August 29, 2015 14:20
Show Gist options
  • Save nickvandewiele/704f278ca3be4b58b7b7 to your computer and use it in GitHub Desktop.
Save nickvandewiele/704f278ca3be4b58b7b7 to your computer and use it in GitHub Desktop.
Chemoinformatics: A way to reconstruct an RMG mol from an obmol.
#!/usr/bin/python
import openbabel as ob
"""
This gist shows how to create a molecule from an OB mol.
OpenBabel does not have the obmol.GetAtomByIdx(int) method.
"""
def createOBMol():
inchistr = "InChI=1S/CH4/h1H4"
obConversion = ob.OBConversion()
obConversion.SetInAndOutFormats("inchi", "smi")
obmol = ob.OBMol()
obConversion.ReadString(obmol, inchistr)
obmol.AddHydrogens()
return obmol
def main():
print 'creating OBMol: '
obmol = createOBMol()
print 'creating mol: '
vertices = []
# iterate through atoms in obmol
for obatom in ob.OBMolAtomIter(obmol):
idx = obatom.GetIdx()#openbabel idx starts at 1!
print 'idx: ', idx
atom = 'Mock atom'
vertices.append(atom)#array index corresponds to idx of obatom!
# iterate through bonds in obmol
for obbond in ob.OBMolBondIter(obmol):
atom1 = vertices[obbond.GetBeginAtomIdx() - 1]
atom2 = vertices[obbond.GetEndAtomIdx() - 1]
order = obbond.GetBondOrder()
bond = 'Mock bond'#python array indices start at 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment