mdtraj.formats.MDCRDTrajectoryFile

class mdtraj.formats.MDCRDTrajectoryFile(filename, n_atoms=None, mode='r', has_box='detect', force_overwrite=True)

Interface for reading and writing to an AMBER mdcrd files. This is a file-like object, that both reading or writing depending on the mode flag. It implements the context manager protocol, so you can also use it with the python ‘with’ statement.

The conventional units in the mdcrd file are angstroms. The format only supports storing the cartesian coordinates and box lengths.

Parameters
filenamestr

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

n_atomsint

The number of atoms in the system. This is _required_ when mode == ‘r’ and irrelevant when mode == ‘w’.

mode{‘r’, ‘w’}

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

has_box = ‘detect’

Does the mdcrd file contain box length information? This is optional when mode == ‘r’ (and irrelevant when mode == ‘w’). The presence or absence of box information can generally be inferred from the file, but there might be corner cases in which this is not possible, because of limitations in the mdcrd format.

force_overwritebool

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

Methods

close(self)

Close the mdcrd file

read(self[, n_frames, stride, atom_indices])

Read data from a mdcrd file

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

Read a trajectory from a mdcrd file

seek(self, offset[, whence])

Move to a new file position

tell(self)

Current file position

write(self, xyz[, cell_lengths])

Write one or more frames of data to a mdcrd file

__init__(self, filename, n_atoms=None, mode='r', has_box='detect', force_overwrite=True)

Open an AMBER mdcrd file for reading/writing.

Methods

__init__(self, filename[, n_atoms, mode, …])

Open an AMBER mdcrd file for reading/writing.

close(self)

Close the mdcrd file

read(self[, n_frames, stride, atom_indices])

Read data from a mdcrd file

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

Read a trajectory from a mdcrd file

seek(self, offset[, whence])

Move to a new file position

tell(self)

Current file position

write(self, xyz[, cell_lengths])

Write one or more frames of data to a mdcrd file

Attributes

distance_unit

close(self)

Close the mdcrd file

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

Read data from a mdcrd 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.

stridenp.ndarray, 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.

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

The cartesian coordinates, in angstroms

cell_lengths{np.ndarray, None}

If the file contains unitcell lengths, they will be returned as an array of shape=(n_frames, 3). Otherwise, unitcell_angles will be None.

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

Read a trajectory from a mdcrd file

Parameters
topologyTopology

The system topology

n_framesint, optional

If positive, then read only the next n_frames frames. Otherwise read all of the frames in the file.

stridenp.ndarray, 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.

seek(self, 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(self)

Current file position

Returns
offsetint

The current frame in the file.

write(self, xyz, cell_lengths=None)

Write one or more frames of data to a mdcrd file

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

The cartesian coordinates of the atoms to write. By convention, the lengths should be in units of angstroms.

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

The length of the periodic box in each frame, in each direction, a, b, c. By convention the lengths should be in units of angstroms.