def test_shape_when_none(): t = md.load(get_fn('frame0.h5')) np.hstack((md.compute_phi(t)[1], md.compute_psi(t)[1], md.compute_chi1(t)[1], md.compute_chi2(t)[1], md.compute_chi3(t)[1], md.compute_chi1(t)[1], md.compute_omega(t)[1]))
def run_corr(args): print "reading trajectory" traj = md.load(args.input_traj, top=args.pdb_file) if args.feature_type == "positions": print "aligning frames" backbone = traj.topology.select_atom_indices("minimal") traj.superpose(traj, atom_indices=backbone) print "computing displacements" alpha_carbons = traj.topology.select_atom_indices("alpha") traj = traj.atom_slice(alpha_carbons) features = np.sqrt( np.sum((traj.xyz - np.mean(traj.xyz, axis=0))**2, axis=2)) elif args.feature_type == "dihedrals": _, phi_angles = md.compute_phi(traj, periodic=False) _, psi_angles = md.compute_psi(traj, periodic=False) features = np.vstack([phi_angles, psi_angles]) elif args.feature_type == "transformed-dihedrals": _, phi_angles = md.compute_phi(traj, periodic=False) _, psi_angles = md.compute_psi(traj, periodic=False) phi_sin = np.sin(phi_angles) phi_cos = np.cos(phi_angles) psi_sin = np.sin(psi_angles) psi_cos = np.cos(psi_angles) features = np.vstack([phi_sin, phi_cos, psi_sin, psi_cos]) elif args.feature_type == "transformed-chi-dihedrals": _, chi_angles = md.compute_chi1(traj, periodic=False) chi_sin = np.sin(chi_angles) chi_cos = np.cos(chi_angles) features = np.vstack([chi_sin, chi_cos]) print features.shape print "Computing correlation matrix" corr = np.corrcoef(features, rowvar=False) print "Plotting correlation matrix" if args.plot_type == "heatmap": plt.pcolor(corr, vmin=-1.0, vmax=1.0) plt.colorbar() plt.tight_layout() elif args.plot_type == "distribution": import seaborn as sns sns.distplot(np.ravel(corr), kde=False) plt.xlabel("Correlation", fontsize=16) plt.ylabel("Occurrences", fontsize=16) plt.savefig(args.figure_fl, DPI=300)
def dataset_chi(traj): dataset = [] indices, angles1 = md.compute_chi1(traj) indices, angles2 = md.compute_chi2(traj) indices, angles3 = md.compute_chi3(traj) #indices,angles4 = md.compute_chi4(traj) #print(angles1) #print(type(angles1)) #print(len(angles1)) angles = np.concatenate((angles1, angles2, angles3), axis=1) dataset.append(angles) print("Done constructing dataset using chi angles") return dataset
def __init__(self, mdtraj_trajectory): WrappedPose.__init__(self) if md is None: print( "MDTrajPoseWrapper requires the mdtraj library to be installed" ) raise ImportError assert isinstance(mdtraj_trajectory, md.Trajectory) assert mdtraj_trajectory.n_frames == 1 self.trajectory = mdtraj_trajectory # RADIANS: self.phi_atoms, self.phis = md.compute_phi(self.trajectory) self.psi_atoms, self.psis = md.compute_psi(self.trajectory) self.chis = [None, None, None, None, None] # Adding zero element just to make indexing easier self.chi_atoms = [None, None, None, None, None] self.chi_atoms[1], self.chis[1] = md.compute_chi1(self.trajectory) self.chi_atoms[2], self.chis[2] = md.compute_chi2(self.trajectory) self.chi_atoms[3], self.chis[3] = md.compute_chi3(self.trajectory) self.chi_atoms[4], self.chis[4] = md.compute_chi4(self.trajectory)
def traj_dihedral_classes(trajin, dihedral_classes): # dihedrals_temp = None dihedral_ids_temp = None for dihedral_class in dihedral_classes: # #print("Loading {:s} angles...".format(dihedral_class)) if re.match("phi", dihedral_class): # phi_atoms, phi = md.compute_phi(trajin) if dihedrals_temp is None: # dihedrals_temp = np.cos(phi) dihedral_ids_temp = extract_res_info(trajin.top, phi_atoms, "phi") # else: # dihedrals_temp = np.append(dihedrals_temp, np.cos(phi), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, phi_atoms, "phi"), axis=0) # # if re.match("psi", dihedral_class): # psi_atoms, psi = md.compute_psi(trajin) if dihedrals_temp is None: # dihedrals_temp = np.sin(psi) dihedral_ids_temp = extract_res_info(trajin.top, psi_atoms, "psi") # else: # dihedrals_temp = np.append(dihedrals_temp, np.sin(psi), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, psi_atoms, "psi"), axis=0) # # if re.match("chi1", dihedral_class): # chi1_atoms, chi1 = md.compute_chi1(trajin) if dihedrals_temp is None: # dihedrals_temp = np.sin(chi1) dihedral_ids_temp = extract_res_info(trajin.top, chi1_atoms, "chi1") # else: # dihedrals_temp = np.append(dihedrals_temp, np.sin(chi1), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, chi1_atoms, "chi1"), axis=0) # # if re.match("chi2", dihedral_class): # chi2_atoms, chi2 = md.compute_chi2(trajin) if dihedrals_temp is None: # dihedrals_temp = np.sin(chi2) dihedral_ids_temp = extract_res_info(trajin.top, chi2_atoms, "chi2") # else: # dihedrals_temp = np.append(dihedrals_temp, np.sin(chi2), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, chi2_atoms, "chi2"), axis=0) # # if re.match("chi3", dihedral_class): # chi3_atoms, chi3 = md.compute_chi3(trajin) if dihedrals_temp is None: # dihedrals_temp = np.sin(chi3) dihedral_ids_temp = extract_res_info(trajin.top, chi3_atoms, "chi3") # else: # dihedrals_temp = np.append(dihedrals_temp, np.sin(chi3), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, chi3_atoms, "chi3"), axis=0) # # if re.match("chi4", dihedral_class): # chi4_atoms, chi4 = md.compute_chi4(trajin) if dihedrals_temp is None: # dihedrals_temp = np.sin(chi4) dihedral_ids_temp = extract_res_info(trajin.top, chi4_atoms, "chi4") # else: # dihedrals_temp = np.append(dihedrals_temp, np.sin(chi4), axis=1) dihedral_ids_temp = np.append(dihedral_ids_temp, extract_res_info( trajin.top, chi4_atoms, "chi4"), axis=0) # # if dihedral_class is None: # raise Exception( "Feature format \"{:s}\" is invalid in a dihedral class measurement context." .format(dihedral_class)) # # return (dihedrals_temp, dihedral_ids_temp)
def extract_features(args): print "reading trajectory" traj = md.load(args.input_traj, top=args.pdb_file) if args.select_residues: selections = [] ranges = args.select_residues.split(",") for range_ in args.select_residues.split(","): if "-" in range_: left, right = map(int, range_.split("-")) selections.append("(residue %s to %s)" % (left, right)) else: singleton = int(range_) selections.append("(residue %s)" % singleton) selection_str = " or ".join(selections) selected_atoms = traj.topology.select(selection_str) traj = traj.atom_slice(selected_atoms) if args.feature_type == "positions": print "aligning frames" traj.superpose(traj) features = traj.xyz.reshape(traj.n_frames, traj.n_atoms * 3) elif args.feature_type == "transformed-dihedrals": print "computing dihedrals" _, phi_angles = md.compute_phi(traj, periodic=False) _, psi_angles = md.compute_psi(traj, periodic=False) phi_sin = np.sin(phi_angles) phi_cos = np.cos(phi_angles) psi_sin = np.sin(psi_angles) psi_cos = np.cos(psi_angles) features = np.hstack([phi_sin, phi_cos, psi_sin, psi_cos]) elif args.feature_type == "transformed-dihedrals-chi": print "computing dihedrals" _, phi_angles = md.compute_phi(traj, periodic=False) _, psi_angles = md.compute_psi(traj, periodic=False) _, chi_angles = md.compute_chi1(traj, periodic=False) phi_sin = np.sin(phi_angles) phi_cos = np.cos(phi_angles) psi_sin = np.sin(psi_angles) psi_cos = np.cos(psi_angles) chi_sin = np.sin(chi_angles) chi_cos = np.cos(chi_angles) features = np.hstack([phi_sin, phi_cos, psi_sin, psi_cos, chi_sin, chi_cos]) elif args.feature_type == "residue-residue-distances": print "computing residue-residue distances" features, _ = md.compute_contacts(traj, scheme="ca", periodic=False) elif args.feature_type == "inverse-residue-residue-distances": print "computing inverse residue-residue distances" features, _ = md.compute_contacts(traj, scheme="ca", periodic=False) features = np.reciprocal(features) else: raise Exception, "Unknown feature type '%s'", args.features return features, args.feature_type
frame = len(phi[1]) files = open(main_files, 'w') char = '' for frame_cur in range(0, frame): for index in range(0, angle_mount): phi_cur = phi[1][frame_cur][index] psi_cur = psi[1][frame_cur][index] char = '%10.6lf %10.6lf %s' % (phi_cur, psi_cur, char) char = '%s \n' % (char) files.write(char) char = '' files.close() files = open(side_files, 'w') chi1 = md.compute_chi1(traj) chi2 = md.compute_chi2(traj) chi3 = md.compute_chi3(traj) chi4 = md.compute_chi4(traj) for frame_cur in range(0, frame): for index in range(0, len(chi1[0])): chi_cur = chi1[1][frame_cur][index] char = '%10.6lf %s' % (chi_cur, char) for index in range(0, len(chi2[0])): chi_cur = chi2[1][frame_cur][index] char = '%10.6lf %s' % (chi_cur, char) for index in range(0, len(chi3[0])): chi_cur = chi3[1][frame_cur][index] char = '%10.6lf %s' % (chi_cur, char) for index in range(0, len(chi4[0])): chi_cur = chi4[1][frame_cur][index]
def compare_dihedral_distributions(args): msm = joblib.load(args.msm_model_file) print "reading trajectory" traj = md.load(args.input_traj, top=args.pdb_file) print "computing dihedrals" if args.angle_type == "phi-psi": _, phi_angles = md.compute_phi(traj, periodic=False) _, psi_angles = md.compute_psi(traj, periodic=False) # first residue has no phi angle # last residue has no psi angle # so we only have pairs for residues 1 to n - 2 angles = np.stack([phi_angles[:, :-1], psi_angles[:, 1:]], axis=2) # 1-based indexing resids = range(2, traj.n_residues) elif args.angle_type == "chi": atom_indices, chi_angles = md.compute_chi1(traj, periodic=False) angles = chi_angles.reshape(chi_angles.shape[0], chi_angles.shape[1], -1) # not all residues have chi dihedrals top = traj.topology # convert to 1-based indexing resids = [top.atom(atoms[0]).residue.index + 1 for atoms in atom_indices] for state_1 in xrange(msm.n_states - 1): state_1_frames = [idx for idx, state in enumerate(msm.labels) \ if state == state_1] state_1_angles = angles[state_1_frames, :, :] for state_2 in xrange(state_1 + 1, msm.n_states): if msm.sym_counts[state_1, state_2] > 0: print "Testing State %s vs State %s" % (state_1, state_2) state_2_frames = [idx for idx, state in enumerate(msm.labels) \ if state == state_2] state_2_angles = angles[state_2_frames, :, :] pvalues = test_residue_dihedral_distributions(state_1_angles, state_2_angles, args.n_bins) if len(pvalues) != len(resids): raise Exception("Number of residue ids (%s) and p-values (%s) mismatched" % (len(resids), len(pvalues))) residue_pvalues = zip(resids, pvalues) flname = os.path.join(args.output_dir, "state_dihedral_tests_%s_%s_%s.tsv" % (args.angle_type, state_1, state_2)) with open(flname, "w") as fl: for resid, pvalue in residue_pvalues: fl.write("%s\t%s\n" % (resid, pvalue))