Home: Perl Programming Help: Intermediate:
string mutation script behaving erraticaly


Jun 24, 2012, 9:17 PM

Views: 8265
string mutation script behaving erraticaly

Dear all,
I have written a perl program which mutates a a protein sequence ( based on 20 alphabets).
For each character it encounters the program generates a random number between 0 and 1.
Im using hashes for mutation matrix. But I dont know why this problem. Try running the code in the attachment.
Attachments: pam1_check.pl (4.68 KB)

Veteran / Moderator

Jun 25, 2012, 7:38 AM

Views: 8241
Re: [rushadrena] string mutation script behaving erraticaly

You didn't specify what problem you are trying to resolve.

What is your expected output and how does that differ from what you're actually getting?

Using backticks to shell out to the system's awk and srand commands doesn't make much sense. Use Perl's built-in rand function.

Remove the -w switch and add these 2 pragmas then fix the problems that they point out.

use strict; 
use warnings;

You should fix your indentation; it's inconsistent and in some areas it's missing.

Use a single hash lookup instead of all of those elsif blocks.


Jun 25, 2012, 11:42 AM

Views: 8233
Re: [FishMonger] string mutation script behaving erraticaly

Heres what the code does.
The string to be mutated has already been provided inside the code(line 30).
The string is made of 20 alphabets(line 5-24).
%nuc (line 4)is a 24x24 matrix made in hash.
The program generates a random number between 0 and 1 for length times the input string (length $string).
It then checks this value in the corresponding hash %nuc. In our case since the $string has been hardcoded consisting of only "A", therefore each time a random number is generated the program goes to line 17 only.
It then traverses the line 17 horizontally until a number greater than the random number is encountered. The value of the counter is noted when a number greater than the random number is encountered.
For example in case our our $string which has only "A", it will mutate to another alphabet only if the random number is greater than .9799 otherwise it will remain as "A" only.
The biggest problem with this code is that hashes dont get printed in the same order you store them,this shuffling leads to erratic results.
I tried rearranging the rows of hash %nuc but it still didnt help.
Another error is that Im getting an alphabet more in the output string than the hardcoded string.

Veteran / Moderator

Jun 25, 2012, 4:59 PM

Views: 8221
Re: [rushadrena] string mutation script behaving erraticaly

Did you make the changes I suggested? If not, why not?

You can cleanup and remove some of those loops by using the List::Util module.

There are a couple ways to work with an ordered hash. One approach is to use the Tie::IxHash module.


Jul 2, 2012, 6:25 AM

Views: 7741
Re: [rushadrena] string mutation script behaving erraticaly

The main error in your script has nothing to do with perl. The hash %nuc is really a matrix of the probabilities of every possible mutation. Even if your code did exactly what you describe, the results would not reflect this. Note that testing this code is not a simple task because the expected result is only a few mutations in tens of thousands of runs.

I recommend developing a corrected mutation subroutine as a module to simplify the testing. Use all of FishMonger's suggestions to help avoid common errors.
Good Luck,