from sys import argv
from commands import getoutput

if len(argv) < 3: print "please supply two filenames"
file1 = open(argv[1]).read().splitlines()[1:]
file2 = open(argv[2]).read().splitlines()[1:]
name1 = argv[1].split(".")[0]
name2 = argv[2].split(".")[0]
out1 = open("%s-%s.txt" % (name1, name2), "w")
out2 = open("%s-%s.txt" % (name2, name1), "w")

def overlap(a, b):
	A = set(a.split(","))
	B = set(b.split(","))
	if len(A) == 1: return a
	if A.intersection(B):
		return A.intersection(B).pop()
	return A.pop()

for a,b in zip(file1, file2):
	a = a.split(";")[1].strip()
	b = b.split(";")[1].strip()
	out1.write(overlap(a, b)+'\n')
	out2.write(overlap(b, a)+'\n')

out1.close()
out2.close()

print getoutput("Rscript kappa1.R %s %s" % (out1.name, out2.name))

