def build2LevelsTree(self, atomlines): """ Function to build a two level tree. """ print 'try to build a 2 level tree' self.mol = Molecule() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename( os.path.splitext(self.filename)[0]) self.mol.children = AtomSet([]) self.mol.childrenName = 'atoms' self.mol.childrenSetClass = AtomSet self.mol.elementType = Atom self.mol.levels = [Molecule, Atom] ##1/18:self.mol.levels = [Protein, Atom] for atmline in atomlines: atom = Atom(atmline[1], self.mol, chemicalElement=string.split(atmline[5], '.')[0], top=self.mol) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [[ float(atmline[2]), float(atmline[3]), float(atmline[4]) ]] if len(atmline) >= 9: atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = 'mol2' # atom.conformation = 0 atom.hetatm = 0 #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) self.mol.atoms = self.mol.children
def build2LevelsTree (self, atomlines): """ Function to build a two level tree. """ print 'try to build a 2 level tree' self.mol= Molecule() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename(os.path.splitext (self.filename)[0]) self.mol.children = AtomSet([]) self.mol.childrenName = 'atoms' self.mol.childrenSetClass = AtomSet self.mol.elementType = Atom self.mol.levels = [Molecule, Atom] ##1/18:self.mol.levels = [Protein, Atom] for atmline in atomlines: atom = Atom(atmline[1], self.mol, chemicalElement = string.split(atmline[5], '.')[0], top = self.mol) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [ [float(atmline[2]), float(atmline[3]), float(atmline[4]) ] ] if len(atmline)>=9: atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = 'mol2' # atom.conformation = 0 atom.hetatm = 0 #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) self.mol.atoms = self.mol.children
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 parse_PDB_ATOM_record(self, rec): """Parse PDB ATOM records using the pdb columns specifications""" self.atomCounter = self.atomCounter + 1 # not sure about altLoc if self.specType=='i': rec = string.split(rec) # Handle the alternate location using a flag. altLoc = self.get_Field_Value(rec, 'altLoc') if altLoc!= ' ': self.altLoc = altLoc else: self.altLoc = '' # changed from None to '' # check for chains break #self.modlflag = modlflag #chainID = rec[21]+ modlflag hascid = 1 chainID = self.get_Field_Value(rec, 'chainID') if not chainID: hascid = 0 chainID = str(self.chaincounter) ## should be unk??? if chainID != self.mol.curChain.id : # has to check if the chain exists already or not !!! if not self.mol.chains.id or not chainID in self.mol.chains.id or \ hascid==0: self.chaincounter = self.chaincounter + 1 if hascid==0: chainID = str(self.chaincounter) self.mol.curChain = Chain(chainID, self.mol, top=self.mol) self.residueCounter = 0 else: self.mol.curChain = self.mol.chains.get(chainID)[0] # check for residue break resName = self.get_Field_Value(rec, 'resName') resSeq = string.strip(self.get_Field_Value(rec, 'resSeq')) #WARNING reSeq is a STRING noresSeq = 0 if not resSeq and resName==self.mol.curRes.type and resName!='HOH': noresSeq = 1 resSeq = self.mol.curRes.number if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = string.strip(resName) + string.strip(resSeq) res = self.mol.curChain.get( na ) if res: self.mol.curRes = res[0] else: self.residueCounter = self.residueCounter + 1 if resName=='HOH': self.HOHCounter = self.HOHCounter + 1 if not resSeq: if resName=='HOH': resSeq = self.HOHCounter else: resSeq = self.residueCounter ## FIXME icodes are ignored self.mol.curRes = Residue(resName, resSeq, '', self.mol.curChain, top=self.mol) icode = self.get_Field_Value(rec, 'iCode') if not icode: pass elif icode != ' ': self.mol.curRes.icode = icode # parse atom info # handle atom names (calcium, hydrogen) and find element type # check validity of chemical element column and charge column ## only works if 'name' is in the pdb format! FIX! n = self.get_Field_Value(rec, 'name') el = self.get_Field_Value(rec, 'element') if n: name, element = self.getPDBAtomName(n, el) # if there is not resSeq spec, use first N to figure out new res if noresSeq and name=='N': At = self.mol.curRes.get('N') if At: self.residueCounter = self.residueCounter + 1 resSeq = self.residueCounter self.mol.curRes = Residue(resName, resSeq, self.mol.curChain, top=self.mol) atom = Atom(name, self.mol.curRes, element, top=self.mol) else: element = el if element: atom = Atom(parent = self.mol.curRes, chemicalElement = element, top=self.mol) else: atom = Atom(parent = self.mol.curRes, top=self.mol) ## elem = string.lower(element) # moved to getPDBAtomName ## if elem =='lp' or elem =='ld': ## element = 'Xx' atom.charge = self.get_Field_Value(rec, 'charge') #should have atom.charge if no charge? # coords are required; where to set default or check? xcoord = self.get_Field_Value(rec, 'x') ycoord = self.get_Field_Value(rec, 'y') zcoord = self.get_Field_Value(rec, 'z') assert xcoord and ycoord and zcoord atom._coords = [ [ float(xcoord), float(ycoord), float(zcoord) ] ] atom.segID = string.strip(self.get_Field_Value(rec, 'segID')) if rec[:4]=='ATOM' or rec[0]=='ATOM': atom.hetatm = 0 else: atom.hetatm = 1 #atom.alternate = [] atom.element = element num = self.get_Field_Value(rec, 'serial') if num: atom.number = int(num) else: atom.number = self.atomCounter occupancy = self.get_Field_Value(rec, 'occupancy') if occupancy: atom.occupancy = float(occupancy) # must check that it is a number atom.conformation = 0 tempFactor = self.get_Field_Value(rec, 'tempFactor') if tempFactor: atom.temperatureFactor = float(tempFactor) # add in user defined fields to atom attributes for field_name in self.recSpecs.UserFieldsDict.keys(): value = self.get_Field_Value(rec, field_name) type = self.recSpecs.get(field_name, 'var_type') if value: if type=='int': atom.__setattr__(field_name, int(value)) elif type=='float': atom.__setattr__(field_name, float(value)) else: atom.__setattr__(field_name, value) else: atom.__setattr__(field_name, value) if self.altLoc : # check if the name of the atom is the same than the #name of the previous atom . name = name + '@'+self.altLoc atom.name = name if len(self.mol.curRes.atoms)>1: # the new atom has been add to the current residue # You have to go to the one before. lastAtom = self.mol.curRes.atoms[-2] altname = string.split(lastAtom.name, '@')[0] if string.split(name, '@')[0] == altname: # Add the new alternate atom to the LastAtom.alternate and # add the lastAtom to the atom.alternate. lastAtom.alternate.append(atom) atom.alternate.append(lastAtom) for l in lastAtom.alternate: if atom.name != l.name: atom.alternate.append(l) l.alternate.append(atom) return atom
def build4LevelsTree(self, subst_chain, atomlines): """ Function to build a 4 level hierarchy Protein-Chain-Residue-Atom. """ self.mol = Protein() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename( os.path.splitext(self.filename)[0]) self.mol.curChain = Chain() self.mol.curRes = Residue() self.mol.levels = [Protein, Chain, Residue, Atom] i = 1 for atmline in atomlines: if len(atmline) >= 10: status = string.split(atmline[9], '|') else: status = None if len(atmline) == 8: tmp = [atmline[5][:5], atmline[5][5:]] atmline[5] = tmp[0] atmline.insert(6, tmp[1]) if status and status[0] == 'WATER': chainID = 'W' atmline[7] = 'HOH' + str(i) subst_chain[atmline[7]] = chainID i = i + 1 if subst_chain == {}: chainID = 'default' elif not subst_chain.has_key(atmline[7]): if subst_chain.has_key('****'): try: chainID = subst_chain[atmline[7]] except: chainID = 'default' else: chainID = 'default' elif type(subst_chain[atmline[7]]) is types.StringType: # that is to say that only chains has this substructure name. chainID = subst_chain[atmline[7]] elif type(subst_chain[atmline[7]]) is types.ListType: # That is to say that several chains have the same substructure. chainID = subst_chain[atmline[7]][0] subst_chain[atmline[7]] = subst_chain[atmline[7]].remove( chainID) if chainID != self.mol.curChain.id: if not self.mol.chains.id or not chainID in self.mol.chains.id: self.mol.curChain = Chain(chainID, self.mol, top=self.mol) else: self.mol.curChain = self.mol.chains.get(chainID)[0] if len(atmline) < 7: # test if the atmline has a res name and resseq: resName = 'RES' resSeq = '1' else: resName = atmline[7][:3] resSeq = atmline[7][3:] if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = string.strip(resName) + string.strip(resSeq) res = self.mol.curChain.get(na) if res: self.mol.curRes = res[0] else: self.mol.curRes = Residue(resName, resSeq, '', self.mol.curChain, top=self.mol) name = atmline[1] if name == 'CA': self.mol.curRes.hasCA = 1 if name == 'O': self.mol.curRes.hasO = 2 atom = Atom(name, self.mol.curRes, top=self.mol, chemicalElement=string.split(atmline[5], '.')[0]) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [[ float(atmline[2]), float(atmline[3]), float(atmline[4]) ]] if len(atmline) >= 9: atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = 'mol2' # atom.conformation = 0 atom.hetatm = 0 #Add a data member containing a list of string describing # the Sybyl status bis of the atoms. atom.status = status #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) delattr(self.mol, 'curRes') delattr(self.mol, 'curChain')
def build3LevelsTree(self, atomlines): """ Function to build a 3 levels hierarchy Molecule-substructure-atoms.""" self.mol = Protein() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename( os.path.splitext(self.filename)[0]) self.mol.children = ResidueSet([]) self.mol.childrenName = 'residues' self.mol.childrenSetClass = ResidueSet self.mol.elementType = Residue self.mol.curRes = Residue() self.mol.curRes.hasCA = 0 self.mol.curRes.hasO = 0 self.mol.levels = [Protein, Residue, Atom] for atmline in atomlines: if len(atmline) >= 10: status = string.split(atmline[9], '|') else: status = None resName = atmline[7][:3] resSeq = atmline[7][3:] if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = string.strip(resName) + string.strip(resSeq) res = self.mol.get(na) if res: self.mol.curRes = res[0] else: self.mol.curRes = Residue(resName, resSeq, '', self.mol, top=self.mol) name = atmline[1] if name == 'CA': self.mol.curRes.hasCA = 1 if name == 'O': self.mol.curRes.hasO = 2 atom = Atom(name, self.mol.curRes, top=self.mol, chemicalElement=string.split(atmline[5], '.')[0]) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [[ float(atmline[2]), float(atmline[3]), float(atmline[4]) ]] atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = mol2 # atom.conformation = 0 atom.hetatm = 0 #Add a data member containing a list of string describing # the Sybyl status bis of the atoms. atom.status = status #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) self.mol.residues = self.mol.children assert hasattr(self.mol, 'chains') delattr(self.mol, 'chains') delattr(self.mol, 'curRes')
def build4LevelsTree(self, subst_chain, atomlines): """ Function to build a 4 level hierarchy Protein-Chain-Residue-Atom. """ self.mol= Protein() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename(os.path.splitext (self.filename)[0]) self.mol.curChain = Chain() self.mol.curRes = Residue() self.mol.levels = [Protein, Chain, Residue, Atom] i = 1 for atmline in atomlines: if len(atmline)>= 10: status = string.split(atmline[9], '|') else: status = None if len(atmline) == 8: tmp = [atmline[5][:5], atmline[5][5:]] atmline[5] = tmp[0] atmline.insert(6, tmp[1]) if status and status[0]=='WATER': chainID = 'W' atmline[7] = 'HOH'+str(i) subst_chain[atmline[7]] = chainID i = i+1 if subst_chain == {}: chainID = 'default' elif not subst_chain.has_key(atmline[7]): if subst_chain.has_key('****'): try: chainID = subst_chain[atmline[7]] except: chainID = 'default' else: chainID = 'default' elif type(subst_chain[atmline[7]]) is types.StringType: # that is to say that only chains has this substructure name. chainID = subst_chain[atmline[7]] elif type(subst_chain[atmline[7]]) is types.ListType: # That is to say that several chains have the same substructure. chainID = subst_chain[atmline[7]][0] subst_chain[atmline[7]] = subst_chain[atmline[7]].remove(chainID) if chainID != self.mol.curChain.id: if not self.mol.chains.id or not chainID in self.mol.chains.id: self.mol.curChain = Chain(chainID, self.mol, top = self.mol) else: self.mol.curChain = self.mol.chains.get(chainID)[0] if len(atmline)<7: # test if the atmline has a res name and resseq: resName = 'RES' resSeq = '1' else: resName = atmline[7][:3] resSeq = atmline[7][3:] if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = string.strip(resName) + string.strip(resSeq) res = self.mol.curChain.get( na ) if res: self.mol.curRes = res[0] else: self.mol.curRes = Residue(resName, resSeq, '', self.mol.curChain, top = self.mol) name = atmline[1] if name == 'CA': self.mol.curRes.hasCA = 1 if name == 'O' : self.mol.curRes.hasO = 2 atom = Atom(name, self.mol.curRes, top = self.mol, chemicalElement = string.split(atmline[5], '.')[0]) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [ [float(atmline[2]), float(atmline[3]), float(atmline[4]) ] ] if len(atmline)>=9: atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = 'mol2' # atom.conformation = 0 atom.hetatm = 0 #Add a data member containing a list of string describing # the Sybyl status bis of the atoms. atom.status = status #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) delattr(self.mol, 'curRes') delattr(self.mol, 'curChain')
def build3LevelsTree(self,atomlines): """ Function to build a 3 levels hierarchy Molecule-substructure-atoms.""" self.mol= Protein() self.mol.allAtoms = AtomSet() self.mol.atmNum = {} self.mol.parser = self if self.mol.name == 'NoName': self.mol.name = os.path.basename(os.path.splitext (self.filename)[0]) self.mol.children = ResidueSet([]) self.mol.childrenName = 'residues' self.mol.childrenSetClass = ResidueSet self.mol.elementType = Residue self.mol.curRes = Residue() self.mol.curRes.hasCA = 0 self.mol.curRes.hasO = 0 self.mol.levels = [Protein, Residue, Atom] for atmline in atomlines: if len(atmline)>= 10: status = string.split(atmline[9], '|') else: status = None resName = atmline[7][:3] resSeq = atmline[7][3:] if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = string.strip(resName) + string.strip(resSeq) res = self.mol.get(na) if res: self.mol.curRes = res[0] else: self.mol.curRes = Residue(resName, resSeq, '', self.mol, top = self.mol) name = atmline[1] if name == 'CA': self.mol.curRes.hasCA = 1 if name == 'O' : self.mol.curRes.hasO = 2 atom = Atom(name, self.mol.curRes, top = self.mol, chemicalElement = string.split(atmline[5], '.')[0]) #atom.element = atmline[5][0] atom.element = atom.chemElem atom.number = int(atmline[0]) self.mol.atmNum[atom.number] = atom atom._coords = [ [float(atmline[2]), float(atmline[3]), float(atmline[4]) ] ] atom._charges['mol2'] = float(atmline[8]) atom.chargeSet = mol2 # atom.conformation = 0 atom.hetatm = 0 #Add a data member containing a list of string describing # the Sybyl status bis of the atoms. atom.status = status #add altname so buildBondsByDist doesn't croak atom.altname = None self.mol.allAtoms.append(atom) self.mol.residues = self.mol.children assert hasattr(self.mol, 'chains') delattr(self.mol, 'chains') delattr(self.mol, 'curRes')
def parse_PDB_ATOM_record(self, rec): """Parse PDB ATOM records using the pdb columns specifications""" self.atomCounter = self.atomCounter + 1 # not sure about altLoc if self.specType == 'i': rec = rec.split() # Handle the alternate location using a flag. altLoc = self.get_Field_Value(rec, 'altLoc') if altLoc != ' ': self.altLoc = altLoc else: self.altLoc = '' # changed from None to '' # check for chains break # self.modlflag = modlflag # chainID = rec[21]+ modlflag hascid = 1 chainID = self.get_Field_Value(rec, 'chainID') if not chainID: hascid = 0 chainID = str(self.chaincounter) ## should be unk??? if chainID != self.mol.curChain.id: # has to check if the chain exists already or not !!! if not self.mol.chains.id or chainID not in self.mol.chains.id or hascid == 0: self.chaincounter = self.chaincounter + 1 if hascid == 0: chainID = str(self.chaincounter) self.mol.curChain = Chain(chainID, self.mol, top=self.mol) self.residueCounter = 0 else: self.mol.curChain = self.mol.chains.get(chainID)[0] # check for residue break resName = self.get_Field_Value(rec, 'resName') resSeq = self.get_Field_Value(rec, 'resSeq').strip() # WARNING reSeq is a STRING noresSeq = 0 if not resSeq and resName == self.mol.curRes.type and resName != 'HOH': noresSeq = 1 resSeq = self.mol.curRes.number if resSeq != self.mol.curRes.number or \ resName != self.mol.curRes.type: # check if this residue already exists na = resName.strip() + resSeq.strip() res = self.mol.curChain.get(na) if res: self.mol.curRes = res[0] else: self.residueCounter = self.residueCounter + 1 if resName == 'HOH': self.HOHCounter = self.HOHCounter + 1 if not resSeq: if resName == 'HOH': resSeq = self.HOHCounter else: resSeq = self.residueCounter ## FIXME icodes are ignored self.mol.curRes = Residue(resName, resSeq, '', self.mol.curChain, top=self.mol) icode = self.get_Field_Value(rec, 'iCode') if not icode: pass elif icode != ' ': self.mol.curRes.icode = icode # parse atom info # handle atom names (calcium, hydrogen) and find element type # check validity of chemical element column and charge column ## only works if 'name' is in the pdb format! FIX! n = self.get_Field_Value(rec, 'name') el = self.get_Field_Value(rec, 'element') if n: name, element = self.getPDBAtomName(n, el) # if there is not resSeq spec, use first N to figure out new res if noresSeq and name == 'N': At = self.mol.curRes.get('N') if At: self.residueCounter = self.residueCounter + 1 resSeq = self.residueCounter self.mol.curRes = Residue(resName, resSeq, self.mol.curChain, top=self.mol) atom = Atom(name, self.mol.curRes, element, top=self.mol) else: element = el if element: atom = Atom(parent=self.mol.curRes, chemicalElement=element, top=self.mol) else: atom = Atom(parent=self.mol.curRes, top=self.mol) ## elem = string.lower(element) # moved to getPDBAtomName ## if elem =='lp' or elem =='ld': ## element = 'Xx' atom.charge = self.get_Field_Value(rec, 'charge') # should have atom.charge if no charge? # coords are required; where to set default or check? xcoord = self.get_Field_Value(rec, 'x') ycoord = self.get_Field_Value(rec, 'y') zcoord = self.get_Field_Value(rec, 'z') assert xcoord and ycoord and zcoord atom._coords = [[float(xcoord), float(ycoord), float(zcoord)]] atom.segID = self.get_Field_Value(rec, 'segID').strip() if rec[:4] == 'ATOM' or rec[0] == 'ATOM': atom.hetatm = 0 else: atom.hetatm = 1 # atom.alternate = [] atom.element = element num = self.get_Field_Value(rec, 'serial') if num: atom.number = int(num) else: atom.number = self.atomCounter occupancy = self.get_Field_Value(rec, 'occupancy') if occupancy: atom.occupancy = float(occupancy) # must check that it is a number atom.conformation = 0 tempFactor = self.get_Field_Value(rec, 'tempFactor') if tempFactor: atom.temperatureFactor = float(tempFactor) # add in user defined fields to atom attributes for field_name in list(self.recSpecs.UserFieldsDict.keys()): value = self.get_Field_Value(rec, field_name) type = self.recSpecs.get(field_name, 'var_type') if value: if type == 'int': atom.__setattr__(field_name, int(value)) elif type == 'float': atom.__setattr__(field_name, float(value)) else: atom.__setattr__(field_name, value) else: atom.__setattr__(field_name, value) if self.altLoc: # check if the name of the atom is the same than the # name of the previous atom . name = name + '@' + self.altLoc atom.name = name if len(self.mol.curRes.atoms) > 1: # the new atom has been add to the current residue # You have to go to the one before. lastAtom = self.mol.curRes.atoms[-2] altname = lastAtom.name.split('@')[0] if name.split('@')[0] == altname: # Add the new alternate atom to the LastAtom.alternate and # add the lastAtom to the atom.alternate. lastAtom.alternate.append(atom) atom.alternate.append(lastAtom) for l in lastAtom.alternate: if atom.name != l.name: atom.alternate.append(l) l.alternate.append(atom) return atom
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 )