##################################
#                                #
# Last modified 05/10/2011       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s input radius peakField outfile' % sys.argv[0]
        sys.exit(1)

    input = sys.argv[1]
    radius = int(sys.argv[2])
    peakID = int(sys.argv[3])
    outfile = open(sys.argv[4],'w')

    listoflines = open(input)
    for line in listoflines:
        if line[0]=='#':
            continue
        fields=line.split('\n')[0].split('\t')
        chr=fields[1]
        peak=int(fields[peakID])
        left=peak-radius
        right=peak+radius
        outfile.write(chr + '\t' + str(left) + '\t'+ str(right) + '\n')

    outfile.close()

run()
