Repository navigation
Description
Thanks for publishing the Biopython project, my team has found it very useful.
Setup
I am reporting a problem with Biopython 1.76, Python 3.6.9, PyTest 5.32, and Ubuntu 18.04 as follows:
# test_seq.py
from io import StringIO
from Bio.SeqIO import parse
def test_seq():
fasta_source = StringIO("""\
>example
ACTG
""")
result = [(seq.name, seq.seq) for seq in parse(fasta_source, 'fasta')]
assert result == [('example', 'ACTG')]Then run the test with Pytest like this:
$ pytest
Expected behaviour
$ pytest
================================================================================== test session starts ==================================================================================
platform linux -- Python 3.6.9, pytest-5.3.2, py-1.7.0, pluggy-0.13.1
rootdir: /home/don/scratch/bio_test
collected 1 item
test_seq.py . [100%]
=================================================================================== 1 passed in 0.01s ===================================================================================
$
Actual behaviour
$ pytest
================================================================================== test session starts ==================================================================================
platform linux -- Python 3.6.9, pytest-5.3.2, py-1.7.0, pluggy-0.13.1
rootdir: /home/don/scratch/bio_test
collected 1 item
test_seq.py . [100%]
=================================================================================== warnings summary ====================================================================================
/mnt/data/don/git/MiCall/venv_micall/lib/python3.6/site-packages/Bio/Alphabet/__init__.py:26
/mnt/data/don/git/MiCall/venv_micall/lib/python3.6/site-packages/Bio/Alphabet/__init__.py:26: PendingDeprecationWarning: We intend to remove or replace Bio.Alphabet in 2020, ideally avoid using it explicitly in your code. Please get in touch if you will be adversely affected by this. https://github.com/biopython/biopython/issues/2046
PendingDeprecationWarning)
-- Docs: https://docs.pytest.org/en/latest/warnings.html
============================================================================= 1 passed, 1 warning in 0.78s ==============================================================================
$
Steps to reproduce
- Create a virtual environment.
- Install Pytest and Biopython.
- Create the test file shown above.
- Run Pytest.
What I'm Asking For
Please clarify whether Bio.SeqIO.parse() is also deprecated, because importing that generates the warning message.
If it is deprecated, I'd appreciate it if you would mark it that way and document the recommended replacement.
If it is not deprecated, I'd appreciate it if you only raise a warning when client code calls something deprecated, like directly instantiating an Alphabet object. Maybe you could swallow the warnings when you import from within Biopython, or maybe inspect the call stack? If that's not practical, perhaps you could add some details to issue #2046 about what exactly is deprecated, and how to deal with the warnings when using code that isn't actually deprecated.
The problem started when I recently upgraded Biopython from 1.72 to 1.76 and upgraded Pytest from 3.5.0 to 5.3.2. I think Pytest started reporting the warnings, and I'm not sure if Biopython was already generating the warnings.
I read through issue #2046 that was mentioned in the warning message, and getting rid of Bio.Alphabet sounds reasonable to me. (We're not directly using it.) I was surprised that issue #2046 was locked - you might want to clarify what the warning message means by "get in touch".
Thanks again for publishing Biopython.
Activity
donkirkby commentedon Jan 3, 2020
Assuming that
Bio.SeqIO.parse()is not actually deprecated, I disabled the warning from Pytest by configuring its warning section in apytest.inifile.The downside to this approach is that we won't see any warnings if someone on the project starts using some code that is actually deprecated, like creating an
Alphabetobject.Ignore deprecation warnings about Bio.Alphabet, because we don't use it.
peterjc commentedon Jan 3, 2020
First the good news: Your example code is perfectly fine, no explicit use of
Bio.Alphabet. We wouldn't expect you to see this warning.PendingDeprecationWarningought to be silent by default. Something in pytest is not interacting as we might hope. See also #2312 with a similar discussion, including asking if we can clarify the warning's wording?Given we're now into 2020, expect some more active changes - likely to include some not-silent-by-default not-pending deprecation warnings.
Zulko commentedon Jan 22, 2020
Hi @peterjc, just came accross this warning and I was wondering if there is a discussion somewhere on the deprecation of Bio.Alphabet. Will all Alphabet subclasses be removed? It seems that they are eveywhere in Biopython and my projects use
DNAAlphabet()a lot as a way to enableSeqIO.write(record, 'genbank')(this errors for sequences which don't have a DNAAlphabet). Is there a way around this?To be more specific, here is the typical scenario where I need a DNAAlphabet:
This raises
ValueError: Need a Nucleotide or Protein alphabet. The only way I found to solve this is to addalphabet=DNAAlphabet()inSeq(...). Is there another way?Thanks in advance!
peterjc commentedon Jan 22, 2020
The meta issue is #2046 (locked to prevent never ending discussions without anything concrete).
The need for some way to record the sequence type in SeqIO is one of the few things blocking a simple removal - GenBank and several other file formats need this, but only at quite a high level.
I started looking at a reduced alphabet, essentially an enum of DNA, RNA, nucleotide, protein or unknown. In https://github.com/peterjc/biopython/tree/alpha_lite I was trying this in place of the existing Seq object's
.alphabetbut a replacement.molecule_typemight be cleaner. With this approach a backward compatible transition seemed very doable.Another idea which would work with a complete alphabet removal would be to record this module type information in the SeqRecord instead of the Seq object, but that can't then be used for basic sanity checking of the biological methods of the Seq object.
Right now having dropped Python 2 support, we're focussed on Python 3 cleanup and black style for Biopython 1.77 - the alphabet effort is likely to be the next big project.
Zulko commentedon Jan 22, 2020
Thanks for your work on this, anything that can make Biopython simpler sounds great to me! I like the molecule_type approach. Do I understand correctly that there is currently no way to bypass Alphabet to write genbanks? (I'm looking for a way to make my libraries future-proof, without freezing the Biopython version).
peterjc commentedon Jan 22, 2020
If your focus is GenBank specific, you could avoid SeqIO and SeqRecord altogether, and use the Bio.GenBank specific record objects - but my guess is that side of the code is less used. Part of the removal work will include documenting how to write backward compatible code if reasonably possible.
mdehoon commentedon Jan 23, 2020
I expect that in the near future we won't have separate Seq and SeqRecord classes, but just a single class that can do both.
Ignore Biopython deprecation warning that we cannot control
peterjc commentedon Jul 29, 2020
We've gone with removal of the
Seqobject's.alphabet, and instead are recording molecule type as a string in theSeqRecordobject's annotation.I've turned this use case into an example on #3156 (covering the old way, new way, and a proposal which would work on both for people who need that). Thank you.