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

import sys
import string

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s inputfile outputfilename' % sys.argv[0]
        print '       the script will sort motif instances by indentity'
        sys.exit(1)
    
    input = sys.argv[1]
    outfilename = sys.argv[2]

    outfile = open(outfilename, 'w')

    RegionDict={}
    inputdatafile = open(input)
    i=0
    for line in inputdatafile:
        i+=1
        if i % 100000 == 0:
            print i
        fields=line.strip().split('\t')
        motif=fields[0]
        score=int(fields[1])
        otherfields=(fields[2],fields[3],fields[4],fields[5])
        chr=motif.split(':')[0]
        left=int(motif.split(':')[1].split('-')[0])
        right=int(motif.split(':')[1].split('-')[1])
        outline = chr +'\t'+ str(left) +'\t'+ str(right) +'\t'+ str(score) +'\t'+ otherfields[0] +'\t'+ otherfields[1] +'\t'+ otherfields[2] +'\t'+ otherfields[3]
        outfile.write(outline+'\n')
 
    outfile.close()
   
run()
