[Bioperl-l] foreach (@array)?

Stefan Kirov skirov at utk.edu
Wed Sep 10 15:00:49 EDT 2003


Hi Vesko,
My underrstanding is you are trying to extract the sequence information 
for a list of ids you have in @mirna.
While the solution given by Jonathan is good, there might be a memory 
problem if your sequences are big and/or the database contains 
considerable amount of sequences.
Now there is another solution:
my (@found, at ids);
while(my $RNA=$DB->next_seq()) {
    my $id=$RNA->display_id;
    push @found, $RNA if  (grep(/\b$id\b/, at mirna));
    push @ids,$id;
}

After that you will have an array with Bio::Seq objects, that are in 
your initial list (@mirna) and you can extract any info you want, using 
the Bio::Seq methods on the fly.
Also you have an array (@ids) with the retrieved sequences, so you can 
see which ones you miss by comparing @ids with mirna. I think this 
should work
-----------
By the way kade rabotish?
Good luck!



The other replies suggested assigning the foreach. While it's probably a 
good idea, you don't *have* to assign it to a variable. What you have 
should work.

The real problem here is the next_seq() call.

This function consumes your $DB Bio::SeqIO object. Once you get a 
next_seq, you can never go back. I think you're trying to iterate over 
it multiple times.

Here's a quick solution:

my %sequences;
## This goes through your sequences once, and stores them for later use.
while(my $RNA=$DB->next_seq()) {
   ## Added bonus of indexing by display_id.
   ## this allows for quick lookup later
   my $RNAid = $RNA->display_id;
   $sequences{$RNAid} = $RNA;
}

## Now just use:
foreach $id (@mirna) {
   next unless (exists $sequences{$id} && defined $sequences{$id});
   $seqRNA = $sequences{$id}->seq();

   # rest of your code here...
    for (my $i=0; $i<length($geneRNA); $i++) {
      $subgene=substr($geneRNA,$i,length($seqRNA));
      $percentage = align_subs($seqRNA, $subgene);

       if($percent<$percentage) {
        push (@RNAname,$id);
        push (@subgene, $subgene);
        push (@percentage,$percentage);
        push (@position,($i+1));
       }
    }

}

Hope this helps,
~Jonathan


Vesko Baev wrote:
Hi,
When I start my script, it return results only for first variable in 
@array, but in the raw 'foreach (@array){' in the @array there is two or 
more variables, but anytime it returns the result whit first variable:

foreach (@mirna) {      $id=$_;   while (my $RNA=$DB->next_seq()) {      
$RNAid=$RNA->display_id;
     if ($RNAid eq $id) {
     $seqRNA=$RNA->seq();
     }
     else {next};


   for (my $i=0; $i<length($geneRNA); $i++) {
     $subgene=substr($geneRNA,$i,length($seqRNA));
     $percentage = align_subs($seqRNA, $subgene);

      if($percent<$percentage) {
       push (@RNAname,$id);
       push (@subgene, $subgene);
       push (@percentage,$percentage);
       push (@position,($i+1));
      }
   }
  };
 };
};
Thanks!

-- 
Stefan Kirov, Ph.D.
University of Tennessee/Oak Ridge National Laboratory
1060 Commerce Park, Oak Ridge
TN 37830-8026
USA
tel +865 576 5120
fax +865 241 1965
e-mail: skirov at utk.edu
sao at ornl.gov




More information about the Bioperl-l mailing list