5.18. Base classes — MDAnalysis.coordinates.base

Derive other Reader and Writer classes from the classes in this module. The derived classes must follow the Trajectory API in MDAnalysis.coordinates.__init__.

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

Timestep data for one frame

Methods:

ts = Timestep(numatoms)

create a timestep object with space for numatoms (done automatically)

ts[i]

return coordinates for the i’th atom (0-based)

ts[start:stop:skip]

return an array of coordinates, where start, stop and skip correspond to atom indices, MDAnalysis.core.AtomGroup.Atom.number (0-based)

for x in ts

iterate of the coordinates, atom by atom

_pos

numpy.ndarray of dtype float32 of shape (numatoms, 3) and internal FORTRAN order, holding the raw cartesian coordinates (in MDAnalysis units, i.e. Å).

Note

Normally one does not directly access _pos but uses the coordinates() method of an AtomGroup but sometimes it can be faster to directly use the raw coordinates. Any changes to this array are immediately reflected in atom positions. If the frame is written to a new trajectory then the coordinates are changed. If a new trajectory frame is loaded, then all contents of _pos are overwritten.

_velocities

numpy.ndarray of dtype float32. of shape (numatoms, 3), holding the raw velocities (in MDAnalysis units, i.e. typically Å/ps).

Note

Normally velocities are accessed through the velocities() method of an AtomGroup but this attribute is documented as there can be occasions when it is required (e.g. in order to change velocities) or much more convenient or faster to access the raw velocities directly.

_velocities only exists if the underlying trajectory format supports velocities. Your code should check for its existence or handle an AttributeError gracefully.

New in version 0.7.5.

numatoms

number of atoms

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)

lengths a, b, c are in the MDAnalysis length unit (Å), and angles are in degrees.

dimensions is read-only because it transforms the actual format of the unitcell (which differs between different trajectory formats) to the representation described here, which is used everywhere in MDAnalysis.

volume

volume of the unitcell

class MDAnalysis.coordinates.base.IObase[source]

Base class bundling common functionality for trajectory I/O.

Changed in version 0.8: Added context manager protocol.

close()[source]

Close the trajectory file.

close_trajectory()[source]

Specific implementation of trajectory closing.

convert_forces_from_native(force, inplace=True)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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.

format = None

override to define trajectory format of the reader/writer (DCD, XTC, ...)

units = {'velocity': None, 'length': None, 'time': None}

dict with units of of time and length (and velocity, force, ... for formats that support it)

class MDAnalysis.coordinates.base.Reader[source]

Base class for trajectory readers.

See the Trajectory API definition in MDAnalysis.coordinates.__init__ for the required attributes and methods.

OtherWriter(filename, **kwargs)[source]

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)[source]

Returns a trajectory writer with the same properties as this trajectory.

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()[source]

Forward one step to next frame.

rewind()[source]

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.base.ChainReader(filenames, **kwargs)[source]

Reader that concatenates multiple trajectories on the fly.

Known issues

  • Trajectory API attributes exist but most of them only reflect the first trajectory in the list; ChainReader.numframes, ChainReader.numatoms, and ChainReader.fixed are properly set, though
  • slicing not implemented
  • time will not necessarily return the true time but just number of frames times a provided time between frames (from the keyword delta)

Set up the chain reader.

Arguments:
filenames

file name or list of file names; the reader will open all file names and provide frames in the order of trajectories from the list. Each trajectory must contain the same number of atoms in the same order (i.e. they all must belong to the same topology). The trajectory format is deduced from the extension of filename.

Extension: filenames are either single filename or list of file names in either plain file names format or (filename,format) tuple combination

skip

skip step (also passed on to the individual trajectory readers); must be same for all trajectories

delta

The time between frames in MDAnalysis time units if no other information is available. If this is not set then any call to time will raise a ValueError.

kwargs

all other keyword arguments are passed on to each trajectory reader unchanged

Changed in version 0.8: The delta keyword was added.

active_reader

Reader instance from which frames are being read.

frame

Cumulative frame number of the current time step.

Note

The frame number is 1-based, i.e. the first frame has frame number 1. However, frame indices (used for indexing and slicing with the trajectory[frame_index] notation use a 0-based index, i.e. frame - 1.

rewind()[source]

Set current frame to the beginning.

time

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

class MDAnalysis.coordinates.base.Writer[source]

Base class for trajectory writers.

See Trajectory API definition in MDAnalysis.coordinates.__init__ for the required attributes and methods.

convert_dimensions_to_unitcell(ts)[source]

Read dimensions from timestep ts and return appropriate unitcell.

The default is to return [A,B,C,alpha,beta,gamma]; if this is not appropriate then this method has to be overriden.

has_valid_coordinates(criteria, x)[source]

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)[source]

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.