annotate SMART/Java/Python/GetReadDistribution.py @ 48:809ed01c8014

Deleted selected files
author m-zytnicki
date Mon, 09 Dec 2013 04:29:22 -0500
parents 2c0c0a89fad7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
2 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
4 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
10 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
16 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
27 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
30 #
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
31 import random, os, glob, subprocess
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
32 from commons.core.parsing.ParserChooser import ParserChooser
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
33 from commons.core.parsing.GffParser import GffParser
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
34 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.misc.Progress import Progress
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
36 from SMART.Java.Python.misc import Utils
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
37 from commons.core.LoggerFactory import LoggerFactory
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
38 from commons.core.utils.RepetOptionParser import RepetOptionParser
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
39
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
40 LOG_DEPTH = "smart"
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
41 DEFAULT_REGION = "_all_"
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
42 MULTIPLE_STR = {1: "", 1000: " (in kbp)", 1000000: " (in Gbp)"}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
43
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
44 class GetReadDistribution(object):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
45
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
46 def __init__(self, verbosity = 0):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
47 self.xLab = ""
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
48 self.yLab = "# reads"
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
49 self.verbosity = verbosity
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
50 self.number = random.randint(0, 100000)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
51 self.log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
52 self.parsers = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
53 self.distribution = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
54 self.factors = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
55 self.regions = None
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
56 self.tmpDatName = None
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
57 self.tmpRName = None
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
58 self.quorum = 1
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
59 self.strands = False
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
60 self.width = 800
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
61 self.height = 300
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
62 self.arial = False
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
63
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
64 def setNames(self, names):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
65 self.names = names
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
66
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
67 def setInputFiles(self, fileNames, format):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
68 chooser = ParserChooser(self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
69 chooser.findFormat(format)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
70 for cpt, fileName in enumerate(fileNames):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
71 self.parsers[self.names[cpt]] = chooser.getParser(fileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
72
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
73 def setOutputFileName(self, fileName):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
74 self.outputFileName = fileName
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
75
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
76 def setLabs(self, xLab, yLab):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
77 self.xLab = xLab
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
78 self.yLab = yLab
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
79
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
80 def setBinSize(self, binSize):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
81 self.binSize = binSize
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
82
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
83 def setColors(self, colors):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
84 self.colors = colors
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
85
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
86 def setFactors(self, factors):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
87 if factors == None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
88 self.factors = dict([name, 1.0] for name in self.names)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
89 else:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
90 self.factors = dict(zip(self.names, factors))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
91
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
92 def setMultiple(self, boolean):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
93 self.multiple = boolean
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
94
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
95 def setImageSize(self, width, height):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
96 if width != None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
97 self.width = width
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
98 if height != None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
99 self.height = height
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
100
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
101 def setQuorum(self, quorum):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
102 self.quorum = quorum
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
103
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
104 def setRegionsFile(self, fileName):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
105 if fileName != None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
106 self._loadRegions(fileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
107
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
108 def setBothStrands(self, strands):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
109 self.strands = strands
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
110
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
111 def setArial(self, arial):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
112 self.arial = arial
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
113
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
114 def _checkOptions(self):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
115 if not self.parsers:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
116 self.logAndRaise("ERROR: Missing input file names")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
117
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
118 def _logAndRaise(self, errorMsg):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
119 self.log.error(errorMsg)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
120 raise Exception(errorMsg)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
121
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
122 def _loadRegions(self, fileName):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
123 self.regions = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
124 parser = GffParser(fileName, self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
125 for transcript in parser.getIterator():
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
126 chromosome = transcript.getChromosome()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
127 start = transcript.getStart()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
128 end = transcript.getEnd()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
129 name = transcript.getName()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
130 if chromosome not in self.regions:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
131 self.regions[chromosome] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
132 if start not in self.regions[chromosome]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
133 self.regions[chromosome][start] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
134 if end not in self.regions[chromosome][start]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
135 self.regions[chromosome][start][end] = []
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
136 self.regions[chromosome][start][end].append(name)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
137
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
138 def _getRegions(self, transcript):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
139 if self.regions == None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
140 return [DEFAULT_REGION]
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
141 chromosome = transcript.getChromosome()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
142 start = transcript.getStart()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
143 end = transcript.getEnd()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
144 if chromosome not in self.regions:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
145 return []
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
146 names = []
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
147 for loadedStart in sorted(self.regions[chromosome].keys()):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
148 if loadedStart > end:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
149 return names
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
150 for loadedEnd in reversed(sorted(self.regions[chromosome][loadedStart].keys())):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
151 if loadedEnd < start:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
152 break
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
153 names.extend(self.regions[chromosome][loadedStart][loadedEnd])
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
154 return names
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
155
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
156 def _parse(self, name):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
157 progress = UnlimitedProgress(10000, "Reading file '%s'" % (name), self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
158 for transcript in self.parsers[name].getIterator():
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
159 if transcript.__class__.__name__ == "Mapping":
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
160 transcript = transcript.getTranscript()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
161 regions = self._getRegions(transcript)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
162 for region in regions:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
163 if region not in self.distribution:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
164 self.distribution[region] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
165 if name not in self.distribution[region]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
166 self.distribution[region][name] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
167 chromosome = transcript.getChromosome()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
168 nbElements = float(transcript.getTagValue("nbElements")) if "nbElements" in transcript.getTagNames() else 1
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
169 nbElements *= self.factors.get(name, 1)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
170 strand = transcript.getDirection() if self.strands else 1
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
171 if chromosome not in self.distribution[region][name]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
172 self.distribution[region][name][chromosome] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
173 if strand not in self.distribution[region][name][chromosome]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
174 self.distribution[region][name][chromosome][strand] = {}
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
175 previousBin = None
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
176 for exon in transcript.getExons():
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
177 for pos in range(exon.getStart(), exon.getEnd()+1):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
178 bin = pos / self.binSize
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
179 if bin != previousBin:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
180 self.distribution[region][name][chromosome][strand][bin] = self.distribution[region][name][chromosome][strand].get(bin, 0) + nbElements
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
181 previousBin = bin
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
182 progress.inc()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
183 progress.done()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
184
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
185 def _checkQuorum(self, region):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
186 if self.quorum == None:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
187 return True
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
188 return max([max([max([max(self.distribution[region][name][chromosome][strand].values()) for strand in self.distribution[region][name][chromosome]]) for chromosome in self.distribution[region][name]]) for name in self.distribution[region]])
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
189
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
190 def _writeData(self, region):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
191 self.tmpDatName = "tmpFile%d.dat" % (self.number)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
192 handle = open(self.tmpDatName, "w")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
193 handle.write("Chr\tPos\tStrand\tCount\tSample\n")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
194 for name in self.distribution[region]:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
195 for chromosome in sorted(self.distribution[region][name].keys()):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
196 for strand in sorted(self.distribution[region][name][chromosome].keys()):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
197 for pos in sorted(self.distribution[region][name][chromosome][strand].keys()):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
198 handle.write("%s\t%d\t%d\t%d\t\"%s\"\n" % (chromosome, pos * self.binSize, strand, self.distribution[region][name][chromosome][strand].get(pos, 0) * strand, name))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
199 handle.close()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
200
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
201 def _findMultiple(self, region):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
202 if not self.multiple:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
203 return 1
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
204 maxPosition = max([max([max([max(self.distribution[region][name][chromosome][strand].keys()) for strand in self.distribution[region][name][chromosome]]) for chromosome in self.distribution[region][name]]) for name in self.distribution[region]]) * self.binSize
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
205 if maxPosition > 2000000:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
206 return 1000000
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
207 elif maxPosition > 2000:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
208 return 1000
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
209 return 1
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
210
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
211 def _writeScript(self, region):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
212 self.tmpRName = "tmpFile%d.R" % (self.number)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
213 fileName = self.outputFileName if region == DEFAULT_REGION else "%s_%s.png" % (os.path.splitext(self.outputFileName)[0], region)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
214 colors = "scale_fill_brewer(palette=\"Set1\") + scale_color_brewer(palette=\"Set1\")" if self.colors == None else "scale_fill_manual(values = c(%s)) + scale_color_manual(values = c(%s))" % (", ".join(["\"%s\"" % (color) for color in self.colors]), ", ".join(["\"%s\"" % (color) for color in self.colors]))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
215 title = "" if region == DEFAULT_REGION else " + labs(title = \"Distribution of %s\") " % (region)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
216 facet = "Sample ~ Chr" if region == DEFAULT_REGION else "Sample ~ ."
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
217 handle = open(self.tmpRName, "w")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
218 multiple = self._findMultiple(region)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
219 arial = ", text = element_text(family=\"Arial\", size=20)" if self.arial else ""
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
220 if self.arial:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
221 handle.write("library(extrafont)\nloadfonts()\n")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
222 handle.write("library(ggplot2)\n")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
223 handle.write("data <- read.table(\"%s\", header = T)\n" % (self.tmpDatName))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
224 handle.write("data$Sample <- factor(data$Sample, levels=c(%s))\n" % (", ".join(["\"%s\"" % (name) for name in self.names])))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
225 handle.write("png(\"%s\", width = %d, height = %d)\n" % (fileName, self.width, self.height))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
226 handle.write("ggplot(data, aes(x = Pos/%d, y = Count, fill = Sample, color = Sample)) %s + geom_bar(stat = \"identity\") + facet_grid(%s, space=\"free\") + xlab(\"%s%s\") + ylab(\"%s\") + %s + theme(legend.position = \"none\", panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()%s)\n" % (multiple, title, facet, self.xLab, MULTIPLE_STR[multiple], self.yLab, colors, arial))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
227 handle.write("dev.off()\n")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
228
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
229 def _runR(self):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
230 rCommand = os.environ["SMARTRPATH"] if "SMARTRPATH" in os.environ else "R"
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
231 command = "\"%s\" CMD BATCH %s" % (rCommand, self.tmpRName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
232 status = subprocess.call(command, shell=True)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
233 if status != 0:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
234 raise Exception("Problem with the execution of script file %s, status is: %s" % (self.tmpRName, status))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
235
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
236 def _plot(self):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
237 progress = Progress(len(self.distribution), "Plotting data", self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
238 for region in self.distribution:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
239 if not self._checkQuorum(region):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
240 self.log.info("Not displaying '%s' for it contains insufficient data." % (region))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
241 else:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
242 self._writeData(region)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
243 self._writeScript(region)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
244 self._runR()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
245 progress.inc()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
246 progress.done()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
247
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
248 def _cleanFiles(self):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
249 for fileName in (self.tmpDatName, self.tmpRName):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
250 if fileName != None and os.path.exists(fileName):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
251 os.remove(fileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
252 for otherFileName in glob.glob("%s*" % (fileName)):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
253 os.remove(otherFileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
254
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
255 def run(self):
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
256 LoggerFactory.setLevel(self.log, self.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
257 self._checkOptions()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
258 self.log.info("START Get Read Distribution")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
259 for name in self.names:
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
260 self._parse(name)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
261 self._plot()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
262 self._cleanFiles()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
263 self.log.info("END Get Read Distribution")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
264
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
265
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
266 if __name__ == "__main__":
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
267 description = "Usage: GetReadDistribution.py [options]\n\nGet Read Distribution v1.0.1: Get the distribution of a set of reads. [Category: Personal]\n"
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
268 epilog = ""
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
269 parser = RepetOptionParser(description = description, epilog = epilog)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
270 parser.add_option("-i", "--input", dest="inputFileNames", action="store", default=None, type="string", help="input files, separated by commas [compulsory] [format: string]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
271 parser.add_option("-f", "--format", dest="format", action="store", default=None, type="string", help="format of the input [compulsory] [format: transcript or sequence file format]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
272 parser.add_option("-n", "--names", dest="names", action="store", default=None, type="string", help="name of the input data, separated by commas [compulsory] [format: string]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
273 parser.add_option("-o", "--output", dest="outputFileName", action="store", default=None, type="string", help="output file [format: output file in PNG format]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
274 parser.add_option("-s", "--binSize", dest="binSize", action="store", default=10000, type="int", help="bin size [format: int] [default: 10000]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
275 parser.add_option("-l", "--xLabel", dest="xLab", action="store", default="", type="string", help="x-axis label name [format: string]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
276 parser.add_option("-L", "--yLabel", dest="yLab", action="store", default="# reads", type="string", help="y-axis label name [format: string] [default: Reads]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
277 parser.add_option("-c", "--colors", dest="colors", action="store", default=None, type="string", help="colors of the bars, separated by commas [format: string]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
278 parser.add_option("-a", "--factors", dest="factors", action="store", default=None, type="string", help="normalization factors, separated by commas [format: string]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
279 parser.add_option("-r", "--regions", dest="regionsFileName", action="store", default=None, type="string", help="regions to plot [format: transcript file in GFF format]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
280 parser.add_option("-2", "--strands", dest="strands", action="store_true", default=False, help="plot negative strands on the negative x-axis [format: boolean] [default: False]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
281 parser.add_option("-m", "--multiple", dest="multiple", action="store_true", default=False, help="use human readable genomic positions (k, G) [format: boolean] [default: False]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
282 parser.add_option("-q", "--quorum", dest="quorum", action="store", default=1, type="int", help="minimum number of intervals to plot a region [format: int] [default: 1]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
283 parser.add_option("-z", "--width", dest="width", action="store", default=800, type="int", help="width of the image [format: int] [default: 800]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
284 parser.add_option("-Z", "--height", dest="height", action="store", default=300, type="int", help="height of the image [format: int] [default: 300]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
285 parser.add_option("-A", "--arial", dest="arial", action="store_true", default=False, help="use Arial font [format: boolean] [default: false]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
286 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
287 options = parser.parse_args()[0]
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
288 iGetReadDistribution = GetReadDistribution(options.verbosity)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
289 iGetReadDistribution.setNames(options.names.split(","))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
290 iGetReadDistribution.setInputFiles(options.inputFileNames.split(","), options.format)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
291 iGetReadDistribution.setOutputFileName(options.outputFileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
292 iGetReadDistribution.setLabs(options.xLab, options.yLab)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
293 iGetReadDistribution.setBinSize(options.binSize)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
294 iGetReadDistribution.setColors(None if options.colors == None else options.colors.split(","))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
295 iGetReadDistribution.setFactors(None if options.factors == None else map(float, options.factors.split(",")))
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
296 iGetReadDistribution.setRegionsFile(options.regionsFileName)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
297 iGetReadDistribution.setMultiple(options.multiple)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
298 iGetReadDistribution.setQuorum(options.quorum)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
299 iGetReadDistribution.setImageSize(options.width, options.height)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
300 iGetReadDistribution.setBothStrands(options.strands)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
301 iGetReadDistribution.setArial(options.arial)
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
302 iGetReadDistribution.run()
2c0c0a89fad7 Uploaded
m-zytnicki
parents:
diff changeset
303