annotate SMART/Java/Python/GetReadSizes.py @ 16:6135c3075bc5

Deleted selected files
author m-zytnicki
date Mon, 22 Apr 2013 11:09:41 -0400
parents 769e306b7933
children 94ab73e8a190
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import random, os, glob, subprocess
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.GffParser import GffParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.LoggerFactory import LoggerFactory
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from commons.core.utils.RepetOptionParser import RepetOptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 LOG_DEPTH = "smart"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 DEFAULT_REGION = "_all_"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 class GetReadSizes(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.xLab = "Size"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.yLab = "# reads"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.number = random.randint(0, 100000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.parsers = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.sizes = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.factors = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.regions = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.tmpDatName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.tmpRName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.width = 800
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self.height = 300
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 def setNames(self, names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.names = names
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 def setInputFiles(self, fileNames, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 chooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 for cpt, fileName in enumerate(fileNames):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.parsers[self.names[cpt]] = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def setOutputFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.outputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def setLabs(self, xLab, yLab):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.xLab = xLab
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.yLab = yLab
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 def setSizes(self, minSize, maxSize):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.minSize = minSize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self.maxSize = maxSize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setColors(self, colors):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.colors = colors
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def setFactors(self, factors):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.factors = dict(zip(self.names, factors))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def setRegionsFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 if fileName != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self._loadRegions(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 def setImageSize(self, width, height):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if width != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self.width = width
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 if height != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 self.height = height
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 def _checkOptions(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if not self.parsers:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 self.logAndRaise("ERROR: Missing input file names")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 def _logAndRaise(self, errorMsg):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 self.log.error(errorMsg)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 raise Exception(errorMsg)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 def _loadRegions(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 self.regions = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 parser = GffParser(fileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 end = transcript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 name = transcript.getName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 if chromosome not in self.regions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.regions[chromosome] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 if start not in self.regions[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self.regions[chromosome][start] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 if end not in self.regions[chromosome][start]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 self.regions[chromosome][start][end] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 self.regions[chromosome][start][end].append(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 def _getRegions(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 if self.regions == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 return [DEFAULT_REGION]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 end = transcript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if chromosome not in self.regions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 return []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 names = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 for loadedStart in sorted(self.regions[chromosome].keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 if loadedStart > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 return names
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 for loadedEnd in reversed(sorted(self.regions[chromosome][loadedStart].keys())):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 if loadedEnd < start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 names.extend(self.regions[chromosome][loadedStart][loadedEnd])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 return names
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def _parse(self, name):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 progress = UnlimitedProgress(10000, "Reading file '%s'" % (name), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 for transcript in self.parsers[name].getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 if transcript.__class__.__name__ == "Mapping":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 transcript = transcript.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 regions = self._getRegions(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 for region in regions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if region not in self.sizes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 self.sizes[region] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 if name not in self.sizes[region]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 self.sizes[region][name] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 size = transcript.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 if (self.minSize == None or size >= self.minSize) and (self.maxSize == None or size <= self.maxSize):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 nbElements = float(transcript.getTagValue("nbElements")) if "nbElements" in transcript.getTagNames() else 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 nbElements *= self.factors.get(name, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 self.sizes[region][name][size] = self.sizes[region][name].get(size, 0) + nbElements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 if self.minSize == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self.minSize = min([min(self.sizes[region][name].keys()) for name in self.names for region in region])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 if self.maxSize == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 self.maxSize = max([max(self.sizes[region][name].keys()) for name in self.names for region in region])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 def _checkQuorum(self, region):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 return (max([sum(self.sizes[region][name].values()) for name in self.sizes[region]]) > 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 def _writeData(self, region):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 self.tmpDatName = "tmpFile%d.dat" % (self.number)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 handle = open(self.tmpDatName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 handle.write("Size\tCount\tSample\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 for name in self.sizes[region]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 for size in sorted(self.sizes[region][name].keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 handle.write("%d\t%d\t\"%s\"\n" % (size, self.sizes[region][name].get(size, 0), name))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 def _writeScript(self, region):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 self.tmpRName = "tmpFile%d.R" % (self.number)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 fileName = self.outputFileName if region == DEFAULT_REGION else "%s_%s.png" % (os.path.splitext(self.outputFileName)[0], region)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 colors = "scale_fill_brewer(palette=\"Set1\")" if self.colors == None else "scale_fill_manual(values = c(%s))" % (", ".join(["\"%s\"" % (color) for color in self.colors]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 title = "" if region == DEFAULT_REGION else " of %s" % (region)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 handle = open(self.tmpRName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 handle.write("library(ggplot2)\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 handle.write("data <- read.table(\"%s\", header = T)\n" % (self.tmpDatName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 handle.write("data$Sample <- factor(data$Sample, levels=c(%s))\n" % (", ".join(["\"%s\"" % (name) for name in self.names])))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 handle.write("data$Size <- factor(data$Size, levels=c(%s))\n" % (", ".join(["%d" % (size) for size in range(self.minSize, self.maxSize+1)])))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 handle.write("png(\"%s\", width = %d, height = %d)\n" % (fileName, self.width, self.height))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 handle.write("ggplot(data, aes(x = Size, y = Count, fill = Size)) + opts(title = \"Size distribution%s\") + geom_bar(stat = \"identity\") + facet_grid(. ~ Sample, space=\"free_x\") + xlab(\"%s\") + ylab(\"%s\") + %s + opts(legend.position = \"none\", panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), panel.background = theme_blank())\n" % (title, self.xLab, self.yLab, colors))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 handle.write("dev.off()\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 def _runR(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 rCommand = "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 if "SMARTRPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 rCommand = os.environ["SMARTRPATH"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 command = "\"%s\" CMD BATCH %s" % (rCommand, self.tmpRName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 status = subprocess.call(command, shell=True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 if status != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 raise Exception("Problem with the execution of script file %s, status is: %s" % (self.tmpRName, status))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 def _plot(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 progress = Progress(len(self.sizes), "Plotting data", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 for region in self.sizes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 if not self._checkQuorum(region):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 self.log.info("Not displaying '%s' for it contains no data." % (region))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 self._writeData(region)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 self._writeScript(region)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self._runR()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 def _cleanFiles(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 for fileName in (self.tmpDatName, self.tmpRName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 if fileName != None and os.path.exists(fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 for otherFileName in glob.glob("%s*" % (fileName)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 os.remove(otherFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 LoggerFactory.setLevel(self.log, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 self._checkOptions()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 self.log.info("START Get Read Sizes")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 for name in self.names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 self._parse(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 self._plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 self._cleanFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 self.log.info("END Get Read Sizes")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 description = "Usage: GetReadSizes.py [options]\n\nGet Read Sizes v1.0.1: Get the sizes of a set of reads. [Category: Personal]\n"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 epilog = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 parser = RepetOptionParser(description = description, epilog = epilog)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 parser.add_option("-i", "--input", dest="inputFileNames", action="store", default=None, type="string", help="input files, separated by commas [compulsory] [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 parser.add_option("-f", "--format", dest="format", action="store", default=None, type="string", help="format of the input [compulsory] [format: transcript or sequence file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 parser.add_option("-n", "--names", dest="names", action="store", default=None, type="string", help="name of the input data, separated by commas [compulsory] [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 parser.add_option("-o", "--output", dest="outputFileName", action="store", default=None, type="string", help="output file [format: output file in PNG format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 parser.add_option("-s", "--minSize", dest="minSize", action="store", default=None, type="int", help="minimum size [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 parser.add_option("-S", "--maxSize", dest="maxSize", action="store", default=None, type="int", help="maximum size [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 parser.add_option("-l", "--xLabel", dest="xLab", action="store", default="Size", type="string", help="x-axis label name [format: string] [default: Size]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 parser.add_option("-L", "--yLabel", dest="yLab", action="store", default="# reads", type="string", help="y-axis label name [format: string] [default: Reads]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 parser.add_option("-c", "--colors", dest="colors", action="store", default=None, type="string", help="colors of the bars, separated by commas [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 parser.add_option("-a", "--factors", dest="factors", action="store", default=None, type="string", help="normalization factors, separated by commas [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 parser.add_option("-r", "--regions", dest="regionsFileName", action="store", default=None, type="string", help="regions to plot [format: transcript file in GFF format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 parser.add_option("-z", "--width", dest="width", action="store", default=800, type="int", help="width of the image [format: int] [default: 800]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 parser.add_option("-Z", "--height", dest="height", action="store", default=300, type="int", help="height of the image [format: int] [default: 300]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 options = parser.parse_args()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 iGetReadSizes = GetReadSizes(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 iGetReadSizes.setNames(options.names.split(","))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 iGetReadSizes.setInputFiles(options.inputFileNames.split(","), options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 iGetReadSizes.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 iGetReadSizes.setLabs(options.xLab, options.yLab)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 iGetReadSizes.setSizes(options.minSize, options.maxSize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 iGetReadSizes.setColors(None if options.colors == None else options.colors.split(","))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 iGetReadSizes.setFactors(None if options.factors == None else map(float, options.factors.split(",")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 iGetReadSizes.setRegionsFile(options.regionsFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 iGetReadSizes.setImageSize(options.width, options.height)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 iGetReadSizes.run()