
import optparse
import re




parser = optparse.OptionParser()
parser.add_option("-i", "--defuse_in", action = "store", type = "string", dest = "defuse_in", help = 'Original defusescan output file')
parser.add_option("-o", "--defuse_out", action = "store", type = "string", dest = "defuse_out", help = 'defusescan corrected output file')

(options, args) = parser.parse_args()



PATH_TO_IN = options.defuse_in
PATH_TO_OUT = options.defuse_out




try:
    f_in = open(PATH_TO_IN, 'r')
except IOError, (errno, strerror):
    print "I/O error(%d): %s" % (errno, strerror)
    sys.exit(0)
try:
    f_out = open(PATH_TO_OUT, 'w')
except IOError, (errno, strerror):
    print "I/O error(%d): %s" % (errno, strerror)
    sys.exit(0)

for line in f_in:
    row_defuse_split = line.split("\t")
    if (row_defuse_split[0]=='cluster_id'):
        f_out.write(row_defuse_split[0] + "\t"  + row_defuse_split[1] +  "\t"  + row_defuse_split[2] +  "\t"  + row_defuse_split[3] +  "\t"  + row_defuse_split[4] +  "\t"  + row_defuse_split[5] +   "\t"  + row_defuse_split[6]  +  "\t"+ row_defuse_split[7] + "\t" + row_defuse_split[8] + "\t" + row_defuse_split[9]  + "\t" + row_defuse_split[10] + "\t" + "break_predict" + "\t" +  row_defuse_split[11] + "\t" + row_defuse_split[12] + "\t" + row_defuse_split[13] + "\t" + "concordant_ratio")
        for i in row_defuse_split[14:]:
            f_out.write("\t" + i)
    else:
        for i in row_defuse_split[0:11]:
            f_out.write(i + "\t")
        f_out.write("splitr" + "\t")
        for i in row_defuse_split[11:14]:
            f_out.write(i + "\t")
        f_out.write("0" + "\t")
        for i in row_defuse_split[14:]:
            if (i != row_defuse_split[-1]):
                f_out.write(i + "\t")
            else:
                f_out.write(i)


f_in.close()
f_out.close()


