From eb1c54436672fe9d249b13e55b020610572619c4 Mon Sep 17 00:00:00 2001 From: Beat Buesser Date: Wed, 11 Dec 2013 23:29:10 -0500 Subject: [PATCH] Add function updateLonePairs() and call in fromRDKitMol() - setting the number of lone electron pairs - also fixes fromSMILES and similar functions using fromRDKitMol() --- rmgpy/molecule/molecule.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/rmgpy/molecule/molecule.py b/rmgpy/molecule/molecule.py index ba41a91b55..93ad7c6d3c 100644 --- a/rmgpy/molecule/molecule.py +++ b/rmgpy/molecule/molecule.py @@ -1048,7 +1048,7 @@ def fromRDKitMol(self, rdkitmol): """ # Below are the declared variables for cythonizing the module cython.declare(i=cython.int) - cython.declare(radicalElectrons=cython.int, spinMultiplicity=cython.int, charge=cython.int) + cython.declare(radicalElectrons=cython.int, spinMultiplicity=cython.int, charge=cython.int, lonePairs=cython.int) cython.declare(atom=Atom, atom1=Atom, atom2=Atom, bond=Bond) self.vertices = [] @@ -1076,7 +1076,7 @@ def fromRDKitMol(self, rdkitmol): # Process charge charge = rdkitatom.GetFormalCharge() - atom = Atom(element, radicalElectrons, spinMultiplicity, charge) + atom = Atom(element, radicalElectrons, spinMultiplicity, charge, '', 0) self.vertices.append(atom) # Add bonds by iterating again through atoms @@ -1098,6 +1098,7 @@ def fromRDKitMol(self, rdkitmol): # Set atom types and connectivity values self.updateConnectivityValues() + self.updateLonePairs() self.updateAtomTypes() return self @@ -1790,4 +1791,25 @@ def getRadicalAtoms(self): if atom.radicalElectrons > 0: radicalAtomsList.append(atom) return radicalAtomsList + + def updateLonePairs(self): + """ + Iterate through the atoms in the structure and calcualte the + number of lone electron pairs, assumin a neutral molecule. + """ + for atom1 in self.vertices: + order = 0 + if not atom1.isHydrogen(): + for atom2, bond12 in atom1.edges.items(): + if bond12.isSingle(): + order = order + 1 + if bond12.isDouble(): + order = order + 2 + if bond12.isTriple(): + order = order + 3 + + atom1.lonePairs = 4 - atom1.radicalElectrons - order + + else: + atom1.lonePairs = 0