This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Print Png Output Using Perl In Html

Hi friends,

I have written perl code to parse blast result and generating .png image for alignment on html page. The image can not be generated when i run the code in apache2 server. The error message is cannot write the file .png and hence not able to view image from the below code. Please if you have any suggestions so it will be greatful.

#!/usr/bin/perl

use Bio::Graphics;
use Bio::SeqFeature::Generic;

print "Content-type: text/html\n\n";

open(FH,"BR_results.txt") or die "Cannot open the blastfile\n";

# i need to show image in html but gives error cant write png file.
open(FW,">Data.png") or die "Cannot write the png file\n"; 

@BR_Query=();
@BR_Gene=();
@BR_Length=();
@BR_start=();
@BR_end=();
$count_hit=0;

while(<FH>)
{
chomp($_);

     if($_=~/^>.*/)
     {

           $count_hit++;

     }
     if($_=~/Query= (.*)/)
     {
        $Query=$1;
        chomp($Query);
        push(@BR_Query,$Query);

    }
    if($_=~/^>(.*)\s+\w+/)
    {
        $Gene=$1;
        push(@BR_Gene,$Gene);

    }
    if($_=~/          Length = (\d+)/)
    {
        $Length=$1; 
        push(@BR_Length,$Length);
    }
    if($_=~/Sbjct: (\d+)\s+.*\s+(\d+)/)
    {
        $start=$1; $end=$2;
        push(@BR_start,$start);
        push(@BR_end,$end);
    }

}

$len=@BR_Gene;

for($i=0;$i<$len;$i++)
{

        my $panel = Bio::Graphics::Panel->new( -length => $BR_Length[$i],
                                               -width  => 700,
                                               -pad_left => 10,
                                               -pad_right => 10,
                                              );

         my $full_length = Bio::SeqFeature::Generic->new(-start=>1,-end=> $BR_Length[$i]);
         $panel->add_track($full_length,     
                           -glyph   => 'arrow',   
                           -tick    => 2,
                           -bump => 0,
                           -fgcolor => 'black',
                           -double  => 2,
                            );

         $panel->add_track($full_length,     
                           -glyph   => 'generic',                           
                           -fgcolor => 'green',
                           -label   => $BR_Gene[$i],

                            );

        my $track = $panel->add_track(-glyph => 'graded_segments',
                                      -label  => 1,
                                      -bgcolor => 'red',
                                      -min_score => 0,
                                      -max_score => 1000);


        my $feature1 = Bio::SeqFeature::Generic->new(-display_name=> $BR_Query[$i],
                                                     -start       => $BR_start[$i],
                                                     -end         => $BR_end[$i] );


        $track->add_feature($feature1);
        # question how to print png image in html directly without saving to image file
        $x=$panel->png;  
        print FW "$x";


}

close FH;
close FW;

How can i show image generated using perl code as shown above. Thanks

bioperl perl graphs

Take a minute to clean up the layout of this post.

Please post the exact error messages you are receiving if you expect any help

Hello Rajesh!

We believe that this post does not fit the main topic of this site.

This post is old, and off-topic, and accruing comments, so I am closing it.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

3 answers

Can you verify whether your script process as unix file system to write the output file in the directory?

I see that open(FW,">Data.png") is writing the file in the current directory, try to prepend a folder (e.g. "/tmp/Data.png")

Hi Pacal,

Thanks for the reply. I modify the code as per your suggestion. But still geting error as "Cannot write the png file".

I am running the code in linux ubuntu. The code is placed in /home/www/cgi-bin/code.pl.

regards, rajesh

Pascal's answer is basically correct: if the file handle FW cannot be opened for writing, it's most likely because the script does not run with the appropriate permissions to write to /home/www/cgi-bin. But any process should be able to write to /tmp.

Hi pascal, thanks a lot. it worked fine

thank you very much

Hi pascal,

I am able to generate now Data.png file when i changed the /tmp/Data.png. But when i am accesing the file

print "<image src="\"/tmp/Data.png\"" border="\"1\"" hspace="\"23\""></image>";

the file Data.png couldnt show on the webpage.

regards, rajesh

OK. Good to know you manage the image. That's the first step. Using /tmp was just to discover whether it was related to a folder rights issue. Now you should generate your image where the apache is able to serve it to web clients (e.g. /var/www => check your apache configuration file).

Hi pascal,

many thanks. I am new to apache2 server so could you plz let me know how can i know configure and know which folder is for web clients.

thanks

You need to encode the PNG data in base64 and embed it in HTML like this:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAAFcCAYAAADYn0eRAAAABmJLR0QA/wD/AP+gvaeTAAAgAElE
..." />

Encoding is done like this:

MIME::Base64::encode_base64( $png_data );

Hi maasha,

could you please elaborate more

thanks

You don't need to do this at all; it's one possible solution as an alternative to writing out the PNG file.

PNG data can be encoded as a base64 string (long garbled text string) which the browsers will understand when instructed to in the tag using the src attribute beginning with "data:image/png;base64,<base64 string="">". Perl's MIME::Base64 will do the encoding for you. I find this a nice alternative to temporary files.

Hi maasha, many Thanks.

I am not able to incorporate code data:image/png;base64,<base64 string="">". Perl's MIME::Base64 in my code.

Could you please provide me steps where actually accordingly to my code. I mean could you please let me know where exactly i should put your code in my code.

Regards, Rajesh

Thanks to all......... I am able to display image by settings config file apache2.

thanks.

Rajesh,

It is old post, but still thought of asking what change exactly you did in httpd.conf file from apache

The issue I am facing is:

I have internal page "report.html" which also contains .png files in it. When accessing this web page "report.html" when using IE/Firefox/chrome then these .pngs also visible

But when I am trying to access this same web page "report.html" from apache server, then all content from this page is displaying properly but .png files just showing alt value - "Memory graph" instead of image

Below is code I used in HTML:

<img src=C:\CGI\htdocs\xyz\TOTAL.png alt="Memory graph"><hr></TD></TR>
<img src=C:\CGI\htdocs\xyz\DATA.png alt="Memory graph"><hr></TD></TR>
<img src=C:\CGI\htdocs\xyz\INFO.png alt="Memory graph"><hr></TD></TR>

Regs,
Mohan

Rajesh,

It is old post, but still thought of asking what change exactly you did in httpd.conf file from apache

The issue I am facing is:

I have internal page "report.html" which also contains .png files in it. When accessing this web page "report.html" when using IE/Firefox/chrome then these .pngs also visible

But when I am trying to access this same web page "report.html" from apache server, then all content from this page is displaying properly but .png files just showing alt value - "Memory graph" instead of image

Below is code I used in HTML:

<img src=C:\CGI\htdocs\xyz\TOTAL.png alt="Memory graph"><hr></TD></TR>
<img src=C:\CGI\htdocs\xyz\DATA.png alt="Memory graph"><hr></TD></TR>
<img src=C:\CGI\htdocs\xyz\INFO.png alt="Memory graph"><hr></TD></TR>

Regs,
Mohan

Log in to answer this question.