def map(self, traj): # TODO: can we merge phi_inds and psi_inds to only call # compute_dihedrals once? y1 = compute_dihedrals(traj, self._phi_inds).astype(np.float32) y2 = compute_dihedrals(traj, self._psi_inds).astype(np.float32) rad = np.hstack((y1, y2)) if self.deg: return np.rad2deg(rad) else: return rad
def read_and_featurize_custom(traj_file, condition=None, location=None, dihedral_residues = None, distance_residues = None): top = md.load_frame(traj_file,index = 0).topology #atom_indices = [a.index for a in top.atoms if a.residue.resSeq != 130] atom_indices = [a.index for a in top.atoms] traj = md.load(traj_file, atom_indices=atom_indices) print traj_file #print traj #print("loaded trajectory") ''' a = time.time() featurizer = DihedralFeaturizer(types = ['phi', 'psi', 'chi2']) features = featurizer.transform(traj) b = time.time() #print(b-a) print("original features has dim") print(np.shape(features)) ''' a = time.time() phi_tuples = phi_indices(traj.topology, dihedral_residues) psi_tuples = psi_indices(traj.topology, dihedral_residues) chi2_tuples = chi2_indices(traj.topology, dihedral_residues) #if distance_residues is not None: #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") phi_angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=phi_tuples)) psi_angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=psi_tuples)) chi2_angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=chi2_tuples)) manual_features = np.concatenate([np.sin(phi_angles), np.cos(phi_angles), np.sin(psi_angles), np.cos(psi_angles), np.sin(chi2_angles), np.cos(chi2_angles)]) b = time.time() #print(b-a) print("new features has shape: ") print(np.shape(manual_features)) if condition is None: condition = get_condition(traj_file) if location is None: location = "/scratch/users/enf/b2ar_analysis/features_allprot" verbosedump(manual_features, "%s/%s.h5" %(location, condition))
def __init__(self, directory="/home/harrigan/projects/msmbuilder/Tutorial/"): # Load from disk print "Loading trajectories" traj_list_in = get_data.get_from_fs(os.path.join(directory, 'XTC'), os.path.join(directory, 'native.pdb')) # Compute dihedrals traj_list_angles = list() traj_list = list() print "Computing Dihedrals" for traj in traj_list_in: # Compute actual angles diheds = dihedral.compute_dihedrals(traj, Dipeptide.INDICES, opt=False) # Compute sin and cos num_diheds = diheds.shape[1] per_diheds = np.zeros((diheds.shape[0], num_diheds * 2)) per_diheds[:, 0:num_diheds] = np.sin(diheds) per_diheds[:, num_diheds:num_diheds * 2] = np.cos(diheds) distances = distance.compute_distances(traj, Dipeptide.PAIRS, periodic=True, opt=False) distances = np.log(distances) # Save traj_list.append(distances) traj_list_angles.append(diheds) # traj_list.append(per_diheds) self.traj_list = traj_list self.traj_list_angles = traj_list_angles
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 : mdtraj.Trajectory Trajectory object containing the internal coordinates Other 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.xyz, 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 = compute_distances(xyzlist, ibonds) a = compute_angles(xyzlist, iangles) d = compute_dihedrals(xyzlist, idihedrals, degrees=False) return np.hstack((b, a, d))
def prepare_trajectory(self, trajectory): """Prepare the dihedral angle representation of a trajectory, suitable for distance calculations. Parameters ---------- trajectory : mdtraj.Trajectory An MDTraj 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 = trajectory.n_frames if self.angles == 'user': indices = self.read_dihedral_indices(self.userfilename) dihedrals = _dihedralcalc.compute_dihedrals(trajectory, indices) else: if self.indices is None: dihedrals = np.hstack(getattr(_dihedralcalc, 'compute_%s' % e)(trajectory)[1] for e in self.angles.split('/')) else: dihedrals = _dihedralcalc.compute_dihedrals(trajectory, self.indices) # 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 read_and_featurize(traj_file, features_dir = None, condition=None, dihedral_types = ["phi", "psi", "chi1", "chi2"], dihedral_residues = None, resSeq_pairs = None, iterative = True): a = time.time() dihedral_indices = [] residue_order = [] if len(dihedral_residues) > 0: for dihedral_type in dihedral_types: if dihedral_type == "phi": dihedral_indices.append(phi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "psi": dihedral_indices.append(psi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi1": dihedral_indices.append(chi1_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi2": dihedral_indices.append(chi2_indices(fix_topology(top), dihedral_residues)) #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") dihedral_angles = [] for dihedral_type in dihedral_indices: angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=dihedral_type)) dihedral_angles.append(np.sin(angles)) dihedral_angles.append(np.cos(angles)) manual_features = np.transpose(np.concatenate(dihedral_angles)) if len(resSeq_pairs) > 0: top = md.load_frame(traj_file, index=0).topology resIndex_pairs = convert_resSeq_to_resIndex(top, resSeq_pairs) contact_features = [] if iterative: try: for chunk in md.iterload(traj_file, chunk = 1000): # chunk = fix_traj(chunk) #chunk = md.load(traj_file,stride=1000) #print(resIndex_pairs[0:10]) chunk_features = md.compute_contacts(chunk, contacts = resIndex_pairs, scheme = 'closest-heavy', ignore_nonprotein=False)[0] print(np.shape(chunk_features)) contact_features.append(chunk_features) contact_features = np.concatenate(contact_features) except Exception,e: print str(e) print("Failed") return #traj = md.load(traj_file) #contact_features = md.compute_contacts(chunk, contacts = contact_residue_pairs, scheme = 'closest-heavy', ignore_nonprotein=False)[0] else: try: traj = md.load(traj_file) contact_features = md.compute_contacts(traj, contacts = resIndex_pairs, scheme = 'closest-heavy', ignore_nonprotein=False)[0] except Exception,e: print str(e) print("Failed for traj") return
def __init__(self, directory="/home/harrigan/projects/msmbuilder/Tutorial/"): # Load from disk print "Loading trajectories" traj_list_in = get_data.get_from_fs( os.path.join(directory, 'XTC'), os.path.join(directory, 'native.pdb')) # Compute dihedrals traj_list_angles = list() traj_list = list() print "Computing Dihedrals" for traj in traj_list_in: # Compute actual angles diheds = dihedral.compute_dihedrals(traj, Dipeptide.INDICES, opt=False) # Compute sin and cos num_diheds = diheds.shape[1] per_diheds = np.zeros((diheds.shape[0], num_diheds * 2)) per_diheds[:, 0:num_diheds] = np.sin(diheds) per_diheds[:, num_diheds:num_diheds * 2] = np.cos(diheds) distances = distance.compute_distances(traj, Dipeptide.PAIRS, periodic=True, opt=False) distances = np.log(distances) # Save traj_list.append(distances) traj_list_angles.append(diheds) # traj_list.append(per_diheds) self.traj_list = traj_list self.traj_list_angles = traj_list_angles
def read_and_featurize_custom(traj_file, condition=None, location=None, dihedral_residues=None, distance_residues=None): top = md.load_frame(traj_file, index=0).topology #atom_indices = [a.index for a in top.atoms if a.residue.resSeq != 130] atom_indices = [a.index for a in top.atoms] traj = md.load(traj_file, atom_indices=atom_indices) print(traj_file) #print traj #print("loaded trajectory") ''' a = time.time() featurizer = DihedralFeaturizer(types = ['phi', 'psi', 'chi2']) features = featurizer.transform(traj) b = time.time() #print(b-a) print("original features has dim") print(np.shape(features)) ''' a = time.time() phi_tuples = phi_indices(traj.topology, dihedral_residues) psi_tuples = psi_indices(traj.topology, dihedral_residues) chi2_tuples = chi2_indices(traj.topology, dihedral_residues) #if distance_residues is not None: #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") phi_angles = np.transpose( ManualDihedral.compute_dihedrals(traj=traj, indices=phi_tuples)) psi_angles = np.transpose( ManualDihedral.compute_dihedrals(traj=traj, indices=psi_tuples)) chi2_angles = np.transpose( ManualDihedral.compute_dihedrals(traj=traj, indices=chi2_tuples)) manual_features = np.concatenate([ np.sin(phi_angles), np.cos(phi_angles), np.sin(psi_angles), np.cos(psi_angles), np.sin(chi2_angles), np.cos(chi2_angles) ]) b = time.time() #print(b-a) print("new features has shape: ") print((np.shape(manual_features))) if condition is None: condition = get_condition(traj_file) if location is None: location = "/scratch/users/enf/b2ar_analysis/features_allprot" verbosedump(manual_features, "%s/%s.h5" % (location, condition))
def read_and_featurize_iter(traj_file, features_dir = None, condition=None, dihedral_types = ["phi", "psi", "chi1", "chi2"], dihedral_residues = None, contact_residues = None): a = time.time() dihedral_indices = [] residue_order = [] if len(dihedral_residues) > 0: for dihedral_type in dihedral_types: if dihedral_type == "phi": dihedral_indices.append(phi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "psi": dihedral_indices.append(psi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi1": dihedral_indices.append(chi1_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi2": dihedral_indices.append(chi2_indices(fix_topology(top), dihedral_residues)) #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") dihedral_angles = [] for dihedral_type in dihedral_indices: angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=dihedral_type)) dihedral_angles.append(np.sin(angles)) dihedral_angles.append(np.cos(angles)) manual_features = np.transpose(np.concatenate(dihedral_angles)) if len(contact_residues) > 0: contact_features = [] for chunk in md.iterload(traj_file, chunk = 10000): fixed_traj = fix_traj(chunk) fixed_top = fixed_traj.topology distance_residues = [] res_objects = [r for r in fixed_top.residues] for r in contact_residues: for res in res_objects: if res.resSeq == r and len(res._atoms) > 5: #print res._atoms distance_residues.append(res.index) if len(contact_residues) != len(distance_residues): print "Residues are missing" print len(contact_residues) print len(distance_residues) #sys.exit() #return None combinations = itertools.combinations(distance_residues, 2) pairs = [c for c in combinations] #print pairs contact_features.append(md.compute_contacts(fixed_traj, contacts = pairs, scheme = 'closest-heavy', ignore_nonprotein=False)[0]) contact_features = np.concatenate(contact_features) if len(dihedral_residues) > 0: manual_features = np.column_stack((manual_features, contact_features)) else: manual_features = contact_features b = time.time() print("new features %s has shape: " %traj_file) print(np.shape(manual_features)) if condition is None: condition = get_condition(traj_file) verbosedump(manual_features, "%s/%s.h5" %(features_dir, condition))
def read_and_featurize_custom(traj_file, features_dir = None, condition=None, dihedral_types = ["phi", "psi", "chi1", "chi2"], dihedral_residues = None, contact_residues = None): #if "23" not in traj_file and "24" not in traj_file: return top = md.load_frame(traj_file,index = 0).topology #atom_indices = [a.index for a in top.atoms if a.residue.resSeq != 130] atom_indices = [a.index for a in top.atoms] traj = md.load(traj_file, atom_indices=atom_indices) print traj_file #print traj #print("loaded trajectory") ''' a = time.time() featurizer = DihedralFeaturizer(types = ['phi', 'psi', 'chi2']) features = featurizer.transform(traj) b = time.time() #print(b-a) print("original features has dim") print(np.shape(features)) ''' a = time.time() dihedral_indices = [] residue_order = [] if len(dihedral_residues) > 0: for dihedral_type in dihedral_types: if dihedral_type == "phi": dihedral_indices.append(phi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "psi": dihedral_indices.append(psi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi1": dihedral_indices.append(chi1_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi2": dihedral_indices.append(chi2_indices(fix_topology(top), dihedral_residues)) #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") dihedral_angles = [] for dihedral_type in dihedral_indices: angles = np.transpose(ManualDihedral.compute_dihedrals(traj=traj,indices=dihedral_type)) dihedral_angles.append(np.sin(angles)) dihedral_angles.append(np.cos(angles)) manual_features = np.transpose(np.concatenate(dihedral_angles)) if len(contact_residues) > 0: fixed_traj = fix_traj(traj) fixed_top = fixed_traj.topology distance_residues = [] res_objects = [r for r in fixed_top.residues] for r in contact_residues: for res in res_objects: if res.resSeq == r and len(res._atoms) > 5: #print res._atoms distance_residues.append(res.index) if len(contact_residues) != len(distance_residues): print "Residues are missing" print len(contact_residues) print len(distance_residues) #sys.exit() #return None combinations = itertools.combinations(distance_residues, 2) pairs = [c for c in combinations] #print pairs contact_features = md.compute_contacts(traj, contacts = pairs, scheme = 'closest-heavy', ignore_nonprotein=False)[0] #print contact_features #print(np.shape(contact_features)) if len(dihedral_residues) > 0: manual_features = np.column_stack((manual_features, contact_features)) else: manual_features = contact_features b = time.time() print("new features %s has shape: " %traj_file) print(np.shape(manual_features)) if condition is None: condition = get_condition(traj_file) verbosedump(manual_features, "%s/%s.h5" %(features_dir, condition))
def read_and_featurize_iter(traj_file, features_dir=None, condition=None, dihedral_types=["phi", "psi", "chi1", "chi2"], dihedral_residues=None, contact_residues=None): a = time.time() dihedral_indices = [] residue_order = [] if len(dihedral_residues) > 0: for dihedral_type in dihedral_types: if dihedral_type == "phi": dihedral_indices.append( phi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "psi": dihedral_indices.append( psi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi1": dihedral_indices.append( chi1_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi2": dihedral_indices.append( chi2_indices(fix_topology(top), dihedral_residues)) #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") dihedral_angles = [] for dihedral_type in dihedral_indices: angles = np.transpose( ManualDihedral.compute_dihedrals(traj=traj, indices=dihedral_type)) dihedral_angles.append(np.sin(angles)) dihedral_angles.append(np.cos(angles)) manual_features = np.transpose(np.concatenate(dihedral_angles)) if len(contact_residues) > 0: contact_features = [] for chunk in md.iterload(traj_file, chunk=10000): fixed_traj = fix_traj(chunk) fixed_top = fixed_traj.topology distance_residues = [] res_objects = [r for r in fixed_top.residues] for r in contact_residues: for res in res_objects: if res.resSeq == r and len(res._atoms) > 5: #print res._atoms distance_residues.append(res.index) if len(contact_residues) != len(distance_residues): print("Residues are missing") print(len(contact_residues)) print(len(distance_residues)) #sys.exit() #return None combinations = itertools.combinations(distance_residues, 2) pairs = [c for c in combinations] #print pairs contact_features.append( md.compute_contacts(fixed_traj, contacts=pairs, scheme='closest-heavy', ignore_nonprotein=False)[0]) contact_features = np.concatenate(contact_features) if len(dihedral_residues) > 0: manual_features = np.column_stack( (manual_features, contact_features)) else: manual_features = contact_features b = time.time() print(("new features %s has shape: " % traj_file)) print((np.shape(manual_features))) if condition is None: condition = get_condition(traj_file) verbosedump(manual_features, "%s/%s.h5" % (features_dir, condition))
def read_and_featurize_custom(traj_file, features_dir=None, condition=None, dihedral_types=["phi", "psi", "chi1", "chi2"], dihedral_residues=None, contact_residues=None): #if "23" not in traj_file and "24" not in traj_file: return top = md.load_frame(traj_file, index=0).topology #atom_indices = [a.index for a in top.atoms if a.residue.resSeq != 130] atom_indices = [a.index for a in top.atoms] traj = md.load(traj_file, atom_indices=atom_indices) print(traj_file) #print traj #print("loaded trajectory") ''' a = time.time() featurizer = DihedralFeaturizer(types = ['phi', 'psi', 'chi2']) features = featurizer.transform(traj) b = time.time() #print(b-a) print("original features has dim") print(np.shape(features)) ''' a = time.time() dihedral_indices = [] residue_order = [] if len(dihedral_residues) > 0: for dihedral_type in dihedral_types: if dihedral_type == "phi": dihedral_indices.append( phi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "psi": dihedral_indices.append( psi_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi1": dihedral_indices.append( chi1_indices(fix_topology(top), dihedral_residues)) if dihedral_type == "chi2": dihedral_indices.append( chi2_indices(fix_topology(top), dihedral_residues)) #print("new features has dim %d" %(2*len(phi_tuples) + 2*len(psi_tuples) + 2*len(chi2_tuples))) #print("feauturizing manually:") dihedral_angles = [] for dihedral_type in dihedral_indices: angles = np.transpose( ManualDihedral.compute_dihedrals(traj=traj, indices=dihedral_type)) dihedral_angles.append(np.sin(angles)) dihedral_angles.append(np.cos(angles)) manual_features = np.transpose(np.concatenate(dihedral_angles)) if len(contact_residues) > 0: fixed_traj = fix_traj(traj) fixed_top = fixed_traj.topology distance_residues = [] res_objects = [r for r in fixed_top.residues] for r in contact_residues: for res in res_objects: if res.resSeq == r and len(res._atoms) > 5: #print res._atoms distance_residues.append(res.index) if len(contact_residues) != len(distance_residues): print("Residues are missing") print(len(contact_residues)) print(len(distance_residues)) #sys.exit() #return None combinations = itertools.combinations(distance_residues, 2) pairs = [c for c in combinations] #print pairs contact_features = md.compute_contacts(traj, contacts=pairs, scheme='closest-heavy', ignore_nonprotein=False)[0] #print contact_features #print(np.shape(contact_features)) if len(dihedral_residues) > 0: manual_features = np.column_stack( (manual_features, contact_features)) else: manual_features = contact_features b = time.time() print(("new features %s has shape: " % traj_file)) print((np.shape(manual_features))) if condition is None: condition = get_condition(traj_file) verbosedump(manual_features, "%s/%s.h5" % (features_dir, condition))