Dear PerlMonks

I am trying to selectively color a sequence of 100 characters and writing a html file.I have written the code and but its somewhat too many loops and is giving some errors.

I was wondering if I get some suggestions in troubleshooting and reducing the code

.

Error: In the output, position number 11 (T). I am also trying to color the last 10 characters as blue instead of red.

Any help will be greatly appreciated.
Regards

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; use Data::Dumper; my $sequence="GGCGCAACGCTGAGCAGCTGGCGCGTCCCGCGCGGCCCCAGTTCTGCGCAGCTTCC +CGAGGCTCCGCACCAGCCGCGCTTCTGTCCGCCTGCAGGGCATT"; ############ Make array of rejected length ############### my $seqlen = length ($sequence); my $input = "fragments.txt"; open(K,">","temp_hash.txt"); open( my $infile, "<", $input ) || die "Check the $input $!\n"; my $old = 0; while ( my $line = <$infile> ){ my $gap = $line - $old; if ($gap > 2){ my $start = $old+1; my $end = $line-1; print K "$start\t$end\n"; } elsif ($gap == 2){ my $start = $old+1; print K "$start\t$start\n"; } $old = $line; } close ($infile); my $line = $seqlen+1; my $gap = $line - $old; if ($gap > 2){ my $start = $old+1; my $end = $line-1; print K "$start\t$end\n"; } elsif ($gap == 2){ my $start = $old+1; print K "$start\t$start\n"; } close (K); close ($infile); ####################### Make a Hash ######################## my $tempfile = "temp_hash.txt"; open(my $file, "<", $tempfile) or die "Check the file $!"; my $cnt = 1;my %split; while (my $line = <$file>){ chomp $line; if ($line =~/(\S+)\s+(\S+)/){ for (my $s=$1;$s<=$2;$s++){ push @{$split{$cnt}}, $s; } } $cnt++; } close ($file); ############# Write HTML file ############################# #my $header; open(AA, ">fragments.html") or die $!; print AA "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/ +EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd\">\n"; print AA "<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" lang=\"en\" + xml:lang=\"en\">\n"; print AA "<head>\n"; print AA "<title> Colored Gene Walk</title> <meta http-equiv=\"content +-type\" content=\"text/html;charset=utf-8\"/>\n"; print AA "</head>\n"; print AA "<body>\n"; print AA "<pre class=\"monofont\">\n"; print AA "<a style=\"font-size: 12pt\">\n"; #print AA "$header\n"; for (my $pos=1;$pos<=length($sequence);$pos++){ my $FLAG=0;my @temp=(); foreach my $k(sort {$a <=> $b} keys %split){ my @pos=@{$split{$k}}; my $p1 =$pos[0]; my $p2 =$pos[$#pos]; if($pos == $p1){ push (@temp, "<span style=\"color:red\">"); for(my $p=$p1;$p<=$p2;$p++){ my $s=substr($sequence,$p-1,1); push (@temp,$s); if($p==int($p/50)*50){ push (@temp,"<br />"); } $FLAG=1; $pos++; } push (@temp, "</span>"); } } if($FLAG ==0){ my $s=substr($sequence,$pos-1,1); print AA "$s"; } else{ printf AA join("",@temp); } if($pos==int($pos/50)*50){ printf AA "<br />"; } } print AA "\n"; print AA "</a>\n"; print AA "</pre>\n"; print AA "</body>\n"; print AA "</html>\n"; close (AA);
fragments.txt 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 51 52 53 54 55 56 57 58 59 60 86 87 88 89 90 91 92 93 94 95

In reply to Code optimization help and troubleshooting by newtoperlprog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.