Skip to content

Commit a5b7bf9

Browse files
committed
resolve comments
1 parent 2ac868b commit a5b7bf9

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

package/MDAnalysis/core/groups.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,6 +3254,16 @@ def select_atoms(self, sel, *othersel, periodic=True, rtol=1e-05,
32543254
For example, ``prop z >= 5.0`` selects all atoms with z
32553255
coordinate greater than 5.0; ``prop abs z <= 5.0`` selects all
32563256
atoms within -5.0 <= z <= 5.0.
3257+
relprop [abs] *property* *operator* *value* *selection*
3258+
selects atoms based on position relative to the center of
3259+
geometry (COG) of a given selection, using *property*
3260+
**x**, **y**, or **z** coordinate. Supports the **abs**
3261+
keyword (for absolute value) and the following
3262+
*operators*: **<, >, <=, >=, ==, !=**.
3263+
For example, ``relprop z >= 5.0 protein`` selects all atoms
3264+
with z coordinate greater than 5.0 relative to the COG
3265+
of protein; ``relprop abs z <= 5.0 protein`` selects all
3266+
atoms within -5.0 <= z <= 5.0 relative to the COG of protein.
32573267
sphzone *radius* *selection*
32583268
Selects all atoms that are within *radius* of the center of
32593269
geometry of *selection*

package/MDAnalysis/core/selection.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ class RelPropertySelection(PropertySelection):
13661366
def __init__(self, parser, tokens):
13671367
super().__init__(parser, tokens)
13681368
self.sel = parser.parse_expression(self.precedence)
1369-
# self.ori_value = self.value
1369+
self.periodic = parser.periodic
13701370

13711371
def _apply(self, group):
13721372
try:
@@ -1382,14 +1382,18 @@ def _apply(self, group):
13821382
try:
13831383
col = {"x": 0, "y": 1, "z": 2}[self.prop]
13841384
except KeyError:
1385-
pass
1385+
errmsg = f"Expected one of x y z for property, got {self.prop}"
1386+
raise SelectionError(errmsg) from None
13861387
else:
1387-
values = values[:, col]
13881388
sel = self.sel.apply(group)
1389-
rel_value = (
1390-
sel.center_of_geometry().reshape(3).astype(np.float32)[col]
1391-
)
1392-
values -= rel_value
1389+
ref_value = sel.center_of_geometry().reshape(3).astype(np.float32)
1390+
box = group.dimensions if self.periodic else None
1391+
if box is not None:
1392+
values = distances.minimize_vectors(
1393+
values - ref_value[None, :], box=box
1394+
)[:, col]
1395+
else:
1396+
values = values[:, col] - ref_value[col]
13931397

13941398
if self.absolute:
13951399
values = np.abs(values)

testsuite/MDAnalysisTests/core/test_atomselections.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,15 @@ def test_invalid_relprop_selection(self, universe):
10921092
universe.select_atoms("relprop parsnip < 2 index 0")
10931093
with pytest.raises(SelectionError, match="Unknown selection token"):
10941094
universe.select_atoms("relprop z < 2")
1095+
with pytest.raises(SelectionError, match="Expected one of x y z for property"):
1096+
universe.select_atoms("relprop id < 2 index 0")
1097+
empty_universe = mda.Universe.empty(
1098+
6, 2, atom_resindex=[0, 0, 0, 1, 1, 1]
1099+
)
1100+
with pytest.raises(
1101+
SelectionError, match="This Universe does not contain"
1102+
):
1103+
empty_universe.select_atoms("relprop z <= 1 index 0")
10951104

10961105

10971106
def test_segid_and_resid():

0 commit comments

Comments
 (0)