[Biopython] error with entrez id code
Peter Cock
p.j.a.cock at googlemail.com
Sat Oct 8 14:10:12 UTC 2011
On Fri, Oct 7, 2011 at 6:34 PM, Dilara Ally <dilara.ally at gmail.com> wrote:
> Is it always the same record that breaks? If so, what is the ID so we can
> try it out.
>
> If not, then it looks like a random network error, maybe you can stick a
> try/except in to refetch the data?
>
> Hi Peter
>
> Individually the identifier has no problem calling up the record, but the
> problem seems to be in the loop. As a newbie, what is a try/except?
>
> Thanks.
By try/except I mean use Python's error handling mechanism to
spot when there is a network error. See:
http://docs.python.org/tutorial/errors.html
e.g. Something like this would give you a second chance.
Note that exception httplib.IncompleteRead is a subclass
of the more general HTTPException, see:
http://docs.python.org/library/httplib.html
from httplib import HTTPException
try:
handle=Entrez.efetch(db="nucleotide", id=NewID,rettype="gb") #
rettype="gb" is GenBank format or XML format retmode="xml"
record=SeqIO.read(handle,"genbank")
handle.close()
except HTTPException, e:
print "Network problem: %s" % e
print "Second (and final) attempt..."
handle=Entrez.efetch(db="nucleotide", id=NewID,rettype="gb") #
rettype="gb" is GenBank format or XML format retmode="xml"
record=SeqIO.read(handle,"genbank")
handle.close()
If the second attempt fails, you'll get an exception like before.
There are more elegant ways to write that (with less repetition,
and making multiple retries easy), but I'm trying to keep this
simple as an introductory example.
Peter
More information about the Biopython
mailing list