6
|
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 import os
|
|
35 from optparse import OptionParser
|
|
36 from commons.core.parsing.GffParser import GffParser
|
|
37 from SMART.Java.Python.misc.RPlotter import RPlotter
|
|
38 from SMART.Java.Python.misc.Progress import Progress
|
|
39
|
|
40
|
|
41 if __name__ == "__main__":
|
|
42
|
|
43 # parse command line
|
|
44 description = "Plot Repartition v1.0.1: Plot the repartition of different data on a whole genome. (This tool uses 1 input file only, the different values being stored in the tags. See documentation to know more about it.) [Category: Visualization]"
|
|
45
|
|
46 parser = OptionParser(description = description)
|
|
47 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file name [compulsory] [format: file in GFF3 format]")
|
|
48 parser.add_option("-n", "--names", dest="names", action="store", default=None, type="string", help="name for the tags (separated by commas and no space) [default: None] [format: string]")
|
|
49 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in PNG format]")
|
|
50 parser.add_option("-c", "--color", dest="colors", action="store", default=None, type="string", help="color of the lines (separated by commas and no space) [format: string]")
|
|
51 parser.add_option("-f", "--format", dest="format", action="store", default="png", type="string", help="format of the output file [format: string] [default: png]")
|
|
52 parser.add_option("-r", "--normalize", dest="normalize", action="store_true", default=False, help="normalize data (when panels are different) [format: bool] [default: false]")
|
|
53 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]")
|
|
54 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
|
|
55 parser.add_option("-D", "--directory", dest="working_Dir", action="store", default=os.getcwd(), type="string", help="the directory to store the results [format: directory]")
|
|
56 (options, args) = parser.parse_args()
|
|
57
|
|
58 strands = [1, -1]
|
|
59 strandToString = {1: "+", -1: "-"}
|
|
60 names = [None] if options.names == None else options.names.split(",")
|
|
61 maxs = {}
|
|
62 nbElements = [0 for name in names]
|
|
63 lines = [{} for i in range(len(names))]
|
|
64 if options.colors == None:
|
|
65 colors = [None for i in range(len(names))]
|
|
66 else:
|
|
67 colors = options.colors.split(",")
|
|
68
|
|
69 parser = GffParser(options.inputFileName, options.verbosity)
|
|
70 progress = Progress(parser.getNbTranscripts(), "Reading %s" % (options.inputFileName), options.verbosity)
|
|
71 for transcript in parser.getIterator():
|
|
72 chromosome = transcript.getChromosome()
|
|
73 direction = transcript.getDirection()
|
|
74 start = transcript.getStart()
|
|
75 for i, name in enumerate(names):
|
|
76 if chromosome not in lines[i]:
|
|
77 lines[i][chromosome] = dict([(strand, {}) for strand in strands])
|
|
78 if chromosome not in maxs:
|
|
79 maxs[chromosome] = transcript.getStart()
|
|
80 else:
|
|
81 maxs[chromosome] = max(maxs[chromosome], start)
|
|
82 if start not in lines[i][chromosome][direction]:
|
|
83 lines[i][chromosome][direction][start] = 0
|
|
84 thisNbElements = float(transcript.getTagValue(name)) if name != None and name in transcript.getTagNames() else 1
|
|
85 lines[i][chromosome][direction][start] += thisNbElements * direction
|
|
86 nbElements[i] += thisNbElements
|
|
87 progress.inc()
|
|
88 progress.done()
|
|
89
|
|
90 if options.normalize:
|
|
91 if options.verbosity >= 10:
|
|
92 print "Normalizing..."
|
|
93 for i, linesPerCondition in enumerate(lines):
|
|
94 for linesPerChromosome in linesPerCondition.values():
|
|
95 for line in linesPerChromosome.values():
|
|
96 for key, value in line.iteritems():
|
|
97 line[key] = value / float(nbElements[i]) * max(nbElements)
|
|
98 if options.verbosity >= 10:
|
|
99 print "... done."
|
|
100
|
|
101 progress = Progress(len(maxs.keys()), "Plotting", options.verbosity)
|
|
102 for chromosome in maxs:
|
|
103 plot = RPlotter("%s%s.%s" % (options.outputFileName, chromosome.capitalize(), options.format), options.verbosity)
|
|
104 plot.setLog(options.log)
|
|
105 plot.setImageSize(2000, 500)
|
|
106 plot.setFormat(options.format)
|
|
107 if maxs[chromosome] <= 1000:
|
|
108 unit = "nt."
|
|
109 ratio = 1.0
|
|
110 elif maxs[chromosome] <= 1000000:
|
|
111 unit = "kb"
|
|
112 ratio = 1000.0
|
|
113 else:
|
|
114 unit = "Mb"
|
|
115 ratio = 1000000.0
|
|
116 plot.setXLabel("Position on %s (in %s)" % (chromosome.replace("_", " "), unit))
|
|
117 plot.setYLabel("# reads")
|
|
118 plot.setLegend(True)
|
|
119 for i, name in enumerate(names):
|
|
120 for strand in strands:
|
|
121 correctedLine = dict([(key / ratio, value) for key, value in lines[i][chromosome][strand].iteritems()])
|
|
122 if name != None:
|
|
123 name = "%s (%s)" % (name.replace("_", " "), strandToString[strand])
|
|
124 plot.addLine(correctedLine, None, colors[i])
|
|
125 plot.plot()
|
|
126 progress.inc()
|
|
127 progress.done()
|
|
128
|