Skip to content

Bio.Alphabet to molecule type transition for file output #3156

Closed

Description

@peterjc

Cross reference #2046, we are removing Bio.Alphabet. There are relatively few use-cases where it is needed, one being working with file formats which track DNA vs RNA vs protein. For example, the SeqXML format needs to know the kind of sequence.

I wanted to write down some examples, how they might have looked the old way, should look the new way, and if and how a backward compatible version might look. This should influence the documentation for Biopython 1.78, and perhaps the code itself.

Old fashioned, worked up to Biopython 1.77:

from Bio.Alphabet import generic_dna
from Bio import SeqIO
# This file has a single record only
record = SeqIO.read("Tests/Fasta/wisteria.nu", "fasta", generic_dna)
rec_start = record[:20]
SeqIO.write(rec_start, "start_only.xml", "seqxml")

Current style (with the code right now on master):

from Bio import SeqIO
# This file has a single record only
record = SeqIO.read("Tests/Fasta/wisteria.nu", "fasta")
rec_start = record[:20]
rec_start.annotations["molecule_type"] = "DNA"
SeqIO.write(rec_start, "start_only.xml", "seqxml")

Possible backward compatible form to run on both, assuming Bio.Alphabet is still here but deprecated - this does not look very nice:

import warnings
from Bio import BiopythonDeprecationWarning
with warnings.catch_warnings():
    warnings.simplefilter("ignore", BiopythonDeprecationWarning)
    from Bio.Alphabet import generic_dna

from Bio import SeqIO
record = SeqIO.read("Tests/Fasta/wisteria.nu", "fasta", generic_dna)
rec_start = record[:20]
rec_start.annotations["molecule_type"] = "DNA"  # harmless on older Biopython
SeqIO.write(rec_start, "start_only.xml", "seqxml")

Possible backward compatible form to run on both, assuming Bio.Alphabet is gone:

try:
    from Bio.Alphabet import generic_dna
except ImportError:
    generic_dna = None

from Bio import SeqIO
# This file has a single record only
record = SeqIO.read("Tests/Fasta/wisteria.nu", "fasta", generic_dna)
rec_start = record[:20]
rec_start.annotations["molecule_type"] = "DNA"
SeqIO.write(rec_start, "start_only.xml", "seqxml")

Both examples assume that SeqIO.read retains a stub alphabet argument (or at least, an optional third argument defaulting to None).

Hopefully only a minority of Biopython usage will require backward compatibility like this.

We could make molecule type an optional argument to SeqIO.read and SeqIO.parse (setting at input time), perhaps replacing the former optional alphabet argument.

We might make molecule type an optional argument to SeqIO.write (setting at output time), perhaps allowing:

from Bio import SeqIO
# This file has a single record only
record = SeqIO.read("Tests/Fasta/wisteria.nu", "fasta")
rec_start = record[:20]
SeqIO.write(rec_start, "start_only.xml", "seqxml", "DNA")

Activity

  1. peterjc commented on Jul 29, 2020

    @peterjc
    MemberAuthor

    Example based on #2488, old code using Bio.Alphabet:

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

    New version:

    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord
    from Bio import SeqIO
    seq = Seq("ATGCGTGCAT")
    record = SeqRecord(seq, id="test", annotations={"molecule_type": "DNA"})
    SeqIO.write(record, "test_write.gb", "genbank")

    Possible backward compatible version assuming Bio.Alphabet is simply removed:

    try:
        from Bio.Alphabet import generic_dna
    except ImportError:
        generic_dna is None
    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord
    from Bio import SeqIO
    if generic_dna:
        # Newer Biopython refuses second argument
        seq = Seq("ATGCGTGCAT", generic_dna)
    else:
        seq = Seq("ATGCGTGCAT")
    record = SeqRecord(seq, id="test", annotations={"molecule_type": "DNA"})
    SeqIO.write(record, "test_write.gb", "genbank")

    There is more than more way to write this - hopefully we can agree something we all find simple and clear to recommend as best practice?

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