Skip to content

Commit 22433b4

Browse files
add 'chain' as alias for 'chainID' in selections (#5514)
1 parent f44c1e0 commit 22433b4

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎package/CHANGELOG‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ The rules for this file:
1414

1515

1616
-------------------------------------------------------------------------------
17-
??/??/?? IAlibay, sumitsaraswat362
17+
??/??/?? IAlibay, sumitsaraswat362, BradyAJohnston
1818

1919
* 2.12.0
2020

2121
Fixes
2222

2323
Enhancements
24+
* Added `chain` selection keyword as an alias of `chainID`, also usable in
25+
`same chain as` selections
2426
* InterRDF and InterRDF_s now expose computed average box volume and densities
2527
as results.box_volume, results.normalization_density, and (for InterRDF)
2628
results.density_1 and results.density_2 when norm is "rdf" or "density"

‎package/MDAnalysis/core/selection.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,17 @@ def _apply(self, group):
713713
return group[np.isin(nmidx, matches)]
714714

715715

716+
class ChainSelection(_ProtoStringSelection):
717+
"""Select atoms by chainID, alias of the ``chainID`` keyword
718+
719+
.. versionadded:: 2.12.0
720+
"""
721+
722+
token = "chain"
723+
field = "chainIDs"
724+
level = "ix"
725+
726+
716727
class AromaticSelection(Selection):
717728
"""Select aromatic atoms.
718729
@@ -1605,6 +1616,7 @@ class SameSelection(Selection):
16051616
"resname": "resnames",
16061617
"resid": "resids",
16071618
"segid": "segids",
1619+
"chain": "chainIDs",
16081620
"mass": "masses",
16091621
"charge": "charges",
16101622
"radius": "radii",

‎package/doc/sphinx/source/documentation_pages/selections.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ altLoc *alternative-location*
180180
that have an altLoc B record.
181181

182182
chainID *chain-name*
183-
a selection for atoms where chainIDs have been defined.
183+
a selection for atoms where chainIDs have been defined. ``chain`` can be
184+
used as an alias, e.g. ``chain A``.
184185

185186
element *element-name*
186187
a selection for atoms where elements have been defined. e.g. ``element H C``

‎testsuite/MDAnalysisTests/core/test_atomselections.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,20 @@ def test_chain_sel():
15501550
assert len(u.select_atoms("chainID A")) == len(u.atoms)
15511551

15521552

1553+
@pytest.mark.parametrize(
1554+
"sel",
1555+
["chain A", "chain A B", "chain A*", "same chain as index 0"],
1556+
)
1557+
def test_chain_alias_sel(sel):
1558+
u = mda.Universe(PDB_elements)
1559+
assert_equal(u.select_atoms(sel).ix, u.select_atoms("chainID A").ix)
1560+
1561+
1562+
def test_chain_alias_nomatch():
1563+
u = mda.Universe(PDB_elements)
1564+
assert len(u.select_atoms("chain B")) == 0
1565+
1566+
15531567
@pytest.fixture()
15541568
def u_fake_masses():
15551569
u = mda.Universe(TPR)

0 commit comments

Comments
 (0)