### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 5. Coordinates modules »

# 5.12. Gromacs TRR file IO — `MDAnalysis.coordinates.TRR`

The Gromacs TRR trajectory format is a lossless format like e.g. the DCD format (see `DCD`) and unlike the `XTC` format, which stores reduced precision coordinates. Therefore, if one wants to convert to Gromacs trajectories without loss of precision then one should use the TRR format.

The TRR format can store velocities and forces in addition to coordinates. It is also used by other Gromacs tools to store and process other data such as modes from a principal component analysis.

## 5.12.1. Tips and Tricks

### 5.12.1.1. Filling a TRR with PCA modes

The following recipe by Ramon Crehuet shows how to convert modes stored in a NumPy-like array (e.g. from a PCA analysis with MMTK) to a TRR usable by Gromacs. The idea is to manually fill a `Timestep` with the desired values and then write it to a file with the appropriate `TRRWriter`. In order to respect the Gromacs format for modes in a TRR file, one must write the average coordinates in the first frame of the TRR and the modes into subsequent ones. The mode number is stored in the `step` attribute and the mode coordinates are filling the `_pos` attribute of `Timestep`:

    # 'modes' is a mode object with M PCs, similar to a MxNx3 array
    # 'xav' the average coordinates, a Nx3 array for N atoms

    N = len(xav)   # number of atoms, i.e. number of coordinates

    W = Writer('pca.trr', numatoms=N)            # TRR writer
    ts = MDAnalysis.coordinates.TRR.Timestep(N)  # TRR time step

    for frame,mode in enumerate(modes[4:16]):
        ts.lmbda = -1
        if frame<=1:
           ts._pos[:] = xav
        else:
           ts._pos[:] = mode.scaledToNorm(1.).array*10   # nm to angstroms
        ts.frame = frame         # manually change the frame number
        ts.step = frame - 1
        if frame <= 1:
           ts.time = frame-1
        else:
           ts.time = mode.frequency
        W.write(ts)             # converts angstrom to nm for gmx

     W.close()

## 5.12.2. Module reference

Reading of Gromacs TRR trajectories.

See also

`MDAnalysis.coordinates.xdrfile.libxdrfile` for low-level bindings to the Gromacs trajectory file formats

### 5.12.2.1. Classes

class `MDAnalysis.coordinates.xdrfile.TRR.``Timestep`(arg)

  Timestep for a Gromacs TRR trajectory.

  `copy`()

    Make an independent (“deep”) copy of the whole `Timestep`.

  `dimensions`

    unitcell dimensions (A, B, C, alpha, beta, gamma)

    - A, B, C are the lengths of the primitive cell vectors e1, e2, e3
    - alpha = angle(e1, e2)
    - beta = angle(e1, e3)
    - gamma = angle(e2, e3)
  `volume`

    volume of the unitcell

class `MDAnalysis.coordinates.xdrfile.TRR.``TRRReader`(filename, convert_units=None, **kwargs)

  Read a Gromacs TRR trajectory.

  `OtherWriter`(filename, **kwargs)

    Returns a writer appropriate for filename.

    Sets the default keywords start, step and delta (if available). numatoms is always set from `Reader.numatoms`.

    See also

    `Reader.Writer()` and `MDAnalysis.Writer()`

  `Writer`(filename, **kwargs)

    Returns a Gromacs TrjWriter for filename with the same parameters as this trajectory.

    All values can be changed through keyword arguments.

    Arguments : | filename filename of the output trajectory
    Keywords : | numatoms number of atoms delta Time interval between frames in ps. precision accuracy for lossy XTC format as a power of 10 (ignored for TRR) [1000.0]
    Returns : | appropriate `TrjWriter`

  `close`()

    Close xdr trajectory file if it was open.

  `close_trajectory`()

    Specific implementation of trajectory closing.

  `convert_forces_from_native`(force, inplace=True)

    In-place conversion of forces array force from native units to base units.

    By default, the input force is modified in place and also returned.

    New in version 0.7.7.

  `convert_forces_to_native`(force, inplace=True)

    In-place conversion of force array force from base units to native units.

    By default, the input force is modified in place and also returned.

    New in version 0.7.7.

  `convert_pos_from_native`(x, inplace=True)

    In-place conversion of coordinate array x from native units to base units.

    By default, the input x is modified in place and also returned.

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_pos_to_native`(x, inplace=True)

    Conversion of coordinate array x from base units to native units.

    By default, the input x is modified in place and also returned.

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_time_from_native`(t, inplace=True)

    Convert time t from native units to base units.

    By default, the input t is modified in place and also returned (although note that scalar values t are passed by value in Python and hence an in-place modification has no effect on the caller.)

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_time_to_native`(t, inplace=True)

    Convert time t from base units to native units.

    By default, the input t is modified in place and also returned. (Also note that scalar values t are passed by value in Python and hence an in-place modification has no effect on the caller.)

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_velocities_from_native`(v, inplace=True)

    In-place conversion of velocities array v from native units to base units.

    By default, the input v is modified in place and also returned.

    New in version 0.7.5.

  `convert_velocities_to_native`(v, inplace=True)

    In-place conversion of coordinate array v from base units to native units.

    By default, the input v is modified in place and also returned.

    New in version 0.7.5.

  `delta`

    Time step length in ps.

    The result is computed from the trajectory and cached. If for any reason the trajectory cannot be read then 0 is returned.

  `dt`

    Time between two trajectory frames in picoseconds.

  `frame`

    Frame number of the current time step.

    This is a simple short cut to `Timestep.frame`.

  `next`()

    Forward one step to next frame.

  `numatoms`

    Read the number of atoms from the trajectory.

    The result is cached. If for any reason the trajectory cannot be read then 0 is returned.

  `numframes`

    Read the number of frames from the trajectory.

    The result is cached. If for any reason the trajectory cannot be read then 0 is returned.

    This takes a long time because the frames are counted by iterating through the whole trajectory.

  `open_trajectory`()

    Open xdr trajectory file.

    Returns : | pointer to XDRFILE (and sets self.xdrfile)
    Raises : | `IOError` with code EALREADY if file was already opened or ENOENT if the file cannot be found

  `rewind`()

    Position at beginning of trajectory

  `time`

    Time of the current frame in MDAnalysis time units (typically ps).

    time = `Timestep.frame` * `Reader.dt`

  `totaltime`

    Total length of the trajectory numframes * dt.

class `MDAnalysis.coordinates.xdrfile.TRR.``TRRWriter`(filename, numatoms, start=0, step=1, delta=1.0, precision=1000.0, remarks=None, convert_units=None)

  Write a Gromacs TRR trajectory.

  Create a new TrjWriter

  Arguments : | filename name of output file numatoms number of atoms in trajectory file
  Keywords : | start starting timestep step skip between subsequent timesteps delta timestep precision accuracy for lossy XTC format [1000] convert_units `True`: units are converted to the MDAnalysis base format; `None` selects the value of `MDAnalysis.core.flags` [‘convert_gromacs_lengths’]. (see Flags)

  `close_trajectory`()

    Specific implementation of trajectory closing.

  `convert_dimensions_to_unitcell`(ts)

    Read dimensions from timestep ts and return Gromacs box vectors

  `convert_forces_from_native`(force, inplace=True)

    In-place conversion of forces array force from native units to base units.

    By default, the input force is modified in place and also returned.

    New in version 0.7.7.

  `convert_forces_to_native`(force, inplace=True)

    In-place conversion of force array force from base units to native units.

    By default, the input force is modified in place and also returned.

    New in version 0.7.7.

  `convert_pos_from_native`(x, inplace=True)

    In-place conversion of coordinate array x from native units to base units.

    By default, the input x is modified in place and also returned.

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_pos_to_native`(x, inplace=True)

    Conversion of coordinate array x from base units to native units.

    By default, the input x is modified in place and also returned.

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_time_from_native`(t, inplace=True)

    Convert time t from native units to base units.

    By default, the input t is modified in place and also returned (although note that scalar values t are passed by value in Python and hence an in-place modification has no effect on the caller.)

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_time_to_native`(t, inplace=True)

    Convert time t from base units to native units.

    By default, the input t is modified in place and also returned. (Also note that scalar values t are passed by value in Python and hence an in-place modification has no effect on the caller.)

    Changed in version 0.7.5: Keyword inplace can be set to `False` so that a modified copy is returned unless no conversion takes place, in which case the reference to the unmodified x is returned.

  `convert_velocities_from_native`(v, inplace=True)

    In-place conversion of velocities array v from native units to base units.

    By default, the input v is modified in place and also returned.

    New in version 0.7.5.

  `convert_velocities_to_native`(v, inplace=True)

    In-place conversion of coordinate array v from base units to native units.

    By default, the input v is modified in place and also returned.

    New in version 0.7.5.

  `has_valid_coordinates`(criteria, x)

    Returns `True` if all values are within limit values of their formats.

    Due to rounding, the test is asymmetric (and min is supposed to be negative):

    > min < x <= max

    Arguments : | criteria dictionary containing the max and min values in native units x `numpy.ndarray` of `(x, y, z)` coordinates of atoms selected to be written out.
    Returns : | boolean

  `write`(obj)

    Write current timestep, using the supplied obj.

    The argument should be a `AtomGroup` or a `Universe` or a `Timestep` instance.

    Note

    The size of the obj must be the same as the number of atom provided when setting up the trajectory.

  `write_next_timestep`(ts=None)

    write a new timestep to the trj file

    ts is a `Timestep` instance containing coordinates to be written to trajectory file

class `MDAnalysis.coordinates.xdrfile.TRR.``TRRReader`(filename, convert_units=None, **kwargs)

  Read a Gromacs TRR trajectory.

class `MDAnalysis.coordinates.xdrfile.TRR.``TRRWriter`(filename, numatoms, start=0, step=1, delta=1.0, precision=1000.0, remarks=None, convert_units=None)

  Write a Gromacs TRR trajectory.

  Create a new TrjWriter

  Arguments : | filename name of output file numatoms number of atoms in trajectory file
  Keywords : | start starting timestep step skip between subsequent timesteps delta timestep precision accuracy for lossy XTC format [1000] convert_units `True`: units are converted to the MDAnalysis base format; `None` selects the value of `MDAnalysis.core.flags` [‘convert_gromacs_lengths’]. (see Flags)

class `MDAnalysis.coordinates.xdrfile.TRR.``Timestep`(arg)

  Timestep for a Gromacs TRR trajectory.

### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 5. Coordinates modules »