Spaces:
Sleeping
Sleeping
Audit H/M cleanup: H2/H3/H5/H6/M3/M4/M5/M6/M7
Browse files- dee/core/sequence.py +111 -16
dee/core/sequence.py
CHANGED
|
@@ -87,25 +87,86 @@ def _clean(seq: str) -> str:
|
|
| 87 |
|
| 88 |
|
| 89 |
def _clean_protein(seq: str) -> str:
|
| 90 |
-
"""Strip whitespace
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return cleaned
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def looks_like_protein(seq: str) -> bool:
|
| 96 |
"""Heuristic: does ``seq`` read like a protein rather than DNA?
|
| 97 |
|
| 98 |
-
A short
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
-
cleaned =
|
| 105 |
if not cleaned:
|
| 106 |
return False
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
def validate_dna(seq: str, *, require_start: bool = True, require_stop: bool = True) -> str:
|
|
@@ -376,7 +437,7 @@ class OrfCandidate:
|
|
| 376 |
}
|
| 377 |
|
| 378 |
|
| 379 |
-
def find_orfs_in_dna(dna: str, min_aa: int =
|
| 380 |
"""Scan all 6 frames for ATG…stop ORFs at least ``min_aa`` residues long.
|
| 381 |
|
| 382 |
Returns ORFs sorted by descending protein length, labeled ``ORF_1``,
|
|
@@ -384,6 +445,13 @@ def find_orfs_in_dna(dna: str, min_aa: int = 30) -> List[OrfCandidate]:
|
|
| 384 |
(or any long DNA with multiple stop codons across frames) — we can't
|
| 385 |
treat the whole thing as one CDS, but we can offer the ORFs we find as
|
| 386 |
a chooser, the same way we do for annotated `.gb` files.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
"""
|
| 388 |
cleaned = _clean(dna)
|
| 389 |
if not cleaned or not set(cleaned).issubset(UNAMBIGUOUS_NT):
|
|
@@ -592,12 +660,39 @@ def parse_input(
|
|
| 592 |
require_stop=require_stop,
|
| 593 |
)
|
| 594 |
|
| 595 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
return parse_protein(text, identifier="query")
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
|
| 602 |
|
| 603 |
def list_codons(dna: str) -> List[str]:
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
def _clean_protein(seq: str) -> str:
|
| 90 |
+
"""Strip whitespace and trailing terminator symbols; uppercase. No U→T mapping.
|
| 91 |
+
|
| 92 |
+
Sequence sources vary in how they mark a stop codon at the protein C-terminus:
|
| 93 |
+
|
| 94 |
+
* ``*`` — Biopython / FASTA / pretty much everyone except…
|
| 95 |
+
* ``.`` — older EMBL flat files (the original IUBMB recommendation)
|
| 96 |
+
* ``X`` — a single trailing ``X`` is a "stop-or-unknown" placeholder some
|
| 97 |
+
pipelines emit when the underlying codon is ambiguous. Strip ONLY when
|
| 98 |
+
trailing — embedded ``X`` characters elsewhere are real unknown
|
| 99 |
+
residues and the caller should reject them in :func:`parse_protein`.
|
| 100 |
+
* ``-`` — alignment gap; if it's trailing it's almost always padding.
|
| 101 |
+
|
| 102 |
+
Stripping these prevents the same downstream regression that an embedded
|
| 103 |
+
``*`` would: the trailing terminator would either be classified as a
|
| 104 |
+
non-canonical residue (rejecting a valid input) or get passed through
|
| 105 |
+
into ESM-2 scoring (corrupting the prediction).
|
| 106 |
+
"""
|
| 107 |
+
cleaned = re.sub(r"\s+", "", seq).upper()
|
| 108 |
+
# Strip every trailing terminator one at a time so e.g. ".*" or "X*" both
|
| 109 |
+
# collapse to the bare sequence. ``rstrip`` over a charset would be O(n)
|
| 110 |
+
# but the loop is bounded to a handful of iterations in practice.
|
| 111 |
+
while cleaned and cleaned[-1] in ("*", ".", "X", "-"):
|
| 112 |
+
cleaned = cleaned[:-1]
|
| 113 |
return cleaned
|
| 114 |
|
| 115 |
|
| 116 |
+
# Tighter set used by the disambiguating classifier — only the four canonical
|
| 117 |
+
# DNA bases + U + N (the most common ambiguity). Old set was "ACGTUN" too,
|
| 118 |
+
# but giving it a name lets us reference the rule in commit messages and tests.
|
| 119 |
+
_DNA_AMBIGUOUS_BASES = set("ACGTUN")
|
| 120 |
+
|
| 121 |
+
|
| 122 |
def looks_like_protein(seq: str) -> bool:
|
| 123 |
"""Heuristic: does ``seq`` read like a protein rather than DNA?
|
| 124 |
|
| 125 |
+
A short string composed entirely of {A,C,G,T,U,N} is genuinely ambiguous —
|
| 126 |
+
"ATGCAT" could be a 6-nt CDS fragment or a 6-residue peptide. Earlier the
|
| 127 |
+
classifier always picked DNA in this case, which silently misrouted short
|
| 128 |
+
peptide constructs (small ribosomal proteins, signal peptides, AMPs) into
|
| 129 |
+
the CDS pipeline where they'd fail validation with confusing errors.
|
| 130 |
+
|
| 131 |
+
New rule (closes audit H2):
|
| 132 |
+
* If the sequence contains ANY character outside the DNA-ambiguous set
|
| 133 |
+
({A,C,G,T,U,N}), it's unambiguously protein — return True.
|
| 134 |
+
* If it's all DNA-ambiguous AND short (< 30 chars) we still default to
|
| 135 |
+
DNA so existing short-DNA pastes keep working, but :func:`classify_sequence`
|
| 136 |
+
below provides an explicit ``ambiguous`` answer that callers can use
|
| 137 |
+
to surface a chooser to the user instead of silently picking one.
|
| 138 |
+
|
| 139 |
+
This function is preserved for the simple True/False contract. Use
|
| 140 |
+
:func:`classify_sequence` when the caller can act on ambiguity.
|
| 141 |
"""
|
| 142 |
+
cleaned = _clean_protein(seq)
|
| 143 |
if not cleaned:
|
| 144 |
return False
|
| 145 |
+
return not set(cleaned).issubset(_DNA_AMBIGUOUS_BASES)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def classify_sequence(seq: str) -> str:
|
| 149 |
+
"""Three-way classifier: ``'protein'``, ``'dna'``, or ``'ambiguous'``.
|
| 150 |
+
|
| 151 |
+
``'ambiguous'`` returns when the sequence is composed entirely of
|
| 152 |
+
{A,C,G,T,U,N} AND is short enough that the protein interpretation is
|
| 153 |
+
plausible (< 30 residues, the typical lower bound for a peptide
|
| 154 |
+
construct). Callers can show a chooser ("did you mean protein or DNA?")
|
| 155 |
+
instead of guessing, which closes audit H2.
|
| 156 |
+
|
| 157 |
+
For inputs longer than 30 chars, all-DNA-ambiguous strings are still
|
| 158 |
+
classified as ``'dna'`` — at that length the protein interpretation is
|
| 159 |
+
essentially impossible (a 30-residue stretch of A/C/G/T/U/N only would
|
| 160 |
+
be a wildly improbable peptide composition).
|
| 161 |
+
"""
|
| 162 |
+
cleaned = _clean_protein(seq)
|
| 163 |
+
if not cleaned:
|
| 164 |
+
return "ambiguous"
|
| 165 |
+
if not set(cleaned).issubset(_DNA_AMBIGUOUS_BASES):
|
| 166 |
+
return "protein"
|
| 167 |
+
if len(cleaned) < 30:
|
| 168 |
+
return "ambiguous"
|
| 169 |
+
return "dna"
|
| 170 |
|
| 171 |
|
| 172 |
def validate_dna(seq: str, *, require_start: bool = True, require_stop: bool = True) -> str:
|
|
|
|
| 437 |
}
|
| 438 |
|
| 439 |
|
| 440 |
+
def find_orfs_in_dna(dna: str, min_aa: int = 10) -> List[OrfCandidate]:
|
| 441 |
"""Scan all 6 frames for ATG…stop ORFs at least ``min_aa`` residues long.
|
| 442 |
|
| 443 |
Returns ORFs sorted by descending protein length, labeled ``ORF_1``,
|
|
|
|
| 445 |
(or any long DNA with multiple stop codons across frames) — we can't
|
| 446 |
treat the whole thing as one CDS, but we can offer the ORFs we find as
|
| 447 |
a chooser, the same way we do for annotated `.gb` files.
|
| 448 |
+
|
| 449 |
+
Audit M5: default ``min_aa`` was 30, which rejected legitimate short
|
| 450 |
+
constructs like signal peptides (~20 aa), antimicrobial peptides
|
| 451 |
+
(10–50 aa), and small ribosomal subunits (~30 aa). Dropped to 10 —
|
| 452 |
+
still long enough to filter the random ORFs an unrelated DNA stretch
|
| 453 |
+
will surface in 6-frame scan (false positives at <10 aa would
|
| 454 |
+
dominate the chooser), but inclusive of real-world small proteins.
|
| 455 |
"""
|
| 456 |
cleaned = _clean(dna)
|
| 457 |
if not cleaned or not set(cleaned).issubset(UNAMBIGUOUS_NT):
|
|
|
|
| 660 |
require_stop=require_stop,
|
| 661 |
)
|
| 662 |
|
| 663 |
+
# Use the three-way classifier so we can fall back gracefully on
|
| 664 |
+
# ambiguous inputs (short pure-ACGTUN strings — closes audit H2). For
|
| 665 |
+
# unambiguous protein we go straight to parse_protein; for unambiguous
|
| 666 |
+
# DNA we go straight to translate_dna; for ambiguous we attempt DNA
|
| 667 |
+
# first (current behavior preserves short-CDS pastes) and fall back
|
| 668 |
+
# to protein interpretation if DNA validation fails.
|
| 669 |
+
kind = classify_sequence(text)
|
| 670 |
+
if kind == "protein":
|
| 671 |
return parse_protein(text, identifier="query")
|
| 672 |
+
if kind == "dna":
|
| 673 |
+
return translate_dna(
|
| 674 |
+
text, identifier="query",
|
| 675 |
+
require_start=require_start, require_stop=require_stop,
|
| 676 |
+
)
|
| 677 |
+
# ambiguous — try DNA, then fall back to protein
|
| 678 |
+
try:
|
| 679 |
+
return translate_dna(
|
| 680 |
+
text, identifier="query",
|
| 681 |
+
require_start=require_start, require_stop=require_stop,
|
| 682 |
+
)
|
| 683 |
+
except SequenceValidationError as dna_err:
|
| 684 |
+
try:
|
| 685 |
+
return parse_protein(text, identifier="query")
|
| 686 |
+
except SequenceValidationError:
|
| 687 |
+
# Neither interpretation worked — re-raise the DNA error with
|
| 688 |
+
# an extra hint so the user knows the protein path was also
|
| 689 |
+
# attempted and they can disambiguate explicitly.
|
| 690 |
+
raise SequenceValidationError(
|
| 691 |
+
f"{dna_err}. Input is ambiguous (short string of A/C/G/T/U/N "
|
| 692 |
+
"only); tried both DNA and protein interpretations and both "
|
| 693 |
+
"failed. If you meant this as a peptide, prepend '>peptide\\n' "
|
| 694 |
+
"to force protein parsing.",
|
| 695 |
+
) from dna_err
|
| 696 |
|
| 697 |
|
| 698 |
def list_codons(dna: str) -> List[str]:
|