From 1ff951d2c2302539fe1483491b5b92d5870a331d Mon Sep 17 00:00:00 2001 From: csbrasnett Date: Fri, 20 Oct 2023 13:47:28 +0200 Subject: [PATCH 1/4] add warning when mod not found --- vermouth/processors/annotate_mut_mod.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/vermouth/processors/annotate_mut_mod.py b/vermouth/processors/annotate_mut_mod.py index a5a834421..5ee17d73b 100644 --- a/vermouth/processors/annotate_mut_mod.py +++ b/vermouth/processors/annotate_mut_mod.py @@ -210,10 +210,12 @@ def annotate_modifications(molecule, modifications, mutations): (mutations, 'mutation', molecule.force_field.blocks)] residue_graph = make_residue_graph(molecule) - for res_idx in residue_graph: - for mutmod, key, library in associations: - for resspec, mod in mutmod: + for mutmod, key, library in associations: + for resspec, mod in mutmod: + mod_found = False + for res_idx in residue_graph: if residue_matches(resspec, residue_graph, res_idx): + mod_found = True if mod != 'none' and mod not in library: raise NameError('{} is not known as a {} for ' 'force field {}' @@ -223,7 +225,11 @@ def annotate_modifications(molecule, modifications, mutations): _format_resname(res), key, mod) for node_idx in res['graph']: molecule.nodes[node_idx][key] = molecule.nodes[node_idx].get(key, []) + [mod] - + if mod_found == False: + LOGGER.warning('{} with resid {} not found. ' + 'No modification made.' + ''.format(resspec['resname'], resspec['resid'])) + class AnnotateMutMod(Processor): """ From 81d9f7138cb80a758311fde98182209f8722b2ed Mon Sep 17 00:00:00 2001 From: csbrasnett Date: Fri, 20 Oct 2023 16:30:00 +0200 Subject: [PATCH 2/4] fixed tests --- vermouth/tests/test_annotate_mut_mod.py | 43 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/vermouth/tests/test_annotate_mut_mod.py b/vermouth/tests/test_annotate_mut_mod.py index 6afd1d1fe..0899eb0bd 100644 --- a/vermouth/tests/test_annotate_mut_mod.py +++ b/vermouth/tests/test_annotate_mut_mod.py @@ -17,6 +17,8 @@ import networkx as nx import pytest +from contextlib import nullcontext as does_not_raise +import logging from vermouth.molecule import Molecule from vermouth.forcefield import ForceField from vermouth.processors.annotate_mut_mod import ( @@ -270,7 +272,7 @@ def test_annotate_mutmod_processor(example_mol, modifications, mutations, expect [(0, 1), (1, 2)], {1: ['N-ter'], 3: ['C-ter']} ), -( + ( [ {'resname': 'XXX', 'resid': 1}, {'resname': 'ALA', 'resid': 2}, @@ -289,7 +291,8 @@ def test_nter_cter_modifications(node_data, edge_data, expected): mol = Molecule(force_field=ForceField(FF_UNIVERSAL_TEST)) mol.add_nodes_from(enumerate(node_data)) mol.add_edges_from(edge_data) - modification = [({'resname': 'cter'}, 'C-ter'), ({'resname': 'nter'}, 'N-ter')] + modification = [({'resname': 'cter', 'resid': 3}, 'C-ter'), + ({'resname': 'nter', 'resid': 1}, 'N-ter')] annotate_modifications(mol, modification, []) @@ -300,3 +303,39 @@ def test_nter_cter_modifications(node_data, edge_data, expected): found[node['resid']] = node['modification'] assert found == expected + +@pytest.mark.parametrize('node_data, edge_data, expected', [ + ( + [ + {'resname': 'GLY', 'resid': 1}, + {'resname': 'ALA', 'resid': 2}, + {'resname': 'ALA', 'resid': 3} + ], + [(0, 1), (1, 2)], + False + ), + ( + [ + {'resname': 'ALA', 'resid': 1}, + {'resname': 'ALA', 'resid': 2}, + {'resname': 'ALA', 'resid': 3} + ], + [(0, 1), (1, 2)], + True + )]) +def test_mod_resid_not_correct(caplog, node_data, edge_data, expected): + """ + Tests that the modification is found in the expected residue. + """ + mol = Molecule(force_field=ForceField(FF_UNIVERSAL_TEST)) + mol.add_nodes_from(enumerate(node_data)) + mol.add_edges_from(edge_data) + mutation = [({'resname': 'GLY', 'resid': 1}, 'MET')] + + caplog.clear() + annotate_modifications(mol, [], mutation) + + if expected: + assert 'GLY with resid 1 not found. No modification made.' in str(caplog.records[0].getMessage()) + else: + assert caplog.records == [] From 3c643aeb04ed44c60537a0cf4817c510becee0ad Mon Sep 17 00:00:00 2001 From: csbrasnett Date: Mon, 23 Oct 2023 11:37:20 +0200 Subject: [PATCH 3/4] made requested changes --- vermouth/processors/annotate_mut_mod.py | 7 +++---- vermouth/tests/test_annotate_mut_mod.py | 6 ++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/vermouth/processors/annotate_mut_mod.py b/vermouth/processors/annotate_mut_mod.py index 5ee17d73b..52bdee045 100644 --- a/vermouth/processors/annotate_mut_mod.py +++ b/vermouth/processors/annotate_mut_mod.py @@ -226,10 +226,9 @@ def annotate_modifications(molecule, modifications, mutations): for node_idx in res['graph']: molecule.nodes[node_idx][key] = molecule.nodes[node_idx].get(key, []) + [mod] if mod_found == False: - LOGGER.warning('{} with resid {} not found. ' - 'No modification made.' - ''.format(resspec['resname'], resspec['resid'])) - + LOGGER.warning('Mutation "{}" not found. ' + 'Check target resid!' + ''.format(_format_resname(resspec))) class AnnotateMutMod(Processor): """ diff --git a/vermouth/tests/test_annotate_mut_mod.py b/vermouth/tests/test_annotate_mut_mod.py index 0899eb0bd..e0f4b3580 100644 --- a/vermouth/tests/test_annotate_mut_mod.py +++ b/vermouth/tests/test_annotate_mut_mod.py @@ -17,8 +17,6 @@ import networkx as nx import pytest -from contextlib import nullcontext as does_not_raise -import logging from vermouth.molecule import Molecule from vermouth.forcefield import ForceField from vermouth.processors.annotate_mut_mod import ( @@ -291,8 +289,8 @@ def test_nter_cter_modifications(node_data, edge_data, expected): mol = Molecule(force_field=ForceField(FF_UNIVERSAL_TEST)) mol.add_nodes_from(enumerate(node_data)) mol.add_edges_from(edge_data) - modification = [({'resname': 'cter', 'resid': 3}, 'C-ter'), - ({'resname': 'nter', 'resid': 1}, 'N-ter')] + modification = [({'resname': 'cter'}, 'C-ter'), + ({'resname': 'nter'}, 'N-ter')] annotate_modifications(mol, modification, []) From 965668900da8d96e62fcc855aa56d2f76fe9f889 Mon Sep 17 00:00:00 2001 From: csbrasnett Date: Mon, 23 Oct 2023 11:51:52 +0200 Subject: [PATCH 4/4] change expected test output --- vermouth/tests/test_annotate_mut_mod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vermouth/tests/test_annotate_mut_mod.py b/vermouth/tests/test_annotate_mut_mod.py index e0f4b3580..3608258be 100644 --- a/vermouth/tests/test_annotate_mut_mod.py +++ b/vermouth/tests/test_annotate_mut_mod.py @@ -334,6 +334,6 @@ def test_mod_resid_not_correct(caplog, node_data, edge_data, expected): annotate_modifications(mol, [], mutation) if expected: - assert 'GLY with resid 1 not found. No modification made.' in str(caplog.records[0].getMessage()) + assert '"GLY1" not found.' in str(caplog.records[0].getMessage()) else: assert caplog.records == []