first pass cleanup of cistematic/genomes; change bamPreprocessing
[erange.git] / combinerds.py
1 #
2 #  combinerds.py
3 #  ENRAGE
4 #
5
6 try:
7     import psyco
8     psyco.full()
9 except:
10     pass
11
12 import sys
13 import optparse
14 import ReadDataset
15 from commoncode import getConfigParser, getConfigOption, getConfigBoolOption
16
17 print "combinerds: version 1.2"
18
19
20 def main(argv=None):
21     if not argv:
22         argv = sys.argv
23
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:])
27
28     if len(args) < 2:
29         print usage
30         sys.exit(1)
31
32     datafile = args[0]
33     infileList = args[1:]
34
35     combinerds(datafile, infileList, options.tableList, options.withFlag, options.doIndex, options.cachePages, options.doInit, options.initRNA)
36
37
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")
46
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", "")
54
55     parser.set_defaults(tableList=[], doInit=doInit, initRNA=initRNA, doIndex=doIndex, cachePages=cachePages,
56                         withFlag=withFlag)
57
58     return parser
59
60
61 def combinerds(datafile, infileList, tableList=[], withFlag="", doIndex=False, cachePages=None, doInit=False, initRNA=False):
62
63     print "destination RDS: %s" % datafile
64     datasetType="DNA"
65     if initRNA:
66         doInit = True
67         datasetType="RNA"
68
69     doCache = False
70     if cachePages is not None:
71         doCache = True
72     else:
73         cachePages = -1
74
75     rds = ReadDataset.ReadDataset(datafile, verbose=True, cache=doCache, initialize=doInit, datasetType=datasetType)
76     if cachePages > rds.getDefaultCacheSize():
77         rds.setDBcache(cachePages)
78     else:
79         cachePages = rds.getDefaultCacheSize()
80
81     if tableList == []:
82         tableList = rds.getTables()
83
84     if withFlag != "":
85         print "restrict to flag = %s" % withFlag
86
87     metaDict = rds.getMetadata()
88     if "numberImports" not in metaDict:
89         origIndex = 0
90         rds.insertMetadata([("numberImports", "0")])
91     else:
92         origIndex = int(metaDict["numberImports"])
93
94     index = origIndex
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)
100             dbColumns = "*"
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
105
106             if table == "metadata":
107                 dbColumns = "name, value || ' (import_%d)'" % index
108                 rds.importFromDB(dbName, table, dbColumns)
109             else:
110                 rds.importFromDB(dbName, table, dbColumns, withFlag)
111
112         rds.detachDB(dbName)
113         rds.insertMetadata([("import_%s" % str(index), "%s %s" % (inputfile, str(tableList)))])
114         index += 1
115
116     rds.updateMetadata("numberImports", index, origIndex)
117     if doIndex:
118         print "building index...."
119         if cachePages > 0:
120             rds.buildIndex(cachePages)
121         else:
122             rds.buildIndex()
123
124     if doCache:
125         rds.saveCacheDB(datafile)
126
127
128 if __name__ == "__main__":
129     main(sys.argv)