#! /usr/bin/env python 
###################################
#    Davi Ortega 7/30/2012 
###################################
import sys
if '-h' in sys.argv:
	print 'Transform R data.frame style data to excell spread-sheet. (Use with cb-cb.py results)'
	sys.exit()


datafile = open(sys.argv[1],'r')

data_dic = {}
index_list = []

for line in datafile:
	fields = line.replace('\n','').split('\t')
	if fields[0] not in index_list:
		index_list.append(fields[0])
		data_dic[fields[0]] = []
	data_dic[fields[0]].append(fields[1:])

datafile.close()

max_data = len(data_dic[index_list[0]])

output = '\t'
for i in index_list:
	output += i + '\t'*len(data_dic[index_list[0]][0][1:])
output += '\n'
print data_dic[index_list[0]][0]
for i in range(max_data):
	output += str(i)
	for j in index_list:
		output += '\t' + '\t'.join(data_dic[j][i][1:])
	output += '\n'

outfile = open(sys.argv[1][:-3] + 'table.txt', 'w')
outfile.write(output)
outfile.close()
		
		

