def write_trajectory(universe, filename): '''A simplified method for writing trajectories ''' w = Writer(filename, universe.atoms.numberOfAtoms()) for ts in universe.trajectory: w.write(ts) w.close()
def Move_2_center(top_file, trj_file, trjout_file): u = Universe(top_file, trj_file) TRAJ_FRAMES = u.trajectory.numframes w = Writer(trjout_file, u.trajectory.numatoms) atoms = Simple_atom.Get_atom_list(top_file) SOLUTE_COUNT = 0 for atom_ID in atoms: if atoms[atom_ID].residue_name != "SOL" and atoms[ atom_ID].residue_name != "WAT": SOLUTE_COUNT += 1 else: pass print '''Note: this program will read the pbc condition and use the dimensions \ read from trajectory files. You should make sure the dimensions are right or \ it will create a wrong output trajectory file.''' START_TIME = Time.time() NUM_ATOMS = u.trajectory.numatoms # loop through the trajectory and write a frame for every step for ts in u.trajectory: dimensions = ts.dimensions for i in range(SOLUTE_COUNT): if ts._x[i] > dimensions[0]: ts._x[i] = ts._x[i] - dimensions[0] if ts._x[i] < 0: ts._x[i] = ts._x[i] + dimensions[0] if ts._y[i] > dimensions[1]: ts._y[i] = ts._y[i] - dimensions[1] if ts._y[i] < 0: ts._y[i] = ts._y[i] + dimensions[1] if ts._z[i] > dimensions[2]: ts._z[i] = ts._z[i] - dimensions[2] if ts._z[i] < 0: ts._z[i] = ts._z[i] + dimensions[2] NOW_TIME = Time.time() BIN_TIME = NOW_TIME - START_TIME if ts.frame % 100 == 0: # usage.echo("%8.4f %8.4f %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2])) usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \ % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) )) # for ts in u.trajectory: w.write(ts) # usage.echo("Writting frame %d\r" %ts.frame) w.close_trajectory() print "Converted %r --> %r" % (intrj, outtrj)
def write_trajectory(universe, filename, maximum_frames=0): '''A simplified method for writing trajectories ''' w = Writer(filename, universe.atoms.numberOfAtoms()) frame_count = 0 for ts in universe.trajectory: if(maximum_frames != 0 and frame_count == maximum_frames): break w.write(ts) frame_count += 1 w.close()
def Move_2_center(top_file,trj_file,trjout_file): u=Universe(top_file,trj_file) TRAJ_FRAMES=u.trajectory.numframes w = Writer(trjout_file, u.trajectory.numatoms) atoms=Simple_atom.Get_atom_list(top_file) SOLUTE_COUNT = 0 for atom_ID in atoms: if atoms[atom_ID].residue_name != "SOL" and atoms[atom_ID].residue_name != "WAT": SOLUTE_COUNT +=1 else: pass print '''Note: this program will read the pbc condition and use the dimensions \ read from trajectory files. You should make sure the dimensions are right or \ it will create a wrong output trajectory file.''' START_TIME=Time.time() NUM_ATOMS = u.trajectory.numatoms # loop through the trajectory and write a frame for every step for ts in u.trajectory: dimensions=ts.dimensions for i in range(SOLUTE_COUNT): if ts._x[i] > dimensions[0]: ts._x[i]=ts._x[i]-dimensions[0] if ts._x[i] <0: ts._x[i]=ts._x[i]+dimensions[0] if ts._y[i] > dimensions[1]: ts._y[i]=ts._y[i]-dimensions[1] if ts._y[i] <0: ts._y[i]=ts._y[i]+dimensions[1] if ts._z[i] > dimensions[2]: ts._z[i]=ts._z[i]-dimensions[2] if ts._z[i] < 0: ts._z[i]=ts._z[i]+dimensions[2] NOW_TIME=Time.time() BIN_TIME=NOW_TIME-START_TIME if ts.frame % 100 == 0: # usage.echo("%8.4f %8.4f %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2])) usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \ % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) )) # for ts in u.trajectory: w.write(ts) # usage.echo("Writting frame %d\r" %ts.frame) w.close_trajectory() print "Converted %r --> %r" % (intrj, outtrj)
from MDAnalysis.tests.datafiles import PRM, TRJ_bz2 from MDAnalysis import Universe, Writer from MDAnalysis.core.util import greedy_splitext import os.path topol = PRM # PRMpbc intrj = TRJ_bz2 # TRJpbc_bz2 ext = '.dcd' # output format determined by extension root, oldext = greedy_splitext(os.path.basename(intrj)) outtrj = root + ext outpdb = root + '.pdb' u = Universe(topol, intrj) # create a writer instance for the output trajectory w = Writer(outtrj, u.trajectory.numatoms) # loop through the trajectory and write a frame for every step for ts in u.trajectory: w.write(ts) print "Converted frame %d" % ts.frame w.close_trajectory() print "Converted %r --> %r" % (intrj, outtrj) # make a pdb file as a simple 'topology' u.trajectory.rewind() u.atoms.write(outpdb) print "Created %r to be used with the trajectory" % outpdb
# coding=utf-8 """ MDAnalysis example: Convert DCD trajectory into XTC =================================================== This example shows how one can use MDAnalysis to convert between different trajectory formats. """ from MDAnalysis.tests.datafiles import PDB_small, DCD from MDAnalysis import Universe, Writer import os.path root, ext = os.path.splitext(os.path.basename(DCD)) xtcname = root + '.xtc' # output format determined by extension u = Universe(PDB_small, DCD) # create a writer instance for the output trajectory w = Writer(xtcname, u.trajectory.numatoms) # loop through the trajectory and write a frame for every step for ts in u.trajectory: w.write(ts) print "Converted frame {0:d}".format(ts.frame) w.close_trajectory() print "Converted {0!r} --> {1!r}".format(DCD, xtcname)
def Move_2_center(top_file,trj_file,trjout_file,skip=1): u=Universe(top_file,trj_file) TRAJ_FRAMES=u.trajectory.numframes atom_l=Simple_atom.Get_Simple_atom_list(top_file) while True: fit_residue=Simple_atom.Get_residue(top_file) if len(fit_residue) > 1: print "Only 1 residue is aviliable." continue else: break while True: fit_atom=Simple_atom.Get_atom(atom_l,fit_residue[0]) if len(fit_atom) > 1: print "Only 1 atom is aviliable." continue else: break fitting_group=Simple_atom.Get_residue(top_file) fit_atom=fit_atom[0] w = Writer(trjout_file, u.trajectory.numatoms) NUM_ATOMS=len(atom_l) print '''Note: this program will read the pbc condition and use the dimensions \ read from trajectory files. You should make sure the dimensions are right or \ it will create a wrong output trajectory file.''' START_TIME=Time.time() # loop through the trajectory and write a frame for every step for ts in u.trajectory: if ts.frame % skip != 1: break if ts.frame==1: label_coordinate=[ts._x[fit_atom],ts._y[fit_atom],ts._z[fit_atom]] # origin_list=list() # for atom in atom_l: # if atom.residue_serial in fitting_group: # origin_list.append([\ # ts._x[atom_l.index(atom)],\ # ts._y[atom_l.index(atom)],\ # ts._z[atom_l.index(atom)],\ # ]) else: shift=[label_coordinate[0]-ts._x[fit_atom],\ label_coordinate[1]-ts._y[fit_atom],\ label_coordinate[2]-ts._z[fit_atom]] dimensions=ts.dimensions usage.echo("%8.4f %8.4f %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2])) for i in range(NUM_ATOMS): ts._x[i] =ts._x[i]+shift[0] if ts._x[i] > dimensions[0]: ts._x[i]=ts._x[i]-dimensions[0] if ts._x[i] <0: ts._x[i]=ts._x[i]+dimensions[0] ts._y[i] =ts._y[i]+shift[1] if ts._y[i] > dimensions[1]: ts._y[i]=ts._y[i]-dimensions[1] if ts._y[i] <0: ts._y[i]=ts._y[i]+dimensions[1] ts._z[i] =ts._z[i]+shift[2] if ts._z[i] > dimensions[2]: ts._z[i]=ts._z[i]-dimensions[2] if ts._z[i] < 0: ts._z[i]=ts._z[i]+dimensions[2] # temp_list=list() # for atom in atom_l: # if atom.residue_serial in fitting_group: # temp_list.append([\ # ts._x[atom_l.index(atom)],\ # ts._y[atom_l.index(atom)],\ # ts._z[atom_l.index(atom)],\ # ]) # # [Rotate,shift]=least_squares_fitting.Fitting(temp_list,origin_list) # atom_matrix=numpy.array([[ts._x[i],ts._y[i],ts._z[i]]for i in range(NUM_ATOMS)]) # # resu_matrix=numpy.matrix(atom_matrix) * numpy.matrix(Rotate).T # # resu_matrix=numpy.array(resu_matrix) # # for i in range(NUM_ATOMS): # # atom_matrix[i,0]=resu_matrix[i,0]+shift[0] # # atom_matrix[i,1]=resu_matrix[i,1]+shift[1] # # atom_matrix[i,2]=resu_matrix[i,2]+shift[2] # for i in range(NUM_ATOMS): # ts._x[i]=atom_matrix[i,0] # ts._y[i]=atom_matrix[i,1] # ts._z[i]=atom_matrix[i,2] # w.write(ts) NOW_TIME=Time.time() BIN_TIME=NOW_TIME-START_TIME usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \ % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) )) # for ts in u.trajectory: w.write(ts) # usage.echo("Writting frame %d\r" %ts.frame) w.close_trajectory() print "Converted %r --> %r" % (intrj, outtrj)
Index[nst] = [n] else: print "Error: ", n, nst, N_str ifile.close() print 'number of clusters:', len(N_str) N_str.sort() N_str.reverse() N_write = int(raw_input('How many clusters you want to look at (eg. 8): ')) universe = Universe(pdbfile, pdbfile) writer = Writer(filename + '_.pdb', universe.atoms.numberOfAtoms()) k = 1 for each in N_str[:N_write]: if k > N_write: break percent = float(each) * 100 / sum(N_str) for i in Index[each]: print 'Writing cluster #' + str(i).ljust(3), universe.trajectory[i - 1] protein = universe.atoms writer.write(protein) print '%3i %5.0f%% %6.2f%%' % (k, percent, percent) k += 1 writer.close()
def Move_2_center(top_file,trj_file,index_file,trjout_file): u=Universe(top_file,trj_file) TRAJ_FRAMES=u.trajectory.numframes w = Writer(trjout_file, u.trajectory.numatoms) atoms=Simple_atom.Get_Simple_atom_list(top_file) index = Index.Read_index_to_Inclass(index_file) Index.Print_Index(index) while True: try: solute_index=int(raw_input("Choosing the group for centering:")) # solvent_index = int(raw_input("Choosing the solvent group:")) break except: print "You should input a number." continue solute_group = index[solute_index].group_list # solvent_group = index[solvent_index].group_list solute_atoms =len(solute_group) NUM_ATOMS = u.trajectory.numatoms # print "\t Reading %d frames from trajectory file: %s" %(nframes,traj_file) START_TIME=Time.time() for ts in u.trajectory: ref_com =np.zeros((3),dtype=np.float32) # sys.stderr.write("\t Reading frame %8d\r" %ts.frame) # sys.stderr.flush() for i,ai in list(enumerate(solute_group)): ref_com[0] += ts._x[ai-1] ref_com[1] += ts._y[ai-1] ref_com[2] += ts._z[ai-1] ref_com = ref_com/solute_atoms dimensions=ts.dimensions ref_com = ref_com - np.array([dimensions[0]/2, dimensions[1]/2, dimensions[2]/2]) # ref_com = np.array([sum(traj_data[:,0])/solute_atoms - dimensions[0]/2,\ # sum(traj_data[:,1])/solute_atoms - dimensions[1]/2,\ # sum(traj_data[:,2])/solute_atoms - dimensions[2]/2]) for i in range(NUM_ATOMS): ts._x[i] = ts._x[i] - ref_com[0] ts._y[i] = ts._y[i] - ref_com[1] ts._z[i] = ts._z[i] - ref_com[2] if (i+1) in solute_group: continue if ts._x[i] > dimensions[0] or ts._x[i] <0: ts._x[i]=ts._x[i]%dimensions[0] if ts._y[i] > dimensions[1] or ts._y[i] <0: ts._y[i]=ts._y[i]%dimensions[1] if ts._z[i] > dimensions[2] or ts._z[i] < 0: ts._z[i]=ts._z[i]%dimensions[2] NOW_TIME=Time.time() BIN_TIME=NOW_TIME-START_TIME # if ts.frame % 10 == 0: # usage.echo("%8.4f %8.4f %8.4f\r" %(dimensions[0],dimensions[1],dimensions[2])) usage.echo(" "*40+"Converted frame %d, time used: %8.2f s, time left: %8.2f s \r" \ % (ts.frame,BIN_TIME,BIN_TIME*(float(TRAJ_FRAMES)/ts.frame-1) )) # for ts in u.trajectory: w.write(ts) # del traj_data # usage.echo("Writting frame %d\r" %ts.frame) w.close_trajectory() print "Converted %r --> %r" % (intrj, outtrj)