#!/bin/perl
##################################################
# Count the Frequence of nucleotides in a 
# file containing a multi-line input sequence.
# This requires an introduction to scalar operations,
# conditional expressions, and iteration. You are
# "given" the list-based code to handle the input.
##################################################

# Initalization
$countA = 0;
$countC = 0;
$countG = 0;
$countT = 0;
$curPos = 0;

## Store the entire nt sequence from the multi-line file in $DNA
@input = <STDIN>;
chomp(@input);
foreach $line (@input) {
   $DNA = $DNA . $line;
}

$numNts = length($DNA);

## Count the nts
while ($curPos <= $numNts) {
   $nt = substr($DNA,$curPos,1);
   if ($nt eq "A") {
      $countA = $countA + 1;
   }
   if ($nt eq "C") {
      $countC = $countC + 1;
   }
   if ($nt eq "G") {
      $countG = $countG + 1;
   }
   if ($nt eq "T") {
      $countT = $countT + 1;
   }
   $curPos = $curPos + 1;
} # End of while loop

## Print Results

print "The sequence contains $numNts nucleotides.\n";
print "Percentage of A's: ", ($countA/$numNts) * 100, "%\n";
print "Percentage of C's: ", ($countC/$numNts) * 100, "%\n";
print "Percentage of G's: ", ($countG/$numNts) * 100, "%\n";
print "Percentage of T's: ", ($countT/$numNts) * 100, "%\n";

exit;                    # End program
