to_d2_graph.py 2.32 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 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 14
    parser.add_argument('--output_prefix', '-o', default="d2_graph",  help="Output file prefix.")
    parser.add_argument('--debug',         '-d', action='store_true', help="Debug")
15
    parser.add_argument('--maxclq',        '-c', action='store_true', help="Enable max clique community detection (default behaviour)")
16 17
    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
18 19 20 21 22 23 24

    args = parser.parse_args()
    return args

def main():
    # Parsing the input file
    args = parse_arguments()
25 26
    
    debug = args.debug
Yoann Dufresne's avatar
Yoann Dufresne committed
27
    filename = args.barcode_graph
28 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')
        if debug: print(t,"[debug]",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

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


if __name__ == "__main__":
    main()