Skip to content

feat(transforms)!: make translate keyword-only and strict - #19

Open
Hoeze wants to merge 3 commits into
feat/variant-matcherfrom
feat/transforms-cleanup
Open

Hoeze wants to merge 3 commits into
feat/variant-matcherfrom
feat/transforms-cleanup

Conversation

@Hoeze

@Hoeze Hoeze commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

tl;dr: translate takes its options only as keyword arguments and raises a ValueError that names unknown codons, also lowercase ones. TRANSLATION_TABLE is read-only. The transform classes are removed, and the functions of kipoiseq2.transforms.functional move to the module kipoiseq2.transforms.

translate(seq, *, transl_table=1, transl_except=None) takes transl_table and transl_except only as keyword arguments. A codon that is not in the genetic code, e.g. ATN, raises a ValueError that names it, instead of a KeyError. The lookup stays case-sensitive, as in Encoder and Tokenizer, so a lowercase codon raises this ValueError too. If the genetic code has the uppercase form of the codon, the message hints at seq.upper() and at FastaStringExtractor, which uppercases by default. translate builds this message with the helper of kipoiseq2.encoders that Encoder and Tokenizer use for unknown letters.

translate raises a TypeError for arguments of the wrong type: a seq that is not a str, a transl_table that is not an int, a transl_except that is not a mapping, and positions or amino acids of the wrong type in transl_except. Before, a position that was not an int raised a ValueError, and a list as transl_except raised an AttributeError.

A length that is not a multiple of 3 still raises a ValueError, unless transl_except covers the partial last codon, e.g. a mitochondrial stop that poly(A) completes.

TRANSLATION_TABLE is a read-only MappingProxyType. Before, it was a dict, and translate used this same dict for the genetic codes 1 and 11, so changing it changed the result of translate. The private table of genetic code 2 is read-only too. The attribute docstring of TRANSLATION_TABLE hides its 64 codons on the API page.

Compose, DummyAxis, SwapAxes and ResizeInterval are removed. They come from the YAML configs of the Kipoi dataloaders. Without kipoi these four classes are thin wrappers: Compose is a for loop, DummyAxis and SwapAxes call np.expand_dims and np.swapaxes, and ResizeInterval calls Interval.resize with stored arguments. MIGRATING.md gives a one-line replacement for each class, e.g. ResizeInterval(width, anchor)(interval) becomes interval.resize(width, anchor=anchor). For the 0.1 behavior of the anchors "start" and "end", pass use_strand=False, as for resize_interval. Where a callable is needed, Encoder(...).one_hot replaces OneHot(...), as an earlier PR of the stack describes.

The functions and constants of kipoiseq2.transforms.functional are in the module kipoiseq2.transforms now, e.g. from kipoiseq2.transforms import translate. The package kipoiseq2.transforms and its module functional are gone without an alias, so import kipoiseq2.transforms.functional raises a ModuleNotFoundError. MIGRATING.md, the docstrings and kipoiseq2.extractors.vcf_seq use the new path. The API reference merges the pages "Functional transforms" and "Class-based transforms" into one page "Transforms", next to the page "Encoders". The tests move from tests/test_0_transforms_functional.py to tests/test_0_transforms.py.

MIGRATING.md lists the changes at the end of "Breaking changes in 0.2".

BREAKING CHANGE: pass transl_table and transl_except to translate as keyword arguments, e.g. translate(seq, transl_table=2). translate raises a ValueError instead of a KeyError for codons that are not in the genetic code, also for lowercase codons. TRANSLATION_TABLE is read-only: use dict(TRANSLATION_TABLE) for a table that you can change. Compose, DummyAxis, SwapAxes and ResizeInterval are removed: use np.expand_dims, np.swapaxes, interval.resize(width, anchor=anchor) or a function that calls the steps in order. As for resize_interval, pass use_strand=False to interval.resize for the 0.1 behavior of the anchors "start" and "end". Import the functions and constants of kipoiseq2.transforms.functional from kipoiseq2.transforms, e.g. from kipoiseq2.transforms import translate.

feat(transforms)!: remove the transform classes
feat(transforms)!: move transforms.functional into kipoiseq2.transforms

@Hoeze
Hoeze added this pull request to stack #16 October 16, 2026 23:52
@Hoeze
Hoeze force-pushed the feat/transforms-cleanup branch from 96d9607 to 93c1b72 Compare October 17, 2026 14:08
@Hoeze Hoeze changed the title feat(transforms)!: make translate keyword-only and case-insensitive feat(transforms)!: make translate keyword-only and strict Oct 17, 2026
@Hoeze
Hoeze force-pushed the feat/transforms-cleanup branch from 93c1b72 to 8d04490 Compare October 17, 2026 16:22
Hoeze added 3 commits October 17, 2026 18:36
translate takes transl_table and transl_except only as keyword
arguments. A codon that is not in the genetic code raises a ValueError
that names it, instead of a KeyError. The lookup stays case-sensitive,
as in the encoders, so a lowercase codon raises this ValueError too. If
the genetic code has the uppercase form of the codon, the message hints
at seq.upper() and at FastaStringExtractor, which uppercases by
default. Arguments of the wrong type raise a TypeError.

TRANSLATION_TABLE is a read-only MappingProxyType, so changing it can
no longer change the result of translate. The private table of the
vertebrate mitochondrial code is read-only too.

A length that is not a multiple of 3 still raises a ValueError, unless
transl_except covers the partial last codon.

BREAKING CHANGE: pass transl_table and transl_except as keyword
arguments, e.g. translate(seq, transl_table=2). Unknown codons, also
lowercase ones, raise a ValueError instead of a KeyError.
TRANSLATION_TABLE is read-only: copy it with dict(TRANSLATION_TABLE) to
change it.
Compose, DummyAxis, SwapAxes and ResizeInterval come from the YAML
configs of the Kipoi dataloaders. Without kipoi these four classes are
thin wrappers: Compose is a for loop, DummyAxis and SwapAxes call
np.expand_dims and np.swapaxes, and ResizeInterval calls
Interval.resize with stored arguments. The module
kipoiseq2.transforms.transforms and its API page go with them.

MIGRATING.md gives a one-line replacement for each class. The bullet
for OneHot no longer mentions Compose. It names the method
Encoder.one_hot as the callable instead.

BREAKING CHANGE: Compose, DummyAxis, SwapAxes and ResizeInterval are
removed. Use np.expand_dims, np.swapaxes,
interval.resize(width, anchor=anchor), or a function that calls the
steps in order.
Without the transform classes, the package kipoiseq2.transforms only
held the module functional. Its functions and constants are in the
module kipoiseq2.transforms now, e.g. from kipoiseq2.transforms import
translate. kipoiseq2.transforms.functional is gone, without an alias.

The API reference has one page for kipoiseq2.transforms, and the tests
of the module are in tests/test_0_transforms.py.

BREAKING CHANGE: import the functions and constants of
kipoiseq2.transforms.functional from kipoiseq2.transforms, e.g.
from kipoiseq2.transforms import translate.
@Hoeze
Hoeze force-pushed the feat/transforms-cleanup branch from 8d04490 to e249795 Compare October 17, 2026 17:19

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant