def test_right_type_impropers(self): assert_equal( self.igroup.dihedrals(), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions)) assert_equal( self.igroup.dihedrals(pbc=True), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions, box=self.u.dimensions)) assert_equal( self.igroup.values(), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions)) assert_equal( self.igroup.values(pbc=True), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions, box=self.u.dimensions))
def _single_frame(self): phi_angles = calc_dihedrals(self.ag1.positions, self.ag2.positions, self.ag3.positions, self.ag4.positions, box=self.ag1.dimensions) psi_angles = calc_dihedrals(self.ag2.positions, self.ag3.positions, self.ag4.positions, self.ag5.positions, box=self.ag1.dimensions) phi_psi = [(phi, psi) for phi, psi in zip(phi_angles, psi_angles)] self.angles.append(phi_psi)
def psi(u): A = u.select_atoms(atom1).positions B = u.select_atoms(atom2).positions C = u.select_atoms(atom3).positions D = u.select_atoms(atom4).positions psi = calc_dihedrals(A, B, C, D) return np.rad2deg(psi)
def _single_frame(self): angle = calc_dihedrals(self.ag1.positions, self.ag2.positions, self.ag3.positions, self.ag4.positions, box=self.ag1.dimensions) self.angles.append(angle)
def calc_torsion(dihedral): """atom 1 -4 are valid atom selections. torsion in degrees is returned""" A = u.select_atoms(dihedral.atom1).positions B = u.select_atoms(dihedral.atom2).positions C = u.select_atoms(dihedral.atom3).positions D = u.select_atoms(dihedral.atom4).positions dihe = calc_dihedrals(A, B, C, D) return np.rad2deg(dihe)
def test_right_type_impropers(self): assert_equal(self.igroup.dihedrals(), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions)) assert_equal(self.igroup.dihedrals(pbc=True), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions, box=self.u.dimensions)) assert_equal(self.igroup.values(), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions)) assert_equal(self.igroup.values(pbc=True), calc_dihedrals(self.igroup.atom1.positions, self.igroup.atom2.positions, self.igroup.atom3.positions, self.igroup.atom4.positions, box=self.u.dimensions))
def test_right_type_impropers(self, igroup, PSFDCD): assert_equal( igroup.dihedrals(), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions)) assert_equal( igroup.dihedrals(pbc=True), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions, box=PSFDCD.dimensions)) assert_equal( igroup.values(), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions)) assert_equal( igroup.values(pbc=True), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions, box=PSFDCD.dimensions))
def test_right_type_impropers(self, igroup, PSFDCD): assert_equal(igroup.dihedrals(), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions)) assert_equal(igroup.dihedrals(pbc=True), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions, box=PSFDCD.dimensions)) assert_equal(igroup.values(), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions)) assert_equal(igroup.values(pbc=True), calc_dihedrals(igroup.atom1.positions, igroup.atom2.positions, igroup.atom3.positions, igroup.atom4.positions, box=PSFDCD.dimensions))
def dihe_measure(sel1, sel2, sel3, sel4): """ This functions measures the dihedral angle between 4 specified atoms and returns the dihedral value between 0 and 360 degrees. The input selections have to be single atoms. """ from numpy import rad2deg for sel in (sel1, sel2, sel3, sel4): if len(sel) != 1: raise NotSingleAtomSelectionError return ((float( rad2deg( mdadist.calc_dihedrals(sel1.positions, sel2.positions, sel3.positions, sel4.positions, backend='OpenMP')))) + 360) % 360
def test_dihedrals(self): from MDAnalysis.lib.distances import calc_dihedrals a2 = (self.a + self.box * (-1, 0, 0)).astype(np.float32) b2 = (self.b + self.box * (1, 0, 1)).astype(np.float32) c2 = (self.c + self.box * (-2, 5, -7)).astype(np.float32) d2 = (self.d + self.box * (0, -5, 0)).astype(np.float32) ref = calc_dihedrals(self.a, self.b, self.c, self.d, backend=self.backend) test1 = calc_dihedrals(a2, self.b, self.c, self.d, box=self.box, backend=self.backend) test2 = calc_dihedrals(self.a, b2, self.c, self.d, box=self.box, backend=self.backend) test3 = calc_dihedrals(self.a, self.b, c2, self.d, box=self.box, backend=self.backend) test4 = calc_dihedrals(self.a, self.b, self.c, d2, box=self.box, backend=self.backend) test5 = calc_dihedrals(a2, b2, c2, d2, box=self.box, backend=self.backend) for val in [test1, test2, test3, test4, test5]: assert_almost_equal( ref, val, self.prec, err_msg="Min image in dihedral calculation failed")
def test_dihedrals(self): from MDAnalysis.lib.distances import calc_dihedrals a2 = (self.a + self.box * (-1, 0, 0)).astype(np.float32) b2 = (self.b + self.box * (1, 0, 1)).astype(np.float32) c2 = (self.c + self.box * (-2, 5, -7)).astype(np.float32) d2 = (self.d + self.box * (0, -5, 0)).astype(np.float32) ref = calc_dihedrals(self.a, self.b, self.c, self.d) test1 = calc_dihedrals(a2, self.b, self.c, self.d, box=self.box) test2 = calc_dihedrals(self.a, b2, self.c, self.d, box=self.box) test3 = calc_dihedrals(self.a, self.b, c2, self.d, box=self.box) test4 = calc_dihedrals(self.a, self.b, self.c, d2, box=self.box) test5 = calc_dihedrals(a2, b2, c2, d2, box=self.box) for val in [test1, test2, test3, test4, test5]: assert_almost_equal(ref, val, self.prec, err_msg="Min image in dihedral calculation failed")
def main(): """Run main procedure.""" # TODO(schneiderfelipe): accept multiple files parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("traj_files", nargs="+") args = parser.parse_args() gnorms = [] energies = [] all_atomnos = [] all_atomcoords = [] for traj_file in args.traj_files: atomnos, comments, atomcoords = read_xyz(traj_file) all_atomnos.extend(atomnos) all_atomcoords.extend(atomcoords) for comment in comments: fields = comment.split() gnorms.append(float(fields[3])) energies.append(float(fields[1])) energies = np.array(energies) energies -= energies.min() energies *= hartree * N_A / (kilo * calorie) u = mda.Universe.empty(n_atoms=len(all_atomnos[0]), trajectory=True) u.add_TopologyAttr("type", [element[i] for i in all_atomnos[0]]) u.load_new(all_atomcoords, order="fac") print(u) selection = None print("(enter 'q' for exit, 'h' for help)") while True: code = input("select> ").strip().split() if code[0] == "q": break elif code[0] == "h": for key in commands: print(f"{key:15s}: {commands[key]}") elif code[0] == "e": fig, ax = plt.subplots(2) ax[0].plot(energies) ax[0].set_xlabel("frame") ax[0].set_ylabel("energy (kcal/mol)") ax[1].plot(gnorms) ax[1].set_xlabel("frame") ax[1].set_ylabel("grad. norm (Eh/a0)") plt.show() elif code[0] == "s": print(selection) if selection is not None: print(selection_text) elif code[0] == "pca": if selection is None: print("empty selection, doing nothing") continue p = pca.PCA(u, select=selection_text) p.run() n_pcs = np.where(p.cumulated_variance > 0.95)[0][0] print(n_pcs) print(p.cumulated_variance[0:n_pcs]) pca_space = p.transform(selection, n_components=n_pcs) print(pca_space) print(pca.cosine_content(pca_space, 0)) elif code[0] == "p": if selection is None: print("empty selection, doing nothing") continue n = len(selection) if n == 2: data_label = "bond length (Å)" elif n == 3: data_label = "bond angle (°)" elif n == 4: data_label = "dihedral angle (°)" else: print("too few or too many indices") continue data = [] for i, (e, ts) in enumerate(zip(energies, u.trajectory)): if n == 2: d = distances.calc_bonds( selection[0].position, selection[1].position ) elif n == 3: d = np.degrees( distances.calc_angles( selection[0].position, selection[1].position, selection[2].position, ) ) elif n == 4: d = np.degrees( distances.calc_dihedrals( selection[0].position, selection[1].position, selection[2].position, selection[3].position, ) ) data.append(d) if i % 100 == 0 or i == len(u.trajectory) - 1: print( f"frame = {ts.frame:4d}: e = {e:5.1f} kcal/mol, {data_label.split('(')[0][:-1]} = {d:7.3f} {data_label[-2]}" ) data = np.array(data) fig, ax = plt.subplots(1, 2) ax[0].plot(data) ax[0].set_xlabel("frame") ax[0].set_ylabel(data_label) ax[1].plot(energies, data, "o", label="data points") ax[1].set_xlabel("energy (kcal/mol)") ax[1].set_ylabel(data_label) if n == 2: dx = 0.1 elif n == 3: dx = 10.0 elif n == 4: dx = 10.0 res = stats.binned_statistic( data, energies, "min", min(25, (data.max() - data.min()) / dx) ) # print(res.statistic) mask = np.isnan(res.statistic) res.statistic[mask] = np.interp( np.flatnonzero(mask), np.flatnonzero(~mask), res.statistic[~mask] ) # print(res.statistic) # ax[1].hlines(res.statistic, res.bin_edges[:-1], res.bin_edges[1:], colors='g', lw=2, label='binned min. energies') ax[1].barh( (res.bin_edges[:-1] + res.bin_edges[1:]) / 2, res.statistic, align="center", height=res.bin_edges[1:] - res.bin_edges[:-1], alpha=0.25, label="binned min. energies", ) ax[1].legend() plt.show() else: try: selection_text = " ".join(code) selection = u.select_atoms(selection_text) except mda.exceptions.SelectionError as e: print(e) print("bye")