| 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 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Bio::EnsEMBL::DensityPlot::BinValueSet | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | package Bio::EnsEMBL::DensityPlot::BinValueSet; | ||
| 45 | $Bio::EnsEMBL::DensityPlot::BinValueSet::VERSION = '114.0.0_50'; # TRIAL | ||
| 46 | $Bio::EnsEMBL::DensityPlot::BinValueSet::VERSION = '114.0.050'; | ||
| 47 | use vars qw($AUTOLOAD @ISA); | ||
| 48 | use strict; | ||
| 49 | use Bio::EnsEMBL::DensityPlot::BinValue; | ||
| 50 | |||
| 51 | # Object preamble - inheriets from Bio::Root::Object | ||
| 52 | |||
| 53 | @ISA = qw(Exporter); | ||
| 54 | #@EXPORT_OK = qw(); | ||
| 55 | # new() is inherited from Bio::Root::Object | ||
| 56 | |||
| 57 | # _initialize is where the heavy stuff will happen when new is called | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my ($class,@args) = @_; | ||
| 61 | |||
| 62 | my $self = {}; | ||
| 63 | bless $self,$class; | ||
| 64 | $self->{'_bin_array'} = []; | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | =head2 add_binvalue | ||
| 71 | |||
| 72 | Title : add_binValue | ||
| 73 | Usage : | ||
| 74 | Function: | ||
| 75 | Example : | ||
| 76 | Returns : | ||
| 77 | Args : | ||
| 78 | |||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub add_binvalue{ | ||
| 83 | my ($self,$value) = @_; | ||
| 84 | |||
| 85 | defined ($value->chromosomestart) || $self->throw( "Bin Value object does not contain a ChromosomeStart method" ); | ||
| 86 | defined ($value->chromosomeend) || $self->throw( "Bin Value object does not contain a ChromosomeEnd method" ); | ||
| 87 | defined ($value->value) || $self->throw( "Bin Value object does not contain a Value method" ); | ||
| 88 | $self->_store_biggest($value->value); | ||
| 89 | $self->_store_smallest($value->value); | ||
| 90 | |||
| 91 | push(@{$self->{'_bin_array'}},$value); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 get_binvalues | ||
| 95 | |||
| 96 | Title : get_binvalues | ||
| 97 | Usage : my @binvalue_objects = $BVSet->get_binvalues | ||
| 98 | Function: scales all the binvalues by the scale_factor and returns them. | ||
| 99 | Example : | ||
| 100 | Returns : array of BinValue objects | ||
| 101 | Args : none | ||
| 102 | |||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub get_binvalues{ | ||
| 107 | my $self = shift; | ||
| 108 | my $biggest_value = $self->{'_biggest_value'} || 0; | ||
| 109 | my $smallest_value = $self->{'_smallest_value'} || 0; | ||
| 110 | |||
| 111 | if (!defined ($biggest_value)||!defined($smallest_value)){ | ||
| 112 | $self->throw("Cannot scale - no values to scale against"); | ||
| 113 | } | ||
| 114 | |||
| 115 | my $width = $self->scale_to_fit(); | ||
| 116 | |||
| 117 | if ($self->stretch && ($biggest_value-$smallest_value) ){ | ||
| 118 | foreach my $bv (@{ $self->{'_bin_array'}}){ | ||
| 119 | my $scaledval = (($bv->value - $smallest_value) / ($biggest_value-$smallest_value) )* $width; | ||
| 120 | $bv->scaledvalue($scaledval); | ||
| 121 | } | ||
| 122 | } elsif($biggest_value) { | ||
| 123 | foreach my $bv (@{ $self->{'_bin_array'}}){ | ||
| 124 | my $scaledval = ($bv->value / $biggest_value) * $width; | ||
| 125 | $bv->scaledvalue($scaledval); | ||
| 126 | } | ||
| 127 | } else { | ||
| 128 | foreach my $bv (@{ $self->{'_bin_array'}}){ | ||
| 129 | $bv->scaledvalue(0); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return ( @{ $self->{'_bin_array'}} ); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | sub size { | ||
| 138 | my $self = shift; | ||
| 139 | return scalar @{$self->{'_bin_array'}}; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 position | ||
| 143 | |||
| 144 | Title : position | ||
| 145 | Usage : $obj->position($newval) | ||
| 146 | Function: | ||
| 147 | Returns : value of position | ||
| 148 | Args : newvalue (optional) | ||
| 149 | |||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub position{ | ||
| 154 | my $self = shift; | ||
| 155 | if( @_ ) { | ||
| 156 | my $value = shift; | ||
| 157 | $self->{'position'} = $value; | ||
| 158 | } | ||
| 159 | return $self->{'position'}; | ||
| 160 | |||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | =head2 label | ||
| 165 | |||
| 166 | Title : label | ||
| 167 | Usage : $obj->label($newval) | ||
| 168 | Function: | ||
| 169 | Returns : value of label | ||
| 170 | Args : newvalue (optional) | ||
| 171 | |||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub label{ | ||
| 176 | my $self = shift; | ||
| 177 | if( @_ ) { | ||
| 178 | my $value = shift; | ||
| 179 | $self->{'label'} = $value; | ||
| 180 | } | ||
| 181 | return $self->{'label'}; | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | =head2 label2 | ||
| 187 | |||
| 188 | Title : label2 | ||
| 189 | Usage : $obj->label2($newval) | ||
| 190 | Function: | ||
| 191 | Returns : value of label2 | ||
| 192 | Args : newvalue (optional) | ||
| 193 | |||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub label2{ | ||
| 198 | my $self = shift; | ||
| 199 | if( @_ ) { | ||
| 200 | my $value = shift; | ||
| 201 | $self->{'label2'} = $value; | ||
| 202 | } | ||
| 203 | return $self->{'label2'}; | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | =head2 color | ||
| 210 | |||
| 211 | Title : color | ||
| 212 | Usage : $obj->color($newval) | ||
| 213 | Function: | ||
| 214 | Returns : value of color | ||
| 215 | Args : newvalue (optional) | ||
| 216 | |||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub color{ | ||
| 221 | my $self = shift; | ||
| 222 | |||
| 223 | |||
| 224 | if( @_ ) { | ||
| 225 | my $value = shift; | ||
| 226 | $self->{'color'} = $value; | ||
| 227 | } | ||
| 228 | return $self->{'color'}; | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | =head2 shape | ||
| 233 | |||
| 234 | Title : shape | ||
| 235 | Usage : $obj->shape($newval) | ||
| 236 | Function: | ||
| 237 | Returns : value of shape | ||
| 238 | Args : newvalue (optional) | ||
| 239 | |||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub shape{ | ||
| 244 | my $self = shift; | ||
| 245 | if( @_ ) { | ||
| 246 | my $value = shift; | ||
| 247 | $self->{'shape'} = $value; | ||
| 248 | } | ||
| 249 | return $self->{'shape'}; | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | =head2 stretch | ||
| 256 | |||
| 257 | Title : stretch | ||
| 258 | Usage : $obj->stretch($newval) | ||
| 259 | Function: gets/sets a boolean for whether we should stretch the data over the | ||
| 260 | range (i.e. from min to max rather than absolute numbers). | ||
| 261 | Returns : value of _stretch | ||
| 262 | Args : newvalue (optional) | ||
| 263 | |||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub stretch{ | ||
| 268 | my ($self,$value) = @_; | ||
| 269 | if( defined $value ) { | ||
| 270 | $self->{'_stretch'} = $value; | ||
| 271 | } | ||
| 272 | return $self->{'_stretch'}; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | =head2 scale_to_fit | ||
| 277 | |||
| 278 | Title : scale_to_fit | ||
| 279 | Usage : $obj->scale_to_fit($newval) | ||
| 280 | Function: gets/sets the number that the BinValues are to be scaled against - | ||
| 281 | i.e. the greatest BinValue->value will be scaled to this number, and the rest | ||
| 282 | scaled in proportion. | ||
| 283 | Returns : scale_to_fit value | ||
| 284 | Args : newvalue (optional) | ||
| 285 | |||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub scale_to_fit{ | ||
| 290 | my $self = shift; | ||
| 291 | if( @_ ) { | ||
| 292 | my $value = shift; | ||
| 293 | $self->{'_scale_to_fit'} = $value; | ||
| 294 | } | ||
| 295 | return $self->{'_scale_to_fit'}; | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | =head2 _store_biggest | ||
| 301 | |||
| 302 | Title : _store_biggest | ||
| 303 | Usage : $self->_store_biggest($newval) | ||
| 304 | Function: internal method for storing the largest BinValue->value in this set. | ||
| 305 | Returns : biggest value seen so far | ||
| 306 | Args : value | ||
| 307 | |||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub _store_biggest { | ||
| 312 | my ($self,$val) = @_; | ||
| 313 | |||
| 314 | if (!defined $self->{'_biggest_value'} || | ||
| 315 | $val > $self->{'_biggest_value'}){ | ||
| 316 | $self->{'_biggest_value'}=$val; | ||
| 317 | } | ||
| 318 | |||
| 319 | return $self->{'_biggest_value'}; | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | =head2 _store_smallest | ||
| 325 | |||
| 326 | Title : _store_smallest | ||
| 327 | Usage : $self->_store_smallest($newval) | ||
| 328 | Function: internal method for storing the smallest BinValue->value in this set. | ||
| 329 | Returns : smallest value seen so far | ||
| 330 | Args : value | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub _store_smallest { | ||
| 335 | my ($self,$val) = @_; | ||
| 336 | |||
| 337 | if (!defined($self->{'_smallest_value'})){ | ||
| 338 | $self->{'_smallest_value'}=$val; | ||
| 339 | } | ||
| 340 | |||
| 341 | if (!defined($self->{'_smallest_value'}) || | ||
| 342 | $val < $self->{'_smallest_value'}){ | ||
| 343 | $self->{'_smallest_value'}=$val; | ||
| 344 | } | ||
| 345 | return $self->{'_smallest_value'}; | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | 1; |