3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
34 Handles mapping between two coordinate systems
using the information
35 stored in the assembly table
40 $asma = $db->get_AssemblyMapperAdaptor();
41 $csa = $db->get_CoordSystemAdaptor();
43 my $chr_cs = $cs_adaptor->fetch_by_name(
'chromosome',
'NCBI33' );
44 my $cln_cs = $cs_adaptor->fetch_by_name(
'clone');
46 $asm_mapper = $map_adaptor->fetch_by_CoordSystems( $cs1, $cs2 );
48 # Map to contig coordinate system from chromosomal
50 $asm_mapper->map(
'X', 1_000_000, 2_000_000, 1, $chr_cs );
52 # Map to chromosome coordinate system from contig
54 $asm_mapper->map(
'AL30421.1', 100, 10000, -1, $cln_cs );
56 # List contig names for a region of chromsome
57 @cln_ids = $asm_mapper->list_ids(
'13', 1_000_000, 1, $chr_cs );
59 # List chromosome names for a contig region
61 $asm_mapper->list_ids(
'AL30421.1', 1, 1000, -1, $cln_cs );
66 that allows
for mappings between coordinate systems that require
67 multi-step mapping. For example
if explicit mappings are defined
68 between the following coordinate systems,
74 between the chromosome and clone coordinate systems. This should be
75 transparent to the user of
this module, and users should not even
76 realise that they are
using a chained assembly mapper as opposed to a
77 normal assembly mapper.
83 package Bio::EnsEMBL::ChainedAssemblyMapper;
87 use integer; #use proper arithmetic bitshifts
92 use Scalar::Util qw(weaken);
96 my $MIDDLE =
'middle';
100 my $CHUNKFACTOR = 20;
102 # max size of the pair cache in the mappers
103 my $DEFAULT_MAX_PAIR_COUNT = 6000;
111 Example : Should use AssemblyMapperAdaptor->fetch_by_CoordSystems
114 Exceptions : thrown
if wrong number of coord_systems are provided
115 Caller : AssemblyMapperAdaptor
121 my ($caller,$adaptor,@coord_systems) = @_;
123 my $class = ref($caller) || $caller;
128 $self->adaptor($adaptor);
130 if(@coord_systems != 3) {
131 throw(
'ChainedMapper can only map between 3 coordinate systems. ' .
132 scalar(@coord_systems) .
' were provided');
137 # Set the component, intermediate and assembled coordinate systems
138 $self->{
'first_cs'} = $coord_systems[0];
139 $self->{
'mid_cs'} = $coord_systems[1];
140 $self->{
'last_cs'} = $coord_systems[2];
142 #maps between first and intermediate coord systems
145 #maps between last and intermediate
148 #mapper that is actually used and is loaded by the mappings generated
149 #by the other two mappers
154 #need registries to keep track of what regions are registered in source
155 #and destination coordinate systems
159 $self->{
'max_pair_count'} = $DEFAULT_MAX_PAIR_COUNT;
165 =head2 max_pair_count
167 Arg [1] : (optional)
int $max_pair_count
168 Example : $mapper->max_pair_count(100000)
169 Description: Getter/Setter
for the number of mapping pairs allowed in the
170 internal cache. This can be used to
override the
default value
171 (6000) to tune the performance and memory
usage for certain
172 scenarios. Higher value = bigger cache, more memory used
182 $self->{
'max_pair_count'} = shift
if(@_);
183 return $self->{
'max_pair_count'};
192 Example : $mapper->max_pair_count(10e6);
193 $mapper->register_all();
194 Description: Pre-registers all assembly information in
this mapper. The
195 cache size should be set to a sufficiently large value
196 so that all of the information can be stored. This method
197 is useful when *a lot* of mapping will be done in regions
198 which are distributed around the genome. After registration
199 the mapper will consume a lot of memory but will not have to
200 perform any SQL and will be faster.
203 Caller : specialised programs doing a lot of mapping
210 $self->adaptor->register_all_chained($self);
219 $self->{
'first_registry'}->flush();
220 $self->{
'last_registry'}->flush();
222 $self->{
'first_mid_mapper'}->flush();
223 $self->{
'last_mid_mapper'}->flush();
224 $self->{
'first_last_mapper'}->flush();
230 Example : $num_of_pairs = $mapper->size();
231 Description:
return the number of pairs currently stored.
241 return ( $self->{
'first_last_mapper'}->{
'pair_count'} +
242 $self->{
'last_mid_mapper'}->{
'pair_count'} +
243 $self->{
'first_mid_mapper'}->{
'pair_count'} );
250 Arg [1] :
string $frm_seq_region
251 The name of the sequence region to transform FROM
252 Arg [2] :
int $frm_start
253 The start of the region to transform FROM
254 Arg [3] :
int $frm_end
255 The end of the region to transform FROM
256 Arg [4] :
int $strand
257 The strand of the region to transform FROM
259 The coordinate system to transform FROM
260 Arg [6] : (optional) fastmap
261 Arg [7] : (optional) Bio::Ensembl::Slice
262 The slice to transform TO
263 Arg [8] : (optional)
boolean
264 Whether to include the original coordinates or not
265 Example : @coords = $asm_mapper->map(
'X', 1_000_000, 2_000_000,
267 Description: Transforms coordinates from one coordinate system
271 Exceptions : thrown
if the specified TO coordinat system is not one
272 of the coordinate systems associated with
this assembly mapper
279 throw(
'Incorrect number of arguments.')
if(@_ < 6);
281 my ($self, $frm_seq_region_name, $frm_start,
282 $frm_end, $frm_strand, $frm_cs, $fastmap, $to_slice, $include_org_coord) = @_;
284 my $mapper = $self->{
'first_last_mapper'};
285 my $first_cs = $self->{
'first_cs'};
286 my $last_cs = $self->{
'last_cs'};
288 my $is_insert = ($frm_end + 1 == $frm_start);
294 push @tmp, $frm_seq_region_name;
295 my $seq_region_id = @{$self->adaptor()->seq_regions_to_ids($frm_cs, \@tmp)}[0];
297 #speed critical section:
298 #try to do simple pointer equality comparisons of the coord system objects
299 #first since this is likely to work most of the time and is much faster
300 #than a function call
302 if($frm_cs == $first_cs ||
303 ($frm_cs != $last_cs && $frm_cs->equals($first_cs))) {
305 $registry = $self->{
'first_registry'};
306 } elsif($frm_cs == $last_cs || $frm_cs->equals($last_cs)) {
308 $registry = $self->{
'last_registry'};
310 throw(
"Coordinate system " . $frm_cs->name .
" " . $frm_cs->version .
311 " is neither the first nor the last coordinate system " .
312 " of this ChainedAssemblyMapper");
315 #the minimum area we want to register if registration is necessary is
316 #about 1MB. Break requested ranges into chunks of 1MB and then register
317 #this larger region if we have a registry miss.
319 #use bitwise shift for fast and easy integer multiplication and division
320 my ($min_start, $min_end);
323 $min_start = (($frm_end >> $CHUNKFACTOR) << $CHUNKFACTOR);
324 $min_end = ((($frm_start >> $CHUNKFACTOR) + 1) << $CHUNKFACTOR) - 1 ;
326 $min_start = (($frm_start >> $CHUNKFACTOR) << $CHUNKFACTOR);
327 $min_end = ((($frm_end >> $CHUNKFACTOR) + 1) << $CHUNKFACTOR) - 1 ;
330 #get a list of ranges in the requested region that have not been registered,
331 #and register them at the same
336 $ranges = $registry->check_and_register($seq_region_id, $frm_end,
337 $frm_start, $min_start, $min_end);
339 $ranges = $registry->check_and_register($seq_region_id, $frm_start,
340 $frm_end, $min_start, $min_end);
343 if(defined($ranges)) {
344 if( $self->size() > $self->{
'max_pair_count'} ) {
348 $ranges = $registry->check_and_register
349 ($seq_region_id, $frm_end, $frm_start, $min_start, $min_end);
351 $ranges = $registry->check_and_register
352 ($seq_region_id, $frm_start, $frm_end, $min_start, $min_end);
355 $self->adaptor->register_chained($self,$frm,$seq_region_id,$ranges,$to_slice);
359 return $mapper->fastmap($seq_region_id, $frm_start, $frm_end,
363 my @coords = $mapper->map_coordinates($seq_region_id, $frm_start, $frm_end,
364 $frm_strand, $frm, $include_org_coord);
366 # decorate (org,)mapped coordinates with their corresponding region names
367 if ($include_org_coord) {
369 check_ref($_,
'Bio::EnsEMBL::Mapper::Coordinate') && # exclude gap
370 $_->{original}->name($self->adaptor->seq_ids_to_regions([$_->{original}->id])->[0]) &&
371 $_->{mapped}->name($self->adaptor->seq_ids_to_regions([$_->{mapped}->id])->[0])
375 check_ref($_,
'Bio::EnsEMBL::Mapper::Coordinate') && # exclude gap
376 $_->name($self->adaptor->seq_ids_to_regions([$_->id])->[0])
386 return $self->map(@_,1);
392 Arg [1] :
string $frm_seq_region
393 The name of the sequence region of interest
394 Arg [2] :
int $frm_start
395 The start of the region of interest
396 Arg [3] :
int $frm_end
397 The end of the region to transform of interest
399 The coordinate system to obtain overlapping ids of
400 Example :
foreach $id ($asm_mapper->list_ids(
'X',1,1000,$chr_cs)) {...}
401 Description: Retrieves a list of overlapping seq_region
internal identifiers
402 of another coordinate system. This is the same as the
403 list_seq_regions method but uses
internal identfiers rather
404 than seq_region strings
405 Returntype : List of ints
414 throw(
'Incorrect number of arguments.')
if(@_ != 5);
415 my($self, $frm_seq_region_name, $frm_start, $frm_end, $frm_cs) = @_;
417 my $is_insert = ($frm_start == $frm_end + 1);
419 #the minimum area we want to register if registration is necessary is
420 #about 1MB. Break requested ranges into chunks of 1MB and then register
421 #this larger region if we have a registry miss.
423 #use bitwise shift for fast and easy integer multiplication and division
424 my ($min_start, $min_end);
427 $min_start = (($frm_end >> $CHUNKFACTOR) << $CHUNKFACTOR);
428 $min_end = ((($frm_start >> $CHUNKFACTOR) + 1) << $CHUNKFACTOR) - 1;
430 $min_start = (($frm_start >> $CHUNKFACTOR) << $CHUNKFACTOR);
431 $min_end = ((($frm_end >> $CHUNKFACTOR) + 1) << $CHUNKFACTOR) - 1;
435 push @tmp, $frm_seq_region_name;
436 my $seq_region_id = @{$self->adaptor()->seq_regions_to_ids($frm_cs, \@tmp)}[0];
438 if($frm_cs->equals($self->{
'first_cs'})) {
439 my $registry = $self->{
'first_registry'};
445 $ranges = $registry->check_and_register
446 ($seq_region_id, $frm_end, $frm_start, $min_start, $min_end);
448 $ranges = $registry->check_and_register
449 ($seq_region_id, $frm_start, $frm_end, $min_start, $min_end);
452 if(defined($ranges)) {
453 $self->adaptor->register_chained($self,$FIRST,$seq_region_id,$ranges);
456 return map {$_->to()->id()}
457 $self->first_last_mapper()->list_pairs($seq_region_id, $frm_start,
460 } elsif($frm_cs->equals($self->{
'last_cs'})) {
461 my $registry = $self->{
'last_registry'};
465 $ranges = $registry->check_and_register
466 ($seq_region_id, $frm_end, $frm_start, $min_start, $min_end);
468 $ranges = $registry->check_and_register
469 ($seq_region_id, $frm_start, $frm_end, $min_start, $min_end);
472 if(defined($ranges)) {
473 $self->adaptor->register_chained($self,$LAST,$seq_region_id,$ranges);
476 return map {$_->from()->id()}
477 $self->first_last_mapper()->list_pairs($seq_region_id, $frm_start,
480 throw(
"Coordinate system " . $frm_cs->name .
" " . $frm_cs->version .
481 " is neither the first nor the last coordinate system " .
482 " of this ChainedAssemblyMapper");
487 =head2 list_seq_regions
489 Arg [1] :
string $frm_seq_region
490 The name of the sequence region of interest
491 Arg [2] :
int $frm_start
492 The start of the region of interest
493 Arg [3] :
int $frm_end
494 The end of the region to transform of interest
496 The coordinate system to obtain overlapping ids of
497 Example :
foreach $id ($asm_mapper->list_ids(
'X',1,1000,$ctg_cs)) {...}
498 Description: Retrieves a list of overlapping seq_region
internal identifiers
499 of another coordinate system. This is the same as the
500 list_ids method but uses seq_region names rather
internal ids
501 Returntype : List of strings
508 sub list_seq_regions {
509 throw(
'Incorrect number of arguments.')
if(@_ != 5);
510 my($self, $frm_seq_region, $frm_start, $frm_end, $frm_cs) = @_;
512 #retrieve the seq_region names
514 $self->list_ids($frm_seq_region,$frm_start,$frm_end,$frm_cs);
516 #The seq_regions are from the 'to' coordinate system not the
517 #from coordinate system we used to obtain them
519 if($frm_cs->equals($self->first_CoordSystem())) {
520 $to_cs = $self->last_CoordSystem();
522 $to_cs = $self->first_CoordSystem();
525 #convert them to names
526 return @{$self->
adaptor()->seq_ids_to_regions(\@seq_regs)};
534 =head2 first_last_mapper
537 Example : $mapper = $cam->first_last_mapper();
538 Description:
return the mapper.
546 sub first_last_mapper {
548 return $self->{
'first_last_mapper'};
551 =head2 first_middle_mapper
554 Example : $mapper = $cam->first_middle_mapper();
555 Description:
return the mapper.
564 sub first_middle_mapper {
566 return $self->{
'first_mid_mapper'};
569 =head2 last_middle_mapper
572 Example : $mapper = $cam->last_middle_mapper();
573 Description:
return the mapper.
581 sub last_middle_mapper {
583 return $self->{
'last_mid_mapper'};
587 =head2 first_CoordSystem
590 Example : $coordsys = $cam->first_CoordSystem();
591 Description:
return the CoordSystem.
599 sub first_CoordSystem {
601 return $self->{
'first_cs'};
605 =head2 middle_CoordSystem
608 Example : $coordsys = $cam->middle_CoordSystem();
609 Description:
return the CoordSystem.
617 sub middle_CoordSystem {
619 return $self->{
'mid_cs'};
622 =head2 last_CoordSystem
625 Example : $coordsys = $cam->last_CoordSystem();
626 Description:
return the CoordSystem.
634 sub last_CoordSystem {
636 return $self->{
'last_cs'};
639 =head2 first_registry
642 Example : $rr = $cam->first_registry();
643 Description:
return the Registry.
653 return $self->{
'first_registry'};
659 Example : $rr = $cam->last_registry();
660 Description:
return the Registry.
670 return $self->{
'last_registry'};
675 # Methods supplied to maintain polymorphism with AssemblyMapper there
676 # is no real assembled or component in the chained mapper, since the
677 # ordering is arbitrary and both ends might actually be assembled, but
678 # these methods provide convenient synonyms
684 Example : $mapper = $cam->mapper();
685 Description:
return the first_last_mapper.
695 return $self->first_last_mapper();
698 =head2 assembled_CoordSystem
701 Example : $coordsys = $cam->assembled_CoordSystem();
702 Description:
return the first CoordSystem.
711 sub assembled_CoordSystem {
713 return $self->{
'first_cs'};
716 =head2 component_CoordSystem
719 Example : $coordsys = $cam->component_CoordSystem();
720 Description:
return the last CoordSystem.
728 sub component_CoordSystem {
730 return $self->{
'last_cs'};
737 Description: get/set
for this objects database adaptor
747 weaken($self->{
'adaptor'} = shift)
if(@_);
748 return $self->{
'adaptor'};