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

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
1 #! /usr/bin/env python
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
2 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2012
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
4 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
9 # "http://www.cecill.info".
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
10 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
15 # liability.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
16 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
26 # same conditions as regards security.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
27 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
30 #
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
31
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
32 import os, struct, time, shutil
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
33 from optparse import OptionParser
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
34 from pyRepetUnit.commons.parsing.ParserChooser import ParserChooser
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
35 from pyRepetUnit.commons.writer.Gff3Writer import Gff3Writer
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
36 from SMART.Java.Python.structure.Transcript import Transcript
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
37 from SMART.Java.Python.structure.Interval import Interval
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
38 from SMART.Java.Python.ncList.NCList import NCList
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
39 from SMART.Java.Python.ncList.ConvertToNCList import ConvertToNCList
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
40 from SMART.Java.Python.ncList.NCListParser import NCListParser
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
41 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
42 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
43 from SMART.Java.Python.ncList.FileSorter import FileSorter
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
44 from SMART.Java.Python.ncList.NCListHandler import NCListHandler
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
45 from SMART.Java.Python.misc.Progress import Progress
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
46 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
47 try:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
48 import cPickle as pickle
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
49 except:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
50 import pickle
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
51
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
52 REFERENCE = 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
53 QUERY = 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
54 TYPES = (REFERENCE, QUERY)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
55 TYPETOSTRING = {0: "reference", 1: "query"}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
56
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
57 class FindOverlapsOptim(object):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
58
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
59 def __init__(self, verbosity = 1):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
60 self._parsers = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
61 self._sortedFileNames = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
62 self._outputFileName = "outputOverlaps.gff3"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
63 self._iWriter = None
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
64 self._inputFileNames = {REFERENCE: None, QUERY: None}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
65 self._convertedFileNames = {REFERENCE: False, QUERY: False}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
66 self._inputFileFormats = {REFERENCE: None, QUERY: None}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
67 self._converted = {REFERENCE: False, QUERY: False}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
68 self._ncListHandlers = {REFERENCE: None, QUERY: None}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
69 self._splittedFileNames = {REFERENCE: {}, QUERY: {}}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
70 self._nbOverlappingQueries = 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
71 self._nbOverlaps = 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
72 self._nbLines = {REFERENCE: 0, QUERY: 0}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
73 self._verbosity = verbosity
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
74 self._ncLists = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
75 self._cursors = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
76 self._nbElementsPerChromosome = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
77 self._tmpDirectories = {REFERENCE: False, QUERY: False}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
78
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
79 def close(self):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
80 self._iWriter.close()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
81 for fileName in (self._sortedFileNames.values()):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
82 if os.path.exists(fileName):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
83 os.remove(fileName)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
84 for fileName in self._convertedFileNames.values():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
85 if fileName:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
86 os.remove(fileName)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
87
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
88 def setRefFileName(self, fileName, format):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
89 self.setFileName(fileName, format, REFERENCE)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
90
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
91 def setQueryFileName(self, fileName, format):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
92 self.setFileName(fileName, format, QUERY)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
93
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
94 def setFileName(self, fileName, format, type):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
95 self._inputFileNames[type] = fileName
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
96 self._inputFileFormats[type] = format
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
97 if format.lower() != "nclist":
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
98 self._converted[type] = True
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
99
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
100 def setOutputFileName(self, outputFileName):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
101 self._outputFileName = outputFileName
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
102 self._iWriter = Gff3Writer(self._outputFileName)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
103
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
104 def createNCLists(self):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
105 startTime = time.time()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
106 if self._verbosity > 1:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
107 print "Building database"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
108 self._ncLists = dict([type, {}] for type in TYPES)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
109 self._indices = dict([type, {}] for type in TYPES)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
110 self._cursors = dict([type, {}] for type in TYPES)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
111 for type in TYPES:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
112 self._ncListHandlers[type] = NCListHandler(self._verbosity-3)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
113 if self._converted[type]:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
114 self._convertedFileNames[type] = "%s_%d.ncl" % (os.path.splitext(self._inputFileNames[type])[0], type)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
115 ncLists = ConvertToNCList(self._verbosity-3)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
116 ncLists.setInputFileName(self._inputFileNames[type], self._inputFileFormats[type])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
117 ncLists.setOutputFileName(self._convertedFileNames[type])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
118 if type == REFERENCE:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
119 ncLists.setIndex(True)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
120 ncLists.run()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
121 self._ncListHandlers[type].setFileName(self._convertedFileNames[type])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
122 else:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
123 self._ncListHandlers[type].setFileName(self._inputFileNames[type])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
124 self._ncListHandlers[type].loadData()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
125 self._nbLines[type] = self._ncListHandlers[type].getNbElements()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
126 self._nbElementsPerChromosome[type] = self._ncListHandlers[type].getNbElementsPerChromosome()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
127 self._ncLists[type] = self._ncListHandlers[type].getNCLists()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
128 for chromosome, ncList in self._ncLists[type].iteritems():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
129 self._cursors[type][chromosome] = NCListCursor(None, ncList, 0, self._verbosity)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
130 if type == REFERENCE:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
131 self._indices[REFERENCE][chromosome] = ncList.getIndex()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
132 endTime = time.time()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
133 if self._verbosity > 1:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
134 print "done (%.2gs)" % (endTime - startTime)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
135
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
136 def compare(self):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
137 nbSkips, nbMoves = 0, 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
138 previousChromosome = None
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
139 done = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
140 startTime = time.time()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
141 progress = Progress(len(self._ncLists[QUERY].keys()), "Checking overlap", self._verbosity)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
142 #print "query:", self._ncLists[QUERY].keys()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
143 #print "reference:", self._ncLists[REFERENCE].keys()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
144 for chromosome, queryNCList in self._ncLists[QUERY].iteritems():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
145 queryParser = self._ncListHandlers[QUERY].getParser(chromosome)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
146 queryCursor = self._cursors[QUERY][chromosome]
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
147 if chromosome != previousChromosome:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
148 skipChromosome = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
149 previousChromosome = chromosome
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
150 if chromosome not in self._ncLists[REFERENCE]:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
151 #print "out ", chromosome
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
152 continue
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
153 refNCList = self._ncLists[REFERENCE][chromosome]
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
154 refCursor = self._cursors[REFERENCE][chromosome]
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
155 #print "starting", chromosome
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
156 while True:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
157 queryTranscript = queryCursor.getTranscript()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
158 #print queryTranscript
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
159 newRefLaddr = self.checkIndex(queryTranscript, refCursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
160 #print "query is", queryTranscript
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
161 if newRefLaddr != None:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
162 nbMoves += 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
163 refCursor.setLIndex(newRefLaddr)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
164 #print "skipping to", refCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
165 done = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
166 refCursor, done, unmatched = self.findOverlapIter(queryTranscript, refCursor, done)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
167 #print "completed with", refCursor, done, unmatched
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
168 if refCursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
169 #print "exiting 1", chromosome
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
170 break
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
171 if unmatched or not queryCursor.hasChildren():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
172 queryCursor.moveNext()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
173 #print "moving next to", queryCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
174 nbSkips += 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
175 else:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
176 queryCursor.moveDown()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
177 #print "moving down to", queryCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
178 if queryCursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
179 #print "exiting 2", chromosome
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
180 break
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
181 progress.inc()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
182 progress.done()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
183 endTime = time.time()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
184 self._timeSpent = endTime - startTime
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
185 if self._verbosity >= 10:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
186 print "# skips: %d" % (nbSkips)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
187 print "# moves: %d" % (nbMoves)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
188
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
189 def findOverlapIter(self, queryTranscript, cursor, done):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
190 chromosome = queryTranscript.getChromosome()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
191 if chromosome not in self._ncLists[REFERENCE]:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
192 return False, None
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
193 ncList = self._ncLists[REFERENCE][chromosome]
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
194 overlappingNames = {}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
195 nextDone = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
196 firstOverlapLAddr = NCListCursor(cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
197 firstOverlapLAddr.setLIndex(-1)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
198 if cursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
199 return firstOverlapLAddr, False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
200 parentCursor = NCListCursor(cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
201 parentCursor.moveUp()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
202 firstParentAfter = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
203 #print "query transcript 1", queryTranscript
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
204 #print "cursor 1", cursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
205 #print "parent 1", parentCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
206 while not parentCursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
207 if self.isOverlapping(queryTranscript, parentCursor) == 0:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
208 #print "overlap parent choice 0"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
209 overlappingNames.update(self._extractID(parentCursor.getTranscript()))
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
210 if firstOverlapLAddr.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
211 #print "overlap parent 2"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
212 firstOverlapLAddr.copy(parentCursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
213 nextDone = True # new
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
214 # lastCursor = NCListCursor(parentCursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
215 # lastCursor.moveDown()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
216 # lastCursor.moveLastSibling()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
217 # if self.isOverlapping(queryTranscript, lastCursor) == -1:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
218 # #print "next done 1"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
219 # nextDone = True
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
220 elif self.isOverlapping(queryTranscript, parentCursor) == 1:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
221 #print "overlap parent choice 1"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
222 firstParentAfter = NCListCursor(parentCursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
223 parentCursor.moveUp()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
224 #print "parent 2", parentCursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
225 if firstParentAfter:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
226 #print "exit parent", firstParentAfter, overlappingNames
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
227 self._writeIntervalInNewGFF3(queryTranscript, overlappingNames)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
228 return firstParentAfter, False, not overlappingNames
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
229 #This loop finds the overlaps with currentRefLAddr.#
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
230 while True:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
231 #print "ref cursor now is", cursor
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
232 parentCursor = NCListCursor(cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
233 parentCursor.moveUp()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
234 #In case: Query is on the right of the RefInterval and does not overlap.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
235 overlap = self.isOverlapping(queryTranscript, cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
236 if overlap == -1:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
237 #print "choice 1"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
238 # if cursor.isLast() and not firstOverlapLAddr.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
239 # if firstOverlapLAddr.compare(parentCursor):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
240 # #print "next done 2"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
241 # nextDone = True
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
242 cursor.moveNext()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
243 #In case: Query overlaps with RefInterval.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
244 elif overlap == 0:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
245 #print "choice 2"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
246 overlappingNames.update(self._extractID(cursor.getTranscript()))
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
247 if firstOverlapLAddr.compare(parentCursor):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
248 firstOverlapLAddr.copy(cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
249 nextDone = True # new
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
250 if done:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
251 cursor.moveNext()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
252 else:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
253 if not cursor.hasChildren():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
254 cursor.moveNext()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
255 if cursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
256 #print "break 1"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
257 break
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
258 else:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
259 cursor.moveDown()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
260 #In case: Query is on the left of the RefInterval and does not overlap.
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
261 else:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
262 #print "choice 3"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
263 if firstOverlapLAddr.isOut() or firstOverlapLAddr.compare(parentCursor):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
264 #print "changing nfo 2"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
265 firstOverlapLAddr.copy(cursor)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
266 nextDone = False # new
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
267 #print "break 2"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
268 break
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
269
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
270 done = False
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
271 if cursor.isOut():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
272 #print "break 3"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
273 break
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
274 self._writeIntervalInNewGFF3(queryTranscript, overlappingNames)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
275 return firstOverlapLAddr, nextDone, not overlappingNames
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
276
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
277 def isOverlapping(self, queryTranscript, refTranscript):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
278 if (queryTranscript.getStart() <= refTranscript.getEnd() and queryTranscript.getEnd() >= refTranscript.getStart()):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
279 return 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
280 if queryTranscript.getEnd() < refTranscript.getStart():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
281 return 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
282 return -1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
283
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
284 def checkIndex(self, transcript, cursor):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
285 chromosome = transcript.getChromosome()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
286 nextLIndex = self._indices[REFERENCE][chromosome].getIndex(transcript)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
287 if nextLIndex == None:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
288 return None
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
289 ncList = self._ncLists[REFERENCE][chromosome]
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
290 nextGffAddress = ncList.getRefGffAddr(nextLIndex)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
291 thisGffAddress = cursor.getGffAddress()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
292 if nextGffAddress > thisGffAddress:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
293 return nextLIndex
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
294 return None
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
295
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
296 def _writeIntervalInNewGFF3(self, transcript, names):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
297 nbOverlaps = 0
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
298 for cpt in names.values():
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
299 nbOverlaps += cpt
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
300 if not names:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
301 return
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
302 transcript.setTagValue("overlapsWith", "--".join(sorted(names.keys())))
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
303 transcript.setTagValue("nbOverlaps", nbOverlaps)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
304 self._iWriter.addTranscript(transcript)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
305 self._iWriter.write()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
306 self._nbOverlappingQueries += 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
307 self._nbOverlaps += nbOverlaps
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
308
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
309 def _extractID(self, transcript):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
310 nbElements = float(transcript.getTagValue("nbElements")) if "nbElements" in transcript.getTagNames() else 1
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
311 id = transcript.getTagValue("ID") if "ID" in transcript.getTagNames() else transcript.getUniqueName()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
312 return {id: nbElements}
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
313
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
314 def run(self):
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
315 self.createNCLists()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
316 self.compare()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
317 self.close()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
318 if self._verbosity > 0:
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
319 print "# queries: %d" % (self._nbLines[QUERY])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
320 print "# refs: %d" % (self._nbLines[REFERENCE])
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
321 print "# written: %d (%d overlaps)" % (self._nbOverlappingQueries, self._nbOverlaps)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
322 print "time: %.2gs" % (self._timeSpent)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
323
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
324
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
325 if __name__ == "__main__":
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
326 description = "Find Overlaps Optim v1.0.0: Finds overlaps with several query intervals. [Category: Data Comparison]"
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
327
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
328 parser = OptionParser(description = description)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
329 parser.add_option("-i", "--query", dest="inputQueryFileName", action="store", type="string", help="Query input file [compulsory] [format: file in transcript or other format given by -f]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
330 parser.add_option("-f", "--queryFormat", dest="queryFormat", action="store", type="string", help="format of previous file (possibly in NCL format) [compulsory] [format: transcript or other file format]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
331 parser.add_option("-j", "--ref", dest="inputRefFileName", action="store", type="string", help="Reference input file [compulsory] [format: file in transcript or other format given by -g]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
332 parser.add_option("-g", "--refFormat", dest="refFormat", action="store", type="string", help="format of previous file (possibly in NCL format) [compulsory] [format: transcript or other file format]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
333 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="Output file [compulsory] [format: output file in GFF3 format]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
334 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="Trace level [format: int] [default: 1]")
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
335 (options, args) = parser.parse_args()
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
336
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
337 iFOO = FindOverlapsOptim(options.verbosity)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
338 iFOO.setRefFileName(options.inputRefFileName, options.refFormat)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
339 iFOO.setQueryFileName(options.inputQueryFileName, options.queryFormat)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
340 iFOO.setOutputFileName(options.outputFileName)
0ab839023fe4 Uploaded
m-zytnicki
parents:
diff changeset
341 iFOO.run()