mdtraj.Trajectory

class mdtraj.Trajectory(xyz, topology, time=None, unitcell_lengths=None, unitcell_angles=None)

Container object for a molecular dynamics trajectory

A Trajectory represents a collection of one or more molecular structures, generally (but not necessarily) from a molecular dynamics trajectory. The Trajectory stores a number of fields describing the system through time, including the cartesian coordinates of each atoms (xyz), the topology of the molecular system (topology), and information about the unitcell if appropriate (unitcell_vectors, unitcell_length, unitcell_angles).

A Trajectory should generally be constructed by loading a file from disk. Trajectories can be loaded from (and saved to) the PDB, XTC, TRR, DCD, binpos, NetCDF or MDTraj HDF5 formats.

Trajectory supports fancy indexing, so you can extract one or more frames from a Trajectory as a separate trajectory. For example, to form a trajectory with every other frame, you can slice with traj[::2].

Trajectory uses the nanometer, degree & picosecond unit system.

See also

mdtraj.load

High-level function that loads files and returns an md.Trajectory

Examples

>>> # loading a trajectory
>>> import mdtraj as md
>>> md.load('trajectory.xtc', top='native.pdb')
<mdtraj.Trajectory with 1000 frames, 22 atoms at 0x1058a73d0>
>>> # slicing a trajectory
>>> t = md.load('trajectory.h5')
>>> print(t)
<mdtraj.Trajectory with 100 frames, 22 atoms>
>>> print(t[::2])
<mdtraj.Trajectory with 50 frames, 22 atoms>
>>> # calculating the average distance between two atoms
>>> import mdtraj as md
>>> import numpy as np
>>> t = md.load('trajectory.h5')
>>> np.mean(np.sqrt(np.sum((t.xyz[:, 0, :] - t.xyz[:, 21, :])**2, axis=1)))
Attributes
n_framesint

Number of frames in the trajectory

n_atomsint

Number of atoms in the trajectory

n_residuesint

Number of residues (amino acids) in the trajectory

timenp.ndarray, shape=(n_frames,)

The simulation time corresponding to each frame, in picoseconds

timestepfloat

Timestep between frames, in picoseconds

topologymd.Topology

Topology of the system, describing the organization of atoms into residues, bonds, etc

topmd.Topology

Alias for self.topology, describing the organization of atoms into residues, bonds, etc

xyznp.ndarray, shape=(n_frames, n_atoms, 3)

Cartesian coordinates of each atom in each simulation frame

unitcell_vectors{np.ndarray, shape=(n_frames, 3, 3), None}

The vectors that define the shape of the unit cell in each frame

unitcell_lengths{np.ndarray, shape=(n_frames, 3), None}

Lengths that define the shape of the unit cell in each frame.

unitcell_angles{np.ndarray, shape=(n_frames, 3), None}

Angles that define the shape of the unit cell in each frame.

Methods

atom_slice(self, atom_indices[, inplace])

Create a new trajectory from a subset of atoms

center_coordinates(self[, mass_weighted])

Center each trajectory frame at the origin (0,0,0).

image_molecules(self[, inplace, …])

Recenter and apply periodic boundary conditions to the molecules in each frame of the trajectory.

join(self, other[, check_topology, …])

Join two trajectories together along the time/frame axis.

load(filenames, \*\*kwargs)

Load a trajectory from disk

make_molecules_whole(self[, inplace, …])

Only make molecules whole

openmm_boxes(self, frame)

OpenMM-compatable box vectors of a single frame.

openmm_positions(self, frame)

OpenMM-compatable positions of a single frame.

remove_solvent(self[, exclude, inplace])

Create a new trajectory without solvent atoms

restrict_atoms(\*args, \*\*kwargs)

DEPRECATED: restrict_atoms was replaced by atom_slice and will be removed in 2.0

save(self, filename, \*\*kwargs)

Save trajectory to disk, in a format determined by the filename extension

save_amberrst7(self, filename[, force_overwrite])

Save trajectory in AMBER ASCII restart format

save_binpos(self, filename[, force_overwrite])

Save trajectory to AMBER BINPOS format

save_dcd(self, filename[, force_overwrite])

Save trajectory to CHARMM/NAMD DCD format

save_dtr(self, filename[, force_overwrite])

Save trajectory to DESMOND DTR format

save_gro(self, filename[, force_overwrite, …])

Save trajectory in Gromacs .gro format

save_gsd(self, filename[, force_overwrite])

Save trajectory to HOOMD GSD format

save_hdf5(self, filename[, force_overwrite])

Save trajectory to MDTraj HDF5 format

save_lammpstrj(self, filename[, force_overwrite])

Save trajectory to LAMMPS custom dump format

save_lh5(self, filename[, force_overwrite])

Save trajectory in deprecated MSMBuilder2 LH5 (lossy HDF5) format.

save_mdcrd(self, filename[, force_overwrite])

Save trajectory to AMBER mdcrd format

save_netcdf(self, filename[, force_overwrite])

Save trajectory in AMBER NetCDF format

save_netcdfrst(self, filename[, force_overwrite])

Save trajectory in AMBER NetCDF restart format

save_pdb(self, filename[, force_overwrite, …])

Save trajectory to RCSB PDB format

save_tng(self, filename[, force_overwrite])

Save trajectory to Gromacs TNG format

save_trr(self, filename[, force_overwrite])

Save trajectory to Gromacs TRR format

save_xtc(self, filename[, force_overwrite])

Save trajectory to Gromacs XTC format

save_xyz(self, filename[, force_overwrite])

Save trajectory to .xyz format.

slice(self, key[, copy])

Slice trajectory, by extracting one or more frames into a separate object

smooth(self, width[, order, atom_indices, …])

Smoothen a trajectory using a zero-delay Buttersworth filter.

stack(self, other)

Stack two trajectories along the atom axis

superpose(self, reference[, frame, …])

Superpose each conformation in this trajectory upon a reference

__init__(self, xyz, topology, time=None, unitcell_lengths=None, unitcell_angles=None)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(self, xyz, topology[, time, …])

Initialize self.

atom_slice(self, atom_indices[, inplace])

Create a new trajectory from a subset of atoms

center_coordinates(self[, mass_weighted])

Center each trajectory frame at the origin (0,0,0).

image_molecules(self[, inplace, …])

Recenter and apply periodic boundary conditions to the molecules in each frame of the trajectory.

join(self, other[, check_topology, …])

Join two trajectories together along the time/frame axis.

load(filenames, \*\*kwargs)

Load a trajectory from disk

make_molecules_whole(self[, inplace, …])

Only make molecules whole

openmm_boxes(self, frame)

OpenMM-compatable box vectors of a single frame.

openmm_positions(self, frame)

OpenMM-compatable positions of a single frame.

remove_solvent(self[, exclude, inplace])

Create a new trajectory without solvent atoms

restrict_atoms(\*args, \*\*kwargs)

DEPRECATED: restrict_atoms was replaced by atom_slice and will be removed in 2.0

save(self, filename, \*\*kwargs)

Save trajectory to disk, in a format determined by the filename extension

save_amberrst7(self, filename[, force_overwrite])

Save trajectory in AMBER ASCII restart format

save_binpos(self, filename[, force_overwrite])

Save trajectory to AMBER BINPOS format

save_dcd(self, filename[, force_overwrite])

Save trajectory to CHARMM/NAMD DCD format

save_dtr(self, filename[, force_overwrite])

Save trajectory to DESMOND DTR format

save_gro(self, filename[, force_overwrite, …])

Save trajectory in Gromacs .gro format

save_gsd(self, filename[, force_overwrite])

Save trajectory to HOOMD GSD format

save_hdf5(self, filename[, force_overwrite])

Save trajectory to MDTraj HDF5 format

save_lammpstrj(self, filename[, force_overwrite])

Save trajectory to LAMMPS custom dump format

save_lh5(self, filename[, force_overwrite])

Save trajectory in deprecated MSMBuilder2 LH5 (lossy HDF5) format.

save_mdcrd(self, filename[, force_overwrite])

Save trajectory to AMBER mdcrd format

save_netcdf(self, filename[, force_overwrite])

Save trajectory in AMBER NetCDF format

save_netcdfrst(self, filename[, force_overwrite])

Save trajectory in AMBER NetCDF restart format

save_pdb(self, filename[, force_overwrite, …])

Save trajectory to RCSB PDB format

save_tng(self, filename[, force_overwrite])

Save trajectory to Gromacs TNG format

save_trr(self, filename[, force_overwrite])

Save trajectory to Gromacs TRR format

save_xtc(self, filename[, force_overwrite])

Save trajectory to Gromacs XTC format

save_xyz(self, filename[, force_overwrite])

Save trajectory to .xyz format.

slice(self, key[, copy])

Slice trajectory, by extracting one or more frames into a separate object

smooth(self, width[, order, atom_indices, …])

Smoothen a trajectory using a zero-delay Buttersworth filter.

stack(self, other)

Stack two trajectories along the atom axis

superpose(self, reference[, frame, …])

Superpose each conformation in this trajectory upon a reference

Attributes

n_atoms

Number of atoms in the trajectory

n_chains

Number of chains in the trajectory

n_frames

Number of frames in the trajectory

n_residues

Number of residues (amino acids) in the trajectory

time

The simulation time corresponding to each frame, in picoseconds

timestep

Timestep between frames, in picoseconds

top

Alias for self.topology, describing the organization of atoms into residues, bonds, etc

topology

Topology of the system, describing the organization of atoms into residues, bonds, etc

unitcell_angles

Angles that define the shape of the unit cell in each frame.

unitcell_lengths

Lengths that define the shape of the unit cell in each frame.

unitcell_vectors

The vectors that define the shape of the unit cell in each frame

unitcell_volumes

Volumes of unit cell for each frame.

xyz

Cartesian coordinates of each atom in each simulation frame