Skip to content

Commit eaf5ecd

Browse files
talagayevamruthesht
authored andcommitted
Implementation of WaterSelection for water resiude selection (MDAnalysis#4854)
* Update selection.py Addition of WaterSelection * Update test_atomselections.py added tests * Update CHANGELOG changelog addition mentioned * Update selection.py
1 parent c39f212 commit eaf5ecd

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

package/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixes
2727
the function to prevent shared state. (Issue #4655)
2828

2929
Enhancements
30+
* Addition of 'water' token for water selection (Issue #4839)
3031
* Enables parallelization for analysis.density.DensityAnalysis (Issue #4677, PR #4729)
3132
* Enables parallelization for analysis.contacts.Contacts (Issue #4660)
3233
* Enable parallelization for analysis.nucleicacids.NucPairDist (Issue #4670)

package/MDAnalysis/core/selection.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,37 @@ def _apply(self, group):
10941094
return group[mask]
10951095

10961096

1097+
class WaterSelection(Selection):
1098+
"""All atoms in water residues with recognized water residue names.
1099+
1100+
Recognized residue names:
1101+
1102+
* recognized 3 Letter resnames: 'H2O', 'HOH', 'OH2', 'HHO', 'OHH'
1103+
'TIP', 'T3P', 'T4P', 'T5P', 'SOL', 'WAT'
1104+
1105+
* recognized 4 Letter resnames: 'TIP2', 'TIP3', 'TIP4'
1106+
1107+
.. versionadded:: 2.9.0
1108+
"""
1109+
token = 'water'
1110+
1111+
# Recognized water resnames
1112+
water_res = {
1113+
'H2O', 'HOH', 'OH2', 'HHO', 'OHH',
1114+
'T3P', 'T4P', 'T5P', 'SOL', 'WAT',
1115+
'TIP', 'TIP2', 'TIP3', 'TIP4'
1116+
}
1117+
1118+
def _apply(self, group):
1119+
resnames = group.universe._topology.resnames
1120+
nmidx = resnames.nmidx[group.resindices]
1121+
1122+
matches = [ix for (nm, ix) in resnames.namedict.items()
1123+
if nm in self.water_res]
1124+
mask = np.isin(nmidx, matches)
1125+
1126+
return group[mask]
1127+
10971128
class BackboneSelection(ProteinSelection):
10981129
"""A BackboneSelection contains all atoms with name 'N', 'CA', 'C', 'O'.
10991130

testsuite/MDAnalysisTests/core/test_atomselections.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
PDB_helix,
4949
PDB_elements,
5050
PDB_charges,
51+
waterPSF,
52+
PDB_full,
5153
)
5254
from MDAnalysisTests import make_Universe
5355

@@ -650,6 +652,38 @@ def test_nucleicsugar(self, universe):
650652
assert_equal(rna.n_atoms, rna.n_residues * 5)
651653

652654

655+
class TestSelectionsWater(object):
656+
@pytest.fixture(scope='class')
657+
def universe(self):
658+
return MDAnalysis.Universe(GRO)
659+
660+
@pytest.fixture(scope='class')
661+
def universe2(self):
662+
return MDAnalysis.Universe(waterPSF)
663+
664+
@pytest.fixture(scope='class')
665+
def universe3(self):
666+
return MDAnalysis.Universe(PDB_full)
667+
668+
def test_water_gro(self, universe):
669+
# Test SOL water with 4 atoms
670+
water_gro = universe.select_atoms("water")
671+
assert_equal(water_gro.n_atoms, 44336)
672+
assert_equal(water_gro.n_residues, 11084)
673+
674+
def test_water_tip3(self, universe2):
675+
# Test TIP3 water with 3 atoms
676+
water_tip3 = universe2.select_atoms('water')
677+
assert_equal(water_tip3.n_atoms, 15)
678+
assert_equal(water_tip3.n_residues, 5)
679+
680+
def test_water_pdb(self, universe3):
681+
# Test HOH water with 1 atom
682+
water_pdb = universe3.select_atoms("water")
683+
assert_equal(water_pdb.n_residues, 188)
684+
assert_equal(water_pdb.n_atoms, 188)
685+
686+
653687
class BaseDistanceSelection(object):
654688
"""Both KDTree and distmat selections on orthogonal system
655689

0 commit comments

Comments
 (0)