##################################
#                                #
# Last modified 06/17/2013       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
from sets import Set

try:
	import psyco
	psyco.full()
except:
	pass

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s junctions.table threshold fields outfile' % sys.argv[0]
        print '\tassumed junctions.table format: chr\tleft\rright\tstrand\treads'
        print '\tfields should be comma-separated integers, 0-based'
        sys.exit(1)

    inputfilename = sys.argv[1]
    minReads = int(sys.argv[2])
    fields = sys.argv[3].split(',')
    fieldIDs = []
    for f in fields:
        fieldIDs.append(int(f))
    outputfilename = sys.argv[4]

    outfile=open(outputfilename, 'w')

    lineslist = open(inputfilename)
    for line in lineslist:
        if line.startswith('#'):
            outfile.write(line)
            continue
        fields = line.strip().split('\t')
        readCounts = []
        for ID in fieldIDs:
            readCounts.append(int(fields[ID]))
        if min(readCounts) >= minReads:
            outfile.write(line)

    outfile.close()

run()

