def validGeometry_DHAB(self, tags1, tags2, reslines1, reslines2): ## implement in subclasses ## D and H coordinates from reslines1 Dcrd, Hcrds = self.DHcrd(reslines1, tags1) ## A and B coordinates from reslines2 Acrd, Bcrd, Aatomname, Batomname = None, None, None, None for tat in tags2: if tat.tag == 'A': Aatomname = tat.atn if tat.tag == 'B': Batomname = tat.atn for rl in reslines2: if Aatomname == line2atomname(rl): Acrd = line2crd(rl) if Batomname == line2atomname(rl): Bcrd = line2crd(rl) ## limit checks tag2points = {} tag2points['D'] = Dcrd tag2points['A'] = Acrd tag2points['B'] = Bcrd if len(Hcrds) == 0: Hcrds = [ (1e6, 1e6, 1e6) ] ## probably hydrogens have no role in this intxn, thats why they were not discovered for hcrd in Hcrds: tag2points['H'] = hcrd all_lims_satisfied = 1 for l in self.lims: #print tag2points if not l.check(tag2points): all_lims_satisfied = 0 break if all_lims_satisfied == 1: return pymol_line(Dcrd, Acrd) return None
def validGeometry_DHAB(self, tags1, tags2, reslines1, reslines2) : ## implement in subclasses ## D and H coordinates from reslines1 Dcrd, Hcrds = self.DHcrd(reslines1, tags1) ## A and B coordinates from reslines2 Acrd, Bcrd, Aatomname, Batomname = None, None, None, None for tat in tags2 : if tat.tag == 'A' : Aatomname = tat.atn if tat.tag == 'B' : Batomname = tat.atn for rl in reslines2 : if Aatomname == line2atomname(rl) : Acrd = line2crd(rl) if Batomname == line2atomname(rl) : Bcrd = line2crd(rl) ## limit checks tag2points = {} tag2points['D'] = Dcrd tag2points['A'] = Acrd tag2points['B'] = Bcrd if len(Hcrds) == 0 : Hcrds = [(1e6,1e6,1e6)] ## probably hydrogens have no role in this intxn, thats why they were not discovered for hcrd in Hcrds : tag2points['H'] = hcrd all_lims_satisfied = 1 for l in self.lims : #print tag2points if not l.check(tag2points) : all_lims_satisfied = 0 break if all_lims_satisfied == 1 : return pymol_line(Dcrd, Acrd) return None
def validGeometry_contact(self, tags1, tags2, reslines1, reslines2, atomnbrs): ## implement in subclasses contact = None for an1 in tags1: if contact: break for an2 in tags2: if contact: break if (an1.atn, an2.atn) in atomnbrs: contact = 1 if not contact: return None c1, c2, H1crd, H2crd = 0, 0, [ 0., 0., 0., ], [ 0., 0., 0., ] for rl in reslines1: for an in tags1: if line2atomname(rl) == an.atn: H1crd = vec_add(H1crd, line2crd(rl)) c1 = c1 + 1 for rl in reslines2: for an in tags2: if line2atomname(rl) == an.atn: H2crd = vec_add(H2crd, line2crd(rl)) c2 = c2 + 1 H1crd = vec_scale(H1crd, 1. / c1) H2crd = vec_scale(H2crd, 1. / c2) return pymol_line(H1crd, H2crd)
def DHcrd(self, reslines, tags) : Datomname = None for tat in tags : if tat.tag == 'D' : Datomname = tat.atn Hatomname = '.H' + Datomname[2:4] Dcrd, Hcrds = None, [] for rl in reslines : if re.compile(Hatomname).search(line2atomname(rl)) : Hcrds.append(line2crd(rl)) if Datomname == line2atomname(rl) : Dcrd = line2crd(rl) return Dcrd, Hcrds
def DHcrd(self, reslines, tags): Datomname = None for tat in tags: if tat.tag == 'D': Datomname = tat.atn Hatomname = '.H' + Datomname[2:4] Dcrd, Hcrds = None, [] for rl in reslines: if re.compile(Hatomname).search(line2atomname(rl)): Hcrds.append(line2crd(rl)) if Datomname == line2atomname(rl): Dcrd = line2crd(rl) return Dcrd, Hcrds
def fixCNSop(pdbfile) : from pdbr import isPdbAAline, isPdbAtomLine, line2atomname, line2resn lines = [] for l in open(pdbfile, 'r').readlines() : if not isPdbAAline(l) : lines.append(l) elif line2resn(l) == "ILE" and line2atomname(l) == " CD " : lines.append(re.sub(" CD ", " CD1", l)) elif line2atomname(l) == " OT1" : lines.append(re.sub(" OT1", " O ", l)) elif line2atomname(l) == " OXT" : continue else : lines.append(l) op = open(pdbfile, 'w') for l in lines : op.write(l) op.close()
def validPartners(self, reslines1, reslines2, atomnbrs, justOnePerPair=1): a1, a2 = [], [] res1, res2 = line2resid(reslines1[0]), line2resid(reslines2[0]) resn1, resn2 = line2resn(reslines1[0]), line2resn(reslines2[0]) print "Checking " + self.name + "-" + res1 + "-" + res2 + "-" for l in reslines1: assert line2resid(l) == res1 a1.append(line2atomname(l)) for l in reslines2: assert line2resid(l) == res2 a2.append(line2atomname(l)) ## does res1 qualify to be D-partner in this interaction ? tags1 = self.acceptableResidue(resn1, 'D', a1) if not tags1: print res1, 'doesnt qualify as D' ## does res2 qualify to be A-partner in this interaction ? tags2 = self.acceptableResidue(resn2, 'A', a2) if not tags2: print res2, 'doesnt qualify as A' if not tags1 or not tags2: return None lines = [] ## check if tags1, tags2 are voronoi nbrs for t1 in tags1: atn1 = [] for t in t1: atn1.append(t.atn) for t2 in tags2: atn2 = [] for t in t2: atn2.append(t.atn) vornbrs = None for an1, an2 in atomnbrs: if an1 in atn1 and an2 in atn2: vornbrs = 1 break if not vornbrs: print "Reqd atoms are not voronoi nbrs" continue ret = None if self.type == 'DHAB': ret = self.validGeometry_DHAB(t1, t2, reslines1, reslines2) elif self.type == 'DHR': ret = self.validGeometry_DHR(t1, t2, reslines1, reslines2) elif self.type == 'RR': ret = self.validGeometry_RR(t1, t2, reslines1, reslines2) elif self.type == 'contact': ret = self.validGeometry_contact(t1, t2, reslines1, reslines2, atomnbrs) else: assert 1 == 0 if ret: if justOnePerPair: return [ret] else: lines.append(ret) if len(lines) > 0: return lines return None
def validPartners(self, reslines1, reslines2, atomnbrs, justOnePerPair=1) : a1, a2 = [], [] res1, res2 = line2resid(reslines1[0]), line2resid(reslines2[0]) resn1, resn2 = line2resn(reslines1[0]), line2resn(reslines2[0]) print "Checking " + self.name + "-" + res1 + "-" + res2 + "-" for l in reslines1 : assert line2resid(l) == res1 a1.append(line2atomname(l)) for l in reslines2 : assert line2resid(l) == res2 a2.append(line2atomname(l)) ## does res1 qualify to be D-partner in this interaction ? tags1 = self.acceptableResidue(resn1, 'D', a1) if not tags1 : print res1, 'doesnt qualify as D' ## does res2 qualify to be A-partner in this interaction ? tags2 = self.acceptableResidue(resn2, 'A', a2) if not tags2 : print res2, 'doesnt qualify as A' if not tags1 or not tags2 : return None lines = [] ## check if tags1, tags2 are voronoi nbrs for t1 in tags1 : atn1 = [] for t in t1 : atn1.append(t.atn) for t2 in tags2 : atn2 = [] for t in t2 : atn2.append(t.atn) vornbrs = None for an1,an2 in atomnbrs : if an1 in atn1 and an2 in atn2 : vornbrs = 1 break if not vornbrs : print "Reqd atoms are not voronoi nbrs" continue ret = None if self.type == 'DHAB' : ret = self.validGeometry_DHAB(t1, t2, reslines1, reslines2) elif self.type == 'DHR' : ret = self.validGeometry_DHR(t1, t2, reslines1, reslines2) elif self.type == 'RR' : ret = self.validGeometry_RR(t1, t2, reslines1, reslines2) elif self.type == 'contact' : ret = self.validGeometry_contact(t1, t2, reslines1, reslines2, atomnbrs) else : assert 1==0 if ret : if justOnePerPair : return [ret] else : lines.append(ret) if len(lines) > 0 : return lines return None
def keepMClinesOnly2(pdbfn,pdbout) : from pdbr import isPdbAAline, isPdbAtomLine, line2atomname lines = [] for l in open(pdbfn, 'r').readlines() : if not isPdbAtomLine(l) : lines.append(l) elif isPdbAAline(l) and line2atomname(l) in [" N "," CA "," C "," O "," CB "] : lines.append(l) ofp = open(pdbout, 'w') for l in lines : ofp.write(l) ofp.close()
def fixCNSop(pdbfile) : from pdbr import isPdbAAline, isPdbAtomLine, line2atomname, line2resn, changeResn, changeAtomname lines = [] if (os.path.isfile(pdbfile)==False) : print "Cannot find file %s "%pdbfile print "No file in directory ", os.getcwd() sys.exit() for l in open(pdbfile, 'r').readlines() : if not isPdbAtomLine(l) : lines.append(l) elif line2resn(l) == "CU " : lines.append( changeResn(l, " CU") ) elif line2resn(l) == "MSE" and line2atomname(l) == " SE " : lines.append( changeAtomname(l, "SE ") ) elif line2resn(l) == "ILE" and line2atomname(l) == " CD " : lines.append(re.sub(" CD ", " CD1", l)) elif line2atomname(l) == " OT1" : lines.append(re.sub(" OT1", " O ", l)) elif line2atomname(l) == " OXT" : continue else : lines.append(l) op = open(pdbfile, 'w') for l in lines : op.write(l) op.close()
def changeBfacs(filename, bfacs) : from pdbr import isPdbAAline, line2atomname, line2occu, changeBfactorOccupancy lines = [] for l in open(filename, 'r').readlines() : if isPdbAAline(l) : if line2atomname(l) in [" N "," CA "," C "," O "] : l = changeBfactorOccupancy(l, bfacs[0], line2occu(l)) else : l = changeBfactorOccupancy(l, bfacs[1], line2occu(l)) lines.append(l) fp = open(filename, 'w') for l in lines : fp.write(l) fp.close()
def adjustBfac2(pdbfile, refpdb,nativeBfac, mcBfac, scBfac) : print "Copy Bfactors from", refpdb, "to", pdbfile from pdbr import line2bfac, isPdbAtomLine, line2bfac, line2atomid, changeBfactor, line2crdstr id2bo = {} # read in crd, bfac, for each atom-id if (os.path.isfile(refpdb)==False) : print "Cannot find file %s "%refpdb print "No file in directory ", os.getcwd() sys.exit() if (os.path.isfile(pdbfile)==False) : print "Cannot find file %s "%pdbfile print "No file in directory ", os.getcwd() sys.exit() for l in open(refpdb, 'r').readlines() : if not isPdbAtomLine(l) : continue if id2bo.has_key(line2atomid(l)) : continue id2bo[line2atomid(l)] = [line2bfac(l), line2crdstr(l)] newlines = [] if nativeBfac == 1: for l in open(pdbfile, 'r').readlines() : if not isPdbAtomLine(l) : newlines.append(l) ; continue bfac, crdstr = id2bo[ line2atomid(l) ] l = changeBfactor(l, bfac) newlines.append(l) else : for l in open(pdbfile, 'r').readlines() : if not isPdbAtomLine(l) : newlines.append(l) ; continue bfac, crdstr = id2bo[ line2atomid(l) ] if crdstr == line2crdstr(l) : l = changeBfactor(l, bfac) else : if line2atomname(l) in [' N ',' CA ',' C ',' O '] : l = changeBfactor(l, mcBfac) else : l = changeBfactor(l, scBfac) newlines.append(l) op = open(pdbfile, 'w') for l in newlines : op.write(l) op.close()
def pdbCopy(pdbfrom, pdbto) : from pdbr import isPdbAtomLine, line2atomid, isPdbAAline, line2atomname, changeBfactorOccupancy op = open(pdbto, 'w') idsSofar = [] for l in open(pdbfrom, 'r').readlines() : if not isPdbAtomLine(l) : op.write(l) ; continue if line2atomid(l) in idsSofar : continue idsSofar.append(line2atomid(l)) bfactor = 30 if isPdbAAline(l) and line2atomname(l) in [' N ',' CA ',' C ',' O '] : bfactor = 20 l = changeBfactorOccupancy(l, bfactor, 1) op.write(l) op.close()
def validGeometry_contact(self, tags1, tags2, reslines1, reslines2, atomnbrs) : ## implement in subclasses contact = None for an1 in tags1 : if contact : break for an2 in tags2 : if contact : break if (an1.atn,an2.atn) in atomnbrs : contact = 1 if not contact : return None c1,c2, H1crd, H2crd = 0,0, [0.,0.,0.,], [0.,0.,0.,] for rl in reslines1 : for an in tags1 : if line2atomname(rl) == an.atn : H1crd = vec_add(H1crd, line2crd(rl)) c1 = c1 + 1 for rl in reslines2 : for an in tags2 : if line2atomname(rl) == an.atn : H2crd = vec_add(H2crd, line2crd(rl)) c2 = c2 + 1 H1crd = vec_scale(H1crd, 1./c1) H2crd = vec_scale(H2crd, 1./c2) return pymol_line(H1crd, H2crd)
def Rcrd(self, reslines, tags) : Ratomnames = [] for tat in tags : if tat.tag == 'R' : Ratomnames.append(tat.atn) Rcrd, AMcrd, ANcrd = [], [0.,0.,0.], [] for rl in reslines : if line2atomname(rl) in Ratomnames : Rcrd.append(line2crd(rl)) assert len(Rcrd) >= 3 for rc in Rcrd : AMcrd = vec_add(AMcrd, rc) AMcrd = vec_scale(AMcrd, 1./len(Rcrd)) AN = cross_product(vec_diff(Rcrd[0], Rcrd[1]), vec_diff(Rcrd[2], Rcrd[1])) ANcrd.append(vec_add(AMcrd, AN)) ANcrd.append(vec_diff(AMcrd, AN)) return AMcrd, ANcrd
def Rcrd(self, reslines, tags): Ratomnames = [] for tat in tags: if tat.tag == 'R': Ratomnames.append(tat.atn) Rcrd, AMcrd, ANcrd = [], [0., 0., 0.], [] for rl in reslines: if line2atomname(rl) in Ratomnames: Rcrd.append(line2crd(rl)) assert len(Rcrd) >= 3 for rc in Rcrd: AMcrd = vec_add(AMcrd, rc) AMcrd = vec_scale(AMcrd, 1. / len(Rcrd)) AN = cross_product(vec_diff(Rcrd[0], Rcrd[1]), vec_diff(Rcrd[2], Rcrd[1])) ANcrd.append(vec_add(AMcrd, AN)) ANcrd.append(vec_diff(AMcrd, AN)) return AMcrd, ANcrd
def readProtRes(prot) : res, resids, resnums, resnames, chids, resics, crds = {}, {}, {}, {}, {}, {}, [] for ri in range(len(prot.reslines)) : start, stop = prot.reslines[ri] resids[ri] = line2resid( prot.atomlines[start] ) resnames[ri] = line2resn( prot.atomlines[start] ) resnums[ri] = line2resnum( prot.atomlines[start] ) chids[ri] = line2chid( prot.atomlines[start] ) resics[ri] = line2resic( prot.atomlines[start] ) res[ri] = {} for ai in range(start, stop) : crd = line2crd( prot.atomlines[ai] ) aname = line2atomname( prot.atomlines[ai] ) crds.append(crd) res[ri][aname] = len(crds)-1 return res, resids, resnums, resnames, chids, resics, crds
def changeBfacs(filename, bfacs) : from pdbr import isPdbAAline, line2atomname, line2occu, changeBfactorOccupancy lines = [] if (os.path.isfile(filename)==False) : print "Cannot find file %s "%filename print "No file in directory ", os.getcwd() sys.exit() for l in open(filename, 'r').readlines() : if isPdbAAline(l) : if line2atomname(l) in [" N "," CA "," C "," O "] : l = changeBfactorOccupancy(l, bfacs[0], line2occu(l)) else : l = changeBfactorOccupancy(l, bfacs[1], line2occu(l)) lines.append(l) fp = open(filename, 'w') for l in lines : fp.write(l) fp.close()
def pdbCopy(pdbfrom, pdbto) : from pdbr import isPdbAtomLine, line2atomid, isPdbAAline, line2atomname, changeBfactorOccupancy op = open(pdbto, 'w') idsSofar = [] if (os.path.isfile(pdbfrom)==False) : print "Cannot find file %s "%pdbfrom print "No file in directory ", os.getcwd() sys.exit() for l in open(pdbfrom, 'r').readlines() : if not isPdbAtomLine(l) : op.write(l) ; continue if line2atomid(l) in idsSofar : continue idsSofar.append(line2atomid(l)) bfactor = 30 if isPdbAAline(l) and line2atomname(l) in [' N ',' CA ',' C ',' O '] : bfactor = 20 l = changeBfactorOccupancy(l, bfactor, 1) op.write(l) op.close()
def readProtRes(prot) : res, resids, resnums, resnames, chids, resics, crds = {}, {}, {}, {}, {}, {}, [] for ri in range(len(prot.reslines)) : start, stop = prot.reslines[ri] if start == 0 and stop == 0: print "No ATOM records found in file" continue resids[ri] = line2resid( prot.atomlines[start] ) resnames[ri] = line2resn( prot.atomlines[start] ) resnums[ri] = line2resnum( prot.atomlines[start] ) chids[ri] = line2chid( prot.atomlines[start] ) resics[ri] = line2resic( prot.atomlines[start] ) res[ri] = {} for ai in range(start, stop) : crd = line2crd( prot.atomlines[ai] ) aname = line2atomname( prot.atomlines[ai] ) crds.append(crd) res[ri][aname] = len(crds)-1 if len(res.keys()) == 0 and start ==0 and stop ==0: print "Coordinate file corrupted, pleas check file format. Unable to read coordinae file" import sys ; sys.exit() return res, resids, resnums, resnames, chids, resics, crds
def readProtRes3(prot) : res, resids, resnums, resnames, chids, resics, crds, hetids = {}, {}, {}, {}, {}, {}, [] , {} for ri in range(len(prot.reslines)) : # print prot.reslines[ri] start, stop = prot.reslines[ri] # print prot.atomlines[start] resids[ri] = line2resid( prot.atomlines[start] ) resnames[ri] = line2resn( prot.atomlines[start] ) resnums[ri] = line2resnum( prot.atomlines[start] ) chids[ri] = line2chid( prot.atomlines[start] ) resics[ri] = line2resic( prot.atomlines[start] ) if isHetAtomLine( prot.atomlines[start] ) : hetids[ri] = 1 else : hetids[ri] = 0 res[ri] = {} for ai in range(start, stop) : crd = line2crd( prot.atomlines[ai] ) aname = line2atomname( prot.atomlines[ai] ) crds.append(crd) res[ri][aname] = len(crds)-1 return res, resids, resnums, resnames, chids, resics, crds , hetids
if not isPdbAAline(prot.atomlines[prot.reslines[i][0]]): continue resid1 = line2resid(prot.atomlines[prot.reslines[i][0]]) for j in range(len(prot.reslines)): if not isPdbAAline(prot.atomlines[prot.reslines[j][0]]): continue resid2 = line2resid(prot.atomlines[prot.reslines[j][0]]) vornbrs[(resid1, resid2)] = [] for l in vorface_lines[1:]: #ignore 1st line of output flds = l.split()[1:] #ignore first field of each line a0, a1 = string.atoi(flds[0]), string.atoi(flds[1]) if a0 >= len(prot.atomlines) or a1 >= len(prot.atomlines): continue if not isPdbAAline(prot.atomlines[a0]) or not isPdbAAline( prot.atomlines[a1]): continue resid1 = line2resid(prot.atomlines[a0]) resid2 = line2resid(prot.atomlines[a1]) an1 = line2atomname(prot.atomlines[a0]) an2 = line2atomname(prot.atomlines[a1]) vornbrs[(resid1, resid2)].append((an1, an2)) vornbrs[(resid2, resid1)].append((an2, an1)) ## reduce pdb file pdbfile = mkstemp(suffix=".pdb")[1] print pdbfile proc_run_exitOnError('reduce %s > %s' % (options.pdbin, pdbfile), [], [256]) prot = protein(pdbfile, read_hydrogens=1, read_waters=0, read_hets=0) ## pdb file here os.unlink(pdbfile) # read interactions from xml file reader = xml.sax.make_parser()
vornbrs = {} for i in range(len(prot.reslines)) : if not isPdbAAline(prot.atomlines[ prot.reslines[i][0] ]) : continue resid1 = line2resid( prot.atomlines[prot.reslines[i][0]] ) for j in range(len(prot.reslines)) : if not isPdbAAline(prot.atomlines[ prot.reslines[j][0] ]) : continue resid2 = line2resid( prot.atomlines[prot.reslines[j][0]] ) vornbrs[(resid1,resid2)] = [] for l in vorface_lines[1:] : #ignore 1st line of output flds = l.split()[1:] #ignore first field of each line a0, a1 = string.atoi(flds[0]), string.atoi(flds[1]) if a0 >= len(prot.atomlines) or a1 >= len(prot.atomlines) : continue if not isPdbAAline(prot.atomlines[a0]) or not isPdbAAline(prot.atomlines[a1]) : continue resid1 = line2resid(prot.atomlines[a0]) resid2 = line2resid(prot.atomlines[a1]) an1 = line2atomname(prot.atomlines[a0]) an2 = line2atomname(prot.atomlines[a1]) vornbrs[(resid1,resid2)].append((an1,an2)) vornbrs[(resid2,resid1)].append((an2,an1)) ## reduce pdb file pdbfile = mkstemp(suffix = ".pdb")[1] print pdbfile proc_run_exitOnError('reduce %s > %s' % (options.pdbin, pdbfile), [], [256]) prot = protein(pdbfile, read_hydrogens=1, read_waters=0, read_hets=0) ## pdb file here os.unlink(pdbfile) # read interactions from xml file reader = xml.sax.make_parser() ih = IntxHandler() reader.setContentHandler(ih)