| 1 | package Bio::Graphics::Glyph::xyplot; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | #use GD 'gdTinyFont'; | ||
| 5 | |||
| 6 | use base qw(Bio::Graphics::Glyph::segments Bio::Graphics::Glyph::minmax); | ||
| 7 | use constant DEFAULT_POINT_RADIUS=>4; | ||
| 8 | use Bio::Root::Version; | ||
| 9 | our $VERSION = ${Bio::Root::Version::VERSION}; | ||
| 10 | |||
| 11 | use constant DEBUG=>0; | ||
| 12 | use constant EXTRA_LABEL_PAD=>8; | ||
| 13 | |||
| 14 | sub my_description { | ||
| 15 | return <<'END'; | ||
| 16 | This glyph is used for drawing features that have a position on the | ||
| 17 | genome and a numeric value. It can be used to represent gene | ||
| 18 | prediction scores, motif-calling scores, percent similarity, | ||
| 19 | microarray intensities, or other features that require a line plot. | ||
| 20 | |||
| 21 | The plot is designed to work on a single feature group that contains | ||
| 22 | subfeatures. It is the subfeatures that carry the score | ||
| 23 | information. For a more efficient implementation that is suitable for | ||
| 24 | dense genome-wide data, use Bio::Graphics::Wiggle and the | ||
| 25 | wiggle_xyplot glyph. | ||
| 26 | END | ||
| 27 | |||
| 28 | } | ||
| 29 | sub my_options { | ||
| 30 | { | ||
| 31 | point_radius => [ | ||
| 32 | 'integer', | ||
| 33 | 1, | ||
| 34 | 'When drawing data points, this specifies the radius of each point.', | ||
| 35 | ], | ||
| 36 | clip => [ | ||
| 37 | 'boolean', | ||
| 38 | 0, | ||
| 39 | 'If min_score and/or max_score are manually specified,', | ||
| 40 | 'then setting this to true will cause values outside the', | ||
| 41 | 'range to be clipped.' | ||
| 42 | ], | ||
| 43 | graph_type => [ | ||
| 44 | ['histogram','line','points','linepoints'], | ||
| 45 | 'histogram', | ||
| 46 | 'Type of graph to generate. Options are "boxes",', | ||
| 47 | '"line","points", or "linepoints".', | ||
| 48 | 'The deprecated "boxes" subtype is equivalent to "histogram".' | ||
| 49 | ], | ||
| 50 | point_symbol => [ | ||
| 51 | 'string', | ||
| 52 | 'none', | ||
| 53 | 'Symbol to use for each data point when drawing line graphs.', | ||
| 54 | 'Options are "triangle", "square", "disc", "filled_triangle",', | ||
| 55 | '"filled_square", "filled_disc", "point" and "none"', | ||
| 56 | ], | ||
| 57 | scale => [ | ||
| 58 | 'string', | ||
| 59 | 'three', | ||
| 60 | 'Position where the Y axis scale is drawn, if any.', | ||
| 61 | 'Options are one of "left", "right", "both", "three" or "none".', | ||
| 62 | '"three" will cause the scale to be drawn in the left, right and center.', | ||
| 63 | ], | ||
| 64 | |||
| 65 | scale_color => [ | ||
| 66 | 'color', | ||
| 67 | 'fgcolor', | ||
| 68 | 'Color of the X and Y scales. Defaults to the same as fgcolor.', | ||
| 69 | ], | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | |||
| 73 | my %SYMBOLS = ( | ||
| 74 | triangle => \&draw_triangle, | ||
| 75 | square => \&draw_square, | ||
| 76 | disc => \&draw_disc, | ||
| 77 | point => \&draw_point, | ||
| 78 | ); | ||
| 79 | |||
| 80 | sub extra_label_pad { | ||
| 81 | return EXTRA_LABEL_PAD; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Default pad_left is recursive through all parts. We certainly | ||
| 85 | # don't want to do this for all parts in the graph. | ||
| 86 | sub pad_left { | ||
| 87 | my $self = shift; | ||
| 88 | return 0 unless $self->level == 0; | ||
| 89 | my $left = $self->SUPER::pad_left(@_); | ||
| 90 | my $side = $self->_determine_side; | ||
| 91 | $left += $self->extra_label_pad if $self->label_position eq 'left' && $side =~ /left|both|three/; | ||
| 92 | return $left; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Default pad_left is recursive through all parts. We certainly | ||
| 96 | # don't want to do this for all parts in the graph. | ||
| 97 | sub pad_right { | ||
| 98 | my $self = shift; | ||
| 99 | return 0 unless $self->level == 0; | ||
| 100 | return $self->SUPER::pad_right(@_); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub point_radius { | ||
| 104 | shift->option('point_radius') || DEFAULT_POINT_RADIUS; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub pad_top { | ||
| 108 | my $self = shift; | ||
| 109 | my $pad = $self->Bio::Graphics::Glyph::generic::pad_top(@_); | ||
| 110 | if ($pad < $self->font_height($self->getfont('gdTinyFont'))+8) { | ||
| 111 | $pad = $self->font_height($self->getfont('gdTinyFont'))+8; # extra room for the scale | ||
| 112 | } | ||
| 113 | $pad; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub pad_bottom { | ||
| 117 | my $self = shift; | ||
| 118 | my $pad = $self->Bio::Graphics::Glyph::generic::pad_bottom(@_); | ||
| 119 | if ($pad < $self->font_height($self->getfont('gdTinyFont'))/4) { | ||
| 120 | $pad = $self->font_height($self->getfont('gdTinyFont'))/4; # extra room for the scale | ||
| 121 | } | ||
| 122 | $pad; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub scalecolor { | ||
| 126 | my $self = shift; | ||
| 127 | local $self->{default_opacity} = 1; | ||
| 128 | my $color = $self->color('scale_color') || $self->fgcolor; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub default_scale | ||
| 132 | { | ||
| 133 | return 'three'; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub record_label_positions { | ||
| 137 | my $self = shift; | ||
| 138 | my $rlp = $self->option('record_label_positions'); | ||
| 139 | return $rlp if defined $rlp; | ||
| 140 | return -1; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub graph_type { | ||
| 144 | my $self = shift; | ||
| 145 | $self->option('graph_type') || | ||
| 146 | $self->option('graphtype') || | ||
| 147 | 'boxes'; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub draw { | ||
| 151 | my $self = shift; | ||
| 152 | my ($gd,$dx,$dy) = @_; | ||
| 153 | |||
| 154 | my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy); | ||
| 155 | my @parts = $self->parts; | ||
| 156 | |||
| 157 | return $self->SUPER::draw(@_) unless @parts > 0; | ||
| 158 | |||
| 159 | $self->panel->startGroup($gd); | ||
| 160 | |||
| 161 | my ($min_score,$max_score) = $self->minmax(\@parts); | ||
| 162 | |||
| 163 | my $side = $self->_determine_side(); | ||
| 164 | |||
| 165 | # if a scale is called for, then we adjust the max and min to be even | ||
| 166 | # multiples of a power of 10. | ||
| 167 | if ($side) { | ||
| 168 | $max_score = max10($max_score); | ||
| 169 | $min_score = min10($min_score); | ||
| 170 | } | ||
| 171 | |||
| 172 | my $height = $bottom - $top; | ||
| 173 | my $scale = $max_score > $min_score ? $height/($max_score-$min_score) | ||
| 174 | : 1; | ||
| 175 | my $x = $left; | ||
| 176 | my $y = $top + $self->pad_top; | ||
| 177 | |||
| 178 | # position of "0" on the scale | ||
| 179 | my $y_origin = $min_score <= 0 ? $bottom - (0 - $min_score) * $scale : $bottom; | ||
| 180 | $y_origin = $top if $max_score < 0; | ||
| 181 | |||
| 182 | my $clip_ok = $self->option('clip'); | ||
| 183 | $self->{_clip_ok} = $clip_ok; | ||
| 184 | $self->{_scale} = $scale; | ||
| 185 | $self->{_min_score} = $min_score; | ||
| 186 | $self->{_max_score} = $max_score; | ||
| 187 | $self->{_top} = $top; | ||
| 188 | $self->{_bottom} = $bottom; | ||
| 189 | |||
| 190 | # now seed all the parts with the information they need to draw their positions | ||
| 191 | foreach (@parts) { | ||
| 192 | my $s = $_->score; | ||
| 193 | $_->{_y_position} = $self->score2position($s); | ||
| 194 | warn "y_position = $_->{_y_position}" if DEBUG; | ||
| 195 | } | ||
| 196 | my $type = $self->option('graph_type') || $self->option('graphtype') || 'boxes'; | ||
| 197 | my (@draw_methods) = $self->lookup_draw_method($type); | ||
| 198 | $self->throw("Invalid graph type '$type'") unless @draw_methods; | ||
| 199 | |||
| 200 | $self->panel->startGroup($gd); | ||
| 201 | $self->_draw_grid($gd,$scale,$min_score,$max_score,$dx,$dy,$y_origin); | ||
| 202 | |||
| 203 | $self->panel->endGroup($gd); | ||
| 204 | |||
| 205 | for my $draw_method (@draw_methods) { | ||
| 206 | $self->$draw_method($gd,$dx,$dy,$y_origin); | ||
| 207 | } | ||
| 208 | |||
| 209 | $self->panel->startGroup($gd); | ||
| 210 | $self->_draw_scale($gd,$scale,$min_score,$max_score,$dx,$dy,$y_origin); | ||
| 211 | $self->panel->endGroup($gd); | ||
| 212 | |||
| 213 | $self->draw_label(@_) if $self->option('label') or $self->record_label_positions; | ||
| 214 | $self->draw_description(@_) if $self->option('description'); | ||
| 215 | $self->draw_legend(@_) if $self->option('overlay'); | ||
| 216 | |||
| 217 | $self->panel->endGroup($gd); | ||
| 218 | } | ||
| 219 | |||
| 220 | sub lookup_draw_method { | ||
| 221 | my $self = shift; | ||
| 222 | my $type = shift; | ||
| 223 | |||
| 224 | return '_draw_boxes' if $type eq 'histogram'; # same thing | ||
| 225 | return '_draw_boxes' if $type eq 'boxes'; | ||
| 226 | return qw(_draw_line _draw_points) if $type eq 'linepoints'; | ||
| 227 | return '_draw_line' if $type eq 'line'; | ||
| 228 | return '_draw_points' if $type eq 'points'; | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub normalize_track { | ||
| 233 | my $self = shift; | ||
| 234 | my @glyphs_in_track = @_; | ||
| 235 | my ($global_min,$global_max); | ||
| 236 | for my $g (@glyphs_in_track) { | ||
| 237 | my ($min_score,$max_score) = $g->minmax($g->get_parts); | ||
| 238 | $global_min = $min_score if !defined $global_min || $min_score < $global_min; | ||
| 239 | $global_max = $max_score if !defined $global_max || $max_score > $global_max; | ||
| 240 | } | ||
| 241 | # note that configure applies to the whole track | ||
| 242 | $glyphs_in_track[0]->configure(-min_score => $global_min); | ||
| 243 | $glyphs_in_track[0]->configure(-max_score => $global_max); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub get_parts { | ||
| 247 | my $self = shift; | ||
| 248 | my @parts = $self->parts; | ||
| 249 | return \@parts; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub score { | ||
| 253 | my $self = shift; | ||
| 254 | my $s = $self->option('score'); | ||
| 255 | return $s if defined $s; | ||
| 256 | return eval { $self->feature->score }; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub score2position { | ||
| 260 | my $self = shift; | ||
| 261 | my $score = shift; | ||
| 262 | |||
| 263 | return undef unless defined $score; | ||
| 264 | |||
| 265 | if ($self->{_clip_ok} && $score < $self->{_min_score}) { | ||
| 266 | return $self->{_bottom}; | ||
| 267 | } | ||
| 268 | |||
| 269 | elsif ($self->{_clip_ok} && $score > $self->{_max_score}) { | ||
| 270 | return $self->{_top}; | ||
| 271 | } | ||
| 272 | |||
| 273 | else { | ||
| 274 | warn "score = $score, _top = $self->{_top}, _bottom = $self->{_bottom}, max = $self->{_max_score}, min=$self->{_min_score}" if DEBUG; | ||
| 275 | my $position = ($score-$self->{_min_score}) * $self->{_scale}; | ||
| 276 | warn "position =$position" if DEBUG; | ||
| 277 | return $self->{_bottom} - $position; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | sub log10 { log(shift)/log(10) } | ||
| 282 | sub max10 { | ||
| 283 | my $a = shift; | ||
| 284 | return 0 if $a==0; | ||
| 285 | return -min10(-$a) if $a<0; | ||
| 286 | return max10($a*10)/10 if $a < 1; | ||
| 287 | |||
| 288 | my $l=int(log10($a)); | ||
| 289 | $l = 10**$l; | ||
| 290 | my $r = $a/$l; | ||
| 291 | return $r*$l if int($r) == $r; | ||
| 292 | return $l*int(($a+$l)/$l); | ||
| 293 | } | ||
| 294 | sub min10 { | ||
| 295 | my $a = shift; | ||
| 296 | return 0 if $a==0; | ||
| 297 | return -max10(-$a) if $a<0; | ||
| 298 | return min10($a*10)/10 if $a < 1; | ||
| 299 | |||
| 300 | my $l=int(log10($a)); | ||
| 301 | $l = 10**$l; | ||
| 302 | my $r = $a/$l; | ||
| 303 | return $r*$l if int($r) == $r; | ||
| 304 | return $l*int($a/$l); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _draw_boxes { | ||
| 308 | my $self = shift; | ||
| 309 | my ($gd,$left,$top,$y_origin) = @_; | ||
| 310 | |||
| 311 | my @parts = $self->parts; | ||
| 312 | my $lw = $self->linewidth; | ||
| 313 | # Make the boxes transparent | ||
| 314 | my $positive = $self->pos_color + 1073741824; | ||
| 315 | my $negative = $self->neg_color + 1073741824; | ||
| 316 | my $height = $self->height; | ||
| 317 | |||
| 318 | my $midpoint = $self->midpoint ? $self->score2position($self->midpoint) | ||
| 319 | : $y_origin; | ||
| 320 | |||
| 321 | my $partcolor = $self->code_option('part_color'); | ||
| 322 | my $factory = $self->factory; | ||
| 323 | |||
| 324 | # draw each of the boxes as a rectangle | ||
| 325 | for (my $i = 0; $i < @parts; $i++) { | ||
| 326 | |||
| 327 | my $part = $parts[$i]; | ||
| 328 | my $next = $parts[$i+1]; | ||
| 329 | |||
| 330 | my ($color,$negcolor); | ||
| 331 | |||
| 332 | # special check here for the part_color being defined so as not to introduce lots of | ||
| 333 | # checking overhead when it isn't | ||
| 334 | if ($partcolor) { | ||
| 335 | $color = $self->translate_color($factory->option($part,'part_color',0,0)); | ||
| 336 | $negcolor = $color; | ||
| 337 | } else { | ||
| 338 | $color = $positive; | ||
| 339 | $negcolor = $negative; | ||
| 340 | } | ||
| 341 | |||
| 342 | my ($x1,$y1,$x2,$y2) = $part->calculate_boundaries($left,$top); | ||
| 343 | next unless defined $part->{_y_position}; | ||
| 344 | # prevent boxes from being less than 1 pixel | ||
| 345 | $x2 = $x1+1 if $x2-$x1 < 1; | ||
| 346 | if ($part->{_y_position} < $midpoint) { | ||
| 347 | $gd->filledRectangle($x1,$part->{_y_position},$x2,$y_origin,$color); | ||
| 348 | } else { | ||
| 349 | $gd->filledRectangle($x1,$y_origin,$x2,$part->{_y_position},$negcolor); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | # That's it. | ||
| 354 | } | ||
| 355 | |||
| 356 | sub _draw_line { | ||
| 357 | my $self = shift; | ||
| 358 | my ($gd,$left,$top) = @_; | ||
| 359 | |||
| 360 | my @parts = $self->parts; | ||
| 361 | my $fgcolor = $self->fgcolor; | ||
| 362 | my $bgcolor = $self->bgcolor; | ||
| 363 | |||
| 364 | # connect to center positions of each interval | ||
| 365 | my $first_part = shift @parts; | ||
| 366 | my ($x1,$y1,$x2,$y2) = $first_part->calculate_boundaries($left,$top); | ||
| 367 | my $current_x = ($x1+$x2)/2; | ||
| 368 | my $current_y = $first_part->{_y_position}; | ||
| 369 | |||
| 370 | for my $part (@parts) { | ||
| 371 | |||
| 372 | ($x1,$y1,$x2,$y2) = $part->calculate_boundaries($left,$top); | ||
| 373 | my $next_x = ($x1+$x2)/2; | ||
| 374 | my $next_y = $part->{_y_position}; | ||
| 375 | $gd->line($current_x,$current_y,$next_x,$next_y,$fgcolor) | ||
| 376 | if defined $current_y and defined $next_y; | ||
| 377 | ($current_x,$current_y) = ($next_x,$next_y); | ||
| 378 | } | ||
| 379 | |||
| 380 | } | ||
| 381 | |||
| 382 | sub _draw_points { | ||
| 383 | my $self = shift; | ||
| 384 | my ($gd,$left,$top) = @_; | ||
| 385 | my $symbol_name = $self->option('point_symbol') || 'point'; | ||
| 386 | my $filled = $symbol_name =~ s/^filled_//; | ||
| 387 | my $symbol_ref = $SYMBOLS{$symbol_name}; | ||
| 388 | |||
| 389 | my @parts = $self->parts; | ||
| 390 | my $fgcolor = $self->fgcolor; | ||
| 391 | my $bgcolor = $self->bgcolor; | ||
| 392 | my $pr = $self->point_radius; | ||
| 393 | |||
| 394 | my $partcolor = $self->code_option('part_color'); | ||
| 395 | my $factory = $self->factory; | ||
| 396 | |||
| 397 | for my $part (@parts) { | ||
| 398 | my ($x1,$y1,$x2,$y2) = $part->calculate_boundaries($left,$top); | ||
| 399 | my $x = ($x1+$x2)/2; | ||
| 400 | my $y = $part->{_y_position}; | ||
| 401 | next unless defined $y; | ||
| 402 | |||
| 403 | my $color; | ||
| 404 | if ($partcolor) { | ||
| 405 | $color = $self->translate_color($factory->option($part,'part_color',0,0)); | ||
| 406 | } else { | ||
| 407 | $color = $fgcolor; | ||
| 408 | } | ||
| 409 | |||
| 410 | $symbol_ref->($gd,$x,$y,$pr,$color,$filled); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _determine_side | ||
| 415 | { | ||
| 416 | my $self = shift; | ||
| 417 | my $side = $self->option('scale'); | ||
| 418 | return if $side eq 'none'; | ||
| 419 | $side ||= $self->default_scale(); | ||
| 420 | return $side; | ||
| 421 | } | ||
| 422 | |||
| 423 | sub _draw_scale { | ||
| 424 | my $self = shift; | ||
| 425 | my ($gd,$scale,$min,$max,$dx,$dy,$y_origin) = @_; | ||
| 426 | my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries($dx,$dy); | ||
| 427 | |||
| 428 | my $crosses_origin = $min < 0 && $max > 0; | ||
| 429 | |||
| 430 | my $side = $self->_determine_side() or return; | ||
| 431 | |||
| 432 | my $fg = $self->scalecolor; | ||
| 433 | my $font = $self->font('gdTinyFont'); | ||
| 434 | |||
| 435 | my $middle = ($x1+$x2)/2; | ||
| 436 | |||
| 437 | # minor ticks - multiples of 10 | ||
| 438 | my $y_scale = $self->minor_ticks($min,$max,$y1,$y2); | ||
| 439 | |||
| 440 | my $p = $self->panel; | ||
| 441 | my $gc = $self->translate_color($p->gridcolor); | ||
| 442 | my $mgc= $self->translate_color($p->gridmajorcolor); | ||
| 443 | |||
| 444 | $gd->line($x1,$y1,$x1,$y2,$fg) if $side eq 'left' || $side eq 'both' || $side eq 'three'; | ||
| 445 | $gd->line($x2,$y1,$x2,$y2,$fg) if $side eq 'right' || $side eq 'both' || $side eq 'three'; | ||
| 446 | $gd->line($middle,$y1,$middle,$y2,$fg) if $side eq 'three'; | ||
| 447 | |||
| 448 | $gd->line($x1,$y_origin,$x2,$y_origin,$mgc); | ||
| 449 | |||
| 450 | my @points = ([$y1,$max],[$y2,$min]); | ||
| 451 | push @points,$crosses_origin ? [$y_origin,0] : [($y1+$y2)/2,($min+$max)/2]; | ||
| 452 | |||
| 453 | my $last_font_pos = -99999999999; | ||
| 454 | |||
| 455 | for (sort {$a->[0]<=>$b->[0]} @points) { | ||
| 456 | $gd->line($x1-3,$_->[0],$x1,$_->[0],$fg) if $side eq 'left' || $side eq 'both' || $side eq 'three'; | ||
| 457 | $gd->line($x2,$_->[0],$x2+3,$_->[0],$fg) if $side eq 'right' || $side eq 'both' || $side eq 'three'; | ||
| 458 | $gd->line($middle,$_->[0],$middle+3,$_->[0],$fg) if $side eq 'three'; | ||
| 459 | |||
| 460 | my $font_pos = $_->[0]-($self->font_height($font)/2); | ||
| 461 | $font_pos-=2 if $_->[1] < 0; # jog a little bit for neg sign | ||
| 462 | |||
| 463 | next unless $font_pos > $last_font_pos + $self->font_height($font)/2; # prevent labels from clashing | ||
| 464 | if ($side eq 'left' or $side eq 'both' or $side eq 'three') { | ||
| 465 | $gd->string($font, | ||
| 466 | $x1 - $self->string_width($_->[1],$font) - 3,$font_pos, | ||
| 467 | $_->[1], | ||
| 468 | $fg); | ||
| 469 | } | ||
| 470 | if ($side eq 'right' or $side eq 'both' or $side eq 'three') { | ||
| 471 | $gd->string($font, | ||
| 472 | $x2 + 5,$font_pos, | ||
| 473 | $_->[1], | ||
| 474 | $fg); | ||
| 475 | } | ||
| 476 | if ($side eq 'three') { | ||
| 477 | $gd->string($font, | ||
| 478 | $middle + 5,$font_pos, | ||
| 479 | $_->[1], | ||
| 480 | $fg); | ||
| 481 | } | ||
| 482 | $last_font_pos = $font_pos; | ||
| 483 | } | ||
| 484 | |||
| 485 | for (my $y = $y2-$y_scale; $y > $y1; $y -= $y_scale) { | ||
| 486 | my $yr = int($y+0.5); | ||
| 487 | $gd->line($x1-3,$yr,$x1,$yr,$fg) if $side eq 'left' or $side eq 'both' or $side eq 'three'; | ||
| 488 | $gd->line($x2,$yr,$x2+3,$yr,$fg) if $side eq 'right' or $side eq 'both' or $side eq 'three'; | ||
| 489 | $gd->line($middle-1,$yr,$middle+2,$yr,$fg) if $side eq 'three'; | ||
| 490 | } | ||
| 491 | |||
| 492 | |||
| 493 | } | ||
| 494 | |||
| 495 | sub _draw_grid { | ||
| 496 | my $self = shift; | ||
| 497 | my ($gd,$scale,$min,$max,$dx,$dy,$y_origin) = @_; | ||
| 498 | my $side = $self->_determine_side(); | ||
| 499 | return unless $side; | ||
| 500 | |||
| 501 | my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries($dx,$dy); | ||
| 502 | my $p = $self->panel; | ||
| 503 | my $gc = $self->translate_color($p->gridcolor); | ||
| 504 | my $y_scale = $self->minor_ticks($min,$max,$y1,$y2); | ||
| 505 | |||
| 506 | for (my $y = $y2-$y_scale; $y > $y1; $y -= $y_scale) { | ||
| 507 | my $yr = int($y+0.5); | ||
| 508 | $gd->line($x1-1,$yr,$x2,$yr,$gc); | ||
| 509 | } | ||
| 510 | $gd->line($x1,$y1,$x2,$y1,$gc); | ||
| 511 | $gd->line($x1,$y2,$x2,$y2,$gc); | ||
| 512 | } | ||
| 513 | |||
| 514 | sub minor_ticks { | ||
| 515 | my $self = shift; | ||
| 516 | my ($min,$max,$top,$bottom) = @_; | ||
| 517 | |||
| 518 | my $interval = 1; | ||
| 519 | my $height = $bottom-$top; | ||
| 520 | my $y_scale = 1; | ||
| 521 | if ($max > $min) { | ||
| 522 | while ($height/(($max-$min)/$interval) < 2) { $interval *= 10 } | ||
| 523 | $y_scale = $height/(($max-$min)/$interval); | ||
| 524 | } | ||
| 525 | |||
| 526 | } | ||
| 527 | |||
| 528 | # Let the feature attributes override the labelcolor | ||
| 529 | sub labelcolor { | ||
| 530 | my $self = shift; | ||
| 531 | my ($labelcolor) = eval {$self->feature->get_tag_values('labelcolor')}; | ||
| 532 | return $labelcolor ? $self->translate_color($labelcolor) | ||
| 533 | : $self->SUPER::labelcolor; | ||
| 534 | } | ||
| 535 | |||
| 536 | # we are unbumpable! | ||
| 537 | sub bump { | ||
| 538 | return 0; | ||
| 539 | } | ||
| 540 | |||
| 541 | sub connector { | ||
| 542 | my $self = shift; | ||
| 543 | my $type = $self->option('graph_type'); | ||
| 544 | return 1 if $type eq 'line' or $type eq 'linepoints'; | ||
| 545 | } | ||
| 546 | |||
| 547 | sub height { | ||
| 548 | my $self = shift; | ||
| 549 | return $self->option('graph_height') || $self->SUPER::height; | ||
| 550 | } | ||
| 551 | |||
| 552 | sub draw_description { | ||
| 553 | my $self = shift; | ||
| 554 | return if $self->bump eq 'overlap'; | ||
| 555 | return $self->SUPER::draw_description(@_); | ||
| 556 | } | ||
| 557 | |||
| 558 | sub draw_triangle { | ||
| 559 | my ($gd,$x,$y,$pr,$color,$filled) = @_; | ||
| 560 | $pr /= 2; | ||
| 561 | my ($vx1,$vy1) = ($x-$pr,$y+$pr); | ||
| 562 | my ($vx2,$vy2) = ($x, $y-$pr); | ||
| 563 | my ($vx3,$vy3) = ($x+$pr,$y+$pr); | ||
| 564 | my $poly = GD::Polygon->new; | ||
| 565 | $poly->addPt($vx1,$vy1,$vx2,$vy2); | ||
| 566 | $poly->addPt($vx2,$vy2,$vx3,$vy3); | ||
| 567 | $poly->addPt($vx3,$vy3,$vx1,$vy1); | ||
| 568 | if ($filled) { | ||
| 569 | $gd->filledPolygon($poly,$color); | ||
| 570 | } else { | ||
| 571 | $gd->polygon($poly,$color); | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | sub draw_square { | ||
| 576 | my ($gd,$x,$y,$pr,$color,$filled) = @_; | ||
| 577 | $pr /= 2; | ||
| 578 | my $poly = GD::Polygon->new; | ||
| 579 | $poly->addPt($x-$pr,$y-$pr); | ||
| 580 | $poly->addPt($x+$pr,$y-$pr); | ||
| 581 | $poly->addPt($x+$pr,$y+$pr); | ||
| 582 | $poly->addPt($x-$pr,$y+$pr); | ||
| 583 | if ($filled) { | ||
| 584 | $gd->filledPolygon($poly,$color); | ||
| 585 | } else { | ||
| 586 | $gd->polygon($poly,$color); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | sub draw_disc { | ||
| 590 | my ($gd,$x,$y,$pr,$color,$filled) = @_; | ||
| 591 | if ($filled) { | ||
| 592 | $gd->filledArc($x,$y,$pr,$pr,0,360,$color); | ||
| 593 | } else { | ||
| 594 | $gd->arc($x,$y,$pr,$pr,0,360,$color); | ||
| 595 | } | ||
| 596 | } | ||
| 597 | sub draw_point { | ||
| 598 | my ($gd,$x,$y,$pr,$color) = @_; | ||
| 599 | $gd->setPixel($x,$y,$color); | ||
| 600 | } | ||
| 601 | |||
| 602 | sub keyglyph { | ||
| 603 | my $self = shift; | ||
| 604 | |||
| 605 | my $scale = 1/$self->scale; # base pairs/pixel | ||
| 606 | |||
| 607 | my $feature = | ||
| 608 | Bio::Graphics::Feature->new( | ||
| 609 | -segments=>[ [ 0*$scale,9*$scale], | ||
| 610 | [ 10*$scale,19*$scale], | ||
| 611 | [ 20*$scale, 29*$scale] | ||
| 612 | ], | ||
| 613 | -name => 'foo bar', | ||
| 614 | -strand => '+1'); | ||
| 615 | ($feature->segments)[0]->score(10); | ||
| 616 | ($feature->segments)[1]->score(50); | ||
| 617 | ($feature->segments)[2]->score(25); | ||
| 618 | my $factory = $self->factory->clone; | ||
| 619 | $factory->set_option(label => 1); | ||
| 620 | $factory->set_option(bump => 0); | ||
| 621 | $factory->set_option(connector => 'solid'); | ||
| 622 | my $glyph = $factory->make_glyph(0,$feature); | ||
| 623 | return $glyph; | ||
| 624 | } | ||
| 625 | |||
| 626 | sub symbols { | ||
| 627 | my $self = shift; | ||
| 628 | return \%SYMBOLS; | ||
| 629 | } | ||
| 630 | |||
| 631 | sub draw_label { | ||
| 632 | my $self = shift; | ||
| 633 | my ($gd,$left,$top,$partno,$total_parts) = @_; | ||
| 634 | my $label = $self->label or return; | ||
| 635 | |||
| 636 | if ($self->bump eq 'overlap') { | ||
| 637 | my $x = $self->left + $left + $self->pad_left; | ||
| 638 | $x = $self->panel->left + 1 if $x <= $self->panel->left; | ||
| 639 | $x += ($self->panel->glyph_scratch||0); | ||
| 640 | |||
| 641 | my $font = $self->labelfont; | ||
| 642 | my $width = $self->string_width($label,$font)+4; | ||
| 643 | my $height= $self->string_height('',$font); | ||
| 644 | unless ($self->record_label_positions) { | ||
| 645 | $gd->filledRectangle($x,$top,$x+$width+6,$top+$height,$self->bgcolor); | ||
| 646 | local $self->{default_opacity} = 1; | ||
| 647 | $gd->string($font,$x+3,$top,$label,$self->contrasting_label_color($gd,$self->bgcolor)); | ||
| 648 | } | ||
| 649 | $self->panel->glyph_scratch($self->panel->glyph_scratch + $width); | ||
| 650 | $self->panel->add_key_box($self,$label,$x,$top) if $self->record_label_positions; | ||
| 651 | |||
| 652 | } elsif ($self->label_position eq 'left') { | ||
| 653 | my $font = $self->labelfont; | ||
| 654 | my $x = $self->left + $left - $self->string_width($label,$font) - $self->extra_label_pad; | ||
| 655 | my $y = $self->{top} + $top; | ||
| 656 | |||
| 657 | $self->render_label($gd, | ||
| 658 | $font, | ||
| 659 | $x, | ||
| 660 | $y, | ||
| 661 | $label); | ||
| 662 | |||
| 663 | } else { | ||
| 664 | $self->SUPER::draw_label(@_); | ||
| 665 | } | ||
| 666 | |||
| 667 | } | ||
| 668 | |||
| 669 | sub contrasting_label_color { | ||
| 670 | my $self = shift; | ||
| 671 | my ($gd,$bgcolor) = @_; | ||
| 672 | my ($r,$g,$b) = $gd->rgb($bgcolor); | ||
| 673 | my $avg = ($r+$g+$b)/3; | ||
| 674 | return $self->translate_color($avg > 128 ? 'black' : 'white'); | ||
| 675 | } | ||
| 676 | |||
| 677 | sub draw_legend { | ||
| 678 | my $self = shift; | ||
| 679 | my ($gd,$left,$top,$partno,$total_parts) = @_; | ||
| 680 | return if $self->bump eq 'overlap'; | ||
| 681 | |||
| 682 | my $color = $self->option('fgcolor'); | ||
| 683 | my $name = $self->feature->{name}; | ||
| 684 | |||
| 685 | my $label = "<a id=\"legend_$name\" target=\"_blank\" href=\"#\"> <font color=\'$color\';\">" . $name . "</font></a>" or return; | ||
| 686 | |||
| 687 | my $font = $self->labelfont; | ||
| 688 | my $x = $self->left + $left - $self->string_width($label,$font) - $self->extra_label_pad; | ||
| 689 | my $y = $self->{top} + $top; | ||
| 690 | my $is_legend = 1; | ||
| 691 | $self->render_label($gd, | ||
| 692 | $font, | ||
| 693 | $x, | ||
| 694 | $y, | ||
| 695 | $label, | ||
| 696 | $is_legend); | ||
| 697 | } | ||
| 698 | |||
| 699 | 1; | ||
| 700 | |||
| 701 | __END__ |