From 0a8298a7a84388c59438a71d05893d696a05a455 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 21:44:42 +0100 Subject: [PATCH 01/42] feat: remove truncate_values, which nothing calls GECKO removed truncateValues, and no function in geckopy calls truncate_values. update_prot_pool was never ported and merge_dlkcat_and_fuzzy_kcats is already a deprecated alias of merge_kcats; a test now checks its warning. --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 1 - src/geckopy/__init__.py | 2 - src/geckopy/kcat_tuning/__init__.py | 2 - src/geckopy/kcat_tuning/truncate_values.py | 84 ------------- tests/test_deprecated_aliases.py | 17 +++ tests/test_parity_fixes_g1a.py | 25 +--- tests/test_truncate_values.py | 140 --------------------- 8 files changed, 19 insertions(+), 253 deletions(-) delete mode 100644 src/geckopy/kcat_tuning/truncate_values.py delete mode 100644 tests/test_truncate_values.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 56ea62c..4d24430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. `merge_dlkcat_and_fuzzy_kcats` stays a deprecated alias of `merge_kcats`. * `geckopy.__version__` is the installed version of geckopy. * `cma` is a required dependency of geckopy and the extra `evotune` is removed. `import geckopy` imports `cma`. * `enzyme_usage` reports `measured_protein`, `demanded_protein` and `demand_ratio`, the usage of the enzymes with a measured concentration divided by their measured concentrations, and the same restricted to enzymes with a usage above 1e-7 mg/gDCW (`measured_protein_active`, `demand_ratio_active`). `report_enzyme_usage` reports `demand_ratio` and `demand_ratio_active`. GECKO's `getEnzymeUsage` and `reportEnzymeUsage` give the same values. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index dd4a3b4..1d74166 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -287,7 +287,6 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `sensitivityTuning` | `sensitivity_tuning` | **`[Py]`** Returns a `TunedKcatsResult` dataclass (kcat changes table + final flux). | | `sigmaFitter` | `fit_sigma` | **`[3→4]`** Returns the model fitted to the **optimal** sigma (GECKO 3 returned the last trial value, sigma = 1.0). **`[Py]`** Renamed; `sigma_fitter` kept as deprecated alias. Returns a `SigmaFitterResult`. | | `findMaxValue` | `find_max_value` | **`[3→4]`** Wildcard EC branch matches real codes. | -| `truncateValues` | `truncate_values` | — | | `cmaesKcatTuning` + helpers | `kcat_tuning.evotune.cmaes_kcat_tuning` + helpers | Fits kcats to experimental flux/growth data with CMA-ES; see [evotune_kcat_tuning.md](evotune_kcat_tuning.md). | ### 5.5 Limit proteins (`src/geckomat/limit_proteins`) diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index 14ccf82..efed8d4 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -127,7 +127,6 @@ fit_sigma, sensitivity_tuning, sigma_fitter, # deprecated; alias of fit_sigma - truncate_values, ) # limit_proteins @@ -253,7 +252,6 @@ "find_max_value", "fit_sigma", "sensitivity_tuning", - "truncate_values", # limit_proteins "ComplexFamilies", "FlexEnzResult", diff --git a/src/geckopy/kcat_tuning/__init__.py b/src/geckopy/kcat_tuning/__init__.py index 71e9c33..70ef567 100644 --- a/src/geckopy/kcat_tuning/__init__.py +++ b/src/geckopy/kcat_tuning/__init__.py @@ -12,7 +12,6 @@ from .find_max_value import find_max_value from .sensitivity_tuning import TunedKcatsResult, sensitivity_tuning from .sigma_fitter import SigmaFitterResult, fit_sigma, sigma_fitter -from .truncate_values import truncate_values from . import evotune @@ -28,5 +27,4 @@ "fit_sigma", "sensitivity_tuning", "sigma_fitter", # deprecated; alias of fit_sigma - "truncate_values", ] diff --git a/src/geckopy/kcat_tuning/truncate_values.py b/src/geckopy/kcat_tuning/truncate_values.py deleted file mode 100644 index 42ff155..0000000 --- a/src/geckopy/kcat_tuning/truncate_values.py +++ /dev/null @@ -1,84 +0,0 @@ -"""Round numeric values to roughly 6 significant figures. - -Ported from GECKO MATLAB: -src/geckomat/kcat_sensitivity_analysis/truncateValues.m. -""" -from __future__ import annotations - -from decimal import ROUND_HALF_UP, Decimal -from typing import Optional - -import numpy as np - - -def truncate_values( - arr: np.ndarray, - columns: Optional[list[int]] = None, -) -> np.ndarray: - """Round values in ``arr`` to ~6 significant figures. - - For each value ``v``: - - order_magn = max(ceil(log10(|v|)), 0) - result = round(v, 6 - order_magn) - - A value exactly halfway between two candidates rounds away from - zero, as MATLAB's ``round(x, n)`` does (``100000.5`` gives - ``100001`` and ``1234565`` gives ``1234570``). The value is taken - at its shortest decimal representation, so ``2.675`` is a tie. - Zero, NaN, and infinite values are returned unchanged. - - For 2D input, only the columns selected by ``columns`` are - processed (default: all). Other columns are copied through - unchanged. The input is never mutated; a new array is returned. - - Ported from GECKO MATLAB: - src/geckomat/kcat_sensitivity_analysis/truncateValues.m. - - Parameters - ---------- - arr - 1-D or 2-D numeric array. - columns - Column indices to process for 2-D input. Default: every - column. Ignored for 1-D input. - - Returns - ------- - numpy.ndarray - New array with the truncated values. - """ - out = np.asarray(arr, dtype=float).copy() - - if out.ndim == 1: - for i in range(out.shape[0]): - out[i] = _truncate_scalar(out[i]) - return out - - if out.ndim != 2: - raise ValueError( - f"arr must be 1-D or 2-D; got {out.ndim}-D" - ) - - if columns is None: - cols_to_process = range(out.shape[1]) - else: - cols_to_process = columns - - for j in cols_to_process: - for i in range(out.shape[0]): - out[i, j] = _truncate_scalar(out[i, j]) - return out - - -def _truncate_scalar(v: float) -> float: - """Round ``v`` to ~6 significant figures. - - Ties round away from zero. Zero, NaN, and infinite values are - returned unchanged. - """ - if v == 0 or np.isnan(v) or np.isinf(v): - return v - order_magn = max(int(np.ceil(np.log10(abs(v)))), 0) - quantum = Decimal(1).scaleb(order_magn - 6) - return float(Decimal(repr(float(v))).quantize(quantum, rounding=ROUND_HALF_UP)) diff --git a/tests/test_deprecated_aliases.py b/tests/test_deprecated_aliases.py index 61be3fc..0668920 100644 --- a/tests/test_deprecated_aliases.py +++ b/tests/test_deprecated_aliases.py @@ -161,3 +161,20 @@ class _MockModel: and "fill_eccodes_from_database" in str(w.message) for w in recorded ) + + +def test_merge_dlkcat_and_fuzzy_kcats_alias_emits_warning(): + """``merge_dlkcat_and_fuzzy_kcats`` (deprecated) -> ``merge_kcats``.""" + from geckopy.gather_kcats import merge_dlkcat_and_fuzzy_kcats + + with warnings.catch_warnings(record=True) as recorded: + warnings.simplefilter("always") + try: + merge_dlkcat_and_fuzzy_kcats(None, None) + except Exception: + pass + assert any( + issubclass(w.category, DeprecationWarning) + and "merge_kcats" in str(w.message) + for w in recorded + ) diff --git a/tests/test_parity_fixes_g1a.py b/tests/test_parity_fixes_g1a.py index 5b92eb2..9f9384e 100644 --- a/tests/test_parity_fixes_g1a.py +++ b/tests/test_parity_fixes_g1a.py @@ -1,38 +1,15 @@ -"""Behaviour that follows GECKO: truncate_values ties, the detail text of a kcat source file, +"""Behaviour that follows GECKO: the detail text of a kcat source file, and the defaults of fill_kcats_from_isozymes.""" import numpy as np import pytest from geckopy.ec_model.pipeline import fill_kcats_from_isozymes, set_kcat_for_reactions from geckopy.gather_kcats import read_kcat_source, save_kcat_source -from geckopy.kcat_tuning import truncate_values -from geckopy.kcat_tuning.truncate_values import _truncate_scalar from test_fill_kcats import _ectestgem_ec_model, _isozyme_model, _kcat from test_kcat_sources import _adapter, _kcat_list, _model -# --------------------------------------------------------------------------- # -# truncate_values: a tie rounds away from zero, as MATLAB round(x, n) does -# --------------------------------------------------------------------------- # - -@pytest.mark.parametrize("value, expected", [ - (100000.5, 100001.0), - (1234565.0, 1234570.0), - (-100000.5, -100001.0), - (0.1234565, 0.123457), -]) -def test_truncate_tie_rounds_away_from_zero(value, expected): - assert _truncate_scalar(value) == expected - - -def test_truncate_array_ties_and_unchanged_values(): - out = truncate_values(np.array([100000.5, 1234565.0, 0.0, np.inf, np.nan, 12.3456789])) - assert out[0] == 100001.0 and out[1] == 1234570.0 - assert out[2] == 0.0 and np.isinf(out[3]) and np.isnan(out[4]) - assert out[5] == pytest.approx(12.3457) - - # --------------------------------------------------------------------------- # # save_kcat_source: the detail column can not split a row # --------------------------------------------------------------------------- # diff --git a/tests/test_truncate_values.py b/tests/test_truncate_values.py deleted file mode 100644 index 622668c..0000000 --- a/tests/test_truncate_values.py +++ /dev/null @@ -1,140 +0,0 @@ -"""Tests for truncate_values.""" -import numpy as np -import pytest - -from geckopy.kcat_tuning import truncate_values -from geckopy.kcat_tuning.truncate_values import _truncate_scalar - - -# --------------------------------------------------------------------------- # -# Scalar helper -# --------------------------------------------------------------------------- # - -def test_scalar_zero_passes_through(): - assert _truncate_scalar(0.0) == 0.0 - - -def test_scalar_value_below_one(): - """For |v| < 10, order_magn = 0; round to 6 decimals.""" - assert _truncate_scalar(0.1234567890) == pytest.approx(0.123457) - - -def test_scalar_value_around_one(): - """For 1 <= |v| < 10, log10 in [0, 1) so ceil = 1; round to 5 decimals.""" - assert _truncate_scalar(1.234567890) == pytest.approx(1.23457) - - -def test_scalar_two_digits(): - """For 10 <= |v| < 100, order_magn = 2; round to 4 decimals.""" - assert _truncate_scalar(12.34567890) == pytest.approx(12.3457) - - -def test_scalar_exact_six_digit_integer(): - """For 100000 <= v < 1e6, order_magn = 6; round to 0 decimals.""" - assert _truncate_scalar(123456.789) == pytest.approx(123457.0) - - -def test_scalar_seven_digit_drops_to_negative_decimals(): - """For 1234567, order_magn = 7; round to -1 decimals -> nearest 10.""" - assert _truncate_scalar(1234567.0) == pytest.approx(1234570.0) - - -def test_scalar_negative_handled(): - assert _truncate_scalar(-1.234567890) == pytest.approx(-1.23457) - assert _truncate_scalar(-1234567.0) == pytest.approx(-1234570.0) - - -def test_scalar_nan_passes_through(): - assert np.isnan(_truncate_scalar(float("nan"))) - - -def test_scalar_inf_passes_through(): - assert np.isinf(_truncate_scalar(float("inf"))) - assert np.isinf(_truncate_scalar(float("-inf"))) - - -def test_scalar_very_small_value(): - """log10 of small values is very negative; order_magn capped at 0; - rounded to 6 decimals.""" - assert _truncate_scalar(0.0001234567) == pytest.approx(0.000123) - - -# --------------------------------------------------------------------------- # -# 1-D array -# --------------------------------------------------------------------------- # - -def test_1d_array_each_value_processed(): - arr = np.array([0.123456789, 1.234567890, 12.345678]) - out = truncate_values(arr) - np.testing.assert_allclose( - out, [0.123457, 1.23457, 12.3457], - ) - - -def test_1d_columns_ignored(): - """`columns` is ignored for 1-D input.""" - arr = np.array([0.1234567, 1.234567, 12.34567]) - out = truncate_values(arr, columns=[0]) # ignored - np.testing.assert_allclose(out, [0.123457, 1.23457, 12.3457]) - - -def test_1d_empty_array(): - out = truncate_values(np.array([], dtype=float)) - assert out.size == 0 - - -# --------------------------------------------------------------------------- # -# 2-D array -# --------------------------------------------------------------------------- # - -def test_2d_default_processes_all_columns(): - arr = np.array([ - [0.1234567, 1.234567], - [12.34567, 123.4567], - ]) - out = truncate_values(arr) - np.testing.assert_allclose( - out, - [[0.123457, 1.23457], - [12.3457, 123.457]], - ) - - -def test_2d_column_subset_only_specified_processed(): - """Columns not in the list are passed through unchanged.""" - arr = np.array([ - [0.1234567890, 999999.999999], # second col not processed - [1.234567890, 999999.999999], - ]) - out = truncate_values(arr, columns=[0]) - np.testing.assert_allclose( - out[:, 0], [0.123457, 1.23457], - ) - # Second column unchanged. - np.testing.assert_array_equal(out[:, 1], arr[:, 1]) - - -def test_2d_empty_array(): - out = truncate_values(np.empty((0, 3), dtype=float)) - assert out.shape == (0, 3) - - -# --------------------------------------------------------------------------- # -# Non-mutation -# --------------------------------------------------------------------------- # - -def test_input_array_not_mutated(): - arr = np.array([1.234567890, 12.345678]) - snapshot = arr.copy() - truncate_values(arr) - np.testing.assert_array_equal(arr, snapshot) - - -# --------------------------------------------------------------------------- # -# Edge: 3-D rejected -# --------------------------------------------------------------------------- # - -def test_3d_array_raises(): - arr = np.zeros((2, 2, 2)) - with pytest.raises(ValueError, match="1-D or 2-D"): - truncate_values(arr) From e282eb51d94be1d0d64ea2971b19d26285d3c458 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 21:46:20 +0100 Subject: [PATCH 02/42] feat: predict DLKcat kcats with OpenKineticsPredictor by default kcats_from_dlkcat takes via ('okp', 'file' or 'docker') and returns (kcat_list, done) as kcats_from_okp does. update_kcat_sources gathers the dlkcat source through OKP when data/DLKcat.tsv is missing instead of requiring Docker. --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 2 +- src/geckopy/gather_kcats/kcat_assignment.py | 16 ++--- src/geckopy/gather_kcats/kcats_from.py | 76 +++++++++++++++------ tests/test_kcat_assignment.py | 28 +++++++- tests/test_kcat_sources.py | 64 ++++++++++++++++- tutorials/full_ecModel/protocol.py | 2 +- 7 files changed, 155 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d24430..0b042f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. * `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. `merge_dlkcat_and_fuzzy_kcats` stays a deprecated alias of `merge_kcats`. * `geckopy.__version__` is the installed version of geckopy. * `cma` is a required dependency of geckopy and the extra `evotune` is removed. `import geckopy` imports `cma`. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 1d74166..43413b0 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -248,7 +248,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `writeOpenKineticsPredictorInput` | *(removed)* | **`[3→4]`** Replaced by `submit_open_kinetics_predictor`. | | `readOpenKineticsPredictorOutput` | *(removed)* | **`[3→4]`** Replaced by `fetch_open_kinetics_predictor`. | | `kcatsFromBrenda` | `kcats_from_brenda` | Fuzzy BRENDA matching, kept as `data/kcats/brenda.tsv`. Loads the BRENDA data and KEGG distances from the adapter unless passed in. | -| `kcatsFromDLKcat` | `kcats_from_dlkcat` | DLKcat (run in Docker with `run=True`, or an earlier output), kept as `data/kcats/dlkcat.tsv`. | +| `kcatsFromDLKcat` | `kcats_from_dlkcat` | DLKcat, kept as `data/kcats/dlkcat.tsv`. `via` is `"okp"` (the DLKcat method of OpenKineticsPredictor), `"file"` (`data/DLKcat.tsv`) or `"docker"` (a local run, `run=True`); returns `(kcat_list, done)` as `kcats_from_okp`. | | `kcatsFromOKP` | `kcats_from_okp` | One OpenKineticsPredictor method, kept as `data/kcats/okp_.tsv`; submits a job when there is none, writes the file once it is done. Returns `(kcat_list, done)`. | | `saveKcatSource`, `loadKcatSource` | `save_kcat_source`, `load_kcat_source`, `read_kcat_source` | Kcat source files: one per source, keyed by conventional reaction and enzyme set, the same file format in both tools. `load_kcat_source` maps a file to the current model and reports rows that fit no reaction. **`[Py]`** `read_kcat_source` reads a file without a model; GECKO gives the same in `loadKcatSource`'s `report.header` and `report.records`. | | `getEcRxnKeys` | `ec_rxn_keys` | Conventional reaction and enzyme set per `ec.rxns` entry. | diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index 5d4189c..e0cf6f9 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -11,7 +11,6 @@ import json import logging import re -import shutil from dataclasses import dataclass, field from pathlib import Path from typing import TYPE_CHECKING, Any, Optional @@ -436,9 +435,11 @@ def update_kcat_sources( :func:`kcats_from_okp`. A file is outdated when its header records other settings than ``[kcat_sources.]`` asks for (presets set these for brenda and ec_tree) or, for brenda and ec_tree, another BRENDA release - than the BRENDA files in use. DLKcat reads - ``data/DLKcat.tsv`` if it exists and runs DLKcat (Docker) otherwise; OKP - submits or fetches a job and reports it as pending while it runs. + than the BRENDA files in use. DLKcat reads ``data/DLKcat.tsv`` if it + exists and otherwise predicts with the DLKcat method of + OpenKineticsPredictor (see :func:`kcats_from_dlkcat`); OKP and DLKcat + submit or fetch a job and report it as pending while it runs; they need + an OKP API key (DLKcat only without ``data/DLKcat.tsv``). ``custom.tsv`` and files it cannot gather are left as they are. Calling it again is safe. @@ -499,10 +500,9 @@ def _gather(model: "EcModel", name: str, wanted: dict[str, Any], adapter: "Model kcats_from_homologs(model, adapter=adapter, **{k: wanted[k] for k in ("k", "min_identity", "coverage") if k in wanted}) elif name == "dlkcat": - run = not (Path(adapter.params.path) / "data" / "DLKcat.tsv").is_file() - if run and shutil.which("docker") is None: - return "failed", "DLKcat needs Docker to run" - kcats_from_dlkcat(model, run=run, adapter=adapter) + _, done = kcats_from_dlkcat(model, adapter=adapter) + if not done: + return "pending", "OKP job for DLKcat still running; call again later" else: method = _okp_method(name, wanted) _, done = kcats_from_okp(model, method=method, save_as=name, adapter=adapter) diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index 0e90894..e33d35f 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -242,56 +242,88 @@ def kcats_from_ec_tree( return kcat_list +_DLKCAT_VIA = ("okp", "file", "docker") + + def kcats_from_dlkcat( model: "EcModel", *, + via: Optional[str] = None, run: bool = False, + wait: bool = False, ec_rxns: Optional[Iterable[str]] = None, save: bool = True, save_as: str = "dlkcat", adapter: Optional["ModelAdapter"] = None, -) -> pd.DataFrame: +) -> tuple[Optional[pd.DataFrame], bool]: """Gather kcats from DLKcat and keep them as ``dlkcat.tsv``. - With ``run=True`` the DLKcat input is written and DLKcat runs through - its Docker image (which needs Docker); otherwise the output of an - earlier run in ``data/DLKcat.tsv`` is read. A run records how many - reactions it covered in ``data/DLKcat_run.txt``, so that the source file - says so also when the output is read later. + ``via`` sets where the predictions come from: + + - ``"okp"``: the DLKcat method of OpenKineticsPredictor (see + :func:`kcats_from_okp`), which needs an API key. OKP is a web service + with a job queue: the first call submits a job, a later call fetches + the result, and ``done`` is False while the job runs. + - ``"file"``: the output of an earlier run in ``data/DLKcat.tsv``, for + example of a GECKO 3 project. + - ``"docker"``: a local run of DLKcat in its Docker image, which needs + Docker. This is the older method; ``"okp"`` runs the same predictor + without Docker. A run records how many reactions it covered in + ``data/DLKcat_run.txt``, so that the source file says so also when the + output is read later. Parameters ---------- model EcModel with metabolite SMILES and protein sequences. + via + ``"okp"``, ``"file"`` or ``"docker"`` (default: ``"file"`` when + ``data/DLKcat.tsv`` exists, ``"okp"`` otherwise). run - Write the input and run DLKcat (default False). + The same as ``via="docker"`` (default False). Combined with another + ``via`` it raises a ``ValueError``. + wait + With ``via="okp"``: wait until the job is done. ec_rxns - ``model.ec.rxns`` to run DLKcat for (default: all); needs ``run``. + ``model.ec.rxns`` to predict for (default: all); needs ``via="okp"`` + or ``via="docker"``. With ``"okp"`` it is only used when a job is + submitted. save, save_as, adapter As in :func:`kcats_from_brenda`. Returns ------- - pandas.DataFrame - The kcat list. + (kcat_list, done) + The kcat list (``None`` while an OKP job is not done), and whether + the kcats were available. """ adapter = _adapter(model, adapter, "kcats_from_dlkcat needs params.path") ec_rxns = _rxn_subset(ec_rxns) - if ec_rxns is not None and not run: - raise ValueError("ec_rxns selects the reactions DLKcat is run for, so it needs run=True.") - if save: - path = kcat_source_file(save_as, model=model, adapter=adapter) - if run: - _warn_subset(ec_rxns, model, path) data_dir = Path(adapter.params.path) / "data" dlkcat_file = data_dir / "DLKcat.tsv" run_file = data_dir / "DLKcat_run.txt" if run: + if via is not None and via != "docker": + raise ValueError("run=True is the same as via='docker'.") + via = "docker" + elif via is None: + via = "file" if dlkcat_file.is_file() else "okp" + elif via not in _DLKCAT_VIA: + raise ValueError(f"via must be one of {', '.join(map(repr, _DLKCAT_VIA))}; got {via!r}.") + if via == "file" and ec_rxns is not None: + raise ValueError("ec_rxns selects the reactions DLKcat is run for, so it needs via='okp' or via='docker'.") + if via == "okp": + return kcats_from_okp(model, method="DLKcat", wait=wait, ec_rxns=ec_rxns, save=save, + save_as=save_as, adapter=adapter) + if save: + path = kcat_source_file(save_as, model=model, adapter=adapter) + if via == "docker": + _warn_subset(ec_rxns, model, path) + if via == "docker": if shutil.which("docker") is None: raise RuntimeError( - "DLKcat runs in a Docker image, but Docker was not found. Install it from " - "https://www.docker.com, or call kcats_from_dlkcat without run=True to read " - "an earlier DLKcat output." + "A local DLKcat run needs Docker, which was not found. Install it from " + "https://www.docker.com, or use via='okp'." ) write_dlkcat_input(model, dlkcat_file, load_dlkcat_ignore_lists(data_dir), ec_rxns=ec_rxns, overwrite=True) @@ -300,11 +332,11 @@ def kcats_from_dlkcat( encoding="utf-8", newline="\n") elif not dlkcat_file.is_file(): raise FileNotFoundError( - f"No DLKcat output at {dlkcat_file}. Run kcats_from_dlkcat(model, run=True) first." + f"No DLKcat output at {dlkcat_file}. Use via='okp' to predict the kcats." ) kcat_list = read_dlkcat_output(model, dlkcat_file) if save: - parameters = {"run": bool(run)} + parameters = {"run": via == "docker"} if run_file.is_file(): m = re.search(r"ecRxns:\s*(\d+)", run_file.read_text(encoding="utf-8")) if m: @@ -314,7 +346,7 @@ def kcats_from_dlkcat( parameters=parameters, ) logger.info("DLKcat kcats written to %s", path) - return kcat_list + return kcat_list, True _JOB_KEYS = ("jobId", "method", "submittedAt", "ecRxns", "resultOf") diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 2a2e3b1..8bf9f2d 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -98,7 +98,7 @@ def test_gecko_median_preset(tmp_path): uniprot = load_uniprot_tsv(data / "uniprot.tsv") a = base.copy() fuzzy = kcats_from_brenda(a, aggregation="median", adapter=adapter) - dlkcat = kcats_from_dlkcat(a, adapter=adapter) + dlkcat, _ = kcats_from_dlkcat(a, adapter=adapter) merged = merge_kcats(fuzzy, dlkcat, source_priority=["database_top", "dlkcat", "database_bottom"], top_origin_limit=4) apply_kcat_list(a, merged) @@ -177,3 +177,29 @@ def test_update_kcat_sources(tmp_path): adapter.params.kcat_assignment.preset = "gecko_classic" report = update_kcat_sources(base, dry_run=True, adapter=adapter) assert report["status"].iloc[1] == "outdated" + + +def test_update_kcat_sources_dlkcat_via_okp(tmp_path, monkeypatch): + """Without data/DLKcat.tsv, dlkcat is gathered through OKP and is pending while the job runs.""" + from geckopy.gather_kcats import kcats_from as kcats_from_module + + base, adapter, project = _project(tmp_path, '[kcat_assignment]\npreset = "gecko_median"\n') + (project / "data" / "DLKcat.tsv").unlink() + jobs = [] + + def fake_okp(model, **kwargs): + jobs.append(kwargs) + return None, False + + monkeypatch.setattr(kcats_from_module, "kcats_from_okp", fake_okp) + report = update_kcat_sources(base, adapter=adapter).set_index("source") + assert report.loc["dlkcat", "status"] == "pending" + assert "DLKcat" in report.loc["dlkcat", "note"] + assert jobs and jobs[0]["method"] == "DLKcat" + + # Without an API key the source fails, with the reason in the note. + monkeypatch.undo() + monkeypatch.delenv("OKP_API_KEY", raising=False) + report = update_kcat_sources(base, adapter=adapter).set_index("source") + assert report.loc["dlkcat", "status"] == "failed" + assert "API key" in report.loc["dlkcat", "note"] diff --git a/tests/test_kcat_sources.py b/tests/test_kcat_sources.py index 0cb2a11..8e64ca9 100644 --- a/tests/test_kcat_sources.py +++ b/tests/test_kcat_sources.py @@ -169,7 +169,8 @@ def test_kcats_from_dlkcat_and_okp_write_files(tmp_path, monkeypatch): (data / "DLKcat.tsv").write_text("placeholder\n", encoding="utf-8") dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) - kcats_from_dlkcat(full) + _, done = kcats_from_dlkcat(full) + assert done records, meta = read_kcat_source(data / "kcats" / "dlkcat.tsv") assert meta["generator"].startswith("kcats_from_dlkcat") assert set(records["source"]) == {"DLKcat"} @@ -404,3 +405,64 @@ def test_subset_warns(tmp_path, monkeypatch, caplog): with caplog.at_level("WARNING"): kcats_from_brenda(full, ec_rxns=["R2"], brenda=object(), phyl_dist=object()) assert "replaced by kcats for 1 reactions only" in caplog.text + + +def test_kcats_from_dlkcat_via(tmp_path, monkeypatch): + adapter = _adapter(tmp_path) + full = _model(adapter, light=False) + data = tmp_path / "data" + data.mkdir() + calls = [] + + def fake_okp(model, **kwargs): + calls.append(kwargs) + return None, False + + monkeypatch.setattr(kcats_from_module, "kcats_from_okp", fake_okp) + # Without data/DLKcat.tsv the default is OKP, with the DLKcat method and the dlkcat source name. + assert kcats_from_dlkcat(full, wait=True) == (None, False) + assert calls[0]["method"] == "DLKcat" and calls[0]["save_as"] == "dlkcat" and calls[0]["wait"] is True + kcats_from_dlkcat(full, via="okp", save_as="dlkcat2", ec_rxns=["R2"]) + assert calls[1]["save_as"] == "dlkcat2" and calls[1]["ec_rxns"] == ["R2"] + + # With the file, the default reads it and does not contact OKP. + (data / "DLKcat.tsv").write_text("placeholder\n", encoding="utf-8") + dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) + monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) + kcat_list, done = kcats_from_dlkcat(full) + assert done and len(kcat_list) == len(dlkcat) and len(calls) == 2 + kcats_from_dlkcat(full, via="okp") + assert len(calls) == 3 + + with pytest.raises(ValueError, match="via"): + kcats_from_dlkcat(full, via="web") + with pytest.raises(ValueError, match="run=True"): + kcats_from_dlkcat(full, via="file", run=True) + with pytest.raises(ValueError, match="run=True"): + kcats_from_dlkcat(full, via="okp", run=True) + with pytest.raises(ValueError, match="via='okp'"): + kcats_from_dlkcat(full, via="file", ec_rxns=["R2"]) + (data / "DLKcat.tsv").unlink() + with pytest.raises(FileNotFoundError, match="via='okp'"): + kcats_from_dlkcat(full, via="file") + + +def test_kcats_from_dlkcat_run_is_docker(tmp_path, monkeypatch): + adapter = _adapter(tmp_path) + full = _model(adapter, light=False) + (tmp_path / "data").mkdir() + monkeypatch.setattr(kcats_from_module.shutil, "which", lambda name: None) + with pytest.raises(RuntimeError, match="Docker"): + kcats_from_dlkcat(full, run=True) + with pytest.raises(RuntimeError, match="Docker"): + kcats_from_dlkcat(full, via="docker") + ran = [] + monkeypatch.setattr(kcats_from_module.shutil, "which", lambda name: "/bin/docker") + monkeypatch.setattr(kcats_from_module, "write_dlkcat_input", lambda *a, **k: ran.append("write")) + monkeypatch.setattr(kcats_from_module, "run_dlkcat", lambda path: ran.append("run")) + dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) + monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) + _, done = kcats_from_dlkcat(full, run=True) + assert done and ran == ["write", "run"] + _, meta = read_kcat_source(tmp_path / "data" / "kcats" / "dlkcat.tsv") + assert meta["parameters"] == '{"run":true}' diff --git a/tutorials/full_ecModel/protocol.py b/tutorials/full_ecModel/protocol.py index d8308d2..f64ccfb 100644 --- a/tutorials/full_ecModel/protocol.py +++ b/tutorials/full_ecModel/protocol.py @@ -170,7 +170,7 @@ # `data/kcats/dlkcat.tsv`. # %% -kcat_list_dlkcat = kcats_from_dlkcat(ec_model) +kcat_list_dlkcat, _ = kcats_from_dlkcat(ec_model, via="file") print(f"DLKcat predictions: {len(kcat_list_dlkcat)} rows") # %% [markdown] From 0786e45bbb00ec3ff767038f9b4294bdb50c7950 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 21:46:41 +0100 Subject: [PATCH 03/42] doc: name the standard and the advanced kcat tuning --- src/geckopy/kcat_tuning/evotune/tuning.py | 6 ++++++ src/geckopy/kcat_tuning/sensitivity_tuning.py | 5 +++++ tests/test_sensitivity_tuning.py | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/src/geckopy/kcat_tuning/evotune/tuning.py b/src/geckopy/kcat_tuning/evotune/tuning.py index 978b3fc..7d033c8 100644 --- a/src/geckopy/kcat_tuning/evotune/tuning.py +++ b/src/geckopy/kcat_tuning/evotune/tuning.py @@ -419,6 +419,12 @@ def cmaes_kcat_tuning( ) -> EvotuneResult: """Tune kcats against experimental data with CMA-ES. + This is the advanced kcat tuning: it fits kcats to maximum growth rates + and exchange fluxes, measured in one or more conditions (see + :func:`load_evotune_data`). + :func:`~geckopy.kcat_tuning.sensitivity_tuning` is the standard tuning, + which needs one maximum growth rate. + Configuration comes entirely from ``EvotuneParams``: trust tiers, ``max_growth_weight``, ``prior_penalty_weight`` and ``tie_isozymes`` control the search, and ``max_generations``/``rmse_threshold`` are diff --git a/src/geckopy/kcat_tuning/sensitivity_tuning.py b/src/geckopy/kcat_tuning/sensitivity_tuning.py index aba983e..4f930b9 100644 --- a/src/geckopy/kcat_tuning/sensitivity_tuning.py +++ b/src/geckopy/kcat_tuning/sensitivity_tuning.py @@ -141,6 +141,11 @@ def sensitivity_tuning( """Iteratively bump the most-limiting kcat by ``fold_change`` until the model can reach ``desired_growth_rate``. + This is the standard kcat tuning: it needs one maximum growth rate. + :func:`~geckopy.kcat_tuning.evotune.cmaes_kcat_tuning` is the advanced + tuning, which fits kcats to maximum growth rates and exchange fluxes, + measured in one or more conditions. + Each iteration: 1. Solves the LP at the current kcat values. diff --git a/tests/test_sensitivity_tuning.py b/tests/test_sensitivity_tuning.py index 6dcabf3..39256c5 100644 --- a/tests/test_sensitivity_tuning.py +++ b/tests/test_sensitivity_tuning.py @@ -304,3 +304,11 @@ def test_infeasible_start_raises_when_gurobi_presolves_it(tmp_path): model.reactions.get_by_id("biomass").lower_bound = 500.0 with pytest.raises(ValueError, match="not optimal"): sensitivity_tuning(model) + + +def test_docstrings_name_the_standard_and_advanced_tuning(): + from geckopy.kcat_tuning import sensitivity_tuning + from geckopy.kcat_tuning.evotune import cmaes_kcat_tuning + + assert "standard kcat tuning" in sensitivity_tuning.__doc__ + assert "advanced kcat tuning" in cmaes_kcat_tuning.__doc__ From c132e41066b72175525988181b1f4b4924537a3b Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 21:53:16 +0100 Subject: [PATCH 04/42] fix: keep the OpenKineticsPredictor API key file out of git geckopy init writes a .gitignore with data/okpApiKey.txt in a new project and the repository ignores okpApiKey.txt. The module help of the OKP functions said the file was git-ignored, which was not the case. --- .gitignore | 3 +++ CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 5 +++-- src/geckopy/cli.py | 2 ++ src/geckopy/gather_kcats/open_kinetics_predictor/run.py | 3 ++- tests/test_cli.py | 1 + 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e4d07c6..ce27fa0 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ build/ htmlcov/ .claude/ +# OpenKineticsPredictor API key of a project (see data/okpApiKey.txt in kcats_from_okp) +okpApiKey.txt + # Tutorial build outputs (the source yeast-GEM.yml is tracked). tutorials/full_ecModel/models/ecYeastGEM*.yml tutorials/full_ecModel/output/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b042f5..c9f4a51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. * `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. * `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. `merge_dlkcat_and_fuzzy_kcats` stays a deprecated alias of `merge_kcats`. * `geckopy.__version__` is the installed version of geckopy. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 43413b0..5313eea 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -137,8 +137,9 @@ apply_complex_data(model, path=Path("/custom/complex.json")) **Secrets** (e.g. an OpenKineticsPredictor API key) are never stored in the adapter. Provide them via a function argument, an environment -variable (`OKP_API_KEY`), or a git-ignored file in the project -`data/` folder (`data/okpApiKey.txt`). +variable (`OKP_API_KEY`), or a file in the project +`data/` folder (`data/okpApiKey.txt`); `geckopy init` writes a +`.gitignore` that lists it. --- diff --git a/src/geckopy/cli.py b/src/geckopy/cli.py index c6cdb51..d29e798 100644 --- a/src/geckopy/cli.py +++ b/src/geckopy/cli.py @@ -63,6 +63,8 @@ def cmd_init(args: argparse.Namespace) -> int: toml_text = generate_template_toml(project_name=folder.name) (folder / "model_adapter.toml").write_text(toml_text) (folder / "adapter.py").write_text(ADAPTER_PY_STUB) + # Keep the OpenKineticsPredictor API key out of version control. + (folder / ".gitignore").write_text("data/okpApiKey.txt\n") print(f"Created ecModel project at {folder}") print( diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index 2643d5f..e7e9097 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -14,7 +14,8 @@ (shown only once; revoke + regenerate if lost). The key is a secret: provide it via the ``api_key`` argument, the ``OKP_API_KEY`` environment variable, or ``data/okpApiKey.txt`` (the -latter is git-ignored). It is never read from the model adapter. +geckopy repository and the ``.gitignore`` that ``geckopy init`` writes +ignore this file). It is never read from the model adapter. """ from __future__ import annotations diff --git a/tests/test_cli.py b/tests/test_cli.py index d043f44..e218253 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -51,6 +51,7 @@ def test_cli_init_creates_folder_structure(tmp_path): assert (target / "data").is_dir() assert (target / "models").is_dir() assert (target / "output").is_dir() + assert (target / ".gitignore").read_text().strip() == "data/okpApiKey.txt" def test_cli_init_refuses_nonempty_folder(tmp_path, capsys): From c24b4d368e11af63e9e91b9da3dbd266bb243b56 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:35:38 +0100 Subject: [PATCH 05/42] doc: correct function help, module notes and the relaxation methods page --- docs/relaxation_methods.md | 11 ++++++----- src/geckopy/ec_model/make_ec_model.py | 8 ++++---- src/geckopy/ec_model/pipeline/protein_pool.py | 4 ++-- src/geckopy/gather_kcats/kcat_assignment.py | 4 +++- src/geckopy/gather_kcats/review.py | 2 +- src/geckopy/get_enzyme_data/ec_from_database.py | 4 ++-- src/geckopy/kcat_tuning/evotune/parsimony.py | 3 +-- src/geckopy/kcat_tuning/evotune/simulate.py | 16 +++++++--------- src/geckopy/kcat_tuning/sensitivity_tuning.py | 10 +++++----- src/geckopy/kcat_tuning/sigma_fitter.py | 4 ++-- src/geckopy/upgrade.py | 6 +++--- src/geckopy/utilities/ec_fseof.py | 11 +++++------ src/geckopy/utilities/ec_fva.py | 9 ++++++--- src/geckopy/utilities/pfba_enzymes.py | 4 ++-- 14 files changed, 49 insertions(+), 47 deletions(-) diff --git a/docs/relaxation_methods.md b/docs/relaxation_methods.md index 196333a..9506eee 100644 --- a/docs/relaxation_methods.md +++ b/docs/relaxation_methods.md @@ -14,14 +14,14 @@ the per-complex options for all of them are described at the end. | aspect | `flexibilize_enz_concs` | `relax_proteomics_greedy` | |---|---|---| | Origin | GECKO MATLAB `flexibilizeEnzConcs.m` (faithful port) | legacy geckopy, Carrasco et al. 2023 | -| Which enzyme to relax | largest **control coefficient** (how much each measured enzyme's bound limits growth, from `get_conc_control_coeffs`: the reduced cost of its usage bound, or a finite difference where the solver exposes no duals) | largest **\|shadow price\|** (LP dual of the usage upper bound) | +| Which enzyme to relax | largest **control coefficient** (how much each measured enzyme's bound limits growth, from `get_conc_control_coeffs`: the reduced cost of its usage bound, or a finite difference where the solver exposes no duals) | largest **\|shadow price\|** (LP dual of the `prot_` metabolite of the enzyme) | | Solves per selection | one LP solve plus one check solve per enzyme with a nonzero coefficient (one solve per candidate protein where the solver exposes no duals) | a **single** `slim_optimize` exposes all duals (cheap) | | Relaxation step | **gradual**: usage `ub ← conc·(1 + fold_change·k)`; the same enzyme can be selected repeatedly and loosened a bit more each time | **all-or-nothing**: usage `ub ← default_upper_bound` (≈ unconstrained) in one shot | | Tighten-back | yes: a refinement pass minimises the protein pool at `exp_growth` and **drops** enzymes whose true usage is below their original conc (un-relaxes over-relaxed ones) | none: each selected enzyme stays fully unconstrained | | Result stringency | **tight** (minimal, proteomics-faithful relaxation) | **loose** (tends to over-relax) | | Pool fallback | yes — relaxes `prot_pool_exchange` when no single enzyme helps | no | | Scope | measured enzymes only (`~isnan(concs)`) | any currently-constrained enzyme (`ub < default`) | -| Non-convergence | returns a partial `FlexEnzResult` + warning (global iteration cap) | **raises** `RuntimeError` | +| Non-convergence | returns a partial `FlexEnzResult` + warning (global iteration cap) | returns `converged=False` when no candidate is left; **raises** `RuntimeError` when `max_iterations` relaxations leave growth below the target | | Result type | `FlexEnzResult` (relaxed enzymes, per-enzyme `frequence`, ratios) | `GreedyRelaxResult` (relaxed→orig-conc dict, step trace, `converged`) | ## Interpretation @@ -38,7 +38,7 @@ the per-complex options for all of them are described at the end. ranks all enzymes by shadow price, and fully unconstraining the worst offender usually moves growth a lot. The cost is that it **over-relaxes** (no tighten-back), so the resulting model is less constrained than necessary, and - it gives up (raises) rather than degrading gracefully. + it raises when `max_iterations` relaxations do not reach the target. ## Recommendation @@ -50,8 +50,9 @@ follow-ups would make them more interchangeable: 1. Give `relax_proteomics_greedy` an optional tighten-back / graded step so its output is not systematically looser than `flexibilize_enz_concs` (this is the over-relaxation point in the modeling-intent review, §3.3). -2. Align the failure convention — currently one returns a `converged=False` - result and the other raises; select one so callers don't special-case each. +2. Align the failure convention: at its iteration cap `flexibilize_enz_concs` + returns a partial result with a warning and `relax_proteomics_greedy` + raises; select one so callers don't special-case each. ## `relax_enz_concs`: the least total relaxation, in one LP diff --git a/src/geckopy/ec_model/make_ec_model.py b/src/geckopy/ec_model/make_ec_model.py index 7147c4b..9ef7ae4 100644 --- a/src/geckopy/ec_model/make_ec_model.py +++ b/src/geckopy/ec_model/make_ec_model.py @@ -124,10 +124,10 @@ def make_ec_model( coupling matrix). The kcat values themselves are NOT filled in here — they stay - at 0 (0 marks "no kcat assigned") until you populate them - via the ``gather_kcats`` functions (BRENDA / DLKcat / custom - files) and call ``apply_kcat_constraints``, or set them manually - via ``set_kcat_for_reactions``. + at 0 (0 marks "no kcat assigned") until ``update_kcat_sources`` + gathers the kcat source files and ``assign_kcats`` decides and + applies one kcat per reaction, or you set them manually via + ``set_kcat_for_reactions``. Parameters ---------- diff --git a/src/geckopy/ec_model/pipeline/protein_pool.py b/src/geckopy/ec_model/pipeline/protein_pool.py index 14047a4..e7d71b3 100644 --- a/src/geckopy/ec_model/pipeline/protein_pool.py +++ b/src/geckopy/ec_model/pipeline/protein_pool.py @@ -10,7 +10,7 @@ are normalized to the forward convention on load (see `load_ec_model.py`), mirroring MATLAB's `loadEcModel.m`. -Ported from GECKO MATLAB: src/geckomat/change_model/makeEcModel.m. +Ported from GECKO MATLAB: src/change_model/makeEcModel.m. """ from __future__ import annotations @@ -259,7 +259,7 @@ def set_prot_pool_size( """Set the upper bound of ``prot_pool_exchange`` (the protein budget). Ported from GECKO MATLAB: - src/geckomat/change_model/setProtPoolSize.m. + src/change_model/setProtPoolSize.m. The protein pool exchange represents the cell's total enzyme supply. Its upper bound is what limits how much protein the diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index e0cf6f9..f4a03e9 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -365,7 +365,9 @@ def collect_kcats( ``decisions`` has one row per ``model.ec`` reaction: ``ec_rxn``, ``stage`` (the stage number, ``custom``, ``isozymes``, ``standard``, or empty without a kcat), ``source``, ``kcat``, ``candidates`` (values - the deciding source had) and ``spread_log10``. ``report`` has the + the deciding source had), ``spread_log10`` (their range, in log10 + units) and ``sd_log10`` (the uncertainty of a combined kcat, a standard + deviation in log10 units; NaN for the other stages). ``report`` has the ``preset``, the ``missing`` source files and, per source file, its load report. ``ec_model`` is a copy of the model with the decided kcats, before ``apply_kcat_constraints``. diff --git a/src/geckopy/gather_kcats/review.py b/src/geckopy/gather_kcats/review.py index 1ebbf24..e0c10cb 100644 --- a/src/geckopy/gather_kcats/review.py +++ b/src/geckopy/gather_kcats/review.py @@ -12,7 +12,7 @@ Two design choices, both from measurement rather than taste. **Rank by leverage, and cut the report there.** On ecYeastGEM the -checks below flag around 1900 of 4834 kcats, which is a report nobody +checks below flag a large share of the kcats, a report nobody reads. Tightening the checks does not fix that: raising the EC-maximum gap from three-fold to ten-fold still returns 216 entries and drops *all six* of the high-leverage ones, because looking odd and mattering diff --git a/src/geckopy/get_enzyme_data/ec_from_database.py b/src/geckopy/get_enzyme_data/ec_from_database.py index 97c6904..cd8967f 100644 --- a/src/geckopy/get_enzyme_data/ec_from_database.py +++ b/src/geckopy/get_enzyme_data/ec_from_database.py @@ -2,7 +2,7 @@ protein database. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/getECfromDatabase.m. +src/get_enzyme_data/getECfromDatabase.m. """ from __future__ import annotations @@ -44,7 +44,7 @@ def fill_eccodes_from_database( up in a UniProt database. Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/getECfromDatabase.m. + src/get_enzyme_data/getECfromDatabase.m. For each entry of ``model.ec.rxns`` selected by ``ec_rxns``: diff --git a/src/geckopy/kcat_tuning/evotune/parsimony.py b/src/geckopy/kcat_tuning/evotune/parsimony.py index 99f42e5..dcd33df 100644 --- a/src/geckopy/kcat_tuning/evotune/parsimony.py +++ b/src/geckopy/kcat_tuning/evotune/parsimony.py @@ -11,8 +11,7 @@ model moves ~99% of kcats by ~8 prior sigmas. Reverting the least-moved of those to their priors costs nothing up to about 3 sigma and beats the shrunk "blend" estimate at every sparsity level, so a frontier is a -more honest report than either single vector. See -``docs/internal/matlab_replication_results.md``. +more honest report than either single vector. """ from __future__ import annotations diff --git a/src/geckopy/kcat_tuning/evotune/simulate.py b/src/geckopy/kcat_tuning/evotune/simulate.py index 03c43d6..34001db 100644 --- a/src/geckopy/kcat_tuning/evotune/simulate.py +++ b/src/geckopy/kcat_tuning/evotune/simulate.py @@ -95,15 +95,13 @@ def simulate_evotune_dataset( bio_rxn_id The biomass reaction ID (``adapter.params.bio_rxn``). make_anaerobic, change_protein_biomass - Optional adapter-specific hooks (MATLAB: - ``modelAdapter.makeModelAnaerobic`` / - ``.changeProteinBiomass``). geckopy has no generic, - organism-agnostic port of these yet -- in MATLAB they're - written per model-adapter subclass (e.g. - ``YeastGEMAdapter.m``). Left as an injection point rather - than a hard requirement: data with no anaerobic/``Ptot`` rows - never needs them, and passing ``None`` for either just skips - that adjustment instead of raising. + Optional organism-specific hooks. GECKO calls the MATLAB + functions named by ``make_model_anaerobic`` and + ``change_protein_biomass`` in the ``[matlab]`` table of + ``model_adapter.toml``; geckopy takes the callables here and has + no organism-agnostic default for either. Data with no + anaerobic/``Ptot`` rows never needs them, and passing ``None`` + for either skips that adjustment instead of raising. Returns ------- diff --git a/src/geckopy/kcat_tuning/sensitivity_tuning.py b/src/geckopy/kcat_tuning/sensitivity_tuning.py index 4f930b9..c74c0f4 100644 --- a/src/geckopy/kcat_tuning/sensitivity_tuning.py +++ b/src/geckopy/kcat_tuning/sensitivity_tuning.py @@ -3,9 +3,9 @@ After ``make_ec_model`` + ``apply_kcat_constraints``, the model often can't reach the experimental growth rate because one or two kcat values are unrealistically low (BRENDA's database has many -small or wrong entries). The right long-term fix is manual -curation, but as a quick automated workaround -``sensitivity_tuning`` does the following loop: +small or wrong entries). ``sensitivity_tuning`` is the standard +kcat tuning (``cmaes_kcat_tuning`` is the advanced one); it runs the +following loop: 1. Solve the model. 2. If we reached the target growth rate, stop. @@ -20,7 +20,7 @@ from the adapter config). Ported from GECKO MATLAB: -src/geckomat/kcat_sensitivity_analysis/sensitivityTuning.m. +src/kcat_tuning/sensitivityTuning.m. """ from __future__ import annotations @@ -169,7 +169,7 @@ def sensitivity_tuning( reaction; ``ec.notes`` and ``ec.source`` are left as they are. Ported from GECKO MATLAB: - src/geckomat/kcat_sensitivity_analysis/sensitivityTuning.m. + src/kcat_tuning/sensitivityTuning.m. Parameters ---------- diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index 3cf176f..52160cc 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -14,7 +14,7 @@ ``[0, 1000]`` first, and stay open on the fitted model. Ported from GECKO MATLAB: -src/geckomat/kcat_sensitivity_analysis/sigmaFitter.m. +src/kcat_tuning/sigmaFitter.m. """ from __future__ import annotations @@ -120,7 +120,7 @@ def fit_sigma( is re-applied to ``model`` before return. Ported from GECKO MATLAB: - src/geckomat/kcat_sensitivity_analysis/sigmaFitter.m. + src/kcat_tuning/sigmaFitter.m. Parameters ---------- diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index 70cb12b..762e705 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -56,8 +56,8 @@ def convert_custom_kcats( is kept: one row per reaction, keyed by conventional reaction and enzyme set (see :func:`~geckopy.gather_kcats.ec_rxn_keys`), with the entry's notes in the note column. The ``stoicho`` column is left out: complex - stoichiometry comes from ``apply_complex_data``. Unlike - ``apply_custom_kcats``, ``custom.tsv`` also applies to light ecModels. + stoichiometry comes from ``apply_complex_data``. The conversion needs the + full ecModel; ``custom.tsv`` applies to full and light ecModels. Parameters ---------- @@ -80,7 +80,7 @@ def convert_custom_kcats( from .gather_kcats.kcat_sources import _geckopy_version, ec_rxn_keys, format_number if model.ec.gecko_light: - raise ValueError("apply_custom_kcats does not work on light ecModels; give the full ecModel. " + raise ValueError("convert_custom_kcats needs the full ecModel. " "The custom.tsv written applies to light ecModels too.") adapter = resolve_adapter(model, adapter, purpose="convert_custom_kcats needs the project folder") project = Path(adapter.params.path) diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index 6b09bea..059acee 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -24,8 +24,8 @@ The selection method and output shape come from raven-toolbox: each reaction's flux is regressed against the enforced product flux across -the scan, and a reaction is a target if ``|correlation| >= -correlation_threshold`` and ``|slope| >= flux_eps``. Targets are +the scan, and a reaction is a target if its flux is not constant across +the scan and ``|correlation| >= correlation_threshold``. Targets are classified as ``amplify`` / ``knockdown`` / ``knockout``. See :class:`raven_toolbox.analysis.fseof.FSEOFResult` for the full output schema. @@ -104,10 +104,9 @@ def ec_fseof( A reaction is a target when ``|corr(flux, enforced)| >=`` this value. Default 0.9. flux_eps - Minimum ``|slope|`` (flux change per enforced-flux step) for - a reaction to count as responsive; below this it is treated - as flat regardless of correlation, so it cannot become a - target. Default 1e-6. + Tolerance below which the theoretical maximum of the production + target flux is treated as zero (the scan then raises + ``ValueError``). It gates only that check. Default 1e-6. Returns ------- diff --git a/src/geckopy/utilities/ec_fva.py b/src/geckopy/utilities/ec_fva.py index dae1d04..7a0f9a1 100644 --- a/src/geckopy/utilities/ec_fva.py +++ b/src/geckopy/utilities/ec_fva.py @@ -1,8 +1,11 @@ """Flux variability analysis (FVA) on an ecModel. -Standard FVA asks, for each reaction in a model, what's the -smallest and largest flux that reaction can carry while keeping -the model feasible at its current objective? The output is a +FVA gives, for each reaction in a model, the smallest and largest +flux that reaction can carry within the bounds of the model. The +current objective is not kept: as in GECKO's ``ecFVA``, every +objective coefficient is set to 0 and each LP optimises only the +reaction scanned, so a growth rate or other flux to hold must be set +with its bounds before the call. The output is a ``(min_flux, max_flux)`` pair per reaction, useful for finding reactions whose flux is tightly constrained vs reactions with a lot of slack. diff --git a/src/geckopy/utilities/pfba_enzymes.py b/src/geckopy/utilities/pfba_enzymes.py index c7abb76..2f654cd 100644 --- a/src/geckopy/utilities/pfba_enzymes.py +++ b/src/geckopy/utilities/pfba_enzymes.py @@ -8,8 +8,8 @@ ``pfba_enzymes`` is the enzyme-aware version: among all biomass-optimal flux distributions, it selects the one that uses the -least enzyme mass (the smallest sum of ``usage_prot_*`` fluxes, -weighted by the cost coefficients in the LP). For ecModels this is +least enzyme mass (the smallest sum of ``usage_prot_*`` fluxes, each +with weight 1: the usage fluxes are already in mg/gDCW). For ecModels this is usually what you want, because it selects the most parsimonious *proteome*, not just the most parsimonious *fluxes*. From 6d839b8d45d667567e8658ffcc82e50e46ebc037 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:36:27 +0100 Subject: [PATCH 06/42] fix: return no complexes from get_complex_data without a taxonomic id --- CHANGELOG.md | 1 + .../databases/complex_portal_download.py | 20 ++++++++++--------- tests/test_complex_portal_download.py | 15 +++++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9f4a51..8d78201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. * `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. * `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. `merge_dlkcat_and_fuzzy_kcats` stays a deprecated alias of `merge_kcats`. diff --git a/src/geckopy/databases/complex_portal_download.py b/src/geckopy/databases/complex_portal_download.py index 04198c2..0cf53e2 100644 --- a/src/geckopy/databases/complex_portal_download.py +++ b/src/geckopy/databases/complex_portal_download.py @@ -1,7 +1,7 @@ """Download complex stoichiometries from EBI Complex Portal. Ported from GECKO MATLAB: -src/geckomat/change_model/getComplexData.m. +src/change_model/getComplexData.m. Network access is required. The result is cached as ``ComplexPortal.json`` in the adapter's data folder; subsequent runs @@ -44,13 +44,15 @@ def get_complex_data( """Download complex stoichiometries from EBI Complex Portal. Ported from GECKO MATLAB: - src/geckomat/change_model/getComplexData.m. + src/change_model/getComplexData.m. Taxonomic ID semantics: - A positive integer queries that organism only. - ``0`` queries all organisms (no species filter). - - ``None`` raises ValueError. + - ``None`` logs a warning and returns no complexes, without a query or + a file written, as GECKO's ``getComplexData`` does without a + taxonomic ID. The response is post-processed to expand "complexes of complexes": when a Complex Portal entry references other complexes as subunits, @@ -69,20 +71,20 @@ def get_complex_data( Returns ------- list of ComplexPortalEntry - Parsed and post-processed complexes. + Parsed and post-processed complexes; empty when ``taxonomic_id`` is + None. Raises ------ - ValueError - If ``taxonomic_id`` is None. requests.HTTPError If the API returns an unrecoverable error. """ if taxonomic_id is None: - raise ValueError( - "taxonomic_id is required. Use 0 to query all organisms, " - "or set adapter.params.complex.taxonomic_id." + logger.warning( + "No taxonomic_id specified; no complexes are downloaded. Use 0 to " + "query all organisms, or set adapter.params.complex.taxonomic_id." ) + return [] session = _make_session() diff --git a/tests/test_complex_portal_download.py b/tests/test_complex_portal_download.py index 223ec1a..0b8daec 100644 --- a/tests/test_complex_portal_download.py +++ b/tests/test_complex_portal_download.py @@ -1,5 +1,7 @@ """Tests for get_complex_data (network mocked).""" +import logging + import pytest import requests @@ -100,9 +102,16 @@ def get(self, url, params=None, timeout=None): assert any(u.endswith("search/*") for u in captured_urls) -def test_none_taxonomic_id_raises(): - with pytest.raises(ValueError, match="taxonomic_id is required"): - get_complex_data(taxonomic_id=None) +def test_none_taxonomic_id_warns_and_returns_nothing(monkeypatch, tmp_path, caplog): + """As getComplexData without a taxonomic ID: a warning, no query, no file.""" + from geckopy.databases import complex_portal_download as mod + + monkeypatch.setattr(mod, "_make_session", lambda: pytest.fail("queried")) + out = tmp_path / "ComplexPortal.json" + with caplog.at_level(logging.WARNING): + assert get_complex_data(taxonomic_id=None, write_to=out) == [] + assert "No taxonomic_id" in caplog.text + assert not out.exists() def test_empty_search_response_raises(monkeypatch): From 41621b3170da86e9bde30851faeed4463fa6c789 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:37:02 +0100 Subject: [PATCH 07/42] fix: check the target after the last allowed greedy relaxation --- CHANGELOG.md | 1 + src/geckopy/limit_proteins/relax_proteomics_greedy.py | 9 +++++++-- tests/test_relax_proteomics_greedy.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d78201..6323c16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed and returns a converged result when that relaxation reaches `minimal_growth`; it raised `RuntimeError` after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. * `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. diff --git a/src/geckopy/limit_proteins/relax_proteomics_greedy.py b/src/geckopy/limit_proteins/relax_proteomics_greedy.py index 0441021..6235c54 100644 --- a/src/geckopy/limit_proteins/relax_proteomics_greedy.py +++ b/src/geckopy/limit_proteins/relax_proteomics_greedy.py @@ -78,8 +78,8 @@ def relax_proteomics_greedy( Subset of uniprot IDs eligible for relaxation. ``None`` = all currently proteomics-constrained enzymes. max_iterations - Safety cap; raises ``RuntimeError`` if exceeded without - convergence. + Safety cap on the number of relaxations; raises ``RuntimeError`` + when growth is still below ``minimal_growth`` after that many. default_upper_bound Value to restore on a relaxed usage reaction. Matches the default set by ``add_protein_usage_reactions``. @@ -144,6 +144,11 @@ def relax_proteomics_greedy( )) growth = new_growth + if growth >= minimal_growth: + return GreedyRelaxResult( + relaxed=relaxed, trace=trace, + final_growth=float(growth), converged=True, + ) raise RuntimeError( f"Did not converge in {max_iterations} iterations " f"(final growth {growth:.4g} < target {minimal_growth:.4g})" diff --git a/tests/test_relax_proteomics_greedy.py b/tests/test_relax_proteomics_greedy.py index a7fdc1a..2f9ef2d 100644 --- a/tests/test_relax_proteomics_greedy.py +++ b/tests/test_relax_proteomics_greedy.py @@ -91,6 +91,16 @@ def test_enzyme_set_filter(): assert set(result.relaxed.keys()).issubset(only) +def test_target_reached_by_the_last_allowed_relaxation_converges(): + """The relaxation made at max_iterations is checked before raising.""" + model, target = _ec_with_tight_concs(growth_target=0.001) + needed = len(relax_proteomics_greedy(model.copy(), minimal_growth=target, max_iterations=20).relaxed) + assert needed >= 1 + result = relax_proteomics_greedy(model, minimal_growth=target, max_iterations=needed) + assert result.converged + assert len(result.relaxed) == needed + + def test_max_iterations_raises(): """Setting max_iterations=1 on a model needing more raises.""" model, _ = _ec_with_tight_concs() From 5de0823c43b39512ff56cc158dc2e1ab64e26728 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:37:33 +0100 Subject: [PATCH 08/42] fix: raise in fit_sigma for a target growth rate of 0 --- CHANGELOG.md | 1 + src/geckopy/kcat_tuning/sigma_fitter.py | 19 ++++++++++--------- tests/test_sigma_fitter.py | 9 +++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6323c16..844cfc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. * `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed and returns a converged result when that relaxation reaches `minimal_growth`; it raised `RuntimeError` after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index 52160cc..97460a7 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -84,9 +84,7 @@ def _growth_at_sigma( def _sigma_error_pct(growth: float, growth_rate: float) -> float: - if growth_rate != 0: - return abs((growth_rate - growth) / growth_rate) * 100.0 - return abs(growth) * 100.0 + return abs((growth_rate - growth) / growth_rate) * 100.0 def fit_sigma( @@ -157,7 +155,8 @@ def fit_sigma( ------ ValueError If ``model.adapter`` is None and any default is needed; if - ``n_sigma_steps`` is < 1; or if ``method`` is unknown. + ``growth_rate`` is 0; if ``n_sigma_steps`` is < 1; or if ``method`` + is unknown. """ if n_sigma_steps < 1: raise ValueError(f"n_sigma_steps must be >= 1, got {n_sigma_steps}") @@ -174,18 +173,20 @@ def fit_sigma( ) params = model.adapter.params if model.adapter is not None else None - # sigmaFitter.m opens the bounds of the objective for good. - for rxn in model.reactions: - if rxn.objective_coefficient != 0: - rxn.bounds = (0.0, 1000.0) - if growth_rate is None: growth_rate = float(params.gr_exp) + if growth_rate == 0: + raise ValueError("growth_rate is 0; fit_sigma needs a target growth rate other than 0.") if p_tot is None: p_tot = float(params.p_tot) if f is None: f = float(params.f) + # sigmaFitter.m opens the bounds of the objective for good. + for rxn in model.reactions: + if rxn.objective_coefficient != 0: + rxn.bounds = (0.0, 1000.0) + plateau_sigma: Optional[float] = None if method == "grid": sigma_grid = np.array( diff --git a/tests/test_sigma_fitter.py b/tests/test_sigma_fitter.py index 9d82325..372e9bd 100644 --- a/tests/test_sigma_fitter.py +++ b/tests/test_sigma_fitter.py @@ -86,6 +86,15 @@ def test_n_sigma_steps_zero_raises(tmp_path): fit_sigma(model, n_sigma_steps=0) +def test_zero_growth_rate_raises_before_changing_the_model(tmp_path): + adapter = _adapter(tmp_path) + model = _build_pool_only_model(adapter) + bounds = [r.bounds for r in model.reactions] + with pytest.raises(ValueError, match="growth_rate is 0"): + fit_sigma(model, growth_rate=0.0) + assert [r.bounds for r in model.reactions] == bounds + + def test_no_adapter_no_defaults_raises(tmp_path): adapter = _adapter(tmp_path) model = _build_pool_only_model(adapter) From 2833c532d58d40d77f337ebb1bf28d43489708f1 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:39:22 +0100 Subject: [PATCH 09/42] fix: leave the isozyme kcats of collect_kcats unapplied --- CHANGELOG.md | 1 + src/geckopy/gather_kcats/kcat_assignment.py | 2 +- tests/test_kcat_assignment.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 844cfc6..096c2d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. * `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. * `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed and returns a converged result when that relaxation reaches `minimal_growth`; it raised `RuntimeError` after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index f4a03e9..11768ec 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -251,7 +251,7 @@ def load(name: str) -> Optional[pd.DataFrame]: stage[i], candidates[i], spread[i] = "custom", 1, 0.0 if config.isozymes and not ec.gecko_light: before = ec.kcat == 0 - fill_kcats_from_isozymes(model, apply=True) + fill_kcats_from_isozymes(model) for i in np.flatnonzero(before & (ec.kcat > 0)): stage[i] = "isozymes" if config.standard: diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 8bf9f2d..2aaf7e0 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -134,6 +134,27 @@ def test_collect_kcats_leaves_model(tmp_path): assert (row["kcat"], row["source"], row["stage"]) == (777, "custom", "1") +def test_collect_kcats_returns_isozyme_kcats_without_constraints(tmp_path): + """As collectKcats: the kcats filled from isozymes are not applied to the + returned model's stoichiometry; assign_kcats applies them.""" + base, adapter, project = _project(tmp_path, '[kcat_assignment]\nstandard = false\n' + '[[kcat_assignment.stage]]\nsources = ["custom"]\n') + rxns, enzymes = ec_rxn_keys(base) + i = base.ec.rxns.index("R2_EXP_1") + (project / "data" / "kcats").mkdir(exist_ok=True) + (project / "data" / "kcats" / "custom.tsv").write_text( + "# source: custom\n# kind: curated\nrxn\tenzymes\tkcat\tsource\n" + f"{rxns[i]}\t{enzymes[i]}\t100\tcustom\n", encoding="utf-8") + decisions, _, decided = collect_kcats(base, adapter=adapter, write_report=False) + filled = list(decisions.loc[decisions["stage"] == "isozymes", "ec_rxn"]) + assert filled == ["R2_EXP_2"] + stoich = lambda m, r: {x.id: c for x, c in m.reactions.get_by_id(r).metabolites.items()} # noqa: E731 + for rxn in filled: + assert stoich(decided, rxn) == stoich(base, rxn) + assign_kcats(base, adapter=adapter, write_report=False) + assert any(stoich(decided, rxn) != stoich(base, rxn) for rxn in filled) + + def test_stages_from_toml(tmp_path): """Stages from model_adapter.toml, and their errors.""" extra = ('[kcat_assignment]\nisozymes = false\nstandard = false\n' From b2b507df43a5a7e5a1f412632b0938668b84dcff Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:40:05 +0100 Subject: [PATCH 10/42] fix: report both complex proposals of a reaction in apply_complex_data --- CHANGELOG.md | 1 + .../ec_model/pipeline/apply_complex_data.py | 31 ++++++++----------- tests/test_apply_complex_data.py | 18 +++++++++++ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 096c2d2..58eaf7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. * `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. * `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. * `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed and returns a converged result when that relaxation reaches `minimal_growth`; it raised `RuntimeError` after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. diff --git a/src/geckopy/ec_model/pipeline/apply_complex_data.py b/src/geckopy/ec_model/pipeline/apply_complex_data.py index 36afe89..0100d24 100644 --- a/src/geckopy/ec_model/pipeline/apply_complex_data.py +++ b/src/geckopy/ec_model/pipeline/apply_complex_data.py @@ -1,6 +1,6 @@ """Apply ComplexPortal subunit stoichiometries to ec.rxn_enz_mat. -Ported from GECKO MATLAB: src/geckomat/change_model/applyComplexData.m. +Ported from GECKO MATLAB: src/change_model/applyComplexData.m. """ from __future__ import annotations @@ -38,12 +38,15 @@ def apply_complex_data( The model's enzyme set equals a complex's protein list, with no extras on either side. Apply the complex's stoichiometry. - Proposed (subset) - Either: every model enzyme is in some complex, but the complex - lists additional subunits not in the model (closest superset - is reported). Or: ``min_match_to_propose`` (default 0.75) of - model enzymes are in some complex, with extras or substitutions - on the model side. Logged as WARNING; not applied. + Proposed + Superset: every model enzyme is in a complex that lists additional + subunits not in the model (the complex with the fewest extra + subunits is reported). Partial: ``min_match_to_propose`` (default + 0.75) of the model enzymes are in a complex that has no subunits + beyond the model's number, with extras or substitutions on the model + side (the complex with the highest match is reported). A reaction + can get one proposal of each kind, as in GECKO's + ``applyComplexData``. Logged as WARNING; not applied. No match Skipped silently. @@ -177,17 +180,9 @@ def apply_complex_data( if mod_units <= 1: continue - proposed: Optional[tuple[int, float]] = None - match_kind = "" - if superset_candidate is not None: - proposed = superset_candidate - match_kind = "superset" - elif partial_candidate is not None: - proposed = partial_candidate - match_kind = "partial" - - if proposed is not None: - ci, score = proposed + proposals = [(c, kind) for c, kind in ((superset_candidate, "superset"), (partial_candidate, "partial")) + if c is not None] + for (ci, score), match_kind in proposals: entry = complex_data[ci] n_proposed += 1 logger.warning( diff --git a/tests/test_apply_complex_data.py b/tests/test_apply_complex_data.py index 8f26a34..53227b0 100644 --- a/tests/test_apply_complex_data.py +++ b/tests/test_apply_complex_data.py @@ -179,6 +179,24 @@ def test_partial_match_above_threshold_logs(caplog): assert "C_partial" in caplog.text +def test_superset_and_partial_both_proposed(caplog): + """As applyComplexData, a reaction gets a superset and a partial proposal.""" + ec_model = _ectestgem_ec_model() + data = [ + ComplexPortalEntry(complex_id="C_super", name="superset", species="t", + gene_names=["G1", "G2", "G_extra"], protein_ids=["P1", "P2", "P_extra"], + stoichiometry=[1, 1, 1]), + ComplexPortalEntry(complex_id="C_partial", name="partial", species="t", + gene_names=["G1"], protein_ids=["P1"], stoichiometry=[1]), + ] + with caplog.at_level(logging.WARNING): + apply_complex_data(ec_model, complex_data=data, apply=False, min_match_to_propose=0.5) + r2 = [r.getMessage() for r in caplog.records if "reaction R2_EXP_1 " in r.getMessage()] + assert len(r2) == 2 + assert "C_super" in r2[0] and "superset" in r2[0] + assert "C_partial" in r2[1] and "partial" in r2[1] + + def test_single_enzyme_reaction_not_proposed(caplog): """Single-enzyme reactions are not eligible for proposed complexes, since their stoichiometry is already unambiguous.""" From 1703cc4bf77c539a1656a40d3bc032b61af48d24 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:40:33 +0100 Subject: [PATCH 11/42] feat: include unreviewed entries in uniprot-download by default --- CHANGELOG.md | 1 + src/geckopy/cli.py | 7 ++++--- tests/test_cli.py | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58eaf7b..fe7c8ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `geckopy uniprot-download` keeps reviewed and unreviewed entries by default, as the adapter default `uniprot.reviewed = false` and GECKO's `loadDatabases` do; `--reviewed-only` keeps the reviewed (Swiss-Prot) entries only. The option `--include-unreviewed` is removed; the command kept only reviewed entries by default. * `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. * `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. * `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. diff --git a/src/geckopy/cli.py b/src/geckopy/cli.py index d29e798..93b26af 100644 --- a/src/geckopy/cli.py +++ b/src/geckopy/cli.py @@ -195,8 +195,9 @@ def main(argv: list[str] | None = None) -> int: help="UniProt field for the Gene Names column (default: gene_oln)", ) uniprot_parser.add_argument( - "--include-unreviewed", action="store_true", - help="Include unreviewed (TrEMBL) entries (default: reviewed only)", + "--reviewed-only", action="store_true", + help="Keep reviewed (Swiss-Prot) entries only (default: reviewed and " + "unreviewed, as the adapter default uniprot.reviewed = false)", ) uniprot_parser.set_defaults(func=cmd_uniprot_download) @@ -236,7 +237,7 @@ def cmd_uniprot_download(args: argparse.Namespace) -> int: args.uniprot_id, out, id_type=args.id_type, gene_id_field=args.gene_id_field, - reviewed=not args.include_unreviewed, + reviewed=args.reviewed_only, ) print(f"Wrote {out}") return 0 diff --git a/tests/test_cli.py b/tests/test_cli.py index e218253..864b3c7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -154,11 +154,11 @@ def fake_download(uid, path, *, id_type, gene_id_field, reviewed): assert calls["args"][0] == "559292" assert calls["args"][2] == "taxonomy_id" assert calls["args"][3] == "gene_oln" - assert calls["args"][4] is True + assert calls["args"][4] is False assert out.exists() -def test_cli_uniprot_download_include_unreviewed(tmp_path, monkeypatch): +def test_cli_uniprot_download_reviewed_only(tmp_path, monkeypatch): calls = {} def fake_download(uid, path, *, id_type, gene_id_field, reviewed): @@ -171,9 +171,9 @@ def fake_download(uid, path, *, id_type, gene_id_field, reviewed): ) main([ "uniprot-download", "559292", str(tmp_path / "u.tsv"), - "--include-unreviewed", + "--reviewed-only", ]) - assert calls["reviewed"] is False + assert calls["reviewed"] is True def test_cli_kegg_download_invokes_function(tmp_path, monkeypatch, capsys): From 562ba29ac5f5dc8c4d28042b23ac781b1bdcdac1 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:41:37 +0100 Subject: [PATCH 12/42] fix: skip the kegg download in load_kegg_tsv without an organism code --- CHANGELOG.md | 1 + src/geckopy/databases/kegg_loader.py | 16 +++++++++++----- tests/test_kegg_loader.py | 10 ++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe7c8ca..73a04ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `load_kegg_tsv` with an empty `kegg_id` and no `kegg.tsv` logs a warning and returns None, as GECKO's `loadDatabases` does without a `kegg.ID`; it called `download_kegg` with an empty organism code, which raised. * `geckopy uniprot-download` keeps reviewed and unreviewed entries by default, as the adapter default `uniprot.reviewed = false` and GECKO's `loadDatabases` do; `--reviewed-only` keeps the reviewed (Swiss-Prot) entries only. The option `--include-unreviewed` is removed; the command kept only reviewed entries by default. * `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. * `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. diff --git a/src/geckopy/databases/kegg_loader.py b/src/geckopy/databases/kegg_loader.py index d78132f..05fc887 100644 --- a/src/geckopy/databases/kegg_loader.py +++ b/src/geckopy/databases/kegg_loader.py @@ -1,7 +1,7 @@ """Parse kegg.tsv into a KeggDB struct, downloading from KEGG REST if missing. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/loadDatabases.m (KEGG branch). +src/download_databases/loadDatabases.m (KEGG branch). Schema in kegg.tsv (comma-delimited, no header, 7 columns): @@ -58,12 +58,14 @@ def load_kegg_tsv( kegg_id: str | None = None, gene_id_field: str = "kegg", auto_download: bool = True, -) -> KeggDB: +) -> KeggDB | None: """Parse a kegg.tsv into a KeggDB. If ``path`` does not exist and ``kegg_id`` is given and ``auto_download`` is True, fetches the data via ``download_kegg`` - first, then parses. + first, then parses. An empty ``kegg_id`` skips the download with a + warning and returns None, as GECKO's ``loadDatabases`` does without a + ``kegg.ID``. Parameters ---------- @@ -72,7 +74,7 @@ def load_kegg_tsv( auto-download. kegg_id KEGG organism code (e.g. ``"sce"``). Required for - auto-download; otherwise unused. + auto-download; otherwise unused. ``""`` means no KEGG data. gene_id_field KEGG entry field to use as the gene matching key. Default ``"kegg"`` uses column 2, the bare KEGG gene ID. @@ -83,7 +85,8 @@ def load_kegg_tsv( Returns ------- - KeggDB + KeggDB or None + None when ``path`` does not exist and ``kegg_id`` is empty. Raises ------ @@ -95,6 +98,9 @@ def load_kegg_tsv( """ path = Path(path) if not path.is_file(): + if auto_download and kegg_id == "": + logger.warning("No KEGG organism code (kegg_id) is given; KEGG data is not downloaded.") + return None if not auto_download or kegg_id is None: raise FileNotFoundError( f"kegg.tsv not found: {path}. " diff --git a/tests/test_kegg_loader.py b/tests/test_kegg_loader.py index 09179fe..56881b9 100644 --- a/tests/test_kegg_loader.py +++ b/tests/test_kegg_loader.py @@ -146,3 +146,13 @@ def test_real_fixture_loads(tmp_path): assert db.genes[0] == "Q0045" assert db.eccodes[0] == "7.1.1.9" assert db.mw[0] == 58798.0 + + +def test_empty_kegg_id_skips_download(tmp_path, monkeypatch, caplog): + """As loadDatabases without a kegg.ID: a warning, no download, no data.""" + import geckopy.databases.kegg_download as kegg_download + + monkeypatch.setattr(kegg_download, "download_kegg", lambda *a, **k: pytest.fail("downloaded")) + with caplog.at_level("WARNING"): + assert load_kegg_tsv(tmp_path / "kegg.tsv", kegg_id="") is None + assert "No KEGG organism code" in caplog.text From 907264b7004101d3665f55e23f3f833bce1b8e39 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:44:21 +0100 Subject: [PATCH 13/42] feat: download missing uniprot and kegg files in make_ec_model --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 4 +-- src/geckopy/ec_model/make_ec_model.py | 41 ++++++++++++++------- tests/test_make_ec_model.py | 51 +++++++++++++++++++++++++-- 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73a04ea..6053423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `make_ec_model` without `uniprot_db` or `kegg_db` downloads a missing `data/uniprot.tsv` or `data/kegg.tsv` with the `[uniprot]` and `[kegg]` settings of the adapter, as GECKO's `makeEcModel` does through `loadDatabases`; an empty `kegg.id` leaves KEGG out with a warning. It used to raise `FileNotFoundError` for a missing `uniprot.tsv` and to use no KEGG data without `kegg.tsv`, so a project without `kegg.tsv` and with a `kegg.id` now gets the KEGG fallback for genes UniProt does not know. * `load_kegg_tsv` with an empty `kegg_id` and no `kegg.tsv` logs a warning and returns None, as GECKO's `loadDatabases` does without a `kegg.ID`; it called `download_kegg` with an empty organism code, which raised. * `geckopy uniprot-download` keeps reviewed and unreviewed entries by default, as the adapter default `uniprot.reviewed = false` and GECKO's `loadDatabases` do; `--reviewed-only` keeps the reviewed (Swiss-Prot) entries only. The option `--include-unreviewed` is removed; the command kept only reviewed entries by default. * `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 5313eea..67106a8 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -221,7 +221,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | MATLAB | geckopy | Notes | | ---------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `makeEcModel` | `make_ec_model` | **`[3→4]`** ec.genes/enzymes/mw/sequence are sorted alphabetically for stable order; KEGG fallback for unmatched genes is opt-in via `kegg_db=`. **`[Py]`** Single return; unmatched genes are logged and annotated on `rxn.notes['geckopy_warning']` instead of being returned. | +| `makeEcModel` | `make_ec_model` | **`[3→4]`** ec.genes/enzymes/mw/sequence are sorted alphabetically for stable order; KEGG fallback for unmatched genes uses `data/kegg.tsv`, downloaded when missing (an empty `kegg.id` leaves KEGG out), or `kegg_db=`. **`[Py]`** Single return; unmatched genes are logged and annotated on `rxn.notes['geckopy_warning']` instead of being returned. | | `applyComplexData` | `apply_complex_data` | — | | `applyCustomKcats` | `apply_custom_kcats` | **`[Py]`** Mutates; diagnostics via logging. Takes a pre-loaded DataFrame, or reads `/data/customKcats.tsv` by default. | | `applyKcatConstraints` | `apply_kcat_constraints` | **`[3→4]`** Writes coefficients with the forward-direction `usage_prot_*` / `prot_pool_exchange` convention. | @@ -279,7 +279,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `getECfromGEM` | `fill_eccodes_from_gem` | **`[3→4]`** Actually assigns EC codes (the GECKO 3 validation regex silently discarded every EC string). **`[Py]`** Renamed; `get_ec_from_gem` kept as deprecated alias. | | `getECstring` | *(internal helper inside `fill_eccodes_*`)* | Not exposed as a public entry-point — folded into the EC-fill helpers. | | `loadBRENDAdata` | `load_brenda_data` | **`[3→4]`** Reads the new TSV schema (`kcat.tsv` / `sa.tsv` / `mw.tsv`, bare EC codes, plain organism names, `references` column). **`[Py]`** Ships **both** `max` and `median` per (ec, substrate, organism) triple — see [kcat_aggregation.md](kcat_aggregation.md). Returns a `BrendaData` dataclass. | -| `loadDatabases` | *(implicit)* | **`[Py]`** No combined-load entry-point; users call `load_brenda_data`, `load_uniprot_tsv`, etc. directly. | +| `loadDatabases` | *(implicit)* | **`[Py]`** No combined-load entry-point: `make_ec_model` loads `uniprot.tsv` and `kegg.tsv` and downloads a missing one with the adapter's settings, as `makeEcModel` does through `loadDatabases`; elsewhere call `load_uniprot_tsv` and `load_kegg_tsv` directly. | ### 5.4 Kcat sensitivity analysis and tuning (`src/kcat_tuning`) diff --git a/src/geckopy/ec_model/make_ec_model.py b/src/geckopy/ec_model/make_ec_model.py index 9ef7ae4..b28eced 100644 --- a/src/geckopy/ec_model/make_ec_model.py +++ b/src/geckopy/ec_model/make_ec_model.py @@ -152,9 +152,11 @@ def make_ec_model( (HumanGEM, Recon3D) where the full layout's LP size becomes impractical. See ``docs/gecko_light_status.md``. uniprot_db - Pre-loaded UniprotDB. If None, the function looks for - ``adapter.params.path / "data" / "uniprot.tsv"`` and loads - it automatically. + Pre-loaded UniprotDB. If None, the function loads + ``adapter.params.path / "data" / "uniprot.tsv"``, and first + downloads it with the ``[uniprot]`` settings of the adapter + (``id``, ``type``, ``gene_id_field``, ``reviewed``) when the file + is missing, as GECKO's ``loadDatabases`` does. kegg_db Pre-loaded KeggDB. Optional fallback source for protein information and EC numbers. When provided, stage 7 fills @@ -163,8 +165,11 @@ def make_ec_model( gene id when that accession is empty), and the EC-number lookup (``fill_eccodes_from_database``) consults KEGG when the UniProt result is empty or ends with ``-``. If None, the - function loads ``adapter.params.path / "data" / "kegg.tsv"`` - when that file exists, and uses no KEGG data otherwise. + function loads ``adapter.params.path / "data" / "kegg.tsv"``, + and first downloads it with the ``[kegg]`` settings of the + adapter (``id``, ``gene_id``) when the file is missing; with an + empty ``kegg.id`` it warns and uses no KEGG data, as GECKO's + ``loadDatabases`` does. Returns ------- @@ -175,8 +180,10 @@ def make_ec_model( Raises ------ FileNotFoundError - If ``uniprot_db`` is None and no ``uniprot.tsv`` is found - under the adapter's data folder. + If ``uniprot_db`` is None, no ``uniprot.tsv`` is found under the + adapter's data folder and ``uniprot.id`` is empty. + RuntimeError + If a download fails. ValueError As ``makeEcModel`` does, if the compartment named by ``adapter.params.enzyme_comp`` is not one of the model's @@ -243,14 +250,24 @@ def make_ec_model( # Promote to EcModel for stages 6-12. ec_model = EcModel.from_cobra(model, adapter, gecko_light=gecko_light) + # As GECKO's loadDatabases: a missing file is downloaded with the adapter's settings. + data_dir = adapter.params.path / "data" if uniprot_db is None: - uniprot_path = adapter.params.path / "data" / "uniprot.tsv" - uniprot_db = load_uniprot_tsv(uniprot_path) + uniprot = adapter.params.uniprot + uniprot_db = load_uniprot_tsv( + data_dir / "uniprot.tsv", + uniprot_id=uniprot.id or None, + id_type=uniprot.type, + gene_id_field=uniprot.gene_id_field, + reviewed=uniprot.reviewed, + ) if kegg_db is None: - kegg_path = adapter.params.path / "data" / "kegg.tsv" - if kegg_path.is_file(): - kegg_db = load_kegg_tsv(kegg_path, auto_download=False) + kegg_db = load_kegg_tsv( + data_dir / "kegg.tsv", + kegg_id=adapter.params.kegg.id, + gene_id_field=adapter.params.kegg.gene_id, + ) if gecko_light: # Light: stage 7 (per-enzyme data) runs first so the coupling diff --git a/tests/test_make_ec_model.py b/tests/test_make_ec_model.py index 70a4a44..dfe8bcb 100644 --- a/tests/test_make_ec_model.py +++ b/tests/test_make_ec_model.py @@ -262,12 +262,13 @@ def test_make_ec_model_gecko_light_enzymes_match_full(): def test_make_ec_model_missing_uniprot_tsv_raises(tmp_path): - """If the adapter points at a folder without uniprot.tsv, and no - explicit uniprot_db is given, raise FileNotFoundError.""" + """If the adapter points at a folder without uniprot.tsv, no explicit + uniprot_db is given and uniprot.id is empty, raise FileNotFoundError.""" toml = ( 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' + '[uniprot]\nid = ""\n' ) (tmp_path / "model_adapter.toml").write_text(toml) adapter = ModelAdapter.from_folder(tmp_path) @@ -285,6 +286,48 @@ def test_make_ec_model_missing_uniprot_tsv_raises(tmp_path): make_ec_model(model, adapter) +def test_make_ec_model_downloads_missing_databases(tmp_path, monkeypatch): + """As loadDatabases: a missing uniprot.tsv or kegg.tsv is downloaded with + the adapter's settings; an empty kegg.id leaves KEGG out.""" + import shutil + + import geckopy.databases.kegg_download as kegg_download + import geckopy.databases.uniprot_download as uniprot_download + + project = tmp_path / "ecTestGEM" + shutil.copytree(EXAMPLE_DIR, project) + data = project / "data" + uniprot_text = (data / "uniprot.tsv").read_text(encoding="utf-8") + kegg_text = (data / "kegg.tsv").read_text(encoding="utf-8") + (data / "uniprot.tsv").unlink() + (data / "kegg.tsv").unlink() + calls = {} + + def fake_uniprot(uniprot_id, path, *, id_type, gene_id_field, reviewed): + calls["uniprot"] = (uniprot_id, id_type, gene_id_field, reviewed) + Path(path).write_text(uniprot_text, encoding="utf-8") + + def fake_kegg(kegg_id, path, *, gene_id_field): + calls["kegg"] = (kegg_id, gene_id_field) + Path(path).write_text(kegg_text, encoding="utf-8") + + monkeypatch.setattr(uniprot_download, "download_uniprot", fake_uniprot) + monkeypatch.setattr(kegg_download, "download_kegg", fake_kegg) + adapter = ModelAdapter.from_folder(project) + model = cobra.io.read_sbml_model(str(adapter.params.conv_gem)) + expected = make_ec_model(model, ModelAdapter.from_folder(EXAMPLE_DIR)) + + ec_model = make_ec_model(model, adapter) + assert calls == {"uniprot": ("TE00000000", "taxonomy", "gene_tst", True), "kegg": ("tst", "kegg")} + assert ec_model.ec.enzymes == expected.ec.enzymes + + (data / "kegg.tsv").unlink() + calls.clear() + adapter.params.kegg.id = "" + make_ec_model(model, adapter) + assert calls == {} + + def test_make_ec_model_refuses_rerun_on_populated_ecmodel(): """Calling make_ec_model twice on the same model should raise the second time, because the model already has a populated ec.""" @@ -302,6 +345,7 @@ def test_make_ec_model_unmatched_genes_logged_and_annotated(tmp_path, caplog): 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' + '[kegg]\nid = ""\n' ) (tmp_path / "model_adapter.toml").write_text(toml) adapter = ModelAdapter.from_folder(tmp_path) @@ -339,6 +383,7 @@ def _toy_with_missing_gene(tmp_path): 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' + '[kegg]\nid = ""\n' ) adapter = ModelAdapter.from_folder(tmp_path) model = cobra.Model("m") @@ -373,7 +418,7 @@ def test_make_ec_model_reads_kegg_tsv_of_the_data_folder(tmp_path): assert sorted(ec_model.ec.enzymes) == ["P1", "P2"] -def test_make_ec_model_without_kegg_tsv_leaves_the_gene_unmatched(tmp_path): +def test_make_ec_model_without_kegg_id_leaves_the_gene_unmatched(tmp_path): model, adapter, db = _toy_with_missing_gene(tmp_path) ec_model = make_ec_model(model, adapter, uniprot_db=db) assert sorted(ec_model.ec.enzymes) == ["P1"] From f040c801cacd126f28f220d83839e61fc4ecedba Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:45:00 +0100 Subject: [PATCH 14/42] feat: take precomputed complex families in level_complex_concs --- CHANGELOG.md | 1 + src/geckopy/limit_proteins/level_complex_concs.py | 6 +++++- tests/test_level_complex_concs.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6053423..9155ed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `level_complex_concs` takes `families`, the complexes of the model as `complex_families` returns them (default: calculated from the model), as GECKO's `levelComplexConcs` does. * `make_ec_model` without `uniprot_db` or `kegg_db` downloads a missing `data/uniprot.tsv` or `data/kegg.tsv` with the `[uniprot]` and `[kegg]` settings of the adapter, as GECKO's `makeEcModel` does through `loadDatabases`; an empty `kegg.id` leaves KEGG out with a warning. It used to raise `FileNotFoundError` for a missing `uniprot.tsv` and to use no KEGG data without `kegg.tsv`, so a project without `kegg.tsv` and with a `kegg.id` now gets the KEGG fallback for genes UniProt does not know. * `load_kegg_tsv` with an empty `kegg_id` and no `kegg.tsv` logs a warning and returns None, as GECKO's `loadDatabases` does without a `kegg.ID`; it called `download_kegg` with an empty organism code, which raised. * `geckopy uniprot-download` keeps reviewed and unreviewed entries by default, as the adapter default `uniprot.reviewed = false` and GECKO's `loadDatabases` do; `--reviewed-only` keeps the reviewed (Swiss-Prot) entries only. The option `--include-unreviewed` is removed; the command kept only reviewed entries by default. diff --git a/src/geckopy/limit_proteins/level_complex_concs.py b/src/geckopy/limit_proteins/level_complex_concs.py index 9274c35..857e4e0 100644 --- a/src/geckopy/limit_proteins/level_complex_concs.py +++ b/src/geckopy/limit_proteins/level_complex_concs.py @@ -241,6 +241,7 @@ def level_complex_concs( *, statistic: str = "median", fill_missing: bool = True, + families: Optional[ComplexFamilies] = None, ) -> LevelComplexResult: """Give every complex in ``model.ec.concs`` one abundance. @@ -258,6 +259,9 @@ def level_complex_concs( fill_missing Give unmeasured, non-shared subunits the complex's concentration. A subunit with a concentration of 0 is measured as absent and stays 0. + families + The complexes of the model, as returned by ``complex_families`` + (default: calculated from the model). Returns ------- @@ -272,7 +276,7 @@ def level_complex_concs( measured = [i for i in range(len(enzymes)) if np.isfinite(molar[i])] ids, levelled, result = level_complex_abundances( model, [enzymes[i] for i in measured], molar[measured], - statistic=statistic, fill_missing=fill_missing, + statistic=statistic, fill_missing=fill_missing, families=families, ) new = concs.copy() index = {e: i for i, e in enumerate(enzymes)} diff --git a/tests/test_level_complex_concs.py b/tests/test_level_complex_concs.py index 0b1a06e..6ef8757 100644 --- a/tests/test_level_complex_concs.py +++ b/tests/test_level_complex_concs.py @@ -120,6 +120,18 @@ def test_level_complex_concs_works_in_mass_units(): assert np.isnan(new["H"]) +def test_level_complex_concs_takes_precomputed_families(): + """As levelComplexConcs' families option: the given complexes are levelled.""" + concs = np.full(len(ENZYMES), np.nan) + concs[ENZYMES.index("A")] = 100.0 + concs[ENZYMES.index("B")] = 1600.0 + model = _ec_only(concs, ROWS) + only_r2 = complex_families(_ec_only(np.full(len(ENZYMES), np.nan), {"R2": ROWS["R2"]})) + result = level_complex_concs(model, families=only_r2) + assert result.adjusted == [] + np.testing.assert_array_equal(model.ec.concs, concs) + + def test_an_unknown_statistic_is_rejected(): model = _ec_only(np.full(len(ENZYMES), np.nan), ROWS) with pytest.raises(ValueError, match="statistic"): From 6868d2853d6ff75ea7f65bfe1ee1fe2076e53494 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:45:45 +0100 Subject: [PATCH 15/42] feat: return the default f factor without proteomics data --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 2 +- .../limit_proteins/calculate_f_factor.py | 51 ++++++++++++++----- tests/test_calculate_f_factor.py | 32 ++++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9155ed4..bcbe63f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. * `level_complex_concs` takes `families`, the complexes of the model as `complex_families` returns them (default: calculated from the model), as GECKO's `levelComplexConcs` does. * `make_ec_model` without `uniprot_db` or `kegg_db` downloads a missing `data/uniprot.tsv` or `data/kegg.tsv` with the `[uniprot]` and `[kegg]` settings of the adapter, as GECKO's `makeEcModel` does through `loadDatabases`; an empty `kegg.id` leaves KEGG out with a warning. It used to raise `FileNotFoundError` for a missing `uniprot.tsv` and to use no KEGG data without `kegg.tsv`, so a project without `kegg.tsv` and with a `kegg.id` now gets the KEGG fallback for genes UniProt does not know. * `load_kegg_tsv` with an empty `kegg_id` and no `kegg.tsv` logs a warning and returns None, as GECKO's `loadDatabases` does without a `kegg.ID`; it called `download_kegg` with an empty organism code, which raised. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 67106a8..46ef2ac 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -294,7 +294,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | MATLAB | geckopy | Notes | | --------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `calculateFfactor` | `calculate_f_factor` | **`[Py]`** Raises `FileNotFoundError` when no proteome data is provided (MATLAB silently returns `0.5`). | +| `calculateFfactor` | `calculate_f_factor` | Without `prot_data` it reads `data/paxDB.tsv` of the project, and without that file it returns `0.5` with a warning, as MATLAB does. | | `constrainEnzConcs` | `constrain_enz_concs` | **`[3→4]`** Sets the **upper** bound of `usage_prot_*` (forward direction). **`[Py]`** Raises `NotImplementedError` on gecko-light (no per-enzyme constraint to set). | | `constrainFluxData` | `apply_flux_data_constraints` | **`[Py]`** Renamed; `constrain_flux_data` kept as deprecated alias. Takes pre-loaded `FluxData`. 0-indexed `condition` (default `0`, MATLAB `1`). | | `fillEnzConcs` | `fill_enz_concs` | — | diff --git a/src/geckopy/limit_proteins/calculate_f_factor.py b/src/geckopy/limit_proteins/calculate_f_factor.py index 8e456d3..5546f86 100644 --- a/src/geckopy/limit_proteins/calculate_f_factor.py +++ b/src/geckopy/limit_proteins/calculate_f_factor.py @@ -13,15 +13,15 @@ geckopy computes f from proteomics data (a ``ProtData`` table loaded by ``load_pax_db`` or similar): for each measured protein, check whether it's in the model, sum the masses, divide by the -total. A default value of 0.5 is a reasonable starting point when -no proteomics data is available. +total. Without proteomics data it returns the default value 0.5. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/calculateFfactor.m. +src/limit_proteins/calculateFfactor.m. """ from __future__ import annotations import logging +from pathlib import Path from typing import TYPE_CHECKING, Optional import numpy as np @@ -35,7 +35,7 @@ def calculate_f_factor( model: "EcModel", - prot_data: "ProtData", + prot_data: Optional["ProtData"] = None, *, enzymes: Optional[list[str]] = None, ) -> float: @@ -53,19 +53,20 @@ def calculate_f_factor( per-protein average is taken first via ``np.nanmean(axis=1)``. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/calculateFfactor.m. + src/limit_proteins/calculateFfactor.m. Parameters ---------- model - EcModel; only used to default ``enzymes`` to - ``model.ec.enzymes``. + EcModel; defaults ``enzymes`` to ``model.ec.enzymes``, and its + adapter locates ``data/paxDB.tsv`` without ``prot_data``. prot_data Pre-loaded proteomics data (typically from ``load_pax_db``). - There is no implicit fallback when proteomics data is - unavailable -- callers without measurements should use a - default value such as ``f = 0.5`` directly instead of calling - this function. + If None, ``data/paxDB.tsv`` of the adapter's project is loaded + with ``load_pax_db`` and the project's ``data/uniprot.tsv`` + (downloaded with the adapter's ``[uniprot]`` settings when + missing). Without that file, a warning is logged and the default + f of 0.5 is returned, as GECKO's ``calculateFfactor`` does. enzymes UniProt IDs counted as "in the model". Defaults to ``model.ec.enzymes``. @@ -73,13 +74,35 @@ def calculate_f_factor( Returns ------- float - The f-factor in [0, 1]. Returns NaN, as MATLAB does, if the total - proteome abundance is zero (the factor is 0/0), and 0.0 if none of - the proteomics UniProt IDs match a model enzyme. + The f-factor in [0, 1]. Returns 0.5 without proteomics data, NaN, + as MATLAB does, if the total proteome abundance is zero (the factor + is 0/0), and 0.0 if none of the proteomics UniProt IDs match a model + enzyme. """ if enzymes is None: enzymes = model.ec.enzymes + if prot_data is None: + adapter = getattr(model, "adapter", None) + pax_path = Path(adapter.params.path) / "data" / "paxDB.tsv" if adapter is not None else None + if pax_path is None or not pax_path.is_file(): + logger.warning( + "calculate_f_factor: no proteomics data is provided or can be " + "found; returning the default f = 0.5." + ) + return 0.5 + from ..databases import load_pax_db, load_uniprot_tsv + + uniprot = adapter.params.uniprot + uniprot_db = load_uniprot_tsv( + pax_path.parent / "uniprot.tsv", + uniprot_id=uniprot.id or None, + id_type=uniprot.type, + gene_id_field=uniprot.gene_id_field, + reviewed=uniprot.reviewed, + ) + prot_data = load_pax_db(pax_path, uniprot_db) + abundances = prot_data.abundances if abundances.ndim == 2: avg = np.nanmean(abundances, axis=1) diff --git a/tests/test_calculate_f_factor.py b/tests/test_calculate_f_factor.py index cc23daf..041cdb0 100644 --- a/tests/test_calculate_f_factor.py +++ b/tests/test_calculate_f_factor.py @@ -189,3 +189,35 @@ def test_explicit_empty_enzyme_list_means_no_enzymes(): pd = _prot_data([("P1", 100.0), ("P2", 100.0)]) assert calculate_f_factor(model, pd, enzymes=[]) == 0.0 assert calculate_f_factor(model, pd) == pytest.approx(1.0) + + +# --------------------------------------------------------------------------- # +# Without proteomics data +# --------------------------------------------------------------------------- # + +def test_no_proteomics_data_returns_the_default(tmp_path, caplog): + """As calculateFfactor: without data or data/paxDB.tsv, 0.5 with a warning.""" + from geckopy import ModelAdapter + + (tmp_path / "model_adapter.toml").write_text('conv_gem = "dummy.xml"\norg_name = "test"\n') + model = _ec_model(["P1"]) + with caplog.at_level("WARNING"): + assert calculate_f_factor(model) == 0.5 + model.adapter = ModelAdapter.from_folder(tmp_path) + assert calculate_f_factor(model) == 0.5 + assert "default f = 0.5" in caplog.text + + +def test_paxdb_of_the_project_is_read_without_prot_data(tmp_path): + from geckopy import ModelAdapter + + (tmp_path / "model_adapter.toml").write_text('conv_gem = "dummy.xml"\norg_name = "test"\n') + data = tmp_path / "data" + data.mkdir() + (data / "uniprot.tsv").write_text( + "Entry\tGene Names\tEC number\tMass\tSequence\n" + "P1\tg1\t\t100\tM\nP2\tg2\t\t100\tM\n", encoding="utf-8") + (data / "paxDB.tsv").write_text("#header\n1\t4932.g1\t1\n2\t4932.g2\t3\n", encoding="utf-8") + model = _ec_model(["P1"]) + model.adapter = ModelAdapter.from_folder(tmp_path) + assert calculate_f_factor(model) == pytest.approx(0.25) From 81968bf6c1019583d37c831a210702a210a9a9e4 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:46:04 +0100 Subject: [PATCH 16/42] feat: take method in the sigma_fitter alias --- CHANGELOG.md | 1 + src/geckopy/kcat_tuning/sigma_fitter.py | 3 ++- tests/test_deprecated_aliases.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcbe63f..67dcc27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. * `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. * `level_complex_concs` takes `families`, the complexes of the model as `complex_families` returns them (default: calculated from the model), as GECKO's `levelComplexConcs` does. * `make_ec_model` without `uniprot_db` or `kegg_db` downloads a missing `data/uniprot.tsv` or `data/kegg.tsv` with the `[uniprot]` and `[kegg]` settings of the adapter, as GECKO's `makeEcModel` does through `loadDatabases`; an empty `kegg.id` leaves KEGG out with a warning. It used to raise `FileNotFoundError` for a missing `uniprot.tsv` and to use no KEGG data without `kegg.tsv`, so a project without `kegg.tsv` and with a `kegg.id` now gets the KEGG fallback for genes UniProt does not know. diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index 97460a7..167a181 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -266,6 +266,7 @@ def sigma_fitter( p_tot: Optional[float] = None, f: Optional[float] = None, n_sigma_steps: int = 100, + method: Literal["grid", "bisect"] = "grid", ) -> SigmaFitterResult: """Deprecated alias for :func:`fit_sigma`. @@ -283,5 +284,5 @@ def sigma_fitter( return fit_sigma( model, growth_rate=growth_rate, p_tot=p_tot, f=f, - n_sigma_steps=n_sigma_steps, + n_sigma_steps=n_sigma_steps, method=method, ) diff --git a/tests/test_deprecated_aliases.py b/tests/test_deprecated_aliases.py index 0668920..b57b889 100644 --- a/tests/test_deprecated_aliases.py +++ b/tests/test_deprecated_aliases.py @@ -178,3 +178,15 @@ def test_merge_dlkcat_and_fuzzy_kcats_alias_emits_warning(): and "merge_kcats" in str(w.message) for w in recorded ) + + +def test_sigma_fitter_alias_passes_method(monkeypatch): + import importlib + + module = importlib.import_module("geckopy.kcat_tuning.sigma_fitter") + seen = {} + monkeypatch.setattr(module, "fit_sigma", lambda model, **kwargs: seen.update(kwargs)) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + module.sigma_fitter(object(), method="bisect") + assert seen["method"] == "bisect" From eecb08831a92b6fd8a6520ce7d28dc5c2374b642 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:47:08 +0100 Subject: [PATCH 17/42] fix: merge a kcat_sources table into the preset settings --- CHANGELOG.md | 1 + src/geckopy/gather_kcats/kcat_assignment.py | 7 +++++-- tests/test_kcat_assignment.py | 9 +++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67dcc27..99e6a0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). * The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. * `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. * `level_complex_concs` takes `families`, the complexes of the model as `complex_families` returns them (default: calculated from the model), as GECKO's `levelComplexConcs` does. diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index 11768ec..f9e3d1a 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -99,7 +99,9 @@ def _preset_stages(preset: str) -> tuple[list[Stage], dict[str, dict[str, Any]]] def kcat_assignment_config(adapter: "ModelAdapter") -> KcatAssignmentConfig: """Read ``[kcat_assignment]`` and ``[kcat_sources]`` of the adapter. - Without a preset or stages the preset is ``brenda_ec_tree``. + Without a preset or stages the preset is ``brenda_ec_tree``. A + ``[kcat_sources.]`` table overrides only the keys it sets of the + preset's settings for that source. """ a = adapter.params.kcat_assignment if a.stage: @@ -110,7 +112,8 @@ def kcat_assignment_config(adapter: "ModelAdapter") -> KcatAssignmentConfig: else: preset = a.preset or "brenda_ec_tree" stages, sources = _preset_stages(preset) - sources = {**sources, **{k: dict(v) for k, v in adapter.params.kcat_sources.items()}} + # A [kcat_sources.] table overrides only the keys it sets. + sources = {**sources, **{k: {**sources.get(k, {}), **v} for k, v in adapter.params.kcat_sources.items()}} return KcatAssignmentConfig(preset=preset, isozymes=a.isozymes, standard=a.standard, stages=stages, sources=sources) diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 2aaf7e0..29d6c03 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -155,6 +155,15 @@ def test_collect_kcats_returns_isozyme_kcats_without_constraints(tmp_path): assert any(stoich(decided, rxn) != stoich(base, rxn) for rxn in filled) +def test_kcat_sources_table_overrides_only_its_keys(tmp_path): + """[kcat_sources.brenda] without aggregation keeps the preset's median.""" + from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config + + _, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "gecko_median"\n' + '[kcat_sources.brenda]\nforce_wildcard_level = 1\n') + assert kcat_assignment_config(adapter).sources["brenda"] == {"aggregation": "median", "force_wildcard_level": 1} + + def test_stages_from_toml(tmp_path): """Stages from model_adapter.toml, and their errors.""" extra = ('[kcat_assignment]\nisozymes = false\nstandard = false\n' From f27b3f657badeff68c24731026b4e19d2bbc86ea Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:47:44 +0100 Subject: [PATCH 18/42] fix: raise in ec_fseof when there is no production range to scan --- CHANGELOG.md | 1 + src/geckopy/utilities/ec_fseof.py | 30 ++++++++++++++++++++++++++++++ tests/test_ec_fseof.py | 13 +++++++++++++ 3 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99e6a0f..0e8c8f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). * The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. * `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index 059acee..3d3ce79 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -46,6 +46,8 @@ import logging from typing import TYPE_CHECKING, Optional +from cobra.exceptions import OptimizationError +from cobra.flux_analysis import pfba from raven_toolbox.analysis.fseof import FSEOFResult, fseof from ..solver_support import optimize_model @@ -57,6 +59,8 @@ _USAGE_PROT_PREFIX = "usage_prot_" _STANDARD_GENE = "standard" +# The fraction of the optimum pFBA keeps, as in raven-toolbox's fseof (RAVEN's solveLP). +_PFBA_FRACTION = 0.999999 def ec_fseof( @@ -123,6 +127,13 @@ def ec_fseof( :class:`raven_toolbox.analysis.fseof.FSEOFResult` for the full field list (``scan``, ``enforced``, ``targets``, plus the ``amplification`` / ``knockout`` / ``gene_targets`` properties). + + Raises + ------ + ValueError + If the production target cannot carry flux, or if its flux at + maximum growth is not below the top of the scan (``max_fraction`` + of its maximum), as GECKO's ``ecFSEOF`` raises ``ecFSEOF:noRange``. """ from ..adapter import resolve_param bio_rxn_id = resolve_param( @@ -134,6 +145,13 @@ def ec_fseof( _check_cs_consistency(model, bio_rxn_id, cs_rxn) min_target = _biomass_optimal_target_flux(model, bio_rxn_id, prod_target_rxn) + max_target = _scan_ceiling(model, prod_target_rxn, max_fraction) + # A blocked target (ceiling 0) is left to fseof, which raises its own error. + if max_target > 0 and min_target >= max_target: + raise ValueError( + f"ec_fseof: the flux of {prod_target_rxn!r} at maximum growth ({min_target:g}) is not " + f"below the top of the scan ({max_target:g}), so there is no range to scan." + ) result = fseof( model, prod_target_rxn, @@ -193,6 +211,18 @@ def _biomass_optimal_target_flux( return float(sol.fluxes.get(prod_target_rxn_id, 0.0)) +def _scan_ceiling(model: "EcModel", prod_target_rxn_id: str, max_fraction: float) -> float: + """The top of the scan, as raven-toolbox's ``fseof`` and ``ecFSEOF.m`` take it: + ``max_fraction`` of the target flux in the pFBA solution that maximises the target.""" + with model: + model.objective = prod_target_rxn_id + try: + flux = pfba(model, fraction_of_optimum=_PFBA_FRACTION).fluxes[prod_target_rxn_id] + except OptimizationError: + return 0.0 + return float(flux) * max_fraction + + def _gated_reaction(model: "EcModel", rxn_id: str) -> bool: """Whether a reaction can be an engineering target: it must have a GPR, and that GPR must not reference the "standard" placeholder diff --git a/tests/test_ec_fseof.py b/tests/test_ec_fseof.py index 6c988fc..cc04875 100644 --- a/tests/test_ec_fseof.py +++ b/tests/test_ec_fseof.py @@ -154,6 +154,19 @@ def test_no_production_headroom_raises(tmp_path): ec_fseof(model, "EX_PROD") +def test_no_range_to_scan_raises(tmp_path, monkeypatch): + """As ecFSEOF:noRange: production at maximum growth at or above the top of + the scan raises before the scan.""" + import importlib + + module = importlib.import_module("geckopy.utilities.ec_fseof") + monkeypatch.setattr(module, "fseof", lambda *a, **k: pytest.fail("scanned")) + model = _build_fseof_model(_adapter(tmp_path)) + model.reactions.get_by_id("EX_PROD").lower_bound = 95.0 # the top of the scan is 90 + with pytest.raises(ValueError, match="no range to scan"): + ec_fseof(model, "EX_PROD") + + # --------------------------------------------------------------------------- # # Carbon-source sanity-check warning # --------------------------------------------------------------------------- # From fbe0ad2ff782df186f17b850b5e3eeff3830a330 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:50:00 +0100 Subject: [PATCH 19/42] feat: label ec_fseof targets and build its gene targets as ecFSEOF --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 2 +- src/geckopy/utilities/__init__.py | 3 +- src/geckopy/utilities/ec_fseof.py | 137 ++++++++++++++++++++++++---- tests/test_ec_fseof.py | 55 +++++++++-- 5 files changed, 169 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e8c8f7..87ea2e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. * `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). * The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 46ef2ac..d83a515 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -310,7 +310,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | MATLAB | geckopy | Notes | | --------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `addNewRxnsToEC` | `add_new_rxns_to_ec` | **`[Py]`** Returns an `AddNewRxnsResult` dataclass. | -| `ecFSEOF` | `ec_fseof` | **`[Py]`** Thin wrapper over `raven_toolbox.analysis.fseof`. Selection method is regression-based (`|correlation| ≥ threshold` + `|slope| ≥ eps`) with pFBA per step, replacing MATLAB's strict-monotonicity + top-25 %-by-slope filter. Result type is raven's `FSEOFResult` (replaces `EcFseofResult`). Action labels are `amplify` / `knockdown` / `knockout` (replaces `OE` / `KD` / `KO`). The per-gene `essentiality` column is dropped — use `cobra.flux_analysis.single_gene_deletion`. | +| `ecFSEOF` | `ec_fseof` | **`[Py]`** Thin wrapper over `raven_toolbox.analysis.fseof`. Targets are classified as in MATLAB, with pFBA per step: the correlation of the absolute flux with the enforced production, labelled `OE` / `KD` / `KO`; `nSteps` and `n_steps` both default to 16. Result type is `EcFSEOFResult`, raven's `FSEOFResult` with the gene targets of MATLAB (`gene_targets`: one action per gene, reconciled with the gene essentiality test). No files are written; MATLAB's `rxnTargets` and `transportTargets` are one `targets` table with a `transport` column. | | `ecFVA` | `ec_fva` | **`[Py]`** Returns a `DataFrame` indexed by reaction id with `minimum` / `maximum` columns; can run in parallel (`n_jobs=`). | | `enzymeUsage` | `enzyme_usage` | **`[3→4]`** Usage flux read from positive `usage_prot_*` flux. **`[Py]`** Returns an `EnzymeUsageResult` dataclass (`per_enzyme` DataFrame + summary scalars). | | `findGECKOroot` | *(not needed)* | Python: `pathlib.Path(__file__)` works. | diff --git a/src/geckopy/utilities/__init__.py b/src/geckopy/utilities/__init__.py index c7387cb..722824a 100644 --- a/src/geckopy/utilities/__init__.py +++ b/src/geckopy/utilities/__init__.py @@ -8,7 +8,7 @@ add_new_rxns_to_ec, ) from .bottlenecks import get_enzyme_bottlenecks -from .ec_fseof import ec_fseof +from .ec_fseof import EcFSEOFResult, ec_fseof from .ec_fva import ec_fva from .enzyme_usage import EnzymeUsageResult, enzyme_usage from .get_subset_ec_model import get_subset_ec_model @@ -21,6 +21,7 @@ __all__ = [ "AddNewRxnsResult", + "EcFSEOFResult", "EnzymeUsageReport", "EnzymeUsageResult", "MapRxnsResult", diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index 3d3ce79..a167754 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -20,32 +20,34 @@ boolean column that marks the group; - filters ``usage_prot_*`` rows out of the returned ``scan`` / ``targets`` DataFrames — those reactions are protein-pool plumbing, - never engineering targets. + never engineering targets; +- labels the targets ``OE`` (over expression), ``KD`` (knock down) and + ``KO`` (knock out), as ``ecFSEOF.m`` does, and builds the gene targets + as ``ecFSEOF.m`` does: one action per gene, reconciled over its + reactions with a gene essentiality test. The selection method and output shape come from raven-toolbox: each reaction's flux is regressed against the enforced product flux across the scan, and a reaction is a target if its flux is not constant across -the scan and ``|correlation| >= correlation_threshold``. Targets are -classified as ``amplify`` / ``knockdown`` / ``knockout``. See -:class:`raven_toolbox.analysis.fseof.FSEOFResult` for the full output -schema. +the scan and ``|correlation| >= correlation_threshold``. See +:class:`raven_toolbox.analysis.fseof.FSEOFResult` for the output schema. -Ported from GECKO MATLAB: ``src/geckomat/utilities/ecFSEOF.m``, +Ported from GECKO MATLAB: ``src/flux_analysis/ecFSEOF.m``, re-designed around :func:`raven_toolbox.analysis.fseof.fseof`. Callers who want the result tables on disk can call -``result.targets.to_csv(...)`` themselves. Per-gene essentiality is -not computed here; use :func:`cobra.flux_analysis.single_gene_deletion`, -which works directly on ecModels since GPRs already gate the catalysed -reactions. Targets come back as one table, reaction targets before transport -targets (``ecFSEOF.m``'s ``rxnTargets`` and ``transportTargets``); the -``transport`` column tells them apart. +``result.targets.to_csv(...)`` themselves. Targets come back as one table, +reaction targets before transport targets (``ecFSEOF.m``'s ``rxnTargets`` +and ``transportTargets``); the ``transport`` column tells them apart. """ from __future__ import annotations import logging +from dataclasses import dataclass, field from typing import TYPE_CHECKING, Optional +import numpy as np +import pandas as pd from cobra.exceptions import OptimizationError from cobra.flux_analysis import pfba from raven_toolbox.analysis.fseof import FSEOFResult, fseof @@ -61,6 +63,36 @@ _STANDARD_GENE = "standard" # The fraction of the optimum pFBA keeps, as in raven-toolbox's fseof (RAVEN's solveLP). _PFBA_FRACTION = 0.999999 +# raven-toolbox's labels and those of ecFSEOF.m. +_ACTIONS = {"amplify": "OE", "knockdown": "KD", "knockout": "KO"} +# Growth below this with a gene's usage blocked makes the gene essential, as in ecFSEOF.m. +_ESSENTIAL_GROWTH = 1e-8 + + +@dataclass +class EcFSEOFResult(FSEOFResult): + """The result of :func:`ec_fseof`: raven-toolbox's ``FSEOFResult`` with the + ``target_type`` labels ``OE``, ``KD`` and ``KO`` of ``ecFSEOF.m``, and the gene + targets of ``ecFSEOF.m`` in ``gene_table``.""" + + gene_table: pd.DataFrame = field(default_factory=pd.DataFrame) + + @property + def amplification(self) -> pd.DataFrame: + """Targets labelled ``OE`` (flux rises with the enforced product).""" + return self.targets[self.targets["target_type"] == "OE"].reset_index(drop=True) + + @property + def knockout(self) -> pd.DataFrame: + """Targets labelled ``KO`` or ``KD`` (flux falls as the enforced product rises).""" + return self.targets[self.targets["target_type"].isin(["KO", "KD"])].reset_index(drop=True) + + @property + def gene_targets(self) -> pd.DataFrame: + """One row per gene of the targets, as ``ecFSEOF.m``'s ``geneTargets``: ``gene``, + ``name``, ``slope`` (the mean absolute slope of its target reactions), ``action``, + ``essentiality`` (``"essential"`` or empty) and ``reactions``, by slope.""" + return self.gene_table def ec_fseof( @@ -68,7 +100,7 @@ def ec_fseof( prod_target_rxn: str, cs_rxn: Optional[str] = None, *, - n_steps: int = 10, + n_steps: int = 16, bio_rxn: Optional[str] = None, max_fraction: float = 0.9, correlation_threshold: float = 0.9, @@ -95,7 +127,8 @@ def ec_fseof( biomass-max (a common mis-configuration). ``None`` skips the check. n_steps - Number of enforced-flux levels in the scan. Default 10. + Number of enforced-flux levels in the scan. Default 16, as in + ``ecFSEOF.m``. bio_rxn Biomass reaction id. Defaults to ``adapter.params.bio_rxn`` when ``model.adapter`` is set. @@ -114,10 +147,13 @@ def ec_fseof( Returns ------- - FSEOFResult + EcFSEOFResult raven-toolbox's FSEOF output dataclass, with ``usage_prot_*`` - rows filtered out of both ``scan`` and ``targets``. ``targets`` - has an added boolean column ``transport`` and lists the reaction + rows filtered out of both ``scan`` and ``targets``. ``target_type`` + is ``OE`` (the absolute flux rises with production), ``KO`` (it + falls to at most 1e-8 at the last level) or ``KD`` (it falls + otherwise). ``targets`` has an added boolean column ``transport`` + and lists the reaction targets first, then the transport targets, each group in the order of the score ``|slope| * |correlation|`` rounded to 9 significant digits (equal scores in model order); a reaction is a transport target when a metabolite @@ -126,7 +162,16 @@ def ec_fseof( top reaction targets, then transport targets. See :class:`raven_toolbox.analysis.fseof.FSEOFResult` for the full field list (``scan``, ``enforced``, ``targets``, plus the - ``amplification`` / ``knockout`` / ``gene_targets`` properties). + ``amplification`` / ``knockout`` properties). + + ``gene_targets`` follows ``ecFSEOF.m``'s ``geneTargets``: a gene + whose target reactions have one action keeps it; with several + actions it is ``OE`` when one of them is ``OE``, otherwise ``KO`` + when the gene is not essential and ``KD`` when it is. A gene is + essential when growth is infeasible or below 1e-8 with the + usage reactions of its enzymes fixed at 0, the production target + fixed at the last enforced level and the biomass reaction + maximised from a lower bound of 0. Raises ------ @@ -165,7 +210,61 @@ def ec_fseof( ) result = _drop_usage_prot(result) result = _drop_ungated_reactions(result, model) - return _group_transport_targets(result, model) + result = _group_transport_targets(result, model) + targets = result.targets.copy() + targets["target_type"] = targets["target_type"].map(_ACTIONS) + genes = _gene_targets(model, targets, bio_rxn_id, prod_target_rxn, result.enforced[-1]) + return EcFSEOFResult(scan=result.scan, enforced=result.enforced, targets=targets, gene_table=genes) + + +def _gene_targets( + model: "EcModel", targets: pd.DataFrame, bio_rxn_id: str, prod_target_rxn_id: str, last_level: float, +) -> pd.DataFrame: + """``ecFSEOF.m``'s ``geneTargets``: per gene of the targets, the mean absolute slope + of its target reactions, one action and the result of the essentiality test.""" + columns = ["gene", "name", "slope", "action", "essentiality", "reactions"] + target_genes = {g for genes in targets["genes"] for g in genes} + if not target_genes: + return pd.DataFrame(columns=columns) + ec_genes = [g.lower() for g in model.ec.genes] + rows = [] + with model: + target = model.reactions.get_by_id(prod_target_rxn_id) + target.bounds = (last_level, last_level) + model.reactions.get_by_id(bio_rxn_id).lower_bound = 0.0 + model.objective = bio_rxn_id + for gene in model.genes: + if gene.id not in target_genes: + continue + own = targets[[gene.id in genes for genes in targets["genes"]]] + usage = [ + f"{_USAGE_PROT_PREFIX}{enzyme}" + for enzyme, ec_gene in zip(model.ec.enzymes, ec_genes) if ec_gene == gene.id.lower() + ] + with model: + for rid in usage: + if rid in model.reactions: + model.reactions.get_by_id(rid).bounds = (0.0, 0.0) + growth = model.slim_optimize(error_value=float("nan")) + essential = not np.isfinite(growth) or growth < _ESSENTIAL_GROWTH + actions = set(own["target_type"]) + if len(actions) == 1: + action = actions.pop() + elif "OE" in actions: + action = "OE" + else: + action = "KD" if essential else "KO" + rows.append({ + "gene": gene.id, + "name": gene.name, + "slope": float(np.mean(np.abs(own["slope"].to_numpy(dtype=float)))), + "action": action, + "essentiality": "essential" if essential else "", + "reactions": ";".join(own["reaction"]), + }) + table = pd.DataFrame(rows, columns=columns) + # A stable sort keeps genes with equal slopes in model order, as MATLAB's sort does. + return table.sort_values("slope", ascending=False, ignore_index=True, kind="stable") def _check_cs_consistency( diff --git a/tests/test_ec_fseof.py b/tests/test_ec_fseof.py index cc04875..ee2d7c7 100644 --- a/tests/test_ec_fseof.py +++ b/tests/test_ec_fseof.py @@ -248,25 +248,24 @@ def test_targets_have_expected_columns(tmp_path): def test_r_prod_classified_as_amplify(tmp_path): """As the enforced EX_PROD flux rises, R_PROD's flux rises with it; - raven's regression-based selection labels that ``amplify``.""" + that is labelled ``OE``, as in ecFSEOF.m.""" adapter = _adapter(tmp_path) model = _build_fseof_model(adapter) result = ec_fseof(model, "EX_PROD", n_steps=8) rprod = result.targets[result.targets["reaction"] == "R_PROD"] assert len(rprod) == 1, "R_PROD must be classified as a target" - assert rprod.iloc[0]["target_type"] == "amplify" + assert rprod.iloc[0]["target_type"] == "OE" def test_r_bio_classified_as_knockdown(tmp_path): """R_BIO's flux falls as biomass loses budget to the enforced - product flux — raven labels that ``knockdown`` (or ``knockout`` - when the fall is to ~0).""" + product flux, labelled ``KD`` (or ``KO`` when the fall is to ~0).""" adapter = _adapter(tmp_path) model = _build_fseof_model(adapter) result = ec_fseof(model, "EX_PROD", n_steps=8) rbio = result.targets[result.targets["reaction"] == "R_BIO"] assert len(rbio) == 1 - assert rbio.iloc[0]["target_type"] in ("knockdown", "knockout") + assert rbio.iloc[0]["target_type"] in ("KD", "KO") # --------------------------------------------------------------------------- # @@ -341,9 +340,8 @@ def test_usage_prot_rxns_dropped_from_targets(tmp_path): def test_gene_targets_property_recomputes_off_filtered_targets(tmp_path): - """raven's FSEOFResult.gene_targets is a @property over .targets; - once we filter usage_prot_* from targets, the per-gene rollup - inherits the filter automatically.""" + """The gene targets are built from the filtered targets, so usage_prot_* + reactions do not enter them.""" adapter = _adapter(tmp_path) model = _build_fseof_model(adapter) result = ec_fseof(model, "EX_PROD", n_steps=6) @@ -455,3 +453,44 @@ def test_scores_equal_to_nine_digits_keep_model_order(): grouped = module._group_transport_targets(result, model).targets assert list(grouped["reaction"]) == ["R1", "R2", "R3"] + + +# --------------------------------------------------------------------------- # +# ecFSEOF.m's labels, default and gene targets +# --------------------------------------------------------------------------- # + +def test_default_n_steps_is_16(tmp_path): + result = ec_fseof(_build_fseof_model(_adapter(tmp_path)), "EX_PROD") + assert len(result.enforced) == 16 + + +def test_gene_targets_follow_ecfseof(tmp_path): + result = ec_fseof(_build_fseof_model(_adapter(tmp_path)), "EX_PROD", n_steps=8) + genes = result.gene_targets.set_index("gene") + assert list(result.gene_targets.columns) == ["gene", "name", "slope", "action", "essentiality", "reactions"] + assert genes.loc["g_PROD", "action"] == "OE" + assert genes.loc["g_BIO", "action"] in ("KD", "KO") + assert genes.loc["g_PROD", "reactions"] == "R_PROD" + slope = result.targets.set_index("reaction").loc["R_PROD", "slope"] + assert genes.loc["g_PROD", "slope"] == pytest.approx(abs(slope)) + + +def test_gene_actions_are_reconciled_as_in_ecfseof(tmp_path): + """Several actions: OE wins; otherwise KO for a gene that is not essential + and KD for an essential one. The mean absolute slope orders the genes.""" + import importlib + + module = importlib.import_module("geckopy.utilities.ec_fseof") + model = _build_fseof_model(_adapter(tmp_path)) + model.reactions.get_by_id("TR_A").gene_reaction_rule = "g_X" + targets = pd.DataFrame({ + "reaction": ["R_PROD", "R_BIO", "TR_A", "R_BIO", "TR_A", "R_PROD"], + "genes": [["g_PROD"], ["g_BIO"], ["g_BIO"], ["g_X"], ["g_X"], ["g_PROD"]], + "target_type": ["OE", "KO", "KD", "KO", "KD", "KD"], + "slope": [4.0, -1.0, -3.0, -1.0, -1.0, -2.0], + }) + genes = module._gene_targets(model, targets, "biomass", "EX_PROD", 50.0) + assert list(genes["gene"]) == ["g_PROD", "g_BIO", "g_X"] + assert list(genes["action"]) == ["OE", "KD", "KO"] + assert list(genes["essentiality"]) == ["essential", "essential", ""] + assert list(genes["slope"]) == [3.0, 2.0, 1.0] From e475e3eee6476cb8867d0192e5341f6a0593f5bc Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:50:16 +0100 Subject: [PATCH 20/42] style: remove the unused pytest import of a parity test --- tests/test_parity_fixes_g1a.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_parity_fixes_g1a.py b/tests/test_parity_fixes_g1a.py index 9f9384e..08a5d8e 100644 --- a/tests/test_parity_fixes_g1a.py +++ b/tests/test_parity_fixes_g1a.py @@ -1,7 +1,6 @@ """Behaviour that follows GECKO: the detail text of a kcat source file, and the defaults of fill_kcats_from_isozymes.""" import numpy as np -import pytest from geckopy.ec_model.pipeline import fill_kcats_from_isozymes, set_kcat_for_reactions from geckopy.gather_kcats import read_kcat_source, save_kcat_source From a1156c2732dad5c11b3de9e93a0671011b8db2eb Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:51:26 +0100 Subject: [PATCH 21/42] fix: raise in get_conc_control_coeffs on a model without optimal solution --- CHANGELOG.md | 1 + .../limit_proteins/get_conc_control_coeffs.py | 28 +++++++++++++++---- tests/test_get_conc_control_coeffs.py | 13 ++++----- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87ea2e1..d36dbb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. * `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). diff --git a/src/geckopy/limit_proteins/get_conc_control_coeffs.py b/src/geckopy/limit_proteins/get_conc_control_coeffs.py index ab05192..b8eb9e6 100644 --- a/src/geckopy/limit_proteins/get_conc_control_coeffs.py +++ b/src/geckopy/limit_proteins/get_conc_control_coeffs.py @@ -1,7 +1,7 @@ """Compute enzyme-concentration control coefficients via LP sensitivity. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/getConcControlCoeffs.m. +src/limit_proteins/getConcControlCoeffs.m. """ from __future__ import annotations @@ -86,7 +86,7 @@ def get_conc_control_coeffs( where it does. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/getConcControlCoeffs.m. + src/limit_proteins/getConcControlCoeffs.m. Parameters ---------- @@ -114,6 +114,13 @@ def get_conc_control_coeffs( control_coeffs Float array of length ``len(proteins)``; 0.0 where the protein was either skipped or showed no sensitivity. + + Raises + ------ + ValueError + If ``method`` is unknown, or if the solve of the model as given is + not optimal (for example infeasible), as GECKO's + ``getConcControlCoeffs`` raises ``getConcControlCoeffs:infeasible``. """ if method not in ("finite_difference", "shadow_price"): raise ValueError( @@ -147,8 +154,7 @@ def _shadow_price_coeffs( coeffs = np.zeros(n, dtype=float) sol = optimize_model(model) - if not _solution_is_optimal(sol): - return enz_mask, coeffs + _require_optimal(sol) initial_growth = sol.objective_value fluxes = sol.fluxes @@ -265,8 +271,7 @@ def _finite_difference_coeffs( coeffs = np.zeros(n, dtype=float) initial_sol = optimize_model(model) - if not _solution_is_optimal(initial_sol): - return enz_mask, coeffs + _require_optimal(initial_sol) initial_growth = initial_sol.objective_value initial_fluxes = initial_sol.fluxes @@ -297,6 +302,17 @@ def _finite_difference_coeffs( return enz_mask, coeffs +def _require_optimal(sol) -> None: + """Raise when the solve of the model as given is not optimal, as GECKO's + ``getConcControlCoeffs`` raises ``getConcControlCoeffs:infeasible``.""" + if not _solution_is_optimal(sol): + status = getattr(sol, "status", None) + raise ValueError( + f"get_conc_control_coeffs: the model has no optimal solution (status {status}); " + "there are no control coefficients to compute." + ) + + def _solution_is_optimal(sol) -> bool: """True iff ``sol`` is a genuine LP optimum with a usable objective. diff --git a/tests/test_get_conc_control_coeffs.py b/tests/test_get_conc_control_coeffs.py index c555b48..14dc424 100644 --- a/tests/test_get_conc_control_coeffs.py +++ b/tests/test_get_conc_control_coeffs.py @@ -374,17 +374,16 @@ def test_equiv_subset_proteins(): np.testing.assert_allclose(fc, sc, rtol=1e-6, atol=1e-12) -def test_equiv_infeasible_model_both_zero(): - """An infeasible LP: both methods return all-False mask and zero coeffs.""" +def test_equiv_infeasible_model_both_raise(): + """An infeasible LP: both methods raise, as getConcControlCoeffs:infeasible.""" model = _build_toy_ec_model(enzyme_ub=5.0) # Force infeasibility: require growth > what the model can deliver. sk = model.reactions.get_by_id("SK_B") sk.lower_bound = 1000.0 # impossible - fm, fc = _fd_helper(model) - sm, sc = _shadow_helper(model) - np.testing.assert_array_equal(fm, sm) - np.testing.assert_allclose(fc, sc, rtol=1e-6, atol=1e-12) - assert not fm.any() and not sm.any() + with pytest.raises(ValueError, match="no optimal solution"): + _fd_helper(model) + with pytest.raises(ValueError, match="no optimal solution"): + _shadow_helper(model) @pytest.mark.parametrize( From 60970a5d514f1da4e0ec5c1cbbf6241b04843581 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:52:07 +0100 Subject: [PATCH 22/42] fix: include unreviewed uniprot entries by default as the adapter does --- CHANGELOG.md | 1 + src/geckopy/databases/uniprot_download.py | 6 +++--- src/geckopy/databases/uniprot_loader.py | 7 ++++--- tests/test_uniprot_download.py | 11 +++++------ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d36dbb0..14c97df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. * `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. diff --git a/src/geckopy/databases/uniprot_download.py b/src/geckopy/databases/uniprot_download.py index aea08db..de09c0d 100644 --- a/src/geckopy/databases/uniprot_download.py +++ b/src/geckopy/databases/uniprot_download.py @@ -34,7 +34,7 @@ def download_uniprot( *, id_type: str = "taxonomy_id", gene_id_field: str = "gene_oln", - reviewed: bool = True, + reviewed: bool = False, session: requests.Session | None = None, ) -> Path: """Download UniProt protein data for an organism and write a TSV. @@ -56,8 +56,8 @@ def download_uniprot( matching key). Default ``"gene_oln"`` (Ordered Locus Names) works for most prokaryotes and budding yeast. reviewed - If True (default), restrict the query to reviewed - (Swiss-Prot) entries. + If True, restrict the query to reviewed (Swiss-Prot) entries. + Default False, as the adapter default ``uniprot.reviewed``. session Optional ``requests.Session`` for connection reuse and test injection. A new transient session is used otherwise. diff --git a/src/geckopy/databases/uniprot_loader.py b/src/geckopy/databases/uniprot_loader.py index eeaa8ef..b5bef2e 100644 --- a/src/geckopy/databases/uniprot_loader.py +++ b/src/geckopy/databases/uniprot_loader.py @@ -59,7 +59,7 @@ def load_uniprot_tsv( uniprot_id: str | int | None = None, id_type: str = "taxonomy_id", gene_id_field: str = "gene_oln", - reviewed: bool = True, + reviewed: bool = False, auto_download: bool = True, ) -> UniprotDB: """Parse a uniprot.tsv file into a UniprotDB. @@ -91,8 +91,9 @@ def load_uniprot_tsv( UniProt field that becomes the Gene Names column. Default ``"gene_oln"``. reviewed - If True (default), restrict the auto-download query to - reviewed (Swiss-Prot) entries. + If True, restrict the auto-download query to reviewed + (Swiss-Prot) entries. Default False, as the adapter default + ``uniprot.reviewed``. auto_download If True (default) and ``path`` is missing, trigger ``download_uniprot`` before parsing. Set to False to keep diff --git a/tests/test_uniprot_download.py b/tests/test_uniprot_download.py index ef7d7da..f1a18d2 100644 --- a/tests/test_uniprot_download.py +++ b/tests/test_uniprot_download.py @@ -41,19 +41,18 @@ def test_basic_download_writes_response_verbatim(tmp_path): assert out.read_text(encoding="utf-8") == payload -def test_query_includes_reviewed_filter_by_default(tmp_path): +def test_query_includes_reviewed_filter_when_true(tmp_path): sess = _make_session("Entry\tGene\tEC\tMass\tSequence\nP1\tG1\t\t10\tA\n") - download_uniprot(559292, tmp_path / "u.tsv", session=sess) + download_uniprot(559292, tmp_path / "u.tsv", reviewed=True, session=sess) params = sess._captured["params"] assert "reviewed:true" in params["query"] assert "taxonomy_id:559292" in params["query"] -def test_query_drops_reviewed_when_false(tmp_path): +def test_query_has_no_reviewed_filter_by_default(tmp_path): + """The default follows the adapter default uniprot.reviewed = false.""" sess = _make_session("Entry\tGene\tEC\tMass\tSequence\nP1\tG1\t\t10\tA\n") - download_uniprot( - 559292, tmp_path / "u.tsv", reviewed=False, session=sess, - ) + download_uniprot(559292, tmp_path / "u.tsv", session=sess) assert "reviewed" not in sess._captured["params"]["query"] From ca90d5a8dc252b3056c70dbb0fb8753dcb4fa2bb Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:53:00 +0100 Subject: [PATCH 23/42] doc: name the current gecko paths and the matlab greedy relaxation --- docs/migrating_from_gecko_matlab.md | 19 ++++++++++--------- src/geckopy/adapter/adapter.py | 2 +- src/geckopy/adapter/params.py | 2 +- src/geckopy/adapter/resolve.py | 2 +- src/geckopy/adapter/template.py | 2 +- src/geckopy/cli.py | 4 ++-- src/geckopy/databases/brenda_loader.py | 4 ++-- .../databases/complex_portal_loader.py | 2 +- src/geckopy/databases/dlkcat_ignore_lists.py | 4 ++-- src/geckopy/databases/flux_data.py | 2 +- src/geckopy/databases/kegg_download.py | 2 +- src/geckopy/databases/mw.py | 4 ++-- src/geckopy/databases/pax_db_loader.py | 4 ++-- src/geckopy/databases/phyl_dist.py | 4 ++-- src/geckopy/databases/prot_data_loader.py | 4 ++-- src/geckopy/databases/pubchem.py | 4 ++-- src/geckopy/databases/uniprot_download.py | 2 +- src/geckopy/databases/uniprot_loader.py | 2 +- src/geckopy/ec_model/make_ec_model.py | 2 +- .../ec_model/pipeline/apply_custom_kcats.py | 2 +- src/geckopy/ec_model/pipeline/apply_kcat.py | 2 +- src/geckopy/ec_model/pipeline/fill_kcats.py | 2 +- src/geckopy/ec_model/pipeline/populate_ec.py | 2 +- src/geckopy/ec_model/pipeline/preprocess.py | 2 +- src/geckopy/ec_model/pipeline/query.py | 2 +- src/geckopy/ec_model/pipeline/set_kcat.py | 2 +- src/geckopy/gather_kcats/__init__.py | 2 +- .../fuzzy_kcat_matching/__init__.py | 4 ++-- .../fuzzy_kcat_matching/_brenda_query.py | 2 +- .../fuzzy_kcat_matching/_escalation.py | 2 +- .../fuzzy_kcat_matching/_organism.py | 2 +- src/geckopy/gather_kcats/get_standard_kcat.py | 4 ++-- .../merge_dlkcat_and_fuzzy_kcats.py | 2 +- src/geckopy/gather_kcats/merge_kcats.py | 2 +- .../gather_kcats/read_dlkcat_output.py | 4 ++-- .../gather_kcats/remove_standard_kcat.py | 4 ++-- src/geckopy/gather_kcats/run_dlkcat.py | 4 ++-- src/geckopy/gather_kcats/select_kcat_value.py | 4 ++-- .../gather_kcats/write_dlkcat_input.py | 4 ++-- src/geckopy/get_enzyme_data/__init__.py | 2 +- src/geckopy/get_enzyme_data/copy_ec_to_gem.py | 4 ++-- src/geckopy/get_enzyme_data/ec_from_gem.py | 4 ++-- src/geckopy/get_enzyme_data/find_ec_in_db.py | 4 ++-- src/geckopy/kcat_tuning/find_max_value.py | 6 ++---- src/geckopy/limit_proteins/__init__.py | 2 +- .../limit_proteins/constrain_enz_concs.py | 4 ++-- .../limit_proteins/constrain_flux_data.py | 4 ++-- src/geckopy/limit_proteins/fill_enz_concs.py | 4 ++-- .../limit_proteins/flexibilize_enz_concs.py | 4 ++-- .../limit_proteins/get_isozyme_coverage.py | 4 ++-- .../zero_unmeasured_isozymes.py | 4 ++-- src/geckopy/utilities/__init__.py | 2 +- src/geckopy/utilities/add_new_rxns_to_ec.py | 4 ++-- src/geckopy/utilities/ec_fva.py | 4 ++-- src/geckopy/utilities/enzyme_usage.py | 4 ++-- src/geckopy/utilities/get_subset_ec_model.py | 4 ++-- .../utilities/load_conventional_gem.py | 2 +- src/geckopy/utilities/load_ec_model.py | 2 +- src/geckopy/utilities/map_rxns_to_conv.py | 4 ++-- src/geckopy/utilities/report_enzyme_usage.py | 4 ++-- src/geckopy/utilities/save_ec_model.py | 2 +- 61 files changed, 101 insertions(+), 102 deletions(-) diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index d83a515..f13861a 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -217,7 +217,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The port choice, not something flowing back to MATLAB). - **(new)** — geckopy entry-point with no direct MATLAB counterpart. -### 5.1 Build / edit the ecModel (`src/geckomat/change_model`) +### 5.1 Build / edit the ecModel (`src/change_model`) | MATLAB | geckopy | Notes | | ---------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | @@ -232,7 +232,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `setKcatForReactions` | `set_kcat_for_reactions` | Matches IDs without regard to letter case, ignores an unknown ID and does not apply the constraints unless `apply=True`, as `setKcatForReactions`; a light-model row is addressed by its `###_` ID. | | `setProtPoolSize` | `set_prot_pool_size` | **`[3→4]`** Sets the **upper** bound of `prot_pool_exchange` (forward direction). | -### 5.2 Gather kcats (`src/geckomat/gather_kcats`) +### 5.2 Gather kcats (`src/gather_kcats`) | MATLAB | geckopy | Notes | | ----------------------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -266,7 +266,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `assignKcats` | `assign_kcats` | `collect_kcats`, then sets `ec.kcat` and `ec.source` and applies the kcat constraints; replaces `merge_kcats` → `apply_kcat_list` → `apply_custom_kcats` → `fill_kcats_from_isozymes` → `assign_standard_kcat` → `apply_kcat_constraints`. In place. | | `updateKcatSources` | `update_kcat_sources` | Gathers the source files the configuration needs that are missing, or outdated against `[kcat_sources.]`; `dry_run` only reports. | -### 5.3 Get enzyme data (`src/geckomat/get_enzyme_data`) +### 5.3 Get enzyme data (`src/get_enzyme_data`) | MATLAB | geckopy | Notes | | --------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | @@ -290,7 +290,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `findMaxValue` | `find_max_value` | **`[3→4]`** Wildcard EC branch matches real codes. | | `cmaesKcatTuning` + helpers | `kcat_tuning.evotune.cmaes_kcat_tuning` + helpers | Fits kcats to experimental flux/growth data with CMA-ES; see [evotune_kcat_tuning.md](evotune_kcat_tuning.md). | -### 5.5 Limit proteins (`src/geckomat/limit_proteins`) +### 5.5 Limit proteins (`src/limit_proteins`) | MATLAB | geckopy | Notes | | --------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -303,9 +303,9 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `getIsozymeCoverage` | `get_isozyme_coverage` | **`[Py]`** Returns the coverage `DataFrame` (columns `rxn`, `n_entries`, `n_measured`, `n_unmeasured`, `measured_enzymes`, `unmeasured_enzymes`, `shared_enzymes`) and the list of zeroable enzymes. Raises `NotImplementedError` on gecko-light. | | `updateProtPool` | *(not ported)* | Obsolete since GECKO 3.2.0. Use `set_prot_pool_size` instead. | | `zeroUnmeasuredIsozymes` | `zero_unmeasured_isozymes` | **`[Py]`** Lowers the usage upper bounds in place and returns the changed enzymes as a `DataFrame` (`uniprot`, `new_upper_bound`). Method `'detectionLimit'` is `"detection_limit"`. | -| — | `relax_proteomics_greedy` *(new)* | **`[Py]`** Alternative to `flexibilize_enz_concs`: relaxes a fixed top-*k* of measured concentrations in a greedy loop until a target growth is reached. Returns a `GreedyRelaxResult`. | +| `relaxProteomicsGreedy` | `relax_proteomics_greedy` | Alternative to `flexibilize_enz_concs`: until growth reaches the target, sets the usage upper bound of the constrained enzyme with the largest absolute shadow price of its `prot_` metabolite to the default bound, one enzyme per iteration. **`[Py]`** Returns a `GreedyRelaxResult` (`converged=False` when no candidate is left) and changes the model in place; raises `RuntimeError` when `max_iterations` relaxations do not reach the target. | -### 5.6 Utilities (`src/geckomat/utilities`) +### 5.6 Utilities (`src/utilities`) | MATLAB | geckopy | Notes | | --------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -328,7 +328,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | — | `pfba_enzymes` *(new)* | **`[Py]`** pFBA variant that minimises total `usage_prot_*` flux instead of all fluxes. Raises `NotImplementedError` on gecko-light. | | — | `get_enzyme_bottlenecks` *(new)* | **`[Py]`** Per-enzyme bottleneck ranker; returns a DataFrame sorted by the re-solved change of the objective when the bound of one enzyme at its bound is raised by `fold_change`. The shadow price is a reference column. | -### 5.7 Model adapter (`src/geckomat/model_adapter`) +### 5.7 Model adapter (`src/model_adapter`) | MATLAB | geckopy | Notes | | ----------------------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -454,13 +454,14 @@ for the MATLAB-side items the port surfaced. `prot_pool_exchange`). - **Refreshed BRENDA TSV schema** (geckopy adds the max+median variant; MATLAB includes only max so far). +- **Greedy relaxation of proteomics constraints** + (`relaxProteomicsGreedy` / `relax_proteomics_greedy`), an alternative + to `flexibilize_enz_concs`. ### geckopy-only (no MATLAB counterpart) - `pfba_enzymes` — enzyme-pool-minimising pFBA. - `get_enzyme_bottlenecks`: bottleneck ranking by the re-solved change of the objective. -- `relax_proteomics_greedy` — greedy alternative to - `flexibilize_enz_concs`. - `merge_kcats` — generalised n-source kcat merge (replaces the DLKcat-only MATLAB helper). - `geckopy brenda-refresh` CLI — rebuilds the BRENDA TSV from a diff --git a/src/geckopy/adapter/adapter.py b/src/geckopy/adapter/adapter.py index 25c3d15..e5590d4 100644 --- a/src/geckopy/adapter/adapter.py +++ b/src/geckopy/adapter/adapter.py @@ -15,7 +15,7 @@ - ``data/`` — cached databases (UniProt, BRENDA, ComplexPortal, proteomics, etc.). -Ported from GECKO MATLAB: src/geckomat/model_adapter/ModelAdapter.m. +Ported from GECKO MATLAB: src/model_adapter/ModelAdapter.m. Then: diff --git a/src/geckopy/adapter/params.py b/src/geckopy/adapter/params.py index 8778c70..6ff1d9e 100644 --- a/src/geckopy/adapter/params.py +++ b/src/geckopy/adapter/params.py @@ -5,7 +5,7 @@ MATLAB's adapter template. ``EvotuneParams`` holds the evotune settings; ``OkpParams`` is a geckopy-only extension with no MATLAB equivalent. -Ported from GECKO MATLAB: src/geckomat/model_adapter/adapterTemplate.m. +Ported from GECKO MATLAB: src/deprecated/adapterTemplate.m. """ from pathlib import Path from typing import Any, Literal, Optional diff --git a/src/geckopy/adapter/resolve.py b/src/geckopy/adapter/resolve.py index eac9b4a..2e93bb8 100644 --- a/src/geckopy/adapter/resolve.py +++ b/src/geckopy/adapter/resolve.py @@ -12,7 +12,7 @@ ``model.adapter``-or-raise" pattern that 30+ functions duplicated inline, with inconsistent error messages. -Ported from GECKO MATLAB: src/geckomat/model_adapter/ModelAdapterManager.m. +Ported from GECKO MATLAB: src/model_adapter/ModelAdapterManager.m. """ from __future__ import annotations diff --git a/src/geckopy/adapter/template.py b/src/geckopy/adapter/template.py index 43ab882..2cf69a8 100644 --- a/src/geckopy/adapter/template.py +++ b/src/geckopy/adapter/template.py @@ -7,7 +7,7 @@ showing its default value (for optional fields). Field descriptions are emitted as TOML comments above each field. -Ported from GECKO MATLAB: src/geckomat/model_adapter/adapterTemplate.m. +Ported from GECKO MATLAB: src/deprecated/adapterTemplate.m. """ from __future__ import annotations diff --git a/src/geckopy/cli.py b/src/geckopy/cli.py index 93b26af..24c73ec 100644 --- a/src/geckopy/cli.py +++ b/src/geckopy/cli.py @@ -1,12 +1,12 @@ """Command-line interface for geckopy. The `init` subcommand ports GECKO MATLAB's project-scaffolding logic -(`src/geckomat/utilities/startGECKOproject.m`): create the project +(`src/deprecated/startGECKOproject.m`): create the project folder tree and write out a template adapter. The `brenda-refresh`, `uniprot-download`, and `kegg-download` subcommands are new tooling with no MATLAB equivalent. -Ported from GECKO MATLAB: src/geckomat/utilities/startGECKOproject.m +Ported from GECKO MATLAB: src/deprecated/startGECKOproject.m (the `init` subcommand only). """ from __future__ import annotations diff --git a/src/geckopy/databases/brenda_loader.py b/src/geckopy/databases/brenda_loader.py index 1e49a77..82aeefa 100644 --- a/src/geckopy/databases/brenda_loader.py +++ b/src/geckopy/databases/brenda_loader.py @@ -1,7 +1,7 @@ """Parse BRENDA TSV dumps into kcat and SA-derived-kcat tables. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/loadBRENDAdata.m. +src/download_databases/loadBRENDAdata.m. Three tab-delimited files are read from the BRENDA folder: @@ -99,7 +99,7 @@ def load_brenda_data( """Load BRENDA kcat, SA, and MW dumps from a folder. Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/loadBRENDAdata.m. + src/download_databases/loadBRENDAdata.m. Reads ``kcat.tsv``, ``sa.tsv``, ``mw.tsv`` from ``folder``, splits the kcat and SA wide tables into per-aggregation views, applies the diff --git a/src/geckopy/databases/complex_portal_loader.py b/src/geckopy/databases/complex_portal_loader.py index 6135fc3..dc08da1 100644 --- a/src/geckopy/databases/complex_portal_loader.py +++ b/src/geckopy/databases/complex_portal_loader.py @@ -1,7 +1,7 @@ """Parse ComplexPortal.json into a list of ComplexPortalEntry. Ported from GECKO MATLAB: -src/geckomat/change_model/applyComplexData.m (the JSON-loading branch). +src/change_model/applyComplexData.m (the JSON-loading branch). The download branch is deferred to a future module. """ from __future__ import annotations diff --git a/src/geckopy/databases/dlkcat_ignore_lists.py b/src/geckopy/databases/dlkcat_ignore_lists.py index b4d4bef..7636bfe 100644 --- a/src/geckopy/databases/dlkcat_ignore_lists.py +++ b/src/geckopy/databases/dlkcat_ignore_lists.py @@ -2,7 +2,7 @@ Ported from GECKO MATLAB: the inline `DLKcatIgnoreMets.tsv` and `DLKcatCurrencyMets.tsv` parsing -in src/geckomat/gather_kcats/writeDLKcatInput.m. +in src/gather_kcats/writeDLKcatInput.m. """ from __future__ import annotations @@ -69,7 +69,7 @@ def load_dlkcat_ignore_lists( Ported from GECKO MATLAB: the inline file-loading parts of - src/geckomat/gather_kcats/writeDLKcatInput.m. + src/gather_kcats/writeDLKcatInput.m. Parameters ---------- diff --git a/src/geckopy/databases/flux_data.py b/src/geckopy/databases/flux_data.py index 4971512..c8a9dce 100644 --- a/src/geckopy/databases/flux_data.py +++ b/src/geckopy/databases/flux_data.py @@ -88,7 +88,7 @@ def load_flux_data(path: str | Path) -> FluxData: returned ``FluxData`` as ``evotune_rmse_weight`` and ``source``. Ported from GECKO MATLAB: - src/geckomat/utilities/loadFluxData.m. + src/io/loadFluxData.m. Parameters ---------- diff --git a/src/geckopy/databases/kegg_download.py b/src/geckopy/databases/kegg_download.py index 91233c4..1bc4440 100644 --- a/src/geckopy/databases/kegg_download.py +++ b/src/geckopy/databases/kegg_download.py @@ -1,7 +1,7 @@ """Download organism-specific protein information from KEGG REST. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/loadDatabases.m +src/download_databases/loadDatabases.m (the KEGG-download branch, extracted into its own module). Two REST endpoints are used: diff --git a/src/geckopy/databases/mw.py b/src/geckopy/databases/mw.py index 6028f24..55035a2 100644 --- a/src/geckopy/databases/mw.py +++ b/src/geckopy/databases/mw.py @@ -1,7 +1,7 @@ """Compute molecular weight of a protein from its amino acid sequence. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/calculateMW.m. +src/get_enzyme_data/calculateMW.m. """ from __future__ import annotations @@ -69,7 +69,7 @@ def calculate_mw(sequence: str) -> float: """Compute the molecular weight of a protein from its sequence. Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/calculateMW.m. + src/get_enzyme_data/calculateMW.m. Each residue contributes its average mass; one water mass is added once (representing the H + OH at the peptide termini, equivalent to diff --git a/src/geckopy/databases/pax_db_loader.py b/src/geckopy/databases/pax_db_loader.py index 69dabbe..2c8f976 100644 --- a/src/geckopy/databases/pax_db_loader.py +++ b/src/geckopy/databases/pax_db_loader.py @@ -2,7 +2,7 @@ Ported from GECKO MATLAB: the inline paxDB-parsing block of -src/geckomat/limit_proteins/calculateFfactor.m. +src/limit_proteins/calculateFfactor.m. """ from __future__ import annotations @@ -64,7 +64,7 @@ def load_pax_db( Ported from GECKO MATLAB: the inline paxDB-parsing block of - src/geckomat/limit_proteins/calculateFfactor.m. + src/limit_proteins/calculateFfactor.m. Parameters ---------- diff --git a/src/geckopy/databases/phyl_dist.py b/src/geckopy/databases/phyl_dist.py index d8b486d..a898aef 100644 --- a/src/geckopy/databases/phyl_dist.py +++ b/src/geckopy/databases/phyl_dist.py @@ -4,7 +4,7 @@ when an exact match is not available in BRENDA. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/fuzzyKcatMatching.m (the inline `KEGG_struct` helper). +src/gather_kcats/fuzzyKcatMatching.m (the inline `KEGG_struct` helper). """ from __future__ import annotations @@ -77,7 +77,7 @@ def load_phyl_dist(path: str | Path) -> PhylDist: -> all matching indices. Ported from GECKO MATLAB (inline ``KEGG_struct`` helper in - src/geckomat/gather_kcats/fuzzyKcatMatching.m). + src/gather_kcats/fuzzyKcatMatching.m). Parameters ---------- diff --git a/src/geckopy/databases/prot_data_loader.py b/src/geckopy/databases/prot_data_loader.py index c89b79c..9fd64ae 100644 --- a/src/geckopy/databases/prot_data_loader.py +++ b/src/geckopy/databases/prot_data_loader.py @@ -1,7 +1,7 @@ """Parse a proteomics.tsv into a ProtData with optional filtering. Ported from GECKO MATLAB: -src/geckomat/utilities/loadProtData.m. +src/io/loadProtData.m. """ from __future__ import annotations @@ -70,7 +70,7 @@ def load_prot_data( the protein pool (see ``fill_enz_concs`` and ``constrain_enz_concs``). Ported from GECKO MATLAB: - src/geckomat/utilities/loadProtData.m. + src/io/loadProtData.m. Parameters ---------- diff --git a/src/geckopy/databases/pubchem.py b/src/geckopy/databases/pubchem.py index b2fe40e..32688f0 100644 --- a/src/geckopy/databases/pubchem.py +++ b/src/geckopy/databases/pubchem.py @@ -1,7 +1,7 @@ """SMILES lookup via PubChem with a local TSV cache. Ported from GECKO MATLAB: -src/geckomat/change_model/findMetSmiles.m. +src/change_model/findMetSmiles.m. The cache file is a two-column TSV with no header: ``namesmiles``. Each successful lookup is appended immediately so interrupted runs @@ -68,7 +68,7 @@ def find_met_smiles( """Populate ``metabolite.annotation['smiles']`` from PubChem. Ported from GECKO MATLAB: - src/geckomat/change_model/findMetSmiles.m. + src/change_model/findMetSmiles.m. For every metabolite in the model: diff --git a/src/geckopy/databases/uniprot_download.py b/src/geckopy/databases/uniprot_download.py index de09c0d..7b5a0da 100644 --- a/src/geckopy/databases/uniprot_download.py +++ b/src/geckopy/databases/uniprot_download.py @@ -1,7 +1,7 @@ """Download a per-organism UniProt TSV from the UniProt REST API. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/loadDatabases.m +src/download_databases/loadDatabases.m (the UniProt-download branch, extracted into its own module). Hits ``https://rest.uniprot.org/uniprotkb/stream`` with a query diff --git a/src/geckopy/databases/uniprot_loader.py b/src/geckopy/databases/uniprot_loader.py index b5bef2e..63a51a9 100644 --- a/src/geckopy/databases/uniprot_loader.py +++ b/src/geckopy/databases/uniprot_loader.py @@ -1,6 +1,6 @@ """Parse uniprot.tsv into a UniprotDB struct. -Ported from GECKO MATLAB: src/geckomat/get_enzyme_data/loadDatabases.m +Ported from GECKO MATLAB: src/download_databases/loadDatabases.m (the UniProt-parsing branch; the download branch is deferred to a separate future module). diff --git a/src/geckopy/ec_model/make_ec_model.py b/src/geckopy/ec_model/make_ec_model.py index b28eced..18faeae 100644 --- a/src/geckopy/ec_model/make_ec_model.py +++ b/src/geckopy/ec_model/make_ec_model.py @@ -18,7 +18,7 @@ The MATLAB tutorials in ``tutorials/full_ecModel/protocol.py`` show the full flow. -Ported from GECKO MATLAB: src/geckomat/change_model/makeEcModel.m. +Ported from GECKO MATLAB: src/change_model/makeEcModel.m. """ from __future__ import annotations diff --git a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py b/src/geckopy/ec_model/pipeline/apply_custom_kcats.py index 736472a..c5a343f 100644 --- a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py +++ b/src/geckopy/ec_model/pipeline/apply_custom_kcats.py @@ -1,6 +1,6 @@ """Apply user-curated custom kcats from a TSV file. -Ported from GECKO MATLAB: src/geckomat/change_model/applyCustomKcats.m. +Ported from GECKO MATLAB: src/change_model/applyCustomKcats.m. """ from __future__ import annotations diff --git a/src/geckopy/ec_model/pipeline/apply_kcat.py b/src/geckopy/ec_model/pipeline/apply_kcat.py index 264ea03..7dc3208 100644 --- a/src/geckopy/ec_model/pipeline/apply_kcat.py +++ b/src/geckopy/ec_model/pipeline/apply_kcat.py @@ -1,6 +1,6 @@ """Apply kcat-derived stoichiometric constraints to an ecModel. -Ported from GECKO MATLAB: src/geckomat/change_model/applyKcatConstraints.m. +Ported from GECKO MATLAB: src/change_model/applyKcatConstraints.m. Supports both the full and gecko-light formulations. The two share the clear-then-write idempotency contract but write to different metabolites: full models write per-enzyme ``prot_`` coefficients on each ec.rxn; diff --git a/src/geckopy/ec_model/pipeline/fill_kcats.py b/src/geckopy/ec_model/pipeline/fill_kcats.py index 8af9f1a..8e2fdcd 100644 --- a/src/geckopy/ec_model/pipeline/fill_kcats.py +++ b/src/geckopy/ec_model/pipeline/fill_kcats.py @@ -1,7 +1,7 @@ """Fill missing kcats by averaging across isozymes. Ported from GECKO MATLAB: -src/geckomat/change_model/getKcatAcrossIsozymes.m. +src/change_model/getKcatAcrossIsozymes.m. """ from __future__ import annotations diff --git a/src/geckopy/ec_model/pipeline/populate_ec.py b/src/geckopy/ec_model/pipeline/populate_ec.py index 61939d5..220d520 100644 --- a/src/geckopy/ec_model/pipeline/populate_ec.py +++ b/src/geckopy/ec_model/pipeline/populate_ec.py @@ -19,7 +19,7 @@ All three functions mutate the ec substructure in place. None of them talk to the network. -Ported from GECKO MATLAB: src/geckomat/change_model/makeEcModel.m. +Ported from GECKO MATLAB: src/change_model/makeEcModel.m. """ from __future__ import annotations diff --git a/src/geckopy/ec_model/pipeline/preprocess.py b/src/geckopy/ec_model/pipeline/preprocess.py index f9e4f30..031e0c1 100644 --- a/src/geckopy/ec_model/pipeline/preprocess.py +++ b/src/geckopy/ec_model/pipeline/preprocess.py @@ -4,7 +4,7 @@ stages do not touch the `ec` substructure; they only reshape the underlying model so subsequent stages operate on a clean irreversible form. -Ported from GECKO MATLAB: src/geckomat/change_model/makeEcModel.m. +Ported from GECKO MATLAB: src/change_model/makeEcModel.m. """ from __future__ import annotations from typing import TYPE_CHECKING diff --git a/src/geckopy/ec_model/pipeline/query.py b/src/geckopy/ec_model/pipeline/query.py index e1a4b7c..c88f531 100644 --- a/src/geckopy/ec_model/pipeline/query.py +++ b/src/geckopy/ec_model/pipeline/query.py @@ -3,7 +3,7 @@ These functions do not mutate the model; they pull subsets of the ec data for inspection, reporting, or downstream pipeline use. -Ported from GECKO MATLAB: src/geckomat/change_model/getReactionsFromEnzyme.m. +Ported from GECKO MATLAB: src/change_model/getReactionsFromEnzyme.m. """ from __future__ import annotations diff --git a/src/geckopy/ec_model/pipeline/set_kcat.py b/src/geckopy/ec_model/pipeline/set_kcat.py index 7f3a4f5..c8574df 100644 --- a/src/geckopy/ec_model/pipeline/set_kcat.py +++ b/src/geckopy/ec_model/pipeline/set_kcat.py @@ -1,6 +1,6 @@ """Set kcat values for individual reactions, with optional auto-apply. -Ported from GECKO MATLAB: src/geckomat/change_model/setKcatForReactions.m. +Ported from GECKO MATLAB: src/change_model/setKcatForReactions.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/__init__.py b/src/geckopy/gather_kcats/__init__.py index f5688ab..a324af0 100644 --- a/src/geckopy/gather_kcats/__init__.py +++ b/src/geckopy/gather_kcats/__init__.py @@ -1,6 +1,6 @@ """Gather kcat values for ec model reactions from various sources. -Mirrors GECKO MATLAB's `src/geckomat/gather_kcats/` directory. +Mirrors GECKO MATLAB's `src/gather_kcats/` directory. """ from .fuzzy_kcat_matching import fuzzy_kcat_matching from .kcat_assignment import assign_kcats, collect_kcats, update_kcat_sources diff --git a/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py b/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py index 6c9a588..25193b7 100644 --- a/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py +++ b/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py @@ -1,7 +1,7 @@ """Fuzzy-match each ec reaction's EC + substrates against BRENDA kcat data. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/fuzzyKcatMatching.m. +src/gather_kcats/fuzzyKcatMatching.m. This subpackage is split into four files by concern: @@ -88,7 +88,7 @@ def fuzzy_kcat_matching( relaxation of EC, substrate, and organism specificity. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/fuzzyKcatMatching.m. + src/gather_kcats/fuzzyKcatMatching.m. For each selected reaction, the EC string in ``model.ec.eccodes[i]`` is split on ``;`` into one or more EC diff --git a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_brenda_query.py b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_brenda_query.py index 276a7c5..102b89f 100644 --- a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_brenda_query.py +++ b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_brenda_query.py @@ -10,7 +10,7 @@ MATLAB-compat order. See the orchestrator's docstring for the full background. -Ported from GECKO MATLAB: src/geckomat/gather_kcats/fuzzyKcatMatching.m. +Ported from GECKO MATLAB: src/gather_kcats/fuzzyKcatMatching.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_escalation.py b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_escalation.py index e8955d0..b3f02e0 100644 --- a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_escalation.py +++ b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_escalation.py @@ -5,7 +5,7 @@ again. These helpers do just that string manipulation -- no BRENDA, no model. -Ported from GECKO MATLAB: src/geckomat/gather_kcats/fuzzyKcatMatching.m. +Ported from GECKO MATLAB: src/gather_kcats/fuzzyKcatMatching.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_organism.py b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_organism.py index fed08cb..ed3a6d5 100644 --- a/src/geckopy/gather_kcats/fuzzy_kcat_matching/_organism.py +++ b/src/geckopy/gather_kcats/fuzzy_kcat_matching/_organism.py @@ -5,7 +5,7 @@ has no exact organism match, the matcher uses this struct to keep the rows for the phylogenetically closest organism(s). -Ported from GECKO MATLAB: src/geckomat/gather_kcats/fuzzyKcatMatching.m. +Ported from GECKO MATLAB: src/gather_kcats/fuzzyKcatMatching.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/get_standard_kcat.py b/src/geckopy/gather_kcats/get_standard_kcat.py index c9f9ddf..9edee97 100644 --- a/src/geckopy/gather_kcats/get_standard_kcat.py +++ b/src/geckopy/gather_kcats/get_standard_kcat.py @@ -2,7 +2,7 @@ without enzyme assignments. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/getStandardKcat.m. +src/gather_kcats/getStandardKcat.m. """ from __future__ import annotations @@ -41,7 +41,7 @@ def assign_standard_kcat( reactions without enzyme constraints. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/getStandardKcat.m. + src/gather_kcats/getStandardKcat.m. A standard MW (median of all proteins in ``uniprot_db``) and a standard kcat (median of all non-zero, non-NaN ``model.ec.kcat`` diff --git a/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py b/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py index 86c129d..e92fee7 100644 --- a/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py +++ b/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py @@ -7,7 +7,7 @@ result with BRENDA / Sabio-RK / CataPro rows). Ported from GECKO MATLAB: -src/geckomat/gather_kcats/mergeDLKcatAndFuzzyKcats.m. +src/deprecated/mergeDLKcatAndFuzzyKcats.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/merge_kcats.py b/src/geckopy/gather_kcats/merge_kcats.py index 7c3b525..aa4b47a 100644 --- a/src/geckopy/gather_kcats/merge_kcats.py +++ b/src/geckopy/gather_kcats/merge_kcats.py @@ -31,7 +31,7 @@ tagged ``source`` string. Ported from / generalizes GECKO MATLAB: -src/geckomat/gather_kcats/mergeDLKcatAndFuzzyKcats.m. +src/deprecated/mergeDLKcatAndFuzzyKcats.m. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/read_dlkcat_output.py b/src/geckopy/gather_kcats/read_dlkcat_output.py index 2795fc8..4653ca3 100644 --- a/src/geckopy/gather_kcats/read_dlkcat_output.py +++ b/src/geckopy/gather_kcats/read_dlkcat_output.py @@ -1,7 +1,7 @@ """Read a DLKcat predictions TSV into a kcat_list DataFrame. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/readDLKcatOutput.m. +src/gather_kcats/readDLKcatOutput.m. """ from __future__ import annotations @@ -39,7 +39,7 @@ def read_dlkcat_output( """Parse DLKcat's output TSV into a kcat_list DataFrame. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/readDLKcatOutput.m. + src/gather_kcats/readDLKcatOutput.m. The input file is the 6-column TSV that DLKcat writes (see ``run_dlkcat``), with one header line and columns diff --git a/src/geckopy/gather_kcats/remove_standard_kcat.py b/src/geckopy/gather_kcats/remove_standard_kcat.py index ca6bfaa..5dc10f9 100644 --- a/src/geckopy/gather_kcats/remove_standard_kcat.py +++ b/src/geckopy/gather_kcats/remove_standard_kcat.py @@ -2,7 +2,7 @@ added by `assign_standard_kcat`. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/removeStandardKcat.m. +src/gather_kcats/removeStandardKcat.m. """ from __future__ import annotations @@ -26,7 +26,7 @@ def remove_standard_kcat(model: "EcModel") -> None: assignments from ``model``. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/removeStandardKcat.m. + src/gather_kcats/removeStandardKcat.m. Inverse of ``assign_standard_kcat``. Cleans up: diff --git a/src/geckopy/gather_kcats/run_dlkcat.py b/src/geckopy/gather_kcats/run_dlkcat.py index 82c30c3..e2f9a5d 100644 --- a/src/geckopy/gather_kcats/run_dlkcat.py +++ b/src/geckopy/gather_kcats/run_dlkcat.py @@ -1,7 +1,7 @@ """Invoke DLKcat (via Docker) to predict kcat values for a model. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/runDLKcat.m. +src/gather_kcats/runDLKcat.m. """ from __future__ import annotations @@ -30,7 +30,7 @@ def run_dlkcat( """Run DLKcat on a prepared input TSV and return the output path. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/runDLKcat.m. + src/gather_kcats/runDLKcat.m. Requires Docker to be installed and the daemon to be running. The DLKcat invocation uses a published image and may take several diff --git a/src/geckopy/gather_kcats/select_kcat_value.py b/src/geckopy/gather_kcats/select_kcat_value.py index 1e54234..b4c8177 100644 --- a/src/geckopy/gather_kcats/select_kcat_value.py +++ b/src/geckopy/gather_kcats/select_kcat_value.py @@ -1,7 +1,7 @@ """Pick one kcat per reaction from a kcat list and write to model.ec.kcat. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/selectKcatValue.m. +src/deprecated/selectKcatValue.m. """ from __future__ import annotations @@ -57,7 +57,7 @@ def apply_kcat_list( """Choose one kcat value per reaction and write it to ``model.ec.kcat``. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/selectKcatValue.m. + src/deprecated/selectKcatValue.m. For each reaction with one or more entries in ``kcat_list``, aggregate the candidate kcats per ``criteria`` (``"max"`` is the diff --git a/src/geckopy/gather_kcats/write_dlkcat_input.py b/src/geckopy/gather_kcats/write_dlkcat_input.py index 2cd7737..ef2084c 100644 --- a/src/geckopy/gather_kcats/write_dlkcat_input.py +++ b/src/geckopy/gather_kcats/write_dlkcat_input.py @@ -1,7 +1,7 @@ """Write a DLKcat-compatible TSV input file for an ec model. Ported from GECKO MATLAB: -src/geckomat/gather_kcats/writeDLKcatInput.m. +src/gather_kcats/writeDLKcatInput.m. """ from __future__ import annotations @@ -40,7 +40,7 @@ def write_dlkcat_input( """Write a DLKcat-compatible TSV input file. Ported from GECKO MATLAB: - src/geckomat/gather_kcats/writeDLKcatInput.m. + src/gather_kcats/writeDLKcatInput.m. For each selected ec.rxn, enumerate (substrate, gene-subunit) pairs from the model's stoichiometry and rxn_enz_mat. Each row diff --git a/src/geckopy/get_enzyme_data/__init__.py b/src/geckopy/get_enzyme_data/__init__.py index bf612ad..8e87cb3 100644 --- a/src/geckopy/get_enzyme_data/__init__.py +++ b/src/geckopy/get_enzyme_data/__init__.py @@ -1,6 +1,6 @@ """Functions that build the enzyme-data side of an ec model. -Mirrors GECKO MATLAB's `src/geckomat/get_enzyme_data/` directory. +Mirrors GECKO MATLAB's `src/get_enzyme_data/` directory. """ from .copy_ec_to_gem import copy_ec_to_gem from .ec_from_database import fill_eccodes_from_database, get_ec_from_database diff --git a/src/geckopy/get_enzyme_data/copy_ec_to_gem.py b/src/geckopy/get_enzyme_data/copy_ec_to_gem.py index f422c4f..fead968 100644 --- a/src/geckopy/get_enzyme_data/copy_ec_to_gem.py +++ b/src/geckopy/get_enzyme_data/copy_ec_to_gem.py @@ -1,7 +1,7 @@ """Copy EC codes from model.ec.eccodes back to per-reaction annotations. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/copyECtoGEM.m. +src/deprecated/copyECtoGEM.m. """ from __future__ import annotations @@ -32,7 +32,7 @@ def copy_ec_to_gem(model: "EcModel", *, overwrite: bool = False) -> None: top-level ``model.eccodes``). Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/copyECtoGEM.m. + src/deprecated/copyECtoGEM.m. For each entry of ``model.ec.rxns`` (after stripping a 4-char prefix when the model is in gecko-light mode), the corresponding diff --git a/src/geckopy/get_enzyme_data/ec_from_gem.py b/src/geckopy/get_enzyme_data/ec_from_gem.py index fdbb927..12ff0c0 100644 --- a/src/geckopy/get_enzyme_data/ec_from_gem.py +++ b/src/geckopy/get_enzyme_data/ec_from_gem.py @@ -1,7 +1,7 @@ """Populate model.ec.eccodes from per-reaction `ec-code` annotations. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/getECfromGEM.m. +src/get_enzyme_data/getECfromGEM.m. """ from __future__ import annotations @@ -45,7 +45,7 @@ def fill_eccodes_from_gem( """Populate ``model.ec.eccodes`` from per-reaction ``ec-code`` annotations. Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/getECfromGEM.m. + src/get_enzyme_data/getECfromGEM.m. For each entry of ``model.ec.rxns``, looks up the corresponding cobra.Reaction (stripping a 4-char prefix first when the model is diff --git a/src/geckopy/get_enzyme_data/find_ec_in_db.py b/src/geckopy/get_enzyme_data/find_ec_in_db.py index ee7c9bb..0ee08ea 100644 --- a/src/geckopy/get_enzyme_data/find_ec_in_db.py +++ b/src/geckopy/get_enzyme_data/find_ec_in_db.py @@ -1,7 +1,7 @@ """Collect EC codes for a reaction's gene_set from a protein database. Ported from GECKO MATLAB: -src/geckomat/get_enzyme_data/findECInDB.m. +src/get_enzyme_data/findECInDB.m. """ from __future__ import annotations @@ -106,7 +106,7 @@ def find_ec_in_db( """Collect EC codes for a reaction's genes from a protein database. Ported from GECKO MATLAB: - src/geckomat/get_enzyme_data/findECInDB.m. + src/get_enzyme_data/findECInDB.m. Per-gene resolution: diff --git a/src/geckopy/kcat_tuning/find_max_value.py b/src/geckopy/kcat_tuning/find_max_value.py index 88fa5b9..df27338 100644 --- a/src/geckopy/kcat_tuning/find_max_value.py +++ b/src/geckopy/kcat_tuning/find_max_value.py @@ -1,7 +1,6 @@ """Find the maximum kcat (or SA-derived kcat) for a set of EC numbers. -Ported from GECKO MATLAB: -src/geckomat/kcat_sensitivity_analysis/findMaxValue.m. +Ported from GECKO 3 MATLAB ``findMaxValue.m``, which GECKO 4 removed. """ from __future__ import annotations @@ -27,8 +26,7 @@ def find_max_value( trigger prefix matching: ``"1.1.1.-"`` matches any code starting with ``"1.1.1."``. - Ported from GECKO MATLAB: - src/geckomat/kcat_sensitivity_analysis/findMaxValue.m. + Ported from GECKO 3 MATLAB ``findMaxValue.m``, which GECKO 4 removed. Parameters ---------- diff --git a/src/geckopy/limit_proteins/__init__.py b/src/geckopy/limit_proteins/__init__.py index 032bcd7..b41e624 100644 --- a/src/geckopy/limit_proteins/__init__.py +++ b/src/geckopy/limit_proteins/__init__.py @@ -1,6 +1,6 @@ """Apply enzyme-concentration constraints to ec models. -Mirrors GECKO MATLAB's `src/geckomat/limit_proteins/` directory. +Mirrors GECKO MATLAB's `src/limit_proteins/` directory. """ from .calculate_f_factor import calculate_f_factor from .constrain_enz_concs import constrain_enz_concs diff --git a/src/geckopy/limit_proteins/constrain_enz_concs.py b/src/geckopy/limit_proteins/constrain_enz_concs.py index 1b543cc..90c9e31 100644 --- a/src/geckopy/limit_proteins/constrain_enz_concs.py +++ b/src/geckopy/limit_proteins/constrain_enz_concs.py @@ -1,7 +1,7 @@ """Constrain enzyme usage reactions by measured protein concentrations. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/constrainEnzConcs.m. +src/limit_proteins/constrainEnzConcs.m. """ from __future__ import annotations @@ -48,7 +48,7 @@ def constrain_enz_concs( ``relax_proteomics_greedy`` sets the bound back to its default. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/constrainEnzConcs.m. + src/limit_proteins/constrainEnzConcs.m. Parameters ---------- diff --git a/src/geckopy/limit_proteins/constrain_flux_data.py b/src/geckopy/limit_proteins/constrain_flux_data.py index 9aa3dc5..4a34bf0 100644 --- a/src/geckopy/limit_proteins/constrain_flux_data.py +++ b/src/geckopy/limit_proteins/constrain_flux_data.py @@ -12,7 +12,7 @@ percentage clamps it within that band. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/constrainFluxData.m. +src/limit_proteins/constrainFluxData.m. """ from __future__ import annotations @@ -46,7 +46,7 @@ def apply_flux_data_constraints( adapter's preferred carbon source so the data determines the choice. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/constrainFluxData.m. + src/limit_proteins/constrainFluxData.m. A ``+/-1000`` measured flux means unconstrained, regardless of ``loose_strict_flux``: diff --git a/src/geckopy/limit_proteins/fill_enz_concs.py b/src/geckopy/limit_proteins/fill_enz_concs.py index 10bbce8..55bdf32 100644 --- a/src/geckopy/limit_proteins/fill_enz_concs.py +++ b/src/geckopy/limit_proteins/fill_enz_concs.py @@ -10,7 +10,7 @@ make the LP respect them, call ``constrain_enz_concs`` next. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/fillEnzConcs.m. +src/limit_proteins/fillEnzConcs.m. """ from __future__ import annotations @@ -51,7 +51,7 @@ def fill_enz_concs( samples), ``data_col`` selects which column to use. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/fillEnzConcs.m. + src/limit_proteins/fillEnzConcs.m. Parameters ---------- diff --git a/src/geckopy/limit_proteins/flexibilize_enz_concs.py b/src/geckopy/limit_proteins/flexibilize_enz_concs.py index 344cccd..9f374bc 100644 --- a/src/geckopy/limit_proteins/flexibilize_enz_concs.py +++ b/src/geckopy/limit_proteins/flexibilize_enz_concs.py @@ -21,7 +21,7 @@ ``docs/relaxation_methods.md`` for the full comparison. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/flexibilizeEnzConcs.m. +src/limit_proteins/flexibilizeEnzConcs.m. """ from __future__ import annotations @@ -138,7 +138,7 @@ def flexibilize_enz_concs( ``flexibilize_enz_concs`` reports no limiting enzyme for them. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/flexibilizeEnzConcs.m. + src/limit_proteins/flexibilizeEnzConcs.m. Parameters ---------- diff --git a/src/geckopy/limit_proteins/get_isozyme_coverage.py b/src/geckopy/limit_proteins/get_isozyme_coverage.py index 44ef8bf..e53b1bf 100644 --- a/src/geckopy/limit_proteins/get_isozyme_coverage.py +++ b/src/geckopy/limit_proteins/get_isozyme_coverage.py @@ -1,7 +1,7 @@ """Report isozyme groups with partial proteomics coverage. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/getIsozymeCoverage.m. +src/limit_proteins/getIsozymeCoverage.m. """ from __future__ import annotations @@ -36,7 +36,7 @@ def get_isozyme_coverage(model: "EcModel") -> tuple[pd.DataFrame, list[str]]: cover. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/getIsozymeCoverage.m. + src/limit_proteins/getIsozymeCoverage.m. Parameters ---------- diff --git a/src/geckopy/limit_proteins/zero_unmeasured_isozymes.py b/src/geckopy/limit_proteins/zero_unmeasured_isozymes.py index 603a955..5be6977 100644 --- a/src/geckopy/limit_proteins/zero_unmeasured_isozymes.py +++ b/src/geckopy/limit_proteins/zero_unmeasured_isozymes.py @@ -1,7 +1,7 @@ """Limit unmeasured isozymes that have a measured sibling. Ported from GECKO MATLAB: -src/geckomat/limit_proteins/zeroUnmeasuredIsozymes.m. +src/limit_proteins/zeroUnmeasuredIsozymes.m. """ from __future__ import annotations @@ -55,7 +55,7 @@ def zero_unmeasured_isozymes( all enzymes. The function is not called by other functions of geckopy. Ported from GECKO MATLAB: - src/geckomat/limit_proteins/zeroUnmeasuredIsozymes.m (method + src/limit_proteins/zeroUnmeasuredIsozymes.m (method ``'detectionLimit'`` there is ``"detection_limit"`` here). Parameters diff --git a/src/geckopy/utilities/__init__.py b/src/geckopy/utilities/__init__.py index 722824a..6f69599 100644 --- a/src/geckopy/utilities/__init__.py +++ b/src/geckopy/utilities/__init__.py @@ -1,6 +1,6 @@ """General utility functions for ecModels. -Mirrors GECKO MATLAB's `src/geckomat/utilities/` directory. +Mirrors GECKO MATLAB's `src/utilities/` directory. """ from .add_new_rxns_to_ec import ( AddNewRxnsResult, diff --git a/src/geckopy/utilities/add_new_rxns_to_ec.py b/src/geckopy/utilities/add_new_rxns_to_ec.py index 2acab5d..a28ca3c 100644 --- a/src/geckopy/utilities/add_new_rxns_to_ec.py +++ b/src/geckopy/utilities/add_new_rxns_to_ec.py @@ -1,7 +1,7 @@ """Add new reactions and enzymes to an ecModel. Ported from GECKO MATLAB: -src/geckomat/utilities/addNewRxnsToEC.m. +src/change_model/addNewRxnsToEC.m. """ from __future__ import annotations @@ -102,7 +102,7 @@ def add_new_rxns_to_ec( gene in its (single, post-split) AND-clause. Ported from GECKO MATLAB: - src/geckomat/utilities/addNewRxnsToEC.m. + src/change_model/addNewRxnsToEC.m. Parameters ---------- diff --git a/src/geckopy/utilities/ec_fva.py b/src/geckopy/utilities/ec_fva.py index 7a0f9a1..2cf6ea8 100644 --- a/src/geckopy/utilities/ec_fva.py +++ b/src/geckopy/utilities/ec_fva.py @@ -22,7 +22,7 @@ ecModels this matters — yeast-GEM has ~4000 conv reactions, each needing two LP solves, so a serial run can be slow. -Ported from GECKO MATLAB: src/geckomat/utilities/ecFVA.m. +Ported from GECKO MATLAB: src/flux_analysis/ecFVA.m. """ from __future__ import annotations @@ -106,7 +106,7 @@ def ec_fva( variant across all reactions' solutions and summing. Ported from GECKO MATLAB: - src/geckomat/utilities/ecFVA.m. + src/flux_analysis/ecFVA.m. Parameters ---------- diff --git a/src/geckopy/utilities/enzyme_usage.py b/src/geckopy/utilities/enzyme_usage.py index 1342bef..303c03c 100644 --- a/src/geckopy/utilities/enzyme_usage.py +++ b/src/geckopy/utilities/enzyme_usage.py @@ -1,7 +1,7 @@ """Compute per-enzyme usage info from a flux distribution. Ported from GECKO MATLAB: -src/geckomat/utilities/enzymeUsage.m. +src/deprecated/enzymeUsage.m. """ from __future__ import annotations @@ -103,7 +103,7 @@ def enzyme_usage( :attr:`Enzyme.cap_usage` and GECKO MATLAB's ``getEnzymeUsage`` give). Ported from GECKO MATLAB: - src/geckomat/utilities/enzymeUsage.m. + src/deprecated/enzymeUsage.m. Parameters ---------- diff --git a/src/geckopy/utilities/get_subset_ec_model.py b/src/geckopy/utilities/get_subset_ec_model.py index 010f6c3..7bde387 100644 --- a/src/geckopy/utilities/get_subset_ec_model.py +++ b/src/geckopy/utilities/get_subset_ec_model.py @@ -1,7 +1,7 @@ """Extract a context-specific ecModel as a subset of a bigger ecModel. Ported from GECKO MATLAB: -src/geckomat/utilities/getSubsetEcModel.m. +src/change_model/getSubsetEcModel.m. """ from __future__ import annotations @@ -70,7 +70,7 @@ def get_subset_ec_model( is not part of the reaction id. Ported from GECKO MATLAB: - src/geckomat/utilities/getSubsetEcModel.m. + src/change_model/getSubsetEcModel.m. If any ``usage_prot_*`` reaction's upper bound differs from the default (1000), a warning is logged: protein-concentration diff --git a/src/geckopy/utilities/load_conventional_gem.py b/src/geckopy/utilities/load_conventional_gem.py index 68d90d3..e3d9972 100644 --- a/src/geckopy/utilities/load_conventional_gem.py +++ b/src/geckopy/utilities/load_conventional_gem.py @@ -13,7 +13,7 @@ anything else via ``read_sbml_model``. Ported from GECKO MATLAB: -src/geckomat/utilities/loadConventionalGEM.m. +src/io/loadConventionalGEM.m. """ from __future__ import annotations diff --git a/src/geckopy/utilities/load_ec_model.py b/src/geckopy/utilities/load_ec_model.py index 7d5a918..24a379c 100644 --- a/src/geckopy/utilities/load_ec_model.py +++ b/src/geckopy/utilities/load_ec_model.py @@ -41,7 +41,7 @@ geckopy has no global default adapter: pass one explicitly, or use an absolute filename. -Ported from GECKO MATLAB: src/geckomat/utilities/loadEcModel.m. +Ported from GECKO MATLAB: src/io/loadEcModel.m. """ from __future__ import annotations diff --git a/src/geckopy/utilities/map_rxns_to_conv.py b/src/geckopy/utilities/map_rxns_to_conv.py index 083e74c..05be430 100644 --- a/src/geckopy/utilities/map_rxns_to_conv.py +++ b/src/geckopy/utilities/map_rxns_to_conv.py @@ -1,7 +1,7 @@ """Map a flux vector from ec-rxn space back to conventional-rxn space. Ported from GECKO MATLAB: -src/geckomat/utilities/mapRxnsToConv.m. +src/change_model/mapRxnsToConv.m. """ from __future__ import annotations @@ -75,7 +75,7 @@ def map_rxns_to_conv( ``prot_pool_exchange``) under their stripped labels. Ported from GECKO MATLAB: - src/geckomat/utilities/mapRxnsToConv.m. + src/change_model/mapRxnsToConv.m. Parameters ---------- diff --git a/src/geckopy/utilities/report_enzyme_usage.py b/src/geckopy/utilities/report_enzyme_usage.py index 3b9b639..8ed265f 100644 --- a/src/geckopy/utilities/report_enzyme_usage.py +++ b/src/geckopy/utilities/report_enzyme_usage.py @@ -1,7 +1,7 @@ """Build human-readable reports of enzyme usage from an LP solution. Ported from GECKO MATLAB: -src/geckomat/utilities/reportEnzymeUsage.m. +src/enzyme_usage/reportEnzymeUsage.m. """ from __future__ import annotations @@ -99,7 +99,7 @@ def report_enzyme_usage( of ``prot_pool_exchange`` capacity) replacing ``cap_usage``. Ported from GECKO MATLAB: - src/geckomat/utilities/reportEnzymeUsage.m. + src/enzyme_usage/reportEnzymeUsage.m. Parameters ---------- diff --git a/src/geckopy/utilities/save_ec_model.py b/src/geckopy/utilities/save_ec_model.py index b460ff6..5a46c7b 100644 --- a/src/geckopy/utilities/save_ec_model.py +++ b/src/geckopy/utilities/save_ec_model.py @@ -25,7 +25,7 @@ writer emits it) and are restored afterwards, leaving the caller's model unchanged. -Ported from GECKO MATLAB: src/geckomat/utilities/saveEcModel.m. +Ported from GECKO MATLAB: src/io/saveEcModel.m. """ from __future__ import annotations From e2c4a01eaad8e96c485df0c14a9bd77b18095b16 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:56:00 +0100 Subject: [PATCH 24/42] feat: deprecate apply_custom_kcats and refuse the gecko 3 custom kcats file --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 2 +- .../ec_model/pipeline/apply_custom_kcats.py | 30 ++++++++++++++-- src/geckopy/gather_kcats/kcat_assignment.py | 34 ++++++++----------- src/geckopy/upgrade.py | 10 +++--- tests/test_apply_custom_kcats.py | 6 ++++ tests/test_ec_tree.py | 4 ++- tests/test_kcat_assignment.py | 27 +++++++++++---- tests/test_upgrade.py | 18 +++++++--- 9 files changed, 92 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14c97df..4558101 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index f13861a..bd64fca 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -223,7 +223,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | ---------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `makeEcModel` | `make_ec_model` | **`[3→4]`** ec.genes/enzymes/mw/sequence are sorted alphabetically for stable order; KEGG fallback for unmatched genes uses `data/kegg.tsv`, downloaded when missing (an empty `kegg.id` leaves KEGG out), or `kegg_db=`. **`[Py]`** Single return; unmatched genes are logged and annotated on `rxn.notes['geckopy_warning']` instead of being returned. | | `applyComplexData` | `apply_complex_data` | — | -| `applyCustomKcats` | `apply_custom_kcats` | **`[Py]`** Mutates; diagnostics via logging. Takes a pre-loaded DataFrame, or reads `/data/customKcats.tsv` by default. | +| `applyCustomKcats` | `apply_custom_kcats` | **`[3→4]`** Deprecated in both: `convertCustomKcats` / `convert_custom_kcats` turn `data/customKcats.tsv` into `data/kcats/custom.tsv`, which `collectKcats` / `collect_kcats` read; they raise an error when a stage lists custom and only `data/customKcats.tsv` exists. **`[Py]`** Mutates; diagnostics via logging; reads `/data/customKcats.tsv` by default. | | `applyKcatConstraints` | `apply_kcat_constraints` | **`[3→4]`** Writes coefficients with the forward-direction `usage_prot_*` / `prot_pool_exchange` convention. | | `findMetSmiles` | `find_met_smiles` | **`[Py]`** Asynchronous PubChem cache via `requests`; uses a TSV cache (`data/smilesDB.tsv`) so re-runs are offline. | | `getComplexData` | `get_complex_data` | — | diff --git a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py b/src/geckopy/ec_model/pipeline/apply_custom_kcats.py index c5a343f..1bd9129 100644 --- a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py +++ b/src/geckopy/ec_model/pipeline/apply_custom_kcats.py @@ -1,12 +1,16 @@ -"""Apply user-curated custom kcats from a TSV file. +"""Apply user-curated custom kcats from a GECKO 3 customKcats.tsv file. -Ported from GECKO MATLAB: src/change_model/applyCustomKcats.m. +Deprecated: GECKO 4 projects keep custom kcats in ``data/kcats/custom.tsv``, +which ``convert_custom_kcats`` writes from ``data/customKcats.tsv``. + +Ported from GECKO MATLAB: src/deprecated/applyCustomKcats.m. """ from __future__ import annotations import logging import math import re +import warnings from pathlib import Path from typing import TYPE_CHECKING, Optional @@ -28,6 +32,28 @@ def apply_custom_kcats( path: Optional[Path] = None, *, apply: bool = True, +) -> None: + """Deprecated: apply the kcats of a GECKO 3 ``customKcats.tsv`` file. + + ``convert_custom_kcats`` turns ``data/customKcats.tsv`` into + ``data/kcats/custom.tsv``, which ``collect_kcats`` and ``assign_kcats`` + read. This function still applies the old file, as described for + :func:`_apply_custom_kcats`, after a ``DeprecationWarning``. + """ + warnings.warn( + "apply_custom_kcats is deprecated; convert data/customKcats.tsv with " + "convert_custom_kcats into data/kcats/custom.tsv, which assign_kcats reads.", + DeprecationWarning, + stacklevel=2, + ) + _apply_custom_kcats(model, path, apply=apply) + + +def _apply_custom_kcats( + model: "EcModel", + path: Optional[Path] = None, + *, + apply: bool = True, ) -> None: """Apply user-curated kcats from a customKcats.tsv file. diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index f9e3d1a..0a561e5 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -18,7 +18,6 @@ import numpy as np import pandas as pd -from ..ec_model.pipeline.apply_custom_kcats import apply_custom_kcats from ..ec_model.pipeline.apply_kcat import apply_kcat_constraints from ..ec_model.pipeline.fill_kcats import fill_kcats_from_isozymes from .get_standard_kcat import assign_standard_kcat @@ -171,24 +170,24 @@ def _decide(model: "EcModel", adapter: "ModelAdapter", uniprot_db: Optional["Uni spread = np.full(n, np.nan) report: dict[str, Any] = {"preset": config.preset, "missing": [], "sources": {}} loaded: dict[str, Optional[pd.DataFrame]] = {} - legacy_custom = False sd = np.full(n, np.nan) params = None def load(name: str) -> Optional[pd.DataFrame]: - nonlocal legacy_custom if name not in loaded: file = kcat_dir / f"{name}.tsv" if file.is_file(): loaded[name], report["sources"][name] = load_kcat_source(model, file) + elif name == "custom" and (path / "data" / "customKcats.tsv").is_file(): + raise ValueError( + f"{path / 'data' / 'customKcats.tsv'} is a GECKO 3 custom kcats file and {file} does " + "not exist. Convert it with convert_custom_kcats (geckopy upgrade does this)." + ) else: loaded[name] = None - if name == "custom" and (path / "data" / "customKcats.tsv").is_file(): - legacy_custom = True - else: - report["missing"].append(name) - logger.warning("No kcat source file %s; it is left out. update_kcat_sources gathers it.", file) + report["missing"].append(name) + logger.warning("No kcat source file %s; it is left out. update_kcat_sources gathers it.", file) return loaded[name] for s, st in enumerate(config.stages, start=1): @@ -246,12 +245,6 @@ def load(name: str) -> Optional[pd.DataFrame]: candidates[i] = len(values) spread[i] = np.log10(values.max()) - np.log10(values.min()) - if legacy_custom: - before = ec.kcat.copy() - apply_custom_kcats(model, path=path / "data" / "customKcats.tsv") - changed = ec.kcat != before - for i in np.flatnonzero(changed): - stage[i], candidates[i], spread[i] = "custom", 1, 0.0 if config.isozymes and not ec.gecko_light: before = ec.kcat == 0 fill_kcats_from_isozymes(model) @@ -345,11 +338,12 @@ def collect_kcats( only where there is no value from measurements. The BRENDA aggregation is ``[kcat_sources.brenda] aggregation`` (see :func:`update_kcat_sources`). - After the stages: custom kcats of ``data/customKcats.tsv`` when a stage - lists custom but there is no ``data/kcats/custom.tsv``; with - ``isozymes``, kcats from isozymes; with ``standard``, the standard kcat - for the rest. A source file a stage names that does not exist is - reported and left out. + After the stages: with ``isozymes``, kcats from isozymes; with + ``standard``, the standard kcat for the rest. A source file a stage names + that does not exist is reported and left out. A stage that lists custom + raises a ``ValueError`` when there is no ``data/kcats/custom.tsv`` but a + GECKO 3 ``data/customKcats.tsv``: :func:`~geckopy.upgrade.convert_custom_kcats` + converts it. Parameters ---------- @@ -366,7 +360,7 @@ def collect_kcats( ------- (decisions, report, ec_model) ``decisions`` has one row per ``model.ec`` reaction: ``ec_rxn``, - ``stage`` (the stage number, ``custom``, ``isozymes``, ``standard``, + ``stage`` (the stage number, ``isozymes``, ``standard``, or empty without a kcat), ``source``, ``kcat``, ``candidates`` (values the deciding source had), ``spread_log10`` (their range, in log10 units) and ``sd_log10`` (the uncertainty of a combined kcat, a standard diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index 762e705..550f82c 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -51,9 +51,9 @@ def convert_custom_kcats( ) -> tuple[Path, dict]: """Convert ``data/customKcats.tsv`` into ``data/kcats/custom.tsv``. - The kcats are those :func:`~geckopy.ec_model.pipeline.apply_custom_kcats` - gives the ecModel, including which of two entries for the same reaction - is kept: one row per reaction, keyed by conventional reaction and enzyme set + The kcats are those the deprecated + :func:`~geckopy.ec_model.pipeline.apply_custom_kcats` gives the ecModel, + including which of two entries for the same reaction is kept: one row per reaction, keyed by conventional reaction and enzyme set (see :func:`~geckopy.gather_kcats.ec_rxn_keys`), with the entry's notes in the note column. The ``stoicho`` column is left out: complex stoichiometry comes from ``apply_complex_data``. The conversion needs the @@ -76,7 +76,7 @@ def convert_custom_kcats( The file written, and a report with ``rows`` (rows written). """ from .adapter import resolve_adapter - from .ec_model.pipeline.apply_custom_kcats import apply_custom_kcats + from .ec_model.pipeline.apply_custom_kcats import _apply_custom_kcats from .gather_kcats.kcat_sources import _geckopy_version, ec_rxn_keys, format_number if model.ec.gecko_light: @@ -95,7 +95,7 @@ def convert_custom_kcats( n = len(applied.ec.rxns) applied.ec.notes = [""] * n applied.ec.source = [""] * n - apply_custom_kcats(applied, path=src, apply=False) + _apply_custom_kcats(applied, path=src, apply=False) base, enzymes = ec_rxn_keys(applied) rows: dict[tuple[str, str], list[str]] = {} for i in range(n): diff --git a/tests/test_apply_custom_kcats.py b/tests/test_apply_custom_kcats.py index d67a7d4..736b8e1 100644 --- a/tests/test_apply_custom_kcats.py +++ b/tests/test_apply_custom_kcats.py @@ -269,6 +269,12 @@ def test_apply_false_leaves_s_matrix_unchanged(tmp_path): # File handling and errors # --------------------------------------------------------------------------- # +def test_apply_custom_kcats_is_deprecated(): + ec_model = _ectestgem_ec_model() + with pytest.warns(DeprecationWarning, match="convert_custom_kcats"): + apply_custom_kcats(ec_model, apply=False) + + def test_missing_file_raises(tmp_path): ec_model = _ectestgem_ec_model() with pytest.raises(FileNotFoundError): diff --git a/tests/test_ec_tree.py b/tests/test_ec_tree.py index f94c32f..1fa157b 100644 --- a/tests/test_ec_tree.py +++ b/tests/test_ec_tree.py @@ -10,7 +10,7 @@ import pandas as pd import pytest -from geckopy import ModelAdapter, make_ec_model +from geckopy import ModelAdapter, convert_custom_kcats, make_ec_model from geckopy.databases import fetch_brenda_asset, load_brenda_measurements from geckopy.gather_kcats import ( assign_kcats, @@ -82,6 +82,8 @@ def _project(tmp_path: Path, toml_extra: str = ""): adapter = ModelAdapter.from_folder(project) model = make_ec_model(cobra.io.read_sbml_model(str(adapter.params.conv_gem)), adapter) fill_eccodes_from_gem(model) + convert_custom_kcats(model, adapter=adapter) + (project / "data" / "customKcats.tsv").unlink() return model, adapter, project diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 29d6c03..c3963ba 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -10,7 +10,7 @@ import pydantic import pytest -from geckopy import ModelAdapter, make_ec_model +from geckopy import ModelAdapter, convert_custom_kcats, make_ec_model from geckopy.databases import load_brenda_data, load_phyl_dist, load_uniprot_tsv from geckopy.ec_model.pipeline import apply_custom_kcats, apply_kcat_constraints, fill_kcats_from_isozymes from geckopy.gather_kcats import ( @@ -38,9 +38,10 @@ ) -def _project(tmp_path: Path, toml_extra: str = ""): +def _project(tmp_path: Path, toml_extra: str = "", convert_custom: bool = True): """ecTestGEM in tmp_path, with its BRENDA files as the project's BRENDA - data and DLKcat output for the test model.""" + data and DLKcat output for the test model; its customKcats.tsv is + converted to data/kcats/custom.tsv unless ``convert_custom`` is False.""" project = tmp_path / "ecTestGEM" shutil.copytree(EXAMPLE_DIR, project) brenda = project / "data" / "brenda" @@ -53,6 +54,9 @@ def _project(tmp_path: Path, toml_extra: str = ""): toml.write_text(toml.read_text(encoding="utf-8") + "\n" + toml_extra, encoding="utf-8") adapter = ModelAdapter.from_folder(project) model = make_ec_model(cobra.io.read_sbml_model(str(adapter.params.conv_gem)), adapter) + if convert_custom: + convert_custom_kcats(model, adapter=adapter) + (project / "data" / "customKcats.tsv").unlink() return model, adapter, project @@ -70,7 +74,8 @@ def test_assign_kcats_reproduces_chain(tmp_path): merged = merge_kcats(fuzzy, dlkcat, source_priority=["database_top", "dlkcat", "database_bottom"], top_origin_limit=6) apply_kcat_list(a, merged) - apply_custom_kcats(a, path=data / "customKcats.tsv") + with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): + apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") fill_kcats_from_isozymes(a) assign_standard_kcat(a, uniprot) apply_kcat_constraints(a) @@ -87,7 +92,7 @@ def test_assign_kcats_reproduces_chain(tmp_path): assert report["preset"] == "gecko_classic" assert list(decisions["ec_rxn"]) == list(b.ec.rxns) assert (project / "output" / f"{base.id}_kcatDecisions.tsv").is_file() - assert {"custom", "1"} <= set(decisions["stage"]) + assert "1" in set(decisions["stage"]) def test_gecko_median_preset(tmp_path): @@ -102,7 +107,8 @@ def test_gecko_median_preset(tmp_path): merged = merge_kcats(fuzzy, dlkcat, source_priority=["database_top", "dlkcat", "database_bottom"], top_origin_limit=4) apply_kcat_list(a, merged) - apply_custom_kcats(a, path=data / "customKcats.tsv") + with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): + apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") fill_kcats_from_isozymes(a) assign_standard_kcat(a, uniprot) apply_kcat_constraints(a) @@ -155,6 +161,13 @@ def test_collect_kcats_returns_isozyme_kcats_without_constraints(tmp_path): assert any(stoich(decided, rxn) != stoich(base, rxn) for rxn in filled) +def test_legacy_custom_kcats_file_raises(tmp_path): + """A stage that lists custom raises on a GECKO 3 customKcats.tsv without custom.tsv.""" + base, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "gecko_median"\n', convert_custom=False) + with pytest.raises(ValueError, match="convert_custom_kcats"): + collect_kcats(base, adapter=adapter, write_report=False) + + def test_kcat_sources_table_overrides_only_its_keys(tmp_path): """[kcat_sources.brenda] without aggregation keeps the preset's median.""" from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config @@ -196,7 +209,7 @@ def test_update_kcat_sources(tmp_path): report = update_kcat_sources(base, dry_run=True, adapter=adapter) assert list(report["source"]) == ["custom", "brenda", "dlkcat"] assert list(report["status"]) == ["skipped", "missing", "missing"] - assert not (project / "data" / "kcats").exists() + assert not (project / "data" / "kcats" / "brenda.tsv").exists() report = update_kcat_sources(base, adapter=adapter) assert list(report["status"]) == ["skipped", "gathered", "gathered"] _, meta = read_kcat_source(project / "data" / "kcats" / "brenda.tsv") diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index f15ab64..7836646 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -8,6 +8,7 @@ import cobra import numpy as np +import pytest from geckopy import ModelAdapter, make_ec_model, save_ec_model from geckopy.cli import main @@ -69,14 +70,23 @@ def test_convert_custom_kcats(tmp_path): def test_converted_custom_kcats_give_same_kcats(tmp_path): + """collect_kcats refuses the GECKO 3 file; once converted, the custom kcats + are those the deprecated apply_custom_kcats gives.""" + from geckopy.ec_model.pipeline import apply_custom_kcats + model, adapter, project = _project(tmp_path) kcats_from_brenda(model, adapter=adapter) - a, _, _ = collect_kcats(model, adapter=adapter, write_report=False) + with pytest.raises(ValueError, match="convert_custom_kcats"): + collect_kcats(model, adapter=adapter, write_report=False) convert_custom_kcats(model, adapter=adapter) b, _, _ = collect_kcats(model, adapter=adapter, write_report=False) - np.testing.assert_array_equal(b["kcat"].to_numpy(), a["kcat"].to_numpy()) - assert list(b["source"] == "custom") == list(a["source"] == "custom") - assert (b["source"] == "custom").any() + ref = model.copy() + with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): + apply_custom_kcats(ref, apply=False) + custom = [i for i, s in enumerate(ref.ec.source) if s == "custom"] + assert custom + assert list(b["source"].iloc[custom]) == ["custom"] * len(custom) + np.testing.assert_array_equal(b["kcat"].to_numpy()[custom], ref.ec.kcat[custom]) def test_upgrade_ec_model(tmp_path): From 02758289c12bed9f390c26f97b8e2f17edb0e96c Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:57:02 +0100 Subject: [PATCH 25/42] feat: deprecate run_dlkcat and read_dlkcat_output --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 4 ++-- src/geckopy/gather_kcats/kcats_from.py | 8 +++---- .../gather_kcats/read_dlkcat_output.py | 23 +++++++++++++++++++ src/geckopy/gather_kcats/run_dlkcat.py | 23 +++++++++++++++++++ tests/test_deprecated_aliases.py | 15 ++++++++++++ tests/test_kcat_sources.py | 8 +++---- 7 files changed, 72 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4558101..af8d01a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. `write_dlkcat_input` stays public. * `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index bd64fca..3578d45 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -239,9 +239,9 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `fuzzyKcatMatching` | `fuzzy_kcat_matching` | **`[3→4]`** Wildcard escalation no longer escalates past the fully-wildcarded form. **`[Py]`** Returns a `DataFrame` (rows: ec / substrate / organism / kcat / source / wildcard\_level …) instead of a struct; takes pre-loaded `BrendaData` + `PhylDist`. | | `getStandardKcat` | `assign_standard_kcat` | **`[3→4]`** Applies per-subsystem mean kcat (GECKO 3 fell back to the global standard kcat unless every subsystem matched). **`[Py]`** Renamed; `get_standard_kcat` kept as deprecated alias. | | `mergeDLKcatAndFuzzyKcats` | `merge_kcats` | **`[Py]`** Generalised to accept any combination of BRENDA / DLKcat / OpenKineticsPredictor / manual rows; records per-row provenance. `merge_dlkcat_and_fuzzy_kcats` kept as deprecated alias. | -| `readDLKcatOutput` | `read_dlkcat_output` | **`[3→4]`** Case-insensitive substrate-name matching. | +| `readDLKcatOutput` | `read_dlkcat_output` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'file')` / `kcats_from_dlkcat(..., via="file")` read the file. Case-insensitive substrate-name matching. | | `removeStandardKcat` | `remove_standard_kcat` | — | -| `runDLKcat` | `run_dlkcat` | — | +| `runDLKcat` | `run_dlkcat` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` run DLKcat. | | `selectKcatValue` | `apply_kcat_list` | **`[Py]`** Renamed; `select_kcat_value` kept as deprecated alias. | | `writeDLKcatInput` | `write_dlkcat_input` | **`[3→4]`** Bounds-safe when `ec_rxns` is a subset. | | `submitOpenKineticsPredictor` | `submit_open_kinetics_predictor` | **`[3→4]`** New REST-based submit (replaced `writeOpenKineticsPredictorInput`). API key via argument / `OKP_API_KEY` env / `data/okpApiKey.txt`. | diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index e33d35f..40f4b85 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -25,8 +25,8 @@ from .kcat_sources import format_number, kcat_source_file, save_kcat_source from .open_kinetics_predictor import fetch_open_kinetics_predictor, submit_open_kinetics_predictor from .open_kinetics_predictor.parse_output import parse_okp_output -from .read_dlkcat_output import read_dlkcat_output -from .run_dlkcat import run_dlkcat +from .read_dlkcat_output import _read_dlkcat_output +from .run_dlkcat import _run_dlkcat from .write_dlkcat_input import write_dlkcat_input if TYPE_CHECKING: @@ -327,14 +327,14 @@ def kcats_from_dlkcat( ) write_dlkcat_input(model, dlkcat_file, load_dlkcat_ignore_lists(data_dir), ec_rxns=ec_rxns, overwrite=True) - run_dlkcat(dlkcat_file) + _run_dlkcat(dlkcat_file) run_file.write_text(f"ecRxns: {len(set(ec_rxns))}\n" if ec_rxns is not None else "", encoding="utf-8", newline="\n") elif not dlkcat_file.is_file(): raise FileNotFoundError( f"No DLKcat output at {dlkcat_file}. Use via='okp' to predict the kcats." ) - kcat_list = read_dlkcat_output(model, dlkcat_file) + kcat_list = _read_dlkcat_output(model, dlkcat_file) if save: parameters = {"run": via == "docker"} if run_file.is_file(): diff --git a/src/geckopy/gather_kcats/read_dlkcat_output.py b/src/geckopy/gather_kcats/read_dlkcat_output.py index 4653ca3..3394850 100644 --- a/src/geckopy/gather_kcats/read_dlkcat_output.py +++ b/src/geckopy/gather_kcats/read_dlkcat_output.py @@ -1,11 +1,15 @@ """Read a DLKcat predictions TSV into a kcat_list DataFrame. +``kcats_from_dlkcat(..., via="file")`` reads it; ``read_dlkcat_output`` is a +deprecated alias. + Ported from GECKO MATLAB: src/gather_kcats/readDLKcatOutput.m. """ from __future__ import annotations import logging +import warnings from pathlib import Path from typing import TYPE_CHECKING @@ -35,6 +39,25 @@ def read_dlkcat_output( file_path: str | Path, *, strict: bool = False, +) -> pd.DataFrame: + """Deprecated: use ``kcats_from_dlkcat(..., via="file")``. + + Reads the file as :func:`_read_dlkcat_output` does, after a + ``DeprecationWarning``. + """ + warnings.warn( + 'read_dlkcat_output is deprecated; use kcats_from_dlkcat(..., via="file").', + DeprecationWarning, + stacklevel=2, + ) + return _read_dlkcat_output(model, file_path, strict=strict) + + +def _read_dlkcat_output( + model: "EcModel", + file_path: str | Path, + *, + strict: bool = False, ) -> pd.DataFrame: """Parse DLKcat's output TSV into a kcat_list DataFrame. diff --git a/src/geckopy/gather_kcats/run_dlkcat.py b/src/geckopy/gather_kcats/run_dlkcat.py index e2f9a5d..30dba94 100644 --- a/src/geckopy/gather_kcats/run_dlkcat.py +++ b/src/geckopy/gather_kcats/run_dlkcat.py @@ -1,5 +1,8 @@ """Invoke DLKcat (via Docker) to predict kcat values for a model. +``kcats_from_dlkcat(..., via="docker")`` runs it; ``run_dlkcat`` is a +deprecated alias. + Ported from GECKO MATLAB: src/gather_kcats/runDLKcat.m. """ @@ -7,6 +10,7 @@ import logging import shutil +import warnings import subprocess import tempfile from pathlib import Path @@ -26,6 +30,25 @@ def run_dlkcat( output_path: Optional[str | Path] = None, docker_image: str = _DEFAULT_DOCKER_IMAGE, timeout: Optional[float] = None, +) -> Path: + """Deprecated: use ``kcats_from_dlkcat(..., via="docker")``. + + Runs DLKcat as :func:`_run_dlkcat` does, after a ``DeprecationWarning``. + """ + warnings.warn( + 'run_dlkcat is deprecated; use kcats_from_dlkcat(..., via="docker").', + DeprecationWarning, + stacklevel=2, + ) + return _run_dlkcat(input_path, output_path=output_path, docker_image=docker_image, timeout=timeout) + + +def _run_dlkcat( + input_path: str | Path, + *, + output_path: Optional[str | Path] = None, + docker_image: str = _DEFAULT_DOCKER_IMAGE, + timeout: Optional[float] = None, ) -> Path: """Run DLKcat on a prepared input TSV and return the output path. diff --git a/tests/test_deprecated_aliases.py b/tests/test_deprecated_aliases.py index b57b889..14d8798 100644 --- a/tests/test_deprecated_aliases.py +++ b/tests/test_deprecated_aliases.py @@ -190,3 +190,18 @@ def test_sigma_fitter_alias_passes_method(monkeypatch): warnings.simplefilter("ignore", DeprecationWarning) module.sigma_fitter(object(), method="bisect") assert seen["method"] == "bisect" + + +def test_dlkcat_entry_points_are_deprecated_aliases(monkeypatch, tmp_path): + """run_dlkcat and read_dlkcat_output warn and forward to the code + kcats_from_dlkcat runs.""" + import importlib + + run_module = importlib.import_module("geckopy.gather_kcats.run_dlkcat") + read_module = importlib.import_module("geckopy.gather_kcats.read_dlkcat_output") + seen = [] + monkeypatch.setattr(run_module, "_run_dlkcat", lambda path, **kwargs: seen.append(("run", path))) + monkeypatch.setattr(read_module, "_read_dlkcat_output", lambda model, path, **kwargs: seen.append(("read", path))) + _assert_deprecated(run_module.run_dlkcat, tmp_path / "in.tsv", expected_substring='via="docker"') + _assert_deprecated(read_module.read_dlkcat_output, None, tmp_path / "out.tsv", expected_substring='via="file"') + assert seen == [("run", tmp_path / "in.tsv"), ("read", tmp_path / "out.tsv")] diff --git a/tests/test_kcat_sources.py b/tests/test_kcat_sources.py index 8e64ca9..a0d506b 100644 --- a/tests/test_kcat_sources.py +++ b/tests/test_kcat_sources.py @@ -168,7 +168,7 @@ def test_kcats_from_dlkcat_and_okp_write_files(tmp_path, monkeypatch): data.mkdir() (data / "DLKcat.tsv").write_text("placeholder\n", encoding="utf-8") dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) - monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) + monkeypatch.setattr(kcats_from_module, "_read_dlkcat_output", lambda model, path: dlkcat) _, done = kcats_from_dlkcat(full) assert done records, meta = read_kcat_source(data / "kcats" / "dlkcat.tsv") @@ -428,7 +428,7 @@ def fake_okp(model, **kwargs): # With the file, the default reads it and does not contact OKP. (data / "DLKcat.tsv").write_text("placeholder\n", encoding="utf-8") dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) - monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) + monkeypatch.setattr(kcats_from_module, "_read_dlkcat_output", lambda model, path: dlkcat) kcat_list, done = kcats_from_dlkcat(full) assert done and len(kcat_list) == len(dlkcat) and len(calls) == 2 kcats_from_dlkcat(full, via="okp") @@ -459,9 +459,9 @@ def test_kcats_from_dlkcat_run_is_docker(tmp_path, monkeypatch): ran = [] monkeypatch.setattr(kcats_from_module.shutil, "which", lambda name: "/bin/docker") monkeypatch.setattr(kcats_from_module, "write_dlkcat_input", lambda *a, **k: ran.append("write")) - monkeypatch.setattr(kcats_from_module, "run_dlkcat", lambda path: ran.append("run")) + monkeypatch.setattr(kcats_from_module, "_run_dlkcat", lambda path: ran.append("run")) dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) - monkeypatch.setattr(kcats_from_module, "read_dlkcat_output", lambda model, path: dlkcat) + monkeypatch.setattr(kcats_from_module, "_read_dlkcat_output", lambda model, path: dlkcat) _, done = kcats_from_dlkcat(full, run=True) assert done and ran == ["write", "run"] _, meta = read_kcat_source(tmp_path / "data" / "kcats" / "dlkcat.tsv") From 80f05e6691ceafc3e14e84229ebb38318507a1d5 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:58:20 +0100 Subject: [PATCH 26/42] refactor: make the openkineticspredictor engine private --- CHANGELOG.md | 1 + docs/migrating_from_gecko_matlab.md | 6 +- src/geckopy/__init__.py | 4 -- src/geckopy/adapter/params.py | 2 +- src/geckopy/gather_kcats/__init__.py | 4 -- src/geckopy/gather_kcats/kcats_from.py | 14 ++-- src/geckopy/gather_kcats/merge_kcats.py | 4 +- .../open_kinetics_predictor/__init__.py | 13 +--- .../open_kinetics_predictor/build_input.py | 4 +- .../open_kinetics_predictor/parse_output.py | 6 +- .../open_kinetics_predictor/run.py | 26 +++---- tests/test_kcat_sources.py | 10 +-- tests/test_merge_dlkcat_and_fuzzy_kcats.py | 2 +- tests/test_open_kinetics_predictor.py | 68 +++++++++++-------- 14 files changed, 78 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af8d01a..421989c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. * `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. `write_dlkcat_input` stays public. * `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 3578d45..d38563b 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -244,10 +244,6 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `runDLKcat` | `run_dlkcat` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` run DLKcat. | | `selectKcatValue` | `apply_kcat_list` | **`[Py]`** Renamed; `select_kcat_value` kept as deprecated alias. | | `writeDLKcatInput` | `write_dlkcat_input` | **`[3→4]`** Bounds-safe when `ec_rxns` is a subset. | -| `submitOpenKineticsPredictor` | `submit_open_kinetics_predictor` | **`[3→4]`** New REST-based submit (replaced `writeOpenKineticsPredictorInput`). API key via argument / `OKP_API_KEY` env / `data/okpApiKey.txt`. | -| `fetchOpenKineticsPredictor` | `fetch_open_kinetics_predictor` | **`[3→4]`** New REST-based fetch (replaced `readOpenKineticsPredictorOutput`); per-row `kcatSource` reports actual provenance (CataPro / BRENDA / Sabio-RK). | -| `writeOpenKineticsPredictorInput` | *(removed)* | **`[3→4]`** Replaced by `submit_open_kinetics_predictor`. | -| `readOpenKineticsPredictorOutput` | *(removed)* | **`[3→4]`** Replaced by `fetch_open_kinetics_predictor`. | | `kcatsFromBrenda` | `kcats_from_brenda` | Fuzzy BRENDA matching, kept as `data/kcats/brenda.tsv`. Loads the BRENDA data and KEGG distances from the adapter unless passed in. | | `kcatsFromDLKcat` | `kcats_from_dlkcat` | DLKcat, kept as `data/kcats/dlkcat.tsv`. `via` is `"okp"` (the DLKcat method of OpenKineticsPredictor), `"file"` (`data/DLKcat.tsv`) or `"docker"` (a local run, `run=True`); returns `(kcat_list, done)` as `kcats_from_okp`. | | `kcatsFromOKP` | `kcats_from_okp` | One OpenKineticsPredictor method, kept as `data/kcats/okp_.tsv`; submits a job when there is none, writes the file once it is done. Returns `(kcat_list, done)`. | @@ -449,7 +445,7 @@ for the MATLAB-side items the port surfaced. - **KEGG as a fallback protein/EC source** (`make_ec_model(kegg_db=…)`, `fill_eccodes_from_database`). - **OpenKineticsPredictor over its REST API** — - `submit_open_kinetics_predictor` / `fetch_open_kinetics_predictor`. + `kcatsFromOKP` / `kcats_from_okp`, which submit a job and fetch its result. - **Forward-direction protein reactions** (`usage_prot_*` / `prot_pool_exchange`). - **Refreshed BRENDA TSV schema** (geckopy adds the max+median diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index efed8d4..526b987 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -86,7 +86,6 @@ assign_standard_kcat, collect_kcats, ec_stats_from_brenda, - fetch_open_kinetics_predictor, fuzzy_kcat_matching, get_standard_kcat, # deprecated; alias of assign_standard_kcat kcats_from_brenda, @@ -104,7 +103,6 @@ run_dlkcat, save_kcat_source, select_kcat_value, # deprecated; alias of apply_kcat_list - submit_open_kinetics_predictor, update_kcat_sources, write_dlkcat_input, ) @@ -222,7 +220,6 @@ "assign_standard_kcat", "collect_kcats", "ec_stats_from_brenda", - "fetch_open_kinetics_predictor", "fuzzy_kcat_matching", "kcats_from_brenda", "kcats_from_dlkcat", @@ -238,7 +235,6 @@ "review_assignment", "run_dlkcat", "save_kcat_source", - "submit_open_kinetics_predictor", "update_kcat_sources", "write_dlkcat_input", # get_enzyme_data diff --git a/src/geckopy/adapter/params.py b/src/geckopy/adapter/params.py index 6ff1d9e..0e104a1 100644 --- a/src/geckopy/adapter/params.py +++ b/src/geckopy/adapter/params.py @@ -279,7 +279,7 @@ def _check_group_keys(self) -> "EvotuneParams": class OkpParams(BaseModel): - """OpenKineticsPredictor settings (used by submit/fetch_open_kinetics_predictor). + """OpenKineticsPredictor settings (used by kcats_from_okp). The API key is intentionally NOT here (it is a secret): provide it as a function argument, the OKP_API_KEY environment variable, or the file diff --git a/src/geckopy/gather_kcats/__init__.py b/src/geckopy/gather_kcats/__init__.py index a324af0..c89daa4 100644 --- a/src/geckopy/gather_kcats/__init__.py +++ b/src/geckopy/gather_kcats/__init__.py @@ -13,8 +13,6 @@ from .open_kinetics_predictor import ( OKPClient, OKPError, - fetch_open_kinetics_predictor, - submit_open_kinetics_predictor, ) from .read_dlkcat_output import read_dlkcat_output from .remove_standard_kcat import remove_standard_kcat @@ -38,7 +36,6 @@ "ec_rxn_keys", "ec_stats_from_brenda", "extract_enzyme_substrate_pairs", - "fetch_open_kinetics_predictor", "format_kcat_source", "fuzzy_kcat_matching", "get_standard_kcat", # deprecated; alias of assign_standard_kcat @@ -58,7 +55,6 @@ "run_dlkcat", "save_kcat_source", "select_kcat_value", # deprecated; alias of apply_kcat_list - "submit_open_kinetics_predictor", "update_kcat_sources", "write_dlkcat_input", ] diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index 40f4b85..636569b 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -23,8 +23,8 @@ from .ec_tree import ECTree from .fuzzy_kcat_matching import fuzzy_kcat_matching from .kcat_sources import format_number, kcat_source_file, save_kcat_source -from .open_kinetics_predictor import fetch_open_kinetics_predictor, submit_open_kinetics_predictor -from .open_kinetics_predictor.parse_output import parse_okp_output +from .open_kinetics_predictor.run import _fetch_open_kinetics_predictor, _submit_open_kinetics_predictor +from .open_kinetics_predictor.parse_output import _parse_okp_output from .read_dlkcat_output import _read_dlkcat_output from .run_dlkcat import _run_dlkcat from .write_dlkcat_input import write_dlkcat_input @@ -369,7 +369,7 @@ def _write_job_file(path: Path, job: dict[str, str]) -> None: def _adopt_shared_result(model: "EcModel", data_dir: Path, method: str, job_file: Path, out_file: Path) -> None: - """Take over a result that ``fetch_open_kinetics_predictor`` stored by + """Take over a result that ``_fetch_open_kinetics_predictor`` stored by itself, in ``data/OKP_output.csv`` with ``data/OKP_job.txt``, only when its job is for this method and its predictions are labelled with this method.""" @@ -377,7 +377,7 @@ def _adopt_shared_result(model: "EcModel", data_dir: Path, method: str, job_file job = _read_job_file(data_dir / "OKP_job.txt") ok = shared.is_file() and "jobId" in job and job.get("method", "").lower() == method.lower() if ok: - sources = [str(x) for x in parse_okp_output(model, shared)["source"]] + sources = [str(x) for x in _parse_okp_output(model, shared)["source"]] predicted = [x for x in sources if x.lower().startswith("okp-") and x.lower() not in _DATABASE_SOURCES] ok = bool(predicted) and all(x.lower() == f"okp-{method}".lower() for x in predicted) if not ok: @@ -473,18 +473,18 @@ def kcats_from_okp( f"No stored result of the current {method} job ({job_file}). Call kcats_from_okp " f"without use_stored to fetch it." ) - kcat_list, done = parse_okp_output(model, out_file), True + kcat_list, done = _parse_okp_output(model, out_file), True else: if submitting: out_file.unlink(missing_ok=True) - job_id = submit_open_kinetics_predictor(model, overwrite=True, method=method, + job_id = _submit_open_kinetics_predictor(model, overwrite=True, method=method, ec_rxns=ec_rxns, adapter=adapter) job = {"jobId": job_id, "method": method, "submittedAt": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")} if ec_rxns is not None: job["ecRxns"] = str(len(set(ec_rxns))) _write_job_file(job_file, job) - done, kcat_list = fetch_open_kinetics_predictor(model, job_id=job["jobId"], wait=wait, + done, kcat_list = _fetch_open_kinetics_predictor(model, job_id=job["jobId"], wait=wait, adapter=adapter, out_file=out_file) if done: job["resultOf"] = job["jobId"] diff --git a/src/geckopy/gather_kcats/merge_kcats.py b/src/geckopy/gather_kcats/merge_kcats.py index aa4b47a..f3e762a 100644 --- a/src/geckopy/gather_kcats/merge_kcats.py +++ b/src/geckopy/gather_kcats/merge_kcats.py @@ -24,7 +24,7 @@ ``source`` after normalisation to lowercase ``snake_case`` (so ``"DLKcat"`` matches ``"dlkcat"`` and ``"Sabio-RK"`` matches ``"sabio_rk"``). A row whose source contains the ``OKP-`` pipeline-stage -tag (``parse_okp_output``'s output, e.g. ``"OKP-BRENDA"``) is matched +tag (``_parse_okp_output``'s output, e.g. ``"OKP-BRENDA"``) is matched and tiered by its underlying source with the tag stripped -- an ``OKP-BRENDA`` row still routes to the database tiers like a bare ``BRENDA`` row would -- while the merged output keeps the original, @@ -58,7 +58,7 @@ #: routed to the ``database_top`` / ``database_bottom`` tiers. _DEFAULT_DATABASE_SOURCES = ("brenda", "sabio_rk") -#: Normalised prefix ``parse_okp_output`` tags a row's source with +#: Normalised prefix ``_parse_okp_output`` tags a row's source with #: (``"OKP-BRENDA"`` normalises to ``"okp_brenda"``) to mark it as #: having come through the OpenKineticsPredictor pipeline stage. _OKP_PREFIX = "okp_" diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/__init__.py b/src/geckopy/gather_kcats/open_kinetics_predictor/__init__.py index 5ccf3fb..7c59317 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/__init__.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/__init__.py @@ -1,22 +1,13 @@ """Submit/fetch kcat predictions via the OpenKineticsPredictor REST API. -Replaces the manual write-CSV -> upload -> download -> parse workflow. +The engine behind ``kcats_from_okp`` and ``kcats_from_dlkcat(via="okp")``: +its submit, fetch, input and output functions are private. """ from __future__ import annotations -from .build_input import build_okp_input_csv from .client import OKPClient, OKPError -from .parse_output import parse_okp_output -from .run import ( - fetch_open_kinetics_predictor, - submit_open_kinetics_predictor, -) __all__ = [ "OKPClient", "OKPError", - "build_okp_input_csv", - "fetch_open_kinetics_predictor", - "parse_okp_output", - "submit_open_kinetics_predictor", ] diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/build_input.py b/src/geckopy/gather_kcats/open_kinetics_predictor/build_input.py index 28af579..0e9c9a5 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/build_input.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/build_input.py @@ -21,7 +21,7 @@ logger = logging.getLogger(__name__) -def build_okp_input_csv( +def _build_okp_input_csv( model: "EcModel", ignore_lists: "DLKcatIgnoreLists", *, @@ -62,6 +62,6 @@ def build_okp_input_csv( for sequence, smiles in zip(unique["sequence"], unique["smiles"]): writer.writerow([sequence, smiles]) - logger.info("build_okp_input_csv: %d unique (sequence, SMILES) pair(s).", + logger.info("_build_okp_input_csv: %d unique (sequence, SMILES) pair(s).", len(unique)) return buffer.getvalue() diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py b/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py index adef38a..e5fb8a3 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py @@ -60,7 +60,7 @@ def _kcat_value(text: str) -> float | None: _PREDICTION_PREFIX_RE = re.compile(r"^Prediction from\s+") -def parse_okp_output( +def _parse_okp_output( model: "EcModel", result: str | Path, ) -> pd.DataFrame: @@ -199,7 +199,7 @@ def parse_okp_output( if unmatched: logger.warning( - "parse_okp_output: %d result row(s) could not be mapped to an " + "_parse_okp_output: %d result row(s) could not be mapped to an " "ec.rxn (sequence and/or SMILES not in the model).", unmatched, ) if not rows: @@ -220,7 +220,7 @@ def parse_okp_output( "origin": pd.array([pd.NA] * n, dtype="Int64"), "similarity": [r[5] for r in rows], }) - logger.info("parse_okp_output: mapped %d kcat row(s).", n) + logger.info("_parse_okp_output: mapped %d kcat row(s).", n) return out diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index e7e9097..a29bc6b 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -2,9 +2,9 @@ Two entry points mirroring the MATLAB submit/fetchOpenKineticsPredictor: -* ``submit_open_kinetics_predictor`` builds the input CSV and submits a +* ``_submit_open_kinetics_predictor`` builds the input CSV and submits a kcat-prediction job, persisting the job id to ``data/OKP_job.txt``. -* ``fetch_open_kinetics_predictor`` checks status; on completion it +* ``_fetch_open_kinetics_predictor`` checks status; on completion it downloads ``data/OKP_output.csv`` and parses it into a kcat_list DataFrame. ``use_stored`` re-parses the saved file without an API call. @@ -30,9 +30,9 @@ import requests from ...databases.dlkcat_ignore_lists import load_dlkcat_ignore_lists -from .build_input import build_okp_input_csv +from .build_input import _build_okp_input_csv from .client import OKPClient, OKPError -from .parse_output import parse_okp_output +from .parse_output import _parse_okp_output if TYPE_CHECKING: from ...adapter import ModelAdapter @@ -44,7 +44,7 @@ _BASE_URL = "https://predictor.openkinetics.org/api/v1" -def submit_open_kinetics_predictor( +def _submit_open_kinetics_predictor( model: "EcModel", *, overwrite: bool = False, @@ -91,7 +91,7 @@ def submit_open_kinetics_predictor( """ from ...adapter import resolve_adapter adapter = resolve_adapter( - model, adapter, purpose="submit_open_kinetics_predictor needs " + model, adapter, purpose="_submit_open_kinetics_predictor needs " "params.path and params.okp", ) data_dir = Path(adapter.params.path) / "data" @@ -106,7 +106,7 @@ def submit_open_kinetics_predictor( else: if ignore_lists is None: ignore_lists = load_dlkcat_ignore_lists(data_dir) - csv_text = build_okp_input_csv( + csv_text = _build_okp_input_csv( model, ignore_lists, ec_rxns=ec_rxns, only_with_smiles=only_with_smiles, ) @@ -130,7 +130,7 @@ def submit_open_kinetics_predictor( return job_id -def fetch_open_kinetics_predictor( +def _fetch_open_kinetics_predictor( model: "EcModel", use_stored: bool = False, *, @@ -175,7 +175,7 @@ def fetch_open_kinetics_predictor( """ from ...adapter import resolve_adapter adapter = resolve_adapter( - model, adapter, purpose="fetch_open_kinetics_predictor needs params.path", + model, adapter, purpose="_fetch_open_kinetics_predictor needs params.path", ) data_dir = Path(adapter.params.path) / "data" out_file = Path(out_file) if out_file is not None else data_dir / "OKP_output.csv" @@ -184,9 +184,9 @@ def fetch_open_kinetics_predictor( if not out_file.is_file(): raise FileNotFoundError( f"use_stored=True but no stored result at {out_file}. " - f"Run fetch_open_kinetics_predictor(use_stored=False) first." + f"Run _fetch_open_kinetics_predictor(use_stored=False) first." ) - return True, parse_okp_output(model, out_file) + return True, _parse_okp_output(model, out_file) api_key = _resolve_api_key(api_key, adapter) if job_id is None: @@ -204,7 +204,7 @@ def fetch_open_kinetics_predictor( out_file.write_text(csv_text, encoding="utf-8") logger.info("OKP job %s completed; result stored at %s.", job_id, out_file) - return True, parse_okp_output(model, csv_text) + return True, _parse_okp_output(model, csv_text) if state == "failed": raise OKPError( @@ -252,7 +252,7 @@ def _read_job_id(meta_file: Path) -> str: if not meta_file.is_file(): raise FileNotFoundError( f"No job_id given and no metadata file at {meta_file}. Run " - f"submit_open_kinetics_predictor first, or pass job_id explicitly." + f"_submit_open_kinetics_predictor first, or pass job_id explicitly." ) for line in meta_file.read_text(encoding="utf-8").splitlines(): stripped = line.strip() diff --git a/tests/test_kcat_sources.py b/tests/test_kcat_sources.py index a0d506b..574c4dc 100644 --- a/tests/test_kcat_sources.py +++ b/tests/test_kcat_sources.py @@ -175,12 +175,12 @@ def test_kcats_from_dlkcat_and_okp_write_files(tmp_path, monkeypatch): assert meta["generator"].startswith("kcats_from_dlkcat") assert set(records["source"]) == {"DLKcat"} - # A result stored by fetch_open_kinetics_predictor alone is taken over + # A result stored by _fetch_open_kinetics_predictor alone is taken over # when its job is for the method and its predictions are of that method. (data / "OKP_job.txt").write_text("jobId: abc123\nmethod: CataPro\n", encoding="utf-8") (data / "OKP_output.csv").write_text("placeholder\n", encoding="utf-8") okp = _kcat_list(full).assign(source="OKP-CataPro", wildcard_level=np.nan, origin=np.nan) - monkeypatch.setattr(kcats_from_module, "parse_okp_output", lambda model, path: okp) + monkeypatch.setattr(kcats_from_module, "_parse_okp_output", lambda model, path: okp) kcat_list, done = kcats_from_okp(full, method="CataPro", use_stored=True) assert done assert (data / "OKP_output_catapro.csv").is_file() @@ -226,9 +226,9 @@ def fake_fetch(model, **kw): def fake_parse(model, path): return _kcat_list(model).assign(source=Path(path).read_text(), wildcard_level=np.nan, origin=np.nan) - monkeypatch.setattr(kcats_from_module, "submit_open_kinetics_predictor", fake_submit) - monkeypatch.setattr(kcats_from_module, "fetch_open_kinetics_predictor", fake_fetch) - monkeypatch.setattr(kcats_from_module, "parse_okp_output", fake_parse) + monkeypatch.setattr(kcats_from_module, "_submit_open_kinetics_predictor", fake_submit) + monkeypatch.setattr(kcats_from_module, "_fetch_open_kinetics_predictor", fake_fetch) + monkeypatch.setattr(kcats_from_module, "_parse_okp_output", fake_parse) assert kcats_from_okp(full, method="CataPro") == (None, False) assert kcats_from_okp(full, method="UniKP", ec_rxns=["R2"]) == (None, False) diff --git a/tests/test_merge_dlkcat_and_fuzzy_kcats.py b/tests/test_merge_dlkcat_and_fuzzy_kcats.py index cd52cc7..e702987 100644 --- a/tests/test_merge_dlkcat_and_fuzzy_kcats.py +++ b/tests/test_merge_dlkcat_and_fuzzy_kcats.py @@ -419,7 +419,7 @@ def _okp_row( eccode: str = "", gene: str = "g1", ) -> dict: - """A row as produced by parse_okp_output: per-row provenance source + """A row as produced by _parse_okp_output: per-row provenance source tagged with the OKP- pipeline-stage prefix, NA wildcard/origin.""" return { "rxn_id": rxn_id, diff --git a/tests/test_open_kinetics_predictor.py b/tests/test_open_kinetics_predictor.py index 0ed19f4..ed5c7e4 100644 --- a/tests/test_open_kinetics_predictor.py +++ b/tests/test_open_kinetics_predictor.py @@ -11,13 +11,12 @@ from geckopy import EcModel, ModelAdapter from geckopy.databases import DLKcatIgnoreLists from geckopy.ec_model.ec_data import EcData -from geckopy.gather_kcats.open_kinetics_predictor import ( - OKPClient, - OKPError, - build_okp_input_csv, - fetch_open_kinetics_predictor, - parse_okp_output, - submit_open_kinetics_predictor, +from geckopy.gather_kcats.open_kinetics_predictor import OKPClient, OKPError +from geckopy.gather_kcats.open_kinetics_predictor.build_input import _build_okp_input_csv +from geckopy.gather_kcats.open_kinetics_predictor.parse_output import _parse_okp_output +from geckopy.gather_kcats.open_kinetics_predictor.run import ( + _fetch_open_kinetics_predictor, + _submit_open_kinetics_predictor, ) @@ -134,11 +133,11 @@ def get(self, url, **kwargs): # --------------------------------------------------------------------------- # -# build_okp_input_csv +# _build_okp_input_csv # --------------------------------------------------------------------------- # def test_build_input_header_and_pairs(): - csv_text = build_okp_input_csv(_ec_model(), _ignore()) + csv_text = _build_okp_input_csv(_ec_model(), _ignore()) lines = csv_text.strip().split("\n") assert lines[0] == "Protein Sequence,Substrate" body = sorted(lines[1:]) @@ -148,22 +147,22 @@ def test_build_input_header_and_pairs(): def test_build_input_dedups_pairs(): # A appears as substrate in both r1 and r2 with the same enzyme S1, # so (S1, CCO) must appear only once. - csv_text = build_okp_input_csv(_ec_model(), _ignore()) + csv_text = _build_okp_input_csv(_ec_model(), _ignore()) assert csv_text.count("S1,CCO") == 1 def test_build_input_only_with_smiles_drops_missing(): - csv_text = build_okp_input_csv(_ec_model(), _ignore(), only_with_smiles=True) + csv_text = _build_okp_input_csv(_ec_model(), _ignore(), only_with_smiles=True) # C and D have no SMILES and are products anyway; only A,B substrates remain. assert "None" not in csv_text # --------------------------------------------------------------------------- # -# parse_okp_output +# _parse_okp_output # --------------------------------------------------------------------------- # def test_parse_maps_back_to_ec_rxns(): - df = parse_okp_output(_ec_model(), _OKP_RESULT_CSV) + df = _parse_okp_output(_ec_model(), _OKP_RESULT_CSV) # S1+CCO (A) -> consumed by r1 and r2 -> 2 rows; S1+CCN (B) -> r1 -> 1 row. assert len(df) == 3 assert set(df["rxn_id"]) == {"r1", "r2"} @@ -179,14 +178,14 @@ def test_parse_maps_back_from_a_reloaded_models_list_valued_smiles(): if met.annotation.get("smiles"): met.annotation["smiles"] = [met.annotation["smiles"]] - df = parse_okp_output(model, _OKP_RESULT_CSV) + df = _parse_okp_output(model, _OKP_RESULT_CSV) assert len(df) == 3 assert set(df["rxn_id"]) == {"r1", "r2"} assert all(s in (["ala"], ["beta"]) for s in df["substrates"]) def test_parse_source_provenance_stripped_and_verbatim(): - df = parse_okp_output(_ec_model(), _OKP_RESULT_CSV) + df = _parse_okp_output(_ec_model(), _OKP_RESULT_CSV) sources = set(df["source"]) # "Prediction from CataPro" -> "CataPro"; "BRENDA" kept verbatim; both # tagged with the OKP- pipeline-stage prefix. @@ -196,7 +195,7 @@ def test_parse_source_provenance_stripped_and_verbatim(): def test_parse_kcat_and_columns(): - df = parse_okp_output(_ec_model(), _OKP_RESULT_CSV) + df = _parse_okp_output(_ec_model(), _OKP_RESULT_CSV) assert list(df.columns) == [ "rxn_id", "source", "eccode", "substrates", "genes", "kcat", "wildcard_level", "origin", "similarity", @@ -215,7 +214,7 @@ def test_parse_no_numeric_kcat_raises(): "NA,Prediction from CataPro,,S1,CCO\n" ) with pytest.raises(ValueError, match="no numeric kcat"): - parse_okp_output(_ec_model(), csv_text) + _parse_okp_output(_ec_model(), csv_text) def test_parse_catrange_class_ranges(): @@ -225,7 +224,7 @@ def test_parse_catrange_class_ranges(): "Predicted kcat range: kcat (1/s),Source kcat,Extra Info kcat,Protein Sequence,Substrate\n" "10 to 100 s^-1,Prediction from CatRange,Predicted range: 10 to 100 s^-1,S1,CCO\n" ) - df = parse_okp_output(_ec_model(), csv_text) + df = _parse_okp_output(_ec_model(), csv_text) assert not df.empty assert all(df["source"] == "OKP-CatRange") assert df["kcat"].iloc[0] == pytest.approx(10 ** 1.5) @@ -252,7 +251,7 @@ def test_kcat_value(text, expected): def test_parse_missing_column_raises(): csv_text = "kcat (1/s),Protein Sequence\n1.0,S1\n" with pytest.raises(ValueError, match="missing expected column"): - parse_okp_output(_ec_model(), csv_text) + _parse_okp_output(_ec_model(), csv_text) def test_parse_unmapped_rows_raise(caplog): @@ -261,7 +260,7 @@ def test_parse_unmapped_rows_raise(caplog): "9.9,Prediction from CataPro,,UNKNOWN_SEQ,CCO\n" ) with pytest.raises(ValueError, match="could be mapped"): - parse_okp_output(_ec_model(), csv_text) + _parse_okp_output(_ec_model(), csv_text) # --------------------------------------------------------------------------- # @@ -317,7 +316,7 @@ def test_submit_writes_csv_and_metadata(tmp_path, monkeypatch): session = _FakeSession([ ("/submit/", _FakeResponse(201, {"jobId": "JOB42", "status": "Pending"})), ]) - job_id = submit_open_kinetics_predictor( + job_id = _submit_open_kinetics_predictor( model, ignore_lists=_ignore(), session=session, ) assert job_id == "JOB42" @@ -333,7 +332,7 @@ def test_submit_missing_key_raises(tmp_path, monkeypatch): model = _ec_model(adapter) monkeypatch.delenv("OKP_API_KEY", raising=False) with pytest.raises(ValueError, match="No OpenKineticsPredictor API key"): - submit_open_kinetics_predictor(model, ignore_lists=_ignore()) + _submit_open_kinetics_predictor(model, ignore_lists=_ignore()) def test_fetch_completed_downloads_and_parses(tmp_path, monkeypatch): @@ -346,7 +345,7 @@ def test_fetch_completed_downloads_and_parses(tmp_path, monkeypatch): ("/status/", _FakeResponse(200, {"status": "Completed"})), ("/result/", _FakeResponse(200, text=_OKP_RESULT_CSV)), ]) - done, df = fetch_open_kinetics_predictor(model, session=session) + done, df = _fetch_open_kinetics_predictor(model, session=session) assert done is True assert len(df) == 3 assert (tmp_path / "data" / "OKP_output.csv").is_file() @@ -363,7 +362,7 @@ def test_fetch_running_without_wait_returns_false(tmp_path, monkeypatch): "progress": {"predictionsMade": 0, "predictionsTotal": 2}})), ]) - done, df = fetch_open_kinetics_predictor(model, session=session) + done, df = _fetch_open_kinetics_predictor(model, session=session) assert done is False assert df is None @@ -376,7 +375,7 @@ def test_fetch_failed_raises(tmp_path, monkeypatch): (tmp_path / "data" / "OKP_job.txt").write_text("jobId: JOB42\n") session = _FakeSession([("/status/", _FakeResponse(200, {"status": "Failed"}))]) with pytest.raises(OKPError, match="failed"): - fetch_open_kinetics_predictor(model, session=session) + _fetch_open_kinetics_predictor(model, session=session) def test_fetch_use_stored_parses_saved_file(tmp_path): @@ -384,7 +383,7 @@ def test_fetch_use_stored_parses_saved_file(tmp_path): model = _ec_model(adapter) (tmp_path / "data").mkdir() (tmp_path / "data" / "OKP_output.csv").write_text(_OKP_RESULT_CSV) - done, df = fetch_open_kinetics_predictor(model, True) + done, df = _fetch_open_kinetics_predictor(model, True) assert done is True assert len(df) == 3 @@ -393,7 +392,7 @@ def test_fetch_use_stored_missing_file_raises(tmp_path): adapter = _adapter(tmp_path) model = _ec_model(adapter) with pytest.raises(FileNotFoundError, match="no stored result"): - fetch_open_kinetics_predictor(model, True) + _fetch_open_kinetics_predictor(model, True) def test_fetch_reads_job_id_from_metadata(tmp_path, monkeypatch): @@ -406,6 +405,19 @@ def test_fetch_reads_job_id_from_metadata(tmp_path, monkeypatch): ("/status/FROMFILE/", _FakeResponse(200, {"status": "Completed"})), ("/result/FROMFILE/", _FakeResponse(200, text=_OKP_RESULT_CSV)), ]) - done, df = fetch_open_kinetics_predictor(model, session=session) + done, df = _fetch_open_kinetics_predictor(model, session=session) assert done is True assert any("FROMFILE" in url for _, url, _ in session.calls) + + +def test_okp_engine_is_private(): + """kcats_from_okp is the entry point; the engine functions are not exported.""" + import geckopy + import geckopy.gather_kcats as gather_kcats + import geckopy.gather_kcats.open_kinetics_predictor as okp + + for name in ("submit_open_kinetics_predictor", "fetch_open_kinetics_predictor", + "build_okp_input_csv", "parse_okp_output"): + for module in (geckopy, gather_kcats, okp): + assert not hasattr(module, name) + assert name not in getattr(module, "__all__", []) From 6f0eab4f149da1a922b45f9dbb7d1869c647ee11 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:58:46 +0100 Subject: [PATCH 27/42] doc: mark the low-level kcat functions in their docstrings --- src/geckopy/ec_model/pipeline/fill_kcats.py | 3 +++ src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py | 3 +++ src/geckopy/gather_kcats/get_standard_kcat.py | 3 +++ src/geckopy/gather_kcats/merge_kcats.py | 3 +++ src/geckopy/gather_kcats/remove_standard_kcat.py | 3 +++ src/geckopy/gather_kcats/select_kcat_value.py | 5 ++++- 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/geckopy/ec_model/pipeline/fill_kcats.py b/src/geckopy/ec_model/pipeline/fill_kcats.py index 8e2fdcd..e0254ca 100644 --- a/src/geckopy/ec_model/pipeline/fill_kcats.py +++ b/src/geckopy/ec_model/pipeline/fill_kcats.py @@ -37,6 +37,9 @@ def fill_kcats_from_isozymes( ) -> None: """Fill in missing kcats by combining known kcats across sibling isozymes. + Low-level: ``collect_kcats`` and ``assign_kcats`` run this function; call + it directly only to change kcats by hand. + Two reactions are siblings if their ``ec.rxns`` IDs are identical after stripping any ``_EXP_`` suffix. The ``_REV`` suffix is NOT stripped: forward and reverse reactions share chemistry but diff --git a/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py b/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py index 25193b7..4fc0573 100644 --- a/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py +++ b/src/geckopy/gather_kcats/fuzzy_kcat_matching/__init__.py @@ -87,6 +87,9 @@ def fuzzy_kcat_matching( """Match each ec reaction to BRENDA kcat data with progressive relaxation of EC, substrate, and organism specificity. + Low-level: ``kcats_from_brenda`` runs this function; call it directly + only to inspect or change the BRENDA matches by hand. + Ported from GECKO MATLAB: src/gather_kcats/fuzzyKcatMatching.m. diff --git a/src/geckopy/gather_kcats/get_standard_kcat.py b/src/geckopy/gather_kcats/get_standard_kcat.py index 9edee97..ae97782 100644 --- a/src/geckopy/gather_kcats/get_standard_kcat.py +++ b/src/geckopy/gather_kcats/get_standard_kcat.py @@ -40,6 +40,9 @@ def assign_standard_kcat( """Add a "standard" pseudoenzyme to ``model`` and assign it to reactions without enzyme constraints. + Low-level: ``collect_kcats`` and ``assign_kcats`` run this function; call + it directly only to change kcats by hand. + Ported from GECKO MATLAB: src/gather_kcats/getStandardKcat.m. diff --git a/src/geckopy/gather_kcats/merge_kcats.py b/src/geckopy/gather_kcats/merge_kcats.py index f3e762a..5c28504 100644 --- a/src/geckopy/gather_kcats/merge_kcats.py +++ b/src/geckopy/gather_kcats/merge_kcats.py @@ -101,6 +101,9 @@ def merge_kcats( ) -> pd.DataFrame: """Merge any number of kcat lists, keeping the best source per reaction. + Low-level: ``collect_kcats`` runs this function; call it directly only + to merge kcat lists by hand. + All input lists are concatenated and each row is assigned a tier (see the module docstring). For every reaction the highest-priority tier present is kept, and **all** rows of that tier for that reaction diff --git a/src/geckopy/gather_kcats/remove_standard_kcat.py b/src/geckopy/gather_kcats/remove_standard_kcat.py index 5dc10f9..28ebdf6 100644 --- a/src/geckopy/gather_kcats/remove_standard_kcat.py +++ b/src/geckopy/gather_kcats/remove_standard_kcat.py @@ -25,6 +25,9 @@ def remove_standard_kcat(model: "EcModel") -> None: """Remove the standard pseudoenzyme and any standard kcat assignments from ``model``. + Low-level: it undoes ``assign_standard_kcat``, which ``collect_kcats`` + and ``assign_kcats`` run; call it directly only to change kcats by hand. + Ported from GECKO MATLAB: src/gather_kcats/removeStandardKcat.m. diff --git a/src/geckopy/gather_kcats/select_kcat_value.py b/src/geckopy/gather_kcats/select_kcat_value.py index b4c8177..6c5f9a8 100644 --- a/src/geckopy/gather_kcats/select_kcat_value.py +++ b/src/geckopy/gather_kcats/select_kcat_value.py @@ -56,8 +56,11 @@ def apply_kcat_list( ) -> list[str]: """Choose one kcat value per reaction and write it to ``model.ec.kcat``. + Low-level: ``collect_kcats`` and ``assign_kcats`` run this function; call + it directly only to change kcats by hand. + Ported from GECKO MATLAB: - src/deprecated/selectKcatValue.m. + src/gather_kcats/assignKcatValues.m. For each reaction with one or more entries in ``kcat_list``, aggregate the candidate kcats per ``criteria`` (``"max"`` is the From 1c6663f1af9ddfadc63435adfef87b1908cb96d5 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 22:59:27 +0100 Subject: [PATCH 28/42] feat: use KinForm-H as the default okp method --- CHANGELOG.md | 1 + src/geckopy/adapter/adapterParameters.tsv | 2 +- src/geckopy/adapter/params.py | 2 +- src/geckopy/gather_kcats/open_kinetics_predictor/run.py | 2 +- tests/test_adapter.py | 6 ++++++ tests/test_open_kinetics_predictor.py | 6 +++--- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 421989c..5ed912a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. * `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. `write_dlkcat_input` stays public. * `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. diff --git a/src/geckopy/adapter/adapterParameters.tsv b/src/geckopy/adapter/adapterParameters.tsv index b0c1144..7d9a9ff 100644 --- a/src/geckopy/adapter/adapterParameters.tsv +++ b/src/geckopy/adapter/adapterParameters.tsv @@ -29,7 +29,7 @@ evotune.max_generations evotune.maxGenerations 150 Maximum number of CMA-ES gene evotune.max_growth_weight evotune.maxGrowthWeight 1.0 Weight of the maximum-growth RMSE against the flux RMSE evotune.prior_penalty_weight evotune.priorPenaltyWeight 0.03 Weight of the prior term in the search objective evotune.tie_isozymes evotune.tieIsozymes true Give isozyme copies of one reaction a single kcat -okp.method okp.method "CataPro" OpenKineticsPredictor method +okp.method okp.method "KinForm-H" OpenKineticsPredictor method okp.handle_long_sequences okp.handleLongSequences "truncate" How OpenKineticsPredictor handles sequences that are too long okp.include_similarity_columns okp.includeSimilarityColumns true Return similarity to the method's training data okp.canonicalize_substrates okp.canonicalizeSubstrates true Canonicalize substrate SMILES before prediction diff --git a/src/geckopy/adapter/params.py b/src/geckopy/adapter/params.py index 0e104a1..acb8b29 100644 --- a/src/geckopy/adapter/params.py +++ b/src/geckopy/adapter/params.py @@ -288,7 +288,7 @@ class OkpParams(BaseModel): model_config = ConfigDict(extra="forbid") method: str = Field( - default="CataPro", + default="KinForm-H", description=( "Predictor method: CataPro, CatPred, CatRange, DLKcat, EITLEM, " "KinForm-H, KinForm-L, OmniESI, OmniESI-O2DENet, TurNup, UniKP " diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index a29bc6b..9c398c5 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -71,7 +71,7 @@ def _submit_open_kinetics_predictor( ``data/okpApiKey.txt`` (see the module docstring). method Predictor method. Resolved as: this arg -> ``params.okp.method`` - -> ``'CataPro'``. + -> ``'KinForm-H'``. ec_rxns Optional iterable of reaction IDs to include (default all). only_with_smiles diff --git a/tests/test_adapter.py b/tests/test_adapter.py index e61723a..eae193b 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -266,3 +266,9 @@ def test_uniprot_table_lookup_reads_quoted_cells_and_first_duplicate(tmp_path): adapter = ModelAdapter.from_folder(folder) result = adapter.get_uniprot_ids_from_table(["YAL001C", "YAL002W"]) assert result == ["P12345", "P67890"] + + +def test_default_okp_method_is_kinform_h(): + from geckopy.adapter.params import ModelParameters + + assert ModelParameters.model_fields["okp"].get_default(call_default_factory=True).method == "KinForm-H" diff --git a/tests/test_open_kinetics_predictor.py b/tests/test_open_kinetics_predictor.py index ed5c7e4..f7f2dc7 100644 --- a/tests/test_open_kinetics_predictor.py +++ b/tests/test_open_kinetics_predictor.py @@ -324,7 +324,7 @@ def test_submit_writes_csv_and_metadata(tmp_path, monkeypatch): assert (data_dir / "OKP.csv").read_text().startswith("Protein Sequence,Substrate") meta = (data_dir / "OKP_job.txt").read_text() assert "jobId: JOB42" in meta - assert "method: CataPro" in meta + assert "method: KinForm-H" in meta def test_submit_missing_key_raises(tmp_path, monkeypatch): @@ -340,7 +340,7 @@ def test_fetch_completed_downloads_and_parses(tmp_path, monkeypatch): model = _ec_model(adapter) monkeypatch.setenv("OKP_API_KEY", "ak_test") (tmp_path / "data").mkdir() - (tmp_path / "data" / "OKP_job.txt").write_text("jobId: JOB42\nmethod: CataPro\n") + (tmp_path / "data" / "OKP_job.txt").write_text("jobId: JOB42\nmethod: KinForm-H\n") session = _FakeSession([ ("/status/", _FakeResponse(200, {"status": "Completed"})), ("/result/", _FakeResponse(200, text=_OKP_RESULT_CSV)), @@ -400,7 +400,7 @@ def test_fetch_reads_job_id_from_metadata(tmp_path, monkeypatch): model = _ec_model(adapter) monkeypatch.setenv("OKP_API_KEY", "ak_test") (tmp_path / "data").mkdir() - (tmp_path / "data" / "OKP_job.txt").write_text("jobId: FROMFILE\nmethod: CataPro\n") + (tmp_path / "data" / "OKP_job.txt").write_text("jobId: FROMFILE\nmethod: KinForm-H\n") session = _FakeSession([ ("/status/FROMFILE/", _FakeResponse(200, {"status": "Completed"})), ("/result/FROMFILE/", _FakeResponse(200, text=_OKP_RESULT_CSV)), From b6d91b3097567bdcfbed48a947db23a331376f3e Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:01:04 +0100 Subject: [PATCH 29/42] feat: use KinForm-H as the default OKP method of the presets --- CHANGELOG.md | 1 + src/geckopy/gather_kcats/kcat_assignment.py | 10 +++++----- tests/test_ec_tree.py | 2 +- tests/test_kcat_assignment.py | 9 +++++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ed912a..5504854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. * The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. * `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. `write_dlkcat_input` stays public. diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index 0a561e5..ed94cc9 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -40,10 +40,10 @@ #: OKP methods that read whole reactions (substrate set, and products for TurNup) #: instead of enzyme-substrate pairs; kcats_from_okp submits pairs only. REACTION_OKP_METHODS = ("CatPred", "TurNup") -#: The OKP methods of the ``okp`` source group by default: those that read -#: enzyme-substrate pairs and have combination parameters (CatRange predicts -#: classes, and has none). -DEFAULT_OKP_METHODS = tuple(m for m in OKP_METHODS if m != "CatRange" and m not in REACTION_OKP_METHODS) +#: The OKP methods of the ``okp`` source group by default; a project lists others in +#: ``[kcat_sources.okp] methods`` (those that read enzyme-substrate pairs and have +#: combination parameters: not CatRange, which predicts classes, nor the reaction methods). +DEFAULT_OKP_METHODS = ("KinForm-H",) def okp_file(method: str) -> str: @@ -306,7 +306,7 @@ def collect_kcats( ``combined (sd=, n=)``. In both, ``okp`` stands for the OKP methods of ``[kcat_sources.okp] - methods`` (default: all methods with combination parameters). + methods`` (default: KinForm-H). The presets: diff --git a/tests/test_ec_tree.py b/tests/test_ec_tree.py index 1fa157b..8f538e7 100644 --- a/tests/test_ec_tree.py +++ b/tests/test_ec_tree.py @@ -139,7 +139,7 @@ def test_brenda_ec_tree_preset(tmp_path): table[["ec", "kcat"]].to_csv(f, sep="\t", index=False) report = update_kcat_sources(model, dry_run=True, adapter=adapter) assert list(report["source"])[:3] == ["custom", "brenda", "ec_tree"] - assert all(s.startswith("okp_") for s in report["source"][3:]) and len(report) == 11 + assert list(report["source"][3:]) == ["okp_kinform_h"] brenda = kcats_from_brenda(model, aggregation="median", adapter=adapter) kcats_from_ec_tree(model, adapter=adapter) decisions, rep, decided = collect_kcats(model, adapter=adapter, write_report=False) diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index c3963ba..dde1c9f 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -246,3 +246,12 @@ def fake_okp(model, **kwargs): report = update_kcat_sources(base, adapter=adapter).set_index("source") assert report.loc["dlkcat", "status"] == "failed" assert "API key" in report.loc["dlkcat", "note"] + + +def test_default_okp_methods_are_kinform_h_and_a_project_list_is_kept(tmp_path): + from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, okp_methods + + _, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "okp"\n') + assert okp_methods(kcat_assignment_config(adapter)) == ["KinForm-H"] + adapter.params.kcat_sources["okp"] = {"methods": ["CataPro", "UniKP"]} + assert okp_methods(kcat_assignment_config(adapter)) == ["CataPro", "UniKP"] From 1644b67ebdea7bdaf0ec2bb891df4c504b9779bf Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:11:32 +0100 Subject: [PATCH 30/42] feat: deprecate write_dlkcat_input in favour of kcats_from_dlkcat --- CHANGELOG.md | 3 ++- docs/migrating_from_gecko_matlab.md | 2 +- src/geckopy/__init__.py | 19 +++++++------ src/geckopy/gather_kcats/__init__.py | 6 ++--- src/geckopy/gather_kcats/kcats_from.py | 4 +-- .../gather_kcats/write_dlkcat_input.py | 27 +++++++++++++++++++ tests/test_deprecated_aliases.py | 11 ++++++++ tests/test_kcat_sources.py | 2 +- 8 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5504854..35f6b7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,11 @@ ## Unreleased +* `write_dlkcat_input` is a deprecated alias that warns with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` writes the DLKcat input with the same code, as GECKO's `kcatsFromDLKcat` does after `writeDLKcatInput` became its local function. `extract_enzyme_substrate_pairs` stays public. * The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. * The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. -* `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. `write_dlkcat_input` stays public. +* `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. * `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index d38563b..2dd2640 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -243,7 +243,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `removeStandardKcat` | `remove_standard_kcat` | — | | `runDLKcat` | `run_dlkcat` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` run DLKcat. | | `selectKcatValue` | `apply_kcat_list` | **`[Py]`** Renamed; `select_kcat_value` kept as deprecated alias. | -| `writeDLKcatInput` | `write_dlkcat_input` | **`[3→4]`** Bounds-safe when `ec_rxns` is a subset. | +| `writeDLKcatInput` | `write_dlkcat_input` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` write the input. Bounds-safe when `ec_rxns` is a subset. | | `kcatsFromBrenda` | `kcats_from_brenda` | Fuzzy BRENDA matching, kept as `data/kcats/brenda.tsv`. Loads the BRENDA data and KEGG distances from the adapter unless passed in. | | `kcatsFromDLKcat` | `kcats_from_dlkcat` | DLKcat, kept as `data/kcats/dlkcat.tsv`. `via` is `"okp"` (the DLKcat method of OpenKineticsPredictor), `"file"` (`data/DLKcat.tsv`) or `"docker"` (a local run, `run=True`); returns `(kcat_list, done)` as `kcats_from_okp`. | | `kcatsFromOKP` | `kcats_from_okp` | One OpenKineticsPredictor method, kept as `data/kcats/okp_.tsv`; submits a job when there is none, writes the file once it is done. Returns `(kcat_list, done)`. | diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index 526b987..0479f9e 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -11,10 +11,9 @@ - **Project setup**: ``ModelAdapter`` - **Build**: ``load_conventional_gem``, ``make_ec_model`` -- **Enrich kcats**: ``fuzzy_kcat_matching``, ``read_dlkcat_output``, - ``merge_dlkcat_and_fuzzy_kcats``, ``apply_kcat_list``, - ``apply_custom_kcats``, ``fill_kcats_from_isozymes``, - ``assign_standard_kcat``, ``apply_kcat_constraints`` +- **Enrich kcats**: ``update_kcat_sources`` (``kcats_from_brenda``, + ``kcats_from_dlkcat``, ``kcats_from_okp``, ...), ``collect_kcats``, + ``assign_kcats`` - **Set the protein budget**: ``calculate_f_factor``, ``set_prot_pool_size``, ``fit_sigma`` - **Curate**: ``set_kcat_for_reactions``, ``sensitivity_tuning`` @@ -96,15 +95,15 @@ load_kcat_source, merge_dlkcat_and_fuzzy_kcats, # deprecated; alias of merge_kcats merge_kcats, - read_dlkcat_output, + read_dlkcat_output, # deprecated; use kcats_from_dlkcat(via="file") read_kcat_source, remove_standard_kcat, review_assignment, - run_dlkcat, + run_dlkcat, # deprecated; use kcats_from_dlkcat(via="docker") save_kcat_source, select_kcat_value, # deprecated; alias of apply_kcat_list update_kcat_sources, - write_dlkcat_input, + write_dlkcat_input, # deprecated; use kcats_from_dlkcat(via="docker") ) # get_enzyme_data @@ -229,14 +228,14 @@ "load_kcat_source", "merge_dlkcat_and_fuzzy_kcats", # deprecated; alias of merge_kcats "merge_kcats", - "read_dlkcat_output", + "read_dlkcat_output", # deprecated; use kcats_from_dlkcat(via="file") "read_kcat_source", "remove_standard_kcat", "review_assignment", - "run_dlkcat", + "run_dlkcat", # deprecated; use kcats_from_dlkcat(via="docker") "save_kcat_source", "update_kcat_sources", - "write_dlkcat_input", + "write_dlkcat_input", # deprecated; use kcats_from_dlkcat(via="docker") # get_enzyme_data "copy_ec_to_gem", "fill_eccodes_from_database", diff --git a/src/geckopy/gather_kcats/__init__.py b/src/geckopy/gather_kcats/__init__.py index c89daa4..ffe121a 100644 --- a/src/geckopy/gather_kcats/__init__.py +++ b/src/geckopy/gather_kcats/__init__.py @@ -49,12 +49,12 @@ "merge_kcats", "normalize_source", "read_kcat_source", - "read_dlkcat_output", + "read_dlkcat_output", # deprecated; use kcats_from_dlkcat(via="file") "remove_standard_kcat", "review_assignment", - "run_dlkcat", + "run_dlkcat", # deprecated; use kcats_from_dlkcat(via="docker") "save_kcat_source", "select_kcat_value", # deprecated; alias of apply_kcat_list "update_kcat_sources", - "write_dlkcat_input", + "write_dlkcat_input", # deprecated; use kcats_from_dlkcat(via="docker") ] diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index 636569b..65e036b 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -27,7 +27,7 @@ from .open_kinetics_predictor.parse_output import _parse_okp_output from .read_dlkcat_output import _read_dlkcat_output from .run_dlkcat import _run_dlkcat -from .write_dlkcat_input import write_dlkcat_input +from .write_dlkcat_input import _write_dlkcat_input if TYPE_CHECKING: from ..adapter import ModelAdapter @@ -325,7 +325,7 @@ def kcats_from_dlkcat( "A local DLKcat run needs Docker, which was not found. Install it from " "https://www.docker.com, or use via='okp'." ) - write_dlkcat_input(model, dlkcat_file, load_dlkcat_ignore_lists(data_dir), + _write_dlkcat_input(model, dlkcat_file, load_dlkcat_ignore_lists(data_dir), ec_rxns=ec_rxns, overwrite=True) _run_dlkcat(dlkcat_file) run_file.write_text(f"ecRxns: {len(set(ec_rxns))}\n" if ec_rxns is not None else "", diff --git a/src/geckopy/gather_kcats/write_dlkcat_input.py b/src/geckopy/gather_kcats/write_dlkcat_input.py index ef2084c..988b8a4 100644 --- a/src/geckopy/gather_kcats/write_dlkcat_input.py +++ b/src/geckopy/gather_kcats/write_dlkcat_input.py @@ -1,5 +1,8 @@ """Write a DLKcat-compatible TSV input file for an ec model. +``kcats_from_dlkcat(..., via="docker")`` writes it; ``write_dlkcat_input`` is +a deprecated alias. + Ported from GECKO MATLAB: src/gather_kcats/writeDLKcatInput.m. """ @@ -7,6 +10,7 @@ import logging import re +import warnings from pathlib import Path from typing import TYPE_CHECKING, Iterable, Optional @@ -36,6 +40,29 @@ def write_dlkcat_input( ec_rxns: Optional[Iterable[str]] = None, only_with_smiles: bool = True, overwrite: bool = False, +) -> pd.DataFrame: + """Deprecated: use ``kcats_from_dlkcat(..., via="docker")``. + + Writes the file as :func:`_write_dlkcat_input` does, after a + ``DeprecationWarning``. + """ + warnings.warn( + 'write_dlkcat_input is deprecated; use kcats_from_dlkcat(..., via="docker").', + DeprecationWarning, + stacklevel=2, + ) + return _write_dlkcat_input(model, output_path, ignore_lists, ec_rxns=ec_rxns, + only_with_smiles=only_with_smiles, overwrite=overwrite) + + +def _write_dlkcat_input( + model: "EcModel", + output_path: str | Path, + ignore_lists: "DLKcatIgnoreLists", + *, + ec_rxns: Optional[Iterable[str]] = None, + only_with_smiles: bool = True, + overwrite: bool = False, ) -> pd.DataFrame: """Write a DLKcat-compatible TSV input file. diff --git a/tests/test_deprecated_aliases.py b/tests/test_deprecated_aliases.py index 14d8798..cb30f44 100644 --- a/tests/test_deprecated_aliases.py +++ b/tests/test_deprecated_aliases.py @@ -205,3 +205,14 @@ def test_dlkcat_entry_points_are_deprecated_aliases(monkeypatch, tmp_path): _assert_deprecated(run_module.run_dlkcat, tmp_path / "in.tsv", expected_substring='via="docker"') _assert_deprecated(read_module.read_dlkcat_output, None, tmp_path / "out.tsv", expected_substring='via="file"') assert seen == [("run", tmp_path / "in.tsv"), ("read", tmp_path / "out.tsv")] + + +def test_write_dlkcat_input_is_a_deprecated_alias(monkeypatch, tmp_path): + import importlib + + module = importlib.import_module("geckopy.gather_kcats.write_dlkcat_input") + seen = [] + monkeypatch.setattr(module, "_write_dlkcat_input", lambda model, path, ignore, **kwargs: seen.append(path)) + _assert_deprecated(module.write_dlkcat_input, None, tmp_path / "DLKcat.tsv", None, + expected_substring='via="docker"') + assert seen == [tmp_path / "DLKcat.tsv"] diff --git a/tests/test_kcat_sources.py b/tests/test_kcat_sources.py index 574c4dc..f7cc2e3 100644 --- a/tests/test_kcat_sources.py +++ b/tests/test_kcat_sources.py @@ -458,7 +458,7 @@ def test_kcats_from_dlkcat_run_is_docker(tmp_path, monkeypatch): kcats_from_dlkcat(full, via="docker") ran = [] monkeypatch.setattr(kcats_from_module.shutil, "which", lambda name: "/bin/docker") - monkeypatch.setattr(kcats_from_module, "write_dlkcat_input", lambda *a, **k: ran.append("write")) + monkeypatch.setattr(kcats_from_module, "_write_dlkcat_input", lambda *a, **k: ran.append("write")) monkeypatch.setattr(kcats_from_module, "_run_dlkcat", lambda path: ran.append("run")) dlkcat = _kcat_list(full).assign(source="DLKcat", wildcard_level=np.nan, origin=np.nan) monkeypatch.setattr(kcats_from_module, "_read_dlkcat_output", lambda model, path: dlkcat) From 9f539e7ed3b17a8a8357787123e13b8caf591760 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:15:07 +0100 Subject: [PATCH 31/42] fix: return converged false when greedy relaxation runs out --- CHANGELOG.md | 2 +- docs/migrating_from_gecko_matlab.md | 2 +- docs/relaxation_methods.md | 5 +++-- .../limit_proteins/relax_proteomics_greedy.py | 10 +++++++++- tests/test_relax_proteomics_greedy.py | 12 ++++++++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35f6b7f..fe65e5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ * `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. * `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. * `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. -* `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed and returns a converged result when that relaxation reaches `minimal_growth`; it raised `RuntimeError` after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. +* `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed: it returns a converged result when that relaxation reaches `minimal_growth`, and a result with `converged=False` and a warning when no candidate is left. It raises `RuntimeError` only when `max_iterations` relaxations leave growth below the target and candidates remain; it raised after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. * `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 2dd2640..7d71df0 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -299,7 +299,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `getIsozymeCoverage` | `get_isozyme_coverage` | **`[Py]`** Returns the coverage `DataFrame` (columns `rxn`, `n_entries`, `n_measured`, `n_unmeasured`, `measured_enzymes`, `unmeasured_enzymes`, `shared_enzymes`) and the list of zeroable enzymes. Raises `NotImplementedError` on gecko-light. | | `updateProtPool` | *(not ported)* | Obsolete since GECKO 3.2.0. Use `set_prot_pool_size` instead. | | `zeroUnmeasuredIsozymes` | `zero_unmeasured_isozymes` | **`[Py]`** Lowers the usage upper bounds in place and returns the changed enzymes as a `DataFrame` (`uniprot`, `new_upper_bound`). Method `'detectionLimit'` is `"detection_limit"`. | -| `relaxProteomicsGreedy` | `relax_proteomics_greedy` | Alternative to `flexibilize_enz_concs`: until growth reaches the target, sets the usage upper bound of the constrained enzyme with the largest absolute shadow price of its `prot_` metabolite to the default bound, one enzyme per iteration. **`[Py]`** Returns a `GreedyRelaxResult` (`converged=False` when no candidate is left) and changes the model in place; raises `RuntimeError` when `max_iterations` relaxations do not reach the target. | +| `relaxProteomicsGreedy` | `relax_proteomics_greedy` | Alternative to `flexibilize_enz_concs`: until growth reaches the target, sets the usage upper bound of the constrained enzyme with the largest absolute shadow price of its `prot_` metabolite to the default bound, one enzyme per iteration. **`[Py]`** Returns a `GreedyRelaxResult` (`converged=False` when no candidate is left) and changes the model in place; raises `RuntimeError` when `max_iterations` relaxations do not reach the target and candidates remain. | ### 5.6 Utilities (`src/utilities`) diff --git a/docs/relaxation_methods.md b/docs/relaxation_methods.md index 9506eee..a2bd6d3 100644 --- a/docs/relaxation_methods.md +++ b/docs/relaxation_methods.md @@ -21,7 +21,7 @@ the per-complex options for all of them are described at the end. | Result stringency | **tight** (minimal, proteomics-faithful relaxation) | **loose** (tends to over-relax) | | Pool fallback | yes — relaxes `prot_pool_exchange` when no single enzyme helps | no | | Scope | measured enzymes only (`~isnan(concs)`) | any currently-constrained enzyme (`ub < default`) | -| Non-convergence | returns a partial `FlexEnzResult` + warning (global iteration cap) | returns `converged=False` when no candidate is left; **raises** `RuntimeError` when `max_iterations` relaxations leave growth below the target | +| Non-convergence | returns a partial `FlexEnzResult` + warning (global iteration cap) | returns `converged=False` when no candidate is left; **raises** `RuntimeError` when `max_iterations` relaxations leave growth below the target and candidates remain | | Result type | `FlexEnzResult` (relaxed enzymes, per-enzyme `frequence`, ratios) | `GreedyRelaxResult` (relaxed→orig-conc dict, step trace, `converged`) | ## Interpretation @@ -38,7 +38,8 @@ the per-complex options for all of them are described at the end. ranks all enzymes by shadow price, and fully unconstraining the worst offender usually moves growth a lot. The cost is that it **over-relaxes** (no tighten-back), so the resulting model is less constrained than necessary, and - it raises when `max_iterations` relaxations do not reach the target. + it raises when `max_iterations` relaxations do not reach the target while + candidates remain. ## Recommendation diff --git a/src/geckopy/limit_proteins/relax_proteomics_greedy.py b/src/geckopy/limit_proteins/relax_proteomics_greedy.py index 6235c54..e7840f0 100644 --- a/src/geckopy/limit_proteins/relax_proteomics_greedy.py +++ b/src/geckopy/limit_proteins/relax_proteomics_greedy.py @@ -79,7 +79,9 @@ def relax_proteomics_greedy( all currently proteomics-constrained enzymes. max_iterations Safety cap on the number of relaxations; raises ``RuntimeError`` - when growth is still below ``minimal_growth`` after that many. + when growth is still below ``minimal_growth`` after that many and + candidates remain. When no candidate is left, a warning is logged + and the result has ``converged=False``. default_upper_bound Value to restore on a relaxed usage reaction. Matches the default set by ``add_protein_usage_reactions``. @@ -149,6 +151,12 @@ def relax_proteomics_greedy( relaxed=relaxed, trace=trace, final_growth=float(growth), converged=True, ) + if not candidates: + logger.warning("No more candidates to relax; stopping") + return GreedyRelaxResult( + relaxed=relaxed, trace=trace, + final_growth=float(growth), converged=False, + ) raise RuntimeError( f"Did not converge in {max_iterations} iterations " f"(final growth {growth:.4g} < target {minimal_growth:.4g})" diff --git a/tests/test_relax_proteomics_greedy.py b/tests/test_relax_proteomics_greedy.py index 2f9ef2d..2a27ab5 100644 --- a/tests/test_relax_proteomics_greedy.py +++ b/tests/test_relax_proteomics_greedy.py @@ -101,6 +101,18 @@ def test_target_reached_by_the_last_allowed_relaxation_converges(): assert len(result.relaxed) == needed +def test_last_relaxation_using_up_the_candidates_returns_not_converged(caplog): + """As relaxProteomicsGreedy: no candidate left after the last allowed + relaxation gives converged=False with a warning, not an error.""" + model, _ = _ec_with_tight_concs() + only = {model.ec.enzymes[0]} + with caplog.at_level("WARNING"): + result = relax_proteomics_greedy(model, minimal_growth=1e9, enzyme_set=only, max_iterations=1) + assert not result.converged + assert list(result.relaxed) == list(only) + assert "No more candidates" in caplog.text + + def test_max_iterations_raises(): """Setting max_iterations=1 on a model needing more raises.""" model, _ = _ec_with_tight_concs() From 8142b551a18753b93d4c865603c83f6e92ee6b6d Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:15:23 +0100 Subject: [PATCH 32/42] fix: raise in fit_sigma for any growth rate not above 0 --- CHANGELOG.md | 2 +- src/geckopy/kcat_tuning/sigma_fitter.py | 6 +++--- tests/test_sigma_fitter.py | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe65e5d..67cb9ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ * `geckopy uniprot-download` keeps reviewed and unreviewed entries by default, as the adapter default `uniprot.reviewed = false` and GECKO's `loadDatabases` do; `--reviewed-only` keeps the reviewed (Swiss-Prot) entries only. The option `--include-unreviewed` is removed; the command kept only reviewed entries by default. * `apply_complex_data` reports up to two proposed complexes for a reaction, the closest superset and the best partial match, as GECKO's `applyComplexData` does; it reported the partial match only when there was no superset. * `collect_kcats` returns the model with the decided kcats before `apply_kcat_constraints`, also for the kcats filled from isozymes, as GECKO's `collectKcats` does; it applied the constraints of the isozyme-filled reactions to the returned copy. `assign_kcats` gives the same model as before. -* `fit_sigma` raises `ValueError` for a target growth rate of 0, before it changes the model, as GECKO's `sigmaFitter` does; it fitted sigma to the smallest growth. +* `fit_sigma` raises `ValueError` for a target growth rate that is not above 0 (0, negative or NaN), before it changes the model, as GECKO's `sigmaFitter` raises `sigmaFitter:growthRate`; for 0 it fitted sigma to the smallest growth. * `relax_proteomics_greedy` checks the growth after the last relaxation it is allowed: it returns a converged result when that relaxation reaches `minimal_growth`, and a result with `converged=False` and a warning when no candidate is left. It raises `RuntimeError` only when `max_iterations` relaxations leave growth below the target and candidates remain; it raised after `max_iterations` relaxations without checking the last one. GECKO's `relaxProteomicsGreedy` does the same. * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index 167a181..1bff697 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -155,7 +155,7 @@ def fit_sigma( ------ ValueError If ``model.adapter`` is None and any default is needed; if - ``growth_rate`` is 0; if ``n_sigma_steps`` is < 1; or if ``method`` + ``growth_rate`` is not above 0 (also NaN); if ``n_sigma_steps`` is < 1; or if ``method`` is unknown. """ if n_sigma_steps < 1: @@ -175,8 +175,8 @@ def fit_sigma( if growth_rate is None: growth_rate = float(params.gr_exp) - if growth_rate == 0: - raise ValueError("growth_rate is 0; fit_sigma needs a target growth rate other than 0.") + if not growth_rate > 0: # also NaN + raise ValueError(f"growth_rate is {growth_rate}; fit_sigma needs a target growth rate above 0.") if p_tot is None: p_tot = float(params.p_tot) if f is None: diff --git a/tests/test_sigma_fitter.py b/tests/test_sigma_fitter.py index 372e9bd..dde8130 100644 --- a/tests/test_sigma_fitter.py +++ b/tests/test_sigma_fitter.py @@ -86,12 +86,13 @@ def test_n_sigma_steps_zero_raises(tmp_path): fit_sigma(model, n_sigma_steps=0) -def test_zero_growth_rate_raises_before_changing_the_model(tmp_path): +@pytest.mark.parametrize("growth_rate", [0.0, -0.1, float("nan")]) +def test_growth_rate_not_above_zero_raises_before_changing_the_model(tmp_path, growth_rate): adapter = _adapter(tmp_path) model = _build_pool_only_model(adapter) bounds = [r.bounds for r in model.reactions] - with pytest.raises(ValueError, match="growth_rate is 0"): - fit_sigma(model, growth_rate=0.0) + with pytest.raises(ValueError, match="above 0"): + fit_sigma(model, growth_rate=growth_rate) assert [r.bounds for r in model.reactions] == bounds From 9862072a5a33cd57e367aa1c2efb498f1b581809 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:15:36 +0100 Subject: [PATCH 33/42] doc: state the okp methods of the preset comparison in collect_kcats --- src/geckopy/gather_kcats/kcat_assignment.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index ed94cc9..8130454 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -333,7 +333,8 @@ def collect_kcats( than the BRENDA median at origins 2 and 4 and with EC wildcards and as close at origins 1 and 3; the EC tree combined with the OKP methods was slightly closer than the EC tree, and the OKP methods combined as close. - In ecModels, values combined with predictions set high-turnover enzymes + That comparison combined the eight OKP methods with combination + parameters; the default ``okp`` group is now KinForm-H alone. In ecModels, values combined with predictions set high-turnover enzymes such as ATP synthase too low, which is why the default uses predictions only where there is no value from measurements. The BRENDA aggregation is ``[kcat_sources.brenda] aggregation`` (see :func:`update_kcat_sources`). From c9a6421634d35bd0b95fab8d41eebec5e6dc40c6 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:23:34 +0100 Subject: [PATCH 34/42] fix: refuse a GECKO 3 custom kcats file before changing the model --- CHANGELOG.md | 2 +- src/geckopy/gather_kcats/kcat_assignment.py | 13 ++++++++----- tests/test_kcat_assignment.py | 5 +++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67cb9ba..0e32831 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ * The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. * `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. -* `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. +* `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`, before they change the model. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index 8130454..e237e44 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -161,6 +161,14 @@ def _decide(model: "EcModel", adapter: "ModelAdapter", uniprot_db: Optional["Uni config = kcat_assignment_config(adapter) path = Path(adapter.params.path) kcat_dir = path / "data" / "kcats" + legacy = path / "data" / "customKcats.tsv" + # Checked before the model is changed, so an error leaves it as it was. + if (any("custom" in st.sources for st in config.stages) + and not (kcat_dir / "custom.tsv").is_file() and legacy.is_file()): + raise ValueError( + f"{legacy} is a GECKO 3 custom kcats file and {kcat_dir / 'custom.tsv'} does " + "not exist. Convert it with convert_custom_kcats (geckopy upgrade does this)." + ) ec = model.ec n = len(ec.rxns) ec.kcat = np.zeros(n, dtype=float) @@ -179,11 +187,6 @@ def load(name: str) -> Optional[pd.DataFrame]: file = kcat_dir / f"{name}.tsv" if file.is_file(): loaded[name], report["sources"][name] = load_kcat_source(model, file) - elif name == "custom" and (path / "data" / "customKcats.tsv").is_file(): - raise ValueError( - f"{path / 'data' / 'customKcats.tsv'} is a GECKO 3 custom kcats file and {file} does " - "not exist. Convert it with convert_custom_kcats (geckopy upgrade does this)." - ) else: loaded[name] = None report["missing"].append(name) diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index dde1c9f..25adbde 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -166,6 +166,11 @@ def test_legacy_custom_kcats_file_raises(tmp_path): base, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "gecko_median"\n', convert_custom=False) with pytest.raises(ValueError, match="convert_custom_kcats"): collect_kcats(base, adapter=adapter, write_report=False) + base.ec.kcat[:] = 7.0 + base.ec.source = ["mine"] * len(base.ec.rxns) + with pytest.raises(ValueError, match="convert_custom_kcats"): + assign_kcats(base, adapter=adapter, write_report=False) + assert np.all(base.ec.kcat == 7.0) and set(base.ec.source) == {"mine"} def test_kcat_sources_table_overrides_only_its_keys(tmp_path): From b4f4fce09c3a9cf65f4b4ccf39a8a86ae908a367 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:23:57 +0100 Subject: [PATCH 35/42] feat: name replacements of removed functions in upgrade_project --- CHANGELOG.md | 1 + src/geckopy/upgrade.py | 10 ++++++++-- tests/test_upgrade.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e32831..031a97c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `upgrade_project` (`geckopy upgrade`) reports the calls of `run_dlkcat`, `write_dlkcat_input`, `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values` in the project's scripts with their replacement, and names `convert_custom_kcats` for `apply_custom_kcats` and the `via` of `kcats_from_dlkcat` for `read_dlkcat_output`. * `write_dlkcat_input` is a deprecated alias that warns with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` writes the DLKcat input with the same code, as GECKO's `kcatsFromDLKcat` does after `writeDLKcatInput` became its local function. `extract_enzyme_substrate_pairs` stays public. * The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. * The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index 550f82c..e45cd32 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -36,9 +36,15 @@ ("select_kcat_value", "apply_kcat_list"), ("merge_dlkcat_and_fuzzy_kcats", "merge_kcats"), ("fuzzy_kcat_matching", "kcats_from_brenda (or kcats_from_ec_tree), then assign_kcats"), - ("read_dlkcat_output", "kcats_from_dlkcat, then assign_kcats"), + ("read_dlkcat_output", 'kcats_from_dlkcat(via="file"), then assign_kcats'), + ("run_dlkcat", 'kcats_from_dlkcat(via="docker"), then assign_kcats'), + ("write_dlkcat_input", 'kcats_from_dlkcat(via="docker"), then assign_kcats'), ("parse_okp_output", "kcats_from_okp, then assign_kcats"), - ("apply_custom_kcats", "data/kcats/custom.tsv and assign_kcats"), + ("submit_open_kinetics_predictor", "kcats_from_okp, then assign_kcats"), + ("fetch_open_kinetics_predictor", "kcats_from_okp, then assign_kcats"), + ("build_okp_input_csv", "kcats_from_okp, then assign_kcats"), + ("apply_custom_kcats", "convert_custom_kcats (data/kcats/custom.tsv), then assign_kcats"), + ("truncate_values", "removed: nothing replaces it"), ) diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index 7836646..5b581c0 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -158,3 +158,13 @@ def test_usage_reaction_fixed_to_zero_is_turned(tmp_path): assert {m.id: c for m, c in loaded.reactions.get_by_id(usage.id).metabolites.items()} == \ {m.id: c for m, c in model.reactions.get_by_id(usage.id).metabolites.items()} assert any("GECKO 3 direction" in str(w.message) for w in caught) + + +def test_upgrade_project_names_the_replacements_of_removed_functions(tmp_path): + from geckopy.upgrade import RENAMED + + replaced = dict(RENAMED) + assert replaced["submit_open_kinetics_predictor"].startswith("kcats_from_okp") + assert replaced["write_dlkcat_input"].startswith('kcats_from_dlkcat(via="docker")') + assert replaced["apply_custom_kcats"].startswith("convert_custom_kcats") + assert replaced["truncate_values"].startswith("removed") From 725ac60d655b65cd6b6560e5c2fc0e7f857c7063 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:24:29 +0100 Subject: [PATCH 36/42] doc: predict dlkcat kcats with kcats_from_dlkcat in the full tutorial --- tutorials/full_ecModel/protocol.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/tutorials/full_ecModel/protocol.py b/tutorials/full_ecModel/protocol.py index f64ccfb..ec8fe4c 100644 --- a/tutorials/full_ecModel/protocol.py +++ b/tutorials/full_ecModel/protocol.py @@ -32,7 +32,6 @@ kcats_from_dlkcat, load_brenda_data, load_conventional_gem, - load_dlkcat_ignore_lists, load_ec_model, load_phyl_dist, load_uniprot_tsv, @@ -40,7 +39,6 @@ save_ec_model, set_kcat_for_reactions, set_prot_pool_size, - write_dlkcat_input, ) from geckopy.databases import download_phyl_dist, load_kegg_tsv @@ -149,22 +147,17 @@ find_met_smiles(ec_model, cache_path=params.path / "data" / "smilesDB.tsv") # %% [markdown] -# **STEP 23** Prepare DLKcat input file. The `data/DLKcat.tsv` -# included here already has predicted kcat values; uncomment to -# regenerate (this discards existing predictions). +# **STEP 23-24** Predict kcats with DLKcat. `kcats_from_dlkcat` writes +# the DLKcat input to `data/DLKcat.tsv` and runs DLKcat in Docker with +# `via="docker"` (this replaces the predictions in that file), or +# predicts with the DLKcat method of OpenKineticsPredictor with +# `via="okp"` (needs an OKP API key). The `data/DLKcat.tsv` included +# here already has predictions, so neither is run here: # # ```python -# ignore_lists = load_dlkcat_ignore_lists(params.path / "data") -# write_dlkcat_input( -# ec_model, params.path / "data" / "DLKcat.tsv", -# ignore_lists, overwrite=True, -# ) +# kcats_from_dlkcat(ec_model, via="docker") # ``` -# %% [markdown] -# **STEP 24** Run DLKcat. (External tool; not invoked here. The -# included `data/DLKcat.tsv` already has output from a previous run.) - # %% [markdown] # **STEP 25** Load DLKcat output from `data/DLKcat.tsv`, kept as # `data/kcats/dlkcat.tsv`. From de056225056b2b7f665ce311bf5d9204ea34fd97 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:24:47 +0100 Subject: [PATCH 37/42] feat: export EcFSEOFResult from geckopy --- CHANGELOG.md | 2 +- src/geckopy/__init__.py | 2 ++ src/geckopy/utilities/ec_fseof.py | 2 +- tests/test_ec_fseof.py | 8 ++++++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 031a97c..b0b64ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ * `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`, before they change the model. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. -* `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult`, raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. +* `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult` (exported from `geckopy` and `geckopy.utilities`), raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. * `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). * The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index 0479f9e..c0271af 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -152,6 +152,7 @@ # utilities from geckopy.utilities import ( AddNewRxnsResult, + EcFSEOFResult, EnzymeUsageReport, EnzymeUsageResult, MapRxnsResult, @@ -268,6 +269,7 @@ "zero_unmeasured_isozymes", # utilities "AddNewRxnsResult", + "EcFSEOFResult", "EnzymeUsageReport", "EnzymeUsageResult", "MapRxnsResult", diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index a167754..539af51 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -105,7 +105,7 @@ def ec_fseof( max_fraction: float = 0.9, correlation_threshold: float = 0.9, flux_eps: float = 1e-6, -) -> FSEOFResult: +) -> EcFSEOFResult: """Run Flux-Scanning with Enforced Objective Function on an ecModel. Enforces an increasing flux toward ``prod_target_rxn`` while diff --git a/tests/test_ec_fseof.py b/tests/test_ec_fseof.py index ee2d7c7..607f86a 100644 --- a/tests/test_ec_fseof.py +++ b/tests/test_ec_fseof.py @@ -494,3 +494,11 @@ def test_gene_actions_are_reconciled_as_in_ecfseof(tmp_path): assert list(genes["action"]) == ["OE", "KD", "KO"] assert list(genes["essentiality"]) == ["essential", "essential", ""] assert list(genes["slope"]) == [3.0, 2.0, 1.0] + + +def test_ec_fseof_result_is_exported_and_returned(tmp_path): + import geckopy + + result = ec_fseof(_build_fseof_model(_adapter(tmp_path)), "EX_PROD", n_steps=4) + assert isinstance(result, geckopy.EcFSEOFResult) + assert "EcFSEOFResult" in geckopy.__all__ From ec9bb2783f0f01c3c6de8eadfe27565f2ceb7b4d Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:25:33 +0100 Subject: [PATCH 38/42] fix: use the no-range tolerance of ecFSEOF in ec_fseof --- CHANGELOG.md | 2 +- src/geckopy/utilities/ec_fseof.py | 12 +++++++----- tests/test_ec_fseof.py | 7 +++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b64ba..f254c84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult` (exported from `geckopy` and `geckopy.utilities`), raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. -* `ec_fseof` raises `ValueError` when the flux of the production target at maximum growth is not below the top of the scan, as GECKO's `ecFSEOF` raises `ecFSEOF:noRange`; the scan used to run on levels that do not rise and returned no targets. +* `ec_fseof` raises `ValueError` when the top of the scan is not above the flux of the production target at maximum growth by more than `1e-9 * max(1, |top|)`, also for a target that cannot carry flux; GECKO's `ecFSEOF` raises `ecFSEOF:noRange` in the same case. The scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). * The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. * `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index 539af51..7911f16 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -176,9 +176,11 @@ def ec_fseof( Raises ------ ValueError - If the production target cannot carry flux, or if its flux at - maximum growth is not below the top of the scan (``max_fraction`` - of its maximum), as GECKO's ``ecFSEOF`` raises ``ecFSEOF:noRange``. + If the top of the scan (``max_fraction`` of the maximum production + flux) is not above the production flux at maximum growth by more than + ``1e-9 * max(1, |top|)``, which includes a production target that + cannot carry flux; GECKO's ``ecFSEOF`` raises ``ecFSEOF:noRange`` in + the same case. """ from ..adapter import resolve_param bio_rxn_id = resolve_param( @@ -191,8 +193,8 @@ def ec_fseof( min_target = _biomass_optimal_target_flux(model, bio_rxn_id, prod_target_rxn) max_target = _scan_ceiling(model, prod_target_rxn, max_fraction) - # A blocked target (ceiling 0) is left to fseof, which raises its own error. - if max_target > 0 and min_target >= max_target: + # The rule and tolerance of ecFSEOF.m's ecFSEOF:noRange; a blocked target (ceiling 0) is caught too. + if max_target <= min_target + 1e-9 * max(1.0, abs(max_target)): raise ValueError( f"ec_fseof: the flux of {prod_target_rxn!r} at maximum growth ({min_target:g}) is not " f"below the top of the scan ({max_target:g}), so there is no range to scan." diff --git a/tests/test_ec_fseof.py b/tests/test_ec_fseof.py index 607f86a..df157c8 100644 --- a/tests/test_ec_fseof.py +++ b/tests/test_ec_fseof.py @@ -145,12 +145,12 @@ def test_explicit_bio_rxn_overrides_adapter(tmp_path): def test_no_production_headroom_raises(tmp_path): - """raven's fseof raises when the target reaction can't carry positive flux.""" + """A target reaction that can't carry flux leaves no range to scan.""" adapter = _adapter(tmp_path) model = _build_fseof_model(adapter) # Block production entirely so EX_PROD's slim-optimise is 0. model.reactions.get_by_id("R_PROD").upper_bound = 0.0 - with pytest.raises(ValueError, match="cannot carry positive flux"): + with pytest.raises(ValueError, match="no range to scan"): ec_fseof(model, "EX_PROD") @@ -165,6 +165,9 @@ def test_no_range_to_scan_raises(tmp_path, monkeypatch): model.reactions.get_by_id("EX_PROD").lower_bound = 95.0 # the top of the scan is 90 with pytest.raises(ValueError, match="no range to scan"): ec_fseof(model, "EX_PROD") + model.reactions.get_by_id("EX_PROD").lower_bound = 90.0 - 1e-8 # within 1e-9 * 90 of the top + with pytest.raises(ValueError, match="no range to scan"): + ec_fseof(model, "EX_PROD") # --------------------------------------------------------------------------- # From 746af93283cafee774a2ef9298a589bc95bf366d Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:25:50 +0100 Subject: [PATCH 39/42] test: keep the dlkcat okp test from submitting a real job --- tests/test_kcat_assignment.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 25adbde..1dc7b81 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -245,9 +245,19 @@ def fake_okp(model, **kwargs): assert "DLKcat" in report.loc["dlkcat", "note"] assert jobs and jobs[0]["method"] == "DLKcat" - # Without an API key the source fails, with the reason in the note. + # Without an API key the source fails, with the reason in the note. The key file is + # removed and the key resolver raises, so no real job can be submitted. + import importlib + + okp_run = importlib.import_module("geckopy.gather_kcats.open_kinetics_predictor.run") monkeypatch.undo() monkeypatch.delenv("OKP_API_KEY", raising=False) + (project / "data" / "okpApiKey.txt").unlink(missing_ok=True) + + def no_key(api_key, adapter): + raise ValueError("No OpenKineticsPredictor API key found (test).") + + monkeypatch.setattr(okp_run, "_resolve_api_key", no_key) report = update_kcat_sources(base, adapter=adapter).set_index("source") assert report.loc["dlkcat", "status"] == "failed" assert "API key" in report.loc["dlkcat", "note"] From 08fb08c0e1de9c0eea9ea4704f234f146e826a7c Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Wed, 11 Nov 2026 23:54:53 +0100 Subject: [PATCH 40/42] feat: leave the UniProt and KEGG identifiers empty by default --- CHANGELOG.md | 1 + src/geckopy/adapter/adapterParameters.tsv | 4 ++-- src/geckopy/adapter/params.py | 12 ++++++++--- src/geckopy/adapter/template.py | 20 ++++++++++++++++++- src/geckopy/databases/uniprot_loader.py | 20 +++++++++++++++++++ src/geckopy/ec_model/make_ec_model.py | 15 +++++--------- .../limit_proteins/calculate_f_factor.py | 18 ++++++----------- tests/test_calculate_f_factor.py | 13 ++++++++++++ tests/test_cli.py | 4 +++- tests/test_make_ec_model.py | 8 +++----- 10 files changed, 81 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f254c84..38af532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `uniprot.id` and `kegg.id` are empty by default instead of the identifiers of Saccharomyces cerevisiae (`"559292"` and `"sce"`), as in GECKO; `adapterParameters.tsv` is the same as GECKO's. `geckopy init` writes both uncommented as `""`, with examples in the comments. `make_ec_model` and `calculate_f_factor` raise `FileNotFoundError` naming `[uniprot] id` when `data/uniprot.tsv` is missing and the identifier is empty, as GECKO's `loadDatabases` raises `loadDatabases:noUniprotID`; an empty `kegg.id` leaves KEGG out with a warning. A project that relied on the yeast defaults sets the identifiers in model_adapter.toml. * `upgrade_project` (`geckopy upgrade`) reports the calls of `run_dlkcat`, `write_dlkcat_input`, `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values` in the project's scripts with their replacement, and names `convert_custom_kcats` for `apply_custom_kcats` and the `via` of `kcats_from_dlkcat` for `read_dlkcat_output`. * `write_dlkcat_input` is a deprecated alias that warns with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` writes the DLKcat input with the same code, as GECKO's `kcatsFromDLKcat` does after `writeDLKcatInput` became its local function. `extract_enzyme_substrate_pairs` stays public. * The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. diff --git a/src/geckopy/adapter/adapterParameters.tsv b/src/geckopy/adapter/adapterParameters.tsv index 7d9a9ff..dd19cd4 100644 --- a/src/geckopy/adapter/adapterParameters.tsv +++ b/src/geckopy/adapter/adapterParameters.tsv @@ -12,10 +12,10 @@ enzyme_comp enzyme_comp "cytoplasm" Compartment of the protein pseudometabolites kcat_aggregate_brenda kcatAggregation "max" BRENDA aggregate read by fuzzyKcatMatching: max or median kcat_aggregate_candidates "max" Aggregation of several candidate kcats for one reaction (geckopy) kcat_aggregate_isozymes "mean" Aggregation of isozyme kcats when filling a missing value (geckopy) -kegg.id kegg.ID "sce" KEGG organism code +kegg.id kegg.ID "" KEGG organism code kegg.gene_id kegg.geneID "kegg" Field in the KEGG entry that matches model gene identifiers uniprot.type uniprot.type "taxonomy" Whether uniprot.id is a taxonomy or a proteome identifier -uniprot.id uniprot.ID "559292" Taxonomy or proteome identifier +uniprot.id uniprot.ID "" Taxonomy or proteome identifier uniprot.gene_id_field uniprot.geneIDfield "gene_oln" UniProt field that matches model gene identifiers uniprot.reviewed uniprot.reviewed false Use only reviewed (Swiss-Prot) entries complex.taxonomic_id complex.taxonomicID Taxonomy identifier for Complex Portal diff --git a/src/geckopy/adapter/params.py b/src/geckopy/adapter/params.py index acb8b29..32d257e 100644 --- a/src/geckopy/adapter/params.py +++ b/src/geckopy/adapter/params.py @@ -16,7 +16,10 @@ class KeggParams(BaseModel): model_config = ConfigDict(extra="forbid") - id: str = Field(default="sce", description="KEGG organism code") + id: str = Field( + default="", + description='KEGG organism code (e.g. "sce"); empty: KEGG is not used', + ) gene_id: str = Field( default="kegg", description=( @@ -35,8 +38,11 @@ class UniprotParams(BaseModel): description="Whether 'id' is a taxonomy ID or a proteome ID", ) id: str = Field( - default="559292", - description="Taxonomy ID (e.g. '559292') or proteome ID, matching 'type'", + default="", + description=( + "Taxonomy ID (e.g. '559292') or proteome ID (e.g. 'UP000002311'), " + "matching 'type'; required unless data/uniprot.tsv is provided" + ), ) gene_id_field: str = Field( default="gene_oln", diff --git a/src/geckopy/adapter/template.py b/src/geckopy/adapter/template.py index 2cf69a8..f90cb11 100644 --- a/src/geckopy/adapter/template.py +++ b/src/geckopy/adapter/template.py @@ -24,6 +24,19 @@ "conv_gem": '"models/REPLACE_ME.xml"', "org_name": '"REPLACE_ME"', } +# Keys written uncommented, with the comment of GECKO's adapterTemplate.toml. +_SET_IN_TEMPLATE = { + ("kegg", "id"): [ + "# KEGG organism code, from https://www.genome.jp/kegg/catalog/org_list.html,", + '# for example "sce" for Saccharomyces cerevisiae. Empty: KEGG is not used.', + ], + ("uniprot", "id"): [ + '# "proteome" (https://www.uniprot.org/proteomes/) or "taxonomy"', + "# (https://www.uniprot.org/taxonomy), and the matching identifier, for", + '# example "559292" (taxonomy) or "UP000002311" (proteome) for', + "# Saccharomyces cerevisiae. Required unless data/uniprot.tsv is provided.", + ], +} def _format_toml_value(value: Any) -> str | None: @@ -101,7 +114,8 @@ def generate_template_toml(*, project_name: str = "my-ecmodel") -> str: Every section of the schema is emitted, evotune kcat tuning hyperparameters included; optional fields are commented out at their defaults, so the template doubles as the reference for what - can be set. + can be set. The organism identifiers ``[kegg] id`` and ``[uniprot] id`` + are written uncommented and empty, with an example in the comment. Parameters ---------- @@ -151,6 +165,10 @@ def generate_template_toml(*, project_name: str = "my-ecmodel") -> str: lines.append(f"[{name}]") instance = model_cls() for sub_name, sub_field in model_cls.model_fields.items(): + if (name, sub_name) in _SET_IN_TEMPLATE: + value = _format_toml_value(getattr(instance, sub_name)) + lines.extend([*_SET_IN_TEMPLATE[(name, sub_name)], f"{sub_name} = {value}", ""]) + continue lines.extend(_emit_scalar( sub_name, description=sub_field.description, annotation=sub_field.annotation, diff --git a/src/geckopy/databases/uniprot_loader.py b/src/geckopy/databases/uniprot_loader.py index 63a51a9..9d21d1b 100644 --- a/src/geckopy/databases/uniprot_loader.py +++ b/src/geckopy/databases/uniprot_loader.py @@ -189,6 +189,26 @@ def load_uniprot_tsv( ) +def _load_project_uniprot(adapter) -> UniprotDB: + """``data/uniprot.tsv`` of the adapter's project, downloaded with its ``[uniprot]`` + settings when missing, as GECKO's ``loadDatabases`` does. + + Raises ``FileNotFoundError`` when the file is missing and ``[uniprot] id`` is + empty, as ``loadDatabases`` raises ``loadDatabases:noUniprotID``. + """ + path = Path(adapter.params.path) / "data" / "uniprot.tsv" + uniprot = adapter.params.uniprot + if not path.is_file() and not uniprot.id: + raise FileNotFoundError( + f"{path} does not exist and [uniprot] id in model_adapter.toml is empty. Set the " + "UniProt taxonomy or proteome identifier of the organism, or provide the file." + ) + return load_uniprot_tsv( + path, uniprot_id=uniprot.id, id_type=uniprot.type, + gene_id_field=uniprot.gene_id_field, reviewed=uniprot.reviewed, + ) + + def _check_duplicate_ids(source_row_ids: list[str], path: Path) -> None: """Raise if any Entry value appears on more than one source row. diff --git a/src/geckopy/ec_model/make_ec_model.py b/src/geckopy/ec_model/make_ec_model.py index 18faeae..65d90d0 100644 --- a/src/geckopy/ec_model/make_ec_model.py +++ b/src/geckopy/ec_model/make_ec_model.py @@ -29,7 +29,8 @@ import cobra from raven_toolbox.utils.sort import sort_identifiers -from ..databases import UniprotDB, load_kegg_tsv, load_uniprot_tsv +from ..databases import UniprotDB, load_kegg_tsv +from ..databases.uniprot_loader import _load_project_uniprot from ..solver_support import repair_scipy_problem, switch_to_scipy from .ec_model import EcModel from .pipeline import ( @@ -181,7 +182,8 @@ def make_ec_model( ------ FileNotFoundError If ``uniprot_db`` is None, no ``uniprot.tsv`` is found under the - adapter's data folder and ``uniprot.id`` is empty. + adapter's data folder and ``[uniprot] id`` is empty (by default), as + GECKO's ``loadDatabases`` raises ``loadDatabases:noUniprotID``. RuntimeError If a download fails. ValueError @@ -253,14 +255,7 @@ def make_ec_model( # As GECKO's loadDatabases: a missing file is downloaded with the adapter's settings. data_dir = adapter.params.path / "data" if uniprot_db is None: - uniprot = adapter.params.uniprot - uniprot_db = load_uniprot_tsv( - data_dir / "uniprot.tsv", - uniprot_id=uniprot.id or None, - id_type=uniprot.type, - gene_id_field=uniprot.gene_id_field, - reviewed=uniprot.reviewed, - ) + uniprot_db = _load_project_uniprot(adapter) if kegg_db is None: kegg_db = load_kegg_tsv( diff --git a/src/geckopy/limit_proteins/calculate_f_factor.py b/src/geckopy/limit_proteins/calculate_f_factor.py index 5546f86..24ce940 100644 --- a/src/geckopy/limit_proteins/calculate_f_factor.py +++ b/src/geckopy/limit_proteins/calculate_f_factor.py @@ -65,7 +65,8 @@ def calculate_f_factor( If None, ``data/paxDB.tsv`` of the adapter's project is loaded with ``load_pax_db`` and the project's ``data/uniprot.tsv`` (downloaded with the adapter's ``[uniprot]`` settings when - missing). Without that file, a warning is logged and the default + missing; ``FileNotFoundError`` when it is missing and ``[uniprot] + id`` is empty). Without that file, a warning is logged and the default f of 0.5 is returned, as GECKO's ``calculateFfactor`` does. enzymes UniProt IDs counted as "in the model". Defaults to @@ -91,17 +92,10 @@ def calculate_f_factor( "found; returning the default f = 0.5." ) return 0.5 - from ..databases import load_pax_db, load_uniprot_tsv - - uniprot = adapter.params.uniprot - uniprot_db = load_uniprot_tsv( - pax_path.parent / "uniprot.tsv", - uniprot_id=uniprot.id or None, - id_type=uniprot.type, - gene_id_field=uniprot.gene_id_field, - reviewed=uniprot.reviewed, - ) - prot_data = load_pax_db(pax_path, uniprot_db) + from ..databases import load_pax_db + from ..databases.uniprot_loader import _load_project_uniprot + + prot_data = load_pax_db(pax_path, _load_project_uniprot(adapter)) abundances = prot_data.abundances if abundances.ndim == 2: diff --git a/tests/test_calculate_f_factor.py b/tests/test_calculate_f_factor.py index 041cdb0..1d9b5bf 100644 --- a/tests/test_calculate_f_factor.py +++ b/tests/test_calculate_f_factor.py @@ -221,3 +221,16 @@ def test_paxdb_of_the_project_is_read_without_prot_data(tmp_path): model = _ec_model(["P1"]) model.adapter = ModelAdapter.from_folder(tmp_path) assert calculate_f_factor(model) == pytest.approx(0.25) + + +def test_paxdb_without_uniprot_tsv_and_uniprot_id_raises(tmp_path): + """As loadDatabases:noUniprotID: data/uniprot.tsv is missing and [uniprot] id is empty.""" + from geckopy import ModelAdapter + + (tmp_path / "model_adapter.toml").write_text('conv_gem = "dummy.xml"\norg_name = "test"\n') + (tmp_path / "data").mkdir() + (tmp_path / "data" / "paxDB.tsv").write_text("#header\n1\t4932.g1\t1\n", encoding="utf-8") + model = _ec_model(["P1"]) + model.adapter = ModelAdapter.from_folder(tmp_path) + with pytest.raises(FileNotFoundError, match=r"\[uniprot\] id"): + calculate_f_factor(model) diff --git a/tests/test_cli.py b/tests/test_cli.py index 864b3c7..f52c6fb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -88,7 +88,9 @@ def test_cli_init_produces_loadable_adapter_after_filling_required(tmp_path): assert adapter.params.org_name == "Testus testus" # Defaults kick in for fields the user did not uncomment. assert adapter.params.sigma == 0.5 - assert adapter.params.kegg.id == "sce" + # The organism identifiers are written empty, with an example in the comment. + assert adapter.params.kegg.id == "" and adapter.params.uniprot.id == "" + assert '\nid = ""\n' in toml_text and '"UP000002311" (proteome)' in toml_text def test_cli_init_stub_adapter_py_is_commented_out(tmp_path): diff --git a/tests/test_make_ec_model.py b/tests/test_make_ec_model.py index dfe8bcb..9ff944b 100644 --- a/tests/test_make_ec_model.py +++ b/tests/test_make_ec_model.py @@ -263,12 +263,12 @@ def test_make_ec_model_gecko_light_enzymes_match_full(): def test_make_ec_model_missing_uniprot_tsv_raises(tmp_path): """If the adapter points at a folder without uniprot.tsv, no explicit - uniprot_db is given and uniprot.id is empty, raise FileNotFoundError.""" + uniprot_db is given and [uniprot] id is empty (the default), raise + FileNotFoundError naming [uniprot] id, as loadDatabases:noUniprotID.""" toml = ( 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' - '[uniprot]\nid = ""\n' ) (tmp_path / "model_adapter.toml").write_text(toml) adapter = ModelAdapter.from_folder(tmp_path) @@ -282,7 +282,7 @@ def test_make_ec_model_missing_uniprot_tsv_raises(tmp_path): r.gene_reaction_rule = "g1" model.add_reactions([r]) - with pytest.raises(FileNotFoundError, match="uniprot.tsv"): + with pytest.raises(FileNotFoundError, match=r"\[uniprot\] id"): make_ec_model(model, adapter) @@ -345,7 +345,6 @@ def test_make_ec_model_unmatched_genes_logged_and_annotated(tmp_path, caplog): 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' - '[kegg]\nid = ""\n' ) (tmp_path / "model_adapter.toml").write_text(toml) adapter = ModelAdapter.from_folder(tmp_path) @@ -383,7 +382,6 @@ def _toy_with_missing_gene(tmp_path): 'conv_gem = "dummy.xml"\n' 'org_name = "test"\n' 'enzyme_comp = "c"\n' - '[kegg]\nid = ""\n' ) adapter = ModelAdapter.from_folder(tmp_path) model = cobra.Model("m") From 89d9b43a7a04320cf81a7e9b6b09c60fcb593b55 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 12 Nov 2026 14:38:58 +0100 Subject: [PATCH 41/42] feat: remove the deprecated aliases with gecko 3 names --- src/geckopy/__init__.py | 27 -- src/geckopy/databases/dlkcat_ignore_lists.py | 2 +- src/geckopy/ec_model/pipeline/__init__.py | 5 +- .../ec_model/pipeline/apply_custom_kcats.py | 347 ------------------ src/geckopy/ec_model/pipeline/fill_kcats.py | 21 -- src/geckopy/gather_kcats/__init__.py | 14 +- src/geckopy/gather_kcats/get_standard_kcat.py | 27 -- src/geckopy/gather_kcats/kcat_sources.py | 2 +- .../merge_dlkcat_and_fuzzy_kcats.py | 85 ----- .../open_kinetics_predictor/parse_output.py | 2 +- .../open_kinetics_predictor/run.py | 2 +- .../gather_kcats/read_dlkcat_output.py | 31 +- src/geckopy/gather_kcats/run_dlkcat.py | 31 +- src/geckopy/gather_kcats/select_kcat_value.py | 26 -- .../gather_kcats/write_dlkcat_input.py | 31 +- src/geckopy/get_enzyme_data/__init__.py | 6 +- .../get_enzyme_data/ec_from_database.py | 28 -- src/geckopy/get_enzyme_data/ec_from_gem.py | 21 -- src/geckopy/kcat_tuning/__init__.py | 5 +- src/geckopy/kcat_tuning/find_max_value.py | 124 ------- src/geckopy/kcat_tuning/sigma_fitter.py | 29 -- src/geckopy/limit_proteins/__init__.py | 2 - .../limit_proteins/constrain_flux_data.py | 31 -- src/geckopy/upgrade.py | 319 +++++++++++++++- tests/test_deprecated_aliases.py | 218 ----------- tests/test_fill_kcats.py | 21 -- tests/test_find_max_value.py | 217 ----------- ...m_kcats.py => test_gecko3_custom_kcats.py} | 52 ++- tests/test_gecko_matlab_parity.py | 15 +- tests/test_kcat_assignment.py | 13 +- tests/test_kcat_contract.py | 16 +- ...and_fuzzy_kcats.py => test_merge_kcats.py} | 73 ++-- tests/test_parity_round3_u2.py | 8 +- tests/test_read_dlkcat_output.py | 46 +-- tests/test_run_dlkcat.py | 28 +- tests/test_upgrade.py | 45 ++- tests/test_write_dlkcat_input.py | 46 +-- 37 files changed, 522 insertions(+), 1494 deletions(-) delete mode 100644 src/geckopy/ec_model/pipeline/apply_custom_kcats.py delete mode 100644 src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py delete mode 100644 src/geckopy/kcat_tuning/find_max_value.py delete mode 100644 tests/test_deprecated_aliases.py delete mode 100644 tests/test_find_max_value.py rename tests/{test_apply_custom_kcats.py => test_gecko3_custom_kcats.py} (86%) rename tests/{test_merge_dlkcat_and_fuzzy_kcats.py => test_merge_kcats.py} (93%) diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index c0271af..707a264 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -68,10 +68,8 @@ # Pipeline stages (these are public utilities, not just internal) from geckopy.ec_model.pipeline import ( apply_complex_data, - apply_custom_kcats, apply_kcat_constraints, fill_kcats_from_isozymes, - get_kcat_across_isozymes, # deprecated; alias of fill_kcats_from_isozymes get_reactions_from_enzyme, set_kcat_for_reactions, set_prot_pool_size, @@ -86,24 +84,18 @@ collect_kcats, ec_stats_from_brenda, fuzzy_kcat_matching, - get_standard_kcat, # deprecated; alias of assign_standard_kcat kcats_from_brenda, kcats_from_dlkcat, kcats_from_ec_tree, kcats_from_homologs, kcats_from_okp, load_kcat_source, - merge_dlkcat_and_fuzzy_kcats, # deprecated; alias of merge_kcats merge_kcats, - read_dlkcat_output, # deprecated; use kcats_from_dlkcat(via="file") read_kcat_source, remove_standard_kcat, review_assignment, - run_dlkcat, # deprecated; use kcats_from_dlkcat(via="docker") save_kcat_source, - select_kcat_value, # deprecated; alias of apply_kcat_list update_kcat_sources, - write_dlkcat_input, # deprecated; use kcats_from_dlkcat(via="docker") ) # get_enzyme_data @@ -112,18 +104,14 @@ fill_eccodes_from_database, fill_eccodes_from_gem, find_ec_in_db, - get_ec_from_database, # deprecated; alias of fill_eccodes_from_database - get_ec_from_gem, # deprecated; alias of fill_eccodes_from_gem ) # kcat_tuning from geckopy.kcat_tuning import ( SigmaFitterResult, TunedKcatsResult, - find_max_value, fit_sigma, sensitivity_tuning, - sigma_fitter, # deprecated; alias of fit_sigma ) # limit_proteins @@ -137,7 +125,6 @@ calculate_f_factor, complex_families, constrain_enz_concs, - constrain_flux_data, # deprecated; alias of apply_flux_data_constraints fill_enz_concs, flexibilize_enz_concs, get_conc_control_coeffs, @@ -208,7 +195,6 @@ "load_uniprot_tsv", # Pipeline / model edits "apply_complex_data", - "apply_custom_kcats", "apply_kcat_constraints", "fill_kcats_from_isozymes", "get_reactions_from_enzyme", @@ -227,16 +213,12 @@ "kcats_from_homologs", "kcats_from_okp", "load_kcat_source", - "merge_dlkcat_and_fuzzy_kcats", # deprecated; alias of merge_kcats "merge_kcats", - "read_dlkcat_output", # deprecated; use kcats_from_dlkcat(via="file") "read_kcat_source", "remove_standard_kcat", "review_assignment", - "run_dlkcat", # deprecated; use kcats_from_dlkcat(via="docker") "save_kcat_source", "update_kcat_sources", - "write_dlkcat_input", # deprecated; use kcats_from_dlkcat(via="docker") # get_enzyme_data "copy_ec_to_gem", "fill_eccodes_from_database", @@ -245,7 +227,6 @@ # kcat sensitivity analysis "SigmaFitterResult", "TunedKcatsResult", - "find_max_value", "fit_sigma", "sensitivity_tuning", # limit_proteins @@ -286,12 +267,4 @@ "pfba_enzymes", "report_enzyme_usage", "save_ec_model", - # Deprecated aliases (kept reachable so old `from geckopy import X` still works) - "constrain_flux_data", # -> apply_flux_data_constraints - "get_ec_from_database", # -> fill_eccodes_from_database - "get_ec_from_gem", # -> fill_eccodes_from_gem - "get_kcat_across_isozymes", # -> fill_kcats_from_isozymes - "get_standard_kcat", # -> assign_standard_kcat - "select_kcat_value", # -> apply_kcat_list - "sigma_fitter", # -> fit_sigma ] diff --git a/src/geckopy/databases/dlkcat_ignore_lists.py b/src/geckopy/databases/dlkcat_ignore_lists.py index 7636bfe..129fd97 100644 --- a/src/geckopy/databases/dlkcat_ignore_lists.py +++ b/src/geckopy/databases/dlkcat_ignore_lists.py @@ -82,7 +82,7 @@ def load_dlkcat_ignore_lists( ------- DLKcatIgnoreLists Normalized ignore lists ready to feed to - ``write_dlkcat_input``. + ``kcats_from_dlkcat``. """ ignore_lines = _strip_header(_read_lines( _resolve_path(project_data_folder, "DLKcatIgnoreMets.tsv") diff --git a/src/geckopy/ec_model/pipeline/__init__.py b/src/geckopy/ec_model/pipeline/__init__.py index 75f431b..568bc5b 100644 --- a/src/geckopy/ec_model/pipeline/__init__.py +++ b/src/geckopy/ec_model/pipeline/__init__.py @@ -1,9 +1,8 @@ """Pipeline stages of make_ec_model, ported from GECKO MATLAB.""" from .apply_complex_data import apply_complex_data -from .apply_custom_kcats import apply_custom_kcats from .apply_kcat import apply_kcat_constraints from .expand import expand_model -from .fill_kcats import fill_kcats_from_isozymes, get_kcat_across_isozymes +from .fill_kcats import fill_kcats_from_isozymes from .populate_ec import ( allocate_ec_and_coupling_light, allocate_ec_for_catalyzed_reactions, @@ -34,13 +33,11 @@ "allocate_ec_and_coupling_light", "allocate_ec_for_catalyzed_reactions", "apply_complex_data", - "apply_custom_kcats", "apply_kcat_constraints", "build_rxn_enzyme_coupling", "convert_to_irreversible", "expand_model", "fill_kcats_from_isozymes", - "get_kcat_across_isozymes", # deprecated; alias of fill_kcats_from_isozymes "get_reactions_from_enzyme", "invert_backwards_only_reactions", "populate_enzyme_data", diff --git a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py b/src/geckopy/ec_model/pipeline/apply_custom_kcats.py deleted file mode 100644 index 1bd9129..0000000 --- a/src/geckopy/ec_model/pipeline/apply_custom_kcats.py +++ /dev/null @@ -1,347 +0,0 @@ -"""Apply user-curated custom kcats from a GECKO 3 customKcats.tsv file. - -Deprecated: GECKO 4 projects keep custom kcats in ``data/kcats/custom.tsv``, -which ``convert_custom_kcats`` writes from ``data/customKcats.tsv``. - -Ported from GECKO MATLAB: src/deprecated/applyCustomKcats.m. -""" -from __future__ import annotations - -import logging -import math -import re -import warnings -from pathlib import Path -from typing import TYPE_CHECKING, Optional - -from .apply_kcat import apply_kcat_constraints - -if TYPE_CHECKING: - from ..ec_model import EcModel - - -logger = logging.getLogger(__name__) - -_EXP_SUFFIX_REGEX = re.compile(r"_EXP_\d+$") -_SOURCE_TAG = "custom" -_NOTES_SEPARATOR = ", " - - -def apply_custom_kcats( - model: "EcModel", - path: Optional[Path] = None, - *, - apply: bool = True, -) -> None: - """Deprecated: apply the kcats of a GECKO 3 ``customKcats.tsv`` file. - - ``convert_custom_kcats`` turns ``data/customKcats.tsv`` into - ``data/kcats/custom.tsv``, which ``collect_kcats`` and ``assign_kcats`` - read. This function still applies the old file, as described for - :func:`_apply_custom_kcats`, after a ``DeprecationWarning``. - """ - warnings.warn( - "apply_custom_kcats is deprecated; convert data/customKcats.tsv with " - "convert_custom_kcats into data/kcats/custom.tsv, which assign_kcats reads.", - DeprecationWarning, - stacklevel=2, - ) - _apply_custom_kcats(model, path, apply=apply) - - -def _apply_custom_kcats( - model: "EcModel", - path: Optional[Path] = None, - *, - apply: bool = True, -) -> None: - """Apply user-curated kcats from a customKcats.tsv file. - - Reads a tab-separated file with seven columns and one header line: - - proteins | genes | gene_name | kcat | rxns | notes | stoicho - - Each row is interpreted in one of three modes, determined by which - of the ``proteins`` and ``rxns`` columns are populated: - - Mode A (rxns only, proteins empty) - Apply the kcat to every entry of ``ec.rxns`` whose ID, after - stripping any ``_EXP_`` suffix, equals one of the listed - reaction IDs (comma-separated). A list in double quotes, as a - spreadsheet exports it, is read without the quotes. Reverse - direction is honored: ``"R2"`` matches ``R2_EXP_*`` but not - ``R2_REV_*``. - - Mode B (proteins only, rxns empty) - Candidate reactions are those catalyzed by any listed protein. - For each candidate, the kcat is applied only if the candidate's - enzyme set exactly matches the listed proteins (full match). - Partial matches (>= 50% but < 100%) are logged for manual review. - - Mode C (both populated) - Same matching rule as Mode B, but candidates are restricted to - reactions matching the listed reaction IDs (with the same - ``_EXP_`` stripping as Mode A). - - Notes from the ``notes`` column are appended to ``ec.notes`` for - each updated reaction (separated by ``, `` from any existing note). - The ``ec.source`` is set to ``"custom"``. The ``stoicho`` column is - parsed but not applied; a warning is logged if any row has - non-trivial stoichiometry. The ``genes`` and ``gene_name`` columns - are read but not used. - - Rows that fail to resolve to any reaction, or whose enzyme match - is only partial, are logged at WARNING level for manual review - rather than raised as errors. - - Parameters - ---------- - model - An EcModel produced by ``make_ec_model``. Mutated in place. - path - Path to the TSV. If None, defaults to - ``adapter.params.path / "data" / "customKcats.tsv"``. - apply - If True (default), call ``apply_kcat_constraints`` after - updating ``ec.kcat`` so the new values reflect in the S matrix. - - Raises - ------ - FileNotFoundError - If the file does not exist. - ValueError - If the file has fewer than 7 tab-delimited columns in the header. - """ - if path is None: - from ...adapter import resolve_adapter - adapter = resolve_adapter( - model, - purpose="apply_custom_kcats needs a customKcats.tsv path " - "(pass `path=` explicitly, or rely on the adapter's " - "default `/data/customKcats.tsv`)", - ) - path = adapter.params.path / "data" / "customKcats.tsv" - path = Path(path) - - if not path.is_file(): - raise FileNotFoundError(f"customKcats.tsv not found: {path}") - - rows = _parse_custom_kcats_tsv(path) - - ec_rxn_ids = model.ec.rxns - no_suffix = [_EXP_SUFFIX_REGEX.sub("", r) for r in ec_rxn_ids] - enzyme_index = {enz: i for i, enz in enumerate(model.ec.enzymes)} - - updated_indices: set[int] = set() - - for row in rows: - proteins, rxns_in, kcat, notes, stoicho = ( - row["proteins"], row["rxns"], row["kcat"], row["notes"], - row["stoicho"], - ) - - # A blank kcat cell parses to NaN. Writing it would mark the - # reaction "unassigned" and silently clear an existing kcat in - # apply_kcat_constraints, so skip the row instead. - if math.isnan(kcat): - logger.warning( - "Row %d: kcat is blank; row skipped (existing kcat kept).", - row["index"], - ) - continue - if kcat < 0: - logger.warning( - "Row %d: kcat %g is negative; row skipped.", - row["index"], kcat, - ) - continue - - if stoicho and any(s.strip() not in ("", "1") for s in stoicho.split("+")): - logger.warning( - "Row %d: stoichiometry %r is ignored. Subunit counts must " - "be set via a separate function (not yet available).", - row["index"], stoicho, - ) - - # Mode A: rxns only. - if not proteins and rxns_in: - matched = _resolve_rxn_ids(rxns_in, no_suffix, ec_rxn_ids) - if not matched: - logger.warning( - "Row %d (rxns=%r): no reactions matched.", - row["index"], rxns_in, - ) - continue - _apply_to_indices(model, matched, kcat, notes, updated_indices) - continue - - # Modes B and C: proteins populated. - if not proteins: - logger.warning( - "Row %d: both 'proteins' and 'rxns' columns are empty; " - "skipping.", row["index"], - ) - continue - - prot_list = [p.strip() for p in proteins.split("+") if p.strip()] - prot_indices: list[int] = [] - for prot in prot_list: - idx = enzyme_index.get(prot) - if idx is None: - logger.warning( - "Row %d: protein %r not found in ec.enzymes.", - row["index"], prot, - ) - prot_indices = [] - break - prot_indices.append(idx) - if not prot_indices: - continue - - # Determine candidate ec.rxns indices. - if rxns_in: - candidate_indices = _resolve_rxn_ids( - rxns_in, no_suffix, ec_rxn_ids - ) - else: - mat_csc = model.ec.rxn_enz_mat.tocsc() - seen: set[int] = set() - for pidx in prot_indices: - col = mat_csc.getcol(pidx) - seen.update(col.nonzero()[0].tolist()) - candidate_indices = sorted(seen) - - if not candidate_indices: - logger.warning( - "Row %d (proteins=%r, rxns=%r): no candidate reactions.", - row["index"], proteins, rxns_in, - ) - continue - - # Apply full-match gate. - prot_set = set(prot_indices) - mat_csr = model.ec.rxn_enz_mat.tocsr() - full_match: list[int] = [] - partial_match: list[tuple[int, float]] = [] - for cand in candidate_indices: - rxn_enzymes = set(mat_csr.getrow(cand).indices.tolist()) - common = prot_set & rxn_enzymes - if len(common) == len(prot_set) == len(rxn_enzymes): - full_match.append(cand) - elif rxn_enzymes: - denom = max(len(prot_set), len(rxn_enzymes)) - fraction = len(common) / denom - if fraction >= 0.5: - partial_match.append((cand, fraction)) - - if full_match: - _apply_to_indices(model, full_match, kcat, notes, updated_indices) - - for cand_idx, fraction in partial_match: - logger.warning( - "Row %d (proteins=%r): partial match (%.0f%%) with reaction " - "%s; not applied. Curate manually.", - row["index"], proteins, fraction * 100, - ec_rxn_ids[cand_idx], - ) - - if not full_match and not partial_match: - logger.warning( - "Row %d (proteins=%r, rxns=%r): no full or partial match.", - row["index"], proteins, rxns_in, - ) - - if not updated_indices: - logger.warning( - "apply_custom_kcats: no reactions were updated. Check that the " - "IDs and proteins in %s match the model.", path, - ) - return - - if apply: - updated_rxn_ids = sorted(ec_rxn_ids[i] for i in updated_indices) - apply_kcat_constraints(model, update_rxns=updated_rxn_ids) - - -def _parse_custom_kcats_tsv(path: Path) -> list[dict]: - """Parse the seven-column TSV into a list of row dicts (keys: - index, proteins, kcat, rxns, notes, stoicho). - """ - rows: list[dict] = [] - with open(path, "r", encoding="utf-8") as f: - header = f.readline().rstrip("\n").split("\t") - if len(header) < 7: - raise ValueError( - f"{path} header has {len(header)} columns; expected at least 7." - ) - for line_no, line in enumerate(f, start=2): - line = line.rstrip("\n") - if not line.strip(): - continue - parts = line.split("\t") - while len(parts) < 7: - parts.append("") - try: - kcat = float(parts[3]) if parts[3].strip() else float("nan") - except ValueError: - logger.warning( - "Row %d in %s: kcat %r is not numeric; row skipped.", - line_no - 1, path, parts[3], - ) - continue - - rows.append({ - "index": line_no - 1, # 1-based row index excluding header - "proteins": parts[0].strip(), - "kcat": kcat, - "rxns": _unquote(parts[4].strip()), - "notes": parts[5].strip(), - "stoicho": parts[6].strip(), - }) - return rows - - -def _unquote(field: str) -> str: - """Strip one pair of double quotes around a field, as MATLAB's ``%q`` does. - - A spreadsheet exports a reaction list such as ``R3,R5`` as ``"R3,R5"``; - a doubled quote inside the field stands for one quote. - """ - if len(field) >= 2 and field.startswith('"') and field.endswith('"'): - return field[1:-1].replace('""', '"') - return field - - -def _resolve_rxn_ids( - rxns_field: str, no_suffix: list[str], ec_rxn_ids: list[str], -) -> list[int]: - """Mode A / Mode C: resolve a comma-separated rxns field to ec-row indices.""" - requested = [r.strip() for r in rxns_field.split(",") if r.strip()] - matched: list[int] = [] - seen: set[int] = set() - for req in requested: - for i, base in enumerate(no_suffix): - if base == req and i not in seen: - matched.append(i) - seen.add(i) - return matched - - -def _apply_to_indices( - model: "EcModel", - indices: list[int], - kcat: float, - notes: str, - updated_indices: set[int], -) -> None: - """Write kcat, source, and notes to ec.* for the given indices.""" - for idx in indices: - model.ec.kcat[idx] = kcat - model.ec.source[idx] = _SOURCE_TAG - if notes: - existing = model.ec.notes[idx] - if existing: - model.ec.notes[idx] = existing + _NOTES_SEPARATOR + notes - else: - model.ec.notes[idx] = notes - updated_indices.add(idx) diff --git a/src/geckopy/ec_model/pipeline/fill_kcats.py b/src/geckopy/ec_model/pipeline/fill_kcats.py index e0254ca..21959ad 100644 --- a/src/geckopy/ec_model/pipeline/fill_kcats.py +++ b/src/geckopy/ec_model/pipeline/fill_kcats.py @@ -56,8 +56,6 @@ def fill_kcats_from_isozymes( Reactions whose siblings all also lack a kcat stay at 0. Reactions with a single isozyme (no siblings beyond themselves) also stay at 0. - ``get_kcat_across_isozymes`` is a deprecated alias for this function. - Parameters ---------- model @@ -146,22 +144,3 @@ def fill_kcats_from_isozymes( if apply: filled_rxn_ids = [model.ec.rxns[i] for i in filled_indices] apply_kcat_constraints(model, update_rxns=filled_rxn_ids) - - -def get_kcat_across_isozymes(model: "EcModel", *, apply: bool = False) -> None: - """Deprecated alias for :func:`fill_kcats_from_isozymes`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``fill_kcats_from_isozymes``. - """ - import warnings - - warnings.warn( - "get_kcat_across_isozymes is deprecated; use " - "fill_kcats_from_isozymes instead. The old name will be " - "removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return fill_kcats_from_isozymes(model, apply=apply) diff --git a/src/geckopy/gather_kcats/__init__.py b/src/geckopy/gather_kcats/__init__.py index ffe121a..7df658d 100644 --- a/src/geckopy/gather_kcats/__init__.py +++ b/src/geckopy/gather_kcats/__init__.py @@ -7,23 +7,19 @@ from .homologs import kcats_from_homologs from .kcat_sources import ec_rxn_keys, load_kcat_source, read_kcat_source, save_kcat_source from .kcats_from import kcats_from_brenda, kcats_from_dlkcat, kcats_from_ec_tree, kcats_from_okp -from .get_standard_kcat import assign_standard_kcat, get_standard_kcat -from .merge_dlkcat_and_fuzzy_kcats import merge_dlkcat_and_fuzzy_kcats +from .get_standard_kcat import assign_standard_kcat from .merge_kcats import merge_kcats, normalize_source from .open_kinetics_predictor import ( OKPClient, OKPError, ) -from .read_dlkcat_output import read_dlkcat_output from .remove_standard_kcat import remove_standard_kcat from .review import EcStats, ec_stats_from_brenda, review_assignment -from .run_dlkcat import run_dlkcat from .select_kcat_value import ( apply_kcat_list, format_kcat_source, - select_kcat_value, ) -from .write_dlkcat_input import extract_enzyme_substrate_pairs, write_dlkcat_input +from .write_dlkcat_input import extract_enzyme_substrate_pairs __all__ = [ "EcStats", @@ -38,23 +34,17 @@ "extract_enzyme_substrate_pairs", "format_kcat_source", "fuzzy_kcat_matching", - "get_standard_kcat", # deprecated; alias of assign_standard_kcat "kcats_from_brenda", "kcats_from_dlkcat", "kcats_from_ec_tree", "kcats_from_homologs", "kcats_from_okp", "load_kcat_source", - "merge_dlkcat_and_fuzzy_kcats", # deprecated; use merge_kcats "merge_kcats", "normalize_source", "read_kcat_source", - "read_dlkcat_output", # deprecated; use kcats_from_dlkcat(via="file") "remove_standard_kcat", "review_assignment", - "run_dlkcat", # deprecated; use kcats_from_dlkcat(via="docker") "save_kcat_source", - "select_kcat_value", # deprecated; alias of apply_kcat_list "update_kcat_sources", - "write_dlkcat_input", # deprecated; use kcats_from_dlkcat(via="docker") ] diff --git a/src/geckopy/gather_kcats/get_standard_kcat.py b/src/geckopy/gather_kcats/get_standard_kcat.py index ae97782..edc7a8a 100644 --- a/src/geckopy/gather_kcats/get_standard_kcat.py +++ b/src/geckopy/gather_kcats/get_standard_kcat.py @@ -449,30 +449,3 @@ def _fill_zero_kcats(model: "EcModel", standard_kcat: float) -> list[str]: for i in indices: model.ec.source[int(i)] = "standard" return [model.ec.rxns[int(i)] for i in indices] - - -def get_standard_kcat( - model: "EcModel", - uniprot_db: "UniprotDB", - *, - threshold: int = 10, - fill_zero_kcat: bool = True, -) -> None: - """Deprecated alias for :func:`assign_standard_kcat`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``assign_standard_kcat``. - """ - import warnings - - warnings.warn( - "get_standard_kcat is deprecated; use assign_standard_kcat " - "instead. The old name will be removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return assign_standard_kcat( - model, uniprot_db, - threshold=threshold, fill_zero_kcat=fill_zero_kcat, - ) diff --git a/src/geckopy/gather_kcats/kcat_sources.py b/src/geckopy/gather_kcats/kcat_sources.py index eba88db..2731024 100644 --- a/src/geckopy/gather_kcats/kcat_sources.py +++ b/src/geckopy/gather_kcats/kcat_sources.py @@ -247,7 +247,7 @@ def save_kcat_source( The ecModel the kcat list refers to. kcat_list A kcat list as returned by the ``kcats_from_*`` functions, - ``fuzzy_kcat_matching``, ``read_dlkcat_output`` or ``merge_kcats``: + ``fuzzy_kcat_matching``, ``kcats_from_dlkcat`` or ``merge_kcats``: columns ``rxn_id`` and ``kcat``, optionally ``substrates``, ``eccode``, ``wildcard_level``, ``origin``, ``source``, ``log10_sd`` and ``detail`` (further ``key=value`` pairs for the diff --git a/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py b/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py deleted file mode 100644 index e92fee7..0000000 --- a/src/geckopy/gather_kcats/merge_dlkcat_and_fuzzy_kcats.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Merge DLKcat and fuzzy-BRENDA kcat lists by priority. - -Thin backward-compatible wrapper around :func:`merge_kcats`, kept for the -common DLKcat + fuzzy-BRENDA case and for parity with the MATLAB name. -New code should call :func:`merge_kcats` directly, which also accepts -single lists that mix several sources (e.g. an OpenKineticsPredictor -result with BRENDA / Sabio-RK / CataPro rows). - -Ported from GECKO MATLAB: -src/deprecated/mergeDLKcatAndFuzzyKcats.m. -""" -from __future__ import annotations - -import warnings - -import pandas as pd - -from .merge_kcats import DATABASE_BOTTOM, DATABASE_TOP, merge_kcats - - -def merge_dlkcat_and_fuzzy_kcats( - dlkcat_kcats: pd.DataFrame, - fuzzy_kcats: pd.DataFrame, - *, - top_origin_limit: int = 4, - bottom_origin_limit: int = 6, - wildcard_limit: int = 3, -) -> pd.DataFrame: - """Merge DLKcat predictions with fuzzy BRENDA matches. - - Deprecated alias for :func:`merge_kcats` with the tiered priority - ``[database_top, dlkcat, database_bottom]`` (BRENDA preferred over - DLKcat, with weaker fuzzy matches as the final fallback). Will be - removed in a future release; switch to ``merge_kcats``. - - Priority (highest first): - - 1. **database_top:** fuzzy ``wildcard_level == 0`` AND - ``origin <= top_origin_limit`` (the best BRENDA matches). - 2. **dlkcat:** any DLKcat row whose reaction is not covered by (1). - 3. **database_bottom:** weaker fuzzy matches - (``wc == 0 AND top < origin <= bottom`` OR - ``0 < wc <= wildcard_limit AND origin <= bottom``) whose reaction - is not covered by (2). - - Parameters - ---------- - dlkcat_kcats - DataFrame produced by ``read_dlkcat_output``. - fuzzy_kcats - DataFrame produced by ``fuzzy_kcat_matching``. - top_origin_limit - Origin upper bound for database_top. Must be in ``[1, 6]``. - Default 4. - bottom_origin_limit - Origin upper bound for database_bottom. Must be in ``[1, 6]``. - wildcard_limit - Maximum wildcard count for the database_bottom wildcard branch. - Must be in ``[0, 3]``. - - Returns - ------- - pandas.DataFrame - Merged kcat list in the canonical 8-column schema. - - Raises - ------ - ValueError - If any limit is outside its allowed range, or if either input - is missing required columns. - """ - warnings.warn( - "merge_dlkcat_and_fuzzy_kcats is deprecated; use merge_kcats " - "instead. The old name will be removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return merge_kcats( - fuzzy_kcats, - dlkcat_kcats, - source_priority=[DATABASE_TOP, "dlkcat", DATABASE_BOTTOM], - top_origin_limit=top_origin_limit, - bottom_origin_limit=bottom_origin_limit, - wildcard_limit=wildcard_limit, - ) diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py b/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py index e5fb8a3..8bc2e24 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py @@ -4,7 +4,7 @@ row, so each prediction is mapped back to the matching reaction(s) via sequence -> ec.genes and SMILES -> metabolite -> ec.rxns, mirroring the MATLAB readOpenKineticsPredictorOutput. The output schema matches -``read_dlkcat_output`` / ``fuzzy_kcat_matching`` so it feeds +``kcats_from_dlkcat`` / ``fuzzy_kcat_matching`` so it feeds ``apply_kcat_list`` directly. """ from __future__ import annotations diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index 9c398c5..1719d32 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -170,7 +170,7 @@ def _fetch_open_kinetics_predictor( (done, kcat_list) ``done`` is True when a result was obtained; ``kcat_list`` is the parsed DataFrame then, otherwise ``None``. The DataFrame matches - the ``read_dlkcat_output`` / ``fuzzy_kcat_matching`` schema and + the ``kcats_from_dlkcat`` / ``fuzzy_kcat_matching`` schema and feeds ``apply_kcat_list``. """ from ...adapter import resolve_adapter diff --git a/src/geckopy/gather_kcats/read_dlkcat_output.py b/src/geckopy/gather_kcats/read_dlkcat_output.py index 3394850..0e15b75 100644 --- a/src/geckopy/gather_kcats/read_dlkcat_output.py +++ b/src/geckopy/gather_kcats/read_dlkcat_output.py @@ -1,7 +1,6 @@ """Read a DLKcat predictions TSV into a kcat_list DataFrame. -``kcats_from_dlkcat(..., via="file")`` reads it; ``read_dlkcat_output`` is a -deprecated alias. +``kcats_from_dlkcat(..., via="file")`` reads it. Ported from GECKO MATLAB: src/gather_kcats/readDLKcatOutput.m. @@ -9,7 +8,6 @@ from __future__ import annotations import logging -import warnings from pathlib import Path from typing import TYPE_CHECKING @@ -34,25 +32,6 @@ ] -def read_dlkcat_output( - model: "EcModel", - file_path: str | Path, - *, - strict: bool = False, -) -> pd.DataFrame: - """Deprecated: use ``kcats_from_dlkcat(..., via="file")``. - - Reads the file as :func:`_read_dlkcat_output` does, after a - ``DeprecationWarning``. - """ - warnings.warn( - 'read_dlkcat_output is deprecated; use kcats_from_dlkcat(..., via="file").', - DeprecationWarning, - stacklevel=2, - ) - return _read_dlkcat_output(model, file_path, strict=strict) - - def _read_dlkcat_output( model: "EcModel", file_path: str | Path, @@ -65,7 +44,7 @@ def _read_dlkcat_output( src/gather_kcats/readDLKcatOutput.m. The input file is the 6-column TSV that DLKcat writes (see - ``run_dlkcat``), with one header line and columns + ``kcats_from_dlkcat`` with ``via="docker"``), with one header line and columns ``rxn_id, gene, substrate, smiles, sequence, kcat``. Rows whose kcat column is not a finite number (``"NA"``, ``"None"``, empty, ``"inf"``, ``"1.2.3"``) are dropped before the reactions are validated @@ -135,7 +114,7 @@ def _read_dlkcat_output( if not valid_mask.any(): raise ValueError( f"{file_path} contains no numeric kcat values. Did you forget " - f"to run run_dlkcat() first?" + f"to run DLKcat first?" ) model_met_names_lower = {m.name.lower() for m in model.metabolites} @@ -190,12 +169,12 @@ def _read_dlkcat_output( unknown_subs_dropped = int(unknown_subs_mask.sum()) if non_numeric_dropped or unknown_subs_dropped: logger.info( - "read_dlkcat_output: read %d row(s) from %s; dropped %d row(s) " + "DLKcat output: read %d row(s) from %s; dropped %d row(s) " "with non-numeric kcat, %d row(s) with an unrecognized " "substrate (a row failing both is counted in each).", n, file_path, non_numeric_dropped, unknown_subs_dropped, ) else: - logger.info("read_dlkcat_output: read %d row(s) from %s.", n, file_path) + logger.info("DLKcat output: read %d row(s) from %s.", n, file_path) return out diff --git a/src/geckopy/gather_kcats/run_dlkcat.py b/src/geckopy/gather_kcats/run_dlkcat.py index 30dba94..348d630 100644 --- a/src/geckopy/gather_kcats/run_dlkcat.py +++ b/src/geckopy/gather_kcats/run_dlkcat.py @@ -1,7 +1,6 @@ """Invoke DLKcat (via Docker) to predict kcat values for a model. -``kcats_from_dlkcat(..., via="docker")`` runs it; ``run_dlkcat`` is a -deprecated alias. +``kcats_from_dlkcat(..., via="docker")`` runs it. Ported from GECKO MATLAB: src/gather_kcats/runDLKcat.m. @@ -10,7 +9,6 @@ import logging import shutil -import warnings import subprocess import tempfile from pathlib import Path @@ -24,25 +22,6 @@ _OUTPUT_FILENAME = "DLKcatOutput.tsv" -def run_dlkcat( - input_path: str | Path, - *, - output_path: Optional[str | Path] = None, - docker_image: str = _DEFAULT_DOCKER_IMAGE, - timeout: Optional[float] = None, -) -> Path: - """Deprecated: use ``kcats_from_dlkcat(..., via="docker")``. - - Runs DLKcat as :func:`_run_dlkcat` does, after a ``DeprecationWarning``. - """ - warnings.warn( - 'run_dlkcat is deprecated; use kcats_from_dlkcat(..., via="docker").', - DeprecationWarning, - stacklevel=2, - ) - return _run_dlkcat(input_path, output_path=output_path, docker_image=docker_image, timeout=timeout) - - def _run_dlkcat( input_path: str | Path, *, @@ -69,7 +48,7 @@ def _run_dlkcat( ---------- input_path Path to the DLKcat input TSV (typically the output of - ``write_dlkcat_input``). + ``kcats_from_dlkcat`` with ``via="docker"``). output_path Where to write DLKcat's predictions. If ``None`` (default), ``input_path`` is overwritten, matching MATLAB. @@ -116,7 +95,7 @@ def _run_dlkcat( ] logger.info( - "run_dlkcat: invoking %s (this may take several minutes, " + "DLKcat: invoking %s (this may take several minutes, " "longer on first run)", docker_image, ) try: @@ -145,7 +124,7 @@ def _run_dlkcat( out_path.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(tmp_output, out_path) - logger.info("run_dlkcat: completed; output written to %s", out_path) + logger.info("DLKcat: completed; output written to %s", out_path) return out_path @@ -159,7 +138,7 @@ def _check_docker_available() -> None: except FileNotFoundError as e: raise RuntimeError( "Cannot find Docker. Install Docker (https://www.docker.com) " - "and ensure the daemon is running before invoking run_dlkcat." + "and ensure the daemon is running before invoking DLKcat." ) from e if result.returncode != 0: raise RuntimeError( diff --git a/src/geckopy/gather_kcats/select_kcat_value.py b/src/geckopy/gather_kcats/select_kcat_value.py index 6c5f9a8..a1e2361 100644 --- a/src/geckopy/gather_kcats/select_kcat_value.py +++ b/src/geckopy/gather_kcats/select_kcat_value.py @@ -208,29 +208,3 @@ def _should_overwrite( return is_unset # "if_higher" return is_unset or new > current - - -def select_kcat_value( - model: "EcModel", - kcat_list: pd.DataFrame, - *, - criteria: Optional[Literal["max", "min", "median", "mean"]] = None, - overwrite: _OverwriteMode = True, -) -> list[str]: - """Deprecated alias for :func:`apply_kcat_list`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``apply_kcat_list``. - """ - import warnings - - warnings.warn( - "select_kcat_value is deprecated; use apply_kcat_list instead. " - "The old name will be removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return apply_kcat_list( - model, kcat_list, criteria=criteria, overwrite=overwrite, - ) diff --git a/src/geckopy/gather_kcats/write_dlkcat_input.py b/src/geckopy/gather_kcats/write_dlkcat_input.py index 988b8a4..4ab4c5e 100644 --- a/src/geckopy/gather_kcats/write_dlkcat_input.py +++ b/src/geckopy/gather_kcats/write_dlkcat_input.py @@ -1,7 +1,6 @@ """Write a DLKcat-compatible TSV input file for an ec model. -``kcats_from_dlkcat(..., via="docker")`` writes it; ``write_dlkcat_input`` is -a deprecated alias. +``kcats_from_dlkcat(..., via="docker")`` writes it. Ported from GECKO MATLAB: src/gather_kcats/writeDLKcatInput.m. @@ -10,7 +9,6 @@ import logging import re -import warnings from pathlib import Path from typing import TYPE_CHECKING, Iterable, Optional @@ -32,29 +30,6 @@ _NORMALIZE_RE = re.compile(r"[^0-9a-zA-Z]+") -def write_dlkcat_input( - model: "EcModel", - output_path: str | Path, - ignore_lists: "DLKcatIgnoreLists", - *, - ec_rxns: Optional[Iterable[str]] = None, - only_with_smiles: bool = True, - overwrite: bool = False, -) -> pd.DataFrame: - """Deprecated: use ``kcats_from_dlkcat(..., via="docker")``. - - Writes the file as :func:`_write_dlkcat_input` does, after a - ``DeprecationWarning``. - """ - warnings.warn( - 'write_dlkcat_input is deprecated; use kcats_from_dlkcat(..., via="docker").', - DeprecationWarning, - stacklevel=2, - ) - return _write_dlkcat_input(model, output_path, ignore_lists, ec_rxns=ec_rxns, - only_with_smiles=only_with_smiles, overwrite=overwrite) - - def _write_dlkcat_input( model: "EcModel", output_path: str | Path, @@ -150,7 +125,7 @@ def _write_dlkcat_input( _write_tsv(output_path, df) logger.info( - "write_dlkcat_input: wrote %d row(s) to %s.", len(df), output_path, + "DLKcat input: wrote %d row(s) to %s.", len(df), output_path, ) return df @@ -164,7 +139,7 @@ def extract_enzyme_substrate_pairs( ) -> pd.DataFrame: """Enumerate (reaction, gene, substrate, SMILES, sequence) rows for an ec model. - Shared core of ``write_dlkcat_input`` and the OpenKineticsPredictor + Shared core of the DLKcat input writer and the OpenKineticsPredictor input builder. Performs the same filtering (ignored metabolites, ``prot_`` pseudometabolites, currency-metabolite pairs) and the same ``only_with_smiles`` handling, but does no file I/O. diff --git a/src/geckopy/get_enzyme_data/__init__.py b/src/geckopy/get_enzyme_data/__init__.py index 8e87cb3..fa00408 100644 --- a/src/geckopy/get_enzyme_data/__init__.py +++ b/src/geckopy/get_enzyme_data/__init__.py @@ -3,8 +3,8 @@ Mirrors GECKO MATLAB's `src/get_enzyme_data/` directory. """ from .copy_ec_to_gem import copy_ec_to_gem -from .ec_from_database import fill_eccodes_from_database, get_ec_from_database -from .ec_from_gem import fill_eccodes_from_gem, get_ec_from_gem +from .ec_from_database import fill_eccodes_from_database +from .ec_from_gem import fill_eccodes_from_gem from .find_ec_in_db import find_ec_in_db __all__ = [ @@ -12,6 +12,4 @@ "fill_eccodes_from_database", "fill_eccodes_from_gem", "find_ec_in_db", - "get_ec_from_database", # deprecated; alias of fill_eccodes_from_database - "get_ec_from_gem", # deprecated; alias of fill_eccodes_from_gem ] diff --git a/src/geckopy/get_enzyme_data/ec_from_database.py b/src/geckopy/get_enzyme_data/ec_from_database.py index cd8967f..5bcaa7e 100644 --- a/src/geckopy/get_enzyme_data/ec_from_database.py +++ b/src/geckopy/get_enzyme_data/ec_from_database.py @@ -286,31 +286,3 @@ def _format_conflict_message( f"proteins {', '.join(protein_ids)}" ) return "\n".join(lines) - - -def get_ec_from_database( - model: "EcModel", - uniprot_db: "UniprotDB", - *, - kegg_db: Optional["KeggDB"] = None, - ec_rxns: Optional[Iterable[str]] = None, - action: Literal["display", "ignore"] = "display", -) -> None: - """Deprecated alias for :func:`fill_eccodes_from_database`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``fill_eccodes_from_database``. - """ - import warnings - - warnings.warn( - "get_ec_from_database is deprecated; use " - "fill_eccodes_from_database instead. The old name will be " - "removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return fill_eccodes_from_database( - model, uniprot_db, kegg_db=kegg_db, ec_rxns=ec_rxns, action=action, - ) diff --git a/src/geckopy/get_enzyme_data/ec_from_gem.py b/src/geckopy/get_enzyme_data/ec_from_gem.py index 12ff0c0..4838367 100644 --- a/src/geckopy/get_enzyme_data/ec_from_gem.py +++ b/src/geckopy/get_enzyme_data/ec_from_gem.py @@ -109,24 +109,3 @@ def fill_eccodes_from_gem( len(invalid), ", ".join(repr(s) for s in invalid), ) - - -def get_ec_from_gem( - model: "EcModel", - ec_rxns: Optional[Iterable[str]] = None, -) -> None: - """Deprecated alias for :func:`fill_eccodes_from_gem`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``fill_eccodes_from_gem``. - """ - import warnings - - warnings.warn( - "get_ec_from_gem is deprecated; use fill_eccodes_from_gem instead. " - "The old name will be removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return fill_eccodes_from_gem(model, ec_rxns=ec_rxns) diff --git a/src/geckopy/kcat_tuning/__init__.py b/src/geckopy/kcat_tuning/__init__.py index 70ef567..ee2aa5e 100644 --- a/src/geckopy/kcat_tuning/__init__.py +++ b/src/geckopy/kcat_tuning/__init__.py @@ -9,9 +9,8 @@ corrections, corrections_tsv, ) -from .find_max_value import find_max_value from .sensitivity_tuning import TunedKcatsResult, sensitivity_tuning -from .sigma_fitter import SigmaFitterResult, fit_sigma, sigma_fitter +from .sigma_fitter import SigmaFitterResult, fit_sigma from . import evotune @@ -23,8 +22,6 @@ "corrections", "corrections_tsv", "evotune", - "find_max_value", "fit_sigma", "sensitivity_tuning", - "sigma_fitter", # deprecated; alias of fit_sigma ] diff --git a/src/geckopy/kcat_tuning/find_max_value.py b/src/geckopy/kcat_tuning/find_max_value.py deleted file mode 100644 index df27338..0000000 --- a/src/geckopy/kcat_tuning/find_max_value.py +++ /dev/null @@ -1,124 +0,0 @@ -"""Find the maximum kcat (or SA-derived kcat) for a set of EC numbers. - -Ported from GECKO 3 MATLAB ``findMaxValue.m``, which GECKO 4 removed. -""" -from __future__ import annotations - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from ..databases.brenda_loader import BrendaData - - -_KCAT_LABEL = "K_cat" -_SA_LABEL = "SA*Mw" - - -def find_max_value( - ec_string: str, - brenda: "BrendaData", -) -> tuple[float, str, str]: - """Find the maximum kinetic value across one or more EC numbers. - - Searches both ``brenda.kcat_max`` and ``brenda.sa_max`` for each EC token - in ``ec_string`` and returns the overall maximum together with - its organism and parameter type. Wildcards in EC tokens (``-``) - trigger prefix matching: ``"1.1.1.-"`` matches any code starting - with ``"1.1.1."``. - - Ported from GECKO 3 MATLAB ``findMaxValue.m``, which GECKO 4 removed. - - Parameters - ---------- - ec_string - Space-separated EC tokens. Each token may optionally start - with ``"EC"`` (matching the MATLAB convention) and may - contain ``-`` wildcards. - brenda - Pre-loaded BRENDA tables. - - Returns - ------- - value - The overall maximum kinetic value (1/s). - organism - The organism contributing the maximum. - parameter - Either ``"K_cat"`` (came from the kcat table) or - ``"SA*Mw"`` (came from the SA-derived kcat table). - - If no EC matches anything in either table, returns - ``(0.0, "", "")``. - """ - tokens = ec_string.split() - if not tokens: - return 0.0, "", "" - - best_value = 0.0 - best_organism = "" - best_parameter = "" - - for token in tokens: - if token.upper().startswith("EC"): - token = token[2:] - if not token: - continue - - # find_max_value is by definition asking for the maximum, so we - # consult the max-aggregated snapshot view regardless of the - # adapter's `kcat_aggregate_brenda` setting. - kcat_value, kcat_org = _max_in_table( - token, brenda.kcat_max, "kcat", "organism", - ) - sa_value, sa_org = _max_in_table( - token, brenda.sa_max, "kcat", "organism", - ) - - if kcat_value >= sa_value and kcat_value > 0: - token_value, token_org, token_param = ( - kcat_value, kcat_org, _KCAT_LABEL, - ) - elif sa_value > 0: - token_value, token_org, token_param = ( - sa_value, sa_org, _SA_LABEL, - ) - else: - continue - - if token_value > best_value: - best_value = token_value - best_organism = token_org - best_parameter = token_param - - return best_value, best_organism, best_parameter - - -def _max_in_table( - token: str, - table, - value_col: str, - organism_col: str, -) -> tuple[float, str]: - """Return (max_value, organism) for the given EC token in the table. - - Wildcard tokens (containing ``-``) prefix-match against the - table's ``ec_code`` column. Otherwise an exact case-insensitive - match is used. - """ - if table.empty: - return 0.0, "" - - if "-" in token: - prefix = token.split("-", 1)[0] - if not prefix: - mask = table["ec_code"].str.len() > 0 - else: - mask = table["ec_code"].str.lower().str.startswith(prefix.lower()) - else: - mask = table["ec_code"].str.lower() == token.lower() - - matched = table[mask] - if matched.empty: - return 0.0, "" - idx = matched[value_col].idxmax() - return float(matched.loc[idx, value_col]), str(matched.loc[idx, organism_col]) diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index 1bff697..e2aa0b7 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -257,32 +257,3 @@ def growth_at(sigma: float) -> float: growth_grid=growth_grid, error_grid=error_grid, ) - - -def sigma_fitter( - model: "EcModel", - *, - growth_rate: Optional[float] = None, - p_tot: Optional[float] = None, - f: Optional[float] = None, - n_sigma_steps: int = 100, - method: Literal["grid", "bisect"] = "grid", -) -> SigmaFitterResult: - """Deprecated alias for :func:`fit_sigma`. - - Kept for backward compatibility. Will be removed in a future - release; switch to ``fit_sigma``. - """ - import warnings - - warnings.warn( - "sigma_fitter is deprecated; use fit_sigma instead. " - "The old name will be removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return fit_sigma( - model, - growth_rate=growth_rate, p_tot=p_tot, f=f, - n_sigma_steps=n_sigma_steps, method=method, - ) diff --git a/src/geckopy/limit_proteins/__init__.py b/src/geckopy/limit_proteins/__init__.py index b41e624..3fd191c 100644 --- a/src/geckopy/limit_proteins/__init__.py +++ b/src/geckopy/limit_proteins/__init__.py @@ -6,7 +6,6 @@ from .constrain_enz_concs import constrain_enz_concs from .constrain_flux_data import ( apply_flux_data_constraints, - constrain_flux_data, ) from .fill_enz_concs import fill_enz_concs from .flexibilize_enz_concs import FlexEnzResult, flexibilize_enz_concs @@ -37,7 +36,6 @@ "calculate_f_factor", "complex_families", "constrain_enz_concs", - "constrain_flux_data", # deprecated; alias of apply_flux_data_constraints "fill_enz_concs", "flexibilize_enz_concs", "get_conc_control_coeffs", diff --git a/src/geckopy/limit_proteins/constrain_flux_data.py b/src/geckopy/limit_proteins/constrain_flux_data.py index 4a34bf0..94e8057 100644 --- a/src/geckopy/limit_proteins/constrain_flux_data.py +++ b/src/geckopy/limit_proteins/constrain_flux_data.py @@ -192,34 +192,3 @@ def apply_flux_data_constraints( hi = flux * (1 + pct / 200.0) rxn.lower_bound = float(min(lo, hi)) rxn.upper_bound = float(max(lo, hi)) - - -def constrain_flux_data( - model: "EcModel", - flux_data: "FluxData", - *, - condition: int | str = 0, - max_min_growth: Literal["max", "min"] = "max", - loose_strict_flux: _LooseStrict = "loose", -) -> None: - """Deprecated alias for :func:`apply_flux_data_constraints`. - - Kept for backward compatibility with the original MATLAB name. - Will be removed in a future release; switch to - ``apply_flux_data_constraints``. - """ - import warnings - - warnings.warn( - "constrain_flux_data is deprecated; use " - "apply_flux_data_constraints instead. The old name will be " - "removed in a future release.", - DeprecationWarning, - stacklevel=2, - ) - return apply_flux_data_constraints( - model, flux_data, - condition=condition, - max_min_growth=max_min_growth, - loose_strict_flux=loose_strict_flux, - ) diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index e45cd32..b80fb98 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -13,6 +13,7 @@ from __future__ import annotations import logging +import math import re import shutil import warnings @@ -23,6 +24,7 @@ import pandas as pd from .ec_model.constants import POOL_EXCHANGE_ID, POOL_ID +from .ec_model.pipeline.apply_kcat import apply_kcat_constraints if TYPE_CHECKING: from .adapter import ModelAdapter @@ -30,9 +32,18 @@ logger = logging.getLogger(__name__) +_EXP_SUFFIX_REGEX = re.compile(r"_EXP_\d+$") +_SOURCE_TAG = "custom" +_NOTES_SEPARATOR = ", " + #: Renamed or replaced functions, as (old name, what to use). RENAMED = ( ("get_standard_kcat", "assign_standard_kcat"), + ("get_kcat_across_isozymes", "fill_kcats_from_isozymes"), + ("get_ec_from_database", "fill_eccodes_from_database"), + ("get_ec_from_gem", "fill_eccodes_from_gem"), + ("sigma_fitter", "fit_sigma"), + ("constrain_flux_data", "apply_flux_data_constraints"), ("select_kcat_value", "apply_kcat_list"), ("merge_dlkcat_and_fuzzy_kcats", "merge_kcats"), ("fuzzy_kcat_matching", "kcats_from_brenda (or kcats_from_ec_tree), then assign_kcats"), @@ -45,6 +56,7 @@ ("build_okp_input_csv", "kcats_from_okp, then assign_kcats"), ("apply_custom_kcats", "convert_custom_kcats (data/kcats/custom.tsv), then assign_kcats"), ("truncate_values", "removed: nothing replaces it"), + ("find_max_value", "removed: nothing replaces it"), ) @@ -57,10 +69,10 @@ def convert_custom_kcats( ) -> tuple[Path, dict]: """Convert ``data/customKcats.tsv`` into ``data/kcats/custom.tsv``. - The kcats are those the deprecated - :func:`~geckopy.ec_model.pipeline.apply_custom_kcats` gives the ecModel, - including which of two entries for the same reaction is kept: one row per reaction, keyed by conventional reaction and enzyme set - (see :func:`~geckopy.gather_kcats.ec_rxn_keys`), with the entry's notes in + The kcats are those GECKO 3 applied to the ecModel from this file + (``applyCustomKcats``), including which of two entries for the same + reaction is kept: one row per reaction, keyed by conventional reaction and + enzyme set (see :func:`~geckopy.gather_kcats.ec_rxn_keys`), with the entry's notes in the note column. The ``stoicho`` column is left out: complex stoichiometry comes from ``apply_complex_data``. The conversion needs the full ecModel; ``custom.tsv`` applies to full and light ecModels. @@ -82,7 +94,6 @@ def convert_custom_kcats( The file written, and a report with ``rows`` (rows written). """ from .adapter import resolve_adapter - from .ec_model.pipeline.apply_custom_kcats import _apply_custom_kcats from .gather_kcats.kcat_sources import _geckopy_version, ec_rxn_keys, format_number if model.ec.gecko_light: @@ -309,3 +320,301 @@ def _stored_predictions(project: Path) -> list[str]: if m and not (data / "kcats" / f"{okp_file(m.group(1))}.tsv").exists() and m.group(1) not in found: found.append(m.group(1)) return found + + +def _apply_custom_kcats( + model: "EcModel", + path: Optional[Path] = None, + *, + apply: bool = True, +) -> None: + """Apply user-curated kcats from a customKcats.tsv file. + + Reads a tab-separated file with seven columns and one header line: + + proteins | genes | gene_name | kcat | rxns | notes | stoicho + + Each row is interpreted in one of three modes, determined by which + of the ``proteins`` and ``rxns`` columns are populated: + + Mode A (rxns only, proteins empty) + Apply the kcat to every entry of ``ec.rxns`` whose ID, after + stripping any ``_EXP_`` suffix, equals one of the listed + reaction IDs (comma-separated). A list in double quotes, as a + spreadsheet exports it, is read without the quotes. Reverse + direction is honored: ``"R2"`` matches ``R2_EXP_*`` but not + ``R2_REV_*``. + + Mode B (proteins only, rxns empty) + Candidate reactions are those catalyzed by any listed protein. + For each candidate, the kcat is applied only if the candidate's + enzyme set exactly matches the listed proteins (full match). + Partial matches (>= 50% but < 100%) are logged for manual review. + + Mode C (both populated) + Same matching rule as Mode B, but candidates are restricted to + reactions matching the listed reaction IDs (with the same + ``_EXP_`` stripping as Mode A). + + Notes from the ``notes`` column are appended to ``ec.notes`` for + each updated reaction (separated by ``, `` from any existing note). + The ``ec.source`` is set to ``"custom"``. The ``stoicho`` column is + parsed but not applied; a warning is logged if any row has + non-trivial stoichiometry. The ``genes`` and ``gene_name`` columns + are read but not used. + + Rows that fail to resolve to any reaction, or whose enzyme match + is only partial, are logged at WARNING level for manual review + rather than raised as errors. + + Parameters + ---------- + model + An EcModel produced by ``make_ec_model``. Mutated in place. + path + Path to the TSV. If None, defaults to + ``adapter.params.path / "data" / "customKcats.tsv"``. + apply + If True (default), call ``apply_kcat_constraints`` after + updating ``ec.kcat`` so the new values reflect in the S matrix. + + Raises + ------ + FileNotFoundError + If the file does not exist. + ValueError + If the file has fewer than 7 tab-delimited columns in the header. + """ + if path is None: + from .adapter import resolve_adapter + adapter = resolve_adapter( + model, + purpose="convert_custom_kcats needs a customKcats.tsv path " + "(pass `path=` explicitly, or rely on the adapter's " + "default `/data/customKcats.tsv`)", + ) + path = adapter.params.path / "data" / "customKcats.tsv" + path = Path(path) + + if not path.is_file(): + raise FileNotFoundError(f"customKcats.tsv not found: {path}") + + rows = _parse_custom_kcats_tsv(path) + + ec_rxn_ids = model.ec.rxns + no_suffix = [_EXP_SUFFIX_REGEX.sub("", r) for r in ec_rxn_ids] + enzyme_index = {enz: i for i, enz in enumerate(model.ec.enzymes)} + + updated_indices: set[int] = set() + + for row in rows: + proteins, rxns_in, kcat, notes, stoicho = ( + row["proteins"], row["rxns"], row["kcat"], row["notes"], + row["stoicho"], + ) + + # A blank kcat cell parses to NaN. Writing it would mark the + # reaction "unassigned" and silently clear an existing kcat in + # apply_kcat_constraints, so skip the row instead. + if math.isnan(kcat): + logger.warning( + "Row %d: kcat is blank; row skipped (existing kcat kept).", + row["index"], + ) + continue + if kcat < 0: + logger.warning( + "Row %d: kcat %g is negative; row skipped.", + row["index"], kcat, + ) + continue + + if stoicho and any(s.strip() not in ("", "1") for s in stoicho.split("+")): + logger.warning( + "Row %d: stoichiometry %r is ignored. Subunit counts must " + "be set via a separate function (not yet available).", + row["index"], stoicho, + ) + + # Mode A: rxns only. + if not proteins and rxns_in: + matched = _resolve_rxn_ids(rxns_in, no_suffix, ec_rxn_ids) + if not matched: + logger.warning( + "Row %d (rxns=%r): no reactions matched.", + row["index"], rxns_in, + ) + continue + _apply_to_indices(model, matched, kcat, notes, updated_indices) + continue + + # Modes B and C: proteins populated. + if not proteins: + logger.warning( + "Row %d: both 'proteins' and 'rxns' columns are empty; " + "skipping.", row["index"], + ) + continue + + prot_list = [p.strip() for p in proteins.split("+") if p.strip()] + prot_indices: list[int] = [] + for prot in prot_list: + idx = enzyme_index.get(prot) + if idx is None: + logger.warning( + "Row %d: protein %r not found in ec.enzymes.", + row["index"], prot, + ) + prot_indices = [] + break + prot_indices.append(idx) + if not prot_indices: + continue + + # Determine candidate ec.rxns indices. + if rxns_in: + candidate_indices = _resolve_rxn_ids( + rxns_in, no_suffix, ec_rxn_ids + ) + else: + mat_csc = model.ec.rxn_enz_mat.tocsc() + seen: set[int] = set() + for pidx in prot_indices: + col = mat_csc.getcol(pidx) + seen.update(col.nonzero()[0].tolist()) + candidate_indices = sorted(seen) + + if not candidate_indices: + logger.warning( + "Row %d (proteins=%r, rxns=%r): no candidate reactions.", + row["index"], proteins, rxns_in, + ) + continue + + # Apply full-match gate. + prot_set = set(prot_indices) + mat_csr = model.ec.rxn_enz_mat.tocsr() + full_match: list[int] = [] + partial_match: list[tuple[int, float]] = [] + for cand in candidate_indices: + rxn_enzymes = set(mat_csr.getrow(cand).indices.tolist()) + common = prot_set & rxn_enzymes + if len(common) == len(prot_set) == len(rxn_enzymes): + full_match.append(cand) + elif rxn_enzymes: + denom = max(len(prot_set), len(rxn_enzymes)) + fraction = len(common) / denom + if fraction >= 0.5: + partial_match.append((cand, fraction)) + + if full_match: + _apply_to_indices(model, full_match, kcat, notes, updated_indices) + + for cand_idx, fraction in partial_match: + logger.warning( + "Row %d (proteins=%r): partial match (%.0f%%) with reaction " + "%s; not applied. Curate manually.", + row["index"], proteins, fraction * 100, + ec_rxn_ids[cand_idx], + ) + + if not full_match and not partial_match: + logger.warning( + "Row %d (proteins=%r, rxns=%r): no full or partial match.", + row["index"], proteins, rxns_in, + ) + + if not updated_indices: + logger.warning( + "convert_custom_kcats: no reactions were updated. Check that the " + "IDs and proteins in %s match the model.", path, + ) + return + + if apply: + updated_rxn_ids = sorted(ec_rxn_ids[i] for i in updated_indices) + apply_kcat_constraints(model, update_rxns=updated_rxn_ids) + + +def _parse_custom_kcats_tsv(path: Path) -> list[dict]: + """Parse the seven-column TSV into a list of row dicts (keys: + index, proteins, kcat, rxns, notes, stoicho). + """ + rows: list[dict] = [] + with open(path, "r", encoding="utf-8") as f: + header = f.readline().rstrip("\n").split("\t") + if len(header) < 7: + raise ValueError( + f"{path} header has {len(header)} columns; expected at least 7." + ) + for line_no, line in enumerate(f, start=2): + line = line.rstrip("\n") + if not line.strip(): + continue + parts = line.split("\t") + while len(parts) < 7: + parts.append("") + try: + kcat = float(parts[3]) if parts[3].strip() else float("nan") + except ValueError: + logger.warning( + "Row %d in %s: kcat %r is not numeric; row skipped.", + line_no - 1, path, parts[3], + ) + continue + + rows.append({ + "index": line_no - 1, # 1-based row index excluding header + "proteins": parts[0].strip(), + "kcat": kcat, + "rxns": _unquote(parts[4].strip()), + "notes": parts[5].strip(), + "stoicho": parts[6].strip(), + }) + return rows + + +def _unquote(field: str) -> str: + """Strip one pair of double quotes around a field, as MATLAB's ``%q`` does. + + A spreadsheet exports a reaction list such as ``R3,R5`` as ``"R3,R5"``; + a doubled quote inside the field stands for one quote. + """ + if len(field) >= 2 and field.startswith('"') and field.endswith('"'): + return field[1:-1].replace('""', '"') + return field + + +def _resolve_rxn_ids( + rxns_field: str, no_suffix: list[str], ec_rxn_ids: list[str], +) -> list[int]: + """Mode A / Mode C: resolve a comma-separated rxns field to ec-row indices.""" + requested = [r.strip() for r in rxns_field.split(",") if r.strip()] + matched: list[int] = [] + seen: set[int] = set() + for req in requested: + for i, base in enumerate(no_suffix): + if base == req and i not in seen: + matched.append(i) + seen.add(i) + return matched + + +def _apply_to_indices( + model: "EcModel", + indices: list[int], + kcat: float, + notes: str, + updated_indices: set[int], +) -> None: + """Write kcat, source, and notes to ec.* for the given indices.""" + for idx in indices: + model.ec.kcat[idx] = kcat + model.ec.source[idx] = _SOURCE_TAG + if notes: + existing = model.ec.notes[idx] + if existing: + model.ec.notes[idx] = existing + _NOTES_SEPARATOR + notes + else: + model.ec.notes[idx] = notes + updated_indices.add(idx) diff --git a/tests/test_deprecated_aliases.py b/tests/test_deprecated_aliases.py deleted file mode 100644 index cb30f44..0000000 --- a/tests/test_deprecated_aliases.py +++ /dev/null @@ -1,218 +0,0 @@ -"""Smoke tests that the deprecated function aliases still work. - -After the rename round, each old function name lives as a thin -``DeprecationWarning``-emitting wrapper around the new canonical -name. These tests just confirm the alias resolves, forwards -arguments, and emits the expected warning class. Behavioural -correctness is covered by the renamed canonical-name tests. -""" -import warnings - - -def _assert_deprecated(callable_, *args, expected_substring: str, **kwargs): - """Call ``callable_`` and assert a DeprecationWarning containing - ``expected_substring`` was raised.""" - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - callable_(*args, **kwargs) - assert any( - issubclass(w.category, DeprecationWarning) - and expected_substring in str(w.message) - for w in recorded - ), ( - f"Expected DeprecationWarning mentioning {expected_substring!r}; " - f"got: {[str(w.message) for w in recorded]}" - ) - - -def test_get_ec_from_gem_alias_emits_warning(): - from geckopy.get_enzyme_data import get_ec_from_gem - from geckopy.ec_model.ec_data import EcData - from geckopy import EcModel - - # An EcModel with no ec.rxns is a valid no-op input; the function - # short-circuits before touching anything, so we can use it to - # exercise the alias-warning path without setting up a full model. - model = EcModel("empty") - model.ec = EcData() - _assert_deprecated( - get_ec_from_gem, model, - expected_substring="fill_eccodes_from_gem", - ) - - -def test_select_kcat_value_alias_emits_warning(): - import pandas as pd - - from geckopy.gather_kcats import select_kcat_value - from geckopy.ec_model.ec_data import EcData - from geckopy import EcModel - - model = EcModel("empty") - model.ec = EcData() - empty_kcat_list = pd.DataFrame( - columns=["rxn_id", "kcat", "wildcard_level", "origin"], - ) - # The underlying call may fail on a malformed kcat_list (missing - # columns, etc.); we only care that the alias emits the warning - # before delegating, not that the delegated call succeeds. - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - select_kcat_value(model, empty_kcat_list) - except Exception: - pass - assert any( - issubclass(w.category, DeprecationWarning) - and "apply_kcat_list" in str(w.message) - for w in recorded - ) - - -def test_constrain_flux_data_alias_emits_warning(): - from pathlib import Path - - from geckopy.databases import load_flux_data - from geckopy.limit_proteins import constrain_flux_data - - # We need a model + flux_data pair. Use a stub model and a flux-data - # file from the test fixtures; the call may raise an inner error - # (the alias only checks for the deprecation warning, not for the - # success of the underlying call), so wrap accordingly. - flux_data_path = ( - Path(__file__).parents[1] - / "tutorials" / "full_ecModel" / "data" / "fluxData.tsv" - ) - if not flux_data_path.is_file(): - import pytest - pytest.skip("flux_data fixture not present") - flux_data = load_flux_data(flux_data_path) - - class _MockModel: - adapter = None - - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - constrain_flux_data(_MockModel(), flux_data) - except (AttributeError, ValueError): - pass # underlying call may fail on the mock; we only test the warning - assert any( - issubclass(w.category, DeprecationWarning) - and "apply_flux_data_constraints" in str(w.message) - for w in recorded - ) - - -def test_sigma_fitter_alias_emits_warning(): - from geckopy.kcat_tuning import sigma_fitter - - class _MockModel: - pass - - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - sigma_fitter(_MockModel()) - except Exception: - pass - assert any( - issubclass(w.category, DeprecationWarning) - and "fit_sigma" in str(w.message) - for w in recorded - ) - - -def test_get_standard_kcat_alias_emits_warning(): - """``get_standard_kcat`` (deprecated) -> ``assign_standard_kcat``.""" - from geckopy.gather_kcats import get_standard_kcat - - class _MockModel: - adapter = None - - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - get_standard_kcat(_MockModel(), None) - except Exception: - pass - assert any( - issubclass(w.category, DeprecationWarning) - and "assign_standard_kcat" in str(w.message) - for w in recorded - ) - - -def test_get_ec_from_database_alias_emits_warning(): - from geckopy.get_enzyme_data import get_ec_from_database - - class _MockModel: - adapter = None - ec = None - - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - get_ec_from_database(_MockModel(), None) - except Exception: - pass - assert any( - issubclass(w.category, DeprecationWarning) - and "fill_eccodes_from_database" in str(w.message) - for w in recorded - ) - - -def test_merge_dlkcat_and_fuzzy_kcats_alias_emits_warning(): - """``merge_dlkcat_and_fuzzy_kcats`` (deprecated) -> ``merge_kcats``.""" - from geckopy.gather_kcats import merge_dlkcat_and_fuzzy_kcats - - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - try: - merge_dlkcat_and_fuzzy_kcats(None, None) - except Exception: - pass - assert any( - issubclass(w.category, DeprecationWarning) - and "merge_kcats" in str(w.message) - for w in recorded - ) - - -def test_sigma_fitter_alias_passes_method(monkeypatch): - import importlib - - module = importlib.import_module("geckopy.kcat_tuning.sigma_fitter") - seen = {} - monkeypatch.setattr(module, "fit_sigma", lambda model, **kwargs: seen.update(kwargs)) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - module.sigma_fitter(object(), method="bisect") - assert seen["method"] == "bisect" - - -def test_dlkcat_entry_points_are_deprecated_aliases(monkeypatch, tmp_path): - """run_dlkcat and read_dlkcat_output warn and forward to the code - kcats_from_dlkcat runs.""" - import importlib - - run_module = importlib.import_module("geckopy.gather_kcats.run_dlkcat") - read_module = importlib.import_module("geckopy.gather_kcats.read_dlkcat_output") - seen = [] - monkeypatch.setattr(run_module, "_run_dlkcat", lambda path, **kwargs: seen.append(("run", path))) - monkeypatch.setattr(read_module, "_read_dlkcat_output", lambda model, path, **kwargs: seen.append(("read", path))) - _assert_deprecated(run_module.run_dlkcat, tmp_path / "in.tsv", expected_substring='via="docker"') - _assert_deprecated(read_module.read_dlkcat_output, None, tmp_path / "out.tsv", expected_substring='via="file"') - assert seen == [("run", tmp_path / "in.tsv"), ("read", tmp_path / "out.tsv")] - - -def test_write_dlkcat_input_is_a_deprecated_alias(monkeypatch, tmp_path): - import importlib - - module = importlib.import_module("geckopy.gather_kcats.write_dlkcat_input") - seen = [] - monkeypatch.setattr(module, "_write_dlkcat_input", lambda model, path, ignore, **kwargs: seen.append(path)) - _assert_deprecated(module.write_dlkcat_input, None, tmp_path / "DLKcat.tsv", None, - expected_substring='via="docker"') - assert seen == [tmp_path / "DLKcat.tsv"] diff --git a/tests/test_fill_kcats.py b/tests/test_fill_kcats.py index 870bdcf..c8b6f1d 100644 --- a/tests/test_fill_kcats.py +++ b/tests/test_fill_kcats.py @@ -271,27 +271,6 @@ def test_filled_kcats_have_isozymes_source(): assert _source(ec_model, "R2_EXP_1") == "setKcatForReactions" -def test_deprecated_alias_still_works(): - """``get_kcat_across_isozymes`` is the legacy name. It now emits - a DeprecationWarning but still forwards to - ``fill_kcats_from_isozymes`` so old callers don't break.""" - import warnings - - from geckopy.ec_model.pipeline import get_kcat_across_isozymes - - ec_model = _ectestgem_ec_model() - set_kcat_for_reactions(ec_model, ["R2_EXP_1"], 100.0, apply=False) - with warnings.catch_warnings(record=True) as recorded: - warnings.simplefilter("always") - get_kcat_across_isozymes(ec_model, apply=False) - assert any( - issubclass(w.category, DeprecationWarning) - and "fill_kcats_from_isozymes" in str(w.message) - for w in recorded - ) - assert _source(ec_model, "R2_EXP_2") == "isozymes" - - # --------------------------------------------------------------------------- # # Adapter-default aggregate (params.kcat_aggregate_isozymes) # --------------------------------------------------------------------------- # diff --git a/tests/test_find_max_value.py b/tests/test_find_max_value.py deleted file mode 100644 index d064d3c..0000000 --- a/tests/test_find_max_value.py +++ /dev/null @@ -1,217 +0,0 @@ -"""Tests for find_max_value.""" -import pandas as pd - -from geckopy.databases import BrendaData -from geckopy.kcat_tuning import find_max_value - - -# --------------------------------------------------------------------------- # -# Fixture builders -# --------------------------------------------------------------------------- # - -def _brenda( - kcat_rows: list[tuple[str, str, str, float]] | None = None, - sa_rows: list[tuple[str, str, float, float]] | None = None, -) -> BrendaData: - """kcat_rows: (ec_code, substrate, organism, kcat). - sa_rows: (ec_code, organism, kcat (= SA*MW already), mw).""" - kcat_rows = kcat_rows or [] - sa_rows = sa_rows or [] - # The same rows fill both the max and median views for these tests; - # find_max_value only consults the max view (via the .kcat back-compat - # alias) so the median rows are inert. - kcat_df = pd.DataFrame( - kcat_rows, columns=["ec_code", "substrate", "organism", "kcat"] - ) - sa_df = pd.DataFrame( - sa_rows, columns=["ec_code", "organism", "kcat", "mw"] - ) - return BrendaData( - kcat_max=kcat_df, kcat_median=kcat_df, - sa_max=sa_df, sa_median=sa_df, - ) - - -# --------------------------------------------------------------------------- # -# Trivial cases -# --------------------------------------------------------------------------- # - -def test_empty_string_returns_empty_result(): - val, org, param = find_max_value("", _brenda()) - assert val == 0.0 - assert org == "" - assert param == "" - - -def test_no_match_in_either_table_returns_empty(): - val, org, param = find_max_value( - "EC9.9.9.9", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 0.0 - assert org == "" - assert param == "" - - -def test_only_kcat_match(): - val, org, param = find_max_value( - "EC1.1.1.1", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 5.0 - assert org == "yeast" - assert param == "K_cat" - - -def test_only_sa_match(): - val, org, param = find_max_value( - "EC1.1.1.1", - _brenda(sa_rows=[("1.1.1.1", "yeast", 8.0, 50.0)]), - ) - assert val == 8.0 - assert org == "yeast" - assert param == "SA*Mw" - - -def test_kcat_wins_over_lower_sa(): - val, _, param = find_max_value( - "EC1.1.1.1", - _brenda( - kcat_rows=[("1.1.1.1", "*", "yeast", 10.0)], - sa_rows=[("1.1.1.1", "yeast", 5.0, 50.0)], - ), - ) - assert val == 10.0 - assert param == "K_cat" - - -def test_sa_wins_over_lower_kcat(): - val, _, param = find_max_value( - "EC1.1.1.1", - _brenda( - kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)], - sa_rows=[("1.1.1.1", "yeast", 10.0, 50.0)], - ), - ) - assert val == 10.0 - assert param == "SA*Mw" - - -# --------------------------------------------------------------------------- # -# Multiple ECs -# --------------------------------------------------------------------------- # - -def test_multiple_ecs_picks_overall_max(): - val, org, _ = find_max_value( - "EC1.1.1.1 EC2.7.7.7", - _brenda(kcat_rows=[ - ("1.1.1.1", "*", "yeast", 5.0), - ("2.7.7.7", "*", "ecoli", 100.0), - ]), - ) - assert val == 100.0 - assert org == "ecoli" - - -def test_multiple_ecs_some_missing(): - val, org, _ = find_max_value( - "EC9.9.9.9 EC1.1.1.1", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 5.0 - assert org == "yeast" - - -# --------------------------------------------------------------------------- # -# EC prefix tolerance -# --------------------------------------------------------------------------- # - -def test_token_with_ec_prefix_matches(): - val, _, _ = find_max_value( - "EC1.1.1.1", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 5.0 - - -def test_token_without_ec_prefix_also_matches(): - """Tolerate the prefix-stripped form.""" - val, _, _ = find_max_value( - "1.1.1.1", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 5.0 - - -def test_lowercase_ec_prefix_tolerated(): - val, _, _ = find_max_value( - "ec1.1.1.1", - _brenda(kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)]), - ) - assert val == 5.0 - - -# --------------------------------------------------------------------------- # -# Wildcard prefix matching (geckopy implementation of MATLAB intent) -# --------------------------------------------------------------------------- # - -def test_wildcard_prefix_matches_all_subclasses(): - val, _, _ = find_max_value( - "EC1.1.1.-", - _brenda(kcat_rows=[ - ("1.1.1.1", "*", "yeast", 5.0), - ("1.1.1.2", "*", "ecoli", 99.0), - ("1.1.2.1", "*", "fugu", 1000.0), # NOT under "1.1.1." - ]), - ) - # Only 1.1.1.1 and 1.1.1.2 qualify; max is 99.0. - assert val == 99.0 - - -def test_full_wildcard_matches_everything(): - val, _, _ = find_max_value( - "EC-.-.-.-", - _brenda(kcat_rows=[ - ("1.1.1.1", "*", "yeast", 5.0), - ("9.9.9.9", "*", "moo", 1000.0), - ]), - ) - assert val == 1000.0 - - -def test_wildcard_searches_both_tables(): - val, _, param = find_max_value( - "EC1.1.-.-", - _brenda( - kcat_rows=[("1.1.1.1", "*", "yeast", 5.0)], - sa_rows=[("1.1.2.1", "ecoli", 100.0, 50.0)], - ), - ) - assert val == 100.0 - assert param == "SA*Mw" - - -# --------------------------------------------------------------------------- # -# Multi-row picking (max within a single matched set) -# --------------------------------------------------------------------------- # - -def test_multiple_rows_for_same_ec_picks_max(): - val, org, _ = find_max_value( - "EC1.1.1.1", - _brenda(kcat_rows=[ - ("1.1.1.1", "*", "low_org", 1.0), - ("1.1.1.1", "*", "high_org", 50.0), - ("1.1.1.1", "*", "mid_org", 10.0), - ]), - ) - assert val == 50.0 - assert org == "high_org" - - -# --------------------------------------------------------------------------- # -# Empty BRENDA tables -# --------------------------------------------------------------------------- # - -def test_empty_brenda_returns_zero(): - val, _, _ = find_max_value("EC1.1.1.1", _brenda()) - assert val == 0.0 diff --git a/tests/test_apply_custom_kcats.py b/tests/test_gecko3_custom_kcats.py similarity index 86% rename from tests/test_apply_custom_kcats.py rename to tests/test_gecko3_custom_kcats.py index 736b8e1..e3c5fac 100644 --- a/tests/test_apply_custom_kcats.py +++ b/tests/test_gecko3_custom_kcats.py @@ -1,4 +1,4 @@ -"""Tests for apply_custom_kcats (legacy TSV-based wrapper).""" +"""Tests for the reader of a GECKO 3 customKcats.tsv that convert_custom_kcats uses.""" import logging from pathlib import Path @@ -6,7 +6,7 @@ import pytest from geckopy import EcModel, ModelAdapter, make_ec_model -from geckopy.ec_model.pipeline import apply_custom_kcats +from geckopy.upgrade import _apply_custom_kcats EXAMPLE_DIR = Path(__file__).parents[1] / "examples" / "ecTestGEM" @@ -56,7 +56,7 @@ def test_mode_a_applies_to_unsuffixed_id(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t50\tR3\tnote_a\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R3") == 50.0 assert _source(ec_model, "R3") == "custom" @@ -69,7 +69,7 @@ def test_mode_a_expands_to_all_isozymes(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR2\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R2_EXP_1") == 10.0 assert _kcat(ec_model, "R2_EXP_2") == 10.0 @@ -82,7 +82,7 @@ def test_mode_a_rev_direction_is_honored(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t20\tR2_REV\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R2_EXP_1") == 0 assert _kcat(ec_model, "R2_REV_EXP_1") == 20.0 @@ -94,7 +94,7 @@ def test_mode_a_multiple_comma_separated_rxns(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t77\tR3, R5\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R3") == 77.0 assert _kcat(ec_model, "R5") == 77.0 @@ -111,7 +111,7 @@ def test_mode_b_single_protein_full_match(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["P3\t\t\t200\t\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R2_EXP_2") == 200.0 assert _kcat(ec_model, "R2_REV_EXP_2") == 200.0 @@ -124,7 +124,7 @@ def test_mode_b_protein_pair_full_match(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["P1 + P2\t\t\t100\t\t\t1 + 1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R2_EXP_1") == 100.0 assert _kcat(ec_model, "R2_REV_EXP_1") == 100.0 @@ -138,7 +138,7 @@ def test_mode_b_partial_match_logs_but_does_not_apply(tmp_path, caplog): _write_tsv(tsv, ["P1\t\t\t999\t\t\t1"]) with caplog.at_level(logging.WARNING): - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R2_EXP_1") == 0 assert "partial match" in caplog.text @@ -150,7 +150,7 @@ def test_mode_b_unknown_protein_logs_warning(tmp_path, caplog): _write_tsv(tsv, ["P_NOT_REAL\t\t\t10\t\t\t1"]) with caplog.at_level(logging.WARNING): - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert "P_NOT_REAL" in caplog.text @@ -166,7 +166,7 @@ def test_mode_c_restricts_to_listed_rxns(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["P3\t\t\t300\tR2\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) # R2 (no _REV) expands to R2_EXP_1 and R2_EXP_2. Only R2_EXP_2 is # catalyzed by P3 alone (full match). @@ -186,7 +186,7 @@ def test_notes_append_to_existing(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR3\tnew note\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _notes(ec_model, "R3") == "pre-existing, new note" @@ -196,7 +196,7 @@ def test_notes_overwrite_when_existing_empty(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR3\tnote\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _notes(ec_model, "R3") == "note" @@ -207,7 +207,7 @@ def test_empty_notes_column_does_not_change_existing(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR3\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _notes(ec_model, "R3") == "keep me" @@ -221,7 +221,7 @@ def test_stoicho_warning_logged_for_nontrivial(tmp_path, caplog): _write_tsv(tsv, ["P1 + P2\t\t\t10\t\t\t3 + 2"]) with caplog.at_level(logging.WARNING): - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert "stoichiometry" in caplog.text @@ -232,7 +232,7 @@ def test_stoicho_trivial_no_warning(tmp_path, caplog): _write_tsv(tsv, ["P1 + P2\t\t\t10\t\t\t1 + 1"]) with caplog.at_level(logging.WARNING): - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert "stoichiometry" not in caplog.text @@ -246,7 +246,7 @@ def test_apply_true_writes_to_s_matrix(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR3\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=True) + _apply_custom_kcats(ec_model, path=tsv, apply=True) r3 = ec_model.reactions.get_by_id("R3") coef = next((c for m, c in r3.metabolites.items() if m.id == "prot_P4"), 0.0) @@ -258,7 +258,7 @@ def test_apply_false_leaves_s_matrix_unchanged(tmp_path): tsv = tmp_path / "customKcats.tsv" _write_tsv(tsv, ["\t\t\t10\tR3\t\t1"]) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) r3 = ec_model.reactions.get_by_id("R3") coef = next((c for m, c in r3.metabolites.items() if m.id == "prot_P4"), 0.0) @@ -269,23 +269,17 @@ def test_apply_false_leaves_s_matrix_unchanged(tmp_path): # File handling and errors # --------------------------------------------------------------------------- # -def test_apply_custom_kcats_is_deprecated(): - ec_model = _ectestgem_ec_model() - with pytest.warns(DeprecationWarning, match="convert_custom_kcats"): - apply_custom_kcats(ec_model, apply=False) - - def test_missing_file_raises(tmp_path): ec_model = _ectestgem_ec_model() with pytest.raises(FileNotFoundError): - apply_custom_kcats(ec_model, path=tmp_path / "no_such.tsv") + _apply_custom_kcats(ec_model, path=tmp_path / "no_such.tsv") def test_default_path_uses_adapter(): """Without an explicit path, the function should look in the adapter's data folder. ecTestGEM ships a customKcats.tsv there.""" ec_model = _ectestgem_ec_model() - apply_custom_kcats(ec_model, apply=False) + _apply_custom_kcats(ec_model, apply=False) # The ecTestGEM customKcats.tsv has rows for P3, P1+P2, and R2_REV+R5. # We rely on at least one having taken effect. has_custom_source = any(s == "custom" for s in ec_model.ec.source) @@ -297,7 +291,7 @@ def test_header_with_too_few_columns_raises(tmp_path): tsv = tmp_path / "bad.tsv" tsv.write_text("only\ttwo\n") with pytest.raises(ValueError, match="header"): - apply_custom_kcats(ec_model, path=tsv) + _apply_custom_kcats(ec_model, path=tsv) def test_no_matches_logs_summary(tmp_path, caplog): @@ -306,7 +300,7 @@ def test_no_matches_logs_summary(tmp_path, caplog): _write_tsv(tsv, ["\t\t\t10\tNOT_A_REACTION\t\t1"]) with caplog.at_level(logging.WARNING): - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert "no reactions were updated" in caplog.text or \ "no reactions matched" in caplog.text @@ -318,5 +312,5 @@ def test_blank_line_skipped(tmp_path): tsv.write_text( HEADER + "\n" + "\t\t\t10\tR3\t\t1\n" + "\n" ) - apply_custom_kcats(ec_model, path=tsv, apply=False) + _apply_custom_kcats(ec_model, path=tsv, apply=False) assert _kcat(ec_model, "R3") == 10.0 diff --git a/tests/test_gecko_matlab_parity.py b/tests/test_gecko_matlab_parity.py index 1145759..abbc069 100644 --- a/tests/test_gecko_matlab_parity.py +++ b/tests/test_gecko_matlab_parity.py @@ -50,7 +50,6 @@ EcModel, ModelAdapter, apply_complex_data, - apply_custom_kcats, apply_kcat_constraints, apply_kcat_list, constrain_enz_concs, @@ -72,8 +71,9 @@ merge_kcats, save_ec_model, set_prot_pool_size, - write_dlkcat_input, ) +from geckopy.gather_kcats.write_dlkcat_input import _write_dlkcat_input +from geckopy.upgrade import _apply_custom_kcats EXAMPLE_DIR = Path(__file__).parents[1] / "examples" / "ecTestGEM" DATA_DIR = EXAMPLE_DIR / "data" @@ -681,9 +681,8 @@ def _dlkcat_list(rxns, genes, substrates, kcats) -> pd.DataFrame: # MATLAB's binary merge always emits "fuzzy followed by dlkcat" regardless # of argument order. geckopy's merge_kcats is n-ary and concatenates the # surviving rows list by list, so the equivalent call passes the fuzzy list -# first: merge_kcats(fuzzy, dlkcat, ...). Note that the deprecated -# merge_dlkcat_and_fuzzy_kcats alias is merge_kcats, so it inherits this -# convention rather than MATLAB's (dlkcat, fuzzy) signature. +# first: merge_kcats(fuzzy, dlkcat, ...). This differs from +# MATLAB's (dlkcat, fuzzy) signature. _MERGE_KWARGS = dict( source_priority=("database_top", "dlkcat", "database_bottom"), top_origin_limit=6, @@ -697,7 +696,7 @@ def test_tc0011_write_dlkcat_input(tmp_path): gene, substrate, sequence); rows 4 and 6 (SMILES, kcat placeholder) are skipped there and here.""" model = _extended_ec_model() - table = write_dlkcat_input( + table = _write_dlkcat_input( model, tmp_path / "DLKcat_input_test.tsv", load_dlkcat_ignore_lists(), @@ -825,11 +824,11 @@ def test_tc0011_get_kcat_across_isozymes(): )) -def test_tc0011_apply_custom_kcats_from_file(): +def test_tc0011_custom_kcats_from_file(): """customKcats.tsv sets P3 -> 200, P1+P2 -> 100, and R2_REV/R5 -> 50.""" model = _constrained_extended_model() apply_kcat_constraints(model) - apply_custom_kcats(model, path=DATA_DIR / "customKcats.tsv", apply=False) + _apply_custom_kcats(model, path=DATA_DIR / "customKcats.tsv", apply=False) assert _kcat_by_rxn(model) == dict(zip( EXP_EXT_EC_RXNS, [100, 200, 50, 50, 100, 200, 1010, 100, 50] diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 1dc7b81..ae80beb 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -12,7 +12,7 @@ from geckopy import ModelAdapter, convert_custom_kcats, make_ec_model from geckopy.databases import load_brenda_data, load_phyl_dist, load_uniprot_tsv -from geckopy.ec_model.pipeline import apply_custom_kcats, apply_kcat_constraints, fill_kcats_from_isozymes +from geckopy.ec_model.pipeline import apply_kcat_constraints, fill_kcats_from_isozymes from geckopy.gather_kcats import ( apply_kcat_list, assign_kcats, @@ -23,10 +23,11 @@ kcats_from_brenda, kcats_from_dlkcat, merge_kcats, - read_dlkcat_output, read_kcat_source, update_kcat_sources, ) +from geckopy.gather_kcats.read_dlkcat_output import _read_dlkcat_output +from geckopy.upgrade import _apply_custom_kcats EXAMPLE_DIR = Path(__file__).parents[1] / "examples" / "ecTestGEM" @@ -70,12 +71,11 @@ def test_assign_kcats_reproduces_chain(tmp_path): a = base.copy() fuzzy = fuzzy_kcat_matching(a, brenda, phyl, adapter=adapter) - dlkcat = read_dlkcat_output(a, data / "DLKcat.tsv") + dlkcat = _read_dlkcat_output(a, data / "DLKcat.tsv") merged = merge_kcats(fuzzy, dlkcat, source_priority=["database_top", "dlkcat", "database_bottom"], top_origin_limit=6) apply_kcat_list(a, merged) - with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): - apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") + _apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") fill_kcats_from_isozymes(a) assign_standard_kcat(a, uniprot) apply_kcat_constraints(a) @@ -107,8 +107,7 @@ def test_gecko_median_preset(tmp_path): merged = merge_kcats(fuzzy, dlkcat, source_priority=["database_top", "dlkcat", "database_bottom"], top_origin_limit=4) apply_kcat_list(a, merged) - with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): - apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") + _apply_custom_kcats(a, path=EXAMPLE_DIR / "data" / "customKcats.tsv") fill_kcats_from_isozymes(a) assign_standard_kcat(a, uniprot) apply_kcat_constraints(a) diff --git a/tests/test_kcat_contract.py b/tests/test_kcat_contract.py index f2e260b..498bc6b 100644 --- a/tests/test_kcat_contract.py +++ b/tests/test_kcat_contract.py @@ -12,10 +12,10 @@ from geckopy.gather_kcats import ( assign_standard_kcat, kcats_from_ec_tree, - read_dlkcat_output, read_kcat_source, - write_dlkcat_input, ) +from geckopy.gather_kcats.read_dlkcat_output import _read_dlkcat_output +from geckopy.gather_kcats.write_dlkcat_input import _write_dlkcat_input from geckopy.gather_kcats.ec_tree import ECTree from geckopy.get_enzyme_data.ec_from_gem import fill_eccodes_from_gem @@ -83,16 +83,16 @@ def write(*rows): encoding="utf-8") write(("R2_EXP_1", "m1", "12.5"), ("R2_EXP_2", "m1", "inf"), ("R2_EXP_2", "m1", "1.2.3")) - out = read_dlkcat_output(model, file) + out = _read_dlkcat_output(model, file) assert list(out["rxn_id"]) == ["R2_EXP_1"] and list(out["kcat"]) == [12.5] write(("R2_EXP_1", "m1", "None"), ("R2_EXP_2", "m1", "None")) with pytest.raises(ValueError, match="no numeric kcat"): - read_dlkcat_output(model, file) + _read_dlkcat_output(model, file) write(("R2_EXP_1", "m1", "12.5"), ("NOTAREACTION_EXP_1", "m1", "NA")) - assert list(read_dlkcat_output(model, file)["rxn_id"]) == ["R2_EXP_1"] + assert list(_read_dlkcat_output(model, file)["rxn_id"]) == ["R2_EXP_1"] write(("R2_EXP_1", "m1", "12.5"), ("NOTAREACTION_EXP_1", "m1", "7")) with pytest.raises(ValueError, match="not present in model.ec.rxns"): - read_dlkcat_output(model, file) + _read_dlkcat_output(model, file) def test_write_dlkcat_input_selection(tmp_path): @@ -100,9 +100,9 @@ def test_write_dlkcat_input_selection(tmp_path): model, _, _ = _project(tmp_path) ignore = load_dlkcat_ignore_lists() target = tmp_path / "DLKcat_in.tsv" - df = write_dlkcat_input(model, target, ignore, ec_rxns=[], overwrite=True, only_with_smiles=False) + df = _write_dlkcat_input(model, target, ignore, ec_rxns=[], overwrite=True, only_with_smiles=False) assert df.empty and target.read_text(encoding="utf-8") == "" - df = write_dlkcat_input(model, target, ignore, ec_rxns=["R5", "R3", "R5"], overwrite=True, + df = _write_dlkcat_input(model, target, ignore, ec_rxns=["R5", "R3", "R5"], overwrite=True, only_with_smiles=False) wanted = [r for r in model.ec.rxns if r in {"R3", "R5"}] assert list(dict.fromkeys(df["rxn_id"])) == wanted diff --git a/tests/test_merge_dlkcat_and_fuzzy_kcats.py b/tests/test_merge_kcats.py similarity index 93% rename from tests/test_merge_dlkcat_and_fuzzy_kcats.py rename to tests/test_merge_kcats.py index e702987..4b2976f 100644 --- a/tests/test_merge_dlkcat_and_fuzzy_kcats.py +++ b/tests/test_merge_kcats.py @@ -1,18 +1,19 @@ -"""Tests for merge_kcats and the deprecated merge_dlkcat_and_fuzzy_kcats.""" +"""Tests for merge_kcats.""" import pandas as pd import pytest -from geckopy.gather_kcats import ( - merge_dlkcat_and_fuzzy_kcats, - merge_kcats, - normalize_source, -) +from geckopy.gather_kcats import merge_kcats, normalize_source +from geckopy.gather_kcats.merge_kcats import DATABASE_BOTTOM, DATABASE_TOP -# The wrapper is intentionally deprecated; silence its warning so the -# legacy-behaviour tests below stay quiet. -pytestmark = pytest.mark.filterwarnings( - "ignore:merge_dlkcat_and_fuzzy_kcats is deprecated:DeprecationWarning" -) + +def _merge_dlkcat_fuzzy(dlkcat_kcats, fuzzy_kcats, **limits): + """merge_kcats for a DLKcat list and a fuzzy BRENDA list.""" + return merge_kcats( + fuzzy_kcats, + dlkcat_kcats, + source_priority=[DATABASE_TOP, "dlkcat", DATABASE_BOTTOM], + **limits, + ) # --------------------------------------------------------------------------- # @@ -88,7 +89,7 @@ def _dlkcat_df(rows: list[dict]) -> pd.DataFrame: @pytest.mark.parametrize("top", [0, 7, -1]) def test_top_origin_out_of_range_raises(top): with pytest.raises(ValueError, match="top_origin_limit"): - merge_dlkcat_and_fuzzy_kcats( + _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([]), top_origin_limit=top, ) @@ -96,7 +97,7 @@ def test_top_origin_out_of_range_raises(top): @pytest.mark.parametrize("bot", [0, 7]) def test_bottom_origin_out_of_range_raises(bot): with pytest.raises(ValueError, match="bottom_origin_limit"): - merge_dlkcat_and_fuzzy_kcats( + _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([]), bottom_origin_limit=bot, ) @@ -104,7 +105,7 @@ def test_bottom_origin_out_of_range_raises(bot): @pytest.mark.parametrize("wc", [-1, 4]) def test_wildcard_limit_out_of_range_raises(wc): with pytest.raises(ValueError, match="wildcard_limit"): - merge_dlkcat_and_fuzzy_kcats( + _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([]), wildcard_limit=wc, ) @@ -112,7 +113,7 @@ def test_wildcard_limit_out_of_range_raises(wc): def test_missing_required_column_raises(): bad_df = pd.DataFrame({"rxn_id": ["r1"]}) # missing most columns with pytest.raises(ValueError, match="missing required"): - merge_dlkcat_and_fuzzy_kcats(bad_df, _fuzzy_df([])) + _merge_dlkcat_fuzzy(bad_df, _fuzzy_df([])) # --------------------------------------------------------------------------- # @@ -120,13 +121,13 @@ def test_missing_required_column_raises(): # --------------------------------------------------------------------------- # def test_both_empty_returns_empty(): - out = merge_dlkcat_and_fuzzy_kcats(_dlkcat_df([]), _fuzzy_df([])) + out = _merge_dlkcat_fuzzy(_dlkcat_df([]), _fuzzy_df([])) assert out.empty assert list(out.columns) == _CANONICAL_COLUMNS def test_only_dlkcat_input(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 5.0), _dlkcat_row("r2", 7.0)]), _fuzzy_df([]), ) @@ -135,7 +136,7 @@ def test_only_dlkcat_input(): def test_only_fuzzy_input(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=1)]), ) @@ -148,7 +149,7 @@ def test_only_fuzzy_input(): # --------------------------------------------------------------------------- # def test_fuzzy_prio1_wins_when_reaction_also_in_dlkcat(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=1)]), top_origin_limit=3, @@ -167,7 +168,7 @@ def test_fuzzy_prio1_uses_default_top_origin_limit_4(): produce a wildly wrong outlier that would otherwise silently outrank a good prediction -- see brenda_loader's filter_outlier_kcats, which this default is meant to pair with.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=6)]), ) @@ -178,7 +179,7 @@ def test_fuzzy_prio1_uses_default_top_origin_limit_4(): def test_fuzzy_prio1_origin_6_qualifies_when_limit_explicitly_widened(): """The pre-4-default behaviour is still reachable by passing top_origin_limit=6 explicitly.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=6)]), top_origin_limit=6, @@ -189,7 +190,7 @@ def test_fuzzy_prio1_origin_6_qualifies_when_limit_explicitly_widened(): def test_fuzzy_with_wildcard_does_not_qualify_for_prio1(): """Wildcard fuzzy is NOT prio1; DLKcat wins.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=1, origin=1)]), top_origin_limit=3, @@ -208,7 +209,7 @@ def test_dlkcat_prio2_when_fuzzy_origin_too_high(): """Fuzzy origin > top_origin_limit -> not prio1. With bottom_origin_limit < fuzzy origin -> not prio3 either. So DLKcat wins (prio2).""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=6)]), top_origin_limit=3, @@ -219,7 +220,7 @@ def test_dlkcat_prio2_when_fuzzy_origin_too_high(): def test_dlkcat_prio2_when_fuzzy_missing(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r2", 5.0, wildcard_level=0, origin=1)]), ) @@ -234,7 +235,7 @@ def test_dlkcat_prio2_when_fuzzy_missing(): def test_fuzzy_prio3_via_origin_branch(): """top < origin <= bottom, wc = 0 -> prio3. DLKcat does NOT cover r1, so fuzzy prio3 wins.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=5)]), top_origin_limit=3, @@ -247,7 +248,7 @@ def test_fuzzy_prio3_via_origin_branch(): def test_fuzzy_prio3_via_wildcard_branch(): """0 < wc <= wildcard_limit AND origin <= bottom -> prio3.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=2, origin=3)]), top_origin_limit=3, @@ -259,7 +260,7 @@ def test_fuzzy_prio3_via_wildcard_branch(): def test_fuzzy_prio3_excluded_when_wildcard_too_high(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=3, origin=1)]), wildcard_limit=2, @@ -268,7 +269,7 @@ def test_fuzzy_prio3_excluded_when_wildcard_too_high(): def test_fuzzy_prio3_excluded_when_origin_above_bottom(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=1, origin=6)]), bottom_origin_limit=3, @@ -279,7 +280,7 @@ def test_fuzzy_prio3_excluded_when_origin_above_bottom(): def test_dlkcat_prio2_wins_over_fuzzy_prio3(): """When the same reaction has both a prio3 fuzzy row AND a DLKcat row, DLKcat (prio2) wins.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=1, origin=3)]), top_origin_limit=3, @@ -297,7 +298,7 @@ def test_dlkcat_prio2_wins_over_fuzzy_prio3(): def test_fuzzy_row_with_na_origin_excluded_from_all_priorities(): """A fuzzy row that didn't match anything (NaN origin / NaN wildcard) must be excluded from prio1 and prio3 alike.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 0.0, wildcard_level=pd.NA, origin=pd.NA)]), ) @@ -310,7 +311,7 @@ def test_fuzzy_row_with_na_origin_excluded_from_all_priorities(): # --------------------------------------------------------------------------- # def test_output_schema_matches_inputs(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r2", 5.0, wildcard_level=0, origin=1)]), ) @@ -318,7 +319,7 @@ def test_output_schema_matches_inputs(): def test_fuzzy_rows_come_before_dlkcat_rows(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r1", 100.0)]), _fuzzy_df([_fuzzy_row("r2", 5.0, wildcard_level=0, origin=1)]), ) @@ -334,7 +335,7 @@ def test_fuzzy_rows_come_before_dlkcat_rows(): # --------------------------------------------------------------------------- # def test_multiple_dlkcat_rows_per_reaction_all_kept_when_prio2(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([ _dlkcat_row("r1", 100.0), _dlkcat_row("r1", 200.0, gene="g2"), @@ -346,7 +347,7 @@ def test_multiple_dlkcat_rows_per_reaction_all_kept_when_prio2(): def test_multiple_fuzzy_rows_per_reaction_all_kept_when_prio1(): - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([]), _fuzzy_df([ _fuzzy_row("r1", 5.0, wildcard_level=0, origin=1, eccode="1.1.1.1"), @@ -361,7 +362,7 @@ def test_multiple_fuzzy_rows_per_reaction_all_kept_when_prio1(): def test_fuzzy_prio1_row_blocks_dlkcat_rows_for_same_reaction(): """One fuzzy prio1 row for r1 makes ALL DLKcat rows for r1 drop out, even though there are multiple.""" - out = merge_dlkcat_and_fuzzy_kcats( + out = _merge_dlkcat_fuzzy( _dlkcat_df([ _dlkcat_row("r1", 100.0), _dlkcat_row("r1", 200.0, gene="g2"), @@ -386,7 +387,7 @@ def test_output_can_feed_apply_kcat_list(tmp_path): from geckopy.ec_model.ec_data import EcData from geckopy.gather_kcats import apply_kcat_list - merged = merge_dlkcat_and_fuzzy_kcats( + merged = _merge_dlkcat_fuzzy( _dlkcat_df([_dlkcat_row("r2", 100.0)]), _fuzzy_df([_fuzzy_row("r1", 5.0, wildcard_level=0, origin=1)]), ) diff --git a/tests/test_parity_round3_u2.py b/tests/test_parity_round3_u2.py index 8de6203..8414726 100644 --- a/tests/test_parity_round3_u2.py +++ b/tests/test_parity_round3_u2.py @@ -1,5 +1,5 @@ """An empty update list in apply_kcat_constraints, and a quoted reaction list in -apply_custom_kcats, as in GECKO.""" +the reader of a GECKO 3 customKcats.tsv, as in GECKO.""" import copy from pathlib import Path from types import SimpleNamespace @@ -9,8 +9,8 @@ import pytest from geckopy import EcModel, ModelAdapter, make_ec_model +from geckopy.upgrade import _apply_custom_kcats from geckopy.ec_model.pipeline import ( - apply_custom_kcats, apply_kcat_constraints, set_kcat_for_reactions, ) @@ -122,7 +122,7 @@ def test_quoted_reaction_list_matches_the_listed_reactions(tmp_path): tsv = tmp_path / "customKcats.tsv" tsv.write_text(HEADER + '\t\t\t50\t"R3,R5"\tquoted\t1\n') - apply_custom_kcats(model, path=tsv, apply=False) + _apply_custom_kcats(model, path=tsv, apply=False) for rxn in ("R3", "R5"): assert model.ec.kcat[model.ec.rxns.index(rxn)] == 50.0 @@ -134,7 +134,7 @@ def test_unquoted_and_single_reaction_lists_are_unchanged(tmp_path): tsv = tmp_path / "customKcats.tsv" tsv.write_text(HEADER + "\t\t\t50\tR3,R5\tplain\t1\n") - apply_custom_kcats(model, path=tsv, apply=False) + _apply_custom_kcats(model, path=tsv, apply=False) assert model.ec.kcat[model.ec.rxns.index("R3")] == 50.0 assert model.ec.kcat[model.ec.rxns.index("R5")] == 50.0 diff --git a/tests/test_read_dlkcat_output.py b/tests/test_read_dlkcat_output.py index 279afaa..a2c91ba 100644 --- a/tests/test_read_dlkcat_output.py +++ b/tests/test_read_dlkcat_output.py @@ -1,4 +1,4 @@ -"""Tests for read_dlkcat_output.""" +"""Tests for the DLKcat output reader.""" from pathlib import Path import cobra @@ -9,7 +9,7 @@ from geckopy.ec_model import EcModel from geckopy.ec_model.ec_data import EcData -from geckopy.gather_kcats import read_dlkcat_output +from geckopy.gather_kcats.read_dlkcat_output import _read_dlkcat_output # --------------------------------------------------------------------------- # @@ -59,7 +59,7 @@ def _write_dlkcat_output(tmp_path: Path, rows: list[tuple]) -> Path: def test_missing_file_raises(tmp_path): model = _ec_model(["r1"], ["alpha"]) with pytest.raises(FileNotFoundError, match="DLKcat output file"): - read_dlkcat_output(model, tmp_path / "missing.tsv") + _read_dlkcat_output(model, tmp_path / "missing.tsv") def test_all_na_kcats_raises(tmp_path): @@ -69,7 +69,7 @@ def test_all_na_kcats_raises(tmp_path): ("r1", "g1", "alpha", "CC", "MASEQ", "NA"), ]) with pytest.raises(ValueError, match="no numeric kcat values"): - read_dlkcat_output(model, p) + _read_dlkcat_output(model, p) def test_empty_kcats_raises(tmp_path): @@ -78,7 +78,7 @@ def test_empty_kcats_raises(tmp_path): ("r1", "g1", "alpha", "CC", "MASEQ", ""), ]) with pytest.raises(ValueError, match="no numeric kcat values"): - read_dlkcat_output(model, p) + _read_dlkcat_output(model, p) # --------------------------------------------------------------------------- # @@ -90,7 +90,7 @@ def test_single_row_basic_parse(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "alpha", "CC", "MASEQ", "5.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 1 row = df.iloc[0] assert row["rxn_id"] == "r1" @@ -110,7 +110,7 @@ def test_multi_row_parse(tmp_path): ("r2", "g2", "beta", "CN", "M2", "7.5"), ("r3", "g3", "gamma", "CO", "M3", "9.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 3 assert list(df["rxn_id"]) == ["r1", "r2", "r3"] assert list(df["kcat"]) == [5.0, 7.5, 9.0] @@ -121,7 +121,7 @@ def test_substrates_and_genes_are_single_element_lists(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "alpha", "CC", "MASEQ", "5.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert isinstance(df.iloc[0]["substrates"], list) assert isinstance(df.iloc[0]["genes"], list) assert len(df.iloc[0]["substrates"]) == 1 @@ -135,7 +135,7 @@ def test_schema_matches_fuzzy_output(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "alpha", "CC", "MASEQ", "5.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert list(df.columns) == [ "rxn_id", "source", "eccode", "substrates", "genes", "kcat", "wildcard_level", "origin", @@ -155,7 +155,7 @@ def test_mixed_valid_and_na_kept_only_valid(tmp_path, caplog): ("r3", "g3", "gamma", "CO", "M3", "9.0"), ]) with caplog.at_level(logging.INFO): - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 2 assert list(df["rxn_id"]) == ["r1", "r3"] assert "dropped 1" in caplog.text @@ -170,7 +170,7 @@ def test_substrate_match_is_case_insensitive(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "ALPHA", "CC", "MASEQ", "5.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 1 # The output preserves the file's casing for the substrate. assert df.iloc[0]["substrates"] == ["ALPHA"] @@ -183,7 +183,7 @@ def test_unknown_substrate_dropped_with_warning(tmp_path, caplog): ("r1", "g1", "completely_unknown", "CC", "M", "5.0"), ]) with caplog.at_level(logging.WARNING): - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) # The single row's substrate is unknown, so it is skipped, not aborted. assert len(df) == 0 assert "substrate name" in caplog.text @@ -197,7 +197,7 @@ def test_unknown_substrates_listed_in_warning(tmp_path, caplog): ("r1", "g1", "phantom", "CC", "M", "5.0"), ]) with caplog.at_level(logging.WARNING): - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 0 assert "ghost" in caplog.text assert "phantom" in caplog.text @@ -209,7 +209,7 @@ def test_strict_raises_on_unknown_substrate(tmp_path): ("r1", "g1", "completely_unknown", "CC", "M", "5.0"), ]) with pytest.raises(ValueError, match="substrate"): - read_dlkcat_output(model, p, strict=True) + _read_dlkcat_output(model, p, strict=True) def test_strict_raises_before_dropping_any_rows(tmp_path): @@ -221,7 +221,7 @@ def test_strict_raises_before_dropping_any_rows(tmp_path): ("r2", "g2", "completely_unknown", "CN", "M", "7.0"), ]) with pytest.raises(ValueError, match="substrate"): - read_dlkcat_output(model, p, strict=True) + _read_dlkcat_output(model, p, strict=True) def test_strict_false_is_the_default(tmp_path): @@ -229,7 +229,7 @@ def test_strict_false_is_the_default(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "completely_unknown", "CC", "M", "5.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 0 @@ -238,7 +238,7 @@ def test_strict_true_does_not_affect_a_clean_file(tmp_path): p = _write_dlkcat_output(tmp_path, [ ("r1", "g1", "alpha", "CC", "M", "5.0"), ]) - df = read_dlkcat_output(model, p, strict=True) + df = _read_dlkcat_output(model, p, strict=True) assert len(df) == 1 @@ -253,7 +253,7 @@ def test_drop_counts_reported_separately(tmp_path, caplog): ("r3", "g3", "unknown_thing", "CO", "M3", "9.0"), ]) with caplog.at_level(logging.INFO): - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 1 assert "1 row(s) with non-numeric kcat" in caplog.text assert "1 row(s) with an unrecognized substrate" in caplog.text @@ -269,7 +269,7 @@ def test_unknown_reaction_raises(tmp_path): ("r_nonexistent", "g1", "alpha", "CC", "M", "5.0"), ]) with pytest.raises(ValueError, match="reaction ID"): - read_dlkcat_output(model, p) + _read_dlkcat_output(model, p) def test_reaction_match_is_case_sensitive(tmp_path): @@ -279,7 +279,7 @@ def test_reaction_match_is_case_sensitive(tmp_path): ("R1", "g1", "alpha", "CC", "M", "5.0"), ]) with pytest.raises(ValueError, match="reaction ID"): - read_dlkcat_output(model, p) + _read_dlkcat_output(model, p) # --------------------------------------------------------------------------- # @@ -294,7 +294,7 @@ def test_unknown_substrate_does_not_drop_good_rows(tmp_path): ("r1", "g1", "alpha", "CC", "M", "5.0"), ("r1", "g1", "unknown_thing", "CN", "M", "7.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) assert len(df) == 1 assert list(df["rxn_id"]) == ["r1"] assert df.iloc[0]["kcat"] == 5.0 @@ -305,7 +305,7 @@ def test_unknown_substrate_does_not_drop_good_rows(tmp_path): # --------------------------------------------------------------------------- # def test_output_can_feed_apply_kcat_list(tmp_path): - """End-to-end: read_dlkcat_output -> apply_kcat_list flow.""" + """End-to-end: the DLKcat output reader -> apply_kcat_list flow.""" from geckopy.gather_kcats import apply_kcat_list model = _ec_model(["r1", "r2"], ["alpha", "beta"]) @@ -313,7 +313,7 @@ def test_output_can_feed_apply_kcat_list(tmp_path): ("r1", "g1", "alpha", "CC", "M", "5.0"), ("r2", "g2", "beta", "CN", "M", "7.0"), ]) - df = read_dlkcat_output(model, p) + df = _read_dlkcat_output(model, p) updated = apply_kcat_list(model, df) assert sorted(updated) == ["r1", "r2"] # apply_kcat_list lowercases the source token (no fuzzy metadata). diff --git a/tests/test_run_dlkcat.py b/tests/test_run_dlkcat.py index 1016a41..fab8c63 100644 --- a/tests/test_run_dlkcat.py +++ b/tests/test_run_dlkcat.py @@ -1,10 +1,10 @@ -"""Tests for run_dlkcat. All Docker invocations are mocked.""" +"""Tests for the DLKcat runner. All Docker invocations are mocked.""" import subprocess from pathlib import Path import pytest -from geckopy.gather_kcats import run_dlkcat +from geckopy.gather_kcats.run_dlkcat import _run_dlkcat # --------------------------------------------------------------------------- # @@ -77,7 +77,7 @@ def _write_input(tmp_path: Path, name: str = "in.tsv") -> Path: def test_missing_input_raises(tmp_path): with pytest.raises(FileNotFoundError, match="DLKcat input file not found"): - run_dlkcat(tmp_path / "missing.tsv") + _run_dlkcat(tmp_path / "missing.tsv") # --------------------------------------------------------------------------- # @@ -89,7 +89,7 @@ def test_docker_not_installed_raises(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) with pytest.raises(RuntimeError, match="Cannot find Docker"): - run_dlkcat(in_path) + _run_dlkcat(in_path) def test_docker_version_nonzero_raises(tmp_path, monkeypatch): @@ -97,7 +97,7 @@ def test_docker_version_nonzero_raises(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) with pytest.raises(RuntimeError, match="Docker is installed but"): - run_dlkcat(in_path) + _run_dlkcat(in_path) # --------------------------------------------------------------------------- # @@ -110,7 +110,7 @@ def test_happy_path_overwrites_input_by_default(tmp_path, monkeypatch): in_path = _write_input(tmp_path) original_input = in_path.read_text(encoding="utf-8") - out = run_dlkcat(in_path) + out = _run_dlkcat(in_path) assert out == in_path new_content = in_path.read_text(encoding="utf-8") @@ -124,7 +124,7 @@ def test_happy_path_writes_to_explicit_output_path(tmp_path, monkeypatch): in_path = _write_input(tmp_path) out_path = tmp_path / "predictions.tsv" - result = run_dlkcat(in_path, output_path=out_path) + result = _run_dlkcat(in_path, output_path=out_path) assert result == out_path assert out_path.is_file() @@ -138,7 +138,7 @@ def test_explicit_output_path_creates_parent_dir(tmp_path, monkeypatch): in_path = _write_input(tmp_path) out_path = tmp_path / "newdir" / "predictions.tsv" - result = run_dlkcat(in_path, output_path=out_path) + result = _run_dlkcat(in_path, output_path=out_path) assert result == out_path assert out_path.is_file() @@ -149,7 +149,7 @@ def test_invokes_default_image(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) - run_dlkcat(in_path) + _run_dlkcat(in_path) docker_run_calls = [ c for c in spy.calls @@ -164,7 +164,7 @@ def test_invokes_custom_image_when_specified(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) - run_dlkcat(in_path, docker_image="my/custom:tag") + _run_dlkcat(in_path, docker_image="my/custom:tag") docker_run_calls = [ c for c in spy.calls @@ -186,7 +186,7 @@ def test_docker_run_nonzero_exit_raises(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) with pytest.raises(RuntimeError, match="DLKcat Docker run failed"): - run_dlkcat(in_path) + _run_dlkcat(in_path) def test_docker_run_succeeds_but_no_output_raises(tmp_path, monkeypatch): @@ -194,7 +194,7 @@ def test_docker_run_succeeds_but_no_output_raises(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) with pytest.raises(RuntimeError, match="produced no output file"): - run_dlkcat(in_path) + _run_dlkcat(in_path) def test_docker_run_failure_does_not_overwrite_input(tmp_path, monkeypatch): @@ -209,7 +209,7 @@ def test_docker_run_failure_does_not_overwrite_input(tmp_path, monkeypatch): original = in_path.read_text(encoding="utf-8") with pytest.raises(RuntimeError): - run_dlkcat(in_path) + _run_dlkcat(in_path) # Input is unchanged. assert in_path.read_text(encoding="utf-8") == original @@ -225,7 +225,7 @@ def test_mounts_a_temp_dir_not_the_project_folder(tmp_path, monkeypatch): monkeypatch.setattr(subprocess, "run", spy) in_path = _write_input(tmp_path) - run_dlkcat(in_path) + _run_dlkcat(in_path) docker_run_call = [ c for c in spy.calls if c[:2] == ["docker", "run"] diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index 5b581c0..1c42395 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -56,7 +56,7 @@ def _stoich(model): def test_convert_custom_kcats(tmp_path): """ecTestGEM's customKcats.tsv: P3 200 and P1 + P2 100 (proteins only), then R2_REV and R5 50 (reactions only), which replaces P1 + P2 on R2_REV, - as apply_custom_kcats does.""" + as GECKO 3 applied them.""" model, adapter, project = _project(tmp_path) path, report = convert_custom_kcats(model, adapter=adapter) assert path == project / "data" / "kcats" / "custom.tsv" @@ -71,8 +71,8 @@ def test_convert_custom_kcats(tmp_path): def test_converted_custom_kcats_give_same_kcats(tmp_path): """collect_kcats refuses the GECKO 3 file; once converted, the custom kcats - are those the deprecated apply_custom_kcats gives.""" - from geckopy.ec_model.pipeline import apply_custom_kcats + are those GECKO 3 applied from the file.""" + from geckopy.upgrade import _apply_custom_kcats model, adapter, project = _project(tmp_path) kcats_from_brenda(model, adapter=adapter) @@ -81,8 +81,7 @@ def test_converted_custom_kcats_give_same_kcats(tmp_path): convert_custom_kcats(model, adapter=adapter) b, _, _ = collect_kcats(model, adapter=adapter, write_report=False) ref = model.copy() - with pytest.warns(DeprecationWarning, match="apply_custom_kcats"): - apply_custom_kcats(ref, apply=False) + _apply_custom_kcats(ref, apply=False) custom = [i for i, s in enumerate(ref.ec.source) if s == "custom"] assert custom assert list(b["source"].iloc[custom]) == ["custom"] * len(custom) @@ -168,3 +167,39 @@ def test_upgrade_project_names_the_replacements_of_removed_functions(tmp_path): assert replaced["write_dlkcat_input"].startswith('kcats_from_dlkcat(via="docker")') assert replaced["apply_custom_kcats"].startswith("convert_custom_kcats") assert replaced["truncate_values"].startswith("removed") + + +REMOVED_ALIASES = { + "run_dlkcat": 'kcats_from_dlkcat(via="docker")', + "read_dlkcat_output": 'kcats_from_dlkcat(via="file")', + "write_dlkcat_input": 'kcats_from_dlkcat(via="docker")', + "apply_custom_kcats": "convert_custom_kcats", + "get_kcat_across_isozymes": "fill_kcats_from_isozymes", + "get_standard_kcat": "assign_standard_kcat", + "select_kcat_value": "apply_kcat_list", + "merge_dlkcat_and_fuzzy_kcats": "merge_kcats", + "get_ec_from_database": "fill_eccodes_from_database", + "get_ec_from_gem": "fill_eccodes_from_gem", + "sigma_fitter": "fit_sigma", + "constrain_flux_data": "apply_flux_data_constraints", + "find_max_value": "removed", +} + + +@pytest.mark.parametrize("name", sorted(REMOVED_ALIASES)) +def test_removed_alias_is_gone_and_upgrade_names_its_replacement(name): + import geckopy + from geckopy.upgrade import RENAMED + + assert not hasattr(geckopy, name) + assert name not in geckopy.__all__ + assert dict(RENAMED)[name].startswith(REMOVED_ALIASES[name]) + + +def test_upgrade_project_reports_every_removed_alias(tmp_path): + model, adapter, project = _project(tmp_path) + (project / "run.py").write_text("\n".join(f"{name}(m)" for name in REMOVED_ALIASES) + "\n", encoding="utf-8") + report = upgrade_project(project) + notes = list(report.loc[report["step"] == "script", "note"]) + for name, replacement in REMOVED_ALIASES.items(): + assert any(f"{name} -> {replacement}" in note for note in notes), name diff --git a/tests/test_write_dlkcat_input.py b/tests/test_write_dlkcat_input.py index cf9593f..98c286b 100644 --- a/tests/test_write_dlkcat_input.py +++ b/tests/test_write_dlkcat_input.py @@ -1,4 +1,4 @@ -"""Tests for write_dlkcat_input.""" +"""Tests for the DLKcat input writer.""" from pathlib import Path import cobra @@ -10,7 +10,7 @@ from geckopy.databases import DLKcatIgnoreLists from geckopy.ec_model import EcModel from geckopy.ec_model.ec_data import EcData -from geckopy.gather_kcats import write_dlkcat_input +from geckopy.gather_kcats.write_dlkcat_input import _write_dlkcat_input # --------------------------------------------------------------------------- # @@ -131,14 +131,14 @@ def test_existing_file_refused_without_overwrite(tmp_path): out.write_text("preexisting", encoding="utf-8") model = _ec_model([]) with pytest.raises(FileExistsError): - write_dlkcat_input(model, out, _ignore_lists()) + _write_dlkcat_input(model, out, _ignore_lists()) def test_existing_file_overwritten_when_requested(tmp_path): out = tmp_path / "DLKcat.tsv" out.write_text("preexisting", encoding="utf-8") model = _ec_model([]) - write_dlkcat_input(model, out, _ignore_lists(), overwrite=True) + _write_dlkcat_input(model, out, _ignore_lists(), overwrite=True) # File was replaced; no rows since model is empty. assert out.read_text(encoding="utf-8") == "" @@ -146,7 +146,7 @@ def test_existing_file_overwritten_when_requested(tmp_path): def test_empty_model_writes_empty_file(tmp_path): model = _ec_model([]) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) assert df.empty assert out.exists() assert out.read_text(encoding="utf-8") == "" @@ -164,7 +164,7 @@ def test_single_reaction_single_substrate_single_gene(tmp_path): met_smiles={"A": "C(C)O"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) assert len(df) == 1 row = df.iloc[0] assert row["rxn_id"] == "r1" @@ -184,7 +184,7 @@ def test_complex_with_multiple_genes_yields_multiple_rows(tmp_path): met_smiles={"A": "SMI_A", "B": "SMI_B"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) assert len(df) == 4 triples = sorted(zip(df["substrate"], df["gene"])) assert triples == [("alpha", "g1"), ("alpha", "g2"), @@ -198,7 +198,7 @@ def test_only_substrates_not_products_emitted(tmp_path): met_smiles={"A": "SMI_A", "B": "SMI_B"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) assert list(df["substrate"]) == ["alpha"] @@ -213,7 +213,7 @@ def test_ignored_metabolite_by_name_dropped(tmp_path): met_smiles={"A": "SMI_A", "B": "O"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(ignore_names=["h2o"]), ) assert list(df["substrate"]) == ["Alpha"] @@ -226,7 +226,7 @@ def test_ignored_metabolite_by_smiles_dropped(tmp_path): met_smiles={"A": "SMI_A", "B": "O"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(ignore_smiles=["O"]), ) assert list(df["substrate"]) == ["alpha"] @@ -241,7 +241,7 @@ def test_protein_pseudometabolites_dropped(tmp_path): met_smiles={"A": "SMI_A"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) assert list(df["substrate"]) == ["alpha"] @@ -263,7 +263,7 @@ def test_currency_pair_removed_when_other_substrate_remains(tmp_path): met_smiles={"atp": "SMI_ATP", "glc": "SMI_GLC"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(currency_pairs=[("atp", "adp")]), ) assert list(df["substrate"]) == ["glucose"] @@ -281,7 +281,7 @@ def test_currency_pair_preserved_when_no_other_substrate(tmp_path): met_smiles={"atp": "SMI_ATP"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(currency_pairs=[("atp", "adp")]), ) # ATP is preserved as the substrate. @@ -300,7 +300,7 @@ def test_currency_pair_only_when_both_present(tmp_path): met_smiles={"atp": "SMI_ATP"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(currency_pairs=[("atp", "adp")]), ) assert list(df["substrate"]) == ["ATP"] @@ -320,7 +320,7 @@ def test_only_with_smiles_drops_no_smiles_rows(tmp_path): met_smiles={"A": "SMI_A"}, # B has no SMILES ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists(), only_with_smiles=True) + df = _write_dlkcat_input(model, out, _ignore_lists(), only_with_smiles=True) assert list(df["substrate"]) == ["alpha"] @@ -331,7 +331,7 @@ def test_without_only_with_smiles_writes_None_placeholder(tmp_path): # No SMILES. ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists(), only_with_smiles=False) + df = _write_dlkcat_input(model, out, _ignore_lists(), only_with_smiles=False) assert list(df["smiles"]) == ["None"] @@ -349,7 +349,7 @@ def test_ec_rxns_subset_only_emitted(tmp_path): met_smiles={"A": "SMI_A", "B": "SMI_B"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input( + df = _write_dlkcat_input( model, out, _ignore_lists(), ec_rxns=["r1"], ) assert list(df["rxn_id"]) == ["r1"] @@ -363,7 +363,7 @@ def test_ec_rxns_unknown_id_raises(tmp_path): ) out = tmp_path / "DLKcat.tsv" with pytest.raises(ValueError, match="not present in model.ec.rxns"): - write_dlkcat_input( + _write_dlkcat_input( model, out, _ignore_lists(), ec_rxns=["nonexistent"], ) @@ -381,7 +381,7 @@ def test_gecko_light_strips_4char_prefix_to_find_reaction(tmp_path): met_smiles={"A": "SMI_A"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) # ec.rxns = ["001_r1"]; cobra reactions = ["r1"]. ec.rxns ID kept in output. assert list(df["rxn_id"]) == ["001_r1"] @@ -398,7 +398,7 @@ def test_written_file_matches_returned_dataframe(tmp_path): met_smiles={"A": "SMI_A"}, ) out = tmp_path / "DLKcat.tsv" - df = write_dlkcat_input(model, out, _ignore_lists()) + df = _write_dlkcat_input(model, out, _ignore_lists()) on_disk = _read_tsv(out) pd.testing.assert_frame_equal( df.reset_index(drop=True), on_disk.reset_index(drop=True), @@ -413,7 +413,7 @@ def test_written_file_has_no_header(tmp_path): met_smiles={"A": "SMI_A"}, ) out = tmp_path / "DLKcat.tsv" - write_dlkcat_input(model, out, _ignore_lists()) + _write_dlkcat_input(model, out, _ignore_lists()) first_line = out.read_text(encoding="utf-8").splitlines()[0] # Should be the data row, NOT a header. assert first_line.startswith("r1\t") @@ -434,7 +434,7 @@ def test_a_reloaded_model_carries_its_smiles_as_a_list(tmp_path): if met.annotation.get("smiles"): met.annotation["smiles"] = [met.annotation["smiles"]] - df = write_dlkcat_input(model, tmp_path / "DLKcat.tsv", _ignore_lists()) + df = _write_dlkcat_input(model, tmp_path / "DLKcat.tsv", _ignore_lists()) assert len(df) == 1 assert df.iloc[0]["smiles"] == "C(C)O" @@ -450,7 +450,7 @@ def test_a_list_valued_smiles_is_still_matched_against_the_ignore_list(tmp_path) if met.annotation.get("smiles"): met.annotation["smiles"] = [met.annotation["smiles"]] - df = write_dlkcat_input( + df = _write_dlkcat_input( model, tmp_path / "DLKcat.tsv", _ignore_lists(ignore_smiles=["O"]), ) assert set(df["substrate"]) == {"alpha"} From ef95b47b0beb6d78cffefd0bf23d835a9b215d4c Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 12 Nov 2026 14:38:58 +0100 Subject: [PATCH 42/42] doc: drop the removed aliases from the docs and changelog --- CHANGELOG.md | 10 ++--- docs/gecko_light_status.md | 2 +- docs/migrating_from_gecko_matlab.md | 58 ++++++++++++++--------------- docs/yaml_format.md | 2 +- tutorials/light_ecModel/protocol.py | 6 +-- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38af532..ebf5068 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,19 +3,17 @@ ## Unreleased * `uniprot.id` and `kegg.id` are empty by default instead of the identifiers of Saccharomyces cerevisiae (`"559292"` and `"sce"`), as in GECKO; `adapterParameters.tsv` is the same as GECKO's. `geckopy init` writes both uncommented as `""`, with examples in the comments. `make_ec_model` and `calculate_f_factor` raise `FileNotFoundError` naming `[uniprot] id` when `data/uniprot.tsv` is missing and the identifier is empty, as GECKO's `loadDatabases` raises `loadDatabases:noUniprotID`; an empty `kegg.id` leaves KEGG out with a warning. A project that relied on the yeast defaults sets the identifiers in model_adapter.toml. -* `upgrade_project` (`geckopy upgrade`) reports the calls of `run_dlkcat`, `write_dlkcat_input`, `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values` in the project's scripts with their replacement, and names `convert_custom_kcats` for `apply_custom_kcats` and the `via` of `kcats_from_dlkcat` for `read_dlkcat_output`. -* `write_dlkcat_input` is a deprecated alias that warns with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` writes the DLKcat input with the same code, as GECKO's `kcatsFromDLKcat` does after `writeDLKcatInput` became its local function. `extract_enzyme_substrate_pairs` stays public. +* `upgrade_project` (`geckopy upgrade`) reports the calls of removed or replaced functions in the project's scripts with their replacement, for example `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values`. +* The deprecated aliases with GECKO 3 names are removed from `geckopy`: `run_dlkcat`, `read_dlkcat_output`, `write_dlkcat_input`, `apply_custom_kcats`, `get_kcat_across_isozymes`, `get_standard_kcat`, `select_kcat_value`, `merge_dlkcat_and_fuzzy_kcats`, `get_ec_from_database`, `get_ec_from_gem`, `sigma_fitter` and `constrain_flux_data`, and `find_max_value`, which ports `findMaxValue` that GECKO 4 removed. `upgrade_project` (`geckopy upgrade`) reports each of them in a project's scripts with its replacement, which `geckopy.upgrade.RENAMED` lists. `extract_enzyme_substrate_pairs` stays public. * The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. * The default of `okp.method` (the OpenKineticsPredictor method of `kcats_from_okp` without `method`) is `KinForm-H` instead of `CataPro`, as in GECKO. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. -* `run_dlkcat` and `read_dlkcat_output` are deprecated aliases that warn with a `DeprecationWarning`; `kcats_from_dlkcat(..., via="docker")` and `kcats_from_dlkcat(..., via="file")` run the same code. GECKO's `runDLKcat` and `readDLKcatOutput` are deprecated in the same way. -* `apply_custom_kcats` is deprecated and warns with a `DeprecationWarning`; `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` no longer apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`, before they change the model. The decisions no longer have the stage `custom`. GECKO's `applyCustomKcats` and `collectKcats` do the same. +* `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` do not apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`, before they change the model. The decisions have no stage `custom`. GECKO's `collectKcats` does the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. * `get_conc_control_coeffs` raises `ValueError` when the solve of the model as given is not optimal (for example infeasible), as GECKO's `getConcControlCoeffs` raises `getConcControlCoeffs:infeasible`; it returned no analysed proteins and zero coefficients. * `ec_fseof` follows `ecFSEOF.m` in its labels, default and gene targets. `target_type` is `OE`, `KD` or `KO` instead of `amplify`, `knockdown` or `knockout`. `n_steps` defaults to 16 instead of 10. It returns an `EcFSEOFResult` (exported from `geckopy` and `geckopy.utilities`), raven-toolbox's `FSEOFResult` whose `gene_targets` has one row per gene with the columns `gene`, `name`, `slope` (the mean absolute slope of its target reactions), `action`, `essentiality` and `reactions`, ordered by slope: a gene with several actions is `OE` when one of them is `OE`, otherwise `KO` when it is not essential and `KD` when it is, essential meaning that growth is infeasible or below 1e-8 with the usage of its enzymes blocked and the production fixed at the last enforced level. `gene_targets` had the columns `gene`, `target_type` (the actions joined by `;`), `reactions` and `max_abs_slope`. * `ec_fseof` raises `ValueError` when the top of the scan is not above the flux of the production target at maximum growth by more than `1e-9 * max(1, |top|)`, also for a target that cannot carry flux; GECKO's `ecFSEOF` raises `ecFSEOF:noRange` in the same case. The scan used to run on levels that do not rise and returned no targets. * A `[kcat_sources.]` table of model_adapter.toml overrides only the keys it sets of the preset's settings for that source, as in GECKO; it replaced them, so `[kcat_sources.brenda]` without `aggregation` under a median preset gathered BRENDA with the default aggregation (the maximum). -* The deprecated alias `sigma_fitter` takes `method` and passes it to `fit_sigma`. * `calculate_f_factor` takes no `prot_data` as well: it then reads `data/paxDB.tsv` of the adapter's project with `load_pax_db`, and without that file logs a warning and returns the default f of 0.5, as GECKO's `calculateFfactor` does. `prot_data` was required. * `level_complex_concs` takes `families`, the complexes of the model as `complex_families` returns them (default: calculated from the model), as GECKO's `levelComplexConcs` does. * `make_ec_model` without `uniprot_db` or `kegg_db` downloads a missing `data/uniprot.tsv` or `data/kegg.tsv` with the `[uniprot]` and `[kegg]` settings of the adapter, as GECKO's `makeEcModel` does through `loadDatabases`; an empty `kegg.id` leaves KEGG out with a warning. It used to raise `FileNotFoundError` for a missing `uniprot.tsv` and to use no KEGG data without `kegg.tsv`, so a project without `kegg.tsv` and with a `kegg.id` now gets the KEGG fallback for genes UniProt does not know. @@ -28,7 +26,7 @@ * `get_complex_data` without a taxonomic ID (`taxonomic_id=None`) logs a warning and returns no complexes, without a query or a file written, as GECKO's `getComplexData` does; it raised `ValueError`. * `geckopy init` writes a `.gitignore` with `data/okpApiKey.txt` in the new project, and the geckopy repository ignores `okpApiKey.txt`, so the OpenKineticsPredictor API key file stays out of git. The help said the file was git-ignored, which was not the case. * `kcats_from_dlkcat` takes `via`: `"okp"` predicts with the DLKcat method of OpenKineticsPredictor (as `kcats_from_okp`), `"file"` reads `data/DLKcat.tsv` and `"docker"` runs DLKcat locally. The default is `"file"` when `data/DLKcat.tsv` exists and `"okp"` otherwise; `run=True` is the same as `via="docker"`. It returns `(kcat_list, done)` as `kcats_from_okp` does, with `kcat_list` `None` while an OKP job runs, and takes `wait`. `update_kcat_sources` gathers `dlkcat` through OKP when `data/DLKcat.tsv` is missing, needs an OKP API key for it and reports `pending` while the job runs, where it needed Docker. GECKO's `kcatsFromDLKcat` has the same option. -* `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. `merge_dlkcat_and_fuzzy_kcats` stays a deprecated alias of `merge_kcats`. +* `truncate_values` is removed from `geckopy.kcat_tuning` and `geckopy`; nothing in geckopy called it, and GECKO has removed `truncateValues`. `update_prot_pool` was never ported: `set_prot_pool_size` sets the protein pool for a condition. * `geckopy.__version__` is the installed version of geckopy. * `cma` is a required dependency of geckopy and the extra `evotune` is removed. `import geckopy` imports `cma`. * `enzyme_usage` reports `measured_protein`, `demanded_protein` and `demand_ratio`, the usage of the enzymes with a measured concentration divided by their measured concentrations, and the same restricted to enzymes with a usage above 1e-7 mg/gDCW (`measured_protein_active`, `demand_ratio_active`). `report_enzyme_usage` reports `demand_ratio` and `demand_ratio_active`. GECKO's `getEnzymeUsage` and `reportEnzymeUsage` give the same values. diff --git a/docs/gecko_light_status.md b/docs/gecko_light_status.md index a4db8b6..b7291a6 100644 --- a/docs/gecko_light_status.md +++ b/docs/gecko_light_status.md @@ -88,7 +88,7 @@ Tests covering the light path: | `Enzyme.concentration` setter (`Kcats.__setitem__`) | requires the per-enzyme usage upper-bound machinery | | `constrain_enz_concs`, `flexibilize_enz_concs` | proteomics integration is per-enzyme; the shared pool can't represent it | | `pfba_enzymes` | minimises per-enzyme usage reactions, which light doesn't have | -| `fill_kcats_from_isozymes` / `get_kcat_across_isozymes` | full-model isozyme aggregation; the light layout already represents isozymes as separate ec rows | +| `fill_kcats_from_isozymes` | full-model isozyme aggregation; the light layout already represents isozymes as separate ec rows | All of these raise `NotImplementedError` with a clear message rather than silently returning meaningless values. diff --git a/docs/migrating_from_gecko_matlab.md b/docs/migrating_from_gecko_matlab.md index 7d71df0..1757d59 100644 --- a/docs/migrating_from_gecko_matlab.md +++ b/docs/migrating_from_gecko_matlab.md @@ -155,7 +155,6 @@ to `rxn.notes['geckopy_warning']`. | MATLAB | geckopy | | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | | `[model, noUniprot] = makeEcModel(...)` | `make_ec_model(...)` mutates; logs unmatched genes, annotates `rxn.notes['geckopy_warning']` | -| `[model, rxnUpdated, notMatch] = applyCustomKcats(...)` | `apply_custom_kcats(...)` returns `None`; logs the rows | | `[model, ...] = getStandardKcat(...)` | `assign_standard_kcat(...)` mutates; diagnostics via logging | | `[minFlux, maxFlux] = ecFVA(...)` | `ec_fva(...)` returns a `DataFrame` indexed by reaction id | | `[rxns, kcat, name, gpr, idx] = getReactionsFromEnzyme(...)` | `get_reactions_from_enzyme(...)` returns one `DataFrame` | @@ -173,19 +172,21 @@ unknown protein id raises `ValueError` instead of returning empty. `camelCase` → `snake_case` throughout. **Most renames are mechanical** (`makeEcModel → make_ec_model`); a handful are intentional re-naming -for clarity (kept reachable as deprecated aliases — `from geckopy -import old_name` still works for one minor cycle): - -| MATLAB | geckopy (canonical) | Deprecated alias | -| ----------------------- | ------------------------------ | ----------------------------- | -| `selectKcatValue` | `apply_kcat_list` | `select_kcat_value` | -| `getKcatAcrossIsozymes` | `fill_kcats_from_isozymes` | `get_kcat_across_isozymes` | -| `getStandardKcat` | `assign_standard_kcat` | `get_standard_kcat` | -| `sigmaFitter` | `fit_sigma` | `sigma_fitter` | -| `getECfromDatabase` | `fill_eccodes_from_database` | `get_ec_from_database` | -| `getECfromGEM` | `fill_eccodes_from_gem` | `get_ec_from_gem` | -| `constrainFluxData` | `apply_flux_data_constraints` | `constrain_flux_data` | -| `mergeDLKcatAndFuzzyKcats` | `merge_kcats` (generalised) | `merge_dlkcat_and_fuzzy_kcats` | +for clarity: + +| MATLAB | geckopy | +| ----------------------- | ------------------------------ | +| `selectKcatValue` | `apply_kcat_list` | +| `getKcatAcrossIsozymes` | `fill_kcats_from_isozymes` | +| `getStandardKcat` | `assign_standard_kcat` | +| `sigmaFitter` | `fit_sigma` | +| `getECfromDatabase` | `fill_eccodes_from_database` | +| `getECfromGEM` | `fill_eccodes_from_gem` | +| `constrainFluxData` | `apply_flux_data_constraints` | +| `mergeDLKcatAndFuzzyKcats` | `merge_kcats` (generalised) | + +geckopy has no alias with a GECKO 3 name. `geckopy upgrade` reports the +calls of the old names in a project's scripts with their replacement. MATLAB is 1-indexed; geckopy is 0-indexed, which shows up in arguments like `data_col` (default `0`) and `condition` (in `apply_flux_data_constraints`). @@ -223,11 +224,11 @@ Each row maps a MATLAB function to its geckopy equivalent. The | ---------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `makeEcModel` | `make_ec_model` | **`[3→4]`** ec.genes/enzymes/mw/sequence are sorted alphabetically for stable order; KEGG fallback for unmatched genes uses `data/kegg.tsv`, downloaded when missing (an empty `kegg.id` leaves KEGG out), or `kegg_db=`. **`[Py]`** Single return; unmatched genes are logged and annotated on `rxn.notes['geckopy_warning']` instead of being returned. | | `applyComplexData` | `apply_complex_data` | — | -| `applyCustomKcats` | `apply_custom_kcats` | **`[3→4]`** Deprecated in both: `convertCustomKcats` / `convert_custom_kcats` turn `data/customKcats.tsv` into `data/kcats/custom.tsv`, which `collectKcats` / `collect_kcats` read; they raise an error when a stage lists custom and only `data/customKcats.tsv` exists. **`[Py]`** Mutates; diagnostics via logging; reads `/data/customKcats.tsv` by default. | +| `applyCustomKcats` | *(none)* | **`[3→4]`** Deprecated in MATLAB: `convertCustomKcats` / `convert_custom_kcats` turn `data/customKcats.tsv` into `data/kcats/custom.tsv`, which `collectKcats` / `collect_kcats` read; they raise an error when a stage lists custom and only `data/customKcats.tsv` exists. **`[Py]`** No counterpart; use `convert_custom_kcats` to move the file. | | `applyKcatConstraints` | `apply_kcat_constraints` | **`[3→4]`** Writes coefficients with the forward-direction `usage_prot_*` / `prot_pool_exchange` convention. | | `findMetSmiles` | `find_met_smiles` | **`[Py]`** Asynchronous PubChem cache via `requests`; uses a TSV cache (`data/smilesDB.tsv`) so re-runs are offline. | | `getComplexData` | `get_complex_data` | — | -| `getKcatAcrossIsozymes` | `fill_kcats_from_isozymes` | **`[Py]`** Renamed; `get_kcat_across_isozymes` kept as deprecated alias. Aggregation strategy (`max` / `median` / `mean`) is adapter-configurable via `kcat_aggregate_isozymes`. | +| `getKcatAcrossIsozymes` | `fill_kcats_from_isozymes` | **`[Py]`** Renamed. Aggregation strategy (`max` / `median` / `mean`) is adapter-configurable via `kcat_aggregate_isozymes`. | | `getReactionsFromEnzyme` | `get_reactions_from_enzyme` | **`[Py]`** Returns one `DataFrame` instead of five parallel cells. | | `setKcatForReactions` | `set_kcat_for_reactions` | Matches IDs without regard to letter case, ignores an unknown ID and does not apply the constraints unless `apply=True`, as `setKcatForReactions`; a light-model row is addressed by its `###_` ID. | | `setProtPoolSize` | `set_prot_pool_size` | **`[3→4]`** Sets the **upper** bound of `prot_pool_exchange` (forward direction). | @@ -237,13 +238,13 @@ Each row maps a MATLAB function to its geckopy equivalent. The | MATLAB | geckopy | Notes | | ----------------------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `fuzzyKcatMatching` | `fuzzy_kcat_matching` | **`[3→4]`** Wildcard escalation no longer escalates past the fully-wildcarded form. **`[Py]`** Returns a `DataFrame` (rows: ec / substrate / organism / kcat / source / wildcard\_level …) instead of a struct; takes pre-loaded `BrendaData` + `PhylDist`. | -| `getStandardKcat` | `assign_standard_kcat` | **`[3→4]`** Applies per-subsystem mean kcat (GECKO 3 fell back to the global standard kcat unless every subsystem matched). **`[Py]`** Renamed; `get_standard_kcat` kept as deprecated alias. | -| `mergeDLKcatAndFuzzyKcats` | `merge_kcats` | **`[Py]`** Generalised to accept any combination of BRENDA / DLKcat / OpenKineticsPredictor / manual rows; records per-row provenance. `merge_dlkcat_and_fuzzy_kcats` kept as deprecated alias. | -| `readDLKcatOutput` | `read_dlkcat_output` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'file')` / `kcats_from_dlkcat(..., via="file")` read the file. Case-insensitive substrate-name matching. | +| `getStandardKcat` | `assign_standard_kcat` | **`[3→4]`** Applies per-subsystem mean kcat (GECKO 3 fell back to the global standard kcat unless every subsystem matched). **`[Py]`** Renamed. | +| `mergeDLKcatAndFuzzyKcats` | `merge_kcats` | **`[Py]`** Generalised to accept any combination of BRENDA / DLKcat / OpenKineticsPredictor / manual rows; records per-row provenance. | +| `readDLKcatOutput` | *(none)* | **`[3→4]`** Deprecated in MATLAB: `kcatsFromDLKcat(..., 'via', 'file')` / `kcats_from_dlkcat(..., via="file")` read the file. Case-insensitive substrate-name matching. | | `removeStandardKcat` | `remove_standard_kcat` | — | -| `runDLKcat` | `run_dlkcat` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` run DLKcat. | -| `selectKcatValue` | `apply_kcat_list` | **`[Py]`** Renamed; `select_kcat_value` kept as deprecated alias. | -| `writeDLKcatInput` | `write_dlkcat_input` | **`[3→4]`** Deprecated in both: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` write the input. Bounds-safe when `ec_rxns` is a subset. | +| `runDLKcat` | *(none)* | **`[3→4]`** Deprecated in MATLAB: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` run DLKcat. | +| `selectKcatValue` | `apply_kcat_list` | **`[Py]`** Renamed. | +| `writeDLKcatInput` | *(none)* | **`[3→4]`** Deprecated in MATLAB: `kcatsFromDLKcat(..., 'via', 'docker')` / `kcats_from_dlkcat(..., via="docker")` write the input. Bounds-safe when `ec_rxns` is a subset. | | `kcatsFromBrenda` | `kcats_from_brenda` | Fuzzy BRENDA matching, kept as `data/kcats/brenda.tsv`. Loads the BRENDA data and KEGG distances from the adapter unless passed in. | | `kcatsFromDLKcat` | `kcats_from_dlkcat` | DLKcat, kept as `data/kcats/dlkcat.tsv`. `via` is `"okp"` (the DLKcat method of OpenKineticsPredictor), `"file"` (`data/DLKcat.tsv`) or `"docker"` (a local run, `run=True`); returns `(kcat_list, done)` as `kcats_from_okp`. | | `kcatsFromOKP` | `kcats_from_okp` | One OpenKineticsPredictor method, kept as `data/kcats/okp_.tsv`; submits a job when there is none, writes the file once it is done. Returns `(kcat_list, done)`. | @@ -255,11 +256,11 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `ecTreeEstimate` | `ECTree.estimate` (`geckopy.gather_kcats.ec_tree`) | The EC-tree value for one or more EC numbers. | | `loadBrendaMeasurements` | `load_brenda_measurements` | The BRENDA measurement table (`measurements.tsv`): wild-type kcats with an EC number, after the outlier screen. | | `fetchBrendaAsset` | `fetch_brenda_asset` | Downloads (once) the BRENDA files of the toolbox-assets release `brenda-2026.1-1` and returns their folder; used when a project has no `data/brenda`. | -| `convertCustomKcats` | `convert_custom_kcats` | `data/customKcats.tsv` to the curated source file `data/kcats/custom.tsv`, with the kcats `apply_custom_kcats` gives the ecModel. | +| `convertCustomKcats` | `convert_custom_kcats` | `data/customKcats.tsv` to the curated source file `data/kcats/custom.tsv`, with the kcats GECKO 3 `applyCustomKcats` gives the ecModel. | | `upgradeEcModel` | `upgrade_ec_model` | Turns GECKO 3 protein usage and pool reactions forward; `load_ec_model` already does this for the files it reads. | | `upgradeGECKOproject` | `upgrade_project` (`geckopy upgrade`)| Lists, or carries out, the steps from a GECKO 3 project to the GECKO 4 layout. The adapter-class step is MATLAB only. | | `collectKcats` | `collect_kcats` | Decides one kcat per reaction from the source files, as `[kcat_assignment]` in model_adapter.toml sets (preset `brenda_ec_tree`, the default, `gecko_median` or `gecko_classic`, or stages), without changing the model. Writes `output/_kcatDecisions.tsv`. | -| `assignKcats` | `assign_kcats` | `collect_kcats`, then sets `ec.kcat` and `ec.source` and applies the kcat constraints; replaces `merge_kcats` → `apply_kcat_list` → `apply_custom_kcats` → `fill_kcats_from_isozymes` → `assign_standard_kcat` → `apply_kcat_constraints`. In place. | +| `assignKcats` | `assign_kcats` | `collect_kcats`, then sets `ec.kcat` and `ec.source` and applies the kcat constraints; replaces `merge_kcats` → `apply_kcat_list` → the custom kcats → `fill_kcats_from_isozymes` → `assign_standard_kcat` → `apply_kcat_constraints`. In place. | | `updateKcatSources` | `update_kcat_sources` | Gathers the source files the configuration needs that are missing, or outdated against `[kcat_sources.]`; `dry_run` only reports. | ### 5.3 Get enzyme data (`src/get_enzyme_data`) @@ -271,8 +272,8 @@ Each row maps a MATLAB function to its geckopy equivalent. The | `downloadKEGG` | `geckopy.databases.kegg_download.download_kegg` | **`[3→4]`** Standalone helper (was split out in 4). **`[Py]`** Per-gene REST cache; `geckopy brenda-refresh` and friends drive it lazily. | | `downloadUniProt` | `geckopy.databases.uniprot_download.download_uniprot` | **`[3→4]`** Standalone helper (was split out in 4). | | `findECInDB` | `find_ec_in_db` | **`[3→4]`** No duplicate EC codes emitted. | -| `getECfromDatabase` | `fill_eccodes_from_database` | **`[3→4]`** Can consult KEGG when UniProt's EC field is empty or ends with `-` (opt-in via `kegg_db=`). **`[Py]`** Renamed; `get_ec_from_database` kept as deprecated alias. | -| `getECfromGEM` | `fill_eccodes_from_gem` | **`[3→4]`** Actually assigns EC codes (the GECKO 3 validation regex silently discarded every EC string). **`[Py]`** Renamed; `get_ec_from_gem` kept as deprecated alias. | +| `getECfromDatabase` | `fill_eccodes_from_database` | **`[3→4]`** Can consult KEGG when UniProt's EC field is empty or ends with `-` (opt-in via `kegg_db=`). **`[Py]`** Renamed. | +| `getECfromGEM` | `fill_eccodes_from_gem` | **`[3→4]`** Actually assigns EC codes (the GECKO 3 validation regex silently discarded every EC string). **`[Py]`** Renamed. | | `getECstring` | *(internal helper inside `fill_eccodes_*`)* | Not exposed as a public entry-point — folded into the EC-fill helpers. | | `loadBRENDAdata` | `load_brenda_data` | **`[3→4]`** Reads the new TSV schema (`kcat.tsv` / `sa.tsv` / `mw.tsv`, bare EC codes, plain organism names, `references` column). **`[Py]`** Ships **both** `max` and `median` per (ec, substrate, organism) triple — see [kcat_aggregation.md](kcat_aggregation.md). Returns a `BrendaData` dataclass. | | `loadDatabases` | *(implicit)* | **`[Py]`** No combined-load entry-point: `make_ec_model` loads `uniprot.tsv` and `kegg.tsv` and downloads a missing one with the adapter's settings, as `makeEcModel` does through `loadDatabases`; elsewhere call `load_uniprot_tsv` and `load_kegg_tsv` directly. | @@ -282,8 +283,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | MATLAB | geckopy | Notes | | ------------------------------------- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `sensitivityTuning` | `sensitivity_tuning` | **`[Py]`** Returns a `TunedKcatsResult` dataclass (kcat changes table + final flux). | -| `sigmaFitter` | `fit_sigma` | **`[3→4]`** Returns the model fitted to the **optimal** sigma (GECKO 3 returned the last trial value, sigma = 1.0). **`[Py]`** Renamed; `sigma_fitter` kept as deprecated alias. Returns a `SigmaFitterResult`. | -| `findMaxValue` | `find_max_value` | **`[3→4]`** Wildcard EC branch matches real codes. | +| `sigmaFitter` | `fit_sigma` | **`[3→4]`** Returns the model fitted to the **optimal** sigma (GECKO 3 returned the last trial value, sigma = 1.0). **`[Py]`** Renamed. Returns a `SigmaFitterResult`. | | `cmaesKcatTuning` + helpers | `kcat_tuning.evotune.cmaes_kcat_tuning` + helpers | Fits kcats to experimental flux/growth data with CMA-ES; see [evotune_kcat_tuning.md](evotune_kcat_tuning.md). | ### 5.5 Limit proteins (`src/limit_proteins`) @@ -292,7 +292,7 @@ Each row maps a MATLAB function to its geckopy equivalent. The | --------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `calculateFfactor` | `calculate_f_factor` | Without `prot_data` it reads `data/paxDB.tsv` of the project, and without that file it returns `0.5` with a warning, as MATLAB does. | | `constrainEnzConcs` | `constrain_enz_concs` | **`[3→4]`** Sets the **upper** bound of `usage_prot_*` (forward direction). **`[Py]`** Raises `NotImplementedError` on gecko-light (no per-enzyme constraint to set). | -| `constrainFluxData` | `apply_flux_data_constraints` | **`[Py]`** Renamed; `constrain_flux_data` kept as deprecated alias. Takes pre-loaded `FluxData`. 0-indexed `condition` (default `0`, MATLAB `1`). | +| `constrainFluxData` | `apply_flux_data_constraints` | **`[Py]`** Renamed. Takes pre-loaded `FluxData`. 0-indexed `condition` (default `0`, MATLAB `1`). | | `fillEnzConcs` | `fill_enz_concs` | — | | `flexibilizeEnzConcs` | `flexibilize_enz_concs` | **`[Py]`** Returns a `FlexEnzResult` dataclass. Raises `NotImplementedError` on gecko-light. | | `getConcControlCoeffs` | `get_conc_control_coeffs` | **`[3→4]`** Probe direction reads the forward `usage_prot_*` upper bound. **`[Py]`** Default path reads the reduced cost of each usage reaction's forward variable (one LP solve plus one check solve per nonzero value, against one solve per enzyme for the finite-difference probe), so an enzyme whose own bound does not bind gets 0 also while the protein pool binds; falls back to finite-difference when the solver exposes no variable duals or returns NaN. Gates on `sol.status == "optimal"`. | diff --git a/docs/yaml_format.md b/docs/yaml_format.md index 8ee95ad..82201d3 100644 --- a/docs/yaml_format.md +++ b/docs/yaml_format.md @@ -148,7 +148,7 @@ A list, one entry per unique enzyme. Fields: | `sequence` | string | optional | Amino acid sequence. Default `""` | | `concs` | number | optional | Measured concentration in mmol/gDW (from proteomics). Write NaN as `.nan`. Default NaN | -The `standard` pseudo-gene (added by `get_standard_kcat` to cover +The `standard` pseudo-gene (added by `assign_standard_kcat` to cover reactions without a real gene assignment) appears as both `genes: "standard"` and `enzymes: "standard"`. diff --git a/tutorials/light_ecModel/protocol.py b/tutorials/light_ecModel/protocol.py index 4a7cdc0..843ecad 100644 --- a/tutorials/light_ecModel/protocol.py +++ b/tutorials/light_ecModel/protocol.py @@ -157,9 +157,9 @@ # raises ``NotImplementedError``. The shared ``prot_pool`` is the # only enzyme constraint, so per-enzyme distinctions don't exist in # the LP. -# - **Isozyme-spanning kcat helpers.** ``fill_kcats_from_isozymes`` / -# ``get_kcat_across_isozymes`` are full-model only — the light layout -# already represents isozymes natively as separate ec rows. +# - **Isozyme-spanning kcat helpers.** ``fill_kcats_from_isozymes`` is +# full-model only — the light layout already represents isozymes +# natively as separate ec rows. # # **What still works.** ``Enzyme`` proxy reads (``mw``, ``gene``, # ``sequence``, ``kcats``, ``reactions``), most kcat-source helpers