##################################
#                                #
# Last modified 11/20/2013       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string

def run():

    if len(sys.argv) < 7:
        print 'usage: python %s input fieldID1 fieldID2 minMaxValue pseudoCount foldChangeThreshold outprefix' % sys.argv[0]
        print '\t the minMaxValue parameter refers to the minimum threshold for the maximum value of the leves in two samples, i.e if you want genes expressed at at least 5 FPKM in at least one condition, its value would be 5'
        sys.exit(1)
    
    input = sys.argv[1]
    ID1 = int(sys.argv[2])
    ID2 = int(sys.argv[3])
    MMV = float(sys.argv[4])
    PC = float(sys.argv[5])
    FCT = float(sys.argv[6])
    outfileUp = open(sys.argv[7] + '-up', 'w')
    outfileDown = open(sys.argv[7] + '-down', 'w')

    linelist = open(input)
    for line in linelist:
        fields=line.strip().split('\t')
        if line.startswith('#'):
            outfileUp.write(line)
            outfileDown.write(line)
            continue
        Value1 = float(fields[ID1])
        Value2 = float(fields[ID2])
        if MMV > max(Value1,Value2):
            continue
        if (Value2 + PC)/(Value1 + PC) >= FCT:
            outfileUp.write(line)
        if (Value1 + PC)/(Value2 + PC) >= FCT:
            outfileDown.write(line)

    outfileUp.close()
    outfileDown.close()
   
run()