| 38 | 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(): | 
| 46 | 137             newKey = min(top, int(math.floor((key - minValue) / float(maxValue - minValue) * self.nbBars)) * step + minValue) | 
| 38 | 138             values[newKey] += value | 
|  | 139         return values | 
|  | 140 | 
|  | 141     def parseFile(self): | 
|  | 142         line     = {} | 
|  | 143         heatLine = {} | 
|  | 144 | 
|  | 145         cpt = 1 | 
|  | 146         for transcript in self.parser.getIterator(): | 
|  | 147             x, y, z = self.getValues(transcript) | 
|  | 148             name = transcript.name | 
|  | 149             if name == "unnamed transcript": | 
|  | 150                 name = "transcript %d" % (cpt) | 
|  | 151                 cpt += 1 | 
|  | 152             if self.shape in ("points", "heatPoints"): | 
|  | 153                 line[name] = (x, y) | 
|  | 154             if self.shape == "heatPoints": | 
|  | 155                 heatLine[name] = z | 
|  | 156             if self.shape == "line": | 
|  | 157                 line[x] = y | 
|  | 158             if self.shape == "barplot": | 
|  | 159                 line[x] = line.get(x, 0) + 1 | 
|  | 160         if self.shape == "barplot": | 
|  | 161             line = self.correctPointsToBarplot(line) | 
|  | 162         self.plotter.setXLabel(self.x) | 
|  | 163         if self.y != None: | 
|  | 164             self.plotter.setYLabel(self.y) | 
|  | 165         else: | 
|  | 166             self.plotter.setYLabel("Count") | 
|  | 167         self.plotter.addLine(line) | 
|  | 168         if self.shape == "heatPoints": | 
|  | 169             self.plotter.addHeatLine(heatLine) | 
|  | 170         self.plotter.plot() | 
|  | 171 | 
|  | 172     def close(self): | 
|  | 173         if self.regression: | 
|  | 174             print self.plotter.getCorrelationData() | 
|  | 175         if self.shape == "points": | 
|  | 176             rho = self.plotter.getSpearmanRho() | 
|  | 177             if rho == None: | 
|  | 178                 print "Cannot compute Spearman rho." | 
|  | 179             else: | 
|  | 180                 print "Spearman rho: %f" % (rho) | 
|  | 181 | 
|  | 182     def run(self): | 
|  | 183         self.createPlotter() | 
|  | 184         self.parseFile() | 
|  | 185         self.close() | 
|  | 186 | 
|  | 187 | 
|  | 188 if __name__ == "__main__": | 
|  | 189 | 
|  | 190     # parse command line | 
|  | 191     description = "Plot v1.0.2: Plot some information from a list of transcripts. [Category: Visualization]" | 
|  | 192 | 
|  | 193     parser = OptionParser(description = description) | 
|  | 194     parser.add_option("-i", "--input",      dest="inputFileName",  action="store",                      type="string", help="input file [compulsory] [format: file in transcript format given by -f]") | 
|  | 195     parser.add_option("-f", "--format",     dest="format",         action="store",                      type="string", help="format of the input [compulsory] [format: transcript file format]") | 
|  | 196     parser.add_option("-x", "--x",          dest="x",              action="store",                      type="string", help="tag for the x value [format: string]") | 
|  | 197     parser.add_option("-y", "--y",          dest="y",              action="store",                      type="string", help="tag for the y value [format: string]") | 
|  | 198     parser.add_option("-z", "--z",          dest="z",              action="store",      default=None,   type="string", help="tag for the z value [format: string]") | 
|  | 199     parser.add_option("-X", "--xDefault",   dest="xDefault",       action="store",      default=None,   type="float",  help="value for x when tag is not present [format: float]") | 
|  | 200     parser.add_option("-Y", "--yDefault",   dest="yDefault",       action="store",      default=None,   type="float",  help="value for y when tag is not present [format: float]") | 
|  | 201     parser.add_option("-Z", "--zDefault",   dest="zDefault",       action="store",      default=None,   type="float",  help="value for z when tag is not present [format: float]") | 
|  | 202     parser.add_option("-o", "--output",     dest="outputFileName", action="store",                      type="string", help="output file names [format: output file in PNG format]") | 
|  | 203     parser.add_option("-s", "--shape",      dest="shape",          action="store", default="barplot",   type="string", help="shape of the plot [format: choice (barplot, line, points, heatPoints)]") | 
|  | 204     parser.add_option("-n", "--nbBars",     dest="nbBars",         action="store",      default=2,                type="int",    help="number of bars in barplot [format: int]") | 
|  | 205     parser.add_option("-k", "--keep",       dest="keep",           action="store_true", default=False,                 help="keep temporary files [format: bool]") | 
|  | 206     parser.add_option("-r", "--regression", dest="regression",     action="store_true", default=False,                 help="plot regression line (in 'points' format) [format: bool]") | 
|  | 207     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]") | 
|  | 208     parser.add_option("-v", "--verbosity",  dest="verbosity",      action="store",      default=1,      type="int",    help="trace level [format: int]") | 
|  | 209     (options, args) = parser.parse_args() | 
|  | 210 | 
|  | 211     plot = Plot(options.verbosity) | 
|  | 212     plot.setInputFileName(options.inputFileName, options.format) | 
|  | 213     plot.setOutputFileName(options.outputFileName) | 
|  | 214     plot.setXData(options.x, options.xDefault) | 
|  | 215     plot.setYData(options.y, options.yDefault) | 
|  | 216     plot.setZData(options.z, options.zDefault) | 
|  | 217     plot.setShape(options.shape) | 
|  | 218     plot.setNbBars(options.nbBars) | 
|  | 219     plot.setRegression(options.regression) | 
|  | 220     plot.setLog(options.log) | 
|  | 221     plot.keepTmpFiles(options.keep) | 
|  | 222     plot.run() | 
|  | 223 |