EnsEMBL/Slice.pm untested

filter: Statementstmt 0.0% 0 / 852 Branchbran 0.0% 0 / 338 Conditioncond 0.0% 0 / 98 Subroutinesub 0.0% 0 / 109 Podpod 0.0% 0 / 95 totaltotal 0.0% 0 / 1492 CC 170 SCAR 39.9
CC
170
Cov
0%
expand
306.0
new
182.0
project
132.0
CRAP 53.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::Slice - Arbitary Slice of a genome
34  
35 =head1 SYNOPSIS
36  
37 $sa = $db->get_SliceAdaptor;
38  
39 $slice =
40 $sa->fetch_by_region( 'chromosome', 'X', 1_000_000, 2_000_000 );
41  
42 # get some attributes of the slice
43 my $seqname = $slice->seq_region_name();
44 my $start = $slice->start();
45 my $end = $slice->end();
46  
47 # get the sequence from the slice
48 my $seq = $slice->seq();
49  
50 # get some features from the slice
51 foreach $gene ( @{ $slice->get_all_Genes } ) {
52 # do something with a gene
53 }
54  
55 foreach my $feature ( @{ $slice->get_all_DnaAlignFeatures } ) {
56 # do something with dna-dna alignments
57 }
58  
59 =head1 DESCRIPTION
60  
61 A Slice object represents a region of a genome. It can be used to retrieve
62 sequence or features from an area of interest.
63  
64 NOTE: The Slice is defined by its Strand, but normal behaviour for get_all_*
65 methods is to return Features on both Strands.
66  
67 =head1 METHODS
68  
69 =cut
70  
71 package Bio::EnsEMBL::Slice;
72 $Bio::EnsEMBL::Slice::VERSION = '114.0.0_50'; # TRIAL
73 $Bio::EnsEMBL::Slice::VERSION = '114.0.050';
74 use vars qw(@ISA);
75 use strict;
76  
77 use Bio::PrimarySeqI;
78  
79  
80 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
81 use Bio::EnsEMBL::Utils::Exception qw(throw deprecate warning);
82 use Bio::EnsEMBL::RepeatMaskedSlice;
83 use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp);
84 use Bio::EnsEMBL::ProjectionSegment;
85 use Bio::EnsEMBL::Registry;
86 use Bio::EnsEMBL::Utils::Iterator;
87 use Bio::EnsEMBL::DBSQL::MergedAdaptor;
88  
89 use Bio::EnsEMBL::Mapper::RangeRegistry;
90 use Bio::EnsEMBL::SeqRegionSynonym;
91 use Scalar::Util qw(weaken isweak);
92  
93 # use Data::Dumper;
94  
95 my $registry = "Bio::EnsEMBL::Registry";
96  
97 @ISA = qw(Bio::PrimarySeqI);
98  
99  
100 =head2 new
101  
102 Arg [...] : List of named arguments
103 Bio::EnsEMBL::CoordSystem COORD_SYSTEM
104 string SEQ_REGION_NAME,
105 int START,
106 int END,
107 int SEQ_REGION_LENGTH, (optional)
108 string SEQ (optional)
109 int STRAND, (optional, defaults to 1)
110 Bio::EnsEMBL::DBSQL::SliceAdaptor ADAPTOR (optional)
111 Example : $slice = Bio::EnsEMBL::Slice->new(-coord_system => $cs,
112 -start => 1,
113 -end => 10000,
114 -strand => 1,
115 -seq_region_name => 'X',
116 -seq_region_length => 12e6,
117 -adaptor => $slice_adaptor);
118 Description: Creates a new slice object. A slice represents a region
119 of sequence in a particular coordinate system. Slices can be
120 used to retrieve sequence and features from an area of
121 interest in a genome.
122  
123 Coordinates start at 1 and are inclusive. Negative
124 coordinates or coordinates exceeding the length of the
125 seq_region are permitted. Start must be less than or equal.
126 to end regardless of the strand.
127  
128 Slice objects are immutable. Once instantiated their attributes
129 (with the exception of the adaptor) may not be altered. To
130 change the attributes a new slice must be created.
131 Returntype : Bio::EnsEMBL::Slice
132 Exceptions : throws if start, end, coordsystem or seq_region_name not specified or not of the correct type
133 Caller : general, Bio::EnsEMBL::SliceAdaptor
134 Status : Stable
135  
136 =cut
137  
138 sub new {
139 my $caller = shift;
140  
141 #new can be called as a class or object method
142 my $class = ref($caller) || $caller;
143  
144 my ($seq, $coord_system, $seq_region_name, $seq_region_length,
145 $start, $end, $strand, $adaptor, $empty) =
146 rearrange([qw(SEQ COORD_SYSTEM SEQ_REGION_NAME SEQ_REGION_LENGTH
147 START END STRAND ADAPTOR EMPTY)], @_);
148  
149 if ( !defined($seq_region_name) ) {
150 throw('SEQ_REGION_NAME argument is required');
151 }
152 if ( !defined($start) ) { throw('START argument is required') }
153 if ( !defined($end) ) { throw('END argument is required') }
154  
155 ## if ( $start > $end + 1 ) {
156 ## throw('start must be less than or equal to end+1');
157 ## }
158  
159 if ( !defined($seq_region_length) ) { $seq_region_length = $end }
160  
161 if ( $seq_region_length <= 0 ) {
162 throw('SEQ_REGION_LENGTH must be > 0');
163 }
164  
165 if ( defined($seq) && CORE::length($seq) != ( $end - $start + 1 ) ) {
166 throw('SEQ must be the same length as the defined LENGTH not '
167 . CORE::length($seq)
168 . ' compared to '
169 . ( $end - $start + 1 ) );
170 }
171  
172 if(defined($coord_system)) {
173 if(!ref($coord_system) || !$coord_system->isa('Bio::EnsEMBL::CoordSystem')){
174 throw('COORD_SYSTEM argument must be a Bio::EnsEMBL::CoordSystem');
175 }
176 if($coord_system->is_top_level()) {
177 throw('Cannot create slice on toplevel CoordSystem.');
178 }
179 } else {
180 warning("Slice without coordinate system");
181 }
182  
183 $strand ||= 1;
184  
185 if($strand != 1 && $strand != -1) {
186 throw('STRAND argument must be -1 or 1');
187 }
188  
189 if(defined($adaptor)) {
190 if(!ref($adaptor) || !$adaptor->isa('Bio::EnsEMBL::DBSQL::SliceAdaptor')) {
191 throw('ADAPTOR argument must be a Bio::EnsEMBL::DBSQL::SliceAdaptor');
192 }
193 }
194  
195 my $self = bless {'coord_system' => $coord_system,
196 'seq' => $seq,
197 'seq_region_name' => $seq_region_name,
198 'seq_region_length' => $seq_region_length,
199 'start' => int($start),
200 'end' => int($end),
201 'strand' => $strand}, $class;
202  
203 $self->adaptor($adaptor);
204  
205 return $self;
206  
207 }
208  
209 =head2 new_fast
210  
211 Arg [1] : hashref to be blessed
212 Description: Construct a new Bio::EnsEMBL::Slice using the hashref.
213 Exceptions : none
214 Returntype : Bio::EnsEMBL::Slice
215 Caller : general
216 Status : Stable
217  
218 =cut
219  
220  
221 sub new_fast {
222 my $class = shift;
223 my $hashref = shift;
224 my $self = bless $hashref, $class;
225 weaken($self->{adaptor}) if ( ! isweak($self->{adaptor}) );
226 return $self;
227 }
228  
229 =head2 adaptor
230  
231 Arg [1] : (optional) Bio::EnsEMBL::DBSQL::SliceAdaptor $adaptor
232 Example : $adaptor = $slice->adaptor();
233 Description: Getter/Setter for the slice object adaptor used
234 by this slice for database interaction.
235 Returntype : Bio::EnsEMBL::DBSQL::SliceAdaptor
236 Exceptions : thorws if argument passed is not a SliceAdaptor
237 Caller : general
238 Status : Stable
239  
240 =cut
241  
242 sub adaptor{
243 my $self = shift;
244  
245 if(@_) {
246 my $ad = shift;
247 if(defined($ad)) {
248 if(!ref($ad) || !$ad->isa('Bio::EnsEMBL::DBSQL::SliceAdaptor')) {
249 throw('Argument must be a Bio::EnsEMBL::DBSQL::SliceAdaptor');
250 }
251 }
252 weaken($self->{'adaptor'} = $ad);
253 }
254  
255 return $self->{'adaptor'};
256 }
257  
258  
259  
260 =head2 seq_region_name
261  
262 Arg [1] : none
263 Example : $seq_region = $slice->seq_region_name();
264 Description: Returns the name of the seq_region that this slice is on. For
265 example if this slice is in chromosomal coordinates the
266 seq_region_name might be 'X' or '10'.
267  
268 This function was formerly named chr_name, but since slices can
269 now be on coordinate systems other than chromosomal it has been
270 changed.
271 Returntype : string
272 Exceptions : none
273 Caller : general
274 Status : Stable
275  
276 =cut
277  
278 sub seq_region_name {
279 my $self = shift;
280 return $self->{'seq_region_name'};
281 }
282  
283 =head2 seq_region_start
284  
285 Example : $seq_region_start = $slice->seq_region_start();
286 Description: Returns the start position of this slice relative to the
287 start of the sequence region that it was created on.
288 Since slices are always in genomic coordinates this is
289 an alias to start()
290 Returntype : int
291 Exceptions : none
292 Caller : general
293 Status : Stable
294  
295 =cut
296  
297 sub seq_region_start {
298 my $self = shift;
299 return $self->start();
300 }
301  
302 =head2 seq_region_end
303  
304 Example : $seq_region_end = $slice->seq_region_end();
305 Description: Returns the end position of this slice relative to the
306 start of the sequence region that it was created on.
307 Since slices are always in genomic coordinates this is
308 an alias to end()
309 Returntype : int
310 Exceptions : none
311 Caller : general
312 Status : Stable
313  
314 =cut
315  
316 sub seq_region_end {
317 my $self = shift;
318 return $self->end();
319 }
320  
321 =head2 seq_region_length
322  
323 Arg [1] : none
324 Example : $seq_region_length = $slice->seq_region_length();
325 Description: Returns the length of the entire seq_region that this slice is
326 on. For example if this slice is on a chromosome this will be
327 the length (in basepairs) of the entire chromosome.
328 Returntype : int
329 Exceptions : none
330 Caller : general
331 Status : Stable
332  
333 =cut
334  
335 sub seq_region_length {
336 my $self = shift;
337 return $self->{'seq_region_length'};
338 }
339  
340  
341 =head2 coord_system
342  
343 Arg [1] : none
344 Example : print $slice->coord_system->name();
345 Description: Returns the coordinate system that this slice is on.
346 Returntype : Bio::EnsEMBL::CoordSystem
347 Exceptions : none
348 Caller : general
349 Status : Stable
350  
351 =cut
352  
353 sub coord_system {
354 my $self = shift;
355 return $self->{'coord_system'};
356 }
357  
358 =head2 source
359  
360 Arg [1] : (optional) String $value
361 Example : print $slice->source();
362 Description: Returns the source this slice is coming from
363 Returntype : string
364 Exceptions : none
365 Caller : general
366 Status : Stable
367  
368 =cut
369  
370 sub source {
371 my $self = shift;
372 $self->{'source'} = shift if (@_);
373 return $self->{'source'};
374 }
375  
376 =head2 coord_system_name
377  
378 Arg [1] : none
379 Example : print $slice->coord_system_name()
380 Description: Convenience method. Gets the name of the coord_system which
381 this slice is on.
382 Returns undef if this Slice does not have an attached
383 CoordSystem.
384 Returntype: string or undef
385 Exceptions: none
386 Caller : general
387 Status : Stable
388  
389 =cut
390  
391 sub coord_system_name {
392 my $self = shift;
393 my $csystem = $self->{'coord_system'};
394 return ($csystem) ? $csystem->name() : undef;
395 }
396  
397  
398 =head2 centrepoint
399  
400 Arg [1] : none
401 Example : $cp = $slice->centrepoint();
402 Description: Returns the mid position of this slice relative to the
403 start of the sequence region that it was created on.
404 Coordinates are inclusive and start at 1.
405 Returntype : int
406 Exceptions : none
407 Caller : general
408 Status : Stable
409  
410 =cut
411  
412 sub centrepoint {
413 my $self = shift;
414 return ($self->{'start'}+$self->{'end'})/2;
415 }
416  
417 =head2 start
418  
419 Arg [1] : none
420 Example : $start = $slice->start();
421 Description: Returns the start position of this slice relative to the
422 start of the sequence region that it was created on.
423 Coordinates are inclusive and start at 1. Negative coordinates
424 or coordinates exceeding the length of the sequence region are
425 permitted. Start is always less than or equal to end
426 regardless of the orientation of the slice.
427 Returntype : int
428 Exceptions : none
429 Caller : general
430 Status : Stable
431  
432 =cut
433  
434 sub start {
435 my $self = shift;
436 return $self->{'start'};
437 }
438  
439  
440  
441 =head2 end
442  
443 Arg [1] : none
444 Example : $end = $slice->end();
445 Description: Returns the end position of this slice relative to the
446 start of the sequence region that it was created on.
447 Coordinates are inclusive and start at 1. Negative coordinates
448 or coordinates exceeding the length of the sequence region are
449 permitted. End is always greater than or equal to start
450 regardless of the orientation of the slice.
451 Returntype : int
452 Exceptions : none
453 Caller : general
454 Status : Stable
455  
456 =cut
457  
458 sub end {
459 my $self = shift;
460 return $self->{'end'};
461 }
462  
463  
464  
465 =head2 strand
466  
467 Arg [1] : none
468 Example : $strand = $slice->strand();
469 Description: Returns the orientation of this slice on the seq_region it has
470 been created on
471 Returntype : int (either 1 or -1)
472 Exceptions : none
473 Caller : general, invert
474 Status : Stable
475  
476 =cut
477  
478 sub strand{
479 my $self = shift;
480 return $self->{'strand'};
481 }
482  
483  
484  
485  
486  
487 =head2 name
488  
489 Arg [1] : none
490 Example : my $results = $cache{$slice->name()};
491 Description: Returns the name of this slice. The name is formatted as a colon
492 delimited string with the following attributes:
493 coord_system:version:seq_region_name:start:end:strand
494  
495 Slices with the same name are equivalent and thus the name can
496 act as a hash key.
497 Returntype : string
498 Exceptions : none
499 Caller : general
500 Status : Stable
501  
502 =cut
503  
504 sub name {
505 my $self = shift;
506  
507 my $cs = $self->{'coord_system'};
508  
509 return join(':',
510 ($cs) ? $cs->name() : '',
511 ($cs) ? $cs->version() : '',
512 $self->{'seq_region_name'},
513 $self->{'start'},
514 $self->{'end'},
515 $self->{'strand'});
516 }
517  
518  
519  
520 =head2 length
521  
522 Arg [1] : none
523 Example : $length = $slice->length();
524 Description: Returns the length of this slice in basepairs
525 Returntype : int
526 Exceptions : none
527 Caller : general
528 Status : Stable
529  
530 =cut
531  
532 sub length {
533 my ($self) = @_;
534  
535 my $length = $self->{'end'} - $self->{'start'} + 1;
536
537 if ( $self->{'start'} > $self->{'end'} && $self->is_circular() ) {
538 $length += $self->{'seq_region_length'};
539 }
540  
541 return $length;
542 }
543  
544 =head2 is_reference
545 Arg : none
546 Example : my $reference = $slice->is_reference()
547 Description: Returns 1 if slice is a reference slice else 0
548 Returntype : int
549 Caller : general
550 Status : At Risk
551  
552 =cut
553  
554 sub is_reference {
555 my ($self) = @_;
556  
557 if ( !defined( $self->{'is_reference'} ) ) {
558 $self->{'is_reference'} =
559 $self->adaptor()->is_reference( $self->get_seq_region_id() );
560 }
561  
562 return $self->{'is_reference'};
563 }
564  
565 =head2 is_toplevel
566 Arg : none
567 Example : my $top = $slice->is_toplevel()
568 Description: Returns 1 if slice is a toplevel slice else 0
569 Returntype : int
570 Caller : general
571 Status : At Risk
572  
573 =cut
574  
575 sub is_toplevel {
576 my ($self) = @_;
577  
578 if ( !defined( $self->{'toplevel'} ) ) {
579 $self->{'toplevel'} =
580 $self->adaptor()->is_toplevel( $self->get_seq_region_id() );
581 }
582  
583 return $self->{'toplevel'};
584 }
585  
586 =head2 has_karyotype
587 Arg : none
588 Example : my $top = $slice->has_karyotype()
589 Description: Returns 1 if slice is part of the karyotype else 0
590 Returntype : int
591 Caller : general
592 Status : At Risk
593  
594 =cut
595  
596 sub has_karyotype {
597 my ($self) = @_;
598  
599 if ( !defined( $self->{'karyotype'} ) ) {
600 $self->{'karyotype'} =
601 $self->adaptor()->has_karyotype( $self->get_seq_region_id() );
602 }
603  
604 return $self->{'karyotype'};
605 }
606  
607 =head2 karyotype_rank
608 Arg : none
609 Example : my $rank = $slice->karyotype_rank()
610 Description: Returns the numeric ranking in the karyotype. Otherwise 0 is returned
611 Returntype : int
612 Caller : general
613 Status : At Risk
614  
615 =cut
616  
617 sub karyotype_rank {
618 my ($self) = @_;
619 if(! defined( $self->{karyotype_rank})) {
620 my $rank = $self->adaptor()->get_karyotype_rank($self->get_seq_region_id());
621 $self->{karyotype_rank} = $rank if $rank;
622 }
623 return $self->{karyotype_rank} || 0;
624 }
625  
626 =head2 is_circular
627 Arg : none
628 Example : my $circ = $slice->is_circular()
629 Description: Returns 1 if slice is a circular slice else 0
630 Returntype : int
631 Caller : general
632 Status : Stable
633  
634 =cut
635  
636 sub is_circular {
637 my ($self) = @_;
638 my $adaptor = $self->adaptor();
639 return 0 if ! defined $adaptor;
640 if (! exists $self->{'circular'}) {
641 my $id = $adaptor->get_seq_region_id($self);
642 $self->{circular} = $adaptor->is_circular($id);
643 }
644 return $self->{circular};
645 }
646  
647 =head2 invert
648  
649 Arg [1] : none
650 Example : $inverted_slice = $slice->invert;
651 Description: Creates a copy of this slice on the opposite strand and
652 returns it.
653 Returntype : Bio::EnsEMBL::Slice
654 Exceptions : none
655 Caller : general
656 Status : Stable
657  
658 =cut
659  
660 sub invert {
661 my $self = shift;
662  
663 # make a shallow copy of the slice via a hash copy and flip the strand
664 my %s = %$self;
665 $s{'strand'} = $self->{'strand'} * -1;
666  
667 # reverse compliment any attached sequence
668 reverse_comp(\$s{'seq'}) if($s{'seq'});
669  
670 # bless and return the copy
671 return bless \%s, ref $self;
672 }
673  
674  
675  
676 =head2 seq
677  
678 Arg [1] : none
679 Example : print "SEQUENCE = ", $slice->seq();
680 Description: Returns the sequence of the region represented by this
681 slice formatted as a string.
682 Only available for the default coord_system
683 Returntype : string
684 Exceptions : none
685 Caller : general
686 Status : Stable
687  
688 =cut
689  
690 sub seq {
691 my $self = shift;
692  
693 # special case for in-between (insert) coordinates
694 return '' if($self->start() == $self->end() + 1);
695  
696 return $self->{'seq'} if($self->{'seq'});
697  
698 if($self->adaptor()) {
699 my $seqAdaptor = $self->adaptor()->db()->get_SequenceAdaptor();
700 return ${$seqAdaptor->fetch_by_Slice_start_end_strand($self,1,undef,1)};
701 }
702  
703 # no attached sequence, and no db, so just return Ns
704 return 'N' x $self->length();
705 }
706  
707  
708  
709 =head2 subseq
710  
711 Arg [1] : int $startBasePair
712 relative to start of slice, which is 1.
713 Arg [2] : int $endBasePair
714 relative to start of slice.
715 Arg [3] : (optional) int $strand
716 The strand of the slice to obtain sequence from. Default
717 value is 1.
718 Description: returns string of dna sequence
719 Returntype : txt
720 Exceptions : end should be at least as big as start
721 strand must be set
722 Caller : general
723 Status : Stable
724  
725 =cut
726  
727 sub subseq {
728 my ( $self, $start, $end, $strand ) = @_;
729  
730 if ( $end+1 < $start ) {
731 throw("End coord + 1 is less than start coord");
732 }
733  
734 # handle 'between' case for insertions
735 return '' if( $start == $end + 1);
736  
737 $strand = 1 unless(defined $strand);
738  
739 if ( $strand != -1 && $strand != 1 ) {
740 throw("Invalid strand [$strand] in call to Slice::subseq.");
741 }
742 my $subseq;
743 if($self->adaptor){
744 my $seqAdaptor = $self->adaptor->db->get_SequenceAdaptor();
745 $subseq = ${$seqAdaptor->fetch_by_Slice_start_end_strand
746 ( $self, $start,
747 $end, $strand )};
748 } else {
749 ## check for gap at the beginning and pad it with Ns
750 if ($start < 1) {
751 $subseq = "N" x (1 - $start);
752 $start = 1;
753 }
754 $subseq .= substr ($self->seq(), $start-1, $end - $start + 1);
755 ## check for gap at the end and pad it with Ns
756 if ($end > $self->length()) {
757 $subseq .= "N" x ($end - $self->length());
758 }
759 reverse_comp(\$subseq) if($strand == -1);
760 }
761 return $subseq;
762 }
763  
764 =head2 sub_Slice_Iterator
765  
766 Arg[1] : int The chunk size to request
767 Example : my $i = $slice->sub_Slice_Iterator(60000);
768 while($i->has_next()) { warn $i->next()->name(); }
769 Description : Returns an iterator which batches subslices of this Slice
770 in the requested chunk size
771 Returntype : Bio::EnsEMBL::Utils::Iterator next() will return the next
772 chunk of Slice
773 Exceptions : None
774  
775 =cut
776  
777 sub sub_Slice_Iterator {
778 my ($self, $chunk_size) = @_;
779 throw "Need a chunk size to divide the slice by" if ! $chunk_size;
780 my $here = 1;
781 my $end = $self->length();
782 my $iterator_sub = sub {
783 while($here <= $end) {
784 my $there = $here + $chunk_size - 1;
785 $there = $end if($there > $end);
786 my $slice = $self->sub_Slice($here, $there);
787 $here = $there + 1;
788 return $slice;
789 }
790 return;
791 };
792 return Bio::EnsEMBL::Utils::Iterator->new($iterator_sub);
793 }
794  
795 =head2 assembly_exception_type
796  
797 Example : $self->assembly_exception_type();
798 Description : Returns the type of slice this is. If it is reference then you
799 will get 'REF' back. Otherwise you will get the first
800 element from C<get_all_AssemblyExceptionFeatures()>. If no
801 assembly exception exists you will get an empty string back.
802 Returntype : String
803 Exceptions : None
804 Caller : Public
805 Status : Beta
806  
807 =cut
808  
809 sub assembly_exception_type {
810 my ($self) = @_;
811 my $type = q{};
812 if($self->is_reference()) {
813 $type = 'REF';
814 }
815 else {
816 my $assembly_exceptions = $self->get_all_AssemblyExceptionFeatures();
817 if(@{$assembly_exceptions}) {
818 $type = $assembly_exceptions->[0]->type();
819 }
820 }
821 return $type;
822 }
823  
824 =head2 is_chromosome
825  
826 Example : print ($slice->is_chromosome()) ? 'I am a chromosome' : 'Not one';
827 Description : A chromosome is a slice with a karyotype rank assigned to it.
828 Returntype : Boolean indicates if the current object is a chromosome
829 Exceptions : None
830  
831 =cut
832  
833 sub is_chromosome {
834 my ($self) = @_;
835  
836 return $self->has_karyotype();
837 }
838  
839  
840 =head2 get_base_count
841  
842 Arg [1] : none
843 Example : $c_count = $slice->get_base_count->{'c'};
844 Description: Retrieves a hashref containing the counts of each bases in the
845 sequence spanned by this slice. The format of the hash is :
846 { 'a' => num,
847 'c' => num,
848 't' => num,
849 'g' => num,
850 'n' => num,
851 '%gc' => num }
852  
853 All bases which are not in the set [A,a,C,c,T,t,G,g] are
854 included in the 'n' count. The 'n' count could therefore be
855 inclusive of ambiguity codes such as 'y'.
856 The %gc is the ratio of GC to AT content as in:
857 total(GC)/total(ACTG) * 100
858 This function is conservative in its memory usage and scales to
859 work for entire chromosomes.
860 Returntype : hashref
861 Exceptions : none
862 Caller : general
863 Status : Stable
864  
865 =cut
866  
867 sub get_base_count {
868 my $self = shift;
869  
870 my $a = 0;
871 my $c = 0;
872 my $t = 0;
873 my $g = 0;
874  
875 my $start = 1;
876 my $end;
877  
878 my $RANGE = 100_000;
879 my $len = $self->length();
880  
881 my $seq;
882  
883 while ( $start <= $len ) {
884 $end = $start + $RANGE - 1;
885 $end = $len if ( $end > $len );
886  
887 $seq = $self->subseq( $start, $end );
888  
889 $a += $seq =~ tr/Aa//;
890 $c += $seq =~ tr/Cc//;
891 $t += $seq =~ tr/Tt//;
892 $g += $seq =~ tr/Gg//;
893  
894 $start = $end + 1;
895 }
896  
897 my $actg = $a + $c + $t + $g;
898  
899 my $gc_content = 0;
900 if ( $actg > 0 ) { # Avoid dividing by 0
901 $gc_content = sprintf( "%1.2f", ( ( $g + $c )/$actg )*100 );
902 }
903  
904 return { 'a' => $a,
905 'c' => $c,
906 't' => $t,
907 'g' => $g,
908 'n' => $len - $actg,
909 '%gc' => $gc_content };
910 }
911  
912  
913  
914 =head2 project
915  
916 Arg [1] : string $name
917 The name of the coordinate system to project this slice onto
918 Arg [2] : string $version
919 The version of the coordinate system (such as 'NCBI34') to
920 project this slice onto
921 Example :
922 my $clone_projection = $slice->project('clone');
923  
924 foreach my $segment (@$clone_projection) {
925 my $clone = $segment->to_Slice();
926 print $slice->seq_region_name(), ':', $segment->from_start(), '-',
927 $segment->from_end(), ' -> ',
928 $clone->seq_region_name(), ':', $clone->start(), '-',$clone->end(),
929 ':', $clone->strand(), "\n";
930 }
931 Description: Returns the results of 'projecting' this slice onto another
932 coordinate system. Projecting to a coordinate system that
933 the slice is assembled from is analagous to retrieving a tiling
934 path. This method may also be used to 'project up' to a higher
935 level coordinate system, however.
936  
937 This method returns a listref of triplets [start,end,slice]
938 which represents the projection. The start and end defined the
939 region of this slice which is made up of the third value of
940 the triplet: a slice in the requested coordinate system.
941 Returntype : list reference of Bio::EnsEMBL::ProjectionSegment objects which
942 can also be used as [$start,$end,$slice] triplets
943 Exceptions : none
944 Caller : general
945 Status : Stable
946  
947 =cut
948  
949 sub project {
950 my $self = shift;
951 my $cs_name = shift;
952 my $cs_version = shift;
953  
954 throw('Coord_system name argument is required') if(!$cs_name);
955  
956 my $slice_adaptor = $self->adaptor();
957  
958 if(!$slice_adaptor) {
959 warning("Cannot project without attached adaptor.");
960 return [];
961 }
962  
963 if(!$self->coord_system()) {
964 warning("Cannot project without attached coord system.");
965 return [];
966 }
967  
968  
969 my $db = $slice_adaptor->db();
970 my $csa = $db->get_CoordSystemAdaptor();
971 my $cs = $csa->fetch_by_name($cs_name, $cs_version);
972 my $slice_cs = $self->coord_system();
973  
974 if(!$cs) {
975 throw("Cannot project to unknown coordinate system " .
976 "[$cs_name $cs_version]");
977 }
978  
979 # no mapping is needed if the requested coord system is the one we are in
980 # but we do need to check if some of the slice is outside of defined regions
981 if($slice_cs->equals($cs)) {
982 return $self->_constrain_to_region();
983 }
984  
985 my @projection;
986  
987 # decompose this slice into its symlinked components.
988 # this allows us to handle haplotypes and PARs
989 my $normal_slice_proj =
990 $slice_adaptor->fetch_normalized_slice_projection($self);
991 foreach my $segment (@$normal_slice_proj) {
992 my $normal_slice = $segment->[2];
993  
994 $slice_cs = $normal_slice->coord_system();
995  
996 my $asma = $db->get_AssemblyMapperAdaptor();
997 my $asm_mapper = $asma->fetch_by_CoordSystems($slice_cs, $cs);
998 next unless defined $asm_mapper;
999
1000 # perform the mapping between this slice and the requested system
1001 my @coords = $asm_mapper->map($normal_slice->seq_region_name(),
1002 $normal_slice->start(),
1003 $normal_slice->end(),
1004 $normal_slice->strand(),
1005 $slice_cs, undef, undef, 1);
1006  
1007 # construct a projection from the mapping results and return it
1008 foreach my $coord (@coords) {
1009 my $original = $coord->{original};
1010 my $mapped = $coord->{mapped};
1011  
1012 # skip gaps
1013 next unless $mapped->isa('Bio::EnsEMBL::Mapper::Coordinate');
1014  
1015 my $mapped_start = $mapped->start();
1016 my $mapped_end = $mapped->end();
1017 my ($current_start, $current_end);
1018 if ($self->strand == 1) {
1019 $current_start = $original->start() - $self->start + 1;
1020 $current_end = $original->end() - $self->start + 1;
1021 } else {
1022 $current_start = $self->end() - $original->end + 1;
1023 $current_end = $self->end - $original->start + 1;
1024 }
1025
1026 my $coord_cs = $mapped->coord_system();
1027  
1028 # If the normalised projection just ended up mapping to the
1029 # same coordinate system we were already in then we should just
1030 # return the original region. This can happen for example, if we
1031 # were on a PAR region on Y which refered to X and a projection to
1032 # 'toplevel' was requested.
1033 if($coord_cs->equals($slice_cs)) {
1034 # trim off regions which are not defined
1035 return $self->_constrain_to_region();
1036 }
1037 #create slices for the mapped-to coord system
1038 my $slice = $slice_adaptor->fetch_by_seq_region_id($mapped->id(),
1039 $mapped_start,
1040 $mapped_end,
1041 $mapped->strand());
1042  
1043 if ($current_end > $slice->seq_region_length() && $slice->is_circular ) {
1044 $current_end -= $slice->seq_region_length();
1045 }
1046
1047 push @projection, bless([$current_start, $current_end, $slice],
1048 "Bio::EnsEMBL::ProjectionSegment");
1049
1050 }
1051 }
1052  
1053 return \@projection;
1054 }
1055  
1056  
1057 sub _constrain_to_region {
1058 my $self = shift;
1059  
1060 my $entire_len = $self->seq_region_length();
1061  
1062 #if the slice has negative coordinates or coordinates exceeding the
1063 #exceeding length of the sequence region we want to shrink the slice to
1064 #the defined region
1065  
1066 if($self->{'start'} > $entire_len || $self->{'end'} < 1) {
1067 #none of this slice is in a defined region
1068 return [];
1069 }
1070  
1071 my $right_contract = 0;
1072 my $left_contract = 0;
1073 if($self->{'end'} > $entire_len) {
1074 $right_contract = $entire_len - $self->{'end'};
1075 }
1076 if($self->{'start'} < 1) {
1077 $left_contract = $self->{'start'} - 1;
1078 }
1079  
1080 my $new_slice;
1081 if($left_contract || $right_contract) {
1082 #if slice in negative strand, need to swap contracts
1083 if ($self->strand == 1) {
1084 $new_slice = $self->expand($left_contract, $right_contract);
1085 }
1086 elsif ($self->strand == -1) {
1087 $new_slice = $self->expand($right_contract, $left_contract);
1088 }
1089 } else {
1090 $new_slice = $self;
1091 }
1092  
1093 return [bless [1-$left_contract, $self->length()+$right_contract,
1094 $new_slice], "Bio::EnsEMBL::ProjectionSegment" ];
1095 }
1096  
1097  
1098 =head2 expand
1099  
1100 Arg [1] : (optional) int $five_prime_expand
1101 The number of basepairs to shift this slices five_prime
1102 coordinate by. Positive values make the slice larger,
1103 negative make the slice smaller.
1104 coordinate left.
1105 Default = 0.
1106 Arg [2] : (optional) int $three_prime_expand
1107 The number of basepairs to shift this slices three_prime
1108 coordinate by. Positive values make the slice larger,
1109 negative make the slice smaller.
1110 Default = 0.
1111 Arg [3] : (optional) bool $force_expand
1112 if set to 1, then the slice will be contracted even in the case
1113 when shifts $five_prime_expand and $three_prime_expand overlap.
1114 In that case $five_prime_expand and $three_prime_expand will be set
1115 to a maximum possible number and that will result in the slice
1116 which would have only 2pbs.
1117 Default = 0.
1118 Arg [4] : (optional) int* $fpref
1119 The reference to a number of basepairs to shift this slices five_prime
1120 coordinate by. Normally it would be set to $five_prime_expand.
1121 But in case when $five_prime_expand shift can not be applied and
1122 $force_expand is set to 1, then $$fpref will contain the maximum possible
1123 shift
1124 Arg [5] : (optional) int* $tpref
1125 The reference to a number of basepairs to shift this slices three_prime
1126 coordinate by. Normally it would be set to $three_prime_expand.
1127 But in case when $five_prime_expand shift can not be applied and
1128 $force_expand is set to 1, then $$tpref will contain the maximum possible
1129 shift
1130 Example : my $expanded_slice = $slice->expand( 1000, 1000);
1131 my $contracted_slice = $slice->expand(-1000,-1000);
1132 my $shifted_right_slice = $slice->expand(-1000, 1000);
1133 my $shifted_left_slice = $slice->expand( 1000,-1000);
1134 my $forced_contracted_slice = $slice->expand(-1000,-1000, 1, \$five_prime_shift, \$three_prime_shift);
1135  
1136 Description: Returns a slice which is a resized copy of this slice. The
1137 start and end are moved outwards from the center of the slice
1138 if positive values are provided and moved inwards if negative
1139 values are provided. This slice remains unchanged. A slice
1140 may not be contracted below 1bp but may grow to be arbitrarily
1141 large.
1142 Returntype : Bio::EnsEMBL::Slice
1143 Exceptions : warning if an attempt is made to contract the slice below 1bp
1144 Caller : general
1145 Status : Stable
1146  
1147 =cut
1148  
1149 sub expand {
1150 my $self = shift;
1151 my $five_prime_shift = shift || 0;
1152 my $three_prime_shift = shift || 0;
1153 my $force_expand = shift || 0;
1154 my $fpref = shift;
1155 my $tpref = shift;
1156  
1157 if ( $self->{'seq'} ) {
1158 warning(
1159 "Cannot expand a slice which has a manually attached sequence ");
1160 return undef;
1161 }
1162  
1163 my $sshift = $five_prime_shift;
1164 my $eshift = $three_prime_shift;
1165  
1166 if ($sshift == 0 && $eshift == 0) { return $self; }
1167  
1168 if ( $self->{'strand'} != 1 ) {
1169 $eshift = $five_prime_shift;
1170 $sshift = $three_prime_shift;
1171 }
1172  
1173 my $new_start = $self->{'start'} - $sshift;
1174 my $new_end = $self->{'end'} + $eshift;
1175  
1176 # Wrap around on circular slices
1177 if (( $new_start <= 0 || $new_start > $self->seq_region_length() || $new_end <= 0
1178 || $new_end > $self->seq_region_length() ) && ( $self->is_circular() ) ) {
1179
1180 if ( $new_start <= 0 ) {
1181 $new_start = $self->seq_region_length() + $new_start;
1182 }
1183 if ( $new_start > $self->seq_region_length() ) {
1184 $new_start -= $self->seq_region_length();
1185 }
1186
1187 if ( $new_end <= 0 ) {
1188 $new_end = $self->seq_region_length() + $new_end;
1189 }
1190 if ( $new_end > $self->seq_region_length() ) {
1191 $new_end -= $self->seq_region_length();
1192 }
1193
1194 }
1195  
1196 if ( $new_start > $new_end && (not $self->is_circular() ) ) {
1197
1198 if ($force_expand) {
1199 # Apply max possible shift, if force_expand is set
1200 if ( $sshift < 0 ) {
1201 # if we are contracting the slice from the start - move the
1202 # start just before the end
1203 $new_start = $new_end - 1;
1204 $sshift = $self->{start} - $new_start;
1205 }
1206  
1207 if ( $new_start > $new_end ) {
1208 # if the slice still has a negative length - try to move the
1209 # end
1210 if ( $eshift < 0 ) {
1211 $new_end = $new_start + 1;
1212 $eshift = $new_end - $self->{end};
1213 }
1214 }
1215 # return the values by which the primes were actually shifted
1216 $$tpref = $self->{strand} == 1 ? $eshift : $sshift;
1217 $$fpref = $self->{strand} == 1 ? $sshift : $eshift;
1218 }
1219 if ( $new_start > $new_end ) {
1220 throw('Slice start cannot be greater than slice end');
1221 }
1222 }
1223  
1224 #fastest way to copy a slice is to do a shallow hash copy
1225 my %new_slice = %$self;
1226 $new_slice{'start'} = int($new_start);
1227 $new_slice{'end'} = int($new_end);
1228  
1229 return bless \%new_slice, ref($self);
1230 } ## end sub expand
1231  
1232 =head2 constrain_to_seq_region
1233 Example : $new_slice = $slice->expand(1000,10000);
1234 $new_slice = $new_slice->constrain_to_seq_region();
1235 Description: Used to prevent overly zealous expand calls going off the end of
1236 the sequence region. It contracts the start and end where needed
1237 and produces a slice copy with the tweaked coordinates.
1238 Returntype : Bio::EnsEMBL::Slice
1239 =cut
1240  
1241 sub constrain_to_seq_region {
1242 my ($self) = @_;
1243 # circular calculations should already be taken care of
1244 if ($self->is_circular) {return $self}
1245 my $new_start = $self->start;
1246 my $new_end = $self->end;
1247
1248 my $seq_region = $self->seq_region_Slice;
1249
1250 if ($new_start < $seq_region->start) {$new_start = $seq_region->start}
1251 if ($new_end > $seq_region->end) {$new_end = $seq_region->end}
1252
1253 my %new_slice = %$self;
1254 $new_slice{'start'} = $new_start;
1255 $new_slice{'end'} = $new_end;
1256  
1257 return bless \%new_slice, ref($self);
1258 }
1259  
1260  
1261 =head2 sub_Slice
1262  
1263 Arg 1 : int $start, refers to the start of the subslice relative to the input slice
1264 Arg 2 : int $end, refers to the end of the subslice relative to the input slice
1265 Arge [3] : int $strand
1266 Example : none
1267 Description: Makes another Slice that covers only part of this Slice
1268 If a Slice is requested which lies outside of the boundaries
1269 of this function will return undef. This means that
1270 behaviour will be consistant whether or not the slice is
1271 attached to the database (i.e. if there is attached sequence
1272 to the slice). Alternatively the expand() method or the
1273 SliceAdaptor::fetch_by_region method can be used instead.
1274 Returntype : Bio::EnsEMBL::Slice or undef if arguments are wrong
1275 Exceptions : none
1276 Caller : general
1277 Status : Stable
1278  
1279 =cut
1280  
1281 sub sub_Slice {
1282 my ( $self, $start, $end, $strand ) = @_;
1283  
1284 my $length = $self->length();
1285  
1286 if( $start < 1 || $start > $length ) {
1287 # throw( "start argument not valid" );
1288 return undef;
1289 }
1290  
1291 if( $end < $start || $end > $length ) {
1292 # throw( "end argument not valid" )
1293 return undef;
1294 }
1295  
1296 my ( $new_start, $new_end, $new_strand, $new_seq );
1297 if( ! defined $strand ) {
1298 $strand = 1;
1299 }
1300  
1301 if( $self->{'strand'} == 1 ) {
1302 $new_start = $self->{'start'} + $start - 1;
1303 $new_end = $self->{'start'} + $end - 1;
1304 $new_strand = $strand;
1305 } else {
1306 $new_start = $self->{'end'} - $end + 1;;
1307 $new_end = $self->{'end'} - $start + 1;
1308 $new_strand = -$strand;
1309 }
1310  
1311 if( defined $self->{'seq'} ) {
1312 $new_seq = $self->subseq( $start, $end, $strand );
1313 }
1314  
1315 #fastest way to copy a slice is to do a shallow hash copy
1316 my $new_slice = {%{$self}};
1317 $new_slice->{'start'} = int($new_start);
1318 $new_slice->{'end'} = int($new_end);
1319 $new_slice->{'strand'} = $new_strand;
1320 if( $new_seq ) {
1321 $new_slice->{'seq'} = $new_seq;
1322 }
1323 weaken($new_slice->{adaptor});
1324  
1325 return bless $new_slice, ref($self);
1326 }
1327  
1328  
1329  
1330 =head2 seq_region_Slice
1331  
1332 Arg [1] : none
1333 Example : $slice = $slice->seq_region_Slice();
1334 Description: Returns a slice which spans the whole seq_region which this slice
1335 is on. For example if this is a slice which spans a small region
1336 of chromosome X, this method will return a slice which covers the
1337 entire chromosome X. The returned slice will always have strand
1338 of 1 and start of 1. This method cannot be used if the sequence
1339 of the slice has been set manually.
1340 Returntype : Bio::EnsEMBL::Slice
1341 Exceptions : warning if called when sequence of Slice has been set manually.
1342 Caller : general
1343 Status : Stable
1344  
1345 =cut
1346  
1347 sub seq_region_Slice {
1348 my $self = shift;
1349  
1350 if($self->{'seq'}){
1351 warning("Cannot get a seq_region_Slice of a slice which has manually ".
1352 "attached sequence ");
1353 return undef;
1354 }
1355  
1356 # quick shallow copy
1357 my $slice;
1358 %{$slice} = %{$self};
1359 bless $slice, ref($self);
1360 weaken($slice->{adaptor});
1361  
1362 $slice->{'start'} = 1;
1363 $slice->{'end'} = $slice->{'seq_region_length'};
1364 $slice->{'strand'} = 1;
1365  
1366 return $slice;
1367 }
1368  
1369  
1370 =head2 get_seq_region_id
1371  
1372 Arg [1] : none
1373 Example : my $seq_region_id = $slice->get_seq_region_id();
1374 Description: Gets the internal identifier of the seq_region that this slice
1375 is on. Note that this function will not work correctly if this
1376 slice does not have an attached adaptor. Also note that it may
1377 be better to go through the SliceAdaptor::get_seq_region_id
1378 method if you are working with multiple databases since is
1379 possible to work with slices from databases with different
1380 internal seq_region identifiers.
1381 Returntype : int or undef if slices does not have attached adaptor
1382 Exceptions : warning if slice is not associated with a SliceAdaptor
1383 Caller : assembly loading scripts, general
1384 Status : Stable
1385  
1386 =cut
1387  
1388 sub get_seq_region_id {
1389 my ($self) = @_;
1390 if($self->adaptor) {
1391 return $self->adaptor->get_seq_region_id($self);
1392 }
1393 else {
1394 warning('Cannot retrieve seq_region_id without attached adaptor.');
1395 return undef;
1396 }
1397 }
1398  
1399 =head2 get_genome_component
1400  
1401 Arg [] : none
1402 Example : my $genome_component = $slice->get_genome_component();
1403 Description: Returns the genome component of the slice
1404 Returntype : Scalar; the identifier of the genome component of the slice
1405 Exceptions : none
1406 Caller : general
1407 Status : Stable
1408  
1409 =cut
1410  
1411 sub get_genome_component {
1412 my $self = shift;
1413 return $self->adaptor->get_genome_component_for_slice($self);
1414 }
1415  
1416 =head2 get_all_Attributes
1417  
1418 Arg [1] : optional string $attrib_code
1419 The code of the attribute type to retrieve values for.
1420 Example : ($htg_phase) = @{$slice->get_all_Attributes('htg_phase')};
1421 @slice_attributes = @{$slice->get_all_Attributes()};
1422 Description: Gets a list of Attributes of this slice''s seq_region.
1423 Optionally just get Attrubutes for given code.
1424 Returntype : listref Bio::EnsEMBL::Attribute
1425 Exceptions : warning if slice does not have attached adaptor
1426 Caller : general
1427 Status : Stable
1428  
1429 =cut
1430  
1431 sub get_all_Attributes {
1432 my ($self, $attrib_code) = @_;
1433 if(my $adaptor = $self->_get_CoreAdaptor('Attribute')) {
1434 my $attribs = $adaptor->fetch_all_by_Slice($self);
1435 if(defined $attrib_code) {
1436 my $uc_attrib = uc($attrib_code);
1437 return [ grep { uc($_->code()) eq $uc_attrib } @{$attribs}];
1438 }
1439 return $attribs;
1440 }
1441 return [];
1442 }
1443  
1444 =head2 get_all_PredictionTranscripts
1445  
1446 Arg [1] : (optional) string $logic_name
1447 The name of the analysis used to generate the prediction
1448 transcripts obtained.
1449 Arg [2] : (optional) boolean $load_exons
1450 If set to true will force loading of all PredictionExons
1451 immediately rather than loading them on demand later. This
1452 is faster if there are a large number of PredictionTranscripts
1453 and the exons will be used.
1454 Example : @transcripts = @{$slice->get_all_PredictionTranscripts};
1455 Description: Retrieves the list of prediction transcripts which overlap
1456 this slice with logic_name $logic_name. If logic_name is
1457 not defined then all prediction transcripts are retrieved.
1458 Returntype : listref of Bio::EnsEMBL::PredictionTranscript
1459 Exceptions : warning if slice does not have attached adaptor
1460 Caller : none
1461 Status : Stable
1462  
1463 =cut
1464  
1465 sub get_all_PredictionTranscripts {
1466 my ($self,$logic_name, $load_exons) = @_;
1467  
1468 if(!$self->adaptor()) {
1469 warning('Cannot get PredictionTranscripts without attached adaptor');
1470 return [];
1471 }
1472 my $pta = $self->adaptor()->db()->get_PredictionTranscriptAdaptor();
1473 return $pta->fetch_all_by_Slice($self, $logic_name, $load_exons);
1474 }
1475  
1476 =head2 get_all_DnaAlignFeatures
1477  
1478 Arg [1] : (optional) string $logic_name
1479 The name of the analysis performed on the dna align features
1480 to obtain.
1481 Arg [2] : (optional) float $score
1482 The mimimum score of the features to retrieve
1483 Arg [3] : (optional) string $dbtype
1484 The name of an attached database to retrieve the features from
1485 instead, e.g. 'otherfeatures'.
1486 Arg [4] : (optional) float hcoverage
1487 The minimum hcoverage od the featurs to retrieve
1488 Example : @dna_dna_align_feats = @{$slice->get_all_DnaAlignFeatures};
1489 Description: Retrieves the DnaDnaAlignFeatures which overlap this slice with
1490 logic name $logic_name and with score above $score. If
1491 $logic_name is not defined features of all logic names are
1492 retrieved. If $score is not defined features of all scores are
1493 retrieved. Strand of the Slice is not considered.
1494 Returntype : listref of Bio::EnsEMBL::DnaDnaAlignFeatures
1495 Exceptions : warning if slice does not have attached adaptor
1496 Caller : general
1497 Status : Stable
1498  
1499 =cut
1500  
1501 sub get_all_DnaAlignFeatures {
1502 my ($self, $logic_name, $score, $dbtype, $hcoverage) = @_;
1503 return $self->_get_AlignFeatures($logic_name, $score, $dbtype, $hcoverage, 'DnaAlignFeature');
1504 }
1505  
1506 =head2 get_all_ProteinAlignFeatures
1507  
1508 Arg [1] : (optional) string $logic_name
1509 The name of the analysis performed on the protein align features
1510 to obtain.
1511 Arg [2] : (optional) float $score
1512 The mimimum score of the features to retrieve
1513 Arg [3] : (optional) string $dbtype
1514 The name of an attached database to retrieve features from
1515 instead.
1516 Arg [4] : (optional) float hcoverage
1517 The minimum hcoverage od the featurs to retrieve
1518 Example : @dna_pep_align_feats = @{$slice->get_all_ProteinAlignFeatures};
1519 Description: Retrieves the DnaPepAlignFeatures which overlap this slice with
1520 logic name $logic_name and with score above $score. If
1521 $logic_name is not defined features of all logic names are
1522 retrieved. If $score is not defined features of all scores are
1523 retrieved.
1524 Returntype : listref of Bio::EnsEMBL::DnaPepAlignFeatures
1525 Exceptions : warning if slice does not have attached adaptor
1526 Caller : general
1527 Status : Stable
1528  
1529 =cut
1530  
1531 sub get_all_ProteinAlignFeatures {
1532 my ($self, $logic_name, $score, $dbtype, $hcoverage) = @_;
1533 return $self->_get_AlignFeatures($logic_name, $score, $dbtype, $hcoverage, 'ProteinAlignFeature');
1534 }
1535  
1536 =head2 _get_AlignFeatures
1537  
1538 Arg [1] : (optional) string $logic_name
1539 The name of the analysis performed on the protein align features
1540 to obtain.
1541 Arg [2] : (optional) float $score
1542 The mimimum score of the features to retrieve
1543 Arg [3] : (optional) string $dbtype
1544 The name of an attached database to retrieve features from
1545 instead.
1546 Arg [4] : (optional) float hcoverage
1547 The minimum hcoverage od the featurs to retrieve
1548 Arg [5] : string $align_type
1549 The type of adaptor to retrieve alignments from. Must be an BaseAlignFeature derived
1550 class
1551 Description: Generic method which deals with the retrieval of either AlignFeature adaptor and allows
1552 you to switch the adaptor values are retrieved from.
1553  
1554 =cut
1555  
1556 sub _get_AlignFeatures {
1557 my ($self, $logic_name, $score, $dbtype, $hcoverage, $align_type) = @_;
1558 throw "No align_type given" unless $align_type;
1559 if(my $adaptor = $self->_get_CoreAdaptor($align_type, $dbtype)) {
1560 if(defined($score) and defined ($hcoverage)){
1561 warning "Cannot specify score and hcoverage when querying for $align_type. Using score only";
1562 }
1563 if(defined($score)){
1564 return $adaptor->fetch_all_by_Slice_and_score($self,$score, $logic_name);
1565 }
1566 return $adaptor->fetch_all_by_Slice_and_hcoverage($self, $hcoverage, $logic_name);
1567 }
1568 return [];
1569 }
1570  
1571 =head2 get_all_SimilarityFeatures
1572  
1573 Arg [1] : (optional) string $logic_name
1574 the name of the analysis performed on the features to retrieve
1575 Arg [2] : (optional) float $score
1576 the lower bound of the score of the features to be retrieved
1577 Example : @feats = @{$slice->get_all_SimilarityFeatures};
1578 Description: Retrieves all dna_align_features and protein_align_features
1579 with analysis named $logic_name and with score above $score.
1580 It is probably faster to use get_all_ProteinAlignFeatures or
1581 get_all_DnaAlignFeatures if a sepcific feature type is desired.
1582 If $logic_name is not defined features of all logic names are
1583 retrieved. If $score is not defined features of all scores are
1584 retrieved.
1585 Returntype : listref of Bio::EnsEMBL::BaseAlignFeatures
1586 Exceptions : warning if slice does not have attached adaptor
1587 Caller : general
1588 Status : Stable
1589  
1590 =cut
1591  
1592 sub get_all_SimilarityFeatures {
1593 my ($self, $logic_name, $score) = @_;
1594  
1595 my @out = ();
1596  
1597 push @out, @{$self->get_all_ProteinAlignFeatures($logic_name, $score) };
1598 push @out, @{$self->get_all_DnaAlignFeatures($logic_name, $score) };
1599  
1600 return \@out;
1601 }
1602  
1603 =head2 get_all_SimpleFeatures
1604  
1605 Arg [1] : (optional) string $logic_name
1606 The name of the analysis performed on the simple features
1607 to obtain.
1608 Arg [2] : (optional) float $score
1609 The mimimum score of the features to retrieve
1610 Example : @simple_feats = @{$slice->get_all_SimpleFeatures};
1611 Description: Retrieves the SimpleFeatures which overlap this slice with
1612 logic name $logic_name and with score above $score. If
1613 $logic_name is not defined features of all logic names are
1614 retrieved. If $score is not defined features of all scores are
1615 retrieved.
1616 Returntype : listref of Bio::EnsEMBL::SimpleFeatures
1617 Exceptions : warning if slice does not have attached adaptor
1618 Caller : general
1619 Status : Stable
1620  
1621 =cut
1622  
1623 sub get_all_SimpleFeatures {
1624 my ($self, $logic_name, $score, $dbtype) = @_;
1625 if(my $adaptor = $self->_get_CoreAdaptor('SimpleFeature', $dbtype)) {
1626 return $adaptor->fetch_all_by_Slice_and_score($self, $score, $logic_name);
1627 }
1628 return [];
1629 }
1630  
1631 =head2 get_all_RepeatFeatures
1632  
1633 Arg [1] : (optional) string $logic_name
1634 The name of the analysis performed on the repeat features
1635 to obtain.
1636 Arg [2] : (optional) string/array $repeat_type
1637 Limits features returned to those of the specified
1638 repeat_type. Can specify a single value or an array reference
1639 to limit by more than one
1640 Arg [3] : (optional) string $db
1641 Key for database e.g. core/vega/cdna/....
1642 Example : @repeat_feats = @{$slice->get_all_RepeatFeatures(undef,'Type II Transposons')};
1643 Description: Retrieves the RepeatFeatures which overlap with
1644 logic name $logic_name and with score above $score. If
1645 $logic_name is not defined features of all logic names are
1646 retrieved.
1647 Returntype : listref of Bio::EnsEMBL::RepeatFeatures
1648 Exceptions : warning if slice does not have attached adaptor
1649 Caller : general
1650 Status : Stable
1651  
1652 =cut
1653  
1654 sub get_all_RepeatFeatures {
1655 my ($self, $logic_name, $repeat_type, $dbtype) = @_;
1656 if(my $adaptor = $self->_get_CoreAdaptor('RepeatFeature', $dbtype)) {
1657 return $adaptor->fetch_all_by_Slice($self, $logic_name, $repeat_type);
1658 }
1659 return [];
1660 }
1661  
1662 =head2 get_all_LD_values
1663  
1664 Arg [1] : (optional) Bio::EnsEMBL::Variation::Population $population
1665 Description : returns all LD values on this slice. This function will only work correctly if the variation
1666 database has been attached to the core database. If the argument is passed, will return the LD information
1667 in that population
1668 ReturnType : Bio::EnsEMBL::Variation::LDFeatureContainer
1669 Exceptions : none
1670 Caller : contigview, snpview
1671 Status : Stable
1672  
1673 =cut
1674  
1675 sub get_all_LD_values {
1676 my $self = shift;
1677 my $population = shift;
1678  
1679 my $ld_adaptor = $self->_get_VariationAdaptor('LDFeatureContainer');
1680 if($ld_adaptor) {
1681 return $ld_adaptor->fetch_by_Slice($self,$population);
1682 }
1683 return [];
1684 }
1685  
1686 =head2 _get_VariationFeatureAdaptor
1687  
1688 Shortcut method here because VariationFeature is an often requested
1689 adaptor type.
1690  
1691 =cut
1692  
1693 sub _get_VariationFeatureAdaptor {
1694 my ($self, $dbtype) = @_;
1695 return $self->_get_VariationAdaptor('VariationFeature', $dbtype);
1696 }
1697  
1698 =head2 _get_StructuralVariationFeatureAdaptor
1699  
1700 Shortcut method here because StructuralVariationFeature is an often requested
1701 adaptor type.
1702  
1703 =cut
1704  
1705 sub _get_StructuralVariationFeatureAdaptor {
1706 my ($self, $dbtype) = @_;
1707 return $self->_get_VariationAdaptor('StructuralVariationFeature', $dbtype);
1708 }
1709  
1710 =head2 _get_VariationAdaptor
1711  
1712 Arg [1] : String object_type to retrieve an adaptor for
1713 Arg [2] : String dbtype to search for the given adaptor in. Defaults to variation
1714 Description : Searches for the specified adaptor in the Registry and returns it. Otherwise
1715 it will return nothing if the adaptor was not found
1716 ReturnType : Bio::EnsEMBL::DBSQL::BaseAdaptor derrived instance (specific to variation)
1717 Exceptions : none
1718  
1719 =cut
1720  
1721 sub _get_VariationAdaptor {
1722 my ($self, $object_type, $dbtype) = @_;
1723 # very important to do this defaulting since we *have* to assume the variation
1724 # database is called 'variation'. Using the current group will not work because
1725 # that will be something like 'core' (most likely), 'ensembl' or 'vega'.
1726 $dbtype ||= 'variation';
1727  
1728 # Also we do not care about Registry->get_db() for variation DBs
1729 my $do_not_check_db = 1;
1730
1731 return $self->_get_Adaptor($object_type, $dbtype, $do_not_check_db);
1732 }
1733  
1734 =head2 _get_CoreAdaptor
1735  
1736 Arg [1] : String object_type to retrieve an adaptor for
1737 Arg [2] : String dbtype to search for the given adaptor in. Defaults to core
1738 Description : Searches for the specified adaptor in the Registry and returns it. Otherwise
1739 it will return nothing if the adaptor was not found
1740 ReturnType : Bio::EnsEMBL::DBSQL::BaseAdaptor derrived instance (specific to core-like dbs)
1741 Exceptions : none
1742  
1743 =cut
1744  
1745 sub _get_CoreAdaptor {
1746 my ($self, $object_type, $dbtype) = @_;
1747 #Simple pass through
1748 return $self->_get_Adaptor($object_type, $dbtype);
1749 }
1750  
1751 =head2 _get_Adaptor
1752  
1753 Arg [1] : String object_type to retrieve an adaptor for
1754 Arg [2] : String dbtype to search for the given adaptor in
1755 Arg [3] : Boolean Turn off the checking of Registry->get_db() for your
1756 adaptor.
1757 Description : Searches for the specified adaptor in the Registry and returns it. Otherwise
1758 it will return nothing if the adaptor was not found. We consult the
1759 "special" adaptors held by Bio::EnsEMBL::Registry::get_db() method and then
1760 fall back to the normal methods of finding an adaptor.
1761  
1762 This method will warn when adaptors are missing but will never through an
1763 exception. It is up to the calling code to decide how to handle the unavailablity
1764 of an adaptor.
1765 ReturnType : Bio::EnsEMBL::DBSQL::BaseAdaptor derrived instance. Otherwise it returns nothing
1766 Exceptions : none
1767  
1768 =cut
1769  
1770 sub _get_Adaptor {
1771 my ($self, $object_type, $dbtype, $do_not_check_db) = @_;
1772  
1773 if(!$object_type) {
1774 warning('Object type is a required parameter');
1775 return;
1776 }
1777  
1778 my $adaptor = $self->adaptor();
1779
1780 if(!$adaptor) {
1781 warning("Cannot get a ${object_type} adaptor without a SliceAdaptor attached to this instance of ".ref($self));
1782 return;
1783 }
1784  
1785 my $local_db = $adaptor->db();
1786 my $species = $local_db->species();
1787  
1788 #First we query for the DBAdaptor using get_db(). This is a deprecated method
1789 #call but "special" adaptors can be registered via this method. We must
1790 #consult here 1st to find the possible special adaptor
1791 if(!$do_not_check_db && $dbtype) {
1792 my $db = $registry->get_db($local_db, $dbtype);
1793 if($db) {
1794 # If we got a return then use this DBAdaptor's species name, group and the given object type.
1795 # Special adaptors can have different species names
1796 $adaptor = $registry->get_adaptor($db->species(), $db->group(), $object_type);
1797 }
1798 else {
1799 #Otherwise just use the current species, dbtype and object type
1800 $adaptor = $registry->get_adaptor($species, $dbtype, $object_type);
1801 }
1802 }
1803 # Otherwise our query group is the one attached to the current adaptor
1804 else {
1805 #If not set use the group attached to the local adaptor
1806 $dbtype ||= $local_db->group();
1807 $adaptor = $registry->get_adaptor($species, $dbtype, $object_type);
1808 }
1809 return $adaptor if $adaptor;
1810  
1811 warning("No adaptor could be found for the species ${species}, database type ${dbtype} and object type ${object_type}");
1812 return;
1813 }
1814  
1815 =head2 get_all_VariationFeatures
1816  
1817 Args [1] : (optional) ArrayRef $so_terms
1818 SequenceOntology terms to limit the fetch to
1819 Args [2] : (optional) boolean $without_children
1820 Do not query using the children of the given SO terms
1821 i.e. query using the given terms directly
1822 Args [3] : (optional) ArrayRef $included_so
1823 ArrayRef of SequenceOntology which should be queried for
1824 without children. This argument allows you to combine SO terms with children
1825 from argument 1 with extra non-child SO terms. e.g. you wish to query for
1826 all protein_altering_variant (specified in argument 1) variations which
1827 would be defined by child SO terms but also wanted stop_retained_variant linked variations
1828 defined by this argument
1829 Args [4] : (optional) string $dbtype
1830 The dbtype of variation to obtain (i.e. can be different from the "variation" type).
1831 This assumes that the extra db has been added to the DBAdaptor under this name (using the
1832 DBConnection::add_db_adaptor method).
1833 Description : Returns all germline variation features on this slice. This function will
1834 only work correctly if the variation database has been attached to the core
1835 database.
1836 If $so_terms is specified, only variation features with a consequence type
1837 that matches or is an ontological child of any of the supplied terms will
1838 be returned
1839 ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature
1840 Exceptions : none
1841 Caller : contigview, snpview
1842 Status : Stable
1843  
1844 =cut
1845  
1846 sub get_all_VariationFeatures{
1847 my ($self, $so_terms, $without_children, $included_so, $dbtype) = @_;
1848 if (my $vf_adaptor = $self->_get_VariationFeatureAdaptor($dbtype)) {
1849 return $vf_adaptor->fetch_all_by_Slice_SO_terms($self, $so_terms, $without_children, $included_so);
1850 }
1851 return [];
1852 }
1853  
1854 =head2 get_all_somatic_VariationFeatures
1855 Args [1] : (optional) ArrayRef $so_terms
1856 SequenceOntology terms to limit the fetch to
1857 Args [2] : (optional) boolean $without_children
1858 Do not query using the children of the given SO terms
1859 i.e. query using the given terms directly
1860 Args [3] : (optional) ArrayRef $included_so
1861 ArrayRef of SequenceOntology which should be queried for
1862 without children. This argument allows you to combine SO terms with children
1863 from argument 1 with extra non-child SO terms. e.g. you wish to query for
1864 all protein_altering_variant (specified in argument 1) variations which
1865 would be defined by child SO terms but also wanted stop_retained_variant linked variations
1866 defined by this argument
1867 Args [4] : (optional) string $dbtype
1868 The dbtype of variation to obtain (i.e. can be different from the "variation" type).
1869 This assumes that the extra db has been added to the DBAdaptor under this name (using the
1870 DBConnection::add_db_adaptor method).
1871 Description : Returns all somatic variation features on this slice. This function will only
1872 work correctly if the variation database has been attached to the core database.
1873 ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature
1874 Exceptions : none
1875 Status : Stable
1876  
1877 =cut
1878  
1879 sub get_all_somatic_VariationFeatures {
1880 my ($self, $so_terms, $without_children, $included_so, $dbtype) = @_;
1881 if (my $vf_adaptor = $self->_get_VariationFeatureAdaptor($dbtype)) {
1882 return $vf_adaptor->fetch_all_somatic_by_Slice_SO_terms($self, $so_terms, $without_children, $included_so);
1883 }
1884 return [];
1885 }
1886  
1887 =head2 get_all_somatic_VariationFeatures_by_source
1888 Arg [1] : string $source [optional]
1889 The name of the source to query for
1890 Arg [2] : string $dbtype [optional]
1891 The dbtype of variation to obtain (i.e. can be different from the "variation" type).
1892 This assumes that the extra db has been added to the DBAdaptor under this name (using the
1893 DBConnection::add_db_adaptor method).
1894 Description : Returns all somatic variation features, from a defined source name (e.g.'COSMIC'),
1895 on this slice. This function will only work correctly if the variation database
1896 has been attached to the core database.
1897 ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature
1898 Exceptions : none
1899 Status : Stable
1900  
1901 =cut
1902  
1903 sub get_all_somatic_VariationFeatures_by_source {
1904 my ($self, $source, $dbtype) = @_;
1905 my $constraint = (defined($source)) ? " s.name='$source' " : undef;
1906 if (my $vf_adaptor = $self->_get_VariationFeatureAdaptor($dbtype)) {
1907 return $vf_adaptor->fetch_all_somatic_by_Slice_constraint($self, $constraint);
1908 }
1909 return [];
1910 }
1911  
1912 =head2 get_all_somatic_VariationFeatures_with_phenotype
1913 Arg [1] : $variation_feature_source [optional]
1914 Arg [2] : $phenotype_source [optional]
1915 Arg [3] : $phenotype_name [optional]
1916 Arg [4] : string $dbtype [optional]
1917 The dbtype of variation to obtain (i.e. can be different from the "variation" type).
1918 This assumes that the extra db has been added to the DBAdaptor under this name (using the
1919 DBConnection::add_db_adaptor method).
1920 Description : returns all somatic variation features on this slice associated with a phenotype.
1921 (see get_all_VariationFeatures_with_phenotype for further documentation)
1922 ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature
1923 Exceptions : none
1924 Status : Stable
1925  
1926 =cut
1927  
1928 sub get_all_somatic_VariationFeatures_with_phenotype {
1929 my ($self, $source, $p_source, $phenotype, $dbtype) = @_;
1930 if (my $vf_adaptor = $self->_get_VariationFeatureAdaptor($dbtype)) {
1931 return $vf_adaptor->fetch_all_somatic_with_phenotype_by_Slice($self, $source, $p_source, $phenotype);
1932 }
1933 return [];
1934 }
1935  
1936  
1937 =head2 get_all_VariationFeatures_by_Population
1938 Arg [1] : Bio::EnsEMBL::Variation::Population
1939 Arg [2] : $minimum_frequency [optional]
1940 Arg [3] : string $dbtype [optional]
1941 The dbtype of variation to obtain (i.e. can be different from the "variation" type).
1942 This assumes that the extra db has been added to the DBAdaptor under this name (using the
1943 DBConnection::add_db_adaptor method).
1944 Example : $pop = $pop_adaptor->fetch_by_dbID(659);
1945 @vfs = @{$slice->get_all_VariationFeatures_by_Population($pop,$slice)};
1946 Description : Retrieves all variation features in a slice which are stored for
1947 a specified population. If $minimum_frequency is supplied, only
1948 variations with a minor allele frequency (MAF) greater than
1949 $minimum_frequency will be returned.
1950 Returntype : listref of Bio::EnsEMBL::Variation::VariationFeature
1951 Exceptions : throw on incorrect argument
1952 Caller : general
1953 Status : At Risk
1954  
1955 =cut
1956  
1957 sub get_all_VariationFeatures_by_Population {
1958 my ($self, $minimum_frequency, $dbtype) = @_;
1959 if (my $vf_adaptor = $self->_get_VariationFeatureAdaptor($dbtype)) {
1960 return $vf_adaptor->fetch_all_by_Slice_Population($self, $minimum_frequency);
1961 }
1962 return [];
1963 }
1964  
1965 =head2 get_all_Genes
1966  
1967 Arg [1] : (optional) string $logic_name
1968 The name of the analysis used to generate the genes to retrieve
1969 Arg [2] : (optional) string $dbtype
1970 The dbtype of genes to obtain. This assumes that the db has
1971 been added to the DBAdaptor under this name (using the
1972 DBConnection::add_db_adaptor method).
1973 Arg [3] : (optional) boolean $load_transcripts
1974 If set to true, transcripts will be loaded immediately rather
1975 than being lazy-loaded on request. This will result in a
1976 significant speed up if the Transcripts and Exons are going to
1977 be used (but a slow down if they are not).
1978 Arg [4] : (optional) string $source
1979 The source of the genes to retrieve.
1980 Arg [5] : (optional) string $biotype
1981 The biotype of the genes to retrieve.
1982 Example : @genes = @{$slice->get_all_Genes};
1983 Description: Retrieves all genes that overlap this slice, including those on
1984 the reverse strand.
1985 Returntype : listref of Bio::EnsEMBL::Genes
1986 Exceptions : none
1987 Caller : none
1988 Status : Stable
1989  
1990 =cut
1991  
1992 sub get_all_Genes{
1993 my ($self, $logic_name, $dbtype, $load_transcripts, $source, $biotype) = @_;
1994 if(my $adaptor = $self->_get_CoreAdaptor('Gene', $dbtype)) {
1995 return $adaptor->fetch_all_by_Slice( $self, $logic_name, $load_transcripts, $source, $biotype);
1996 }
1997 return [];
1998 }
1999  
2000 =head2 get_all_Genes_by_type
2001  
2002 Arg [1] : string $type
2003 The biotype of genes wanted.
2004 Arg [2] : (optional) string $logic_name
2005 Arg [3] : (optional) boolean $load_transcripts
2006 If set to true, transcripts will be loaded immediately rather
2007 than being lazy-loaded on request. This will result in a
2008 significant speed up if the Transcripts and Exons are going to
2009 be used (but a slow down if they are not).
2010 Example : @genes = @{$slice->get_all_Genes_by_type('protein_coding',
2011 'ensembl')};
2012 Description: Retrieves genes that overlap this slice of biotype $type.
2013 This is primarily used by the genebuilding code when several
2014 biotypes of genes are used.
2015  
2016 The logic name is the analysis of the genes that are retrieved.
2017 If not provided all genes will be retrieved instead. Both
2018 positive and negative strand Genes will be returned.
2019  
2020 Returntype : listref of Bio::EnsEMBL::Genes
2021 Exceptions : none
2022 Caller : genebuilder, general
2023 Status : Stable
2024  
2025 =cut
2026  
2027 sub get_all_Genes_by_type {
2028 my ($self, $type, $logic_name, $load_transcripts) = @_;
2029 return $self->get_all_Genes($logic_name, undef, $load_transcripts, undef, $type);
2030 }
2031  
2032  
2033 =head2 get_all_Genes_by_source
2034  
2035 Arg [1] : string source
2036 Arg [2] : (optional) boolean $load_transcripts
2037 If set to true, transcripts will be loaded immediately rather
2038 than being lazy-loaded on request. This will result in a
2039 significant speed up if the Transcripts and Exons are going to
2040 be used (but a slow down if they are not).
2041 Example : @genes = @{$slice->get_all_Genes_by_source('ensembl')};
2042 Description: Retrieves genes that overlap this slice of source $source.
2043 Strand of the Slice does not affect the result.
2044 Returntype : listref of Bio::EnsEMBL::Genes
2045 Exceptions : none
2046 Caller : general
2047 Status : Stable
2048  
2049 =cut
2050  
2051 sub get_all_Genes_by_source {
2052 my ($self, $source, $load_transcripts) = @_;
2053 return $self->get_all_Genes(undef, undef, $load_transcripts, $source);
2054 }
2055  
2056 =head2 get_all_Transcripts
2057  
2058 Arg [1] : (optional) boolean $load_exons
2059 If set to true exons will not be lazy-loaded but will instead
2060 be loaded right away. This is faster if the exons are
2061 actually going to be used right away.
2062 Arg [2] : (optional) string $logic_name
2063 the logic name of the type of features to obtain
2064 Arg [3] : (optional) string $db_type
2065 Example : @transcripts = @{$slice->get_all_Transcripts)_};
2066 Description: Gets all transcripts which overlap this slice. If you want to
2067 specify a particular analysis or type, then you are better off
2068 using get_all_Genes or get_all_Genes_by_type and iterating
2069 through the transcripts of each gene. Strand of the Slice is
2070 ignored.
2071 Returntype : reference to a list of Bio::EnsEMBL::Transcripts
2072 Exceptions : none
2073 Caller : general
2074 Status : Stable
2075  
2076 =cut
2077  
2078 sub get_all_Transcripts {
2079 my ($self, $load_exons, $logic_name, $dbtype, $source, $biotype) = @_;
2080 if(my $adaptor = $self->_get_CoreAdaptor('Transcript', $dbtype)) {
2081 return $adaptor->fetch_all_by_Slice($self, $load_exons, $logic_name, undef, $source, $biotype);
2082 }
2083 return [];
2084 }
2085  
2086 =head2 get_all_Transcripts_by_type
2087  
2088 Arg [1] : string $type
2089 The biotype of transcripts wanted.
2090 Arg [2] : (optional) string $logic_name
2091 Arg [3] : (optional) boolean $load_exons
2092 If set to true exons will not be lazy-loaded but will instead
2093 be loaded right away. This is faster if the exons are
2094 actually going to be used right away.
2095  
2096 Example : @transcripts = @{$slice->get_all_Transcripts_by_type('protein_coding',
2097 'ensembl')};
2098 Description: Retrieves transcripts that overlap this slice of biotype $type.
2099 This is primarily used by the genebuilding code when several
2100 biotypes of transcripts are used.
2101  
2102 The logic name is the analysis of the transcripts that are retrieved.
2103 If not provided all transcripts will be retrieved instead. Both
2104 positive and negative strand transcripts will be returned.
2105  
2106 Returntype : listref of Bio::EnsEMBL::Transcripts
2107 Exceptions : none
2108 Caller : genebuilder, general
2109 Status : Stable
2110  
2111 =cut
2112  
2113 sub get_all_Transcripts_by_type {
2114 my ($self, $biotype, $logic_name, $load_exons) = @_;
2115 return $self->get_all_Transcripts($load_exons, $logic_name, undef, undef, $biotype);
2116 }
2117  
2118  
2119 =head2 get_all_Transcripts_by_source
2120  
2121 Arg [1] : string source
2122 Arg [2] : (optional) boolean $load_exons
2123 If set to true exons will not be lazy-loaded but will instead
2124 be loaded right away. This is faster if the exons are
2125 actually going to be used right away.
2126 Example : @transcripts = @{$slice->get_all_Transcripts_by_source('ensembl')};
2127 Description: Retrieves transcripts that overlap this slice of source $source.
2128 Strand of the Slice does not affect the result.
2129 Returntype : listref of Bio::EnsEMBL::Transcripts
2130 Exceptions : none
2131 Caller : general
2132 Status : Stable
2133  
2134 =cut
2135  
2136 sub get_all_Transcripts_by_source {
2137 my ($self, $source, $load_exons) = @_;
2138 return $self->get_all_Transcripts($load_exons, undef, undef, $source);
2139  
2140 }
2141  
2142  
2143 =head2 get_all_Exons
2144  
2145 Arg [1] : none
2146 Example : @exons = @{$slice->get_all_Exons};
2147 Description: Gets all exons which overlap this slice. Note that these exons
2148 will not be associated with any transcripts, so this may not
2149 be terribly useful.
2150 Returntype : reference to a list of Bio::EnsEMBL::Exons
2151 Exceptions : none
2152 Caller : general
2153 Status : Stable
2154  
2155 =cut
2156  
2157 sub get_all_Exons {
2158 my $self = shift;
2159 if(!$self->adaptor()) {
2160 warning('Cannot get Exons without attached adaptor');
2161 return [];
2162 }
2163 return $self->adaptor->db->get_ExonAdaptor->fetch_all_by_Slice($self);
2164 }
2165  
2166 =head2 get_all_KaryotypeBands
2167  
2168 Arg [1] : none
2169 Example : @kary_bands = @{$slice->get_all_KaryotypeBands};
2170 Description: Retrieves the karyotype bands which this slice overlaps.
2171 Returntype : listref oif Bio::EnsEMBL::KaryotypeBands
2172 Exceptions : none
2173 Caller : general, contigview
2174 Status : Stable
2175  
2176 =cut
2177  
2178 sub get_all_KaryotypeBands {
2179 my ($self) = @_;
2180 if (my $adaptor = $self->_get_CoreAdaptor('KaryotypeBand')) {
2181 return $adaptor->fetch_all_by_Slice($self);
2182 }
2183 return [];
2184 }
2185  
2186 =head2 get_repeatmasked_seq
2187  
2188 Arg [1] : listref of strings $logic_names (optional)
2189 Arg [2] : int $soft_masking_enable (optional)
2190 Arg [3] : hash reference $not_default_masking_cases (optional, default is {})
2191 The values are 0 or 1 for hard and soft masking respectively
2192 The keys of the hash should be of 2 forms
2193 "repeat_class_" . $repeat_consensus->repeat_class,
2194 e.g. "repeat_class_SINE/MIR"
2195 "repeat_name_" . $repeat_consensus->name
2196 e.g. "repeat_name_MIR"
2197 depending on which base you want to apply the not default
2198 masking either the repeat_class or repeat_name. Both can be
2199 specified in the same hash at the same time, but in that case,
2200 repeat_name setting has priority over repeat_class. For example,
2201 you may have hard masking as default, and you may want soft
2202 masking of all repeat_class SINE/MIR, but repeat_name AluSp
2203 (which are also from repeat_class SINE/MIR).
2204 Your hash will be something like {"repeat_class_SINE/MIR" => 1,
2205 "repeat_name_AluSp" => 0}
2206 Example : $rm_slice = $slice->get_repeatmasked_seq();
2207 $softrm_slice = $slice->get_repeatmasked_seq(['RepeatMask'],1);
2208 Description: Returns Bio::EnsEMBL::Slice that can be used to create repeat
2209 masked sequence instead of the regular sequence.
2210 Sequence returned by this new slice will have repeat regions
2211 hardmasked by default (sequence replaced by N) or
2212 or soft-masked when arg[2] = 1 (sequence in lowercase)
2213 Will only work with database connection to get repeat features.
2214 Returntype : Bio::EnsEMBL::RepeatMaskedSlice
2215 Exceptions : none
2216 Caller : general
2217 Status : Stable
2218  
2219 =cut
2220  
2221 sub get_repeatmasked_seq {
2222 my ($self,$logic_names,$soft_mask,$not_default_masking_cases) = @_;
2223  
2224 return Bio::EnsEMBL::RepeatMaskedSlice->new
2225 (-START => $self->{'start'},
2226 -END => $self->{'end'},
2227 -STRAND => $self->{'strand'},
2228 -ADAPTOR => $self->adaptor(),
2229 -SEQ => $self->{'seq'},
2230 -SEQ_REGION_NAME => $self->{'seq_region_name'},
2231 -SEQ_REGION_LENGTH => $self->{'seq_region_length'},
2232 -COORD_SYSTEM => $self->{'coord_system'},
2233 -REPEAT_MASK => $logic_names,
2234 -SOFT_MASK => $soft_mask,
2235 -NOT_DEFAULT_MASKING_CASES => $not_default_masking_cases);
2236 }
2237  
2238  
2239  
2240 =head2 _mask_features
2241  
2242 Arg [1] : reference to a string $dnaref
2243 Arg [2] : array_ref $repeats
2244 reference to a list Bio::EnsEMBL::RepeatFeature
2245 give the list of coordinates to replace with N or with
2246 lower case
2247 Arg [3] : int $soft_masking_enable (optional)
2248 Arg [4] : hash reference $not_default_masking_cases (optional, default is {})
2249 The values are 0 or 1 for hard and soft masking respectively
2250 The keys of the hash should be of 2 forms
2251 "repeat_class_" . $repeat_consensus->repeat_class,
2252 e.g. "repeat_class_SINE/MIR"
2253 "repeat_name_" . $repeat_consensus->name
2254 e.g. "repeat_name_MIR"
2255 depending on which base you want to apply the not default masking either
2256 the repeat_class or repeat_name. Both can be specified in the same hash
2257 at the same time, but in that case, repeat_name setting has priority over
2258 repeat_class. For example, you may have hard masking as default, and
2259 you may want soft masking of all repeat_class SINE/MIR,
2260 but repeat_name AluSp (which are also from repeat_class SINE/MIR).
2261 Your hash will be something like {"repeat_class_SINE/MIR" => 1,
2262 "repeat_name_AluSp" => 0}
2263 Example : none
2264 Description: replaces string positions described in the RepeatFeatures
2265 with Ns (default setting), or with the lower case equivalent
2266 (soft masking). The reference to a dna string which is passed
2267 is changed in place.
2268 Returntype : none
2269 Exceptions : none
2270 Caller : seq
2271 Status : Stable
2272  
2273 =cut
2274  
2275 sub _mask_features {
2276 my ($self,$dnaref,$repeats,$soft_mask,$not_default_masking_cases) = @_;
2277  
2278 $soft_mask = 0 unless (defined $soft_mask);
2279 $not_default_masking_cases = {} unless (defined $not_default_masking_cases);
2280  
2281 # explicit CORE::length call, to avoid any confusion with the Slice
2282 # length method
2283 my $dnalen = CORE::length($$dnaref);
2284  
2285 REP:foreach my $old_f (@{$repeats}) {
2286 my $f = $old_f->transfer( $self );
2287 my $start = $f->start;
2288 my $end = $f->end;
2289 my $length = ($end - $start) + 1;
2290  
2291 # check if we get repeat completely outside of expected slice range
2292 if ($end < 1 || $start > $dnalen) {
2293 # warning("Unexpected: Repeat completely outside slice coordinates.");
2294 next REP;
2295 }
2296  
2297 # repeat partly outside slice range, so correct
2298 # the repeat start and length to the slice size if needed
2299 if ($start < 1) {
2300 $start = 1;
2301 $length = ($end - $start) + 1;
2302 }
2303  
2304 # repeat partly outside slice range, so correct
2305 # the repeat end and length to the slice size if needed
2306 if ($end > $dnalen) {
2307 $end = $dnalen;
2308 $length = ($end - $start) + 1;
2309 }
2310  
2311 $start--;
2312  
2313 my $padstr;
2314 # if we decide to define masking on the base of the repeat_type, we'll need
2315 # to add the following, and the other commented line few lines below.
2316 my $rc_class;
2317 my $rc_name;
2318  
2319 if ($f->isa('Bio::EnsEMBL::RepeatFeature')) {
2320 $rc_class = "repeat_class_" . $f->repeat_consensus->repeat_class;
2321 $rc_name = "repeat_name_" . $f->repeat_consensus->name;
2322 }
2323  
2324 my $masking_type;
2325 $masking_type = $not_default_masking_cases->{$rc_class} if (defined $not_default_masking_cases->{$rc_class});
2326 $masking_type = $not_default_masking_cases->{$rc_name} if (defined $not_default_masking_cases->{$rc_name});
2327  
2328 $masking_type = $soft_mask unless (defined $masking_type);
2329  
2330 if ($masking_type) {
2331 $padstr = lc substr ($$dnaref,$start,$length);
2332 } else {
2333 $padstr = 'N' x $length;
2334 }
2335 substr ($$dnaref,$start,$length) = $padstr;
2336 }
2337 }
2338  
2339  
2340 =head2 get_all_SearchFeatures
2341  
2342 Arg [1] : scalar $ticket_ids
2343 Example : $slice->get_all_SearchFeatures('BLA_KpUwwWi5gY');
2344 Description: Retrieves all search features for stored blast
2345 results for the ticket that overlap this slice
2346 Returntype : listref of Bio::EnsEMBL::SeqFeatures
2347 Exceptions : none
2348 Caller : general (webby!)
2349 Status : Stable
2350  
2351 =cut
2352  
2353 sub get_all_SearchFeatures {
2354 my $self = shift;
2355 my $ticket = shift;
2356 local $_;
2357 unless($ticket) {
2358 throw("ticket argument is required");
2359 }
2360  
2361 if(!$self->adaptor()) {
2362 warning("Cannot get SearchFeatures without an attached adaptor");
2363 return [];
2364 }
2365  
2366 my $sfa = $self->adaptor()->db()->get_db_adaptor('blast');
2367  
2368 my $offset = $self->start-1;
2369  
2370 my $features = $sfa ? $sfa->get_all_SearchFeatures($ticket, $self->seq_region_name, $self->start, $self->end) : [];
2371  
2372 foreach( @$features ) {
2373 $_->start( $_->start - $offset );
2374 $_->end( $_->end - $offset );
2375 };
2376 return $features;
2377  
2378 }
2379  
2380 =head2 get_all_AssemblyExceptionFeatures
2381  
2382 Example : $slice->get_all_AssemblyExceptionFeatures();
2383 Description: Retrieves all misc features which overlap this slice. If
2384 a set code is provided only features which are members of
2385 the requested set are returned.
2386 Returntype : listref of Bio::EnsEMBL::AssemblyExceptionFeatures
2387 Exceptions : none
2388 Caller : general
2389 Status : Stable
2390  
2391 =cut
2392  
2393 sub get_all_AssemblyExceptionFeatures {
2394 my ($self) = @_;
2395 if(my $adaptor = $self->_get_CoreAdaptor('AssemblyExceptionFeature')) {
2396 return $adaptor->fetch_all_by_Slice($self);
2397 }
2398 return [];
2399 }
2400  
2401  
2402  
2403 =head2 get_all_MiscFeatures
2404  
2405 Arg [1] : string $set (optional)
2406 Arg [2] : string $database (optional)
2407 Example : $slice->get_all_MiscFeatures('cloneset');
2408 Description: Retrieves all misc features which overlap this slice. If
2409 a set code is provided only features which are members of
2410 the requested set are returned.
2411 Returntype : listref of Bio::EnsEMBL::MiscFeatures
2412 Exceptions : none
2413 Caller : general
2414 Status : Stable
2415  
2416 =cut
2417  
2418 sub get_all_MiscFeatures {
2419 my ($self, $misc_set, $dbtype) = @_;
2420 if(my $adaptor = $self->_get_CoreAdaptor('MiscFeature', $dbtype)) {
2421 if($misc_set) {
2422 return $adaptor->fetch_all_by_Slice_and_set_code($self,$misc_set);
2423 }
2424 return $adaptor->fetch_all_by_Slice($self);
2425 }
2426 return [];
2427 }
2428  
2429 =head2 get_all_MarkerFeatures
2430  
2431 Arg [1] : (optional) string logic_name
2432 The logic name of the marker features to retrieve
2433 Arg [2] : (optional) int $priority
2434 Lower (exclusive) priority bound of the markers to retrieve
2435 Arg [3] : (optional) int $map_weight
2436 Upper (exclusive) priority bound of the markers to retrieve
2437 Example : my @markers = @{$slice->get_all_MarkerFeatures(undef,50, 2)};
2438 Description: Retrieves all markers which lie on this slice fulfilling the
2439 specified map_weight and priority parameters (if supplied).
2440 Returntype : reference to a list of Bio::EnsEMBL::MarkerFeatures
2441 Exceptions : none
2442 Caller : contigview, general
2443 Status : Stable
2444  
2445 =cut
2446  
2447 sub get_all_MarkerFeatures {
2448 my ($self, $logic_name, $priority, $map_weight) = @_;
2449 if(my $adaptor = $self->_get_CoreAdaptor('MarkerFeature')) {
2450 return $adaptor->fetch_all_by_Slice_and_priority($self, $priority, $map_weight, $logic_name);
2451 }
2452 return [];
2453 }
2454  
2455  
2456 =head2 get_MarkerFeatures_by_Name
2457  
2458 Arg [1] : string marker Name
2459 The name (synonym) of the marker feature(s) to retrieve
2460 Example : my @markers = @{$slice->get_MarkerFeatures_by_Name('z1705')};
2461 Description: Retrieves all markers with this ID
2462 Returntype : reference to a list of Bio::EnsEMBL::MarkerFeatures
2463 Exceptions : none
2464 Caller : contigview, general
2465 Status : Stable
2466  
2467 =cut
2468  
2469 sub get_MarkerFeatures_by_Name {
2470 my ($self, $name) = @_;
2471 if(my $adaptor = $self->_get_CoreAdaptor('MarkerFeature')) {
2472 return $adaptor->fetch_all_by_Slice_and_MarkerName($self, $name);
2473 }
2474 return [];
2475 }
2476  
2477  
2478 =head2 get_all_compara_DnaAlignFeatures
2479  
2480 Arg [1] : string $qy_species
2481 The name of the species to retrieve similarity features from
2482 Arg [2] : string $qy_assembly
2483 The name of the assembly to retrieve similarity features from
2484 Arg [3] : string $type
2485 The type of the alignment to retrieve similarity features from
2486 Arg [4] : <optional> compara dbadptor to use.
2487 Example : $fs = $slc->get_all_compara_DnaAlignFeatures('Mus musculus',
2488 'MGSC3',
2489 'WGA');
2490 Description: Retrieves a list of DNA-DNA Alignments to the species specified
2491 by the $qy_species argument.
2492 The compara database must be attached to the core database
2493 for this call to work correctly. As well the compara database
2494 must have the core dbadaptors for both this species, and the
2495 query species added to function correctly.
2496 Returntype : reference to a list of Bio::EnsEMBL::DnaDnaAlignFeatures
2497 Exceptions : warning if compara database is not available
2498 Caller : contigview
2499 Status : Stable
2500  
2501 =cut
2502  
2503 sub get_all_compara_DnaAlignFeatures {
2504 my ($self, $qy_species, $qy_assembly, $alignment_type, $compara_db) = @_;
2505  
2506 if(!$self->adaptor()) {
2507 warning("Cannot retrieve DnaAlignFeatures without attached adaptor");
2508 return [];
2509 }
2510  
2511 unless($qy_species && $alignment_type # && $qy_assembly
2512 ) {
2513 throw("Query species and assembly and alignmemt type arguments are required");
2514 }
2515  
2516 if(!defined($compara_db)){
2517 $compara_db = Bio::EnsEMBL::Registry->get_DBAdaptor("compara", "compara");
2518 }
2519 unless($compara_db) {
2520 warning("Compara database must be attached to core database or passed ".
2521 "as an argument to " .
2522 "retrieve compara information");
2523 return [];
2524 }
2525  
2526 my $dafa = $compara_db->get_DnaAlignFeatureAdaptor;
2527 return $dafa->fetch_all_by_Slice($self, $qy_species, $qy_assembly, $alignment_type);
2528 }
2529  
2530 =head2 get_all_compara_Syntenies
2531  
2532 Arg [1] : string $query_species e.g. "Mus_musculus" or "Mus musculus"
2533 Arg [2] : string $method_link_type, default is "SYNTENY"
2534 Arg [3] : <optional> compara dbadaptor to use.
2535 Description: gets all the compara syntenyies for a specfic species
2536 Returns : arrayref of Bio::EnsEMBL::Compara::SyntenyRegion
2537 Status : Stable
2538  
2539 =cut
2540  
2541 sub get_all_compara_Syntenies {
2542 my ($self, $qy_species, $method_link_type, $compara_db) = @_;
2543  
2544 if(!$self->adaptor()) {
2545 warning("Cannot retrieve features without attached adaptor");
2546 return [];
2547 }
2548  
2549 unless($qy_species) {
2550 throw("Query species and assembly arguments are required");
2551 }
2552  
2553 unless (defined $method_link_type) {
2554 $method_link_type = "SYNTENY";
2555 }
2556  
2557 if(!defined($compara_db)){
2558 $compara_db = Bio::EnsEMBL::Registry->get_DBAdaptor("compara", "compara");
2559 }
2560 unless($compara_db) {
2561 warning("Compara database must be attached to core database or passed ".
2562 "as an argument to " .
2563 "retrieve compara information");
2564 return [];
2565 }
2566 my $gdba = $compara_db->get_GenomeDBAdaptor();
2567 my $mlssa = $compara_db->get_MethodLinkSpeciesSetAdaptor();
2568 my $sra = $compara_db->get_SyntenyRegionAdaptor();
2569  
2570 my $this_gdb = $gdba->fetch_by_core_DBAdaptor($self->adaptor()->db());
2571 my $query_gdb = $gdba->fetch_by_registry_name($qy_species);
2572 my $mlss;
2573 if($this_gdb eq $query_gdb) {
2574 $mlss = $mlssa->fetch_by_method_link_type_GenomeDBs($method_link_type, [$this_gdb]);
2575 } else {
2576 $mlss = $mlssa->fetch_by_method_link_type_GenomeDBs($method_link_type, [$this_gdb, $query_gdb]);
2577 }
2578  
2579 return $sra->fetch_all_by_MethodLinkSpeciesSet_Slice($mlss, $self);
2580 }
2581  
2582 =head2 get_all_Haplotypes
2583  
2584 Arg [1] : (optional) boolean $lite_flag
2585 if true lightweight haplotype objects are used
2586 Example : @haplotypes = $slice->get_all_Haplotypes;
2587 Description: Retrieves all of the haplotypes on this slice. Only works
2588 if the haplotype adaptor has been attached to the core adaptor
2589 via $dba->add_db_adaptor('haplotype', $hdba);
2590 Returntype : listref of Bio::EnsEMBL::External::Haplotype::Haplotypes
2591 Exceptions : warning is Haplotype database is not available
2592 Caller : contigview, general
2593 Status : Stable
2594  
2595 =cut
2596  
2597 sub get_all_Haplotypes {
2598 my($self, $lite_flag) = @_;
2599  
2600 if(!$self->adaptor()) {
2601 warning("Cannot retrieve features without attached adaptor");
2602 return [];
2603 }
2604  
2605 my $haplo_db = $self->adaptor->db->get_db_adaptor('haplotype');
2606  
2607 unless($haplo_db) {
2608 warning("Haplotype database must be attached to core database to " .
2609 "retrieve haplotype information" );
2610 return [];
2611 }
2612  
2613 my $haplo_adaptor = $haplo_db->get_HaplotypeAdaptor;
2614  
2615 my $haplotypes = $haplo_adaptor->fetch_all_by_Slice($self, $lite_flag);
2616  
2617 return $haplotypes;
2618 }
2619  
2620  
2621 sub get_all_DASFactories {
2622 my $self = shift;
2623 return [ $self->adaptor()->db()->_each_DASFeatureFactory ];
2624 }
2625  
2626 sub get_all_DASFeatures_dsn {
2627 my ($self, $source_type, $dsn) = @_;
2628  
2629 if(!$self->adaptor()) {
2630 warning("Cannot retrieve features without attached adaptor");
2631 return [];
2632 }
2633 my @X = grep { $_->adaptor->dsn eq $dsn } $self->adaptor()->db()->_each_DASFeatureFactory;
2634  
2635 return [ $X[0]->fetch_all_Features( $self, $source_type ) ];
2636 }
2637  
2638 =head2 get_all_DAS_Features
2639  
2640 Arg [1] : none
2641 Example : $features = $slice->get_all_DASFeatures;
2642 Description: Retrieves a hash reference to a hash of DAS feature
2643 sets, keyed by the DNS, NOTE the values of this hash
2644 are an anonymous array containing:
2645 (1) a pointer to an array of features;
2646 (2) a pointer to the DAS stylesheet
2647 Returntype : hashref of Bio::SeqFeatures
2648 Exceptions : ?
2649 Caller : webcode
2650 Status : Stable
2651  
2652 =cut
2653 sub get_all_DAS_Features{
2654 my ($self) = @_;
2655  
2656 $self->{_das_features} ||= {}; # Cache
2657 $self->{_das_styles} ||= {}; # Cache
2658 $self->{_das_segments} ||= {}; # Cache
2659 my %das_features;
2660 my %das_styles;
2661 my %das_segments;
2662 my $slice = $self;
2663  
2664 foreach my $dasfact( @{$self->get_all_DASFactories} ){
2665 my $dsn = $dasfact->adaptor->dsn;
2666 my $name = $dasfact->adaptor->name;
2667 # my $type = $dasfact->adaptor->type;
2668 my $url = $dasfact->adaptor->url;
2669  
2670 my ($type) = $dasfact->adaptor->mapping;
2671 if (ref $type eq 'ARRAY') {
2672 $type = shift @$type;
2673 }
2674 $type ||= $dasfact->adaptor->type;
2675 # Construct a cache key : SOURCE_URL/TYPE
2676 # Need the type to handle sources that serve multiple types of features
2677  
2678 my $key = join('/', $name, $type);
2679 if( $self->{_das_features}->{$key} ){ # Use cached
2680 $das_features{$name} = $self->{_das_features}->{$key};
2681 $das_styles{$name} = $self->{_das_styles}->{$key};
2682 $das_segments{$name} = $self->{_das_segments}->{$key};
2683 } else { # Get fresh data
2684 my ($featref, $styleref, $segref) = $dasfact->fetch_all_Features( $slice, $type );
2685 $self->{_das_features}->{$key} = $featref;
2686 $self->{_das_styles}->{$key} = $styleref;
2687 $self->{_das_segments}->{$key} = $segref;
2688 $das_features{$name} = $featref;
2689 $das_styles{$name} = $styleref;
2690 $das_segments{$name} = $segref;
2691 }
2692 }
2693  
2694 return (\%das_features, \%das_styles, \%das_segments);
2695 }
2696  
2697 sub get_all_DASFeatures{
2698 my ($self, $source_type) = @_;
2699  
2700  
2701 if(!$self->adaptor()) {
2702 warning("Cannot retrieve features without attached adaptor");
2703 return [];
2704 }
2705  
2706 my %genomic_features = map { ( $_->adaptor->dsn => [ $_->fetch_all_Features($self, $source_type) ] ) } $self->adaptor()->db()->_each_DASFeatureFactory;
2707 return \%genomic_features;
2708  
2709 }
2710  
2711 sub old_get_all_DASFeatures{
2712 my ($self,@args) = @_;
2713  
2714 if(!$self->adaptor()) {
2715 warning("Cannot retrieve features without attached adaptor");
2716 return [];
2717 }
2718  
2719 my %genomic_features =
2720 map { ( $_->adaptor->dsn => [ $_->fetch_all_by_Slice($self) ] ) }
2721 $self->adaptor()->db()->_each_DASFeatureFactory;
2722 return \%genomic_features;
2723  
2724 }
2725  
2726  
2727 =head2 get_all_ExternalFeatures
2728  
2729 Arg [1] : (optional) string $track_name
2730 If specified only features from ExternalFeatureAdaptors with
2731 the track name $track_name are retrieved.
2732 If not set, all features from every ExternalFeatureAdaptor are
2733 retrieved.
2734 Example : @x_features = @{$slice->get_all_ExternalFeatures}
2735 Description: Retrieves features on this slice from external feature adaptors
2736 Returntype : listref of Bio::SeqFeatureI implementing objects in slice
2737 coordinates
2738 Exceptions : none
2739 Caller : general
2740 Status : Stable
2741  
2742 =cut
2743  
2744 sub get_all_ExternalFeatures {
2745 my ($self, $track_name) = @_;
2746  
2747 if(!$self->adaptor()) {
2748 warning("Cannot retrieve features without attached adaptor");
2749 return [];
2750 }
2751  
2752 my $features = [];
2753  
2754 my $xfa_hash = $self->adaptor->db->get_ExternalFeatureAdaptors;
2755 my @xf_adaptors = ();
2756  
2757 if($track_name) {
2758 #use a specific adaptor
2759 if(exists $xfa_hash->{$track_name}) {
2760 push @xf_adaptors, $xfa_hash->{$track_name};
2761 }
2762 } else {
2763 #use all of the adaptors
2764 push @xf_adaptors, values %$xfa_hash;
2765 }
2766  
2767  
2768 foreach my $xfa (@xf_adaptors) {
2769 push @$features, @{$xfa->fetch_all_by_Slice($self)};
2770 }
2771  
2772 return $features;
2773 }
2774  
2775  
2776 =head2 get_all_DitagFeatures
2777  
2778 Arg [1] : (optional) string ditag type
2779 Arg [1] : (optional) string logic_name
2780 Example : @dna_dna_align_feats = @{$slice->get_all_DitagFeatures};
2781 Description: Retrieves the DitagFeatures of a specific type which overlap
2782 this slice. If type is not defined, all features are retrieved.
2783 Strandedness of the Slice is ignored.
2784 Returntype : listref of Bio::EnsEMBL::DitagFeatures
2785 Exceptions : warning if slice does not have attached adaptor
2786 Caller : general
2787 Status : Stable
2788  
2789 =cut
2790  
2791 sub get_all_DitagFeatures {
2792 my ($self, $type, $logic_name) = @_;
2793 if(my $adaptor = $self->_get_CoreAdaptor('DitagFeature')) {
2794 return $adaptor->fetch_all_by_Slice($self, $type, $logic_name);
2795 }
2796 return [];
2797 }
2798  
2799 # GENERIC FEATURES (See DBAdaptor.pm)
2800  
2801 =head2 get_generic_features
2802  
2803 Arg [1] : (optional) List of names of generic feature types to return.
2804 If no feature names are given, all generic features are
2805 returned.
2806 Example : my %features = %{$slice->get_generic_features()};
2807 Description: Gets generic features via the generic feature adaptors that
2808 have been added via DBAdaptor->add_GenricFeatureAdaptor (if
2809 any)
2810 Returntype : Hash of named features.
2811 Exceptions : none
2812 Caller : none
2813 Status : Stable
2814  
2815 =cut
2816  
2817 sub get_generic_features {
2818  
2819 my ($self, @names) = @_;
2820  
2821 if(!$self->adaptor()) {
2822 warning('Cannot retrieve features without attached adaptor');
2823 return [];
2824 }
2825  
2826 my $db = $self->adaptor()->db();
2827  
2828 my %features = (); # this will hold the results
2829  
2830 # get the adaptors for each feature
2831 my %adaptors = %{$db->get_GenericFeatureAdaptors(@names)};
2832  
2833 foreach my $adaptor_name (keys(%adaptors)) {
2834  
2835 my $adaptor_obj = $adaptors{$adaptor_name};
2836 # get the features and add them to the hash
2837 my $features_ref = $adaptor_obj->fetch_all_by_Slice($self);
2838 # add each feature to the hash to be returned
2839 foreach my $feature (@$features_ref) {
2840 $features{$adaptor_name} = $feature;
2841 }
2842 }
2843  
2844 return \%features;
2845  
2846 }
2847  
2848 =head2 project_to_slice
2849  
2850 Arg [1] : Slice to project to.
2851 Example : my $chr_projection = $clone_slice->project_to_slice($chrom_slice);
2852 foreach my $segment ( @$chr_projection ){
2853 $chr_slice = $segment->to_Slice();
2854 print $clone_slice->seq_region_name(). ':'. $segment->from_start(). '-'.
2855 $segment->from_end(). ' -> '.$chr_slice->seq_region_name(). ':'. $chr_slice->start().
2856 '-'.$chr_slice->end().
2857 $chr_slice->strand(). " length: ".($chr_slice->end()-$chr_slice->start()+1). "\n";
2858 }
2859 Description: Projection of slice to another specific slice. Needed for where we have multiple mappings
2860 and we want to state which one to project to.
2861 Returntype : list reference of Bio::EnsEMBL::ProjectionSegment objects which
2862 can also be used as [$start,$end,$slice] triplets.
2863 Exceptions : none
2864 Caller : none
2865 Status : At Risk
2866  
2867 =cut
2868  
2869 sub project_to_slice {
2870 my $self = shift;
2871 my $to_slice = shift;
2872  
2873 throw('Slice argument is required') if(!$to_slice);
2874  
2875 my $slice_adaptor = $self->adaptor();
2876  
2877 if(!$slice_adaptor) {
2878 warning("Cannot project without attached adaptor.");
2879 return [];
2880 }
2881  
2882  
2883 my $mapper_aptr = $slice_adaptor->db->get_AssemblyMapperAdaptor();
2884  
2885 my $cs = $to_slice->coord_system();
2886 my $slice_cs = $self->coord_system();
2887 my $to_slice_id = $to_slice->get_seq_region_id;
2888  
2889 my @projection;
2890  
2891 # decompose this slice into its symlinked components.
2892 # this allows us to handle haplotypes and PARs
2893 my $normal_slice_proj =
2894 $slice_adaptor->fetch_normalized_slice_projection($self);
2895 foreach my $segment (@$normal_slice_proj) {
2896 my $normal_slice = $segment->[2];
2897  
2898 $slice_cs = $normal_slice->coord_system();
2899  
2900 my $asma = $self->adaptor->db->get_AssemblyMapperAdaptor();
2901 my $asm_mapper = $asma->fetch_by_CoordSystems($slice_cs, $cs);
2902 next unless defined $asm_mapper;
2903
2904 # perform the mapping between this slice and the requested system
2905 my @coords = $asm_mapper->map($normal_slice->seq_region_name(),
2906 $normal_slice->start(),
2907 $normal_slice->end(),
2908 $normal_slice->strand(),
2909 $slice_cs, undef, $to_slice, 1);
2910  
2911 #construct a projection from the mapping results and return it
2912 foreach my $coord (@coords) {
2913 my $original = $coord->{original};
2914 my $mapped = $coord->{mapped};
2915  
2916 # skip gaps
2917 next unless $mapped->isa('Bio::EnsEMBL::Mapper::Coordinate');
2918  
2919 my $mapped_start = $mapped->start();
2920 my $mapped_end = $mapped->end();
2921 my ($current_start, $current_end);
2922 if ($self->strand == 1) {
2923 $current_start = $original->start() - $self->start + 1;
2924 $current_end = $original->end() - $self->start + 1;
2925 } else {
2926 $current_start = $self->end() - $original->end + 1;
2927 $current_end = $self->end - $original->start + 1;
2928 }
2929
2930 # for multiple mappings only get the correct one
2931 next unless $mapped->id == $to_slice_id;
2932
2933 my $coord_cs = $mapped->coord_system();
2934  
2935 # If the normalised projection just ended up mapping to the
2936 # same coordinate system we were already in then we should just
2937 # return the original region. This can happen for example, if we
2938 # were on a PAR region on Y which refered to X and a projection to
2939 # 'toplevel' was requested.
2940 # if($coord_cs->equals($slice_cs)) {
2941 # # trim off regions which are not defined
2942 # return $self->_constrain_to_region();
2943 # }
2944  
2945 # create slices for the mapped-to coord system
2946 my $slice = $slice_adaptor->fetch_by_seq_region_id($mapped->id(),
2947 $mapped_start,
2948 $mapped_end,
2949 $mapped->strand());
2950
2951 push @projection, bless([$current_start, $current_end, $slice],
2952 "Bio::EnsEMBL::ProjectionSegment");
2953 }
2954 }
2955  
2956  
2957 # delete the cache as we may want to map to different set next time and old
2958 # results will be cached.
2959 $mapper_aptr->delete_cache;
2960  
2961 return \@projection;
2962 }
2963  
2964  
2965 =head2 get_all_synonyms
2966  
2967 Args [1] : String external_db_name The name of the database to retrieve
2968 the synonym for
2969 Args [2] : (optional) Integer external_db_version Optionally restrict
2970 results from external_db_name to a specific version of
2971 the the specified external database
2972 Example : my @alternative_names = @{$slice->get_all_synonyms()};
2973 @alternative_names = @{$slice->get_all_synonyms('EMBL')};
2974 Description: get a list of alternative names for this slice
2975 Returntype : reference to list of SeqRegionSynonym objects.
2976 Exception : none
2977 Caller : general
2978 Status : At Risk
2979  
2980 =cut
2981  
2982 sub get_all_synonyms {
2983 my ($self, $external_db_name, $external_db_version) = @_;
2984  
2985 if ( !defined( $self->{'synonym'} ) ) {
2986 my $adap = $self->adaptor->db->get_SeqRegionSynonymAdaptor();
2987 $self->{'synonym'} =
2988 $adap->get_synonyms( $self->get_seq_region_id($self) );
2989 }
2990
2991 if(! $external_db_name) {
2992 return $self->{'synonym'};
2993 }
2994 my @args = ($external_db_version) ?
2995 ($external_db_name, $external_db_version) :
2996 ($external_db_name, undef, 1);
2997 my $external_db_id = $self->adaptor->db()->get_DBEntryAdaptor()->get_external_db_id(@args);
2998 if(!$external_db_id) {
2999 my $extra = ($external_db_version) ? "and version $external_db_version " : q{};
3000 throw "The external database $external_db_name ${extra}did not result in a valid identifier";
3001 }
3002  
3003 return [ grep { $_->external_db_id() == $external_db_id } @{$self->{synonym}} ];
3004 }
3005  
3006 =head2 add_synonym
3007  
3008 Args[0] : synonym.
3009 Example : $slice->add_synonym("alt_name");
3010 Description: add an alternative name for this slice
3011 Returntype : none
3012 Exception : none
3013 Caller : general
3014 Status : At Risk
3015  
3016 =cut
3017  
3018 sub add_synonym{
3019 my $self = shift;
3020 my $syn = shift;
3021 my $external_db_id = shift;
3022
3023 my $adap = $self->adaptor->db->get_SeqRegionSynonymAdaptor();
3024 if ( !defined( $self->{'synonym'} ) ) {
3025 $self->{'synonym'} = $self->get_all_synonyms();
3026 }
3027 my $new_syn = Bio::EnsEMBL::SeqRegionSynonym->new( #-adaptor => $adap,
3028 -synonym => $syn,
3029 -external_db_id => $external_db_id,
3030 -seq_region_id => $self->get_seq_region_id($self));
3031  
3032 push (@{$self->{'synonym'}}, $new_syn);
3033  
3034 return;
3035 }
3036  
3037  
3038 # package variable to minimize duplication
3039 my %region_so_mapping = (
3040 'chromosome' => 'SO:0000340',
3041 'supercontig' => 'SO:0000148',
3042 'scaffold' => 'SO:0000148',
3043 'contig' => 'SO:0000149'
3044 );
3045  
3046 =head2 feature_so_acc
3047  
3048 Example : $slice_so_acc = $slice->feature_so_acc;
3049 Description : This method returns a string containing the SO accession number of the slice, based on the coordinate system name.
3050 Returns : string (Sequence Ontology accession number)
3051  
3052 =cut
3053  
3054 sub feature_so_acc {
3055 my $self = shift;
3056  
3057 # return the region SO acc, or Slice acc
3058 return $region_so_mapping{$self->coord_system_name} // 'SO:0000001';
3059 }
3060  
3061 =head2 feature_so_term
3062  
3063 Description: This method returns a string containing the SO term of the slice, based on the coordinate system name
3064 Define constant SEQUENCE_ONTOLOGY in classes that require it, or override it for multiple possible values for a class.
3065 Returntype : String (Sequence Ontology term)
3066 Exceptions : Thrown if caller SEQUENCE_ONTOLOGY is undefined and is not a Bio::EnsEMBL::Slice
3067  
3068 =cut
3069  
3070 sub feature_so_term {
3071 my ($self) = shift;
3072  
3073 return defined($region_so_mapping{$self->coord_system_name}) ?
3074 $self->coord_system_name :
3075 'region';
3076 }
3077  
3078 =head2 summary_as_hash
3079  
3080 Example : $slice_summary = $slice->summary_as_hash();
3081 Description : Retrieves a textual summary of this slice.
3082 Returns : hashref of descriptive strings
3083 =cut
3084  
3085 sub summary_as_hash {
3086 my $self = shift;
3087 my %summary;
3088 my @aliases = map { $_->name } @{$self->slice->get_all_synonyms()};
3089  
3090 $summary{'seq_region_name'} = $self->seq_region_name;
3091 $summary{'id'} = $self->seq_region_name;
3092 $summary{'start'} = $self->start;
3093 $summary{'end'} = $self->end;
3094 $summary{'strand'} = 0;
3095 $summary{'source'} = $self->source || $self->coord_system->version;
3096 $summary{'Alias'} = \@aliases if scalar(@aliases);
3097 $summary{'Is_circular'} = $self->is_circular ? "true" : undef;
3098 return \%summary;
3099 }
3100  
3101  
3102 sub slice {
3103 my $self = shift;
3104 return $self;
3105 }
3106  
3107 #
3108 # Bioperl Bio::PrimarySeqI methods:
3109 #
3110  
3111 =head2 id
3112  
3113 Description: Included for Bio::PrimarySeqI interface compliance (0.7)
3114  
3115 =cut
3116  
3117 sub id { name(@_); }
3118  
3119  
3120 =head2 display_id
3121  
3122 Description: Included for Bio::PrimarySeqI interface compliance (1.2)
3123  
3124 =cut
3125  
3126 sub display_id { name(@_); }
3127  
3128  
3129 =head2 primary_id
3130  
3131 Description: Included for Bio::PrimarySeqI interface compliance (1.2)
3132  
3133 =cut
3134  
3135 sub primary_id { name(@_); }
3136  
3137  
3138 =head2 desc
3139  
3140 Description: Included for Bio::PrimarySeqI interface compliance (1.2)
3141  
3142 =cut
3143  
3144 sub desc{ return $_[0]->coord_system->name().' '.$_[0]->seq_region_name(); }
3145  
3146  
3147 =head2 moltype
3148  
3149 Description: Included for Bio::PrimarySeqI interface compliance (0.7)
3150  
3151 =cut
3152  
3153 sub moltype { return 'dna'; }
3154  
3155 =head2 alphabet
3156  
3157 Description: Included for Bio::PrimarySeqI interface compliance (1.2)
3158  
3159 =cut
3160  
3161 sub alphabet { return 'dna'; }
3162  
3163  
3164 =head2 accession_number
3165  
3166 Description: Included for Bio::PrimarySeqI interface compliance (1.2)
3167  
3168 =cut
3169  
3170 sub accession_number { name(@_); }
3171  
3172 1;