def test_actionlist(self): '''test_actionlist ''' dslist = DatasetList() actlist = ActionList() traj = pt.iterload("./data/Tc5b.x", "./data/Tc5b.top") mask_list = ['@CB @CA', '@CA @H'] for mask in mask_list: actlist.add(CA.Action_Vector(), mask, traj.top, dslist=dslist) actlist.compute(traj) dslist2 = pt.calc_vector(traj, mask_list) dslist4 = va.vector_mask(traj, mask_list) dslist3_0 = pt.calc_vector(traj, mask_list[0]) dslist3_1 = pt.calc_vector(traj, mask_list[1]) aa_eq(dslist3_0, dslist2[0]) aa_eq(dslist3_1, dslist2[1]) aa_eq(dslist3_0, dslist4[0]) aa_eq(dslist3_1, dslist4[1]) aa_eq(dslist3_0, dslist[0].values) aa_eq(dslist3_1, dslist[1].values)
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 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 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_ired_vector(self): '''test mask as a list of strings or as a 2D array of integers ''' parm_dir = os.path.join(cpptraj_test_dir, 'Test_IRED', '1IEE_A_prot.prmtop') trajin_dir = os.path.join(cpptraj_test_dir, 'Test_IRED', '1IEE_A_test.mdcrd') traj = pt.iterload(trajin_dir, parm_dir) # get a list of mask from cpptraj input maskes = [] lines = None n_indices_cpp = [] with open('data/ired.in', 'r') as fh: lines = fh.readlines() for line in lines: if 'vector' in line and 'ired' in line: # example: vector v100 @1541 ired @1542 sline = line.split() mask = ' '.join((sline[2], sline[4])) n_indices_cpp.append(int(sline[2][1:]) - 1) maskes.append(mask) h_indices_cpp = [i + 1 for i in n_indices_cpp] # calcuate vector from a list of strings data_vec = va.vector_mask(traj, maskes) # calcuate vector from a 2d array of integers nh_indices = np.array(list(zip(n_indices_cpp, h_indices_cpp))) data_vec_2 = va.vector_mask(traj, nh_indices) # re-create cpptraj input to run cpptraj txt = ''.join(lines) # add parm and trajin lines txt = 'parm ' + parm_dir + '\n' + \ 'trajin ' + trajin_dir + '\n' + \ txt state = pt.datafiles.load_cpptraj_output(txt, dtype='state') state.run() cpp_data = state.datasetlist cpp_vectors = cpp_data.grep('vector', mode='dtype').values cpp_matired = cpp_data.grep('matrix', mode='dtype')['matired'] # assert between pytraj's data_vec and cpptraj's cpp_vectors aa_eq(data_vec, cpp_vectors) # from a 2D array of integers aa_eq(data_vec_2, cpp_vectors) # test ired vector with ired matrix # open file with open('data/ired_reduced.in', 'r') as fh: text = ''.join(fh.readlines()) state2 = pt.load_batch(traj, text) state2.run() data = pt.ired_vector_and_matrix(traj, nh_indices, order=2) data_vec_3 = data[0] assert len(data_vec_3) == 126, 'must have 126 vectors' matired = data[1] # TODO: know why?? matired /= matired[0, 0] aa_eq(data_vec_3, cpp_vectors) assert pt.tools.rmsd(matired.flatten(), cpp_matired.values) < 1E-6, 'matired'
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
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