Edit: Looks like kennethk also rewrote your code in almost the same way as I was adding my last part, so it must be the right way to do it! :P

Edit2: kennethk pointed out to me that my code is incrementing by three positions when there is an unknown codon, but yours only increments by one position. Is this by design? It seems to me like you would want to move on to the next codon regardless of there being a match or mismatch.

First, your code would be much more simple if you had your codon table in a hash and just looked up the codon in the hash to get the amino acid. Before you entered the loop, you would define the table:

my %codon_table = ( 'TTT' => 'F', 'TTC' => 'F', 'TTA' => 'L', 'TTG' => 'L', 'CTT' => 'L', 'CTC' => 'L', 'CTA' => 'L', 'CTG' => 'L', # AND SO ON );

Second, I'm not really familiar with this usage:

@marcos=<,$protein>;

I think what you want instead of this line is:

push @marcos, $protein;

Then if you move your last little loop outside of the main loop, I think it should behave the way you expect. Here is a simplified version of that last loop:

print "La secuencia $c es: $_\n\n" for @marcos;

Third, I suspect that you aren't using use strict; use warnings;... Use these!

I've combined all of these suggestions and made a few more changes to rewrite your code as:

my %codon_table = ( 'TTT' => 'F', 'TTC' => 'F', 'TTA' => 'L', 'TTG' => 'L', 'CTT' => 'L', 'CTC' => 'L', 'CTA' => 'L', 'CTG' => 'L', # AND SO ON ); my @marcos; for my $reading_frame ( 0 .. 2 ) { my $position = $reading_frame; while ( $position < length($DNA) - 2 ) { my $codon = substr( $DNA, $position, 3 ); my $amino_acid = $codon_table{$codon}; if ( defined $amino_acid ) { $protein = $protein . $amino_acid; } else { $protein .= "x"; } $position += 3; } push @marcos, $protein; } print "La secuencia $reading_frame es: $_\n\n" for @marcos;

In reply to Re: Storage of proteins from diferent ORF by frozenwithjoy
in thread Storage of proteins from diferent ORF by dmunoze

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.