From 071a8e726f4107d6b6ab44bfa71812a6391add99 Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 13:33:29 +0200 Subject: [PATCH 1/6] InChI Key getter in molecule.Molecule similar to InChI getter via OpenBabel. Removes the checksum characters (last 2) --- rmgpy/molecule.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rmgpy/molecule.py b/rmgpy/molecule.py index da8e3b5bd8..2d938b9c33 100644 --- a/rmgpy/molecule.py +++ b/rmgpy/molecule.py @@ -998,6 +998,23 @@ def toInChI(self): obConversion.SetOutFormat('inchi') obConversion.SetOptions('w', openbabel.OBConversion.OUTOPTIONS) return obConversion.WriteString(obmol).strip() + + 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 toSMILES(self): """ From b4248bde6ffc72e294c5008e9822df0018b19e35 Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 14:45:55 +0200 Subject: [PATCH 2/6] Unit test for InChIKey generation. --- unittest/moleculeTest.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/unittest/moleculeTest.py b/unittest/moleculeTest.py index 5da1ea8c21..6b3cd133b2 100644 --- a/unittest/moleculeTest.py +++ b/unittest/moleculeTest.py @@ -872,6 +872,14 @@ 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') ################################################################################ class TestMoleculeSymmetry(unittest.TestCase): From 4999a5a51a4fbde3da9cf2e8ab384c3696566904 Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 15:13:08 +0200 Subject: [PATCH 3/6] Augmented InChI getter in Molecule Augmented InChI adds a layer denoted by /mult at the end of the existing inchi to cope with polyradicals (>=2). The number returned in the /mult layer equals the total number of unpaired electrons + 1 --- rmgpy/molecule.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rmgpy/molecule.py b/rmgpy/molecule.py index 2d938b9c33..89951c48e8 100644 --- a/rmgpy/molecule.py +++ b/rmgpy/molecule.py @@ -999,6 +999,20 @@ def toInChI(self): 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 From 860eb261070438a873ee82c37fcc7829b39bfb6f Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 15:14:19 +0200 Subject: [PATCH 4/6] Augmented InChIKey getter in Molecule similar to the augmented InChI the InChIKey is extended with a /mult layer --- rmgpy/molecule.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/rmgpy/molecule.py b/rmgpy/molecule.py index 89951c48e8..f6a0d2d123 100644 --- a/rmgpy/molecule.py +++ b/rmgpy/molecule.py @@ -1029,7 +1029,21 @@ def toInChIKey(self): 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 From c1316a912ab758ec04289f0ee17067cb06d0b1d7 Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 15:15:06 +0200 Subject: [PATCH 5/6] unit test for Augmented InChI --- unittest/moleculeTest.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/unittest/moleculeTest.py b/unittest/moleculeTest.py index 6b3cd133b2..51c128eca2 100644 --- a/unittest/moleculeTest.py +++ b/unittest/moleculeTest.py @@ -880,6 +880,18 @@ def testInChIKey(self): 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') + ################################################################################ class TestMoleculeSymmetry(unittest.TestCase): From 9ce542c3c763cdac540383793ec8792333dc21ea Mon Sep 17 00:00:00 2001 From: Nick Vandewiele Date: Thu, 17 May 2012 15:15:26 +0200 Subject: [PATCH 6/6] unit test for Augmented InChIKey --- unittest/moleculeTest.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unittest/moleculeTest.py b/unittest/moleculeTest.py index 51c128eca2..d788d9d33c 100644 --- a/unittest/moleculeTest.py +++ b/unittest/moleculeTest.py @@ -892,6 +892,16 @@ def testAugmentedInChI(self): 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):