def getMolecularWeight(self): """ Return the molecular weight of the molecule in kg/mol. """ mass = 0 H = elements.getElement('H') for atom in self.vertices: mass += atom.element.mass return mass
def fromOBMol(self, obmol, implicitH=False): """ Convert an OpenBabel OBMol object `obmol` to a molecular structure. Uses `OpenBabel <http://openbabel.org/>`_ to perform the conversion. """ self.vertices = [] self.edges = {} # Add hydrogen atoms to complete molecule if needed obmol.AddHydrogens() # Iterate through atoms in obmol for i in range(0, obmol.NumAtoms()): obatom = obmol.GetAtom(i + 1) # Use atomic number as key for element number = obatom.GetAtomicNum() element = elements.getElement(number=number) # Process spin multiplicity radicalElectrons = 0 spinMultiplicity = obatom.GetSpinMultiplicity() if spinMultiplicity == 0: radicalElectrons = 0; spinMultiplicity = 1 elif spinMultiplicity == 1: radicalElectrons = 2; spinMultiplicity = 1 elif spinMultiplicity == 2: radicalElectrons = 1; spinMultiplicity = 2 elif spinMultiplicity == 3: radicalElectrons = 2; spinMultiplicity = 3 # Process charge charge = obatom.GetFormalCharge() coord = np.array([obatom.x(), obatom.y(), obatom.z()]) label = obatom.GetIdx() atom = Atom(element, coord, radicalElectrons, spinMultiplicity, 0, charge, str(label)) self.vertices.append(atom) self.edges[atom] = {} # Add bonds by iterating again through atoms for j in range(0, i): obatom2 = obmol.GetAtom(j + 1) obbond = obatom.GetBond(obatom2) if obbond is not None: order = 0 # Process bond type if obbond.IsSingle(): order = 'S' elif obbond.IsDouble(): order = 'D' elif obbond.IsTriple(): order = 'T' elif obbond.IsAromatic(): order = 'B' bond = Bond(order) atom1 = self.vertices[i] atom2 = self.vertices[j] self.edges[atom1][atom2] = bond self.edges[atom2][atom1] = bond # Make hydrogens implicit to conserve memory if implicitH: self.makeHydrogensImplicit() return self
def fromOBMol(self, obmol): """ Convert an OpenBabel OBMol object `obmol` to a molecular structure. Uses `OpenBabel <http://openbabel.org/>`_ to perform the conversion. """ cython.declare(i=cython.int) cython.declare(radicalElectrons=cython.int, spinMultiplicity=cython.int, charge=cython.int) cython.declare(atom=Atom, atom1=Atom, atom2=Atom, bond=Bond) self.vertices = [] # Add hydrogen atoms to complete molecule if needed obmol.AddHydrogens() # Iterate through atoms in obmol for i in range(0, obmol.NumAtoms()): obatom = obmol.GetAtom(i + 1) # Use atomic number as key for element number = obatom.GetAtomicNum() element = elements.getElement(number) # Process spin multiplicity radicalElectrons = 0 spinMultiplicity = obatom.GetSpinMultiplicity() if spinMultiplicity == 0: radicalElectrons = 0; spinMultiplicity = 1 elif spinMultiplicity == 1: radicalElectrons = 2; spinMultiplicity = 1 elif spinMultiplicity == 2: radicalElectrons = 1; spinMultiplicity = 2 elif spinMultiplicity == 3: radicalElectrons = 2; spinMultiplicity = 3 elif spinMultiplicity == 4: radicalElectrons = 3; spinMultiplicity = 4 elif spinMultiplicity == 5: radicalElectrons = 4; spinMultiplicity = 5 # Process charge charge = obatom.GetFormalCharge() atom = Atom(element, radicalElectrons, spinMultiplicity, charge) self.vertices.append(atom) # Add bonds by iterating again through atoms for j in range(0, i): obatom2 = obmol.GetAtom(j + 1) obbond = obatom.GetBond(obatom2) if obbond is not None: order = 0 # Process bond type if obbond.IsSingle(): order = 'S' elif obbond.IsDouble(): order = 'D' elif obbond.IsTriple(): order = 'T' elif obbond.IsAromatic(): order = 'B' bond = Bond(self.vertices[i], self.vertices[j], order) self.addBond(bond) # Set atom types and connectivity values self.updateConnectivityValues() self.updateAtomTypes() return self
def fromOBMol(self, obmol, implicitH=False): """ Convert an OpenBabel OBMol object `obmol` to a molecular structure. Uses `OpenBabel <http://openbabel.org/>`_ to perform the conversion. """ self.vertices = [] self.edges = {} # Add hydrogen atoms to complete molecule if needed obmol.AddHydrogens() # Iterate through atoms in obmol for i in range(0, obmol.NumAtoms()): obatom = obmol.GetAtom(i + 1) # Use atomic number as key for element number = obatom.GetAtomicNum() element = elements.getElement(number=number) # Process spin multiplicity radicalElectrons = 0 spinMultiplicity = obatom.GetSpinMultiplicity() if spinMultiplicity == 0: radicalElectrons = 0 spinMultiplicity = 1 elif spinMultiplicity == 1: radicalElectrons = 2 spinMultiplicity = 1 elif spinMultiplicity == 2: radicalElectrons = 1 spinMultiplicity = 2 elif spinMultiplicity == 3: radicalElectrons = 2 spinMultiplicity = 3 # Process charge charge = obatom.GetFormalCharge() coord = np.array([obatom.x(), obatom.y(), obatom.z()]) label = obatom.GetIdx() atom = Atom(element, coord, radicalElectrons, spinMultiplicity, 0, charge, str(label)) self.vertices.append(atom) self.edges[atom] = {} # Add bonds by iterating again through atoms for j in range(0, i): obatom2 = obmol.GetAtom(j + 1) obbond = obatom.GetBond(obatom2) if obbond is not None: order = 0 # Process bond type if obbond.IsSingle(): order = 'S' elif obbond.IsDouble(): order = 'D' elif obbond.IsTriple(): order = 'T' elif obbond.IsAromatic(): order = 'B' bond = Bond(order) atom1 = self.vertices[i] atom2 = self.vertices[j] self.edges[atom1][atom2] = bond self.edges[atom2][atom1] = bond # Make hydrogens implicit to conserve memory if implicitH: self.makeHydrogensImplicit() return self