Skip to content

Loosen restriction of FormattedSeq._table in Restriction module #4940

Closed

Description

@manulera

Hi @peterjc, nowadays the restriction module only allows IUPAC DNA characters (see below).

def _make_FormattedSeq_table() -> bytes:
table = bytearray(256)
upper_to_lower = ord("A") - ord("a")
for c in b"ABCDGHKMNRSTVWY": # Only allow IUPAC letters
table[c] = c # map uppercase to uppercase
table[c - upper_to_lower] = c # map lowercase to uppercase
return bytes(table)

However, even if only IUPAC characters are allowed, degenerate base codes are not interpreted as such (e.g. N is not treated as "any of ACGT" but as an N, character, so the below snippet returns an empty list without matches.

from Bio.Restriction import EcoRI
from Bio.Seq import Seq

print(EcoRI.search(Seq("NNNNNNNNNNN")))

Given this, I think it would be safe to remove this restriction and allow all letters. Below some use-cases where this would allow better usage of biopython. Will submit a PR on this.

Impact on generic use-case

This is a bit problematic for a biopython general use-case if people are using dU bases in DNA. This is done for USER cloning.

Impact on pydna

In addition, in pydna we are thinking to use non-DNA characters to represent single-strand DNA bits in sequences. For instance, the below DNA molecule

GATCGUAAAC
    CAUTUGCTAG   

PEXIGULAOCQFZJ

can be represented as a single string PEXIGULAOCQFZJ where PEXI represents ACGT on top strand only, and QFZJ represents CTAG on bottom strand only.

The problem is that these cannot be used for restriction using the search method of restriction enzymes because they have non-allowed characters.

Activity

  1. manulera commented on Feb 18, 2025

    @manulera
    ContributorAuthor
  2. added a commit that references this issue on Feb 18, 2025
    7a6743e
  3. peterjc commented on Feb 18, 2025

    @peterjc
    Member

    This module is without a dedicated maintainer and while there have been a few efforts to rewrite it (avoiding the current structure with a class per enzyme), that is unlikely to happen soon.

    I'm unclear how relaxing this check would help, given ambiguous based as not interpreted as such anyway - nor would your RNA extensions be understood.

  4. BjornFJohansson commented on Feb 19, 2025

    @BjornFJohansson
    Contributor

    @peterjc @manulera We have a use case in Pydna where we expanded the alphabet to deal with uracil as used in USER-cloning for example.

    The problem is not so much getting a result for non-standard bases, I think that part is fine the way it is currently.

    The problem is that the Bio.Restriction module cast all sequences as FormattedSeq where there is a check that will not allow initiating a FormattedSeq with anything else than what is in the matching dict defined in line 98 in the Bio.Restriction module.

    I think this check is not needed. IMHO, removing it would comply with the idea of the responsible user.

    That said, I think there are other options that would work as well. For example, the hard coded _table variable could be made a default argument of FormattedSeq.__init__.

  5. manulera commented on Feb 19, 2025

    @manulera
    ContributorAuthor

    Hi @peterjc I think the TLDR is:

    • As it is, restricting allowed characters to IUPAC ABCDGHKMNRSTVWY is arbitrary, since they are interpreted as the characters themselves and not as degenerate nucleotides.
    • Therefore, removing this restriction and allowing other letters should not produce unintended behaviour.
    • We have important use-cases where we want to pass sequences that have extra characters to the .search method on enzymes. Removing this constraint would enable that use-case
  6. peterjc commented on Feb 19, 2025

    @peterjc
    Member

    It is reassuring to have multiple voices in support when I feel out of my depth on an area of Biopython - so #4941 approved in principle, just a suggestion on the code comment following the dicussion here.

  7. BjornFJohansson commented on Feb 19, 2025

    @BjornFJohansson
    Contributor

    Thank you @peterjc and @manulera. This will remove some unnecessary code in Pydna.

  8. manulera commented on Feb 19, 2025

    @manulera
    ContributorAuthor

    Also @peterjc I am relatively familiar with the Restriction module by now. I don't have time for a full re-write (although I would love it to happen), but if you have some issues about the module feel free to tag me and I can try to help answering questions or fixing bugs if there are any.

  9. peterjc commented on Feb 19, 2025

    @peterjc
    Member

    Thank you @manulera - I'll try to remember that next time something is reported, or if we see renewed interest in a rewrite of the digest code.

  10. peterjc commented on Feb 19, 2025

    @peterjc
    Member

    Merged - thank you both!

  11. added a commit that references this issue on Feb 19, 2025
    650d3af
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