EnsEMBL/DBSQL/DensityFeatureAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 294 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 73 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 516 CC 61 SCAR 60.5
CC
61
Cov
0%
_objs_from_sth
930.0
fetch_all_by_Slice
272.0
fetch_all_by_Slice_constraint
90.0
CRAP 424.1
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $dfa = $database_adaptor->get_DensityFeatureAdaptor();
38  
39 my $interpolate = 1;
40 my $blocks_wanted = 50;
41  
42 @dense_feats = @{
43 $dfa->fetch_all_by_Slice( $slice, 'SNPDensity', $blocks_wanted,
44 $interpolate );
45 }
46  
47 =head1 DESCRIPTION
48  
49 Density Feature Adaptor - An adaptor responsible for the creation of density
50 features from the database.
51  
52 =head1 METHODS
53  
54 =cut
55  
56 package Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor;
57 $Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL
58 $Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor::VERSION = '114.0.050';
59 use vars qw(@ISA);
60 use strict;
61  
62  
63 use POSIX;
64 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
65 use Bio::EnsEMBL::Utils::Cache;
66 use Bio::EnsEMBL::DensityFeature;
67 use Bio::EnsEMBL::DensityFeatureSet;
68 use Bio::EnsEMBL::DensityType;
69 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
70  
71 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
72  
73 our $DENSITY_FEATURE_CACHE_SIZE = 20;
74  
75  
76 =head2 new
77  
78 Arg [1] : list of args @args
79 Superclass constructor arguments
80 Example : none
81 Description: Constructor which just initializes internal cache structures
82 Returntype : Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor
83 Exceptions : none
84 Caller : implementing subclass constructors
85 Status : Stable
86  
87 =cut
88  
89 sub new {
90 my $caller = shift;
91  
92 my $class = ref($caller) || $caller;
93  
94 my $self = $class->SUPER::new(@_);
95  
96 #initialize an LRU cache
97 my %cache;
98 tie(%cache, 'Bio::EnsEMBL::Utils::Cache', $DENSITY_FEATURE_CACHE_SIZE);
99 $self->{'_density_feature_cache'} = \%cache;
100  
101 return $self;
102 }
103  
104 =head2 fetch_all_by_Slice
105  
106 Arg [1] : Bio::EnsEMBL::Slice $slice - The slice representing the region
107 to retrieve density features from.
108 Arg [2] : string $logic_name - The logic name of the density features to
109 retrieve.
110 Arg [3] : int $num_blocks (optional; default = 50) - The number of
111 features that are desired. The ratio between the size of these
112 features and the size of the features in the database will be
113 used to determine which database features will be used.
114 Arg [4] : boolean $interpolate (optional; default = 0) - A flag indicating
115 whether the features in the database should be interpolated to
116 fit them to the requested number of features. If true the
117 features will be interpolated to provide $num_blocks features.
118 This will not guarantee that exactly $num_blocks features are
119 returned due to rounding etc. but it will be close.
120 Arg [5] : float $max_ratio - The maximum ratio between the size of the
121 requested features (as determined by $num_blocks) and the actual
122 size of the features in the database. If this value is exceeded
123 then an empty list will be returned. This can be used to
124 prevent excessive interpolation of the database values.
125 Example : #interpolate:
126 $feats = $dfa->fetch_all_by_Slice($slice,'SNPDensity', 10, 1);
127 #do not interpoloate, get what is in the database:
128 $feats = $dfa->fetch_all_by_Slice($slice,'SNPDensity', 50);
129 #interpolate, but not too much
130 $feats = $dfa->fetch_all_by_Slice($slice,'SNPDensity',50,1,5.0);
131 Description: Retrieves a set of density features which overlap the region
132 of this slice. Density features are a discrete representation
133 of a continuous value along a sequence, such as a density or
134 percent coverage. Density Features may be stored in chunks of
135 different sizes in the database, and interpolated to fit the
136 desired size for the requested region. For example the database
137 may store a single density value for each 1KB and also for each
138 1MB. When fetching for an entire chromosome the 1MB density
139 chunks will be used if the requested number of blocks is not
140 very high.
141 Note that features which have been interpolated are not stored
142 in the database and as such will have no dbID or adaptor set.
143 Returntype : Bio::EnsEMBL::DensityFeature
144 Exceptions : warning on invalid $num_blocks argument
145 warning if no features with logic_name $logic_name exist
146 warning if density_type table has invalid block_size value
147 Caller : general
148 Status : Stable
149  
150 =cut
151  
152 sub fetch_all_by_Slice {
153 my ($self, $slice, $logic_name, $num_blocks, $interpolate, $max_ratio) = @_;
154  
155 if(defined($num_blocks) && $num_blocks < 1) {
156 warning("Invalid number of density blocks [$num_blocks] requested.\n" .
157 "Returning empty list.");
158 return [];
159 }
160  
161 $num_blocks ||= 50;
162 my $length = $slice->length();
163  
164 my $wanted_block_size = POSIX::ceil($length/$num_blocks);
165  
166 #
167 # get out all of the density types and choose the one with the
168 # block size closest to our desired block size
169 #
170  
171 my $dta = $self->db()->get_DensityTypeAdaptor();
172  
173 my @dtypes = @{$dta->fetch_all_by_logic_name($logic_name)};
174 if( ! @dtypes ){
175 my @all_dtypes = @{ $dta->fetch_all() };
176 @all_dtypes or warning( "No DensityTypes in $dta" ) && return [];
177 my $valid_list = join( ", ", map{$_->analysis->logic_name} @all_dtypes );
178 warning( "No DensityTypes for logic name $logic_name. ".
179 "Select from $valid_list" );
180 return [];
181 }
182  
183 my $best_ratio = undef;
184 my $density_type = undef;
185 my $best_ratio_large = undef;
186 my $density_type_large = undef;
187 my %dt_ratio_hash;
188  
189 foreach my $dt (@dtypes) {
190  
191 my $ratio;
192 if( $dt->block_size() > 0 ) {
193 $ratio = $wanted_block_size/$dt->block_size();
194 } else {
195 # This is only valid if the requested seq_region is the one the
196 # features are stored on. Please use sensibly. Or find better implementation.
197  
198 my $block_size = $slice->seq_region_length() / $dt->region_features();
199 $ratio = $wanted_block_size / $block_size;
200 }
201
202 # we prefer to use a block size that's smaller than the required one
203 # (better results on interpolation).
204 # give larger bits a disadvantage and make them comparable
205 if( $ratio < 1 ) {
206 $ratio = 5/$ratio;
207 }
208  
209 $dt_ratio_hash{ $ratio } = $dt;
210 }
211  
212 $best_ratio = (sort {$a<=>$b} (keys %dt_ratio_hash))[0];
213
214 #the ratio was not good enough, or this logic name was not in the
215 #density type table, return an empty list
216 if(!defined($best_ratio) ||
217 (defined($max_ratio) && $best_ratio > $max_ratio)) {
218 return [];
219 }
220  
221 $density_type = $dt_ratio_hash{$best_ratio};
222  
223 my $constraint = "df.density_type_id = " . $density_type->dbID();
224  
225 my @features =
226 @{$self->fetch_all_by_Slice_constraint($slice,$constraint)};
227  
228 return \@features if(!$interpolate);
229  
230 #interpolate the features into new features of a different size
231 my @out;
232 #sort the features on start position
233 @features = sort( { $a->start() <=> $b->start() } @features );
234  
235 #resize the features that were returned
236 my $start = 1;
237 my $end = $start+$wanted_block_size-1;
238  
239 my $value_type = $density_type->value_type();
240  
241 # create a new density type object for the interpolated features that
242 # is not stored in the database
243 $density_type = Bio::EnsEMBL::DensityType->new
244 (-VALUE_TYPE => $value_type,
245 -ANALYSIS => $density_type->analysis(),
246 -BLOCK_SIZE => $wanted_block_size);
247  
248 while($start < $length) {
249 # $end = $length if($end > $length);
250  
251 my $density_value = 0.0;
252 my ($f, $fstart, $fend, $portion);
253 my @dvalues;
254  
255 #construct a new feature using all of the old density features that
256 #we overlapped
257 while(($f = shift(@features)) && $end > $f->{'start'}) {
258  
259 #what portion of this feature are we using to construct our new block?
260 $fstart = ($f->{'start'} < $start) ? $start : $f->{'start'};
261 $fend = ($f->{'end'} > $end ) ? $end : $f->{'end'};
262 $fend = $length if($fend > $length);
263  
264 if($value_type eq 'sum') {
265  
266 $portion = ($fend-$fstart+1)/$f->length();
267  
268 #take a percentage of density value, depending on how much of the
269 #feature we overlapped
270 $density_value += $portion * $f->{'density_value'};
271  
272 } elsif($value_type eq 'ratio') {
273  
274 #maintain a running total of the length used from each feature
275 #and its value
276 push(@dvalues,[$fend-$fstart+1,$f->{'density_value'}]);
277  
278 } else {
279 throw("Unknown density value type [$value_type].");
280 }
281  
282 #do not want to look at next feature if we only used part of this one:
283 last if($fend < $f->{'end'});
284 }
285  
286 #if we did not completely overlap the last feature, put it back on so
287 #it can be partially used by the next block
288 if(defined($f) && (!defined($fend) || $fend < $f->{'end'})) {
289 unshift(@features, $f);
290 }
291  
292 if($value_type eq 'ratio') {
293 #take a weighted average of the all the density values of the features
294 #used to construct this one
295 my $total_len = $end - $start + 1;
296 if($total_len > 0) {
297 foreach my $pair (@dvalues) {
298 my ($dlen, $dval) = @$pair;
299 $density_value += $dval * ($dlen/$total_len);
300 }
301 }
302 }
303  
304 # interpolated features are not stored in the db so do not set the dbID
305 # or the adaptor
306 push @out, Bio::EnsEMBL::DensityFeature->new
307 (-seq_region => $slice,
308 -start => $start,
309 -end => $end,
310 -density_type => $density_type,
311 -density_value => $density_value);
312  
313 $start = $end + 1;
314 $end += $wanted_block_size;
315 }
316  
317 return \@out;
318 }
319  
320  
321 sub fetch_all {
322 my $self = shift;
323 my $logic_name = shift;
324  
325 my ($sth, $seq_region_id, $start, $end, $density_value, $density_type_id);
326 my ($slice, $density_type, @out);
327 my $sa = $self->db()->get_SliceAdaptor();
328 my $dta = $self->db()->get_DensityTypeAdaptor();
329  
330 if ($logic_name) {
331 $sth = $self->prepare("SELECT df.seq_region_id, df.seq_region_start, df.seq_region_end, df.density_value, df.density_type_id "
332 . "FROM density_feature df, density_type dt, analysis a "
333 . "WHERE df.density_type_id = dt.density_type_id "
334 . "AND dt.analysis_id = a.analysis_id "
335 . "AND logic_name = ?" );
336 $sth->bind_param(1, $logic_name, SQL_VARCHAR);
337 $sth->execute();
338 } else {
339 $sth = $self->prepare("SELECT df.seq_region_id, df.seq_region_start, df.seq_region_end, df.density_value, df.density_type_id "
340 . "FROM density_feature df, density_type dt "
341 . "WHERE df.density_type_id = dt.density_type_id" );
342 $sth->execute();
343 }
344  
345  
346 $sth->bind_columns(\($seq_region_id, $start, $end, $density_value, $density_type_id));
347  
348  
349 while ($sth->fetch()) {
350 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
351 $density_type = $dta->fetch_by_dbID($density_type_id);
352 push (@out, Bio::EnsEMBL::DensityFeature->new
353 (-seq_region => $slice,
354 -start => $start,
355 -end => $end,
356 -density_type => $density_type,
357 -density_value => $density_value));
358 }
359 return \@out;
360 }
361  
362  
363 =head2 fetch_all_by_Slice_constraint
364  
365 Arg [1] : Bio::EnsEMBL::Slice $slice
366 the slice from which to obtain features
367 Arg [2] : (optional) string $constraint
368 An SQL query constraint (i.e. part of the WHERE clause)
369 Example : $fs = $a->fetch_all_by_Slice_constraint($slc, 'density_type_id = 88');
370 Description: Returns a listref of features created from the database which
371 are on the Slice defined by $slice and fulfill the SQL
372 constraint defined by $constraint.
373 Note that this is a re-implementation of a method with the same name
374 in the BaseFeatureAdaptor.
375 This was necessary to remove the use of symliked sequences for density features
376 Returntype : listref of Bio::EnsEMBL::SeqFeatures in Slice coordinates
377 Exceptions : thrown if $slice is not defined
378 Caller : Bio::EnsEMBL::Slice
379 Status : Stable
380  
381 =cut
382  
383  
384 sub fetch_all_by_Slice_constraint {
385 my $self = shift;
386 my $slice = shift;
387 my $constraint = shift;
388  
389 my @result;
390  
391 #Pull out here as we need to remember them & reset accordingly
392 my $bind_params = $self->bind_param_generic_fetch();
393  
394 if ( !ref($slice)
395 || !( $slice->isa('Bio::EnsEMBL::Slice')
396 or $slice->isa('Bio::EnsEMBL::LRGSlice') ) )
397 {
398 throw("Bio::EnsEMBL::Slice argument expected.");
399 }
400  
401 $constraint ||= '';
402  
403 # If the logic name was invalid, undef was returned
404 if ( !defined($constraint) ) { return [] }
405  
406 my $key;
407 my $cache;
408  
409 # Will only use feature_cache if hasn't been no_cache attribute set
410 if (
411 !( defined( $self->db()->no_cache() ) && $self->db()->no_cache() ) )
412 {
413  
414 #strain test and add to constraint if so to stop caching.
415 if ( $slice->isa('Bio::EnsEMBL::Variation::StrainSlice') ) {
416 my $string =
417 $self->dbc()->db_handle()->quote( $slice->strain_name() );
418  
419 if ( $constraint ne "" ) {
420 $constraint .= " AND $string = $string ";
421 } else {
422 $constraint .= " $string = $string ";
423 }
424 }
425  
426 # Check the cache and return the cached results if we have already
427 # done this query. The cache key is the made up from the slice
428 # name, the constraint, and the bound parameters (if there are any).
429 $key = uc( join( ':', $slice->name(), $constraint ) );
430  
431 if ( defined($bind_params) ) {
432 $key .= ':'
433 . join( ':', map { $_->[0] . '/' . $_->[1] } @{$bind_params} );
434 }
435  
436 $cache = $self->_slice_feature_cache();
437 if ( exists( $cache->{$key} ) ) {
438 # Clear the bound parameters and return the cached data.
439 $self->{'_bind_param_generic_fetch'} = ();
440 return $cache->{$key};
441 }
442 } ## end if ( !( defined( $self...)))
443  
444 $self->_bind_param_generic_fetch($bind_params);
445 my $features = $self->_slice_fetch( $slice, $constraint );
446  
447 if ( defined($key) ) {
448 $cache->{$key} = $features;
449 }
450  
451 return $features;
452 }
453  
454  
455 sub _tables {
456 my $self = shift;
457  
458 return (['density_feature', 'df']);
459 }
460  
461  
462 sub _columns {
463 my $self = shift;
464  
465 return qw( df.density_feature_id
466 df.seq_region_id df.seq_region_start df.seq_region_end
467 df.density_value df.density_type_id );
468 }
469  
470  
471  
472 sub _objs_from_sth {
473 my ($self, $sth, $mapper, $dest_slice) = @_;
474  
475 #
476 # This code is ugly because an attempt has been made to remove as many
477 # function calls as possible for speed purposes. Thus many caches,
478 # and a fair bit of gymnastics have been used.
479 #
480  
481 my $sa = $self->db()->get_SliceAdaptor();
482 my $dta = $self->db()->get_DensityTypeAdaptor();
483  
484 my @features;
485 my %dtype_hash;
486 my %slice_hash;
487 my %sr_name_hash;
488 my %sr_cs_hash;
489  
490 my(
491 $density_feature_id, $seq_region_id, $seq_region_start,
492 $seq_region_end, $density_value, $density_type_id );
493  
494 $sth->bind_columns(\(
495 $density_feature_id, $seq_region_id, $seq_region_start,
496 $seq_region_end, $density_value, $density_type_id));
497  
498 my $dest_slice_start;
499 my $dest_slice_end;
500 my $dest_slice_strand;
501 my $dest_slice_length;
502 my $dest_slice_cs;
503 my $dest_slice_sr_name;
504 my $dest_slice_sr_id;
505 my $asma;
506  
507 if ($dest_slice) {
508 $dest_slice_start = $dest_slice->start();
509 $dest_slice_end = $dest_slice->end();
510 $dest_slice_strand = $dest_slice->strand();
511 $dest_slice_length = $dest_slice->length();
512 $dest_slice_cs = $dest_slice->coord_system();
513 $dest_slice_sr_name = $dest_slice->seq_region_name();
514 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
515 $asma = $self->db->get_AssemblyMapperAdaptor();
516 }
517  
518 FEATURE: while($sth->fetch()) {
519  
520 #get the density type object
521 my $density_type = $dtype_hash{$density_type_id} ||= $dta->fetch_by_dbID($density_type_id);
522  
523 #need to get the internal_seq_region, if present
524 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
525 my $slice = $slice_hash{"ID:".$seq_region_id};
526  
527 if (!$slice) {
528 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
529 $slice_hash{"ID:".$seq_region_id} = $slice;
530 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
531 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
532 }
533  
534 #obtain a mapper if none was defined, but a dest_seq_region was
535 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
536 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
537 }
538  
539 my $sr_name = $sr_name_hash{$seq_region_id};
540 my $sr_cs = $sr_cs_hash{$seq_region_id};
541  
542 #
543 # remap the feature coordinates to another coord system
544 # if a mapper was provided
545 #
546  
547 if ($mapper) {
548  
549 my $len = $seq_region_end - $seq_region_start + 1;
550  
551 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
552 ($seq_region_id, $seq_region_start, $seq_region_end) =
553 $mapper->map( $sr_name, $seq_region_start, $seq_region_end, 1, $sr_cs, 0, $dest_slice);
554  
555 } else {
556 ($seq_region_id, $seq_region_start, $seq_region_end) =
557 $mapper->map($sr_name, $seq_region_start, $seq_region_end, 1, $sr_cs);
558 }
559  
560 #skip features that map to gaps or coord system boundaries
561 next FEATURE if (!defined($seq_region_id));
562  
563 if($density_type->value_type() eq 'sum') {
564 #adjust density value so it reflects length of feature actually used
565 my $newlen = $seq_region_end - $seq_region_start +1;
566 $density_value *= $newlen/$len if($newlen != $len);
567 }
568  
569 #get a slice in the coord system we just mapped to
570 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
571 }
572  
573 #
574 # If a destination slice was provided convert the coords.
575 #
576 if (defined($dest_slice)) {
577 my $seq_region_len = $dest_slice->seq_region_length();
578  
579 if ( $dest_slice_strand == 1 ) {
580 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
581 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
582  
583 if ( $dest_slice->is_circular ) {
584 # Handle circular chromosomes.
585  
586 if ( $seq_region_start > $seq_region_end ) {
587 # Looking at a feature overlapping the chromosome origin.
588  
589 if ( $seq_region_end > $dest_slice_start ) {
590 # Looking at the region in the beginning of the chromosome
591 $seq_region_start -= $seq_region_len;
592 }
593 if ( $seq_region_end < 0 ) {
594 $seq_region_end += $seq_region_len;
595 }
596 } else {
597 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
598 # Looking at the region overlapping the chromosome
599 # origin and a feature which is at the beginning of the
600 # chromosome.
601 $seq_region_start += $seq_region_len;
602 $seq_region_end += $seq_region_len;
603 }
604 }
605 }
606 } else {
607  
608 my $start = $dest_slice_end - $seq_region_end + 1;
609 my $end = $dest_slice_end - $seq_region_start + 1;
610  
611 if ($dest_slice->is_circular()) {
612  
613 if ($dest_slice_start > $dest_slice_end) {
614 # slice spans origin or replication
615  
616 if ($seq_region_start >= $dest_slice_start) {
617 $end += $seq_region_len;
618 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
619  
620 } elsif ($seq_region_start <= $dest_slice_end) {
621 # do nothing
622 } elsif ($seq_region_end >= $dest_slice_start) {
623 $start += $seq_region_len;
624 $end += $seq_region_len;
625  
626 } elsif ($seq_region_end <= $dest_slice_end) {
627 $end += $seq_region_len if $end < 0;
628  
629 } elsif ($seq_region_start > $seq_region_end) {
630 $end += $seq_region_len;
631 }
632  
633 } else {
634  
635 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
636 # do nothing
637 } elsif ($seq_region_start > $seq_region_end) {
638 if ($seq_region_start <= $dest_slice_end) {
639 $start -= $seq_region_len;
640 } elsif ($seq_region_end >= $dest_slice_start) {
641 $end += $seq_region_len;
642 }
643 }
644 }
645 }
646  
647 $seq_region_start = $start;
648 $seq_region_end = $end;
649 }
650  
651 # Throw away features off the end of the requested slice or on
652 # different seq_region.
653 if ($seq_region_end < 1
654 || $seq_region_start > $dest_slice_length
655 || ($dest_slice_sr_id != $seq_region_id)) {
656 next FEATURE;
657 }
658 $slice = $dest_slice;
659 }
660  
661 push(
662 @features,
663 $self->_create_feature(
664 'Bio::EnsEMBL::DensityFeature', {
665 -dbID => $density_feature_id,
666 -adaptor => $self,
667 -start => $seq_region_start,
668 -end => $seq_region_end,
669 -seq_region => $slice,
670 -density_value => $density_value,
671 -density_type => $density_type
672 } ) );
673  
674 }
675  
676 return \@features;
677 }
678  
679  
680 =head2 list_dbIDs
681  
682 Arg [1] : none
683 Example : @feature_ids = @{$density_feature_adaptor->list_dbIDs()};
684 Description: Gets an array of internal ids for all density features in the
685 current db
686 Arg[1] : <optional> int. not set to 0 for the ids to be sorted by the seq_region.
687 Returntype : list of ints
688 Exceptions : none
689 Caller : ?
690 Status : Stable
691  
692 =cut
693  
694 sub list_dbIDs {
695 my ($self, $ordered) = @_;
696  
697 return $self->_list_dbIDs("density_feature",undef, $ordered);
698 }
699  
700  
701 =head2 store
702  
703 Arg [1] : list of Bio::EnsEMBL::DensityFeatures @df
704 the simple features to store in the database
705 Example : $density_feature_adaptor->store(1234, @density_feats);
706 Description: Stores a list of density feature objects in the database
707 Returntype : none
708 Exceptions : thrown if @df is not defined, if any of the features do not
709 have an attached slice.
710 or if any elements of @df are not Bio::EnsEMBL::SeqFeatures
711 Caller : general
712 Status : Stable
713  
714 =cut
715  
716 sub store{
717 my ($self,@df) = @_;
718  
719 if( scalar(@df) == 0 ) {
720 throw("Must call store with list of DensityFeatures");
721 }
722 #mysql> desc density_feature;
723 #+--------------------+---------+------+-----+---------+----------------+
724 #| Field | Type | Null | Key | Default | Extra |
725 #+--------------------+---------+------+-----+---------+----------------+
726 #| density_feature_id | int(11) | | PRI | NULL | auto_increment |
727 #| density_type_id | int(11) | | MUL | 0 | |
728 #| seq_region_id | int(11) | | | 0 | |
729 #| seq_region_start | int(11) | | | 0 | |
730 #| seq_region_end | int(11) | | | 0 | |
731 #| density_value | float | | | 0 | |
732 #+--------------------+---------+------+-----+---------+----------------+
733  
734 my $sth = $self->prepare
735 ("INSERT INTO density_feature (seq_region_id, seq_region_start, " .
736 "seq_region_end, density_type_id, " .
737 "density_value) " .
738 "VALUES (?,?,?,?,?)");
739  
740 my $db = $self->db();
741 my $analysis_adaptor = $db->get_AnalysisAdaptor();
742  
743 FEATURE: foreach my $df ( @df ) {
744  
745 if( !ref $df || !$df->isa("Bio::EnsEMBL::DensityFeature") ) {
746 throw("DensityFeature must be an Ensembl DensityFeature, " .
747 "not a [".ref($df)."]");
748 }
749
750 # we dont store 0 value density features
751 next if( $df->density_value == 0 );
752 if($df->is_stored($db)) {
753 warning("DensityFeature [".$df->dbID."] is already stored" .
754 " in this database.");
755 next FEATURE;
756 }
757  
758 if(!defined($df->density_type)) {
759 throw("A density type must be attached to the features to be stored.");
760 }
761  
762 #store the density_type if it has not been stored yet
763  
764 if(!$df->density_type->is_stored($db)) {
765 my $dta = $db->get_DensityTypeAdaptor();
766 $dta->store($df->density_type());
767 }
768  
769 my $original = $df;
770 my $seq_region_id;
771 ($df, $seq_region_id) = $self->_pre_store($df);
772  
773 $sth->bind_param(1,$seq_region_id,SQL_INTEGER);
774 $sth->bind_param(2,$df->start,SQL_INTEGER);
775 $sth->bind_param(3,$df->end,SQL_INTEGER);
776 $sth->bind_param(4,$df->density_type->dbID,SQL_INTEGER);
777 $sth->bind_param(5,$df->density_value,SQL_FLOAT);
778 $sth->execute();
779  
780 $original->dbID($self->last_insert_id('density_feature_id', undef, 'density_feature'));
781 $original->adaptor($self);
782 }
783 }
784  
785 =head2 fetch_Featureset_by_Slice
786  
787 Arg [1-5] : see
788 Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor::fetch_all_by_Slice()
789 for argument documentation
790 Example : $featureset = $dfa->fetch_FeatureSet_by_Slice($slice,'SNPDensity', 10, 1);
791 Description: wrapper around
792 Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor::fetch_all_by_Slice()
793 which returns a Bio::EnsEMBL::DensityFeatureSet and also caches
794 results
795 Returntype : Bio::EnsEMBL::DensityFeatureSet
796 Exceptions : none
797 Caller : general
798 Status : Stable
799  
800 =cut
801  
802 sub fetch_Featureset_by_Slice {
803 my ($self, $slice, $logic_name, $num_blocks, $interpolate, $max_ratio) = @_;
804  
805 my $key = join(":", $slice->name,
806 $logic_name,
807 $num_blocks || 50,
808 $interpolate || 0,
809 $max_ratio);
810
811 unless ($self->{'_density_feature_cache'}->{$key}) {
812 my $dfeats = $self->fetch_all_by_Slice($slice, $logic_name, $num_blocks,
813 $interpolate, $max_ratio);
814 $self->{'_density_feature_cache'}->{$key} =
815 new Bio::EnsEMBL::DensityFeatureSet($dfeats);
816 }
817 return $self->{'_density_feature_cache'}->{$key};
818 }
819  
820  
821 1;