Skip to content

Getters to InChIKey and augmented InChI in Molecule #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion rmgpy/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,52 @@ def toInChI(self):
obConversion.SetOutFormat('inchi')
obConversion.SetOptions('w', openbabel.OBConversion.OUTOPTIONS)
return obConversion.WriteString(obmol).strip()


def toAugmentedInChI(self):
"""
Adds an extra layer to the InChI denoting the number of unpaired electrons in case
more than 1 ( >= 2) unpaired electrons are present in the molecule.
"""
inchi = self.toInChI()

radicalNumber = sum([i.radicalElectrons for i in self.atoms])

if radicalNumber >= 2:
return inchi+'/mult'+str(radicalNumber+1)
else:
return inchi

def toInChIKey(self):
"""
Convert a molecular structure to an InChI Key string. Uses
`OpenBabel <http://openbabel.org/>`_ to perform the conversion.

Removes check-sum dash (-) and character so that only
the 14 + 9 characters remain.
"""
import openbabel
# This version does not write a warning to stderr if stereochemistry is undefined
obmol = self.toOBMol()
obConversion = openbabel.OBConversion()
obConversion.SetOutFormat('inchi')
obConversion.SetOptions('w', openbabel.OBConversion.OUTOPTIONS)
obConversion.SetOptions('K', openbabel.OBConversion.OUTOPTIONS)
return obConversion.WriteString(obmol).strip()[:-2]

def toAugmentedInChIKey(self):
"""
Adds an extra layer to the InChIKey denoting the number of unpaired electrons in case
more than 1 ( >= 2) unpaired electrons are present in the molecule.
"""
key = self.toInChIKey()

radicalNumber = sum([i.radicalElectrons for i in self.atoms])

if radicalNumber >= 2:
return key+'/mult'+str(radicalNumber+1)
else:
return key

def toSMILES(self):
"""
Convert a molecular structure to an SMILES string. Uses
Expand Down
30 changes: 30 additions & 0 deletions unittest/moleculeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,36 @@ def testRadicalCH2(self):
molecule = Molecule().fromSMILES('[CH2]')
self.assertEqual(molecule.atoms[0].radicalElectrons, 2)
self.assertEqual(molecule.atoms[0].spinMultiplicity, 3)

def testInChIKey(self):
"""
Test that InChI Key generation is working properly.
"""
molecule = Molecule().fromInChI('InChI=1S/C7H12/c1-2-7-4-3-6(1)5-7/h6-7H,1-5H2')
key = molecule.toInChIKey()
self.assertEqual(key, 'UMRZSTCPUPJPOJ-UHFFFAOYSA')

def testAugmentedInChI(self):
"""
Test that Augmented InChI generation is printing the /mult layer
"""
mol = Molecule().fromAdjacencyList("""
1 C 1 {2,S}
2 C 1 {1,S}
""")

self.assertEqual(mol.toAugmentedInChI(self), 'InChI=1S/C2H4/c1-2/h1-2H2/mult3')

def testAugmentedInChIKey(self):
"""
Test that Augmented InChI Key generation is printing the /mult layer
"""
mol = Molecule().fromAdjacencyList("""
1 C 1 {2,S}
2 C 1 {1,S}
""")

self.assertEqual(mol.toAugmentedInChIKey(self), 'VGGSQFUCUMXWEO-UHFFFAOYSA/mult3')
################################################################################

class TestMoleculeSymmetry(unittest.TestCase):
Expand Down