13 from commoncode import readDataset
15 print '%s: version 1.1' % sys.argv[0]
23 print 'usage: python %s destinationRDS inputrds1 [inputrds2 ....] [-table table_name] [--init] [--initrna] [--index] [--cache pages]' % argv[0]
24 #print '\nwhere the optional metadata name::value pairs are added to the existing dataset\n'
32 cachePages = int(argv[sys.argv.index('-cache') + 1])
38 for index in range(2, len(argv)):
39 if argv[index][0] == '-':
41 infileList.append(sys.argv[index])
43 print "destination RDS: %s" % datafile
45 if '--initrna' in argv:
46 rds = readDataset(datafile, initialize=True, datasetType='RNA')
47 elif '--init' in argv:
48 rds = readDataset(datafile, initialize=True)
52 withFlag = argv[sys.argv.index('-flag') + 1]
53 print "restrict to flag = %s" % withFlag
55 rds = readDataset(datafile, verbose=True, cache=doCache)
57 if cachePages > rds.getDefaultCacheSize():
58 rds.setDBcache(cachePages)
61 cacheVal = rds.getDefaultCacheSize()
69 tableList.append(argv[argv.index('-table') + 1])
71 tableList = rds.getTables()
73 combinerds(datafile, rds, infileList, cacheVal, tableList, withFlag, doIndex, doCache)
76 def combinerds(datafile, rds, infileList, cacheVal, tableList=[], withFlag="", doIndex=False, doCache=False):
77 metaDict = rds.getMetadata()
78 if "numberImports" not in metaDict:
80 rds.insertMetadata([("numberImports", str(0))])
82 origIndex = int(metaDict["numberImports"])
85 for inputfile in infileList:
86 asname = "input" + str(index)
87 rds.attachDB(inputfile,asname)
88 for table in tableList:
89 print "importing table %s from file %s" % (table, inputfile)
92 ascols = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % asname
93 elif table == "multi":
94 ascols = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % asname
95 elif table == "splices":
96 ascols = "NULL, '%s' || readID, chrom, startL, stopL, startR, stopR, sense, weight, flag, mismatch" % asname
97 elif table == "metadata":
98 ascols = "name, value || ' (import_%d)'" % index
99 rds.importFromDB(asname, table, ascols)
101 if table != "metadata":
102 rds.importFromDB(asname, table, ascols, withFlag)
105 rds.insertMetadata([("import_" + str(index), "%s %s" % (inputfile, str(tableList)))])
108 rds.updateMetadata("numberImports", index, origIndex)
110 print "building index...."
112 rds.buildIndex(cacheVal)
117 rds.saveCacheDB(datafile)
120 if __name__ == "__main__":