Archive Ensembl HomeArchive Ensembl Home
PaddedSlice.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::PaddedSlice
00024 
00025 =head1 DESCRIPTION
00026 
00027 Used when dumping Slices which represet a portion of the sequence region
00028 they map to e.g. the first section of human Y. The code will return N
00029 as sequence if an attempt is made to retrieve sequence not covered by the
00030 Slice given. This makes the code very memory efficient if sequence dumping
00031 is carried out using C<subseq()> calls.
00032 
00033 =head1 METHODS
00034 
00035 =cut
00036 
00037 package Bio::EnsEMBL::PaddedSlice;
00038 
00039 use Bio::EnsEMBL::Utils::Argument qw/rearrange/;
00040 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref assert_strand/;
00041 use base qw/Bio::EnsEMBL::Utils::Proxy/;
00042 
00043 =head2 new()
00044 
00045   Arg [SLICE] : The Slice to proxy  
00046   Example     : my $newobj = Bio::EnsEMBL::PaddedSlice->new($myobj);
00047   Description : Provides a new instance of a padded slice
00048   Returntype  : Bio::EnsEMBL::PaddedSlice
00049   Exceptions  : None 
00050   Caller      : public
00051   Status      : -
00052 
00053 =cut
00054 
00055 sub new {
00056   my ($class, @args) = @_;
00057   my ($slice) = rearrange([qw/slice/], @args);
00058   return $class->SUPER::new($slice);
00059 }
00060 
00061 =head2 start()
00062 
00063   Example     : $slice->start();
00064   Description : Always returns 1 since all padded slices start at 1
00065   Returntype  : Int
00066   Exceptions  : None 
00067   Caller      : public
00068   Status      : -
00069 
00070 =cut
00071 
00072 sub start {
00073   my ($self) = @_;
00074   return 1;
00075 }
00076 
00077 =head2 end()
00078   
00079   Example     : $slice->end();
00080   Description : Always returns the backing slice sequence region length
00081   Returntype  : Int
00082   Exceptions  : None 
00083   Caller      : public
00084   Status      : -
00085 
00086 =cut
00087 
00088 sub end {
00089   my ($self) = @_;
00090   return $self->seq_region_length();
00091 }
00092 
00093 =head2 length()
00094   
00095   Example     : $slice->length();
00096   Description : Delegates to C<end()>
00097   Returntype  : Int
00098   Exceptions  : None 
00099   Caller      : public
00100   Status      : -
00101 
00102 =cut
00103 
00104 sub length {
00105   my ($self) = @_;
00106   return $self->end();
00107 }
00108 
00109 =head2 seq()
00110   
00111   Example     : my $seq = $slice->seq()
00112   Description : Returns the entire sequence of the backing slice but padded
00113                 with N's at the beginning and the end of the slice where
00114                 applicable
00115   Returntype  : Scalar string
00116   Exceptions  : None 
00117   Caller      : public
00118   Status      : -
00119 
00120 =cut
00121 
00122 sub seq {
00123   my ($self) = @_;
00124   my $parent_slice = $self->__proxy();
00125   my $pad_start = 'N' x ( $parent_slice->start() - 1 );
00126   my $pad_end   = 'N' x ( $parent_slice->seq_region_length() - $parent_slice->end() );
00127   my $seq = $parent_slice->seq();
00128   return $pad_start . $seq . $pad_end;
00129 }
00130 
00131 =head2 subseq()
00132   
00133   Arg [1]     : Int; start position of the subslice
00134   Arg [2]     : Int; end position of the subslice
00135   Arg [3]     : Int; strand of the subslice  
00136   Example     : my $subseq = $slice->subseq(1, 1_000_000);
00137   Description : Returns a portion of the sequence padded with N's if required 
00138   Returntype  : Scalar string
00139   Exceptions  : None 
00140   Caller      : public
00141   Status      : -
00142 
00143 =cut
00144 
00145 sub subseq {
00146   my ( $self, $start, $end, $strand ) = @_;
00147 
00148   if ( $end+1 < $start ) {
00149     throw("End coord + 1 is less then start coord");
00150   }
00151   
00152   return '' if( $start == $end + 1);
00153   
00154   $strand = 1 unless(defined $strand);
00155   assert_strand($strand, 'strand');
00156   
00157   my $parent_slice = $self->__proxy();
00158     
00159   #Coords relative to the SeqRegion i.e. huge
00160   my $parent_start = $parent_slice->start();
00161   my $parent_end = $parent_slice->end();
00162   
00163   #Return if we were upstream of overlap
00164   if($start < $parent_start && $end < $parent_start) {
00165     return N x (( $end - $start )+1);
00166   }
00167   #Return if we were downstream of overlap
00168   if($start > $parent_end && $end > $parent_end) {
00169     return N x (( $end - $start )+1);
00170   }
00171   
00172   my $prefix  = '';
00173   my $suffix = '';
00174   my $subslice_start = ($start - $parent_start)+1;
00175   my $subslice_end = ($end - $parent_start) + 1;
00176   if($start < $parent_start) {
00177     $prefix = N x ($parent_start - $start);
00178     $subslice_start = 1;
00179   }
00180   if($end > $parent_end) {
00181     $suffix = N x ($end - $parent_end);
00182     $subslice_end = (($parent_end - $parent_start)+1);
00183   }
00184   
00185   my $subseq = $parent_slice->subseq($subslice_start, $subslice_end, $strand);
00186   
00187   return $prefix . $subseq . $suffix;
00188 }
00189 
00190 =head2 subseq()
00191   
00192   Arg [1]     : Int; start position of the subslice
00193   Arg [2]     : Int; end position of the subslice
00194   Arg [3]     : Int; strand of the subslice  
00195   Example     : my $subseq = $slice->subseq(1, 1_000_000);
00196   Description : Returns a portion of the sequence padded with N's if required 
00197   Returntype  : Scalar string
00198   Exceptions  : None 
00199   Caller      : public
00200   Status      : -
00201 
00202 =cut
00203 
00204 sub sub_Slice {
00205   die "Unsupported";
00206 }
00207 
00208 
00209 =head2 __resolver()
00210 
00211   Description : Delegates all non-overriden actions onto the backing slice 
00212   Returntype  : CodeRef
00213   Exceptions  : None 
00214   Caller      : public
00215   Status      : -
00216 
00217 =cut
00218 
00219 sub __resolver {
00220   my ($self, $package_name, $method) = @_;
00221   return sub {
00222     my ($local_self, @args) = @_;
00223     return $local_self->__proxy()->$method(@args);
00224   };
00225 }
00226 
00227 
00228 1;