##################################
#                                #
# Last modified 03/14/2013       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
from sets import Set

def run():

    if len(sys.argv) < 6:
        print 'usage: python %s ExonIntronExon Exon1_RPKM_fielID Intron_RPKM_fielID Exon2_RPKM_fielID minRPKM outputfilename' % sys.argv[0]
        print '\tThe script will append the following values to the end of each entry: intron RPKM vs the average RPKM of the flanking exons and intron RPKM vs the maximum RPKM of the flanking exons, in that order'
        sys.exit(1)

    input = sys.argv[1]
    Exon1FieldID = int(sys.argv[2])
    IntronFieldID = int(sys.argv[3])
    Exon2FieldID = int(sys.argv[4])
    minRPKM = float(sys.argv[5])
    outfilename = sys.argv[6]

    outfile = open(outfilename, 'w')

    linelist=open(input)
    for line in linelist:
        if line.startswith('#'):
            continue
        fields=line.strip().split('\t')
        Exon1RPKM = float(fields[Exon1FieldID])
        Exon2RPKM = float(fields[Exon2FieldID])
        IntronRPKM = float(fields[IntronFieldID])
        if max(Exon1RPKM,Exon2RPKM,IntronRPKM) < minRPKM:
            outline = line.strip() + '\tbelow_RPKM_cutoff\tbelow_RPKM_cutoff' 
        else:
            if Exon1RPKM + Exon2RPKM > 0:
                outline = line.strip() + '\t' + str(IntronRPKM/((Exon1RPKM + Exon2RPKM)/2)) + '\t' + str(IntronRPKM/max(Exon1RPKM,Exon2RPKM))
            else:
                outline = line.strip() + '\t' + 'Inf' + '\t' + 'Inf'
        outfile.write(outline + '\n')
	
    outfile.close()
   
run()
