http://qs1969.pair.com?node_id=1203047

FIJI42 has asked for the wisdom of the Perl Monks concerning the following question:

I have a subroutine for a basic one frame translation that is giving me an error for "Use of uninitialized value $codon in hash element" and "substr outside of string". I think my problem is I need to modify the subroutine's for loop to account for nucleotide sequences with odd numbers of acids (i.e. not in multiples of 3).

Does anyone have suggestions for how to modify the code properly?

Here is the subroutine I'm using in a simple example:

use strict; use warnings; my $amino_acid=''; my $s1 = 'ATGCCCGTAC'; ## Sequence 1 my $s2 = 'GCTTCCCAGCGC'; ## Sequence 2 print "Sequence 1 Translation:"; OneFrameTranslation ($s1); ## Calls subroutine print "$amino_acid\n"; print "Sequence 2 Translation:"; OneFrameTranslation ($s2); ## Calls subroutine print "$amino_acid\n"; ### Subroutine ### sub OneFrameTranslation { my ($seq) = shift; my $amino_acid=''; my $seqarray=''; my %genetic_code = ( 'TTT' => 'F', 'TTC' => 'F', 'TTA' => 'L', 'TTG' => 'L', 'CTT' => 'L', 'CTC' => 'L', 'CTA' => 'L', 'CTG' => 'L', 'ATT' => 'I', 'ATC' => 'I', 'ATA' => 'I', 'ATG' => 'M', 'GTT' => 'V', 'GTC' => 'V', 'GTA' => 'V', 'GTG' => 'V', 'TCT' => 'S', 'TCC' => 'S', 'TCA' => 'S', 'TCG' => 'S', 'CCT' => 'P', 'CCC' => 'P', 'CCA' => 'P', 'CCG' => 'P', 'ACT' => 'T', 'ACC' => 'T', 'ACA' => 'T', 'ACG' => 'T', 'GCT' => 'A', 'GCC' => 'A', 'GCA' => 'A', 'GCG' => 'A', 'TAT' => 'Y', 'TAC' => 'Y', 'TAA' => '*', 'TAG' => '*', 'CAT' => 'H', 'CAC' => 'H', 'CAA' => 'Q', 'CAG' => 'Q', 'AAT' => 'N', 'AAC' => 'N', 'AAA' => 'K', 'AAG' => 'K', 'GAT' => 'D', 'GAC' => 'D', 'GAA' => 'E', 'GAG' => 'E', 'TGT' => 'C', 'TGC' => 'C', 'TGA' => '*', 'TGG' => 'W', 'CGT' => 'R', 'CGC' => 'R', 'CGA' => 'R', 'CGG' => 'R', 'AGT' => 'S', 'AGC' => 'S', 'AGA' => 'R', 'AGG' => 'R', 'GGT' => 'G', 'GGC' => 'G', 'GGA' => 'G', 'GGG' => 'G' ); ## '---' = 3 character codon in hash above ## '-' = one letter amino acid abbreviation in hash above my @seqarray = split(//,$seq); ## Explodes the string for (my $i=0; $i<=$#seqarray-2; $i=$i+3) { my $codon = substr($seqarray,$i,3); $amino_acid = $genetic_code{$codon}; } return ($amino_acid); }