def addHydrogens(self, mol): #check for bonds if len(mol.allAtoms.bonds[0]) == 0: mol.buildBondsByDistance() bonds = mol.allAtoms.bonds[0] #could have preset babel_types #so check if allAtoms are already typed try: t = mol.allAtoms.babel_type except: #if all are not pretyped, type them babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) if self.method == 'withBondOrder': mol.rings = RingFinder() mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0]) mol.rings.bondRings = {} for ind in xrange(len(mol.rings.rings)): r = mol.rings.rings[ind] for b in r['bonds']: if not mol.rings.bondRings.has_key(b): mol.rings.bondRings[b] = [ ind, ] else: mol.rings.bondRings[b].append(ind) bo = BondOrder() bo.assignBondOrder(mol.allAtoms, bonds, mol.rings) mol.allAtoms._bndtyped = 1 # do aromatic here arom = Aromatic(mol.rings) arom.find_aromatic_atoms(mol.allAtoms) hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method) bondedAtomDict = {} # key is heavy atom for a in hat: if bondedAtomDict.has_key(a[1]): bondedAtomDict[a[1]].append(a) else: bondedAtomDict[a[1]] = [a] # now create Atom object for hydrogens # and add the to the residues's atom list molNewHs = AtomSet([]) # list of created H atoms for this molecule heavyAtoms = AtomSet([]) # list of atoms that need new radii for heavyAtom, HatmsDscr in bondedAtomDict.items(): #don't add hydrogens to carbons: polar Only!!! if self.htype != 'all' and heavyAtom.element == 'C': continue res = heavyAtom.parent # find where to insert H atom childIndex = res.children.index(heavyAtom) + 1 # loop over H atoms description to be added # start at the end to number correctly l = len(HatmsDscr) for i in range(l - 1, -1, -1): a = HatmsDscr[i] # build H atom's name if len(heavyAtom.name) == 1: name = 'H' + heavyAtom.name else: name = 'H' + heavyAtom.name[1:] # if more than 1 H atom, add H atom index # for instance HD11, HD12, Hd13 (index is 1,2,3) if l > 1: name = name + str(i + 1) # create the H atom object atom = Atom(name, res, top=heavyAtom.top, chemicalElement='H', childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [a[0]] if hasattr(a[1], 'segID'): atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] #atom.element = 'H' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic = 1 atom.radius = 1.2 # create the Bond object bonding Hatom to heavyAtom bond = Bond(a[1], atom, bondOrder=1) # add the created atom the the list molNewHs.append(atom) # in case this new hydrogen atom ever ends up in pmv # HAVE TO CREATE THESE ENTRIES # create the color entries for all geoemtries # available for the heavyAtom for key, value in heavyAtom.colors.items(): atom.colors[key] = (0.0, 1.0, 1.0) atom.opacities[key] = 1.0 mol.allAtoms = mol.chains.residues.atoms if self.renumber: mol.allAtoms.number = range(1, len(mol.allAtoms) + 1) return len(molNewHs)
def add_oxt(self, catom): if catom.element != 'C': return mol = catom.top ##check for bonds #if len(mol.allAtoms.bonds[0])==0: # mol.buildBondsByDistance() #check whether residue already has OXT res = catom.parent if 'OXT' in res.atoms.name: print('not adding OXT to ', res.full_name(), '\n', 'it already has an OXT atom') return #check whether catom has a hydrogen to delete hatoms = catom.parent.atoms.get(lambda x: x.name == 'HC') if len(hatoms): hatom = hatoms[0] #check for hbonds if hasattr(hatom, 'hbonds'): #remove hbonds for b in hatom.hbonds: atList = [b.donAt, b.accAt] if b.hAt is not None: atList.append(b.hAt) for at in atList: #hbonds might already be gone if not hasattr(at, 'hbonds'): continue okhbnds = [] for hb in at.hbonds: if hb != b: okhbnds.append(hb) if len(okhbnds): at.hbonds = okhbnds else: delattr(at, 'hbonds') #remove covalent bonds for b in hatom.bonds: at2 = b.atom1 if at2 == hatom: at2 = b.atom2 at2.bonds.remove(b) hatom.parent.remove(hatom, cleanup=1) #have to type atoms before call to add_sp2_hydrogen: if not hasattr(catom, 'babel_type'): print('catom has no babel_type: calling typeAtoms') #self.warningMsg(msg) #typeAtoms does whole molecule babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) #NB: bond_length 1.28 measured from OXT-C bond in 1crn tup1 = self.addh.add_sp2_hydrogen(catom, 1.28) res = catom.parent # find where to insert H atom childIndex = res.children.index(catom) + 1 name = 'OXT' # create the OXT atom object atom = Atom(name, res, top=mol, childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [tup1[0][0]] if hasattr(catom, 'segID'): atom.segID = catom.segID atom.hetatm = 0 atom.alternate = [] atom.element = 'O' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = 8 atom.babel_type = 'O-' atom.babel_organic = 1 # create the Bond object bonding Hatom to heavyAtom bond = Bond(catom, atom, bondOrder=2) # create the color entries for all geometries # available for the other oxygen atom attached to 'C' oatom = res.atoms.get(lambda x: x.name == 'O')[0] if oatom is not None: for key, value in list(oatom.colors.items()): atom.colors[key] = value #atom.opacities[key] = oatom.opacities[key] # update the allAtoms set in the molecule mol.allAtoms = mol.chains.residues.atoms # update numbers of allAtoms fst = mol.allAtoms[0].number mol.allAtoms.number = list(range(fst, len(mol.allAtoms) + fst)) # update _uniqIndex of this residues atoms res.assignUniqIndex() #return AtomSet([atom]) return atom
print "done" ## db = filter(lambda x:x.bondOrder==2, bonds) ## for b in db: ## print b addh = AddHydrogens() #pdb.run("hat = addh.addHydrogens(allAtoms)") hat = addh.addHydrogens(allAtoms) from MolKit.molecule import Atom, Bond for a in hat: atom = Atom('H', a[1].parent, top=a[1].top) atom._coords = [a[0]] atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] atom.element = 'H' atom.number = -1 atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic = 1 bond = Bond(a[1], atom) from Pmv.moleculeViewer import MoleculeViewer mv = MoleculeViewer() mv.addMolecule(mol) mv.lines(mol)
def addHydrogens(self, mol): #check for bonds if len(mol.allAtoms.bonds[0])==0: mol.buildBondsByDistance() bonds = mol.allAtoms.bonds[0] #could have preset babel_types #so check if allAtoms are already typed try: t = mol.allAtoms.babel_type except: #if all are not pretyped, type them babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) if self.method=='withBondOrder': mol.rings = RingFinder() mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0]) mol.rings.bondRings = {} for ind in xrange(len(mol.rings.rings)): r = mol.rings.rings[ind] for b in r['bonds']: if not mol.rings.bondRings.has_key(b): mol.rings.bondRings[b] = [ind,] else: mol.rings.bondRings[b].append(ind) bo = BondOrder() bo.assignBondOrder(mol.allAtoms, bonds, mol.rings) mol.allAtoms._bndtyped = 1 # do aromatic here arom = Aromatic(mol.rings) arom.find_aromatic_atoms(mol.allAtoms) hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method) bondedAtomDict = {} # key is heavy atom for a in hat: if bondedAtomDict.has_key(a[1]): bondedAtomDict[a[1]].append(a) else: bondedAtomDict[a[1]] = [a] # now create Atom object for hydrogens # and add the to the residues's atom list molNewHs = AtomSet([]) # list of created H atoms for this molecule heavyAtoms = AtomSet([]) # list of atoms that need new radii for heavyAtom, HatmsDscr in bondedAtomDict.items(): #don't add hydrogens to carbons: polar Only!!! if self.htype!='all' and heavyAtom.element=='C': continue res = heavyAtom.parent # find where to insert H atom childIndex = res.children.index(heavyAtom)+1 # loop over H atoms description to be added # start at the end to number correctly l = len(HatmsDscr) for i in range(l-1,-1,-1): a = HatmsDscr[i] # build H atom's name if len(heavyAtom.name)==1: name = 'H' + heavyAtom.name else: name = 'H' + heavyAtom.name[1:] # if more than 1 H atom, add H atom index # for instance HD11, HD12, Hd13 (index is 1,2,3) if l > 1: name = name + str(i+1) # create the H atom object atom = Atom(name, res, top=heavyAtom.top, chemicalElement='H', childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [ a[0] ] if hasattr(a[1], 'segID'): atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] #atom.element = 'H' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic = 1 atom.radius = 1.2 # create the Bond object bonding Hatom to heavyAtom bond = Bond( a[1], atom, bondOrder=1) # add the created atom the the list molNewHs.append(atom) # in case this new hydrogen atom ever ends up in pmv # HAVE TO CREATE THESE ENTRIES # create the color entries for all geoemtries # available for the heavyAtom for key, value in heavyAtom.colors.items(): atom.colors[key]=(0.0, 1.0, 1.0) atom.opacities[key]=1.0 mol.allAtoms = mol.chains.residues.atoms if self.renumber: mol.allAtoms.number = range(1, len(mol.allAtoms)+1) return len(molNewHs)
def add_oxt(self, catom): if catom.element!='C': return mol = catom.top ##check for bonds #if len(mol.allAtoms.bonds[0])==0: # mol.buildBondsByDistance() #check whether residue already has OXT res = catom.parent if 'OXT' in res.atoms.name: print 'not adding OXT to ',res.full_name(),'\n', 'it already has an OXT atom' return #check whether catom has a hydrogen to delete hatoms = catom.parent.atoms.get(lambda x: x.name=='HC') if len(hatoms): hatom = hatoms[0] #check for hbonds if hasattr(hatom, 'hbonds'): #remove hbonds for b in hatom.hbonds: atList = [b.donAt, b.accAt] if b.hAt is not None: atList.append(b.hAt) for at in atList: #hbonds might already be gone if not hasattr(at, 'hbonds'): continue okhbnds = [] for hb in at.hbonds: if hb!=b: okhbnds.append(hb) if len(okhbnds): at.hbonds = okhbnds else: delattr(at, 'hbonds') #remove covalent bonds for b in hatom.bonds: at2 = b.atom1 if at2 == hatom: at2 = b.atom2 at2.bonds.remove(b) hatom.parent.remove(hatom, cleanup=1) #have to type atoms before call to add_sp2_hydrogen: if not hasattr(catom,'babel_type'): print 'catom has no babel_type: calling typeAtoms' #self.warningMsg(msg) #typeAtoms does whole molecule babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) #NB: bond_length 1.28 measured from OXT-C bond in 1crn tup1 = self.addh.add_sp2_hydrogen(catom, 1.28) res = catom.parent # find where to insert H atom childIndex = res.children.index(catom)+1 name = 'OXT' # create the OXT atom object atom = Atom(name, res, top=mol, childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [ tup1[0][0] ] if hasattr(catom, 'segID'): atom.segID = catom.segID atom.hetatm = 0 atom.alternate = [] atom.element = 'O' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = 8 atom.babel_type = 'O-' atom.babel_organic = 1 # create the Bond object bonding Hatom to heavyAtom bond = Bond( catom, atom, bondOrder=2) # create the color entries for all geometries # available for the other oxygen atom attached to 'C' oatom = res.atoms.get(lambda x: x.name=='O')[0] if oatom is not None: for key, value in oatom.colors.items(): atom.colors[key] = value #atom.opacities[key] = oatom.opacities[key] # update the allAtoms set in the molecule mol.allAtoms = mol.chains.residues.atoms # update numbers of allAtoms fst = mol.allAtoms[0].number mol.allAtoms.number = range(fst, len(mol.allAtoms)+fst) # update _uniqIndex of this residues atoms res.assignUniqIndex() #return AtomSet([atom]) return atom
print "done" ## db = filter(lambda x:x.bondOrder==2, bonds) ## for b in db: ## print b addh = AddHydrogens() #pdb.run("hat = addh.addHydrogens(allAtoms)") hat = addh.addHydrogens(allAtoms) from MolKit.molecule import Atom, Bond for a in hat: atom = Atom('H', a[1].parent, top=a[1].top) atom._coords = [ a[0] ] atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] atom.element = 'H' atom.number = -1 atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic=1 bond = Bond( a[1], atom ) from Pmv.moleculeViewer import MoleculeViewer mv = MoleculeViewer() mv.addMolecule(mol) mv.lines( mol )