dimitris852 has asked for the wisdom of the Perl Monks concerning the following question:

After executing the script, a Tree in newick form is generated (form a fasta file given). How can I output this result into a file and not just leave it in my command promt? Thanks!

#!/usr/bin/perl $ENV{CLUSTALDIR} = 'C:\\\\\\\\\\\\\\\\\\\\; use Bio::Tools::Run::Alignment::Clustalw; my $file = <>; # Get file name (fasta file) from command prompt. # Build a clustalw alignment factory my $factory = Bio::Tools::Run::Alignment::Clustalw->new(-matrix => 'BL +OSUM',-outfile=>'out', -output =>'clustalw'); my $ktuple = 2; $factory->ktuple($ktuple); my $inseq = Bio::SeqIO->new( -file=> "<$file", -format=> 'fasta' ); my $seq; my @seq_array; while ($seq = $inseq->next_seq) { push(@seq_array, $seq); } # Now we do the actual alignment. my $seq_array_ref = \@seq_array; my $aln = $factory->align($seq_array_ref); #constarcting the Tree my $alnio = Bio::AlignIO->new(-file => 'out', -format=>'clustalw'); my $dfactory = Bio::Tree::DistanceFactory->new(-method => 'NJ'); my $stats = Bio::Align::ProteinStatistics->new; my $treeout = Bio::TreeIO->new(-format => 'newick'); while( my $aln = $alnio->next_aln ) { my $mat = $stats->distance(-method => 'Kimura', -align => $aln); my $tree = $dfactory->make_tree($mat); $treeout->write_tree($tree); } exit;

Replies are listed 'Best First'.
Re: how to output the final Tree into a file?
by Mr. Muskrat (Canon) on Mar 15, 2016 at 22:27 UTC

    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"); ...

      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.

      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.
Re: how to output the final Tree into a file?
by 1nickt (Canon) on Mar 15, 2016 at 21:23 UTC

    Based on http://www.bioperl.org/wiki/HOWTO:Trees#Reading_and_Writing_Trees, it seems you need something like:

    open my $fh, '>', 'filename' or die $!; while ( my $tree = $treeout->next_tree ) { for my $node ( $tree->get_nodes ) { printf $fh "id: %s bootstrap: %s\n", $node->id || '', $node->b +ootstrap || '', "\n"; } } close $fh or die $!;

    Hope this helps!


    Edit: fix typo
    The way forward always starts with a minimal test.

      hi!.i thinks this codes is just for nodes and bootstrap values.i cant use it to print the tree in newick form into a file.it just reads a file with newick form.