#
#  rdsmetadata.py
#  ENRAGE
#
try:
    import psyco
    psyco.full()
except:
    pass

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

print "rdsmetadata: version 2.8"


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

    usage = "usage: python %prog rdsfile [propertyName1::propertyValue1] ... [propertyNameN::propertyValueN] [options]"

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

    if len(args) < 1:
        print usage
        print "where the optional metadata name::value pairs are added to the existing dataset"
        sys.exit(1)

    datafile = args[0]

    propertyList=[]
    for arg in args:
        if "::" in arg:
            (pname, pvalue) = arg.strip().split("::")
            print "adding %s : %s" % (pname, pvalue)
            propertyList.append((pname, pvalue))

    rdsmetadata(datafile, propertyList, options.cacheVal, options.buildIndex,
                options.dropIndex, options.doCount, options.doComplexity,
                options.resetFlags, options.rnaDataType, options.cachePages)


def makeParser(usage=""):
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("--defaultcache", type="int", dest="cacheVal")
    parser.add_option("--index", action="store_true", dest="buildIndex")
    parser.add_option("--dropindex", action="store_true", dest="dropIndex")
    parser.add_option("--nocount", action="store_false", dest="doCount")
    parser.add_option("--complexity", action="store_true", dest="doComplexity")
    parser.add_option("--reset", action="store_true", dest="resetFlags")
    parser.add_option("--initrna", action="store_true", dest="rnaDataType")
    parser.add_option("--cache", type="int", dest="cachePages")

    configParser = getConfigParser()
    section = "rdsmetadata"
    cacheVal = getConfigIntOption(configParser, section, "cacheVal", 0)
    buildIndex = getConfigBoolOption(configParser, section, "buildIndex", False)
    dropIndex = getConfigBoolOption(configParser, section, "dropIndex", False)
    doCount = getConfigBoolOption(configParser, section, "doCount", True)
    doComplexity = getConfigBoolOption(configParser, section, "doComplexity", False)
    resetFlags = getConfigBoolOption(configParser, section, "resetFlags", False)
    rnaDataType = getConfigBoolOption(configParser, section, "rnaDataType", False)
    cachePages = getConfigIntOption(configParser, section, "cachePages", -1)

    parser.set_defaults(cacheVal=cacheVal, buildIndex=buildIndex, dropIndex=dropIndex, doCount=doCount,
                        doComplexity=doComplexity, resetFlags=resetFlags, rnaDataType=rnaDataType,
                        cachePages=cachePages)

    return parser


def rdsmetadata(datafile, propertyList=[], cacheVal=0, buildIndex=False,
                dropIndex=False, doCount=True, doComplexity=False, resetFlags=False,
                rnaDataType=False, cachePages=-1):

    doCache = False
    if cachePages != -1:
        doCache = True

    if rnaDataType:
        rds = ReadDataset.ReadDataset(datafile, initialize=True, datasetType="RNA", verbose=True, cache=doCache)
    else:
        rds = ReadDataset.ReadDataset(datafile, verbose=True, reportCount=doCount, cache=doCache)

    if cachePages > rds.getDefaultCacheSize():
        rds.setDBcache(cachePages)

    if cacheVal > 0:
        rds.setDBcache(cacheVal, default=True)
        print "set default cache size to %d pages" % cacheVal

    if resetFlags:
        print "clearing read flags"
        rds.resetFlags()

    if dropIndex:
        try:
            rds.dropIndex()
        except:
            print "could not drop index"

    if buildIndex:
        print "building index...."
        if cacheVal > 0:
            rds.buildIndex(cacheVal)
        else:
            rds.buildIndex()

    if doComplexity:
        print "calculating uniq read complexity..."
        uniqs = rds.getUniqsCount(distinct=False)
        distincts = rds.getUniqsCount(distinct=True)
        print "%d distincts / %d uniqs = %.2f" % (distincts, uniqs, float(distincts) / uniqs)

    if len(propertyList) > 0:
        rds.insertMetadata(propertyList)


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