| 1 | package Bio::Graphics::Glyph::stackedplot; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'Bio::Graphics::Glyph::fixedwidth'; | ||
| 5 | use GD::Simple; | ||
| 6 | use Carp 'cluck'; | ||
| 7 | use Memoize; | ||
| 8 | memoize('scale_width'); | ||
| 9 | memoize('width_needed'); | ||
| 10 | |||
| 11 | sub width_needed { | ||
| 12 | my $self = shift; | ||
| 13 | my $column_width = $self->column_width; | ||
| 14 | my $column_spacing = $self->column_spacing; | ||
| 15 | my $columns = $self->data_series; | ||
| 16 | my $needed = @$columns * ($column_width + $column_spacing); | ||
| 17 | return $needed; | ||
| 18 | } | ||
| 19 | |||
| 20 | #sub pad_right { | ||
| 21 | # my $self = shift; | ||
| 22 | # my $pr = $self->SUPER::pad_right; | ||
| 23 | # my $sw = $self->scale_width; | ||
| 24 | # return $pr+$sw; | ||
| 25 | #} | ||
| 26 | sub pad_right { | ||
| 27 | my $self = shift; | ||
| 28 | my $pr = $self->SUPER::pad_right; | ||
| 29 | my $sw = $self->scale_width + $self->column_width/2; | ||
| 30 | my $content_width = $self->width_needed; | ||
| 31 | my $total = $sw + $content_width; | ||
| 32 | my $additional = $pr - $total; | ||
| 33 | return $pr if $pr > $additional; | ||
| 34 | return $additional; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub scale_width { | ||
| 38 | my $self = shift; | ||
| 39 | return 0 unless $self->do_draw_scale; | ||
| 40 | my ($min,$max) = $self->min_max; | ||
| 41 | my $middle = ($min+$max)/2; | ||
| 42 | my ($longest) = sort {$b<=>$a} map {length($_)} ($min,$middle,$max); | ||
| 43 | return $longest * $self->scale_font->width; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub pad_bottom { | ||
| 47 | my $self = shift; | ||
| 48 | my @labels = $self->column_labels; | ||
| 49 | my $bottom = $self->SUPER::pad_bottom; | ||
| 50 | return $bottom unless @labels; | ||
| 51 | return $bottom + $self->font('gdTinyFont')->height; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub column_width { shift->option('column_width') || 8 } | ||
| 55 | sub column_spacing { shift->option('column_spacing') || 2 } | ||
| 56 | sub maxdepth { 0 } | ||
| 57 | |||
| 58 | sub min_max { | ||
| 59 | my $self = shift; | ||
| 60 | my $min_score = $self->option('min_score') || 0.0; | ||
| 61 | my $max_score = $self->option('max_score') || 1.0; | ||
| 62 | return ($min_score,$max_score); | ||
| 63 | } | ||
| 64 | |||
| 65 | # this behaves more like the image glyph -- it draws a generic glyph, two diagonal lines, and then the | ||
| 66 | # plot underneath. | ||
| 67 | sub draw_contents { | ||
| 68 | my $self = shift; | ||
| 69 | my ($gd,$left,$top,$right,$bottom) = @_; | ||
| 70 | |||
| 71 | my ($min_score,$max_score) = $self->min_max; | ||
| 72 | my $height = $bottom-$top; | ||
| 73 | |||
| 74 | my $scale = $max_score > $min_score ? $height/($max_score-$min_score) : 1; | ||
| 75 | my $y_origin = $min_score <= 0 ? $bottom - (0 - $min_score) * $scale : $bottom; | ||
| 76 | my $scale_width = $self->scale_width; | ||
| 77 | $y_origin = $top if $max_score < 0; | ||
| 78 | $self->draw_stackedplot($gd,$left,$right,$top,$y_origin,$scale,$min_score,$max_score); | ||
| 79 | |||
| 80 | $self->draw_scale($gd,$left,$top,$right,$bottom); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub draw_stackedplot { | ||
| 84 | my $self = shift; | ||
| 85 | my ($gd,$left,$right,$top,$bottom,$scale,$min,$max) = @_; | ||
| 86 | |||
| 87 | my $fgcolor = $self->fgcolor; | ||
| 88 | my $bgcolor = $self->bgcolor; | ||
| 89 | my @colors = $self->series_colors; | ||
| 90 | my @labels = $self->column_labels; | ||
| 91 | my $column_width = $self->column_width; | ||
| 92 | my $column_spacing = $self->column_spacing; | ||
| 93 | my $font = $self->column_font; | ||
| 94 | my $fwidth = $font->width; | ||
| 95 | my $fontcolor = $self->fontcolor; | ||
| 96 | |||
| 97 | # data_series() returns 1 or more values to stack upwards | ||
| 98 | # the totals of the values must be no greater than max_score | ||
| 99 | if (my $values = $self->data_series) { | ||
| 100 | my $x_offset = 0; | ||
| 101 | |||
| 102 | for (my $cluster = 0; $cluster < @$values; $cluster++) { | ||
| 103 | # this will give us a series of data series | ||
| 104 | my $series = $values->[$cluster]; | ||
| 105 | my $y_offset = 0; | ||
| 106 | for (my $i = 0; $i < @$series; $i++) { | ||
| 107 | my $value = $series->[$i]; | ||
| 108 | my $v = $self->clip($value,$min,$max); | ||
| 109 | my $color = $colors[$i] || $bgcolor; | ||
| 110 | |||
| 111 | my $y = $bottom - ($v-$min) * $scale; | ||
| 112 | my $box_bottom = $bottom - $y_offset; | ||
| 113 | my $box_top = $y - $y_offset; | ||
| 114 | $self->filled_box($gd,$left+$x_offset,$box_top,$left+$column_width+$x_offset,$box_bottom,$color); | ||
| 115 | $y_offset += $box_bottom-$box_top; | ||
| 116 | } | ||
| 117 | if (@labels) { | ||
| 118 | my $x = $left+$x_offset+($column_width-$fwidth*$labels[$cluster])/2-1; | ||
| 119 | $gd->string($font,$x,$bottom,$labels[$cluster],$fontcolor); | ||
| 120 | } | ||
| 121 | $x_offset += $column_spacing+$column_width; | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub clip { | ||
| 128 | my $self = shift; | ||
| 129 | my ($value,$min,$max) = @_; | ||
| 130 | $value = $min if defined $min && $value < $min; | ||
| 131 | $value = $max if defined $max && $value > $max; | ||
| 132 | return $value; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub series_colors { | ||
| 136 | my $self = shift; | ||
| 137 | my $values = $self->option('series_colors'); | ||
| 138 | my @colors; | ||
| 139 | if ($values && !ref $values) { | ||
| 140 | @colors = split /\s+/,$values; | ||
| 141 | } elsif (ref $values eq 'ARRAY') { | ||
| 142 | @colors = @$values; | ||
| 143 | } else { | ||
| 144 | @colors = qw(red blue green orange brown grey black); | ||
| 145 | } | ||
| 146 | return map {$self->translate_color($_)} @colors; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub column_labels { | ||
| 150 | my $self = shift; | ||
| 151 | my $values = $self->option('column_labels'); | ||
| 152 | my @labels; | ||
| 153 | if ($values && !ref $values) { | ||
| 154 | @labels = split /\s+/,$values; | ||
| 155 | } elsif (ref $values eq 'ARRAY') { | ||
| 156 | @labels = @$values; | ||
| 157 | } | ||
| 158 | return @labels; | ||
| 159 | } | ||
| 160 | |||
| 161 | # NOTE! | ||
| 162 | # probably data_series should return this: | ||
| 163 | # [series1 => [value1,value2,value3,value4], | ||
| 164 | # series2 => [value1,value2,value3,value4], | ||
| 165 | # series3 => [value1,value2,value3,value4], | ||
| 166 | # ... | ||
| 167 | # ] | ||
| 168 | sub data_series { | ||
| 169 | my $self = shift; | ||
| 170 | my $values = $self->option('series'); | ||
| 171 | return $values if defined $values; | ||
| 172 | |||
| 173 | # otherwise get it from the feature | ||
| 174 | my @values; | ||
| 175 | my @tagvalues = $self->feature->get_tag_values('series'); | ||
| 176 | for my $v (@tagvalues) { | ||
| 177 | if (ref $v && ref $v eq 'ARRAY') { # already in right format | ||
| 178 | push @values,$v; | ||
| 179 | } else { | ||
| 180 | push @values,[split /[,\s]+/,$v]; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | return \@values; | ||
| 184 | } | ||
| 185 | sub do_draw_scale { | ||
| 186 | my $self = shift; | ||
| 187 | my $drawit = $self->option('draw_scale'); | ||
| 188 | return defined $drawit ? $drawit : 1; | ||
| 189 | } | ||
| 190 | sub scale_font { | ||
| 191 | my $self = shift; | ||
| 192 | $self->getfont('scale_font','gdTinyFont'); | ||
| 193 | } | ||
| 194 | sub column_font { | ||
| 195 | my $self = shift; | ||
| 196 | $self->getfont('column_font','gdSmallFont'); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub draw_scale { | ||
| 200 | my $self = shift; | ||
| 201 | my ($gd,$left,$top,$right,$bottom) = @_; | ||
| 202 | my ($min,$max) = $self->min_max; | ||
| 203 | |||
| 204 | $self->panel->startGroup($gd); | ||
| 205 | |||
| 206 | my $simple = GD::Simple->new($gd); | ||
| 207 | $simple->font($self->scale_font); | ||
| 208 | my $dx = 1; | ||
| 209 | my $dy = $simple->font->height/2; | ||
| 210 | |||
| 211 | # these drew a vertical scale line, which didn't look very nice | ||
| 212 | # $simple->moveTo($right,$bottom); | ||
| 213 | # $simple->lineTo($right,$top); | ||
| 214 | |||
| 215 | $simple->moveTo($right,$top); | ||
| 216 | $simple->line(3); | ||
| 217 | $simple->move($dx,$dy); | ||
| 218 | $simple->string($max); | ||
| 219 | |||
| 220 | $simple->moveTo($right,($bottom+$top)/2); | ||
| 221 | $simple->line(3); | ||
| 222 | $simple->move($dx,$dy); | ||
| 223 | $simple->string(($max+$min)/2); | ||
| 224 | |||
| 225 | $simple->moveTo($right,$bottom); | ||
| 226 | $simple->line(3); | ||
| 227 | $simple->move($dx,$dy); | ||
| 228 | $simple->string($min); | ||
| 229 | |||
| 230 | $self->panel->endGroup($gd); | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; | ||
| 234 | |||
| 235 | __END__ |