Examples » Reading trajectories

Trajectory construction is two-step process.

First you need to provide a reference topology as Frame (usually read from .pdb file). The coordinates of reference frame are not used in trajectory traverse.

Next you need to add files to trajectory by calling Trajectory.extend(). The supplied instances must be derived from TrajectoryInputFile.

To keep example self-contained, lets import all needed classes and define paths to trajectories to read.

import os
from pyxmolpp2 import PdbFile, Trajectory, TrjtoolDatFile, GromacsXtcFile, AmberNetCDF

dat_path = os.path.join(os.environ["TEST_DATA_PATH"], "trjtool/GB1/")
nc_path = os.path.join(os.environ["TEST_DATA_PATH"],
                       "amber/1ubq_intolerant_shake_ewald_SPCE/")
xtc_path = os.path.join(os.environ["TEST_DATA_PATH"], "gromacs/xtc/")

Composing a trajectory isn't very different for all formats:

# Trjtool .dat format
dat_traj = Trajectory(PdbFile(dat_path + "/run00001.pdb").frames()[0])
dat_traj.extend(TrjtoolDatFile(dat_path + "/run00001.dat"))

# Amber NetCDF format
nc_traj = Trajectory(PdbFile(nc_path + "/box.pdb").frames()[0])
nc_traj.extend(AmberNetCDF(nc_path + "/run00001.0-10.nc"))

# Gromacs xtc format
xtc_traj = Trajectory(PdbFile(xtc_path + "/1am7_protein.pdb").frames()[0])
xtc_traj.extend(GromacsXtcFile(xtc_path + "/1am7_corrected.xtc", n_frames=51))
# note: GromacsXtcFile constructor requires number of
#       frames to be known ahead of time to avoid file re-read

Now all trajectories are ready to work:

for fmt, traj in [
    ("Amber NetCDF", nc_traj),
    ("Gromacs XTC", xtc_traj),
    ("Trjtool", dat_traj)
]:
    print(f"{fmt}: {traj.n_atoms} atoms, {traj.n_frames} frames")
    for frame in traj[:3]:
        print(f"    index={frame.index:2},"
              f" volume={frame.cell.volume:15.10g},"
              f" geom_center={frame.coords.mean()}")
    print()
Amber NetCDF: 18751 atoms, 10 frames
    index= 0, volume=    186678.6911, geom_center=[0.189061, 0.269716, -0.583244]
    index= 1, volume=    186678.6911, geom_center=[0.215094, 0.217089, -0.597345]
    index= 2, volume=    186678.6911, geom_center=[0.378229, 0.255259, -0.589538]

Gromacs XTC: 2504 atoms, 51 frames
    index= 0, volume=    222150.1913, geom_center=[38.265685, 39.276188, 38.698967]
    index= 1, volume=    221725.9681, geom_center=[37.974315, 39.488728, 38.510166]
    index= 2, volume=    222255.1505, geom_center=[37.624543, 39.803744, 38.542170]

Trjtool: 880 atoms, 1000 frames
    index= 0, volume=              1, geom_center=[8.422286, 0.967190, -13.856332]
    index= 1, volume=              1, geom_center=[8.273981, 1.172387, -13.994633]
    index= 2, volume=              1, geom_center=[8.226298, 1.172632, -13.963189]