first pass cleanup of cistematic/genomes; change bamPreprocessing
[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 import ReadDataset
14 from commoncode import getConfigParser, getConfigBoolOption, getConfigIntOption
15
16 print "rdsmetadata: version 2.8"
17
18
19 def main(argv=None):
20     if not argv:
21         argv = sys.argv
22
23     usage = "usage: python %prog rdsfile [propertyName1::propertyValue1] ... [propertyNameN::propertyValueN] [options]"
24
25     parser = makeParser(usage)
26     (options, args) = parser.parse_args(argv[1:])
27
28     if len(args) < 1:
29         print usage
30         print "where the optional metadata name::value pairs are added to the existing dataset"
31         sys.exit(1)
32
33     datafile = args[0]
34
35     propertyList=[]
36     for arg in args:
37         if "::" in arg:
38             (pname, pvalue) = arg.strip().split("::")
39             print "adding %s : %s" % (pname, pvalue)
40             propertyList.append((pname, pvalue))
41
42     rdsmetadata(datafile, propertyList, options.cacheVal, options.buildIndex,
43                 options.dropIndex, options.doCount, options.doComplexity,
44                 options.resetFlags, options.rnaDataType, options.cachePages)
45
46
47 def makeParser(usage=""):
48     parser = optparse.OptionParser(usage=usage)
49     parser.add_option("--defaultcache", type="int", dest="cacheVal")
50     parser.add_option("--index", action="store_true", dest="buildIndex")
51     parser.add_option("--dropindex", action="store_true", dest="dropIndex")
52     parser.add_option("--nocount", action="store_false", dest="doCount")
53     parser.add_option("--complexity", action="store_true", dest="doComplexity")
54     parser.add_option("--reset", action="store_true", dest="resetFlags")
55     parser.add_option("--initrna", action="store_true", dest="rnaDataType")
56     parser.add_option("--cache", type="int", dest="cachePages")
57
58     configParser = getConfigParser()
59     section = "rdsmetadata"
60     cacheVal = getConfigIntOption(configParser, section, "cacheVal", 0)
61     buildIndex = getConfigBoolOption(configParser, section, "buildIndex", False)
62     dropIndex = getConfigBoolOption(configParser, section, "dropIndex", False)
63     doCount = getConfigBoolOption(configParser, section, "doCount", True)
64     doComplexity = getConfigBoolOption(configParser, section, "doComplexity", False)
65     resetFlags = getConfigBoolOption(configParser, section, "resetFlags", False)
66     rnaDataType = getConfigBoolOption(configParser, section, "rnaDataType", False)
67     cachePages = getConfigIntOption(configParser, section, "cachePages", -1)
68
69     parser.set_defaults(cacheVal=cacheVal, buildIndex=buildIndex, dropIndex=dropIndex, doCount=doCount,
70                         doComplexity=doComplexity, resetFlags=resetFlags, rnaDataType=rnaDataType,
71                         cachePages=cachePages)
72
73     return parser
74
75
76 def rdsmetadata(datafile, propertyList=[], cacheVal=0, buildIndex=False,
77                 dropIndex=False, doCount=True, doComplexity=False, resetFlags=False,
78                 rnaDataType=False, cachePages=-1):
79
80     doCache = False
81     if cachePages != -1:
82         doCache = True
83
84     if rnaDataType:
85         rds = ReadDataset.ReadDataset(datafile, initialize=True, datasetType="RNA", verbose=True, cache=doCache)
86     else:
87         rds = ReadDataset.ReadDataset(datafile, verbose=True, reportCount=doCount, cache=doCache)
88
89     if cachePages > rds.getDefaultCacheSize():
90         rds.setDBcache(cachePages)
91
92     if cacheVal > 0:
93         rds.setDBcache(cacheVal, default=True)
94         print "set default cache size to %d pages" % cacheVal
95
96     if resetFlags:
97         print "clearing read flags"
98         rds.resetFlags()
99
100     if dropIndex:
101         try:
102             rds.dropIndex()
103         except:
104             print "could not drop index"
105
106     if buildIndex:
107         print "building index...."
108         if cacheVal > 0:
109             rds.buildIndex(cacheVal)
110         else:
111             rds.buildIndex()
112
113     if doComplexity:
114         print "calculating uniq read complexity..."
115         uniqs = rds.getUniqsCount(distinct=False)
116         distincts = rds.getUniqsCount(distinct=True)
117         print "%d distincts / %d uniqs = %.2f" % (distincts, uniqs, float(distincts) / uniqs)
118
119     if len(propertyList) > 0:
120         rds.insertMetadata(propertyList)
121
122
123 if __name__ == "__main__":
124     main(sys.argv)