annotate SMART/Java/Python/CollapseReads.py @ 71:d96f6c9a39e0 draft default tip

Removed pyc files.
author m-zytnicki
date Thu, 07 Apr 2016 09:25:18 -0400
parents 44d5973c188c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import os
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 from optparse import OptionParser, OptionGroup
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 from commons.core.parsing.ParserChooser import ParserChooser
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 from commons.core.writer.Gff3Writer import Gff3Writer
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 from SMART.Java.Python.ncList.NCListFilePickle import NCListFileUnpickle
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37 from SMART.Java.Python.ncList.FileSorter import FileSorter
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 from SMART.Java.Python.misc.Progress import Progress
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41 class CollapseReads(object):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 Merge two reads if they have exactly the same genomic coordinates
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 def __init__(self, verbosity = 0):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 self.inputReader = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 self.outputWriter = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50 self.strands = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51 self.nbRead = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 self.nbWritten = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 self.nbMerges = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 self.splittedFileNames = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 def __del__(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 for fileName in self.splittedFileNames.values():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 os.remove(fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 def close(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 self.outputWriter.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 def setInputFile(self, fileName, format):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 parserChooser = ParserChooser(self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 parserChooser.findFormat(format)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 self.parser = parserChooser.getParser(fileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 self.sortedFileName = "%s_sorted.pkl" % (os.path.splitext(fileName)[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 def setOutputFile(self, fileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 self.outputWriter = Gff3Writer(fileName, self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 def getNbElements(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 return self.parser.getNbTranscripts()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 def _sortFile(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 fs = FileSorter(self.parser, self.verbosity-4)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 fs.perChromosome(True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78 fs.setOutputFileName(self.sortedFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 fs.sort()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 self.splittedFileNames = fs.getOutputFileNames()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 self.nbElementsPerChromosome = fs.getNbElementsPerChromosome()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 self.nbRead = fs.getNbElements()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 def _iterate(self, chromosome):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 progress = Progress(self.nbElementsPerChromosome[chromosome], "Checking chromosome %s" % (chromosome), self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 transcripts = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 parser = NCListFileUnpickle(self.splittedFileNames[chromosome], self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 for newTranscript in parser.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 newTranscripts = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 for oldTranscript in transcripts:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 if self._checkOverlap(newTranscript, oldTranscript):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 self._merge(newTranscript, oldTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 elif self._checkPassed(newTranscript, oldTranscript):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 self._write(oldTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 newTranscripts.append(oldTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 newTranscripts.append(newTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 transcripts = newTranscripts
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 for transcript in transcripts:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 self._write(transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 def _merge(self, transcript1, transcript2):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 self.nbMerges += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 transcript2.setDirection(transcript1.getDirection())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 transcript1.merge(transcript2)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 def _write(self, transcript):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 self.nbWritten += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 self.outputWriter.addTranscript(transcript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 def _checkOverlap(self, transcript1, transcript2):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 if transcript1.getStart() != transcript2.getStart() or transcript1.getEnd() != transcript2.getEnd():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 return False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 return (not self.strands or transcript1.getDirection() == transcript2.getDirection())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 def _checkPassed(self, transcript1, transcript2):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 return (transcript2.getStart() < transcript1.getStart())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 def collapseChromosome(self, chromosome):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 progress = Progress(table.getNbElements(), "Analysing chromosome %s" % (chromosome), self.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 command = "SELECT * FROM %s ORDER BY start ASC, end DESC" % (table.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 transcriptStart = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 transcriptEnd = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 transcriptDirection = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 currentTranscript = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 if self.strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 command += ", direction"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 for index, transcript in table.selectTranscripts(command, True):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 self.nbRead += 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 if not self.strands:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 transcript.setDirection("+")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 if transcriptStart != transcript.getStart() or transcriptEnd != transcript.getEnd() or transcriptDirection != transcript.getDirection():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 self.writeTranscript(currentTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 transcriptStart = transcript.getStart()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 transcriptEnd = transcript.getEnd()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 transcriptDirection = transcript.getDirection()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 currentTranscript = transcript
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 currentTranscript.setTagValue("nbElements", (currentTranscript.getTagValue("nbElements") + 1) if "nbElements" in currentTranscript.getTagNames() else 1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 progress.inc()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 self.writeTranscript(currentTranscript)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 progress.done()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 def collapse(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 self._sortFile()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 for chromosome in sorted(self.nbElementsPerChromosome.keys()):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 self._iterate(chromosome)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 self.outputWriter.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 if self.verbosity > 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 print "# reads read: %d" % (self.nbRead)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 print "# reads written: %d (%.2f%%)" % (self.nbWritten, float(self.nbWritten) / self.nbRead * 100)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 print "# reads merges: %d" % (self.nbMerges)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 if __name__ == "__main__":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 # parse command line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 description = "Collapse Reads v1.0.3: Merge two reads if they have exactly the same genomic coordinates. [Category: Merge]"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 parser = OptionParser(description = description)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in mapping format given by -f]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the file [compulsory] [format: mapping file format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in GFF3 format]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 parser.add_option("-s", "--strands", dest="strands", action="store_true", default=False, help="merge elements on 2 different strands [format: bool] [default: false]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 (options, args) = parser.parse_args()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 collapser = CollapseReads(options.verbosity)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 collapser.setInputFile(options.inputFileName, options.format)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 collapser.setOutputFile(options.outputFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 collapser.strands = not options.strands
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 collapser.collapse()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 collapser.close()