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:

@a=@c=@t=@g=@n=@a2=@c2=@t2=@g2=@n2=();
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.
for (my $k=0;$k<$number_positions; $k++) { my (@a,@c,@t,@g,@n,@a2,@c2,@t2,@g2,@n2);
Next is the pretty hideous train of test like
if ($letter1=~ /[aA]/) { ... } ...
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 (uc $letter1 eq 'A') { ... } if (uc $letter1 eq 'C') { ... } if (uc $letter1 eq 'T') { ... } if (uc $letter1 eq 'G') { ... } # ...
But since you're doing that a zillion times, you can just do it once and be done with:
$letter1 = uc $letter1; if ($letter1 eq 'A') { ... } if ($letter1 eq 'C') { ... } if ($letter1 eq 'T') { ... } # ...
Still too much repetition. Since all these arrays are collecting related information, you should use hashes instead:
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;
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); # ... $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

"$number_a" + "$number_c" + "$number_t" + "$number_g" + "$number_n"
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:
$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!

my $total1 = $base1{A} + $base1{C} + $base1{T} + $base1{G} + $base1{N} +;
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 makes
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, );
There is still too much repetition:
my %percent1 = map { $_ => ( $base{$_} * 100) / $total1 } 'A', 'C', 'T +', 'G', 'N';
Since we have a list of strings that don't contain whitespace we can make it a little more readable:
my %percent1 = map { $_ => ( $base1{$_} * 100) / $total1 } qw(A C T G +N);
The same change (using a for loop) can be applied to your output code. The bottom line is:
#!/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);
Further reading: Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series on Perl.com.

Makeshifts last the longest.


In reply to OT: program repair shop (was: Finding elements without a counter) by Aristotle
in thread Finding elements without a counter... by bioinformatics

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.