annotate commons/core/coord/Range.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -0500
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 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
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 ## Record a region on a given sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 class Range( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 ## Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 # @param seqname the name of the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 # @param start the start coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 # @param end the end coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def __init__(self, seqname="", start=-1, end=-1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.seqname = seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self.start = int(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.end = int(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 ## Equal operator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 def __eq__(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 if self.seqname == o.seqname and self.start == o.start and self.end == o.end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 ## Unequal operator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 def __ne__(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 return not self.__eq__(o)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 ## Convert the object into a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 # @note used in 'print myObject'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 def __str__( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 return self.toString()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 ## Convert the object into a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 # @note used in 'repr(myObject)' for debugging
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 def __repr__( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 return self.toString().replace("\t",";")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 def setStart(self, start):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self.start = start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def setEnd(self, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.end = end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def setSeqName(self, seqName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.seqname = seqName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 ## Reset
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 def reset(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self.seqname = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.start = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self.end = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 ## Return the attributes as a formatted string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def toString(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 string = "%s" % (self.seqname)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 string += "\t%d" % (self.start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 string += "\t%d" % (self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 ## Show the attributes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def show(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 print self.toString()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 ## Return seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 def getSeqname(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 return self.seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 ## Return the start coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 def getStart(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 return self.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 ## Return the end coordinate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 def getEnd(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 return self.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 ## Return the lowest value between start and end coordinates
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 def getMin(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 return min(self.start, self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 ## Return the greatest value between start and end attributes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 def getMax(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 return max(self.start, self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 ## Return True if the instance is on the direct strand, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 def isOnDirectStrand(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if self.start <= self.end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 ## Return True if the instance is on the reverse strand, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 def isOnReverseStrand(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 return not self.isOnDirectStrand()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 ## Return '+' if the instance is on the direct strand, '-' otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 def getStrand(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 if self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 return '+'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 return '-'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 ## Exchange start and end coordinates
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 def reverse(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 tmp = self.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 self.start = self.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self.end = tmp
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 ## Return the length of the instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 # @warning old name is 'length'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 def getLength(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 return int(abs(self.start-self.end))+1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 ## Return True if the instance is empty, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 def isEmpty(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 if self.start==self.end and (self.start==0 or self.start==-1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 ## Set attributes from tuple
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 # @param tuple a tuple with (name,start,end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 def setFromTuple(self, tuple):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 self.seqname = tuple[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 self.start = int(tuple[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.end = int(tuple[2])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 ## Set attributes from string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 # @param string a string formatted like name<sep>start<sep>end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 # @param sep field separator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 def setFromString(self, string, sep="\t"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 if string[-1] == "\n":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 string = string[:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 self.setFromTuple( string.split(sep) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 ## Merge the instance with another Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 def merge(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 if self.seqname != o.seqname:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 if self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 self.start = min(self.getMin(), o.getMin())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 self.end = max(self.getMax(), o.getMax())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 self.start = max(self.getMax(), o.getMax())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 self.end = min(self.getMin(), o.getMin())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 ## Return True if the instance overlaps with another Range instance, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 def isOverlapping(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 if o.seqname != self.seqname:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 smin = self.getMin()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 smax = self.getMax()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 omin = o.getMin()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 omax = o.getMax()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 if omin <= smin and omax >= smax:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 if omin >= smin and omin <= smax or omax >= smin and omax <= smax:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 ## Return the length of the overlap between the instance and another Range, 0 if no overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 def getOverlapLength( self, o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 if self.isOverlapping( o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 if self.isIncludedIn( o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 return self.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 elif o.isIncludedIn( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 return o.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 elif o.getMin() <= self.getMax() and o.getMin() >= self.getMin():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 return self.getMax() - o.getMin() + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 elif o.getMax() <= self.getMax() and o.getMax() >= self.getMin():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 return o.getMax() - self.getMin() + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 ## Return True if the instance is included within another Range, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 # @note the min (respectively max) coordinates can be equal
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 def isIncludedIn( self, o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 if o.seqname != self.seqname:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 if self.getMin() >= o.getMin() and self.getMax() <= o.getMax():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 ## Return the distance between the start of the instance and the start of another Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 def getDistance(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 if self.isOnDirectStrand() == o.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 if self.isOverlapping(o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 elif self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 if self.start > o.start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 return self.start - o.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 return o.start - self.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 if self.start > o.start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 return self.end - o.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 return o.end - self.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 return -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 ## Remove in the instance the region overlapping with another Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 # @param o a Range instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 def diff(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 new_range = Range(self.seqname)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 if not self.isOverlapping(o) or self.seqname != o.seqname:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 return new_range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 istart = min(self.start, self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 iend = max(self.start, self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 jstart = min(o.start, o.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 jend = max(o.start, o.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 if istart < jstart:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 if iend <= jend:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 if self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 self.start = istart
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 self.end = jstart - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 self.start = jstart - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 self.end = istart
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 if self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 self.start = istart
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 self.end = jstart - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 new_range.start = jend + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 new_range.end = iend
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 self.start = jstart - 1;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 self.end = istart;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 new_range.start = iend
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 new_range.end = jend + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 else: #istart>=jstart
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 if iend <= jend:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 self.start = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 self.end = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 if self.isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 self.start = jend + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 self.end = iend
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 self.start = iend
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 self.end = jend + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 return new_range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 ## Find the bin that contains the instance and compute its index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 # @note Required for coordinate indexing via a hierarchical bin system
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 def findIdx(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 min_lvl = 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 max_lvl = 6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 for bin_lvl in xrange(min_lvl, max_lvl):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 if getBin(self.start, bin_lvl) == getBin(self.end, bin_lvl):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 return getIdx(self.start, bin_lvl)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 return getIdx(self.start, max_lvl)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 ## Get a bin for fast database access
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 # @return bin number (float)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 def getBin(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 for i in xrange(3, 8):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 bin_lvl = pow(10, i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 if int(self.start/bin_lvl) == int(self.end/bin_lvl):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 return float(bin_lvl+(int(self.start/bin_lvl)/1e10))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 bin_lvl = pow(10, 8)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 return float(bin_lvl+(int(self.start/bin_lvl)/1e10))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 # Functions
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 # Get the bin number of a coordinate according to the bin level. Required for coordinate indexing with hierarchical bin system
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 def getBin(val, bin_lvl):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 bin_size = pow(10, bin_lvl)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 return long(val / bin_size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 # Get an index from a coordinate according to the bin level. Required for coordinate indexing with hierarchical bin system
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 def getIdx(val, bin_lvl):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 min_lvl = 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 max_lvl = 6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 if bin_lvl >= max_lvl:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 return long((bin_lvl-min_lvl+1)*pow(10,max_lvl))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 return long(((bin_lvl-min_lvl+1)*pow(10,max_lvl))+getBin(val,bin_lvl))