From ef2547c17f82edf03fda2a0fa924d56c9c2de123 Mon Sep 17 00:00:00 2001 From: Klaus Weinbauer <51136324+klausweinbauer@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:43:41 +0200 Subject: [PATCH] v0.0.8 (#3) * Update README.md * Update test-and-lint.yml * prepare release v0.0.7 * fix issue in implicit hydrogen computation * allow non-carbon atoms to belong to no functional group * fix linting * prepare release v0.0.8 --------- Co-authored-by: Klaus Weinbauer --- fgutils/query.py | 9 --------- fgutils/utils.py | 2 +- pyproject.toml | 2 +- test/test_utils.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/fgutils/query.py b/fgutils/query.py index b31502c..ae813e4 100644 --- a/fgutils/query.py +++ b/fgutils/query.py @@ -99,15 +99,6 @@ def __get_functional_groups(self, graph: nx.Graph) -> list[tuple[str, list[int]] elif i in unidentified_ids: unidentified_ids.remove(i) groups.append((node.fgconfig.name, indices)) - if len(unidentified_ids) > 0: - raise RuntimeError( - "Could not find a functional group for atom(s) {}.".format( - [ - "{}@{}".format(graph.nodes[i]["symbol"], i) - for i in unidentified_ids - ] - ) - ) return groups def get(self, value) -> list[tuple[str, list[int]]]: diff --git a/fgutils/utils.py b/fgutils/utils.py index f5e94a8..b543ad4 100644 --- a/fgutils/utils.py +++ b/fgutils/utils.py @@ -42,8 +42,8 @@ def add_implicit_hydrogens(graph: nx.Graph) -> nx.Graph: n_sym in valence_table.keys() ), "Element {} not found in valence table.".format(n_sym) bond_cnt = sum([b for _, _, b in graph.edges(n_id, data="bond")]) # type: ignore + # h_cnt can be negative; aromaticity is complicated, we just ignore that h_cnt = int(8 - valence_table[n_sym] - bond_cnt) - assert h_cnt >= 0, "Negative hydrogen count." for h_id in range(len(graph), len(graph) + h_cnt): graph.add_node(h_id, symbol="H") graph.add_edge(n_id, h_id, bond=1) diff --git a/pyproject.toml b/pyproject.toml index 532d594..ae11187 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "fgutils" -version = "0.0.7" +version = "0.0.8" authors = [{name="Klaus Weinbauer", email="klaus@bioinf.uni-leipzig.de"}] description = "Library to get functional groups from molecular graphs." readme = "README.md" diff --git a/test/test_utils.py b/test/test_utils.py index 5d6b6f1..69dc986 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -36,3 +36,38 @@ def test_add_implicit_hydrogens_3(): assert 6 == len(graph) _assert_Hs(graph, 1, 3) _assert_Hs(graph, 4, 1) + + +def test_sulfur_ring(): + graph = parse("C:1N:C:S:C:1") + graph = add_implicit_hydrogens(graph) + assert 8 == len(graph) + _assert_Hs(graph, 0, 1) + _assert_Hs(graph, 1, 0) + _assert_Hs(graph, 2, 1) + _assert_Hs(graph, 3, 0) + _assert_Hs(graph, 4, 1) + + +def test_nitrogen_5ring(): + graph = parse("C:1C:N(H):C:C:1") + graph = add_implicit_hydrogens(graph) + assert 10 == len(graph) + _assert_Hs(graph, 0, 1) + _assert_Hs(graph, 1, 1) + _assert_Hs(graph, 2, 1) + _assert_Hs(graph, 3, 0) + _assert_Hs(graph, 4, 1) + _assert_Hs(graph, 5, 1) + + +def test_nitrogen_6ring(): + graph = parse("C:1C:C:N:C:C:1") + graph = add_implicit_hydrogens(graph) + assert 11 == len(graph) + _assert_Hs(graph, 0, 1) + _assert_Hs(graph, 1, 1) + _assert_Hs(graph, 2, 1) + _assert_Hs(graph, 3, 0) + _assert_Hs(graph, 4, 1) + _assert_Hs(graph, 5, 1)