to_d2_graph.py 2.48 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 15
    parser.add_argument('--threads',       '-t', default=1, type=int, help='Number of thread to use for dgraph computation')
    # 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
   
    def dprint(s):
        from datetime import datetime
        t = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
33
        # if debug: print(t,"[debug]",s)
34 35 36 37 38 39 40 41

    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)
42
        exit(1)
43
    dprint("barcode graph loaded")
44

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

Yoann Dufresne's avatar
Yoann Dufresne committed
52
    # Index size must be changed for general purpose. 8 is good for d=5
53
    dprint("creating D2graph object")
54
    d2g = d2.D2Graph(G)
55 56
    dprint("D2 graph object created")
    dprint("constructing d2 graph from barcode graph")
57 58
    index_size = 4 #if clique_mode is None else 3
    d2g.construct_from_barcodes(index_size=index_size, clique_mode=clique_mode, threads=args.threads)
59
    dprint("[debug] d2 graph constructed")
Yoann Dufresne's avatar
Yoann Dufresne committed
60
    
61
    # d2g.save(f"{args.output_prefix}.tsv")
62
    nx.write_gexf(d2g, f"{args.output_prefix}.gexf")
Yoann Dufresne's avatar
Yoann Dufresne committed
63 64 65 66


if __name__ == "__main__":
    main()