EnsEMBL/Mapper/RangeRegistry.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 25 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 218 CC 28 SCAR 55.7
CC
28
Cov
0%
check_and_register
420.0
overlap_size
90.0
new
2.0
CRAP 263.5
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::Mapper::RangeRegistry
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Mapper::RangeRegistry;
38  
39 $rr = Bio::EnsEMBL::Mapper::RangeRegistry->new();
40  
41 # Get a fixed width chunk around the range of intereset. This
42 # will be used if any registration is actually necessary.
43 $chunk_start = ( $start >> 20 ) << 20 + 1;
44 $chunk_end = ( ( $end >> 20 ) + 1 ) << 20;
45  
46 # Check if any registration is necessary for the range. If it is
47 # register a large chunked area instead and return a listref of
48 # unregistered areas that need to be loaded.
49 if (
50 $pairs = $rr->check_and_register(
51 $id, $start, $end, $chunk_start, $chunk_end
52 ) )
53 {
54 foreach my $pair (@$pairs) {
55 my ( $pair_start, $pair_end ) = @$pair;
56 # Fetch mappings for these regions from the assembly table and
57 # load them into the mapper.
58 ...;
59 }
60 } else {
61 # The range ($start - $end) is already registered
62 ...;
63 }
64  
65 # Check if any registration is necessary. If it is register the
66 # region and return a listref of pairs that need to be loaded.
67 if ( $pairs = $rr->check_and_register( $id, $start, $end ) ) {
68 ...;
69 }
70  
71 =head1 DESCRIPTION
72  
73 This module maintains an internal list of registered regions and is
74 used to quickly ascertain if and what regions of a provided range need
75 registration.
76  
77 =head1 METHODS
78  
79 =cut
80  
81 package Bio::EnsEMBL::Mapper::RangeRegistry;
82 $Bio::EnsEMBL::Mapper::RangeRegistry::VERSION = '114.0.0_50'; # TRIAL
83 $Bio::EnsEMBL::Mapper::RangeRegistry::VERSION = '114.0.050';
84 use strict;
85  
86 use Bio::EnsEMBL::Utils::Exception qw(throw);
87 use integer;
88  
89 =head2 new
90  
91 Arg [1] : none
92 Example : my $rr = Bio::EnsEMBL::Mapper::RangeRegistry->new();
93 Description: Creates a new RangeRegistry object
94 Returntype : Bio::EnsEMBL::Mapper::RangeRegistry
95 Exceptions : none
96 Caller : AssemblyMapperAdaptor
97 Status : Stable
98  
99 =cut
100  
101 sub new {
102 my ($proto) = @_;
103  
104 my $class = ref($proto) || $proto;
105  
106 return bless( { 'registry' => {} }, $class );
107 }
108  
109 sub flush {
110 my ($self) = @_;
111 $self->{'registry'} = {};
112 }
113  
114 =head2 check_and_register
115  
116 Arg [1] : string $id
117 The id of the range to be checked/registered (e.g. a sequenceid)
118 Arg [2] : int $start
119 The start of the range to be checked
120 Arg [3] : int $end
121 The end of the range to be checked
122 Arg [4] : (optional) int $rstart
123 The start of the range to be registered
124 if the checked range was not fully registered
125 Arg [5] : (optional) int $rend
126 The end of the range to be registerd
127 if the checked range was not fully registered
128 Example : $ranges=$rr->check_and_register('X',500,600, 1,1_000_000));
129 Description: Checks the range registry to see if the entire range
130 denoted by ($id : $start-$end) is already registered. If
131 it already is, then undef is returned. If it is not then
132 the range specified by $rstart and $rend is registered, and
133 a list of regions that were required to completely register
134 $rstart-$rend is returned. If $rstart and $rend are not
135 defined they default to $start and $end respectively.
136  
137 The reason there is a single call to do both the checking and
138 registering is to reduce the overhead. Much of the work to
139 check if a range is registered is the same as registering a
140 region around that range.
141 Returntype : undef or listref of [start,end] range pairs
142 Exceptions : throw if rstart is greater than start
143 throw if rend is less than end
144 throw if end is less than start
145 throw if id, start, or end are not defined
146 Caller : AssemblyMapperAdaptor
147 Status : Stable
148  
149 =cut
150  
151 #"constants"
152 my $START = 0;
153 my $END = 1;
154  
155 sub check_and_register {
156 my ( $self, $id, $start, $end, $rstart, $rend ) = @_;
157  
158 $rstart = $start if ( !defined($rstart) );
159 $rend = $end if ( !defined($rend) );
160  
161 #
162 # Sanity checks
163 #
164 if ( !defined($id) || !defined($start) || !defined($end) ) {
165 throw("ID, start, end arguments are required");
166 }
167  
168 # The following was commented out due to Ensembl Genomes requirements
169 # for bacterial genomes.
170 #
171 ## if ( $start > $end ) {
172 ## throw( "start argument [$start] must be less than "
173 ## . "(or equal to) end argument [$end]" );
174 ## }
175 ##
176 ## if ( $rstart > $rend ) {
177 ## throw( "rstart argument [$rstart] must be less than "
178 ## . "(or equal to) rend argument [$rend] argument" );
179 ## }
180  
181 if ( $rstart > $start ) {
182 throw("rstart [$rstart] must be less than or equal to start [$start]");
183 }
184  
185 if ( $rend < $end ) {
186 throw("rend [$rend] must be greater than or equal to end [$end]");
187 }
188  
189 my $reg = $self->{'registry'};
190 my $list = $reg->{$id} ||= [];
191  
192 my @gap_pairs;
193  
194 my $len = scalar(@$list);
195  
196 if ( $len == 0 ) {
197 # this is the first request for this id, return a gap pair for the
198 # entire range and register it as seen
199 $list->[0] = [ $rstart, $rend ];
200 return [ [ $rstart, $rend ] ];
201 }
202  
203 #####
204 # loop through the list of existing ranges recording any "gaps" where
205 # the existing range does not cover part of the requested range
206 #
207  
208 my $start_idx = 0;
209 my $end_idx = $#$list;
210 my ( $mid_idx, $range );
211  
212 # binary search the relevant pairs
213 # helps if the list is big
214 while ( ( $end_idx - $start_idx ) > 1 ) {
215 $mid_idx = ( $start_idx + $end_idx ) >> 1;
216 $range = $list->[$mid_idx];
217  
218 if ( $range->[1] < $rstart ) {
219 $start_idx = $mid_idx;
220 } else {
221 $end_idx = $mid_idx;
222 }
223 }
224  
225 my ( $gap_start, $gap_end, $r_idx, $rstart_idx, $rend_idx );
226 $gap_start = $rstart;
227  
228 for ( my $CUR = $start_idx ; $CUR < $len ; $CUR++ ) {
229 my ( $pstart, $pend ) = @{ $list->[$CUR] };
230  
231 # no work needs to be done at all if we find a range pair that
232 # entirely overlaps the requested region
233 if ( $pstart <= $start && $pend >= $end ) {
234 return undef;
235 }
236  
237 # find adjacent or overlapping regions already registered
238 if ( $pend >= ( $rstart - 1 ) && $pstart <= ( $rend + 1 ) ) {
239 if ( !defined($rstart_idx) ) {
240 $rstart_idx = $CUR;
241 }
242 $rend_idx = $CUR;
243 }
244  
245 if ( $pstart > $rstart ) {
246 $gap_end = ( $rend < $pstart ) ? $rend : $pstart - 1;
247 push @gap_pairs, [ $gap_start, $gap_end ];
248 }
249  
250 $gap_start = ( $rstart > $pend ) ? $rstart : $pend + 1;
251  
252 # if($pstart > $rend && !defined($r_idx)) {
253 if ( $pend >= $rend && !defined($r_idx) ) {
254 $r_idx = $CUR;
255 last;
256 }
257 } ## end for ( my $CUR = $start_idx...
258  
259 # do we have to make another gap?
260 if ( $gap_start <= $rend ) {
261 push @gap_pairs, [ $gap_start, $rend ];
262 }
263  
264 #
265 # Merge the new range into the registered list
266 #
267 if ( defined($rstart_idx) ) {
268 my ( $new_start, $new_end );
269  
270 if ( $rstart < $list->[$rstart_idx]->[0] ) {
271 $new_start = $rstart;
272 } else {
273 $new_start = $list->[$rstart_idx]->[0];
274 }
275  
276 if ( $rend > $list->[$rend_idx]->[1] ) {
277 $new_end = $rend;
278 } else {
279 $new_end = $list->[$rend_idx]->[1];
280 }
281  
282 splice( @$list, $rstart_idx,
283 $rend_idx - $rstart_idx + 1,
284 [ $new_start, $new_end ] );
285  
286 } elsif ( defined($r_idx) ) {
287 splice( @$list, $r_idx, 0, [ $rstart, $rend ] );
288 } else {
289 push( @$list, [ $rstart, $rend ] );
290 }
291  
292 return \@gap_pairs;
293 } ## end sub check_and_register
294  
295 # overlap size is just added to make RangeRegistry generally more useful
296  
297 =head2 overlap_size
298  
299 Arg [1] : string $id
300 Arg [2] : int $start
301 Arg [3] : int $end
302 Example : my $overlap_size = $rr->( "chr1", 1, 100_000_000 )
303 Description: finds out how many bases of the given range are registered
304 Returntype : int
305 Exceptions : none
306 Caller : general
307 Status : Stable
308  
309 =cut
310  
311 sub overlap_size {
312 my ( $self, $id, $start, $end ) = @_;
313  
314 my $overlap = 0;
315  
316 if ( $start > $end ) { return 0 }
317  
318 my $reg = $self->{'registry'};
319 my $list = $reg->{$id} ||= [];
320  
321 my $len = scalar(@$list);
322  
323 if ( $len == 0 ) {
324 # this is the first request for this id, return a gap pair for the
325 # entire range and register it as seen
326 return 0;
327 }
328  
329 #####
330 # loop through the list of existing ranges recording any "gaps" where
331 # the existing range does not cover part of the requested range
332 #
333  
334 my $start_idx = 0;
335 my $end_idx = $#$list;
336 my ( $mid_idx, $range );
337  
338 # binary search the relevant pairs
339 # helps if the list is big
340 while ( ( $end_idx - $start_idx ) > 1 ) {
341 $mid_idx = ( $start_idx + $end_idx ) >> 1;
342 $range = $list->[$mid_idx];
343 if ( $range->[1] < $start ) {
344 $start_idx = $mid_idx;
345 } else {
346 $end_idx = $mid_idx;
347 }
348 }
349  
350 for ( my $CUR = $start_idx ; $CUR < $len ; $CUR++ ) {
351 my ( $pstart, $pend ) = @{ $list->[$CUR] };
352  
353 if ( $pstart > $end ) {
354 # No more interesting ranges here.
355 last;
356 }
357  
358 # no work needs to be done at all if we find a range pair that
359 # entirely overlaps the requested region
360 if ( $pstart <= $start && $pend >= $end ) {
361 $overlap = $end - $start + 1;
362 last;
363 }
364  
365 my $mstart = ( $start < $pstart ? $pstart : $start );
366 my $mend = ( $end < $pend ? $end : $pend );
367  
368 if ( $mend - $mstart >= 0 ) {
369 $overlap += ( $mend - $mstart + 1 );
370 }
371 }
372  
373 return $overlap;
374 } ## end sub overlap_size
375  
376  
377 # low level function to access the ranges
378 # only use for read access
379  
380 sub get_ranges {
381 my ( $self, $id ) = @_;
382  
383 return $self->{'registry'}->{$id};
384 }
385  
386 1;
387