Atom selections¶
Analysis commands and Python objects select atoms with
PQAnalysis.topology.selection.Selection. The same string language is
used in input-file keys such as reference_selection and
target_selection. Indices are 0-based.
Always check that the selection contains the intended atoms. RDF normalization, MSD statistics and VACF amplitudes all scale with that population.
String language¶
A selection string is parsed with a Lark grammar. Applied to
examples/water/trajectory.xyz, whose atoms are O (index 0), H (1) and
H (2), the common forms select:
String |
Meaning |
Atoms selected |
|---|---|---|
|
Atom-type name |
O |
|
Atom-type name |
both H |
|
Single index |
O |
|
Inclusive index range |
both H |
|
Inclusive index range |
all three |
|
Range with step |
O and the second H |
|
Element number or symbol |
O |
|
Element symbol |
both H |
|
Atom type and element |
O |
|
Every atom |
all three |
|
Set difference (all except H) |
O |
|
Union |
all three |
|
Intersection |
both H |
Operators¶
Statements combine with:
,union
&intersection
|set difference (left minus right)
Use parentheses whenever you mix operators. The parser’s implicit grouping
does not follow the order the class docstring describes (O,H&H is read as
(O,H)&H; see issue #185), so an
unparenthesized mix is not portable across versions.
Selections in input files¶
An unquoted input-file value may contain only letters, digits, _, -,
. and /. Put any selection with (, ), |, &, ,,
~, * or spaces in double or single quotes:
reference_selection = O
target_selection = "elem(O)|elem(H)"
target_selection = 'res~0-3, atom(H1, 1)'
Without quotes the parser stops at the first such character and the error names the line and column and shows the quoted form.
Ranges¶
Ranges are inclusive and read start..stop or start..step..stop. The
step is the middle value:
target_selection = 304..3..4822 # 304, 307, ..., 4822
304..4822..3 means start 304, step 4822 and stop 3, so it selects no
atoms. PQAnalysis logs a warning for a range whose start is larger than its
stop and an error for a step of 0 or less. If a whole selection of rdf,
msd, vacf or check_momentum selects no atoms, the analysis stops
with an error that names the selection.
Python¶
import numpy as np
from PQAnalysis.io import read_trajectory
from PQAnalysis.topology import Selection
traj = read_trajectory("examples/water/trajectory.xyz")
topology = traj[0].topology
Selection("O").select(topology) # array([0])
Selection("H").select(topology) # array([1, 2])
Selection("*|H").select(topology) # array([0])
Selection(np.array([0, 2])).select(topology) # array([0, 2])
Selection(None).select(topology) # all atoms
Selection also accepts a single Atom or Element. A plain Python
list of integers is rejected; pass a NumPy integer array.
use_full_atom_info¶
By default select(..., use_full_atom_info=False) matches on element type
only. Set use_full_atom_info=True to distinguish atom-type names that share
an element (PQ residue atom types). Residue-aware RDF exclusions additionally
need a restart and a moldescriptor; see Radial Distribution Function.
Analysis objects take the same strings as reference_species,
target_species or selection:
from PQAnalysis.analysis import RDF
from PQAnalysis.io import TrajectoryReader
RDF(
TrajectoryReader("examples/water/trajectory.xyz"),
reference_species="O",
target_species="H",
delta_r=0.5,
r_max=4.0,
)