2878423a9716a0e4f31d0a1738a0770339c8ed6c
[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():
39     usage = __doc__
40
41     parser = optparse.OptionParser(usage=usage)
42     parser.add_option("--table", action="append", dest="tablelist")
43     parser.add_option("--init", action="store_true", dest="doInit")
44     parser.add_option("--initrna", action="store_true", dest="initRNA")
45     parser.add_option("--index", action="store_true", dest="doIndex")
46     parser.add_option("--cache", type="int", dest="cachePages")
47     parser.add_option("--flag", dest="withFlag")
48
49     configParser = getConfigParser()
50     section = "combinerds"
51     doInit = getConfigBoolOption(configParser, section, "doInit", False)
52     initRNA = getConfigBoolOption(configParser, section, "initRNA", False)
53     doIndex = getConfigBoolOption(configParser, section, "doIndex", False)
54     cachePages = getConfigOption(configParser, section, "cachePages", None)
55     withFlag = getConfigOption(configParser, section, "withFlag", "")
56
57     parser.set_defaults(tableList=[], doInit=doInit, initRNA=initRNA, doIndex=doIndex, cachePages=cachePages,
58                         withFlag=withFlag)
59
60     return parser
61
62
63 def combinerds(datafile, infileList, tableList=[], withFlag="", doIndex=False, cachePages=None, doInit=False, initRNA=False):
64
65     print "destination RDS: %s" % datafile
66     datasetType="DNA"
67     if initRNA:
68         doInit = True
69         datasetType="RNA"
70
71     doCache = False
72     if cachePages is not None:
73         doCache = True
74     else:
75         cachePages = -1
76
77     rds = ReadDataset.ReadDataset(datafile, verbose=True, cache=doCache, initialize=doInit, datasetType=datasetType)
78     if cachePages > rds.getDefaultCacheSize():
79         rds.setDBcache(cachePages)
80     else:
81         cachePages = rds.getDefaultCacheSize()
82
83     if tableList == []:
84         tableList = rds.getTables()
85
86     if withFlag != "":
87         print "restrict to flag = %s" % withFlag
88
89     metaDict = rds.getMetadata()
90     if "numberImports" not in metaDict:
91         origIndex = 0
92         rds.insertMetadata([("numberImports", "0")])
93     else:
94         origIndex = int(metaDict["numberImports"])
95
96     index = origIndex
97     for inputfile in infileList:
98         dbName = "input%s" % str(index)
99         rds.attachDB(inputfile, dbName)
100         for table in tableList:
101             print "importing table %s from file %s" % (table, inputfile)
102             dbColumns = "*"
103             if table == "uniqs":
104                 dbColumns = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % dbName
105             elif table == "multi":
106                 dbColumns = "NULL, '%s' || readID, chrom, start, stop, sense, weight, flag, mismatch" % dbName
107             elif table == "splices":
108                 dbColumns = "NULL, '%s' || readID, chrom, startL, stopL, startR, stopR, sense, weight, flag, mismatch" % dbName
109
110             if table == "metadata":
111                 dbColumns = "name, value || ' (import_%d)'" % index
112                 rds.importFromDB(dbName, table, dbColumns)
113             else:
114                 rds.importFromDB(dbName, table, dbColumns, withFlag)
115
116         rds.detachDB(dbName)
117         rds.insertMetadata([("import_%s" % str(index), "%s %s" % (inputfile, str(tableList)))])
118         index += 1
119
120     rds.updateMetadata("numberImports", index, origIndex)
121     if doIndex:
122         print "building index...."
123         if cachePages > 0:
124             rds.buildIndex(cachePages)
125         else:
126             rds.buildIndex()
127
128     if doCache:
129         rds.saveCacheDB(datafile)
130
131
132 if __name__ == "__main__":
133     main(sys.argv)