annotate SMART/Java/Python/clusterizeBySlidingWindows.py @ 60:90f4b29d884f

Uploaded
author m-zytnicki
date Fri, 21 Feb 2014 08:32:36 -0500
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 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.writer.WriterChooser import WriterChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 Cluster the data into regions (defined by size and overlap with next region) and keep only highest peaks.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 import os, os.path
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.misc.RPlotter import RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 class ClusterizeBySlidingWindows(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 = 0):
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.strands = (0, )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.normalize = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.plot = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.excel = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.outputFileName = ''
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.defaultValue = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 pass
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def setInputFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self.parser = TranscriptContainer(fileName, format, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 def setOutputFileName(self, fileName, format="gff", title="S-MART", feature="transcript", featurePart="exon"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 writerChooser = WriterChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 writerChooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.writer = writerChooser.getWriter(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self.writer.setTitle(title)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.writer.setFeature(feature)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.writer.setFeaturePart(featurePart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 # self.outputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 # self.outputFormat = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def setWindowSize(self, size):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.size = size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 def setWindowOverlap(self, overlap):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.overlap = overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 def setTag(self, tag):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 self.tag = tag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 def setOperation(self, operation):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self.operation = operation
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 def setBothStrands(self, bothStrands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 if bothStrands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self.strands = (-1, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 def setNormalize(self, normalize):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self.normalize = normalize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 def setPlot(self, plot):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self.plot = plot
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 def setExcel(self, excel):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self.excel = excel
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 def setOutputTag(self, tag):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 self.outputTagName = tag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 def setDefaultValue(self, defaultValue):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 self.defaultValue = defaultValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def checkOptions(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 # if self.operation != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 # raise Exception("Trying to combine the values without specifying tag! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 if self.operation != None and self.operation not in ("sum", "avg", "med", "min", "max"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 raise Exception("Do not understand tag '%s'! Aborting..." % (self.operation))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 def getChromosomeSizes(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 self.sizes = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 progress = Progress(self.parser.getNbTranscripts(), "Getting sizes in genome", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 for transcript in self.parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.sizes[transcript.getChromosome()] = max(transcript.getStart(), self.sizes.get(transcript.getChromosome(), 0))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 def getBinsFromPos(self, pos):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 bin = (pos - 1) / (self.size - self.overlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 if bin >= 1 and pos <= bin * (self.size - self.overlap) + self.overlap:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 return (bin - 1, bin)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 return (bin, )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 def getPosFromBin(self, bin):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 return (bin * (self.size - self.overlap) + 1, bin * (self.size - self.overlap) + self.size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 def initializeBins(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 self.binsPerStrand = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 self.sumsPerStrand = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self.valuesPerStrand = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 self.toBePlottedPerStrand = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 self.binsPerStrand[strand] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.sumsPerStrand[strand] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 self.valuesPerStrand[strand] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self.toBePlottedPerStrand[strand] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 for chromosome in self.sizes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 binRange = range(self.getBinsFromPos(self.sizes[chromosome])[-1] + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 self.binsPerStrand[strand][chromosome] = dict([[i, 0] for i in binRange])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self.sumsPerStrand[strand][chromosome] = dict([[i, 0.0] for i in binRange])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 self.valuesPerStrand[strand][chromosome] = dict([[i, []] for i in binRange])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 self.toBePlottedPerStrand[strand][chromosome] = dict([[i, 0] for i in binRange])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 def getNbElements(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 nbOccurrences = 1 if "nbOccurrences" not in transcript.getTagNames() else transcript.getTagValue("nbOccurrences")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 nbElements = 1 if "nbElements" not in transcript.getTagNames() else transcript.getTagValue("nbElements")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 nbOccurrences = float(nbOccurrences)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 nbElements = float(nbElements)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 nbElements /= float(nbOccurrences)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 return nbElements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 def setBins(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 progress = Progress(self.parser.getNbTranscripts(), "Setting bins", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 for transcript in self.parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 nbElements = self.getNbElements(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 strand = transcript.getDirection() if len(self.strands) == 2 else 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 for bin in self.getBinsFromPos(transcript.getStart()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self.binsPerStrand[strand][transcript.getChromosome()][bin] += nbElements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 if self.tag != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 if self.tag not in transcript.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 if self.defaultValue is None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 raise Exception("Tag %s undefined in transcript %s" % (self.tag, transcript))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 value = self.defaultValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 value = float(transcript.getTagValue(self.tag))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 self.sumsPerStrand[strand][transcript.getChromosome()][bin] += value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 self.valuesPerStrand[strand][transcript.getChromosome()][bin].append(value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 def aggregateData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 if self.operation == "sum":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 self.computeSumData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 elif self.operation == "avg":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 self.computeAvgData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 elif self.operation == "med":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 self.computeMedData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 elif self.operation == "min":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 self.computeMinData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 elif self.operation == "max":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.computeMaxData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 elif self.operation == "GCpercent":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 self.computeGCPercent()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 self.toBePlottedPerStrand = self.binsPerStrand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 def computeSumData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 self.toBePlottedPerStrand = self.sumsPerStrand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 def computeAvgData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 for chromosome in self.binsPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 for bin in self.binsPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 if self.binsPerStrand[strand][chromosome][bin] != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 self.toBePlottedPerStrand[strand][chromosome][bin] = float(self.sumsPerStrand[strand][chromosome][bin]) / self.binsPerStrand[strand][chromosome][bin]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 def computeMedData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 for chromosome in self.binsPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 for bin in self.binsPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 if self.valuesPerStrand[strand][chromosome][bin]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 self.valuesPerStrand[strand][chromosome][bin].sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 size = len(self.valuesPerStrand[strand][chromosome][bin])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 if size % 2 == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self.toBePlottedPerStrand[strand][chromosome][bin] = self.valuesPerStrand[strand][chromosome][bin][(size - 1) / 2]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 self.toBePlottedPerStrand[strand][chromosome][bin] = (self.valuesPerStrand[strand][chromosome][bin][size / 2 - 1] + self.valuesPerStrand[strand][chromosome][bin][size / 2]) / 2.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 def computeMinData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 for chromosome in self.binsPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 for bin in self.binsPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 if self.valuesPerStrand[strand][chromosome][bin]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 self.toBePlottedPerStrand[strand][chromosome][bin] = min(self.valuesPerStrand[strand][chromosome][bin])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 def computeMaxData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 for chromosome in self.binsPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 for bin in self.binsPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 if self.valuesPerStrand[strand][chromosome][bin]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 self.toBePlottedPerStrand[strand][chromosome][bin] = max(self.valuesPerStrand[strand][chromosome][bin])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 def computeGCPercent(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 for chromosome in self.binsPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 for bin in self.binsPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 if self.valuesPerStrand[strand][chromosome][bin]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 subSequence = self.valuesPerStrand[strand][chromosome][bin]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 NPercent = 100 * (subSequence.countNt("N") / float(subSequence.getSize()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 if NPercent >= 50:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 currentGCpercent = "NA"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 currentGCpercent = subSequence.getGCpercentageInSequenceWithoutCountNInLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 self.toBePlottedPerStrand[strand][chromosome][bin] = currentGCpercent
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 #TODO: see if a map method could be used for the various "compute" methods
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 #return currentGCpercent, NPercent
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 def plotData(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 if self.plot != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 adjunct = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 if strand != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 adjunct = "Strand%d" % (strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 for chromosome in self.toBePlottedPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if len(self.toBePlottedPerStrand[strand][chromosome].keys()) > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 plotter = RPlotter(self.plot, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 plotter.setFill(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 plotter.addLine(self.toBePlottedPerStrand[strand][chromosome], chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 plotter.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 def writeExcel(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 if self.excel != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 excelFile = open(self.excel, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 maxBin = max([max(self.toBePlottedPerStrand[strand][chromosome].keys()) for chromosome in self.binsPerStrand[strand]])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 for bin in range(0, maxBin + 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 excelFile.write(",%d-%d" % self.getPosFromBin(bin))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 excelFile.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 for chromosome in self.toBePlottedPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 excelFile.write("%s" % (chromosome))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 for bin in self.toBePlottedPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 excelFile.write(",%f" % (self.toBePlottedPerStrand[strand][chromosome][bin]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 excelFile.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 excelFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 def printRegions(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 cpt = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 tagOp = "nb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 tagName = "Elements"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 outputTagName = "nbElements"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 if self.operation != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 tagOp = self.operation.lower()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 if self.tag != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 tagName = self.tag.title()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 if self.outputTagName != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 outputTagName = self.outputTagName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 #writer = Gff3Writer(self.outputFileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 for strand in self.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 for chromosome in self.toBePlottedPerStrand[strand]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 for bin in self.toBePlottedPerStrand[strand][chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 transcript.setName("region%d" % cpt)
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(self.getPosFromBin(bin)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 transcript.setEnd(self.getPosFromBin(bin)[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 transcript.setDirection(1 if strand == 0 else strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 transcript.setTagValue(outputTagName, self.binsPerStrand[strand][chromosome][bin])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 transcript.setTagValue("%s%s" % (tagOp, tagName), str(self.toBePlottedPerStrand[strand][chromosome][bin]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 self.writer.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 self.writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 self.checkOptions()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 self.getChromosomeSizes()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 self.initializeBins()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 self.setBins()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 self.aggregateData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 if self.excel:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 self.writeExcel()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 if self.plot:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 self.plotData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 self.printRegions()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 description = "Clusterize by Sliding Windows v1.0.1: Produces a GFF3 file that clusters a list of transcripts using a sliding window. [Category: Sliding Windows]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 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
316 parser.add_option("-f", "--inputFormat", dest="inputFormat", 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
317 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in transcript format given by -u]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 parser.add_option("-u", "--outputFormat", dest="outputFormat", action="store", default="gff", type="string", help="format of the output file [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 parser.add_option("-s", "--size", dest="size", action="store", type="int", help="size of the regions [compulsory] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 parser.add_option("-e", "--overlap", dest="overlap", action="store", type="int", help="overlap between two consecutive regions [compulsory] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 parser.add_option("-m", "--normalize", dest="normalize", action="store_true", default=False, help="normalize the number of reads per cluster by the number of mappings per read [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 parser.add_option("-g", "--tag", dest="tag", action="store", default=None, type="string", help="use a given tag as input (instead of summing number of features) [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 parser.add_option("-r", "--operation", dest="operation", action="store", default=None, type="string", help="combine tag value with given operation [format: choice (sum, avg, med, min, max)]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 parser.add_option("-d", "--defaultValue",dest="defaultValue", action="store", type="float", help="default value for input tag [format: float]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 parser.add_option("-w", "--write", dest="writeTag", action="store", default=None, type="string", help="print the result in the given tag (default usually is 'nbElements') [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 parser.add_option("-2", "--strands", dest="strands", action="store_true", default=False, help="consider the two strands separately [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 parser.add_option("-p", "--plot", dest="plot", action="store", default=None, type="string", help="plot regions to the given file [format: output file in PNG format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 parser.add_option("-x", "--excel", dest="excel", action="store", default=None, type="string", help="write an Excel file to the given file [format: output file in Excel format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int] [default: 1]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 cbsw = ClusterizeBySlidingWindows(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 cbsw.setInputFile(options.inputFileName, options.inputFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 cbsw.setOutputFileName(options.outputFileName, options.outputFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 cbsw.setWindowSize(options.size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 cbsw.setWindowOverlap(options.overlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 cbsw.setTag(options.tag)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 cbsw.setDefaultValue(options.defaultValue)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 cbsw.setOperation(options.operation)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 cbsw.setOutputTag(options.writeTag)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 cbsw.setBothStrands(options.strands)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 cbsw.setPlot(options.plot)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 cbsw.setExcel(options.excel)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 cbsw.run()