##################################
#                                #
# Last modified 03/19/2011       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import pysam
from sets import Set

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

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s junctions.bed outfilename' % sys.argv[0]
        print '   BAM file support in progress'

        sys.exit(1)

    inputfilename = sys.argv[1]
    outputfilename = sys.argv[2]

    outfile = open(outputfilename, 'w')

    lineslist = open(inputfilename)
    TopHatJunctionDict={}
    for line in lineslist:
        if line.startswith('track name'):
            continue
        fields=line.strip().split('\t')
        if len(fields)<11:
            continue
        chr=fields[0]
        leftShift=int(fields[10].split(',')[0])+1
        rightShift=int(fields[10].split(',')[1])-1
        start=int(fields[1])+leftShift
        stop=int(fields[2])-rightShift
        strand=fields[5]
        counts=int(fields[4])
        outline=chr+'\t'+str(start)+'\t'+str(stop)+'\t'+strand+'\t'+str(counts)
        outfile.write(outline+'\n')

    outfile.close()

run()
