[Bioperl-l] handling errors?

Stephen Henderson s.henderson@ucl.ac.uk
Wed, 23 May 2001 11:05:57 +0100


Thanks, your script doesn't seem to work on Win32, BUT eval one line, and
using the next command seems to be working so far.

-----Original Message-----
From: Heikki Lehvaslaiho [mailto:heikki@ebi.ac.uk]
Sent: Wednesday, May 23, 2001 9:49 AM
To: Stephen Henderson
Cc: 'bioperl-l@bioperl.org'
Subject: Re: [Bioperl-l] handling errors?


Stephen,

You probably store your ids in the file one per line. When you read
them in the newline character is read in too. 'chomp @ets' gets rid of
them.

Also, put only the critical line into the eval statement and then test
it before doing anything else.

Have a look at my simplified code which reads in from the filename
given as the first argument (under UNIX):

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

use Bio::DB::GenBank;

$gb = new Bio::DB::GenBank();

foreach (<>) {
    chop;
    eval {
        $seq = $gb->get_Seq_by_id($_);
    };
    next if $@;
    $detail= $seq->display_id;
    print $detail, "\n";
}
--------------------------------

	-Heikki

Stephen Henderson wrote:
> 
> I am having trouble handling run-time errors in my code.
> 
> Simply I am trying to go through a batch of accession no. and connect to
> Genbank to download sequences and write them to a file.
> But I only get so far before I get some sort of connection or unrecognised
> error I have tried using eval to handle this but the program just dies
> instead of continuing.
> 
> Here is the code
> 
> open (ACCS, "C:\\Perl\\BioPerl-0.7\\Seqdata\\Accessionlist.txt") or die
> "can't open Accessions file";
> $seqout = Bio::SeqIO->new( '-format' => 'Fasta', -file =>"+>DNAout")or die
> "can't open DNAoutput file";
> 
> @ets=<ACCS>;
> $ets_count=@ets;
> $gb= new Bio::DB::GenBank();
> 
>         for($i=0; $i < $ets_count; $i++)
>         {
>                 eval
>                 {
>                         $seq = $gb->get_Seq_by_id($ets[$i]);
>                         $detail= $seq->display_id();
>                         print $detail, "\n";
>                 };
> 
> The errors are not however caught and instead I get
> 
> "Can't call method "display_id" on an undefined value at NCBI.pl line 22"
> and
> "Can't call method "seq" on an undefined value at Bio/SeqIO/fasta.pm line
> 166"
> 
> Does anyone see the problem with using eval here?
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l@bioperl.org
> http://bioperl.org/mailman/listinfo/bioperl-l

-- 
______ _/      _/_____________________________________________________
      _/      _/                      http://www.ebi.ac.uk/mutations/
     _/  _/  _/  Heikki Lehvaslaiho          heikki@ebi.ac.uk
    _/_/_/_/_/  EMBL Outstation, European Bioinformatics Institute
   _/  _/  _/  Wellcome Trust Genome Campus, Hinxton
  _/  _/  _/  Cambs. CB10 1SD, United Kingdom
     _/      Phone: +44 (0)1223 494 644   FAX: +44 (0)1223 494 468
___ _/_/_/_/_/________________________________________________________