Hello, this is my first time on Perl Monks. I'm a grad student practicing Bioinformatics for my dissertation. I found out about Rosalind beta bioinformatics problems (http://rosalind.info, no affiliation posted for interested people only) and decided to give them a shot. My problem is I am trying to parse a given string $s1, and find substrings designated by specific beginning and ending sequences. My code compiles, runs, returns strings which appear to be correct, but is "wrong" according to the website. If anyone can help, my question is if anyone can see any bugs in the code, and especially if anyone can suggest ways to optimize my code, I would greatly appreciate it. Thanks!
#!/usr/bin/perl -w # use Data::Dumper; $s1 = 'AGCCATGTAGCTAACTCAGGTTACATGGGGATGACCCCGCGACTTGGATTAGAGTCTCTTTTG +GAATAAGCCTGAATGATCCGAGTAGCATCTCAG'; print "$s1\n"; # copy string $s1 to array @arr_ @arr_ = split ('', $s1); # must test for initial methionine # assign @arr_ array to string $arr_met to test $arr_met = join('', @arr_); $sub_met = substr($arr_met, 0, 3); if ($sub_met =~ m/ATG/) { # if match is TRUE, call &proteintrans to translate # else do nothing my $peptide = proteintrans($arr_met); print "\n$peptide\n"; } else { print "\nno initial match, moving along...\n"; } # for each iteration of loop # shift $j element off front of array to alter reading frame # assign array @arr_ to string $dna_string for processing # call &testformethionine subroutine print "@arr_\n"; $len = @arr_; for ($j = 0; $j < $len; $j++) { shift @arr_; $dna_string = join('', @arr_); # debugging step # print "\n$dna_string\n"; testformethionine($dna_string); } sub testformethionine { my ($dnastr) = @_; # create substring from first 3 amino acids my $sub1 = substr($dnastr, 0, 3); # print "$sub1\n"; # if matches ATG (Met) proceed through translation &proteintrans # else do nothing if ($sub1 =~ m/ATG/) { my $peptide = proteintrans($dnastr); print "$peptide\n"; } else { # debugging line of code # print "move along, polymerase\n"; } } sub proteintrans { my ($dna) = @_; my $protein = ''; # split amino acids into 3 with for loop and concatenate for(my $i = 0; $i < (length($dna) - 2) ; $i +=3) { $protein .= codons(substr($dna, $i, 3)); } # return $protein; # $len_ = length($protein); if ($protein =~ m/_/) { # if string $protein matches stop codon '_' # find match with index() # form a substring before stop codon with substr() # return new substring protein_ $index = index($protein, '_'); $protein_ = substr($protein, 0, $index); return $protein_; } else { # else return protein unmodified return $protein; } } sub codons { # use hash table to assign codons my($codon) = @_; $codon = uc $codon; my (%genetic_code) = ( 'TCA' => 'S', # Serine 'TCC' => 'S', # Serine 'TCG' => 'S', # Serine 'TCT' => 'S', # Serine 'TTC' => 'F', # Phenylalanine 'TTT' => 'F', # Phenylalanine 'TTA' => 'L', # Leucine 'TTG' => 'L', # Leucine 'TAC' => 'Y', # Tyrosine 'TAT' => 'Y', # Tyrosine 'TAA' => '_', # exit 'TAG' => '_', # exit 'TGC' => 'C', # Cysteine 'TGT' => 'C', # Cysteine 'TGA' => '_', # exit 'TGG' => 'W', # Tryptophan 'CTA' => 'L', # Leucine 'CTC' => 'L', # Leucine 'CTG' => 'L', # Leucine 'CTT' => 'L', # Leucine 'GTT' => 'V', # Valine 'GTC' => 'V', # Valine 'GTA' => 'V', # Valine 'GTG' => 'V', # Valine 'GCT' => 'A', # Alanine 'GCC' => 'A', # Alanine 'GCA' => 'A', # Alanine 'GCG' => 'A', # Alanine 'GAT' => 'D', # Aspartic Acid 'GAC' => 'D', # Aspartic Acid 'GAA' => 'E', # Glutamate 'GAG' => 'E', # Glutamate 'GGT' => 'G', # Glycine 'GGC' => 'G', # Glycine 'GGA' => 'G', # Glycine 'GGG' => 'G', # Glycine 'CCA' => 'P', # Phenylalanine 'CCC' => 'P', # Phenylalanine 'CCG' => 'P', # Phenylalanine 'CCT' => 'P', # Phenylalanine 'CAC' => 'H', # Histidine 'CAT' => 'H', # Histidine 'CAA' => 'Q', # Glutamine 'CAG' => 'Q', # Glutamine 'CGA' => 'R', # Arginine 'CGC' => 'R', # Arginine 'CGG' => 'R', # Arginine 'CGT' => 'R', # Arginine 'ATA' => 'I', # Isoleucine 'ATC' => 'I', # Isoleucine 'ATT' => 'I', # Isoleucine 'ATG' => 'M', # Methionine 'ACA' => 'T', # Threonine 'ACC' => 'T', # Threonine 'ACG' => 'T', # Threonine 'ACT' => 'T', # Threonine 'AAC' => 'N', # Asparagine 'AAT' => 'N', # Asparagine 'AAA' => 'K', # Lysine 'AAG' => 'K', # Lysine 'AGC' => 'S', # Serine 'AGT' => 'S', # Serine 'AGA' => 'R', # Arginine 'AGG' => 'R', # Arginine # hash table and subroutine from "Beginning Perl for Bioinformatics" # by James Tisdall. c2001 O'Reilly Press. ); if(exists $genetic_code{$codon}) { return $genetic_code{$codon}; } else { print STDERR "$codon not found\n"; exit; } }

In reply to Need help with code by disulfidebond

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.