diff --git a/rmgpy/molecule.py b/rmgpy/molecule.py index da8e3b5bd8..f6a0d2d123 100644 --- a/rmgpy/molecule.py +++ b/rmgpy/molecule.py @@ -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 `_ 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 diff --git a/unittest/moleculeTest.py b/unittest/moleculeTest.py index 5da1ea8c21..d788d9d33c 100644 --- a/unittest/moleculeTest.py +++ b/unittest/moleculeTest.py @@ -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):