to_d2_graph.py 2.71 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 11 12


def parse_arguments():
    parser = argparse.ArgumentParser(description='Transform a 10X barcode graph into a d2 graph. The program dig for the d-graphs and then merge them into a d2-graph.')
    parser.add_argument('barcode_graph', help='The barcode graph file. Must be a gefx formated file.')
13
    parser.add_argument('--output_prefix', '-o', default="d2_graph",  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")
16
    parser.add_argument('--maxclq',        '-c', action='store_true', help="Enable max clique community detection (default behaviour)")
17 18
    parser.add_argument('--louvain',       '-l', action='store_true', help="Enable Louvain community detection instead of all max-cliques")
    parser.add_argument('--comtest',       '-k', action='store_true', help="Enable [placeholder] community detection algorithm instead of max-cliques")
Yoann Dufresne's avatar
Yoann Dufresne committed
19 20 21 22 23 24 25

    args = parser.parse_args()
    return args

def main():
    # Parsing the input file
    args = parse_arguments()
26
    
27
    # debug = args.debug
Yoann Dufresne's avatar
Yoann Dufresne committed
28
    filename = args.barcode_graph
29 30 31 32 33 34 35 36 37 38 39 40
   
    def dprint(s):
        from datetime import datetime
        t = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    dprint("loading barcode graph")
    if filename.endswith('.gexf'):
        G = nx.read_gexf(filename)
    elif filename.endswith('.graphml'):
        G = nx.read_graphml(filename)
    else:
        print("Input file must be gexf or graphml formatted", file=sys.stderr)
41
        exit(1)
42
    dprint("barcode graph loaded")
43

44 45 46 47 48 49 50
    if args.louvain:
        clique_mode = "louvain"
    elif args.comtest:
        clique_mode = "testing"
    else:
        clique_mode = None

51 52 53 54 55 56
    # Debug config
    debug = False
    debug_path = "/dev/null"
    if args.debug:
        debug = True
        debug_path = f"{args.output_prefix}_debug"
57
        import os, shutil
58
        if os.path.isdir(debug_path):
59
            shutil.rmtree(debug_path)
60 61
        os.mkdir(debug_path)

Yoann Dufresne's avatar
Yoann Dufresne committed
62
    # Index size must be changed for general purpose. 8 is good for d=5
63
    dprint("creating D2graph object")
64
    d2g = d2.D2Graph(G, debug=debug, debug_path=debug_path)
65 66
    dprint("D2 graph object created")
    dprint("constructing d2 graph from barcode graph")
Yoann Dufresne's avatar
Yoann Dufresne committed
67
    d2g.construct_from_barcodes(neighbor_threshold=d2_threshold, clique_mode=clique_mode, threads=args.threads)
68
    dprint("[debug] d2 graph constructed")
Yoann Dufresne's avatar
Yoann Dufresne committed
69
    
70
    # d2g.save(f"{args.output_prefix}.tsv")
71
    nx.write_gexf(d2g, f"{args.output_prefix}.gexf")
Yoann Dufresne's avatar
Yoann Dufresne committed
72 73 74 75


if __name__ == "__main__":
    main()