annotate SMART/Java/Python/FindOverlapsOptim.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -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-2012
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 os, struct, time, shutil
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.structure.Interval import Interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.ncList.NCList import NCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.ncList.ConvertToNCList import ConvertToNCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.ncList.NCListParser import NCListParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from SMART.Java.Python.ncList.NCListHandler import NCListHandler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 try:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 import cPickle as pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 except:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 import pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 REFERENCE = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 QUERY = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 TYPES = (REFERENCE, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 TYPETOSTRING = {0: "reference", 1: "query"}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 class FindOverlapsOptim(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def __init__(self, verbosity = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._parsers = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self._sortedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self._outputFileName = "outputOverlaps.gff3"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self._iWriter = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self._inputFileNames = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self._convertedFileNames = {REFERENCE: False, QUERY: False}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self._inputFileFormats = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self._converted = {REFERENCE: False, QUERY: False}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self._ncListHandlers = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self._splittedFileNames = {REFERENCE: {}, QUERY: {}}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self._nbOverlappingQueries = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self._nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self._nbLines = {REFERENCE: 0, QUERY: 0}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self._sorted = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self._index = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self._verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self._cursors = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self._nbElementsPerChromosome = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._tmpDirectories = {REFERENCE: False, QUERY: False}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self._iWriter.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 for fileName in (self._sortedFileNames.values()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 if os.path.exists(fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 for fileName in self._convertedFileNames.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if fileName:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 def setRefFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.setFileName(fileName, format, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 def setQueryFileName(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self.setFileName(fileName, format, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def setFileName(self, fileName, format, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self._inputFileNames[type] = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 self._inputFileFormats[type] = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if format.lower() != "nclist":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 self._converted[type] = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 def setOutputFileName(self, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 self._outputFileName = outputFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 self._iWriter = Gff3Writer(self._outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 def setSorted(self, sorted):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self._sorted = sorted
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 def setIndex(self, index):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 self._index = index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 def createNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 if self._verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 print "Building database"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self._ncLists = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self._indices = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 self._cursors = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 for type in TYPES:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 self._ncListHandlers[type] = NCListHandler(self._verbosity-3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 if self._converted[type]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self._convertedFileNames[type] = "%s_%d.ncl" % (os.path.splitext(self._inputFileNames[type])[0], type)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 ncLists = ConvertToNCList(self._verbosity-3)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 ncLists.setInputFileName(self._inputFileNames[type], self._inputFileFormats[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 ncLists.setSorted(self._sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 ncLists.setOutputFileName(self._convertedFileNames[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if type == REFERENCE and self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 ncLists.setIndex(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 ncLists.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self._ncListHandlers[type].setFileName(self._convertedFileNames[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 self._ncListHandlers[type].setFileName(self._inputFileNames[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 self._ncListHandlers[type].loadData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self._nbLines[type] = self._ncListHandlers[type].getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 self._nbElementsPerChromosome[type] = self._ncListHandlers[type].getNbElementsPerChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self._ncLists[type] = self._ncListHandlers[type].getNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 for chromosome, ncList in self._ncLists[type].iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 self._cursors[type][chromosome] = NCListCursor(None, ncList, 0, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 if type == REFERENCE and self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self._indices[REFERENCE][chromosome] = ncList.getIndex()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 if self._verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 print "done (%.2gs)" % (endTime - startTime)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 def compare(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 nbSkips, nbMoves = 0, 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 previousChromosome = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 progress = Progress(len(self._ncLists[QUERY].keys()), "Checking overlap", self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 #print "query:", self._ncLists[QUERY].keys()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 #print "reference:", self._ncLists[REFERENCE].keys()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 for chromosome, queryNCList in self._ncLists[QUERY].iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 queryParser = self._ncListHandlers[QUERY].getParser(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 queryCursor = self._cursors[QUERY][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 if chromosome != previousChromosome:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 skipChromosome = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 previousChromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 if chromosome not in self._ncLists[REFERENCE]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 #print "out ", chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 refNCList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 refCursor = self._cursors[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 #print "starting", chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 queryTranscript = queryCursor.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 newRefLaddr = self.checkIndex(queryTranscript, refCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 #print "query is", queryTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 if newRefLaddr != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 nbMoves += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 refCursor.setLIndex(newRefLaddr)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 #print "skipping to", refCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 refCursor, done, unmatched = self.findOverlapIter(queryTranscript, refCursor, done)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 #print "completed with", refCursor, done, unmatched
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if refCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 #print "exiting 1", chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 if unmatched or not queryCursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 queryCursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 #print "moving next to", queryCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 nbSkips += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 queryCursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 #print "moving down to", queryCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 if queryCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 #print "exiting 2", chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 self._timeSpent = endTime - startTime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 if self._verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 print "# skips: %d" % (nbSkips)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 print "# moves: %d" % (nbMoves)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 def findOverlapIter(self, queryTranscript, cursor, done):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 chromosome = queryTranscript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 if chromosome not in self._ncLists[REFERENCE]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 return False, None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 ncList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 overlappingNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 nextDone = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 firstOverlapLAddr = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 firstOverlapLAddr.setLIndex(-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 return firstOverlapLAddr, False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 parentCursor = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 firstParentAfter = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 #print "query transcript 1", queryTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 #print "cursor 1", cursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 #print "parent 1", parentCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 while not parentCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 if self.isOverlapping(queryTranscript, parentCursor) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 #print "overlap parent choice 0"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 overlappingNames.update(self._extractID(parentCursor.getTranscript()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 if firstOverlapLAddr.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 #print "overlap parent 2"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 firstOverlapLAddr.copy(parentCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 nextDone = True # new
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 elif self.isOverlapping(queryTranscript, parentCursor) == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 #print "overlap parent choice 1"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 firstParentAfter = NCListCursor(parentCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 #print "parent 2", parentCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 if firstParentAfter:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 #print "exit parent", firstParentAfter, overlappingNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 self._writeIntervalInNewGFF3(queryTranscript, overlappingNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 return firstParentAfter, False, not overlappingNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 #This loop finds the overlaps with currentRefLAddr.#
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 #print "ref cursor now is", cursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 parentCursor = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 #In case: Query is on the right of the RefInterval and does not overlap.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 overlap = self.isOverlapping(queryTranscript, cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 if overlap == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 #In case: Query overlaps with RefInterval.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 elif overlap == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 #print "choice 2"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 overlappingNames.update(self._extractID(cursor.getTranscript()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 if firstOverlapLAddr.compare(parentCursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 firstOverlapLAddr.copy(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 nextDone = True # new
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 if done:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 if not cursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 #print "break 1"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 cursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 #In case: Query is on the left of the RefInterval and does not overlap.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 #print "choice 3"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 if firstOverlapLAddr.isOut() or firstOverlapLAddr.compare(parentCursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 #print "changing nfo 2"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 firstOverlapLAddr.copy(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 nextDone = False # new
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 #print "break 2"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 #print "break 3"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 self._writeIntervalInNewGFF3(queryTranscript, overlappingNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 return firstOverlapLAddr, nextDone, not overlappingNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 def isOverlapping(self, queryTranscript, refTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 if (queryTranscript.getStart() <= refTranscript.getEnd() and queryTranscript.getEnd() >= refTranscript.getStart()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 if queryTranscript.getEnd() < refTranscript.getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 return 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 return -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 def checkIndex(self, transcript, cursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 if not self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 chromosome = transcript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 nextLIndex = self._indices[REFERENCE][chromosome].getIndex(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 if nextLIndex == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 ncList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 nextGffAddress = ncList.getRefGffAddr(nextLIndex)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 thisGffAddress = cursor.getGffAddress()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 if nextGffAddress > thisGffAddress:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 return nextLIndex
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 def _writeIntervalInNewGFF3(self, transcript, names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 for cpt in names.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 nbOverlaps += cpt
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 if not names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 transcript.setTagValue("overlapsWith", "--".join(sorted(names.keys())))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 transcript.setTagValue("nbOverlaps", nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 self._iWriter.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 self._iWriter.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 self._nbOverlappingQueries += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 self._nbOverlaps += nbOverlaps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 def _extractID(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 nbElements = float(transcript.getTagValue("nbElements")) if "nbElements" in transcript.getTagNames() else 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 id = transcript.getTagValue("ID") if "ID" in transcript.getTagNames() else transcript.getUniqueName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 return {id: nbElements}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 self.createNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 self.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 self.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 if self._verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 print "# queries: %d" % (self._nbLines[QUERY])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 print "# refs: %d" % (self._nbLines[REFERENCE])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 print "# written: %d (%d overlaps)" % (self._nbOverlappingQueries, self._nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 print "time: %.2gs" % (self._timeSpent)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 description = "Find Overlaps Optim v1.0.0: Finds overlaps with several query intervals. [Category: Data Comparison]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 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
332 parser.add_option("-d", "--index", dest="index", action="store_true", default=False, help="add an index to the reference file (faster but more memory) [format: boolean] [default: False]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 parser.add_option("-s", "--sorted", dest="sorted", action="store_true", default=False, help="input files are already sorted [format: boolean] [default: False]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 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
335 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 iFOO = FindOverlapsOptim(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 iFOO.setRefFileName(options.inputRefFileName, options.refFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 iFOO.setQueryFileName(options.inputQueryFileName, options.queryFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 iFOO.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 iFOO.setIndex(options.index)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 iFOO.setSorted(options.sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 iFOO.run()