15 from commoncode import getConfigParser, getConfigOption, getConfigBoolOption
17 print "combinerds: version 1.2"
24 usage = "usage: python %s destinationRDS inputrds1 [inputrds2 ....] [--table table_name] [--init] [--initrna] [--index] [--cache pages]" % argv[0]
25 parser = makeParser(usage)
26 (options, args) = parser.parse_args(argv[1:])
35 combinerds(datafile, infileList, options.tableList, options.withFlag, options.doIndex, options.cachePages, options.doInit, options.initRNA)
38 def makeParser(usage):
39 parser = optparse.OptionParser(usage=usage)
40 parser.add_option("--table", action="append", dest="tablelist")
41 parser.add_option("--init", action="store_true", dest="doInit")
42 parser.add_option("--initrna", action="store_true", dest="initRNA")
43 parser.add_option("--index", action="store_true", dest="doIndex")
44 parser.add_option("--cache", type="int", dest="cachePages")
45 parser.add_option("--flag", dest="withFlag")
47 configParser = getConfigParser()
48 section = "combinerds"
49 doInit = getConfigBoolOption(configParser, section, "doInit", False)
50 initRNA = getConfigBoolOption(configParser, section, "initRNA", False)
51 doIndex = getConfigBoolOption(configParser, section, "doIndex", False)
52 cachePages = getConfigOption(configParser, section, "cachePages", None)
53 withFlag = getConfigOption(configParser, section, "withFlag", "")
55 parser.set_defaults(tableList=[], doInit=doInit, initRNA=initRNA, doIndex=doIndex, cachePages=cachePages,
61 def combinerds(datafile, infileList, tableList=[], withFlag="", doIndex=False, cachePages=None, doInit=False, initRNA=False):
63 print "destination RDS: %s" % datafile
70 if cachePages is not None:
75 rds = ReadDataset.ReadDataset(datafile, verbose=True, cache=doCache, initialize=doInit, datasetType=datasetType)
76 if cachePages > rds.getDefaultCacheSize():
77 rds.setDBcache(cachePages)
79 cachePages = rds.getDefaultCacheSize()
82 tableList = rds.getTables()
85 print "restrict to flag = %s" % withFlag
87 metaDict = rds.getMetadata()
88 if "numberImports" not in metaDict:
90 rds.insertMetadata([("numberImports", "0")])
92 origIndex = int(metaDict["numberImports"])
95 for inputfile in infileList:
96 dbName = "input%s" % str(index)
97 rds.attachDB(inputfile, dbName)
98 for table in tableList:
99 print "importing table %s from file %s" % (table, inputfile)
101 if table in ["uniqs", "multi"]:
102 dbColumns = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % dbName
103 elif table == "splices":
104 dbColumns = "NULL, '%s' || readID, chrom, startL, stopL, startR, stopR, sense, weight, flag, mismatch" % dbName
106 if table == "metadata":
107 dbColumns = "name, value || ' (import_%d)'" % index
108 rds.importFromDB(dbName, table, dbColumns)
110 rds.importFromDB(dbName, table, dbColumns, withFlag)
113 rds.insertMetadata([("import_%s" % str(index), "%s %s" % (inputfile, str(tableList)))])
116 rds.updateMetadata("numberImports", index, origIndex)
118 print "building index...."
120 rds.buildIndex(cachePages)
125 rds.saveCacheDB(datafile)
128 if __name__ == "__main__":