### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 6. Selection exporters »

# 6.5. Base classes for the selection writers

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

class `MDAnalysis.selections.base.``SelectionWriter`(filename, mode='wa', numterms=None, preamble=None, **kwargs)

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

  The `SelectionWriter` 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.

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

  Set up for writing to filename.

  Arguments : | filename output file mode overwrite (“w”) for every write, append (“a”) to existing file, or overwrite an existing file and then append (“wa”) [“wa”] 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='wa', numterms=None, preamble=None, **kwargs)

    Set up for writing to filename.

    Arguments : | filename output file mode overwrite (“w”) for every write, append (“a”) to existing file, or overwrite an existing file and then append (“wa”) [“wa”] 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()`

  `write`(selection, number=None, name=None, frame=None, mode=None)

    Write selection to the output file.

    Arguments : | selection a `MDAnalysis.core.AtomGroup.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`]

  `_translate`(atoms, **kwargs)

    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)

    Initial output to open file object out.

  `_write_tail`(out, **kwargs)

    Last output to open file object out.

  `comment`(s)

    Return string s interpolated into the comment format string.

    If no `SelectionWriter.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.

`MDAnalysis.selections.base.``join`(seq, string='', func=None)

  Create a list from sequence.

  string is appended to each element but the last.

  func is applied to every element before appending string.

`MDAnalysis.selections.base.``get_writer`(filename, defaultformat)

  Return a `SelectionWriter` for filename or a defaultformat.

### Navigation

- index
- modules |
- next |
- previous |
- MDAnalysis 0.7.7 documentation »
- 6. Selection exporters »