This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Supress/silence warnings from BioPython

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

2 answers

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

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)
python -W ignore your_script.py

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

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

Did you check the link that I posted in comments?

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.

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")

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.

Log in to answer this question.