#!/usr/bin/env python3 import networkx as nx import argparse import sys from deconvolution.d2graph import d2_graph as d2 from deconvolution.d2graph import d2_algorithms as d2a def parse_arguments(): parser = argparse.ArgumentParser(description='Reduce the number of edges of a d2 graph. The reduction is performed on edges that are redundant.') parser.add_argument('lcp_graph', help='lcp graph to reduce. Must be a gefx formated file.') parser.add_argument('--output_lcpg_name', '-o', default="", help="Output file prefix.") parser.add_argument('--component_min_size', '-c', type=int, default=10, help="Minimum size of a component to keep it in the lcp graph, after the edge simplifications.") args = parser.parse_args() if args.output_lcpg_name == "": args.output_lcpg_name = args.lcp_graph[:args.lcp_graph.rfind('.')] + "_reduced.gexf" return args def main(): # Parsing the arguments and validate them args = parse_arguments() lcpg_name = args.lcp_graph if not lcpg_name.endswith(".gexf"): print("Inputs file must be gexf formatted", file=sys.stderr) exit(1) # Loading print("--- lcp graph loading ---") lcpg = d2.D2Graph() lcpg.load(lcpg_name) # Algorithms for reduction print("--- 3-reduction processing ---") d2a.transitive_reduction(lcpg) # Take the principal component print("--- Extract and write the graph ---") components = [c for c in nx.connected_components(lcpg) if len(c) > args.component_min_size] nodes = set() for comp in components: nodes.update(comp) subgraph = lcpg.subgraph(nodes) # Write the simplified graph nx.write_gexf(subgraph, args.output_lcpg_name) if __name__ == "__main__": main()