Link to home
Start Free TrialLog in
Avatar of team
team

asked on

infinite loop

Hi I would appreciate if someone could tell me why this prog is running into infinite loop.

#!/usr/local/bin/perl -w

use strict;
use warnings;
# Defining the family file.
my ($file_name) = 'families' ;

# Opening the file
open(file_handle, $file_name) or die "\n Unable to open families file";

# declaring a hash
my (%hash) = ();

# Reading a line from the file and creating a hash.
while (my $line = <file_handle>)  #start of while loop a
 {
     my (@temp) = split(" ", $line);
     $hash{$temp[0]} = "$temp[1]";
 } # end of while loop a

# Closing the file.
close($file_name);

# Opening the query file to pull out the query sequences.
my ($file_n) = 'query.fa';

open(han, $file_n) or die "unable to open query file";

# Declare an array to hold the query sequences.
my(@array);
my ($i) = 0;
while(my $line = <han>)  # start of while loop a
{
 if($line=~ /^>/)  # start of if loop a
  {
    my (@temp_arr ) = split (" ", $line);
    $array[$i]  =  $temp_arr[0] ;
    $array[$i]  =~ s/>//g;
    $i = $i + 1;
  } # end of if loop a
} # end of while loop a

# Defining the blosum matrices to act upon
#my (@mat) = (45, 62, 80);
my (@mat) = (45);
# Computing for each matrix.
foreach my $val (@mat)  # start of foreach loop a
 {
# To extract only the query to act upon.
    foreach my $query (@array) # start of foreach loop b
      {
# Opening the blosum file
        my $f_name = "blosum$val.test";
         open(file_han, $f_name) or die "\n Unable to open blosum$val.test file";
# Building an array out of the results of a query
         my(@result) = ();
         while (my $blos_res = <file_han>) # while loop a
          {
# Checking for the results of the corresponding query.
## goes into infinite loops somewhere over here
# Check works by looking for the occurance  of the query in the starting
# of the result.
                if($blos_res =~ /^$query/)
                {
                   push(@result, $blos_res);
                }
               foreach my $a (@result)
                {
                  print "\n $a";
                }
          } # end of while loop a
    } # end of foreach loop b
} # end of foreach loop a

Avatar of team
team

ASKER

typical enteries of family

d3sdha_     1.001.001.001.001
d1flp__     1.001.001.001.001
d1hbg__     1.001.001.001.001
d1mbd__     1.001.001.001.001
d1mba__     1.001.001.001.001
d1eca__     1.001.001.001.001
Avatar of team

ASKER

typical enteries of query.fa
>d3sdha_ Hemoglobin I [ark clam (Scapharca inaequivalvis)]
SVYDAAAQLTADVKKDLRDSWKVIGSDKKGNGVALMTTLFADNQETIGYFKRLGNVSQGMANDKLRGHSITLMYALQNFIDQLDNPDDLVCVVEKFAVNHITRKISAAEFGKINGPIKKVLASKNFGDKYANAWAKLVAVVQAAL
>d1flp__ Hemoglobin I [clam (Lucina pectinata)]
SLEAAQKSNVTSSWAKASAAWGTAGPEFFMALFDAHDDVFAKFSGLFSGAAKGTVKNTPEMAAQAQSFKGLVSNWVDNLDNAGALEGQCKTFAANHKARGISAGQLEAAFKVLSGFMKSYGGDEGAWTAVAGALMGEIEPDM
>d1hbg__ Glycera globin [marine bloodworm (Glycera dibranchiata)]
GLSAAQRQVIAATWKDIAGADNGAGVGKKCLIKFLSAHPQMAAVFGFSGASDPGVAALGAKVLAQIGVAVSHLGDEGKMVAQMKAVGVRHKGYGNKHIKAQYFEPLGASLLSAMEHRIGGKMNAAAKDAWAAAYADISGALISGLQS
>d1mbd__ Myoglobin [sperm whale (Physeter catodon)]
VLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRFKHLKTEAEMKASEDLKKHGVTVLTALGAILKKKGHHEAELKPLAQSHATKHKIPIKYLEFISEAIIHVLHSRHPGDFGADAQGAMNKALELFRKDIAAKYKELGYQG
>d1mba__ Myoglobin [sea hare (Aplysia limacina)]
Avatar of team

ASKER

typical enteries of blosum$val.test

d3sdha_     d3sdha_     100.00     145     0     0     1     145     1     145     1.3e-80     288.9
d3sdha_     d1hlb__     2d1flp__     d1xgsa1     33.33     30     20     0     92     121     41     70      94.5     16.93
d1flp__     d1xgsa1     33.33     30     20     0     92     121     41     70      94.5     16.93
d1hbg__     d1hbg__     100.00     147     0     0     1     147     1     147     2.1e-73     265.0
d1mbd__     d1bfma_     40.00     20     12     0     126     145     23     42      80.8     17.32
d1mba__     d1mba__     100.00     146     0     0     1     146     1     146     5.9e-68     246.9

Avatar of team

ASKER

please ignore 2 line of enteries in blosum$val.test
I copied the program and three test files locally.  The program completed without incident.  Are you processing very large amounts of data?  It could be that you are just seeing a long process time.  If you really think you have an infinite loop problem then try running the progam with debugging turned on and step through the program's execution.

Good Luck
The program will be faster (and probably will not loose any functionality) if:

              foreach my $a (@result)
               {
                 print "\n $a";
               }
         } # end of while loop a

is changed to:
         } # end of while loop a
         foreach my $a (@result)
          {
            print "\n $a";
          }
To monitor the progress of the 'while' loop that appears to hang, add this line into the loop:

     print "Read $f_name line $.\n";
Hi,

Try changing the line

while (my $line = <file_handle>)  #start of while loop a

to

while my $line (<file_handle>)  # start of while loop a


also change the line:
while (my $line = <han>)  #start of while loop a

to

while my $line (<han>)  #start of while loop a
ASKER CERTIFIED SOLUTION
Avatar of PC_User321
PC_User321

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial