codon-constrain
codon-constrain is a dependency-free-runtime synonymous codon optimizer. It maximizes the sum of log relative codon adaptiveness for bundled E. coli or human profiles while preserving translation and jointly enforcing whole-construct GC, forbidden motifs on either strand, homopolymer length, and fixed DNA flanks.
Install
python -m pip install codon-constrain
For development:
python -m pip install -e '.[dev]'
pytest --cov=codon_constrain --cov-branch --cov-fail-under=100
ruff check .
Quickstart
Given enzyme.faa:
>enzyme
KK
run:
codon-constrain enzyme.faa \
--output optimized.fasta \
--report report.json \
--host ecoli \
--input-type protein \
--gc-min 0.30 --gc-max 0.80 \
--forbid GAATTC \
--homopolymer-max 3
The FASTA contains the complete flanked construct. The JSON records translation preservation, the CAI-like geometric mean of relative adaptiveness, GC fraction, constraint checks, whether the solution is exact, and the unconstrained greedy baseline.
Python API:
from codon_constrain import Constraints, optimize
result = optimize(
"KK",
host="ecoli",
input_type="protein",
constraints=Constraints(homopolymer_max=3),
)
assert result.coding_sequence == "AAGAAA"
assert result.report["translation_preserved"]
Coding-DNA FASTA is also accepted with --input-type dna (or auto when unambiguous). Stop codons and unsupported residues are rejected with position-aware messages.
Algorithm and exactness
flowchart LR; A[protein or coding DNA] --> S[DP states: GC count × suffix × homopolymer run]; S --> T[extend by synonymous codons]; T --> C{constraints: GC, motifs, flanks}; C -->|feasible| B[max log adaptiveness]; B --> R[traceback: optimal construct + report]
At each amino-acid position, dynamic programming extends every reachable finite state by one synonymous codon. A state consists of:
- total GC count;
- the suffix needed to recognize a forbidden motif at the next base;
- the final base and current homopolymer run length.
Motifs are expanded to include their reverse complements. Fixed 5' and 3' flanks pass through the same state machine, so violations spanning a flank/coding boundary are detected. For identical states, only the highest-scoring prefix is retained because all future feasibility and score contributions depend solely on that state. Therefore the default unpruned mode proves the maximum score over these represented finite-state constraints; this is dynamic programming, not full-sequence enumeration. --beam-width N retains only the best N states per residue to cap memory, and the report honestly marks that result non-exact.
The objective is
sum(log(relative_adaptiveness(codon)))
and its reported CAI-like value is the geometric mean, exp(log_score / codon_count). Bundled weights are normalized within each synonymous family; they are useful deterministic host profiles rather than an expression guarantee.
Reproducible capability evidence
The test suite contains an exhaustive tiny oracle for protein KTL: it enumerates only that test's 36 synonymous sequences and verifies that exact DP returns the same feasible optimum under GC, motif, and homopolymer constraints. This independently checks the optimality recurrence.
A deterministic material improvement over greedy selection is exercised end to end:
protein: KK
host: E. coli
greedy best codons: AAAAAA (violates homopolymer_max=3)
exact DP result: AAGAAA (translation KK; feasible)
Run the evidence:
pytest tests/test_optimizer.py::test_exact_matches_tiny_brute_force_oracle \
tests/test_optimizer.py::test_greedy_failure_dp_success_is_deterministic_capability_example \
tests/test_cli.py::test_installed_module_cli_end_to_end_from_clean_directory
Mutation testing
From the repository root, reproduce the mutation run with:
source .venv/bin/activate
mutmut run
mutmut results
The run generated 807 mutants and killed 768 (95.17%). The remaining 39 were individually reviewed as behavior-equivalent under the supported contract, not missed mutants. There were zero suspicious results and zero timeouts. Observable CLI help changes were killed rather than classified as equivalent.
| Reviewed equivalent rationale | Count |
|---|---|
| Explicit UTF-8 versus the platform default, and type-cast identities | 9 |
FASTA header tokenization with an equivalent maxsplit |
1 |
| Sentinels made redundant by validated DNA and motif alphabets | 6 |
| Surplus suffix/initial state retained by the DP | 7 |
| Deterministic tie-breaking and beam/pruning boundaries | 8 |
| Reporting, default-value, and formatting identities | 8 |
| Total reviewed equivalents | 39 |
Limitations
- Optimization models synonymous codon choice only; it does not predict expression, mRNA folding, ribosome traffic, splicing, synthesis success, or biological safety.
- Inputs must use the 20 canonical amino acids or stop-free
ACGTcoding DNA. Ambiguous bases, stop codons, and selenocysteine are unsupported. - GC is enforced globally across the complete flanked construct, not in sliding windows.
- Long motifs and broad GC ranges can produce many states. Beam search bounds memory but removes the optimality proof.
- Host tables are built-in relative profiles and are not tissue-, strain-, condition-, or gene-specific. Validate designs experimentally and against current domain-specific requirements.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codon_constrain-1.0.0.tar.gz.
File metadata
- Download URL: codon_constrain-1.0.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
775891f25020382463efd4d9957706f6f8e163c9f8bf9f5a238c039c6a5d23a3
|
|
| MD5 |
9b641b802e1f1cb49335c101ba3684d6
|
|
| BLAKE2b-256 |
a45fbc30fa5ecc3d80e9c675b4a35acb7cbf4aacc7bd22532648ce88f1797de9
|
Provenance
The following attestation bundles were made for codon_constrain-1.0.0.tar.gz:
Publisher:
publish.yml on bmouler/codon-constrain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codon_constrain-1.0.0.tar.gz -
Subject digest:
775891f25020382463efd4d9957706f6f8e163c9f8bf9f5a238c039c6a5d23a3 - Sigstore transparency entry: 2443166064
- Sigstore integration time:
-
Permalink:
bmouler/codon-constrain@07a9b99fb7020613fc1c2275b8a89074761d399a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/bmouler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@07a9b99fb7020613fc1c2275b8a89074761d399a -
Trigger Event:
release
-
Statement type:
File details
Details for the file codon_constrain-1.0.0-py3-none-any.whl.
File metadata
- Download URL: codon_constrain-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
228cb2039e9667ffa0738b073b7833e78ea5975a8768c862320146eaa7f24712
|
|
| MD5 |
8ba3e77ebd31acfc0263ccdde9b1a7a7
|
|
| BLAKE2b-256 |
f4c60a692aac46343f6a41089fc478d5cb2e8485795835b1c8f6d73e66aa2733
|
Provenance
The following attestation bundles were made for codon_constrain-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on bmouler/codon-constrain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codon_constrain-1.0.0-py3-none-any.whl -
Subject digest:
228cb2039e9667ffa0738b073b7833e78ea5975a8768c862320146eaa7f24712 - Sigstore transparency entry: 2443166140
- Sigstore integration time:
-
Permalink:
bmouler/codon-constrain@07a9b99fb7020613fc1c2275b8a89074761d399a -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/bmouler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@07a9b99fb7020613fc1c2275b8a89074761d399a -
Trigger Event:
release
-
Statement type: