Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with foreach loops..and chdir

Status
Not open for further replies.

broloc

Programmer
Jun 10, 2002
27
JP
...somebody..please..
Code:
  @filelocation = Location::destination ($database);

  $location = $filelocation[0];

  chdir $location;

  opendir (DIR, "$location")|| die "Couldn't open directory  \n";

  @fileList = readdir(DIR);
  foreach my $file (@fileList) {
     #...do something with the files..

  }

the problem is..the loop keep on going non stop.i'm not sure if there's problem with my foreach loop or..did i misuse the CHDIR function?

 
what exactly are you doing with the files?
Tips:
1) chdir $location or die "cannt chdir to $location\n";#always check for errors!

2) @fileList = readdir(DIR);
print "filelist=@fileList\n";

3) post the code in between,
preferably write is as a subroutine

svar
 
here's the things inside the foreach loop.i use bioperl module to access sequence of amino acids in the local database.this program suppose to match sequence that were entered by user with existed sequence and find out if it existed.

Code:
sub access_seq {

$in = Bio::SeqIO ->new(-file =>"$file",'-format'=>"$format");

      while (my $seq = $in->next_seq()) {

          my @sequence = $seq->seq;
      }
      if (@sequence = ~/$input/) {
      #$input is a user input.
       print "Sequence found in ",$file,"\n";

       push @foundfile,$file;

       $found = $found + 1;
       }
}

..i already change the coding based on your tips,but it doesn't do much change...
 
First, it would help if you posted the full code, including the diagnostic statements

e.g.
@fileList = readdir(DIR);print "filelist=@fileList\n";

Second, I am not sure a space is allowed between
Bio::SeqIO ->new, but I guess it's ok

Thirs, you can always try
perk -d::ptkdb yourscript.pl

This is a visual debugging interface and you can trace what your code does
(You need of course to install the module ptkdb first:))

Fourth, I suggest using strict and mying all your variables. If $input and $format will not be modified by the subroutine(as it looks like), then please call the subroutine
as access_seq($format,$input,$file,\$found,\@foundfile);
and then have
sub accessA_seq{my $format=shift;my $input =shift;
my $file=shift; my $rfound=shift;my $rfoundfiles=shift;
$in = Bio::SeqIO ->new(-file =>"$file",'-format'=>"$format"); you do not need the quotes "$file"
and "$format"

Last, and here is the answer it is obvious that the infinite loop is in

while (my $seq = $in->next_seq()) {

my @sequence = $seq->seq;
}
if (@sequence = ~/$input/) {
#$input is a user input.
print "Sequence found in ",$file,"\n";

push @foundfile,$file;

$found = $found + 1;
}

Here note thatyour check criterion will be true if
the number of elements in @sequence includes a string $input, e.g. igf you have 1000 elements in @sequence, then if $input is ffggf100055965yiutu, then you have a match
Is this really what you want, or are you trying to say: If @sequence contains an element $input, then...

The reason is that
@sequence is an array and you are evaluating it in a scalar context


I am not familiar with the specific module, nor do I have it installed, but how does the while condition become false?

Here is my further suggestion. This will not give you a working code, but it will tell us what your code is doing. Please try it and post the printout
I realize you do not want to use references and stuff, so
I am deliberately keeping the code with
as few changes from your original as possible
Pleas epost the outcome

svar

#####
#debugging rewrite

$input= .... ;
$format=....;
$database= ...;
@foundfile=();
$found=0;
@filelocation = Location::destination ($database);

$location = $filelocation[0];


chdir $location or die "cannt chdir to $location\n";#always check for errors!

opendir (DIR, "$location")|| die "Couldn't open directory \n";
@fileList = readdir(DIR);
print "filelist=@fileList\n";

foreach my $file (@fileList) {
#...do something with the files..
&access_seq(); }


sub access_seq {

$in = Bio::SeqIO ->new(-file =>$file,'-format'=>$format);

while (my $seq = $in->next_seq()) {

my @sequence = $seq->seq;
}
print "sequence =@sequence\n";
my $ref=ref($seq}\n";
print "ref=$ref\n";
die;
if (@sequence = ~/$input/) {
#$input is a user input.
print "Sequence found in ",$file,"\n";

push @foundfile,$file;

$found = $found + 1;
}
}



 
urm..i dun really know how to explain this..but i'll try my best.
first the while loop
Code:
 while (my $seq = $in->next_seq())
i'm not sure what it does as i picked this solely from the module documentation.but i think it going to stop when it reach the end of the sequence.I have run this program before but without the Location.pm module..and it was doing just fine.

then...
Code:
if (@sequence = ~/$input/)
this line supposed to do..'if @sequence contain $input,then..'but i already change it back to original where i used
Code:
if ($sequence = ~/$input/)
..this seems to be working already.

third..actually i dun use subroutines in the foreach loops.just create it to make it easier for u.well..here's my full coding.
Code:
#! usr\bin\perl

use Bio::SeqIO;
use Bio::Seq;
use Location;


$found = 0;

print "
***************************************************

SWISSPROT
EMBL
ACE

***************************************************
";
print "NOTES : You can use the table above to input databases\n";
print "Enter the database to search from:";
$database = <STDIN>;
chomp $database;

if    (/$database/i == swissprot) {
   $format = swiss;
}
elsif ( /$database/i == ace) {
   $format = 'ace';
}
elseif (/$database/i == EMBL) {
   $format = 'embl';
else {

   print&quot;The database that you choose is not supported by this program\n&quot;;

}

  @filelocation = Location::destination ($database);

  my $location = $filelocation[0];

  print &quot;$location\n&quot;;

  chdir $location || die &quot;cannot chdir to&quot;, $location,&quot;\n&quot;;

  print &quot;Enter sequence:&quot;;
  $input = <STDIN>;
  chomp $input;
  print &quot;\n&quot;;

  opendir (DIR, &quot;$location&quot;)|| die &quot;Couldn't open directory or directory not found\n&quot;;

  @fileList = readdir(DIR);
  foreach  my $file (@fileList) {

      next if (my $file =~/^\./) ;

      $in = Bio::SeqIO->new(-file =>$file,'-format'=>$format);

      while (my $seq = $in->next_seq()) {

          my $sequence = $seq->seq;

            if (my $sequence = ~/$input/) {

            print &quot;Sequence found in &quot;,$file,&quot;\n&quot;;

            push @foundfile,$file;

            $found = $found + 1;
            }
       }
    }

print $foundfile[$_],&quot;\n&quot;;

for $f(@foundfile) {

   open (FILE,$f) || die &quot;Couldn't open file\n&quot;;

   $in = Bio::SeqIO ->new(-file =>$f,'-format'=>$format);

     while (my $seq = $in->next_seq()) {

       print &quot;Sequence ID equal to &quot;,$seq->id,&quot;\n&quot;;

     }
}
 
and...for ur suggestion and ur request for the output,can i get back to it later?..maybe tommorow or the other day.but..really,thanks for ur help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top