comparison SMART/Java/Python/plot.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents
children 169d364ddd91
comparison
equal deleted inserted replaced
37:d22fadc825e3 38:2c0c0a89fad7
1 #! /usr/bin/env python
2 #
3 # Copyright INRA-URGI 2009-2010
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30 #
31
32 """
33 Plot the data from the data files
34 """
35
36 import os, re, math
37 from optparse import OptionParser
38 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
39 from SMART.Java.Python.misc.RPlotter import RPlotter
40 from SMART.Java.Python.misc.Progress import Progress
41 from commons.core.utils.FileUtils import FileUtils
42
43 class Plot(object):
44
45 def __init__(self, verbosity):
46 self.verbosity = verbosity
47 self.keep = False
48
49 def keepTmpFiles(self, boolean):
50 self.keep = boolean
51
52 def setShape(self, shape):
53 self.shape = shape
54
55 def setInputFileName(self, fileName, format):
56 self.parser = TranscriptContainer(fileName, format, self.verbosity)
57
58 def setXData(self, tag, default):
59 self.x = tag
60 self.xDefault = default
61
62 def setYData(self, tag, default):
63 self.y = tag
64 self.yDefault = default
65
66 def setZData(self, tag, default):
67 self.z = tag
68 self.zDefault = default
69
70 def setNbBars(self, nbBars):
71 self.nbBars = nbBars
72
73 def setOutputFileName(self, fileName):
74 self.outputFileName = fileName
75
76 def setRegression(self, regression):
77 self.regression = regression
78
79 def setLog(self, log):
80 self.log = log
81
82 def createPlotter(self):
83 self.plotter = RPlotter(self.outputFileName, self.verbosity, self.keep)
84 if self.shape == "barplot":
85 self.plotter.setBarplot(True)
86 elif self.shape == "line":
87 pass
88 elif self.shape == "points":
89 self.plotter.setPoints(True)
90 elif self.shape == "heatPoints":
91 self.plotter.setHeatPoints(True)
92 else:
93 raise Exception("Do not understand shape '%s'\n" % (self.shape))
94
95 self.plotter.setLog(self.log)
96 self.plotter.setRegression(self.regression)
97
98 def getValues(self, transcript):
99 x = transcript.getTagValue(self.x)
100 y = None
101 z = None
102 if self.y != None:
103 y = transcript.getTagValue(self.y)
104 if self.z != None:
105 z = transcript.getTagValue(self.z)
106 if x == None:
107 if self.xDefault != None:
108 x = self.xDefault
109 else:
110 raise Exception("Error! Transcript %s do not have the x-tag %s\n" % (transcript, self.x))
111 if self.y != None:
112 if y == None:
113 if self.yDefault != None:
114 y = self.yDefault
115 else:
116 raise Exception("Error! Transcript %s do not have the y-tag %s\n" % (transcript, self.y))
117 if self.z != None:
118 if z == None:
119 if self.zDefault != None:
120 z = self.zDefault
121 else:
122 raise Exception("Error! Transcript %s do not have the z-tag %s\n" % (transcript, self.z))
123 x = float(x)
124 if self.y != None:
125 y = float(y)
126 if self.z != None:
127 z = float(z)
128 return (x, y, z)
129
130 def correctPointsToBarplot(self, line):
131 minValue = int(math.floor(min(line.keys())))
132 maxValue = int(math.ceil(max(line.keys())))
133 step = (maxValue - minValue) / self.nbBars
134 values = dict([i * step + minValue, 0] for i in range(0, self.nbBars))
135 top = (self.nbBars - 1) * step + minValue
136 for key, value in line.iteritems():
137 divisor = float(maxValue - minValue) * self.nbBars
138 tmpMinValue = top
139 if divisor != 0:
140 tmpMinValue = min(top, int(math.floor((key - minValue) / divisor)))
141 newKey = tmpMinValue * step + minValue
142 values[newKey] += value
143 return values
144
145 def parseFile(self):
146 line = {}
147 heatLine = {}
148
149 cpt = 1
150 for transcript in self.parser.getIterator():
151 x, y, z = self.getValues(transcript)
152 name = transcript.name
153 if name == "unnamed transcript":
154 name = "transcript %d" % (cpt)
155 cpt += 1
156 if self.shape in ("points", "heatPoints"):
157 line[name] = (x, y)
158 if self.shape == "heatPoints":
159 heatLine[name] = z
160 if self.shape == "line":
161 line[x] = y
162 if self.shape == "barplot":
163 line[x] = line.get(x, 0) + 1
164 if self.shape == "barplot":
165 line = self.correctPointsToBarplot(line)
166 self.plotter.setXLabel(self.x)
167 if self.y != None:
168 self.plotter.setYLabel(self.y)
169 else:
170 self.plotter.setYLabel("Count")
171 self.plotter.addLine(line)
172 if self.shape == "heatPoints":
173 self.plotter.addHeatLine(heatLine)
174 self.plotter.plot()
175
176 def close(self):
177 if self.regression:
178 print self.plotter.getCorrelationData()
179 if self.shape == "points":
180 rho = self.plotter.getSpearmanRho()
181 if rho == None:
182 print "Cannot compute Spearman rho."
183 else:
184 print "Spearman rho: %f" % (rho)
185
186 def run(self):
187 self.createPlotter()
188 self.parseFile()
189 self.close()
190
191
192 if __name__ == "__main__":
193
194 # parse command line
195 description = "Plot v1.0.2: Plot some information from a list of transcripts. [Category: Visualization]"
196
197 parser = OptionParser(description = description)
198 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]")
199 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the input [compulsory] [format: transcript file format]")
200 parser.add_option("-x", "--x", dest="x", action="store", type="string", help="tag for the x value [format: string]")
201 parser.add_option("-y", "--y", dest="y", action="store", type="string", help="tag for the y value [format: string]")
202 parser.add_option("-z", "--z", dest="z", action="store", default=None, type="string", help="tag for the z value [format: string]")
203 parser.add_option("-X", "--xDefault", dest="xDefault", action="store", default=None, type="float", help="value for x when tag is not present [format: float]")
204 parser.add_option("-Y", "--yDefault", dest="yDefault", action="store", default=None, type="float", help="value for y when tag is not present [format: float]")
205 parser.add_option("-Z", "--zDefault", dest="zDefault", action="store", default=None, type="float", help="value for z when tag is not present [format: float]")
206 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file names [format: output file in PNG format]")
207 parser.add_option("-s", "--shape", dest="shape", action="store", default="barplot", type="string", help="shape of the plot [format: choice (barplot, line, points, heatPoints)]")
208 parser.add_option("-n", "--nbBars", dest="nbBars", action="store", default=2, type="int", help="number of bars in barplot [format: int]")
209 parser.add_option("-k", "--keep", dest="keep", action="store_true", default=False, help="keep temporary files [format: bool]")
210 parser.add_option("-r", "--regression", dest="regression", action="store_true", default=False, help="plot regression line (in 'points' format) [format: bool]")
211 parser.add_option("-l", "--log", dest="log", action="store", default="y", type="string", help="use log on x- or y-axis (write 'x', 'y' or 'xy') [format: string]")
212 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
213 (options, args) = parser.parse_args()
214
215 plot = Plot(options.verbosity)
216 plot.setInputFileName(options.inputFileName, options.format)
217 plot.setOutputFileName(options.outputFileName)
218 plot.setXData(options.x, options.xDefault)
219 plot.setYData(options.y, options.yDefault)
220 plot.setZData(options.z, options.zDefault)
221 plot.setShape(options.shape)
222 plot.setNbBars(options.nbBars)
223 plot.setRegression(options.regression)
224 plot.setLog(options.log)
225 plot.keepTmpFiles(options.keep)
226 plot.run()
227