Пример #1
0
def test_distances_t_at_0(get_fn):
    times = np.array([[0, 0]], dtype=np.int32)
    a = compute_distances_t(ptraj, pairs, times, periodic=True, opt=True)
    b = compute_distances_t(ptraj, pairs, times, periodic=True, opt=False)
    c = compute_distances(ptraj[:1], pairs, periodic=True, opt=True)
    d = compute_distances(ptraj[:1], pairs, periodic=True, opt=False)
    eq(a, c)
    eq(b, d)
Пример #2
0
    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
Пример #3
0
def test_2p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=False)
    b = compute_displacements(ptraj, pairs, periodic=True, opt=False)
    assert a.shape == (len(ptraj), len(pairs))
    assert b.shape == (len(ptraj), len(pairs), 3), str(b.shape)
    b = np.sqrt(np.sum(np.square(b), axis=2))
    eq(a, b, decimal=5)
Пример #4
0
def test_2p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=False)
    b = compute_displacements(ptraj, pairs, periodic=True, opt=False)
    assert a.shape == (len(ptraj), len(pairs))
    assert b.shape == (len(ptraj), len(pairs), 3), str(b.shape)
    b = np.sqrt(np.sum(np.square(b), axis=2))
    eq(a, b, decimal=5)
Пример #5
0
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))
Пример #6
0
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))
Пример #7
0
def compute_rdf(traj, pairs=None, r_range=None, bin_width=0.005,
                periodic=True, opt=True):
    """Compute radial distribution functions for pairs in every frame.

    Parameters
    ----------
    traj : Trajectory
        Trajectory to compute radial distribution function in.
    pairs : array-like, shape=(n_pairs, 2), dtype=int, optional, default=None
        Each row gives the indices of two atoms.
    r_range : array-like, shape=(2,), optional, default=(0.0, 1.0)
        Minimum and maximum radii.
    bin_width : int, optional, default=0.005
        Width of the bins in nanometers.
    periodic : bool, default=True
        If `periodic` is True and the trajectory contains unitcell
        information, we will compute distances under the minimum image
        convention.
    opt : bool, default=True
        Use an optimized native library to compute the pair wise distances.

    Returns
    -------
    r : np.ndarray, shape=(np.diff(r_range) / bin_width - 1), dtype=float
        Radii values corresponding to the centers of the bins.
    g_r : np.ndarray, shape=(np.diff(r_range) / bin_width - 1), dtype=float
        Radial distribution function values at r.

    See also
    --------
    Topology.select_pairs

    """
    if r_range is None:
        r_range = np.array([0.0, 1.0])
    r_range = ensure_type(r_range, dtype=np.float64, ndim=1, name='r_range',
                          shape=(2,), warn_on_cast=False)
    bins = np.arange(r_range[0], r_range[1], bin_width)

    distances = compute_distances(traj, pairs, periodic=periodic, opt=opt)
    g_r, edges = np.histogram(distances, bins=bins)
    r = 0.5 * (edges[1:] + edges[:-1])

    # Normalize by volume of the spherical shell.
    # See discussion https://github.com/mdtraj/mdtraj/pull/724. There might be
    # a less biased way to accomplish this. The conclusion was that this could
    # be interesting to try, but is likely not hugely consequential. This method
    # of doing the calculations matches the implementation in other packages like
    # AmberTools' cpptraj and gromacs g_rdf.
    V = (4 / 3) * np.pi * (np.power(edges[1:], 3) - np.power(edges[:-1], 3))
    norm = len(pairs) * np.sum(1.0 / traj.unitcell_volumes) * V
    g_r = g_r.astype(np.float64) / norm  # From int64.
    return r, g_r
Пример #8
0
    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
Пример #9
0
    def prepare_trajectory(self, trajectory):
        """Prepare a trajectory for distance calculations based on the contact map.

        Each frame in the trajectory will be represented by a vector where
        each entries represents the distance between two residues in the structure.
        Depending on what contacts you pick to use, this can be a 'native biased'
        picture or not.

        Paramters
        ---------
        trajectory : mdtraj.Trajectory
            The trajectory to prepare

        Returns
        -------
        pairwise_distances : ndarray
            1D array of various residue-residue distances
        """

        xyzlist = trajectory.xyz
        num_residues = trajectory.n_residues
        num_atoms = trajectory.n_atoms

        if self.contacts == 'all':
            contacts = np.empty(((num_residues - 2) * (num_residues - 3) / 2, 2), dtype=np.int32)
            p = 0
            for (a, b) in itertools.combinations(range(num_residues), 2):
                if max(a, b) > min(a, b) + 2:
                    contacts[p, :] = [a, b]
                    p += 1
            assert p == len(contacts), 'Something went wrong generating "all"'

        else:
            num, width = self.contacts.shape
            contacts = self.contacts
            if not width == 2:
                raise ValueError('contacts must be width 2')
            if not (0 < len(np.unique(contacts[:, 0])) < num_residues):
                raise ValueError('contacts should refer to zero-based indexing of the residues')
            if not np.all(np.logical_and(0 <= np.unique(contacts), np.unique(contacts) < num_residues)):
                raise ValueError('contacts should refer to zero-based indexing of the residues')

        if self.scheme == 'ca':
            atom_contacts = np.zeros_like(contacts)
            residue_to_alpha = np.zeros(num_residues)  # zero based indexing
            for i in range(num_atoms):
                if trajectory.topology.atom(i).name == 'CA':
                    residue = trajectory.topology.residue(i).index - 1
                    residue_to_alpha[residue] = i
            # print 'contacts (residues)', contacts
            # print 'residue_to_alpja', residue_to_alpha.shape
            # print 'residue_to_alpja', residue_to_alpha
            atom_contacts = residue_to_alpha[contacts]
            # print 'atom_contacts', atom_contacts
            output = distance.compute_distances(trajectory, atom_contacts)

        elif self.scheme in ['closest', 'closest-heavy']:
            if self.scheme == 'closest':
                residue_membership = [[atom.index for atom in residue.atoms]
                                      for residue in trajectory.topology.residues]
            elif self.scheme == 'closest-heavy':
                residue_membership = [[] for i in range(num_residues)]
                for i in range(num_atoms):
                    residue = trajectory.topology.atom(i).residue.index - 1
                    if not trajectory.topology.atom(i).name.lstrip('0123456789').startswith('H'):
                        residue_membership[residue].append(i)

            # print 'Residue Membership'
            # print residue_membership
            # for row in residue_membership:
            #    for col in row:
            #        print "%s-%s" % (trajectory['AtomNames'][col], trajectory['ResidueID'][col]),
            #    print
            output = _contactcalc.residue_distances(xyzlist, residue_membership, contacts)
        else:
            raise ValueError('This is not supposed to happen!')

        return np.double(output)
Пример #10
0
def test_generator():
    pairs2 = itertools.combinations(range(N_ATOMS), 2)
    a = compute_distances(ptraj, pairs)
    b = compute_distances(ptraj, pairs2)
    eq(a, b)
Пример #11
0
def compute_rdf(traj,
                pairs,
                r_range=None,
                bin_width=0.005,
                n_bins=None,
                periodic=True,
                opt=True):
    """Compute radial distribution functions for pairs in every frame.

    Parameters
    ----------
    traj : Trajectory
        Trajectory to compute radial distribution function in.
    pairs : array-like, shape=(n_pairs, 2), dtype=int
        Each row gives the indices of two atoms.
    r_range : array-like, shape=(2,), optional, default=(0.0, 1.0)
        Minimum and maximum radii.
    bin_width : float, optional, default=0.005
        Width of the bins in nanometers.
    n_bins : int, optional, default=None
        The number of bins. If specified, this will override the `bin_width`
         parameter.
    periodic : bool, default=True
        If `periodic` is True and the trajectory contains unitcell
        information, we will compute distances under the minimum image
        convention.
    opt : bool, default=True
        Use an optimized native library to compute the pair wise distances.

    Returns
    -------
    r : np.ndarray, shape=(np.diff(r_range) / bin_width - 1), dtype=float
        Radii values corresponding to the centers of the bins.
    g_r : np.ndarray, shape=(np.diff(r_range) / bin_width - 1), dtype=float
        Radial distribution function values at r.

    See also
    --------
    Topology.select_pairs

    """
    if r_range is None:
        r_range = np.array([0.0, 1.0])
    r_range = ensure_type(r_range,
                          dtype=np.float64,
                          ndim=1,
                          name='r_range',
                          shape=(2, ),
                          warn_on_cast=False)
    if n_bins is not None:
        n_bins = int(n_bins)
        if n_bins <= 0:
            raise ValueError('`n_bins` must be a positive integer')
    else:
        n_bins = int((r_range[1] - r_range[0]) / bin_width)

    distances = compute_distances(traj, pairs, periodic=periodic, opt=opt)
    g_r, edges = np.histogram(distances, range=r_range, bins=n_bins)
    r = 0.5 * (edges[1:] + edges[:-1])

    # Normalize by volume of the spherical shell.
    # See discussion https://github.com/mdtraj/mdtraj/pull/724. There might be
    # a less biased way to accomplish this. The conclusion was that this could
    # be interesting to try, but is likely not hugely consequential. This method
    # of doing the calculations matches the implementation in other packages like
    # AmberTools' cpptraj and gromacs g_rdf.
    V = (4 / 3) * np.pi * (np.power(edges[1:], 3) - np.power(edges[:-1], 3))
    norm = len(pairs) * np.sum(1.0 / traj.unitcell_volumes) * V
    g_r = g_r.astype(np.float64) / norm  # From int64.
    return r, g_r
Пример #12
0
 def prepare_trajectory(self, trajectory):
     ptraj = distance.compute_distances(trajectory, self.atom_pairs)
     return np.double(ptraj)
Пример #13
0
def test_3p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=True)
    b = compute_displacements(ptraj, pairs, periodic=True, opt=True)
    eq(a, np.sqrt(np.sum(np.square(b), axis=2)))
Пример #14
0
atom_pairs = []
for this_lig in inds_N:
    for lig_atom, rec_atom in zip(this_lig, ind_res[0]):
        atom_pairs.append([lig_atom, rec_atom])
atom_pairs = np.array(atom_pairs)
print 'We are tracking distances between the following pairs of atoms:'
print atom_pairs

# comput and record distances between ligand and binding pocket ASP113
distances = []
for this_sim in simulations:
    for this_atom_pair in atom_pairs:
        this_lig = []
        for this_traj in [this_sim]:
            this_lig.extend(
                md_dist.compute_distances(this_traj, [this_atom_pair]))
        distances.append(this_lig)

dist_path = '/home/shenglan/TryMSMbuilder/output/ten_ligands/dist_to_binding'\
+'_s'+str(LOAD_STRIDE)+'.out'
pickle.dump(distances, open(dist_path, 'wb'))

# get N positions
sequences_all = []
for this_sim in simulations:
    this_seq = util.featurize_RawPos(inds_N, [this_sim])
    sequences_all.extend(this_seq)
seq_path = '/home/shenglan/TryMSMbuilder/output/ten_ligands/sequences' + '_s' + str(
    LOAD_STRIDE) + '.out'
pickle.dump(sequences_all, open(seq_path, 'wb'))
Пример #15
0
    ii = [atom.index for atom in this_res.atoms if atom.name in ['OD1']]
    ind_res.append(ii)
atom_pairs = []
for this_lig in inds_N:
    for lig_atom,rec_atom in zip(this_lig,ind_res[0]):
        atom_pairs.append([lig_atom,rec_atom])
atom_pairs = np.array(atom_pairs)
print 'We are tracking distances between the following pairs of atoms:'     
print atom_pairs

distances = []
for this_sim in simulations:
    for this_atom_pair in atom_pairs:
        this_lig = []
        for this_traj in this_sim: 
            this_lig.extend(md_dist.compute_distances(this_traj,[this_atom_pair]))
        distances.append(this_lig)

    


#sequences of coordinates of ligands
total_frames = 0
sequences_all = []
for this_sim in simulations:
    if use_COM:
        this_seq = util.featurize_RawPos(inds_all,this_sim,average = True)
    else:
        this_seq = util.featurize_RawPos(inds_N,this_sim)
    total_frames = this_seq[0].shape[0]*10+total_frames
    sequences_all.extend(this_seq)
Пример #16
0
res_ind = [[atom.index for atom in target_res.atoms if atom.name in ['OD1']]]

atom_pairs = []
for this_lig in inds_N:
    for lig_atom,rec_atom in zip(this_lig,res_ind[0]):
        atom_pairs.append([lig_atom,rec_atom])
atom_pairs = np.array(atom_pairs)
print 'We are tracking distances between the following pairs of atoms:'     
print atom_pairs

distances = []
for this_sim in simulations:
    this_dist = []
    for this_traj in this_sim:
        
        this_dist.extend(md_dist.compute_distances(this_traj,atom_pairs))
    this_dist = np.array(this_dist)
    distances.append(this_dist)

sequences_all = []
total_frames = 0
for this_sim in simulations:
    if use_COM:
        this_seq = util.featurize_RawPos(inds_all,this_sim,average = True)
    else:
        this_seq = util.featurize_RawPos(inds_N,this_sim)
    total_frames = this_seq[0].shape[0]*10+total_frames
    sequences_all.extend(this_seq)
print('the total number of conformations we are clustering is %d.' % total_frames)
    
# convert to pdb so I can view in vmd
Пример #17
0
    ind_res.append(ii)
atom_pairs = []
for this_lig in inds_N:
    for lig_atom,rec_atom in zip(this_lig,ind_res[0]):
        atom_pairs.append([lig_atom,rec_atom])
atom_pairs = np.array(atom_pairs)
print 'We are tracking distances between the following pairs of atoms:'     
print atom_pairs

# comput and record distances between ligand and binding pocket ASP113
distances = []
for this_sim in simulations:
    for this_atom_pair in atom_pairs:
        this_lig = []
        for this_traj in [this_sim]: 
            this_lig.extend(md_dist.compute_distances(this_traj,[this_atom_pair]))
        distances.append(this_lig)

dist_path = '/home/shenglan/TryMSMbuilder/output/ten_ligands/dist_to_binding'\
+'_s'+str(LOAD_STRIDE)+'.out'
pickle.dump(distances,open(dist_path,'wb'))

# get N positions
sequences_all = []
for this_sim in simulations:
    this_seq = util.featurize_RawPos(inds_N,[this_sim])
    sequences_all.extend(this_seq)
seq_path = '/home/shenglan/TryMSMbuilder/output/ten_ligands/sequences'+'_s'+str(LOAD_STRIDE)+'.out'
pickle.dump(sequences_all,open(seq_path,'wb'))

clustering = KCenters(n_clusters = N_CLUSTER)
Пример #18
0
def test_0():
    a = compute_distances(ptraj, pairs, periodic=False, opt=True)
    b = compute_distances(ptraj, pairs, periodic=False, opt=False)
    eq(a, b)
Пример #19
0
def test_generator():
    pairs2 = itertools.combinations(range(N_ATOMS), 2)
    a = compute_distances(ptraj, pairs)
    b = compute_distances(ptraj, pairs2)
    eq(a, b)
Пример #20
0
def test_0p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=True)
    b = compute_distances(ptraj, pairs, periodic=True, opt=False)
    eq(a, b, decimal=3)
Пример #21
0
def test_0():
    a = compute_distances(ptraj, pairs, periodic=False, opt=True)
    b = compute_distances(ptraj, pairs, periodic=False, opt=False)
    eq(a, b)
Пример #22
0
def test_3p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=True)
    b = compute_displacements(ptraj, pairs, periodic=True, opt=True)
    eq(a, np.sqrt(np.sum(np.square(b), axis=2)))
Пример #23
0
def test_0p():
    a = compute_distances(ptraj, pairs, periodic=True, opt=True)
    b = compute_distances(ptraj, pairs, periodic=True, opt=False)
    eq(a, b, decimal=3)