def test_compute_dihedrals(self): indices = np.array([[0,1,2,3]]) rads = _dihedralcalc.compute_dihedrals(self.traj, indices, degrees=False) eq(rads.shape, (3, 1)) eq(rads.flatten(), np.array([0, 0, np.pi])) degrees = _dihedralcalc.compute_dihedrals(self.traj, indices, degrees=True) eq(degrees.shape, (3,1)) eq(degrees.flatten(), np.array([0, 0, 180]))
def test_compute_dihedrals(self): indices = np.array([[0, 1, 2, 3]]) rads = _dihedralcalc.compute_dihedrals(self.traj, indices, degrees=False) npt.assert_array_equal(rads.shape, [3, 1]) npt.assert_array_almost_equal(rads.flatten(), [0, 0, np.pi]) degrees = _dihedralcalc.compute_dihedrals(self.traj, indices, degrees=True) npt.assert_array_equal(degrees.shape, [3, 1]) npt.assert_array_almost_equal(degrees.flatten(), [0, 0, 180])
def get_redundant_internal_coordinates(trajectory, **kwargs): """Compute internal coordinates from the cartesian coordinates This extracts all of the bond lengths, bond angles and dihedral angles from every frame in a trajectory. Parameters ---------- trajectory : msmbuilder.Trajectory Trajectory object containing the internal coordinates Additional Parameters --------------------- ibonds : np.ndarray, optional, shape[n_bonds, 2], dtype=int Each row gives the indices of two atoms involved in a bond iangles : np.ndarray, optional shape[n_angles, 3], dtype=int Each row gives the indices of three atoms which together make an angle idihedrals : np.ndarray, optional, shape[n_dihedrals, 4], dtype=int Each row gives the indices of the four atoms which together make a dihedral Notes ----- ibonds, iangles, and idihedrals will be computed usig the first frame in the trajectory, if not supplied Returns ------- internal_coords : np.ndarray, shape=[n_frames, n_bonds+n_angles+n_dihedrals] All of the internal coordinates collected into a big array, such that internal_coords[i,j] gives the jth coordinate for the ith frame. """ if 'ibonds' in kwargs and 'iangles' in kwargs and 'idihedrals' in kwargs: ibonds = kwargs['ibonds'] iangles = kwargs['iangles'] idihedrals = kwargs['idihedrals'] else: ibonds, iangles, idihedrals = get_connectivity(trajectory) # convert everything to the right shape and C ordering, since # all of these methods are in C and are going to need things to be # the right type. The methods will all do a copy for things that # aren't the right type, but hopefully we can only do the copy once # instead of three times if xyzlist really does need to be reordered # in memory xyzlist = np.array(trajectory['XYZList'], dtype=np.float32, order='c') ibonds = np.array(ibonds, dtype=np.int32, order='c') iangles = np.array(iangles, dtype=np.int32, order='c') idihedrals = np.array(idihedrals, dtype=np.int32, order='c') b = atom_distances(xyzlist, ibonds) a = bond_angles(xyzlist, iangles) d = compute_dihedrals(xyzlist, idihedrals, degrees=False) return np.hstack((b, a, d))
def ComputeDihedralsFromTrajectory(Trajectory): """ Compute dihedrals directly from trajectory files. """ traj = {} traj['XYZList'] = Trajectory['XYZList']/1.0 #convert XYZList to float type indices = dihedral.get_indices(Trajectory,'phi/psi') dihedrals = dihedral.compute_dihedrals(traj,indices,'phi/psi') return dihedrals
def prepare_trajectory(self, trajectory): """Prepare the dihedral angle representation of a trajectory, suitable for distance calculations. Parameters ---------- trajectory : msmbuilder.Trajectory An MSMBuilder trajectory to prepare Returns ------- projected_angles : ndarray A 2D array of dimension len(trajectory) x (2*number of dihedral angles per frame), such that in each row, the first half of the entries contain the cosine of the dihedral angles and the later dihedral angles contain the sine of the dihedral angles. This transform is necessary so that distance calculations preserve the periodic symmetry. """ traj_length = len(trajectory['XYZList']) if self.angles=='user': indices = self.read_dihedral_indices(self.userfilename) else: if self.indices is None: indices = _dihedralcalc.get_indices(trajectory, self.angles) else: indices = self.indices dihedrals = _dihedralcalc.compute_dihedrals(trajectory, indices, degrees=False) # these dihedrals go between -pi and pi but obviously because of the # periodicity, when we take distances we want the distance between -179 # and +179 to be very close, so we need to do a little transform num_dihedrals = dihedrals.shape[1] transformed = np.empty((traj_length, 2 * num_dihedrals)) transformed[:, 0:num_dihedrals] = np.cos(dihedrals) transformed[:, num_dihedrals:2*num_dihedrals] = np.sin(dihedrals) return np.double(transformed)
def main(genfile): dir=os.path.dirname(genfile) traj=Trajectory.load_from_lhdf(genfile) rmsd=numpy.loadtxt('%s.rmsd.dat' % genfile.split('.lh5')[0]) atom_pairs=numpy.loadtxt('./atompairs.dat', dtype=int) names=numpy.loadtxt('./atompairs-map.txt', usecols=(2,), dtype=str) indices=[i for i in atom_pairs] for (ind, name) in zip(indices, names): index1=numpy.zeros((1,2)) # here i just need to tell _dihedralcalc that we have one set of 4 coordinates index1[0]=ind pairmetric= metrics.AtomPairs(metric='euclidean', p=1, atom_pairs=index1) distances=pairmetric.prepare_trajectory(traj) pylab.figure() pylab.scatter(rmsd, distances, label=name) pylab.legend() pylab.show() numpy.savetxt('%s_%spair.dat' % (genfile.split('.lh5')[0], name), distances) index1=numpy.zeros((1,4)) # here i just need to tell _dihedralcalc that we have one set of 4 coordinates index1[0]=numpy.loadtxt('omega_indices.txt', dtype=int, ndmin=1) index2=numpy.zeros((1,4)) # here i just need to tell _dihedralcalc that we have one set of 4 coordinates index2[0]=numpy.loadtxt('phi_indices.txt', dtype=int, ndmin=1) indices=[index1, index2] names=['omega', 'phi'] for (ind, name) in zip(indices, names): dihed=_dihedralcalc.compute_dihedrals(traj['XYZList'], ind, degrees=True) dihed=[i[0] for i in dihed] numpy.savetxt('%s_%s.dat' % (genfile.split('.lh5')[0], name), dihed) atom_pairs=numpy.loadtxt('conformation_pairs.dat', dtype=int) names=['oxos_dist', 'hydrophob_dist'] for (pair, name) in zip(atom_pairs, names): index=numpy.zeros((1,2), dtype=int) index[0]=pair metric= metrics.AtomPairs(metric='euclidean', p=1, atom_pairs=index) distances=metric.prepare_trajectory(traj) distances=[i[0]*10 for i in distances] pylab.figure() pylab.scatter(rmsd, distances, label=name) pylab.legend() pylab.show() numpy.savetxt('%s_pairs.dat' % genfile.split('.lh5')[0], distances)
def prepare_trajectory(self, trajectory): """Prepare the dihedral angle representation of a trajectory, suitable for distance calculations. Parameters ---------- trajectory : msmbuilder.Trajectory An MSMBuilder trajectory to prepare Returns ------- projected_angles : ndarray A 2D array of dimension len(trajectory) x (2*number of dihedral angles per frame), such that in each row, the first half of the entries contain the cosine of the dihedral angles and the later dihedral angles contain the sine of the dihedral angles. This transform is necessary so that distance calculations preserve the periodic symmetry. """ traj_length = len(trajectory['XYZList']) if self.indices is None: indices = _dihedralcalc.get_indices(trajectory, self.angles) else: indices = self.indices dihedrals = _dihedralcalc.compute_dihedrals(trajectory, indices, degrees=False) # these dihedrals go between -pi and pi but obviously because of the # periodicity, when we take distances we want the distance between -179 # and +179 to be very close, so we need to do a little transform num_dihedrals = dihedrals.shape[1] transformed = np.empty((traj_length, 2 * num_dihedrals)) transformed[:, 0:num_dihedrals] = np.cos(dihedrals) transformed[:, num_dihedrals:2 * num_dihedrals] = np.sin(dihedrals) return np.double(transformed)
def get_dihedrals(dir, ind, traj): dihed=_dihedralcalc.compute_dihedrals(traj['XYZList'], ind, degrees=True) return numpy.array([i[0] for i in dihed])
group_wts[group] = numpy.hstack( (group_wts[group], get_weights(types))) indices = [i - 1 for i in groups[group]] x = [coor[0] for coor in conf['XYZList'][0][indices]] y = [coor[1] for coor in conf['XYZList'][0][indices]] z = [coor[2] for coor in conf['XYZList'][0][indices]] # here just using one frame coors = numpy.vstack((x, y, z)) coms[group] = numpy.array([ numpy.average(numpy.array(x), weights=group_wts[group]), numpy.average(numpy.array(y), weights=group_wts[group]), numpy.average(numpy.array(z), weights=group_wts[group]) ]) # msmbuilder _dihedralcalc needs a trajectory-like object of coordinates, so stack the # com coordinates com_traj = numpy.zeros( (1, 4, 3)) # here the size is 1 because one frame, 4 groups for the dihedral indices = numpy.zeros( (1, 4) ) # here i just need to tell _dihedralcalc that we have one set of 4 coordinates for group in groups.keys(): index = group - 1 com_traj[0][index] = coms[group] indices[0][index] = index pseudodihed = _dihedralcalc.compute_dihedrals(com_traj, indices, degrees=True) print residues print pseudodihed
import numpy as np from msmbuilder import Trajectory from msmbuilder.geometry import dihedral ff_list = ["amber96","amber99","amber99sbnmr-ildn","oplsaa","charmm27"] for ff in ff_list: print(ff) directory = "/home/kyleb/dat/lvbp/%s/" % ff R = Trajectory.load_from_xtc([directory + "/production/trajout.xtc"],directory + "/final.pdb") ind = dihedral.get_indices(R) di = dihedral.compute_dihedrals(R,ind).T phi = di[0] psi = di[3] data = np.array([phi,psi]) np.savez_compressed(directory + "/rama.npz", data) """ ff = "amber99" R = Trajectory.load_from_xtc(["/home/kyleb/dat/lvbp/GA-%s/md/trajout.xtc"%ff],"/home/kyleb/dat/lvbp/GA-%s/equil/native.pdb"%ff) ind = dihedral.get_indices(R) di = dihedral.compute_dihedrals(R,ind) io.saveh("/home/kyleb/dat/lvbp/GA-%s/rama.h5"%ff,di) """
for resid in residues[group]: array=numpy.where(conf['ResidueID']==resid)[0] types=conf['AtomNames'][array] if resid==start: groups[group]=conf['AtomID'][array] group_wts[group]=get_weights(types) else: groups[group]=numpy.hstack((groups[group], conf['AtomID'][array])) group_wts[group]=numpy.hstack((group_wts[group], get_weights(types))) indices=[i-1 for i in groups[group]] x=[coor[0] for coor in conf['XYZList'][0][indices]] y=[coor[1] for coor in conf['XYZList'][0][indices]] z=[coor[2] for coor in conf['XYZList'][0][indices]] # here just using one frame coors=numpy.vstack((x,y,z)) coms[group]=numpy.array([numpy.average(numpy.array(x), weights=group_wts[group]), numpy.average(numpy.array(y), weights=group_wts[group]), numpy.average(numpy.array(z), weights=group_wts[group])]) # msmbuilder _dihedralcalc needs a trajectory-like object of coordinates, so stack the # com coordinates com_traj=numpy.zeros((1,4, 3)) # here the size is 1 because one frame, 4 groups for the dihedral indices=numpy.zeros((1,4)) # here i just need to tell _dihedralcalc that we have one set of 4 coordinates for group in groups.keys(): index=group-1 com_traj[0][index]=coms[group] indices[0][index]=index pseudodihed=_dihedralcalc.compute_dihedrals(com_traj, indices, degrees=True) print residues print pseudodihed