annotate SMART/Java/Python/GetDistribution.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents 44d5973c188c
children 169d364ddd91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2012
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import os
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 from optparse import OptionParser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 from commons.core.parsing.ParserChooser import ParserChooser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 from commons.core.parsing.FastaParser import FastaParser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 from commons.core.writer.Gff3Writer import Gff3Writer
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37 from SMART.Java.Python.misc.RPlotter import RPlotter
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 from SMART.Java.Python.misc.MultipleRPlotter import MultipleRPlotter
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40 from SMART.Java.Python.misc.Progress import Progress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 TWOSTRANDS = {True: [1, -1], False: [0]}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 STRANDTOSTR = {1: "(+)", -1: "(-)", 0: ""}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 class GetDistribution(object):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 def __init__(self, verbosity):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 self.sizes = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50 self.twoStrands = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51 self.start = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 self.names = ["nbElements"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 self.average = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 self.nbValues = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 self.height = 300
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 self.width = 600
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 self.colors = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 self.gffFileName = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 self.csvFileName = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 self.yMin = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 self.yMax = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62 self.chromosome = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 self.merge = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 self.nbTranscripts = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 def setInputFile(self, fileName, format):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 chooser = ParserChooser(self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 chooser.findFormat(format)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 self.parser = chooser.getParser(fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71 def setReferenceFile(self, fileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 if fileName == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 fastaParser = FastaParser(fileName, self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 self.chromosomes = fastaParser.getRegions()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 self.sizes = dict([region, fastaParser.getSizeOfRegion(region)] for region in self.chromosomes)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 self.maxSize = max(self.sizes.values())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 def setRegion(self, chromosome, start, end):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 if chromosome == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 self.maxSize = options.end
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 self.sizes = {chromosome: end}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 self.chromosomes = [chromosome]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 self.chromosome = chromosome
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 self.start = start
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 self.end = end
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 def setOutputFile(self, fileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 self.outputFileName = fileName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 def setNbBins(self, nbBins):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 self.nbBins = nbBins
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 def set2Strands(self, twoStrands):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 self.twoStrands = twoStrands
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 def setNames(self, names):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 self.names = names
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 def setAverage(self, average):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 self.average = average
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 def setNormalization(self, normalization):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 self.normalization = normalization
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 def setImageSize(self, height, width):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 self.height = height
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 self.width = width
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 def setYLimits(self, yMin, yMax):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 self.yMin = yMin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 self.yMax = yMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 def setColors(self, colors):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 self.colors = colors
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 def writeGff(self, fileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 self.gffFileName = fileName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 def writeCsv(self, fileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 self.csvFileName = fileName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 def mergePlots(self, merge):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 self.merge = merge
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 def _estimateSizes(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 progress = UnlimitedProgress(10000, "Reading input for chromosome size estimate", self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 self.sizes = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 for self.nbTranscripts, transcript in enumerate(self.parser.getIterator()):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 chromosome = transcript.getChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 start = transcript.getStart()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 self.sizes[chromosome] = max(start, self.sizes.get(chromosome, 0))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 def _computeSliceSize(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 if self.nbBins == 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 tmp1 = int(max(self.sizes.values()) / float(self.nbBins))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 tmp2 = 10 ** (len("%d" % (tmp1))-2)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 self.sliceSize = max(1, int((tmp1 / tmp2) * tmp2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 if self.verbosity > 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 print "choosing bin size of %d" % (self.sliceSize)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 def _initBins(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 self.bins = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 for chromosome in self.sizes:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 self.bins[chromosome] = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 for name in self.names:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 self.bins[chromosome][name] = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 for strand in TWOSTRANDS[self.twoStrands]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 if self.nbBins == 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 self.bins[chromosome][name][strand] = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 self.bins[chromosome][name][strand] = dict([(i * self.sliceSize + 1, 0.0) for i in range(self.start / self.sliceSize, self.sizes[chromosome] / self.sliceSize + 1)])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 def _populateBins(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 if self.nbTranscripts == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 progress = UnlimitedProgress(10000, "Counting data", self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 progress = Progress(self.nbTranscripts, "Counting data", self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 for transcript in self.parser.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 if transcript.__class__.__name__ == "Mapping":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 transcript = transcript.getTranscript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 chromosome = transcript.getChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 start = transcript.getStart()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 if self.chromosome and (chromosome != self.chromosome or start < self.start or start > self.end):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 strand = transcript.getDirection() if self.twoStrands else 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 if self.nbBins != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 bin = (start / self.sliceSize) * self.sliceSize + 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 bin = start
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176 for name in self.names:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177 value = float(transcript.tags.get(name, 1))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178 self.bins[chromosome][name][strand][bin] = self.bins[chromosome][name][strand].get(bin, 0) + value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179 self.nbValues[name] = self.nbValues.get(name, 0) + value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182 def _normalize(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183 average = float(sum(self.nbValues)) / len(self.nbValues.keys())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184 factors = dict([name, float(average) / self.nbValues[name]] for name in self.nbValues)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185 for chromosome in self.bins:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 for name in self.bins[chromosome]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 for bin in self.bins[chromosome][name][strand]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189 self.bins[chromosome][name][strand][bin] *= factors[name]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 def _computeAverage(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192 for chromosome in self.bins:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193 for name in self.bins[chromosome]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195 for bin in self.bins[chromosome][name][strand]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196 self.bins[chromosome][name][strand][bin] = float(self.bins[chromosome][name][strand][bin]) / self.sliceSize
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 def _getPlotter(self, chromosome):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 plot = RPlotter("%s_%s.png" % (os.path.splitext(self.outputFileName)[0], chromosome), self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 plot.setImageSize(self.width, self.height)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201 if self.sizes[chromosome] <= 1000:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202 unit = "nt."
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203 ratio = 1.0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204 elif self.sizes[chromosome] <= 1000000:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205 unit = "kb"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206 ratio = 1000.0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208 unit = "Mb"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209 ratio = 1000000.0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210 if self.yMin != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211 plot.setMinimumY(self.yMin)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212 if self.yMax != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213 plot.setMaximumY(self.yMax)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 plot.setXLabel("Position on %s (in %s)" % (chromosome.replace("_", " "), unit))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215 plot.setLegend(True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216 for i, name in enumerate(self.bins[chromosome]):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 fullName = "%s %s" % (name.replace("_", " ")[:6], STRANDTOSTR[strand])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 factor = 1 if strand == 0 else strand
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220 correctedLine = dict([(key / ratio, value * factor) for key, value in self.bins[chromosome][name][strand].iteritems()])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221 plot.addLine(correctedLine, fullName, self.colors[i] if self.colors else None)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222 return plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224 def _plot(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 if self.merge:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226 multiplePlot = MultipleRPlotter(self.outputFileName, self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227 multiplePlot.setImageSize(self.width, self.height * len(self.bins.keys()))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228 progress = Progress(len(self.bins.keys()), "Plotting", options.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 for chromosome in sorted(self.bins.keys()):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230 plot = self._getPlotter(chromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231 if self.merge:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
232 multiplePlot.addPlot(plot)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 plot.plot()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
235 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236 if self.merge:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 multiplePlot.plot()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240 def _writeCsv(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241 if self.verbosity > 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242 print "Writing CSV file..."
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243 csvHandle = open(self.csvFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244 csvHandle.write("chromosome;tag;strand")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 if self.nbBins != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 xValues = range(self.start / self.sliceSize, max(self.sizes.values()) / self.sliceSize + 1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 for value in xValues:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248 csvHandle.write(";%d-%d" % (value * self.sliceSize + 1, (value+1) * self.sliceSize))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249 csvHandle.write("\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251 xValues = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252 for chromosome in self.bins:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253 for name in self.bins[chromosome]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 for bin in self.bins[chromosome][name][strand]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256 xValues.extend(self.bins[chromosome][name][strand].keys())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
257 xValues = sorted(list(set(xValues)))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258 for value in xValues:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 csvHandle.write(";%d" % (value))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
260 csvHandle.write("\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261 for chromosome in self.bins:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262 csvHandle.write("%s" % (chromosome))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263 for name in self.bins[chromosome]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264 csvHandle.write(";%s" % (name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 csvHandle.write(";%s" % (STRANDTOSTR[strand]))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 for bin in xValues:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268 csvHandle.write(";%.2f" % (self.bins[chromosome][name][strand].get(bin, 0)))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269 csvHandle.write("\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 csvHandle.write(";")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271 csvHandle.write(";")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272 csvHandle.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 if self.verbosity > 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 print "...done"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 def _writeGff(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277 if self.verbosity > 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278 print "Writing GFF file..."
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279 writer = Gff3Writer(self.gffFileName, self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 cpt = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 for chromosome in self.bins:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 for name in self.bins[chromosome]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 for strand in self.bins[chromosome][name]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 for bin in self.bins[chromosome][name][strand]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 transcript = Transcript()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286 transcript.setChromosome(chromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
287 transcript.setStart(bin)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
288 if self.nbBins > 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
289 transcript.setEnd(bin + self.sliceSize)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 transcript.setEnd(self.start)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 transcript.setDirection(1 if strand == 0 else strand)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293 transcript.setTagValue("ID", "region%d" % (cpt))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294 cpt += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295 writer.write()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296 if self.verbosity > 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297 print "...done"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299 def run(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 if self.sizes == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301 self._estimateSizes()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
302 self._computeSliceSize()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303 self._initBins()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304 self._populateBins()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305 if self.normalization:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306 self._normalize()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307 if self.average:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 self._computeAverage()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 self._plot()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310 if self.csvFileName != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311 self._writeCsv()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 if self.gffFileName != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 self._writeGff()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 if __name__ == "__main__":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318 description = "Get Distribution v1.0.2: Get the distribution of the genomic coordinates on a genome. [Category: Visualization]"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 parser = OptionParser(description = description)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the input file [compulsory] [format: transcript file format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in GFF3 format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324 parser.add_option("-r", "--reference", dest="referenceFileName", action="store", default=None, type="string", help="file containing the genome [format: file in FASTA format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325 parser.add_option("-b", "--nbBins", dest="nbBins", action="store", default=1000, type="int", help="number of bins [default: 1000] [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326 parser.add_option("-2", "--bothStrands", dest="bothStrands", action="store_true", default=False, help="plot one curve per strand [format: bool] [default: false]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 parser.add_option("-c", "--chromosome", dest="chromosome", action="store", default=None, type="string", help="plot only a chromosome [format: string]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 parser.add_option("-s", "--start", dest="start", action="store", default=None, type="int", help="start from a given region [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 parser.add_option("-e", "--end", dest="end", action="store", default=None, type="int", help="end from a given region [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 parser.add_option("-y", "--yMin", dest="yMin", action="store", default=None, type="int", help="minimum value on the y-axis to plot [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 parser.add_option("-Y", "--yMax", dest="yMax", action="store", default=None, type="int", help="maximum value on the y-axis to plot [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332 parser.add_option("-x", "--csv", dest="csv", action="store", default=None, help="write a .csv file [format: output file in CSV format] [default: None]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333 parser.add_option("-g", "--gff", dest="gff", action="store", default=None, help="also write GFF3 file [format: output file in GFF format] [default: None]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
334 parser.add_option("-H", "--height", dest="height", action="store", default=300, type="int", help="height of the graphics [format: int] [default: 300]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
335 parser.add_option("-W", "--width", dest="width", action="store", default=600, type="int", help="width of the graphics [format: int] [default: 1000]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
336 parser.add_option("-a", "--average", dest="average", action="store_true", default=False, help="plot average (instead of sum) [default: false] [format: boolean]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
337 parser.add_option("-n", "--names", dest="names", action="store", default="nbElements", type="string", help="name for the tags (separated by commas and no space) [default: None] [format: string]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
338 parser.add_option("-l", "--color", dest="colors", action="store", default=None, type="string", help="color of the lines (separated by commas and no space) [format: string]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
339 parser.add_option("-z", "--normalize", dest="normalize", action="store_true", default=False, help="normalize data (when panels are different) [format: bool] [default: false]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
340 parser.add_option("-m", "--merge", dest="mergePlots", action="store_true", default=False, help="merge all plots in one figure [format: bool] [default: false]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
341 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
342 (options, args) = parser.parse_args()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
343
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
344 gt = GetDistribution(options.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
345 gt.setInputFile(options.inputFileName, options.format)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
346 gt.setOutputFile(options.outputFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
347 gt.setReferenceFile(options.referenceFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
348 gt.setNbBins(int(options.nbBins))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
349 gt.set2Strands(options.bothStrands)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
350 gt.setRegion(options.chromosome, options.start, options.end)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
351 gt.setNormalization(options.normalize)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
352 gt.setAverage(options.average)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
353 gt.setYLimits(options.yMin, options.yMax)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
354 gt.writeCsv(options.csv)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
355 gt.writeGff(options.gff)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
356 gt.setImageSize(options.height, options.width)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
357 gt.setNames(options.names.split(","))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
358 gt.setColors(None if options.colors == None else options.colors.split(","))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
359 gt.setNormalization(options.normalize)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
360 gt.mergePlots(options.mergePlots)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
361 gt.run()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
362