annotate SMART/Java/Python/plotRepartition.py @ 9:1eb55963fe39

Updated CompareOverlappingSmall*.py
author m-zytnicki
date Thu, 14 Mar 2013 05:23:05 -0400
parents 769e306b7933
children
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 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 Plot the data from the data files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.parsing.GffParser import GffParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.misc.RPlotter import RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file name [compulsory] [format: file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in PNG format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 parser.add_option("-f", "--format", dest="format", action="store", default="png", type="string", help="format of the output file [format: string] [default: png]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 parser.add_option("-r", "--normalize", dest="normalize", action="store_true", default=False, help="normalize data (when panels are different) [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 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
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 strands = [1, -1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 strandToString = {1: "+", -1: "-"}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 names = [None] if options.names == None else options.names.split(",")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 maxs = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 nbElements = [0 for name in names]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 lines = [{} for i in range(len(names))]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 if options.colors == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 colors = [None for i in range(len(names))]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 colors = options.colors.split(",")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 parser = GffParser(options.inputFileName, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 progress = Progress(parser.getNbTranscripts(), "Reading %s" % (options.inputFileName), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 direction = transcript.getDirection()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 for i, name in enumerate(names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 if chromosome not in lines[i]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 lines[i][chromosome] = dict([(strand, {}) for strand in strands])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 if chromosome not in maxs:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 maxs[chromosome] = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 maxs[chromosome] = max(maxs[chromosome], start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 if start not in lines[i][chromosome][direction]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 lines[i][chromosome][direction][start] = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 thisNbElements = float(transcript.getTagValue(name)) if name != None and name in transcript.getTagNames() else 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 lines[i][chromosome][direction][start] += thisNbElements * direction
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 nbElements[i] += thisNbElements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 if options.normalize:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if options.verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 print "Normalizing..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 for i, linesPerCondition in enumerate(lines):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 for linesPerChromosome in linesPerCondition.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 for line in linesPerChromosome.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 for key, value in line.iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 line[key] = value / float(nbElements[i]) * max(nbElements)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if options.verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 print "... done."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 progress = Progress(len(maxs.keys()), "Plotting", options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 for chromosome in maxs:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 plot = RPlotter("%s%s.%s" % (options.outputFileName, chromosome.capitalize(), options.format), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 plot.setLog(options.log)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 plot.setImageSize(2000, 500)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 plot.setFormat(options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 if maxs[chromosome] <= 1000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 unit = "nt."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 ratio = 1.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 elif maxs[chromosome] <= 1000000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 unit = "kb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 ratio = 1000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 unit = "Mb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 ratio = 1000000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 plot.setXLabel("Position on %s (in %s)" % (chromosome.replace("_", " "), unit))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 plot.setYLabel("# reads")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 plot.setLegend(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 for i, name in enumerate(names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 correctedLine = dict([(key / ratio, value) for key, value in lines[i][chromosome][strand].iteritems()])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 if name != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 name = "%s (%s)" % (name.replace("_", " "), strandToString[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 plot.addLine(correctedLine, None, colors[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 plot.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128