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

Hello dear PerlMonks, I want to create a program that "reads" from the STDIN a protein sequence and prints the total Molecular Weight of the protein sequence. When I am finished with typing amino acids as input and press "enter" I get the same error message: Use of uninitialized value in addition (+) at MW line 45, <STDIN> chunk 1. Use of uninitialized value in addition (+) at MW line 45, <STDIN> chunk 2. Use of uninitialized value in addition (+) at MW line 45, <STDIN> chunk 3. Use of uninitialized value in addition (+) at MW line 45, <STDIN> chunk 4. Your help is much appreciated. Thank you in advance.
#!/usr/bin/perl -w use strict; use warnings; my %MWs; my $line; my @AA; my $AA; #create the hash MWs with the amino acids and their molecular weight. %MWs = ( "A" => 71.1, "D" => 115.0, "F" => 147.1, "H" => 137.1, "K" => 128.1, "M" => 131.1, "P" => 97.1, "R" => 156.1, "T" => 101.1, "W" => 186.2, "C" => 103.1, "E" => 129.1, "G" => 57.1, "I" => 113.1, "L" => 113.1, "N" => 114.1, "Q" => 128.1, "S" => 87.1, "V" => 99.1, "Y" => 163.1, "\n" => 0.0 ); my $sum = 0.0; #I want the amino acids one by one and not all of them. $/ = \1; print "Type amino acids.\n"; while ( $line = <STDIN> ) #I create an array which contains all the typed amino acids.Each cell +of the array contains one amino acid. { @AA = $line; foreach $AA(@AA) { #I want now the sum of protein's molecular weight. $sum += $MWs{ "$AA(@AA)" }; } } print "Molecular weight of this peptide is: $sum \n"

Replies are listed 'Best First'.
Re: MOLECULAR WEIGHT SUM
by pme (Monsignor) on Mar 20, 2016 at 21:50 UTC
    Hi mbgbioinfo,

    I hope this helps.

    #!/usr/bin/perl -w use strict; use warnings; my %MWs; %MWs = ( "A" => 71.1, "D" => 115.0, "F" => 147.1, "H" => 137.1, "K" => 128.1, "M" => 131.1, "P" => 97.1, "R" => 156.1, "T" => 101.1, "W" => 186.2, "C" => 103.1, "E" => 129.1, "G" => 57.1, "I" => 113.1, "L" => 113.1, "N" => 114.1, "Q" => 128.1, "S" => 87.1, "V" => 99.1, "Y" => 163.1, "\n" => 0.0 ); my $sum = 0.0; print "Type amino acids.\n"; while ( my $line = <STDIN> ) { chomp $line; last if $line eq ''; my @AA = split '', $line; foreach my $AA (@AA) { unless (defined $MWs{$AA}) { print "'$AA' does not exist\n"; next; } $sum += $MWs{$AA}; } } print "Molecular weight of this peptide is: $sum \n";
Re: MOLECULAR WEIGHT SUM
by 1nickt (Canon) on Mar 20, 2016 at 21:56 UTC

    Hi mgbioinfo,

    Have you printed out your variables at various points during the script to see that they contain what you think? Like:

    #!/usr/bin/perl -w use strict; use Data::Dumper; ... while ( my $line = <STDIN> ) { # No need to declare $line until here. my @AA = $line; # No need to declare @AA until here. # Might need to split the line, though +... print Dumper \@AA; # See what's really in that array. Note the bac +kslash. ...

    Later, in your loop, you pull an element from your array, but when you try to use it as the hash key, your syntax is wrong. Change:

    foreach $AA(@AA) { #I want now the sum of protein's molecular weight. $sum += $MWs{ "$AA(@AA)" }; # ^^ # incorrect syntax }
    To this:
    # Sum the proteins' molecular weights. my $sum; # no need to declare $sum until here. # Also no need to initialize to zero; Perl will do that when + you add to it. foreach my $AA ( @AA ) { print "AA: $AA Weight: $MWs{ $AA }\n"; $sum += $MWs{ $AA }; }

    Hope this helps!


    The way forward always starts with a minimal test.
Re: MOLECULAR WEIGHT SUM
by davido (Cardinal) on Mar 20, 2016 at 22:06 UTC

    This code has more than a few problems. It's important to "keep your eye on the ball", so to speak, when programming. I use that term to stand in for the process of knowing where your data is, what it looks like, what variables hold what at each point while progressing through the code. The code above sort of takes guesses at what might be happening. For one thing, what do you think "$AA(@AA)" contains (you can print it to find out, and it will surprise you).

    Here's one way to do it that will work for inputs that have a combination of upper case and lower case amino acids.

    #!/usr/bin/env perl use strict; use warnings; my %MWs; #create the hash MWs with the amino acids and their molecular weight. %MWs = ( "A" => 71.1, "D" => 115.0, "F" => 146.1, "H" => 137.1, "K" => 128.1, "M" => 131.1, "P" => 97.1, "R" => 156.1, "T" => 101.1, "W" => 186.2, "C" => 103.1, "E" => 129.1, "G" => 57.1, "I" => 113.1, "L" => 113.1, "N" => 114.1, "Q" => 128.1, "S" => 87.1, "V" => 99.1, "Y" => 163.1, ); my $sum = 0.0; print "Type amino acids. Hit [ctrl]-D when done.\n"; while ( my $line = <STDIN> ){ chomp $line; next unless length $line; $sum += $MWs{$_} foreach grep {exists $MWs{$_}} map {uc} split //, $line; } print "Molecular weight of this peptide is: $sum \n"

    Dave

Re: MOLECULAR WEIGHT SUM
by kevbot (Vicar) on Mar 20, 2016 at 21:54 UTC

    Hello mbgbioinfo,

    You are not using the proper key when looking up the MW. Change this:

    $sum += $MWs{ "$AA(@AA)" };

    to this:

    $sum += $MWs{ $AA };

    Now, when you press enter you will not get the error messages. When you press Ctrl+D you will see the calculated MW.