#!/usr/bin/env python
"""
Usage: graph_merge.py [OPTION] INPUTDIR OUTPUTFILE

Option:
	INPUTDIR is the directory containing all the graph files in gspan format.
	OUTPUTFILE is the file to store the merged graphs also in gspan format.
	-s ..., --support=...	minimum support for the edge to be kept. 5(default)
	-h, --help              show this help
	
Examples:
	graph_merge.py -s 6 gph_result/sc/ gph_result/sc_mcl/mcl_gph_dataset1

Description:
	This program merges all the graphs in gspan format, generated by
	graph_reorganize.py. The ouput is also in gspan format.
	After this, either run reverse+kMax or gspan2mcl_input.py+mcl to
	get the dense clusters.

"""


import sys, os, re, getopt

class graph_merge:
	'''
	'''
	def __init__(self, support, dir, ofname):
		self.support = int(support)
		self.dir = dir
		self.of = open(ofname, 'w')
		#data structure to store the merged graph. key is the edge.
		#value is the recurrence
		self.graph_dict = {}
		#output block put before edges
		self.first_block = ''
	
	def dstruc_loadin(self):
		files = os.listdir(self.dir)
		sys.stderr.write("\tTotally, %d files to be processed.\n"%len(files))
		for f in files:
			pathname = os.path.join(self.dir, f)
			sys.stderr.write("%d/%d:\t%s\n"%(files.index(f)+1,len(files),f))
			file_no = files.index(f)+1
			inf = open(pathname, 'r')
			for line in inf:
				if line[0] == 'e':
					#edge here, like 'e 3807 3859 0.804645'
					line_list = line[:-1].split()
					vertex1 = int(line_list[1])
					vertex2 = int(line_list[2])
					if vertex1 <= vertex2:
						edge = (vertex1, vertex2)
					else:
						edge = (vertex2, vertex1)
					if edge in self.graph_dict:
						self.graph_dict[edge] += 1
					else:
						self.graph_dict[edge] = 1
				elif file_no == 1:
					self.first_block += line
			inf.close()

	def output(self):
		#output the preceding block first
		self.of.write(self.first_block)
		for (edge, recurrence) in self.graph_dict.iteritems():
			if recurrence >= self.support:
				self.of.write("e %d %d %d\n"%(edge[0], edge[1], recurrence))
	
	def run(self):
		self.dstruc_loadin()
		self.output()
	
if __name__ == '__main__':
	if len(sys.argv) == 1:
		print __doc__
		sys.exit(2)
		
	try:
		opts, args = getopt.getopt(sys.argv[1:], "s:h", ["support=", "help"])
	except:
		print __doc__
		sys.exit(2)
	
	support = 5
	for opt, arg in opts:
		if opt in ("-s", "--support"):
			support = int(arg)
		elif opt in ("-h", "--help"):
			print __doc__
			sys.exit(2)

			
	if len(args) == 2:
		instance = graph_merge(support, args[0], args[1])
		instance.run()
	else:
		print __doc__
		sys.exit(2)
