Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bond deletion #4763

Merged
merged 8 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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