From 87e71bb2fccfbf8ff3127130d1716c86f6682ccb Mon Sep 17 00:00:00 2001 From: Aditya Kamath <48089312+aditya-kamath@users.noreply.github.com> Date: Sun, 3 Jan 2021 23:38:37 -0500 Subject: [PATCH] Quick fix for atommethods to return empty residue group (#3089) Returns empty residue group for _get_prev_residues_by_resid and _get_next_residues_by_resid --- package/AUTHORS | 4 +++- package/CHANGELOG | 3 ++- package/MDAnalysis/core/topologyattrs.py | 10 ++++++++-- testsuite/MDAnalysisTests/core/test_topologyattrs.py | 12 ++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package/AUTHORS b/package/AUTHORS index 52b6003b9cf..960e078a627 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -151,7 +151,9 @@ Chronological list of authors - Nicholas Craven - Mieczyslaw Torchala - Haochuan Chen - +2021 + - Aditya Kamath + External code ------------- diff --git a/package/CHANGELOG b/package/CHANGELOG index eb6baa1bc40..590a7fb5fbc 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -16,11 +16,12 @@ The rules for this file: ??/??/?? tylerjereddy, richardjgowers, IAlibay, hmacdope, orbeckst, cbouy, lilyminium, daveminh, jbarnoud, yuxuanzhuang, VOD555, ianmkenney, calcraven,xiki-tempula, mieczyslaw, manuel.nuno.melo, PicoCentauri, - hanatok, rmeli + hanatok, rmeli, aditya-kamath * 2.0.0 Fixes + * atommethods(_get_prev/next_residues_by_resid) returns empty residue group when given empty residue group (Issue #3089) * MOL2 files without bonds can now be read and written (Issue #3057) * Write `CONECT` record only once in multi-model PDB files (Issue #3018) * Add '.nc' as a recognised extension for the NCDFWriter (Issue #3030) diff --git a/package/MDAnalysis/core/topologyattrs.py b/package/MDAnalysis/core/topologyattrs.py index 7515a519d80..2282b706f01 100644 --- a/package/MDAnalysis/core/topologyattrs.py +++ b/package/MDAnalysis/core/topologyattrs.py @@ -874,7 +874,10 @@ def _get_next_residues_by_resid(residues): .. versionadded:: 1.0.0 """ - u = residues[0].universe + try: + u = residues[0].universe + except IndexError: + return residues nxres = np.array([None]*len(residues), dtype=object) ix = np.arange(len(residues)) # no guarantee residues is ordered or unique @@ -914,7 +917,10 @@ def _get_prev_residues_by_resid(residues): .. versionadded:: 1.0.0 """ - u = residues[0].universe + try: + u = residues[0].universe + except IndexError: + return residues pvres = np.array([None]*len(residues)) pvres[:] = prev = u.residues[residues.ix-1] rsid = residues.segids diff --git a/testsuite/MDAnalysisTests/core/test_topologyattrs.py b/testsuite/MDAnalysisTests/core/test_topologyattrs.py index 2a80d99899e..bf82974550c 100644 --- a/testsuite/MDAnalysisTests/core/test_topologyattrs.py +++ b/testsuite/MDAnalysisTests/core/test_topologyattrs.py @@ -180,6 +180,18 @@ class TestAtomnames(TestAtomAttr): single_value = 'Ca2' attrclass = tpattrs.Atomnames + @pytest.fixture() + def u(self): + return mda.Universe(PSF, DCD) + + def test_prev_emptyresidue(self, u): + assert_equal(u.residues[[]]._get_prev_residues_by_resid(), + u.residues[[]]) + + def test_next_emptyresidue(self, u): + assert_equal(u.residues[[]]._get_next_residues_by_resid(), + u.residues[[]]) + class AggregationMixin(TestAtomAttr): def test_get_residues(self, attr):