##################################
#                                #
# Last modified 10/28/2010       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
from sets import Set

def run():

    print sys.argv

    if len(sys.argv) < 3:
        print 'usage: python %s inputfilename <gtfcomparison|junctions> outputfilename' % sys.argv[0]
        print '       Note: this script works with files formatted the following way:'
        print '       for the gtfcomparison option: ID1 <tab> FPKM_#1 <tab> FPKM_lo_#1 <tab> FPKM_hi_#1 <tab> ID2 <tab> FPKM_#2 <tab> FPKM_lo_#2 <tab> FPKM_hi_#2 <tab>'
        print '       for the junctions option: chr <tab> left <tab> right <tab> strand <tab> staggered_counts_#1 <tab> staggered_counts_#2'
        sys.exit(1)
    
    input = sys.argv[1]
    type = sys.argv[2]
    outfilename = sys.argv[3]

    linelist = open(input)
    outfile = open(outfilename, 'w')
    i=0
    if type == 'gtfcomparison':
        for line in linelist:
            i+=1
            fields=line.strip().split('\t')
            if i==1 and line.startswith('ID1'):
                continue
            ID1=fields[0]
            FPKM1=fields[1]
            ID2=fields[4]
            FPKM2=fields[5]
            if ID1=='-':
                ID1=ID2
#                ID1='-1'
                FPKM1='-1'
            if ID2=='-':
#                ID2='-1'
                FPKM2='-1'
#            outline=ID1+'\t'+FPKM1+'\t'+ID2+'\t'+FPKM2
            outline=ID1+'\t'+FPKM1+'\t'+ID1+'\t'+FPKM2
            outfile.write(outline+'\n')
    if type == 'junctions':
        for line in linelist:
            i+=1
            fields=line.strip().split('\t')
            if i==1 and line.startswith('#'):
                continue
            chr=fields[0]
            left=fields[1]
            right=fields[2]
            strand=fields[3]
            counts1=fields[4]
            counts2=fields[5]
            ID1=chr+'-'+left+':'+right+strand
            ID2=chr+'-'+left+':'+right+strand
            if counts1=='0':
#                ID1='-1'
                counts1='-1'
            if counts2=='0':
#                ID2='-1'
                counts2='-1'
            outline=ID1+'\t'+counts1+'\t'+ID1+'\t'+counts2
            outfile.write(outline+'\n')

    outfile.close()
            
run()

