7.2.6. Base classes for the selection writers

Specialized SelectionWriters are derived from SelectionWriterBase. Override the _write_head(), _translate(), and _write_tail() methods.

class MDAnalysis.selections.base.SelectionWriterBase(filename, mode='w', numterms=None, preamble=None, **kwargs)[source]

Export a selection in MDAnalysis to a format usable in an external package.

The SelectionWriterBase writes a selection string to a file that can be used in another package such as VMD, PyMOL, Gromacs or CHARMM. In this way, analysis and visualization can be done with the best or most convenient tools at hand.

SelectionWriterBase is a base class and child classes are derived with the appropriate customizations for the package file format.

Changed in version 0.11.0: Can now also write to a NamedStream instead of a normal file (using openany).

Changed in version 0.16.0: Remove the wa mode. The file is now open when the instance is created and closed with the close() method or when exiting the with statement.

Set up for writing to filename.

Parameters:
  • filename – output file
  • mode – create a new file (“w”), or append (“a”) to existing file [“w”]
  • numterms – number of individual index numbers per line for output formats that write multiple entries in one line. If set to 0 or False then no special formatting is done [8]
  • preamble – string that is written as a comment at the top of the file []
  • kwargs – use as defaults for write()
__init__(filename, mode='w', numterms=None, preamble=None, **kwargs)[source]

Set up for writing to filename.

Parameters:
  • filename – output file
  • mode – create a new file (“w”), or append (“a”) to existing file [“w”]
  • numterms – number of individual index numbers per line for output formats that write multiple entries in one line. If set to 0 or False then no special formatting is done [8]
  • preamble – string that is written as a comment at the top of the file []
  • kwargs – use as defaults for write()
_translate(atoms, **kwargs)[source]

Translate atoms into a list of native selection terms.

  • build list of ALL selection terms as if this was a single line, e.g. ['index 12 |', 'index 22 |', 'index 33']

  • only one term per atom!!

  • terms must be strings

  • something like::

    ” “.join(terms)

    must work

_write_head(out, **kwargs)[source]

Initial output to open file object out.

_write_tail(out, **kwargs)[source]

Last output to open file object out.

comment(s)[source]

Return string s interpolated into the comment format string.

If no SelectionWriterBase.commentfmt is defined (None) then the empty string is returned because presumably there is no way to enter comments into the file.

A newline is appended to non-empty strings.

write(selection, number=None, name=None, frame=None, mode=None)[source]

Write selection to the output file.

Parameters:
  • selection – a MDAnalysis.core.groups.AtomGroup
  • number – selection will be named “mdanalysis<number>” (None auto increments between writes; useful when appending) [None]
  • name – selection will be named name (instead of numbered) [None]
  • frame – write selection of this frame (or the current one if None [None]
MDAnalysis.selections.base.join(seq, string='', func=None)[source]

Create a list from sequence.

string is appended to each element but the last.

func is applied to every element before appending string.