MDTraj

1.11.1.post1 · active · verified Thu Apr 16

MDTraj is a modern, open-source library for the analysis of molecular dynamics (MD) trajectories in Python. It provides high-performance tools for reading, writing, and manipulating MD data, supporting a wide range of file formats. The current version is 1.11.1.post1, and it typically has a few releases per year, often driven by new feature development, bug fixes, or compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates loading a minimal PDB file into an MDTraj Trajectory object, inspecting its basic properties, and performing a simple distance calculation. It cleans up the temporary file afterward.

import mdtraj as md
import os

# Create a minimal PDB file for demonstration
pdb_content = """
ATOM      1  N   ALA A   1       1.000   2.000   3.000  1.00 10.00           N
ATOM      2  CA  ALA A   1       2.000   2.000   3.000  1.00 10.00           C
ATOM      3  C   ALA A   1       3.000   2.000   3.000  1.00 10.00           C
TER
"""
pdb_filename = "minimal.pdb"
with open(pdb_filename, "w") as f:
    f.write(pdb_content)

# Load a trajectory
traj = md.load(pdb_filename)

print(f"Trajectory loaded: {traj.n_frames} frames, {traj.n_atoms} atoms.")
print(f"Topology has {traj.n_residues} residues.")

# Example calculation: compute distance between atoms 1 and 2 (CA and C)
# Indices are 0-based
distances = md.compute_distances(traj, [[1, 2]])
print(f"Distance between atom 1 and 2: {distances[0][0]:.3f} nm")

# Clean up the dummy file
os.remove(pdb_filename)

view raw JSON →