#!/usr/bin/env python
#
# GPLv3 2012 by anonymous
#

from itertools import chain
from sys import argv, exit
from hashlib import md5

if len(argv) != 3:
    print "Usage: %s full.graphml input-file" % argv[0]
    exit(1)

print "Loading graph %s..." % argv[1]
import igraph
g = igraph.load(argv[1])

hfilename = argv[2]
max_hood = 100
mrns = set([ f.strip().upper() for f in open(hfilename).readlines() ])

hashes = set()

# select all MRNs
vsg = g.vs.select(label_in=mrns).subgraph()

# group if connected
vclusters = vsg.clusters()

for sg in vclusters.subgraphs():
    # cluster and subgraph creates new indices
    # translate them back to the indices from the full graph
    vs = g.vs.select(label_in=sg.vs['label'])
    horder = 1
    hsize = 0
    last_hsize = None
    # find best order value
    while True:
        hsize = g.neighborhood_size(vs, order=horder)
        if hsize[0] > max_hood or hsize[0] == 1 or hsize[0] == last_hsize:
            last_hsize = None
            break;
        else:
            horder = horder + 1
            last_hsize = hsize[0]

    hsg = g.subgraph(chain(*g.neighborhood(vs, order=horder)))

    hsg.es['betweenness'] = hsg.edge_betweenness(directed=hsg.is_directed())
    if hsg.vcount() > 5:
        idconcat = ''.join(sorted(hsg.vs['label']))
        digest = md5(idconcat).hexdigest()
        if digest in hashes:
            print "%s Already created this graph. Skipped" % digest
            continue
        else:
            hashes.add(digest)

        filename = '%s.graphml' % digest
        with open(filename, 'w') as fh:
            hsg.write_graphml(fh)
        fh.close()

        print filename, hsg.vcount(), hsg.ecount(), ' '.join(sg.vs['label'])
    else:
        print "No graph for MRN %s" % ' '.join(sg.vs['label'])

    del hsg

