def cyclesfit(atoms1, atoms2,iterations=5,cutoff=2): """ Performs a PyMol-style fit with rejection cycles After every cycle, atoms with more than <cutoff> standard deviations error (in A2) get rejected from the selection """ a1, a2 = numpy.array(atoms1), numpy.array(atoms2) pivot = numpy.sum(a2,axis=0) / float(len(a2)) for n in range(iterations): rotmat, offset, rmsd = rmsdlib.fit(a1,a2) aa2 = apply_matrix(a2, pivot, rotmat, offset) dif = aa2 - a1 d = dif.sum(axis=1) sd = d * d msd = sd.sum()/len(sd) #rmsd = sqrt(msd) std = numpy.std(sd) if (std < 0.1): std = 0.1 keep = numpy.less(sd,cutoff*std) aa1 = a1[keep] aa2 = a2[keep] a1 = aa1 a2 = aa2 return rmsdlib.fit(a1, a2)
def run(runarg): mobile, outputfile = runarg lines2, atoms2, extralines2 = read_pdb(mobile) #select backbone if args.allatoms: assert not args.ca atoms2_fit = atoms2 elif args.ca: atoms2_fit = select_ca(lines2, atoms2) else: atoms2_fit = select_bb(lines2, atoms2) assert len(atoms1_fit) and len(atoms1_fit) == len(atoms2_fit), ( len(atoms1_fit), len(atoms2_fit)) if args.iterative: #perform a Pymol-style iterative fit rotmat, offset, rmsd = cyclesfit(atoms1_fit, atoms2_fit, args.iterative_cycles, args.iterative_cutoff) else: #perform a direct fit rotmat, offset, rmsd = rmsdlib.fit(atoms1_fit, atoms2_fit) if args.rmsd: return rmsd else: pivot = numpy.sum(atoms2_fit, axis=0) / float(len(atoms2_fit)) fitted_atoms = apply_matrix(atoms2, pivot, rotmat, offset) write_pdb(outputfile, lines2, fitted_atoms, extralines2) return outputfile
def run(runarg): ref = runarg print(ref) lines_ref, atoms_ref, extralines_ref = read_pdb(mobiles[ref]) results = np.zeros(len(mobiles)) for ii in range(len(mobiles)): lines, atoms, extralines = read_pdb(mobiles[ii]) rotmap, offset, rmsd = rmsdlib.fit(atoms_ref, atoms) results[ii] = round(rmsd, 3) return ref, results
def run2(runarg): ref = runarg print(ref) atoms_ref = test[ref] results = np.zeros(len(mobiles)) for ii in range(len(mobiles)): if ii == ref: rmsd = 0 else: atoms = test[ii] rotmap, offset, rmsd = rmsdlib.fit(atoms_ref, atoms) results[ii] = round(rmsd, 3) return ref, results
def apply_rnalib(pdb, lib, heavy): """ Adds missing atoms using an RNA lib """ assert heavy == True #TODO: remove this when the rna library has hydrogens import numpy syspath = list(sys.path) sys.path.insert(0, os.environ["ATTRACTTOOLS"]) import rmsdlib from scipy.spatial.distance import cdist sys.path[:] = syspath for res in pdb: if res.resname not in ("RA", "RC", "RG", "RU"): continue while 1: #keep fixing as long as we can missing = set() top = res.topology for a in top.atomorder: aa = a.upper() if heavy and aa[0].startswith("H"): continue if aa not in res.coords: missing.add(aa) if not missing: break nuc = res.resname[1] for fmode, fatoms in ( ("base", lib.baseatoms[nuc]), ("sugar", lib.sugaratoms), ("nucleotide", lib.monoatoms[nuc]), ): #we can fix if there are any missing atoms, and there are at least three non-lacking atoms if any([(m in fatoms) for m in missing]) and \ len([a for a in fatoms if a in res.coords]) >= 3: fixmode = fmode break else: break #no more fixing to be done... if fixmode == "base": libcoor = lib.base[nuc][numpy.newaxis] atoms = lib.baseatoms[nuc] elif fixmode == "sugar": libcoor = lib.sugar atoms = lib.sugaratoms elif fixmode == "nucleotide": libcoor = lib.mono[nuc] atoms = lib.monoatoms[nuc] coor = numpy.array([res.coords[a] for a in atoms if a in res.coords]) mask = numpy.array([(a in res.coords) for a in atoms]) refecoor = numpy.compress(mask, libcoor, axis=1) #or: refecoor = libcoor[:,mask] (slower) rotmat, offset, rmsd = rmsdlib.fit(refecoor[0],coor) pivot = numpy.sum(coor,axis=0) / float(len(coor)) fitcoor = numpy.array(_apply_matrix(coor, pivot, rotmat, offset)) fitcoor = fitcoor.flatten()[numpy.newaxis] refecoor = refecoor.reshape((len(refecoor), 3 * len(coor))) d = cdist(fitcoor, refecoor, 'sqeuclidean')[0] libconfnr = numpy.argmin(d) libconf = libcoor[libconfnr] libconf = _apply_matrix(libconf, pivot+offset, rotmat.T, -offset) for anr, a in enumerate(atoms): if a in missing or fixmode == "base": x,y,z = libconf[anr] res.coords[a] = x,y,z if fixmode == "nucleotide": break
sys.stdout.flush() if name is not None: newargs = initargs + [ '--imodes', 'flexm-' + str(nstruc + 1) + name + '.dat' ] if not os.path.exists('flexm-' + str(nstruc + 1) + name + '.dat'): break collectlib.collect_iattract(newargs) result = collectlib.collect_next() if result: break nstruc += 1 coor = collectlib.collect_all_coor() coor = numpy.compress(unbound_hmask, coor, axis=0) fcoor = numpy.compress(selmask, coor, axis=0) U = rmsdlib.fit(fcoor, fboundatoms)[0] rotfcoor = numpy.array([], ndmin=2) L = len(fcoor) COM1 = numpy.sum(fcoor, axis=0) / float(L) COM2 = numpy.sum(fboundatoms, axis=0) / float(L) for c in fcoor: rotfcoor = numpy.append(rotfcoor, U.dot(c - COM1)) rotfcoor = numpy.reshape(rotfcoor, (-1, 3)) f1.write(str(nstruc)) for start, end in ligandselmask: sel1 = rotfcoor[start:end] sel2 = fboundatoms[start:end] sel2 = sel2 - COM2 irmsd = rmsdlib.rmsd(sel1, sel2) f1.write(" %.3f" % irmsd)
imask = rmsdlib.build_interfacemask(bounds, thresh) selmask = (selmask & imask) if not opt_allatoms: amask = rmsdlib.build_atommask(bounds, atomnames) selmask = (selmask & amask) fboundatoms = allboundatoms[selmask] nstruc = 0 f1 = sys.stdout if output is not None: f1 = open(output, 'w') while 1: sys.stdout.flush() if name is not None: newargs = initargs + [ '--imodes', 'flexm-' + str(nstruc + 1) + name + '.dat' ] if not os.path.exists('flexm-' + str(nstruc + 1) + name + '.dat'): break collectlib.collect_iattract(newargs) result = collectlib.collect_next() if result: break nstruc += 1 coor = collectlib.collect_all_coor() coor = numpy.compress(unbound_hmask, coor, axis=0) fcoor = numpy.compress(selmask, coor, axis=0) irmsd = rmsdlib.fit(fboundatoms, fcoor)[2] f1.write(str(nstruc) + " %.3f\n" % irmsd)
coor = [] for p in unbounds: p.remove_hydrogens() for c in p.coordinates(): coor.append(c) coor = numpy.array(coor) if len(coor) == 0: nstruc += 1 f1.write(str(nstruc)+" None\n") continue rmsdlib.check_pdbs(unbounds, bounds) nstruc += 1 fcoor = numpy.compress(selmask, coor, axis=0) U = rmsdlib.fit(fboundatoms,fcoor)[0] rotfcoor = numpy.array([],ndmin=2) for c in fcoor: rotfcoor = numpy.append(rotfcoor,U.dot(c)) rotfcoor = numpy.reshape(rotfcoor,(-1,3)) f1.write(str(nstruc)) for start, end in ligandselmask: sel1 = rotfcoor[start:end] sel2 = fboundatoms[start:end] irmsd = rmsdlib.rmsd(sel1,sel2) f1.write(" %.3f" % irmsd) f1.write("\n")
#select backbone if args.allatoms: assert not args.ca atoms1_fit = atoms1 atoms2_fit = atoms2 elif args.ca: atoms1_fit = select_ca(lines1, atoms1) atoms2_fit = select_ca(lines2, atoms2) else: atoms1_fit = select_bb(lines1, atoms1) atoms2_fit = select_bb(lines2, atoms2) assert len(atoms1_fit) and len(atoms1_fit) == len(atoms2_fit), (len(atoms1_fit), len(atoms2_fit)) if args.iterative: #perform a Pymol-style iterative fit rotmat, offset, rmsd = cyclesfit(atoms1_fit,atoms2_fit, args.iterative_cycles, args.iterative_cutoff) else: #perform a direct fit rotmat, offset, rmsd = rmsdlib.fit(atoms1_fit,atoms2_fit) pivot = numpy.sum(atoms2_fit,axis=0) / float(len(atoms2_fit)) fitted_atoms = apply_matrix(atoms2, pivot, rotmat, offset) if args.rmsd: print "%.3f" % rmsd else: write_pdb(lines2, fitted_atoms, extralines2)
coor = [] for p in unbounds: p.remove_hydrogens() for c in p.coordinates(): coor.append(c) coor = numpy.array(coor) if len(coor) == 0: nstruc += 1 f1.write(str(nstruc) + " None\n") continue rmsdlib.check_pdbs(unbounds, bounds) nstruc += 1 fcoor = numpy.compress(selmask, coor, axis=0) U, t, irmsd = rmsdlib.fit(fcoor, fboundatoms) rotfcoor = numpy.array([], ndmin=2) L = len(fcoor) COM1 = numpy.sum(fcoor, axis=0) / float(L) COM2 = numpy.sum(fboundatoms, axis=0) / float(L) for c in fcoor: rotfcoor = numpy.append(rotfcoor, U.dot(c - COM1)) rotfcoor = numpy.reshape(rotfcoor, (-1, 3)) f1.write(str(nstruc)) for start, end in ligandselmask: sel1 = rotfcoor[start:end, :] sel2 = fboundatoms[start:end, :] sel2 = sel2 - COM2 irmsd = rmsdlib.rmsd(sel1, sel2) f1.write(" %.3f" % irmsd)