"""
    usage: python regiontobed label regionfile outbedfile [--color r,g,b] [--score field] [--narrowPeak] [--broadPeak] [--itemRgb] [--nolabel]
           where color is in comma-delimited RGB without space
           and field is a column with a score (first column is 0, second is 1,...)
           t-narrowPeak assumes that findall.py was run with -listPeak
           t-broadPeak assumes that findall.py was *NOT* run with -listPeak
"""

try:
    import psyco
    psyco.full()
except:
    pass

import sys
import math
import optparse
from commoncode import getConfigParser, getConfigOption, getConfigBoolOption

print "regiontobed: version 3.2"


def usage():
    print __doc__


def main(argv=None):
    if not argv:
        argv = sys.argv

    usage = __doc__

    parser = getParser(usage)
    (options, args) = parser.parse_args(argv[1:])

    if len(args) < 3:
        usage()
        sys.exit(2)

    factorlabel = args[0]
    regionfile = args[1]
    outfile = args[2]

    regiontobed(factorlabel, regionfile, outfile, options.color,
                options.scoreField, options.doNarrow, options.doBroad,
                options.itemRGB, options.noLabel)


def getParser(usage):
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("--color", dest="color")
    parser.add_option("--score", type="int", dest="scoreField")
    parser.add_option("--narrowPeak", action="store_true", dest="doNarrow")
    parser.add_option("--broadPeak", action="store_true", dest="doBroad")
    parser.add_option("--itemRgb", action="store_true", dest="itemRGB")
    parser.add_option("--nolabel", action="store_true", dest="noLabel")

    configParser = getConfigParser()
    section = "regiontobed"
    color = getConfigOption(configParser, section, "color", "0,0,0")
    scoreField = getConfigOption(configParser, section, "scoreField", None)
    doNarrow = getConfigBoolOption(configParser, section, "doNarrow", False)
    doBroad = getConfigBoolOption(configParser, section, "doBroad", False)
    itemRGB = getConfigBoolOption(configParser, section, "itemRGB", False)
    noLabel = getConfigBoolOption(configParser, section, "noLabel", False)

    parser.set_defaults(color=color, scoreField=scoreField, doNarrow=doNarrow,
                        doBroad=doBroad, itemRGB=itemRGB, noLabel=noLabel)

    return parser


def regiontobed(factorlabel, regionFileName, outFileName, color="0,0,0",
                scoreField=None, doNarrow=False, doBroad=False, itemRGB=False,
                noLabel=False):

    regionfile = open(regionFileName)
    outfile = open(outFileName, "w")

    if itemRGB:
        print "assigning each item its color"

    if noLabel:
        if itemRGB:
            outfile.write('track name=%s visibility=4 itemRgb="on"\n' % factorlabel)
        else:
            outfile.write("track name=%s visibility=4 color=%s\n" % (factorlabel, color))

    for line in regionfile:
        if line[0] == "#":
            continue

        fields = line.strip().split()
        if doNarrow:
            signalVal = float(fields[4])
            pval = float(fields[-1])
            if pval == 0.:
                pValue = 350
            else:
                pValue = -1. * math.log(pval, 10)

            peakPos = int(fields[9]) - int(fields[2])
            outfile.write("%s\t%s\t%s\t%s\t%d\t.\t%.4f\t%.4f\t-1\t%d" % (fields[1], fields[2], fields[3], fields[0], 0, signalVal, pValue, peakPos))
        elif doBroad:
            signalVal = float(fields[4])
            pval = float(fields[-1])
            if pval == 0.:
                pValue = 350
            else:
                pValue = -1. * math.log(pval, 10)

            outfile.write("%s\t%s\t%s\t%s\t%d\t.\t%.4f\t%.4f\t-1" % (fields[1], fields[2], fields[3], fields[0], 0, signalVal, pValue))
        elif scoreField is not None:
            score = int(float(fields[scoreField]))
            if score > 1000:
                score = 1000

            outfile.write("%s\t%s\t%s\t%s\t%s" % (fields[1], fields[2], fields[3], fields[0], score))
            if itemRGB:
                outfile.write("\t+\t-\t-\t%s" % color)
        else:
            outfile.write("%s\t%s\t%s\t%s" % (fields[1], fields[2], fields[3], fields[0]))
            if itemRGB:
                outfile.write("\t1000\t+\t-\t-\t%s" % color)

        outfile.write("\n")

    outfile.close()


if __name__ == "__main__":
    main(sys.argv)