#!/usr/bin/perl -w

(@ARGV==1) || die("usage: list-total-inits fasta-file\n");

$seqs=shift(@ARGV);

open(SEQ,$seqs) || die ("No $seqs!\n"); 

$ATG=0;$GTG=0;$TTG=0;
$seq=" ";$header="";

while($seq ne "") {
  ($seq,$header)=load_next_seq(\*SEQ);
  $ATG+=count("ATG")+count("CAT");
  $GTG+=count("GTG")+count("CAC");
  $TTG+=count("TTG")+count("CAA");
}
close(SEQ);
printf("$ATG ATG\n");
printf("$GTG GTG\n");
printf("$TTG TTG\n");

sub load_next_seq {
  my $fname=shift;
  my $seq=""; my $header=""; my $curpos=0; 
  while(<$fname>) {
    chop($_);
    if (/^>/) {
      if ($header ne "") {last;}
      else {$header=substr($_,1);}
    }
    else{$seq.=uc($_);}
    $curpos = tell($fname);
  }
  seek($fname, $curpos, 0);
  return ($seq,$header);
}

sub count {
  ($pat)=@_;
  my $count=0;
  $_=$seq;
  s/$pat/*/g;
  $count+=tr/*/*/;
  return $count;
}

