#!/usr/bin/perl -w
# Translate_aa_codes
#    VERSION: Version 1 (27 October 2003)
#    PURPOSE: Translates amino acid 1-letter code to name

############## LIBRARIES AND PRAGMAS ################
  use strict;

#################### CONSTANTS ######################

  my $LF  = "\n";
  my $tab = "\t";
  my %amino_acid = 
    ("A" => "alanine",
     "C" => "cysteine",
     "D" => "aspartate",
     "E" => "glutamate",
     "F" => "phenylalanine",
     "G" => "glycine",
     "H" => "histidine",
     "I" => "isoleucine",
     "K" => "lysine",
     "L" => "leucine",
     "M" => "methionine",
     "N" => "asparagine",
     "Q" => "glutamine",
     "R" => "arginine",
     "S" => "serine",
     "T" => "threonine",
     "V" => "valine",
     "W" => "tryptophan",
     "Y" => "tyrosine");
  my $aa_codes = "ACDEFGHIKLMNPQRSTVWYX";
  my $all = "X";
     
#################### VARIABLES ######################

  my $code;

################### MAIN PROGRAM ####################

  do {
     $code = Get_one_letter_code();
     if ($code) {
        if ($code eq $all) { Print_all_codes() }
        elsif ($aa_codes =~ /$code/) { print $code, " is ", $amino_acid{$code} }
        else { print $code, " is not an amino acid!" }
     }
  } until (not $code);

#################### SUBROUTINES ####################

#### GET_ONE_LETTER_CODE
#    Solicits from keyboard a one letter code of an amino acid
#    Doesn't check for validity
#    Returns input as upper case
  sub Get_one_letter_code {
     my $code;
     print $LF;
     print "Enter one letter amino acid code (or X for all): ";
     $code = <STDIN>;
     chomp $code;
     return uc($code);
  }
   
#### PRINT_ALL_CODES
#    Prints codes/names of amino acids sorted by code
#    Then prints same ordered by name
  sub Print_all_codes {
     my $code;
     my $aa_name;
     print $LF;
     print "CODE", $tab, "NAME", $LF;
     for $code ( sort (keys %amino_acid) ) {
        print $code, $tab, $amino_acid{$code}, $LF;
     }
     print $LF;
     print "PRESS A KEY TO CONTINUE";
     <STDIN>;
     print "CODE", $tab, "NAME", $LF;
     for $code ( sort { $amino_acid{$a} cmp $amino_acid{$b} } keys %amino_acid) {
        print $code, $tab, $amino_acid{$code}, $LF;
     }
  }
