annotate SMART/Java/Python/CompareOverlapping.py @ 36:44d5973c188c

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 15:02:29 -0400
parents 769e306b7933
children 169d364ddd91
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 import os, struct, time, random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.structure.Interval import Interval
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.NCListCursor import NCListCursor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.ncList.NCListHandler import NCListHandler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.ncList.ConvertToNCList import ConvertToNCList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 from SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 try:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 import cPickle as pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 except:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 import pickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 REFERENCE = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 QUERY = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 TYPES = (REFERENCE, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 TYPETOSTRING = {0: "reference", 1: "query"}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 class CompareOverlapping(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 def __init__(self, verbosity = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self._outputFileName = "outputOverlaps.gff3"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._iWriter = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self._nbOverlappingQueries = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self._nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self._nbLines = {REFERENCE: 0, QUERY: 0}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self._verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self._ncLists = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self._cursors = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 self._splittedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self._nbElements = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self._nbElementsPerChromosome = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self._inputFileNames = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self._inputFileFormats = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self._starts = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self._ends = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self._fivePrimes = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self._threePrimes = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self._ncListHandlers = {REFERENCE: None, QUERY: None}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self._convertedFileNames = {REFERENCE: False, QUERY: False}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self._sorted = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self._index = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 self._introns = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self._antisense = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self._colinear = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 self._invert = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self._distance = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self._minOverlap = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 self._pcOverlap = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self._included = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 self._including = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self._outputNotOverlapping = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self._tmpRefFileName = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self._currentQueryTranscript = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self._currentOrQueryTranscript = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self._currentExQueryTranscript = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self._randInt = random.randint(0, 100000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 for fileName in [self._tmpRefFileName] + self._convertedFileNames.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if fileName != None and os.path.exists(fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 self._iWriter.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def setInput(self, fileName, format, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 chooser = ParserChooser(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self._inputFileNames[type] = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 self._inputFileFormats[type] = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 def setOutput(self, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 if outputFileName != '':
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self._outputFileName = outputFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self._iWriter = Gff3Writer(self._outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 def setSorted(self, sorted):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self._sorted = sorted
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 def setIndex(self, index):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 self._index = index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 def restrictToStart(self, distance, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self._starts[type] = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 def restrictToEnd(self, distance, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 self._ends[type] = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 def extendFivePrime(self, distance, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 self._fivePrimes[type] = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def extendThreePrime(self, distance, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 self._threePrimes[type] = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 def acceptIntrons(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self._introns = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 def getAntisenseOnly(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 self._antisense = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def getColinearOnly(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self._colinear = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 def getInvert(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 self._invert = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 def setMaxDistance(self, distance):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 self._distance = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 def setMinOverlap(self, overlap):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 self._minOverlap = overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 def setPcOverlap(self, overlap):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 self._pcOverlap = overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 def setIncludedOnly(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 self._included = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 def setIncludingOnly(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self._including = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 def includeNotOverlapping(self, boolean):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 self._outputNotOverlapping = boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 def transformTranscript(self, transcript, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 if self._starts[type] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 transcript.restrictStart(self._starts[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 if self._ends[type] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 transcript.restrictEnd(self._ends[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 if self._fivePrimes[type] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 transcript.extendStart(self._fivePrimes[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 if self._threePrimes[type] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 transcript.extendEnd(self._threePrimes[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 if self._introns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 transcript.exons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 if type == REFERENCE and self._distance > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 transcript.extendExons(self._distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 return transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 def extendQueryTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 self._currentExQueryTranscript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 self._currentExQueryTranscript.copy(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 if self._fivePrimes[QUERY] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 self._currentExQueryTranscript.extendStart(self._fivePrimes[QUERY])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 if self._threePrimes[QUERY] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 self._currentExQueryTranscript.extendEnd(self._threePrimes[QUERY])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 transcript.exons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 def createTmpRefFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 self._tmpRefFileName = "tmp_ref_%d.pkl" % (self._randInt)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 if "SMARTTMPPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 self._tmpRefFileName = os.path.join(os.environ["SMARTTMPPATH"], self._tmpRefFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 chooser = ParserChooser(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 chooser.findFormat(self._inputFileFormats[REFERENCE])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 parser = chooser.getParser(self._inputFileNames[REFERENCE])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 writer = NCListFilePickle(self._tmpRefFileName, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 transcript = self.transformTranscript(transcript, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 writer.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 self._inputFileNames[REFERENCE] = self._tmpRefFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 self._inputFileFormats[REFERENCE] = "pkl"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 def createNCLists(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 self._ncLists = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 self._indices = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self._cursors = dict([type, {}] for type in TYPES)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 for type in TYPES:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 print "Creating %s NC-list..." % (TYPETOSTRING[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 self._convertedFileNames[type] = "%s_%d_%d.ncl" % (self._inputFileNames[type], self._randInt, type)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 ncLists = ConvertToNCList(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 ncLists.setInputFileName(self._inputFileNames[type], self._inputFileFormats[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 ncLists.setOutputFileName(self._convertedFileNames[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 ncLists.setSorted(self._sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 if type == REFERENCE and self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 ncLists.setIndex(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 ncLists.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 self._ncListHandlers[type] = NCListHandler(self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 self._ncListHandlers[type].setFileName(self._convertedFileNames[type])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 self._ncListHandlers[type].loadData()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 self._nbLines[type] = self._ncListHandlers[type].getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 self._nbElementsPerChromosome[type] = self._ncListHandlers[type].getNbElementsPerChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 self._ncLists[type] = self._ncListHandlers[type].getNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 for chromosome, ncList in self._ncLists[type].iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 self._cursors[type][chromosome] = NCListCursor(None, ncList, 0, self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 if type == REFERENCE and self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 self._indices[REFERENCE][chromosome] = ncList.getIndex()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 if self._verbosity > 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 print " ...done"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 def compare(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 nbSkips, nbMoves = 0, 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 previousChromosome = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 refNCList = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 queryNCList = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 progress = Progress(len(self._ncLists[QUERY].keys()), "Checking overlap", self._verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 for chromosome, queryNCList in self._ncLists[QUERY].iteritems():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 queryParser = self._ncListHandlers[QUERY].getParser(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 queryNCList = self._ncLists[QUERY][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 queryCursor = self._cursors[QUERY][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 if chromosome != previousChromosome:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 skipChromosome = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 previousChromosome = chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 if chromosome not in self._ncLists[REFERENCE]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if self._outputNotOverlapping:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 while not queryCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 self._currentQueryTranscript = queryCursor.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 self._writeIntervalInNewGFF3({})
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 if queryCursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 queryCursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 queryCursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 refNCList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 refCursor = self._cursors[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 self._currentOrQueryTranscript = queryCursor.getTranscript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 self._currentQueryTranscript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 self._currentQueryTranscript.copy(self._currentOrQueryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 self._currentQueryTranscript = self.transformTranscript(self._currentQueryTranscript, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 self.extendQueryTranscript(self._currentOrQueryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 newRefLaddr = self.checkIndex(refCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 if newRefLaddr != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 nbMoves += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 refCursor.setLIndex(newRefLaddr)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 refCursor, done, unmatched = self.findOverlapIter(refCursor, done)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 if refCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 if not self._invert and not self._outputNotOverlapping:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 if (unmatched and not self._invert and not self._outputNotOverlapping) or not queryCursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 queryCursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 nbSkips += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 queryCursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 if queryCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 self._timeSpent = endTime - startTime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 if self._verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 print "# skips: %d" % (nbSkips)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 print "# moves: %d" % (nbMoves)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 def findOverlapIter(self, cursor, done):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 chromosome = self._currentQueryTranscript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 matched = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 if chromosome not in self._ncLists[REFERENCE]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 return None, False, True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 ncList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 overlappingNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 nextDone = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 firstOverlapLAddr = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 firstOverlapLAddr.setLIndex(-1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 self._writeIntervalInNewGFF3(overlappingNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 return firstOverlapLAddr, False, True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 parentCursor = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 firstParentAfter = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 while not parentCursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 if self.isOverlapping(parentCursor) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 matched = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 if self._checkOverlap(parentCursor.getTranscript()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 overlappingNames.update(self._extractID(parentCursor.getTranscript()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 if firstOverlapLAddr.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 firstOverlapLAddr.copy(parentCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 nextDone = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 elif self.isOverlapping(parentCursor) == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 firstParentAfter = NCListCursor(parentCursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 if firstParentAfter:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 written = self._writeIntervalInNewGFF3(overlappingNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 return firstParentAfter, False, not written if self._invert else not matched
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 #This loop finds the overlaps with currentRefLAddr.#
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 parentCursor = NCListCursor(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 parentCursor.moveUp()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 #In case: Query is on the right of the RefInterval and does not overlap.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 overlap = self.isOverlapping(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 if overlap == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 #In case: Query overlaps with RefInterval.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 elif overlap == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 matched = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 if self._checkOverlap(cursor.getTranscript()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 overlappingNames.update(self._extractID(cursor.getTranscript()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 if firstOverlapLAddr.compare(parentCursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 firstOverlapLAddr.copy(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 nextDone = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 if done:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 if not cursor.hasChildren():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 cursor.moveNext()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 cursor.moveDown()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 #In case: Query is on the left of the RefInterval and does not overlap.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 if firstOverlapLAddr.isOut() or firstOverlapLAddr.compare(parentCursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 firstOverlapLAddr.copy(cursor)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 nextDone = False # new
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 done = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 written = self._writeIntervalInNewGFF3(overlappingNames)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 return firstOverlapLAddr, nextDone, not written if self._invert else not matched
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 def isOverlapping(self, refTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 if (self._currentExQueryTranscript.getStart() <= refTranscript.getEnd() and self._currentExQueryTranscript.getEnd() >= refTranscript.getStart()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 if self._currentExQueryTranscript.getEnd() < refTranscript.getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 return 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 return -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 def checkIndex(self, cursor):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 if not self._index:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 if cursor.isOut():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 chromosome = self._currentExQueryTranscript.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 nextLIndex = self._indices[REFERENCE][chromosome].getIndex(self._currentExQueryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 if nextLIndex == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 ncList = self._ncLists[REFERENCE][chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 nextGffAddress = ncList.getRefGffAddr(nextLIndex)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 thisGffAddress = cursor.getGffAddress()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 if nextGffAddress > thisGffAddress:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 return nextLIndex
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 def _writeIntervalInNewGFF3(self, names):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 for cpt in names.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 nbOverlaps += cpt
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 self._nbOverlappingQueries += 1 if Utils.xor(names, self._invert) else 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 self._nbOverlaps += nbOverlaps if Utils.xor(names, self._invert) else 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 if names:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 self._currentQueryTranscript.setTagValue("overlapWith", ",".join(names))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 self._currentQueryTranscript.setTagValue("nbOverlaps", nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 if self._invert:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390 if self._outputNotOverlapping:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 self._currentQueryTranscript.setTagValue("nbOverlaps", 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392 elif not self._invert:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394 self._iWriter.addTranscript(self._currentQueryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395 self._iWriter.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398 def _extractID(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399 id = transcript.getTagValue("ID") if "ID" in transcript.getTagNames() else transcript.getUniqueName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 nbElements = transcript.getTagValue("nbElements") if "nbElements" in transcript.getTagNames() else 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 return {id: float(nbElements)}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403 def _checkOverlap(self, refTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404 if self._currentQueryTranscript.getDistance(refTranscript) > self._distance:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406 minOverlap = self._minOverlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407 if self._pcOverlap != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 minOverlap = max(self._minOverlap, self._currentQueryTranscript.getSize() / 100.0 * self._pcOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 if not self._currentQueryTranscript.overlapWith(refTranscript, minOverlap):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 if self._antisense and self._currentQueryTranscript.getDirection() == refTranscript.getDirection():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413 if self._colinear and self._currentQueryTranscript.getDirection() != refTranscript.getDirection():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 if self._included and not refTranscript.include(self._currentQueryTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 if self._including and not self._currentQueryTranscript.include(refTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419 if self._introns:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 return self._currentQueryTranscript.overlapWithExon(refTranscript, minOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424 self.createTmpRefFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425 self.createNCLists()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426 self.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 self.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 if self._verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 print "# queries: %d" % (self._nbLines[QUERY])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430 print "# refs: %d" % (self._nbLines[REFERENCE])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431 print "# written: %d (%d overlaps)" % (self._nbOverlappingQueries, self._nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 print "time: %ds" % (self._timeSpent)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436 description = "Compare Overlapping v1.0.4: Get the data which overlap with a reference set. [Category: Data Comparison]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="input file 1 [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of file 1 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="input file 2 [compulsory] [format: file in transcript format given by -g]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of file 2 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443 parser.add_option("-o", "--output", dest="output", action="store", default=None, type="string", help="output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 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
445 parser.add_option("-r", "--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
446 parser.add_option("-S", "--start1", dest="start1", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 1 (do not use it with -U) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 parser.add_option("-s", "--start2", dest="start2", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 2 (do not use it with -u) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 parser.add_option("-U", "--end1", dest="end1", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 1 (do not use it with -S) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 parser.add_option("-u", "--end2", dest="end2", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 2 (do not use it with -s) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 parser.add_option("-t", "--intron", dest="introns", action="store_true", default=False, help="also report introns [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 parser.add_option("-E", "--5primeExtension1", dest="fivePrime1", action="store", default=None, type="int", help="extension towards 5' in file 1 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452 parser.add_option("-e", "--5primeExtension2", dest="fivePrime2", action="store", default=None, type="int", help="extension towards 5' in file 2 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453 parser.add_option("-N", "--3primeExtension1", dest="threePrime1", action="store", default=None, type="int", help="extension towards 3' in file 1 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454 parser.add_option("-n", "--3primeExtension2", dest="threePrime2", action="store", default=None, type="int", help="extension towards 3' in file 2 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455 parser.add_option("-c", "--colinear", dest="colinear", action="store_true", default=False, help="colinear only [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456 parser.add_option("-a", "--antisense", dest="antisense", action="store_true", default=False, help="antisense only [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 parser.add_option("-d", "--distance", dest="distance", action="store", default=0, type="int", help="accept some distance between query and reference [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458 parser.add_option("-k", "--included", dest="included", action="store_true", default=False, help="keep only elements from file 1 which are included in an element of file 2 [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459 parser.add_option("-K", "--including", dest="including", action="store_true", default=False, help="keep only elements from file 2 which are included in an element of file 1 [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460 parser.add_option("-m", "--minOverlap", dest="minOverlap", action="store", default=1, type="int", help="minimum number of nucleotides overlapping to declare an overlap [format: int] [default: 1]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 parser.add_option("-p", "--pcOverlap", dest="pcOverlap", action="store", default=None, type="int", help="minimum percentage of nucleotides to overlap to declare an overlap [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
462 parser.add_option("-O", "--notOverlapping", dest="notOverlapping", action="store_true", default=False, help="also output not overlapping data [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
463 parser.add_option("-x", "--exclude", dest="exclude", action="store_true", default=False, help="invert the match [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
464 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
465 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
466
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
467 co = CompareOverlapping(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
468 co.setInput(options.inputFileName1, options.format1, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
469 co.setInput(options.inputFileName2, options.format2, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
470 co.setOutput(options.output)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
471 co.setSorted(options.sorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
472 co.setIndex(options.index)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
473 co.restrictToStart(options.start1, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
474 co.restrictToStart(options.start2, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
475 co.restrictToEnd(options.end1, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
476 co.restrictToEnd(options.end2, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
477 co.extendFivePrime(options.fivePrime1, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
478 co.extendFivePrime(options.fivePrime2, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
479 co.extendThreePrime(options.threePrime1, QUERY)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
480 co.extendThreePrime(options.threePrime2, REFERENCE)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
481 co.acceptIntrons(options.introns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
482 co.getAntisenseOnly(options.antisense)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
483 co.getColinearOnly(options.colinear)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
484 co.getInvert(options.exclude)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
485 co.setMaxDistance(options.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
486 co.setMinOverlap(options.minOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
487 co.setPcOverlap(options.pcOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
488 co.setIncludedOnly(options.included)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
489 co.setIncludingOnly(options.including)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
490 co.includeNotOverlapping(options.notOverlapping)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
491 co.run()