[Bioperl-l] Bio::TreeIO - tree object to string

Sendu Bala bix at sendu.me.uk
Mon Mar 10 16:51:59 UTC 2008


Daniel Gerlach wrote:
> Dear all,
> 
> This is a very basic question. I have a tree object in $tree and want to 
> save its newick representation in a variable as a string:
> 
> my $out = new Bio::TreeIO(-fh => $tree_string, -format => 'newick');
> $out->write_tree($tree);
> print $tree_string;
> 
> Unfortunately this does not work and he prints out the newick tree on 
> stdout plus the message "Use of uninitialized value in print at ...". He 
> also prints out the tree on the stdout if I remove the line "print 
> $tree_string". The variable $tree_string seems to be empty.

The -fh argument is supposed to be a file handle, not a string. You can 
use whatever standard Perl method you like for attaching a filehandle to 
a scalar.

Eg.
my $tree_string = '';
open(my $fake_fh, "+<", \$tree_string);
my $out = new Bio::TreeIO(-fh => $fake_fh, -format => 'newick');
$out->write_tree($tree);
print $tree_string;

Alternatively, my $tree_string = $tree->simplify_to_leaves_string() 
might give you want you want.



More information about the Bioperl-l mailing list