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

import sys
import string
import math
from sets import Set

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s gtf outfile' % sys.argv[0]
        sys.exit(1)
    
    gtf=sys.argv[1]
    outfile = open(sys.argv[2], 'w')

    DataDict={}
    linelist = open(gtf)
    for line in linelist:
        fields=line.strip().split('\t')
        chr=fields[0]
        left=int(fields[3])
        right=int(fields[4])
        strand=fields[6]
        annotation=fields[1]
        type=fields[2]
        score=fields[5]
        dot=fields[7]
        attributes=fields[8]
        DataDict[(chr,left,right,strand,annotation,type,score,dot,attributes)]=line

    keys=DataDict.keys()
    keys.sort()
    
    for (chr,left,right,strand,annotation,type,score,dot,attributes) in keys:
        outfile.write(DataDict[(chr,left,right,strand,annotation,type,score,dot,attributes)])

    outfile.close()

run()