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_frames
intNumber of frames in the trajectory
n_atoms
intNumber of atoms in the trajectory
n_residues
intNumber of residues (amino acids) in the trajectory
time
np.ndarray, shape=(n_frames,)The simulation time corresponding to each frame, in picoseconds
timestep
floatTimestep between frames, in picoseconds
topology
md.TopologyTopology of the system, describing the organization of atoms into residues, bonds, etc
top
md.TopologyAlias for self.topology, describing the organization of atoms into residues, bonds, etc
xyz
np.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
(atom_indices[, inplace])Create a new trajectory from a subset of atoms
center_coordinates
([mass_weighted])Center each trajectory frame at the origin (0,0,0).
image_molecules
([inplace, anchor_molecules, …])Recenter and apply periodic boundary conditions to the molecules in each frame of the trajectory.
join
(other[, check_topology, …])Join two trajectories together along the time/frame axis.
load
(filenames, **kwargs)Load a trajectory from disk
make_molecules_whole
([inplace, sorted_bonds])Only make molecules whole
openmm_boxes
(frame)OpenMM-compatable box vectors of a single frame.
openmm_positions
(frame)OpenMM-compatable positions of a single frame.
remove_solvent
([exclude, inplace])Create a new trajectory without solvent atoms
restrict_atoms
(**kwargs)DEPRECATED: restrict_atoms was replaced by atom_slice and will be removed in 2.0
save
(filename, **kwargs)Save trajectory to disk, in a format determined by the filename extension
save_amberrst7
(filename[, force_overwrite])Save trajectory in AMBER ASCII restart format
save_binpos
(filename[, force_overwrite])Save trajectory to AMBER BINPOS format
save_dcd
(filename[, force_overwrite])Save trajectory to CHARMM/NAMD DCD format
save_dtr
(filename[, force_overwrite])Save trajectory to DESMOND DTR format
save_gro
(filename[, force_overwrite, precision])Save trajectory in Gromacs .gro format
save_gsd
(filename[, force_overwrite])Save trajectory to HOOMD GSD format
save_hdf5
(filename[, force_overwrite])Save trajectory to MDTraj HDF5 format
save_lammpstrj
(filename[, force_overwrite])Save trajectory to LAMMPS custom dump format
save_lh5
(filename[, force_overwrite])Save trajectory in deprecated MSMBuilder2 LH5 (lossy HDF5) format.
save_mdcrd
(filename[, force_overwrite])Save trajectory to AMBER mdcrd format
save_netcdf
(filename[, force_overwrite])Save trajectory in AMBER NetCDF format
save_netcdfrst
(filename[, force_overwrite])Save trajectory in AMBER NetCDF restart format
save_pdb
(filename[, force_overwrite, bfactors])Save trajectory to RCSB PDB format
save_tng
(filename[, force_overwrite])Save trajectory to Gromacs TNG format
save_trr
(filename[, force_overwrite])Save trajectory to Gromacs TRR format
save_xtc
(filename[, force_overwrite])Save trajectory to Gromacs XTC format
save_xyz
(filename[, force_overwrite])Save trajectory to .xyz format.
slice
(key[, copy])Slice trajectory, by extracting one or more frames into a separate object
smooth
(width[, order, atom_indices, inplace])Smoothen a trajectory using a zero-delay Buttersworth filter.
stack
(other[, keep_resSeq])Stack two trajectories along the atom axis
superpose
(reference[, frame, atom_indices, …])Superpose each conformation in this trajectory upon a reference
-
__init__
(xyz, topology, time=None, unitcell_lengths=None, unitcell_angles=None)¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__init__
(xyz, topology[, time, …])Initialize self.
atom_slice
(atom_indices[, inplace])Create a new trajectory from a subset of atoms
center_coordinates
([mass_weighted])Center each trajectory frame at the origin (0,0,0).
image_molecules
([inplace, anchor_molecules, …])Recenter and apply periodic boundary conditions to the molecules in each frame of the trajectory.
join
(other[, check_topology, …])Join two trajectories together along the time/frame axis.
load
(filenames, **kwargs)Load a trajectory from disk
make_molecules_whole
([inplace, sorted_bonds])Only make molecules whole
openmm_boxes
(frame)OpenMM-compatable box vectors of a single frame.
openmm_positions
(frame)OpenMM-compatable positions of a single frame.
remove_solvent
([exclude, inplace])Create a new trajectory without solvent atoms
restrict_atoms
(**kwargs)DEPRECATED: restrict_atoms was replaced by atom_slice and will be removed in 2.0
save
(filename, **kwargs)Save trajectory to disk, in a format determined by the filename extension
save_amberrst7
(filename[, force_overwrite])Save trajectory in AMBER ASCII restart format
save_binpos
(filename[, force_overwrite])Save trajectory to AMBER BINPOS format
save_dcd
(filename[, force_overwrite])Save trajectory to CHARMM/NAMD DCD format
save_dtr
(filename[, force_overwrite])Save trajectory to DESMOND DTR format
save_gro
(filename[, force_overwrite, precision])Save trajectory in Gromacs .gro format
save_gsd
(filename[, force_overwrite])Save trajectory to HOOMD GSD format
save_hdf5
(filename[, force_overwrite])Save trajectory to MDTraj HDF5 format
save_lammpstrj
(filename[, force_overwrite])Save trajectory to LAMMPS custom dump format
save_lh5
(filename[, force_overwrite])Save trajectory in deprecated MSMBuilder2 LH5 (lossy HDF5) format.
save_mdcrd
(filename[, force_overwrite])Save trajectory to AMBER mdcrd format
save_netcdf
(filename[, force_overwrite])Save trajectory in AMBER NetCDF format
save_netcdfrst
(filename[, force_overwrite])Save trajectory in AMBER NetCDF restart format
save_pdb
(filename[, force_overwrite, bfactors])Save trajectory to RCSB PDB format
save_tng
(filename[, force_overwrite])Save trajectory to Gromacs TNG format
save_trr
(filename[, force_overwrite])Save trajectory to Gromacs TRR format
save_xtc
(filename[, force_overwrite])Save trajectory to Gromacs XTC format
save_xyz
(filename[, force_overwrite])Save trajectory to .xyz format.
slice
(key[, copy])Slice trajectory, by extracting one or more frames into a separate object
smooth
(width[, order, atom_indices, inplace])Smoothen a trajectory using a zero-delay Buttersworth filter.
stack
(other[, keep_resSeq])Stack two trajectories along the atom axis
superpose
(reference[, frame, atom_indices, …])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