perltreebioinformaticsscientific-computingbioperl

Converting PHYLIP text file to a PNG image


Does anyone know how I can convert a phylip tree format file in text format to a PNG image programmically in perl?

Tree file:

(  
B:6.0,  
(  
A:5.0,  
C:3.0,  
E:4.0)  
:5.0,  
D:11.0);    

I have tried using the Bio::Tree::Tree module but it writes incorrect nodes into an SVG file.


Solution

  • This code produces SVG output on STDOUT (I'm not qualified to comment on whether the correct nodes are displayed):

    use v5.16;     # sets strict and warnings
    use Bio::Phylo::Forest;
    use Bio::Phylo::Treedrawer;
    
    my $string = '(B:6.0,(A:5.0,C:3.0,E:4.0):5.0,D:11.0);';   
    
    my $forest 
      = Bio::Phylo::IO->parse( -format => 'newick', -string => $string )->first;
    
    my $treedrawer = Bio::Phylo::Treedrawer->new(
               -width  => 800,
               -height => 600,
               -shape  => 'CURVY', # curvogram
               -mode   => 'PHYLO', # phylogram
               -format => 'SVG'
    );
    
    $treedrawer->set_scale_options(
               -width => '100%',
               -major => '10%', # major cross hatch interval
               -minor => '2%',  # minor cross hatch interval
               -label => 'MYA',
    );
    
    $treedrawer->set_tree($forest);
    $treedrawer->set_format('Svg');
    print $treedrawer->draw;
    

    To produce a PNG image I used the script to direct the SVG output to a file and converted it to PNG with ImageMagick's convert utility. It looks like this:

    <code>Bio::Phylo::Treedrawer</code> output

    There are facilities for directly creating PNG images with Bio::Phylo::Treedrawer as well. To have Bio::Phylo::Treedrawer output PNG images you can modify the object's properties using: $treedrawer->set_format('Png');

    However on my system I get a warning when using the PNG image format:

    WARN Bio::Phylo::Treedrawer::Abstract::_draw_legend [Bio/Phylo/Treedrawer/Abstract.pm: 106] - Bio::Phylo::Treedrawer::Png can't draw a legend. I don't think this is a bug. The message would probably be better put as:

    Bio::Phylo::Treedrawer::Png does not support drawing a legend

    If the SVG output from Bio::Phylo::Treedrawer is preferable to that of Bio::Tree::Tree then you may want to just use that as your data format. You can convert your output file with either an image editor or a perl module that can convert your SVG data to PNG as part of the script.