Select Git revision
      
  d2_to_path.py
  d2_to_path.py  2.17 KiB 
#!/usr/bin/env python3
import networkx as nx
import argparse
import sys
from deconvolution.d2graph import d2_graph as d2, path_optimization as po
def parse_arguments():
    parser = argparse.ArgumentParser(description='Greedy construction of a path through the d2 graph.')
    parser.add_argument('barcode_graph', help='The barcode graph file. Must be a gefx formatted file.')
    parser.add_argument('d2_graph', help='d2 graph to reduce. Must be a gexf formatted file.')
    parser.add_argument('--out_prefix', '-o', default="", help="Output file prefix.")
    parser.add_argument('--verbose', '-v', action="store_true", help="Verbose")
    args = parser.parse_args()
    if args.out_prefix == "":
        args.out_prefix = '.'.join(args.d2_graph.split('.')[:-1])
    return args
def main():
    # Parsing the arguments and validate them
    args = parse_arguments()
    barcode_file = args.barcode_graph
    d2_file = args.d2_graph
    if (not barcode_file.endswith('.gexf')) or (not d2_file.endswith(".gexf")):
        print("Inputs file must be gexf formatted", file=sys.stderr)
        exit(1)
    
    # Loading
    G = nx.read_gexf(barcode_file)
    d2g = d2.D2Graph(G)
    d2g.load(d2_file)
    # Take the principal component
    largest_component_nodes = max(nx.connected_components(d2g), key=len)
    largest_component = d2g.subgraph(largest_component_nodes)
    # Start optimization
    optimizer = po.Optimizer(largest_component)
    nodes = optimizer.bb_solution(verbose=args.verbose)
    solution = po.Solution(largest_component)
    solution.add_path([largest_component.node_by_idx[int(n)] for n in nodes])
    # optimizer.init_random_solutions(1)
    # solution = optimizer.solutions[0]
    # print("Solution creation...")
    # optimizer.extends_until_end(solution)
    print(f"covering score: {solution.covering_score()}")
    #
    # solution.save_path_in_graph(f"{args.out_prefix}_d2_path.gexf")
    solution.save_path(f"{args.out_prefix}_path.gexf")
    # solution.save_barcode_path(f"{args.out_prefix}_barcode_count.gexf")
    print("Solution saved")
    # from d2_path import d2_path_to_barcode_path
    # d2_path_to_barcode_path(solution)
if __name__ == "__main__":
    main()