How to read a trajectory#
Goal#
Attach trajectory frames to a topology Molecule so that all frames are available for analysis.
Minimal example#
from moleculekit.molecule import Molecule
# Load topology first, then attach the trajectory
mol = Molecule("topology.psf")
mol.read("trajectory.xtc")
print(mol.numFrames)
Parameters that matter#
Parameter |
Type |
Default |
What it does |
|---|---|---|---|
|
|
|
Read only the listed frame indices from the trajectory |
|
|
|
Skip every N frames (e.g. |
|
|
|
Append frames to existing coordinates instead of replacing them |
Common variations#
# Load topology first, then read a specific frame range
mol = Molecule("topology.psf")
mol.read("trajectory.xtc", frames=list(range(0, 500)))
# Concatenate two trajectory files
mol = Molecule("topology.psf")
mol.read("run1.xtc")
mol.read("run2.xtc", append=True)
print(mol.numFrames)
Gotchas#
All frames are loaded into memory at once; for very long trajectories use
frames=to read a slice.The atom count of the trajectory must exactly match the topology — a mismatch raises an error.
skipandframescan be combined:framesselects the subset to consider, thenskipfurther subsamples within that subset.When
append=False(default) a secondmol.read(traj)call replaces the existing coordinates rather than extending them.