##################################
#                                #
# Last modified 02/15/2011       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
from sets import Set

def run():

    if len(sys.argv) < 3:
        print 'usage: python %s junctions1 junctions1 outfile' % sys.argv[0]
        print '       junctions format: chr <tab> left <tab> right <tab> strand <tab> total_counts <tab> staggered_counts'
        sys.exit(1)
    
    junctions1 = sys.argv[1]
    junctions2 = sys.argv[2]
    outfilename = sys.argv[3]

    outfile = open(outfilename, 'w')
    outfile2 = open(outfilename+'-scores', 'w')
    outfile1 = open(outfilename+'-IDs', 'w')

    junctions1Dict={}
    junctions2Dict={}
    IDlist=[]
    
    linelist = open(junctions1)
    for line in linelist:
        if line.startswith('#'):
            continue
        fields=line.strip().split('\t')
        chr=fields[0]
        left=fields[1]
        right=fields[2]
        strand=fields[3]
        staggered=fields[5]
        ID=chr+':'+left+'-'+right+strand
        junctions1Dict[ID]=staggered
        IDlist.append(ID)
   
    linelist = open(junctions2)
    for line in linelist:
        if line.startswith('#'):
            continue
        fields=line.strip().split('\t')
        chr=fields[0]
        left=fields[1]
        right=fields[2]
        strand=fields[3]
        staggered=fields[5]
        ID=chr+':'+left+'-'+right+strand
        junctions2Dict[ID]=staggered
        IDlist.append(ID)

    IDlist=list(set(IDlist))    

    IDlist.sort()

    for ID in IDlist:
        outline1=ID
        if junctions2Dict.has_key(ID):
            counts2=junctions2Dict[ID]
        else:
            counts2='0'
        if junctions1Dict.has_key(ID):
            counts1=junctions1Dict[ID]
        else:
            counts1='0'
        outline2=counts1+'\t'+counts2
        outline=outline1+'\t'+outline2
        outfile.write(outline+'\n')
        outfile1.write(outline1+'\n')
        outfile2.write(outline2+'\n')

    outfile.close()
    outfile1.close()
    outfile2.close()   

run()