'''
Find linear & ranked correlation
'''

import numpy
import scipy.stats

fdIn = open('multipleAttributes.txt', 'r')
fdOut = open('correlation.txt', 'w')

data = []
line = fdIn.readline()
tmp = line.split('\t')
tmpStr = ''
numAtt = len(tmp)
#numAtt = len(tmp) - 2
for i in range(numAtt):
    data.append([])
    if i != numAtt - 1: tmpStr += tmp[i] + '\t' 
    #if i != numAtt - 1: tmpStr += tmp[i + 2] + '\t' 
    else: tmpStr += tmp[i]
    #else: tmpStr += tmp[i + 2]
fdOut.write(tmpStr)

lineNum = 0
for line in fdIn:
    lineNum += 1
    tmp = line.split('\t')
    for i in range(len(tmp)):
        #if i <= 1: continue
        value = float(tmp[i])
        if value > 1000000000000000000: print lineNum
        data[i].append(float(value))
        #data[i - 2].append(float(tmp[i]))
fdIn.close()


#Linear correlation
p = numpy.corrcoef(data)
for i in p:
    tmpStr = ''
    for j in i: 
        tmpStr += str(round(j, 4)) + '\t'
    fdOut.write(tmpStr + '\n')

fdOut.write('\n\n')
#ranked correlation
for i in range(numAtt): 
    tmpStr = ''
    for j in range(numAtt):
        rho, pval = scipy.stats.spearmanr(data[i], data[j])
        tmpStr += str(round(rho, 4)) + '\t'
    fdOut.write(tmpStr + '\n')

fdOut.close()
