d2_to_barcode_path.py 1.69 KB
Newer Older
Yoann Dufresne's avatar
Yoann Dufresne committed
1
2
3
4
5
6
7
8
#!/usr/bin/env python3

import networkx as nx
import argparse
import sys
import random

from deconvolution.d2graph import d2_graph as d2
9
from barcodes.partialorder import greedy_partial_order, bb_partial_order
Yoann Dufresne's avatar
Yoann Dufresne committed
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
37
38
39
40
41
42
43
44
45
46


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

    all_nodes = list(largest_component.nodes())
    rnd_node = all_nodes[random.randint(0, len(all_nodes)-1)]
47
48
49
    # po = greedy_partial_order(largest_component, rnd_node)
    for po in bb_partial_order(largest_component, rnd_node):
        print("barcodes", len(po), "sets", po.len_sets, "udgs", po.len_udgs, "score", po.score)
Yoann Dufresne's avatar
Yoann Dufresne committed
50
51
52
53


if __name__ == "__main__":
    main()