| 1 | package Bio::Phylo::Treedrawer::Canvas; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Treedrawer::Abstract'; | ||
| 5 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 6 | use Bio::Phylo::Util::Logger ':levels'; | ||
| 7 | use Bio::Phylo::Util::CONSTANT '_PI_'; | ||
| 8 | |||
| 9 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 10 | my $PI = _PI_; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Bio::Phylo::Treedrawer::Canvas - Graphics format writer used by treedrawer, no | ||
| 15 | serviceable parts inside | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This module creates an HTML5 canvas graphic from a Bio::Phylo::Forest::DrawTree | ||
| 20 | object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to | ||
| 21 | learn how to create tree drawings. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | sub _new { | ||
| 26 | my $class = shift; | ||
| 27 | my %args = @_; | ||
| 28 | my $tmpl = do { local $/; <DATA> }; | ||
| 29 | my $canvas = sprintf( $tmpl, | ||
| 30 | 'myCanvas', | ||
| 31 | $args{'-drawer'}->get_width, | ||
| 32 | $args{'-drawer'}->get_height, 'myCanvas' ); | ||
| 33 | my $self = $class->SUPER::_new( %args, '-api' => \$canvas ); | ||
| 34 | return bless $self, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _finish { | ||
| 38 | my $self = shift; | ||
| 39 | my $api = $self->_api; | ||
| 40 | my $shape = $self->_drawer->get_shape; | ||
| 41 | if ( $shape =~ /^curvy/i ) { | ||
| 42 | $$api .= "drawCurvedTree(branches);\n"; | ||
| 43 | } | ||
| 44 | elsif ( $shape =~ /^rect/i ) { | ||
| 45 | $$api .= "drawRectangularTree(branches);\n"; | ||
| 46 | } | ||
| 47 | elsif ( $shape =~ /^diag/i ) { | ||
| 48 | $$api .= "drawDiagonalTree(branches);\n"; | ||
| 49 | } | ||
| 50 | elsif( $shape =~ /^radial/i ) { | ||
| 51 | $$api .= "drawRadialTree(branches);\n"; | ||
| 52 | } | ||
| 53 | $$api .= '</script>'; | ||
| 54 | return $$api; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _draw_text { | ||
| 58 | my $self = shift; | ||
| 59 | my %args = @_; | ||
| 60 | my ($x,$y,$text,$url,$rotation) = @args{qw(-x -y -text -url -rotation)}; | ||
| 61 | my $api = $self->_api; | ||
| 62 | $$api .= "drawText(ctx,$x,$y,'$text',0);\n"; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _draw_circle { | ||
| 66 | my $self = shift; | ||
| 67 | my %args = @_; | ||
| 68 | my ( $x, $y, $radius, $width, $stroke, $fill, $api, $url ) = | ||
| 69 | @args{qw(-x -y -radius -width -stroke -fill -api -url)}; | ||
| 70 | if ($radius) { | ||
| 71 | my $api = $self->_api; | ||
| 72 | $$api .= "drawCircle(ctx,$x,$y,$radius);\n"; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _draw_line { | ||
| 77 | my $self = shift; | ||
| 78 | my %args = @_; | ||
| 79 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color ); | ||
| 80 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 81 | my $api = $self->_api; | ||
| 82 | $$api .= "drawLine(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _draw_curve { | ||
| 86 | my $self = shift; | ||
| 87 | my %args = @_; | ||
| 88 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 89 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 90 | my $api = $self->_api; | ||
| 91 | $$api .= "drawCurve(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _draw_multi { | ||
| 95 | my $self = shift; | ||
| 96 | my %args = @_; | ||
| 97 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 98 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 99 | my $api = $self->_api; | ||
| 100 | $$api .= "drawMulti(ctx,$x1,$y1,$x2,$y2);\n"; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _draw_triangle { | ||
| 104 | my $self = shift; | ||
| 105 | my %args = @_; | ||
| 106 | my @coord = qw(-x1 -y1 -x2 -y2 -x3 -y3); | ||
| 107 | my ( $x1, $y1, $x2, $y2, $x3, $y3 ) = @args{@coord}; | ||
| 108 | my @optional = qw(-fill -stroke -width -url -api); | ||
| 109 | my $fill = $args{'-fill'} || 'white'; | ||
| 110 | my $stroke = $args{'-stroke'} || 'black'; | ||
| 111 | my $width = $args{'-width'} || 1; | ||
| 112 | my $api = $self->_api; | ||
| 113 | $$api .= "drawTriangle(ctx,$x1,$y1,$x2,$y2,$x3,$y3);\n"; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _draw_branch { | ||
| 117 | my ( $self, $node ) = @_; | ||
| 118 | $logger->info( "Drawing branch for " . $node->get_internal_name ); | ||
| 119 | if ( my $parent = $node->get_parent ) { | ||
| 120 | my ( $x1, $x2 ) = ( int $parent->get_x, int $node->get_x ); | ||
| 121 | my ( $y1, $y2 ) = ( int $parent->get_y, int $node->get_y ); | ||
| 122 | my $width = $self->_drawer->get_branch_width($node); | ||
| 123 | my $shape = $self->_drawer->get_shape; | ||
| 124 | my $drawer = '_draw_curve'; | ||
| 125 | if ( $shape =~ m/CURVY/i ) { | ||
| 126 | $drawer = '_draw_curve'; | ||
| 127 | } | ||
| 128 | elsif ( $shape =~ m/RECT/i ) { | ||
| 129 | $drawer = '_draw_multi'; | ||
| 130 | } | ||
| 131 | elsif ( $shape =~ m/DIAG/i ) { | ||
| 132 | $drawer = '_draw_line'; | ||
| 133 | } | ||
| 134 | elsif ( $shape =~ m/RADIAL/i ) { | ||
| 135 | $drawer = '_draw_arc'; | ||
| 136 | } | ||
| 137 | my $api = $self->_api; | ||
| 138 | $$api .= "branches.push({x1:$x1,y1:$y1,x2:$x2,y2:$y2});\n"; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 SEE ALSO | ||
| 143 | |||
| 144 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 145 | for any user or developer questions and discussions. | ||
| 146 | |||
| 147 | =over | ||
| 148 | |||
| 149 | =item L<Bio::Phylo::Treedrawer> | ||
| 150 | |||
| 151 | The canvas treedrawer is called by the L<Bio::Phylo::Treedrawer> object. Look | ||
| 152 | there to learn how to create tree drawings. | ||
| 153 | |||
| 154 | =item L<Bio::Phylo::Manual> | ||
| 155 | |||
| 156 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 157 | |||
| 158 | =back | ||
| 159 | |||
| 160 | =head1 CITATION | ||
| 161 | |||
| 162 | If you use Bio::Phylo in published research, please cite it: | ||
| 163 | |||
| 164 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 165 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 166 | I<BMC Bioinformatics> B<12>:63. | ||
| 167 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | 1; | ||
| 172 | __DATA__ |