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

Hello Monks, I am a newbie to the programming world and recently started a master in bioinformatics (must admit i am enjoying it alot but my background is in pharmaceutics so I know nothing of perl html etc)

I have only had a week of lessons so my knowledge is very, very limited and please take this into account when your laughing at my post/coding comments lol

I have produced a very basic program in perl which allows a user to enter a file, it extracts the DNA data, I format the data and then allow the user to input another file to format the data further before printing it out.

The last part of the assignment requires me to display this in a HTML format and allow the bases to be colour coordinated etc so here is my code:
#writing the results into an output HTML file. $outputfile = 'html_assignment.html'; open (OUTFILE, ">$outputfile"); print OUTFILE "<html> <head> <title> Applied Bioinformatics Assignment 1 </title> </body> </html>"; # user entry of DNA sequence prompt asking if DNA will be manually ent +ered or a file uploaded #manual input will allow a check to occur to ensure correct entry. print "To upload a file containing your DNA sequence press 1, or alter +nativly press 2 to manually enter your DNA sequence \n\n"; $usrinput = <STDIN>; chomp $usrinput;
Then the second part of my code to display my results is this:
#start the string from position 0 $revprotein3 = substr($dna,3); #Translate the DNA to protein $revprotein3 = dna2peptide($revprotein3); #Print the sequence in lines of 50 characters long print "\n\n<----------------Reverse compliment peptide frame 3-------- +--------> \n\n"; print_sequence ($revprotein3,50); # printing data sequences to HTML print OUTFILE "<table boarder = 0 width = 25> <tr><td> Peptide sequence frame 1</td></tr> <tr></tr> <tr><td> $protein </td></tr> <tr></tr> <tr><td> Peptide sequence frame 2 </td></tr> <tr></tr> <tr><td> $protein2 </td></tr> <tr></tr> <tr><td> Peptide sequence frame 3 </td></tr> <tr></tr> <tr><td> $protein3 </td></tr> <tr></tr> <tr><td> Peptide reverse compliment sequence Frame 1 </tr></td> <tr></tr> <tr><td> $revprotein </td></tr> <tr></tr> <tr><td> Peptide reverse compliment sequence Frame 2 </tr></td> <tr></tr> <tr><td> $revprotein2 </td></tr> <tr></tr> <tr><td> Peptide reverse compliment sequence Frame 2 </tr></td> <tr></tr> <tr><td> $revprotein3 </td></tr>";
The question's I have are:

As you will have noticed I have tried to limit the column table length hoping it will displayed onto another line (like you get in word) but it does not seem to be working can anyone help?

Second part I want each character to be displayed in a different colour can anyone provide some pointers?

Last question, is there a more efficient way to import stuff outinto HTML format, taking into consideration I am a newbie I need something that is easy to follow please

I dont want to be spoon fed but obviously alot of people reply back with their own interpution of code as an answer, if possible could you explain what is going on as its more important to me to learn then to copy and paste someone's answer or any links to tutorials or guides for beginners would be nice

thanks in advance :)

Replies are listed 'Best First'.
Re: PERL HTML HELP
by blue_cowdawg (Monsignor) on Oct 19, 2012 at 15:53 UTC

    Really this is less of a Perl question and more of an HTML question, but I'll bite.

    My favorite way to render HTML pages is using HTML::Template which allows me to create a set of template files with "placeholders" that I use later to populate real data into the rendered page. I won't go into a whole tutorial on that here, but that's one thing to look at.

    My second favorite is to use the CGI module. Example:

    #!/usr/bin/perl -w use strict; use CGI qw/:all/; print start_html,p("Hello world"),end_html;
    which produces:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <p>Hello world</p> </body> </html>
    If your trying to output to a file instead of STDOUT you'd do something like this:
    #!/usr/bin/perl -w use strict; use CGI qw/:all/; open FOUT,"> my_html_page.html" or die $!; print FOUT start_html,p("Hello world"),end_html;

    As far as each letter having a different color goes I'm not sure what you are after but if I want to change the color of some text it's going to look something like:

    #!/usr/bin/perl -w use strict; use CGI qw/:all/; print start_html,p({ style => "color: red;"},"Hello world"),end_html;
    and if you're using a CSS sheet and have a class or id set up for what you want you can invoke it with {id => "foo"} just as I did with the style invocation above.

    As far as keeping your columns even and forcing a wraparound within a cell goes, that is strictly and HTML issue. Here (from a Perl perspective) what that looks like:

    print table({width=>'80%',tr(td({width=>'30%',"thing1"), td({width=>'60%',"thing2"), td({width=>'10%',"a really long thing") ));
    The very right most column being the smallest and depending on the screen size should wrap nicely.

    There are other subtleties to explore in a combination of Perl-fu and HTML-fu and are beyond the scope of this thread.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      ah great info thank you, its funny the code seems so basic and logical but as a newbie i guess it takes a little time to grasp, but thank you very much for your help :)
Re: PERL HTML HELP
by flexvault (Monsignor) on Oct 19, 2012 at 15:50 UTC

    Welcome Singh121,

    First, I would suggest using the 3 parameter version of 'open'

    open ( my $OUTFILE, ">", $outputfile ) or die "! open $outputfile: $!\ +n";
    You'll find out 'why?' when you want to work with different types of character sets. And I know you have in you code: 'use strict; use warnings;' :-)

    As a help, I would test with border="1" so you can see how the table is formatted, and I would use a percentage for your width, ie width="90%". For color, check out the '<font size="+2" color="blue">' html command. And if you really want to have some fun with HTML, check out what you can do with CSS. (You may want to wait a little on that!)

    Experiment a little and just remember, you can learn something new about Perl each and every day.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

      Thank you for your help, i shall be messing around with this today :)
Re: PERL HTML HELP
by tobyink (Canon) on Oct 19, 2012 at 15:58 UTC

    So in my experience as a programmer, syntax highlighting is a very helpful tool to indicate whether you've made an error in your coding. Things like missed closing quotes (which are quite tiny glyphs so not easy to spot) are made very visible by unexpected changes in colour.

    Which brings me to my point. Colour-coded DNA! Syntax highlighting for the genome! If this helps stop DNA transcriptional errors, this could be the cure for cancer! :-)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      makes sense :)
      I like the idea of having my protein sequences and the restriction enzymes displayed in various colours and i have been messing around with this but have had no luck, this is what i have tried:
      sub color { my($colorkey) = @_; my $color = ''; my %color_key = ( A => "<font color=#99CCCC> A </font>", C => "<font color=#00CC00> C </font>", G => "<font color=#FF99CC> G </font>", T => "<font color=#33CC66> T </font>", R => "<font color=#996666> R </font>", Y => "<font color=#006699> Y </font>", M => "<font color=#33CC00> M </font>", K => "<font color=#99CC00> K </font>", S => "<font color=#000066> S </font>", W => "<font color=#990000> W </font>", B => "<font color=#0000FF> F </font>", D => "<font color=#FF9900> F </font>", H => "<font color=#9900CC> H </font>", V => "<font color=#99CC66> V </font>", N => "<font color=#33CCFF> N </font>", L => "<font color=#006633> L </font>", P => "<font color=#990066> P </font>", Q => "<font color=#996600> Q </font>", I => "<font color=#9966CC> I </font>", E => "<font color=#FF9966> E </font>", _ => "<font color=#000000> _ </font>", ); # Translate each character in the sequence coloured character for ( my $i = 0 ; $i < length($colorkey) ; ++$i ) { $color .= $color_key{substr($colorkey, $i, 1)}; } return }
      it was a long shot but obviously did not work...trail and error eh lol either way i was wondering how would one get a key or something to store an individual amino acid as a different colour. I am assuming because the amino acid in my sequence is displayed in a string (here is my code for that):
      print "please enter the start point of your DNA sequence\n\n"; $dnastart = <STDIN>; chomp $dnastart; print "please enter the end point of your DNA sequence\n\n"; $dnaend = <STDIN>; chomp $dnaend; $dna = substr($dna, $dnastart, ($dnaend - $dnastart)); print"Here is your selected DNA sequence:\n\n $dna\n\n"; } #start the string from position 0 $protein = substr($dna,0); #Translate the DNA to protein $protein = dna2peptide($protein); #Print the sequence in lines of 50 characters long print"\n\n<----------------Peptide frame 1----------------> \n\n"; print_sequence ($protein,50); # printing data sequences to HTML print OUTFILE "<table border=1 width=50% bgcolor=#CCFFFF> <tr><td><b> Peptide sequence frame 1</td></tr></b> <tr></tr> <tr><td> $protein </td></tr> </table>";
      i would need some sort of code which does a substitution for each letter into color, which is what i was trying to accomplish with my horrible attempt above lol
        Your html still has some problems, but some are easily fixed by simply revising the quoting. Some may frown, and with reason, at this use of a 'heredoc' but it "just works" for a one-off:
        #!/usr/bin/perl use 5.012; my $protein = "chuck steak"; my $heredoc = <<END <table border="1" width="50%" bgcolor="#CCFFFF"> <tr><td><b> Peptide sequence frame 1</b></td></tr>\n <tr><td>$protein </td> <td>also 'walnuts'</td></tr> </table> END ; print $heredoc;

        OUTPUT:

        <table border="1" width="50%" bgcolor="#CCFFFF"> <tr><td><b> Peptide sequence frame 1</b></td></tr> <tr><td>chuck steak </td> <td>also 'walnuts'</td></tr> </table>

        Note the newline at the end of line 09; your empty tr></tr> will provoke complaints from some validators.

        Better, however, to devote a couple hours to familiarizing yourself with CSS1 and 2. And better yet to follow the broad guidance provided by blue_cowdawg ( above ) and to use such CSS constructs as "max-width: nn%" to assure that the wraps work (...err, semi-work.....) based on the user's current screen width.

WHAT YOU SAY?
by LanX (Saint) on Oct 19, 2012 at 15:58 UTC
    SCNR! ;)

    Cheers Rolf