Skip to content

Instantly share code, notes, and snippets.

@jmbr
Created February 1, 2021 19:50
Show Gist options
  • Save jmbr/f9d12c608a51dbda26557480db4318a7 to your computer and use it in GitHub Desktop.
Save jmbr/f9d12c608a51dbda26557480db4318a7 to your computer and use it in GitHub Desktop.
Compute RMS distances between frames in an mdtraj Trajectory object
import numpy as np
import mdtraj as md
import tqdm
def compute_distance_matrix(trajectory: md.Trajectory) -> np.ndarray:
"""Compute distance matrix.
"""
topology = trajectory.topology
CA_indices = [atom.index for atom in topology.atoms_by_name('CA')]
num_frames = len(trajectory)
distance_matrix = np.zeros((num_frames, num_frames), dtype=float)
for i in tqdm.tqdm(range(num_frames)):
rmsds = md.rmsd(trajectory, trajectory, frame=i,
atom_indices=CA_indices)
distance_matrix[i, :] = rmsds
return distance_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment