in reply to Finding elements without a counter...
I haven't understood your problem, but there's a lot of things about your style that are better done different, and some that outright need fixing. So I'll just take notes along the way as I'm cleaning it up.
First thing I notice:
If you actually needed that line, you should better write it as@a=@c=@t=@g=@n=@a2=@c2=@t2=@g2=@n2=();
But you're not using the variables outside the loop; so you can just move the my inside the loop.(@a, @c, @t, @g, @n, @a2, @c2, @t2, @g2, @n2) = ();
Next is the pretty hideous train of test likefor (my $k=0;$k<$number_positions; $k++) { my (@a,@c,@t,@g,@n,@a2,@c2,@t2,@g2,@n2);
You're only testing a single character; so use uc to normalize them to upper case (or lc for lower case if you prefer).if ($letter1=~ /[aA]/) { ... } ...
But since you're doing that a zillion times, you can just do it once and be done with:if (uc $letter1 eq 'A') { ... } if (uc $letter1 eq 'C') { ... } if (uc $letter1 eq 'T') { ... } if (uc $letter1 eq 'G') { ... } # ...
Still too much repetition. Since all these arrays are collecting related information, you should use hashes instead:$letter1 = uc $letter1; if ($letter1 eq 'A') { ... } if ($letter1 eq 'C') { ... } if ($letter1 eq 'T') { ... } # ...
But it gets better! Because you only want the number of items in the array anyway. So why keep the elements? Just use the hash elements as counters. Note that I got rid of the separate uppercasing step since we're only using the variables once now.for (my $k=0;$k<$number_positions; $k++) { my (%base1, %base2); # ... $letter1 = uc $letter1; $letter2 = uc $letter2; push @${$base1{$letter1}}, $letter1; push @${$base2{$letter2}}, $letter2;
for (my $k=0;$k<$number_positions; $k++) { my (%base1, %base2); # ... $base1{uc $letter1}++; $base2{uc $letter2}++;
Now, the $number_* variables were already crying out to become elements of a %number hash. But we've already taken care of that with the %base counter hashes.
Next we have stuff like
Perl is not shell code. Don't use quotes were needless. You are forcing Perl to stringify the numbers to create the strings for your quotes, then it has to numify the created strings in order to do math on them. Why? Just say"$number_a" + "$number_c" + "$number_t" + "$number_g" + "$number_n"
Or, now that we have the %base hashes:$number_a + $number_c + $number_t + $number_g + $number_n
$base1{A} + $base1{C} + $base1{T} + $base1{G} + $base1{N}
Also note how you're doing this calculation over and over and over. Just do it once!
Also note how you are declaring $number_a, $number_g, $number_t, etc. That's a huge blinking sign that you should declare %number and work with $number{a}, $number{g}, etc, instead. Together with the fact that you can assign a list of values to a hash when you create it, that makesmy $total1 = $base1{A} + $base1{C} + $base1{T} + $base1{G} + $base1{N} +;
There is still too much repetition:my %percent1 = ( A => ($base{A} * 100) / $total1, C => ($base{C} * 100) / $total1, T => ($base{T} * 100) / $total1, G => ($base{G} * 100) / $total1, N => ($base{N} * 100) / $total1, );
Since we have a list of strings that don't contain whitespace we can make it a little more readable:my %percent1 = map { $_ => ( $base{$_} * 100) / $total1 } 'A', 'C', 'T +', 'G', 'N';
The same change (using a for loop) can be applied to your output code. The bottom line is:my %percent1 = map { $_ => ( $base1{$_} * 100) / $total1 } qw(A C T G +N);
Further reading: Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com.#!/usr/bin/perl use warnings; use strict; chomp(my $input = <STDIN>); open(INPUT, "$input") or die "Cannot open file: $!"; chomp(my $number_positions = <STDIN>); chomp(my $patient_number = <STDIN>); chomp(my $output = <STDIN>); open(OUTPUT, ">$output.txt") or die "Cannot open file: $!"; my @prettybase = <INPUT>; close(INPUT); for my $k (0 .. $number_positions - 1) { my (%base1, %base2); for my $i (0 .. $patient_number) { my ($position, $patient, $letter1, $letter2) = split (/\t/, $prettybase[$i]); $base1{uc $letter1}++; $base2{uc $letter2}++; } my $total1 = $base1{A} + $base1{C} + $base1{T} + $base1{G} + $base +1{N}; my %percent1 = map { $_ => ( $base1{$_} * 100) / $total1 } qw(A C +T G N); my $total2 = $base2{A} + $base2{C} + $base2{T} + $base2{G} + $base +2{N}; my %percent2 = map { $_ => ( $base2{$_} * 100) / $total2 } qw(A C +T G N); print OUTPUT "Position\#\: $patient\n"; print OUTPUT "\tAllele 1 Allele 1 percentage\n"; for(qw(A C T G N)) { print OUTPUT "$_:\t$base1{$_}\t$percent1{$_}\%\n\n"; } print OUTPUT "\tAllele 2 Allele 2 percentage\n"; for(qw(A C T G N)) { print OUTPUT "$_:\t$base2{$_}\t$percent2{$_}\%\n\n"; } } close(OUTPUT);
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OT: program repair shop (was: Finding elements without a counter)
by bioinformatics (Friar) on Oct 13, 2003 at 19:21 UTC |