blib/lib/Bio/Graphics/Glyph/ternary_plot.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 143 CC 14 SCAR 23.6
CC
14
Cov
0%
draw_component
20.0
pad_left
12.0
pad_right
12.0
CRAP 10.5
1 package Bio::Graphics::Glyph::ternary_plot;
2 # Draw ternary (triangle plots)
3  
4 use strict;
5 use base qw(Bio::Graphics::Glyph::generic);
6 use Bio::Graphics::Glyph::xyplot;
7  
8 use constant Sin60 =>0.866025403784439;
9 use constant Tan60 =>1.73205080756888;
10  
11 sub calculate_side {
12 my $self = shift;
13 $self->option('height') / Sin60;
14 }
15  
16 # positioning this properly is a bit tricky, because if the side of the triangle
17 # is greater than the width of the feature, then we need to add extra left and right
18 # padding.
19 sub pad_left {
20 my $self = shift;
21 my $feature = $self->feature;
22 my $left = $self->SUPER::pad_left;
23  
24 my $side = $self->calculate_side;
25 my ($a,$b) = $self->map_pt($feature->start,$feature->stop);
26 my $width = abs($b-$a);
27 my $extra = $width > $side ? 0 : ($side-$width)/2;
28  
29 return $extra > $left ? $extra : $left;
30 }
31  
32 sub pad_right {
33 my $self = shift;
34 my $right = $self->SUPER::pad_right;
35 my $side = $self->calculate_side;
36 my $feature = $self->feature;
37 my ($a,$b) = $self->map_pt($feature->start,$feature->stop);
38 my $width = abs($b-$a);
39 my $extra = $width > $side ? 0 : ($side-$width)/2;
40 return $extra > $right ? $extra : $right;
41 }
42  
43 sub pad_top {
44 my $self = shift;
45 my $pad = $self->SUPER::pad_top;
46 return $pad unless $self->option('vertices');
47 my $font = $self->image_class->gdTinyFont();
48 my $lh = $font->height/2;
49 return $pad > $lh ? $pad : $lh;
50 }
51  
52 sub pad_bottom {
53 my $self = shift;
54 my $pad = $self->SUPER::pad_bottom;
55 return $pad unless $self->option('vertices');
56 my $font = $self->image_class->gdTinyFont();
57 my $lh = $font->height/2;
58 return $pad > $lh ? $pad : $lh;
59 }
60  
61 sub triples {
62 my $self = shift;
63 my $triples = $self->option('triples');
64 return $triples if defined $triples;
65 my @triples = $self->feature->get_tag_values('triples');
66 for my $t (@triples) {
67 next if ref $t && ref $t eq 'ARRAY'; # already in right format
68 $t = [split /[,\w]/,$t];
69 }
70 return \@triples;
71 }
72  
73 sub draw_component {
74 my $self = shift;
75 my $gd = shift;
76 my $fg = $self->fgcolor;
77 my $bg = $self->bgcolor;
78  
79 # position the left (A) edge
80 my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries(@_);
81 my $xmid = ($x1+$x2)/2;
82 my $side = $self->calculate_side;
83  
84 my $left = $xmid - $side/2;
85 my $right = $left + $side;
86 my $top = $y1;
87 my $bottom=$y2;
88  
89 # draw the triangle
90 my $poly_pkg = $self->polygon_package;
91 my $poly = $poly_pkg->new();
92 $poly->addPt($left,$bottom);
93 $poly->addPt($right,$bottom);
94 $poly->addPt($xmid,$top);
95 $gd->polygon($poly,$fg);
96  
97 # draw vertex labels, if any
98 my $fontcolor = $self->fontcolor;
99 my $font = $self->image_class->gdTinyFont();
100 my $lh = $font->height;
101 my $lw = $font->width;
102  
103 if (my $vertex_labels = $self->option('vertices')) {
104 my @labels = @$vertex_labels;
105 $gd->string($font,$left-$lw*length($labels[0]),$bottom+$lh/2,$labels[0],$fontcolor);
106 $gd->string($font,$right,$bottom+$lh/2,$labels[1],$fontcolor);
107 $gd->string($font,$xmid-$lw*length($labels[2])-3,$top-3,$labels[2],$fontcolor);
108 }
109  
110 # get triples
111 my $data = $self->triples;
112 for my $triple (@$data) {
113 my ($a,$b,$c,$color,$label) = @$triple;
114 $color = defined $color ? $self->factory->translate_color($color) : $bg;
115 my $x = $xmid + $side * ($b - $a)/2;
116 my $y = $bottom - $side * ($c * Tan60)/2;
117 draw_disc($gd,$x,$y,3,$color);
118 if ($label) {
119 $gd->string($font,$x+3,$y,$label,$fontcolor);
120 }
121 }
122 }
123  
124 sub draw_disc {
125 my ($gd,$x,$y,$pr,$color) = @_;
126 $gd->filledArc($x,$y,$pr,$pr,0,360,$color);
127 }
128  
129 1;
130  
131 __END__