| 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::PaddedSlice | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | Used when dumping Slices which represet a portion of the sequence region | ||
| 38 | they map to e.g. the first section of human Y. The code will return N | ||
| 39 | as sequence if an attempt is made to retrieve sequence not covered by the | ||
| 40 | Slice given. This makes the code very memory efficient if sequence dumping | ||
| 41 | is carried out using C<subseq()> calls. | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | package Bio::EnsEMBL::PaddedSlice; | ||
| 48 | $Bio::EnsEMBL::PaddedSlice::VERSION = '114.0.0'; | ||
| 49 | use strict; | ||
| 50 | use warnings; | ||
| 51 | |||
| 52 | use Bio::EnsEMBL::Utils::Argument qw/rearrange/; | ||
| 53 | use Bio::EnsEMBL::Utils::Scalar qw/assert_ref assert_strand/; | ||
| 54 | use base qw/Bio::EnsEMBL::Utils::Proxy/; | ||
| 55 | |||
| 56 | =head2 new() | ||
| 57 | |||
| 58 | Arg [SLICE] : The Slice to proxy | ||
| 59 | Example : my $newobj = Bio::EnsEMBL::PaddedSlice->new($myobj); | ||
| 60 | Description : Provides a new instance of a padded slice | ||
| 61 | Returntype : Bio::EnsEMBL::PaddedSlice | ||
| 62 | Exceptions : None | ||
| 63 | Caller : public | ||
| 64 | Status : - | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ($class, @args) = @_; | ||
| 70 | my ($slice) = rearrange([qw/slice/], @args); | ||
| 71 | return $class->SUPER::new($slice); | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 start() | ||
| 75 | |||
| 76 | Example : $slice->start(); | ||
| 77 | Description : Always returns 1 since all padded slices start at 1 | ||
| 78 | Returntype : Int | ||
| 79 | Exceptions : None | ||
| 80 | Caller : public | ||
| 81 | Status : - | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub start { | ||
| 86 | my ($self) = @_; | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 end() | ||
| 91 | |||
| 92 | Example : $slice->end(); | ||
| 93 | Description : Always returns the backing slice sequence region length | ||
| 94 | Returntype : Int | ||
| 95 | Exceptions : None | ||
| 96 | Caller : public | ||
| 97 | Status : - | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub end { | ||
| 102 | my ($self) = @_; | ||
| 103 | return $self->seq_region_length(); | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 length() | ||
| 107 | |||
| 108 | Example : $slice->length(); | ||
| 109 | Description : Delegates to C<end()> | ||
| 110 | Returntype : Int | ||
| 111 | Exceptions : None | ||
| 112 | Caller : public | ||
| 113 | Status : - | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub length { | ||
| 118 | my ($self) = @_; | ||
| 119 | return $self->end(); | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 seq() | ||
| 123 | |||
| 124 | Example : my $seq = $slice->seq() | ||
| 125 | Description : Returns the entire sequence of the backing slice but padded | ||
| 126 | with N's at the beginning and the end of the slice where | ||
| 127 | applicable | ||
| 128 | Returntype : Scalar string | ||
| 129 | Exceptions : None | ||
| 130 | Caller : public | ||
| 131 | Status : - | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub seq { | ||
| 136 | my ($self) = @_; | ||
| 137 | my $parent_slice = $self->__proxy(); | ||
| 138 | my $pad_start = 'N' x ( $parent_slice->start() - 1 ); | ||
| 139 | my $pad_end = 'N' x ( $parent_slice->seq_region_length() - $parent_slice->end() ); | ||
| 140 | my $seq = $parent_slice->seq(); | ||
| 141 | return $pad_start . $seq . $pad_end; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 subseq() | ||
| 145 | |||
| 146 | Arg [1] : Int; start position of the subslice | ||
| 147 | Arg [2] : Int; end position of the subslice | ||
| 148 | Arg [3] : Int; strand of the subslice | ||
| 149 | Example : my $subseq = $slice->subseq(1, 1_000_000); | ||
| 150 | Description : Returns a portion of the sequence padded with N's if required | ||
| 151 | Returntype : Scalar string | ||
| 152 | Exceptions : None | ||
| 153 | Caller : public | ||
| 154 | Status : - | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub subseq { | ||
| 159 | my ( $self, $start, $end, $strand ) = @_; | ||
| 160 | |||
| 161 | if ( $end+1 < $start ) { | ||
| 162 | throw("End coord + 1 is less than start coord"); | ||
| 163 | } | ||
| 164 | |||
| 165 | return '' if( $start == $end + 1); | ||
| 166 | |||
| 167 | $strand = 1 unless(defined $strand); | ||
| 168 | assert_strand($strand, 'strand'); | ||
| 169 | |||
| 170 | my $parent_slice = $self->__proxy(); | ||
| 171 | |||
| 172 | #Coords relative to the SeqRegion i.e. huge | ||
| 173 | my $parent_start = $parent_slice->start(); | ||
| 174 | my $parent_end = $parent_slice->end(); | ||
| 175 | |||
| 176 | #Return if we were upstream of overlap | ||
| 177 | if($start < $parent_start && $end < $parent_start) { | ||
| 178 | return 'N' x (( $end - $start )+1); | ||
| 179 | } | ||
| 180 | #Return if we were downstream of overlap | ||
| 181 | if($start > $parent_end && $end > $parent_end) { | ||
| 182 | return 'N' x (( $end - $start )+1); | ||
| 183 | } | ||
| 184 | |||
| 185 | my $prefix = ''; | ||
| 186 | my $suffix = ''; | ||
| 187 | my $subslice_start = ($start - $parent_start)+1; | ||
| 188 | my $subslice_end = ($end - $parent_start) + 1; | ||
| 189 | if($start < $parent_start) { | ||
| 190 | $prefix = 'N' x ($parent_start - $start); | ||
| 191 | $subslice_start = 1; | ||
| 192 | } | ||
| 193 | if($end > $parent_end) { | ||
| 194 | $suffix = 'N' x ($end - $parent_end); | ||
| 195 | $subslice_end = (($parent_end - $parent_start)+1); | ||
| 196 | } | ||
| 197 | |||
| 198 | my $subseq = $parent_slice->subseq($subslice_start, $subslice_end, $strand); | ||
| 199 | |||
| 200 | return $prefix . $subseq . $suffix; | ||
| 201 | } | ||
| 202 | |||
| 203 | =head2 subseq() | ||
| 204 | |||
| 205 | Arg [1] : Int; start position of the subslice | ||
| 206 | Arg [2] : Int; end position of the subslice | ||
| 207 | Arg [3] : Int; strand of the subslice | ||
| 208 | Example : my $subseq = $slice->subseq(1, 1_000_000); | ||
| 209 | Description : Returns a portion of the sequence padded with N's if required | ||
| 210 | Returntype : Scalar string | ||
| 211 | Exceptions : None | ||
| 212 | Caller : public | ||
| 213 | Status : - | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub sub_Slice { | ||
| 218 | die "Unsupported"; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | =head2 __resolver() | ||
| 223 | |||
| 224 | Description : Delegates all non-overriden actions onto the backing slice | ||
| 225 | Returntype : CodeRef | ||
| 226 | Exceptions : None | ||
| 227 | Caller : public | ||
| 228 | Status : - | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub __resolver { | ||
| 233 | my ($self, $package_name, $method) = @_; | ||
| 234 | return sub { | ||
| 235 | my ($local_self, @args) = @_; | ||
| 236 | return $local_self->__proxy()->$method(@args); | ||
| 237 | }; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | 1; |