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

Replace hard-coded attribute keys #110

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
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import networkx as nx
from pathlib import Path
from networkx.algorithms.components import is_connected

from tucan.graph_attributes import ATOMIC_NUMBER, ELEMENT_SYMBOL
from tucan.io import graph_from_file


Expand All @@ -25,8 +27,8 @@ def graph_from_dimacs(filepath):

graph = nx.Graph()
graph.add_nodes_from(node_labels)
nx.set_node_attributes(graph, "C", "element_symbol")
nx.set_node_attributes(graph, 6, "atomic_number")
nx.set_node_attributes(graph, "C", ELEMENT_SYMBOL)
nx.set_node_attributes(graph, 6, ATOMIC_NUMBER)
graph.add_edges_from(bonds)

return graph
Expand Down
82 changes: 76 additions & 6 deletions tests/io/test_molfile_v2000_reader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import pytest
from pathlib import Path

from tucan.graph_attributes import (
ATOMIC_NUMBER,
CHG,
ELEMENT_SYMBOL,
MASS,
PARTITION,
RAD,
X_COORD,
Y_COORD,
Z_COORD,
)
from tucan.io import graph_from_file
from tucan.io.exception import MolfileParserException
from tucan.io.molfile_v2000_reader import (
_merge_tuples_into_additional_attributes,
_parse_atom_line,
_parse_atom_value_assignments,
_to_int,
_to_float,
Expand All @@ -27,6 +40,63 @@ def test_graphs_from_v2000_and_v3000_molfiles_match(mol):
assert e1 == e2


@pytest.mark.parametrize(
"line, expected_additional_attr",
[
(
" 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0",
{},
),
(
" 0.0000 0.0000 0.0000 C 0 1 0 0 0 0 0 0 0 0 0 0",
{CHG: 3},
),
(
" 0.0000 0.0000 0.0000 C 0 2 0 0 0 0 0 0 0 0 0 0",
{CHG: 2},
),
(
" 0.0000 0.0000 0.0000 C 0 3 0 0 0 0 0 0 0 0 0 0",
{CHG: 1},
),
(
" 0.0000 0.0000 0.0000 C 0 4 0 0 0 0 0 0 0 0 0 0",
{RAD: 2},
),
(
" 0.0000 0.0000 0.0000 C 0 5 0 0 0 0 0 0 0 0 0 0",
{CHG: -1},
),
(
" 0.0000 0.0000 0.0000 C 0 6 0 0 0 0 0 0 0 0 0 0",
{CHG: -2},
),
(
" 0.0000 0.0000 0.0000 C 0 7 0 0 0 0 0 0 0 0 0 0",
{CHG: -3},
),
(
" 0.0000 0.0000 0.0000 C 0 8 0 0 0 0 0 0 0 0 0 0",
{}, # ignored
),
],
)
def test_parse_atom_line_charge_field(line, expected_additional_attr):
expected_attrs = {
ELEMENT_SYMBOL: "C",
ATOMIC_NUMBER: 6,
PARTITION: 0,
X_COORD: 0,
Y_COORD: 0,
Z_COORD: 0,
}
expected_attrs.update(expected_additional_attr)

atom_attrs = _parse_atom_line(line)

assert atom_attrs == expected_attrs


@pytest.mark.parametrize(
"tuples, additional_attrs, expected_additional_attrs_after_merge",
[
Expand All @@ -37,21 +107,21 @@ def test_graphs_from_v2000_and_v3000_molfiles_match(mol):
(2, 3), # atom index is not in additional_attrs yet
],
{
0: {"chg": 2}, # will add new key
1: {"mass": 1}, # will overwrite value
0: {CHG: 2}, # will add new key
1: {MASS: 1}, # will overwrite value
},
{
0: {"chg": 2, "mass": 2},
1: {"mass": 13},
2: {"mass": 3},
0: {CHG: 2, MASS: 2},
1: {MASS: 13},
2: {MASS: 3},
},
),
],
)
def test_merge_tuples_into_additional_attributes(
tuples, additional_attrs, expected_additional_attrs_after_merge
):
_merge_tuples_into_additional_attributes(tuples, "mass", additional_attrs)
_merge_tuples_into_additional_attributes(tuples, MASS, additional_attrs)
assert additional_attrs == expected_additional_attrs_after_merge


Expand Down
Loading