from commands import getoutput
from os import listdir

m = 0
#m = 78
#m = 78 + 25
some = [143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 98, 99, 100, 101, 102,73, 74, 75, 76, 77]

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()

files = [a for a in listdir(".") if ".csv" in a]
out = open("tables.txt", "w")

for n, f in enumerate(files):
	for g in files[n+1:]:
		file1 = open(f).read().splitlines()[1:]
		file2 = open(g).read().splitlines()[1:]
		name1 = f.split(".")[0]
		name2 = g.split(".")[0]
		print name1, name2
		out1 = open("%s-%s.txt" % (name1, name2), "w")
		out2 = open("%s-%s.txt" % (name2, name1), "w")

		for a,b in zip(file1, file2):
			n = int(a.split(";")[0])
			if n < m: continue
			if n not in some: continue
			if n <> int(b.split(";")[0]): raise ValueError
			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()
		
		# Run the R script
		out.write(getoutput("Rscript kappa1.R %s %s" % (out1.name, out2.name)))
		out.write("\n\n")
out.close()
