Utils Traj#

traj #

FUNCTION DESCRIPTION
distances

Compute pairwise distances between atoms given by selection.

fraction_of_native_contacts

Compute the fraction of native contacts [1].

Functions#

distances(traj: md.Trajectory, selection: str = 'all', stride: int = 1) -> torch.Tensor #

Compute pairwise distances between atoms given by selection.

Selects atoms using the topology's select_atom_indices and forms all unique pairs whose residues are separated by more than stride residues. Distances are computed via mdtraj.compute_distances.

For selection semantics, see: https://mdtraj.org/1.9.3/atom_selection.html.

PARAMETER DESCRIPTION
traj

The trajectory to compute distances.

TYPE: Trajectory

selection

Atom selection string passed to Topology.select_atom_indices (e.g. "alpha", "heavy").

TYPE: str, optional, by default "all" DEFAULT: 'all'

stride

Minimum residue separation between atom pairs. Pairs whose residues are separated by stride or fewer residues are excluded.

TYPE: int, optional, by default 1 DEFAULT: 1

RETURNS DESCRIPTION
Tensor

Pairwise distances for each frame of the trajectory.

Source code in spectre/utils/traj.py
def distances(
    traj: md.Trajectory, selection: str = "all", stride: int = 1
) -> torch.Tensor:
    """
    Compute pairwise distances between atoms given by selection.

    Selects atoms using the topology's `select_atom_indices` and forms
    all unique pairs whose residues are separated by more than `stride`
    residues. Distances are computed via `mdtraj.compute_distances`.

    For selection semantics, see: <https://mdtraj.org/1.9.3/atom_selection.html>.

    Parameters
    ----------
    traj : md.Trajectory
        The trajectory to compute distances.

    selection : str, optional, by default "all"
        Atom selection string passed to
        `Topology.select_atom_indices` (e.g. "alpha", "heavy").

    stride : int, optional, by default 1
        Minimum residue separation between atom pairs. Pairs whose
        residues are separated by `stride` or fewer residues are
        excluded.

    Returns
    -------
    torch.Tensor
        Pairwise distances for each frame of the trajectory.
    """
    top = traj.topology
    atom_ndx = top.select_atom_indices(selection)
    atom_pairs = np.array(
        [
            (i, j)
            for (i, j) in combinations(iterable=atom_ndx, r=2)
            if abs(top.atom(i).residue.index - top.atom(j).residue.index) > stride
        ]
    )
    distances = md.compute_distances(traj=traj, atom_pairs=atom_pairs)

    return torch.tensor(distances)

fraction_of_native_contacts(traj: md.Trajectory, native: md.Trajectory, beta: float = 50.0, gamma: float = 1.5, native_cutoff: float = 0.8, selection: str = 'alpha', stride: int = 3) #

Compute the fraction of native contacts [1].

PARAMETER DESCRIPTION
traj

The trajectory to do the computation.

TYPE: Trajectory

native

The 'native state'. This can be an entire trajecory, or just a single frame. Only the first conformation is used.

TYPE: Trajectory

beta

Beta constant in 1/nm units.

TYPE: float, optional, by default 50.0 DEFAULT: 50.0

gamma

Gamma constant.

TYPE: float, optional, by default 1.5 DEFAULT: 1.5

native_cutoff

Cutoff distance for native contacts in nanometers.

TYPE: float, optional, by default 0.8 DEFAULT: 0.8

RETURNS DESCRIPTION
Tensor

Fraction of native contacts in each frame.

Source code in spectre/utils/traj.py
def fraction_of_native_contacts(
    traj: md.Trajectory,
    native: md.Trajectory,
    beta: float = 50.0,
    gamma: float = 1.5,
    native_cutoff: float = 0.8,
    selection: str = "alpha",
    stride: int = 3,
):
    """
    Compute the fraction of native contacts [@best2013native].

    Parameters
    ----------
    traj : md.Trajectory
        The trajectory to do the computation.

    native : md.Trajectory
        The 'native state'. This can be an entire trajecory, or just a single
        frame. Only the first conformation is used.

    beta : float, optional, by default 50.0
        Beta constant in 1/nm units.

    gamma : float, optional, by default 1.5
        Gamma constant.

    native_cutoff : float, optional, by default 0.8
        Cutoff distance for native contacts in nanometers.

    Returns
    -------
    torch.Tensor
        Fraction of native contacts in each frame.
    """
    top = traj.topology
    atom_ndx = top.select_atom_indices(selection)
    atom_pairs = np.array(
        [
            (i, j)
            for (i, j) in combinations(iterable=atom_ndx, r=2)
            if abs(top.atom(i).residue.index - top.atom(j).residue.index) > stride
        ]
    )
    distances = md.compute_distances(native[0], atom_pairs)[0]
    native_contacts = atom_pairs[distances < native_cutoff]

    r = torch.from_numpy(md.compute_distances(traj, native_contacts))
    r0 = torch.from_numpy(md.compute_distances(native[0], native_contacts))

    q = torch.mean(1.0 / (1 + torch.exp(beta * (r - gamma * r0))), dim=1)

    return q