def mdanalysis_concat_trr(job): import MDAnalysis print(job.workspace()) for load in [5, 15, 25]: if len( glob.glob(job.workspace() + "shear_{}nN_combined.trr".format(load))) is 0: trr_list = sorted( glob.glob(job.workspace() + "/shear_{}nN.*.trr".format(load))) if len(trr_list) is 0: continue else: tpr_file = sorted(glob.glob( str(job.workspace() + "/shear_{}nN*tpr").format(load)), reverse=True)[0] u = MDAnalysis.Universe(tpr_file, trr_list) all_system = u.select_atoms("all") with MDAnalysis.Writer( "{}/shear_{}nN_combined.trr".format( job.workspace(), load), all_system.n_atoms) as outfile: for ts in u.trajectory: outfile.write(all_system) else: continue
def setUp(self): self.universe = mda.Universe(TRZ_psf, TRZ) self.ag = self.universe.select_atoms('name N') self.prec = 3 self.tmpdir = tempdir.TempDir() self.outfile = self.tmpdir.name + '/partial-write-test.pdb' self.Writer = mda.Writer(self.outfile, n_atoms=len(self.ag))
def test_write_AtomGroup(self, universe, outfile, outtop): """test to write NCDF from AtomGroup (Issue 116)""" p = universe.select_atoms("not resname WAT") p.write(outtop) with mda.Writer(outfile, n_atoms=p.n_atoms, format="ncdf") as W: for ts in universe.trajectory: W.write(p) uw = mda.Universe(outtop, outfile) pw = uw.atoms for orig_ts, written_ts in zip(universe.trajectory, uw.trajectory): assert_almost_equal(p.positions, pw.positions, self.prec, err_msg="coordinate mismatch between " "original and written trajectory at " "frame %d (orig) vs %d (written)" % (orig_ts.frame, written_ts.frame)) assert_almost_equal(orig_ts.time, written_ts.time, self.prec, err_msg="Time for step {0} are not the " "same.".format(orig_ts.frame)) assert_almost_equal(written_ts.dimensions, orig_ts.dimensions, self.prec, err_msg="unitcells are not identical")
def write_cluster_traj(cluster_idx, top_file, trj_file, out_name, start_frame=0): """ Writes a trajectory into a separate file for each cluster. Args: cluster_idx (int array): Cluster index for each frame. top_file (str): Reference topology for the second trajectory. trj_file (str): Trajetory file from which the frames are picked. out_name (str): Core part of the name of the output files. start_frame (int, optional): Frame from which to start reading the trajectory. """ # Load and select the protein u = mda.Universe(top_file, trj_file) protein = u.select_atoms('all') # Loop over clusters num_clusters = np.max(cluster_idx) + 1 for nr in range(num_clusters): # For each cluster, write the corresponding frames to their new trajectory. with mda.Writer(out_name + "_c" + str(nr) + ".xtc", protein.n_atoms) as W: for ts in u.trajectory: if ts.frame >= start_frame and cluster_idx[ts.frame - start_frame] == nr: W.write(protein) return
def write_pdb_frame(traj_file, pdb_file, frame_number, output_pdb): mda_traj = mda.Universe(pdb_file, traj_file) mda_traj.trajectory[frame_number] mda_traj.atoms.translate(-mda_traj.atoms.center_of_mass()) PDB = mda.Writer(output_pdb) PDB.write(mda_traj.atoms) return output_pdb
def build_trajectories(folder, sequences, fmt='xtc'): """ A scenario is given as a series of time sequences. The result is returned as a list of time and origin of the frame. Each element of that result list correspond to a frame of the conatenated trajectory, The first element of each tuple is the time it corresponds to, the second element is the index of the input the frame comes from. See original gist and PR 1728 for comparison with gromacs trjcat. https://gist.github.com/jbarnoud/cacd0957d3df01d1577f640b20e86039 """ template = 'trjcat_test_{{}}.{}'.format(fmt) template = os.path.join(folder, template) # Use an empty universe to have a topology utop = mda.Universe.empty(1, trajectory=True) # Create synthetic trajectories. The times come from the user input, # the coordinates indicate the index of the input. fnames = [] for index, subseq in enumerate(sequences): coords = np.zeros((len(subseq), 1, 3), dtype=np.float32) + index u = mda.Universe(utop._topology, coords) out_traj = mda.Writer(template.format(index), n_atoms=len(u.atoms)) fnames.append(out_traj.filename) with out_traj: for ts, time in zip(u.trajectory, subseq): # The time step needs a box to avoid a warning ts.dimensions = [10, 10, 10, 90, 90, 90] # The time is set from the user input ts.time = time out_traj.write(ts) return utop, fnames
def traj_pare(traj, out="path.dcd", top="step5_assembly.psf", select="all", b=0, e=None, skip=10): u = mda.Universe(top, traj) system = u.select_atoms(select) with mda.Writer(out, system.n_atoms) as W: for i, ts in enumerate(u.trajectory[b:e:skip]): W.write(system) print("Frame #" + str(i+1))
def extract_coordinates_combined(ref, trj, sel_string, out_name, start_frame=0): """ Extracts selected coordinates from several trajectory files. Args: ref (list of str): File names for the reference topologies. Can read all MDAnalysis-compatible topology formats. trj (list of str): File names for the input trajectories. Can read all MDAnalysis-compatible trajectory formats. out_name (str): Core of the file names for the output files. start_frame (int, optional): First frame to read from the trajectory. """ # Determine the number of atoms from the first trajectory u = mda.Universe(ref[0], trj[0]) selection = u.select_atoms(sel_string[0]) num_at = selection.n_atoms # Go through trajectories and write selections with mda.Writer(out_name+'.xtc', num_at) as W: for r, t, s in zip(ref, trj, sel_string): print(r, t) print(s) u = mda.Universe(r, t) selection = u.select_atoms(s) for ts in u.trajectory[start_frame:]: W.write(selection) return
def convert_pdbs_to_traj(pdbdir:list, selection='all', outfile=None) -> None: """Convert multiple pdb files to a single trajectory file. Args: pdbdir (list): A floder contains pdb files. selection (str, optional): [description]. Defaults to 'all'. outfile ([type], optional): [description]. Defaults to None. """ fileformat = '.pdb' pdbfiles = sorted([os.path.join(pdbdir, pdbfile) for pdbfile in os.listdir(pdbdir) if pdbfile.endswith(fileformat)]) u = mda.Universe(pdbfiles[0], pdbfiles) SelGroup = u.select_atoms(selection) pbar = tqdm(pdbfiles) pdbNum = len(pdbfiles) if outfile is None: pdbname = pdbdir.rstrip(os.path.sep).split(os.path.sep)[-1] print(pdbname) outfile = '%s_%d.xtc'%(pdbname, pdbNum) outTopfile = outfile[:-4] + '.pdb' SelGroup.write(outTopfile) with mda.Writer(outfile, SelGroup.n_atoms) as W: for i, pdbfile in enumerate(pbar): pbar.set_description('Processing: %s'%pdbfile) W.write(SelGroup) print("Output trajectory file: %s"%os.path.abspath(outfile)) print("Output topology file: %s"%os.path.abspath(outTopfile))
def test_write_istart(universe_dcd, tmpdir, fstart): outfile = str(tmpdir.join('test.dcd')) nsavc = universe_dcd.trajectory._file.header['nsavc'] istart = fstart * nsavc if fstart is not None else None with mda.Writer(outfile, universe_dcd.atoms.n_atoms, nsavc=nsavc, istart=istart) as w: for ts in universe_dcd.trajectory: w.write(universe_dcd.atoms) u = mda.Universe(PSF, outfile) assert_almost_equal( u.trajectory._file.header['istart'], istart if istart is not None else u.trajectory._file.header['nsavc']) # issue #1819 times = [ts.time for ts in u.trajectory] fstart = fstart if fstart is not None else 1 ref_times = (np.arange(universe_dcd.trajectory.n_frames) + fstart) * universe_dcd.trajectory.dt assert_almost_equal( times, ref_times, decimal=5, err_msg="Times not identical after setting istart={}".format(istart))
def extract_coordinates(ref, pdb, trj_list, out_name, sel_string, start_frame=0): """ Extracts selected coordinates from a trajectory file. Args: ref (str): File name for reference topology. Can read all MDAnalysis-compatible topology formats. pdb (str): File name for the reference PDB file. trj_list (list of str): File names for the input trajectory. Can read all MDAnalysis-compatible trajectory formats. out_name (str): Core of the file names for the output files. start_frame (int, optional): First frame to read from the trajectory. """ # Read the reference+PDB files and extract selected parts. u = mda.Universe(ref,pdb) selection = u.select_atoms(sel_string) selection.write(out_name+'.pdb') selection.write(out_name+'.gro') # Read the trajectories and extract selected parts. with mda.Writer(out_name+'.xtc', selection.n_atoms) as W: for trj in trj_list: u = mda.Universe(ref,trj) selection = u.select_atoms(sel_string) for ts in u.trajectory[start_frame:]: W.write(selection) return
def write_trj_subset(filename, trj, top, selstring, interval, wd): """Write a subset of a full trajectory containing only a subset of frames given as an interval. Can be used to write a trajectory or single frames. """ # reset the distributed.worker logger logger = reset_worker_logger() # make sure that the directory exists. If it does not, create it. os.makedirs(wd, exist_ok=True) # create a new Universe from the topology and the trajectory u = mda.Universe(top, trj) # select the atoms atoms = u.select_atoms(selstring) # get the trajectory trajectory = u.trajectory # set the file path filepath = os.path.join(wd, filename) # create the writer with mda.Writer(filepath, atoms.n_atoms) as Wset: # for each portion of the interval for start, end in interval: # for each frame in the trajectory for ts in trajectory[start:end]: Wset.write(atoms) # inform the user about the file written logger.debug(f"Trajectory {filepath} written. " \ f"Frames: {start}-{end}.") # return the file path return filepath
def test_check_MODEL_multiframe(self, universe2, outfile, startframe, maxframes): """Check whether MODEL number is in the right column (Issue #1950)""" u = universe2 protein = u.select_atoms("protein and name CA") with mda.Writer(outfile, multiframe=True, start=startframe) as pdb: for ts in u.trajectory[:maxframes]: pdb.write(protein) def get_MODEL_lines(filename): with open(filename) as pdb: for line in pdb: if line.startswith("MODEL"): yield line MODEL_lines = list(get_MODEL_lines(outfile)) assert len(MODEL_lines) == maxframes for model, line in enumerate(MODEL_lines, start=startframe + 1): # test that only the right-most 4 digits are stored (rest must be space) # line[10:14] == '9999' or ' 1' # test appearance with white space assert line[5:14] == "{0:>9d}".format(int(str(model)[-4:])) # test number (only last 4 digits) assert int(line[10:14]) == model % 10000
def write_xyz(selection: str, unv: mda.Universe, frame: int): group = universe.select_atoms('resid ' + selection) path = selection + '_' + str(frame) + '.xyz' with mda.Writer(path, group.n_atoms) as coords: coords.write(group) return path
def get_sasa(self, dssp_loc="/usr/bin/mkdssp", skip=None): self.dssp_loc = dssp_loc SASA = {} protein = self.universe.selectAtoms("protein") for ts in self.universe.trajectory: if skip: self.universe.trajectory.skip = skip sys.stdout.flush() sys.stdout.write('\rsasa [step {0}] '.format( self.universe.trajectory.frame)) writer = MDAnalysis.Writer("tmp.pdb") writer.write(protein) writer.close() parser = bp.PDBParser() structure = parser.get_structure('tmp', 'tmp.pdb') dssp = bp.DSSP(structure[0], 'tmp.pdb', self.dssp_loc) for key in dssp.keys(): resnum = dssp[key][0] sasa = dssp[key][2] if resnum.id[1] in SASA: SASA[resnum.id[1]].append(sasa) else: SASA[resnum.id[1]] = [sasa] count = 0 fp = open(self.out_path + self.out_file + "_sasa.dat", 'w') for key in SASA: fp.write("{0}\t{1}\t{2}\t{3}\n".format(protein.resnames()[count], key, np.mean(SASA[key]), np.std(SASA[key], ddof=1))) count += 1 fp.close() sys.stdout.write('\rSASA table created ') print
def convert_traj(args): u = mda.Universe(args.coords, args.traj) if args.output is None: args.output = args.traj.rsplit(".")[0] + ".pdb" sel = mda.AtomGroup(u.atoms) # so we can use sel.n_atoms later if args.nodrudes: sel = sel.select_atoms("not name D*") if args.novirtuals: sel = sel.select_atoms("not name V*") if args.sel: sel = sel.select_atoms(args.sel) if args.start and args.end: frames = u.trajectory[args.start:args.end] elif args.start: frames = u.trajectory[args.start:] elif args.end: frames = u.trajectory[:args.end] else: frames = u.trajectory with mda.Writer(args.output, sel.n_atoms) as f: for _ in completion(frames): f.write(sel)
def cat(direction="i2occ2o", trjdir=".", psf="2jln_r10_g470_c22.psf", n_trj=100, select="protein"): out = "full_dims_mhp1_" + direction + ".dcd" trj_name_list = [] dcd_basename = "dims_mhp1_" + direction # Append all trajectory (filesnames) to complete list of parts for i in xrange(n_trj): print "writing out frames for trajectory %i" % (i + 1) # trj_name = dcd_basename + str(i+1) + ".dcd" trj_name = '{}/{}_{:n}.dcd'.format(trjdir, dcd_basename, i + 1) # trj_name = '%s_%i.dcd' % (dcd_basename, i+1) if not os.path.exists(trj_name) or os.path.getsize(trj_name) == 0: break trj_name_list.append(trj_name) # Use MDAnalysis (ChainReader) to read in constructed list of trajectory parts mhp1 = mda.Universe(psf, trj_name_list) ag = mhp1.select_atoms(select) with mda.Writer(trjdir + out, ag.n_atoms) as W: for ts in mhp1.trajectory: W.write(ag)
def subsample(psf_file, xtc_file, out_file): u = mda.Universe(psf_file, xtc_file) print(len(u.trajectory)) a = u.select_atoms('all') with mda.Writer(out_file, a.n_atoms) as W: for ts in u.trajectory[400:500:10]: W.write(a) return
def Dcd2Xtc(psf, dcd): psffile = psf dcdfile = dcd xtcfile = dcdfile[0:-4] + '.xtc' u = mda.Universe(psffile, dcdfile) with mda.Writer(xtcfile, u.atoms.n_atoms) as w: for ts in tqdm(u.trajectory, desc=f'{xtcfile}', leave=False): w.write(u)
def build_trr(trr="./rotamer1_R1A_298K_2011.trr"): L = MDAnalysis.Universe("./rotamer1_R1A_298K_2011.pdb", "./rotamer1_R1A_298K_2011.dcd") pop = np.loadtxt("R1A_298K_populations_2011.dat") with MDAnalysis.Writer(trr, L.atoms.n_atoms) as W: for ts, p in izip(L.trajectory, pop): ts.lmbda = p # add lambda for TRR W.write(ts) return trr
def test_writer_resid_false(self, u, tmpdir): with tmpdir.as_cwd(): with mda.Writer('temp.gro', reindex=False) as w: w.write(u.atoms) with open('temp.gro', 'r') as grofile: grofile.readline() grofile.readline() line = grofile.readline() assert line.startswith(' 2CL CL 3')
def generate_top_from_trj(u, select='all', outname=None, ext='pdb', frame=0): selection = u.select_atoms(select) if outname: u.trajectory[frame] # Start writing from first frame if type(outname) is not str: base, ext = os.path.splitext(u.trajectory.filename) outname = base + '_' + select + '.' + topext with mda.Writer(outname, selection.n_atoms) as W: W.write(selection)
def main(argdict): """ Main function for entry point checking. Expects a dictionary of command line arguments. """ # find all CSV files in with given basename in this directory: csv_files = glob.glob(args.basename + "_*.csv") # load all csv files: csv_data = [ pd.read_csv(csv_file, delimiter="\t") for csv_file in csv_files ] # concatenate into single data frame: csv_data = pd.concat(csv_data) # sort by second column (should contain time stamps): csv_data = csv_data.sort_values('Time (ps)') # write to overall CSV file: csv_data.to_csv(args.basename + ".csv", sep="\t", index=False) # find all DCD files in with given basename in this directory: dcd_files = glob.glob(args.basename + "_*.dcd") # sort DCD files by restart number: restart_number = [ int(dcd_file.split("_")[-1].split(".dcd")[0]) for dcd_file in dcd_files ] sorted_dcd_files = [x for _, x in sorted(zip(restart_number, dcd_files))] # load trajectories into an MDAnalsysis universe: u = mda.Universe(args.basename + ".pdb", sorted_dcd_files) # select all particles: system = u.select_atoms("all") # write to XTC trajectory: prev_time = u.trajectory.time - u.trajectory.dt with mda.Writer(args.basename + ".xtc", system.n_atoms) as w: for ts in u.trajectory: print(ts) # advance time step: ts.time = prev_time + ts.dt # check for blowups: if (not min(ts.positions.flatten()) <= -1000 and not max(ts.positions.flatten()) >= 9999.999): w.write(system) else: warnings.warn("Possible blow up, skipping this frame.") # advance time step: prev_time = ts.time