def shrink(xtc,gro,simname): if os.path.exists(simname + '.xtc'): print simname + '_ai.xtc', "exists" else: print "writing", simname + '.xtc' traj = md.load_xtc(xtc,top=gro) traj = pt.Trajectory(xyz=m_traj.xyz.astype('f8'), top=topology_name) traj.autoimage() traj.save(simname + '_ai.xtc') traj_pt = [] traj = md.load_xtc(simname + '_ai.xtc',top=gro) exit() with md.formats.XTCTrajectoryFile(simname + '.xtc', 'w') as f: for fr in range(50000)[::100]: print "frame", fr structure = traj[fr] peptide = structure.atom_slice(structure.topology.select('protein'))[0] peptide.center_coordinates() f.write(peptide.xyz*10) peptide.save(simname + '.pdb') return None
def main(): inp, topf, sel, outn, dt = parse_args() if os.path.isdir(inp): xtcfs = sorted([os.path.join(inp, _) for _ in os.listdir(inp)]) if topf is None: print("Eorror: top file is required!") exit(0) else: meta = pd.read_pickle(inp) topf = meta["top_fn"].values[0] xtcfs = meta["traj_fn"].values top = md.load_pdb(topf) ndx = top.top.select(sel) top = top.atom_slice(ndx) xtcall = None for xtcf in xtcfs: xtc = md.load_xtc(xtcf, top=topf, atom_indices=ndx, stride=dt) if xtcall is None: xtcall = xtc else: xtc.topology = xtcall.topology xtcall = xtcall.join(xtc) outtop = "%s.pdb" % outn outdcd = "%s.dcd" % outn top.save_pdb(outtop) xtcall.superpose(top) xtcall.save_dcd(outdcd)
def lipic(traj_file, top_file, stride, toAl): # load trajectory traj = md.load_xtc(traj_file, top=top_file, stride=stride) # align if needed if toAl: # align to the first frame traj.superpose(traj[0]) # get average structure avg_xyz = traj.xyz.astype(np.float64).\ mean(axis = 0, dtype=np.float64) avg_traj = md.Trajectory([avg_xyz], traj.top) # second step alignment to the average structure traj.superpose(avg_traj[0]) # calculate average structure avg_xyz = traj.xyz.astype(np.float64).\ mean(axis = 0, dtype=np.float64) avg_traj = md.Trajectory([avg_xyz], traj.top) # add average structure to the trajectory traj = traj.join(avg_traj) return traj
def load_xtc(self): """ :param xtc_file: Trajectory in xtc file :param top_file: Topology file of such trajectory :return: trajectory loaded """ return md.load_xtc(self.xtc_file, self.top_file)
def main(): args = parse_args() #We read some input trj=md.load_xtc(args.trj, top=args.pdb) #We load the trj #Here we can split the trajectory according to some analysis paDG, Emol=PADG(trj, args.top) #We calc the avere contribution of every atom to the DG* and the molecular DG* write_res(trj[0], paDG) #We write the res files in pdb print 'DG*_np = ',Emol[0] #and we print the molecular DG* components print 'DG*_p = ',Emol[1] print 'DG*_tot= ',Emol[2]
def assign_(xtcf, cf, top, sel, dt=1, outfname=None): if not outfname: outfname = os.path.split(xtcf)[1].split(".")[0] top = md.load_pdb(top) ndx = top.top.select(sel) centerpdbs = md.load(cf, top=top, atom_indices=ndx) xtc = md.load_xtc(xtcf, top=top, atom_indices=ndx, stride=dt) seq, iter0 = libdistance.assign_nearest(xtc, centerpdbs, metric="rmsd") np.savez_compressed(outfname + '.npz', a=seq) return seq
def main(): print("start cal.") inp, topf, reff, sel, oup = parse_args() T = 4 if inp.endswith(".pdb"): pdb = md.load_pdb(inp) dCNH = func([pdb, inp, reff, sel]) print(dCNH) elif inp.endswith(".xtc"): xtc = md.load_xtc(inp, top=topf) dCNH = func([xtc, topf, reff, sel]) print(dCNH) np.savetxt(oup, dCNH.T, fmt="%.4f", delimiter=',') elif inp.startswith("meta"): meta = pd.read_pickle(inp) topfn = meta["top_fn"].iloc[0] files = list(meta['traj_fn']) args = [(fi, topfn, reff, sel) for fi in files] pool = Pool(T) dCNHs = pool.map(func, args) if not os.path.exists(oup): os.mkdir(oup) names = [] for ref in reff: name = os.path.split(ref)[-1] names.append(name) title = ",".join(names) for dCNH, arg in zip(dCNHs, args): fi = arg[0] fname = os.path.split(fi)[-1] + ".txt" fname = os.path.join(oup, fname) np.savetxt(fname, dCNH.T, fmt="%.4f", delimiter=',', header=title) elif os.path.isdir(inp): files = [ os.path.join(inp, _) for _ in os.listdir(inp) if _.endswith(".xtc") ] args = [(fi, topf, reff, sel) for fi in files] pool = Pool(T) #func(args[0]) #return dCNHs = pool.map(func, args) if not os.path.exists(oup): os.mkdir(oup) names = [] for ref in reff: name = os.path.split(ref)[-1] names.append(name) title = ",".join(names) for dCNH, arg in zip(dCNHs, args): fi = arg[0] fname = os.path.split(fi)[-1] + ".txt" fname = os.path.join(oup, fname) np.savetxt(fname, dCNH.T, fmt="%.4f", delimiter=',', header=title) else: print("Error: check the input file.")
def Ana_xtc(name_itp, name_gro, name_xtc, option, rang, skip, strd): df_noes = Read_itp(name_itp, name_gro) traj = md.load_xtc(name_xtc, name_gro, strd) top = traj.top nb_noes = list(set(df_noes.Ind)) for i in range(0, len(nb_noes)): couples = [] for row in df_noes[df_noes.Ind == i].itertuples(): couples.append([row.Atom1, row.Atom2]) dist = md.compute_distances(traj, couples) if option == 1: moy = np.mean(dist) * 10 elif option == 2: moy = 0 cpt = 0 for d1 in dist: for d2 in d1: moy = moy + (1 / math.pow(d2, 6)) cpt += 1 moy = moy / cpt moy = (1 / (moy**(1 / 6))) * 10 elif option == 3: moy_r3 = [] for couple in range(0, len(dist[0])): moy = 0 for frame in dist[skip::]: moy = moy + (1 / math.pow(frame[couple], 3)) moy = moy / len(dist[skip::]) moy = (1 / (moy**(1 / 3))) * 10 moy_r3.append(moy) moy = 0 for moy3 in moy_r3: moy = moy + (1 / math.pow(moy3, 6)) moy = 1 / (moy**(1 / 6)) moy = round(moy, 2) df_noes.loc[df_noes.Ind == i, 'Distance'] = moy for row in df_noes.itertuples(): lower = row.Dist_RMN - (row.Dist_RMN * rang) / 100 upper = row.Dist_RMN + (row.Dist_RMN * rang) / 100 if (row.Distance < upper): df_noes.loc[df_noes.Ind == row.Ind, 'Violation'] = 'Res' return df_noes
def cal_rmsdmatrix(xtcf0, xtcf1, top, sel, dt=1, outfname=None): if not outfname: fname0, fname1 = os.path.split(xtcf0)[1].split(".")[0], os.path.split( xtcf1)[1].split(".")[0] outfname = "%s_%s" % (fname0, fname1) top = md.load_pdb(top) ndx = top.top.select(sel) xtc0 = md.load_xtc(xtcf0, top=top, atom_indices=ndx, stride=dt) if xtcf0 == xtcf1: xtc1 = xtc0 else: xtc1 = md.load_xtc(xtcf1, top=top, atom_indices=ndx, stride=dt) rmsd = libdistance.cdist(xtc0, xtc1, metric="rmsd") rmsd = np.triu(rmsd) #np.save(outfname+".npy", rmsd) #from scipy import sparse #b = sparse.csr_matrix(rmsd) #sparse.save_npz('b_compressed.npz', b, True) x, y, v = maxmatrix(rmsd) np.savez_compressed(outfname + '.npz', a=rmsd, b=np.array([x, y, v])) return rmsd, v, x, y
def load_traj_and_indices(path, traj="step7_analysis.xtc", pdb="step7_analysis.pdb", index="step7_analysis.ndx"): ''' Convenience function for loading in mdtraj object Params: path - string with path to directory Returns: tuple of (mdtraj trajectory, index dictionary) ''' return md.load_xtc(path + traj, top=path + pdb), load_gromacs_index(path + index)
def func(arg): xtcf, topf, reffs, p, sel = arg toppdb = md.load_pdb(topf) atm0 = toppdb.top.select(sel) xtc = md.load_xtc(xtcf, topf) xtc = xtc.atom_slice(atm0) rmsd = [] for reff in reffs: refpdb = md.load_pdb(reff) atm1 = refpdb.top.select(sel) ref = refpdb.atom_slice(atm1) rmsd.append(md.rmsd(xtc, ref)) rmsd.append([p] * len(xtc)) return np.array(rmsd).T
def get_structures_from_dataframe(df, traj_col="traj"): if os.path.exists("selected_poses"): shutil.rmtree("selected_poses") os.mkdir("selected_poses") for i, row in df.iterrows(): traj = row[traj_col] top_path = str(os.path.join("/".join(traj.split("/")[:-2]), "topologies/topology_0.pdb")) struct = md.load_xtc(traj, top=top_path) snap = row["numberOfAcceptedPeleSteps"] pdb_name = "{}_epoch_{}_traj_{}_model_{}.pdb".format(i, row["epoch"], row["Processor"], snap) struct[snap].save_pdb(os.path.join("selected_poses", pdb_name)) print("PDB file stored in {}".format(os.path.join("selected_poses", pdb_name)))
def read_MD(MDfile, ref, slice_idx, XTC=False): if XTC: trj = md.load_xtc(MDfile, ref.top) else: trj = md.load_pdb(MDfile) ref = ref.atom_slice(slice_idx) trj = trj.atom_slice(slice_idx) trj = trj.superpose(ref) xyzfile = trj.xyz n_atoms = trj.n_atoms n_frames = trj.n_frames trj = np.reshape(xyzfile, (n_frames, n_atoms * 3)) return pd.DataFrame(trj)
def p_extract_ligand_coords(lig_resname, topology_path, traj_path): try: traj = md.load_xtc(str(traj_path), top=str(topology_path)) except OSError: print(' - Warning: problems loading trajectory ' '{}, it will be ignored'.format(traj_path)) return {} lig = traj.topology.select('resname {}'.format(lig_resname)) lig_coords = traj.atom_slice(lig).xyz reshaped_lig_coords = [] for chunk in lig_coords: reshaped_lig_coords.append(np.concatenate(chunk)) return reshaped_lig_coords
def find_hbonds_in_trajectory( lig_resname: str, distance: float, angle: float, pseudo: bool, topology_path: Path, chain_ids: List[str], report_name: str, include_rejected_steps: bool, traj_path: Path ) -> Tuple[List[List[HBondLinker]], List[int], List[int], Set[md.core.topology.Atom], Set[md.core.topology.Atom]]: try: traj = md.load_xtc(str(traj_path), top=str(topology_path)) except OSError: print(' - Warning: problems loading trajectory ' '{}, it will be ignored'.format(traj_path)) # Return empty data structures return list(), list(), list(), set(), set() lig = traj.topology.select('resname {}'.format(lig_resname)) hbonds_in_traj, donors, acceptors = find_ligand_hbonds( traj, lig, distance, angle, pseudo, chain_ids) # Recover corresponding report num = int(''.join(filter(str.isdigit, traj_path.name))) path = traj_path.parent report_path = path.joinpath(report_name + '_{}'.format(num)) metrics = extract_metrics((report_path, ), (2, 3))[0] total_steps = [] accepted_steps = [] for m in metrics: total_steps.append(int(m[0])) accepted_steps.append(int(m[1])) try: if (include_rejected_steps): hbonds_in_traj = account_for_ignored_hbonds( hbonds_in_traj, accepted_steps) else: if (len(hbonds_in_traj) != len(total_steps) or len(hbonds_in_traj) != len(accepted_steps)): raise IndexError except IndexError: print(' - Warning: inconsistent number of models found in ' + 'trajectory {} from {}, '.format(num, path) + 'this trajectory will be ignored') # Return empty data structures return list(), list(), list(), set(), set() return hbonds_in_traj, total_steps, accepted_steps, donors, acceptors
def basic_force_test(): spring_constant = 1000 # kj/ mol nm^2 # load trajectory and reference positions prefix = './files/test_basic_force_calculation/' ref_pdb = md.load(prefix + 'dummy_ref.pdb') dummy_traj = md.load_xtc(prefix + 'dummy_coords.xtc', top=prefix + 'dummy_firstframe.pdb') # get trajectory info n_frames = dummy_traj.xyz.shape[0] ref_dims = ref_pdb.unitcell_lengths.flatten() traj_dims = dummy_traj.unitcell_lengths # do frame matching, scaling, and force calculation ref_xyz = force_analysis.multiply_coordinate_frame(ref_pdb.xyz, n_frames) # match size of trajectory ref_xyz = force_analysis.scale_box_coordinates(ref_xyz, traj_dims, ref_dims) # scale reference dimensions forces = force_analysis.calc_posres_forces(dummy_traj.xyz, ref_xyz, spring_constant) print("upper force average = {}".format(forces[:, 0:400, :].mean(axis=0).mean(axis=0))) print("lower force average = {}".format(forces[:, 400:, :].mean(axis=0).mean(axis=0)))
def lipic(traj_file, top_file, stride): traj = md.load_xtc(traj_file, top=top_file, stride=stride) traj = traj.superpose(traj[0]) traj.unitcell_vectors = None avg_xyz = traj.xyz.astype(np.float64) avg_xyz = avg_xyz.mean(axis=0, dtype=np.float64) avg_traj = md.Trajectory([avg_xyz], traj.top) new_traj = md.Trajectory( traj[0].xyz.astype(np.float64) - md.compute_center_of_mass(traj[0]).astype(np.float64), traj[0].top) for i in range(1, len(traj)): new_traj = new_traj.join( md.Trajectory( traj[i].xyz.astype(np.float64) - md.compute_center_of_mass(traj[i]).astype(np.float64), traj[i].top)) new_traj = new_traj.superpose(avg_traj) new_traj = new_traj.join(avg_traj) print(new_traj.xyz.shape) return new_traj
def main(): xtc_p, top, PH, Tem, shiftx1 = parse_arg() if shiftx1: shiftx1 = "-x" else: shiftx1 = "" if xtc_p.endswith('.pdb'): xtcs = md.load_pdb(xtc_p) else: xtcs = md.load_xtc(xtc_p, top) pwd = os.getcwd() shift_temp = "shift_temp" if not os.path.exists(shift_temp): os.mkdir(shift_temp) rs_p = xtc_p.split('/')[-1] temp = rs_p[:-4] rs_p = rs_p + '.cs' if not os.path.exists(temp): os.mkdir(temp) os.chdir(temp) for i in range(len(xtcs)): xtcs[i].save_pdb("%d.pdb" % i) tempf = tempfile.mkdtemp() cmd = "shiftx2.py -a BACKBONE -b '*.pdb' -f TABULAR -p %f -t %d -z %s %s" % ( PH, Tem, tempf, shiftx1) os.system(cmd) data_avg = average() #print(data_avg[:3]) data_avg.to_csv(rs_p, na_rep='****', float_format="%.2f", index=False, sep=" ") os.system("mv %s %s" % (rs_p, os.path.join(pwd, shift_temp))) os.chdir(pwd) os.system("rm -rf %s" % temp)
if (value > slice_start and value <= slice_start + slice_range) ]) slice_start += slice_range return hist ######################################MAIN##################################### print(""" PROJET DE MODELISATION n°8 Diffussion des lipides au sein d'une membrane Par Anatole et Pierre """) print("Chargement de la trajectoire des lipides...") # Stockage de la trajectoire dans une variable trajectory = md.load_xtc("E:/MODELISATION/input/md_200ns_OK.xtc", top="E:\MODELISATION\input\start.pdb") print(trajectory) print("Chargement de la topologie a partir de la trajectoire...") # Stockage de la topologie topology = trajectory.topology print(topology) # Test de liste de distance pour 1 atome et 1 fenetre: #distances_list = get_distances_list(trajectory, 50, 61) #print(distances_list) # Test liste de distances pour 1 fenetre mais pour tous les atomes de phosphate #atoms_dict = get_atoms_dict(trajectory, topology, 50) #print(atoms_dict) # Initialisation de la liste des intervalles de fenetres
def load_traj(traj_file, top_file): traj = md.load_xtc(traj_file, top_file) return traj
Created on Tue Jun 23 11:26:36 2020 @author: semccomas """ import mdtraj as md import numpy as np from sklearn.preprocessing import MinMaxScaler scalar = MinMaxScaler() ### from old demystifying where I had all sims together #traj = md.load_xtc('../input_f/protein_only/clipped/GLUT5_all.clipped.xtc', top='../input_f/protein_only/clipped/GLUT5_in.clipped.protein.start.pdb') ### now here is the new demystifying where I just have in and out open (9 traj = md.load_xtc( '../input_f/protein_only/clipped/GLUT5_in_GLUT5_out_ONLY.protein.clipped.xtc', top='../input_f/protein_only/clipped/GLUT5_in.clipped.protein.start.pdb') top = traj.topology contacts = md.compute_contacts(traj, scheme='ca') ## since indexing will be off with clipped trajectory, we need to relate the resids to their index number ## so we loop through these to make a dict, which we can reassign to the contacts output, so we have correct ## residue numbers resid_dict = {} for resid in top.residues: print(resid.resSeq, resid.index) # == resid # resid_dict[resid.index] = resid.resSeq feature_to_resid = contacts[1] feature_to_resid = np.vectorize(resid_dict.get)( feature_to_resid) #take the value for each of these keys
peptide_data['atom2'] = np.array( [int(line.split('\t')[1]) for line in lines] ) # Make sure that the pair_number tags start at 0 peptide_data['pair_number'] = np.array( [int(line.split('\t')[7])-1 for line in lines] ) return peptide_data ########## Main ######### #if __name__ == "__main__": if (1): #traj = mdtraj.load('/Users/vv/data/cilengitide_cistrans_remd_2.4us/analysis/Trajectories/trj0.lh5') traj = mdtraj.load_xtc('/Users/vv/data/cilengitide_cistrans_remd_2.4us/remd/traj0.xtc', top='/Users/vv/data/cilengitide_cistrans_remd_2.4us/remd/conf_prod0.pdb') #Load in all the pair data and add to dictionary# peptide_data = read_NOE_data() print "peptide_data['atom1']", peptide_data['atom1'] print "peptide_data['atom1']", peptide_data['atom2'] print "peptide_data['pair_number']", peptide_data['pair_number'] savetxt('atom1.txt', peptide_data['atom1']) savetxt('atom2.txt', peptide_data['atom2']) savetxt('pair_number.txt', peptide_data['pair_number']) min_pairnum = peptide_data['pair_number'].min() max_pairnum = peptide_data['pair_number'].max() all_indices = np.arange(0,peptide_data['pair_number'].shape[0])
parser.add_argument('-d', action = 'store', dest = 'nBlocks', help = 'Number of blocks for block averaging') parser.add_argument('-m', action = 'store', dest = 'model', help = 'Interface') parser.add_argument('-o', action = 'store', dest = 'output', help = 'Interface') args = parser.parse_args() if args.inter == "True" : inter = True else : inter = False nBlocks = int(args.nBlocks) if args.model == "aa" : t = md.load_xtc(args.xtc, top = args.gro) sns = [["C5", "C12"], ["C26", "C33"], ["C47", "C54"]] z_central_TO = get_z_coord(t, "C22", "TO") elif args.model == "cg" : t = md.load(args.xtc, top = args.gro) sns = [["ES1", "D2A"], ["ES2", "D2B"], ["ES3", "D2C"]] #sns = [["ES1", "C4A"], ["ES2", "C4B"], ["ES3", "C4C"]] z_central_TO = get_z_coord(t, "GLY", "TO") elif args.model == "sdk" : t = md.load(args.xtc, top = args.gro) sns = [["ETO1", "C13"], ["ETO2", "C23"], ["ETO3", "C33"]] #sns = [["ESTA", "C13"], ["ESTB", "C23"], ["ESTC", "C33"]] z_central_TO = get_z_coord(t, "GL", "TO") #Only works for POPC and AA model if args.model == "aa" :
def load_xtc(self, filename, **kwargs): return md.load_xtc(filename, top=self.traj.top, **kwargs)
def load_full_trajectory(trajman, md_info, mem_opt=True): # get current path in database db_path = trajman.define_path(md_info) # load reference structure ref_filepath = trajman.find_files(db_path, 'ref.pdb')[0] ref = md.load_pdb(ref_filepath) # convert to float32 to reduce memomry usage if mem_opt: ref.xyz = ref.xyz.astype(np.float32) # get all trajectories sorted by pid traj_info_dict = {} for traj_type in ['nvt', 'npt', 'prod']: # fetch info traj_info_l = trajman.load_info(db_path, '{}*'.format(traj_type)) # sort trajectories by pid ids_srtd = np.argsort( [int(traj_info['pid']) for traj_info in traj_info_l]) traj_info_dict[traj_type] = [traj_info_l[i] for i in ids_srtd] # set order of trajectories traj_info_l = [] # insert NPT/NVT if (len(traj_info_dict['nvt']) == 2) and (len(traj_info_dict['npt']) == 2): traj_info_l += [traj_info_dict['nvt'][0], traj_info_dict['npt'][0]] + [ traj_info_dict['nvt'][1], traj_info_dict['npt'][1] ] else: print("ERROR: NVT and/or NPT missing") # insert productions if len(traj_info_dict['prod']) > 0: traj_info_l += traj_info_dict['prod'] else: print("ERROR: productons missing") # load trajectories traj_l = [] for traj_info in traj_info_l: # get production info traj_type = traj_info['type'] pid = traj_info['pid'] # find trajectory in database prod_filepath_l = trajman.find_files(db_path, '{}{}.xtc'.format(traj_type, pid)) # check that file exists if len(prod_filepath_l) == 1: # get trajectory filepath in the database prod_filepath = prod_filepath_l[0] # debug print # print("Loading {}".format(prod_filepath)) # load trajectory traj_l.append(md.load_xtc(prod_filepath, top=ref.topology)) # hotfix shift time after npt1 because nvt2 starts at zero if '{}{}'.format(traj_info['type'], traj_info['pid']) not in ['nvt1', 'npt1']: traj_l[-1].time += 1000.0 # convert to float32 to reduce memory usage if mem_opt: traj_l[-1].xyz = traj_l[-1].xyz.astype(np.float32) elif len(prod_filepath_l) == 0: # no file found print("ERROR: {} not found".format('{}{}.xtc'.format( traj_type, pid))) else: print("WARNING: too many files {}, selecting first one".format( prod_filepath_l)) # check productions are loaded in the correct order for k in range(len(traj_l) - 1): if traj_l[k].time[-1] > traj_l[k + 1].time[0]: traj_k_name = '{}{}'.format(traj_info_l[k]['type'], traj_info_l[k]['pid']) traj_kp1_name = '{}{}'.format(traj_info_l[k + 1]['type'], traj_info_l[k + 1]['pid']) print("WARNING: time overlap between {} and {}".format( traj_k_name, traj_kp1_name)) return md.join([ref] + traj_l)
F = -kX (kX to keep a particle at a specific distance from reference) Parameters -displacements - n_frames * n_particles * xyz -spring constant - force constant that keeps dummy particles in place. Gromacs units are k=kJ/(mol nm^2) Returns - forces - n_frames * n_particles * xyz - dimensional components of forces ''' return spring_constant * displacements if __name__ == '__main__': # testing prefix = '/home/kevin/hdd/Projects/software_validation/dummy_particles/flat_bilayer/dummy_3.6nm/' top_force = file_io.load_xvg(prefix + 'freeze/data/dummy_top_force.xvg', dims=3) dummy_ref = md.load(prefix + 'pos_res/dummy_ref.pdb') dummy_traj = md.load_xtc(prefix + 'pos_res/dummy_coords.xtc', top=prefix + 'pos_res/dummy_firstframe.pdb') dummy_ref_dims = dummy_ref.unitcell_lengths dummy_traj_dims = dummy_traj.unitcell_lengths a = calc_posres_forces(dummy_traj.xyz, dummy_ref.xyz, 1000, scaling=True, traj_dims=dummy_traj_dims, ref_dims=dummy_ref_dims)
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding: utf-8 -*- # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 """ This example shows how to use pytim classes on trajectories loaded with MDTraj (http://mdtraj.org/) (see also the openmm interoperability) """ import mdtraj import pytim from pytim.datafiles import WATER_GRO, WATER_XTC t = mdtraj.load_xtc(WATER_XTC, top=WATER_GRO) inter = pytim.ITIM(t) for step in t[:]: print("surface atoms: " + repr(inter.atoms.indices))
plt.title(title) plt.show() def set_nans_to_value(heatmaps, value): for heatmap in heatmaps: heatmap[np.isnan(heatmap)] = value if __name__ == "__main__": topdir = '/home/kevin/hdd/Projects/ATP/production/angles_longer/PC_15nm/production/trajectories' pdb_path = os.path.join(topdir, "firstframe.pdb") trajectories = [ md.load_xtc(os.path.join(topdir, "{}degrees.xtc".format(i)), top=pdb_path) for i in range(0, 91, 3) ] lipid_indices = trajectories[0].topology.select("resname POPC") prot_indices = trajectories[0].topology.select("not resname POPC") theta, rho, z = get_cylindrical_coordinates(trajectories, lipid_indices, prot_indices, 50) theta_bins = np.arange(-180, 180) z_bins = np.arange(-35, 35, 0.5) heatmaps = get_heatmaps(theta, rho, z, theta_bins, z_bins) rho_mean = rho.mean() set_nans_to_value(heatmaps, rho_mean)
exit() import mdtraj as md import argparse import numpy as np from sklearn.decomposition import PCA # Name of topology file top_name = argv[1] # Name of trajectory traj_name = argv[2] # Output file name stem out_stem = argv[3] # Import trajectory traj = md.load_xtc(traj_name, top_name) # Define helix indexes print('Computing helix direction') helix_inds = [0, 0, 0, 0, 0, 0, 0, 0] helix_inds[0] = traj.topology.select('residue 13 to 18') # 13 to 18 for holo, 6 to 18 for apo helix_inds[1] = traj.topology.select('residue 29 to 38') helix_inds[2] = traj.topology.select('residue 45 to 54') helix_inds[3] = traj.topology.select('residue 65 to 74') helix_inds[4] = traj.topology.select('residue 83 to 91') helix_inds[5] = traj.topology.select('residue 102 to 111') helix_inds[6] = traj.topology.select('residue 118 to 127') helix_inds[7] = traj.topology.select('residue 139 to 145') helix_parts = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
def initialize(filename, top, mesh, alpha, molecules, nskip, layers): sel = ' or '.join([ 'name ' + m for mol in molecules for m in mol.split(' ') if m[0] != 'H' ]) traj = md.load_xtc(filename, top=top, atom_indices=top.top.select(sel))[nskip:] box = traj.unitcell_lengths.mean(0) hist, edges = np.histogram(traj.xyz[:, :, 2], bins=np.arange(0, box[2], 1).astype(int)) z = edges[:-1] + (edges[1] - edges[0]) / 2. hist = hist / (box[0] * box[1] * traj.n_frames) z_min = np.floor(z[hist > hist.max() / 2].min()).astype(int) z_max = np.ceil(z[hist > hist.max() / 2].max()).astype(int) level = 0.5 * hist[np.logical_and(z > z_min + 2, z < z_max - 2)].mean() edges = np.arange(-1, z_max - z_min + 2, mesh * 0.1) z = edges[:-1] + (edges[1] - edges[0]) / 2. thetaedges = np.arange(0, 181, 2) theta = thetaedges[:-1] + (thetaedges[1] - thetaedges[0]) / 2. grid, spacing, grid_shape = make_grid(box, mesh) data = {} for molecule in molecules: atoms = molecule.split(' ') print(atoms) if len(atoms) == 1: data[atoms[0]] = { 'upper': np.zeros(z.size), 'lower': np.zeros(z.size) } elif len(atoms) == 3: for atom in atoms[::2]: data[atom] = { 'upper': np.zeros(z.size), 'lower': np.zeros(z.size) } data[atoms[1]] = { 'upper': dict(cosine=np.zeros(z.size), conc=np.zeros(z.size), pair=atoms[::2], theta=np.zeros((len(layers) - 1, theta.size))), 'lower': dict(cosine=np.zeros(z.size), conc=np.zeros(z.size), pair=atoms[::2], theta=np.zeros((len(layers) - 1, theta.size))) } elif len(atoms) == 2: data[atoms[0]] = { 'upper': np.zeros(z.size), 'lower': np.zeros(z.size) } data[atoms[1]] = { 'upper': dict(cosine=np.zeros(z.size), conc=np.zeros(z.size), pair=atoms, theta=np.zeros(theta.size), all=np.empty(0), ndang=np.empty(0)), 'lower': dict(cosine=np.zeros(z.size), conc=np.zeros(z.size), pair=atoms, theta=np.zeros(theta.size), all=np.empty(0), ndang=np.empty(0)) } params = { "radius": 3 * alpha, "scale": 2. * alpha**2, "factor": np.power(2. * np.pi * alpha**2, -3 / 2.), "box": box, "mesh": mesh, "bw": mesh * 0.1, "n_frames": traj.n_frames + nskip, "spacing": spacing, "grid": grid, "grid_shape": grid_shape, "edges": edges, "theta": theta, "z": z, "sel": sel, "surface_area": np.empty(0), "surface_zstd": np.empty(0), "level": level, "layers": layers, "data": data } del traj return pd.Series(params)
def run(self, mode='default'): """ Runs and handles all function calls. All data is stored in class object. """ from timeit import default_timer as timer start = timer() global file_ext file_ext = '_raw.npy' global traj_ext traj_ext = self.trajname.split('.')[-1] #---Code can currently be run in two modes: default, and 'score' mode self.mode = mode #---Check to see which calculations need to be run self.check_input() print self.trajname #---Load existing data if self.mode == 'default' and self.OVERWRITE == 'n': self.load_data() elif self.OVERWRITE == 'y': print "OVERWRITING OLD DATA!" #---Print log of tasks left to complete print "calculations left to do:", self.calcs #---Decide if it's necessary to load trajectories/PDBs if len(self.calcs) == 0: LOAD = False else: LOAD = True #---Run the code if self.mode == 'default': #---Set LOAD = False if you just want to post-process existing data if LOAD == True: print "Loading Trajectory" #---Right now the code expects either a list of pdbs (.txt), a .dcd, or a .xtc if traj_ext in ['dcd', 'xtc', 'txt']: #---XTCs and DCDs will be loaded all at once. Extract basic info about structure if traj_ext == 'dcd': traj = md.load_dcd(self.trajname, self.top) self.struc_info(traj[0], len(traj)) elif traj_ext == 'xtc': traj = md.load_xtc(self.trajname, top=self.top) self.struc_info(traj[0], len(traj)) #---Load names of PDBs to be loaded elif traj_ext == 'txt': with open(self.trajname) as t: self.top = t.readline().rstrip() nlines = sum(1 for line in t) self.struc_info(md.load(self.top), nlines) traj = open(self.trajname) #---Only load the necessary frames if self.last_frame != -1: traj = traj[self.first_frame:self.last_frame] #---Frame-by-frame calculations for fr_, struc in enumerate(traj): #---If .txt, then the structures have to be loaded one-by-one if traj_ext == 'txt': struc = md.load(struc.split("\n")[0]) #---Many calculations only require protein coordinates prot = struc.atom_slice( struc.topology.select('protein'))[0] #---Run calculations that only require protein coordinates self.protein_calcs(prot) #---Run calculations requiring protein, lipid, and water coordinates self.membrane_calcs(struc) #---Special case: protein and water coordinates self.diffusion(fr_) #---Calculations done on trajectory all at once self.traj_calcs(traj) #---Write data self.write_data() #---Code can be run in special mode where it references a Rosetta score file and only computes statistics on top N structures elif self.mode == 'score': self.run_score_mode( ) # I copy and pasted the code above without modification. If it doesn't work, move it back. #---Run post-processing functions, i.e. plotting, etc. self.post_process() end = timer() print "Total execution time:", end - start return None