##################################
#                                #
# Last modified 5/25/2009         # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import math

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s  GO-sig-file-of-interest reference-GO-stat-file outfilename threshold' % sys.argv[0]
        sys.exit(1)

    GO_sig = sys.argv[1]
    ref_GO_stat = sys.argv[2]
    outfilename = sys.argv[3]
    threshold = float(sys.argv[4])

    outfile = open(outfilename, 'w')

    listoflines = open(GO_sig)
    lineslist = listoflines.readlines()
    GOsigDict = {}
    for line in lineslist:
        fields = line.split('\n')[0].split('\t')
        GOsigDict[fields[0]]={}
        GOsigDict[fields[0]]['numbers']=fields[1]
        GOsigDict[fields[0]]['e-value']=float(fields[3])
        GOsigDict[fields[0]]['enrichmentStatus']=fields[4]
        GOsigDict[fields[0]]['description']=fields[5]

    listoflines = open(ref_GO_stat)
    lineslist = listoflines.readlines()
    refGOstatDict = {}
    for line in lineslist:
        fields = line.split('\n')[0].split('\t')
        refGOstatDict[fields[0]]={}
        refGOstatDict[fields[0]]['numbers']=fields[1]
        refGOstatDict[fields[0]]['e-value']=float(fields[4])
        refGOstatDict[fields[0]]['description']=fields[5]

    for GOcat in GOsigDict.keys():
        GO_enrichment_enrichment=GOsigDict[GOcat]['e-value']/refGOstatDict[GOcat]['e-value']
        GO_enrichment_enrichment=1/GO_enrichment_enrichment;
        print GO_enrichment_enrichment
        if GO_enrichment_enrichment>=threshold:
            GO_enrichment_enrichment=math.log(GO_enrichment_enrichment,10)
            line = GOcat+'\t'+GOsigDict[GOcat]['numbers']+'\t'+str(GOsigDict[GOcat]['e-value'])+'\t'+refGOstatDict[GOcat]['numbers']+'\t'+str(refGOstatDict[GOcat]['e-value'])+'\t'+GOsigDict[GOcat]['description']+'\t'+ str(GO_enrichment_enrichment)+'\n'
            outfile.write(line)
    
    outfile.close()

run()
