#
#  combinerds.py
#  ENRAGE
#

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

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

print "combinerds: version 1.2"


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

    usage = "usage: python %s destinationRDS inputrds1 [inputrds2 ....] [-table table_name] [--init] [--initrna] [--index] [--cache pages]" % argv[0]
    parser = makeParser(usage)
    (options, args) = parser.parse_args(argv[1:])

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

    datafile = args[0]
    infileList = args[1:]

    combinerds(datafile, infileList, options.tableList, options.withFlag, options.doIndex, options.cachePages, options.doInit, options.initRNA)


def makeParser():
    usage = __doc__

    parser = optparse.OptionParser(usage=usage)
    parser.add_option("--table", action="append", dest="tablelist")
    parser.add_option("--init", action="store_true", dest="doInit")
    parser.add_option("--initrna", action="store_true", dest="initRNA")
    parser.add_option("--index", action="store_true", dest="doIndex")
    parser.add_option("--cache", type="int", dest="cachePages")
    parser.add_option("--flag", dest="withFlag")

    configParser = getConfigParser()
    section = "combinerds"
    doInit = getConfigBoolOption(configParser, section, "doInit", False)
    initRNA = getConfigBoolOption(configParser, section, "initRNA", False)
    doIndex = getConfigBoolOption(configParser, section, "doIndex", False)
    cachePages = getConfigOption(configParser, section, "cachePages", None)
    withFlag = getConfigOption(configParser, section, "withFlag", "")

    parser.set_defaults(tableList=[], doInit=doInit, initRNA=initRNA, doIndex=doIndex, cachePages=cachePages,
                        withFlag=withFlag)

    return parser


def combinerds(datafile, infileList, tableList=[], withFlag="", doIndex=False, cachePages=None, doInit=False, initRNA=False):

    print "destination RDS: %s" % datafile
    datasetType="DNA"
    if initRNA:
        doInit = True
        datasetType="RNA"

    doCache = False
    if cachePages is not None:
        doCache = True
    else:
        cachePages = -1

    rds = ReadDataset.ReadDataset(datafile, verbose=True, cache=doCache, initialize=doInit, datasetType=datasetType)
    if cachePages > rds.getDefaultCacheSize():
        rds.setDBcache(cachePages)
    else:
        cachePages = rds.getDefaultCacheSize()

    if tableList == []:
        tableList = rds.getTables()

    if withFlag != "":
        print "restrict to flag = %s" % withFlag

    metaDict = rds.getMetadata()
    if "numberImports" not in metaDict:
        origIndex = 0
        rds.insertMetadata([("numberImports", "0")])
    else:
        origIndex = int(metaDict["numberImports"])

    index = origIndex
    for inputfile in infileList:
        dbName = "input%s" % str(index)
        rds.attachDB(inputfile, dbName)
        for table in tableList:
            print "importing table %s from file %s" % (table, inputfile)
            dbColumns = "*"
            if table == "uniqs":
                dbColumns = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % dbName
            elif table == "multi":
                dbColumns = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % dbName
            elif table == "splices":
                dbColumns = "NULL, '%s' || readID, chrom, startL, stopL, startR, stopR, sense, weight, flag, mismatch" % dbName

            if table == "metadata":
                dbColumns = "name, value || ' (import_%d)'" % index
                rds.importFromDB(dbName, table, dbColumns)
            else:
                rds.importFromDB(dbName, table, dbColumns, withFlag)

        rds.detachDB(dbName)
        rds.insertMetadata([("import_%s" % str(index), "%s %s" % (inputfile, str(tableList)))])
        index += 1

    rds.updateMetadata("numberImports", index, origIndex)
    if doIndex:
        print "building index...."
        if cachePages > 0:
            rds.buildIndex(cachePages)
        else:
            rds.buildIndex()

    if doCache:
        rds.saveCacheDB(datafile)


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