Visualising the khloraascaf multiplied doubled contig graph with GraphML#

khloraascaf scaffolding uses a directed graph defined as a “Multiplied Doubled Contig Graph”.

If you want to visualise it e.g. thanks to Gephi or Cytoscape graph visualisers, you can use the khloraascaf_utils.to_networkx.mdcg_to_nxdigraph() and the networkx.write_graphml_lxml() functions:

from pathlib import Path

from khloraascaf import IOConfig, MetadataAllDebugs
from khloraascaf.multiplied_doubled_contig_graph import (
    mdcg_with_id_from_input_files,
)
from networkx import write_graphml_lxml

from khloraascaf_utils.to_networkx import (
    add_result_in_nxdigraph_from_debug,
    mdcg_to_nxdigraph,
)


data_dir = Path('tests/data/ir_dr_sc')
res_dir = data_dir / '2023-05-25_16:00:00_khloraascaf'

io_cfg = IOConfig.from_run_directory(res_dir)
#
# The debug option here was set to true,
# so we obtained supplementary files we can use to fill the graph
#
all_debugs_metadata = MetadataAllDebugs.from_run_directory(res_dir)
#
# Get the multiplied doubled contig graph (mdcg)
#   and the identifier container (id_container)
#
mdcg, id_container = mdcg_with_id_from_input_files(
    io_cfg.contig_attrs(),
    io_cfg.contig_links(),
)
#
# Prepare the GraphML file's directory
#
outdir = Path('tmp')
outdir.mkdir(exist_ok=True)
#
# Get a NetworkX DiGraph from mdcg
#
nx_mdcg = mdcg_to_nxdigraph(mdcg, id_container)
#
# Add all the circuit solutions to the graph,
# even the one that were not finally kept
#
for debug_meta in all_debugs_metadata:
    add_result_in_nxdigraph_from_debug(nx_mdcg, debug_meta)
#
# Output a GraphML file from DiGraph
#
graphml_path = outdir / 'mdcg.graphml'
write_graphml_lxml(nx_mdcg, graphml_path)
#
# Verify the outputs
#
files = set(outdir.glob('*'))
assert len(files) == 1
assert graphml_path in files