EnsEMBL/DBSQL/DensityFeatureAdaptor.pm untested

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