#!/usr/bin/python 

# this script will generate the vector for running the classification on 
# the number of components in the vector will be the number of elements in the data 
# it will also replace the labels with pure integers by utilizing the sorted list 
# of labels from labels_unique_sorted.txt to map them to integers 
# input : without_permutations_unique.txt (after running "sort -u" on without_permutations.txt 
# input : elements.txt (generated by running list_elements.py on groups.txt)
# input : labels_unique_sorted.txt (the list of all labels in sorted order)
# output : vector.txt
# the file vector.txt contains on each line, the label and the vector for that compound.
# the vector for each component element conatins the composition if it is present or else a 0

import sys
import string

if len(sys.argv) < 2:
	print "Specify the file with permutations and duplicated removed as input\n(without_permutations_unique.txt)\n"
	sys.exit(1)
element_file = file("elements.txt")
labels_file = file("label_count.txt")
inf = file(sys.argv[1])
outf = file("vector.txt", "w")
outf_binary = file("vector_binary.txt", "w")
#initializing the list containing the labels so that we can rewrite them as integers
labels=[]
for line in labels_file:
	#getting the label and the count
	l,c = line.split(" ")
	labels.append(l)
labels.sort()
#check for the contents of the labels list 
print len(labels)
print labels
#initialize the list with elements to make it easier for creating the vectors
elements = []
for line in element_file:
	#list_elements.py adds a \n at the end of each line, we need to strip it
	line = line.rstrip("\n")
	elements.append(line)

# check if the list is fine 
print len(elements)
print elements
element_file.close()
# note: the vectors will contain strings and not integer values of the compositions
num_elements = len(elements)
for line in inf:
	c1, c2, c3, e1, e2, e3, label, weight = line.split(" ")
	label=label[1:-1]
	#we have got some buggy inputs because of some errors in get_not_null.py
	#ignore the input tuple when the label is "()"
	if label == "" :
		continue
	elif labels.count(label) == 0:
		print label
		continue
	else:
		#assigning the compositions in the vector
		vector = [ "0" for i in range(num_elements)]
		vector[elements.index(e1)] = c1
		print "vector["+str(elements.index(e1))+","+e1+"] = " + c1
		vector[elements.index(e2)] = c2
		print "vector["+str(elements.index(e2))+","+e2+"] = " + c2
		vector[elements.index(e3)] = c3
		print "vector["+str(elements.index(e3))+","+e3+"] = " + c3
		outf.write(str(labels.index(label)+1))
		outf_binary.write(str(labels.index(label)+1))
		for i in vector:
			if i!="0":
				outf_binary.write(" 1")
			else:
				outf_binary.write(" 0")
			outf.write(" "+str(i))
		outf.write("\n")
		outf_binary.write("\n")

inf.close()
outf.close()
outf_binary.close()
	

	




