Groups of atoms¶
MDAnalysis has a hierarchy of Atom
containers that are used throughout the code.
First and foremost is the AtomGroup
. An AtomGroup
is the primary Atom
container; virtually everything can be accessed through it, as detailed in AtomGroup. This includes chemically meaningful groups of Atom
s such as a Residue
or a Segment
.
Residues and Segments¶
A Residue
is composed of Atom
s, and a Segment
is composed of Residues
.
The corresponding container groups are ResidueGroup
and SegmentGroup
. These have similar properties and available methods as AtomGroup
.
In [1]: import MDAnalysis as mda
In [2]: from MDAnalysis.tests.datafiles import TPR, XTC
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-3cec83db3cb9> in <module>
----> 1 from MDAnalysis.tests.datafiles import TPR, XTC
~/checkouts/readthedocs.org/user_builds/mdauserguide/conda/rms-1.0/lib/python3.8/site-packages/MDAnalysis/tests/datafiles.py in <module>
41
42 try:
---> 43 from MDAnalysisTests.datafiles import *
44 except ImportError:
45 print("*** ERROR ***")
~/checkouts/readthedocs.org/user_builds/mdauserguide/conda/rms-1.0/lib/python3.8/site-packages/MDAnalysisTests/__init__.py in <module>
124 try:
125 import matplotlib
--> 126 matplotlib.use('agg', warn=False)
127 except ImportError:
128 pass
TypeError: use() got an unexpected keyword argument 'warn'
In [3]: u = mda.Universe(TPR, XTC)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-b98a46cb93a1> in <module>
----> 1 u = mda.Universe(TPR, XTC)
NameError: name 'TPR' is not defined
In [4]: ag = u.atoms.select_atoms('resname ARG and name CA')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-80aae3768247> in <module>
----> 1 ag = u.atoms.select_atoms('resname ARG and name CA')
NameError: name 'u' is not defined
In [5]: ag
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-dc54cc151225> in <module>
----> 1 ag
NameError: name 'ag' is not defined
Each of these container groups can be accessed through another. The behaviour of this differs by level. For example, the residues of the ag
are the residues that the atoms of ag
belong to.
In [6]: ag.residues
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-85f67552c7f1> in <module>
----> 1 ag.residues
NameError: name 'ag' is not defined
Accessing the atoms of those residues, however, returns all the atoms in the residues, not just those originally in ag
.
In [7]: ag.residues.atoms
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-1ceccd273aeb> in <module>
----> 1 ag.residues.atoms
NameError: name 'ag' is not defined
The same applies to segments.
In [8]: ag[:3].segments.atoms
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-8f2401384f14> in <module>
----> 1 ag[:3].segments.atoms
NameError: name 'ag' is not defined
Similarly, an Atom
has direct knowledge of the Residue
and Segment
it belongs to. Note that an Atom
belongs to one Residue
and the residue belongs to one Segment
, but a Segment
has multiple residues.
In [9]: a = u.atoms[0]
In [10]: a.residue
Out[10]: <Residue LYSH, 0>
In [11]: a.residue.segment
Out[11]: <Segment seg_0_Protein_A>
In [12]: a.residue.segment.residues
Out[12]: <ResidueGroup with 129 residues>
For information on adding custom Residues or Segments, have a look at Adding a Residue or Segment to a Universe.
Fragments¶
Certain analysis methods in MDAnalysis also make use of additional ways to group atoms. A key concept is a fragment. A fragment is what is typically considered a molecule: an AtomGroup where any atom is reachable from any other atom in the AtomGroup by traversing bonds, and none of its atoms is bonded to any atoms outside the AtomGroup. (A ‘molecule’ in MDAnalysis methods refers to a GROMACS-specific concept). The fragments of a Universe are determined by MDAnalysis as a derived quantity. They can only be determined if bond information is available.
The fragments of an AtomGroup
are accessible via the fragments
property. Below is a Universe from a GROMACS TPR file of lysozyme (PDB ID: 2LYZ) with 101 water molecules. While it has 230 residues, there are only 102 fragments: 1 protein and 101 water fragments.
In [12]: len(u.residues)
Out[12]: 230
In [13]: len(u.atoms.fragments)
Out[13]: 102
See Topology objects for more on bonds and which file formats give MDAnalysis bond information.
You can also look at which fragment a particular Atom
belongs to:
In [9]: u.atoms[0].fragment # first atom of lysozyme
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-f5f6a0558be2> in <module>
----> 1 u.atoms[0].fragment # first atom of lysozyme
NameError: name 'u' is not defined
and see which fragments are associated with atoms in a smaller AtomGroup
:
In [10]: u.atoms[1959:1961].fragments
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-9bf24618be6c> in <module>
----> 1 u.atoms[1959:1961].fragments
NameError: name 'u' is not defined