5.14. Gromacs XTC file IO — MDAnalysis.coordinates.XTC

The Gromacs XTC trajectory format is a format with lossy compression. Coordinates are only stored with a fixed precision (by default, 1/1000 of a nm). The XTC format can only store coordinates. Its main advantage is that it requires less disk space than e.g. TRR or DCD trajectories and the loss of precision is usually not a problem.

If one wants to store Gromacs trajectories without loss of precision or with velocities and/or forces then one should use the TRR format (see module TRR).

The XTC I/O interface uses libxdrfile2 to implement random access to frames. This works by initially building an internal index of all frames and then using this index for direct seeks. Building the index is triggered by read_xtc_numframes(), which typically happens when one accesses the XTCReader.numframes attribute for the first time. Building the index may take many minutes for large trajectories but afterwards access is faster than with native Gromacs tools.

Changed in version 0.8.0: The XTC I/O interface now uses libxdrfile2, which has seeking and indexing capabilities. Note that unlike libxdrfile before it, libxdrfile2 is distributed under the GNU GENERAL PUBLIC LICENSE, version 2 (or higher). Timestep now correctly deals with presence/absence of coordinate/velocity/force information on a per-frame basis.

5.14.1. Module reference

class MDAnalysis.coordinates.XTC.Timestep(arg, **kwargs)[source]

Timestep for a Gromacs XTC trajectory.

copy()[source]

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

copy_slice(sel)[source]

Make a new Timestep containing a subset of the original Timestep.

ts.copy_slice(slice(start, stop, skip)) ts.copy_slice([list of indices])

Returns:A Timestep object of the same type containing all header information and all atom information relevent to the selection.

Note

The selection must be a 0 based slice or array of the atom indices in this Timestep

New in version 0.8.

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.XTC.XTCReader(filename, convert_units=None, sub=None, **kwargs)[source]

Read Gromacs XTC trajectory.

Arguments:
filename

the name of the trr file.

Keywords:
sub

an numpy integer array of what subset of trajectory atoms to load into the timestep. Intended to work similarly to the ‘sub’ argument to Gromacs‘ trjconv.

This is usefull when one has a Universe loaded with only an unsolvated protein, and wants to read a solvated trajectory.

The length of this array must be <= to the actual number of atoms in the trajectory, and equal to number of atoms in the Universe.

refresh_offsets

if True, do not retrieve stored offsets, but instead generate new ones; if False, use retrieved offsets if available [False]

Changed in version 0.9.0: New keyword refresh_offsets

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.

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.

load_offsets(filename, check=False)

Loads current trajectory offsets from pickled filename.

Checks if ctime and size of trajectory file matches that stored in pickled filename. If either one does not match (and check == True) then the offsets are not loaded. This is intended to conservatively avoid loading out-of-date offsets.

The offset file is expected to be a pickled dictionary with keys/values::
ctime
the ctime of the trajectory file
size
the size of the trajectory file
offsets
a numpy array of the offsets themselves
Arguments:
filename

filename of pickle file saved with save_offsets() with the frame offsets for the loaded trajectory

Keywords:
check

if False, ignore ctime and size check of trajectory file

Raises:

IOError if the file cannot be read (see open()).

next()

Forward one step to next frame.

numatoms

The number of publically available atoms that this reader will store in the timestep.

If ‘sub’ was not given in the ctor, then this value will just be the actual number of atoms in the underlying trajectory file. If however ‘sub’ was given, then this value is the number specified by the ‘sub’ sub-selection.

If for any reason the trajectory cannot be read then a negative value 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. If the trajectory was previously loaded and saved offsets exist, then loading will be significantly faster.

See also

TrjReader.load_offsets() and TrjReader.save_offsets()

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

save_offsets(filename)

Saves current trajectory offsets into filename, as a pickled object.

Along with the offsets themselves, the ctime and file size of the trajectory file are also saved. These are used upon load as a check to ensure the offsets still match the trajectory they are being applied to.

The offset file is a pickled dictionary with keys/values::
ctime
the ctime of the trajectory file
size
the size of the trajectory file
offsets
a numpy array of the offsets themselves
Arguments:
filename

filename in which to save the frame offsets

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.XTC.XTCWriter(filename, numatoms, start=0, step=1, delta=None, precision=1000.0, remarks=None, convert_units=None)[source]

Write a Gromacs XTC trajectory.

Create a new TrjWriter

Arguments:
filename

name of output file

numatoms

number of atoms in trajectory file

Keywords:
start

starting timestep; only used when delta is set.

step

skip between subsequent timesteps; only used when delta is set.

delta

timestep to use. If set will override any time information contained in the passed Timestep objects; otherwise that will be used. If in the latter case time is unavailable the TrjWriter will default to setting the trajectory time at 1 MDAnalysis unit (typically 1ps) per step.

precision

accuracy for lossy XTC format as a power of 10 (ignored for TRR) [1000.0]

convert_units

True: units are converted to the MDAnalysis base format; None selects the value of MDAnalysis.core.flags [‘convert_lengths’]. (see Flags)

Changed in version 0.8.0: The TRR writer is now able to write TRRs without coordinates/velocities/forces, depending on the properties available in the Timestep objects passed to write().

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