in reply to how to output the final Tree into a file?

You can open a file for writing and pass the file handle into Bio::TreeIO->new.

... my $outfile = '/path/to/file/to/create'; open my $fh, '>', $outfile or die "Cannot create $outfile, $!\n"; my $treeout = Bio::TreeIO->new(-format => 'newick', -fh => $fh); ...

Alternately, you can simply pass a filename to Bio::TreeIO->new.

... my $outfile = '/path/to/file/to/create'; my $treeout = Bio::TreeIO->new(-format => 'newick', -file => ">$outfil +e"); ...

Replies are listed 'Best First'.
Re^2: how to output the final Tree into a file?
by 1nickt (Canon) on Mar 16, 2016 at 00:40 UTC

    Hmm, I've not used it myself, but the docs seemed pretty clear that the file/filehandle is for reading, and that Bio::TreeIO doesn't have a print method, but relies on you to print the contents of the tree. From the DESCRIPTION:

    This is the driver module for Tree reading from data streams and flatf +iles. This is intended to be able to create Bio::Tree::TreeI objects.
    and the SYNOPSIS:
    { use Bio::TreeIO; my $treeio = Bio::TreeIO->new(-format => 'newick', -file => 'globin.dnd'); while( my $tree = $treeio->next_tree ) { print "Tree is ", $tree->number_nodes, "\n"; } }
    In this case the OP has created his tree with Bio::TreeIO and it's held in $treeout.

    The way forward always starts with a minimal test.
Re^2: how to output the final Tree into a file?
by dimitris852 (Acolyte) on Mar 15, 2016 at 23:46 UTC

    I did that

    my $outfile = 'C:\Users\Ioulia\Desktop\outree.tre'; my $treeout = Bio::TreeIO->new(-format => 'newick', -file => ">$outfil +e");

    But I get this: "my" variable $treeout masks earlier declaration in same scope at....line 50.

    Then I can normaly run the script but an empty file outree.tre is created.

    please excuse me but I m really new and it takes me a long time to understand many things

      please excuse me but I m really new and it takes me a long time to understand many things

      :) fragments which don't reproduce the error message, don't reproduce the error message -- you can't learn about the error message from those :)

      diagnostics/perldiag/splain

      perl -we" my $foo = 1; my $foo = 2; " "my" variable $foo masks earlier declaration in same scope at -e line +1. $ perl -Mdiagnostics -we" my $foo = 1; my $foo = 2; " "my" variable $foo masks earlier declaration in same scope at -e line +1 (#1) (W misc) A "my", "our" or "state" variable has been redeclared in +the current scope or statement, effectively eliminating all access to +the previous instance. This is almost always a typographical error. +Note that the earlier variable will still exist until the end of the sc +ope or until all closure referents to it are destroyed.
        ok i can understand that i have already declared $treeout before. But I think that all the information of the tree that i need is stored in $treeout and i cant use it. Just emptying $treeout with undef has no point at all.