def _rmsd(res): #mol is an md universe #res,name_file_dcd,mol=args mol = MD.Universe(pdb_file, dcd_file) print "["+str(res)+"] Aligning Trajectory" ref_sel = ref.selectAtoms('backbone and resid ' + str(res),'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') print ref_sel.atoms ref_sel_str = '' for i in list(ref_sel): useful =str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>',' ').replace("'",'').replace(',',' and').replace('of','and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] #print ref_sel_str filenamedcd = "LA" + cutoff + "rmsd.noh.R" + str(res) + '.dcd' MDA.rms_fit_trj(mol, ref, select=ref_sel_str, filename = filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "["+str(res)+"] Calculating RMSD for residue " + str(res) + "..." + str( 1 + range(res_beg,res_end).index(res) ) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() atoms_mol = mol.selectAtoms("resid " + str(res) + " and not name H") mol.trajectory[ref_frame] atoms_ref = copy.deepcopy(mol.selectAtoms("resid " + str(res) + " and not name H").coordinates()) rmsd = [] for ts1 in mol.trajectory[0:-1:skip]: rmsd.append(MDA.rmsd(atoms_mol.coordinates(), atoms_ref)) rmsd = numpy.array(rmsd) numpy.savetxt(name_file_dcd + "rmsd.noh.LA" + cutoff +".%04d.txt" % res , rmsd) os.remove(filenamedcd)
def _bfactor(res): global ave_flag #mol is an md universe #res,name_file_dcd,mol=args ref = MD.Universe(pdb_file) mol = MD.Universe(pdb_file, dcd_file) print "["+str(res)+"] Aligning Trajectory" ref_sel = ref.selectAtoms('backbone and resid ' + str(res),'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') print ref_sel.atoms #for i in list(ref_sel): # print i # print ref_sel.indices ref_sel_str = '' for i in list(ref_sel): useful =str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>',' ').replace("'",'').replace(',',' and').replace('of','and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] #print ref_sel_str filenamedcd = "LA" + cutoff + "b-factor.R" + str(res) + '.' + chain + '.dcd' MDA.rms_fit_trj(mol, ref, select=ref_sel_str, filename = filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "["+str(res)+"] Calculating RMSF for residue " + str(res) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() atoms_mol = mol.selectAtoms("resid " + str(res) + " and name CA and segid " +str(chain)) atoms_ref = ref.selectAtoms("resid " + str(res) + " and name CA and segid " +str(chain)) var = [] if ave_flag == 0: for ts1 in mol.trajectory[0:-1:skip]: var.append((numpy.linalg.norm(numpy.subtract(atoms_mol.coordinates(), atoms_ref.coordinates())))**2) elif ave_flag: ref_coord = [] for ts1 in mol.trajectory[0:-1:skip]: ref_coord.append(atoms_mol.coordinates()[0]) ref_coord = numpy.array(ref_coord) #print ref_coord.shape #ref_coord = ref_coord[1:5] #print numpy.average( ref_coord[:,0]) #print numpy.average( ref_coord[:,1]) #print numpy.average( ref_coord[:,2]) ave = [numpy.average( ref_coord[:,0]) , numpy.average( ref_coord[:,1]), numpy.average( ref_coord[:,2])] for ts1 in mol.trajectory[0:-1:skip]: var.append((numpy.linalg.norm(numpy.subtract(atoms_mol.coordinates(), ave)))**2) var = numpy.array(var) rmsf = numpy.average(var) err_rmsf = numpy.std(var) bf = (8*numpy.pi*numpy.pi/3)*rmsf*rmsf err_bf = (8*numpy.pi**2/3)*err_rmsf*2*rmsf # error propagation of quadractic function filename = name_file_dcd + "b-factor.LA" + cutoff +".%03d." % res + chain + ".txt" dataout = open(filename, 'w') dataout.write(str(res) + "\t%.5f" % rmsf + "\t%.5f" % err_rmsf + '\n') dataout.close() os.remove(filenamedcd)
def test_rms_fit_trj(self): """Testing align.rms_fit_trj() for all atoms (Issue 58)""" # align to *last frame* in target... just for the heck of it self.reference.trajectory[-1] align.rms_fit_trj(self.universe, self.reference, select="all", filename=self.outfile, verbose=False) fitted = mda.Universe(PSF, self.outfile) # RMSD against the reference frame # calculated on Mac OS X x86 with MDA 0.7.2 r689 # VMD: 6.9378711 self._assert_rmsd(fitted, 0, 6.929083044751061) self._assert_rmsd(fitted, -1, 0.0)
def test_rms_fit_trj_defaultfilename(self): filename = 'rmsfit_' + os.path.basename(self.universe.trajectory.filename) with tempdir.in_tempdir(): # Need to pretend to have the universe trajectory INSIDE the tempdir because # filename=None uses the full path self.universe.trajectory.filename = os.path.abspath( os.path.join( os.curdir, os.path.basename(self.universe.trajectory.filename))) #test filename=none and different selection align.rms_fit_trj(self.universe, self.reference, select="name CA", filename=None, verbose=False) assert_(os.path.exists(filename), "rms_fit_trj did not write to {}".format(filename))
def test_rms_fit_trj_defaultfilename(self): filename = 'rmsfit_' + os.path.basename( self.universe.trajectory.filename) with tempdir.in_tempdir(): # Need to pretend to have the universe trajectory INSIDE the tempdir because # filename=None uses the full path self.universe.trajectory.filename = os.path.abspath( os.path.join( os.curdir, os.path.basename(self.universe.trajectory.filename))) #test filename=none and different selection align.rms_fit_trj(self.universe, self.reference, select="name CA", filename=None, verbose=False) assert_(os.path.exists(filename), "rms_fit_trj did not write to {}".format(filename))
def _rmsd(res): #mol is an md universe #res,name_file_dcd,mol=args mol = MD.Universe(pdb_file, dcd_file) print "[" + str(res) + "] Aligning Trajectory" ref_sel = ref.selectAtoms( 'backbone and resid ' + str(res), 'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') print ref_sel.atoms ref_sel_str = '' for i in list(ref_sel): useful = str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>', ' ').replace("'", '').replace( ',', ' and').replace('of', 'and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] #print ref_sel_str filenamedcd = "LA" + cutoff + "rmsd.noh.R" + str(res) + '.dcd' MDA.rms_fit_trj( mol, ref, select=ref_sel_str, filename=filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "[" + str(res) + "] Calculating RMSD for residue " + str( res) + "..." + str( 1 + range(res_beg, res_end).index(res)) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() atoms_mol = mol.selectAtoms("resid " + str(res) + " and not name H") mol.trajectory[ref_frame] atoms_ref = copy.deepcopy( mol.selectAtoms("resid " + str(res) + " and not name H").coordinates()) rmsd = [] for ts1 in mol.trajectory[0:-1:skip]: rmsd.append(MDA.rmsd(atoms_mol.coordinates(), atoms_ref)) rmsd = numpy.array(rmsd) numpy.savetxt(name_file_dcd + "rmsd.noh.LA" + cutoff + ".%04d.txt" % res, rmsd) os.remove(filenamedcd)
def rms_fit(self): """Superimpose whole trajectory on first frame""" if self.trajectoryAligned and not self.rerun: self.vout('Trajectory of {0} already fitted\n'.format(self.name)) else: self.vout('Fitting trajectory of {0}\n'.format(self.name)) self.universe.trajectory.rewind() if not self.verbose: # redirect stdout nirvana = open('/dev/null', 'w') sys.stdout = nirvana sys.stderr = nirvana align.rms_fit_trj(self.universe, self.universe, select='backbone', filename=self.alignedTrj) if not self.verbose: # restore stdout sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ nirvana.close() # pickle trajectory meta data dumpFile = open(self.trjmetafile, 'wb') pickle.dump(self.trjMetaData, dumpFile, protocol=pickle.HIGHEST_PROTOCOL) dumpFile.close() # load fittet trajectory self.vout("Reading fittet trajectory of {0}\n".format(self.name)) self.universe = mda.Universe(self.topology, self.alignedTrj) self.trajectoryAligned = True
def _ct_part(res): #mol is an md universe # res,name_file_dcd,mol=args ref = MD.Universe(pdb_file) mol = MD.Universe(pdb_file, dcd_file) print "[" + str(res) + "] Aligning Trajectory" ref_sel = ref.selectAtoms( 'backbone and resid ' + str(res), 'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') # print ref_sel.atoms # for i in list(ref_sel): # print i # print ref_sel.indices ref_sel_str = '' for i in list(ref_sel): useful = str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>', ' ').replace("'", '').replace( ',', ' and').replace('of', 'and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] # print ref_sel_str filenamedcd = "LA" + cutoff + ".R" + str(res) + '.dcd' MDA.rms_fit_trj( mol, ref, select=ref_sel_str, filename=filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "[" + str(res) + "] Calculating C(t) for residue " + str( res) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() N_atoms = mol.selectAtoms("resid " + str(res) + " and (name N or name HN) and segid " + str(chain)) if len(N_atoms.coordinates()) == 2: bond_vec = [] ct_numpy = [] ct_std_numpy = [] ct_python = [] for ts1 in mol.trajectory[0:-1:skip]: coords = N_atoms.coordinates() coords = numpy.subtract(coords[1], coords[0]) bond_vec.append(coords / numpy.linalg.norm(coords)) print "[" + str( res) + "] Calculating Correlation function as Chen 2004" for t in range(len(bond_vec) / 2): T = t * step # x_chen.append(T) if t == 0: a = numpy.array(bond_vec) b = numpy.array(bond_vec) else: a = numpy.array(bond_vec[:-t]) b = numpy.array(bond_vec[t:]) np_dot = inner1d(a, b) np_dot = (3 * np_dot**2 - 1) / 2 ct = numpy.mean(np_dot) ct_std = numpy.std(np_dot) # ct = (3*(X)/a.shape[0]- 1)/2 ct_numpy.append(ct) ct_std_numpy.append(ct_std) # NH_dot = 0 # for tal in range(len(bond_vec)-t): # NH_dot += (3*(numpy.dot(bond_vec[tal], bond_vec[tal+t])**2)-1)/2 # NH_dot = NH_dot/(len(bond_vec)-t) # ct_python.append(NH_dot) # output += str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\n" output += str(res) + "\t" + str( T) + "\t%.5f" % ct + "\t%.5f" % ct_std + "\n" # output += str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\t" + str(ct) + "\n" # print str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\t" + str(ct) + "\n" # time.sleep(5) ct_numpy = numpy.around(ct_numpy, 5) print "[" + str(res) + "] Done in " + str(time.time() - cf_timer) + " seconds.\n" filename = name_file_dcd + "ct_numpy.LA" + cutoff + ".%03d." % res + chain + ".txt" outfile = open(filename, 'w') outfile.write(output) outfile.close() print "[" + str(res) + "] Collecting Trash" os.remove(filenamedcd) print "[" + str(res) + "] Done for real"
topology = sys.argv[1] init_pdb = sys.argv[2] traj_file = sys.argv[3] lig_name = sys.argv[4] filter_lig = 'resname ' + lig_name filter_structure = 'protein or ' + filter_lig init = mda.Universe(topology, init_pdb) traj = mda.Universe(topology, traj_file) frames = traj.trajectory nframes = len(frames) align.rms_fit_trj(traj, init, select="protein and backbone") ref = init avg_select = ref.selectAtoms(filter_structure) align_struct = traj.selectAtoms(filter_structure) p_avg = numpy.zeros_like(avg_select.positions) # do a quick average of the protein for ts in frames: p_avg += align_struct.positions p_avg /= nframes # temporarily replace positions with the average avg_select.set_positions(p_avg)
from MDAnalysis.tests.datafiles import PSF, DCD, PDB_small try: import matplotlib matplotlib.use('agg') from pylab import plot, xlabel, ylabel, savefig have_matplotlib = True except: have_matplotlib = False ref = Universe(PSF, PDB_small) # reference structure 4AKE trj = Universe(PSF, DCD) # trajectory of change 1AKE->4AKE rms_fit_trj(trj, ref, filename="output/rmsfit.dcd", select="not name H*", mass_weighted=True, rmsdfile="output/rmsfit_rmsd.dat") print "Fitted trajectory: output/rmsfit.dcd" rmsd = numpy.loadtxt("output/rmsfit_rmsd.dat") print "RMSD: output/rmsfit_rmsd.dat" if have_matplotlib: plot(rmsd, 'k-', linewidth=2) xlabel("frame number") ylabel(r"mass weighted heavy atom RMSD ($\AA$)") savefig("figures/rmsdfit_rmsd.pdf") savefig("figures/rmsdfit_rmsd.png") print "RMSD graph: figures/rmsdfit_rmsd.{pdf,png}"
def _rmsf(res): global ave_flag global fop_flag #mol is an md universe #res,name_file_dcd,mol=args ref = MD.Universe(pdb_file) mol = MD.Universe(pdb_file, dcd_file) print "[" + str(res) + "] Aligning Trajectory" ref_sel = ref.selectAtoms( 'backbone and resid ' + str(res), 'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') print ref_sel.atoms #for i in list(ref_sel): # print i # print ref_sel.indices ref_sel_str = '' for i in list(ref_sel): useful = str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>', ' ').replace("'", '').replace( ',', ' and').replace('of', 'and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] #print ref_sel_str filenamedcd = "LA" + cutoff + "rmsf.R" + str(res) + '.' + chain + '.dcd' MDA.rms_fit_trj( mol, ref, select=ref_sel_str, filename=filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "[" + str(res) + "] Calculating RMSF for residue " + str( res) + "..." + str( 1 + range(res_beg, res_end).index(res)) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() atoms_mol = mol.selectAtoms("resid " + str(res) + " and name CA and segid " + str(chain)) atoms_ref = ref.selectAtoms("resid " + str(res) + " and name CA and segid " + str(chain)) var = [] if ave_flag == 0: for ts1 in mol.trajectory[0:-1:skip]: var.append((numpy.linalg.norm( numpy.subtract(atoms_mol.coordinates(), atoms_ref.coordinates())))**2) elif ave_flag: ref_coord = [] for ts1 in mol.trajectory[0:-1:skip]: ref_coord.append(atoms_mol.coordinates()[0]) ref_coord = numpy.array(ref_coord) #print ref_coord.shape #ref_coord = ref_coord[1:5] #print numpy.average( ref_coord[:,0]) #print numpy.average( ref_coord[:,1]) #print numpy.average( ref_coord[:,2]) ave = [ numpy.average(ref_coord[:, 0]), numpy.average(ref_coord[:, 1]), numpy.average(ref_coord[:, 2]) ] for ts1 in mol.trajectory[0:-1:skip]: var.append( (numpy.linalg.norm(numpy.subtract(atoms_mol.coordinates(), ave)))**2) var = numpy.array(var) rmsf = numpy.average(var) err_rmsf = numpy.std(var) filename = name_file_dcd + "rmsf.LA" + cutoff + ".%04d." % res + chain + ".txt" dataout = open(filename, 'w') dataout.write(str(res) + "\t%.5f" % rmsf + "\t%.5f" % err_rmsf + '\n') dataout.close() os.remove(filenamedcd) if fop_flag == 1: output = str(res) + '\n' for i in range(len(var)): output += '%.5f' % var[i] + '\n' fopfile = open( name_file_dcd + "rmsf.LA" + cutoff + ".%04d." % res + chain + ".fulldata", 'w') fopfile.write(output) fopfile.close()
def _ct_part(res): #mol is an md universe # res,name_file_dcd,mol=args ref = MD.Universe(pdb_file) mol = MD.Universe(pdb_file, dcd_file) print "["+str(res)+"] Aligning Trajectory" ref_sel = ref.selectAtoms('backbone and resid ' + str(res),'backbone and around ' + cutoff + ' (backbone and resid ' + str(res) + ')') # print ref_sel.atoms # for i in list(ref_sel): # print i # print ref_sel.indices ref_sel_str = '' for i in list(ref_sel): useful =str(i).split(":")[1] ref_sel_str += '(' + useful.replace('>',' ').replace("'",'').replace(',',' and').replace('of','and') + ') or ' #ref_sel_str += "(" + useful.replace('>',' ').replace("'",'') + ') or ' ref_sel_str = ref_sel_str[:-4] # print ref_sel_str filenamedcd = "LA" + cutoff + ".R" + str(res) + '.dcd' MDA.rms_fit_trj(mol, ref, select=ref_sel_str, filename = filenamedcd ) #'name N and type N and resname MET resid 1 and segid P1') output = '' print "["+str(res)+"] Calculating C(t) for residue " + str(res) + " of " + str(res_num) mol = MD.Universe(pdb_file, filenamedcd) cf_timer = time.time() N_atoms = mol.selectAtoms("resid " + str(res) + " and (name N or name HN) and segid " +str(chain)) if len(N_atoms.coordinates()) == 2: bond_vec = [] ct_numpy = [] ct_std_numpy = [] ct_python = [] for ts1 in mol.trajectory[0:-1:skip]: coords = N_atoms.coordinates() coords = numpy.subtract(coords[1], coords[0]) bond_vec.append(coords/numpy.linalg.norm(coords)) print "["+str(res)+"] Calculating Correlation function as Chen 2004" for t in range(len(bond_vec)/2): T = t*step # x_chen.append(T) if t == 0: a = numpy.array(bond_vec) b = numpy.array(bond_vec) else: a = numpy.array(bond_vec[:-t]) b = numpy.array(bond_vec[t:]) np_dot = inner1d(a, b) np_dot = (3*np_dot**2 - 1)/2 ct = numpy.mean(np_dot) ct_std = numpy.std(np_dot) # ct = (3*(X)/a.shape[0]- 1)/2 ct_numpy.append(ct) ct_std_numpy.append(ct_std) # NH_dot = 0 # for tal in range(len(bond_vec)-t): # NH_dot += (3*(numpy.dot(bond_vec[tal], bond_vec[tal+t])**2)-1)/2 # NH_dot = NH_dot/(len(bond_vec)-t) # ct_python.append(NH_dot) # output += str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\n" output += str(res) + "\t" + str(T) + "\t%.5f" % ct + "\t%.5f" % ct_std + "\n" # output += str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\t" + str(ct) + "\n" # print str(res) + "\t" + str(T) + "\t" + str(NH_dot) + "\t" + str(ct) + "\n" # time.sleep(5) ct_numpy = numpy.around(ct_numpy, 5) print "["+str(res)+"] Done in " + str(time.time()-cf_timer) + " seconds.\n" filename = name_file_dcd + "ct_numpy.LA" + cutoff +".%03d." % res + chain + ".txt" outfile = open(filename, 'w') outfile.write(output) outfile.close() print "["+str(res)+"] Collecting Trash" os.remove(filenamedcd) print "["+str(res)+"] Done for real"
def RMS_fit_trj(*args, **kwargs): warnings.warn("RMS_fit_trj is deprecated and will be removed. Use rms_fit_trj", category=DeprecationWarning) return rms_fit_trj(*args, **kwargs)
try: import matplotlib matplotlib.use('agg') from pylab import plot, xlabel, ylabel, savefig have_matplotlib = True except: have_matplotlib = False ref = Universe(PSF, PDB_small) # reference structure 4AKE trj = Universe(PSF, DCD) # trajectory of change 1AKE->4AKE rms_fit_trj(trj, ref, filename="output/rmsfit.dcd", select="not name H*", mass_weighted=True, rmsdfile="output/rmsfit_rmsd.dat") print "Fitted trajectory: output/rmsfit.dcd" rmsd = numpy.loadtxt("output/rmsfit_rmsd.dat") print "RMSD: output/rmsfit_rmsd.dat" if have_matplotlib: plot(rmsd, 'k-', linewidth=2) xlabel("frame number") ylabel(r"mass weighted heavy atom RMSD ($\AA$)") savefig("figures/rmsdfit_rmsd.pdf") savefig("figures/rmsdfit_rmsd.png") print "RMSD graph: figures/rmsdfit_rmsd.{pdf,png}"
import MDAnalysis.analysis.align as align topology = sys.argv[1] init_pdb = sys.argv[2] traj_file = sys.argv[3] lig_name = sys.argv[4] filter_lig = 'resname ' + lig_name filter_structure = 'protein or ' + filter_lig init = mda.Universe(topology, init_pdb) traj = mda.Universe(topology, traj_file) frames = traj.trajectory nframes = len(frames) align.rms_fit_trj(traj, init, select="protein and backbone") ref = init avg_select = ref.selectAtoms(filter_structure) align_struct = traj.selectAtoms(filter_structure) p_avg = numpy.zeros_like(avg_select.positions) # do a quick average of the protein for ts in frames: p_avg += align_struct.positions p_avg /= nframes # temporarily replace positions with the average avg_select.set_positions(p_avg)