| 1 | package Bio::Graphics::Glyph::heat_map_ideogram; | ||
| 2 | |||
| 3 | # Glyph to draw chromosome heat_map ideograms | ||
| 4 | |||
| 5 | use strict qw/vars refs/; | ||
| 6 | use GD; | ||
| 7 | |||
| 8 | use base qw(Bio::Graphics::Glyph::ideogram Bio::Graphics::Glyph::heat_map); | ||
| 9 | |||
| 10 | sub my_description { | ||
| 11 | return <<END; | ||
| 12 | This glyph draws a chromosome ideogram using scored features instead | ||
| 13 | of cytobands. It is a hybrid of the heat_map and ideograms glyphs | ||
| 14 | and accepts options for both. A typical usage would be to pair this | ||
| 15 | glyph with an aggregator that groups scored features such as blast hits | ||
| 16 | or gene_density bins, etc with a centromere. The result is a chromosome | ||
| 17 | ideogram that has bands whose colors vary porportionate to the feature | ||
| 18 | score. | ||
| 19 | |||
| 20 | See L<Bio::Graphics::Glyph> for a full explanation of standard options. | ||
| 21 | |||
| 22 | See L<Bio::Graphics::Glyph::heat_map> for an explanation of heat_map options. | ||
| 23 | |||
| 24 | See L<Bio::Graphics::Glyph::ideogram> for an explanation of ideogram options. | ||
| 25 | END | ||
| 26 | } | ||
| 27 | |||
| 28 | sub draw { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | my @parts = $self->parts; | ||
| 32 | |||
| 33 | @parts = $self if !@parts && $self->level == 0; | ||
| 34 | return $self->SUPER::draw(@_) unless @parts; | ||
| 35 | |||
| 36 | $self->{single}++ if @parts == 1; | ||
| 37 | |||
| 38 | $self->calculate_gradient(\@parts); | ||
| 39 | |||
| 40 | # adjust for label and description | ||
| 41 | my ($gd,$x,$y) = @_; | ||
| 42 | $x += $self->left + $self->pad_left; | ||
| 43 | $y += $self->top + $self->pad_top; | ||
| 44 | |||
| 45 | # Draw centromeres and telomeres last | ||
| 46 | my @last; | ||
| 47 | for my $part (@parts) { | ||
| 48 | push @last, $part and next if | ||
| 49 | $part->feature->method eq 'centromere' || | ||
| 50 | $part->feature->start <= 1 || | ||
| 51 | $part->feature->stop >= $self->panel->end - 1000; | ||
| 52 | |||
| 53 | $self->draw_component($part,$gd,$x,$y); | ||
| 54 | } | ||
| 55 | |||
| 56 | for my $part (@last) { | ||
| 57 | my $tile = $self->create_tile('right') | ||
| 58 | if $part->feature->method eq 'centromere'; | ||
| 59 | $self->draw_component($part,$gd,$x,$y); | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->draw_label(@_) if $self->option('label'); | ||
| 63 | $self->draw_description(@_) if $self->option('description'); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub draw_component { | ||
| 67 | my $self = shift; | ||
| 68 | my $glyph = shift; | ||
| 69 | my $gd = shift; | ||
| 70 | my ( $x1, $y1, $x2, $y2 ) = $glyph->bounds(@_); | ||
| 71 | # force odd width so telomere arcs are centered | ||
| 72 | $y2 ++ if ($y2 - $y1) % 2; | ||
| 73 | |||
| 74 | my $arcradius = $self->option('arcradius') || 7; | ||
| 75 | my $feature = $glyph->feature; | ||
| 76 | my $score = $feature->score; | ||
| 77 | my $is_cent = 1 if $feature->method eq 'centromere'; | ||
| 78 | my $fgcolor = $self->fgcolor; | ||
| 79 | my $bgcolor; | ||
| 80 | |||
| 81 | # skip normal cytobands | ||
| 82 | return if $feature->attributes('stain') && !$is_cent; | ||
| 83 | |||
| 84 | # Set the bgcolor | ||
| 85 | unless ($is_cent || defined $score || defined $self->score_range ) { | ||
| 86 | my @rgb = @{$self->low_rgb}; | ||
| 87 | $bgcolor = $self->color_index(@rgb); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | my @rgb = $self->calculate_color($score); | ||
| 91 | $bgcolor = $self->color_index(@rgb); | ||
| 92 | } | ||
| 93 | |||
| 94 | # bgcolorindex must return true | ||
| 95 | $bgcolor ||= $self->adjust_bgcolor; | ||
| 96 | |||
| 97 | # Is this a centromere? | ||
| 98 | if ( $is_cent ) { | ||
| 99 | $fgcolor = $self->color_index(0,0,0); | ||
| 100 | |||
| 101 | if ( $self->panel->image_class =~ /SVG/ ) { | ||
| 102 | $bgcolor = $gd->colorResolve( 102, 102, 153 ); | ||
| 103 | $self->draw_centromere( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | $self->draw_centromere( $gd, $x1, $y1, $x2, $y2, gdTiled, $fgcolor ); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | # a telomere? | ||
| 110 | elsif ( $feature->start <= 1 ) { | ||
| 111 | # left (top) | ||
| 112 | my $status = 1 unless $self->panel->flip; | ||
| 113 | # if this is a full-length chromosome? | ||
| 114 | $status = -1 if $feature->stop >= $self->panel->end - 1000; | ||
| 115 | |||
| 116 | $self->draw_telomere( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor, | ||
| 117 | $arcradius, $status ); | ||
| 118 | } | ||
| 119 | elsif ( $feature->stop >= $self->panel->end - 1000 ) { | ||
| 120 | # right (bottom) | ||
| 121 | my $status = 1 if $self->panel->flip; | ||
| 122 | $self->draw_telomere( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor, | ||
| 123 | $arcradius, $status ); | ||
| 124 | } | ||
| 125 | # or a regular band? | ||
| 126 | else { | ||
| 127 | $self->draw_cytoband( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | # Nudge the color over just a bit if the color index | ||
| 133 | # is 0 (panel bgcolor). This overcomes default bgcolor | ||
| 134 | # and fgcolor when the index does not return true | ||
| 135 | sub adjust_bgcolor { | ||
| 136 | my $self = shift; | ||
| 137 | my $gd = $self->panel->gd; | ||
| 138 | my @rgb = $self->panel->rgb($self->panel->bgcolor); | ||
| 139 | |||
| 140 | for (@rgb) { | ||
| 141 | $_++ if $_ < 255; | ||
| 142 | $_-- if $_ == 255; | ||
| 143 | } | ||
| 144 | |||
| 145 | return $gd->colorResolve(@rgb); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub fgcolor { | ||
| 149 | my $self = shift; | ||
| 150 | my $clr = $self->option('fgcolor') || | ||
| 151 | $self->option('outline') || | ||
| 152 | 'black'; | ||
| 153 | return $self->panel->translate_color($clr); | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |