##################################
#                                #
# Last modified 2016/12/11       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import math

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s phastConsWig minScore maxGap outputfilename' % sys.argv[0]
        print '\t use - for stdIn'
        sys.exit(1)
    
    input = sys.argv[1]
    minScore = float(sys.argv[2])
    maxGap = float(sys.argv[3])
    outfilename = sys.argv[4]

    outfile = open(outfilename, 'w')

    RegionsDict = {}
    if input == '-':
        lineslist  = sys.stdin
    else:
        lineslist = open(input)
    InRegion = False
    for line in lineslist:
        if line.startswith('fixedStep'):
            chr=line.split(' ')[1].split('=')[1]
            currentPos=int(line.split(' ')[2].split('=')[1])
            continue
        score = float(line.split('\n')[0])
        if InRegion:
            if score >= minScore:
                pass
            else:
                if RegionsDict.has_key(chr):
                    pass
                else:
                    RegionsDict[chr] = []
                RegionsDict[chr].append((initialPos,currentPos))
                InRegion = False
        else:
            if score >= minScore:
                InRegion = True
                initialPos = currentPos
            else:
                pass
        currentPos+=1

    chromosomes = RegionsDict.keys()
    chromosomes.sort()

    print chromosomes

    for chr in chromosomes:
        print len(RegionsDict[chr])
        RegionsDict[chr].sort()
        for i in range(len(RegionsDict[chr]) - 1):
            (left1,right1) = RegionsDict[chr][i]
            (left2,right2) = RegionsDict[chr][i+1]
            if left2 - right1 <= maxGap:
                RegionsDict[chr][i+1] = (left1,right2)
                if i+1 == len(RegionsDict[chr]) - 1:
                    outline = chr + '\t' + str(left1) + '\t' + str(right2)
                    outfile.write(outline + '\n')
            else:
                outline = chr + '\t' + str(left1) + '\t' + str(right1)
                outfile.write(outline + '\n')
                if i+1 == len(RegionsDict[chr]) - 1:
                    outline = chr + '\t' + str(left2) + '\t' + str(right2)
                    outfile.write(outline + '\n')

    outfile.close()
   
run()
