| 1 | package Bio::Graphics::Glyph::minmax; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw(Bio::Graphics::Glyph); | ||
| 5 | |||
| 6 | sub my_description { | ||
| 7 | return <<END; | ||
| 8 | This is an internal glyph that defines options standard to those glyphs that | ||
| 9 | use colors or xyplots to display a range of quantitative values. Do not use it directly. | ||
| 10 | END | ||
| 11 | } | ||
| 12 | sub my_options { | ||
| 13 | return { | ||
| 14 | min_score => [ | ||
| 15 | 'float', | ||
| 16 | undef, | ||
| 17 | 'The minimum score of the quantitative range.'], | ||
| 18 | max_score => [ | ||
| 19 | 'float', | ||
| 20 | undef, | ||
| 21 | 'The maximum score of the quantitative range.'], | ||
| 22 | bicolor_pivot => [ | ||
| 23 | ['mean','zero','float','max','min','1SD','2SD','3SD'], | ||
| 24 | undef, | ||
| 25 | 'A value to pivot the display on. Typically this involves changing the color of the', | ||
| 26 | 'glyph (and scale axis) depending on whether the feature is above or below the pivot value.', | ||
| 27 | 'Provide "mean" to pivot on the mean of the data series, "zero" to pivot on the', | ||
| 28 | 'zero value, "min" to pivot on the min and "max" on max of data series, also it is', | ||
| 29 | 'possible to use any arbitrary integer or floating point number to pivot at that value.'], | ||
| 30 | pos_color => [ | ||
| 31 | 'color', | ||
| 32 | undef, | ||
| 33 | 'The color to use for values that exceed the bicolor_pivot value.'], | ||
| 34 | neg_color => [ | ||
| 35 | 'color', | ||
| 36 | undef, | ||
| 37 | 'The color to use for values that are below the bicolor_pivot value.'], | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub min_score { | ||
| 42 | shift->option('min_score'); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub max_score { | ||
| 46 | shift->option('max_score'); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub minmax { | ||
| 50 | my $self = shift; | ||
| 51 | my $parts = shift; | ||
| 52 | |||
| 53 | # figure out the colors | ||
| 54 | my $max_score = $self->max_score; | ||
| 55 | my $min_score = $self->min_score; | ||
| 56 | |||
| 57 | my $do_min = !defined $min_score; | ||
| 58 | my $do_max = !defined $max_score; | ||
| 59 | |||
| 60 | if ($do_min or $do_max) { | ||
| 61 | my $first = $parts->[0]; | ||
| 62 | for my $part (@$parts) { | ||
| 63 | my $s = eval { $part->feature->score } || $part; | ||
| 64 | next unless defined $s; | ||
| 65 | $max_score = $s if $do_max && (!defined $max_score or $s > $max_score); | ||
| 66 | $min_score = $s if $do_min && (!defined $min_score or $s < $min_score); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | return $self->sanity_check($min_score,$max_score); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub sanity_check { | ||
| 73 | my $self = shift; | ||
| 74 | my ($min_score,$max_score,@rest) = @_; | ||
| 75 | return ($min_score,$max_score,@rest) if $max_score > $min_score; | ||
| 76 | |||
| 77 | if ($max_score > 0) { | ||
| 78 | $min_score = 0; | ||
| 79 | } else { | ||
| 80 | $max_score = $min_score + 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | return ($min_score,$max_score,@rest); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub midpoint { | ||
| 87 | my $self = shift; | ||
| 88 | my $default = shift; | ||
| 89 | |||
| 90 | my $pivot = $self->bicolor_pivot; | ||
| 91 | if ($pivot eq 'none') { | ||
| 92 | return | ||
| 93 | } elsif ($pivot eq 'zero') { | ||
| 94 | return 0; | ||
| 95 | } elsif ($pivot eq 'mean') { | ||
| 96 | return eval {$self->series_mean} || 0; | ||
| 97 | } elsif ($pivot eq 'min') { | ||
| 98 | return eval {$self->series_min} || 0; | ||
| 99 | } elsif ($pivot eq 'max') { | ||
| 100 | return eval {$self->series_max} || 0; | ||
| 101 | } elsif ($pivot =~ /^(\d+)SD/i) { | ||
| 102 | my $stdevs = $1; | ||
| 103 | return eval {$self->series_mean + $self->series_stdev * $stdevs} || 0; | ||
| 104 | } elsif ($pivot =~ /^[\d.eE+-]+$/){ | ||
| 105 | return $pivot; | ||
| 106 | } else { | ||
| 107 | my $min = $self->min_score or return $default; | ||
| 108 | my $max = $self->max_score or return $default;; | ||
| 109 | return (($min+$max)/2); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub bicolor_pivot { | ||
| 114 | my $self = shift; | ||
| 115 | my $pivot = $self->option('bicolor_pivot'); | ||
| 116 | return if defined $pivot && $pivot eq 'none'; | ||
| 117 | return $pivot; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub pos_color { | ||
| 121 | my $self = shift; | ||
| 122 | my $pivot = $self->bicolor_pivot || 'none'; | ||
| 123 | return $self->bgcolor if $pivot eq 'none'; | ||
| 124 | return defined $self->color('pos_color') ? $self->color('pos_color') : $self->bgcolor; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub neg_color { | ||
| 128 | my $self = shift; | ||
| 129 | my $pivot = $self->bicolor_pivot || 'none'; | ||
| 130 | return $self->bgcolor if $pivot eq 'none'; | ||
| 131 | return defined $self->color('neg_color') ? $self->color('neg_color') : $self->bgcolor; | ||
| 132 | } | ||
| 133 | |||
| 134 | # change the scaling of the y axis | ||
| 135 | sub rescale { | ||
| 136 | my $self = shift; | ||
| 137 | my ($min,$max) = @_; | ||
| 138 | return ($min,$max); # don't do anything here | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | 1; | ||
| 144 | |||
| 145 | __END__ |