Dear AnomalousMonk and Athanasius

Thank you for your suggestions and comments, I have incorporated suggestions and it really shortened the code.

Could I get some help in the debugging, somehow, in the sequence after 1-10 character the 11th character is getting truncated. I checked the code for substr and couldn't figure out why this is happening.

and I am trying to color the last 10 (91-100) characters in blue while reading in for loop.

Again thank you for your help.

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; use Data::Dumper; my $sequence="GGCGCAACGCTGAGCAGCTGGCGCGTCCCGCGCGGCCCCAGTTCTGCGCAGCTTCC +CGAGGCTCCGCACCAGCCGCGCTTCTGTCCGCCTGCAGGGCATT"; ############ Make hash of rejected length ############### my $input = "fragments.txt"; my %split; open (my $infile, "<", $input) or die "Cannot open file '$input' for r +eading: $!"; build_hash($_) while <$infile>; close ($infile) or die "Cannot close file '$input': $!"; build_hash(length($sequence) + 1); sub build_hash{ use feature 'state'; state $old = 0; state $cnt = 1; my ($line) = @_; my $gap = $line - $old; my $start = $old + 1; my $end = ($gap > 2) ? $line - 1 : $start; $split{$cnt++} = [$start .. $end] if $gap >= 2; $old = $line; } ############# Write HTML file ############################# #my $header; open (AA, ">fragments.html") or die "Cannot open file:$!"; print AA <<"EndOfHTML"; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" +> <head> <title> Colored Gene Walk</title> <meta http-equiv="content-type" +content="text/html;charset=utf-8"/> </head> <body> <pre class="monofont"> <a style="font-size: 12pt"> EndOfHTML #print AA "$header\n"; my $red = '<span style="color:red">'; my $span = '</span>'; my $brk = qq{<br />\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, $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, $brk); } $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 "$brk"; } } print AA <<"EndOfHTML"; </a> </pre> </body> </html> EndOfHTML close (AA);

In reply to Re: Code optimization help and troubleshooting by newtoperlprog
in thread 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.