From 180e584d400e147fd197a545f568045289ebd1f7 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 12:39:00 +0100 Subject: [PATCH 01/10] feat: mark reverse directions in the EC-tree kcat detail BRENDA measurements have no direction, so a reverse direction gets the value of its forward direction; its detail now ends with direction=reverse. --- CHANGELOG.md | 1 + src/geckopy/gather_kcats/kcats_from.py | 11 +++++++++-- tests/test_ec_tree.py | 5 ++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0c76d..a7a9801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `kcats_from_ec_tree` marks a reverse direction (`_REV`) in `detail` with `direction=reverse`. The estimate depends on the EC number only, as BRENDA measurements have no direction, so the reverse direction has the value of the forward direction. GECKO's `kcatsFromECTree` does the same. * `load_uniprot_tsv` always splits a gene cell of uniprot.tsv with several gene names, separated by semicolons and/or whitespace (`YPR080W; YBR118W`), into one row per gene name; the other columns are repeated on every row and an empty cell stays one row. A model gene that is any of the names finds the entry. The `split_gene_cells` option is removed. GECKO's `loadDatabases` does the same. * The user guides (evotune, YAML format, relaxation methods, migration from GECKO MATLAB, kcat sources) moved to [gecko-docs](https://gecko-docs.readthedocs.io/en/latest/). The `docs/` folder is gone; the developer notes are in `dev/`. * `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. diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index a31bacc..f236a47 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -167,6 +167,9 @@ def _measurements(adapter: "ModelAdapter") -> pd.DataFrame: return table +_REVERSE = re.compile(r"_REV(_EXP_\d+)?$") + + def kcats_from_ec_tree( model: "EcModel", *, @@ -211,7 +214,11 @@ def kcats_from_ec_tree( ------- pandas.DataFrame The kcat list; ``detail`` has the number of measurements of the EC - (``n``) and the deepest EC level with measurements (``depth``). + (``n``) and the deepest EC level with measurements (``depth``). The + estimate depends on the EC number only, because BRENDA measurements + have no direction: a reverse direction (``_REV``) gets the value of + its forward direction, and its ``detail`` ends with + ``direction=reverse``. """ adapter = _adapter(model, adapter, "kcats_from_ec_tree needs the BRENDA folder and project folder") ec_rxns = None if ec_rxns is None else list(ec_rxns) @@ -231,7 +238,7 @@ def kcats_from_ec_tree( continue rows.append({"rxn_id": rxn, "source": "ec_tree", "eccode": ec, "substrates": [], "genes": [], "kcat": float(10 ** value), "wildcard_level": np.nan, "origin": np.nan, - "detail": f"n={n}; depth={depth}"}) + "detail": f"n={n}; depth={depth}" + ("; direction=reverse" if _REVERSE.search(rxn) else "")}) kcat_list = pd.DataFrame(rows, columns=["rxn_id", "source", "eccode", "substrates", "genes", "kcat", "wildcard_level", "origin", "detail"]) if save: diff --git a/tests/test_ec_tree.py b/tests/test_ec_tree.py index 8f538e7..e9af070 100644 --- a/tests/test_ec_tree.py +++ b/tests/test_ec_tree.py @@ -110,7 +110,10 @@ def test_kcats_from_ec_tree_writes_file(tmp_path): assert meta["parameters"] == '{"k":5}' assert meta["data"] == "BRENDA release test" assert set(records["source"]) == {"ec_tree"} - assert records["detail"].str.match(r"^n=\d+; depth=[234]$").all() + assert records["detail"].str.match(r"^n=\d+; depth=[234](; direction=reverse)?$").all() + reverse = records["detail"].str.endswith("direction=reverse") + assert reverse.equals(records["rxn"].str.endswith("_REV")) + assert reverse.any() # Without a measurement table in the project, geckopy's BRENDA release is used. kcats_from_ec_tree(model, adapter=adapter, save_as="ec_tree_bundled") _, meta = read_kcat_source(project / "data" / "kcats" / "ec_tree_bundled.tsv") From f177dfbd3789626869ab3db9eb4fc8d749440f4a Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:13:20 +0100 Subject: [PATCH 02/10] feat: show the kcats of every source for selected reactions kcat_evidence puts the kcat of each source file next to the model kcat and, for a tuned kcat, its prior. kcats_for_reactions adds the EC-tree estimate and OKP predictions for a few reactions, kept apart from the OKP job of all reactions by a job tag. --- src/geckopy/__init__.py | 4 + src/geckopy/gather_kcats/__init__.py | 4 + src/geckopy/gather_kcats/evidence.py | 303 ++++++++++++++++++ src/geckopy/gather_kcats/kcats_from.py | 18 +- .../open_kinetics_predictor/run.py | 12 +- tests/test_kcat_evidence.py | 89 +++++ 6 files changed, 425 insertions(+), 5 deletions(-) create mode 100644 src/geckopy/gather_kcats/evidence.py create mode 100644 tests/test_kcat_evidence.py diff --git a/src/geckopy/__init__.py b/src/geckopy/__init__.py index 45a58eb..8acb5cf 100644 --- a/src/geckopy/__init__.py +++ b/src/geckopy/__init__.py @@ -86,6 +86,8 @@ ec_stats_from_brenda, format_kcat_source, fuzzy_kcat_matching, + kcat_evidence, + kcats_for_reactions, kcats_from_brenda, kcats_from_dlkcat, kcats_from_ec_tree, @@ -213,6 +215,8 @@ "ec_stats_from_brenda", "format_kcat_source", "fuzzy_kcat_matching", + "kcat_evidence", + "kcats_for_reactions", "kcats_from_brenda", "kcats_from_dlkcat", "kcats_from_ec_tree", diff --git a/src/geckopy/gather_kcats/__init__.py b/src/geckopy/gather_kcats/__init__.py index 69982e0..743726e 100644 --- a/src/geckopy/gather_kcats/__init__.py +++ b/src/geckopy/gather_kcats/__init__.py @@ -7,6 +7,7 @@ from .homologs import kcats_from_homologs from .kcat_sources import ec_rxn_keys, load_kcat_source, read_kcat_source, save_kcat_source from .kcat_source_format import KcatSource, format_kcat_source, parse_kcat_source +from .evidence import KcatEvidence, kcat_evidence, kcats_for_reactions 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 from .merge_kcats import merge_kcats, normalize_source @@ -38,6 +39,9 @@ "kcats_from_ec_tree", "kcats_from_homologs", "kcats_from_okp", + "kcat_evidence", + "kcats_for_reactions", + "KcatEvidence", "load_kcat_source", "merge_kcats", "normalize_source", diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py new file mode 100644 index 0000000..677c1be --- /dev/null +++ b/src/geckopy/gather_kcats/evidence.py @@ -0,0 +1,303 @@ +"""The kcats that every source gives for selected reactions. + +:func:`kcat_evidence` puts the kcat of each source file of the project next +to the kcat of the model and, for a tuned kcat, the kcat before tuning. +:func:`kcats_for_reactions` adds the EC-tree estimate and predictions of +OpenKineticsPredictor for a few reactions. GECKO has the same functions as +``kcatEvidence`` and ``kcatsForReactions``. +""" +from __future__ import annotations + +import logging +import re +from pathlib import Path +from typing import TYPE_CHECKING, Iterable, NamedTuple, Optional, Sequence + +import numpy as np +import pandas as pd + +from .ec_tree import ECTree +from .kcat_source_format import parse_kcat_source +from .kcat_sources import OKP_METHODS, ec_rxn_keys, load_kcat_source, okp_file + +if TYPE_CHECKING: + from ..adapter import ModelAdapter + from ..ec_model.ec_model import EcModel + +logger = logging.getLogger(__name__) + +# Methods that predict from one enzyme and one substrate: kcats_from_okp +# rejects CatPred and TurNup. +_PAIR_METHODS = tuple(OKP_METHODS) + + +class KcatEvidence(NamedTuple): + """Result of :func:`kcat_evidence`: ``evidence``, ``summary`` and ``rows``.""" + + evidence: pd.DataFrame + summary: pd.DataFrame + rows: np.ndarray + + +def _names_to_rows(model: "EcModel", base: list[str], names: Iterable[str], label: str) -> np.ndarray: + names = list(dict.fromkeys(names)) + known = set(model.ec.rxns) | set(base) + missing = [n for n in names if n not in known] + if missing: + raise ValueError(f"Not reactions of model.ec ({label}): {', '.join(missing)}.") + wanted = set(names) + return np.array([r in wanted or b in wanted for r, b in zip(model.ec.rxns, base)], dtype=bool) + + +def _select_rows(model: "EcModel", rxns, enzymes, tuned) -> np.ndarray: + n_ec = len(model.ec.rxns) + if rxns is None and enzymes is None and tuned is None: + return np.arange(n_ec) + selected = np.zeros(n_ec, dtype=bool) + base, _ = ec_rxn_keys(model) + if rxns is not None: + if isinstance(rxns, str): + rxns = [rxns] + rxns = list(rxns) if not isinstance(rxns, np.ndarray) else rxns + arr = np.asarray(rxns) + if arr.dtype == bool: + selected |= arr + elif np.issubdtype(arr.dtype, np.integer): + selected[arr] = True + else: + selected |= _names_to_rows(model, base, [str(r) for r in rxns], "rxns") + if tuned is not None: + names = getattr(tuned, "rxns", tuned) + selected |= _names_to_rows(model, base, [str(r) for r in names], "tuned") + if enzymes is not None: + enzymes = [enzymes] if isinstance(enzymes, str) else list(enzymes) + missing = sorted(set(enzymes) - set(model.ec.enzymes)) + if missing: + raise ValueError(f"Not in model.ec.enzymes: {', '.join(missing)}.") + cols = [j for j, e in enumerate(model.ec.enzymes) if e in set(enzymes)] + mat = model.ec.rxn_enz_mat.tocsc()[:, cols] + selected |= np.asarray((mat != 0).sum(axis=1)).ravel() > 0 + return np.flatnonzero(selected) + + +def kcat_evidence( + model: "EcModel", + *, + rxns=None, + enzymes: Optional[Iterable[str]] = None, + tuned=None, + sources: Optional[Sequence[str]] = None, + compute: Sequence[str] = (), + measurements: Optional[pd.DataFrame] = None, + save_as: Optional[str | Path] = None, + adapter: Optional["ModelAdapter"] = None, +) -> KcatEvidence: + """Show the kcats that every source gives for selected reactions. + + For each selected ``model.ec`` reaction, reads the kcat of every source + file in ``data/kcats`` (see :func:`~geckopy.gather_kcats.load_kcat_source`) + and puts it next to the kcat that the model has now and, for a tuned + kcat, the kcat before tuning. A reaction that ``rxns``, ``enzymes`` or + ``tuned`` selects is included; without a selection, all reactions are. The + source files are only read. + + Parameters + ---------- + model + The ecModel. + rxns + Reactions to include: names of ``model.ec.rxns`` or conventional + reaction names (a reverse direction ends with ``_REV``), integer + indices, or a boolean array over ``model.ec.rxns``. + enzymes + UniProt accessions; every reaction catalysed by one of them is + included, for example the enzymes that + :func:`~geckopy.limit_proteins.flexibilize_enz_concs` relaxed. + tuned + The result of :func:`~geckopy.kcat_tuning.sensitivity_tuning`, or a + list of reaction names. + sources + Names of the source files, without ``.tsv`` (default: all files in + ``data/kcats``; an empty list for none). + compute + Sources to compute for the selected reactions when the project has no + file of that name; ``"ec_tree"`` is supported. + measurements + BRENDA measurement table for the EC-tree estimate (default: the + adapter's table). + save_as + Path of a tab-separated file to write ``summary`` to. + adapter + Model adapter (default: ``model.adapter``). + + Returns + ------- + KcatEvidence + ``evidence``: one row per kcat of a source, with ``ec_rxn``, + ``source``, ``kcat``, ``substrate``, ``detail`` and ``assigned`` (the + kcat is the one assigned to the reaction: the kcat before tuning for + a tuned reaction, the current kcat otherwise). ``summary``: one row + per selected reaction, with ``ec_rxn``, ``enzymes``, ``eccodes``, + ``kcat`` (current), ``origin``, ``tuned``, ``prior`` (NaN when + untuned), ``fold`` (``kcat / prior``) and one column ``src_`` + with the median kcat of that source (NaN without one). ``rows``: the + selected indices of ``model.ec.rxns``. + """ + from .kcats_from import _adapter, _measurements + + unknown = sorted(set(compute) - {"ec_tree"}) + if unknown: + raise ValueError(f"Cannot compute {', '.join(unknown)}; supported: ec_tree.") + adapter = _adapter(model, adapter, "kcat_evidence needs params.path") + rows = _select_rows(model, rxns, enzymes, tuned) + ec_rxns = [model.ec.rxns[i] for i in rows] + + if sources is None: + folder = Path(adapter.params.path) / "data" / "kcats" + sources = sorted(p.stem for p in folder.glob("*.tsv")) + sources = list(sources) + + parts = [] + for name in sources: + kcat_list, _ = load_kcat_source(model, name, adapter=adapter) + keep = kcat_list["rxn_id"].isin(ec_rxns) + sub = kcat_list[keep] + parts.append(pd.DataFrame({ + "ec_rxn": sub["rxn_id"].to_numpy(), + "source": name, + "kcat": sub["kcat"].to_numpy(dtype=float), + "substrate": ["; ".join(s) for s in sub["substrates"]], + "detail": sub["detail"].to_numpy(), + })) + if "ec_tree" in compute and "ec_tree" not in sources and len(rows): + from .kcats_from import kcats_from_ec_tree + + tree = kcats_from_ec_tree(model, ec_rxns=ec_rxns, save=False, measurements=measurements, adapter=adapter) + parts.append(pd.DataFrame({ + "ec_rxn": tree["rxn_id"].to_numpy(), "source": "ec_tree", + "kcat": tree["kcat"].to_numpy(dtype=float), "substrate": "", "detail": tree["detail"].to_numpy(), + })) + sources.append("ec_tree") + columns = ["ec_rxn", "source", "kcat", "substrate", "detail"] + evidence = pd.concat(parts, ignore_index=True) if parts else pd.DataFrame(columns=columns) + + info = [parse_kcat_source(model.ec.source[i]) for i in rows] + current = np.asarray(model.ec.kcat, dtype=float)[rows] + prior = np.array([np.nan if x.prior is None else x.prior for x in info], dtype=float) + tuned_mask = ~np.isnan(prior) + reference = np.where(tuned_mask, prior, current) + ref_of = dict(zip(ec_rxns, reference)) + ref = evidence["ec_rxn"].map(ref_of).to_numpy(dtype=float) + evidence["assigned"] = np.abs(np.log(evidence["kcat"].to_numpy(dtype=float) / ref)) < 1e-6 + evidence = evidence[columns + ["assigned"]] + + mat = model.ec.rxn_enz_mat.tocsr() + enzyme_text = [] + for i in rows: + row = mat[i] + enzyme_text.append(";".join(model.ec.enzymes[j] for j in row.indices[row.data != 0])) + summary = pd.DataFrame({ + "ec_rxn": ec_rxns, + "enzymes": enzyme_text, + "eccodes": [";".join(c) if isinstance(c, (list, tuple)) else (c if isinstance(c, str) else "") + for c in (model.ec.eccodes[i] for i in rows)], + "kcat": current, + "origin": [x.origin for x in info], + "tuned": tuned_mask, + "prior": prior, + "fold": current / prior, + }) + medians = evidence.groupby(["source", "ec_rxn"])["kcat"].median() + for name in sources: + summary[f"src_{name}"] = [medians.get((name, r), np.nan) for r in ec_rxns] + if save_as is not None: + summary.to_csv(save_as, sep="\t", index=False) + return KcatEvidence(evidence, summary, rows) + + +def _okp_methods(okp, adapter: "ModelAdapter") -> list[str]: + if okp is None or okp is False: + return [] + if okp is True: + from .kcat_assignment import default_okp_method + + return [default_okp_method(adapter)] + if isinstance(okp, str): + if okp.lower() == "all": + return list(_PAIR_METHODS) + okp = [okp] + methods = list(okp) + unknown = [m for m in methods if m not in _PAIR_METHODS] + if unknown: + raise ValueError(f"Unknown OKP methods: {', '.join(unknown)}. Methods: {', '.join(_PAIR_METHODS)}.") + return methods + + +def kcats_for_reactions( + model: "EcModel", + rxns, + *, + okp=False, + sources: Optional[Sequence[str]] = None, + compute: Sequence[str] = ("ec_tree",), + measurements: Optional[pd.DataFrame] = None, + adapter: Optional["ModelAdapter"] = None, +) -> KcatEvidence: + """Get the kcats of every source for a few reactions. + + Reads the kcats that the source files of the project give for the + reactions, computes the EC-tree estimate for them, and optionally predicts + their kcats with one or several OpenKineticsPredictor (OKP) methods; the + result is shown as :func:`kcat_evidence` does. + + The OKP jobs have their own tag (``job_tag`` of + :func:`~geckopy.gather_kcats.kcats_from_okp`): they submit the + enzyme-substrate pairs of the selected reactions only, and leave the job + and the result that ``kcats_from_okp`` keeps for all reactions as they + are. The predictions are written to ``data/kcats/okp__query.tsv`` + and read back as a source. The function waits for each job to finish and + submits a new job on every call. A method that fails is logged as a + warning and the others continue. Each OKP method submits a job and counts + against the daily prediction quota of the API key. + + Parameters + ---------- + model + EcModel with metabolite SMILES and protein sequences when OKP is used. + rxns + The reactions, as in :func:`kcat_evidence`. + okp + OKP methods to predict with: ``False`` for none (default), ``True`` + for the first method of the adapter, ``"all"`` for every method + that :func:`~geckopy.gather_kcats.kcats_from_okp` accepts, or a method + name or a list of names. + sources, compute, measurements, adapter + As in :func:`kcat_evidence`; ``compute`` is ``("ec_tree",)`` by default. + + Returns + ------- + KcatEvidence + """ + from .kcats_from import _adapter, kcats_from_okp + + adapter = _adapter(model, adapter, "kcats_for_reactions needs params.path") + rows = kcat_evidence(model, rxns=rxns, sources=[], adapter=adapter).rows + if len(rows) == 0: + raise ValueError("No reaction of model.ec is selected.") + ec_rxns = [model.ec.rxns[i] for i in rows] + queried = [] + for method in _okp_methods(okp, adapter): + try: + _, done = kcats_from_okp(model, method=method, ec_rxns=ec_rxns, job_tag="query", + resubmit=True, wait=True, adapter=adapter) + except Exception as err: # one method failing does not stop the others + logger.warning("OKP %s failed: %s", method, err) + continue + if done: + queried.append(f"{okp_file(method)}_query") + if sources is None: + folder = Path(adapter.params.path) / "data" / "kcats" + sources = [p.stem for p in sorted(folder.glob("*.tsv")) + if not (re.fullmatch(r"okp_.*_query", p.stem) and p.stem not in queried)] + return kcat_evidence(model, rxns=rows, sources=sources, compute=compute, + measurements=measurements, adapter=adapter) diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index f236a47..bb62787 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -408,6 +408,7 @@ def kcats_from_okp( resubmit: bool = False, use_stored: bool = False, ec_rxns: Optional[Iterable[str]] = None, + job_tag: str = "", save: bool = True, save_as: Optional[str] = None, adapter: Optional["ModelAdapter"] = None, @@ -439,6 +440,14 @@ def kcats_from_okp( ec_rxns ``model.ec.rxns`` to predict for (default: all); only used when a job is submitted. + job_tag + Keeps the job, its input and its result apart from the job of the + method without a tag: they are read and written as + ``data/OKP_job__.txt``, ``data/OKP_.csv`` + and ``data/OKP_output__.csv``, and the source file is + ``okp__.tsv`` unless ``save_as`` is given. A job for a + few reactions with a tag leaves the job and the result of all + reactions as they are. save, save_as, adapter As in :func:`kcats_from_brenda`; ``save_as`` defaults to ``okp_``, in lower case, with other characters than letters @@ -461,6 +470,10 @@ def kcats_from_okp( ec_rxns = _rxn_subset(ec_rxns) name_default = okp_file(method) tag = name_default[len("okp_"):] + job_tag = re.sub(r"^_+|_+$", "", re.sub(r"[^a-z0-9]+", "_", job_tag.lower())) + if job_tag: + tag = f"{tag}_{job_tag}" + name_default = f"okp_{tag}" data_dir = Path(adapter.params.path) / "data" job_file = data_dir / f"OKP_job_{tag}.txt" out_file = data_dir / f"OKP_output_{tag}.csv" @@ -478,7 +491,7 @@ def kcats_from_okp( _warn_subset(ec_rxns, model, path) if use_stored: - if "jobId" not in job: + if "jobId" not in job and not job_tag: _adopt_shared_result(model, data_dir, method, job_file, out_file) job = _read_job_file(job_file) if not out_file.is_file() or job.get("resultOf") != job.get("jobId"): @@ -491,7 +504,8 @@ def kcats_from_okp( if submitting: out_file.unlink(missing_ok=True) job_id = _submit_open_kinetics_predictor(model, overwrite=True, method=method, - ec_rxns=ec_rxns, adapter=adapter) + ec_rxns=ec_rxns, adapter=adapter, + job_tag=job_tag) job = {"jobId": job_id, "method": method, "submittedAt": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")} if ec_rxns is not None: diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index ee51453..3442e3c 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -56,6 +56,7 @@ def _submit_open_kinetics_predictor( adapter: Optional["ModelAdapter"] = None, base_url: str = _BASE_URL, session: Optional[requests.Session] = None, + job_tag: str = "", ) -> str: """Build the OKP input and submit a kcat-prediction job. @@ -83,11 +84,15 @@ def _submit_open_kinetics_predictor( Model adapter; falls back to ``model.adapter``. base_url, session For testing / custom deployments. + job_tag + With a tag, the input is ``data/OKP_.csv`` and + ``data/OKP_job.txt`` is left as it is. Returns ------- str - The OKP job id (also written to ``data/OKP_job.txt``). + The OKP job id (also written to ``data/OKP_job.txt`` without a + ``job_tag``). """ from ...adapter import resolve_adapter adapter = resolve_adapter( @@ -102,7 +107,7 @@ def _submit_open_kinetics_predictor( method = default_okp_method(adapter) okp = adapter.params.okp - okp_csv = data_dir / "OKP.csv" + okp_csv = data_dir / (f"OKP_{job_tag}.csv" if job_tag else "OKP.csv") if okp_csv.is_file() and not overwrite: csv_text = okp_csv.read_text(encoding="utf-8") logger.info("Using existing %s (set overwrite=True to rebuild).", okp_csv) @@ -128,7 +133,8 @@ def _submit_open_kinetics_predictor( if not job_id: raise OKPError(f"OKP submit returned no jobId: {resp!r}") - _write_job_metadata(data_dir / "OKP_job.txt", job_id, method) + if not job_tag: + _write_job_metadata(data_dir / "OKP_job.txt", job_id, method) logger.info("Submitted OKP job %s (method: %s).", job_id, method) return job_id diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py new file mode 100644 index 0000000..f520ddb --- /dev/null +++ b/tests/test_kcat_evidence.py @@ -0,0 +1,89 @@ +"""Tests for kcat_evidence and kcats_for_reactions. + +GECKO's kcatEvidenceTests test the same behaviour. +""" +import numpy as np +import pandas as pd +import pytest + +from geckopy.gather_kcats import kcat_evidence, kcats_for_reactions, kcats_from_okp, save_kcat_source +from test_ec_tree import _measurements_for, _project + + +def _rows_with_enzymes(model, n): + mat = model.ec.rxn_enz_mat.tocsr() + return [i for i in range(len(model.ec.rxns)) + if mat[i].nnz and str(model.ec.eccodes[i] or "").strip()][:n] + + +def _save(model, adapter, name, rxns, kcats): + save_kcat_source(model, pd.DataFrame({"rxn_id": rxns, "kcat": kcats}), name, adapter=adapter) + + +def test_evidence_lists_every_source(tmp_path): + model, adapter, _ = _project(tmp_path) + rows = _rows_with_enzymes(model, 3) + sel = [model.ec.rxns[i] for i in rows] + _save(model, adapter, "src_a", sel, [1.0, 2.0, 4.0]) + _save(model, adapter, "src_b", sel[:1], [8.0]) + result = kcat_evidence(model, rxns=sel, sources=["src_a", "src_b"], adapter=adapter) + assert list(result.rows) == rows + assert list(result.summary["ec_rxn"]) == sel + assert list(result.summary["src_src_a"]) == [1.0, 2.0, 4.0] + assert result.summary["src_src_b"].iloc[0] == 8.0 and result.summary["src_src_b"].iloc[1:].isna().all() + assert len(result.evidence) == 4 + assert list(result.summary["kcat"]) == list(np.asarray(model.ec.kcat)[rows]) + only_b = kcat_evidence(model, rxns=rows, sources=["src_b"], adapter=adapter) + assert "src_src_a" not in only_b.summary + with pytest.raises(ValueError, match="Not reactions of model.ec"): + kcat_evidence(model, rxns=["nope"], adapter=adapter) + with pytest.raises(ValueError, match="Cannot compute"): + kcat_evidence(model, compute=["x"], adapter=adapter) + + +def test_evidence_of_a_tuned_kcat(tmp_path): + model, adapter, _ = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + rxn = model.ec.rxns[i] + model.ec.kcat[i] = 5.0 + model.ec.source[i] = "custom | tuned=sensitivity (prior=0.5)" + _save(model, adapter, "src_a", [rxn], [0.5]) + out = tmp_path / "summary.tsv" + result = kcat_evidence(model, tuned=[rxn], sources=["src_a"], save_as=out, adapter=adapter) + row = result.summary.iloc[0] + assert (row["kcat"], row["prior"], row["fold"], row["origin"], row["tuned"]) == (5.0, 0.5, 10.0, "custom", True) + assert result.evidence["assigned"].all() + assert pd.read_csv(out, sep="\t")["fold"].iloc[0] == pytest.approx(10.0) + + +def test_evidence_by_enzyme_and_ec_tree(tmp_path): + model, adapter, _ = _project(tmp_path) + j = int(np.flatnonzero(np.asarray((model.ec.rxn_enz_mat != 0).sum(axis=0)).ravel())[0]) + expected = sorted(model.ec.rxn_enz_mat.tocsc()[:, j].nonzero()[0].tolist()) + result = kcat_evidence(model, enzymes=[model.ec.enzymes[j]], sources=[], compute=["ec_tree"], + measurements=_measurements_for(model), adapter=adapter) + assert sorted(result.rows.tolist()) == expected + with_ec = [i for i in expected if str(model.ec.eccodes[i] or "").strip()] + assert int(result.summary["src_ec_tree"].notna().sum()) == len(with_ec) + assert set(result.evidence["source"]) == {"ec_tree"} + with pytest.raises(ValueError, match="Not in model.ec.enzymes"): + kcat_evidence(model, enzymes=["X00000"], adapter=adapter) + + +def test_kcats_for_reactions_without_okp(tmp_path): + model, adapter, _ = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + result = kcats_for_reactions(model, [i], measurements=_measurements_for(model), adapter=adapter) + assert list(result.summary["ec_rxn"]) == [model.ec.rxns[i]] + assert result.summary["src_ec_tree"].notna().all() + with pytest.raises(ValueError, match="Unknown OKP methods"): + kcats_for_reactions(model, [i], okp="NoSuchMethod", adapter=adapter) + + +def test_okp_job_tag_has_its_own_files(tmp_path): + model, adapter, project = _project(tmp_path) + data = project / "data" + (data / "OKP_job_kinform_h.txt").write_text("jobId: abc\nmethod: KinForm-H\nresultOf: abc\n", encoding="utf-8") + (data / "OKP_output_kinform_h.csv").write_text("x\n", encoding="utf-8") + with pytest.raises(FileNotFoundError, match="OKP_job_kinform_h_query.txt"): + kcats_from_okp(model, method="KinForm-H", job_tag="query", use_stored=True, adapter=adapter) From 2f58b17e03c072c9d96adc4485c7f7c03e12b0fe Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:14:01 +0100 Subject: [PATCH 03/10] fix: compare the kcats of the evidence without dividing by zero --- CHANGELOG.md | 1 + src/geckopy/gather_kcats/evidence.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7a9801..8146cc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `kcat_evidence` shows, for selected reactions, the kcat of every source file next to the kcat of the model and, for a tuned kcat, the kcat before tuning. The reactions are selected by name (`rxns`), by enzyme (`enzymes`, for example the enzymes that `flexibilize_enz_concs` relaxed) or by the result of `sensitivity_tuning` (`tuned`); `compute=["ec_tree"]` adds the EC-tree estimate without a source file. `kcats_for_reactions` gets the kcats of every source for a few reactions, computes the EC-tree estimate and, with `okp`, predicts them with one, several or all OpenKineticsPredictor methods. `kcats_from_okp` has `job_tag`, which keeps a job for a few reactions apart from the job of all reactions: it has its own job, input and result files. GECKO has `kcatEvidence`, `kcatsForReactions` and the `jobTag` option of `kcatsFromOKP`. * `kcats_from_ec_tree` marks a reverse direction (`_REV`) in `detail` with `direction=reverse`. The estimate depends on the EC number only, as BRENDA measurements have no direction, so the reverse direction has the value of the forward direction. GECKO's `kcatsFromECTree` does the same. * `load_uniprot_tsv` always splits a gene cell of uniprot.tsv with several gene names, separated by semicolons and/or whitespace (`YPR080W; YBR118W`), into one row per gene name; the other columns are repeated on every row and an empty cell stays one row. A model gene that is any of the names finds the entry. The `split_gene_cells` option is removed. GECKO's `loadDatabases` does the same. * The user guides (evotune, YAML format, relaxation methods, migration from GECKO MATLAB, kcat sources) moved to [gecko-docs](https://gecko-docs.readthedocs.io/en/latest/). The `docs/` folder is gone; the developer notes are in `dev/`. diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index 677c1be..075e414 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -188,7 +188,7 @@ def kcat_evidence( reference = np.where(tuned_mask, prior, current) ref_of = dict(zip(ec_rxns, reference)) ref = evidence["ec_rxn"].map(ref_of).to_numpy(dtype=float) - evidence["assigned"] = np.abs(np.log(evidence["kcat"].to_numpy(dtype=float) / ref)) < 1e-6 + evidence["assigned"] = np.isclose(evidence["kcat"].to_numpy(dtype=float), ref, rtol=1e-6, atol=0) evidence = evidence[columns + ["assigned"]] mat = model.ec.rxn_enz_mat.tocsr() From 358f7ea7920b94f51c97527722386f7883bef908 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:48:40 +0100 Subject: [PATCH 04/10] fix: review findings of the kcat evidence functions --- src/geckopy/gather_kcats/evidence.py | 91 +++++++++++++------ src/geckopy/gather_kcats/kcats_from.py | 40 ++++++-- .../open_kinetics_predictor/run.py | 4 +- src/geckopy/kcat_tuning/sensitivity_tuning.py | 23 ++++- src/geckopy/upgrade.py | 4 + tests/test_kcat_evidence.py | 80 +++++++++++++++- tests/test_sensitivity_tuning.py | 12 +++ 7 files changed, 212 insertions(+), 42 deletions(-) diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index 075e414..03a5a51 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -39,6 +39,13 @@ class KcatEvidence(NamedTuple): rows: np.ndarray +def _as_list(value) -> list: + """A list from a string, an iterable or a numpy array.""" + if isinstance(value, str): + return [value] + return list(value) + + def _names_to_rows(model: "EcModel", base: list[str], names: Iterable[str], label: str) -> np.ndarray: names = list(dict.fromkeys(names)) known = set(model.ec.rxns) | set(base) @@ -56,21 +63,24 @@ def _select_rows(model: "EcModel", rxns, enzymes, tuned) -> np.ndarray: selected = np.zeros(n_ec, dtype=bool) base, _ = ec_rxn_keys(model) if rxns is not None: - if isinstance(rxns, str): - rxns = [rxns] - rxns = list(rxns) if not isinstance(rxns, np.ndarray) else rxns - arr = np.asarray(rxns) - if arr.dtype == bool: + arr = np.asarray(_as_list(rxns)) + if arr.size == 0: + pass + elif arr.dtype == bool: + if arr.shape != (n_ec,): + raise ValueError(f"A boolean rxns needs {n_ec} entries, one per row of model.ec.rxns.") selected |= arr elif np.issubdtype(arr.dtype, np.integer): + if arr.min() < -n_ec or arr.max() >= n_ec: + raise ValueError(f"rxns has an index outside model.ec.rxns ({n_ec} rows).") selected[arr] = True else: - selected |= _names_to_rows(model, base, [str(r) for r in rxns], "rxns") + selected |= _names_to_rows(model, base, [str(r) for r in arr], "rxns") if tuned is not None: - names = getattr(tuned, "rxns", tuned) + names = _as_list(tuned.rxns if hasattr(tuned, "rxns") else tuned) selected |= _names_to_rows(model, base, [str(r) for r in names], "tuned") if enzymes is not None: - enzymes = [enzymes] if isinstance(enzymes, str) else list(enzymes) + enzymes = _as_list(enzymes) missing = sorted(set(enzymes) - set(model.ec.enzymes)) if missing: raise ValueError(f"Not in model.ec.enzymes: {', '.join(missing)}.") @@ -118,10 +128,13 @@ def kcat_evidence( list of reaction names. sources Names of the source files, without ``.tsv`` (default: all files in - ``data/kcats``; an empty list for none). + ``data/kcats``, including the ``okp__query.tsv`` files that + :func:`kcats_for_reactions` wrote; an empty list for none). compute - Sources to compute for the selected reactions when the project has no - file of that name; ``"ec_tree"`` is supported. + Sources to compute for the selected reactions; ``"ec_tree"`` is + supported. The computed estimate replaces a source file of the same + name, so it follows ``measurements`` and the ``k`` of the adapter + (default 5) and is not outdated. measurements BRENDA measurement table for the EC-tree estimate (default: the adapter's table). @@ -139,12 +152,17 @@ def kcat_evidence( a tuned reaction, the current kcat otherwise). ``summary``: one row per selected reaction, with ``ec_rxn``, ``enzymes``, ``eccodes``, ``kcat`` (current), ``origin``, ``tuned``, ``prior`` (NaN when - untuned), ``fold`` (``kcat / prior``) and one column ``src_`` - with the median kcat of that source (NaN without one). ``rows``: the - selected indices of ``model.ec.rxns``. + untuned or without a positive prior), ``fold`` (``kcat / prior``) and + one column ``src_`` (characters other than letters, digits and + ``_`` in the source name become ``_``) with the median kcat of that + source (NaN without one). ``rows``: the selected indices of + ``model.ec.rxns``. A conventional reaction name in ``rxns`` or + ``tuned`` selects every isozyme row of the reaction, as ``tuned`` + names the reactions of a light ecModel by their conventional name. """ from .kcats_from import _adapter, _measurements + compute = _as_list(compute) unknown = sorted(set(compute) - {"ec_tree"}) if unknown: raise ValueError(f"Cannot compute {', '.join(unknown)}; supported: ec_tree.") @@ -155,7 +173,9 @@ def kcat_evidence( if sources is None: folder = Path(adapter.params.path) / "data" / "kcats" sources = sorted(p.stem for p in folder.glob("*.tsv")) - sources = list(sources) + sources = _as_list(sources) + if "ec_tree" in compute: + sources = [s for s in sources if s != "ec_tree"] parts = [] for name in sources: @@ -169,7 +189,7 @@ def kcat_evidence( "substrate": ["; ".join(s) for s in sub["substrates"]], "detail": sub["detail"].to_numpy(), })) - if "ec_tree" in compute and "ec_tree" not in sources and len(rows): + if "ec_tree" in compute and len(rows): from .kcats_from import kcats_from_ec_tree tree = kcats_from_ec_tree(model, ec_rxns=ec_rxns, save=False, measurements=measurements, adapter=adapter) @@ -179,16 +199,20 @@ def kcat_evidence( })) sources.append("ec_tree") columns = ["ec_rxn", "source", "kcat", "substrate", "detail"] - evidence = pd.concat(parts, ignore_index=True) if parts else pd.DataFrame(columns=columns) + evidence = (pd.concat(parts, ignore_index=True) if parts + else pd.DataFrame({"ec_rxn": pd.Series(dtype=object), "source": pd.Series(dtype=object), + "kcat": pd.Series(dtype=float), "substrate": pd.Series(dtype=object), + "detail": pd.Series(dtype=object)})) info = [parse_kcat_source(model.ec.source[i]) for i in rows] current = np.asarray(model.ec.kcat, dtype=float)[rows] prior = np.array([np.nan if x.prior is None else x.prior for x in info], dtype=float) - tuned_mask = ~np.isnan(prior) - reference = np.where(tuned_mask, prior, current) + tuned_mask = np.array([bool(x.tuned) or x.prior is not None for x in info], dtype=bool) + has_prior = ~np.isnan(prior) + reference = np.where(has_prior, prior, current) ref_of = dict(zip(ec_rxns, reference)) ref = evidence["ec_rxn"].map(ref_of).to_numpy(dtype=float) - evidence["assigned"] = np.isclose(evidence["kcat"].to_numpy(dtype=float), ref, rtol=1e-6, atol=0) + evidence["assigned"] = np.isclose(evidence["kcat"].to_numpy(dtype=float), ref, rtol=1e-5, atol=0) evidence = evidence[columns + ["assigned"]] mat = model.ec.rxn_enz_mat.tocsr() @@ -205,11 +229,11 @@ def kcat_evidence( "origin": [x.origin for x in info], "tuned": tuned_mask, "prior": prior, - "fold": current / prior, + "fold": np.where(prior > 0, current / np.where(prior > 0, prior, 1.0), np.nan), }) medians = evidence.groupby(["source", "ec_rxn"])["kcat"].median() for name in sources: - summary[f"src_{name}"] = [medians.get((name, r), np.nan) for r in ec_rxns] + summary[f"src_{re.sub(r'[^0-9A-Za-z_]', '_', name)}"] = [medians.get((name, r), np.nan) for r in ec_rxns] if save_as is not None: summary.to_csv(save_as, sep="\t", index=False) return KcatEvidence(evidence, summary, rows) @@ -254,8 +278,11 @@ def kcats_for_reactions( :func:`~geckopy.gather_kcats.kcats_from_okp`): they submit the enzyme-substrate pairs of the selected reactions only, and leave the job and the result that ``kcats_from_okp`` keeps for all reactions as they - are. The predictions are written to ``data/kcats/okp__query.tsv`` - and read back as a source. The function waits for each job to finish and + are. The predictions are written to ``data/kcats/okp___query.tsv`` + and read back as a source; with ``sources``, the files of the methods that + were asked for are added to it. The files of earlier queries are sources + as well, so a default call also shows the predictions of an earlier query + for the same reactions. The function waits for each job to finish and submits a new job on every call. A method that fails is logged as a warning and the others continue. Each OKP method submits a job and counts against the daily prediction quota of the API key. @@ -285,19 +312,23 @@ def kcats_for_reactions( if len(rows) == 0: raise ValueError("No reaction of model.ec is selected.") ec_rxns = [model.ec.rxns[i] for i in rows] - queried = [] + queried, failed = [], [] for method in _okp_methods(okp, adapter): try: _, done = kcats_from_okp(model, method=method, ec_rxns=ec_rxns, job_tag="query", resubmit=True, wait=True, adapter=adapter) except Exception as err: # one method failing does not stop the others logger.warning("OKP %s failed: %s", method, err) + failed.append(method) continue if done: - queried.append(f"{okp_file(method)}_query") - if sources is None: - folder = Path(adapter.params.path) / "data" / "kcats" - sources = [p.stem for p in sorted(folder.glob("*.tsv")) - if not (re.fullmatch(r"okp_.*_query", p.stem) and p.stem not in queried)] + queried.append(f"{okp_file(method)}__query") + else: + failed.append(method) + if failed: + logger.warning("No OKP predictions from: %s.", ", ".join(failed)) + if sources is not None: + sources = _as_list(sources) + sources = sources + [q for q in queried if q not in sources] return kcat_evidence(model, rxns=rows, sources=sources, compute=compute, measurements=measurements, adapter=adapter) diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index bb62787..c0e52f2 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -52,6 +52,26 @@ def _rxn_subset(ec_rxns: Optional[Iterable[str]]) -> Optional[list[str]]: return ec_rxns +def _okp_rxn_subset(model: "EcModel", ec_rxns) -> Optional[list[str]]: + """``ec_rxns`` as names of ``model.ec.rxns``, from names, integer indices or a boolean array.""" + if ec_rxns is None: + return None + arr = np.asarray([ec_rxns] if isinstance(ec_rxns, str) else list(ec_rxns)) + if arr.size and arr.dtype == bool: + if arr.shape != (len(model.ec.rxns),): + raise ValueError(f"A boolean ec_rxns needs {len(model.ec.rxns)} entries, one per row of model.ec.rxns.") + return _rxn_subset(model.ec.rxns[i] for i in np.flatnonzero(arr)) + if arr.size and np.issubdtype(arr.dtype, np.integer): + if arr.min() < -len(model.ec.rxns) or arr.max() >= len(model.ec.rxns): + raise ValueError(f"ec_rxns has an index outside model.ec.rxns ({len(model.ec.rxns)} rows).") + return _rxn_subset(model.ec.rxns[i] for i in arr) + names = [str(r) for r in arr] + missing = sorted(set(names) - set(model.ec.rxns)) + if missing: + raise ValueError(f"Not in model.ec.rxns: {', '.join(missing)}.") + return _rxn_subset(names) + + def _warn_subset(ec_rxns: Optional[list[str]], model: "EcModel", path: Path) -> None: if ec_rxns is not None and len(set(ec_rxns)) < len(model.ec.rxns) and path.is_file(): logger.warning("%s is replaced by kcats for %d reactions only. Give save_as to keep it.", @@ -438,14 +458,16 @@ def kcats_from_okp( use_stored Read the stored result of this method's job without contacting OKP. ec_rxns - ``model.ec.rxns`` to predict for (default: all); only used when a - job is submitted. + ``model.ec.rxns`` to predict for (default: all), as names, integer + indices or a boolean array; only used when a job is submitted. job_tag Keeps the job, its input and its result apart from the job of the method without a tag: they are read and written as - ``data/OKP_job__.txt``, ``data/OKP_.csv`` - and ``data/OKP_output__.csv``, and the source file is - ``okp__.tsv`` unless ``save_as`` is given. A job for a + ``data/OKP_job___.txt``, ``data/OKP_query_.csv`` + and ``data/OKP_output___.csv``, and the source file is + ``okp___.tsv`` unless ``save_as`` is given. The tag is + written in lower case, with each run of other characters than letters + and digits as ``_``; it must have at least one letter or digit. A job for a few reactions with a tag leaves the job and the result of all reactions as they are. save, save_as, adapter @@ -470,10 +492,14 @@ def kcats_from_okp( ec_rxns = _rxn_subset(ec_rxns) name_default = okp_file(method) tag = name_default[len("okp_"):] - job_tag = re.sub(r"^_+|_+$", "", re.sub(r"[^a-z0-9]+", "_", job_tag.lower())) if job_tag: - tag = f"{tag}_{job_tag}" + clean = re.sub(r"^_+|_+$", "", re.sub(r"[^a-z0-9]+", "_", job_tag.lower())) + if not clean: + raise ValueError(f"job_tag {job_tag!r} has no letters or digits.") + job_tag = clean + tag = f"{tag}__{job_tag}" name_default = f"okp_{tag}" + ec_rxns = _okp_rxn_subset(model, ec_rxns) data_dir = Path(adapter.params.path) / "data" job_file = data_dir / f"OKP_job_{tag}.txt" out_file = data_dir / f"OKP_output_{tag}.csv" diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index 3442e3c..d019339 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -85,7 +85,7 @@ def _submit_open_kinetics_predictor( base_url, session For testing / custom deployments. job_tag - With a tag, the input is ``data/OKP_.csv`` and + With a tag, the input is ``data/OKP_query_.csv`` and ``data/OKP_job.txt`` is left as it is. Returns @@ -107,7 +107,7 @@ def _submit_open_kinetics_predictor( method = default_okp_method(adapter) okp = adapter.params.okp - okp_csv = data_dir / (f"OKP_{job_tag}.csv" if job_tag else "OKP.csv") + okp_csv = data_dir / (f"OKP_query_{job_tag}.csv" if job_tag else "OKP.csv") if okp_csv.is_file() and not overwrite: csv_text = okp_csv.read_text(encoding="utf-8") logger.info("Using existing %s (set overwrite=True to rebuild).", okp_csv) diff --git a/src/geckopy/kcat_tuning/sensitivity_tuning.py b/src/geckopy/kcat_tuning/sensitivity_tuning.py index a74e6b1..0bc1cb2 100644 --- a/src/geckopy/kcat_tuning/sensitivity_tuning.py +++ b/src/geckopy/kcat_tuning/sensitivity_tuning.py @@ -42,6 +42,8 @@ from .corrections import Correction, corrections as build_corrections if TYPE_CHECKING: + import pandas as pd + from ..ec_model.ec_model import EcModel @@ -78,6 +80,10 @@ class TunedKcatsResult: source kcat provenance (``model.ec.source``) of the input model, parallel to ``rxns``. + evidence + With ``evidence=True``, the ``summary`` table of + :func:`~geckopy.gather_kcats.kcat_evidence` for the tuned reactions + (``None`` otherwise). """ rxns: list[str] = field(default_factory=list) @@ -90,6 +96,7 @@ class TunedKcatsResult: default_factory=lambda: np.empty(0, dtype=float) ) source: list[str] = field(default_factory=list) + evidence: Optional["pd.DataFrame"] = None def corrections( self, @@ -123,6 +130,7 @@ def sensitivity_tuning( max_iterations: int = 1000, bio_rxn: Optional[str] = None, verbose: bool = True, + evidence: bool = False, ) -> TunedKcatsResult: """Iteratively bump the most-limiting kcat by ``fold_change`` until the model can reach ``desired_growth_rate``. @@ -183,6 +191,11 @@ def sensitivity_tuning( neither is available the model's current objective is used. verbose Whether per-iteration progress is logged at INFO. + evidence + Add ``TunedKcatsResult.evidence``, the kcats that every source file of + the project gives for the tuned reactions next to the tuned kcat (see + :func:`~geckopy.gather_kcats.kcat_evidence`). A failure to read the + source files is logged as a warning and leaves ``evidence`` empty. Returns ------- @@ -367,7 +380,15 @@ def sensitivity_tuning( max_iterations, desired_growth_rate, ) - return _build_result(model, tuned_ec_indices, input_kcat, input_source) + result = _build_result(model, tuned_ec_indices, input_kcat, input_source) + if evidence and result.rxns: + from ..gather_kcats.evidence import kcat_evidence + + try: + result.evidence = kcat_evidence(model, tuned=result).summary + except Exception as err: # the tuning is done; the evidence is extra + logger.warning("The evidence of the tuned kcats could not be read: %s", err) + return result def _mark_tuned(model: "EcModel", ec_idx: int) -> None: diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index 066eecd..5ca0498 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -306,12 +306,16 @@ def keep_original(path: Path) -> None: def _stored_predictions(project: Path) -> list[str]: """Stored predictions without a source file: ``dlkcat`` and OKP methods.""" from .gather_kcats.kcat_assignment import okp_file + from .gather_kcats.kcat_sources import OKP_METHODS data = project / "data" found = [] if (data / "DLKcat.tsv").is_file() and not (data / "kcats" / "dlkcat.tsv").exists(): found.append("dlkcat") + known = {okp_file(m)[len("okp_"):] for m in OKP_METHODS} for out in sorted(data.glob("OKP_output*.csv")): + if out.name != "OKP_output.csv" and out.stem[len("OKP_output_"):] not in known: + continue # the result of a tagged query (kcats_from_okp job_tag) job = data / ("OKP_job.txt" if out.name == "OKP_output.csv" else "OKP_job_" + out.stem[len("OKP_output_"):] + ".txt") if not job.is_file(): diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py index f520ddb..9dfe546 100644 --- a/tests/test_kcat_evidence.py +++ b/tests/test_kcat_evidence.py @@ -6,7 +6,7 @@ import pandas as pd import pytest -from geckopy.gather_kcats import kcat_evidence, kcats_for_reactions, kcats_from_okp, save_kcat_source +from geckopy.gather_kcats import ec_rxn_keys, kcat_evidence, kcats_for_reactions, kcats_from_okp, save_kcat_source from test_ec_tree import _measurements_for, _project @@ -85,5 +85,81 @@ def test_okp_job_tag_has_its_own_files(tmp_path): data = project / "data" (data / "OKP_job_kinform_h.txt").write_text("jobId: abc\nmethod: KinForm-H\nresultOf: abc\n", encoding="utf-8") (data / "OKP_output_kinform_h.csv").write_text("x\n", encoding="utf-8") - with pytest.raises(FileNotFoundError, match="OKP_job_kinform_h_query.txt"): + with pytest.raises(FileNotFoundError, match="OKP_job_kinform_h__query.txt"): kcats_from_okp(model, method="KinForm-H", job_tag="query", use_stored=True, adapter=adapter) + with pytest.raises(ValueError, match="no letters or digits"): + kcats_from_okp(model, method="KinForm-H", job_tag="!!", use_stored=True, adapter=adapter) + with pytest.raises(ValueError, match="Not in model.ec.rxns"): + kcats_from_okp(model, method="KinForm-H", ec_rxns=["nope"], adapter=adapter) + with pytest.raises(ValueError, match="outside model.ec.rxns"): + kcats_from_okp(model, method="KinForm-H", ec_rxns=[10**6], adapter=adapter) + + +def test_tagged_okp_submit_leaves_the_untagged_files(tmp_path, monkeypatch): + from geckopy.gather_kcats.open_kinetics_predictor.run import _submit_open_kinetics_predictor + from test_open_kinetics_predictor import _FakeResponse, _FakeSession, _adapter, _ec_model, _ignore + + adapter = _adapter(tmp_path) + model = _ec_model(adapter) + monkeypatch.setenv("OKP_API_KEY", "ak_test") + session = _FakeSession([("/submit/", _FakeResponse(201, {"jobId": "JOB7", "status": "Pending"}))]) + assert _submit_open_kinetics_predictor(model, ignore_lists=_ignore(), session=session, + job_tag="query", overwrite=True) == "JOB7" + data = tmp_path / "data" + assert (data / "OKP_query_query.csv").read_text().startswith("Protein Sequence,Substrate") + assert not (data / "OKP.csv").exists() and not (data / "OKP_job.txt").exists() + + +def test_empty_and_string_arguments(tmp_path): + model, adapter, _ = _project(tmp_path) + for kwargs in ({"rxns": []}, {"enzymes": []}, {"tuned": []}): + assert len(kcat_evidence(model, sources=[], adapter=adapter, **kwargs).rows) == 0 + rxn = model.ec.rxns[_rows_with_enzymes(model, 1)[0]] + result = kcat_evidence(model, tuned=rxn, sources=[], compute="ec_tree", + measurements=_measurements_for(model), adapter=adapter) + assert list(result.summary["ec_rxn"]) == [rxn] and result.summary["src_ec_tree"].notna().all() + with pytest.raises(ValueError, match="outside model.ec.rxns"): + kcat_evidence(model, rxns=[10**6], adapter=adapter) + with pytest.raises(ValueError, match="one per row"): + kcat_evidence(model, rxns=np.array([True, False]), adapter=adapter) + with pytest.raises(ValueError, match="No reaction"): + kcats_for_reactions(model, [], adapter=adapter) + + +def test_assigned_survives_the_rounding_of_the_prior(tmp_path): + model, adapter, _ = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + rxn = model.ec.rxns[i] + model.ec.kcat[i] = 123.456789 + model.ec.source[i] = "custom | tuned=sensitivity (prior=12.3457)" + _save(model, adapter, "src_a", [rxn], [12.3456789]) + result = kcat_evidence(model, rxns=[rxn], sources=["src_a"], adapter=adapter) + assert result.evidence["assigned"].all() + # A tuning status without a prior is tuned, and compared with the current kcat. + model.ec.source[i] = "custom | tuned=cmaes" + row = kcat_evidence(model, rxns=[rxn], sources=[], adapter=adapter).summary.iloc[0] + assert row["tuned"] and np.isnan(row["prior"]) and np.isnan(row["fold"]) + + +def test_a_reverse_direction_is_selected_by_its_conventional_name(tmp_path): + model, adapter, _ = _project(tmp_path) + base, _ = ec_rxn_keys(model) + reverse = [b for b in dict.fromkeys(base) if b.endswith("_REV")] + if not reverse: + pytest.skip("the test model has no reverse direction") + result = kcat_evidence(model, rxns=reverse[:1], sources=[], adapter=adapter) + assert len(result.rows) and all(base[i] == reverse[0] for i in result.rows) + forward = kcat_evidence(model, rxns=[reverse[0][:-4]], sources=[], adapter=adapter) + assert not set(forward.rows) & set(result.rows) + + +def test_a_tagged_query_result_is_not_a_stored_prediction_of_a_method(tmp_path): + from geckopy.upgrade import _stored_predictions + + data = tmp_path / "data" + data.mkdir() + (data / "OKP_output_unikp.csv").write_text("x\n") + (data / "OKP_job_unikp.txt").write_text("jobId: a\nmethod: UniKP\n") + (data / "OKP_output_kinform_h__query.csv").write_text("x\n") + (data / "OKP_job_kinform_h__query.txt").write_text("jobId: b\nmethod: KinForm-H\n") + assert _stored_predictions(tmp_path) == ["UniKP"] diff --git a/tests/test_sensitivity_tuning.py b/tests/test_sensitivity_tuning.py index 65cb18a..b91563d 100644 --- a/tests/test_sensitivity_tuning.py +++ b/tests/test_sensitivity_tuning.py @@ -188,6 +188,18 @@ def test_re_tuning_keeps_one_method_and_the_first_prior(tmp_path): # prot_to_ignore # --------------------------------------------------------------------------- # +def test_evidence_of_the_tuned_reactions(tmp_path): + adapter = _adapter(tmp_path) + model = _build_toy(adapter, initial_kcat=1.0) + off = sensitivity_tuning(model, verbose=False) + assert off.evidence is None + model = _build_toy(adapter, initial_kcat=1.0) + tuned = sensitivity_tuning(model, verbose=False, evidence=True) + assert list(tuned.evidence["ec_rxn"]) == tuned.rxns + assert tuned.evidence["kcat"].tolist() == tuned.new_kcat.tolist() + assert tuned.evidence["prior"].tolist() == tuned.old_kcat.tolist() + + def test_prot_to_ignore_excludes_enzyme(tmp_path): """If the only limiting enzyme is in `prot_to_ignore`, no tuning should happen and the function should warn and break.""" From 395526dd2f53d37b9b08267d4fd104962c26d12e Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:49:59 +0100 Subject: [PATCH 05/10] test: tune to a growth rate that needs a kcat change in the evidence test --- tests/test_sensitivity_tuning.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_sensitivity_tuning.py b/tests/test_sensitivity_tuning.py index b91563d..48eab70 100644 --- a/tests/test_sensitivity_tuning.py +++ b/tests/test_sensitivity_tuning.py @@ -189,10 +189,10 @@ def test_re_tuning_keeps_one_method_and_the_first_prior(tmp_path): # --------------------------------------------------------------------------- # def test_evidence_of_the_tuned_reactions(tmp_path): - adapter = _adapter(tmp_path) + adapter = _adapter(tmp_path, gr_exp=300.0) model = _build_toy(adapter, initial_kcat=1.0) off = sensitivity_tuning(model, verbose=False) - assert off.evidence is None + assert off.rxns and off.evidence is None model = _build_toy(adapter, initial_kcat=1.0) tuned = sensitivity_tuning(model, verbose=False, evidence=True) assert list(tuned.evidence["ec_rxn"]) == tuned.rxns From 87d54be371578541bc4eaa36cc5cf563a7eb9cbf Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:57:56 +0100 Subject: [PATCH 06/10] fix: second-round review findings of the kcat evidence functions --- src/geckopy/gather_kcats/evidence.py | 67 ++++++++++++------- src/geckopy/gather_kcats/kcats_from.py | 6 +- src/geckopy/kcat_tuning/sensitivity_tuning.py | 2 +- src/geckopy/upgrade.py | 4 +- tests/test_kcat_evidence.py | 52 ++++++++++++++ 5 files changed, 99 insertions(+), 32 deletions(-) diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index 03a5a51..25b6a1e 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -40,8 +40,10 @@ class KcatEvidence(NamedTuple): def _as_list(value) -> list: - """A list from a string, an iterable or a numpy array.""" - if isinstance(value, str): + """A list from a scalar, a string, an iterable or a numpy array.""" + if value is None: + return [] + if isinstance(value, (str, int, np.integer)) or np.ndim(value) == 0: return [value] return list(value) @@ -133,8 +135,8 @@ def kcat_evidence( compute Sources to compute for the selected reactions; ``"ec_tree"`` is supported. The computed estimate replaces a source file of the same - name, so it follows ``measurements`` and the ``k`` of the adapter - (default 5) and is not outdated. + name, so it follows ``measurements`` and ``[kcat_sources.ec_tree] k`` + of the adapter (default 5) and is not outdated. measurements BRENDA measurement table for the EC-tree estimate (default: the adapter's table). @@ -173,13 +175,21 @@ def kcat_evidence( if sources is None: folder = Path(adapter.params.path) / "data" / "kcats" sources = sorted(p.stem for p in folder.glob("*.tsv")) - sources = _as_list(sources) + explicit = sources is not None + sources = list(dict.fromkeys(_as_list(sources))) if "ec_tree" in compute: sources = [s for s in sources if s != "ec_tree"] parts = [] - for name in sources: - kcat_list, _ = load_kcat_source(model, name, adapter=adapter) + for name in list(sources): + try: + kcat_list, _ = load_kcat_source(model, name, adapter=adapter) + except Exception as err: + if explicit: + raise + logger.warning("%s is not read: %s", name, err) # a file of the default list that is no source file + sources.remove(name) + continue keep = kcat_list["rxn_id"].isin(ec_rxns) sub = kcat_list[keep] parts.append(pd.DataFrame({ @@ -189,14 +199,18 @@ def kcat_evidence( "substrate": ["; ".join(s) for s in sub["substrates"]], "detail": sub["detail"].to_numpy(), })) - if "ec_tree" in compute and len(rows): - from .kcats_from import kcats_from_ec_tree - - tree = kcats_from_ec_tree(model, ec_rxns=ec_rxns, save=False, measurements=measurements, adapter=adapter) - parts.append(pd.DataFrame({ - "ec_rxn": tree["rxn_id"].to_numpy(), "source": "ec_tree", - "kcat": tree["kcat"].to_numpy(dtype=float), "substrate": "", "detail": tree["detail"].to_numpy(), - })) + if "ec_tree" in compute: + if len(rows): + from .kcat_assignment import kcat_assignment_config + from .kcats_from import kcats_from_ec_tree + + k = float(kcat_assignment_config(adapter).sources.get("ec_tree", {}).get("k", 5)) + tree = kcats_from_ec_tree(model, k=k, ec_rxns=ec_rxns, save=False, measurements=measurements, + adapter=adapter) + parts.append(pd.DataFrame({ + "ec_rxn": tree["rxn_id"].to_numpy(), "source": "ec_tree", + "kcat": tree["kcat"].to_numpy(dtype=float), "substrate": "", "detail": tree["detail"].to_numpy(), + })) sources.append("ec_tree") columns = ["ec_rxn", "source", "kcat", "substrate", "detail"] evidence = (pd.concat(parts, ignore_index=True) if parts @@ -240,9 +254,9 @@ def kcat_evidence( def _okp_methods(okp, adapter: "ModelAdapter") -> list[str]: - if okp is None or okp is False: - return [] - if okp is True: + if okp is None or isinstance(okp, (bool, np.bool_, int, np.integer)): + if not okp: + return [] from .kcat_assignment import default_okp_method return [default_okp_method(adapter)] @@ -250,7 +264,7 @@ def _okp_methods(okp, adapter: "ModelAdapter") -> list[str]: if okp.lower() == "all": return list(_PAIR_METHODS) okp = [okp] - methods = list(okp) + methods = list(dict.fromkeys(okp)) unknown = [m for m in methods if m not in _PAIR_METHODS] if unknown: raise ValueError(f"Unknown OKP methods: {', '.join(unknown)}. Methods: {', '.join(_PAIR_METHODS)}.") @@ -296,8 +310,10 @@ def kcats_for_reactions( okp OKP methods to predict with: ``False`` for none (default), ``True`` for the first method of the adapter, ``"all"`` for every method - that :func:`~geckopy.gather_kcats.kcats_from_okp` accepts, or a method - name or a list of names. + that :func:`~geckopy.gather_kcats.kcats_from_okp` accepts (``CatRange`` + predicts kcat classes, which are read as their geometric midpoint), or + a method name or a list of names. A method that has no result in this + call leaves no query file of an earlier call behind. sources, compute, measurements, adapter As in :func:`kcat_evidence`; ``compute`` is ``("ec_tree",)`` by default. @@ -313,22 +329,23 @@ def kcats_for_reactions( raise ValueError("No reaction of model.ec is selected.") ec_rxns = [model.ec.rxns[i] for i in rows] queried, failed = [], [] + folder = Path(adapter.params.path) / "data" / "kcats" for method in _okp_methods(okp, adapter): try: _, done = kcats_from_okp(model, method=method, ec_rxns=ec_rxns, job_tag="query", resubmit=True, wait=True, adapter=adapter) except Exception as err: # one method failing does not stop the others logger.warning("OKP %s failed: %s", method, err) - failed.append(method) - continue + done = False if done: queried.append(f"{okp_file(method)}__query") else: failed.append(method) + # The predictions of an earlier query are not a result of this call. + (folder / f"{okp_file(method)}__query.tsv").unlink(missing_ok=True) if failed: logger.warning("No OKP predictions from: %s.", ", ".join(failed)) if sources is not None: - sources = _as_list(sources) - sources = sources + [q for q in queried if q not in sources] + sources = list(dict.fromkeys(_as_list(sources) + queried)) return kcat_evidence(model, rxns=rows, sources=sources, compute=compute, measurements=measurements, adapter=adapter) diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index c0e52f2..10c50de 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -489,9 +489,10 @@ def kcats_from_okp( if method in ("CatPred", "TurNup"): raise ValueError(f"{method} reads whole reactions (substrate set{', and products' if method == 'TurNup' else ''}); " "kcats_from_okp submits enzyme-substrate pairs only.") - ec_rxns = _rxn_subset(ec_rxns) + ec_rxns = _okp_rxn_subset(model, ec_rxns) name_default = okp_file(method) tag = name_default[len("okp_"):] + job_tag = str(job_tag) if job_tag else "" if job_tag: clean = re.sub(r"^_+|_+$", "", re.sub(r"[^a-z0-9]+", "_", job_tag.lower())) if not clean: @@ -499,7 +500,6 @@ def kcats_from_okp( job_tag = clean tag = f"{tag}__{job_tag}" name_default = f"okp_{tag}" - ec_rxns = _okp_rxn_subset(model, ec_rxns) data_dir = Path(adapter.params.path) / "data" job_file = data_dir / f"OKP_job_{tag}.txt" out_file = data_dir / f"OKP_output_{tag}.csv" @@ -513,7 +513,7 @@ def kcats_from_okp( ) if save: path = kcat_source_file(name, model=model, adapter=adapter) # fails before submitting - if submitting: + if submitting and not job_tag: _warn_subset(ec_rxns, model, path) if use_stored: diff --git a/src/geckopy/kcat_tuning/sensitivity_tuning.py b/src/geckopy/kcat_tuning/sensitivity_tuning.py index 0bc1cb2..fd32c48 100644 --- a/src/geckopy/kcat_tuning/sensitivity_tuning.py +++ b/src/geckopy/kcat_tuning/sensitivity_tuning.py @@ -381,7 +381,7 @@ def sensitivity_tuning( ) result = _build_result(model, tuned_ec_indices, input_kcat, input_source) - if evidence and result.rxns: + if evidence: from ..gather_kcats.evidence import kcat_evidence try: diff --git a/src/geckopy/upgrade.py b/src/geckopy/upgrade.py index 5ca0498..238ac78 100644 --- a/src/geckopy/upgrade.py +++ b/src/geckopy/upgrade.py @@ -306,15 +306,13 @@ def keep_original(path: Path) -> None: def _stored_predictions(project: Path) -> list[str]: """Stored predictions without a source file: ``dlkcat`` and OKP methods.""" from .gather_kcats.kcat_assignment import okp_file - from .gather_kcats.kcat_sources import OKP_METHODS data = project / "data" found = [] if (data / "DLKcat.tsv").is_file() and not (data / "kcats" / "dlkcat.tsv").exists(): found.append("dlkcat") - known = {okp_file(m)[len("okp_"):] for m in OKP_METHODS} for out in sorted(data.glob("OKP_output*.csv")): - if out.name != "OKP_output.csv" and out.stem[len("OKP_output_"):] not in known: + if "__" in out.stem: continue # the result of a tagged query (kcats_from_okp job_tag) job = data / ("OKP_job.txt" if out.name == "OKP_output.csv" else "OKP_job_" + out.stem[len("OKP_output_"):] + ".txt") diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py index 9dfe546..1755580 100644 --- a/tests/test_kcat_evidence.py +++ b/tests/test_kcat_evidence.py @@ -163,3 +163,55 @@ def test_a_tagged_query_result_is_not_a_stored_prediction_of_a_method(tmp_path): (data / "OKP_output_kinform_h__query.csv").write_text("x\n") (data / "OKP_job_kinform_h__query.txt").write_text("jobId: b\nmethod: KinForm-H\n") assert _stored_predictions(tmp_path) == ["UniKP"] + + +def test_scalar_and_numpy_arguments(tmp_path): + model, adapter, _ = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + assert list(kcat_evidence(model, rxns=i, sources=[], adapter=adapter).rows) == [i] + assert list(kcat_evidence(model, rxns=np.int64(i), sources=[], compute=None, adapter=adapter).rows) == [i] + assert list(kcat_evidence(model, rxns=model.ec.rxns[i], sources=[], adapter=adapter).rows) == [i] + with pytest.raises(ValueError, match="Not in model.ec.rxns"): + kcats_from_okp(model, method="KinForm-H", ec_rxns=model.ec.rxns[i] + "x", adapter=adapter) + # A single name is one reaction, not a list of characters. + with pytest.raises(FileNotFoundError): + kcats_from_okp(model, method="KinForm-H", ec_rxns=model.ec.rxns[i], job_tag="q", use_stored=True, + adapter=adapter) + + +def test_the_ec_tree_k_of_the_adapter_and_a_source_list_without_duplicates(tmp_path): + model, adapter, _ = _project(tmp_path, "[kcat_sources.ec_tree]\nk = 10\n") + i = _rows_with_enzymes(model, 1)[0] + m = _measurements_for(model) + result = kcat_evidence(model, rxns=[i], sources=["ec_tree", "ec_tree"], compute=["ec_tree"], + measurements=m, adapter=adapter) + from geckopy.gather_kcats.ec_tree import ECTree + expected = 10 ** ECTree(m, k=10).estimate(model.ec.eccodes[i])[0] + assert result.summary["src_ec_tree"].iloc[0] == pytest.approx(expected) + assert len(result.evidence) == 1 + empty = kcat_evidence(model, rxns=[], sources=[], compute=["ec_tree"], adapter=adapter) + assert "src_ec_tree" in empty.summary and len(empty.summary) == 0 + + +def test_a_file_that_is_no_source_is_skipped_in_the_default_list_only(tmp_path): + model, adapter, project = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + (project / "data" / "kcats" / "notes.tsv").write_text("this is not a source file\n", encoding="utf-8") + result = kcat_evidence(model, rxns=[i], adapter=adapter) + assert "src_notes" not in result.summary + with pytest.raises(Exception): + kcat_evidence(model, rxns=[i], sources=["notes"], adapter=adapter) + + +def test_a_method_without_result_leaves_no_old_query_file(tmp_path, monkeypatch): + model, adapter, project = _project(tmp_path) + i = _rows_with_enzymes(model, 1)[0] + stale = project / "data" / "kcats" / "okp_unikp__query.tsv" + _save(model, adapter, "okp_unikp__query", [model.ec.rxns[i]], [3.0]) + assert stale.is_file() + monkeypatch.delenv("OKP_API_KEY", raising=False) + result = kcats_for_reactions(model, [i], okp=["UniKP", "UniKP"], measurements=_measurements_for(model), + adapter=adapter) + assert not stale.exists() and "src_okp_unikp__query" not in result.summary + result = kcats_for_reactions(model, [i], okp=np.True_, measurements=_measurements_for(model), adapter=adapter) + assert len(result.summary) == 1 From 4bbbc82cc9387f9cbd3744a92f978cccacac2582 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 13:59:19 +0100 Subject: [PATCH 07/10] fix: the default source list skips a file that is no source --- src/geckopy/gather_kcats/evidence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index 25b6a1e..50acc42 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -130,7 +130,7 @@ def kcat_evidence( list of reaction names. sources Names of the source files, without ``.tsv`` (default: all files in - ``data/kcats``, including the ``okp__query.tsv`` files that + ``data/kcats``, including the ``okp___query.tsv`` files that :func:`kcats_for_reactions` wrote; an empty list for none). compute Sources to compute for the selected reactions; ``"ec_tree"`` is @@ -172,10 +172,10 @@ def kcat_evidence( rows = _select_rows(model, rxns, enzymes, tuned) ec_rxns = [model.ec.rxns[i] for i in rows] + explicit = sources is not None if sources is None: folder = Path(adapter.params.path) / "data" / "kcats" sources = sorted(p.stem for p in folder.glob("*.tsv")) - explicit = sources is not None sources = list(dict.fromkeys(_as_list(sources))) if "ec_tree" in compute: sources = [s for s in sources if s != "ec_tree"] From 6d79dcb4479b96d7207e28d6e819390a6081c396 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 14:00:11 +0100 Subject: [PATCH 08/10] test: a single reaction name is one reaction for the OKP job --- tests/test_kcat_evidence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py index 1755580..f769da2 100644 --- a/tests/test_kcat_evidence.py +++ b/tests/test_kcat_evidence.py @@ -174,7 +174,7 @@ def test_scalar_and_numpy_arguments(tmp_path): with pytest.raises(ValueError, match="Not in model.ec.rxns"): kcats_from_okp(model, method="KinForm-H", ec_rxns=model.ec.rxns[i] + "x", adapter=adapter) # A single name is one reaction, not a list of characters. - with pytest.raises(FileNotFoundError): + with pytest.raises(ValueError, match="selects the reactions of a new job"): kcats_from_okp(model, method="KinForm-H", ec_rxns=model.ec.rxns[i], job_tag="q", use_stored=True, adapter=adapter) From e2d54f456bad929ce00ef022a7a671a7efbb82db Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 22:03:20 +0100 Subject: [PATCH 09/10] Order the columns of kcat_evidence by what is compared first --- src/geckopy/gather_kcats/evidence.py | 24 ++++++++++++------------ tests/test_kcat_evidence.py | 2 ++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index 50acc42..b0265cd 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -149,12 +149,12 @@ def kcat_evidence( ------- KcatEvidence ``evidence``: one row per kcat of a source, with ``ec_rxn``, - ``source``, ``kcat``, ``substrate``, ``detail`` and ``assigned`` (the + ``kcat``, ``source``, ``substrate``, ``detail`` and ``assigned`` (the kcat is the one assigned to the reaction: the kcat before tuning for a tuned reaction, the current kcat otherwise). ``summary``: one row - per selected reaction, with ``ec_rxn``, ``enzymes``, ``eccodes``, - ``kcat`` (current), ``origin``, ``tuned``, ``prior`` (NaN when - untuned or without a positive prior), ``fold`` (``kcat / prior``) and + per selected reaction, with ``ec_rxn``, ``kcat`` (current), + ``tuned``, ``fold`` (``kcat / prior``), ``prior`` (NaN when untuned or + without a positive prior), ``origin``, ``enzymes``, ``eccodes`` and one column ``src_`` (characters other than letters, digits and ``_`` in the source name become ``_``) with the median kcat of that source (NaN without one). ``rows``: the selected indices of @@ -212,10 +212,10 @@ def kcat_evidence( "kcat": tree["kcat"].to_numpy(dtype=float), "substrate": "", "detail": tree["detail"].to_numpy(), })) sources.append("ec_tree") - columns = ["ec_rxn", "source", "kcat", "substrate", "detail"] + columns = ["ec_rxn", "kcat", "source", "substrate", "detail"] evidence = (pd.concat(parts, ignore_index=True) if parts - else pd.DataFrame({"ec_rxn": pd.Series(dtype=object), "source": pd.Series(dtype=object), - "kcat": pd.Series(dtype=float), "substrate": pd.Series(dtype=object), + else pd.DataFrame({"ec_rxn": pd.Series(dtype=object), "kcat": pd.Series(dtype=float), + "source": pd.Series(dtype=object), "substrate": pd.Series(dtype=object), "detail": pd.Series(dtype=object)})) info = [parse_kcat_source(model.ec.source[i]) for i in rows] @@ -236,14 +236,14 @@ def kcat_evidence( enzyme_text.append(";".join(model.ec.enzymes[j] for j in row.indices[row.data != 0])) summary = pd.DataFrame({ "ec_rxn": ec_rxns, - "enzymes": enzyme_text, - "eccodes": [";".join(c) if isinstance(c, (list, tuple)) else (c if isinstance(c, str) else "") - for c in (model.ec.eccodes[i] for i in rows)], "kcat": current, - "origin": [x.origin for x in info], "tuned": tuned_mask, - "prior": prior, "fold": np.where(prior > 0, current / np.where(prior > 0, prior, 1.0), np.nan), + "prior": prior, + "origin": [x.origin for x in info], + "enzymes": enzyme_text, + "eccodes": [";".join(c) if isinstance(c, (list, tuple)) else (c if isinstance(c, str) else "") + for c in (model.ec.eccodes[i] for i in rows)], }) medians = evidence.groupby(["source", "ec_rxn"])["kcat"].median() for name in sources: diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py index f769da2..f5b93f3 100644 --- a/tests/test_kcat_evidence.py +++ b/tests/test_kcat_evidence.py @@ -29,6 +29,8 @@ def test_evidence_lists_every_source(tmp_path): result = kcat_evidence(model, rxns=sel, sources=["src_a", "src_b"], adapter=adapter) assert list(result.rows) == rows assert list(result.summary["ec_rxn"]) == sel + assert list(result.summary.columns)[:8] == ["ec_rxn", "kcat", "tuned", "fold", "prior", "origin", "enzymes", "eccodes"] + assert list(result.evidence.columns) == ["ec_rxn", "kcat", "source", "substrate", "detail", "assigned"] assert list(result.summary["src_src_a"]) == [1.0, 2.0, 4.0] assert result.summary["src_src_b"].iloc[0] == 8.0 and result.summary["src_src_b"].iloc[1:].isna().all() assert len(result.evidence) == 4 From d0062ede533bf170469edf3d478ef90acaa91b45 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Thu, 19 Nov 2026 23:51:54 +0100 Subject: [PATCH 10/10] Put the src_ columns of kcat_evidence in alphabetical order --- src/geckopy/gather_kcats/evidence.py | 4 ++-- tests/test_kcat_evidence.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/geckopy/gather_kcats/evidence.py b/src/geckopy/gather_kcats/evidence.py index b0265cd..fe29eb6 100644 --- a/src/geckopy/gather_kcats/evidence.py +++ b/src/geckopy/gather_kcats/evidence.py @@ -155,7 +155,7 @@ def kcat_evidence( per selected reaction, with ``ec_rxn``, ``kcat`` (current), ``tuned``, ``fold`` (``kcat / prior``), ``prior`` (NaN when untuned or without a positive prior), ``origin``, ``enzymes``, ``eccodes`` and - one column ``src_`` (characters other than letters, digits and + one column ``src_`` per source, in alphabetical order of the source names (characters other than letters, digits and ``_`` in the source name become ``_``) with the median kcat of that source (NaN without one). ``rows``: the selected indices of ``model.ec.rxns``. A conventional reaction name in ``rxns`` or @@ -246,7 +246,7 @@ def kcat_evidence( for c in (model.ec.eccodes[i] for i in rows)], }) medians = evidence.groupby(["source", "ec_rxn"])["kcat"].median() - for name in sources: + for name in sorted(sources, key=str.lower): summary[f"src_{re.sub(r'[^0-9A-Za-z_]', '_', name)}"] = [medians.get((name, r), np.nan) for r in ec_rxns] if save_as is not None: summary.to_csv(save_as, sep="\t", index=False) diff --git a/tests/test_kcat_evidence.py b/tests/test_kcat_evidence.py index f5b93f3..37f0136 100644 --- a/tests/test_kcat_evidence.py +++ b/tests/test_kcat_evidence.py @@ -31,6 +31,7 @@ def test_evidence_lists_every_source(tmp_path): assert list(result.summary["ec_rxn"]) == sel assert list(result.summary.columns)[:8] == ["ec_rxn", "kcat", "tuned", "fold", "prior", "origin", "enzymes", "eccodes"] assert list(result.evidence.columns) == ["ec_rxn", "kcat", "source", "substrate", "detail", "assigned"] + assert list(kcat_evidence(model, rxns=sel, sources=["src_b", "src_a"], adapter=adapter).summary.columns)[-2:] == ["src_src_a", "src_src_b"] assert list(result.summary["src_src_a"]) == [1.0, 2.0, 4.0] assert result.summary["src_src_b"].iloc[0] == 8.0 and result.summary["src_src_b"].iloc[1:].isna().all() assert len(result.evidence) == 4