From 299b89889661a179b24893791c9dbb2dcfff7852 Mon Sep 17 00:00:00 2001 From: jac16 Date: Tue, 27 Sep 2022 11:32:16 -0400 Subject: [PATCH] Amended hbond test for full coverage, added _get_dh_pairs ability to find hydrogen atoms. --- .../analysis/hydrogenbonds/hbond_analysis.py | 7 +++- .../analysis/test_hydrogenbonds_analysis.py | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py b/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py index 94ec0ab80d9..f99b98b80bf 100644 --- a/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py +++ b/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py @@ -592,7 +592,12 @@ def _get_dh_pairs(self): # Otherwise, use d_h_cutoff as a cutoff distance else: - hydrogens = self.u.select_atoms(self.hydrogens_sel) + if self.hydrogens_sel is None: + hydrogens_sel = self.guess_hydrogens() + else: + hydrogens_sel = self.hydrogens_sel + + hydrogens = self.u.select_atoms(hydrogens_sel) donors = self.u.select_atoms(self.donors_sel) donors_indices, hydrogen_indices = capped_distance( donors.positions, diff --git a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py index be434af6940..eb1a87c1af1 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py +++ b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py @@ -335,6 +335,40 @@ def test_no_bond_info_exception(self, universe): h = HydrogenBondAnalysis(universe, **kwargs) h._get_dh_pairs() + def test_no_bond_no_mass_donor_sel(self, universe): + + kwargs = { + 'donors_sel': "type O", + 'hydrogens_sel': None, + 'acceptors_sel': None, + 'd_h_cutoff': 1.2, + 'd_a_cutoff': 3.0, + 'd_h_a_angle_cutoff': 120.0 + } + + with pytest.raises(NoDataError, match="not contain mass information"): + h = HydrogenBondAnalysis(universe, **kwargs) + h._get_dh_pairs() + + def test_no_bond_donor_sel(self, universe): + + kwargs = { + 'donors_sel': "type O", + 'hydrogens_sel': None, + 'acceptors_sel': None, + 'd_h_cutoff': 1.2, + 'd_a_cutoff': 3.0, + 'd_h_a_angle_cutoff': 120.0 + } + u = universe.copy() + n_residues = 2 + u.add_TopologyAttr('mass', [15.999, 1.008, 1.008] * n_residues) + u.add_TopologyAttr('charge', [-1.04, 0.52, 0.52] * n_residues) + h = HydrogenBondAnalysis(u, **kwargs) + pairs = h._get_dh_pairs() + + assert len(pairs) == 2 + def test_first_hbond(self, hydrogen_bonds): assert len(hydrogen_bonds.results.hbonds) == 2 frame_no, donor_index, hydrogen_index, acceptor_index, da_dst, angle =\