Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Translation Substring Error

by Laurent_R (Canon)
on Nov 09, 2017 at 16:01 UTC ( [id://1203054]=note: print w/replies, xml ) Need Help??


in reply to Translation Substring Error

Hi,

Try this:

use strict; use warnings; my $s1 = 'ATGCCCGTAC'; ## Sequence 1 my $s2 = 'GCTTCCCAGCGC'; ## Sequence 2 print "Sequence 1 Translation:"; my $amino_acid = OneFrameTranslation ($s1); ## Calls subroutine print "$amino_acid\n"; print "Sequence 2 Translation:"; $amino_acid = 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($seq,$i,3); $amino_acid .= $genetic_code{$codon}; } return ($amino_acid); }
The main errors in your code is that the $seqarray is never initialized to anything (note that this is different from @seqarray) and that you don't use the return values from your subroutines.

Update: haukex and toolic were faster than me. Also note I only made the minimal changes, you don't really need to create @seqarray, since you're not really using it (except in the $i<=$#seqarray-2 for loop termination clause where you could simply use the length of the sequence).

Replies are listed 'Best First'.
Re^2: Translation Substring Error
by FIJI42 (Acolyte) on Nov 09, 2017 at 16:08 UTC

    This works great, thank you.

    Yeah, I see the error with with $seqarray - I'll try to more dynamic variable names to minimize confusion next time.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1203054]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found