7.32. Common functions for coordinate reading — MDAnalysis.coordinates.core

Important base classes are collected in MDAnalysis.coordinates.base.

MDAnalysis.coordinates.core.reader(filename, format=None, **kwargs)[source]

Provide a trajectory reader instance for filename.

This function guesses the file format from the extension of filename and it will throw a TypeError if the extension is not recognized.

In most cases, no special keyword arguments are necessary.

All other keywords are passed on to the underlying Reader classes; see their documentation for details.

Parameters:
  • filename (str or tuple) – filename (or tuple of filenames) of the input coordinate file

  • kwargs – Keyword arguments for the selected Reader class.

Returns:

A trajectory Reader instance

Return type:

Reader

MDAnalysis.coordinates.core.writer(filename, n_atoms=None, **kwargs)[source]

Initialize a trajectory writer instance for filename.

Parameters:
  • filename (str) – Output filename of the trajectory; the extension determines the format.

  • n_atoms (int (optional)) – The number of atoms in the output trajectory; can be ommitted for single-frame writers.

  • multiframe (bool (optional)) – True: write a trajectory with multiple frames; False only write a single frame snapshot; None first try to get a multiframe writer and then fall back to single frame [None]

  • kwargs (optional) – Keyword arguments for the writer; all trajectory Writers accept start: starting time [0], step: step size in frames [1], dt: length of time between two frames, in ps [1.0] Some readers accept additional arguments, which need to be looked up in the documentation of the reader.

Returns:

A trajectory Writer instance

Return type:

Writer

Changed in version 0.7.6: Added multiframe keyword. See also get_writer_for().

Helper functions:

MDAnalysis.coordinates.core.get_reader_for(filename, format=None)[source]

Return the appropriate trajectory reader class for filename.

Parameters:
  • filename – filename of the input trajectory or coordinate file. Also can handle a few special cases, see notes below.

  • format (str or Reader (optional)) – Define the desired format. Can be a string to request a given Reader. If a class is passed, it will be assumed that this is a Reader and will be returned.

Returns:

A Reader object

Return type:

Reader

Raises:

ValueError – If no appropriate Reader is found

Notes

There are a number of special cases that can be handled:

  • If filename is a numpy array, MemoryReader is returned.

  • If filename is an MMTF object, MMTFReader is returned.

  • If filename is a ParmEd Structure, ParmEdReader is returned.

  • If filename is an iterable of filenames, ChainReader is returned.

Automatic detection is disabled when an explicit format is provided, unless a list of filenames is given, in which case ChainReader is returned and format passed to the ChainReader.

Changed in version 1.0.0: Added format_hint functionalityx

MDAnalysis.coordinates.core.get_writer_for(filename, format=None, multiframe=None)[source]

Return an appropriate trajectory or frame writer class for filename.

The format is determined by the format argument or the extension of filename. If format is provided, it takes precedence over the extension of filename.

Parameters:
  • filename (str or None) – If no format is supplied, then the filename for the trajectory is examined for its extension and the Writer is chosen accordingly. If None is provided, then NullWriter is selected (and all output is discarded silently).

  • format (str (optional)) – Explicitly set a format.

  • multiframe (bool (optional)) – True: write multiple frames to the trajectory; False: only write a single coordinate frame; None: first try trajectory (multi frame writers), then the single frame ones. Default is None.

Returns:

A Writer object

Return type:

Writer

Raises:
  • ValueError: – The format could not be deduced from filename or an unexpected value was provided for the multiframe argument.

  • TypeError: – No writer was found for the required format or the required filename argument was omitted.

Changed in version 0.7.6: Added multiframe keyword; the default None reflects the previous behaviour.

Changed in version 0.14.0: Removed the default value for the format argument. Now, the value provided with the format parameter takes precedence over the extension of filename. A ValueError is raised if the format cannot be deduced from filename.

Changed in version 0.16.0: The filename argument has been made mandatory.