Mercurial > repos > yufei-luo > s_mart
comparison smart_toolShed/SMART/Java/Python/plotCsv.py @ 0:e0f8dcca02ed
Uploaded S-MART tool. A toolbox manages RNA-Seq and ChIP-Seq data.
| author | yufei-luo |
|---|---|
| date | Thu, 17 Jan 2013 10:52:14 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e0f8dcca02ed |
|---|---|
| 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 Plot the data from the data files | |
| 33 """ | |
| 34 | |
| 35 import os | |
| 36 import re | |
| 37 from optparse import OptionParser | |
| 38 from SMART.Java.Python.misc.RPlotter import * | |
| 39 from SMART.Java.Python.misc.Progress import * | |
| 40 | |
| 41 | |
| 42 def mergeData(line1, line2): | |
| 43 if line1.keys() != line2.keys(): | |
| 44 sys.exit("Error! Input files do not correspond to each other! Aborting...") | |
| 45 mergedData = {} | |
| 46 for key in line1: | |
| 47 mergedData[key] = (line1[key], line2[key]) | |
| 48 return mergedData | |
| 49 | |
| 50 | |
| 51 | |
| 52 if __name__ == "__main__": | |
| 53 | |
| 54 # parse command line | |
| 55 description = "Plot CSV v1.0.1: Plot the content of a CSV file. [Category: Personnal]" | |
| 56 | |
| 57 parser = OptionParser(description = description) | |
| 58 parser.add_option("-i", "--input", dest="inputFileNames", action="store", type="string", help="input file [compulsory] [format: file in CSV format]") | |
| 59 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in PNG format]") | |
| 60 parser.add_option("-s", "--shape", dest="shape", action="store", type="string", help="shape of the plot [format: choice (line, bar, points, heatPoints)]") | |
| 61 parser.add_option("-l", "--log", dest="log", action="store", default="", type="string", help="use log on x- or y-axis (write 'x', 'y' or 'xy') [format: string] [default: ]") | |
| 62 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]") | |
| 63 (options, args) = parser.parse_args() | |
| 64 | |
| 65 plotter = RPlotter(options.outputFileName, options.verbosity) | |
| 66 if options.shape == "bar": | |
| 67 plotter.setBarplot(True) | |
| 68 elif options.shape == "points": | |
| 69 plotter.setPoints(True) | |
| 70 elif options.shape == "heatPoints": | |
| 71 plotter.setHeatPoints(True) | |
| 72 | |
| 73 plotter.setLog(options.log) | |
| 74 | |
| 75 lines = [] | |
| 76 nbsColumns = [] | |
| 77 for inputFileName in options.inputFileNames.split(","): | |
| 78 inputFile = open(inputFileName) | |
| 79 line = {} | |
| 80 nbColumns = None | |
| 81 | |
| 82 for point in inputFile: | |
| 83 point = point.strip() | |
| 84 | |
| 85 m = re.search(r"^\s*(\S+)\s+(\d+\.?\d*)\s+(\d+\.?\d*)\s*$", point) | |
| 86 if m != None: | |
| 87 line[m.group(1)] = (float(m.group(2)), float(m.group(3))) | |
| 88 if nbColumns == None: | |
| 89 nbColumns = 3 | |
| 90 nbsColumns.append(nbColumns) | |
| 91 elif nbColumns != 3: | |
| 92 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName)) | |
| 93 else: | |
| 94 m = re.search(r"^\s*(\d+\.?\d*)\s+(\d+\.?\d*)\s*$", point) | |
| 95 if m != None: | |
| 96 line[float(m.group(1))] = float(m.group(2)) | |
| 97 if nbColumns == None: | |
| 98 nbColumns = 2 | |
| 99 nbsColumns.append(nbColumns) | |
| 100 if nbColumns != 2: | |
| 101 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName)) | |
| 102 else: | |
| 103 m = re.search(r"^\s*(\S+)\s+(\d+\.?\d*)\s*$", point) | |
| 104 if m != None: | |
| 105 line[m.group(1)] = float(m.group(2)) | |
| 106 if nbColumns == None: | |
| 107 nbColumns = 1 | |
| 108 nbsColumns.append(nbColumns) | |
| 109 if nbColumns != 1: | |
| 110 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName)) | |
| 111 else: | |
| 112 sys.exit("Do not understand line '%s' of file '%s'! Aborting..." % (point, inputFileName)) | |
| 113 | |
| 114 lines.append(line) | |
| 115 | |
| 116 if len(lines) != len(nbsColumns): | |
| 117 sys.exit("Something is wrong in the input files! Aborting...") | |
| 118 | |
| 119 if options.shape == "bar": | |
| 120 if len(lines) != 1: | |
| 121 sys.exit("Error! Bar plot should have exactly one input file! Aborting...") | |
| 122 if nbsColumns[0] != 2: | |
| 123 sys.exit("Error! Bar plot input file should have exactly two columns! Aborting...") | |
| 124 plotter.addLine(lines[0]) | |
| 125 elif options.shape == "points": | |
| 126 if len(lines) != 2: | |
| 127 sys.exit("Error! Points cloud should have exactly two input file! Aborting...") | |
| 128 if nbsColumns[0] != 2 or nbsColumns[1] != 2: | |
| 129 sys.exit("Error! Points cloud plot input file should have exactly two columns! Aborting...") | |
| 130 plotter.addLine(mergedData(lines[0], lines[1])) | |
| 131 elif options.shape == "heatPoints": | |
| 132 if len(lines) != 3: | |
| 133 sys.exit("Error! Heat points cloud should have exactly three input file! Aborting...") | |
| 134 plotter.addLine(mergeData(lines[0], lines[1])) | |
| 135 plotter.addHeatLine(lines[2]) | |
| 136 elif options.shape == "line": | |
| 137 for i in range(0, len(lines)): | |
| 138 if (nbsColumns[i] != 2): | |
| 139 sys.exit("Error! Curve plot input file should have exactly two columns! Aborting...") | |
| 140 plotter.addLine(lines[i]) | |
| 141 else: | |
| 142 sys.exit("Do not understand shape '%s'" % (options.shape)) | |
| 143 | |
| 144 | |
| 145 plotter.plot() | |
| 146 |
