#!/usr/bin/env python2
#
# Copyright John Reid 2013
#

"""
Takes 3 column BED input with just

    chr start stop

and adds fourth name column.
"""

import sys
from optparse import OptionParser


#
# Parse the options
#
option_parser = OptionParser()
option_parser.add_option(
    "-t",
    "--tag",
    help="Region names will all start with TAG.",
    default="r",
    metavar="TAG",
)
options, args = option_parser.parse_args()


# open the input
if len(args) < 1:
    input = sys.stdin
else:
    input = open(args[0])

i = 0
for line in input:
    if line.startswith('track') or line.startswith('browser'):
        sys.stdout.write(line)
        continue
    record = line.strip().split('\t')
    if len(record) < 3:
        raise ValueError('Not enough columns in BED file.')
    name = '%s%d' % (options.tag, i)
    if len(record) < 4:
        record.append(name)
    else:
        record[3] = name
    print '\t'.join(record)
    i += 1
