d2_to_path_simple.py 2.34 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python3

import networkx as nx
import argparse
import sys

Yoann Dufresne's avatar
Yoann Dufresne committed
7
8
from deconvolution.lcpgraph import lcp_graph as d2, path_optimization as po
from deconvolution.lcpgraph.lcp_path import Path
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


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.")

    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)
Yoann Dufresne's avatar
Yoann Dufresne committed
37
    d2g = d2.LcpGraph(G)
38
39
40
41
42
43
44
45
46
47
48
    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)

    # pick the first node of this component, find the farthest node to it
    spl = nx.shortest_path_length(largest_component,list(largest_component.nodes())[0])
    initial_node = max(spl.items(),key=lambda x:x[1])[0]
    # now find the farthest node to it again
    spl1 = nx.shortest_path_length(largest_component,initial_node)
Rayan  CHIKHI's avatar
fix    
Rayan CHIKHI committed
49
    one_extremity = max(spl1.items(),key=lambda x:x[1])[0]
50
51
    # and find the farthest node to it again
    spl2 = nx.shortest_path_length(largest_component,one_extremity)
Rayan  CHIKHI's avatar
fix    
Rayan CHIKHI committed
52
    other_extremity = max(spl2.items(),key=lambda x:x[1])[0]
53

Yoann Dufresne's avatar
Yoann Dufresne committed
54
55
56
57
    node_path = nx.shortest_path(largest_component,one_extremity, other_extremity)
    nodes = [largest_component.node_by_idx[int(x)] for x in node_path]
    path = Path(d2g)
    path.add_path(nodes)
58

Yoann Dufresne's avatar
Yoann Dufresne committed
59
    print(f"covering score: {path.covering_score()}")
60
61

    # solution.save_path_in_graph(f"{args.out_prefix}_d2_path.gexf")
62
63
    lcpg = path.to_lcpg()
    nx.write_gexf(lcpg, f"{args.out_prefix}_path.gexf")
64
65
66
67
68
    print("Solution saved")


if __name__ == "__main__":
    main()