erange version 4.0a dev release
[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 ReadDataset
14
15 print "combinerds: version 1.2"
16
17
18 def main(argv=None):
19     if not argv:
20         argv = sys.argv
21
22     if len(argv) < 2:
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'
25         sys.exit(1)
26
27     doCache = False
28     cachePages = -1
29     if '--cache' in argv:
30         doCache = True
31         try:
32             cachePages =  int(argv[sys.argv.index('-cache') + 1])
33         except: 
34             pass
35
36     datafile = argv[1]
37     infileList = []
38     for index in range(2, len(argv)):
39         if argv[index][0] == '-':
40             break
41         infileList.append(sys.argv[index])
42
43     print "destination RDS: %s" % datafile
44
45     if '--initrna' in argv:
46         rds = ReadDataset.ReadDataset(datafile, initialize=True, datasetType='RNA')
47     elif '--init' in argv:
48         rds = ReadDataset.ReadDataset(datafile, initialize=True)
49
50     withFlag = ''
51     if '--flag' in argv:
52         withFlag = argv[sys.argv.index('-flag') + 1]
53         print "restrict to flag = %s" % withFlag
54
55     rds = ReadDataset.ReadDataset(datafile, verbose=True, cache=doCache)
56
57     if cachePages > rds.getDefaultCacheSize():
58         rds.setDBcache(cachePages)
59         cacheVal = cachePages
60     else:
61         cacheVal = rds.getDefaultCacheSize()
62
63     doIndex = False
64     if '--index' in argv:
65         doIndex = True
66
67     tableList = []
68     if '--table' in argv:
69         tableList.append(argv[argv.index('-table') + 1])
70     else:
71         tableList = rds.getTables()
72
73     combinerds(datafile, rds, infileList, cacheVal, tableList, withFlag, doIndex, doCache)
74
75
76 def combinerds(datafile, rds, infileList, cacheVal, tableList=[], withFlag="", doIndex=False, doCache=False):
77     metaDict = rds.getMetadata()
78     if "numberImports" not in metaDict:
79         origIndex = 0
80         rds.insertMetadata([("numberImports", str(0))])
81     else:
82         origIndex = int(metaDict["numberImports"])
83
84     index = origIndex
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)
90             ascols = "*"
91             if table == "uniqs":
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)
100
101             if table != "metadata":
102                 rds.importFromDB(asname, table, ascols, withFlag)
103
104         rds.detachDB(asname)
105         rds.insertMetadata([("import_" + str(index), "%s %s" % (inputfile, str(tableList)))])
106         index += 1
107
108     rds.updateMetadata("numberImports", index, origIndex)
109     if doIndex:
110         print "building index...."
111         if cacheVal > 0:
112             rds.buildIndex(cacheVal)
113         else:
114             rds.buildIndex()
115
116     if doCache:
117         rds.saveCacheDB(datafile)
118
119
120 if __name__ == "__main__":
121     main(sys.argv)