cool has asked for the wisdom of the Perl Monks concerning the following question:
Below is the code that generates jpg image using GD package. This is written by some previous student of my lab. Pl pardon me as this code is without any '-w' and 'use strict'. I want to use this for my analysis. I want to write it properly but got stuck in the sub part. How the while in the subroutine is working? I am not getting how the while loop is executing for complete arrays and how subrouteing is just graph() without passing any value. I really had a tough time to upgrade the code to use 'use strict'. So, can any monk would suggest me any better way to write this code that is more robust, with use strict'. Thank you for taking out time to read it.
Input files are also there with the code.
#!/usr/bin/perl use GD; #use strict; my @x1; my @y1; my $max1; my $max2; my $min1; my $min2; my $i; open(FILE1,"fwd.stb"); while(<FILE1>) { my @value = split(/\s+/,$_); $x1[$i] = $value[0]-7; $y1[$i] = $value[1]; if($i != 0 && $y1[$i] > $max1) { $max1 = $y1[$i]; } if($i != 0 && $y1[$i] < $min1) { $min1 = $y1[$i]; } if($i == 0) { $min1 = $max1 = $y1[$i]; } $i++; } my $lines = $i; # Define constants my $X1start = 50+20; # X-axis my $Y1start = 300; # X-axis my $X1end = 450+$lines-400; # X-axis my $Y1end = 300; # X-axis my $X2start = 50+20; my $Y2start = 100; my $X2end = 50+20; my $Y2end = 300; # create a new image my $im = new GD::Image($X1end+50,$Y2end+100); # allocate some colors my $white = $im->colorAllocate(255,255,255); my $green = $im->colorAllocate(0,255,0); my $black = $im->colorAllocate(0,0,0); my $red = $im->colorAllocate(255,0,0); my $blue = $im->colorAllocate(0,0,255); # set the caption for the graph $im->string(gdLargeFont,int($lines/2)-50,20,"ENERGY GRAPH",$black) +; # draw the y-axis & x-axis $im->line($X2start,$Y2start,$X2end,$Y2end,$black); $im->line($X1start,$Y1start,$X1end,$Y1end,$black); # set the caption for x-axis and y-axis $im->string(gdLargeFont,int($lines/2)-50,335,"NUCLEOTIDE POSITION" +,$black); $im->stringUp(gdLargeFont,5,260,"FREE ENERGY(delta G)",$black); $im->string(gdSmallFont,int($lines/2)+100,20," Genomic Sequence - +------",$red); $im->string(gdSmallFont,int($lines/2)+100,40," Shuffled Sequence - +------",$blue); open(FILE2,"total_shuffle.stb"); $i = 0; my @x2; my @Y2; my $min; my $max; while(<FILE2>) { my @value = split(/\s+/,$_); $x2[$i] = $value[0]-7; $y2[$i] = $value[1]; if($i != 0 && $y2[$i] > $max2)if($i != 0 && $y2[$i] > $max2) { $max2 = $y2[$i]; } if($i != 0 && $y2[$i] < $min2) { $min2 = $y2[$i]; } if($i == 0) { $min2 = $max2 = $y2[$i]; } $i++; } if($min1 <= $min2) { $min = $min1; } else { $min = $min2; } if($max1 >= $max2) { $max = $max1; } else { $max = $max2; } @y = @y1; @x = @x1; graph(); @y = @y2; @x = @x2;my $png_data = $im->png; open (DISPLAY,"| display -") || die; #open (DISPLAY,">image.jpg") || die; binmode DISPLAY; print DISPLAY $png_data; close DISPLAY; sub graph { # set min and max energy my $minEnergy = $min; my $maxEnergy = $max; # set the length of x-axis and y-axis my $xLen = $X1end-$X1start; my $yLen = $Y2end-$Y2start; # Scaling the values $Yscale = $yLen/(abs($minEnergy)-abs($maxEnergy)); $scale = ($minEnergy-$maxEnergy)/5; $incEnergy = $maxEnergy; while($incEnergy > $minEnergy) ##### Prob loop#### { $x1 = $X1start-2; $y1 = int(((abs($incEnergy)-abs($maxEnergy))*$Yscale)+ +$Y2start); $x2 = $X1start+2; $im->line($x1,$y1,$x2,$y1,$black); $displayEnergy = sprintf("%2.1f",$incEnergy); $im->string(gdSmallFont,$x1-35,$y1-10,$displayEnergy,$ +black); $incEnergy = $incEnergy+$scale; } # set the length of nucleotide $len = @x; # mark 10 points on the position axis $posSkip = int(@x/10); # If the length of the fragment is lesser than the x-axis $skipPixel = $xLen/$len; for($i=0;$i<$len-1;$i++) { $x1 = int(($i*$skipPixel)+$X1start); $y1 = int(((abs($y[$i])-abs($maxEnergy))*$Yscale)+$Y2s +tart); $x2 = int((($i+1)*$skipPixel)+$X1start); $y2 = int(((abs($y[$i+1])-abs($maxEnergy))*$Yscale)+$Y +2start); if($call == 0) { $im->line($x1,$y1,$x2,$y2,$red); } else { $im->line($x1,$y1,$x2,$y2,$blue); } $count++; if($count == $posSkip) { $x1 = int(($i*$skipPixel)+$X1start); $y1 = $Y1end-2; $x2 = int(($i*$skipPixel)+$X1start); $y2 = $Y1end+2; if($call == 0) { $im->line($x1,$y1,$x2,$y2,$black); $im->string(gdSmallFont,$x1,$y1+10,$i+ +1,$black); } $count = 0; } } $call++; }
File 'fwd.stb' and file 'total_shuffle.stb'
8 -19.74 9 -19.89 10 -19.91 11 -18.67 12 -18.27 13 -17.55 14 -16.71 15 -16.29 16 -16.59 17 -16.99 18 -16.82 19 -16.4 20 -16.84 21 -18.13 22 -19.37 23 -19.76 24 -19.91 25 -20.19 26 -20.99 27 -20.82 28 -21.27 29 -22.63 30 -23.17 31 -22.78 32 -22.5 33 -23.36 34 -23.76 35 -23.76 36 -22.96 37 -22.57 38 -22.96 39 -23.92 40 -23.52 41 -24.41 42 -24.8 43 -23.86 44 -23.3 45 -24.09 46 -24.54 47 -24.38 48 -24.38 49 -23.65 50 -23.66
8 -20.496 9 -20.986 10 -21.534 11 -21.652 12 -21.77 13 -22.184 14 -22.024 15 -21.546 16 -21.298 17 -21.116 18 -20.928 19 -21.054 20 -20.728 21 -20.394 22 -20.212 23 -19.808 24 -19.346 25 -19.42 26 -19.47 27 -19.282 28 -19.37 29 -19.624 30 -19.916 31 -19.874 32 -20.24 33 -20.22 34 -20.394 35 -20.89 36 -21.142 37 -21.192 38 -21.432 39 -21.448 40 -21.08 41 -20.98 42 -21.252 43 -21.446 44 -21.2 45 -21.29 46 -21.556 47 -21.956 48 -22.03 49 -21.562 50 -21.424
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prob with 'while' loop and subroutine calling
by imp (Priest) on Aug 18, 2006 at 18:57 UTC | |
by cool (Scribe) on Aug 18, 2006 at 21:14 UTC | |
|
Re: Prob with 'while' loop and subroutine calling
by GrandFather (Saint) on Aug 18, 2006 at 20:04 UTC | |
by cool (Scribe) on Aug 18, 2006 at 21:12 UTC | |
by runrig (Abbot) on Aug 18, 2006 at 21:41 UTC | |
by GrandFather (Saint) on Aug 18, 2006 at 22:31 UTC | |
|
Re: Prob with 'while' loop and subroutine calling
by jwkrahn (Abbot) on Aug 18, 2006 at 22:38 UTC | |
by cool (Scribe) on Aug 19, 2006 at 11:28 UTC | |
|
Re: Prob with 'while' loop and subroutine calling
by starbolin (Hermit) on Aug 18, 2006 at 21:15 UTC |