Skip to content

Commit

Permalink
TST: Ensure current parsers create MultiDiGraphs
Browse files Browse the repository at this point in the history
... even though the fastg / gfa ones don't actually allow for parallel
edges in parsing (marbl#239). We just wanna *create* a multidigraph so that
in the later stages of pattern identification, etc. (marbl#202), we can
assume safely that the object we are working with is a multidigraph.
  • Loading branch information
fedarko committed Mar 31, 2023
1 parent 5c2a583 commit 26fe499
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import networkx as nx
from .utils import run_tempfile_test


Expand All @@ -24,6 +25,7 @@ def get_test_fastg():

def test_good():
g = run_tempfile_test("fastg", get_test_fastg(), None, None)
assert type(g) is nx.MultiDiGraph
# CODELINK: remaining code in this function is taken from
# test_parse_small_assembly_graph() in the pyfastg codebase.
# (...I mean, I literally wrote that function, but might as well cite it?)
Expand Down
12 changes: 6 additions & 6 deletions metagenomescope/tests/assembly_graph_parser/test_parse_gfa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# from .utils import run_tempfile_test
import networkx as nx
from metagenomescope.input_node_utils import negate_node_id
from metagenomescope.assembly_graph_parser import parse_gfa
from metagenomescope.errors import GraphParsingError
Expand All @@ -8,7 +8,7 @@

def check_sample_gfa_digraph(digraph):
"""Checks that all of the collected data (nodes, edges, node gc content)
looks good for the NX DiGraph produced by running parse_gfa() on
looks good for the NX MultiDiGraph produced by running parse_gfa() on
sample1.gfa or sample2.gfa.
(sample1.gfa and sample2.gfa describe the same graph structure, albeit in
Expand All @@ -19,6 +19,7 @@ def check_sample_gfa_digraph(digraph):
the ways parse_gfa() cares about), and 2) it lets me be lazy and reuse all
of this test code ;)
"""
assert type(digraph) is nx.MultiDiGraph
assert len(digraph.nodes) == 12
assert len(digraph.edges) == 8

Expand Down Expand Up @@ -82,12 +83,11 @@ def test_parse_self_implied_edge():
We define a "self-implied edge" as an edge whose complement is itself. An
example of such an edge is 2+ -> 2- in loop.gfa; the complement of this
edge is -(2-) -> -(2+), which is equal to 2+ -> 2- (my notation looks kind
of sillyt but you get the idea).
of silly but you get the idea).
The expected behavior is that self-implied edges are only added to the
assembly graph visualization once, since duplicate edges in general don't
really make sense here. This corroborates the visualization of loop.gfa in
Shaun's repository containing it
assembly graph visualization once. This corroborates the visualization of
loop.gfa in Shaun's repository containing it
(https://github.com/sjackman/assembly-graph).
"""
digraph = parse_gfa("metagenomescope/tests/input/loop.gfa")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import networkx as nx
from .utils import run_tempfile_test
from metagenomescope.errors import GraphParsingError
from metagenomescope.assembly_graph_parser import parse_lastgraph
Expand All @@ -10,7 +11,9 @@ def test_parse_lastgraph_good():
digraph = parse_lastgraph(
"metagenomescope/tests/input/cycletest_LastGraph"
)
# Verify that a NetworkX DiGraph was computed based on this file accurately
# Verify that a NetworkX MultiDiGraph was computed based on this file
# accurately.
assert type(digraph) is nx.MultiDiGraph
# We expect 4 nodes and 4 edges due to the graph being interpreted as
# unoriented (i.e. each node's forward or reverse orientation can be used)
assert len(digraph.nodes) == 4
Expand Down Expand Up @@ -44,10 +47,11 @@ def test_parse_lastgraph_good():
assert digraph.nodes["-2"]["gc_content"] == (1 / 6)

# Check that edges were properly stored in the digraph
for edge_id in (("1", "2"), ("-2", "-1")):
# (The 0s correspond to keys, since this is a multigraph)
for edge_id in (("1", "2", 0), ("-2", "-1", 0)):
assert edge_id in digraph.edges
assert digraph.edges[edge_id]["multiplicity"] == 5
for edge_id in (("2", "1"), ("-1", "-2")):
for edge_id in (("2", "1", 0), ("-1", "-2", 0)):
assert edge_id in digraph.edges
assert digraph.edges[edge_id]["multiplicity"] == 9

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import networkx as nx
from networkx import NetworkXError
from .utils import run_tempfile_test
from metagenomescope.errors import GraphParsingError
Expand All @@ -20,6 +21,7 @@ def test_parse_metacarvel_gml_good():
digraph = parse_metacarvel_gml(
"metagenomescope/tests/input/marygold_fig2a.gml"
)
assert type(digraph) is nx.MultiDiGraph
# Make sure that the appropriate amounts of nodes and edges are read
assert len(digraph.nodes) == 12
assert len(digraph.edges) == 16
Expand Down

0 comments on commit 26fe499

Please sign in to comment.