annotate TEisotools-1.1.a/commons/core/coord/Range.py @ 15:255c852351c5 draft

Uploaded
author urgi-team
date Thu, 21 Jul 2016 07:36:44 -0400
parents feef9a0db09d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32 ## Record a region on a given sequence
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 class Range( object ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 __slots__ = ("seqname", "start", "end", '__dict__')
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 ## Constructor
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40 # @param seqname the name of the sequence
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 # @param start the start coordinate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 # @param end the end coordinate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 def __init__(self, seqname="", start=-1, end=-1):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 self.seqname = seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 self.start = int(start)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 self.end = int(end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 ## Equal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 def __eq__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 if type(o) is type(self) and self.seqname == o.seqname and self.start == o.start and self.end == o.end:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 ## Unequal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 def __ne__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 return not self.__eq__(o)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 ## Convert the object into a string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 # @note used in 'print myObject'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 def __str__( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 return self.toString()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 ## Convert the object into a string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 # @note used in 'repr(myObject)' for debugging
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 def __repr__( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 return self.toString().replace("\t",";")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 def setStart(self, start):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 self.start = start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 def setEnd(self, end):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 self.end = end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 def setSeqName(self, seqName):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 self.seqname = seqName
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 ## Reset
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 def reset(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 self.seqname = ""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 self.start = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 self.end = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 ## Return the attributes as a formatted string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 def toString(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 string = "%s" % (self.seqname)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 string += "\t%d" % (self.start)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 string += "\t%d" % (self.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 ## Show the attributes
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 def show(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 print self.toString()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 ## Return seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 def getSeqname(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 return self.seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 ## Return the start coordinate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 def getStart(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 return self.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 ## Return the end coordinate
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 def getEnd(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 return self.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 ## Return the lowest value between start and end coordinates
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 def getMin(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 return min(self.start, self.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 ## Return the greatest value between start and end attributes
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 def getMax(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 return max(self.start, self.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 ## Return True if the instance is on the direct strand, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 def isOnDirectStrand(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 if self.start <= self.end:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 ## Return True if the instance is on the reverse strand, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 def isOnReverseStrand(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 return not self.isOnDirectStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 ## Return '+' if the instance is on the direct strand, '-' otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 def getStrand(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149 if self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 return '+'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 return '-'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154 ## Exchange start and end coordinates
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156 def reverse(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 tmp = self.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 self.start = self.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 self.end = tmp
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161 ## Return the length of the instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 # @warning old name is 'length'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 def getLength(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 return int(abs(self.start-self.end))+1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 ## Return True if the instance is empty, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170 def isEmpty(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171 if self.start==self.end and (self.start==0 or self.start==-1):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 ## Set attributes from tuple
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 # @param tuple a tuple with (name,start,end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179 def setFromTuple(self, tuple):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180 self.seqname = tuple[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181 self.start = int(tuple[1])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 self.end = int(tuple[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 ## Set attributes from string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 # @param string a string formatted like name<sep>start<sep>end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187 # @param sep field separator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 def setFromString(self, string, sep="\t"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 if string[-1] == "\n":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191 string = string[:-1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 self.setFromTuple( string.split(sep) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194 ## Merge the instance with another Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198 def merge(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 if self.seqname != o.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200 return
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201 if self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202 self.start = min(self.getMin(), o.getMin())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 self.end = max(self.getMax(), o.getMax())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205 self.start = max(self.getMax(), o.getMax())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206 self.end = min(self.getMin(), o.getMin())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208 ## Return True if the instance overlaps with another Range instance, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 def isOverlapping(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213 if o.seqname != self.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
214 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
215 smin = self.getMin()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
216 smax = self.getMax()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
217 omin = o.getMin()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
218 omax = o.getMax()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
219 if omin <= smin and omax >= smax:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
220 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
221 if omin >= smin and omin <= smax or omax >= smin and omax <= smax:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
222 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
223 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
224
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
225
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
226 ## Return the length of the overlap between the instance and another Range, 0 if no overlap
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
227 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
228 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
229 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
230 def getOverlapLength( self, o ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
231 if self.isOverlapping( o ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
232 if self.isIncludedIn( o ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
233 return self.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
234 elif o.isIncludedIn( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
235 return o.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
236 elif o.getMin() <= self.getMax() and o.getMin() >= self.getMin():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
237 return self.getMax() - o.getMin() + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
238 elif o.getMax() <= self.getMax() and o.getMax() >= self.getMin():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
239 return o.getMax() - self.getMin() + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
240 return 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
241
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
242
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
243 ## Return True if the instance is included within another Range, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
244 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
245 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
246 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
247 # @note the min (respectively max) coordinates can be equal
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
248 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
249 def isIncludedIn( self, o ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
250 if o.seqname != self.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
251 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
252 if self.getMin() >= o.getMin() and self.getMax() <= o.getMax():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
253 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
254 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
255 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
256
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
257
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
258 ## Return the distance between the start of the instance and the start of another Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
259 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
260 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
261 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
262 def getDistance(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
263 if self.isOnDirectStrand() == o.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
264 if self.isOverlapping(o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
265 return 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
266 elif self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
267 if self.start > o.start:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
268 return self.start - o.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
269 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
270 return o.start - self.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
271 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
272 if self.start > o.start:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
273 return self.end - o.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
274 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
275 return o.end - self.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
276 return -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
277
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
278 ## Remove in the instance the region overlapping with another Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
279 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
280 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
281 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
282 def diff(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
283 new_range = Range(self.seqname)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
284 if not self.isOverlapping(o) or self.seqname != o.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
285 return new_range
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
286
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
287 istart = min(self.start, self.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
288 iend = max(self.start, self.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
289 jstart = min(o.start, o.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
290 jend = max(o.start, o.end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
291 if istart < jstart:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
292 if iend <= jend:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
293 if self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
294 self.start = istart
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
295 self.end = jstart - 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
296 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
297 self.start = jstart - 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
298 self.end = istart
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
299 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
300 if self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
301 self.start = istart
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
302 self.end = jstart - 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
303 new_range.start = jend + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
304 new_range.end = iend
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
305 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
306 self.start = jstart - 1;
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
307 self.end = istart;
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
308 new_range.start = iend
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
309 new_range.end = jend + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
310 else: #istart>=jstart
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
311 if iend <= jend:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
312 self.start = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
313 self.end = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
314 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
315 if self.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
316 self.start = jend + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
317 self.end = iend
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
318 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
319 self.start = iend
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
320 self.end = jend + 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
321 return new_range
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
322
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
323 ## Find the bin that contains the instance and compute its index
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
324 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
325 # @note Required for coordinate indexing via a hierarchical bin system
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
326 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
327 def findIdx(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
328 min_lvl = 3
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
329 max_lvl = 6
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
330 for bin_lvl in xrange(min_lvl, max_lvl):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
331 if getBin(self.start, bin_lvl) == getBin(self.end, bin_lvl):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
332 return getIdx(self.start, bin_lvl)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
333 return getIdx(self.start, max_lvl)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
334
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
335 ## Get a bin for fast database access
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
336 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
337 # @return bin number (float)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
338 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
339 def getBin(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
340 for i in xrange(3, 8):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
341 bin_lvl = pow(10, i)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
342 if int(self.start/bin_lvl) == int(self.end/bin_lvl):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
343 return float(bin_lvl+(int(self.start/bin_lvl)/1e10))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
344 bin_lvl = pow(10, 8)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
345 return float(bin_lvl+(int(self.start/bin_lvl)/1e10))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
346
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
347
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
348 # Functions
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
349
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
350 # Get the bin number of a coordinate according to the bin level. Required for coordinate indexing with hierarchical bin system
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
351 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
352 def getBin(val, bin_lvl):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
353 bin_size = pow(10, bin_lvl)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
354 return long(val / bin_size)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
355
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
356 # Get an index from a coordinate according to the bin level. Required for coordinate indexing with hierarchical bin system
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
357 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
358 def getIdx(val, bin_lvl):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
359 min_lvl = 3
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
360 max_lvl = 6
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
361 if bin_lvl >= max_lvl:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
362 return long((bin_lvl-min_lvl+1)*pow(10,max_lvl))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
363 return long(((bin_lvl-min_lvl+1)*pow(10,max_lvl))+getBin(val,bin_lvl))