Simplify some code
[bioperl-live.git] / Bio / TreeIO / tabtree.pm
blob96f59664394009b51f7bae33826990a20d967b56
2 # BioPerl module for Bio::TreeIO::tabtree
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason@bioperl.org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::TreeIO::tabtree - A simple output format which displays a tree as an ASCII drawing
18 =head1 SYNOPSIS
20 use Bio::TreeIO;
21 my $in = Bio::TreeIO->new(-file => 'input', -format => 'newick');
22 my $out = Bio::TreeIO->new(-file => '>output', -format => 'tabtree');
24 while( my $tree = $in->next_tree ) {
25 $out->write_tree($tree);
28 =head1 DESCRIPTION
30 This is a made up format just for outputting trees as an ASCII drawing.
32 =head1 FEEDBACK
34 =head2 Mailing Lists
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to
38 the Bioperl mailing list. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
43 =head2 Support
45 Please direct usage questions or support issues to the mailing list:
47 I<bioperl-l@bioperl.org>
49 rather than to the module maintainer directly. Many experienced and
50 reponsive experts will be able look at the problem and quickly
51 address it. Please include a thorough description of the problem
52 with code and data examples if at all possible.
54 =head2 Reporting Bugs
56 Report bugs to the Bioperl bug tracking system to help us keep track
57 of the bugs and their resolution. Bug reports can be submitted via the
58 web:
60 https://github.com/bioperl/bioperl-live/issues
62 =head1 AUTHOR - Jason Stajich
64 Email jason@bioperl.org
66 =head1 APPENDIX
68 The rest of the documentation details each of the object methods.
69 Internal methods are usually preceded with a _
71 =cut
74 # Let the code begin...
77 package Bio::TreeIO::tabtree;
78 use strict;
80 # Object preamble - inherits from Bio::Root::Root
84 use base qw(Bio::TreeIO);
86 =head2 new
88 Title : new
89 Usage : my $obj = Bio::TreeIO::tabtree->new();
90 Function: Builds a new Bio::TreeIO::tabtree object
91 Returns : Bio::TreeIO::tabtree
92 Args :
95 =cut
97 sub new {
98 my($class,@args) = @_;
100 my $self = $class->SUPER::new(@args);
104 =head2 write_tree
106 Title : write_tree
107 Usage : $treeio->write_tree($tree);
108 Function: Write a tree out to data stream in newick/phylip format
109 Returns : none
110 Args : Bio::Tree::TreeI object
112 =cut
114 sub write_tree{
115 my ($self,$tree) = @_;
116 my $line = _write_tree_Helper($tree->get_root_node,"");
117 $self->_print($line. "\n");
118 $self->flush if $self->_flush_on_write && defined $self->_fh;
119 return;
122 sub _write_tree_Helper {
123 my ($node,$indent) = @_;
124 return unless defined $node;
126 my @d = $node->each_Descendent();
127 my $line = "";
128 my ($i,$lastchild) = (0,scalar @d - 1);
129 for my $n ( @d ) {
130 if( $n->is_Leaf ) {
131 $line .= sprintf("%s| \n%s\\-%s\n",
132 $indent,$indent,$n->id || '');
133 } else {
134 $line .= sprintf("$indent| %s\n",( $n->id ?
135 sprintf("(%s)",$n->id) : ''));
137 my $new_indent = $indent . (($i == $lastchild) ? "| " : " ");
138 if( $n != $node ) {
139 # avoid the unlikely case of cycles
140 $line .= _write_tree_Helper($n,$new_indent);
143 return $line;
146 =head2 next_tree
148 Title : next_tree
149 Usage :
150 Function: Sorry not possible with this format
151 Returns : none
152 Args : none
155 =cut
157 sub next_tree{
158 $_[0]->throw("Sorry the format 'tabtree' can only be used as an output format at this time");