try:
    import psyco
    psyco.full()
except:
    print "psyco not running"

print "altSpliceCounts: version 3.7"

import sys
import optparse
import ReadDataset
from commoncode import getConfigParser, getConfigOption

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

    usage = "usage: python %s rdsfile outfilename [--cache pages]"

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

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

    hitfile =  args[0]
    outfilename = args[1]

    if options.numCachePages is not None:
        doCache = True
        cachePages = options.numCachePages
    else:
        doCache = False
        cachePages = 100000

    altSpliceCounts(hitfile, outfilename, doCache, cachePages)


def makeParser(usage=""):
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("--cache", type="int", dest="numCachePages",
                      help="number of cache pages to use [default: 100000]")

    configParser = getConfigParser()
    section = "altSpliceCounts"
    numCachePages = getConfigOption(configParser, section, "numCachePages", None)

    parser.set_defaults(numCachePages=numCachePages)

    return parser


def altSpliceCounts(hitfile, outfilename, doCache=False, cachePages=100000):
    startDict = {}
    stopDict = {}
    resultDict = {}

    hitRDS = ReadDataset.ReadDataset(hitfile, verbose=True, cache=doCache)
    if cachePages > hitRDS.getDefaultCacheSize():
        hitRDS.setDBcache(cachePages)

    readlen = hitRDS.getReadSize()
    hitDict = hitRDS.getSplicesDict(noSense=True)
    outfile = open(outfilename, "w")

    for chrom in hitDict:
        startDict[chrom] = []
        stopDict[chrom] = []
        resultDict[chrom] = []

    index = 0
    for chrom in hitDict:
        for read in hitDict[chrom]:
            tagStart = read["startL"]
            tagStop = read["stopR"]
            index += 1
            length = tagStop - tagStart
            if length < readlen + 5:
                continue

            startDict[chrom].append((tagStart, length))
            stopDict[chrom].append((tagStop, length))

        startDict[chrom].sort()
        stopDict[chrom].sort()

    spliceEvent = 0
    altSpliceEvent = 0
    alternative = 1
    for chrom in startDict:
        firstIndex = 0
        maxIndex = len(startDict[chrom])
        while firstIndex < maxIndex:
            (fstart, flen) = startDict[chrom][firstIndex]
            (start, length) = (fstart, flen)
            secondIndex = firstIndex
            secondLengths = []
            while (start - fstart) < readlen:
                if secondIndex >= maxIndex:
                    break

                (start, length) = startDict[chrom][secondIndex]
                if (start - fstart) < readlen and abs(length - flen) > readlen:
                    line =  (chrom, fstart, fstart + flen, chrom, start, start + length)
                    alreadySeen = False
                    for slength in secondLengths:
                        if abs(slength - length) < readlen:
                            alreadySeen = True

                    if len(resultDict[chrom]) == 0:
                        resultDict[chrom].append(line)
                    elif line != resultDict[chrom][-1] and not alreadySeen:
                        resultDict[chrom].append(line)
                        secondLengths.append(length)
                        altSpliceEvent += 1
                        spliceEvent += 1

                secondIndex += 1

            firstIndex = secondIndex
            spliceEvent += 1

        firstIndex = 0
        maxIndex = len(stopDict[chrom])
        while firstIndex < maxIndex:
            (fstop, flen) = stopDict[chrom][firstIndex]
            (stop, length) = (fstop, flen)
            secondIndex = firstIndex
            secondLengths = []
            while (stop - fstop) < readlen:
                if secondIndex >= maxIndex:
                    break
                (stop, length) = stopDict[chrom][secondIndex]
                if (stop - fstop) < readlen and abs(length - flen) > readlen:
                    line = (chrom, fstop - flen, fstop, chrom, stop - length, stop)
                    alreadySeen = False
                    for slength in secondLengths:
                        if abs(slength - length) < readlen:
                            alreadySeen = True

                    if len(resultDict[chrom]) == 0:
                        resultDict[chrom].append(line)

                    if line != resultDict[chrom][-1] and not alreadySeen:
                        resultDict[chrom].append(line)
                        secondLengths.append(length)
                        altSpliceEvent += 1
                        spliceEvent += 1

                secondIndex += 1

            firstIndex = secondIndex
            spliceEvent += 1

        resultDict[chrom].sort()
        for line in resultDict[chrom]:
            outfile.write("alt%d" % alternative + "\tchr%s\t%d\t%d\tchr%s\t%d\t%d\n"  % line)
            alternative += 1

        print chrom, maxIndex, spliceEvent, altSpliceEvent

    print spliceEvent, altSpliceEvent
    outfile.close()

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