def test_ired_simple_for_coverage(): ''' ''' traj = pt.iterload(traj_dir, parm_dir) h_indices = pt.select_atoms('@H', traj.top) n_indices = pt.select_atoms('@H', traj.top) - 1 nh_indices = list(zip(n_indices, h_indices)) vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple') vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple') state_vecs = vecs_and_mat[0] mat_ired = vecs_and_mat[1] # get eigenvalues and eigenvectors modes = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs)) evals, evecs = modes data_0 = _ired( state_vecs, modes=(evals, evecs), NHbond=True, tcorr=10000, tstep=1.) data_1 = _ired(state_vecs, modes=modes, NHbond=True, tcorr=10000, tstep=1) for d0, d1 in zip(data_0, data_1): if d0.dtype not in [ 'modes', ]: aa_eq(d0.values, d1.values) else: # modes # values: tuple aa_eq(d0.values[0], d1.values[0]) aa_eq(d0.values[1], d1.values[1]) # try different dtype out_try_new_dtype = pt.ired_vector_and_matrix( traj, nh_indices, dtype='cpptraj_dataset')
def calcNOEfull(self, tstep=1.0, numtaus=128): print('calculating correlation functions\n') self.getDistances() self.ACFfull = dict() for (r1, a1), (r2, a2) in zip(self.list1, self.list2): label = str(r1) + a1 + ':' + str(r2) + a2 first_in_seq = list(self.traj.top.residues)[0].index idx1 = pt.select_atoms( searchAT[a1] + ' & :' + str(r1 - first_in_seq), self.traj.top) idx2 = pt.select_atoms( searchAT[a2] + ' & :' + str(r2 - first_in_seq), self.traj.top) pairs = list(map(list, product(idx1, idx2))) data_vec = va.vector_mask(self.traj, pairs, dtype='ndarray') self.ACFfull[label] = [] if len(data_vec.shape) < 3: data_vec = [data_vec] #self.dist[label] ? for n, vals in enumerate(data_vec): r = self.dist[label][n] cosines = np.array([ np.dot(unit_vector(vals[0]), unit_vector(v)) for v in vals ]) cosines = 1.5 * cosines**2 - .5 cosines = cosines / ((r * 1e-10)**3) tot = mycorrelate2(cosines, norm=False) self.ACFfull[label].append(tot) del self.traj dump(self.ACFfull, open(get_today() + '-ACFfull.pkl', 'wb')) print('fitting correlation functions\n') self.ACFfull_fit = dict() for label, acfs in self.ACFfull.items(): #print(acfs) acf = np.mean(acfs, axis=0) norm_fact = acf[0] acf /= acf[0] #print(acf) taus, amps, nu1, nu2 = \ smooth_and_fit(acf, tstep=tstep, lp_threshold=0.15, lp_threshold_2=0.05, mintau=2., numtaus=numtaus) self.ACFfull_fit[label] = [taus, np.array(amps) * norm_fact] #print(taus, np.array(amps)*norm_fact) dump(self.ACFfull_fit, open(get_today() + '-ACFfull-fit.pkl', 'wb')) print('calculating NOE rates\n') self.sigma_full = dict() for label, fit in self.ACFfull_fit.items(): taus, amps = fit taus = np.array(taus) * 1e-12 sigma = (mu_0 / (4 * pi))**2 sigma = sigma * gamma**4 * hbar**2 * .1 #sigma = sigma/(self.reff[label]*1e-10)**6 temp = [a * t for a, t in zip(amps, taus)] self.sigma_full[label] = sigma * np.sum(temp) dump(self.sigma_full, open(get_today() + '-sigma-full.pkl', 'wb'))
def test_residue_distance(self): traj = pt.iterload(tz2_trajin, tz2_top) top = traj.top ref = traj[0] top.set_reference(ref) ref.top = top indices_smaler = pt.select_atoms(':3@CA <:5.0', top) ca_indices = pt.select_atoms(':3@CA', traj.top) all_pairs_smaller = list(product(ca_indices, indices_smaler)) pt.tools.flatten(pt.distance(ref, all_pairs_smaller))
def main(): # add filenames parmfile = '../cpptraj/test/Test_IRED/1IEE_A_prot.prmtop' trajfile = '../cpptraj/test/Test_IRED/1IEE_A_test.mdcrd' # load to TrajectoryIterator, first 4000 frames traj = pt.iterload(trajfile, parmfile) # create N-H pairs h_indices = pt.select_atoms(traj.top, '@H') n_indices = pt.select_atoms(traj.top, '@H') - 1 nh_indices = list(zip(n_indices, h_indices)) print(NH_order_paramters(traj, nh_indices, tcorr=2000.))
def test_residue_distance(self): traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7") top = traj.top ref = traj[0] top.set_distance_mask_reference(ref) ref.top = top indices_smaler = pt.select_atoms(':3@CA <:5.0', top) ca_indices = pt.select_atoms(':3@CA', traj.top) all_pairs_smaller = list(product(ca_indices, indices_smaler)) distances = pt.tools.flatten(pt.distance(ref, all_pairs_smaller))
def test_pairwise(self): traj = pt.iterload('data/tz2.nc', 'data/tz2.parm7') distances = pt.pairwise_distance(traj, '@CA', '@CB')[0] ca_indices = pt.select_atoms('@CA', traj.top) cb_indices = pt.select_atoms('@CB', traj.top) known_shape = (traj.n_frames, len(ca_indices), len(cb_indices)) assert known_shape == distances.shape, 'distance array shape' slow_distances = [] for ca_i in ca_indices: for cb_i in cb_indices: slow_distances.append(pt.distance(traj, [ca_i, cb_i])) slow_distances = np.array(slow_distances).T aa_eq(slow_distances.flatten(), distances.flatten())
def test_nh_paramters(self): parmfile = cpptraj_test_dir + '/Test_IRED/1IEE_A_prot.prmtop' trajfile = cpptraj_test_dir + '/Test_IRED/1IEE_A_test.mdcrd' traj = pt.iterload(trajfile, parmfile) h_indices = pt.select_atoms('@H', traj.top) n_indices = h_indices - 1 nh_indices = list(zip(n_indices, h_indices)) # single core orders = pt.NH_order_parameters(traj, nh_indices, tcorr=8000.) saved_S2 = np.loadtxt(cpptraj_test_dir + '/Test_IRED/orderparam.save').T[-1] aa_eq(orders, saved_S2) # multiple core # default 2 orders = pt.pmap(pt.NH_order_parameters, traj, nh_indices, tcorr=8000.) aa_eq(orders, saved_S2) for n_cores in [1, 2, 3, 4, -1]: orders = pt.NH_order_parameters(traj, nh_indices, tcorr=8000., n_cores=n_cores) aa_eq(orders, saved_S2) orders = pt.pmap(pt.NH_order_parameters, traj, nh_indices, tcorr=8000., n_cores=n_cores) aa_eq(orders, saved_S2)
def test_get_velocity(self): traj = pt.iterload( fn('issue807/trunc.nc'), fn("issue807/system.prmtop")) traj[0] # no mask, no frame_indices vels = pt.get_velocity(traj) assert vels.shape == (traj.n_frames, traj.n_atoms, 3), 'vels.shape' # string mask vels = pt.get_velocity(traj, '@O', frame_indices=[0, 2]) fi = traj(frame_indices=[0, 2], mask='@O') assert vels.shape == (fi.n_frames, fi.top.n_atoms, 3), 'vels.shape' # atom indices atm_indices = pt.select_atoms('@O', traj.top) vels_ = pt.get_velocity(traj, atm_indices, frame_indices=[0, 2]) fi = traj(frame_indices=[0, 2], mask='@O') assert vels_.shape == (fi.n_frames, fi.top.n_atoms, 3), 'vels.shape' aa_eq(vels, vels_) # raise if not having velocity traj2 = pt.iterload(fn('tz2.nc'), fn('tz2.parm7')) with pytest.raises(ValueError): pt.get_velocity(traj2)
def test_velocity(self): traj = pt.iterload("./data/issue807/trunc.nc", "data/issue807/system.prmtop") f = traj[0] # no mask, no frame_indices vels = pt.get_velocity(traj) assert vels.shape == (traj.n_frames, traj.n_atoms, 3), 'vels.shape' # string mask vels = pt.get_velocity(traj, '@O', frame_indices=[0, 2]) fi = traj(frame_indices=[0, 2], mask='@O') assert vels.shape == (fi.n_frames, fi.top.n_atoms, 3), 'vels.shape' # atom indices atm_indices = pt.select_atoms('@O', traj.top) vels_ = pt.get_velocity(traj, atm_indices, frame_indices=[0, 2]) fi = traj(frame_indices=[0, 2], mask='@O') assert vels_.shape == (fi.n_frames, fi.top.n_atoms, 3), 'vels.shape' aa_eq(vels, vels_) # raise if not having velocity traj2 = pt.iterload('data/tz2.nc', 'data/tz2.parm7') self.assertRaises(ValueError, lambda: pt.get_velocity(traj2))
def test_ired_need_lapack_cpptraj(self): state = pt.load_cpptraj_state(txt) state.run() xyz = state.data['CRD1'].xyz top = state.data['CRD1'].top traj = pt.Trajectory(xyz=xyz, top=top) state_vecs = state.data[1:-3].values h_indices = pt.select_atoms('@H', traj.top) n_indices = pt.select_atoms('@H', traj.top) - 1 nh_indices = list(zip(n_indices, h_indices)) mat_ired = pt.ired_vector_and_matrix(traj, mask=nh_indices, order=2)[-1] mat_ired /= mat_ired[0, 0] # matired: make sure to reproduce cpptraj output aa_eq(mat_ired, state.data['matired'].values) # get modes modes = state.data[-2] cpp_eigenvalues = modes.eigenvalues cpp_eigenvectors = modes.eigenvectors evals, evecs = np.linalg.eigh(mat_ired) # need to sort a bit evals = evals[::-1] # cpptraj's eigvenvalues aa_eq(evals, cpp_eigenvalues) # cpptraj's eigvenvectors # use absolute values to avoid flipped sign # from Dan Roe # In practice, the "sign" of an eigenvector depends on the math library used to calculate it. # This is in fact why the modes command displacement test is disabled for cpptraj. # I bet if you use a different math library (e.g. use your system BLAS/LAPACK instead of the one bundled with Amber # or vice versa) you will get different signs. # Bottom line is that eigenvector sign doesn't matter. aa_eq(np.abs(evecs[:, ::-1].T), np.abs(cpp_eigenvectors), decimal=4) data = _ired(state_vecs, modes=(cpp_eigenvalues, cpp_eigenvectors)) order_s2 = data['IRED_00127[S2]'] # load cpptraj's output and compare to pytraj' values for S2 order paramters cpp_order_s2 = np.loadtxt(os.path.join(cpptraj_test_dir, 'Test_IRED', 'orderparam.save')).T[-1] aa_eq(order_s2, cpp_order_s2, decimal=5)
def test_load_pdb(): """ Load benzene PDB file without mask """ itraj = os.path.join(path, "benzene.pdb") traj = tools.load_traj(itraj) assert traj.n_frames == 1 assert traj.n_atoms == 12 C = pt.select_atoms("@C", traj.top) assert len(C) == 6 H = pt.select_atoms("@H", traj.top) assert len(H) == 6
def test_ired_lapack_in_numpy(self): parmfile = parm_dir trajfile = traj_dir # load to TrajectoryIterator traj = pt.iterload(trajfile, parmfile) # create N-H vectors h_indices = pt.select_atoms('@H', traj.top) n_indices = pt.select_atoms('@H', traj.top) - 1 nh_indices = list(zip(n_indices, h_indices)) # compute N-H vectors and ired matrix vecs_and_mat = pt.ired_vector_and_matrix(traj, mask=nh_indices, order=2) state_vecs = vecs_and_mat[:-1].values mat_ired = vecs_and_mat[-1] mat_ired /= mat_ired[0, 0] # cpptraj data_cpp = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs))[0] print(data_cpp.eigenvectors) # numpy data_np = pt.matrix._diag_np(mat_ired, n_vecs=len(state_vecs)) def order_(modes): data = _ired(state_vecs, modes=modes) order_s2_v0 = data['IRED_00127[S2]'] # make sure the S2 values is 1st array # load cpptraj's output and compare to pytraj' values for S2 order paramters cpp_order_s2 = np.loadtxt(os.path.join( cpptraj_test_dir, 'Test_IRED', 'orderparam.save')).T[-1] aa_eq(order_s2_v0.values, cpp_order_s2, decimal=4) order_(data_cpp.values) def plot_(x, y): import seaborn as sb sb.heatmap(x - y) pt.show() print((data_cpp.values[1] - data_np[1]).shape)
def test_load_traj(): """ Load analine dipeptide trajectory without mask """ itraj = os.path.join(path, "alanine_dipeptide.nc") itop = os.path.join(path, "alanine_dipeptide.parm7") traj = tools.load_traj(itraj, itop) assert traj.n_frames == 25 assert traj.n_atoms == 1912 dipeptide = pt.select_atoms(":ALA,ACE,NME", traj.top) assert len(dipeptide) == 22 water = pt.select_atoms(":WAT", traj.top) assert len(water) == traj.n_atoms - len(dipeptide)
def calcACFreff(self): self.ACFreff = dict() #self.test=dict() for (r1, a1), (r2, a2) in zip(self.list1, self.list2): label = str(r1) + a1 + ':' + str(r2) + a2 first_in_seq = list(self.traj.top.residues)[0].index idx1 = pt.select_atoms( searchAT[a1] + ' & :' + str(r1 - first_in_seq), self.traj.top) idx2 = pt.select_atoms( searchAT[a2] + ' & :' + str(r2 - first_in_seq), self.traj.top) pairs = list(map(list, product(idx1, idx2))) data_vec = va.vector_mask(self.traj, pairs, dtype='ndarray') if len(data_vec.shape) < 3: data_vec = [data_vec] self.ACFreff[label] = [ pt.timecorr(vals, vals, order=2, tstep=1, tcorr=len(vals), norm=False, dtype='ndarray') for vals in data_vec ] #for testing only: calculate the ACF without pytraj.timecorr #self.test[label] = [] #for vals in data_vec: #vals2 = np.array([unit_vector(v) for v in vals]) #x = vals2[:, 0] #y = vals2[:, 1] #z = vals2[:, 2] #x2 = mycorrelate2(x**2, norm=False) #y2 = mycorrelate2(y**2, norm=False) #z2 = mycorrelate2(z**2, norm=False) #xy = mycorrelate2(x*y, norm=False) #yz = mycorrelate2(y*z, norm=False) #xz = mycorrelate2(x*z, norm=False) #tot = (x2+y2+z2+2*xy+2*xz+2*yz) #tot /= tot[0] #tot = 1.5*(tot)-.5 dump(self.ACFreff, open(get_today() + '-ACFreff.pkl', 'wb'))
def test_simple_for_coverage(self): ''' ''' traj = pt.iterload(traj_dir, parm_dir) h_indices = pt.select_atoms('@H', traj.top) n_indices = pt.select_atoms('@H', traj.top) - 1 nh_indices = list(zip(n_indices, h_indices)) vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple') vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple') state_vecs = vecs_and_mat[0] mat_ired = vecs_and_mat[1] # get eigenvalues and eigenvectors modes = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs)) evals, evecs = modes data_0 = _ired(state_vecs, modes=(evals, evecs), NHbond=True, tcorr=10000, tstep=1.) data_1 = _ired(state_vecs, modes=modes, NHbond=True, tcorr=10000, tstep=1) for d0, d1 in zip(data_0, data_1): if d0.dtype not in ['modes', ]: aa_eq(d0.values, d1.values) else: # modes # values: tuple aa_eq(d0.values[0], d1.values[0]) aa_eq(d0.values[1], d1.values[1]) # try different dtype out_try_new_dtype = pt.ired_vector_and_matrix(traj, nh_indices, dtype='cpptraj_dataset')
def testsuper_dispatch(self): traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7") funclist = [pt.radgyr, pt.molsurf] for func in funclist: mask = '@CA' atom_indices = pt.select_atoms(mask, traj.top) # mask aa_eq(func(traj, mask=mask), func(traj, mask=atom_indices)) # specify traj=traj aa_eq(func(traj=traj, mask=mask), func(traj, mask=atom_indices)) # frame_indices with mask frame_indices = [0, 5, 8] aa_eq(func(traj[frame_indices], mask=mask), func(traj, mask=atom_indices, frame_indices=frame_indices))
def test_atom_distance(self): traj = pt.iterload(tz2_trajin, tz2_top) top = traj.top ref = traj[0] # test for 1st frame top.set_reference(ref) ref.top = top # all atoms within 5 Angtrom from :3@CA indices = top.select(":3@CA <@5.0") saved_indices = np.loadtxt( fn("mask.tz2.dat"), skiprows=1, usecols=(1, )) neighbors_smaller = pt.search_neighbors( traj, mask=':3@CA <@5.0', frame_indices=[ 0, ]) # subtract by '1' since cpptraj uses "1" as starting index for output saved_indices = saved_indices - 1 aa_eq(indices, saved_indices) aa_eq(neighbors_smaller.values, indices) # re-calculate the distance ca_indices = pt.select_atoms(':3@CA', traj.top) all_pairs = list(product(ca_indices, indices)) distances = pt.tools.flatten(pt.distance(ref, all_pairs)) for dist in distances: assert dist < 5.0, 'all distances must be smaller than 5.0 Angstrom' # test larger # why do we need to set reference frame again? top.set_reference(ref) indices_larger = top.select(":3@CA >@5.0") all_pairs_larger = list(product(ca_indices, indices_larger)) distances = pt.tools.flatten(pt.distance(ref, all_pairs_larger)) for dist in distances: assert dist > 5.0, 'all distances must be larger than 5.0 Angstrom' # search_neighbors neighbors_larger = pt.search_neighbors( traj, mask=':3@CA >@5.0', frame_indices=[ 0, ]) aa_eq(neighbors_larger.values, indices_larger)
def test_atom_distance(self): traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7") top = traj.top ref = traj[0] # test for 1st frame top.set_distance_mask_reference(ref) ref.top = top # all atoms within 5 Angtrom from :3@CA indices = top.select(":3@CA <@5.0") saved_indices = np.loadtxt("./data/mask.tz2.dat", skiprows=1, usecols=(1, )) neighbors_smaller = pt.search_neighbors(traj, mask=':3@CA <@5.0', frame_indices=[0, ]) # subtract by '1' since cpptraj uses "1" as starting index for output saved_indices = saved_indices - 1 aa_eq(indices, saved_indices) aa_eq(neighbors_smaller.values, indices) # re-calculate the distance ca_indices = pt.select_atoms(':3@CA', traj.top) all_pairs = list(product(ca_indices, indices)) distances = pt.tools.flatten(pt.distance(ref, all_pairs)) for dist in distances: assert dist < 5.0, 'all distances must be smaller than 5.0 Angstrom' # test larger # why do we need to set reference frame again? top.set_distance_mask_reference(ref) indices_larger = top.select(":3@CA >@5.0") all_pairs_larger = list(product(ca_indices, indices_larger)) distances = pt.tools.flatten(pt.distance(ref, all_pairs_larger)) for dist in distances: assert dist > 5.0, 'all distances must be larger than 5.0 Angstrom' # search_neighbors neighbors_larger = pt.search_neighbors(traj, mask=':3@CA >@5.0', frame_indices=[0, ]) aa_eq(neighbors_larger.values, indices_larger)
def testsuper_dispatch(self): traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7") funclist = [pt.radgyr, pt.molsurf] for func in funclist: mask = '@CA' atom_indices = pt.select_atoms(mask, traj.top) # mask aa_eq(func(traj, mask=mask), func(traj, mask=atom_indices)) # specify traj=traj aa_eq(func(traj=traj, mask=mask), func(traj, mask=atom_indices)) # frame_indices with mask frame_indices = [0, 5, 8] aa_eq( func(traj[frame_indices], mask=mask), func(traj, mask=atom_indices, frame_indices=frame_indices))
def RDC_from_S(eigx, eigz, Aa, Ar, trajfiles, topfile, rdc_types, masks, step=1, seg=None): results = dict() for n, tr in enumerate(trajfiles): temp =dict() traj = pt.iterload(tr, topfile, frame_slice=(0, -1, step)) if seg==None: residues = [r.index+1 for r in traj.topology.residues if r.name!='PRO' and r.index>0] else: residues = [r.index+1 for r in traj.topology.residues if r.name!='PRO' and r.index>=1 and r.index+1>=seg[0] and r.index+1<=seg[1]] #get vectors from traj for m, el in enumerate(rdc_types): if seg==None: indices1 = pt.select_atoms(masks[m][0]+' & !(:1) & !(:PRO)', traj.top) indices2 = pt.select_atoms(masks[m][1]+' & !(:1) & !(:PRO)', traj.top) else: indices1 = np.array([ pt.select_atoms(masks[m][0]+' & :'+str(res)+' & !(:PRO)', traj.top)[0] for res in residues #range(seg[0], seg[1]+1) if len(pt.select_atoms(masks[m][0]+' & :'+str(res)+' & !(:PRO)', traj.top))>0]) indices2 = np.array([ pt.select_atoms(masks[m][1]+' & :'+str(res)+' & !(:PRO)', traj.top)[0] for res in residues #range(seg[0], seg[1]+1) if len(pt.select_atoms(masks[m][1]+' & :'+str(res)+' & !(:PRO)', traj.top))>0]) if el=='CAHA': indices2 = indices1+1 pairs = np.array(list(zip(indices1, indices2))) data_vec = va.vector_mask(traj, pairs, dtype='ndarray') #calculate theta and phi angles thetas = [[angle_between(vec, eigz) for vec in veclist] for veclist in data_vec] projs = [[projection(vec, eigz) for vec in veclist] for veclist in data_vec] phis = [[angle_between(vec, eigx) for vec in veclist] for veclist in projs] Dmax = -(gammas[masks[m][0]]*gammas[masks[m][1]]*h*mu_0)/(8*pi**3*bond[masks[m][0]+masks[m][1]]**3) temp[el] = {residues[idx]:np.mean([Dmax*(0.5*(3*np.cos(th)**2-1)*Aa+0.75*Ar*np.sin(th)**2*np.cos(2*phis[idx][f]) ) for f, th in enumerate(ths)]) for idx, ths in enumerate(thetas)} results[n] = temp with open('rdc_'+str(n)+'.pkl', 'wb') as tf: dump(temp, tf) return results
def main(args): parser = argparse.ArgumentParser(description='Prepare config files for umbrella sampling run with a dihedral ' 'angle as reaction coordinate. By default the alpha carbons atoms of ' 'the specified residues are used to apply the constraint.') parser.add_argument('raf', help='list like: "residues_1:angle_1_from,angle_1_to:force_1|residues_2:angle_2_from,' 'angle_2_to:force_2|... e.g. 28,31,39,56:-130,50:200"') parser.add_argument('umbrellas', help='how many umbrella do we want to span?') parser.add_argument('init', help='names of initial topologies and trajectories, ' 'e.g. "WT.prmtop,rel_3.rst:WT.prmtop,prod_1.rst"') parser.add_argument('sim_config_path', help='directory where MD settings be found') parser.add_argument('-si', '--sim_config_files', help='list of file names of configuration files. (' 'default: ' '"min_1.umbin","rel_1.umbin","rel_2_25C.umbin","rel_3_25C.umbin",' '"prod_25C.umbin"', default='min_1.umbin,rel_1.umbin,' 'rel_2_25C.umbin,rel_3_25C.umbin,' 'prod_25C.umbin') parser.add_argument('-o', '--output_directory', help='directory where umbrella config files are written to (' 'default:' ' umbrella_config)', default='umbrella_config/') parser.add_argument('-i', '--init_value', help='initial value that should be started with', nargs='?', default=False) parser.add_argument('-s', action='store_true', help='define weather the last frame of each simulation is used for the next one as a start. ' 'In this case there should be only one init file.') args = parser.parse_args() umbrellas = int(args.umbrellas) configs_directory = args.output_directory try: mkdir(configs_directory) except OSError: pass # initial/template configuration files sim_configuration_path = args.sim_config_path sim_config_files = args.sim_config_files.split(',') sim_configuration_files = [] for scf in sim_config_files: sim_configuration_files.append(join(sim_configuration_path, scf)) # there is possibly more than one angle that gets transformed residues_angles_force = args.raf.split('|') init_files = args.init.split(':') if len(init_files) != len(residues_angles_force): print('Error: Number of input file tuples has to be same as sets of "residue angles force")') print('init_files:', init_files, 'length', len(init_files)) print('residue_angles_force:', residues_angles_force, 'length', len(residues_angles_force)) exit() init_prmtop = init_files[0].split(",")[0] init_traj = init_files[0].split(",")[1] print('Input file tuples: {}'.format(init_files)) # angle increments contains the step size for every configuration angle_increments = [] # contains [[atom1,atom2,...][angle1,angle2][force]] umbrella_configs = [] # read input arguments into variables for raf, i_file in zip(residues_angles_force, init_files): raf = re.sub('\'|\"', '', raf) residues, angles, force = raf.split(':') angles = angles.split(',') angles = list(map(float, angles)) print(residues) top, traj = i_file.split(',') pdb = pt.load(traj, top) atoms = [str(pt.select_atoms(':' + k + '@CA', pdb.top)[0] + 1) for k in residues.split(',')] print(atoms) umbrella_config = [] umbrella_config.append(atoms) umbrella_config.append(angles) umbrella_config.append(force) umbrella_configs.append(umbrella_config) angle_increments.append(abs(umbrella_config[1][0] - umbrella_config[1][1]) / umbrellas) umbrella_constraint_files = [] n_cnfgs = len(umbrella_configs) # create dictionary to hold constraint files meta_files = {} range_n_confgs = range(n_cnfgs) for n in range_n_confgs: meta_files[n] = "" init_value = args.init_value if init_value: init_value = float(init_value) if init_value < angles[0] or init_value > angles[1]: print('Error: init value must lie between min and max angle value') exit() else: angle = init_value c = 0 meta_out = "" next_traj = init_traj init_relaxed = "" run_script = "" # !!! only implemented for one init file for i in range_n_confgs: min = np.min(umbrella_configs[i][1]) max = np.max(umbrella_configs[i][1]) # go down to min while angle >= min: angle_low = angle - 180 angle_high = angle + 180 force = umbrella_configs[i][2] out, meta_out_temp = generate_umb_config(angle, angle_low, angle_high, force, c, atoms) # write constraint file umbrella_constraint_file_name = '{}umb_out_{}.dat'.format(configs_directory, c) with open(umbrella_constraint_file_name, 'w') as f: f.write(out) meta_out += meta_out_temp md_configs = generate_sim_configs(configs_directory, sim_configuration_files, umbrella_constraint_file_name, c) # old_traj old_traj = next_traj run_temp, next_traj = generate_run_script(md_configs, init_prmtop, next_traj) run_script += "#{}\n".format(c) run_script += run_temp if old_traj == init_traj: init_relaxed = next_traj # increment angle -= angle_increments[i] c -= 1 # reset to init value angle = init_value + angle_increments[i] c = 0 next_traj = init_relaxed # go up while angle <= max: angle_low = angle - 180 angle_high = angle + 180 out, meta_out_temp = generate_umb_config(angle, angle_low, angle_high, force, c, atoms) # write constraint file umbrella_constraint_file_name = '{}umb_out_{}.dat'.format(configs_directory, c) with open(umbrella_constraint_file_name, 'w') as f: f.write(out) meta_out += meta_out_temp md_configs = generate_sim_configs(configs_directory, sim_configuration_files, umbrella_constraint_file_name, c) run_temp, next_traj = generate_run_script(md_configs, init_prmtop, next_traj) run_script += "#{}\n".format(c) run_script += run_temp # increment angle += angle_increments[i] c += 1 # write meta file with open(configs_directory + 'meta_file', 'w') as mf: mf.write(meta_out) with open("run.sh", "w") as o: o.write("#!/bin/bash\n\n") o.write(run_script) # not properly implemented else: pass # # generate meta files and umbrella contstraint files umb_out_...dat # meta_out = "" # for c in range(umbrellas + 1): # out = "Harmonic restraints\n" # for i in range_n_confgs: # # out += " &rst\n" # # residues = umbrella_configs[i][0] # out += " iat=" + ','.join(atoms) + ",\n" # # angle_low_bound = umbrella_configs[i][1][0] - 180 # angle = umbrella_configs[i][1][0] # angle_high_bound = umbrella_configs[i][1][0] + 180 # out += " r1={}, r2={}, r3={}, r4={}\n".format(angle_low_bound, angle, angle, angle_high_bound) # # force = umbrella_configs[i][2] # out += " rk2={}, rk3={}\n/\n".format(force, force) # # meta_out += '../umbrella_productions/prod_25C_{}.dat {} 0.12184\n'.format(c, angle) # # if angles[0] > angles[1]: # umbrella_configs[i][1][0] -= angle_increments[i] # else: # umbrella_configs[i][1][0] += angle_increments[i] # # umbrella_constraint_file_name = '{}umb_out_{}.dat'.format(configs_directory, c) # with open(umbrella_constraint_file_name, 'w') as f: # f.write(out) # umbrella_constraint_files.append(umbrella_constraint_file_name) # # # write meta file # print(meta_out) # with open(configs_directory + 'meta_file', 'w') as mf: # mf.write(meta_out) # # ## generate run scripts # # if args.s and len(init_files) > 1: # print("too many input files for option s. Exiting.") # exit() # # runs_per_script = umbrellas // len(init_files) # umbrellas = umbrellas + 1 # idx = 0 # for init_f in init_files: # top, traj = init_f.split(',') # traj_temp = traj # out = "" # while idx <= runs_per_script and idx <= umbrellas and idx + umbrellas * 4 < len(md_files): # b_name_min = basename(md_files[idx]).split('.')[0] # run_min = \ # "pmemd -O " \ # "-i {} " \ # "-o umbrella_productions/{}.out " \ # "-p {} " \ # "-c {} " \ # "-r umbrella_productions/{}.rst " \ # "-inf umbrella_productions/{}.mdinfo \n" \ # .format(md_files[idx], b_name_min, top, traj_temp, b_name_min, b_name_min) # # b_name_rel_1 = basename(md_files[idx + umbrellas]).split('.')[0] # run_rel_1 = \ # "pmemd.cuda -O " \ # "-i {} " \ # "-o umbrella_productions/{}.out " \ # "-p {} " \ # "-c umbrella_productions/{}.rst " \ # "-r umbrella_productions/{}.rst " \ # "-inf umbrella_productions/{}.mdinfo " \ # "-ref umbrella_productions/{}.rst \n" \ # .format(md_files[idx + umbrellas], b_name_rel_1, top, b_name_min, b_name_rel_1, b_name_rel_1, \ # b_name_min) # # b_name_rel_2 = basename(md_files[idx + umbrellas * 2]).split('.')[0] # run_rel_2 = \ # "pmemd.cuda -O " \ # "-i {} " \ # "-o umbrella_productions/{}.out " \ # "-p {} " \ # "-c umbrella_productions/{}.rst " \ # "-r umbrella_productions/{}.rst " \ # "-inf umbrella_productions/{}.mdinfo " \ # "-ref umbrella_productions/{}.rst \n" \ # .format(md_files[idx + umbrellas * 2], b_name_rel_2, top, b_name_rel_1, b_name_rel_2, # b_name_rel_2, # b_name_rel_1) # # b_name_rel_3 = basename(md_files[idx + umbrellas * 3]).split('.')[0] # run_rel_3 = \ # "pmemd.cuda -O " \ # "-i {} " \ # "-o umbrella_productions/{}.out " \ # "-p {} " \ # "-c umbrella_productions/{}.rst " \ # "-r umbrella_productions/{}.rst " \ # "-inf umbrella_productions/{}.mdinfo " \ # "-ref umbrella_productions/{}.rst \n" \ # .format(md_files[idx + umbrellas * 3], b_name_rel_3, top, b_name_rel_2, b_name_rel_3, # b_name_rel_3, # b_name_rel_2) # # b_name_prod = basename(md_files[idx + umbrellas * 4]).split('.')[0] # run_prod = \ # "pmemd.cuda -O " \ # "-i {} " \ # "-o umbrella_productions/{}.out " \ # "-p {} " \ # "-c umbrella_productions/{}.rst " \ # "-r umbrella_productions/{}.rst " \ # "-inf umbrella_productions/{}.mdinfo " \ # "-x umbrella_productions/{}.nc " \ # "-ref umbrella_productions/{}.rst \n" \ # .format(md_files[idx + umbrellas * 4], b_name_prod, top, b_name_rel_3, b_name_prod, b_name_prod, # b_name_prod, \ # b_name_rel_3) # # out += "#{}\n".format(idx) # idx += 1 # out += run_min + "\n" + run_rel_1 + "\n" + run_rel_2 + "\n" + run_rel_3 + "\n" + run_prod + "\n" # # if args.s: # traj_temp = "umbrella_productions/" + b_name_prod + ".rst" # # # write run script # if args.s: # script_file_name = 'init_WT_run_s_' + str(idx - 1) + ".sh" # # else: # script_file_name = 'init_' + basename(init_f).split('.')[0] + "_run_" + str(idx - 1) + ".sh" # # print(script_file_name) # with open(script_file_name, "w") as o: # o.write("#!/bin/bash\n\n") # o.write(out) # print(runs_per_script) # runs_per_script *= 2 # print out first constraint file for inspection by user with open(configs_directory + 'umb_out_0.dat', 'r') as f: print(f.readlines())
v = self.get_knn_vol(k) return 8.*np.pi**2/v def get_entropy(self, k=1): pdf = self.get_pdf(k=1) return R_gas/self.N * (np.sum(np.log(pdf)) + np.euler_gamma) # In[133]: traj = pt.iterload(traj_path, parm_path) O_idx = pt.select_atoms(traj.top, ':WAT@O') Solute_idx = pt.select_atoms(traj.top, sol_mask) N_frames = traj.n_frames ### Key : distance ### Item: [water_O_idx, frame_idx, count] crd_dict = dict() for d in range(int((stop-start)/step)): crd_dict[start+d*step] = list() for frame in traj.iterframe(): pt.autoimage(frame, top=traj.topology)
def RDC(trajfiles, topfile, rdc_types, masks, step=1, seg=None): results = dict() for n, tr in enumerate(trajfiles): traj = pt.iterload(tr, topfile, frame_slice=(0, -1, step)) if seg==None: residues = [r.index+1 for r in traj.topology.residues if r.name!='PRO' and r.index>0] else: residues = [r.index+1 for r in traj.topology.residues if r.name!='PRO' and r.index>=1 and r.index+1>=seg[0] and r.index+1<=seg[1]] temp = dict() Aa = [] Ar = [] eigx = [] eigy = [] eigz = [] alpha = [] gamma = [] beta = [] for f, frame in enumerate(traj): pt.write_traj("temp.pdb", frame, top=traj.top, overwrite=True) if seg==None: os.system("pales -pdb temp.pdb > pales.out") else: os.system("pales -pdb temp.pdb -s1 "+str(seg[0])+" -sN "+str(seg[1])+" > pales.out") os.system("grep \"DATA EIGENVALUES (Sxx_d,Syy_d,Szz_d)\" pales.out > eigenvalues.out") with open('eigenvalues.out', 'r') as tf: data = tf.readlines() data = data[0].split() Sxx_d=float(data[-3]) Syy_d=float(data[-2]) Szz_d=float(data[-1]) Aa.append(Szz_d) Ar.append((2./3.)*(Sxx_d-Syy_d)) os.system("grep \"DATA EULER_ANGLES\" pales.out > euler.out") with open('euler.out', 'r') as tf: data = tf.readlines() data = data[0].split() alpha.append(float(data[-3])) beta.append(float(data[-2])) gamma.append(float(data[-1])) os.system("grep \"DATA EIGENVECTORS X_AXIS\" pales.out > eigenvalues1.out") with open('eigenvalues1.out', 'r') as tf: data = tf.readlines() data = data[0].split() eigx.append([float(data[-3]), float(data[-2]), float(data[-1])]) os.system("grep \"DATA EIGENVECTORS Y_AXIS\" pales.out > eigenvalues2.out") with open('eigenvalues2.out', 'r') as tf: data = tf.readlines() data = data[0].split() eigy.append([float(data[-3]), float(data[-2]), float(data[-1])]) os.system("grep \"DATA EIGENVECTORS Z_AXIS\" pales.out > eigenvalues3.out") with open('eigenvalues3.out', 'r') as tf: data = tf.readlines() data = data[0].split() eigz.append([float(data[-3]), float(data[-2]), float(data[-1])]) R = np.array(Ar)/np.array(Aa) #get vectors from traj for m, el in enumerate(rdc_types): if seg==None: indices1 = pt.select_atoms(masks[m][0]+' & !(:1) & !(:PRO)', traj.top) indices2 = pt.select_atoms(masks[m][1]+' & !(:1) & !(:PRO)', traj.top) else: indices1 = np.array([ pt.select_atoms(masks[m][0]+' & :'+str(res)+' & !(:PRO)', traj.top)[0] for res in residues #range(seg[0], seg[1]+1) if len(pt.select_atoms(masks[m][0]+' & :'+str(res)+' & !(:PRO)', traj.top))>0]) indices2 = np.array([ pt.select_atoms(masks[m][1]+' & :'+str(res)+' & !(:PRO)', traj.top)[0] for res in residues #range(seg[0], seg[1]+1) if len(pt.select_atoms(masks[m][1]+' & :'+str(res)+' & !(:PRO)', traj.top))>0]) if el=='CAHA': indices2 = indices1+1 pairs = np.array(list(zip(indices1, indices2))) data_vec = va.vector_mask(traj, pairs, dtype='ndarray') #calculate theta and phi angles thetas = [[angle_between(vec, zaxis) for vec, zaxis in zip(veclist, eigz)] for veclist in data_vec] projs = [[projection(vec, zaxis) for vec, zaxis in zip(veclist, eigz)] for veclist in data_vec] phis = [[angle_between(vec, xaxis) for vec, xaxis in zip(veclist, eigx)] for veclist in projs] #calculate RDC values #rPQcub = [np.nanmean([(np.linalg.norm(v)*1e-10)**3 for v in veclist]) for veclist in data_vec] #Dmax = [-(gammas[masks[m][0]]*gammas[masks[m][1]]*h*mu_0)/(8*pi**3*rPQ) for rPQ in rPQcub] Dmax = -(gammas[masks[m][0]]*gammas[masks[m][1]]*h*mu_0)/(8*pi**3*bond[masks[m][0]+masks[m][1]]**3) # temp[el] = {residues[idx]:[Dmax[idx]*(0.5*(3*np.cos(th)**2-1)*Aa[f]+0.75*Ar[f]*np.sin(th)**2*np.cos(2*phis[idx][f]) ) # for f, th in enumerate(ths)] # for idx, ths in enumerate(thetas)} temp[el] = {residues[idx]:np.mean([Dmax*(0.5*(3*np.cos(th)**2-1)*Aa[f]+0.75*Ar[f]*np.sin(th)**2*np.cos(2*phis[idx][f]) ) for f, th in enumerate(ths)]) for idx, ths in enumerate(thetas)} results[n] = temp with open('rdc_'+str(n)+'.pkl', 'wb') as tf: dump(temp, tf) return results
def main(args): parser = argparse.ArgumentParser( description='Prepare config files for umbrella sampling run.') parser.add_argument( 'raf', help= 'list like: "residues_1,angle_1_fromp,angle_1_to,force_1:residues_2,angle_2_from,' 'angle_2_to,force_2:..."') parser.add_argument('umbrellas', help='how many umbrella do we want to span?') parser.add_argument('init', help='names of initial topologies and trajectories, ' 'e.g. "WT.prmtop,rel_3.rst:WT.prmtop,prod_1.rst"') parser.add_argument('sim_config_path', help='here can MD settings be found') parser.add_argument( '-s', action='store_true', help= 'define weather the last frame of each simulation is used for the next one as a start. ' 'In this case there should be only one init file') args = parser.parse_args() umbrellas = int(args.umbrellas) configs_directory = "umbrella_config/" try: mkdir(configs_directory) except OSError: pass # there is possibly more than one angle that gets transformed residues_angles_force = args.raf.split('|') init_files = args.init.split(':') if len(init_files) != len(residues_angles_force): print( 'Error: Number of input file tuples has to be same as sets of "residue angles force")' ) print('init_files:', init_files, 'length', len(init_files)) print('residue_angles_force:', residues_angles_force, 'length', len(residues_angles_force)) exit() print('Input file tuples: {}'.format(init_files)) # angle increments contains the step size for every configuration angle_increments = [] # contains [[atom1,atom2,...][angle1,angle2][force]] umbrella_configs = [] # read input arguments into variables for raf, i_file in zip(residues_angles_force, init_files): raf = re.sub('\'|\"', '', raf) residues, angles, force = raf.split(':') angles = angles.split(',') angles = list(map(float, angles)) print(residues) top, traj = i_file.split(',') pdb = pt.load(traj, top) atoms = [ str(pt.select_atoms(':' + k + '@CA', pdb.top)[0] + 1) for k in residues.split(',') ] print(atoms) umbrella_config = [] umbrella_config.append(atoms) umbrella_config.append(angles) umbrella_config.append(force) umbrella_configs.append(umbrella_config) angle_increments.append( abs(umbrella_config[1][0] - umbrella_config[1][1]) / umbrellas) umbrella_constraint_files = [] n_cnfgs = len(umbrella_configs) # create dictionary to hold meta files meta_files = {} for n in range(n_cnfgs): meta_files[n] = "" # generate meta files and umbrella contstraint files umb_out_...dat meta_out = "" for c in range(umbrellas + 1): out = "Harmonic restraints\n" for i in range(n_cnfgs): out += " &rst\n" residues = umbrella_configs[i][0] out += " iat=" + ','.join(atoms) + ",\n" angle_low_bound = umbrella_configs[i][1][0] - 180 angle = umbrella_configs[i][1][0] angle_high_bound = umbrella_configs[i][1][0] + 180 out += " r1={}, r2={}, r3={}, r4={}\n".format( angle_low_bound, angle, angle, angle_high_bound) force = umbrella_configs[i][2] out += " rk2={}, rk3={}\n/\n".format(force, force) meta_out += '../umbrella_productions/prod_25C_{}.dat {} 0.12184\n'.format( c, angle) if angles[0] > angles[1]: umbrella_configs[i][1][0] -= angle_increments[i] else: umbrella_configs[i][1][0] += angle_increments[i] umbrella_constraint_file_name = '{}umb_out_{}.dat'.format( configs_directory, c) with open(umbrella_constraint_file_name, 'w') as f: f.write(out) umbrella_constraint_files.append(umbrella_constraint_file_name) # write meta file print(meta_out) with open(configs_directory + 'meta_file', 'w') as mf: mf.write(meta_out) ## generate simulation configurations with umbrella constraints # initial/template configuration files sim_configuration_path = args.sim_config_path sim_configuration_files = [ join(sim_configuration_path, 'min_1.umbin'), join(sim_configuration_path, 'rel_1.umbin'), join(sim_configuration_path, 'rel_2_25C.umbin'), join(sim_configuration_path, 'rel_3_25C.umbin'), join(sim_configuration_path, 'prod_25C.umbin') ] md_files = [] for init_f in sim_configuration_files: with open(init_f, 'r') as m: sim_config_content = m.readlines() # add umbrella constraints to simulation configuration for i, f in enumerate(umbrella_constraint_files): if "prod_25C" in init_f: # remove the last two lines md_in_temp = "".join(sim_config_content[:-2]) # add contraints md_in_temp += "DISANG=./{}\n".format(f) # add meta data file name md_in_temp += "DUMPAVE=./umbrella_productions/prod_25C_{}.dat\n".format( i) else: md_in_temp = "".join(sim_config_content[:-1]) md_in_temp += "DISANG=./{}\n".format(f) # write simulation configuration file sim_file_name = basename(init_f).split('.') with open( configs_directory + sim_file_name[0] + "_" + str(i) + "." + sim_file_name[1], 'w') as o: o.write(md_in_temp) md_files.append(configs_directory + sim_file_name[0] + "_" + str(i) + "." + sim_file_name[1]) ## generate run scripts init_files = args.init.split(':') if args.s and len(init_files) > 1: print("too many input files for option s. Exiting.") exit() runs_per_script = umbrellas // len(init_files) umbrellas = umbrellas + 1 idx = 0 for init_f in init_files: top, traj = init_f.split(',') traj_temp = traj out = "" while idx <= runs_per_script and idx <= umbrellas and idx + umbrellas * 4 < len( md_files): b_name_min = basename(md_files[idx]).split('.')[0] run_min = \ "pmemd -O " \ "-i {} " \ "-o umbrella_productions/{}.out " \ "-p {} " \ "-c {} " \ "-r umbrella_productions/{}.rst " \ "-inf umbrella_productions/{}.mdinfo \n" \ .format(md_files[idx], b_name_min, top, traj_temp, b_name_min, b_name_min) b_name_rel_1 = basename(md_files[idx + umbrellas]).split('.')[0] run_rel_1 = \ "pmemd.cuda -O " \ "-i {} " \ "-o umbrella_productions/{}.out " \ "-p {} " \ "-c umbrella_productions/{}.rst " \ "-r umbrella_productions/{}.rst " \ "-inf umbrella_productions/{}.mdinfo " \ "-ref umbrella_productions/{}.rst \n" \ .format(md_files[idx + umbrellas], b_name_rel_1, top, b_name_min, b_name_rel_1, b_name_rel_1, \ b_name_min) b_name_rel_2 = basename(md_files[idx + umbrellas * 2]).split('.')[0] run_rel_2 = \ "pmemd.cuda -O " \ "-i {} " \ "-o umbrella_productions/{}.out " \ "-p {} " \ "-c umbrella_productions/{}.rst " \ "-r umbrella_productions/{}.rst " \ "-inf umbrella_productions/{}.mdinfo " \ "-ref umbrella_productions/{}.rst \n" \ .format(md_files[idx + umbrellas * 2], b_name_rel_2, top, b_name_rel_1, b_name_rel_2, b_name_rel_2, b_name_rel_1) b_name_rel_3 = basename(md_files[idx + umbrellas * 3]).split('.')[0] run_rel_3 = \ "pmemd.cuda -O " \ "-i {} " \ "-o umbrella_productions/{}.out " \ "-p {} " \ "-c umbrella_productions/{}.rst " \ "-r umbrella_productions/{}.rst " \ "-inf umbrella_productions/{}.mdinfo " \ "-ref umbrella_productions/{}.rst \n" \ .format(md_files[idx + umbrellas * 3], b_name_rel_3, top, b_name_rel_2, b_name_rel_3, b_name_rel_3, b_name_rel_2) b_name_prod = basename(md_files[idx + umbrellas * 4]).split('.')[0] run_prod = \ "pmemd.cuda -O " \ "-i {} " \ "-o umbrella_productions/{}.out " \ "-p {} " \ "-c umbrella_productions/{}.rst " \ "-r umbrella_productions/{}.rst " \ "-inf umbrella_productions/{}.mdinfo " \ "-x umbrella_productions/{}.nc " \ "-ref umbrella_productions/{}.rst \n" \ .format(md_files[idx + umbrellas * 4], b_name_prod, top, b_name_rel_3, b_name_prod, b_name_prod, b_name_prod, \ b_name_rel_3) out += "#{}\n".format(idx) idx += 1 out += run_min + "\n" + run_rel_1 + "\n" + run_rel_2 + "\n" + run_rel_3 + "\n" + run_prod + "\n" if args.s: traj_temp = "umbrella_productions/" + b_name_prod + ".rst" # write run script if args.s: script_file_name = 'init_WT_run_s_' + str(idx - 1) + ".sh" else: script_file_name = 'init_' + basename(init_f).split( '.')[0] + "_run_" + str(idx - 1) + ".sh" print(script_file_name) with open(script_file_name, "w") as o: o.write("#!/bin/bash\n\n") o.write(out) print(runs_per_script) runs_per_script *= 2
angle_increments = [] # contains [[atom1,atom2,...][angle1,angle2][force]] umbrella_configs = [] # read input arguments into variables for raf, i_file in zip(residues_angles_force, init_files): raf = re.sub('\'|\"', '', raf) residues, angles, force = raf.split(':') angles = angles.split(',') angles = list(map(float, angles)) print(residues) top, traj = i_file.split(',') pdb = pt.load(traj, top) atoms = [str(pt.select_atoms(':' + k + '@CA', pdb.top)[0] + 1) for k in residues.split(',')] print(atoms) umbrella_config = [] umbrella_config.append(atoms) umbrella_config.append(angles) umbrella_config.append(force) umbrella_configs.append(umbrella_config) angle_increments.append(abs(umbrella_config[1][0] - umbrella_config[1][1]) / umbrellas) umbrella_constraint_files = [] n_cnfgs = len(umbrella_configs) # create dictionary to hold meta files meta_files = {} range_n_confgs = range(n_cnfgs)
def __init__(self, top, trajs, rotfit=False, mask='@CA', stride=1, n_blocks=1, bframes=50000, skip=25000): #read sequence if trajs[0][-3:] == 'dcd': traj = pt.iterload(trajs[0], top) else: #filename, top_name = get_fn(trajs[0]), get_fn(top) m_traj = md.load(trajs[0], top=top) traj = pt.Trajectory(xyz=m_traj.xyz.astype('f8'), top=top) self.sequence = {res.index + 1: res.name for res in traj.top.residues} self.top = top self.trajs = trajs n_frames = len(traj) self.n_indices = pt.select_atoms('@N & !(:1) & !(:PRO)', traj.top) self.h_indices = self.n_indices + 1 self.nh_pairs = np.array(list(zip(self.n_indices, self.h_indices))) res_list = sorted( [k for k, val in self.sequence.items() if val != 'PRO' and k != 1]) self.acf = dict() self.S2 = dict() if n_blocks == 1: for tt, t in enumerate(self.trajs): if t[-3:] == 'dcd': traj = pt.load(t, top, stride=stride) else: #filename, top_name = get_fn(t), get_fn(top) m_traj = md.load(t, top=top) traj = pt.Trajectory(xyz=m_traj.xyz.astype('f8'), top=top) if rotfit == True: #print('RMSD fitting on '+mask) _ = traj.superpose(ref=-1, mask='@CA') data_vec = va.vector_mask(traj, self.nh_pairs, dtype='ndarray') self.acf[tt] = { res_list[n]: pt.timecorr(vals, vals, order=2, tstep=1, tcorr=len(vals), norm=False, dtype='ndarray') for n, vals in enumerate(data_vec) } self.S2[tt] = { res_list[n]: S2(vals) for n, vals in enumerate(data_vec) } else: index = 0 bcount = 0 for tt, t in enumerate(self.trajs): print(tt + 1, '/', len(self.trajs)) while bcount < n_blocks: first_frame = bcount * skip last_frame = first_frame + bframes if t[-3:] == 'dcd': traj = pt.load(t, top, frame_indices=slice( first_frame, last_frame, stride)) else: #filename, top_name = get_fn(t), get_fn(top) m_traj = md.load(t, top=top) m_traj = m_traj[first_frame:last_frame:stride] traj = pt.Trajectory(xyz=m_traj.xyz.astype('f8'), top=top) if rotfit == True: _ = traj.superpose(ref=-1, mask='@CA') data_vec = va.vector_mask(traj, self.nh_pairs, dtype='ndarray') self.acf[index] = { res_list[n]: pt.timecorr(vals, vals, order=2, tstep=1, tcorr=len(vals), norm=False, dtype='ndarray') for n, vals in enumerate(data_vec) } self.S2[index] = { res_list[n]: S2(vals) for n, vals in enumerate(data_vec) } bcount += 1 index += 1 bcount = 0
# load mdtraj import mdtraj as md from mdtraj.testing import get_fn # load h5 file #filename, topology_name = get_fn('tfit_10.xtc'), get_fn('prot0.pdb') m_traj = md.load('tfit_10.xtc', top='prot0.pdb') m_traj traj = pt.Trajectory(xyz=m_traj.xyz.astype('f8'), top='prot0.pdb') traj # creat N-H vector pairs # select H (backbone) indices with given residue number (from previous step) #H_mask = ':' + ','.join(str(i) for i in resnums) + '@H' #print('H_mask: ', H_mask) h_indices = pt.select_atoms(traj.top, '@H') # select N (backbone) indices n_indices = h_indices - 1 # create pairs nh_pairs = list(zip(n_indices, h_indices)) nh_pairs[:3] # do calculation s2 = pt.nh_order_parameters(traj, nh_pairs, tcorr=52801.0, tstep=1.0) st20 = pt.nh_order_parameters(traj, nh_pairs, tcorr=20000.0, tstep=1.0) st10 = pt.nh_order_parameters(traj, nh_pairs, tcorr=10000.0, tstep=1.0) st5 = pt.nh_order_parameters(traj, nh_pairs, tcorr=5000.0, tstep=1.0) # plot