3 # BioPerl module for Bio::TreeIO::tabtree
5 # Cared for by Jason Stajich <jason@bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::TreeIO::tabtree - A simple output format which displays a tree as an ASCII drawing
20 my $in = Bio::TreeIO->new(-file => 'input', -format => 'newick');
21 my $out = Bio::TreeIO->new(-file => '>output', -format => 'tabtree');
23 while( my $tree = $in->next_tree ) {
24 $out->write_tree($tree);
29 This is a made up format just for outputting trees as an ASCII drawing.
35 User feedback is an integral part of the evolution of this and other
36 Bioperl modules. Send your comments and suggestions preferably to
37 the Bioperl mailing list. Your participation is much appreciated.
39 bioperl-l@bioperl.org - General discussion
40 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
44 Report bugs to the Bioperl bug tracking system to help us keep track
45 of the bugs and their resolution. Bug reports can be submitted via the
48 http://bugzilla.open-bio.org/
50 =head1 AUTHOR - Jason Stajich
52 Email jason@bioperl.org
56 The rest of the documentation details each of the object methods.
57 Internal methods are usually preceded with a _
62 # Let the code begin...
65 package Bio
::TreeIO
::tabtree
;
68 # Object preamble - inherits from Bio::Root::Root
72 use base
qw(Bio::TreeIO);
77 Usage : my $obj = Bio::TreeIO::tabtree->new();
78 Function: Builds a new Bio::TreeIO::tabtree object
79 Returns : Bio::TreeIO::tabtree
86 my($class,@args) = @_;
88 my $self = $class->SUPER::new
(@args);
95 Usage : $treeio->write_tree($tree);
96 Function: Write a tree out to data stream in newick/phylip format
98 Args : Bio::Tree::TreeI object
103 my ($self,$tree) = @_;
104 my $line = _write_tree_Helper
($tree->get_root_node,"");
105 $self->_print($line. "\n");
106 $self->flush if $self->_flush_on_write && defined $self->_fh;
110 sub _write_tree_Helper
{
111 my ($node,$indent) = @_;
112 return unless defined $node;
114 my @d = $node->each_Descendent();
116 my ($i,$lastchild) = (0,scalar @d - 1);
119 $line .= sprintf("%s| \n%s\\-%s\n",
120 $indent,$indent,$n->id || '');
122 $line .= sprintf("$indent| %s\n",( $n->id ?
123 sprintf("(%s)",$n->id) : ''));
125 my $new_indent = $indent . (($i == $lastchild) ?
"| " : " ");
127 # avoid the unlikely case of cycles
128 $line .= _write_tree_Helper
($n,$new_indent);
138 Function: Sorry not possible with this format
146 $_[0]->throw("Sorry the format 'tabtree' can only be used as an output format at this time");