in reply to A critique (was Re: Need help with code)
in thread Need help with code

It helped both my coding style and efficiency, thank you! I have a question, if I only wanted to get the 3-triplet amino acid sequence for each start/stop sequence, and not translate it, I modified the code but can't figure out what I'm doing wrong. The string is passed to the while loop, the conditions are checked to terminate and match for the start of the substring, the substring is passed to the subroutine. Inside the subroutine, it enters a for loop, where it makes a substring $codon, if codon matches a regex termination pattern, it returns $dna_string. Here's the code:
#!/usr/bin/perl -w use strict; my $s1 = "AGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTT +"; print "$s1\n"; my $idx = -1; my $start = 'ATG'; while (my $prefix = substr($s1, ++$idx, 3)) { last if length $prefix < 3; # check for start indicator next unless $prefix eq 'ATG'; my $peptide = proteinseq(substr($s1, $idx)); print "$peptide\n"; } sub proteinseq { my ($dna) = @_; my $dna_seq = ''; for (my $i; $i < length($dna)-2; $i +=3) { my $codon = substr($dna, $i, 3); print "$codon\t"; return $dna_seq if ($codon =~ /TA[GA]|TGA/); $dna_seq .= $codon; } }

Replies are listed 'Best First'.
Re^2: A critique (was Re: Need help with code)
by roboticus (Chancellor) on Oct 21, 2012 at 06:05 UTC

    disulphidebond:

    I ran your program, but couldn't really tell what the output meant. Since you have two print statements in there, I couldn't always tell which print statement was generating which string. That made it a bit difficult to interpret. In cases like that, I usually decorate the strings to clarify things to myself. (When debugging it's important to know exactly what's happening. Sometimes I'll use the debugger, but usually, I just put a few print statements to tell me the values of intermediate variables. I usually put prefixes or odd punctuation in them to make things stand out.)

    So I first changed print "$peptide\n" to print "\nPEPTIDE <$peptide>\n"; to put the result on it's own line. When I ran it, I saw this:

    $ perl 1000161.pl AGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTT ATG TAG PEPTIDE <ATG> ATG GGG ATG ACC CCG CGA CTT GGA TTA GAG +TCT CTT PEPTIDE <> ATG ACC CCG CGA CTT GGA TTA GAG TCT CTT PEPTIDE <>

    When I look at it like this, I see that it's not returning the dna peptide when it's missing the ending sequence, nor does it include the ending codon when it's present. Adding the ending codon is really simple: The code adds the codon to the end of the peptide after it checks whether it found the ending codon. Swapping the last two lines in the for loop makes the peptide include the ending codon.

    Next, it seems rather anticlimactic to drop the last two peptides simply because the ending codon is missing. It's happening because the for loop runs out of dna, and we're not returning anything after the loop. So I added a return statement at the end of the loop. But to show that we didn't find the end codon, I add "***" to the peptide string to indicate that we hit the end of our dna string.

    $ cat 1000161.pl #!/usr/bin/perl -w use strict; my $s1 = "AGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTT +"; print "$s1\n"; my $idx = -1; my $start = 'ATG'; while (my $prefix = substr($s1, ++$idx, 3)) { last if length $prefix < 3; # check for start indicator next unless $prefix eq 'ATG'; my $peptide = proteinseq(substr($s1, $idx)); print "\nPEPTIDE <$peptide>\n"; } sub proteinseq { my ($dna) = @_; my $dna_seq = ''; for (my $i=0; $i < length($dna)-2; $i +=3) { my $codon = substr($dna, $i, 3); print "$codon\t"; $dna_seq .= $codon; # moved up a line # We're done if we found the ending codon return $dna_seq if ($codon =~ /TA[GA]|TGA/); } # We hit end of our dna fragment return $dna_seq . "***"; } $ perl 1000161.pl AGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTT ATG TAG PEPTIDE <ATGTAG> ATG GGG ATG ACC CCG CGA CTT GGA TTA GAG +TCT CTT PEPTIDE <ATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTT***> ATG ACC CCG CGA CTT GGA TTA GAG TCT CTT PEPTIDE <ATGACCCCGCGACTTGGATTAGAGTCTCTT***>

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.