From bd9590012bfae8523755c1d7b9ff6ee12dd3cf4d Mon Sep 17 00:00:00 2001 From: jac16 Date: Wed, 28 Sep 2022 20:55:16 -0400 Subject: [PATCH] hbond_analysis complete codecov --- .gitignore | 2 +- .../analysis/hydrogenbonds/hbond_analysis.py | 5 ++- .../analysis/test_hydrogenbonds_analysis.py | 31 +++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 0760f8129e4..ab758cf45e1 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ examples/output.txt # ignore Vagrant virtual machines .vagrant # ignore coverage files -.coverage +.coverage* .noseids htmlcov # ignore trajectory offset caches diff --git a/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py b/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py index f99b98b80bf..9201f04fe7a 100644 --- a/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py +++ b/package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py @@ -486,7 +486,10 @@ def guess_donors(self, select='all', max_charge=-0.5): hydrogens_sel = self.hydrogens_sel hydrogens_ag = self.u.select_atoms(hydrogens_sel) - if hasattr(hydrogens_ag[0],"bonded_atoms") and hydrogens_ag[0].bonded_atoms: + # We're using u._topology.bonds rather than u.bonds as it is a million times faster to access. + # This is because u.bonds also calculates properties of each bond (e.g bond length). + # See https://github.com/MDAnalysis/mdanalysis/issues/2396#issuecomment-596251787 + if (hasattr(self.u._topology, 'bonds') and len(self.u._topology.bonds.values) != 0): donors_ag = find_hydrogen_donors(hydrogens_ag) else: ag = hydrogens_ag.residues.atoms.select_atoms( diff --git a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py index eb1a87c1af1..bf44af95de0 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py +++ b/testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py @@ -204,6 +204,23 @@ def test_no_bond_info_exception(self, universe): 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) + donors = u.select_atoms(h.guess_donors()) + 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 =\ @@ -319,6 +336,16 @@ def hydrogen_bonds(universe): h.run() return h + def test_count_by_type(self, universe): + + h = HydrogenBondAnalysis( + universe, + **TestHydrogenBondAnalysisNoRes.kwargs + ) + h.run() + counts = h.count_by_type() + ref_count = 2 + assert int(counts[0, 2]) == ref_count def test_no_bond_info_exception(self, universe): @@ -365,9 +392,9 @@ def test_no_bond_donor_sel(self, universe): 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() + donors = u.select_atoms(h.guess_donors()) - assert len(pairs) == 2 + assert len(donors) == 2 def test_first_hbond(self, hydrogen_bonds): assert len(hydrogen_bonds.results.hbonds) == 2