| 1 | package Bio::Phylo::Treedrawer::Swf; | ||
| 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::CONSTANT qw'looks_like_hash _PI_'; | ||
| 7 | use Bio::Phylo::Util::Dependency 'SWF::Builder'; | ||
| 8 | use Bio::Phylo::Util::Logger; | ||
| 9 | |||
| 10 | our $FONT = __PACKAGE__->_font_path; | ||
| 11 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 12 | my $PI = _PI_; | ||
| 13 | my %colors; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Bio::Phylo::Treedrawer::Swf - Graphics format writer used by treedrawer, no | ||
| 18 | serviceable parts inside | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This module creates a flash movie from a Bio::Phylo::Forest::DrawTree | ||
| 23 | object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to | ||
| 24 | learn how to create tree drawings. | ||
| 25 | |||
| 26 | |||
| 27 | =begin comment | ||
| 28 | |||
| 29 | Type : Constructor | ||
| 30 | Title : _new | ||
| 31 | Usage : my $swf = Bio::Phylo::Treedrawer::Swf->_new(%args); | ||
| 32 | Function: Initializes a Bio::Phylo::Treedrawer::Swf object. | ||
| 33 | Alias : | ||
| 34 | Returns : A Bio::Phylo::Treedrawer::Swf object. | ||
| 35 | Args : none. | ||
| 36 | |||
| 37 | =end comment | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub _new { | ||
| 42 | my $class = shift; | ||
| 43 | my %opt = looks_like_hash @_; | ||
| 44 | my $self = $class->SUPER::_new( | ||
| 45 | %opt, | ||
| 46 | '-api' => SWF::Builder->new( | ||
| 47 | 'FrameRate' => 15, | ||
| 48 | 'FrameSize' => | ||
| 49 | [ 0, 0, $opt{'-drawer'}->get_width, $opt{'-drawer'}->get_height ], | ||
| 50 | 'BackgroundColor' => 'ffffff' | ||
| 51 | ) | ||
| 52 | ); | ||
| 53 | return bless $self, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _finish { | ||
| 57 | $logger->debug("finishing drawing"); | ||
| 58 | my $self = shift; | ||
| 59 | require File::Temp; | ||
| 60 | my ( $fh, $filename ) = File::Temp::tempfile(); | ||
| 61 | $self->_api->save($filename); | ||
| 62 | my $result = do { local $/; <$fh> }; | ||
| 63 | unlink $filename; | ||
| 64 | return $result; | ||
| 65 | } | ||
| 66 | |||
| 67 | # -x1 => $x1, | ||
| 68 | # -x2 => $x2, | ||
| 69 | # -y1 => $y1, | ||
| 70 | # -y2 => $y2, | ||
| 71 | # -width => $width, | ||
| 72 | # -color => $color | ||
| 73 | sub _draw_curve { | ||
| 74 | $logger->debug("drawing curved branch"); | ||
| 75 | my $self = shift; | ||
| 76 | my %args = @_; | ||
| 77 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 78 | my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys}; | ||
| 79 | my ( $x2, $y2 ) = ( $x1, $y3 ); | ||
| 80 | return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' ) | ||
| 81 | ->moveto( $x1, $y1 )->curveto( $x1, $y1, $x1, $y1, $x2, $y2, $x3, $y3 ) | ||
| 82 | ->place; | ||
| 83 | } | ||
| 84 | |||
| 85 | =begin comment | ||
| 86 | |||
| 87 | # -x1 => $x1, | ||
| 88 | # -x2 => $x2, | ||
| 89 | # -y1 => $y1, | ||
| 90 | # -y2 => $y2, | ||
| 91 | # -radius => $radius | ||
| 92 | # -width => $width, | ||
| 93 | # -color => $color | ||
| 94 | |||
| 95 | =end comment | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub _draw_arc { | ||
| 100 | $logger->debug("darwing arc"); | ||
| 101 | my $self = shift; | ||
| 102 | my %args = @_; | ||
| 103 | my @keys = qw(-x1 -y1 -x2 -y2 -radius -width -color); | ||
| 104 | my ($x1, $y1, $x2, $y2, $radius, $width, $color) = @args{@keys}; | ||
| 105 | |||
| 106 | # get center of arc | ||
| 107 | my $drawer = $self->_drawer; | ||
| 108 | my $cx = $drawer->get_width / 2; | ||
| 109 | my $cy = $drawer->get_height / 2; | ||
| 110 | |||
| 111 | # compute start and end | ||
| 112 | my ( $r1, $start ) = $drawer->cartesian_to_polar( $x1 - $cx, $y1 - $cy ); | ||
| 113 | my ( $r2, $end ) = $drawer->cartesian_to_polar( $x2 - $cx, $y2 - $cy ); | ||
| 114 | $start += 360 if $start < 0; | ||
| 115 | $end += 360 if $end < 0; | ||
| 116 | $end -= $start; | ||
| 117 | |||
| 118 | my $shape = $self->_api->new_shape->linestyle( $width || 1, $color || '000000' ); | ||
| 119 | $shape->moveto( $x1, $y1 ); | ||
| 120 | $shape->arcto( $start, $end, $r1, $r2 ); | ||
| 121 | $shape->place; | ||
| 122 | } | ||
| 123 | |||
| 124 | # required: | ||
| 125 | # -x1 => $x1, | ||
| 126 | # -y1 => $y1, | ||
| 127 | # -x2 => $x2, | ||
| 128 | # -y2 => $y2, | ||
| 129 | # -x3 => $x3, | ||
| 130 | # -y3 => $y3, | ||
| 131 | # optional: | ||
| 132 | # -fill => $fill, | ||
| 133 | # -stroke => $stroke, | ||
| 134 | # -width => $width, | ||
| 135 | # -url => $url, | ||
| 136 | # -api => $api, | ||
| 137 | sub _draw_triangle { | ||
| 138 | my $self = shift; | ||
| 139 | $logger->debug("drawing triangle @_"); | ||
| 140 | my %args = @_; | ||
| 141 | my @keys = qw(-x1 -y1 -x2 -y2 -x3 -y3 -fill -stroke -width -url -api); | ||
| 142 | my ( $x1, $y1, $x2, $y2, $x3, $y3, $fill, $stroke, $width, $url, $api ) = | ||
| 143 | @args{@keys}; | ||
| 144 | return $self->_api->new_shape # red triangle. | ||
| 145 | ->fillstyle( $fill || 'ffffff' ) | ||
| 146 | ->linestyle( $width || 1, $stroke || '000000' ) | ||
| 147 | ->moveto( int $x1, int $y1 )->lineto( int $x2, int $y2 ) | ||
| 148 | ->lineto( int $x3, int $y3 )->lineto( int $x1, int $y1 )->place; | ||
| 149 | } | ||
| 150 | |||
| 151 | # -x1 => $x1, | ||
| 152 | # -x2 => $x2, | ||
| 153 | # -y1 => $y1, | ||
| 154 | # -y2 => $y2, | ||
| 155 | # -width => $width, | ||
| 156 | # -color => $color | ||
| 157 | sub _draw_line { | ||
| 158 | $logger->debug("drawing line"); | ||
| 159 | my $self = shift; | ||
| 160 | my %args = @_; | ||
| 161 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 162 | my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys}; | ||
| 163 | return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' ) | ||
| 164 | ->moveto( $x1, $y1 )->lineto( $x1, $y1, $x2, $y2 )->place; | ||
| 165 | } | ||
| 166 | |||
| 167 | # -x1 => $x1, | ||
| 168 | # -x2 => $x2, | ||
| 169 | # -y1 => $y1, | ||
| 170 | # -y2 => $y2, | ||
| 171 | # -width => $width, | ||
| 172 | # -color => $color | ||
| 173 | sub _draw_multi { | ||
| 174 | $logger->debug("drawing rectangular branch"); | ||
| 175 | my $self = shift; | ||
| 176 | my %args = @_; | ||
| 177 | my @keys = qw(-x1 -y1 -x2 -y2 -width -color); | ||
| 178 | my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys}; | ||
| 179 | my ( $x2, $y2 ) = ( $x1, $y3 ); | ||
| 180 | return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' ) | ||
| 181 | ->moveto( $x1, $y1 )->lineto( $x1, $y1, $x2, $y2, $x3, $y3 )->place; | ||
| 182 | } | ||
| 183 | |||
| 184 | # required: | ||
| 185 | # -x => $x, | ||
| 186 | # -y => $y, | ||
| 187 | # -text => $text, | ||
| 188 | # | ||
| 189 | # optional: | ||
| 190 | # -url => $url, | ||
| 191 | sub _draw_text { | ||
| 192 | my $self = shift; | ||
| 193 | if ( not $self->{'FONT'} ) { | ||
| 194 | $self->{'FONT'} = | ||
| 195 | $self->_api->new_font($Bio::Phylo::Treedrawer::Swf::FONT); | ||
| 196 | } | ||
| 197 | my %args = @_; | ||
| 198 | my ( $x, $y, $text, $url, $size ) = @args{qw(-x -y -text -url -size)}; | ||
| 199 | $logger->debug("drawing text $text"); | ||
| 200 | if ($url) { | ||
| 201 | $text = sprintf( '<a href="%s">%s</a>', $url, $text ); | ||
| 202 | } | ||
| 203 | my $textobj = | ||
| 204 | $self->_api->new_html_text->font( $self->{'FONT'} )->size( $size || 12 ) | ||
| 205 | ->text($text); | ||
| 206 | return $textobj->place->moveto( $x, $y ); | ||
| 207 | } | ||
| 208 | |||
| 209 | # -x => $x, | ||
| 210 | # -y => $y, | ||
| 211 | # -width => $width, | ||
| 212 | # -stroke => $color, | ||
| 213 | # -radius => $radius, | ||
| 214 | # -fill => $file, | ||
| 215 | # -api => $api, | ||
| 216 | # -url => $url, | ||
| 217 | sub _draw_circle { | ||
| 218 | $logger->debug("drawing circle"); | ||
| 219 | my $self = shift; | ||
| 220 | my %args = @_; | ||
| 221 | my @keys = qw(-x -y -width -stroke -radius -fill -api -url); | ||
| 222 | my ( $x, $y, $width, $stroke, $radius, $fill, $api, $url ) = @args{@keys}; | ||
| 223 | my $circle = | ||
| 224 | $self->_api->new_shape->fillstyle( $fill || '000000' ) | ||
| 225 | ->linestyle( $width || 1, $stroke || '000000' )->circle($radius); | ||
| 226 | return $circle->place->moveto( $x, $y ); | ||
| 227 | } | ||
| 228 | |||
| 229 | =head1 SEE ALSO | ||
| 230 | |||
| 231 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 232 | for any user or developer questions and discussions. | ||
| 233 | |||
| 234 | =over | ||
| 235 | |||
| 236 | =item L<Bio::Phylo::Treedrawer> | ||
| 237 | |||
| 238 | The SWF treedrawer is called by the L<Bio::Phylo::Treedrawer> object. Look there | ||
| 239 | to learn how to create tree drawings. | ||
| 240 | |||
| 241 | =item L<Bio::Phylo::Manual> | ||
| 242 | |||
| 243 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 244 | |||
| 245 | =back | ||
| 246 | |||
| 247 | =head1 CITATION | ||
| 248 | |||
| 249 | If you use Bio::Phylo in published research, please cite it: | ||
| 250 | |||
| 251 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 252 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 253 | I<BMC Bioinformatics> B<12>:63. | ||
| 254 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | 1; |