def create_simulation(base_dir, starting_dir, gpu_index,tic_index, plumed_script, sim_save_rate, platform): print("Creating simulation for tic %d"%tic_index) os.chdir((os.path.join(base_dir,"tic_%d"%tic_index))) state, system, integrator, pdb = load_sim_files(starting_dir) with open("./plumed_script.dat",'w') as f: f.writelines(plumed_script) new_f = PlumedForce(str(plumed_script)) force_group = 30 new_f.setForceGroup(force_group) system.addForce(new_f) platform, properties = get_platform(platform,gpu_index) simulation = app.Simulation(pdb.topology, system, integrator, platform, properties) if os.path.isfile("./checkpt.chk"): with open("checkpt.chk",'rb') as f: simulation.context.loadCheckpoint(f.read()) else: simulation.context.setState(state) print("Done creating simulation for tic %d"%tic_index) f = open("./speed_report.txt",'w') backup("trajectory.dcd") simulation.reporters.append(app.DCDReporter('trajectory.dcd', sim_save_rate)) simulation.reporters.append(app.StateDataReporter(f, 1000, step=True,\ potentialEnergy=True, temperature=True, progress=True, remainingTime=True,\ speed=True, totalSteps=200*100, separator='\t')) return simulation, force_group
def create_neutral_simulation(base_dir, starting_dir, gpu_index, sim_save_rate, platform): print("Creating simulation for neutral_replica") os.chdir((os.path.join(base_dir,"neutral_replica"))) state, system, integrator, pdb = load_sim_files(starting_dir) platform, properties = get_platform(platform,gpu_index) simulation = app.Simulation(pdb.topology, system, integrator, platform, properties) if os.path.isfile("./checkpt.chk"): with open("checkpt.chk",'rb') as f: simulation.context.loadCheckpoint(f.read()) else: simulation.context.setState(state) print("Done creating simulation neutral_replica") f = open("./speed_report.txt",'w') backup("trajectory.dcd") simulation.reporters.append(app.DCDReporter('trajectory.dcd', sim_save_rate)) simulation.reporters.append(app.StateDataReporter(f, 1000, step=True,\ potentialEnergy=True, temperature=True, progress=True, remainingTime=True,\ speed=True, totalSteps=200*100, separator='\t')) return simulation
def run_simulation(simulation, n_steps): os.path.chdir(base_dir, "tic_%d" % tic_index) simulation.step(n_steps) backup("state.xml") state=simulation.context.getState(getPositions=True, getVelocities=True,\ getForces=True,getEnergy=True,getParameters=True,enforcePeriodicBox=True) serializeObject(state, 'state.xml') return simulation
def create_simulation(base_dir, tic_index, plumed_script): print("Creating simulation for tic %d" % tic_index) starting_dir = os.path.join(base_dir, "starting_coordinates") os.chdir((os.path.join(base_dir, "tic_%d" % tic_index))) if os.path.isfile("./state.xml"): state = XmlSerializer.deserialize(open("./state.xml").read()) else: state = XmlSerializer.deserialize( open("%s/state0.xml" % starting_dir).read()) system = XmlSerializer.deserialize( open("%s/system.xml" % starting_dir).read()) integrator = XmlSerializer.deserialize( open("%s/integrator.xml" % starting_dir).read()) pdb = app.PDBFile("%s/0.pdb" % starting_dir) new_f = PlumedForce(plumed_script) new_f.setForceGroup(1) system.addForce(new_f) platform = Platform.getPlatformByName("CUDA") properties = {'CudaPrecision': 'mixed', 'CudaDeviceIndex': str(tic_index)} simulation = app.Simulation(pdb.topology, system, integrator, platform, properties) if os.path.isfile("./checkpt.chk"): with open("checkpt.chk", 'rb') as f: simulation.context.loadCheckpoint(f.read()) else: simulation.context.setState(state) print("Done creating simulation for tic %d" % tic_index) f = open("./speed_report.txt", 'w') backup("trajectory.dcd") simulation.reporters.append(app.DCDReporter('trajectory.dcd', 100000)) simulation.reporters.append(app.StateDataReporter(f, 1000, step=True,\ potentialEnergy=True, temperature=True, progress=True, remainingTime=True,\ speed=True, totalSteps=200*100, separator='\t')) return simulation
def sample_clusters(meta, trajs, df): clust_id = df['Trajectory'].unique() for i in clust_id: print(i) df_smp = df.ix[df['Trajectory'] == i, ['Key', 'Frame']].sample(1000) inds = zip(df_smp['Key'], df_smp['Frame']) # Use loc because sample_dimension is nice traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=int(frame_i), top=meta.loc[traj_i]['top_fn']) for traj_i, frame_i in inds) # Save traj_fn = "/Users/robert_arbon/Code/AADH/Analysis/KR_Comparison/pcca_cluster-{}.dcd".format( i) backup(traj_fn) traj.save(traj_fn)
def continue_running(): print(plumed_script) if os.path.isfile("./state.xml"): state = XmlSerializer.deserialize(open("./state.xml").read()) else: state = XmlSerializer.deserialize( open("../../starting_coordinates/state.xml").read()) system = XmlSerializer.deserialize( open("../../starting_coordinates/system.xml").read()) integrator = XmlSerializer.deserialize( open("../../starting_coordinates/integrator.xml").read()) pdb = app.PDBFile("../../starting_coordinates/0.pdb") system.addForce(PlumedForce(plumed_script)) print("here") platform = Platform.getPlatformByName("CUDA") properties = {'CudaPrecision': 'mixed', 'CudaDeviceIndex': str(0)} simulation = app.Simulation(pdb.topology, system, integrator, platform, properties) simulation.context.setState(state) backup("trajectory.dcd") f = open("./speed_report.txt", 'w') simulation.reporters.append(app.DCDReporter('trajectory.dcd', 5000)) simulation.reporters.append(app.StateDataReporter(f, 5000, step=True,\ potentialEnergy=True, temperature=True, progress=True, remainingTime=True,\ speed=True, totalSteps=200*100000, separator='\t')) #run for 23hrs simulation.runForClockTime(3, stateFile="state.xml") backup("state.xml") state=simulation.context.getState(getPositions=True, getVelocities=True,\ getForces=True,getEnergy=True,getParameters=True,enforcePeriodicBox=True) serializeObject(state, 'state.xml')
def sample_tica_dim(dim=0, n_frames=200, meta=None, ttrajs=None): ## Load if (not meta is None) & (not ttrajs is None): ## Sample # These are apparently ordered according tica value inds = sample_dimension(ttrajs, dimension=dim, n_frames=n_frames, scheme='random') save_generic(inds, "tica-dimension-{}-inds.pickl".format(dim + 1)) ## Get tica components tica_values = np.array( [ttrajs[traj_i][frame_i][dim] for traj_i, frame_i in inds]) tica_values = (tica_values - tica_values.min()) / (tica_values.max() - tica_values.min()) tica_values *= 10 ## Make trajectory top = preload_top(meta) # Use loc because sample_dimension is nice traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=top) for traj_i, frame_i in inds) ## Supperpose ## Save traj_fn = "tica-dimension-{}.dcd".format(dim + 1) backup(traj_fn) traj.save(traj_fn) else: raise ValueError('Specify meta data and trajectory objects')
def sample_clusters(): meta = load_meta() tops = preload_tops(meta) print('Sampling trajectories') ref = md.load('topology.pdb') for i in range(int(num_clusters)): print(i) df_smp = df.ix[df['Trajectory']==i, ['Key', 'Time_ps']].sample(100) inds = zip(df_smp['Key'], df_smp['Time_ps']) # Use loc because sample_dimension is nice traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=meta.loc[traj_i]['top_fn']) for traj_i, frame_i in inds ) # Original trajectories include both BT1 and BT2 so need to superpose traj.superpose(reference=ref) # Save traj_fn = "clusters/rmsd_cluster-{}.dcd".format(i) backup(traj_fn) traj.save(traj_fn)
## Try to limit RAM usage def guestimate_stride(): total_data = meta['nframes'].sum() want = kmed.n_clusters * 10 stride = max(1, total_data // want) print("Since we have", total_data, "frames, we're going to stride by", stride, "during fitting, because this is probably adequate for", kmed.n_clusters, "clusters") return stride ## Fit kmed.fit([traj for _, traj in itertrajs(meta, stride=guestimate_stride())]) print(kmed.summarize()) ## Save save_generic(kmed, 'clusterer.pickl') ## Save centroids def frame(traj_i, frame_i): # Note: kmedoids does 0-based, contiguous integers so we use .iloc row = meta.iloc[traj_i] return md.load_frame(row['traj_fn'], frame_i, top=row['top_fn']) centroids = md.join((frame(ti, fi) for ti, fi in kmed.cluster_ids_), check_topology=False) centroids_fn = 'centroids.xtc' backup(centroids_fn) centroids.save("centroids.xtc")
please cite msmbuilder in any publications """ import mdtraj as md import os from msmbuilder.io.sampling import sample_states from msmbuilder.io import load_trajs, save_generic, preload_top, backup, load_generic ## Load meta, ttrajs = load_trajs('ttrajs') kmeans = load_generic("kmeans.pickl") ## Sample inds = sample_states(ttrajs, kmeans.cluster_centers_, k=10) save_generic(inds, "cluster-sample-inds.pickl") ## Make trajectories top = preload_top(meta) out_folder = "cluster_samples" backup(out_folder) os.mkdir(out_folder) for state_i, state_inds in enumerate(inds): traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=top) for traj_i, frame_i in state_inds) traj.save("{}/{}.xtc".format(out_folder, state_i))
please cite msmbuilder in any publications """ import mdtraj as md from msmbuilder.io import load_trajs, save_generic, preload_top, backup, load_generic from msmbuilder.io.sampling import sample_msm ## Load meta, ttrajs = load_trajs('ttrajs') msm = load_generic('msm.pickl') kmeans = load_generic('kmeans.pickl') ## Sample # Warning: make sure ttrajs and kmeans centers have # the same number of dimensions inds = sample_msm(ttrajs, kmeans.cluster_centers_, msm, n_steps=200, stride=1) save_generic(inds, "msm-traj-inds.pickl") ## Make trajectory top = preload_top(meta) traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=top) for traj_i, frame_i in inds) ## Save traj_fn = "msm-traj.xtc" backup(traj_fn) traj.save(traj_fn)
- trajs """ import mdtraj as md from msmbuilder.io import load_trajs, save_generic, preload_top, backup, load_generic from msmbuilder.io.sampling import sample_msm ## Load meta, ttrajs = load_trajs('ttrajs') msm = load_generic('msm.pickl') kmeans = load_generic('kmeans.pickl') ## Sample # Warning: make sure ttrajs and kmeans centers have # the same number of dimensions inds = sample_msm(ttrajs, kmeans.cluster_centers_, msm, n_steps=200, stride=1) save_generic(inds, "msm-traj-inds.pickl") ## Make trajectory top = preload_top(meta) traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=top) for traj_i, frame_i in inds ) ## Save traj_fn = "msm-traj.xtc" backup(traj_fn) traj.save(traj_fn)
def guestimate_stride(): total_data = meta['nframes'].sum() want = kmed.n_clusters * 10 stride = max(1, total_data // want) print("Since we have", total_data, "frames, we're going to stride by", stride, "during fitting, because this is probably adequate for", kmed.n_clusters, "clusters") return stride ## Fit kmed.fit([traj for _, traj in itertrajs(meta, stride=guestimate_stride())]) print(kmed.summarize()) ## Save save_generic(kmed, 'clusterer.pickl') ## Save centroids def frame(traj_i, frame_i): # Note: kmedoids does 0-based, contiguous integers so we use .iloc row = meta.iloc[traj_i] return md.load_frame(row['traj_fn'], frame_i, top=row['top_fn']) centroids = md.join((frame(ti, fi) for ti, fi in kmed.cluster_ids_), check_topology=False) centroids_fn = 'centroids.xtc' backup(centroids_fn) centroids.save("centroids.xtc")
print(args) if len(args.traj) == 1: traj_name = os.path.basename(args.traj[0])[:-3] # drop the .nc ending print('Loading traj...') traj = mdtraj.load(args.traj[0], top=args.top) print('Superposing...') atoms = traj.topology.select('name CA') if args.ref is None: traj.superpose(traj, 0, atom_indices=atoms) else: ref = mdtraj.load(args.ref) traj.superpose(ref, 0, atom_indices=atoms) if args.width > 1: print('Smoothing...') traj.smooth(args.width, inplace=True) backup(args.name) traj.save_netcdf(args.name) elif len(args.traj) > 1: print('Loading {} trajs as one...'.format(len(args.traj))) traj = mdtraj.load(args.traj, top=args.top) print('Superposing...') atoms = traj.topology.select('name CA') if args.ref is None: traj.superpose(traj, 0, atom_indices=atoms) else: ref = mdtraj.load(args.ref) traj.superpose(ref, 0, atom_indices=atoms) if args.width > 1: print('Smoothing...') traj.smooth(args.width, inplace=True) backup(args.name)
import mdtraj as md import os from msmbuilder.io.sampling import sample_states from msmbuilder.io import load_trajs, save_generic, preload_top, backup, load_generic ## Load meta, ttrajs = load_trajs('ttrajs') kmeans = load_generic("kmeans.pickl") ## Sample inds = sample_states(ttrajs, kmeans.cluster_centers_, k=10) save_generic(inds, "cluster-sample-inds.pickl") ## Make trajectories top = preload_top(meta) out_folder = "cluster_samples" backup(out_folder) os.mkdir(out_folder) for state_i, state_inds in enumerate(inds): traj = md.join( md.load_frame(meta.loc[traj_i]['traj_fn'], index=frame_i, top=top) for traj_i, frame_i in state_inds ) traj.save("{}/{}.xtc".format(out_folder, state_i))