Skip to content

Alphabet PendingDeprecationWarning when I didn't use Alphabet #2488

Closed

Description

@donkirkby

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

  1. Create a virtual environment.
  2. Install Pytest and Biopython.
  3. Create the test file shown above.
  4. 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

  1. donkirkby commented on Jan 3, 2020

    @donkirkby
    ContributorAuthor

    Assuming that Bio.SeqIO.parse() is not actually deprecated, I disabled the warning from Pytest by configuring its warning section in a pytest.ini file.

    [pytest]
    filterwarnings = ignore::PendingDeprecationWarning:Bio.Alphabet
    

    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 Alphabet object.

  2. peterjc commented on Jan 3, 2020

    @peterjc
    Member

    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.

    PendingDeprecationWarning ought 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.

  3. Zulko commented on Jan 22, 2020

    @Zulko

    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 enable SeqIO.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:

    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord
    from Bio import SeqIO
    seq = Seq("ATGCGTGCAT")
    record = SeqRecord(seq=seq)
    SeqIO.write(record, "test_write.gb", "genbank")

    This raises ValueError: Need a Nucleotide or Protein alphabet. The only way I found to solve this is to add alphabet=DNAAlphabet() in Seq(...). Is there another way?

    Thanks in advance!

  4. peterjc commented on Jan 22, 2020

    @peterjc
    Member

    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 .alphabet but a replacement .molecule_type might 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.

  5. Zulko commented on Jan 22, 2020

    @Zulko

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

  6. peterjc commented on Jan 22, 2020

    @peterjc
    Member

    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.

  7. mdehoon commented on Jan 23, 2020

    @mdehoon
    Contributor

    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.

    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.

  8. peterjc commented on Jul 29, 2020

    @peterjc
    Member

    We've gone with removal of the Seq object's .alphabet, and instead are recording molecule type as a string in the SeqRecord object'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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions