The classes in this module allow writing a selection to a file that can be read by another program to select the atoms in a MDAnalysis MDAnalysis.core.groups.AtomGroup
. Such cross-package interoperability allows a user to combine their favourite tools with MDAnalysis for further visualization or simulation.
Name | extension | IO | remarks |
---|---|---|---|
vmd | tcl | w | VMD macros, available in Representations; module MDAnalysis.selections.vmd |
pymol | pml | w | simple PyMOL selection string; module MDAnalysis.selections.pymol |
gromacs | ndx | w | Gromacs index file; module MDAnalysis.selections.gromacs |
charmm | str | w | CHARMM selection of individual atoms; module MDAnalysis.selections.charmm |
jmol | spt | w | Jmol selection commands; module MDAnalysis.selections.jmol |
The typical situation is that one has an ~MDAnalysis.core.groups.AtomGroup
and wants to work with the same selection of atoms in a different package, for example, to visualize the atoms in VMD. First create an AtomGroup
(named g
in the example below) and then use its ~MDAnalysis.core.groups.AtomGroup.write
method with the appropriate file extension (see Supported selection
exporters
for the recognized extension):
g = u.select_atoms('protein")
g.write("selections.vmd", name="mda_protein")
In VMD, sourcing the file selections.vmd
(written in Tcl) defines the "macro" mda_protein
that contains the atom indices to select
source selection.vmd
set sel [atomselect top mda_mdanalysis]
and in the GUI the macro appears in the Graphics -->
Representations
window in the list Selections: Singlewords
as "mda_protein".
The ~MDAnalysis.core.groups.AtomGroup.write
method can take additional keyword arguments, including mode
. The default is mode="w"
, which will overwrite the provided file. If mode="a"
then the selection is appended to the file.
Alternatively, one may use the ~MDAnalysis.selections.base.SelectionWriter
itself as a context manager and write each AtomGroup
inside the context. For example, to write multiple groups that were selected to mark different parts of a lipid bilayer to Gromacs index file named "leaflets.ndx":
with mda.selections.gromacs.SelectionWriter('leaflets.ndx', mode='w') as ndx:
ndx.write(upper_saturated, name='upper_sat')
ndx.write(lower_saturated, name='lower_sat')
ndx.write(upper_unsaturated, name='upper_unsat')
ndx.write(lower_unsaturated, name='lower_unsat')
There is a separate SelectionWriter
for each format, as described next.
There exist different ~MDAnalysis.selections.base.SelectionWriterBase
classes for different packages. The ~MDAnalysis.selections.get_writer
function can automatically pick the appropriate one, based on the file name extension in the Supported
selection exporters
.
MDAnalysis.selections.get_writer
Formats
Each module implements a SelectionWriter
for a specific format.
selections/vmd selections/pymol selections/gromacs selections/charmm selections/jmol selections/base