| 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::DensityFeatureSet - | ||
| 34 | A feature representing a set of density features | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Bio::EnsEMBL::DensityFeatureSet; | ||
| 39 | |||
| 40 | my $densitySet = Bio::EnsEMBL::DensityFeatureSet->new( | ||
| 41 | -bin_array = \@out, | ||
| 42 | -stretch = 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | A density feature set is a wrap around a array of density features with | ||
| 48 | additional information about the collective density feature set, such as | ||
| 49 | max_min_values and scale factors etc. a given region. | ||
| 50 | |||
| 51 | This module is part of the Ensembl project http://www.ensembl.org | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | |||
| 58 | package Bio::EnsEMBL::DensityFeatureSet; | ||
| 59 | $Bio::EnsEMBL::DensityFeatureSet::VERSION = '114.0.0_50'; # TRIAL | ||
| 60 | $Bio::EnsEMBL::DensityFeatureSet::VERSION = '114.0.050'; | ||
| 61 | use strict; | ||
| 62 | use warnings; | ||
| 63 | |||
| 64 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 65 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 66 | |||
| 67 | =head2 new | ||
| 68 | |||
| 69 | Description: Creates a new density feature set. | ||
| 70 | Returntype : Bio::EnsEMBL::DensityFeatureSet | ||
| 71 | Exceptions : throw if invalid density value type is provided | ||
| 72 | Caller : general | ||
| 73 | Status : Stable | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my $class = shift; | ||
| 79 | |||
| 80 | my $max_value = undef; | ||
| 81 | my $min_value = undef; | ||
| 82 | |||
| 83 | my($dfeats, $stretch, $scale_to_fit) = | ||
| 84 | rearrange(['FEATURES', 'STRETCH', 'SCALE_TO_FIT'], @_); | ||
| 85 | foreach (@$dfeats){ | ||
| 86 | my $value = $_->density_value; | ||
| 87 | $max_value = $value if (!defined($max_value) || $value > $max_value); | ||
| 88 | $min_value = $value if (!defined($min_value) || $value < $min_value); | ||
| 89 | } | ||
| 90 | |||
| 91 | return bless {'bin_array' => $dfeats, | ||
| 92 | 'stretch' => $stretch, | ||
| 93 | 'scale_to_fit' => $scale_to_fit, | ||
| 94 | 'min_value' => $min_value, | ||
| 95 | 'max_value' => $max_value}, $class; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | =head2 stretch | ||
| 100 | |||
| 101 | Title : stretch | ||
| 102 | Usage : $obj->stretch($newval) | ||
| 103 | Function: gets/sets a boolean for whether we should stretch the data over the | ||
| 104 | range (i.e. from min to max rather than absolute numbers). | ||
| 105 | Returns : value of _stretch | ||
| 106 | Args : newvalue (optional) | ||
| 107 | Status : Stable | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub stretch{ | ||
| 112 | my $self = shift; | ||
| 113 | $self->{'stretch'} = shift if(@_); | ||
| 114 | return $self->{'stretch'}; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | =head2 scale_to_fit | ||
| 119 | |||
| 120 | Title : scale_to_fit | ||
| 121 | Usage : $obj->scale_to_fit($newval) | ||
| 122 | Function: gets/sets the number that the BinValues are to be scaled against - | ||
| 123 | i.e. the greatest BinValue->value will be scaled to this number, and the rest | ||
| 124 | scaled in proportion. | ||
| 125 | Returns : scale_to_fit value | ||
| 126 | Args : newvalue (optional) | ||
| 127 | Status : Stable | ||
| 128 | |||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub scale_to_fit{ | ||
| 133 | my $self = shift; | ||
| 134 | $self->{'scale_to_fit'} = shift if (@_); | ||
| 135 | return $self->{'scale_to_fit'}; | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | =head2 colour | ||
| 140 | |||
| 141 | Title : colour | ||
| 142 | Usage : $obj->colour($newval) | ||
| 143 | Function: | ||
| 144 | Returns : value of colour | ||
| 145 | Args : newvalue (optional) | ||
| 146 | Status : Stable | ||
| 147 | |||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | |||
| 152 | sub colour{ | ||
| 153 | my $self = shift; | ||
| 154 | $self->{'color'} = shift if(@_); | ||
| 155 | return $self->{'color'}; | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | =head2 label | ||
| 160 | |||
| 161 | Title : label | ||
| 162 | Usage : $obj->label($newval) | ||
| 163 | Function: | ||
| 164 | Returns : String containing label | ||
| 165 | Args : newvalue (optional) | ||
| 166 | Status : Stable | ||
| 167 | |||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub label{ | ||
| 172 | my $self = shift; | ||
| 173 | $self->{'label'} = shift if (@_); | ||
| 174 | return $self->{'label'}; | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 label2 | ||
| 180 | |||
| 181 | Title : label2 | ||
| 182 | Usage : $obj->label2($newval) | ||
| 183 | Function: | ||
| 184 | Returns : String containing label2 | ||
| 185 | Args : newvalue (optional) | ||
| 186 | Status : Stable | ||
| 187 | |||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub label2{ | ||
| 192 | my $self = shift; | ||
| 193 | $self->{'label2'} = shift if (@_); | ||
| 194 | return $self->{'label2'}; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | =head2 get_all_binvalues | ||
| 200 | |||
| 201 | Arg [1] : none | ||
| 202 | Example : @binvalues = @{$dfs->get_all_binvalues}; | ||
| 203 | Description: Scales all of the contained DensityFeatures by $scalefactor | ||
| 204 | and returns them. | ||
| 205 | Returntype : reference to a list of DensityFeatures | ||
| 206 | Exceptions : none | ||
| 207 | Caller : general | ||
| 208 | Status : Stable | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub get_all_binvalues{ | ||
| 213 | my $self = shift; | ||
| 214 | my $max_value = $self->max_value(); | ||
| 215 | my $min_value = $self->min_value(); | ||
| 216 | |||
| 217 | return [] if(!@{$self->{'bin_array'}}); | ||
| 218 | |||
| 219 | my $width = $self->scale_to_fit(); | ||
| 220 | return [] unless defined($width); | ||
| 221 | # throw("Cannot scale values - scale_to_fit has not been set"); | ||
| 222 | |||
| 223 | if ($self->stretch && ($max_value-$min_value) ){ | ||
| 224 | foreach my $bv (@{ $self->{'bin_array'}}){ | ||
| 225 | my $scaledval = (($bv->density_value - $min_value) / | ||
| 226 | ($max_value-$min_value) )* $width; | ||
| 227 | $bv->scaledvalue($scaledval); | ||
| 228 | } | ||
| 229 | } elsif($max_value) { | ||
| 230 | foreach my $bv (@{ $self->{'bin_array'}}){ | ||
| 231 | my $scaledval = ($bv->density_value / $max_value) * $width; | ||
| 232 | $bv->scaledvalue($scaledval); | ||
| 233 | } | ||
| 234 | } else { | ||
| 235 | foreach my $bv (@{ $self->{'bin_array'}}){ | ||
| 236 | $bv->scaledvalue(0); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | return $self->{'bin_array'}; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | =head2 max_value | ||
| 245 | |||
| 246 | Arg [1] : none | ||
| 247 | Example : my $max = $dfs->max_value(); | ||
| 248 | Description: Returns the maximum density feature value from the density | ||
| 249 | feature set | ||
| 250 | Returntype : int | ||
| 251 | Exceptions : none | ||
| 252 | Caller : general | ||
| 253 | Status : Stable | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub max_value{ $_[0]->{'max_value'};} | ||
| 258 | |||
| 259 | |||
| 260 | =head2 min_value | ||
| 261 | |||
| 262 | Arg [1] : none | ||
| 263 | Example : my $min = $dfs->min_value(); | ||
| 264 | Description: Returns the minimum density feature value from the density | ||
| 265 | feature set. | ||
| 266 | Returntype : int | ||
| 267 | Exceptions : none | ||
| 268 | Caller : general | ||
| 269 | Status : Stable | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub min_value{ $_[0]->{'min_value'};} | ||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | =head2 size | ||
| 278 | |||
| 279 | Arg [1] : none | ||
| 280 | Example : my $num_features = $dfs->size(); | ||
| 281 | Description: Returns the number of density features in this density feature | ||
| 282 | set. | ||
| 283 | Returntype : int | ||
| 284 | Exceptions : none | ||
| 285 | Caller : general | ||
| 286 | Status : Stable | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub size { | ||
| 291 | my $self = shift; | ||
| 292 | return scalar @{$self->{'bin_array'}}; | ||
| 293 | } | ||
| 294 | |||
| 295 | 1; | ||
| 296 | |||
| 297 | |||
| 298 |