| 1 | package Bio::Graphics::Glyph::pairplot; | ||
| 2 | |||
| 3 | # Triangle plot for showing pairwise quantitative relationships. | ||
| 4 | # Similar to pairwise_plot, which was originally distributed in GBrowse, | ||
| 5 | # except that the signal intensity is handled using a callback that takes | ||
| 6 | # the two features to be compared. | ||
| 7 | |||
| 8 | sub my_description { | ||
| 9 | return <<END; | ||
| 10 | This is a triangle plot that displays quantitative relationships between point-like features, | ||
| 11 | such as LD between SNPs. The quantitative information is generated by the "score" callback, | ||
| 12 | which receives two feature objects and returns a floating point number between "min_score" | ||
| 13 | and "max_score". The bgcolor intensity will be scaled from min to max like graded_segments. | ||
| 14 | |||
| 15 | NOTE (2 April 2008): This glyph is not yet fully functional. | ||
| 16 | END | ||
| 17 | } | ||
| 18 | |||
| 19 | sub my_options { | ||
| 20 | { | ||
| 21 | score => [ | ||
| 22 | 'CODEREF', | ||
| 23 | undef, | ||
| 24 | 'Pass a coderef to a subroutine with the following signature: sub ($$). The', | ||
| 25 | 'two arguments are the first and second feature to compare. Return a floating point', | ||
| 26 | 'number to indicate the score at the intersection of these two features.' | ||
| 27 | ], | ||
| 28 | min_score => [ | ||
| 29 | 'float', | ||
| 30 | 0.0, | ||
| 31 | 'Minimum possible pairwise score.' | ||
| 32 | ], | ||
| 33 | max_score => [ | ||
| 34 | 'float', | ||
| 35 | 1.0, | ||
| 36 | 'Maximum possible pairwise score.' | ||
| 37 | ], | ||
| 38 | angle => [ | ||
| 39 | 'float', | ||
| 40 | 45, | ||
| 41 | 'Angle between the side of the triangle and the base, in degrees.' | ||
| 42 | ], | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | use strict; | ||
| 49 | use Math::Trig; | ||
| 50 | |||
| 51 | use base 'Bio::Graphics::Glyph::generic'; | ||
| 52 | |||
| 53 | sub maxdepth { | ||
| 54 | my $self = shift; | ||
| 55 | my $md = $self->Bio::Graphics::Glyph::maxdepth; | ||
| 56 | return $md if defined $md; | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | # return angle in radians | ||
| 61 | sub angle { | ||
| 62 | my $self = shift; | ||
| 63 | my $angle = $self->{angle} ||= $self->option('angle') || 45; | ||
| 64 | $self->{angle} = shift if @_; | ||
| 65 | deg2rad($angle); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub slope { | ||
| 69 | my $self = shift; | ||
| 70 | return $self->{slope} if exists $self->{slope}; | ||
| 71 | return $self->{slope} = tan($self->angle); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub x2y { | ||
| 75 | my $self = shift; | ||
| 76 | shift() * $self->slope; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub intercept { | ||
| 80 | my $self = shift; | ||
| 81 | my ($x1,$x2) = @_; | ||
| 82 | my $mid = ($x1+$x2)/2; | ||
| 83 | my $y = $self->x2y($mid-$x1); | ||
| 84 | return (int($mid+0.5),int($y+0.5)); | ||
| 85 | } | ||
| 86 | |||
| 87 | # height calculated from width | ||
| 88 | sub layout_height { | ||
| 89 | my $self = shift; | ||
| 90 | return $self->{height} if exists $self->{height}; | ||
| 91 | return $self->{height} = $self->x2y($self->width)/2; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub min_score { | ||
| 95 | my $self = shift; | ||
| 96 | my $min = $self->option('min_score'); | ||
| 97 | return 0 unless defined $min; | ||
| 98 | } | ||
| 99 | sub max_score { | ||
| 100 | my $self = shift; | ||
| 101 | my $max = $self->option('max_score'); | ||
| 102 | return 1.0 unless defined $max; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub calculate_color { | ||
| 106 | my $self = shift; | ||
| 107 | my ($s,$rgb) = @_; | ||
| 108 | return $self->{colors}{$s} if exists $self->{colors}{$s}; | ||
| 109 | my $max_score = $self->max_score; | ||
| 110 | my $min_score = $self->min_score; | ||
| 111 | my $scale = 255/($max_score-$min_score); | ||
| 112 | my $value = ($s-$min_score) * $scale; # will range from 0 to 255 | ||
| 113 | return $self->{colors}{$s} = | ||
| 114 | $self->panel->translate_color(map { 255 - $value} @$rgb); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub draw { | ||
| 118 | my $self = shift; | ||
| 119 | my $gd = shift; | ||
| 120 | my ($left,$top,$partno,$total_parts) = @_; | ||
| 121 | my $fgcolor = $self->fgcolor; | ||
| 122 | |||
| 123 | my ($red,$green,$blue) = $self->panel->rgb($self->bgcolor); | ||
| 124 | |||
| 125 | my @points = $self->get_points(); | ||
| 126 | $gd->line($self->left+$left, $top+1, | ||
| 127 | $self->right+$left,$top+1, | ||
| 128 | $fgcolor); | ||
| 129 | |||
| 130 | my $points = $self->option('point'); | ||
| 131 | |||
| 132 | my @parts = sort {$a->left<=>$b->left} $self->parts; | ||
| 133 | $_->draw_component($gd,$left,$top-10) foreach @parts; | ||
| 134 | |||
| 135 | # assumption: parts are not overlapping | ||
| 136 | if ($points) { | ||
| 137 | @points = map { int (($parts[$_]->right+$parts[$_+1]->left)/2)} (0..$#parts-1); | ||
| 138 | unshift @points,int($parts[0]->left); | ||
| 139 | push @points,int($parts[-1]->right); | ||
| 140 | } | ||
| 141 | |||
| 142 | for (my $ia=0;$ia<@parts-1;$ia++) { | ||
| 143 | for (my $ib=$ia+1;$ib<@parts;$ib++) { | ||
| 144 | my ($l1,$r1,$l2,$r2); | ||
| 145 | if (@points) { | ||
| 146 | ($l1,$r1) = ($points[$ia]+1,$points[$ia+1]-1); | ||
| 147 | ($l2,$r2) = ($points[$ib]+1,$points[$ib+1]-1); | ||
| 148 | } else { | ||
| 149 | ($l1,$r1) = ($parts[$ia]->left,$parts[$ia]->right); | ||
| 150 | ($l2,$r2) = ($parts[$ib]->left,$parts[$ib]->right); | ||
| 151 | } | ||
| 152 | |||
| 153 | my $intensity = eval{$self->feature->pair_score($parts[$ia],$parts[$ib])}; | ||
| 154 | warn $@ if $@; | ||
| 155 | $intensity = 1.0 unless defined $intensity; | ||
| 156 | my $c = $self->calculate_color($intensity,[$red,$green,$blue]); | ||
| 157 | |||
| 158 | # left corner | ||
| 159 | my ($lcx,$lcy) = $self->intercept($l1,$l2); | ||
| 160 | my ($tcx,$tcy) = $self->intercept($r1,$l2); | ||
| 161 | my ($rcx,$rcy) = $self->intercept($r1,$r2); | ||
| 162 | my ($bcx,$bcy) = $self->intercept($l1,$r2); | ||
| 163 | my $poly = GD::Polygon->new(); | ||
| 164 | $poly->addPt($lcx+$left,$lcy+$top); | ||
| 165 | $poly->addPt($tcx+$left,$tcy+$top); | ||
| 166 | $poly->addPt($rcx+$left,$rcy+$top); | ||
| 167 | $poly->addPt($bcx+$left,$bcy+$top); | ||
| 168 | $gd->filledPolygon($poly,$c); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | sub get_points { | ||
| 174 | my $self = shift; | ||
| 175 | my @points; | ||
| 176 | my @parts = $self->parts; | ||
| 177 | return unless @parts; | ||
| 178 | |||
| 179 | for my $g (@parts) { | ||
| 180 | push @points,$g->left; | ||
| 181 | push @points,$g->right; | ||
| 182 | } | ||
| 183 | @points; | ||
| 184 | } | ||
| 185 | |||
| 186 | # never allow our internal parts to bump; | ||
| 187 | sub bump { 0 } | ||
| 188 | |||
| 189 | 1; | ||
| 190 | |||
| 191 | __END__ |