mdtraj.formats.XTCTrajectoryFile

class mdtraj.formats.XTCTrajectoryFile(filenamee, mode='r', force_overwrite=True, **kwargs)

Interface for reading and writing to a GROMACS XTC file. This is a file-like objec that supports both reading and writing. It also supports the context manager ptorocol, so you can use it with the python ‘with’ statement.

The conventional units in the XTC file are nanometers and picoseconds. The format only supports saving coordinates, the time, the md step, and the unit cell parametrs (box vectors)

Parameters
filenamestr

The filename to open. A path to a file on disk.

mode{‘r’, ‘w’}

The mode in which to open the file, either ‘r’ for read or ‘w’ for write.

force_overwritebool

If opened in write mode, and a file by the name of filename already exists on disk, should we overwrite it?

Other Parameters
min_chunk_sizeint, default=100

In read mode, we need to allocate a buffer in which to store the data without knowing how many frames are in the file. This parameter is the minimum size of the buffer to allocate.

chunk_size_multiplierint, default=1.5

In read mode, we need to allocate a buffer in which to store the data without knowing how many frames are in the file. We can guess this information based on the size of the file on disk, but it’s not perfect. This parameter inflates the guess by a multiplicative factor.

See also

mdtraj.load_xtc

High-level wrapper that returns a md.Trajectory

Examples

>>> # read the data from from an XTC file
>>> with XTCTrajectoryFile('traj.xtc') as f:
>>>    xyz, time, step, box = f.read()
>>> # write some random coordinates to an XTC file
>>> with XTCTrajectoryFile('output.xtc', 'w') as f:
>>>     f.write(np.random.randn(10,1,3))
Attributes
distance_unit
offsets

get byte offsets from current xtc file

Methods

close()

Close the XTC file handle

read([n_frames, stride, atom_indices])

Read data from an XTC file

read_as_traj(topology[, n_frames, stride, …])

Read a trajectory from an XTC file

seek(offset[, whence])

Move to a new file position

tell()

Current file position

write(xyz[, time, step, box])

Write data to an XTC file

flush

__init__(self, /, *args, **kwargs)

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

Methods

__init__(self, /, \*args, \*\*kwargs)

Initialize self.

close()

Close the XTC file handle

flush()

read([n_frames, stride, atom_indices])

Read data from an XTC file

read_as_traj(topology[, n_frames, stride, …])

Read a trajectory from an XTC file

seek(offset[, whence])

Move to a new file position

tell()

Current file position

write(xyz[, time, step, box])

Write data to an XTC file

Attributes

distance_unit

offsets

get byte offsets from current xtc file

close()

Close the XTC file handle

offsets

get byte offsets from current xtc file

read(n_frames=None, stride=None, atom_indices=None)

Read data from an XTC file

Parameters
n_framesint, None

The number of frames you would like to read from the file. If None, all of the remaining frames will be loaded.

strideint, optional

Read only every stride-th frame.

atom_indicesarray_like, optional

If not none, then read only a subset of the atoms coordinates from the file. This may be slightly slower than the standard read because it required an extra copy, but will save memory.

Returns
xyznp.ndarray, shape=(n_frames, n_atoms, 3), dtype=np.float32

The cartesian coordinates, in nanometers

timenp.ndarray, shape=(n_frames), dtype=np.float32

The simulation time, in picoseconds, corresponding to each frame

stepnp.ndarray, shape=(n_frames), dtype=np.int32

The step in the simulation corresponding to each frame

boxnp.ndarray, shape=(n_frames, 3, 3), dtype=np.float32

The box vectors in each frame.

status: int

either _EXDROK or _EXDRENDOFFILE

See also

read_as_traj

Returns a Trajectory object

read_as_traj(topology, n_frames=None, stride=None, atom_indices=None)

Read a trajectory from an XTC file

Parameters
topologyTopology

The system topology

n_framesint, None

The number of frames you would like to read from the file. If None, all of the remaining frames will be loaded.

strideint, optional

Read only every stride-th frame.

atom_indicesarray_like, optional

If not none, then read only a subset of the atoms coordinates from the file. This may be slightly slower than the standard read because it required an extra copy, but will save memory.

Returns
trajectoryTrajectory

A trajectory object containing the loaded portion of the file.

See also

read

Returns the raw data from the file

seek(offset, whence=0)

Move to a new file position

Parameters
offsetint

A number of frames.

whence{0, 1, 2}

0: offset from start of file, offset should be >=0. 1: move relative to the current position, positive or negative 2: move relative to the end of file, offset should be <= 0. Seeking beyond the end of a file is not supported

tell()

Current file position

Returns
offsetint

The current frame in the file.

write(xyz, time=None, step=None, box=None)

Write data to an XTC file

Parameters
xyznp.ndarray, dtype=np.float32, shape=(n_frames, n_atoms, 3)

The cartesian coordinates of the atoms, in nanometers

timenp.ndarray, dtype=float32, shape=(n_frames), optional

The simulation time corresponding to each frame, in picoseconds. If not supplied, the numbers 0..n_frames will be written.

stepnp.ndarray, dtype=int32, shape=(n_frames), optional

The simulation timestep corresponding to each frame, in steps. If not supplied, the numbers 0..n_frames will be written

boxnp.ndarray, dtype=float32, shape=(n_frames, 3, 3), optional

The periodic box vectors of the simulation in each frame, in nanometers. If not supplied, the vectors (1,0,0), (0,1,0) and (0,0,1) will be written for each frame.