moleculekit.trr module#
- class moleculekit.trr.TRRTrajectoryFile(filename, mode='r', force_overwrite=True, **kwargs)#
Bases:
objectInterface for reading and writing to a GROMACS TRR file. This is a file-like objec that supports both reading and writing. It also supports the context manager protocol, so you can use it with the python ‘with’ statement.
The conventional units in the TRR file are nanometers and picoseconds. The format only supports saving coordinates, the time, the md step, and the unit cell parametrs (box vectors)
- Parameters:
filename (str) – 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_overwrite (bool) – If opened in write mode, and a file by the name of filename already exists on disk, should we overwrite it?
min_chunk_size (int, 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_multiplier – 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.
int – 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.
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.
Examples
>>> # load up the data from a trr >>> with TRRTrajectoryFile('traj.trr') as f: >>> xyz, time, step, box, lambdas = f.read()
- __enter__()#
Support the context manager protocol
- __exit__(*exc_info)#
Support the context manager protocol
- close()#
Close the TRR file
- distance_unit#
- offsets#
get byte offsets from current xtc file .. seealso::
set_offsets
- read(n_frames=None, stride=None, atom_indices=None)#
Read data from a TRR file
- Parameters:
n_frames (int, None) – The number of frames you would like to read from the file. If None, all of the remaining frames will be loaded.
stride (int, optional) – Read only every stride-th frame.
atom_indices (array_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:
xyz (np.ndarray, shape=(n_frames, n_atoms, 3), dtype=np.float32) – The cartesian coordinates, in nanometers
time (np.ndarray, shape=(n_frames), dtype=np.float32) – The simulation time, in picoseconds, corresponding to each frame
step (np.ndarray, shape=(n_frames), dtype=np.int32) – The step in the simulation corresponding to each frame
box (np.ndarray, shape=(n_frames, 3, 3), dtype=np.float32) – The box vectors in each frame.
lambd (np.ndarray, shape=(n_frames), dtype=np.float32) – The alchemical lambda value of each frame.
Notes
The TRR format DOES support saving velocities and forces, but those fields are not read (or written) by the current implementation of this wrapper. However, this functionality is accessible in the internal TRRTrajectoryFile._read()/._write() methods.
- seek(offset, whence=0)#
Move to a new file position
- Parameters:
offset (int) – 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
- write(xyz, time=None, step=None, box=None, lambd=None)#
Write data to a TRR file
- Parameters:
xyz (np.ndarray, dtype=np.float32, shape=(n_frames, n_atoms, 3)) – The cartesian coordinates of the atoms, in nanometers
time (np.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.
step (np.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
box (np.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.
lambd (np.ndarray, dtype=np.float32, shape=(n_frames), optional) – The alchemical lambda value at each frame. If not supplied, all zeros will be written.
- moleculekit.trr.load_trr(filename, top=None, stride=None, atom_indices=None, frame=None)#
Load a Gromacs TRR file from disk.
The .trr format is a cross-platform compressed binary trajectory format produced by the gromacs software that stores atomic coordinates, box vectors, and time information.
- Parameters:
filename (str) – Filename of TRR trajectory file.
stride (int, default=None) – Only read every stride-th frame
atom_indices (array_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 requires an extra copy, but will save memory.
frame (int, optional) – Use this option to load only a single frame from a trajectory on disk. If frame is None, the default, the entire trajectory will be loaded. If supplied,
stridewill be ignored.