Supress/silence warnings from BioPython
2
2
Entering edit mode
7.4 years ago
Joe 21k

Does anyone know how to suppress warnings from BioPython? Specifically, the Experimental warning from the use of SearchIO?

This script is perfectly functional, but I'd like to get rid of the annoying message about SearchIO - I'm using it for such a basic function that I'm not concerned that it's still experimental

I've tried:

>>> import warnings
>>> from Bio import BiopythonWarning
>>> warnings.simplefilter('ignore', BiopythonWarning)

as per the BioPython website, but that doesn't work in this case. Changing to BiopythonExperimentalWarning isn't correct either.

biopython • 8.0k views
ADD COMMENT
0
Entering edit mode

I don't use Python, but this may be helpful (section 28.6.3)

https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

ADD REPLY
4
Entering edit mode
7.4 years ago
Joe 21k

With some help from Peter Cock on twitter, it turns out that warnings.catch_warnings():, is the correct way to go, but it should be around the module import, not the function call:

# Import SearchIO and suppress experimental warning
from Bio import BiopythonExperimentalWarning
with warnings.catch_warnings():
    warnings.simplefilter('ignore', BiopythonExperimentalWarning)
    from Bio import SearchIO
ADD COMMENT
0
Entering edit mode

I solved it with Joe's @jrj.healey help above like this:

from Bio import BiopythonWarning
import warnings
with warnings.catch_warnings():
    warnings.simplefilter('ignore', BiopythonWarning)
ADD REPLY
0
Entering edit mode
7.4 years ago
st.ph.n ★ 2.7k
python -W ignore your_script.py
ADD COMMENT
0
Entering edit mode

This is an 'external' solution. I guess OP wants to turn off from inside the script

ADD REPLY
0
Entering edit mode

Santosh is right, if there is an internal solution that would be great. -W is definitely worth knowing though thanks.

ADD REPLY
0
Entering edit mode

Did you check the link that I posted in comments?

ADD REPLY
0
Entering edit mode

Yeah I've looked at it, I'm struggling to make it work with BioPython though. It doesn't seem like it does anything the code I put in the OP shouldn't do, I think I just need to figure out what warnings.simplefilter('ignore', BiopythonWarning) should be changed to, to handle experimental warnings instead of general warnings.

ADD REPLY
0
Entering edit mode

That's pretty weird! My guess is that since BioPythonWarning is a subclass of Warning, the following code should work

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
ADD REPLY
0
Entering edit mode

Nope. Doesn't seem to. Unless I'm misusing it. Posts on SO say to wrap the offending code within the with block, but

with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        blast_result = SearchIO.read(resultHandle, 'blast-tab')

Doesn't make any difference.

ADD REPLY

Login before adding your answer.

Traffic: 763 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6