snapshot of 4.0a development. initial git repo commit
[erange.git] / rdsmetadata.py
1 #
2 #  rdsmetadata.py
3 #  ENRAGE
4 #
5 try:
6     import psyco
7     psyco.full()
8 except:
9     pass
10
11 import sys
12 import optparse
13 from commoncode import readDataset
14
15 print "%prog: version 2.7"
16
17
18 def main(argv=None):
19     if not argv:
20         argv = sys.argv
21
22     usage = "usage: python %prog rdsfile [propertyName1::propertyValue1] ... [propertyNameN::propertyValueN] [options]"
23
24     parser = optparse.OptionParser(usage=usage)
25     parser.add_option("--defaultcache", type="int", dest="cacheVal")
26     parser.add_option("--index", action="store_true", dest="buildIndex")
27     parser.add_option("--dropindex", action="store_true", dest="dropIndex")
28     parser.add_option("--nocount", action="store_false", dest="doCount")
29     parser.add_option("--complexity", action="store_true", dest="doComplexity")
30     parser.add_option("--reset", action="store_true", dest="resetFlags")
31     parser.add_option("--initrna", action="store_true", dest="rnaDataType")
32     parser.add_option("--cache", type="int", dest="cachePages")
33     parser.set_defaults(cacheVal=0, buildIndex=False, dropIndex=False, doCount=True,
34                         doComplexity=False, resetFlags=False, rnaDataType=False,
35                         cachePages=-1)
36
37     (options, args) = parser.parse_args(argv[1:])
38
39     if len(args) < 1:
40         print usage
41         print "where the optional metadata name::value pairs are added to the existing dataset"
42         sys.exit(1)
43
44     datafile = args[0]
45
46     propertyList=[]
47     for arg in args:
48         if "::" in arg:
49             (pname, pvalue) = arg.strip().split("::")
50             print "adding %s : %s" % (pname, pvalue)
51             propertyList.append((pname, pvalue))
52
53     rdsmetadata(datafile, propertyList, options.cacheVal, options.buildIndex,
54                 options.dropIndex, options.doCount, options.doComplexity,
55                 options.resetFlags, options.rnaDataType, options.cachePages)
56
57
58 def rdsmetadata(datafile, propertyList=[], cacheVal=0, buildIndex=False,
59                 dropIndex=False, doCount=True, doComplexity=False, resetFlags=False,
60                 rnaDataType=False, cachePages=-1):
61
62     doCache = False
63     if cachePages != -1:
64         doCache = True
65
66     if rnaDataType:
67         rds = readDataset(datafile, initialize=True, datasetType="RNA", verbose=True, cache=doCache)
68     else:
69         rds = readDataset(datafile, verbose=True, reportCount=doCount, cache=doCache)
70
71     if cachePages > rds.getDefaultCacheSize():
72         rds.setDBcache(cachePages)
73
74     if cacheVal > 0:
75         rds.setDBcache(cacheVal, default=True)
76         print "set default cache size to %d pages" % cacheVal
77
78     if resetFlags:
79         print "clearing read flags"
80         rds.resetFlags()
81
82     if dropIndex:
83         try:
84             rds.dropIndex()
85         except:
86             print "could not drop index"
87
88     if buildIndex:
89         print "building index...."
90         if cacheVal > 0:
91             rds.buildIndex(cacheVal)
92         else:
93             rds.buildIndex()
94
95     if doComplexity:
96         print "calculating uniq read complexity..."
97         uniqs = rds.getUniqsCount(distinct=False)
98         distincts = rds.getUniqsCount(distinct=True)
99         print "%d distincts / %d uniqs = %.2f" % (distincts, uniqs, float(distincts) / uniqs)
100
101     if len(propertyList) > 0:
102         rds.insertMetadata(propertyList)
103
104
105 if __name__ == "__main__":
106     main(sys.argv)