Skip to content

Commit

Permalink
Fix bond deletion (#4763)
Browse files Browse the repository at this point in the history
* Fix issue where duplicate bonds were not being adequately deleted.
---------

Co-authored-by: Irfan Alibay <[email protected]>
  • Loading branch information
lilyminium and IAlibay authored Oct 26, 2024
1 parent 961cbd5 commit 800b4b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The rules for this file:
* 2.8.0

Fixes
* Fixes bug where deleting connections by index would only delete
one of multiple, if multiple are present (Issue #4762, PR #4763)
* Changes error to warning on Universe creation if guessing fails
due to missing information (Issue #4750, PR #4754)
* Adds guessed attributes documentation back to each parser page
Expand Down
3 changes: 2 additions & 1 deletion package/MDAnalysis/core/topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3146,7 +3146,8 @@ def _delete_bonds(self, values):
'{attrname} with atom indices:'
'{indices}').format(attrname=self.attrname,
indices=indices))
idx = [self.values.index(v) for v in to_check]
# allow multiple matches
idx = [i for i, x in enumerate(self.values) if x in to_check]
for i in sorted(idx, reverse=True):
del self.values[i]

Expand Down
11 changes: 11 additions & 0 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
two_water_gro, two_water_gro_nonames,
TRZ, TRZ_psf,
PDB, MMTF, CONECT,
PDB_conect
)

import MDAnalysis as mda
Expand Down Expand Up @@ -1247,6 +1248,16 @@ def test_delete_bonds_refresh_fragments(self, universe):
universe.delete_bonds([universe.atoms[[2, 3]]])
assert len(universe.atoms.fragments) == n_fragments + 1

@pytest.mark.parametrize("filename, n_bonds", [
(CONECT, 72),
(PDB_conect, 8)
])
def test_delete_all_bonds(self, filename, n_bonds):
u = mda.Universe(filename)
assert len(u.bonds) == n_bonds
u.delete_bonds(u.bonds)
assert len(u.bonds) == 0

@pytest.mark.parametrize(
'attr,values', existing_atom_indices
)
Expand Down

0 comments on commit 800b4b2

Please sign in to comment.