Skip to content

Commit

Permalink
Merge pull request #484 from marrink-lab/issue_483
Browse files Browse the repository at this point in the history
Correct the version check in Molecule.remove_interaction
  • Loading branch information
fgrunewald committed Nov 18, 2022
2 parents 7ca7d6d + 07da7ef commit c50404d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion vermouth/molecule.py
Expand Up @@ -589,13 +589,15 @@ def remove_interaction(self, type_, atoms, version=0):
"""
idx = 0
for idx, interaction in enumerate(self.interactions[type_]):
if interaction.atoms == atoms and interaction.meta.get('version', 0):
if interaction.atoms == atoms and interaction.meta.get('version', 0) == version:
break
else: # no break
msg = ("Can't find interaction of type {} between atoms {} "
"and with version {}")
raise KeyError(msg.format(type_, atoms, version))
del self.interactions[type_][idx]
if not self.interactions[type_]:
del self.interactions[type_]

def remove_matching_interaction(self, type_, template_interaction):
"""
Expand Down
55 changes: 55 additions & 0 deletions vermouth/tests/test_molecule.py
Expand Up @@ -1279,3 +1279,58 @@ def test_to_molecule():
parameters=['a', '0.1', '300'],
meta={'b': 1}),]
assert ref_bonds == test_molecule.interactions['bonds']


@pytest.mark.parametrize('atoms, bonds, interactions, removed, expected', [
# empty molecule
([], [], [], {}, {}),
# interactions that all need to be removed
([1, 2], [(1, 2)], [('bond', (1, 2), {})], {'type_': 'bond', "atoms": (1, 2)}, {}),
# Molecule with interactions of different types of which some need to be removed
(
[1, 2, 3, 4, 5, 6, 7],
[(1, 2), (3, 4), (5, 7), (6, 7)],
[('bond', (1, 6), {}), ('bond', (5, 4), {}), ('angle', (1, 2), {})],
{'type_': 'bond', 'atoms': (5, 4)},
{'angle': [vermouth.molecule.Interaction(atoms=(1, 2), meta={}, parameters={})],
'bond': [vermouth.molecule.Interaction(atoms=(1, 6), meta={}, parameters={})]}
),
(
[1, 2, 3, 4],
[(1, 2), (2, 3), (3, 4)],
[('bond', (1, 2), {}), ('bond', (2, 3), {}), ('bond', (2, 3), {}, {'version': 1})],
{'type_': 'bond', 'atoms': (2, 3), 'version': 1},
{'bond': [
vermouth.molecule.Interaction(atoms=(1, 2), meta={}, parameters={}),
vermouth.molecule.Interaction(atoms=(2, 3), meta={}, parameters={}),
]},
),
(
[1, 2, 3, 4, 5, 6, 7],
[(1, 2), (3, 4), (5, 7), (6, 7)],
[('bond', (1, 6), [1, 2, 3], {'comment': 'a'}), ('bond', (5, 4), {'comment': 'b'}), ('angle', (1, 2), (4, 5, 6), {'version': 1})],
{'type_': 'bond', 'atoms': (5, 4)},
{'angle': [vermouth.molecule.Interaction(atoms=(1, 2), meta={'version': 1}, parameters=(4, 5, 6))],
'bond': [vermouth.molecule.Interaction(atoms=(1, 6), meta={'comment': 'a'}, parameters=[1, 2, 3])]}
),
])
def test_remove_interaction(atoms, bonds, interactions, removed, expected):
"""
Test whether molecule.remove_node also removes the corresponding
interactions
"""
molecule = vermouth.molecule.Molecule()
molecule.add_nodes_from(atoms)
molecule.add_edges_from(bonds)
for type_, atoms, params, *meta in interactions:
if meta:
meta = meta[0]
else:
meta = None
molecule.add_interaction(type_, atoms, params, meta=meta)

if removed:
molecule.remove_interaction(**removed)

assert molecule.interactions == expected

0 comments on commit c50404d

Please sign in to comment.