barcode_to_lcp_graph.py 2.28 KB
Newer Older
Yoann Dufresne's avatar
Yoann Dufresne committed
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
from deconvolution.lcpgraph import lcp_graph as d2
Yoann Dufresne's avatar
Yoann Dufresne committed
8 9 10


def parse_arguments():
11
    parser = argparse.ArgumentParser(description='Transform a barcode graph into a lcp graph. The program dig for a set of lcps and then merge them into a lcp graph.')
Yoann Dufresne's avatar
Yoann Dufresne committed
12
    parser.add_argument('barcode_graph', help='The barcode graph file. Must be a gefx formated file.')
13
    parser.add_argument('--outfile', '-o', default="",  help="Output file name for lcp graph.")
Yoann Dufresne's avatar
Yoann Dufresne committed
14
    parser.add_argument('--threads',       '-t', default=8, type=int, help='Number of thread to use for lcp computation')
15
    parser.add_argument('--debug',         '-d', action='store_true', help="Debug")
Yoann Dufresne's avatar
Yoann Dufresne committed
16
    parser.add_argument('--verbose',         '-v', action='store_true', help="Verbose")
Yoann Dufresne's avatar
Yoann Dufresne committed
17
    parser.add_argument('--edge_divergence_threshold', '-dt', default=0.25, type=float, help='Divergence threshold value to link two udgs in the d2-graph')
18
    parser.add_argument('--maxclq',        '-c', action='store_true', help="Enable max clique community detection (default behaviour)")
19
    parser.add_argument('--louvain',       '-l', action='store_true', help="Enable Louvain community detection instead of all max-cliques")
Yoann Dufresne's avatar
Yoann Dufresne committed
20 21

    args = parser.parse_args()
22

23 24
    if args.outfile == "":
        args.outfile = ".".join(args.barcode_graph.split(".")[:-1]) + "_lcpg.gexf"
25

Yoann Dufresne's avatar
Yoann Dufresne committed
26 27 28 29 30
    return args

def main():
    # Parsing the input file
    args = parse_arguments()
31
    
32
    # debug = args.debug
Yoann Dufresne's avatar
Yoann Dufresne committed
33
    filename = args.barcode_graph
34
    barcode_graph = nx.read_gexf(filename)
35

36 37 38 39 40
    if args.louvain:
        clique_mode = "louvain"
    else:
        clique_mode = None

41 42 43 44 45
    # Debug config
    debug = False
    debug_path = "/dev/null"
    if args.debug:
        debug = True
46
        debug_path = ".".join(args.barcode_graph.split(".")[:-1]) + "_debug"
47
        import os, shutil
48
        if os.path.isdir(debug_path):
49
            shutil.rmtree(debug_path)
50 51
        os.mkdir(debug_path)

Yoann Dufresne's avatar
Yoann Dufresne committed
52
    d2g = d2.LcpGraph(debug=debug, debug_path=debug_path)
53 54 55 56 57 58 59 60
    d2g.construct_from_barcodes(
        barcode_graph,
        neighbor_threshold=args.edge_divergence_threshold,
        clique_mode=clique_mode,
        threads=args.threads,
        verbose=args.verbose
    )

61
    nx.write_gexf(d2g, args.outfile)
Yoann Dufresne's avatar
Yoann Dufresne committed
62 63 64 65


if __name__ == "__main__":
    main()