##################################
#                                #
# Last modified 2018/07/05       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import os

def run():

    if len(sys.argv) < 3:
        print 'usage: python %s inputfilename chrFieldID outfileprefix [-splitBy separator]' % sys.argv[0]
        sys.exit(1)

    inputfilename = sys.argv[1]
    fieldID = int(sys.argv[2])
    outfileprefix = sys.argv[3]

    SB = '\t'
    if '-splitBy' in sys.argv:
       SB = sys.argv[sys.argv.index('-splitBy') + 1]

    outfileDict={}
    header = ''
    if inputfilename.endswith('.bz2'):
        cmd = 'bzip2 -cd ' + inputfilename
    elif inputfilename.endswith('.gz') or inputfilename.endswith('.bgz'):
        cmd = 'zcat ' + inputfilename
    else:
        cmd = 'cat ' + inputfilename
    p = os.popen(cmd, "r")
    line = 'line'
    i=0
    while line != '':
        line = p.readline()
        if line == '':
            break
        if line.startswith('#'):
            header = line
            continue
        fields = line.strip().split(SB)
        chr = fields[fieldID].replace('/',',')
        if outfileDict.has_key(chr):
            pass
        else:
            outfileDict[chr]=open(outfileprefix+'.' + chr,'w')
            if header != '':
                outfileDict[chr].write(header)
        outfileDict[chr].write(line)

    for chr in outfileDict.keys():
        outfileDict[chr].close()
        
run()

