Skip to content

Commit a44e6cd

Browse files
committed
add some preliminary guesser docs
1 parent 3287ec3 commit a44e6cd

File tree

4 files changed

+261
-72
lines changed

4 files changed

+261
-72
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
.. _default-guesser:
2+
3+
==============
4+
DefaultGuesser
5+
==============
6+
7+
8+
.. warning::
9+
10+
The default guesser has been created to reproduce pre-v2.8.0 MDAnalysis guessing behaviour as much as possible. **Default behaviours will change in MDAnalysis version 3**, as detailed in deprecation warnings.
11+
12+
13+
The :class:`~MDAnalysis.guesser.default_guesser.DefaultGuesser` is the default guessing context for MDAnalysis. For historical reasons the ``DefaultGuesser`` largely works with biological conventions; for example, an atom named CA will be assumed to be a carbon rather than a calcium atom.
14+
15+
16+
Attributes guessed
17+
==================
18+
19+
The topology attributes guessed by the default guesser are listed below, as are their dependencies and broad assumptions.
20+
Please see the `API documentation <https://docs.mdanalysis.org/stable/documentation_pages/guesser_modules/default_guesser.html>`_ for more details.
21+
22+
.. _default-guesser-types:
23+
24+
------------------
25+
Elements and types
26+
------------------
27+
28+
The default guesser guesses atom ``element``\ s and ``type``\ s using the same pathway; when atom ``type``\ s are guessed, they represent the atom ``element``. Atom elements are guessed from the atom name. The default guesser follows biological naming conventions, where atoms named "CA" are much more likely to represent an alpha-carbon than a calcium atom. This guesser is still relatively fragile for non-traditionally biological atom names.
29+
30+
The :meth:`~MDAnalysis.guesser.default_guesser.DefaultGuesser.guess_atom_element` method is used to guess atom elements or types following a process by which numbers, symbols, and some letters are stripped from the atom name and checked against a look-up table, as detailed in the linked API documentation. With this method, for example, "AO5*" would be guessed as "O", and "3hg2" as "H".
31+
32+
33+
------
34+
Masses
35+
------
36+
37+
Masses are guessed by using a look-up table to get masses from the atom's ``element`` attribute. If ``element``\ s are not available, the atom's ``type`` is used in place of the element. If the ``type`` is not available, that is
38+
:ref:`guessed first <default-guesser-types>`.
39+
40+
41+
.. warning::
42+
43+
When an atom mass cannot be guessed from the atom ``type`` or ``name``, the atom is currently assigned a mass of 0.0.
44+
45+
Masses are guessed atom-by-atom, so even if most atoms have been guessed correctly, it is possible that some have been given masses of 0. It is important to check for non-zero masses before using methods that rely on them, such as :meth:`AtomGroup.center_of_mass`.
46+
47+
48+
.. important::
49+
50+
`np.nan` will be used as a default or "missing" value
51+
in place of 0.0 for atom masses in version 3.0 of MDAnalysis.
52+
53+
54+
-------------
55+
Aromaticities
56+
-------------
57+
58+
These are guessed using the :ref:`RDKit <RDKit-format>` converter by using the ``GetIsAromatic`` method.
59+
60+
61+
-----------------------------------
62+
Bonds, Angles, Dihedrals, Impropers
63+
-----------------------------------
64+
65+
MDAnalysis can guess if bonds exist between two atoms, based on the distance between them. A bond is created if the 2 atoms are within
66+
67+
.. math::
68+
69+
d < f \cdot (R_1 + R_2)
70+
71+
of each other, where :math:`R_1` and :math:`R_2` are the VdW radii
72+
of the atoms and :math:`f` is an ad-hoc *fudge_factor*. This is
73+
the `same algorithm that VMD uses`_.
74+
75+
.. note::
76+
77+
Previously, guessing bonds would also guess angles and dihedrals. This is no longer the case. Angles, dihedrals, and impropers are not guessed by the default guesser unless
78+
explicitly requested by the user.
79+
80+
81+
Angles can be guessed from the bond connectivity. MDAnalysis assumes that if atoms 1 & 2 are bonded, and 2 & 3 are bonded, then (1,2,3) must be an angle.
82+
83+
::
84+
85+
1
86+
\
87+
2 -- 3
88+
89+
Dihedral angles and improper dihedrals can both be guessed from angles. Proper dihedrals are guessed by assuming that if (1,2,3) is an angle, and 3 & 4 are bonded, then (1,2,3,4) must be a dihedral.
90+
91+
::
92+
93+
1 4
94+
\ /
95+
2 -- 3
96+
97+
Likewise, if (1,2,3) is an angle, and 2 & 4 are bonded, then (2, 1, 3, 4) must be an improper dihedral (i.e. the improper dihedral is the angle between the planes formed by (1, 2, 3) and (1, 3, 4))
98+
99+
::
100+
101+
1
102+
\
103+
2 -- 3
104+
/
105+
4
106+
107+
108+
.. _`same algorithm that VMD uses`:
109+
http://www.ks.uiuc.edu/Research/vmd/vmd-1.9.1/ug/node26.html

doc/source/formats/guessing.rst

Lines changed: 0 additions & 71 deletions
This file was deleted.

doc/source/guessing.rst

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.. -*- coding: utf-8 -*-
2+
.. _guessing:
3+
4+
============================
5+
Guessing Topology Attributes
6+
============================
7+
8+
Since version 2.8.0 MDAnalysis has introduced a new context-dependent guessing API to guess topology attributes that are not read from the file. This allows topology attributes such as masses, charges, and atom types to be guessed from existing information in a context-dependent manner (e.g. biological naming conventions) rather than file formats, as was previously done.
9+
10+
.. list-table:: Supported guesser contexts
11+
:widths: 25 25 50
12+
:header-rows: 1
13+
14+
* - Guesser
15+
- Context name
16+
- Topology attributes guessed
17+
* - :ref:`DefaultGuesser <default-guesser>`
18+
- "default"
19+
- elements, types, masses, bonds, angles, dihedrals, impropers, aromaticities
20+
21+
22+
Guessing at Universe creation
23+
=============================
24+
25+
Topology attributes can be guessed at Universe creation by passing in topology attributes to guess to the ``to_guess`` keyword. By default, as of version 2.8.0, the default guesser is used to guess ``types`` and ``masses``.
26+
27+
28+
.. ipython:: python
29+
30+
import MDAnalysis as mda
31+
from MDAnalysis.tests.datafiles import PRM12
32+
u = mda.Universe(PRM12, context="default", to_guess=["types", "masses", "bonds"])
33+
u.atoms.bonds
34+
35+
36+
In general, guessing at Universe creation works very similarly to guessing using the guess_TopologyAttrs interface documented below. The main difference is that passing guesser-specific keyword arguments such as ``fudge_factor`` and ``vdwradii`` into Universe creation is **now deprecated and will be removed in version 3.0**. Instead, we recommend specifying these arguments through an explicit call to the :ref:`guess_TopologyAttrs <guess-topologyAttrs>` method.
37+
38+
.. _guess-topologyAttrs:
39+
40+
Guessing using the guess_TopologyAttrs interface
41+
================================================
42+
43+
Topology attributes can also be guessed after Universe creation using the :meth:`~MDAnalysis.core.universe.Universe.guess_TopologyAttrs` method. The ``to_guess``, ``force_guess``, and ``context`` keywords are used to specify which attributes to guess, which attributes to forcibly re-guess, and which guesser to use, respectively. These three keywords perform the same way here as they do in Universe creation.
44+
45+
As with Universe creation, the :ref:`DefaultGuesser <default-guesser>` is used as the default ``context``. The following example demonstrates how to guess atom types and masses after Universe creation.
46+
47+
.. ipython:: python
48+
49+
u = mda.Universe(PRM12, to_guess=[]) # in v2.8.0 masses and types are guessed by default
50+
u.guess_TopologyAttrs(to_guess=["types", "masses"])
51+
u.atoms.types
52+
53+
54+
The context can be specified either using a string (e.g. ``"default"``) or an already created Guesser object. It may be convenient to pass in an already-created Guesser object if there are particular keywords you want to use in guessing methods, such as the ``fudge_factor``, ``vdwradii`` or ``lower_bound`` keywords for controlling bond guessing. However, if additional keyword arguments are passed into ``guess_TopologyAttrs``, they will **replace** any existing arguments inside the guesser.
55+
56+
.. ipython:: python
57+
58+
from MDAnalysis.guesser import DefaultGuesser
59+
from MDAnalysis.tests.datafiles import CONECT
60+
61+
u = mda.Universe(CONECT)
62+
guesser = DefaultGuesser(u, fudge_factor=1.2)
63+
u.guess_TopologyAttrs(to_guess=["bonds"], context=guesser, fudge_factor=0.5)
64+
guesser._kwargs["fudge_factor"]
65+
66+
67+
--------------------
68+
Forcibly re-guessing
69+
--------------------
70+
71+
MDAnalysis will preferentially read topology attributes from file instead of re-guessing them, even if the attribute is passed into ``to_guess``. For example, below, the ``types`` attributes reflects the actual atom types in the file.
72+
73+
.. ipython::
74+
75+
u = mda.Universe(PRM12, to_guess=["types", "masses"])
76+
u.atoms.types
77+
78+
.. note::
79+
80+
In cases where the attribute is only present for *some* atoms in the file (e.g. a patchy element column in a PDB), MDAnalysis will only guess the attribute for atoms where it is not present in the file.
81+
82+
To force MDAnalysis to re-guess a TopologyAttr, pass in the attribute to the ``force_guess`` keyword. This will force MDAnalysis to guess the attribute even if it is present in the file.
83+
84+
.. ipython:: python
85+
86+
u.guess_TopologyAttrs(to_guess=["types"], force_guess=["types"])
87+
u.atoms.types
88+
89+
90+
------------------------------------
91+
Guessing bonds, angles, and torsions
92+
------------------------------------
93+
94+
Whereas most attributes are guessed at the atom, residue, or segment level, guessing topology objects such as bonds, angles, dihedrals and impropers behaves somewhat differently, and interacts with the ``force_guess`` keyword specially.
95+
96+
Specifically, if these connectivity attributes are guessed, they are by default guessed *additively*. Therefore, if bonds and other objects are guessed twice, the bonds of the second guess are added on. Below, we see the number of bonds increase when guessed again with a looser criteria.
97+
98+
.. ipython::
99+
100+
from MDAnalysis.tests.datafiles import CONECT
101+
102+
u = mda.Universe(CONECT, to_guess=["bonds"])
103+
print(len(u.bonds))
104+
u.guess_TopologyAttrs(to_guess=["bonds"], fudge_factor=1.2) # looser
105+
print(len(u.bonds))
106+
107+
However, the number of bonds doesn't change when the bonds are guessed again with a stricter criteria -- no new bonds are found.
108+
109+
.. ipython::
110+
111+
u.guess_TopologyAttrs(to_guess=["bonds"], fudge_factor=0.5) # stricter
112+
print(len(u.bonds))
113+
114+
Moreover, bonds are unique, so if the bonds are guessed again with a same criteria, the guessed bonds doesn't change.
115+
116+
.. ipython::
117+
118+
u.guess_TopologyAttrs(to_guess=["bonds"], fudge_factor=0.5) # same
119+
print(len(u.bonds))
120+
121+
However, if you want to forcibly overwrite all existing bonds, angles, dihedrals or impropers, you can pass the object to the ``force_guess`` keyword. This will remove all existing objects of that type before guessing. Below, we see the number of bonds has shrunk when guessed with stricter criteria.
122+
123+
.. ipython::
124+
125+
u.guess_TopologyAttrs(to_guess=["bonds"], force_guess=["bonds"], fudge_factor=0.5)
126+
print(len(u.bonds))
127+
128+
-----------------
129+
Order of guessing
130+
-----------------
131+
132+
The order of the attributes guessed can matter in some cases. For example, bond guessing with the DefaultGuesser relies on looking up the vdW radii of the atoms involved by their atom ``type``. (**Note: this behaviour will likely change to looking up by element in version 3.0**). That means that for file formats where the atom ``type`` is not a valid element, the atom ``type`` must be forcefully re-guessed for bond-guessing to work.
133+
134+
Therefore the following will not work due to the types encoded in the PSF file:
135+
136+
.. ipython:: python
137+
:okexcept:
138+
139+
from MDAnalysis.tests.datafiles import PSF, DCD
140+
u = mda.Universe(PSF, DCD)
141+
print(u.atoms.types)
142+
u.guess_TopologyAttrs(to_guess=["bonds"])
143+
144+
However, the snippet below will re-guess the types, and now bond-guessing can work as the elements have vdW radii defined:
145+
146+
.. ipython:: python
147+
148+
u.guess_TopologyAttrs(to_guess=["types", "bonds"], force_guess=["types"])
149+
print(u.atoms.types)
150+
151+

doc/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Wherever possible, do not take these conversations to private channels, includin
8585
groups_of_atoms
8686
selections
8787
topology_system
88+
guessing
8889

8990
.. toctree::
9091
:maxdepth: 1
@@ -103,7 +104,6 @@ Wherever possible, do not take these conversations to private channels, includin
103104

104105
reading_and_writing
105106
formats/index
106-
formats/guessing
107107
formats/auxiliary
108108
formats/selection_exporters
109109
formats/format_reference

0 commit comments

Comments
 (0)