EnsEMBL/DBSQL/AssemblyMapperAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 668 Branchbran 0.0% 0 / 180 Conditioncond 0.0% 0 / 42 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 930 CC 91 SCAR 53.5
CC
91
Cov
0%
register_chained
552.0
_register_chained_special
272.0
register_assembled
182.0
CRAP 210.8
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::DBSQL::AssemblyMapperAdaptor
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Registry;
38  
39 Bio::EnsEMBL::Registry->load_registry_from_db(
40 -host => 'ensembldb.ensembl.org',
41 -user => 'anonymous'
42 );
43  
44 $asma = Bio::EnsEMBL::Registry->get_adaptor( "human", "core",
45 "assemblymapper" );
46  
47 $csa = Bio::EnsEMBL::Registry->get_adaptor( "human", "core",
48 "coordsystem" );
49  
50 my $chr33_cs = $csa->fetch_by_name( 'chromosome', 'NCBI33' );
51 my $chr34_cs = $csa->fetch_by_name( 'chromosome', 'NCBI34' );
52 my $ctg_cs = $csa->fetch_by_name('contig');
53 my $clone_cs = $csa->fetch_by_name('clone');
54  
55 my $chr_ctg_mapper =
56 $asma->fetch_by_CoordSystems( $chr33_cs, $ctg_cs );
57  
58 my $ncbi33_ncbi34_mapper =
59 $asm_adptr->fetch_by_CoordSystems( $chr33, $chr34 );
60  
61 my $ctg_clone_mapper =
62 $asm_adptr->fetch_by_CoordSystems( $ctg_cs, $clone_cs );
63  
64  
65 =head1 DESCRIPTION
66  
67 Adaptor for handling Assembly mappers. This is a I<Singleton> class.
68 ie: There is only one per database (C<DBAdaptor>).
69  
70 This is used to retrieve mappers between any two coordinate systems
71 whose makeup is described by the assembly table. Currently one step
72 (explicit) and two step (implicit) pairwise mapping is supported. In
73 one-step mapping an explicit relationship between the coordinate systems
74 is defined in the assembly table. In two-step 'chained' mapping no
75 explicit mapping is present but the coordinate systems must share a
76 common mapping to an intermediate coordinate system.
77  
78 =head1 METHODS
79  
80 =cut
81  
82 package Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor;
83 $Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor::VERSION = '114.0.0_50'; # TRIAL
84 $Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor::VERSION = '114.0.050';
85 use vars qw(@ISA);
86 use strict;
87  
88 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
89 use Bio::EnsEMBL::AssemblyMapper;
90 use Bio::EnsEMBL::ChainedAssemblyMapper;
91 use Bio::EnsEMBL::TopLevelAssemblyMapper;
92  
93 use Bio::EnsEMBL::Utils::Cache; #CPAN LRU cache
94 use Bio::EnsEMBL::Utils::Exception qw(throw);
95 use Bio::EnsEMBL::Utils::SeqRegionCache;
96  
97 use integer; #do proper arithmetic bitshifts
98  
99 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
100  
101  
102 my $CHUNKFACTOR = 20; # 2^20 = approx. 10^6
103  
104 =head2 new
105  
106 Arg [1] : Bio::EnsEMBL::DBAdaptor $dbadaptor the adaptor for
107 the database this assembly mapper is using.
108 Example : my $asma = new Bio::EnsEMBL::AssemblyMapperAdaptor($dbadaptor);
109 Description: Creates a new AssemblyMapperAdaptor object
110 Returntype : Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor
111 Exceptions : none
112 Caller : Bio::EnsEMBL::DBSQL::DBAdaptor
113 Status : Stable
114  
115 =cut
116  
117 sub new {
118 my($class, $dbadaptor) = @_;
119  
120 my $self = $class->SUPER::new($dbadaptor);
121  
122 $self->{'_asm_mapper_cache'} = {};
123  
124 # use a shared cache (for this database) that contains info about
125 # seq regions
126 my $seq_region_cache = $self->db->get_SeqRegionCache();
127 $self->{'sr_name_cache'} = $seq_region_cache->{'name_cache'};
128 $self->{'sr_id_cache'} = $seq_region_cache->{'id_cache'};
129  
130 return $self;
131 }
132  
133  
134  
135 =head2 cache_seq_ids_with_mult_assemblys
136  
137 Example : $self->adaptor->cache_seq_ids_with_mult_assemblys();
138 Description: Creates a hash of the component seq region ids that
139 map to more than one assembly from the assembly table.
140 Retruntype : none
141 Exceptions : none
142 Caller : AssemblyMapper, ChainedAssemblyMapper
143 Status : At Risk
144  
145 =cut
146  
147 sub cache_seq_ids_with_mult_assemblys{
148 my $self = shift;
149 my %multis;
150  
151 return if (defined($self->{'multi_seq_ids'}));
152  
153 $self->{'multi_seq_ids'} = {};
154  
155 my $sql = qq(
156 SELECT sra.seq_region_id
157 FROM seq_region_attrib sra,
158 attrib_type at,
159 seq_region sr,
160 coord_system cs
161 WHERE sra.attrib_type_id = at.attrib_type_id
162 AND code = "MultAssem"
163 AND sra.seq_region_id = sr.seq_region_id
164 AND sr.coord_system_id = cs.coord_system_id
165 AND cs.species_id = ?);
166  
167 my $sth = $self->prepare($sql);
168  
169 $sth->bind_param( 1, $self->species_id(), SQL_INTEGER );
170  
171 $sth->execute();
172  
173 my ($seq_region_id);
174  
175 $sth->bind_columns(\$seq_region_id);
176  
177 while($sth->fetch()) {
178 $self->{'multi_seq_ids'}->{$seq_region_id} = 1;
179 }
180 $sth->finish;
181 }
182  
183  
184 =head2 fetch_by_CoordSystems
185  
186 Arg [1] : Bio::EnsEMBL::CoordSystem $cs1
187 One of the coordinate systems to retrieve the mapper
188 between
189 Arg [2] : Bio::EnsEMBL::CoordSystem $cs2
190 The other coordinate system to map between
191 Description: Retrieves an Assembly mapper for two coordinate
192 systems whose relationship is described in the
193 assembly table.
194  
195 The ordering of the coodinate systems is arbitrary.
196 The following two statements are equivalent:
197 $mapper = $asma->fetch_by_CoordSystems($cs1,$cs2);
198 $mapper = $asma->fetch_by_CoordSystems($cs2,$cs1);
199 Returntype : Bio::EnsEMBL::AssemblyMapper
200 Exceptions : wrong argument types
201 Caller : general
202 Status : Stable
203  
204 =cut
205  
206 sub fetch_by_CoordSystems {
207 my $self = shift;
208 my $cs1 = shift;
209 my $cs2 = shift;
210  
211 if(!ref($cs1) || !$cs1->isa('Bio::EnsEMBL::CoordSystem')) {
212 throw("cs1 argument must be a Bio::EnsEMBL::CoordSystem.");
213 }
214 if(!ref($cs2) || !$cs2->isa('Bio::EnsEMBL::CoordSystem')) {
215 throw("cs2 argument must be a Bio::EnsEMBL::CoordSystem.");
216 }
217  
218 # if($cs1->equals($cs2)) {
219 # throw("Cannot create mapper between same coord systems: " .
220 # $cs1->name . " " . $cs1->version);
221 # }
222  
223 if($cs1->is_top_level()) {
224 return Bio::EnsEMBL::TopLevelAssemblyMapper->new($self, $cs1, $cs2);
225 }
226 if($cs2->is_top_level()) {
227 return Bio::EnsEMBL::TopLevelAssemblyMapper->new($self, $cs2, $cs1);
228 }
229  
230 my $csa = $self->db->get_CoordSystemAdaptor();
231  
232 #retrieve the shortest possible mapping path between these systems
233 my @mapping_path = @{$csa->get_mapping_path($cs1,$cs2)};
234  
235 if(!@mapping_path) {
236  
237 return undef;
238 }
239  
240 my $key = join(':', map({defined($_)?$_->dbID():"-"} @mapping_path));
241  
242 my $asm_mapper = $self->{'_asm_mapper_cache'}->{$key};
243  
244 return $asm_mapper if($asm_mapper);
245  
246 if(@mapping_path == 1) {
247 throw("Incorrect mapping path defined in meta table. " .
248 "0 step mapping encountered between:\n" .
249 $cs1->name() . " " . $cs1->version() . " and " . $cs2->name . " " .
250 $cs2->version());
251 }
252  
253 if(@mapping_path == 2) {
254 #1 step regular mapping
255 $asm_mapper = Bio::EnsEMBL::AssemblyMapper->new($self, @mapping_path);
256  
257 # If you want multiple pieces on two seqRegions to map to each other
258 # you need to make an assembly.mapping entry that is seperated with a #
259 # instead of an |.
260  
261 $self->{'_asm_mapper_cache'}->{$key} = $asm_mapper;
262 return $asm_mapper;
263 }
264  
265 if(@mapping_path == 3) {
266 #two step chained mapping
267 $asm_mapper = Bio::EnsEMBL::ChainedAssemblyMapper->new($self,@mapping_path);
268 #in multi-step mapping it is possible get requests with the
269 #coordinate system ordering reversed since both mappings directions
270 #cache on both orderings just in case
271 #e.g. chr <-> contig <-> clone and clone <-> contig <-> chr
272  
273 $self->{'_asm_mapper_cache'}->{$key} = $asm_mapper;
274 $key = join(':', map({defined($_)?$_->dbID():"-"} reverse(@mapping_path)));
275 $self->{'_asm_mapper_cache'}->{$key} = $asm_mapper;
276 return $asm_mapper;
277 }
278  
279 throw("Only 1 and 2 step coordinate system mapping is currently\n" .
280 "supported. Mapping between " .
281 $cs1->name() . " " . $cs1->version() . " and " .
282 $cs2->name() . " " . $cs2->version() .
283 " requires ". (scalar(@mapping_path)-1) . " steps.");
284 }
285  
286  
287  
288 =head2 register_assembled
289  
290 Arg [1] : Bio::EnsEMBL::AssemblyMapper $asm_mapper
291 A valid AssemblyMapper object
292 Arg [2] : integer $asm_seq_region
293 The dbID of the seq_region to be registered
294 Arg [3] : int $asm_start
295 The start of the region to be registered
296 Arg [4] : int $asm_end
297 The end of the region to be registered
298 Description: Declares an assembled region to the AssemblyMapper.
299 This extracts the relevant data from the assembly
300 table and stores it in Mapper internal to the $asm_mapper.
301 It therefore must be called before any mapping is
302 attempted on that region. Otherwise only gaps will
303 be returned. Note that the AssemblyMapper automatically
304 calls this method when the need arises.
305 Returntype : none
306 Exceptions : throw if the seq_region to be registered does not exist
307 or if it associated with multiple assembled pieces (bad data
308 in assembly table)
309 Caller : Bio::EnsEMBL::AssemblyMapper
310 Status : Stable
311  
312 =cut
313  
314  
315 sub register_assembled {
316 my $self = shift;
317 my $asm_mapper = shift;
318 my $asm_seq_region = shift;
319 my $asm_start = shift;
320 my $asm_end = shift;
321  
322 if(!ref($asm_mapper) || !$asm_mapper->isa('Bio::EnsEMBL::AssemblyMapper')) {
323 throw("Bio::EnsEMBL::AssemblyMapper argument expected");
324 }
325  
326 throw("asm_seq_region argument expected") if(!defined($asm_seq_region));
327 throw("asm_start argument expected") if(!defined($asm_start));
328 throw("asm_end argument expected") if(!defined($asm_end));
329  
330 my $asm_cs_id = $asm_mapper->assembled_CoordSystem->dbID();
331 my $cmp_cs_id = $asm_mapper->component_CoordSystem->dbID();
332  
333 #split up the region to be registered into fixed chunks
334 #this allows us to keep track of regions that have already been
335 #registered and also works under the assumption that if a small region
336 #is requested it is likely that other requests will be made in the
337 #vicinity (the minimum size registered the chunksize (2^chunkfactor)
338  
339 my @chunk_regions;
340 #determine span of chunks
341 #bitwise shift right is fast and easy integer division
342  
343 my($start_chunk, $end_chunk);
344  
345 $start_chunk = $asm_start >> $CHUNKFACTOR;
346 $end_chunk = $asm_end >> $CHUNKFACTOR;
347  
348 # inserts have start = end + 1, on boundary condition start_chunk
349 # could be less than end chunk
350 if($asm_start == $asm_end + 1) {
351 ($start_chunk, $end_chunk) = ($end_chunk, $start_chunk);
352 }
353  
354 #find regions of continuous unregistered chunks
355 my $i;
356 my ($begin_chunk_region,$end_chunk_region);
357 for ($i = $start_chunk; $i <= $end_chunk; $i++) {
358 if($asm_mapper->have_registered_assembled($asm_seq_region, $i)) {
359 if(defined($begin_chunk_region)) {
360 #this is the end of an unregistered region.
361 my $region = [($begin_chunk_region << $CHUNKFACTOR),
362 (($end_chunk_region+1) << $CHUNKFACTOR)-1];
363 push @chunk_regions, $region;
364 $begin_chunk_region = $end_chunk_region = undef;
365 }
366 } else {
367 $begin_chunk_region = $i if(!defined($begin_chunk_region));
368 $end_chunk_region = $i+1;
369 $asm_mapper->register_assembled($asm_seq_region,$i);
370 }
371 }
372  
373 #the last part may have been an unregistered region too
374 if(defined($begin_chunk_region)) {
375 my $region = [($begin_chunk_region << $CHUNKFACTOR),
376 (($end_chunk_region+1) << $CHUNKFACTOR) -1];
377 push @chunk_regions, $region;
378 }
379  
380 return if(!@chunk_regions);
381  
382 # keep the Mapper to a reasonable size
383 if( $asm_mapper->size() > $asm_mapper->max_pair_count() ) {
384 $asm_mapper->flush();
385 #we now have to go and register the entire requested region since we
386 #just flushed everything
387  
388 @chunk_regions = ( [ ( $start_chunk << $CHUNKFACTOR)
389 , (($end_chunk+1) << $CHUNKFACTOR)-1 ] );
390  
391 for( my $i = $start_chunk; $i <= $end_chunk; $i++ ) {
392 $asm_mapper->register_assembled( $asm_seq_region, $i );
393 }
394 }
395  
396 # my $asm_seq_region_id =
397 # $self->_seq_region_name_to_id($asm_seq_region,$asm_cs_id);
398  
399 # Retrieve the description of how the assembled region is made from
400 # component regions for each of the continuous blocks of unregistered,
401 # chunked regions
402  
403 my $q = qq{
404 SELECT
405 asm.cmp_start,
406 asm.cmp_end,
407 asm.cmp_seq_region_id,
408 sr.name,
409 sr.length,
410 asm.ori,
411 asm.asm_start,
412 asm.asm_end
413 FROM
414 assembly asm, seq_region sr
415 WHERE
416 asm.asm_seq_region_id = ? AND
417 ? <= asm.asm_end AND
418 ? >= asm.asm_start AND
419 asm.cmp_seq_region_id = sr.seq_region_id AND
420 sr.coord_system_id = ?
421 };
422  
423 my $sth = $self->prepare($q);
424  
425 foreach my $region (@chunk_regions) {
426 my($region_start, $region_end) = @$region;
427 $sth->bind_param(1,$asm_seq_region,SQL_INTEGER);
428 $sth->bind_param(2,$region_start,SQL_INTEGER);
429 $sth->bind_param(3,$region_end,SQL_INTEGER);
430 $sth->bind_param(4,$cmp_cs_id,SQL_INTEGER);
431  
432 $sth->execute();
433  
434 my($cmp_start, $cmp_end, $cmp_seq_region_id, $cmp_seq_region, $ori,
435 $asm_start, $asm_end, $cmp_seq_region_length);
436  
437 $sth->bind_columns(\$cmp_start, \$cmp_end, \$cmp_seq_region_id,
438 \$cmp_seq_region, \$cmp_seq_region_length, \$ori,
439 \$asm_start, \$asm_end);
440  
441 #
442 # Load the unregistered regions of the mapper
443 #
444 while($sth->fetch()) {
445 next if($asm_mapper->have_registered_component($cmp_seq_region_id)
446 and !defined($self->{'multi_seq_ids'}->{$cmp_seq_region_id}));
447 $asm_mapper->register_component($cmp_seq_region_id);
448 $asm_mapper->mapper->add_map_coordinates(
449 $asm_seq_region, $asm_start, $asm_end,
450 $ori,
451 $cmp_seq_region_id, $cmp_start, $cmp_end);
452  
453 my $arr = [ $cmp_seq_region_id, $cmp_seq_region,
454 $cmp_cs_id, $cmp_seq_region_length ];
455  
456 $self->{'sr_name_cache'}->{"$cmp_seq_region:$cmp_cs_id"} = $arr;
457 $self->{'sr_id_cache'}->{"$cmp_seq_region_id"} = $arr;
458 }
459 }
460  
461 $sth->finish();
462 }
463  
464  
465  
466 sub _seq_region_name_to_id {
467 my $self = shift;
468 my $sr_name = shift;
469 my $cs_id = shift;
470  
471 if(!defined($sr_name) or
472 !defined($cs_id)){
473 throw('seq_region_name and coord_system_id args are required');
474 }
475  
476 my $arr = $self->{'sr_name_cache'}->{"$sr_name:$cs_id"};
477 if( $arr ) {
478 return $arr->[0];
479 }
480  
481 # Get the seq_region_id via the name. This would be quicker if we just
482 # used internal ids instead but stored but then we lose the ability
483 # the transform accross databases with different internal ids
484  
485 my $sth = $self->prepare("SELECT seq_region_id, length " .
486 "FROM seq_region " .
487 "WHERE name = ? AND coord_system_id = ?");
488  
489 $sth->bind_param(1,$sr_name,SQL_VARCHAR);
490 $sth->bind_param(2,$cs_id,SQL_INTEGER);
491 $sth->execute();
492  
493 my @row = $sth->fetchrow_array();
494 unless ( @row ) {
495 throw("No-existent seq_region [$sr_name] in coord system $cs_id");
496 }
497 my @more = $sth->fetchrow_array();
498 if ( @more ) {
499 throw("Ambiguous seq_region [$sr_name] in coord system $cs_id");
500 }
501  
502 my ($sr_id, $sr_length) = @row;
503 $sth->finish();
504  
505 $arr = [ $sr_id, $sr_name, $cs_id, $sr_length ];
506  
507 $self->{'sr_name_cache'}->{"$sr_name:$cs_id"} = $arr;
508 $self->{'sr_id_cache'}->{"$sr_id"} = $arr;
509  
510 return $sr_id;
511 }
512  
513 sub _seq_region_id_to_name {
514 my $self = shift;
515 my $sr_id = shift;
516  
517 ($sr_id) || throw('seq_region_is required');
518  
519 my $arr = $self->{'sr_id_cache'}->{"$sr_id"};
520 if( $arr ) {
521 return $arr->[1];
522 }
523  
524 # Get the seq_region name via the id. This would be quicker if we just
525 # used internal ids instead but stored but then we lose the ability
526 # the transform accross databases with different internal ids
527  
528 my $sth = $self->prepare("SELECT name, length ,coord_system_id " .
529 "FROM seq_region " .
530 "WHERE seq_region_id = ? ");
531  
532 $sth->bind_param(1,$sr_id,SQL_INTEGER);
533 $sth->execute();
534  
535 my @row = $sth->fetchrow_array();
536 if(!$sth->rows() == 1) {
537 throw("non-existant seq_region [$sr_id]");
538 }
539  
540 my ($sr_name, $sr_length, $cs_id) = @row;
541 $sth->finish();
542  
543 $arr = [ $sr_id, $sr_name, $cs_id, $sr_length ];
544  
545 $self->{'sr_name_cache'}->{"$sr_name:$cs_id"} = $arr;
546 $self->{'sr_id_cache'}->{"$sr_id"} = $arr;
547  
548 return $sr_name;
549 }
550  
551  
552 =head2 register_component
553  
554 Arg [1] : Bio::EnsEMBL::AssemblyMapper $asm_mapper
555 A valid AssemblyMapper object
556 Arg [2] : integer $cmp_seq_region
557 The dbID of the seq_region to be registered
558 Description: Declares a component region to the AssemblyMapper.
559 This extracts the relevant data from the assembly
560 table and stores it in Mapper internal to the $asm_mapper.
561 It therefore must be called before any mapping is
562 attempted on that region. Otherwise only gaps will
563 be returned. Note that the AssemblyMapper automatically
564 calls this method when the need arises.
565 Returntype : none
566 Exceptions : throw if the seq_region to be registered does not exist
567 or if it associated with multiple assembled pieces (bad data
568 in assembly table)
569 Caller : Bio::EnsEMBL::AssemblyMapper
570 Status : Stable
571  
572 =cut
573  
574 sub register_component {
575 my $self = shift;
576 my $asm_mapper = shift;
577 my $cmp_seq_region = shift;
578  
579 if(!ref($asm_mapper) || !$asm_mapper->isa('Bio::EnsEMBL::AssemblyMapper')) {
580 throw("Bio::EnsEMBL::AssemblyMapper argument expected");
581 }
582  
583 if(!defined($cmp_seq_region)) {
584 throw("cmp_seq_region argument expected");
585 }
586  
587 my $cmp_cs_id = $asm_mapper->component_CoordSystem()->dbID();
588 my $asm_cs_id = $asm_mapper->assembled_CoordSystem()->dbID();
589  
590 #do nothing if this region is already registered or special case
591 return if($asm_mapper->have_registered_component($cmp_seq_region)
592 and !defined($self->{'multi_seq_ids'}->{$cmp_seq_region}));
593  
594 # my $cmp_seq_region_id =
595 # $self->_seq_region_name_to_id($cmp_seq_region, $cmp_cs_id);
596  
597 # Determine what part of the assembled region this component region makes up
598  
599 my $q = qq{
600 SELECT
601 asm.asm_start,
602 asm.asm_end,
603 asm.asm_seq_region_id,
604 sr.name,
605 sr.length
606 FROM
607 assembly asm, seq_region sr
608 WHERE
609 asm.cmp_seq_region_id = ? AND
610 asm.asm_seq_region_id = sr.seq_region_id AND
611 sr.coord_system_id = ?
612 };
613  
614 my $sth = $self->prepare($q);
615 $sth->bind_param(1,$cmp_seq_region,SQL_INTEGER);
616 $sth->bind_param(2,$asm_cs_id,SQL_INTEGER);
617 $sth->execute();
618  
619 my @rows = $sth->fetchrow_array();
620  
621 if($sth->rows() == 0) {
622 #this component is not used in the assembled part i.e. gap
623 $asm_mapper->register_component($cmp_seq_region);
624 $sth->finish();
625 return;
626 }
627  
628 #we do not currently support components mapping to multiple assembled
629 # make sure that you've got the correct mapping in the meta-table :
630 # chromosome:EquCab2#contig ( use'#' for multiple mappings )
631 # chromosome:EquCab2|contig ( use '|' delimiter for 1-1 mappings )
632 #
633 my @more = $sth->fetchrow_array();
634 if($sth->rows() != 1) {
635 $sth->finish();
636 throw("Multiple assembled regions for single " .
637 "component region cmp_seq_region_id=[$cmp_seq_region]\n".
638 "Remember that multiple mappings use the \#-operaator".
639 " in the meta-table (i.e. chromosome:EquCab2\#contig\n");
640 }
641  
642 my ($asm_start, $asm_end, $asm_seq_region_id,
643 $asm_seq_region, $asm_seq_region_length) = @rows;
644  
645 my $arr = [ $asm_seq_region_id, $asm_seq_region,
646 $asm_cs_id, $asm_seq_region_length ];
647  
648 $self->{'sr_name_cache'}->{"$asm_seq_region:$asm_cs_id"} = $arr;
649 $self->{'sr_id_cache'}->{"$asm_seq_region_id"} = $arr;
650  
651 $sth->finish();
652  
653 # Register the corresponding assembled region. This allows a us to
654 # register things in assembled chunks which allows us to:
655 # (1) Keep track of what assembled regions are registered
656 # (2) Use locality of reference (if they want something in same general
657 # region it will already be registered).
658  
659 $self->register_assembled($asm_mapper,$asm_seq_region_id,$asm_start,$asm_end);
660 }
661  
662  
663  
664 =head2 register_chained
665  
666 Arg [1] : Bio::EnsEMBL::ChainedAssemblyMapper $casm_mapper
667 The chained assembly mapper to register regions on
668 Arg [2] : string $from ('first' or 'last')
669 The direction we are registering from, and the name of the
670 internal mapper.
671 Arg [3] : string $seq_region_name
672 The name of the seqregion we are registering on
673 Arg [4] : listref $ranges
674 A list of ranges to register (in [$start,$end] tuples).
675 Arg [5] : (optional) $to_slice
676 Only register those on this Slice.
677 Description: Registers a set of ranges on a chained assembly mapper.
678 This function is at the heart of the chained mapping process.
679 It retrieves information from the assembly table and
680 dynamically constructs the mappings between two coordinate
681 systems which are 2 mapping steps apart. It does this by using
682 two internal mappers to load up a third mapper which is
683 actually used by the ChainedAssemblyMapper to perform the
684 mapping.
685  
686 This method must be called before any mapping is
687 attempted on regions of interest, otherwise only gaps will
688 be returned. Note that the ChainedAssemblyMapper automatically
689 calls this method when the need arises.
690 Returntype : none
691 Exceptions : throw if the seq_region to be registered does not exist
692 or if it associated with multiple assembled pieces (bad data
693 in assembly table)
694  
695 throw if the mapping between the coordinate systems cannot
696 be performed in two steps, which means there is an internal
697 error in the data in the meta table or in the code that creates
698 the mapping paths.
699 Caller : Bio::EnsEMBL::AssemblyMapper
700 Status : Stable
701  
702 =cut
703  
704 sub register_chained {
705 my $self = shift;
706 my $casm_mapper = shift;
707 my $from = shift;
708 my $seq_region_id = shift;
709 my $ranges = shift;
710 my $to_slice = shift;
711  
712 my $to_seq_region_id;
713 if(defined($to_slice)){
714 if($casm_mapper->first_CoordSystem()->equals($casm_mapper->last_CoordSystem())){
715 return $self->_register_chained_special($casm_mapper, $from, $seq_region_id, $ranges, $to_slice);
716 }
717 $to_seq_region_id = $to_slice->get_seq_region_id();
718 if(!defined($to_seq_region_id)){
719 die "Could not get seq_region_id for to_slice".$to_slice->seq_region_name."\n";
720 }
721 }
722  
723 my ($start_name, $start_mid_mapper, $start_cs, $start_registry,
724 $end_name, $end_mid_mapper, $end_cs, $end_registry);
725  
726 if($from eq 'first') {
727 $start_name = 'first';
728 $start_mid_mapper = $casm_mapper->first_middle_mapper();
729 $start_cs = $casm_mapper->first_CoordSystem();
730 $start_registry = $casm_mapper->first_registry();
731 $end_mid_mapper = $casm_mapper->last_middle_mapper();
732 $end_cs = $casm_mapper->last_CoordSystem();
733 $end_registry = $casm_mapper->last_registry();
734 $end_name = 'last';
735 } elsif($from eq 'last') {
736 $start_name = 'last';
737 $start_mid_mapper = $casm_mapper->last_middle_mapper();
738 $start_cs = $casm_mapper->last_CoordSystem();
739 $start_registry = $casm_mapper->last_registry();
740 $end_mid_mapper = $casm_mapper->first_middle_mapper();
741 $end_cs = $casm_mapper->first_CoordSystem();
742 $end_registry = $casm_mapper->first_registry();
743 $end_name = 'first';
744 } else {
745 throw("Invalid from argument: [$from], must be 'first' or 'last'");
746 }
747  
748 my $combined_mapper = $casm_mapper->first_last_mapper();
749 my $mid_cs = $casm_mapper->middle_CoordSystem();
750 my $mid_name = 'middle';
751 my $csa = $self->db->get_CoordSystemAdaptor();
752  
753 # Check for the simple case where the ChainedMapper is short
754 if( ! defined $mid_cs ) {
755 $start_mid_mapper = $combined_mapper;
756 }
757  
758  
759 ##############
760 # obtain the first half of the mappings and load them into the start mapper
761 #
762  
763 #ascertain which is component and which is actually assembled coord system
764 my @path;
765  
766 # check for the simple case, where the ChainedMapper is short
767 if( defined $mid_cs ) {
768 @path = @{$csa->get_mapping_path($start_cs, $mid_cs)};
769 } else {
770 @path = @{$csa->get_mapping_path( $start_cs, $end_cs )};
771 }
772  
773 if(@path != 2 && defined( $path[1] )) {
774 my $path = join(',', map({$_->name .' '. $_->version} @path));
775 my $len = scalar(@path) - 1;
776 throw("Unexpected mapping path between start and intermediate " .
777 "coord systems (". $start_cs->name . " " . $start_cs->version .
778 " and " . $mid_cs->name . " " . $mid_cs->version . ")." .
779 "\nExpected path length 1, got $len. " .
780 "(path=$path)");
781 }
782  
783 my $sth;
784 my ($asm_cs,$cmp_cs);
785 $asm_cs = $path[0];
786 $cmp_cs = $path[-1];
787  
788 #the SQL varies depending on whether we are coming from assembled or
789 #component coordinate system
790  
791 my $asm2cmp = (<<ASMCMP);
792 SELECT
793 asm.cmp_start,
794 asm.cmp_end,
795 asm.cmp_seq_region_id,
796 sr.name,
797 sr.length,
798 asm.ori,
799 asm.asm_start,
800 asm.asm_end
801 FROM
802 assembly asm, seq_region sr
803 WHERE
804 asm.asm_seq_region_id = ? AND
805 ? <= asm.asm_end AND
806 ? >= asm.asm_start AND
807 asm.cmp_seq_region_id = sr.seq_region_id AND
808 sr.coord_system_id = ?
809 ASMCMP
810  
811  
812 my $cmp2asm = (<<CMPASM);
813 SELECT
814 asm.asm_start,
815 asm.asm_end,
816 asm.asm_seq_region_id,
817 sr.name,
818 sr.length,
819 asm.ori,
820 asm.cmp_start,
821 asm.cmp_end
822 FROM
823 assembly asm, seq_region sr
824 WHERE
825 asm.cmp_seq_region_id = ? AND
826 ? <= asm.cmp_end AND
827 ? >= asm.cmp_start AND
828 asm.asm_seq_region_id = sr.seq_region_id AND
829 sr.coord_system_id = ?
830 CMPASM
831  
832 my $asm2cmp_sth;
833 my $cmp2asm_sth;
834 if(defined($to_slice)){
835 my $to_cs = $to_slice->coord_system;
836 if($asm_cs->equals($to_cs)){
837 $asm2cmp_sth = $self->prepare($asm2cmp);
838 $cmp2asm_sth = $self->prepare($cmp2asm." AND asm.asm_seq_region_id = $to_seq_region_id");
839 }
840 elsif($cmp_cs->equals($to_cs)){
841 $asm2cmp_sth = $self->prepare($asm2cmp." AND asm.cmp_seq_region_id = $to_seq_region_id");
842 $cmp2asm_sth = $self->prepare($cmp2asm);
843 }
844 else{
845 $asm2cmp_sth = $self->prepare($asm2cmp);
846 $cmp2asm_sth = $self->prepare($cmp2asm);
847 }
848 }
849 else{
850 $asm2cmp_sth = $self->prepare($asm2cmp);
851 $cmp2asm_sth = $self->prepare($cmp2asm);
852 }
853  
854  
855  
856 $sth = ($asm_cs->equals($start_cs)) ? $asm2cmp_sth : $cmp2asm_sth;
857  
858 my $mid_cs_id;
859  
860 # check for the simple case where the ChainedMapper is short
861 if( defined $mid_cs ) {
862 $mid_cs_id = $mid_cs->dbID();
863 } else {
864 $mid_cs_id = $end_cs->dbID();
865 }
866  
867 my @mid_ranges;
868 my @start_ranges;
869  
870 #need to perform the query for each unregistered range
871 foreach my $range (@$ranges) {
872 my ($start, $end) = @$range;
873 $sth->bind_param(1,$seq_region_id,SQL_INTEGER);
874 $sth->bind_param(2,$start,SQL_INTEGER);
875 $sth->bind_param(3,$end,SQL_INTEGER);
876 $sth->bind_param(4,$mid_cs_id,SQL_INTEGER);
877 $sth->execute();
878  
879 #load the start <-> mid mapper with the results and record the mid cs
880 #ranges we just added to the mapper
881  
882 my ($mid_start, $mid_end, $mid_seq_region_id, $mid_seq_region, $mid_length,
883 $ori, $start_start, $start_end);
884  
885 $sth->bind_columns(\$mid_start, \$mid_end, \$mid_seq_region_id,
886 \$mid_seq_region, \$mid_length, \$ori, \$start_start,
887 \$start_end);
888  
889 while($sth->fetch()) {
890  
891 if( defined $mid_cs ) {
892 $start_mid_mapper->add_map_coordinates
893 (
894 $seq_region_id,$start_start, $start_end, $ori,
895 $mid_seq_region_id, $mid_start, $mid_end
896 );
897 } else {
898 if( $from eq "first" ) {
899 $combined_mapper->add_map_coordinates
900 (
901 $seq_region_id,$start_start, $start_end, $ori,
902 $mid_seq_region_id, $mid_start, $mid_end
903 );
904 } else {
905 $combined_mapper->add_map_coordinates
906 (
907 $mid_seq_region_id, $mid_start, $mid_end, $ori,
908 $seq_region_id,$start_start, $start_end
909 );
910 }
911 }
912  
913 #update sr_name cache
914 my $arr = [ $mid_seq_region_id, $mid_seq_region,
915 $mid_cs_id, $mid_length ];
916  
917 $self->{'sr_name_cache'}->{"$mid_seq_region:$mid_cs_id"} = $arr;
918 $self->{'sr_id_cache'}->{"$mid_seq_region_id"} = $arr;
919  
920 push @mid_ranges,[$mid_seq_region_id,$mid_seq_region,
921 $mid_start,$mid_end];
922 push @start_ranges, [ $seq_region_id, $start_start, $start_end ];
923  
924 #the region that we actually register may actually be larger or smaller
925 #than the region that we wanted to register.
926 #register the intersection of the region so we do not end up doing
927 #extra work later
928  
929 if($start_start < $start || $start_end > $end) {
930 $start_registry->check_and_register($seq_region_id,$start_start,
931 $start_end);
932 }
933 }
934 $sth->finish();
935 }
936  
937 # in the one step case, we load the mid ranges in the
938 # last_registry and we are done
939 if( ! defined $mid_cs ) {
940 for my $range ( @mid_ranges ) {
941 $end_registry->check_and_register( $range->[0], $range->[2],
942 $range->[3] );
943 }
944  
945 # and thats it for the simple case ...
946 return;
947 }
948  
949  
950 ###########
951 # now the second half of the mapping
952 # perform another query and load the mid <-> end mapper using the mid cs
953 # ranges
954 #
955  
956 #ascertain which is component and which is actually assembled coord system
957 @path = @{$csa->get_mapping_path($mid_cs, $end_cs)};
958 if(@path == 2 || ( @path == 3 && !defined $path[1])) {
959  
960 $asm_cs = $path[0];
961 $cmp_cs = $path[-1];
962 } else {
963 my $path = join(',', map({$_->name .' '. $_->version} @path));
964 my $len = scalar(@path)-1;
965 throw("Unexpected mapping path between intermediate and last" .
966 "coord systems (". $mid_cs->name . " " . $mid_cs->version .
967 " and " . $end_cs->name . " " . $end_cs->version . ")." .
968 "\nExpected path length 1, got $len. " .
969 "(path=$path)");
970 }
971  
972 if(defined($to_slice)){
973 my $to_cs = $to_slice->coord_system;
974 if($asm_cs->equals($to_cs)){
975 $asm2cmp_sth = $self->prepare($asm2cmp);
976 $cmp2asm_sth = $self->prepare($cmp2asm." AND asm.asm_seq_region_id = $to_seq_region_id");
977 }
978 elsif($cmp_cs->equals($to_cs)){
979 $asm2cmp_sth = $self->prepare($asm2cmp." AND asm.cmp_seq_region_id = $to_seq_region_id");
980 $cmp2asm_sth = $self->prepare($cmp2asm);
981 }
982 else{
983 $asm2cmp_sth = $self->prepare($asm2cmp);
984 $cmp2asm_sth = $self->prepare($cmp2asm);
985 }
986 }
987  
988 $sth = ($asm_cs->equals($mid_cs)) ? $asm2cmp_sth : $cmp2asm_sth;
989  
990 my $end_cs_id = $end_cs->dbID();
991 foreach my $mid_range (@mid_ranges) {
992 my ($mid_seq_region_id, $mid_seq_region,$start, $end) = @$mid_range;
993 $sth->bind_param(1,$mid_seq_region_id,SQL_INTEGER);
994 $sth->bind_param(2,$start,SQL_INTEGER);
995 $sth->bind_param(3,$end,SQL_INTEGER);
996 $sth->bind_param(4,$end_cs_id,SQL_INTEGER);
997 $sth->execute();
998  
999 #load the end <-> mid mapper with the results and record the mid cs
1000 #ranges we just added to the mapper
1001  
1002 my ($end_start, $end_end, $end_seq_region_id, $end_seq_region, $end_length,
1003 $ori, $mid_start, $mid_end);
1004  
1005 $sth->bind_columns(\$end_start, \$end_end, \$end_seq_region_id,
1006 \$end_seq_region, \$end_length, \$ori, \$mid_start,
1007 \$mid_end);
1008  
1009 while($sth->fetch()) {
1010 $end_mid_mapper->add_map_coordinates
1011 (
1012 $end_seq_region_id, $end_start, $end_end, $ori,
1013 $mid_seq_region_id, $mid_start, $mid_end
1014 );
1015  
1016 #update sr_name cache
1017 my $arr = [ $end_seq_region_id,$end_seq_region,$end_cs_id,$end_length ];
1018  
1019 $self->{'sr_name_cache'}->{"$end_seq_region:$end_cs_id"} = $arr;
1020 $self->{'sr_id_cache'}->{"$end_seq_region_id"} = $arr;
1021  
1022 #register this region on the end coord system
1023 $end_registry->check_and_register($end_seq_region_id, $end_start, $end_end);
1024 }
1025 $sth->finish();
1026 }
1027  
1028 #########
1029 # Now that both halves are loaded
1030 # Do stepwise mapping using both of the loaded mappers to load
1031 # the final start <-> end mapper
1032 #
1033  
1034 _build_combined_mapper(\@start_ranges, $start_mid_mapper, $end_mid_mapper,
1035 $combined_mapper, $start_name);
1036 #all done!
1037 return;
1038 }
1039  
1040  
1041 =head2 _register_chained_special
1042  
1043 Arg [1] : Bio::EnsEMBL::ChainedAssemblyMapper $casm_mapper
1044 The chained assembly mapper to register regions on
1045 Arg [2] : string $from ('first' or 'last')
1046 The direction we are registering from, and the name of the
1047 internal mapper.
1048 Arg [3] : string $seq_region_name
1049 The name of the seqregion we are registering on
1050 Arg [4] : listref $ranges
1051 A list of ranges to register (in [$start,$end] tuples).
1052 Arg [5] : (optional) $to_slice
1053 Only register those on this Slice.
1054 Description: Registers a set of ranges on a chained assembly mapper.
1055 This function is at the heart of the chained mapping process.
1056 It retrieves information from the assembly table and
1057 dynamically constructs the mappings between two coordinate
1058 systems which are 2 mapping steps apart. It does this by using
1059 two internal mappers to load up a third mapper which is
1060 actually used by the ChainedAssemblyMapper to perform the
1061 mapping.
1062  
1063 This method must be called before any mapping is
1064 attempted on regions of interest, otherwise only gaps will
1065 be returned. Note that the ChainedAssemblyMapper automatically
1066 calls this method when the need arises.
1067 Returntype : none
1068 Exceptions : throw if the seq_region to be registered does not exist
1069 or if it associated with multiple assembled pieces (bad data
1070 in assembly table)
1071  
1072 throw if the mapping between the coordinate systems cannot
1073 be performed in two steps, which means there is an internal
1074 error in the data in the meta table or in the code that creates
1075 the mapping paths.
1076 Caller : Bio::EnsEMBL::AssemblyMapper
1077 Status : Stable
1078  
1079 =cut
1080  
1081 sub _register_chained_special {
1082 my $self = shift;
1083 my $casm_mapper = shift;
1084 my $from = shift;
1085 my $seq_region_id = shift;
1086 my $ranges = shift;
1087 my $to_slice = shift;
1088 my $found = 0;
1089  
1090 my $sth = $self->prepare("SELECT
1091 asm.cmp_start,
1092 asm.cmp_end,
1093 asm.cmp_seq_region_id,
1094 sr.name,
1095 sr.length,
1096 asm.ori,
1097 asm.asm_start,
1098 asm.asm_end
1099 FROM
1100 assembly asm, seq_region sr
1101 WHERE
1102 asm.asm_seq_region_id = ? AND
1103 ? <= asm.asm_end AND
1104 ? >= asm.asm_start AND
1105 asm.cmp_seq_region_id = sr.seq_region_id AND
1106 sr.coord_system_id = ? AND
1107 asm.cmp_seq_region_id = ?");
1108  
1109  
1110 my ($start_name, $start_mid_mapper, $start_cs, $start_registry,
1111 $end_name, $end_mid_mapper, $end_cs, $end_registry);
1112  
1113 if($from eq 'first') {
1114 $start_name = 'first';
1115 $start_mid_mapper = $casm_mapper->first_middle_mapper();
1116 $start_cs = $casm_mapper->first_CoordSystem();
1117 $start_registry = $casm_mapper->first_registry();
1118 $end_mid_mapper = $casm_mapper->last_middle_mapper();
1119 $end_cs = $casm_mapper->last_CoordSystem();
1120 $end_registry = $casm_mapper->last_registry();
1121 $end_name = 'last';
1122 } elsif($from eq 'last') {
1123 $start_name = 'last';
1124 $start_mid_mapper = $casm_mapper->last_middle_mapper();
1125 $start_cs = $casm_mapper->last_CoordSystem();
1126 $start_registry = $casm_mapper->last_registry();
1127 $end_mid_mapper = $casm_mapper->first_middle_mapper();
1128 $end_cs = $casm_mapper->first_CoordSystem();
1129 $end_registry = $casm_mapper->first_registry();
1130 $end_name = 'first';
1131 } else {
1132 throw("Invalid from argument: [$from], must be 'first' or 'last'");
1133 }
1134  
1135 my $combined_mapper = $casm_mapper->first_last_mapper();
1136 my $mid_cs = $casm_mapper->middle_CoordSystem();
1137 my $mid_name = 'middle';
1138 my $csa = $self->db->get_CoordSystemAdaptor();
1139  
1140 # Check for the simple case where the ChainedMapper is short
1141 if( ! defined $mid_cs ) {
1142 $start_mid_mapper = $combined_mapper;
1143 }
1144  
1145  
1146 my @path;
1147 if( defined $mid_cs ) {
1148 @path = @{$csa->get_mapping_path($start_cs, $mid_cs)};
1149 } else {
1150 @path = @{$csa->get_mapping_path( $start_cs, $end_cs )};
1151 }
1152 if( ! defined $mid_cs ) {
1153 $start_mid_mapper = $combined_mapper;
1154 }
1155  
1156 if(@path != 2 && defined( $path[1] )) {
1157 my $path = join(',', map({$_->name .' '. $_->version} @path));
1158 my $len = scalar(@path) - 1;
1159 throw("Unexpected mapping path between start and intermediate " .
1160 "coord systems (". $start_cs->name . " " . $start_cs->version .
1161 " and " . $mid_cs->name . " " . $mid_cs->version . ")." .
1162 "\nExpected path length 1, got $len. " .
1163 "(path=$path)");
1164 }
1165  
1166 my ($asm_cs,$cmp_cs);
1167 $asm_cs = $path[0];
1168 $cmp_cs = $path[-1];
1169  
1170 $combined_mapper = $casm_mapper->first_last_mapper();
1171 $mid_cs = $casm_mapper->middle_CoordSystem();
1172 $mid_name = 'middle';
1173 $csa = $self->db->get_CoordSystemAdaptor();
1174  
1175 my $mid_cs_id;
1176  
1177 # Check for the simple case where the ChainedMapper is short
1178 if ( !defined $mid_cs ) {
1179 $start_mid_mapper = $combined_mapper;
1180 } else {
1181 $mid_cs_id = $mid_cs->dbID();
1182 }
1183  
1184 my @mid_ranges;
1185 my @start_ranges;
1186  
1187 my $to_cs = $to_slice->coord_system;
1188 foreach my $direction (1, 0){
1189 my $id1;
1190 my $id2;
1191 if($direction){
1192 $id1 = $seq_region_id;
1193 $id2 = $to_slice->get_seq_region_id();
1194 }
1195 else{
1196 $id2 = $seq_region_id;
1197 $id1 = $to_slice->get_seq_region_id();
1198 }
1199  
1200 foreach my $range (@$ranges) {
1201 my ($start, $end) = @$range;
1202 $sth->bind_param(1,$id1,SQL_INTEGER);
1203 $sth->bind_param(2,$start,SQL_INTEGER);
1204 $sth->bind_param(3,$end,SQL_INTEGER);
1205 $sth->bind_param(4,$to_cs->dbID,SQL_INTEGER);
1206 $sth->bind_param(5,$id2,SQL_INTEGER);
1207 $sth->execute();
1208  
1209 my ($mid_start, $mid_end, $mid_seq_region_id, $mid_seq_region, $mid_length,
1210 $ori, $start_start, $start_end);
1211  
1212 $sth->bind_columns(\$mid_start, \$mid_end, \$mid_seq_region_id,
1213 \$mid_seq_region, \$mid_length, \$ori, \$start_start,
1214 \$start_end);
1215  
1216 while($sth->fetch()) {
1217 $found = 1;
1218  
1219 if( defined $mid_cs ) {
1220 $start_mid_mapper->add_map_coordinates
1221 (
1222 $id1,$start_start, $start_end, $ori,
1223 $mid_seq_region_id, $mid_start, $mid_end
1224 );
1225 } else {
1226 if( $from eq "first") {
1227 if($direction){
1228 $combined_mapper->add_map_coordinates
1229 (
1230 $id1,$start_start, $start_end, $ori,
1231 $mid_seq_region_id, $mid_start, $mid_end
1232 );
1233 }
1234 else{
1235 $combined_mapper->add_map_coordinates
1236 (
1237 $mid_seq_region_id, $mid_start, $mid_end, $ori,
1238 $id1,$start_start, $start_end
1239 );
1240 }
1241 } else {
1242 if($direction){
1243 $combined_mapper->add_map_coordinates
1244 (
1245 $mid_seq_region_id, $mid_start, $mid_end, $ori,
1246 $id1,$start_start, $start_end
1247 );
1248 }
1249 else{
1250 $combined_mapper->add_map_coordinates
1251 (
1252 $id1,$start_start, $start_end, $ori,
1253 $mid_seq_region_id, $mid_start, $mid_end
1254 );
1255 }
1256 }
1257 }
1258
1259 #update sr_name cache
1260 my $arr = [ $mid_seq_region_id, $mid_seq_region,
1261 $mid_cs_id, $mid_length ];
1262
1263 $self->{'sr_name_cache'}->{"$mid_seq_region:$mid_cs_id"} = $arr;
1264 $self->{'sr_id_cache'}->{"$mid_seq_region_id"} = $arr;
1265
1266 push @mid_ranges,[$mid_seq_region_id,$mid_seq_region,
1267 $mid_start,$mid_end];
1268 push @start_ranges, [ $id1, $start_start, $start_end ];
1269
1270 #the region that we actually register may actually be larger or smaller
1271 #than the region that we wanted to register.
1272 #register the intersection of the region so we do not end up doing
1273 #extra work later
1274
1275 if($start_start < $start || $start_end > $end) {
1276 $start_registry->check_and_register($id1,$start_start,
1277 $start_end);
1278 }
1279 }
1280 $sth->finish();
1281 }
1282 if($found){
1283 if( ! defined $mid_cs ) {
1284 for my $range ( @mid_ranges ) {
1285 $end_registry->check_and_register( $range->[0], $range->[2],
1286 $range->[3] );
1287 }
1288
1289 # and thats it for the simple case ...
1290 return;
1291 }
1292 }
1293 }
1294 }
1295  
1296  
1297 =head2 register_all
1298  
1299 Arg [1] : Bio::EnsEMBL::AssemblyMapper $mapper
1300 Example : $mapper = $asm_mapper_adaptor->fetch_by_CoordSystems($cs1,$cs2);
1301  
1302 # make cache large enough to hold all of the mappings
1303 $mapper->max_pair_count(10e6);
1304 $asm_mapper_adaptor->register_all($mapper);
1305  
1306 # perform mappings as normal
1307 $mapper->map($slice->seq_region_name(), $sr_start, $sr_end,
1308 $sr_strand, $cs1);
1309 ...
1310 Description: This function registers the entire set of mappings between
1311 two coordinate systems in an assembly mapper.
1312 This will use a lot of memory but will be much more efficient
1313 when doing a lot of mapping which is spread over the entire
1314 genome.
1315 Returntype : none
1316 Exceptions : none
1317 Caller : specialised prograhsm
1318 Status : Stable
1319  
1320 =cut
1321  
1322 sub register_all {
1323 my $self = shift;
1324 my $mapper = shift;
1325  
1326 my $asm_cs_id = $mapper->assembled_CoordSystem()->dbID();
1327 my $cmp_cs_id = $mapper->component_CoordSystem()->dbID();
1328  
1329 # retrieve every relevant assembled/component pair from the assembly table
1330  
1331 my $q = qq{
1332 SELECT
1333 asm.cmp_start,
1334 asm.cmp_end,
1335 asm.cmp_seq_region_id,
1336 cmp_sr.name,
1337 cmp_sr.length,
1338 asm.ori,
1339 asm.asm_start,
1340 asm.asm_end,
1341 asm.asm_seq_region_id,
1342 asm_sr.name,
1343 asm_sr.length
1344 FROM
1345 assembly asm, seq_region asm_sr, seq_region cmp_sr
1346 WHERE
1347 asm.cmp_seq_region_id = cmp_sr.seq_region_id AND
1348 asm.asm_seq_region_id = asm_sr.seq_region_id AND
1349 cmp_sr.coord_system_id = ? AND
1350 asm_sr.coord_system_id = ?
1351 };
1352  
1353 my $sth = $self->prepare($q);
1354  
1355 $sth->bind_param(1,$cmp_cs_id,SQL_INTEGER);
1356 $sth->bind_param(2,$asm_cs_id,SQL_INTEGER);
1357 $sth->execute();
1358  
1359 # load the mapper with the assembly information
1360  
1361 my ($cmp_start, $cmp_end, $cmp_seq_region_id, $cmp_seq_region, $cmp_length,
1362 $ori,
1363 $asm_start, $asm_end, $asm_seq_region_id, $asm_seq_region, $asm_length);
1364  
1365 $sth->bind_columns(\$cmp_start, \$cmp_end, \$cmp_seq_region_id,
1366 \$cmp_seq_region, \$cmp_length, \$ori,
1367 \$asm_start, \$asm_end, \$asm_seq_region_id,
1368 \$asm_seq_region, \$asm_length);
1369  
1370 my %asm_registered;
1371  
1372 while($sth->fetch()) {
1373 $mapper->register_component($cmp_seq_region_id);
1374 $mapper->mapper->add_map_coordinates(
1375 $asm_seq_region_id, $asm_start, $asm_end,
1376 $ori,
1377 $cmp_seq_region_id, $cmp_start, $cmp_end);
1378  
1379 my $arr = [$cmp_seq_region_id, $cmp_seq_region, $cmp_cs_id, $cmp_length];
1380  
1381 $self->{'sr_name_cache'}->{"$cmp_seq_region:$cmp_cs_id"} = $arr;
1382 $self->{'sr_id_cache'}->{"$cmp_seq_region_id"} = $arr;
1383  
1384 # only register each asm seq_region once since it requires some work
1385 if(!$asm_registered{$asm_seq_region_id}) {
1386 $asm_registered{$asm_seq_region_id} = 1;
1387  
1388 # register all chunks from start of seq region to end
1389 my $end_chunk = $asm_length >> $CHUNKFACTOR;
1390 for(my $i = 0; $i <= $end_chunk; $i++) {
1391 $mapper->register_assembled($asm_seq_region_id, $i);
1392 }
1393  
1394 $arr = [$asm_seq_region_id, $asm_seq_region, $asm_cs_id, $asm_length];
1395  
1396 $self->{'sr_name_cache'}->{"$asm_seq_region:$asm_cs_id"} = $arr;
1397 $self->{'sr_id_cache'}->{"$asm_seq_region_id"} = $arr;
1398 }
1399 }
1400  
1401 $sth->finish();
1402  
1403 return;
1404 }
1405  
1406  
1407  
1408 =head2 register_all_chained
1409  
1410 Arg [1] : Bio::EnsEMBL::ChainedAssemblyMapper $casm_mapper
1411 Example : $mapper = $asm_mapper_adaptor->fetch_by_CoordSystems($cs1,$cs2);
1412  
1413 # make the cache large enough to hold all of the mappings
1414 $mapper->max_pair_count(10e6);
1415 # load all of the mapping data
1416 $asm_mapper_adaptor->register_all_chained($mapper);
1417  
1418 # perform mappings as normal
1419 $mapper->map($slice->seq_region_name(), $sr_start, $sr_end,
1420 $sr_strand, $cs1);
1421 ...
1422 Description: This function registers the entire set of mappings between
1423 two coordinate systems in a chained mapper. This will use a lot
1424 of memory but will be much more efficient when doing a lot of
1425 mapping which is spread over the entire genome.
1426 Returntype : none
1427 Exceptions : throw if mapper is between coord systems with unexpected
1428 mapping paths
1429 Caller : specialised programs doing a lot of genome-wide mapping
1430 Status : Stable
1431  
1432 =cut
1433  
1434 sub register_all_chained {
1435 my $self = shift;
1436 my $casm_mapper = shift;
1437  
1438 my $first_cs = $casm_mapper->first_CoordSystem();
1439 my $mid_cs = $casm_mapper->middle_CoordSystem();
1440 my $last_cs = $casm_mapper->last_CoordSystem();
1441  
1442 my $start_mid_mapper = $casm_mapper->first_middle_mapper();
1443 my $end_mid_mapper = $casm_mapper->last_middle_mapper();
1444 my $combined_mapper = $casm_mapper->first_last_mapper();
1445  
1446 my @ranges;
1447  
1448 my $sth = $self->prepare(
1449 'SELECT
1450 asm.cmp_start,
1451 asm.cmp_end,
1452 asm.cmp_seq_region_id,
1453 sr_cmp.name,
1454 sr_cmp.length,
1455 asm.ori,
1456 asm.asm_start,
1457 asm.asm_end,
1458 asm.asm_seq_region_id,
1459 sr_asm.name,
1460 sr_asm.length
1461 FROM
1462 assembly asm, seq_region sr_asm, seq_region sr_cmp
1463 WHERE
1464 sr_asm.seq_region_id = asm.asm_seq_region_id AND
1465 sr_cmp.seq_region_id = asm.cmp_seq_region_id AND
1466 sr_asm.coord_system_id = ? AND
1467 sr_cmp.coord_system_id = ?');
1468  
1469 my $csa = $self->db()->get_CoordSystemAdaptor();
1470  
1471 my @path;
1472  
1473 if ( !defined $mid_cs ) {
1474 @path = @{ $csa->get_mapping_path( $first_cs, $last_cs ) };
1475 if ( !defined( $path[1] ) ) {
1476 splice( @path, 1, 1 );
1477 }
1478 } else {
1479 @path = @{ $csa->get_mapping_path( $first_cs, $mid_cs ) };
1480 # fix for when we have something like supercontig#contig#chromosome
1481 if ( !defined( $path[1] ) ) {
1482 splice( @path, 1, 1 );
1483 }
1484 }
1485  
1486 if ( @path != 2 ) {
1487 my $path =
1488 join( ',', map( { $_->name . ' ' . $_->version } @path ) );
1489 my $len = scalar(@path) - 1;
1490 throw( "Unexpected mapping path between start and intermediate "
1491 . "coord systems ("
1492 . $first_cs->name . " "
1493 . $first_cs->version . " and "
1494 . $mid_cs->name . " "
1495 . $mid_cs->version . ")."
1496 . "\nExpected path length 1, got $len. "
1497 . "(path=$path)" );
1498 }
1499  
1500 my ($asm_cs,$cmp_cs) = @path;
1501  
1502 $sth->{mysql_use_result} = 1;
1503 $sth->bind_param(1,$asm_cs->dbID,SQL_INTEGER);
1504 $sth->bind_param(2,$cmp_cs->dbID,SQL_INTEGER);
1505 $sth->execute();
1506  
1507  
1508 my ($mid_start, $mid_end, $mid_seq_region_id, $mid_seq_region, $mid_length,
1509 $ori, $start_start, $start_end, $start_seq_region_id, $start_seq_region,
1510 $start_length);
1511  
1512 if($asm_cs->equals($first_cs)) {
1513 $sth->bind_columns(\$mid_start, \$mid_end, \$mid_seq_region_id,
1514 \$mid_seq_region, \$mid_length, \$ori, \$start_start,
1515 \$start_end, \$start_seq_region_id, \$start_seq_region,
1516 \$start_length);
1517 } else {
1518 $sth->bind_columns(\$start_start, \$start_end, \$start_seq_region_id,
1519 \$start_seq_region, \$start_length, \$ori,
1520 \$mid_start, \$mid_end, \$mid_seq_region_id,
1521 \$mid_seq_region, \$mid_length);
1522  
1523 }
1524  
1525 my ( $mid_cs_id, $start_cs_id, $registry, $mapper );
1526 if ( !defined $mid_cs ) {
1527 $mid_cs_id = $last_cs->dbID();
1528 $start_cs_id = $first_cs->dbID();
1529 $mapper = $combined_mapper;
1530 } else {
1531 $mid_cs_id = $mid_cs->dbID();
1532 $start_cs_id = $first_cs->dbID();
1533 $mapper = $start_mid_mapper;
1534 }
1535  
1536 $registry = $casm_mapper->first_registry();
1537  
1538 while($sth->fetch()) {
1539 $mapper->add_map_coordinates
1540 (
1541 $start_seq_region_id, $start_start, $start_end, $ori,
1542 $mid_seq_region_id, $mid_start, $mid_end
1543 );
1544 push( @ranges, [$start_seq_region_id, $start_start, $start_end ] );
1545  
1546 $registry->check_and_register( $start_seq_region_id, 1, $start_length );
1547 if( ! defined $mid_cs ) {
1548 $casm_mapper->last_registry()->check_and_register
1549 ( $mid_seq_region_id, $mid_start, $mid_end );
1550 }
1551  
1552 my $arr = [ $mid_seq_region_id, $mid_seq_region,
1553 $mid_cs_id, $mid_length ];
1554  
1555 $self->{'sr_name_cache'}->{"$mid_seq_region:$mid_cs_id"} = $arr;
1556 $self->{'sr_id_cache'}->{"$mid_seq_region_id"} = $arr;
1557  
1558 $arr = [ $start_seq_region_id, $start_seq_region,
1559 $start_cs_id, $start_length ];
1560  
1561 $self->{'sr_name_cache'}->{"$start_seq_region:$start_cs_id"} = $arr;
1562 $self->{'sr_id_cache'}->{"$start_seq_region_id"} = $arr;
1563 }
1564  
1565 if( ! defined $mid_cs ) {
1566 # thats it for the simple case
1567 return;
1568 }
1569  
1570  
1571 @path = @{ $csa->get_mapping_path( $last_cs, $mid_cs ) };
1572 if ( defined($mid_cs) ) {
1573 if ( !defined( $path[1] ) ) {
1574 splice( @path, 1, 1 );
1575 }
1576 }
1577  
1578 if ( @path != 2 ) {
1579 my $path =
1580 join( ',', map( { $_->name . ' ' . $_->version } @path ) );
1581 my $len = scalar(@path) - 1;
1582 throw( "Unexpected mapping path between intermediate and last "
1583 . "coord systems ("
1584 . $last_cs->name . " "
1585 . $last_cs->version . " and "
1586 . $mid_cs->name . " "
1587 . $mid_cs->version . ")."
1588 . "\nExpected path length 1, got $len. "
1589 . "(path=$path)" );
1590 }
1591  
1592 ($asm_cs,$cmp_cs) = @path;
1593  
1594 $sth->bind_param(1,$asm_cs->dbID,SQL_INTEGER);
1595 $sth->bind_param(2,$cmp_cs->dbID,SQL_INTEGER);
1596 $sth->execute();
1597  
1598  
1599 my ($end_start, $end_end, $end_seq_region_id, $end_seq_region,
1600 $end_length);
1601  
1602 if($asm_cs->equals($mid_cs)) {
1603 $sth->bind_columns(\$end_start, \$end_end, \$end_seq_region_id,
1604 \$end_seq_region, \$end_length, \$ori,
1605 \$mid_start, \$mid_end, \$mid_seq_region_id,
1606 \$mid_seq_region, \$mid_length);
1607 } else {
1608 $sth->bind_columns(\$mid_start, \$mid_end, \$mid_seq_region_id,
1609 \$mid_seq_region, \$mid_length, \$ori, \$end_start,
1610 \$end_end, \$end_seq_region_id, \$end_seq_region,
1611 \$end_length);
1612 }
1613  
1614 my $end_cs_id = $last_cs->dbID();
1615 $registry = $casm_mapper->last_registry();
1616  
1617 while($sth->fetch()) {
1618 $end_mid_mapper->add_map_coordinates
1619 (
1620 $end_seq_region_id, $end_start, $end_end, $ori,
1621 $mid_seq_region_id, $mid_start, $mid_end
1622 );
1623  
1624 $registry->check_and_register( $end_seq_region_id, 1, $end_length );
1625  
1626 my $arr = [ $end_seq_region_id, $end_seq_region,
1627 $end_cs_id, $end_length ];
1628 $self->{'sr_name_cache'}->{"$end_seq_region:$end_cs_id"} = $arr;
1629 $self->{'sr_id_cache'}->{"$end_seq_region_id"} = $arr;
1630 }
1631  
1632 _build_combined_mapper( \@ranges, $start_mid_mapper, $end_mid_mapper,
1633 $combined_mapper, "first" );
1634  
1635 return;
1636 }
1637  
1638  
1639  
1640 # after both halves of a chained mapper are loaded
1641 # this function maps all ranges in $ranges and loads the
1642 # results into the combined mapper
1643 sub _build_combined_mapper {
1644 my $ranges = shift;
1645 my $start_mid_mapper = shift;
1646 my $end_mid_mapper = shift;
1647 my $combined_mapper = shift;
1648 my $start_name = shift;
1649  
1650 my $mid_name = "middle";
1651  
1652 foreach my $range (@$ranges) {
1653 my ( $seq_region_id, $start, $end) = @$range;
1654  
1655 my $sum = 0;
1656  
1657 my @initial_coords = $start_mid_mapper->map_coordinates($seq_region_id,
1658 $start,$end,1,
1659 $start_name);
1660  
1661 foreach my $icoord (@initial_coords) {
1662 #skip gaps
1663 if($icoord->isa('Bio::EnsEMBL::Mapper::Gap')) {
1664 $sum += $icoord->length();
1665 next;
1666 }
1667  
1668  
1669 #feed the results of the first mapping into the second mapper
1670 my @final_coords =
1671 $end_mid_mapper->map_coordinates($icoord->id, $icoord->start,
1672 $icoord->end,
1673 $icoord->strand, $mid_name);
1674  
1675  
1676 foreach my $fcoord (@final_coords) {
1677 #load up the final mapper
1678  
1679 if($fcoord->isa('Bio::EnsEMBL::Mapper::Coordinate')) {
1680 my $total_start = $start + $sum;
1681 my $total_end = $total_start + $fcoord->length - 1;
1682 my $ori = $fcoord->strand();
1683  
1684 if($start_name eq 'first') { # add coords in consistant order
1685 $combined_mapper->add_map_coordinates(
1686 $seq_region_id, $total_start, $total_end, $ori,
1687 $fcoord->id(), $fcoord->start(), $fcoord->end());
1688 } else {
1689 $combined_mapper->add_map_coordinates(
1690 $fcoord->id(), $fcoord->start(), $fcoord->end(),$ori,
1691 $seq_region_id, $total_start, $total_end);
1692 }
1693  
1694 }
1695 $sum += $fcoord->length();
1696 }
1697 }
1698 }
1699 #all done!
1700 }
1701  
1702  
1703 =head2 seq_regions_to_ids
1704  
1705 Arg [1] : Bio::EnsEMBL::CoordSystem $coord_system
1706 Arg [2] : listref of strings $seq_regions
1707 Example : my @ids = @{$asma->seq_regions_to_ids($coord_sys, \@seq_regs)};
1708 Description: Converts a list of seq_region names to internal identifiers
1709 using the internal cache that has accumulated while registering
1710 regions for AssemblyMappers. If any requested regions are
1711 not found in the cache an attempt is made to retrieve them
1712 from the database.
1713 Returntype : listref of ints
1714 Exceptions : throw if a non-existant seqregion is provided
1715 Caller : general
1716 Status : Stable
1717  
1718 =cut
1719  
1720 sub seq_regions_to_ids {
1721 my $self = shift;
1722 my $coord_system = shift;
1723 my $seq_regions = shift;
1724  
1725 my $cs_id = $coord_system->dbID();
1726  
1727 my @out;
1728  
1729 foreach my $sr (@$seq_regions) {
1730 my $arr = $self->{'sr_name_cache'}->{"$sr:$cs_id"};
1731 if( $arr ) {
1732 push( @out, $arr->[0] );
1733 } else {
1734 push @out, $self->_seq_region_name_to_id($sr,$cs_id);
1735 }
1736 }
1737  
1738 return \@out;
1739 }
1740  
1741  
1742 =head2 seq_ids_to_regions
1743  
1744 Arg [1] : listref of seq_region ids
1745 Example : my @ids = @{$asma->ids_to_seq_regions(\@seq_ids)};
1746 Description: Converts a list of seq_region ids to seq region names
1747 using the internal cache that has accumulated while registering
1748 regions for AssemblyMappers. If any requested regions are
1749 not found in the cache an attempt is made to retrieve them
1750 from the database.
1751 Returntype : listref of strings
1752 Exceptions : throw if a non-existant seq_region_id is provided
1753 Caller : general
1754 Status : Stable
1755  
1756 =cut
1757  
1758 sub seq_ids_to_regions {
1759 my $self = shift;
1760 my $seq_region_ids = shift;
1761  
1762 my @out;
1763  
1764 foreach my $sr (@$seq_region_ids) {
1765 my $arr = $self->{'sr_id_cache'}->{"$sr"};
1766 if( $arr ) {
1767 push( @out, $arr->[1] );
1768 } else {
1769 push @out, $self->_seq_region_id_to_name($sr);
1770 }
1771 }
1772  
1773 return \@out;
1774 }
1775  
1776 =head2 delete_cache
1777  
1778 Description: Delete all the caches for the mappings/seq_regions
1779 Returntype : none
1780 Exceptions : none
1781 Caller : General
1782 Status : At risk
1783  
1784 =cut
1785  
1786 sub delete_cache{
1787 my ($self) = @_;
1788  
1789 %{$self->{'sr_name_cache'}} = ();
1790 %{$self->{'sr_id_cache'}} = ();
1791  
1792 foreach my $key (keys %{$self->{'_asm_mapper_cache'}}){
1793 $self->{'_asm_mapper_cache'}->{$key}->flush();
1794 }
1795 %{$self->{'_asm_mapper_cache'}} = ();
1796 return;
1797 }
1798  
1799  
1800 1;