def save(self): "Save the trajs as a n MSMBuilder project" traj_dir = pjoin(self.project_dir, 'Trajectories') if not os.path.exists(traj_dir): os.makedirs(traj_dir) t = Trajectory.load_trajectory_file(self.conf_filename) traj_paths = [] for i, xyz in enumerate(self.trajectories): t['IndexList'] = None # bug in msmbuilder t['XYZList'] = xyz traj_paths.append(pjoin(traj_dir, 'trj%d.lh5' % i)) t.save(traj_paths[-1]) p = Project( { 'conf_filename': os.path.abspath(self.conf_filename), 'traj_lengths': self.n_frames * np.ones(self.n_trajs), 'traj_paths': [os.path.abspath(e) for e in traj_paths], 'traj_converted_from': [[] for i in range(self.n_trajs)], 'traj_errors': [None for i in range(self.n_trajs)], }, project_dir=self.project_dir, validate=True) p.save(pjoin(self.project_dir, 'Project.yaml')) # just check again p = Project.load_from(pjoin(self.project_dir, 'Project.yaml')) p._validate() assert np.all( (p.load_traj(0)['XYZList'] - self.trajectories[0])**2 < 1e-6)
def save(self): "Save the trajs as a n MSMBuilder project" traj_dir = pjoin(self.project_dir, 'Trajectories') if not os.path.exists(traj_dir): os.makedirs(traj_dir) t = Trajectory.load_trajectory_file(self.conf_filename) traj_paths = [] for i, xyz in enumerate(self.trajectories): t['IndexList'] = None # bug in msmbuilder t['XYZList'] = xyz traj_paths.append(pjoin(traj_dir, 'trj%d.lh5' % i)) t.save(traj_paths[-1]) p = Project({'conf_filename': os.path.abspath(self.conf_filename), 'traj_lengths': self.n_frames*np.ones(self.n_trajs), 'traj_paths': [os.path.abspath(e) for e in traj_paths], 'traj_converted_from': [[] for i in range(self.n_trajs)], 'traj_errors': [None for i in range(self.n_trajs)], }, project_dir=self.project_dir, validate=True) p.save(pjoin(self.project_dir,'Project.yaml')) # just check again p = Project.load_from(pjoin(self.project_dir,'Project.yaml')) p._validate() assert np.all((p.load_traj(0)['XYZList'] - self.trajectories[0])**2 < 1e-6)
def run(traj_dir, conf_filename, project_filename, iext): logger.info("Rebuilding project.") file_list = glob.glob(traj_dir + "/trj*%s" % iext) num_traj = len(file_list) traj_lengths = np.zeros(num_traj, 'int') traj_paths = [] if not os.path.exists(conf_filename): raise(IOError("Cannot find conformation file %s" % conf_filename)) file_list = sorted(file_list, key=utils.keynat) for i, filename in enumerate(file_list): traj_lengths[i] = len(md.open(filename)) traj_paths.append(filename) records = { "conf_filename": conf_filename, "traj_lengths": traj_lengths, "traj_paths": traj_paths, "traj_errors": [None for i in xrange(num_traj)], "traj_converted_from": [[] for i in xrange(num_traj)] } p = Project(records) p.save(project_filename) logger.info("Wrote %s" % project_filename)
def run(traj_dir, conf_filename, project_filename): logger.info("Rebuilding project.") file_list = glob.glob(traj_dir + "/trj*.lh5") num_traj = len(file_list) traj_lengths = np.zeros(num_traj,'int') traj_paths = [] file_list = sorted(file_list, key=utils.keynat) for i,filename in enumerate(file_list): traj_lengths[i] = Trajectory.load_trajectory_file(filename,JustInspect=True)[0] traj_paths.append(filename) records = { "conf_filename":conf_filename, "traj_lengths":traj_lengths, "traj_paths":traj_paths, "traj_errors": [None for i in xrange(num_traj)], "traj_converted_from":[[] for i in xrange(num_traj)] } p = Project(records) p.save(project_filename) logger.info("Wrote %s" % project_filename)
parser = arglib.ArgumentParser() parser.add_argument('traj_dir', help='Directory to find trajectory files.') parser.add_argument('conf_fn', help='Conformation filename that has the same atom names and residue IDs, etc. as the trajectories.') parser.add_argument('output', default='./ProjectInfo.yaml', help='Output filename [ ./ProjectInfo.yaml ]') args = parser.parse_args() traj_list = [ os.path.join(args.traj_dir, fn) for fn in os.listdir(args.traj_dir)] traj_list.sort(key=utils.keynat) # = list.sort(traj_list, key=utils.keynat) print traj_list traj_lens = [] for i in xrange(len(traj_list)): print i shape = Trajectory.load_from_hdf(traj_list[i], JustInspect=True) traj_lens.append(shape[0]) records = { 'conf_filename' : args.conf_fn, 'traj_lengths' : traj_lens, 'traj_paths' : traj_list, 'traj_converted_from' : [[] for fn in traj_list], 'traj_errors' : [None for fn in traj_list] } project = Project(records=records) project.save(args.output)
not_too_short_inds = np.where( traj_lens >= ( args.min_length + args.trim_first ) )[0] os.mkdir( os.path.join( args.write_dir, 'Trajectories' ) ) print "Will limit this project to %d trajectories." % len( not_too_short_inds ) for i in xrange( len( not_too_short_inds ) ): print "Copying trajectory %d -> %d (length=%d)" % ( not_too_short_inds[i], i, Proj.traj_lengths[ not_too_short_inds[i] ] - args.trim_first ) trj0 = tables.openFile( Proj.traj_filename( not_too_short_inds[i] ) ) trj1 = tables.openFile( os.path.abspath( os.path.join( args.write_dir, 'Trajectories', '%s%d%s'% ('trj',i, '.lh5' ) ) ), 'w' ) #os.system( 'ln -s %s %s' % ( trj0, trj1 ) ) #os.symlink( trj0, trj1 ) for n0 in trj0.iterNodes('/'): if n0.name != 'XYZList': trj0.copyNode( where='/', name=n0.name, newparent=trj1.root ) else: temp_ary = n0[ args.trim_first : ] io.saveh( trj1, XYZList=temp_ary ) trj0.close() trj1.close() new_records = {'conf_filename': Proj.conf_filename.split('/')[-1], 'traj_lengths': Proj.traj_lengths[ not_too_short_inds ] - args.trim_first, 'traj_paths': Proj._traj_paths[ : len( not_too_short_inds ) ], # This works because they're named relatively and they are re-numbered 'traj_converted_from': Proj._traj_converted_from[ not_too_short_inds ], 'traj_errors': Proj._traj_errors[ not_too_short_inds ] } new_proj_dir = args.write_dir # Copy the trajectories New_Proj = Project( new_records, project_dir = new_proj_dir ) New_Proj.save( os.path.join( args.write_dir, 'ProjectInfo.yaml' ) )