2 # BioPerl module for Bio::TreeIO::svg-graph
4 # Cared for by Allen Day <allenday@ucla.edu>
6 # Copyright Brian O'Connor
8 # You may distribute this module under the same terms as perl itself
10 # POD documentation - main docs before the code
14 Bio::TreeIO::svggraph - A simple output format that converts a Tree object to an SVG output
19 my $in = Bio::TreeIO->new(-file => 'input', -format => 'newick');
20 my $out = Bio::TreeIO->new(-file => '>output', -format => 'svggraph');
22 while( my $tree = $in->next_tree ) {
23 my $svg_xml = $out->write_tree($tree);
28 This outputs a tree as an SVG graphic using the SVG::Graph API
34 User feedback is an integral part of the evolution of this and other
35 Bioperl modules. Send your comments and suggestions preferably to
36 the Bioperl mailing list. Your participation is much appreciated.
38 bioperl-l@bioperl.org - General discussion
39 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
43 Report bugs to the Bioperl bug tracking system to help us keep track
44 of the bugs and their resolution. Bug reports can be submitted via the
47 http://bugzilla.open-bio.org/
49 =head1 AUTHOR - Brian OConnor
51 Email brian.oconnor-at-excite.com
56 Guillaume Rousse, Guillaume-dot-Rousse-at-inria-dot-fr
60 The rest of the documentation details each of the object methods.
61 Internal methods are usually preceded with a _
66 # Let the code begin...
69 package Bio
::TreeIO
::svggraph
;
72 # Object preamble - inherits from Bio::Root::Root
76 use SVG
::Graph
::Data
::Tree
;
77 use SVG
::Graph
::Data
::Node
;
83 use base
qw(Bio::TreeIO);
88 Usage : my $obj = Bio::TreeIO::svggraph->new();
89 Function: Builds a new Bio::TreeIO::svggraph object
90 Returns : Bio::TreeIO::svggraph
91 Args :-width => image width (default 1600)
92 -height => image height (default 1000)
93 -margin => margin (default 30)
94 -stroke => stroke color (default 'black')
95 -stroke_width=> stroke width (default 2)
96 -font_size=> font size (default '10px')
97 -nomalize => undef or 'log' (default is undef)
103 my ($width,$height,$margin,$stroke,
104 $stroke_width,$font_size,
105 $normalize) = $self->_rearrange([qw
114 $self->{_width
} = $width || 1600;
115 $self->{_height
} = $height || 1000;
116 $self->{_margin
} = defined $margin ?
$margin : 30;
117 $self->{_stroke
} = $stroke || 'black';
118 $self->{_stroke_width
} = $stroke_width || 2;
119 $self->{_font_size
} = $font_size || '10px';
120 $self->{_normalize
} = $normalize || '';
121 $self->SUPER::_initialize
(@_);
127 Usage : $treeio->write_tree($tree);
128 Function: Write a tree out to data stream in newick/phylip format
130 Args : Bio::Tree::TreeI object
135 my ($self,$tree) = @_;
136 my $line = $self->_write_tree_Helper($tree->get_root_node);
137 $self->_print($line. "\n");
138 $self->flush if $self->_flush_on_write && defined $self->_fh;
142 sub _write_tree_Helper
{
143 my ($self,$node) = @_;
145 my $graph = SVG
::Graph
->new
146 ('width' => $self->{'_width'},
147 'height' => $self->{'_height'},
148 'margin' => $self->{'_margin'});
150 my $group0 = $graph->add_frame;
151 my $tree = SVG
::Graph
::Data
::Tree
->new;
152 my $root = SVG
::Graph
::Data
::Node
->new;
153 $root->name($node->id);
154 $self->_decorateRoot($root, $node->each_Descendent());
156 $group0->add_data($tree);
158 $group0->add_glyph('tree',
159 'stroke' =>$self->{'_stroke'},
160 'stroke-width'=>$self->{'_stroke_width'},
161 'font-size' =>$self->{'_font_size'});
163 return($graph->draw);
169 Title : _decorateRoot
170 Usage : internal methods
180 my ($self,$previousNode,@children) = @_;
181 for my $child (@children) {
182 my $currNode = SVG
::Graph
::Data
::Node
->new;
183 $currNode->branch_label($child->id);
184 my $length = $child->branch_length;
186 { # is this right? copies from Guillame
187 if ($self->{_normalize
} eq 'log') {
188 $length = log($length + 1);
192 $currNode->branch_length($length);
193 $previousNode->add_daughter($currNode);
194 $self->_decorateRoot($currNode, $child->each_Descendent());
203 Function: Sorry not possible with this format
211 $_[0]->throw("Sorry the format 'svggraph' can only be used as an output format");