to_d2_graph.py 2.27 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

7
from deconvolution.d2graph import d2_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('--output_prefix', '-o', default="",  help="Output file prefix.")
14
    parser.add_argument('--threads',       '-t', default=8, type=int, help='Number of thread to use for dgraph 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 25

    if args.output_prefix == "":
        args.output_prefix = ".".join(args.barcode_graph.split(".")[:-1]) + "_lcpg"

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 46
    # Debug config
    debug = False
    debug_path = "/dev/null"
    if args.debug:
        debug = True
        debug_path = f"{args.output_prefix}_debug"
47
        import os, shutil
48
        if os.path.isdir(debug_path):
49
            shutil.rmtree(debug_path)
50 51
        os.mkdir(debug_path)

52 53 54 55 56 57 58 59 60
    d2g = d2.D2Graph(debug=debug, debug_path=debug_path)
    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, f"{args.output_prefix}.gexf")
Yoann Dufresne's avatar
Yoann Dufresne committed
62 63 64 65


if __name__ == "__main__":
    main()