annotate SMART/Java/Python/GetDistribution.py @ 13:03045debed6e

Uploaded
author m-zytnicki
date Wed, 17 Apr 2013 10:39:35 -0400
parents 769e306b7933
children 94ab73e8a190
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-2012
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 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.FastaParser import FastaParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.writer.Gff3Writer import Gff3Writer
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.MultipleRPlotter import MultipleRPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 TWOSTRANDS = {True: [1, -1], False: [0]}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 STRANDTOSTR = {1: "(+)", -1: "(-)", 0: ""}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 class GetDistribution(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.sizes = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.twoStrands = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.start = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.names = ["nbElements"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.average = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.nbValues = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.height = 300
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.width = 600
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.colors = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self.gffFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self.csvFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self.yMin = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.yMax = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self.chromosome = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.merge = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.nbTranscripts = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 def setInputFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 chooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self.parser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def setReferenceFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 if fileName == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 fastaParser = FastaParser(fileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.chromosomes = fastaParser.getRegions()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.sizes = dict([region, fastaParser.getSizeOfRegion(region)] for region in self.chromosomes)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.maxSize = max(self.sizes.values())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def setRegion(self, chromosome, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if chromosome == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self.maxSize = options.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self.sizes = {chromosome: end}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.chromosomes = [chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 self.chromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self.start = start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 self.end = end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 def setOutputFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.outputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 def setNbBins(self, nbBins):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self.nbBins = nbBins
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def set2Strands(self, twoStrands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self.twoStrands = twoStrands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 def setNames(self, names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 self.names = names
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 def setAverage(self, average):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 self.average = average
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 def setNormalization(self, normalization):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 self.normalization = normalization
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 def setImageSize(self, height, width):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 self.height = height
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 self.width = width
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 def setYLimits(self, yMin, yMax):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self.yMin = yMin
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.yMax = yMax
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 def setColors(self, colors):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.colors = colors
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 def writeGff(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 self.gffFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 def writeCsv(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self.csvFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def mergePlots(self, merge):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 self.merge = merge
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 def _estimateSizes(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 progress = UnlimitedProgress(10000, "Reading input for chromosome size estimate", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self.sizes = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 for self.nbTranscripts, transcript in enumerate(self.parser.getIterator()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.sizes[chromosome] = max(start, self.sizes.get(chromosome, 0))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 def _computeSliceSize(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 if self.nbBins == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 tmp1 = int(max(self.sizes.values()) / float(self.nbBins))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 tmp2 = 10 ** (len("%d" % (tmp1))-2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 self.sliceSize = max(1, int((tmp1 / tmp2) * tmp2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 if self.verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 print "choosing bin size of %d" % (self.sliceSize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 def _initBins(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 self.bins = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 for chromosome in self.sizes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 self.bins[chromosome] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 for name in self.names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 self.bins[chromosome][name] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 for strand in TWOSTRANDS[self.twoStrands]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 if self.nbBins == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 self.bins[chromosome][name][strand] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 else:
769e306b7933 Change the repository level.
yufei-luo
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)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 def _populateBins(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 if self.nbTranscripts == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 progress = UnlimitedProgress(10000, "Counting data", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 progress = Progress(self.nbTranscripts, "Counting data", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 for transcript in self.parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 if transcript.__class__.__name__ == "Mapping":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 transcript = transcript.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 if self.chromosome and (chromosome != self.chromosome or start < self.start or start > self.end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 strand = transcript.getDirection() if self.twoStrands else 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 if self.nbBins != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 bin = (start / self.sliceSize) * self.sliceSize + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 bin = start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 for name in self.names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 value = float(transcript.tags.get(name, 1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 self.bins[chromosome][name][strand][bin] = self.bins[chromosome][name][strand].get(bin, 0) + value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 self.nbValues[name] = self.nbValues.get(name, 0) + value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 def _normalize(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 average = float(sum(self.nbValues)) / len(self.nbValues.keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 factors = dict([name, float(average) / self.nbValues[name]] for name in self.nbValues)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 for chromosome in self.bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 for name in self.bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 for bin in self.bins[chromosome][name][strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 self.bins[chromosome][name][strand][bin] *= factors[name]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 def _computeAverage(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 for chromosome in self.bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 for name in self.bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 for bin in self.bins[chromosome][name][strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 self.bins[chromosome][name][strand][bin] = float(self.bins[chromosome][name][strand][bin]) / self.sliceSize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 def _getPlotter(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 plot = RPlotter("%s_%s.png" % (os.path.splitext(self.outputFileName)[0], chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 plot.setImageSize(self.width, self.height)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 if self.sizes[chromosome] <= 1000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 unit = "nt."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 ratio = 1.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 elif self.sizes[chromosome] <= 1000000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 unit = "kb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 ratio = 1000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 unit = "Mb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 ratio = 1000000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 if self.yMin != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 plot.setMinimumY(self.yMin)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 if self.yMax != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 plot.setMaximumY(self.yMax)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 plot.setXLabel("Position on %s (in %s)" % (chromosome.replace("_", " "), unit))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 plot.setLegend(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 for i, name in enumerate(self.bins[chromosome]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 fullName = "%s %s" % (name.replace("_", " ")[:6], STRANDTOSTR[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 factor = 1 if strand == 0 else strand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 correctedLine = dict([(key / ratio, value * factor) for key, value in self.bins[chromosome][name][strand].iteritems()])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 plot.addLine(correctedLine, fullName, self.colors[i] if self.colors else None)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 return plot
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 def _plot(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 if self.merge:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 multiplePlot = MultipleRPlotter(self.outputFileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 multiplePlot.setImageSize(self.width, self.height * len(self.bins.keys()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 progress = Progress(len(self.bins.keys()), "Plotting", options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 for chromosome in sorted(self.bins.keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 plot = self._getPlotter(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 if self.merge:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 multiplePlot.addPlot(plot)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 plot.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 if self.merge:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 multiplePlot.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 def _writeCsv(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 if self.verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 print "Writing CSV file..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 csvHandle = open(self.csvFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 csvHandle.write("chromosome;tag;strand")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if self.nbBins != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 xValues = range(self.start / self.sliceSize, max(self.sizes.values()) / self.sliceSize + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 for value in xValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 csvHandle.write(";%d-%d" % (value * self.sliceSize + 1, (value+1) * self.sliceSize))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 xValues = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 for chromosome in self.bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 for name in self.bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 for bin in self.bins[chromosome][name][strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 xValues.extend(self.bins[chromosome][name][strand].keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 xValues = sorted(list(set(xValues)))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 for value in xValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 csvHandle.write(";%d" % (value))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 for chromosome in self.bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 csvHandle.write("%s" % (chromosome))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 for name in self.bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 csvHandle.write(";%s" % (name))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 csvHandle.write(";%s" % (STRANDTOSTR[strand]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 for bin in xValues:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 csvHandle.write(";%.2f" % (self.bins[chromosome][name][strand].get(bin, 0)))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 csvHandle.write(";")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 csvHandle.write(";")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 csvHandle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 if self.verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 print "...done"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 def _writeGff(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 if self.verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 print "Writing GFF file..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 writer = Gff3Writer(self.gffFileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 cpt = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 for chromosome in self.bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 for name in self.bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 for strand in self.bins[chromosome][name]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 for bin in self.bins[chromosome][name][strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 transcript.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 transcript.setStart(bin)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 if self.nbBins > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 transcript.setEnd(bin + self.sliceSize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 transcript.setEnd(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 transcript.setDirection(1 if strand == 0 else strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 transcript.setTagValue("ID", "region%d" % (cpt))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 writer.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 if self.verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 print "...done"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 if self.sizes == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 self._estimateSizes()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 self._computeSliceSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 self._initBins()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 self._populateBins()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 if self.normalization:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 self._normalize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 if self.average:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 self._computeAverage()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 self._plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 if self.csvFileName != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 self._writeCsv()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 if self.gffFileName != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 self._writeGff()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 description = "Get Distribution v1.0.2: Get the distribution of the genomic coordinates on a genome. [Category: Visualization]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 parser.add_option("-c", "--chromosome", dest="chromosome", action="store", default=None, type="string", help="plot only a chromosome [format: string]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 gt = GetDistribution(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 gt.setInputFile(options.inputFileName, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 gt.setOutputFile(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 gt.setReferenceFile(options.referenceFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 gt.setNbBins(int(options.nbBins))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 gt.set2Strands(options.bothStrands)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 gt.setRegion(options.chromosome, options.start, options.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 gt.setNormalization(options.normalize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 gt.setAverage(options.average)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 gt.setYLimits(options.yMin, options.yMax)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 gt.writeCsv(options.csv)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 gt.writeGff(options.gff)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 gt.setImageSize(options.height, options.width)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 gt.setNames(options.names.split(","))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 gt.setColors(None if options.colors == None else options.colors.split(","))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 gt.setNormalization(options.normalize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 gt.mergePlots(options.mergePlots)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 gt.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362