annotate SMART/Java/Python/ncList/FindOverlapsWithOneInterval.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import struct
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import math
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.ncList.NCList import NCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.ncList.FileSorter import FileSorter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 LONGSIZE = struct.calcsize('l')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 class FindOverlapsWithOneInterval(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self._sortedFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self._verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self._overlappingNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self._nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self._nbWritten = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 if self._sortedFileName and os.path.exists(self._sortedFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 os.remove(self._sortedFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._iWriter.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def setOutputFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self._iWriter = Gff3Writer(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 def setFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 chooser = ParserChooser(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self._parser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self._sortedFileName = "%s_sorted.pkl" % (os.path.splitext(fileName)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 def setInterval(self, chromosome, start, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self._chromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self._start = start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self._end = end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self._transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self._transcript.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self._transcript.setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self._transcript.setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._transcript.setDirection("+")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 if transcript.__class__.__name__ == "Mapping":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 transcript = transcript.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self._chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self._start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 self._end = transcript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self._transcript = transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 def prepareIntermediateFiles(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 fs = FileSorter(self._parser, self._verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 fs.selectChromosome(self._chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 fs.perChromosome(False)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 fs.setOutputFileName(self._sortedFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 fs.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 self._nbTotalLines = fs.getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self._nbLines = fs.getNbElementsPerChromosome()[self._chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 def createNCList(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 print "Creating NC-list..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 ncList = NCList(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 ncList.createIndex(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 ncList.setChromosome(self._chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 ncList.setFileName(self._sortedFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 ncList.setNbElements(self._nbTotalLines)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 ncList.buildLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self.setNCList(ncList, ncList.getIndex())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 print " ...done (%ds)" % (endTime - startTime)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def setNCList(self, ncList, index):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self._ncList = ncList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self._indix = index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 def binarySearch(self, cursor, startL, endL):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 if startL > endL:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 middleL = (startL + endL) / 2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 cursor.moveSibling(middleL)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 overlap = self.isOverlapping(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 if overlap == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 if middleL == startL:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 return cursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 return self.binarySearch(cursor, startL, middleL)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 if overlap == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 return self.binarySearch(cursor, middleL + 1, endL)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 return self.binarySearch(cursor, startL, middleL - 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def compare(self, cursor = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 self._ncList.openFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 if cursor == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 dump = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 cursor = NCListCursor(None, self._ncList, 0, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 cursor._getSiblingData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 cursor = self.binarySearch(cursor, cursor._firstSiblingLIndex, cursor._lastSiblingLIndex)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 if cursor == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 while not cursor.isOut() and self.isOverlapping(cursor) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self.write(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 newCursor = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 if newCursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 newCursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 self.compare(newCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 if cursor.isLast():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 cursor.moveRight()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 def isOverlapping(self, cursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 if self._end < cursor.getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 return 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 if self._start > cursor.getEnd():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 return -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 def write(self, cursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 self._nbOverlaps += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 refTranscript = cursor.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 self._overlappingNames.append(refTranscript.getName())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 def dumpWriter(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 if (not self._overlappingNames) or self._transcript == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 self._transcript.setTagValue("nbOverlaps", len(self._overlappingNames))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 self._transcript.setTagValue("overlapsWith", "--".join(self._overlappingNames))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 self._iWriter.addTranscript(self._transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 self._nbWritten += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 self._overlappingNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 self.prepareIntermediateFiles()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 self.createNCList()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 self.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 self.dumpWriter()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 self.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if self._verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 print "# refs: %d" % (self._nbLines)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 print "# written: %d (%d overlaps)" % (self._nbOverlappingQueries, self._nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 description = "FindOverlapsWithOneInterval: Finds overlaps with one query interval."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 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
185 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="Format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 parser.add_option("-s", "--start", dest="start", action="store", type="int", help="The start of the query interval [compulsory] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 parser.add_option("-e", "--end", dest="end", action="store", type="int", help="The end of the query interval [compulsory] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 parser.add_option("-c", "--chromosome", dest="chromosome", action="store", type="string", help="Chromosome of the query interval [compulsory] [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="Output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 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
191 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 iFOWOI = FindOverlapsWithOneInterval(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 iFOWOI.setFileName(options.inputFileName, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 iFOWOI.setInterval(options.chromosome, options.start, options.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 iFOWOI.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 iFOWOI.run()