EnsEMBL/Mapper.pm untested

filter: Statementstmt 0.0% 0 / 462 Branchbran 0.0% 0 / 160 Conditioncond 0.0% 0 / 87 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 755 CC 81 SCAR 53.0
CC
81
Cov
0%
map_coordinates
552.0
map_insert
342.0
list_pairs
110.0
CRAP 200.1
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
34  
35 =head1 SYNOPSIS
36  
37 $map = Bio::EnsEMBL::Mapper->new( 'rawcontig', 'chromosome' );
38  
39 # add a coodinate mapping - supply two pairs or coordinates
40 $map->add_map_coordinates(
41 $contig_id, $contig_start, $contig_end, $contig_ori,
42 $chr_name, chr_start, $chr_end
43 );
44  
45 # map from one coordinate system to another
46 my @coordlist =
47 $mapper->map_coordinates( 627012, 2, 5, -1, "rawcontig" );
48  
49 =head1 DESCRIPTION
50  
51 Generic mapper to provide coordinate transforms between two disjoint
52 coordinate systems. This mapper is intended to be 'context neutral' - in
53 that it does not contain any code relating to any particular coordinate
54 system. This is provided in, for example, Bio::EnsEMBL::AssemblyMapper.
55  
56 Mappings consist of pairs of 'to-' and 'from-' contigs with coordinates on each.
57 Orientation is abbreviated to 'ori',
58  
59 The contig pair hash is divided into mappings per seq_region, the code
60 below makes assumptions about how to filter these results, thus the comparisons
61 for some properties are absent in the code but implicit by data structure.
62  
63 The assembly mapping hash '_pair_last' orders itself by the target seq region and looks like this:
64  
65 1 => ARRAY(0x1024c79c0)
66 0 Bio::EnsEMBL::Mapper::Pair=HASH(0x1024d6198)
67 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025edf98)
68 'end' => 4
69 'id' => 4
70 'start' => 1
71 'ori' => 1
72 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025edf68)
73 'end' => 4
74 'id' => 1
75 'start' => 1
76 1 Bio::EnsEMBL::Mapper::Pair=HASH(0x1026c20f0)
77 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee3a0)
78 'end' => 12
79 'id' => 4
80 'start' => 9
81 'ori' => 1
82 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee370)
83 'end' => 4
84 'id' => 1
85 'start' => 1
86 2 => ARRAY(0x1025ee460)
87 0 Bio::EnsEMBL::Mapper::Pair=HASH(0x1025ee400)
88 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee2c8)
89 'end' => 8
90 'id' => 4
91 'start' => 5
92 'ori' => 1
93 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee2b0)
94 'end' => 4
95 'id' => 2
96 'start' => 1
97 1 Bio::EnsEMBL::Mapper::Pair=HASH(0x1025ee658)
98 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025eea48)
99 'end' => 16
100 'id' => 4
101 'start' => 13
102 'ori' => 1
103 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025eea18)
104 'end' => 4
105 'id' => 2
106 'start' => 1
107  
108 The other mapping hash available is the reverse sense, putting the 'from' seq_region as the
109 sorting key. Here is an excerpt.
110  
111 0 HASH(0x102690bb8)
112 4 => ARRAY(0x1025ee028)
113 0 Bio::EnsEMBL::Mapper::Pair=HASH(0x1024d6198)
114 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025edf98)
115 'end' => 4
116 'id' => 4
117 'start' => 1
118 'ori' => 1
119 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025edf68)
120 'end' => 4
121 'id' => 1
122 'start' => 1
123 1 Bio::EnsEMBL::Mapper::Pair=HASH(0x1025ee400)
124 'from' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee2c8)
125 'end' => 8
126 'id' => 4
127 'start' => 5
128 'ori' => 1
129 'to' => Bio::EnsEMBL::Mapper::Unit=HASH(0x1025ee2b0)
130 'end' => 4
131 'id' => 2
132 'start' => 1
133  
134  
135 =head1 METHODS
136  
137 =cut
138  
139 package Bio::EnsEMBL::Mapper;
140 $Bio::EnsEMBL::Mapper::VERSION = '114.0.0_50'; # TRIAL
141 $Bio::EnsEMBL::Mapper::VERSION = '114.0.050';
142 use strict;
143 use integer;
144  
145 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
146 use Bio::EnsEMBL::Mapper::Pair;
147 use Bio::EnsEMBL::Mapper::IndelPair;
148 use Bio::EnsEMBL::Mapper::Unit;
149 use Bio::EnsEMBL::Mapper::Coordinate;
150 use Bio::EnsEMBL::Mapper::IndelCoordinate;
151 use Bio::EnsEMBL::Mapper::Gap;
152  
153 use Bio::EnsEMBL::Utils::Exception qw(throw);
154 use Bio::EnsEMBL::Utils::Interval;
155 use Bio::EnsEMBL::Utils::Tree::Interval::Immutable;
156  
157 =head2 new
158  
159 Arg [1] : string $from
160 The name of the 'from' coordinate system
161 Arg [2] : string $to
162 The name of the 'to' coordinate system
163 Arg [3] : (optional) Bio::EnsEMBL::CoordSystem $from_cs
164 The 'from' coordinate system
165 Arg [4] : (optional) Bio::EnsEMBL::CoordSystem $to_cs
166 Example : my $mapper = Bio::EnsEMBL::Mapper->new('FROM', 'TO');
167 Description: Constructor. Creates a new Bio::EnsEMBL::Mapper object.
168 Returntype : Bio::EnsEMBL::Mapper
169 Exceptions : none
170 Caller : general
171  
172 =cut
173  
174 sub new {
175 my ( $proto, $from, $to, $from_cs, $to_cs ) = @_;
176  
177 if ( !defined($to) || !defined($from) ) {
178 throw("Must supply 'to' and 'from' tags");
179 }
180  
181 my $class = ref($proto) || $proto;
182  
183 my $self = bless( { "_pair_$from" => {},
184 "_pair_$to" => {},
185 "_tree_$from" => {},
186 "_tree_$to" => {},
187 'pair_count' => 0,
188 'to' => $to,
189 'from' => $from,
190 'to_cs' => $to_cs,
191 'from_cs' => $from_cs
192 },
193 $class );
194  
195 # do sql to get any componente with muliple assemblys.
196  
197 return $self;
198 }
199  
200 =head2 flush
201  
202 Args : none
203 Example : none
204 Description: removes all cached information out of this mapper
205 Returntype : none
206 Exceptions : none
207 Caller : AssemblyMapper, ChainedAssemblyMapper
208  
209 =cut
210  
211 sub flush {
212 my $self = shift;
213 my $from = $self->from();
214 my $to = $self->to();
215  
216 $self->{"_pair_$from"} = {};
217 $self->{"_pair_$to"} = {};
218 $self->{"_tree_$from"} = {};
219 $self->{"_tree_$to"} = {};
220  
221 $self->{'pair_count'} = 0;
222 }
223  
224  
225  
226 =head2 map_coordinates
227  
228 Arg 1 string $id
229 id of 'source' sequence
230 Arg 2 int $start
231 start coordinate of 'source' sequence
232 Arg 3 int $end
233 end coordinate of 'source' sequence
234 Arg 4 int $strand
235 raw contig orientation (+/- 1)
236 Arg 5 string $type
237 nature of transform - gives the type of
238 coordinates to be transformed *from*
239 Arg 6 boolean (0 or 1) $include_original_region
240 option to include original input coordinate region mappings in the result
241 Arg 7 int $cdna_coding_start
242 cdna coding start
243 Function generic map method
244 Returntype if $include_original_region == 0
245 array of mappped Bio::EnsEMBL::Mapper::Coordinate
246 and/or Bio::EnsEMBL::Mapper::Gap
247 if $include_original_region == 1
248 hash of mapped and original Bio::EnsEMBL::Mapper::Coordinate
249 and/or Bio::EnsEMBL::Mapper::Gap
250 Exceptions none
251 Caller Bio::EnsEMBL::Mapper
252  
253 =cut
254  
255 sub map_coordinates {
256 my ( $self, $id, $start, $end, $strand, $type, $include_original_region, $cdna_coding_start ) = @_;
257 unless ( defined($id)
258 && defined($start)
259 && defined($end)
260 && defined($strand)
261 && defined($type) )
262 {
263 throw("Expecting at least 5 arguments");
264 }
265
266 $cdna_coding_start = defined $cdna_coding_start ? $cdna_coding_start : 1;
267
268 # special case for handling inserts:
269 if ( $start == $end + 1 ) {
270 return $self->map_insert( $id, $start, $end, $strand, $type, undef, $include_original_region);
271 }
272  
273 if ( !$self->{'_is_sorted'} ) { $self->_sort() }
274  
275 my $hash = $self->{"_pair_$type"};
276  
277 my ( $from, $to, $cs );
278  
279 if ( $type eq $self->{'to'} ) {
280 $from = 'to';
281 $to = 'from';
282 $cs = $self->{'from_cs'};
283 } else {
284 $from = 'from';
285 $to = 'to';
286 $cs = $self->{'to_cs'};
287 }
288  
289 unless ( defined $hash ) {
290 throw("Type $type is neither to or from coordinate systems");
291 }
292  
293 my @result;
294 my @paired_result;
295  
296 if ( !defined $hash->{ uc($id) } ) {
297 # one big gap!
298 my $gap = Bio::EnsEMBL::Mapper::Gap->new( $start, $end );
299 if ($include_original_region) {
300 push @paired_result, { 'original' => $gap, 'mapped' => $gap };
301 return @paired_result;
302 } else {
303 push @result, $gap;
304 return @result;
305 }
306 }
307  
308 my $last_used_pair;
309  
310 # if we don't already have an interval tree built for this id,
311 # build one now
312 if ( !defined( $self->{"_tree_$type"}->{ uc($id) } )) {
313 $self->{"_tree_$type"}->{ uc($id) } = _build_immutable_tree($from,
314 $hash->{ uc($id) });
315 }
316 # query the interval tree (either cached or created new) for overlapping intervals
317 my $overlap = $self->{"_tree_$type"}->{ uc($id) }->query($start, $end);
318  
319 my $rank = 0;
320 my $orig_start = $start;
321 my $last_target_coord = undef;
322 foreach my $i (@{$overlap}) {
323 my $pair = $i->data;
324 my $self_coord = $pair->{$from};
325 my $target_coord = $pair->{$to};
326  
327 #
328 # But not the case for haplotypes!! need to test for this case???
329 # so removing this till a better solution is found
330 #
331 #
332 # if($self_coord->{'start'} < $start){
333 # $start = $orig_start;
334 # $rank++;
335 # }
336  
337 if ( defined($last_target_coord) && $target_coord->{'id'} ne $last_target_coord ) {
338 if ( $self_coord->{'start'} < $start ) {
339 # i.e. the same bit is being mapped to another assembled bit
340 $start = $orig_start;
341 }
342 } else {
343 $last_target_coord = $target_coord->{'id'};
344 }
345  
346 if ( $start < $self_coord->{'start'} ) {
347 # gap detected
348 my $gap = Bio::EnsEMBL::Mapper::Gap->new( $start, $self_coord->{'start'} - 1, $rank );
349 push( @result, $gap );
350 push(@paired_result, { 'original' => $gap, 'mapped' => $gap });
351  
352 $start = $gap->{'end'} + 1;
353 }
354 my ( $target_start, $target_end);
355 my ( $ori_start, $ori_end);
356
357 my $res;
358 if ( exists $pair->{'indel'} ) {
359 # When next pair is an IndelPair and not a Coordinate, create the
360 # new mapping Coordinate, the IndelCoordinate.
361 $target_start = $target_coord->{'start'};
362 $target_end = $target_coord->{'end'};
363
364 #original coordinates
365 $ori_start = $self_coord->{'start'};
366 $ori_end = $self_coord->{'end'};
367
368 #create a Gap object
369 my $gap = Bio::EnsEMBL::Mapper::Gap->new( $start,
370 ( $self_coord->{'end'} < $end ? $self_coord->{'end'} : $end ) );
371 #create the Coordinate object
372 my $coord =
373 Bio::EnsEMBL::Mapper::Coordinate->new( $target_coord->{'id'},
374 $target_start, $target_end, $pair->{'ori'}*$strand, $cs );
375 #and finally, the IndelCoordinate object with
376 $res = Bio::EnsEMBL::Mapper::IndelCoordinate->new( $gap, $coord );
377
378 } else {
379 # start is somewhere inside the region
380 if ( $pair->{'ori'} == 1 ) {
381 $target_start = $target_coord->{'start'} + ( $start - $self_coord->{'start'} );
382 } else {
383 $target_end = $target_coord->{'end'} - ( $start - $self_coord->{'start'} );
384 }
385
386 # Either we are enveloping this map or not. If yes, then end
387 # point (self perspective) is determined solely by target. If
388 # not we need to adjust.
389  
390 if ( $end > $self_coord->{'end'} ) {
391 # enveloped
392 if ( $pair->{'ori'} == 1 ) {
393 $target_end = $target_coord->{'end'};
394 } else {
395 $target_start = $target_coord->{'start'};
396 }
397 } else {
398 # need to adjust end
399 if ( $pair->{'ori'} == 1 ) {
400 $target_end =
401 $target_coord->{'start'} +
402 ( $end - $self_coord->{'start'} );
403 } else {
404
405 $target_start =
406 $target_coord->{'end'} - ( $end - $self_coord->{'start'} );
407 }
408
409 }
410
411 $res = Bio::EnsEMBL::Mapper::Coordinate->new( $target_coord->{'id'},
412 $target_start, $target_end, $pair->{'ori'}*$strand,$cs, $rank );
413
414 #save the ori coordinate info
415 $ori_start = ($start - $cdna_coding_start) + 1;
416 $ori_end = $ori_start + ($target_end - $target_start);
417
418
419 } ## end else [ if ( exists $pair->{'indel'...})]
420  
421 push( @result, $res );
422 my $res_ori = Bio::EnsEMBL::Mapper::Coordinate->new( $self_coord->{'id'},
423 $ori_start, $ori_end, $pair->{'ori'}*$strand,$cs, $rank);
424 push(@paired_result, { 'original' => $res_ori, 'mapped' => $res });
425
426  
427 $last_used_pair = $pair;
428 $start = $self_coord->{'end'} + 1;
429  
430 } ## end for ( my $i = $start_idx...)
431  
432 if ( !defined $last_used_pair ) {
433 my $gap = Bio::EnsEMBL::Mapper::Gap->new( $start, $end );
434 push( @result, $gap );
435 push(@paired_result, { 'original' => $gap, 'mapped' => $gap });
436
437  
438 } elsif ( $last_used_pair->{$from}->{'end'} < $end ) {
439 # gap at the end
440 my $gap = Bio::EnsEMBL::Mapper::Gap->new(
441 $last_used_pair->{$from}->{'end'} + 1,
442 $end, $rank );
443 push( @result, $gap );
444
445 push(@paired_result, { 'original' => $gap, 'mapped' => $gap });
446 }
447  
448 if ( $strand == -1 ) {
449 @result = reverse(@result);
450 @paired_result = reverse(@paired_result);
451 }
452  
453 if ($include_original_region){
454 return @paired_result;
455 }else{
456 return @result;
457 }
458  
459 } ## end sub map_coordinates
460  
461  
462  
463 =head2 map_insert
464  
465 Arg [1] : string $id
466 Arg [2] : int $start - start coord. Since this is an insert should always
467 be one greater than end.
468 Arg [3] : int $end - end coord. Since this is an insert should always
469 be one less than start.
470 Arg [4] : int $strand (0, 1, -1)
471 Arg [5] : string $type - the coordinate system name the coords are from.
472 Arg [6] : boolean $fastmap - if specified, this is being called from
473 the fastmap call. The mapping done is not any faster for
474 inserts, but the return value is different.
475 Example :
476 Description: This is in internal function which handles the special mapping
477 case for inserts (start = end +1). This function will be called
478 automatically by the map function so there is no reason to
479 call it directly.
480 Returntype : list of Bio::EnsEMBL::Mapper::Coordinate and/or Gap objects
481 Exceptions : none
482 Caller : map_coordinates()
483  
484 =cut
485  
486 sub map_insert {
487 my ($self, $id, $start, $end, $strand, $type, $fastmap, $include_original_region) = @_;
488  
489 # swap start/end and map the resultant 2bp coordinate
490 ($start, $end) =($end,$start);
491  
492 my @coords = $self->map_coordinates($id, $start, $end, $strand, $type, $include_original_region);
493  
494 if(@coords == 1) {
495 my $c = $coords[0];
496 if ($include_original_region) {
497 my $m = $c->{'mapped'};
498 my $orig = $c->{'original'};
499 ($m->{'start'}, $m->{'end'}) = ($m->{'end'}, $m->{'start'});
500 ($orig->{'start'}, $orig->{'end'}) = ($orig->{'end'}, $orig->{'start'});
501 } else {
502 ($c->{'start'}, $c->{'end'}) = ($c->{'end'}, $c->{'start'});
503 }
504 } else {
505 throw("Unexpected: Got ",scalar(@coords)," expected 2.") if(@coords != 2);
506  
507 # adjust coordinates, remove gaps
508 my ($c1, $c2);
509  
510 if($strand == -1) {
511 ($c2,$c1) = @coords;
512 } else {
513 ($c1, $c2) = @coords;
514 }
515 @coords = ();
516  
517 my ($m1, $m2);
518 if ($include_original_region) {
519 ($m1, $m2) = ($c1->{'mapped'}, $c2->{'mapped'});
520 } else {
521 ($m1, $m2) = ($c1, $c2);
522 }
523 if(ref($m1) eq 'Bio::EnsEMBL::Mapper::Coordinate') {
524 # insert is after first coord
525 if($m1->{'strand'} * $strand == -1) {
526 $m1->{'end'}--;
527 } else {
528 $m1->{'start'}++;
529 }
530 if ($include_original_region) {
531 $c1->{'mapped'} = $m1;
532 @coords = ($c1);
533 } else {
534 @coords = ($m1);
535 }
536 }
537 if(ref($m2) eq 'Bio::EnsEMBL::Mapper::Coordinate') {
538 # insert is before second coord
539 if($m2->{'strand'} * $strand == -1) {
540 $m2->{'start'}++;
541 } else {
542 $m2->{'end'}--;
543 }
544 if($strand == -1) {
545 if ($include_original_region) {
546 $c2->{'mapped'} = $m2;
547 unshift @coords, $c2;
548 } else {
549 unshift @coords, $m2;
550 }
551 } else {
552 if ($include_original_region) {
553 $c2->{'mapped'} = $m2;
554 push @coords, $c2;
555 } else {
556 push @coords, $m2;
557 }
558 }
559 }
560  
561 # as with @coords == 1 above, if we are in
562 # include_original_region mode, then we have
563 # to flip back the original region as well
564 if ($include_original_region) {
565 foreach my $coord (@coords) {
566 my $orig = $coord->{'original'};
567 ($orig->{'start'}, $orig->{'end'}) =
568 ($orig->{'end'}, $orig->{'start'});
569 }
570 }
571 }
572  
573 if($fastmap) {
574 return undef if(@coords != 1);
575 my $c = $include_original_region ? $coords[0]->{'mapped'} : $coords[0];
576 return ($c->{'id'}, $c->{'start'}, $c->{'end'},
577 $c->{'strand'}, $c->{'coord_system'});
578 }
579  
580 return @coords;
581 }
582  
583  
584  
585  
586  
587  
588 =head2 fastmap
589  
590 Arg 1 string $id
591 id of 'source' sequence
592 Arg 2 int $start
593 start coordinate of 'source' sequence
594 Arg 3 int $end
595 end coordinate of 'source' sequence
596 Arg 4 int $strand
597 raw contig orientation (+/- 1)
598 Arg 5 int $type
599 nature of transform - gives the type of
600 coordinates to be transformed *from*
601 Function inferior map method. Will only do ungapped unsplit mapping.
602 Will return id, start, end strand in a list.
603 Returntype list of results
604 Exceptions none
605 Caller Bio::EnsEMBL::AssemblyMapper
606  
607 =cut
608  
609 sub fastmap {
610 my ($self, $id, $start, $end, $strand, $type) = @_;
611  
612 my ($from, $to, $cs);
613  
614 if($end+1 == $start) {
615 return $self->map_insert($id, $start, $end, $strand, $type, 1);
616 }
617  
618 if( ! $self->{'_is_sorted'} ) { $self->_sort() }
619  
620 if($type eq $self->{'to'}) {
621 $from = 'to';
622 $to = 'from';
623 $cs = $self->{'from_cs'};
624 } else {
625 $from = 'from';
626 $to = 'to';
627 $cs = $self->{'to_cs'};
628 }
629  
630 my $hash = $self->{"_pair_$type"} or
631 throw("Type $type is neither to or from coordinate systems");
632  
633  
634 my $pairs = $hash->{uc($id)};
635  
636 foreach my $pair (@$pairs) {
637 my $self_coord = $pair->{$from};
638 my $target_coord = $pair->{$to};
639  
640 # only super easy mapping is done
641 if( $start < $self_coord->{'start'} ||
642 $end > $self_coord->{'end'} ) {
643 next;
644 }
645  
646 if( $pair->{'ori'} == 1 ) {
647 return ( $target_coord->{'id'},
648 $target_coord->{'start'}+$start-$self_coord->{'start'},
649 $target_coord->{'start'}+$end-$self_coord->{'start'},
650 $strand, $cs );
651 } else {
652 return ( $target_coord->{'id'},
653 $target_coord->{'end'} - ($end - $self_coord->{'start'}),
654 $target_coord->{'end'} - ($start - $self_coord->{'start'}),
655 -$strand, $cs );
656 }
657 }
658  
659 return ();
660 }
661  
662  
663  
664 =head2 add_map_coordinates
665  
666 Arg 1 int $id
667 id of 'source' sequence
668 Arg 2 int $start
669 start coordinate of 'source' sequence
670 Arg 3 int $end
671 end coordinate of 'source' sequence
672 Arg 4 int $strand
673 relative orientation of source and target (+/- 1)
674 Arg 5 int $id
675 id of 'target' sequence
676 Arg 6 int $start
677 start coordinate of 'target' sequence
678 Arg 7 int $end
679 end coordinate of 'target' sequence
680 Function Stores details of mapping between
681 'source' and 'target' regions.
682 Returntype none
683 Exceptions none
684 Caller Bio::EnsEMBL::Mapper
685  
686 =cut
687  
688 sub add_map_coordinates {
689 my ( $self, $contig_id, $contig_start, $contig_end, $contig_ori,
690 $chr_name, $chr_start, $chr_end )
691 = @_;
692  
693 unless ( defined($contig_id)
694 && defined($contig_start)
695 && defined($contig_end)
696 && defined($contig_ori)
697 && defined($chr_name)
698 && defined($chr_start)
699 && defined($chr_end) )
700 {
701 throw("7 arguments expected");
702 }
703  
704 if ( ( $chr_end > $chr_start ) and ( $contig_end - $contig_start ) != ( $chr_end - $chr_start ) ) {
705 throw("Cannot deal with mis-lengthed mappings so far");
706 }
707  
708 my $from = Bio::EnsEMBL::Mapper::Unit->new( $contig_id, $contig_start,
709 $contig_end );
710 my $to =
711 Bio::EnsEMBL::Mapper::Unit->new( $chr_name, $chr_start, $chr_end );
712  
713 my $pair = Bio::EnsEMBL::Mapper::Pair->new( $from, $to, $contig_ori );
714  
715 # place into hash on both ids
716 my $map_to = $self->{'to'};
717 my $map_from = $self->{'from'};
718  
719 push( @{ $self->{"_pair_$map_to"}->{ uc($chr_name) } }, $pair );
720 push( @{ $self->{"_pair_$map_from"}->{ uc($contig_id) } }, $pair );
721  
722 # any interval trees for this set of intervals are now invalid
723 # so remove them
724 $self->{"_tree_$map_to"}->{ uc($contig_id) } = undef;
725 $self->{"_tree_$map_from"}->{ uc($contig_id) } = undef;
726  
727 $self->{'pair_count'}++;
728 $self->{'_is_sorted'} = 0;
729 } ## end sub add_map_coordinates
730  
731  
732 =head2 add_indel_coordinates
733  
734 Arg 1 int $id
735 id of 'source' sequence
736 Arg 2 int $start
737 start coordinate of 'source' sequence
738 Arg 3 int $end
739 end coordinate of 'source' sequence
740 Arg 4 int $strand
741 relative orientation of source and target (+/- 1)
742 Arg 5 int $id
743 id of 'targe' sequence
744 Arg 6 int $start
745 start coordinate of 'targe' sequence
746 Arg 7 int $end
747 end coordinate of 'targe' sequence
748 Function stores details of mapping between two regions:
749 'source' and 'target'. Returns 1 if the pair was added, 0 if it
750 was already in. Used when adding an indel
751 Returntype int 0,1
752 Exceptions none
753 Caller Bio::EnsEMBL::Mapper
754  
755 =cut
756  
757 sub add_indel_coordinates{
758 my ($self, $contig_id, $contig_start, $contig_end,
759 $contig_ori, $chr_name, $chr_start, $chr_end) = @_;
760  
761 unless(defined($contig_id) && defined($contig_start) && defined($contig_end)
762 && defined($contig_ori) && defined($chr_name) && defined($chr_start)
763 && defined($chr_end)) {
764 throw("7 arguments expected");
765 }
766  
767 #we need to create the IndelPair object to add to both lists, to and from
768 my $from =
769 Bio::EnsEMBL::Mapper::Unit->new($contig_id, $contig_start, $contig_end);
770 my $to =
771 Bio::EnsEMBL::Mapper::Unit->new($chr_name, $chr_start, $chr_end);
772  
773 my $pair = Bio::EnsEMBL::Mapper::IndelPair->new($from, $to, $contig_ori);
774  
775 # place into hash on both ids
776 my $map_to = $self->{'to'};
777 my $map_from = $self->{'from'};
778  
779 push( @{$self->{"_pair_$map_to"}->{uc($chr_name)}}, $pair );
780 push( @{$self->{"_pair_$map_from"}->{uc($contig_id)}}, $pair );
781  
782 # any interval trees for this set of intervals are now invalid
783 # so remove them
784 $self->{"_tree_$map_to"}->{ uc($chr_name) } = undef;
785 $self->{"_tree_$map_from"}->{ uc($contig_id) } = undef;
786  
787 $self->{'pair_count'}++;
788  
789 $self->{'_is_sorted'} = 0;
790 return 1;
791 }
792  
793  
794 =head2 map_indel
795  
796 Arg [1] : string $id
797 Arg [2] : int $start - start coord. Since this is an indel should always
798 be one greater than end.
799 Arg [3] : int $end - end coord. Since this is an indel should always
800 be one less than start.
801 Arg [4] : int $strand (0, 1, -1)
802 Arg [5] : string $type - the coordinate system name the coords are from.
803 Example : @coords = $mapper->map_indel();
804 Description: This is in internal function which handles the special mapping
805 case for indels (start = end +1). It will be used to map from
806 a coordinate system with a gap to another that contains an
807 insertion. It will be mainly used by the Variation API.
808 Returntype : Bio::EnsEMBL::Mapper::Unit objects
809 Exceptions : none
810 Caller : general
811  
812 =cut
813  
814 sub map_indel {
815 my ( $self, $id, $start, $end, $strand, $type ) = @_;
816  
817 # swap start/end and map the resultant 2bp coordinate
818 ( $start, $end ) = ( $end, $start );
819  
820 if ( !$self->{'_is_sorted'} ) { $self->_sort() }
821  
822 my $hash = $self->{"_pair_$type"};
823  
824 my ( $from, $to, $cs );
825  
826 if ( $type eq $self->{'to'} ) {
827 $from = 'to';
828 $to = 'from';
829 $cs = $self->{'from_cs'};
830 } else {
831 $from = 'from';
832 $to = 'to';
833 $cs = $self->{'to_cs'};
834 }
835  
836 unless ( defined $hash ) {
837 throw("Type $type is neither to or from coordinate systems");
838 }
839 my @indel_coordinates;
840  
841 # if we don't already have an interval tree built for this id,
842 # build one now
843 if ( !defined $self->{"_tree_$type"}->{ uc($id) } ) {
844 $self->{"_tree_$type"}->{ uc($id) } = _build_immutable_tree($from,
845 $hash->{ uc($id) });
846 }
847 # query the interval tree (either cached or created new) for overlapping intervals
848 my $overlap = $self->{"_tree_$type"}->{ uc($id) }->query($start, $end);
849  
850 foreach my $i (@{$overlap}) {
851 my $pair = $i->data;
852 my $self_coord = $pair->{$from};
853 my $target_coord = $pair->{$to};
854  
855 if ( exists $pair->{'indel'} ) {
856 #need to return unit coordinate
857 my $to =
858 Bio::EnsEMBL::Mapper::Unit->new( $target_coord->{'id'},
859 $target_coord->{'start'},
860 $target_coord->{'end'}, );
861 push @indel_coordinates, $to;
862 last;
863 }
864 }
865  
866 return @indel_coordinates;
867 } ## end sub map_indel
868  
869  
870 =head2 add_Mapper
871  
872 Arg 1 Bio::EnsEMBL::Mapper $mapper2
873 Example $mapper->add_Mapper($mapper2)
874 Function add all the map coordinates from $mapper to this mapper.
875 This object will contain mapping pairs from both the old
876 object and $mapper2.
877 Returntype int 0,1
878 Exceptions throw if 'to' and 'from' from both Bio::EnsEMBL::Mappers
879 are incompatible
880 Caller $mapper->methodname()
881  
882 =cut
883  
884 sub add_Mapper{
885 my ($self, $mapper) = @_;
886  
887 my $mapper_to = $mapper->{'to'};
888 my $mapper_from = $mapper->{'from'};
889 if ($mapper_to ne $self->{'to'} or $mapper_from ne $self->{'from'}) {
890 throw("Trying to add an incompatible Mapper");
891 }
892  
893 my $count_a = 0;
894 foreach my $seq_name (keys %{$mapper->{"_pair_$mapper_to"}}) {
895 push(@{$self->{"_pair_$mapper_to"}->{$seq_name}},
896 @{$mapper->{"_pair_$mapper_to"}->{$seq_name}});
897 $self->{"_tree_$mapper_to"}->{ uc($seq_name) } = undef;
898 $count_a += scalar(@{$mapper->{"_pair_$mapper_to"}->{$seq_name}});
899 }
900 my $count_b = 0;
901 foreach my $seq_name (keys %{$mapper->{"_pair_$mapper_from"}}) {
902 push(@{$self->{"_pair_$mapper_from"}->{$seq_name}},
903 @{$mapper->{"_pair_$mapper_from"}->{$seq_name}});
904 $self->{"_tree_$mapper_from"}->{ uc($seq_name) } = undef;
905 $count_b += scalar(@{$mapper->{"_pair_$mapper_from"}->{$seq_name}});
906 }
907  
908 if ($count_a == $count_b) {
909 $self->{'pair_count'} += $count_a;
910 } else {
911 throw("Trying to add a funny Mapper");
912 }
913  
914 $self->{'_is_sorted'} = 0;
915 return 1;
916 }
917  
918  
919  
920 =head2 list_pairs
921  
922 Arg 1 int $id
923 id of 'source' sequence
924 Arg 2 int $start
925 start coordinate of 'source' sequence
926 Arg 3 int $end
927 end coordinate of 'source' sequence
928 Arg 4 string $type
929 nature of transform - gives the type of
930 coordinates to be transformed *from*
931 Function list all pairs of mappings in a region
932 Returntype list of Bio::EnsEMBL::Mapper::Pair
933 Exceptions none
934 Caller Bio::EnsEMBL::Mapper
935  
936 =cut
937  
938 sub list_pairs {
939 my ( $self, $id, $start, $end, $type ) = @_;
940  
941 if ( !$self->{'_is_sorted'} ) { $self->_sort() }
942  
943 if ( !defined $type ) {
944 throw("Expected 4 arguments");
945 }
946  
947 if ( $start > $end ) {
948 throw( "Start is greater than end "
949 . "for id $id, start $start, end $end\n" );
950 }
951  
952 my $hash = $self->{"_pair_$type"};
953  
954 my ( $from, $to );
955  
956 if ( $type eq $self->{'to'} ) {
957 $from = 'to';
958 $to = 'from';
959 } else {
960 $from = 'from';
961 $to = 'to';
962 }
963  
964 unless ( defined $hash ) {
965 throw("Type $type is neither to or from coordinate systems");
966 }
967  
968 my @list;
969  
970 unless ( exists $hash->{ uc($id) } ) {
971 return ();
972 }
973  
974 @list = @{ $hash->{ uc($id) } };
975  
976 my @output;
977 if ( $start == -1 && $end == -1 ) {
978 return @list;
979 } else {
980  
981 foreach my $p (@list) {
982  
983 if ( $p->{$from}->{'end'} < $start ) {
984 next;
985 }
986 if ( $p->{$from}->{'start'} > $end ) {
987 last;
988 }
989 push( @output, $p );
990 }
991 return @output;
992 }
993 } ## end sub list_pairs
994  
995  
996 =head2 to
997  
998 Arg 1 Bio::EnsEMBL::Mapper::Unit $id
999 id of 'source' sequence
1000 Function accessor method form the 'source'
1001 and 'target' in a Mapper::Pair
1002 Returntype Bio::EnsEMBL::Mapper::Unit
1003 Exceptions none
1004 Caller Bio::EnsEMBL::Mapper
1005  
1006 =cut
1007  
1008 sub to {
1009 my ( $self, $value ) = @_;
1010  
1011 if ( defined($value) ) {
1012 $self->{'to'} = $value;
1013 }
1014  
1015 return $self->{'to'};
1016 }
1017  
1018 =head2 from
1019  
1020 Arg 1 Bio::EnsEMBL::Mapper::Unit $id
1021 id of 'source' sequence
1022 Function accessor method form the 'source'
1023 and 'target' in a Mapper::Pair
1024 Returntype Bio::EnsEMBL::Mapper::Unit
1025 Exceptions none
1026 Caller Bio::EnsEMBL::Mapper
1027  
1028 =cut
1029 sub from {
1030 my ( $self, $value ) = @_;
1031  
1032 if ( defined($value) ) {
1033 $self->{'from'} = $value;
1034 }
1035  
1036 return $self->{'from'};
1037 }
1038  
1039  
1040 # _dump
1041 #
1042 # Arg 1 *FileHandle $fh
1043 # Function convenience dump function
1044 # possibly useful for debugging
1045 # Returntype none
1046 # Exceptions none
1047 # Caller internal
1048 #
1049  
1050 sub _dump{
1051 my ($self,$fh) = @_;
1052  
1053 if( !defined $fh ) {
1054 $fh = \*STDERR;
1055 }
1056  
1057 my $from = $self->{'from'};
1058 my $to = $self->{'to'};
1059  
1060 print $fh "dumping from-hash _pair_$from\n";
1061 foreach my $id ( keys %{$self->{"_pair_$from"}} ) {
1062 print $fh "{_pair_$from}->{" . uc($id) . "}:\n";
1063 foreach my $pair ( @{$self->{"_pair_$from"}->{uc($id)}} ) {
1064 print $fh " ",$pair->from->start," ",$pair->from->end,":",$pair->to->start," ",$pair->to->end," ",$pair->to->id,"\n";
1065 }
1066 if (defined($self->{"_tree_$from"}->{uc($id)})) {
1067 print $fh "{_tree_$from}->{" . uc($id) . "} instantiated\n";
1068 } else {
1069 print $fh "{_tree_$from}->{" . uc($id) . "} empty\n";
1070 }
1071 }
1072 print $fh "dumping to-hash _pair_$to\n";
1073 foreach my $id ( keys %{$self->{"_pair_$to"}} ) {
1074 print $fh "{_pair_$to}->{" . uc($id) . "}:\n";
1075 foreach my $pair ( @{$self->{"_pair_$to"}->{uc($id)}} ) {
1076 print $fh " ",$pair->to->start," ",$pair->to->end,":",$pair->from->start," ",$pair->from->end," ",$pair->from->id,"\n";
1077 }
1078 if (defined($self->{"_tree_$to"}->{uc($id)})) {
1079 print $fh "{_tree_$to}->{" . uc($id) . "} instantiated\n";
1080 } else {
1081 print $fh "{_tree_$to}->{" . uc($id) . "} empty\n";
1082 }
1083 }
1084 }
1085  
1086  
1087 # _sort
1088 #
1089 # Function sort function so that all
1090 # mappings are sorted by
1091 # chromosome start
1092 # Returntype none
1093 # Exceptions none
1094 # Caller internal
1095 #
1096  
1097 sub _sort {
1098 my ($self) = @_;
1099  
1100 my $to = $self->{'to'};
1101 my $from = $self->{'from'};
1102  
1103 foreach my $id ( keys %{ $self->{"_pair_$from"} } ) {
1104 @{ $self->{"_pair_$from"}->{$id} } =
1105 sort { $a->{'from'}->{'start'} <=> $b->{'from'}->{'start'} }
1106 @{ $self->{"_pair_$from"}->{$id} };
1107 }
1108  
1109 foreach my $id ( keys %{ $self->{"_pair_$to"} } ) {
1110 @{ $self->{"_pair_$to"}->{$id} } =
1111 sort { $a->{'to'}->{'start'} <=> $b->{'to'}->{'start'} }
1112 @{ $self->{"_pair_$to"}->{$id} };
1113 }
1114  
1115 $self->_merge_pairs();
1116 $self->_is_sorted(1);
1117 }
1118  
1119 # this function merges pairs that are adjacent into one
1120 sub _merge_pairs {
1121 my $self = shift;
1122  
1123 my ( $lr, $lr_from, $del_pair, $next_pair, $current_pair );
1124  
1125 my $map_to = $self->{'to'};
1126 my $map_from = $self->{'from'};
1127 $self->{'pair_count'} = 0;
1128  
1129 for my $key ( keys %{$self->{"_pair_$map_to"}} ) {
1130 # merging pairs means all interval trees for
1131 # this set of intervals are now invalid, so delete them
1132 $self->{"_tree_$map_to"}->{ uc($key) } = undef;
1133 $lr = $self->{"_pair_$map_to"}->{$key};
1134
1135 my $i = 0;
1136 my $next = 1;
1137 my $length = $#{$lr};
1138 while( $next <= $length ) {
1139 $current_pair = $lr->[$i];
1140 $next_pair = $lr->[$next];
1141 $del_pair = undef;
1142
1143 if(exists $current_pair->{'indel'} || exists $next_pair->{'indel'}){
1144 #necessary to modify the merge function to not merge indels
1145 $next++;
1146 $i++;
1147 }
1148 else{
1149 # duplicate filter
1150 if( $current_pair->{'to'}->{'start'} == $next_pair->{'to'}->{'start'}
1151 && $current_pair->{'from'}->{'id'} == $next_pair->{'from'}->{'id'}
1152 && $current_pair->{'from'}->{'start'} == $next_pair->{'from'}->{'start'}) {
1153 # Modified in e75 to support GRCh38. Contigs used repeatedly in one seq region were
1154 # being pre-emptively deleted as copies. Extra from-start condition above ensures copy
1155 # deletion is restricted to same-location copies. Even more stringent checks can be made
1156 # at cost of speed.
1157 $del_pair = $next_pair;
1158 } elsif ( ( $current_pair->{'from'}->{'id'} eq $next_pair->{'from'}->{'id'} ) &&
1159 ( $next_pair->{'ori'} == $current_pair->{'ori'} ) &&
1160 ( $next_pair->{'to'}->{'start'} -1 == $current_pair->{'to'}->{'end'} )) {
1161
1162 if( $current_pair->{'ori'} == 1 ) {
1163 # check forward strand merge
1164 if( $next_pair->{'from'}->{'start'} - 1 == $current_pair->{'from'}->{'end'} ) {
1165 # normal merge with previous element
1166 $current_pair->{'to'}->{'end'} = $next_pair->{'to'}->{'end'};
1167 $current_pair->{'from'}->{'end'} = $next_pair->{'from'}->{'end'};
1168 $del_pair = $next_pair;
1169 }
1170 } else {
1171 # check backward strand merge
1172 if( $next_pair->{'from'}->{'end'} + 1 == $current_pair->{'from'}->{'start'} ) {
1173 # yes its a merge
1174 $current_pair->{'to'}->{'end'} = $next_pair->{'to'}->{'end'};
1175 $current_pair->{'from'}->{'start'} = $next_pair->{'from'}->{'start'};
1176 $del_pair = $next_pair;
1177 }
1178 }
1179 }
1180
1181 if( defined $del_pair ) {
1182 splice( @$lr, $next, 1 );
1183 $lr_from = $self->{"_pair_$map_from"}->{uc($del_pair->{'from'}->{'id'})};
1184 for( my $j=0; $j <= $#{$lr_from}; $j++ ) {
1185 if( $lr_from->[$j] == $del_pair ) {
1186 splice( @$lr_from, $j, 1 );
1187 last;
1188 }
1189 }
1190 $length--;
1191 if( $length < $next ) { last; }
1192 }
1193 else {
1194 $next++;
1195 $i++;
1196 }
1197 }
1198
1199 }
1200 $self->{'pair_count'} += scalar( @$lr );
1201 }
1202 }
1203  
1204  
1205 # _is_sorted
1206 #
1207 # Arg 1 int $sorted
1208 # Function toggle for whether the (internal)
1209 # map data are sorted
1210 # Returntype int
1211 # Exceptions none
1212 # Caller internal
1213 #
1214  
1215 sub _is_sorted {
1216 my ($self, $value) = @_;
1217 $self->{'_is_sorted'} = $value if (defined($value));
1218 return $self->{'_is_sorted'};
1219 }
1220  
1221 # _build_immutable_tree
1222 #
1223 # Arg 1 string $pair_side - the from or to half of each pair to be
1224 # the source of intervals
1225 # Arg 2 listref $pair_list - a list of Bio::EnsEMBL::Mapper::Pair
1226 # Function builds a Bio::EnsEMBL::Utils::Tree::Interval::Immutable with
1227 # intervals corresponding to the chosen side (from or to) of
1228 # each Pair in $pair_list and a pointer to each Pair
1229 # Returntype Bio::EnsEMBL::Utils::Tree::Interval::Immutable
1230 # Exceptions none
1231 # Caller internal
1232  
1233 sub _build_immutable_tree {
1234 my ($pair_side, $pair_list) = @_;
1235 # create set of intervals for the tree
1236 my $from_intervals;
1237  
1238 foreach my $i (@{$pair_list}) {
1239 my $start = $i->{$pair_side}{start};
1240 my $end = $i->{$pair_side}{end};
1241  
1242 if ($end < $start) {
1243 ($end, $start) = ($start, $end);
1244 }
1245  
1246 push @{$from_intervals}, Bio::EnsEMBL::Utils::Interval->new($start, $end, $i);
1247 }
1248  
1249 # Create the interval tree defined on the above set of intervals
1250 #
1251 # As of release/99, we have more experience with the immutable
1252 # interval tree, so we will stick with this one. A future
1253 # refactoring effort may wish to replace this with a dynamically
1254 # maintained mutable interval tree, rather than simply throwing
1255 # trees away and rebuilding when the underlying interval set changes
1256 return Bio::EnsEMBL::Utils::Tree::Interval::Immutable->new($from_intervals);
1257 }
1258  
1259 1;