EnsEMBL/DBSQL/BaseSequenceAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 140 CC 8 SCAR 26.6
CC
8
Cov
0%
expand_Slice
42.0
_fetch_seq
12.0
fetch_by_Slice_start_end_strand
2.0
CRAP 14.3
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 =head1 CONTACT
19  
20 Please email comments or questions to the public Ensembl
21 developers list at <dev@ensembl.org>.
22  
23 Questions may also be sent to the Ensembl help desk at
24 <helpdesk@ensembl.org>.
25  
26 =cut
27  
28 =head1 NAME
29  
30 Bio::EnsEMBL::DBSQL::BaseSequenceAdaptor
31  
32 =head1 DESCRIPTION
33  
34 The BaseSequenceAdaptor is responsible for the conversion of calls from
35 C<fetch_by_Slice_start_end_strand()> for Sequence data into requests for a
36 backing data store. In Ensembl these are the B<seqlevel> sequence region
37 records held in the MySQL database.
38  
39 The base adaptor also provides sequence caching based on normalisation
40 technique similar to the UCSC and BAM binning indexes. The code works
41 by right-shifting the requested start and end by a seq chunk power
42 (by default 18 approx. 250,000bp) and then left-shifting by the same
43 value. This means any value within a given window will always result in
44 the same value. Please see the worked examples below:
45  
46 # Equation
47 p=position
48 o=seq chunk power
49 offset=( (p-1)>>o ) << o
50
51 # Using real values
52 p=1340001
53 o=18
54 right_shifted = (1340001-1) >> 18 == 5
55 offset = 5 << 18 == 1310720
56  
57 To control the size of the cache and sequences stored you can provide
58 the seq chunk power and the number of sequences cached.
59  
60 =cut
61  
62 package Bio::EnsEMBL::DBSQL::BaseSequenceAdaptor;
63 $Bio::EnsEMBL::DBSQL::BaseSequenceAdaptor::VERSION = '114.0.0_50'; # TRIAL
64 $Bio::EnsEMBL::DBSQL::BaseSequenceAdaptor::VERSION = '114.0.050';
65 use strict;
66 use warnings;
67  
68 use Bio::EnsEMBL::Utils::Cache;
69 use Bio::EnsEMBL::Utils::Exception qw(throw);
70  
71 our $SEQ_CHUNK_PWR = 18; # 2^18 = approx. 250KB
72 our $SEQ_CACHE_SIZE = 5;
73  
74 =head2 fetch_by_Slice_start_end_strand
75  
76 Arg [1] : Bio::EnsEMBL::Slice slice
77 The slice from which you want the sequence
78 Arg [2] : Integer; $strand (optional)
79 The start base pair relative to the start of the slice. Negative
80 values or values greater than the length of the slice are fine.
81 default = 1
82 Arg [3] : (optional) int endBasePair
83 The end base pair relative to the start of the slice. Negative
84 values or values greater than the length of the slice are fine,
85 but the end must be greater than or equal to the start
86 count from 1
87 default = the length of the slice
88 Arg [4] : Integer; $strand (optional)
89 Strand of DNA to fetch
90 Returntype : StringRef (DNA requested)
91 Description: Performs the fetching of DNA based upon a Slice. All fetches
92 should use this method and no-other.
93
94 Implementing classes are responsible for converting the
95 given Slice and values into something which can be processed by
96 the underlying storage engine. Implementing class are also
97 responsible for the reverse complementing of sequence.
98 Exceptions : Thrown if not redefined
99  
100 =cut
101  
102 sub fetch_by_Slice_start_end_strand {
103 my ( $self, $slice, $start, $end, $strand ) = @_;
104 throw "fetch_by_Slice_start_end_strand() must be implemented";
105 }
106  
107 =head2 can_access_Slice
108  
109 Description : Returns a boolean indiciating if the adaptor understands
110 the given Slice.
111 Returntype : Boolean; if true you can get sequence for the given Slice
112 Exceptions : Thrown if not redefined
113  
114 =cut
115  
116 sub can_access_Slice {
117 my ($self, $slice) = @_;
118 throw "can_access_Slice() must be implemented";
119 }
120  
121 =head2 expand_Slice
122  
123 Arg [1] : Bio::EnsEMBL::Slice slice
124 The slice from which you want the sequence
125 Arg [2] : Integer; $strand (optional)
126 The start base pair relative to the start of the slice. Negative
127 values or values greater than the length of the slice are fine.
128 default = 1
129 Arg [3] : (optional) int endBasePair
130 The end base pair relative to the start of the slice. Negative
131 values or values greater than the length of the slice are fine,
132 but the end must be greater than or equal to the start
133 count from 1
134 default = the length of the slice
135 Arg [4] : Integer; $strand (optional)
136 Strand of DNA to fetch
137 Returntype : Bio::EnsEMBL::Slice
138 Description : Creates a new Slice which represents the requested region. Provides
139 logic applicable to all SliceAdaptor instance
140 Exceptions : Thrown if the Slice is circular (we currently do not support this as generic logic)
141  
142 =cut
143  
144 sub expand_Slice {
145 my ($self, $slice, $start, $end, $strand) = @_;
146
147 $start = 1 if ! defined $start;
148 $end = ($slice->end() - $slice->start()) + 1 if ! defined $end;
149 $strand = 1 if ! defined $strand;
150  
151 my $right_expand = $end - $slice->length(); #negative is fine
152 my $left_expand = 1 - $start; #negative is fine
153  
154 my $new_slice = $slice;
155 if ( $right_expand || $left_expand ) {
156 $new_slice =
157 $slice->strand > 0
158 ? $slice->expand( $left_expand, $right_expand )
159 : $slice->expand( $right_expand, $left_expand );
160 }
161
162 return $new_slice;
163 }
164  
165 =head2 new
166  
167 Arg [1] : Int $chunk_power; sets the size of each element of
168 the sequence cache. Defaults to 18 which gives
169 block sizes of ~250Kb (it is actually 2^18)
170 Arg [2] : Int $cache_size; size of the cache. Defaults to 5 meaning
171 a cache of 1Mb if you use default values
172 Example : my $sa = $db_adaptor->get_SequenceAdaptor();
173 Description: Constructor. Calls superclass constructor and initialises
174 internal cache structure.
175 Returntype : Bio::EnsEMBL::DBSQL::SequenceAdaptor
176 Exceptions : none
177 Caller : DBAdaptor::get_SequenceAdaptor
178 Status : Stable
179  
180 =cut
181  
182 sub new {
183 my ($class, $chunk_power, $cache_size) = @_;
184 $class = ref($class) || $class;
185 my $self = bless({}, $class);
186 $self->_init_seq_instance($chunk_power, $cache_size);
187 return $self;
188 }
189  
190 sub _init_seq_instance {
191 my ($self, $chunk_power, $cache_size) = @_;
192 $chunk_power ||= $SEQ_CHUNK_PWR;
193 $cache_size ||= $SEQ_CACHE_SIZE;
194
195 # use a LRU cache to limit the size
196 tie my %cache, 'Bio::EnsEMBL::Utils::Cache', $cache_size, {};
197  
198 $self->{cache_size} = $cache_size;
199 $self->{chunk_power} = $chunk_power;
200 $self->{seq_cache_max} = ((2 ** $chunk_power) * $cache_size);
201 $self->{seq_cache} = \%cache;
202 return;
203 }
204  
205 =head2 clear_cache
206  
207 Example : $sa->clear_cache();
208 Description : Removes all entries from the associcated sequence cache
209 Returntype : None
210 Exceptions : None
211  
212 =cut
213  
214 sub clear_cache {
215 my ($self) = @_;
216 %{$self->{seq_cache}} = ();
217 return;
218 }
219  
220 sub chunk_power {
221 my ($self) = @_;
222 return $self->{chunk_power};
223 }
224  
225 sub cache_size {
226 my ($self) = @_;
227 return $self->{cache_size};
228 }
229  
230 sub seq_cache_max {
231 my ($self) = @_;
232 return $self->{seq_cache_max};
233 }
234  
235 =head2 _fetch_raw_seq
236  
237 Arg [1] : String $id
238 The identifier of the sequence to fetch.
239 Arg [2] : Integer $start
240 Where to start fetching sequence from
241 Arg [2] : Integer $length
242 Total length of seuqence to fetch
243 Description : Performs the fetch of DNA from the backing storage
244 engine and provides it to the _fetch_seq() method
245 for optional caching.
246 Returntype : ScalarRef of DNA fetched. All bases should be uppercased
247 Exceptions : Thrown if the method is not reimplemented
248  
249 =cut
250 sub _fetch_raw_seq {
251 my ($self, $id, $start, $length) = @_;
252 throw "Need to implement _fetch_raw_seq(). Code must return a ScalarRef";
253 }
254  
255 =head2 _fetch_seq
256  
257 Arg [1] : String $id
258 The identifier of the sequence to fetch.
259 Arg [2] : Integer $start
260 Where to start fetching sequence from
261 Arg [2] : Integer $length
262 Total length of seuqence to fetch
263 Description : If the requested region is smaller than our maximum length
264 cachable region we will see if the cache already contains
265 this chunk. If not we will request the region from C<_fetch_raw_seq()>
266 and cache it. If the region requested is larger than
267 the maximum cacheable sequence length we pass the request
268 onto C<_fetch_raw_seq()> with no caching layer.
269
270 This module is also responsible for the conversion of
271 requested regions into normalised region reuqests based
272 on C<chunk_power>.
273 Returntype : ScalarRef of DNA fetched. All bases should be uppercased
274 Exceptions : Thrown when C<_fetch_raw_seq()> is not re-implemented
275  
276 =cut
277  
278 sub _fetch_seq {
279 my ($self, $id, $start, $length) = @_;
280 my $cache = $self->{'seq_cache'};
281 my $cache_size = $self->cache_size();
282 my $seq_chunk_pwr = $self->chunk_power();
283 my $seq_cache_max = $self->seq_cache_max();
284  
285 my $seq_ref;
286  
287 # If the length of the region is less than the max size of the
288 # cache then we can use the cache. The region is converted into
289 # a min to max range of bins we have to query to get all the
290 # required sequence. Of these bins the cache may have all of them,
291 # none of them or a combination of the two states
292 if($length < $seq_cache_max) {
293 my $chunk_min = ($start-1) >> $seq_chunk_pwr;
294 my $chunk_max = ($start + $length - 1) >> $seq_chunk_pwr;
295  
296 # piece together sequence from cached component parts
297 my $entire_seq = q{};
298 for(my $i = $chunk_min; $i <= $chunk_max; $i++) {
299 my $key = "${id}:${i}";
300 #If it exists within the cache then add to the string. We will trim
301 #down to the requested region later on
302 my $cached_seq_ref = $cache->{$key};
303 if($cached_seq_ref) {
304 $entire_seq .= ${$cached_seq_ref};
305 }
306 #Otherwise it is uncached so fetch, concat and store in the cache
307 else {
308 my $min = ($i << $seq_chunk_pwr) + 1;
309 my $length = 1 << $seq_chunk_pwr;
310 my $tmp_seq_ref = $self->_fetch_raw_seq($id, $min, $length);
311 $entire_seq .= ${$tmp_seq_ref};
312 $cache->{$key} = $tmp_seq_ref;
313 }
314 }
315  
316 # now return only the requested portion of the entire sequence
317 my $min = ( $chunk_min << $seq_chunk_pwr ) + 1;
318 my $seq = substr( $entire_seq, $start - $min, $length );
319 $seq_ref = \$seq;
320 }
321 else {
322 # If it was too big then just ask it from the backing store. There's
323 # no use in caching this
324 $seq_ref = $self->_fetch_raw_seq($id, $start, $length);
325 }
326
327 return $seq_ref;
328 }
329  
330 1;