From b5b0ae348c80c82dd879494e14f44e0afcbc0aa1 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Fri, 20 Nov 2026 20:11:13 +0100 Subject: [PATCH 1/5] refactor: move the experimental data and condition scoring out of evotune The loader, the condition simulation and the distance live in geckopy.experiments, where the kcat tuning and the flux constraints of the protein step can both use them. The data files are fluxData.tsv, maxGrowth.tsv and zeroFlux.tsv, and the weight column is called weight. The tutorial's fluxData.tsv holds the single protein-step condition with weight 0 next to the tuning conditions. --- src/geckopy/databases/flux_data.py | 22 ++-- src/geckopy/experiments/__init__.py | 30 +++++ .../simulate.py => experiments/conditions.py} | 22 ++-- .../evotune => experiments}/data.py | 55 ++++---- .../evotune => experiments}/distance.py | 60 ++++----- src/geckopy/kcat_tuning/evotune/__init__.py | 18 --- src/geckopy/kcat_tuning/evotune/mask_size.py | 14 +- src/geckopy/kcat_tuning/evotune/tuning.py | 98 +++++++------- tests/test_evotune.py | 74 +++++------ tests/test_evotune_data.py | 85 ------------- ...tance.py => test_experiment_conditions.py} | 120 +++++++++--------- tests/test_experiment_data.py | 85 +++++++++++++ tests/test_load_flux_data.py | 44 +++++-- tests/test_mask_size.py | 30 ++--- .../full_ecModel/data/evotuneFluxData.tsv | 34 ----- tutorials/full_ecModel/data/fluxData.tsv | 37 +++++- .../{evotuneMaxGrowth.tsv => maxGrowth.tsv} | 2 +- .../{evotuneZeroExch.tsv => zeroFlux.tsv} | 0 18 files changed, 434 insertions(+), 396 deletions(-) create mode 100644 src/geckopy/experiments/__init__.py rename src/geckopy/{kcat_tuning/evotune/simulate.py => experiments/conditions.py} (91%) rename src/geckopy/{kcat_tuning/evotune => experiments}/data.py (53%) rename src/geckopy/{kcat_tuning/evotune => experiments}/distance.py (82%) delete mode 100644 tests/test_evotune_data.py rename tests/{test_evotune_simulate_distance.py => test_experiment_conditions.py} (83%) create mode 100644 tests/test_experiment_data.py delete mode 100644 tutorials/full_ecModel/data/evotuneFluxData.tsv rename tutorials/full_ecModel/data/{evotuneMaxGrowth.tsv => maxGrowth.tsv} (89%) rename tutorials/full_ecModel/data/{evotuneZeroExch.tsv => zeroFlux.tsv} (100%) diff --git a/src/geckopy/databases/flux_data.py b/src/geckopy/databases/flux_data.py index c8a9dcee..7000cb57 100644 --- a/src/geckopy/databases/flux_data.py +++ b/src/geckopy/databases/flux_data.py @@ -39,10 +39,10 @@ class FluxData: exch_rxn_ids Exchange reaction IDs in the model, matching the columns of ``exch_fluxes`` and ``exch_mets``. - evotune_rmse_weight - Optional per-condition weights for the evotune-kcat-tuning - RMSE term. ``None`` if the source file did not have the - ``evotuneRMSEweight`` column. + weight + Optional per-condition weights of the kcat-tuning + RMSE term (0 leaves the condition out). ``None`` if the source file did not have the + ``weight`` column. source Optional per-condition free-text describing where the data came from. ``None`` if the source file did not have a @@ -61,7 +61,7 @@ class FluxData: ) exch_mets: list[str] = field(default_factory=list) exch_rxn_ids: list[str] = field(default_factory=list) - evotune_rmse_weight: Optional[np.ndarray] = None + weight: Optional[np.ndarray] = None source: Optional[list[str]] = None @@ -76,7 +76,7 @@ def load_flux_data(path: str | Path) -> FluxData: Optional extra columns (in any position): - * ``evotuneRMSEweight`` (float): weights for evotune kcat tuning. + * ``weight`` (float): weight of the condition in the kcat tuning. * ``source`` (str): free-text describing the data origin. An exchange column header has the form `` ()``, split at @@ -85,7 +85,7 @@ def load_flux_data(path: str | Path) -> FluxData: ``EX_a(e)``). Both columns are dropped from the matrix and surfaced on the - returned ``FluxData`` as ``evotune_rmse_weight`` and ``source``. + returned ``FluxData`` as ``weight`` and ``source``. Ported from GECKO MATLAB: src/io/loadFluxData.m. @@ -136,7 +136,11 @@ def load_flux_data(path: str | Path) -> FluxData: # Pull out optional columns first; collect indices to drop. optional_indices: list[int] = [] - tuning_idx = _index_of(header, "evotuneRMSEweight") + if _index_of(header, "evotuneRMSEweight") is not None: + raise ValueError( + f"{path}: the column 'evotuneRMSEweight' is now called 'weight'." + ) + tuning_idx = _index_of(header, "weight") source_idx = _index_of(header, "source") tuning_weights: Optional[np.ndarray] = None @@ -180,7 +184,7 @@ def load_flux_data(path: str | Path) -> FluxData: exch_fluxes=exch_fluxes, exch_mets=exch_mets, exch_rxn_ids=exch_rxn_ids, - evotune_rmse_weight=tuning_weights, + weight=tuning_weights, source=source_values, ) diff --git a/src/geckopy/experiments/__init__.py b/src/geckopy/experiments/__init__.py new file mode 100644 index 00000000..c4be65e3 --- /dev/null +++ b/src/geckopy/experiments/__init__.py @@ -0,0 +1,30 @@ +"""Experimental data on growth and fluxes, and how a model is scored against it. + +Several functions compare a model with measured conditions: the kcat tuning +of :mod:`geckopy.kcat_tuning` and the flux constraints of +:mod:`geckopy.limit_proteins`. They share the files that hold the +measurements (:func:`load_experiment_data`), the setup of one condition in +the model and its simulation (:func:`simulate_conditions`) and the distance +between simulated and measured values (:func:`experiment_distance`). +""" +from .conditions import ConditionSimResult, simulate_conditions +from .data import ExperimentData, load_experiment_data +from .distance import ( + BIOMASS_CARBON_EQUIV, + INFEASIBLE_PENALTY, + compute_excarbon, + dataset_rmse, + experiment_distance, +) + +__all__ = [ + "BIOMASS_CARBON_EQUIV", + "INFEASIBLE_PENALTY", + "ConditionSimResult", + "ExperimentData", + "compute_excarbon", + "dataset_rmse", + "experiment_distance", + "load_experiment_data", + "simulate_conditions", +] diff --git a/src/geckopy/kcat_tuning/evotune/simulate.py b/src/geckopy/experiments/conditions.py similarity index 91% rename from src/geckopy/kcat_tuning/evotune/simulate.py rename to src/geckopy/experiments/conditions.py index 34001db6..d38adba2 100644 --- a/src/geckopy/kcat_tuning/evotune/simulate.py +++ b/src/geckopy/experiments/conditions.py @@ -1,8 +1,8 @@ -"""FBA simulation half of the evotune kcat tuning distance function. +"""Simulate the measured conditions in a model. -This module only does the FBA half: given an already kcat-constrained +This module only does the FBA half of the distance: given an already kcat-constrained ``EcModel`` and one :class:`~geckopy.databases.flux_data.FluxData` -dataset (either ``evotune_data.flux_data`` or ``evotune_data.max_grate``), +dataset (either ``experiment_data.flux_data`` or ``experiment_data.max_growth``), simulate every condition and report growth + exchange fluxes. :mod:`.distance` turns those raw numbers into an RMSE against the matching experimental values -- kept separate so the RMSE math is @@ -14,11 +14,11 @@ from typing import TYPE_CHECKING, Callable, Optional import numpy as np -from ...solver_support import optimize_model +from ..solver_support import optimize_model if TYPE_CHECKING: - from ...databases.flux_data import FluxData - from ...ec_model.ec_model import EcModel + from ..databases.flux_data import FluxData + from ..ec_model.ec_model import EcModel @dataclass @@ -37,7 +37,7 @@ class ConditionSimResult: block_fluxes: dict[str, float] = field(default_factory=dict) -def simulate_evotune_dataset( +def simulate_conditions( model: "EcModel", flux_data: "FluxData", *, @@ -59,8 +59,8 @@ def simulate_evotune_dataset( another row's simulation. 2. For row ``i``, that row's own carbon-source reaction is unblocked: fixed at the measured uptake rate if ``constrain`` - is True (``evotune_data.flux_data``), or fully opened to ``-1000`` - if ``constrain`` is False (``evotune_data.max_grate`` -- "what's + is True (``experiment_data.flux_data``), or fully opened to ``-1000`` + if ``constrain`` is False (``experiment_data.max_growth`` -- "what's the *best possible* growth on this carbon source", not "growth at this measured rate"). 3. Optional per-condition adjustments run if the corresponding @@ -68,7 +68,7 @@ def simulate_evotune_dataset( column reads exactly 0, protein-content rescaling when ``Ptot`` is given and nonzero. 4. Solve; record growth, plus (``constrain`` datasets only, since - ``max_grate`` has no exchange measurements to compare against) + ``max_growth`` has no exchange measurements to compare against) every non-NaN measured exchange's simulated flux and every ``zero_flux_rxns`` entry's simulated flux. @@ -90,7 +90,7 @@ def simulate_evotune_dataset( for max-growth data (open uptake fully). zero_flux_rxns Reaction IDs assumed zero-flux in every condition - (``evotune_data.zero_flux``); the row's own carbon source is + (``experiment_data.zero_flux``); the row's own carbon source is skipped even if it's also listed here. bio_rxn_id The biomass reaction ID (``adapter.params.bio_rxn``). diff --git a/src/geckopy/kcat_tuning/evotune/data.py b/src/geckopy/experiments/data.py similarity index 53% rename from src/geckopy/kcat_tuning/evotune/data.py rename to src/geckopy/experiments/data.py index 75d03ec7..9240050b 100644 --- a/src/geckopy/kcat_tuning/evotune/data.py +++ b/src/geckopy/experiments/data.py @@ -1,12 +1,13 @@ -"""Load experimental data for evotune (CMA-ES) kcat tuning. +"""Load the measured growth rates and fluxes of the conditions. -The flux and max-growth files, ``evotuneFluxData.tsv`` and -``evotuneMaxGrowth.tsv``, share the column layout -``Condition Ptot grRate () ... evotuneRMSEweight source`` with -regular flux data, so both reuse -:func:`~geckopy.databases.flux_data.load_flux_data` unchanged; only -``evotuneZeroExch.tsv`` (a single ``Rxns`` id column) needs a loader of -its own. +The flux and max-growth files, ``fluxData.tsv`` and ``maxGrowth.tsv``, +share the column layout +``Condition Ptot grRate () ... weight source``, so both use +:func:`~geckopy.databases.flux_data.load_flux_data`; only ``zeroFlux.tsv`` +(a single ``Rxns`` id column) needs a loader of its own. The functions that +constrain a model with one condition read the same ``fluxData.tsv``; the +kcat tuning uses every condition in it, where a ``weight`` of 0 leaves a +condition out of the tuning. All three files are optional: a missing file yields ``None`` (or an empty list for ``zero_flux``), never an exception. @@ -17,46 +18,46 @@ from pathlib import Path from typing import TYPE_CHECKING, Optional -from ...databases.flux_data import FluxData, load_flux_data +from ..databases.flux_data import FluxData, load_flux_data if TYPE_CHECKING: - from ...adapter import ModelAdapter + from ..adapter import ModelAdapter -_FLUX_DATA_FILENAME = "evotuneFluxData.tsv" -_MAX_GROWTH_FILENAME = "evotuneMaxGrowth.tsv" -_ZERO_EXCH_FILENAME = "evotuneZeroExch.tsv" +_FLUX_DATA_FILENAME = "fluxData.tsv" +_MAX_GROWTH_FILENAME = "maxGrowth.tsv" +_ZERO_EXCH_FILENAME = "zeroFlux.tsv" @dataclass -class EvotuneData: - """Experimental data used by evotune kcat tuning. +class ExperimentData: + """Measured conditions, as the tuning functions use them. Attributes ---------- flux_data Per-condition growth/exchange-flux measurements, from - ``evotuneFluxData.tsv``. ``None`` if that file isn't present. - max_grate + ``fluxData.tsv``. ``None`` if that file isn't present. + max_growth Per-condition maximum-growth-rate measurements (one active carbon source per condition, at ``-1000`` i.e. unconstrained), - from ``evotuneMaxGrowth.tsv``. ``None`` if that file isn't + from ``maxGrowth.tsv``. ``None`` if that file isn't present. zero_flux Reaction IDs assumed to carry zero flux, from - ``evotuneZeroExch.tsv``. Empty if that file isn't present. + ``zeroFlux.tsv``. Empty if that file isn't present. """ flux_data: Optional[FluxData] = None - max_grate: Optional[FluxData] = None + max_growth: Optional[FluxData] = None zero_flux: list[str] = field(default_factory=list) -def load_evotune_data(adapter: "ModelAdapter") -> EvotuneData: - """Load the three evotune-kcat-tuning experimental data files. +def load_experiment_data(adapter: "ModelAdapter") -> ExperimentData: + """Load the three experimental data files. Each file is optional and resolved as ``/data/``; a missing file yields - ``None`` (``flux_data``/``max_grate``) or an empty list + ``None`` (``flux_data``/``max_growth``) or an empty list (``zero_flux``) rather than raising. Parameters @@ -67,12 +68,12 @@ def load_evotune_data(adapter: "ModelAdapter") -> EvotuneData: Returns ------- - EvotuneData + ExperimentData """ data_dir = Path(adapter.params.path) / "data" - return EvotuneData( + return ExperimentData( flux_data=_load_optional_flux_data(data_dir / _FLUX_DATA_FILENAME), - max_grate=_load_optional_flux_data(data_dir / _MAX_GROWTH_FILENAME), + max_growth=_load_optional_flux_data(data_dir / _MAX_GROWTH_FILENAME), zero_flux=_load_zero_exch(data_dir / _ZERO_EXCH_FILENAME), ) @@ -84,7 +85,7 @@ def _load_optional_flux_data(path: Path) -> Optional[FluxData]: def _load_zero_exch(path: Path) -> list[str]: - """Parse ``evotuneZeroExch.tsv``: a single ``Rxns`` header, then + """Parse ``zeroFlux.tsv``: a single ``Rxns`` header, then one reaction ID per line.""" if not path.is_file(): return [] diff --git a/src/geckopy/kcat_tuning/evotune/distance.py b/src/geckopy/experiments/distance.py similarity index 82% rename from src/geckopy/kcat_tuning/evotune/distance.py rename to src/geckopy/experiments/distance.py index 45944710..fa390183 100644 --- a/src/geckopy/kcat_tuning/evotune/distance.py +++ b/src/geckopy/experiments/distance.py @@ -1,6 +1,6 @@ -"""Carbon- and condition-weighted RMSE distance for evotune kcat tuning. +"""Carbon- and condition-weighted RMSE distance between a model and the data. -:mod:`.simulate` does the FBA half; this module is pure numpy (plus one +:mod:`.conditions` does the FBA half; this module is pure numpy (plus one small model-reading helper for the carbon weights) so the RMSE math is directly testable against hand-computed expected values, no ``EcModel``/FBA involved. @@ -21,12 +21,12 @@ import numpy as np -from .simulate import ConditionSimResult +from .conditions import ConditionSimResult if TYPE_CHECKING: - from ...ec_model.ec_model import EcModel - from .data import EvotuneData - from ...databases.flux_data import FluxData + from ..ec_model.ec_model import EcModel + from .data import ExperimentData + from ..databases.flux_data import FluxData #: MATLAB's hardcoded assumption of the biomass reaction's carbon #: content, in Cmmol/gDCW, used to put growth-rate deviations on the @@ -102,15 +102,15 @@ def dataset_rmse( Parameters ---------- flux_data - The dataset being scored (``evotune_data.flux_data`` or - ``evotune_data.max_grate``). + The dataset being scored (``experiment_data.flux_data`` or + ``experiment_data.max_growth``). sims - One :class:`~.simulate.ConditionSimResult` per + One :class:`~.conditions.ConditionSimResult` per ``flux_data.conds`` row, e.g. from - :func:`~.simulate.simulate_evotune_dataset`. + :func:`~.conditions.simulate_conditions`. constrain Must match the value passed to - :func:`~.simulate.simulate_evotune_dataset` for these + :func:`~.conditions.simulate_conditions` for these ``sims``: True includes carbon-weighted exchange-flux terms (flux data), False scores growth alone (max-growth data). excarbon @@ -127,7 +127,7 @@ def dataset_rmse( Returns ------- rmse : float - Weighted (by ``flux_data.evotune_rmse_weight``, if present) + Weighted (by ``flux_data.weight``, if present) mean of the per-condition RMSEs. rmse_list : numpy.ndarray The per-condition RMSEs (post-penalty, pre-weighting), shape @@ -174,16 +174,16 @@ def dataset_rmse( rmse_list[i] = float(np.sqrt(np.mean(diff ** 2))) weighted = rmse_list - if flux_data.evotune_rmse_weight is not None: - weighted = rmse_list * flux_data.evotune_rmse_weight + if flux_data.weight is not None: + weighted = rmse_list * flux_data.weight return float(np.mean(weighted)), rmse_list -def evotune_distance( - evotune_data: "EvotuneData", +def experiment_distance( + experiment_data: "ExperimentData", *, flux_sims: list[ConditionSimResult] | None, - max_grate_sims: list[ConditionSimResult] | None, + max_growth_sims: list[ConditionSimResult] | None, excarbon: dict[str, float], bio_rxn_id: str, penalty: float = INFEASIBLE_PENALTY, @@ -191,9 +191,9 @@ def evotune_distance( ) -> tuple[float, dict[str, np.ndarray]]: """Combine both datasets' RMSE into one score. - ``flux_sims`` must be given (non-``None``) iff ``evotune_data.flux_data`` - is not ``None``, and likewise for ``max_grate_sims`` / - ``evotune_data.max_grate``. A dataset absent from ``evotune_data`` doesn't + ``flux_sims`` must be given (non-``None``) iff ``experiment_data.flux_data`` + is not ``None``, and likewise for ``max_growth_sims`` / + ``experiment_data.max_growth``. A dataset absent from ``experiment_data`` doesn't contribute to the combined RMSE (MATLAB: a missing dataset's ``rmse_*`` stays ``[]`` and drops out of ``validIdx``). @@ -201,7 +201,7 @@ def evotune_distance( ---------- max_growth_weight Relative weight of the max-growth dataset against the flux - dataset, giving ``(rmse_flux + w * rmse_max_grate) / (w + 1)``. + dataset, giving ``(rmse_flux + w * rmse_max_growth) / (w + 1)``. At 2 the eight max-growth conditions have twice the weight of the 33 flux conditions, which is the intended emphasis here: the max-growth set is where the carbon-source diversity lives, @@ -215,17 +215,17 @@ def evotune_distance( if neither dataset is present. detail : dict[str, numpy.ndarray] Per-condition RMSE arrays, keyed ``"flux_data"`` / - ``"max_grate"`` for whichever datasets were scored. + ``"max_growth"`` for whichever datasets were scored. """ parts: list[float] = [] weights: list[float] = [] detail: dict[str, np.ndarray] = {} - if evotune_data.flux_data is not None: + if experiment_data.flux_data is not None: if flux_sims is None: - raise ValueError("evotune_data.flux_data is set but flux_sims is None.") + raise ValueError("experiment_data.flux_data is set but flux_sims is None.") rmse, per_cond = dataset_rmse( - evotune_data.flux_data, flux_sims, + experiment_data.flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id=bio_rxn_id, penalty=penalty, ) @@ -233,17 +233,17 @@ def evotune_distance( weights.append(1.0) detail["flux_data"] = per_cond - if evotune_data.max_grate is not None: - if max_grate_sims is None: - raise ValueError("evotune_data.max_grate is set but max_grate_sims is None.") + if experiment_data.max_growth is not None: + if max_growth_sims is None: + raise ValueError("experiment_data.max_growth is set but max_growth_sims is None.") rmse, per_cond = dataset_rmse( - evotune_data.max_grate, max_grate_sims, + experiment_data.max_growth, max_growth_sims, constrain=False, excarbon=excarbon, bio_rxn_id=bio_rxn_id, penalty=penalty, ) parts.append(rmse) weights.append(float(max_growth_weight)) - detail["max_grate"] = per_cond + detail["max_growth"] = per_cond if parts: rmse = float(np.dot(parts, weights) / np.sum(weights)) diff --git a/src/geckopy/kcat_tuning/evotune/__init__.py b/src/geckopy/kcat_tuning/evotune/__init__.py index 9fc8530c..a6263f79 100644 --- a/src/geckopy/kcat_tuning/evotune/__init__.py +++ b/src/geckopy/kcat_tuning/evotune/__init__.py @@ -5,14 +5,6 @@ :func:`~.tuning.select_tunable_mask`, then :func:`~.tuning.cmaes_kcat_tuning`. """ -from .data import EvotuneData, load_evotune_data -from .distance import ( - BIOMASS_CARBON_EQUIV, - INFEASIBLE_PENALTY, - evotune_distance, - compute_excarbon, - dataset_rmse, -) from .mask_size import ( MaskSizePoint, recommend_target_impact_share, @@ -25,7 +17,6 @@ classify_kcat_source, classify_kcat_sources, ) -from .simulate import ConditionSimResult, simulate_evotune_dataset from .tying import isozyme_tie_map from .tuning import ( EvotuneResult, @@ -36,29 +27,20 @@ ) __all__ = [ - "BIOMASS_CARBON_EQUIV", - "INFEASIBLE_PENALTY", "UNLABELLED_GROUP", - "EvotuneData", "EvotuneResult", - "ConditionSimResult", "MaskSizePoint", - "evotune_distance", "best_parsimonious", "build_sigma0_log", "classify_kcat_source", "classify_kcat_sources", "cmaes_kcat_tuning", - "compute_excarbon", - "dataset_rmse", "isozyme_tie_map", - "load_evotune_data", "n_changed", "parsimony_frontier", "recommend_target_impact_share", "screen_kcat_leverage", "select_tunable_mask", - "simulate_evotune_dataset", "sweep_tunable_mask_size", "tune_prior_penalty_weight", ] diff --git a/src/geckopy/kcat_tuning/evotune/mask_size.py b/src/geckopy/kcat_tuning/evotune/mask_size.py index 76f0ee8b..801109f0 100644 --- a/src/geckopy/kcat_tuning/evotune/mask_size.py +++ b/src/geckopy/kcat_tuning/evotune/mask_size.py @@ -37,7 +37,7 @@ from ...adapter import ModelAdapter from ...adapter.params import EvotuneParams from ...ec_model.ec_model import EcModel - from .data import EvotuneData + from ...experiments.data import ExperimentData @dataclass(frozen=True) @@ -94,7 +94,7 @@ def sweep_tunable_mask_size( *, adapter: Optional["ModelAdapter"] = None, params: Optional["EvotuneParams"] = None, - evotune_data: Optional["EvotuneData"] = None, + experiment_data: Optional["ExperimentData"] = None, bio_rxn: Optional[str] = None, make_anaerobic: Optional[Callable[["EcModel"], None]] = None, change_protein_biomass: Optional[Callable[["EcModel", float], None]] = None, @@ -156,7 +156,7 @@ def sweep_tunable_mask_size( screen A precomputed :func:`~.tuning.screen_kcat_leverage` report. Computed once, fresh, if not given. - adapter, params, evotune_data, bio_rxn, make_anaerobic, + adapter, params, experiment_data, bio_rxn, make_anaerobic, change_protein_biomass, n_proc, verbose Forwarded to :func:`~.tuning.screen_kcat_leverage` and :func:`~.tuning.cmaes_kcat_tuning`; see their docstrings. @@ -166,12 +166,12 @@ def sweep_tunable_mask_size( list of :class:`MaskSizePoint`, one per share, sorted by ``target_impact_share`` ascending. """ - params, evotune_data, bio_rxn = _resolve_context( - model, adapter, params, evotune_data, bio_rxn) + params, experiment_data, bio_rxn = _resolve_context( + model, adapter, params, experiment_data, bio_rxn) if screen is None: screen = screen_kcat_leverage( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, bio_rxn=bio_rxn, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, n_proc=n_proc, @@ -185,7 +185,7 @@ def sweep_tunable_mask_size( for seed in seeds: run_model = model.copy() result = cmaes_kcat_tuning( - run_model, adapter=adapter, params=params, evotune_data=evotune_data, + run_model, adapter=adapter, params=params, experiment_data=experiment_data, bio_rxn=bio_rxn, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, tunable_mask=mask, n_proc=n_proc, seed=seed, verbose=False, diff --git a/src/geckopy/kcat_tuning/evotune/tuning.py b/src/geckopy/kcat_tuning/evotune/tuning.py index a7ae0054..6507bf5e 100644 --- a/src/geckopy/kcat_tuning/evotune/tuning.py +++ b/src/geckopy/kcat_tuning/evotune/tuning.py @@ -71,11 +71,11 @@ from ...ec_model.pipeline.apply_kcat import apply_kcat_constraints from ...gather_kcats.kcat_source_format import format_kcat_source, parse_kcat_source from ..corrections import Correction, corrections as build_corrections -from .data import EvotuneData, load_evotune_data -from .distance import evotune_distance, compute_excarbon +from ...experiments.data import ExperimentData, load_experiment_data +from ...experiments.distance import experiment_distance, compute_excarbon from .parsimony import fold_change, n_changed from .priors import build_sigma0_log, classify_kcat_sources, warn_unlabelled -from .simulate import simulate_evotune_dataset +from ...experiments.conditions import simulate_conditions from .tying import isozyme_tie_map with warnings.catch_warnings(): @@ -159,7 +159,7 @@ def corrections( ) -def _resolve_context(model, adapter, params, evotune_data, bio_rxn): +def _resolve_context(model, adapter, params, experiment_data, bio_rxn): """Resolve every optional input against ``model``'s adapter, or raise if it can't be and none was given explicitly.""" if adapter is None: @@ -171,22 +171,22 @@ def _resolve_context(model, adapter, params, evotune_data, bio_rxn): "adapter.params.evotune from." ) params = adapter.params.evotune - if evotune_data is None: + if experiment_data is None: if adapter is None: raise ValueError( - "evotune_data not given and model has no adapter to load it from." + "experiment_data not given and model has no adapter to load it from." ) - evotune_data = load_evotune_data(adapter) - if evotune_data.flux_data is None and evotune_data.max_grate is None: + experiment_data = load_experiment_data(adapter) + if experiment_data.flux_data is None and experiment_data.max_growth is None: raise ValueError( - "evotune_data has neither flux_data nor max_grate -- nothing to " + "experiment_data has neither flux_data nor max_growth -- nothing to " "tune against." ) if bio_rxn is None: if adapter is None: raise ValueError("bio_rxn not given and model has no adapter.") bio_rxn = adapter.params.bio_rxn - return params, evotune_data, bio_rxn + return params, experiment_data, bio_rxn #: Termination criteria of ``cma`` that a flat objective triggers. @@ -194,7 +194,7 @@ def _resolve_context(model, adapter, params, evotune_data, bio_rxn): def _tunable_context( - model, params, evotune_data, bio_rxn, tunable_mask, + model, params, experiment_data, bio_rxn, tunable_mask, ): """The tunable subset plus everything derived from it: tie groups, trust weights, and the excarbon table. Shared by every function in @@ -224,10 +224,10 @@ def _tunable_context( sigma0_log = build_sigma0_log(groups, params) excarbon_rxn_ids: set[str] = {bio_rxn} - for data in (evotune_data.flux_data, evotune_data.max_grate): + for data in (experiment_data.flux_data, experiment_data.max_growth): if data is not None: excarbon_rxn_ids.update(data.exch_rxn_ids) - excarbon_rxn_ids.update(evotune_data.zero_flux) + excarbon_rxn_ids.update(experiment_data.zero_flux) excarbon = compute_excarbon(model, excarbon_rxn_ids, bio_rxn_id=bio_rxn) tie_map = ( @@ -243,7 +243,7 @@ def screen_kcat_leverage( *, adapter: Optional["ModelAdapter"] = None, params: Optional["EvotuneParams"] = None, - evotune_data: Optional[EvotuneData] = None, + experiment_data: Optional[ExperimentData] = None, bio_rxn: Optional[str] = None, make_anaerobic: Optional[Callable[["EcModel"], None]] = None, change_protein_biomass: Optional[Callable[["EcModel", float], None]] = None, @@ -290,11 +290,11 @@ def screen_kcat_leverage( :func:`select_tunable_mask` needs to expand a row back into a mask; drop it before showing the table to a person. """ - params, evotune_data, bio_rxn = _resolve_context( - model, adapter, params, evotune_data, bio_rxn) + params, experiment_data, bio_rxn = _resolve_context( + model, adapter, params, experiment_data, bio_rxn) (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, excarbon, tie_map) = _tunable_context( - model, params, evotune_data, bio_rxn, tunable_mask) + model, params, experiment_data, bio_rxn, tunable_mask) reps = np.flatnonzero(tie_map == np.arange(len(tie_map))) members_of = {int(r): np.flatnonzero(tie_map == r) for r in reps} @@ -316,7 +316,7 @@ def screen_kcat_leverage( if n_proc == 1: rmses = [ _score_kcat_vector( - model, tunable_idx, ec_rxn_ids_tunable, evotune_data, excarbon, + model, tunable_idx, ec_rxn_ids_tunable, experiment_data, excarbon, bio_rxn, v, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, max_growth_weight=params.max_growth_weight, @@ -326,7 +326,7 @@ def screen_kcat_leverage( else: with ProcessPool( n_proc, initializer=_init_worker, - initargs=(model, tunable_idx, ec_rxn_ids_tunable, evotune_data, + initargs=(model, tunable_idx, ec_rxn_ids_tunable, experiment_data, excarbon, bio_rxn, make_anaerobic, change_protein_biomass, params.max_growth_weight, 0.0, None, None), ) as pool: @@ -403,7 +403,7 @@ def cmaes_kcat_tuning( *, adapter: Optional["ModelAdapter"] = None, params: Optional["EvotuneParams"] = None, - evotune_data: Optional[EvotuneData] = None, + experiment_data: Optional[ExperimentData] = None, bio_rxn: Optional[str] = None, make_anaerobic: Optional[Callable[["EcModel"], None]] = None, change_protein_biomass: Optional[Callable[["EcModel", float], None]] = None, @@ -419,7 +419,7 @@ def cmaes_kcat_tuning( 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:`load_experiment_data`). :func:`~geckopy.kcat_tuning.sensitivity_tuning` is the standard tuning, which needs one maximum growth rate. @@ -460,7 +460,7 @@ def cmaes_kcat_tuning( verbose Whether per-generation progress is logged at INFO. make_anaerobic, change_protein_biomass - Forwarded to ``simulate.simulate_evotune_dataset`` -- see its + Forwarded to ``simulate.simulate_conditions`` -- see its docstring; geckopy has no generic organism-agnostic implementation of these yet. See the module docstring's "Parallel scoring" section for a picklability caveat when @@ -482,13 +482,13 @@ def cmaes_kcat_tuning( parameters remain after masking and tying -- too little for CMA-ES to search over. """ - params, evotune_data, bio_rxn = _resolve_context( - model, adapter, params, evotune_data, bio_rxn) + params, experiment_data, bio_rxn = _resolve_context( + model, adapter, params, experiment_data, bio_rxn) if tunable_mask is None: if screen is None: screen = screen_kcat_leverage( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, bio_rxn=bio_rxn, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, @@ -499,7 +499,7 @@ def cmaes_kcat_tuning( (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, excarbon, tie_map) = _tunable_context( - model, params, evotune_data, bio_rxn, tunable_mask) + model, params, experiment_data, bio_rxn, tunable_mask) reps = np.flatnonzero(tie_map == np.arange(len(tie_map))) if len(reps) < 2: @@ -542,7 +542,7 @@ def expand(xm: np.ndarray) -> np.ndarray: nullcontext(None) if n_proc == 1 else ProcessPool( n_proc, initializer=_init_worker, - initargs=(model, tunable_idx, ec_rxn_ids_tunable, evotune_data, + initargs=(model, tunable_idx, ec_rxn_ids_tunable, experiment_data, excarbon, bio_rxn, make_anaerobic, change_protein_biomass, params.max_growth_weight, params.prior_penalty_weight, kcat0, sigma0_log), @@ -553,7 +553,7 @@ def _score_batch(kcat_matrix, pool): if pool is None: return np.array([ _score_kcat_vector( - model, tunable_idx, ec_rxn_ids_tunable, evotune_data, + model, tunable_idx, ec_rxn_ids_tunable, experiment_data, excarbon, bio_rxn, kcat_matrix[:, j], make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, @@ -629,7 +629,7 @@ def tune_prior_penalty_weight( *, adapter: Optional["ModelAdapter"] = None, params: Optional["EvotuneParams"] = None, - evotune_data: Optional[EvotuneData] = None, + experiment_data: Optional[ExperimentData] = None, bio_rxn: Optional[str] = None, make_anaerobic: Optional[Callable[["EcModel"], None]] = None, change_protein_biomass: Optional[Callable[["EcModel", float], None]] = None, @@ -700,13 +700,13 @@ def tune_prior_penalty_weight( ``median_fold_spread`` and ``max_fold_spread`` (among movers, the largest fold-change between what the two seeds landed on). """ - params, evotune_data, bio_rxn = _resolve_context( - model, adapter, params, evotune_data, bio_rxn) + params, experiment_data, bio_rxn = _resolve_context( + model, adapter, params, experiment_data, bio_rxn) if tunable_mask is None: if screen is None: screen = screen_kcat_leverage( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, bio_rxn=bio_rxn, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, @@ -733,7 +733,7 @@ def _reset(): for seed in seeds: _reset() result = cmaes_kcat_tuning( - model, adapter=adapter, params=lam_params, evotune_data=evotune_data, + model, adapter=adapter, params=lam_params, experiment_data=experiment_data, bio_rxn=bio_rxn, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, @@ -844,7 +844,7 @@ def _score_kcat_vector( model: "EcModel", tunable_idx: np.ndarray, ec_rxn_ids_tunable: list[str], - evotune_data: EvotuneData, + experiment_data: ExperimentData, excarbon: dict[str, float], bio_rxn_id: str, kcat_vec: np.ndarray, @@ -874,25 +874,25 @@ def _score_kcat_vector( _reset_solver_basis(model) flux_sims = None - if evotune_data.flux_data is not None: - flux_sims = simulate_evotune_dataset( - model, evotune_data.flux_data, - constrain=True, zero_flux_rxns=evotune_data.zero_flux, + if experiment_data.flux_data is not None: + flux_sims = simulate_conditions( + model, experiment_data.flux_data, + constrain=True, zero_flux_rxns=experiment_data.zero_flux, bio_rxn_id=bio_rxn_id, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, ) - max_grate_sims = None - if evotune_data.max_grate is not None: - max_grate_sims = simulate_evotune_dataset( - model, evotune_data.max_grate, + max_growth_sims = None + if experiment_data.max_growth is not None: + max_growth_sims = simulate_conditions( + model, experiment_data.max_growth, constrain=False, zero_flux_rxns=[], bio_rxn_id=bio_rxn_id, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, ) - rmse, _ = evotune_distance( - evotune_data, flux_sims=flux_sims, max_grate_sims=max_grate_sims, + rmse, _ = experiment_distance( + experiment_data, flux_sims=flux_sims, max_growth_sims=max_growth_sims, excarbon=excarbon, bio_rxn_id=bio_rxn_id, max_growth_weight=max_growth_weight, ) @@ -914,7 +914,7 @@ def _score_kcat_vector( _WORKER_MODEL: Optional["EcModel"] = None _WORKER_TUNABLE_IDX: Optional[np.ndarray] = None _WORKER_EC_RXN_IDS_TUNABLE: Optional[list[str]] = None -_WORKER_EVOTUNE_DATA: Optional[EvotuneData] = None +_WORKER_EXPERIMENT_DATA: Optional[ExperimentData] = None _WORKER_EXCARBON: Optional[dict[str, float]] = None _WORKER_BIO_RXN: Optional[str] = None _WORKER_MAKE_ANAEROBIC = None @@ -929,7 +929,7 @@ def _init_worker( model: "EcModel", tunable_idx: np.ndarray, ec_rxn_ids_tunable: list[str], - evotune_data: EvotuneData, + experiment_data: ExperimentData, excarbon: dict[str, float], bio_rxn_id: str, make_anaerobic, @@ -943,14 +943,14 @@ def _init_worker( (deserialised by ``ProcessPool``, not by us) and everything else needed to score a candidate.""" global _WORKER_MODEL, _WORKER_TUNABLE_IDX, _WORKER_EC_RXN_IDS_TUNABLE - global _WORKER_EVOTUNE_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN + global _WORKER_EXPERIMENT_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN global _WORKER_MAKE_ANAEROBIC, _WORKER_CHANGE_PROTEIN_BIOMASS global _WORKER_MAX_GROWTH_WEIGHT, _WORKER_PRIOR_PENALTY_WEIGHT global _WORKER_KCAT0, _WORKER_SIGMA0_LOG _WORKER_MODEL = model _WORKER_TUNABLE_IDX = tunable_idx _WORKER_EC_RXN_IDS_TUNABLE = ec_rxn_ids_tunable - _WORKER_EVOTUNE_DATA = evotune_data + _WORKER_EXPERIMENT_DATA = experiment_data _WORKER_EXCARBON = excarbon _WORKER_BIO_RXN = bio_rxn_id _WORKER_MAKE_ANAEROBIC = make_anaerobic @@ -965,7 +965,7 @@ def _score_worker(kcat_vec: np.ndarray) -> tuple[float, float]: assert _WORKER_MODEL is not None, "_score_worker called before _init_worker" return _score_kcat_vector( _WORKER_MODEL, _WORKER_TUNABLE_IDX, _WORKER_EC_RXN_IDS_TUNABLE, - _WORKER_EVOTUNE_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN, kcat_vec, + _WORKER_EXPERIMENT_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN, kcat_vec, make_anaerobic=_WORKER_MAKE_ANAEROBIC, change_protein_biomass=_WORKER_CHANGE_PROTEIN_BIOMASS, max_growth_weight=_WORKER_MAX_GROWTH_WEIGHT, diff --git a/tests/test_evotune.py b/tests/test_evotune.py index 71f2fcab..6e3dbcde 100644 --- a/tests/test_evotune.py +++ b/tests/test_evotune.py @@ -27,7 +27,7 @@ select_tunable_mask, tune_prior_penalty_weight, ) -from geckopy.kcat_tuning.evotune.data import EvotuneData +from geckopy.experiments.data import ExperimentData from geckopy.kcat_tuning.evotune.tuning import EvotuneResult GROUPS = { @@ -117,8 +117,8 @@ def _build_toy(adapter: ModelAdapter) -> EcModel: return model -def _evotune_data() -> EvotuneData: - max_grate = FluxData( +def _experiment_data() -> ExperimentData: + max_growth = FluxData( conds=["glucose", "ethanol"], p_tot=np.array([np.nan, np.nan]), gr_rate=np.array([_TRUE_GROWTH, _TRUE_GROWTH]), @@ -128,7 +128,7 @@ def _evotune_data() -> EvotuneData: exch_mets=["glucose", "ethanol"], exch_rxn_ids=["EX_glc", "EX_eth"], ) - return EvotuneData(flux_data=None, max_grate=max_grate, zero_flux=[]) + return ExperimentData(flux_data=None, max_growth=max_growth, zero_flux=[]) def _build_tied_toy(adapter: ModelAdapter) -> EcModel: @@ -214,8 +214,8 @@ def _build_tied_toy(adapter: ModelAdapter) -> EcModel: return model -def _tied_evotune_data() -> EvotuneData: - max_grate = FluxData( +def _tied_experiment_data() -> ExperimentData: + max_growth = FluxData( conds=["glucose", "ethanol"], p_tot=np.array([np.nan, np.nan]), gr_rate=np.array([_TRUE_GROWTH, _TRUE_GROWTH]), @@ -225,16 +225,16 @@ def _tied_evotune_data() -> EvotuneData: exch_mets=["glucose", "ethanol"], exch_rxn_ids=["EX_glc", "EX_eth"], ) - return EvotuneData(flux_data=None, max_grate=max_grate, zero_flux=[]) + return ExperimentData(flux_data=None, max_growth=max_growth, zero_flux=[]) def test_screen_ranks_both_load_bearing_kcats(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() screen = screen_kcat_leverage( - model, adapter=adapter, evotune_data=evotune_data, n_proc=1, + model, adapter=adapter, experiment_data=experiment_data, n_proc=1, ) assert set(screen["rxn_id"]) == {"R_glc", "R_eth"} @@ -248,9 +248,9 @@ def test_screen_ranks_both_load_bearing_kcats(tmp_path): def test_select_tunable_mask_is_relative_and_monotone(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() screen = screen_kcat_leverage( - model, adapter=adapter, evotune_data=evotune_data, n_proc=1, + model, adapter=adapter, experiment_data=experiment_data, n_proc=1, ) tiny = select_tunable_mask(model, screen, target_impact_share=0.01) @@ -276,12 +276,12 @@ def test_select_tunable_mask_empty_screen_returns_empty_mask(tmp_path): def test_cmaes_tuning_moves_kcats_toward_truth(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=15, rmse_threshold=1e-6) before = model.ec.kcat.copy() result = cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, n_proc=1, seed=0, verbose=False, ) @@ -306,7 +306,7 @@ def test_cmaes_tuning_extends_the_source_of_changed_kcats(tmp_path): origins = ["brenda", "dlkcat"] result = cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=_evotune_data(), + model, adapter=adapter, params=params, experiment_data=_experiment_data(), n_proc=1, seed=0, verbose=False, ) @@ -318,7 +318,7 @@ def test_cmaes_tuning_extends_the_source_of_changed_kcats(tmp_path): first = list(model.ec.source) kcat_first = model.ec.kcat.copy() cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=_evotune_data(), + model, adapter=adapter, params=params, experiment_data=_experiment_data(), n_proc=1, seed=1, verbose=False, ) assert (model.ec.kcat != kcat_first).any() @@ -331,9 +331,9 @@ def test_cmaes_tuning_n_proc_matches_serial(tmp_path): def _run(n_proc): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() return cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, n_proc=n_proc, seed=0, popsize=6, verbose=False, ) @@ -351,19 +351,19 @@ def _run(n_proc): def test_tie_isozymes_forces_equal_values(tmp_path): adapter = _adapter(tmp_path) model = _build_tied_toy(adapter) - evotune_data = _tied_evotune_data() + experiment_data = _tied_experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=8, rmse_threshold=-1.0, tie_isozymes=True) screen = screen_kcat_leverage( - model, adapter=adapter, params=params, evotune_data=evotune_data, n_proc=1, + model, adapter=adapter, params=params, experiment_data=experiment_data, n_proc=1, ) # Two rows: the tied isozyme pair as one group, R_eth as another. assert len(screen) == 2 assert sorted(screen["n_isozymes"]) == [1, 2] result = cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, n_proc=1, seed=0, verbose=False, ) assert result.rxns == ["R_iso_EXP_1", "R_iso_EXP_2", "R_eth"] @@ -373,11 +373,11 @@ def test_tie_isozymes_forces_equal_values(tmp_path): def test_tie_isozymes_false_keeps_groups_separate(tmp_path): adapter = _adapter(tmp_path) model = _build_tied_toy(adapter) - evotune_data = _tied_evotune_data() + experiment_data = _tied_experiment_data() params = EvotuneParams(source_groups=GROUPS, tie_isozymes=False) screen = screen_kcat_leverage( - model, adapter=adapter, params=params, evotune_data=evotune_data, n_proc=1, + model, adapter=adapter, params=params, experiment_data=experiment_data, n_proc=1, ) assert len(screen) == 3 assert (screen["n_isozymes"] == 1).all() @@ -387,19 +387,19 @@ def test_no_tunable_kcats_raises(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) model.ec.kcat[:] = 0.0 - evotune_data = _evotune_data() + experiment_data = _experiment_data() with pytest.raises(ValueError, match="No tunable kcats"): - cmaes_kcat_tuning(model, adapter=adapter, evotune_data=evotune_data, n_proc=1) + cmaes_kcat_tuning(model, adapter=adapter, experiment_data=experiment_data, n_proc=1) def test_too_few_free_parameters_raises(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() mask = np.array([True, False]) with pytest.raises(ValueError, match="free parameter"): cmaes_kcat_tuning( - model, adapter=adapter, evotune_data=evotune_data, + model, adapter=adapter, experiment_data=experiment_data, tunable_mask=mask, n_proc=1, ) @@ -407,11 +407,11 @@ def test_too_few_free_parameters_raises(tmp_path): def test_tune_prior_penalty_weight_shape_and_fit_cost(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=6, rmse_threshold=-1.0) report = tune_prior_penalty_weight( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, candidates=(0.0, 1000.0), seeds=(0, 1), n_proc=1, verbose=False, ) @@ -430,13 +430,13 @@ def test_tune_prior_penalty_weight_shape_and_fit_cost(tmp_path): def test_tune_prior_penalty_weight_restores_model(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() before = model.ec.kcat.copy() source_before = list(model.ec.source) params = EvotuneParams(source_groups=GROUPS, max_generations=4, rmse_threshold=-1.0) tune_prior_penalty_weight( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, candidates=(0.0,), seeds=(0,), n_proc=1, verbose=False, ) @@ -449,11 +449,11 @@ def test_tune_prior_penalty_weight_restores_model(tmp_path): def test_tune_prior_penalty_weight_single_seed_gives_nan_reproducibility(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=4, rmse_threshold=-1.0) report = tune_prior_penalty_weight( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, candidates=(0.0,), seeds=(0,), n_proc=1, verbose=False, ) @@ -487,7 +487,7 @@ def test_screen_orders_rows_of_equal_rank_as_the_tie_groups(tmp_path, monkeypatc adapter = _adapter(tmp_path) model = _build_toy(adapter) screen = screen_kcat_leverage( - model, adapter=adapter, evotune_data=_evotune_data(), n_proc=1, + model, adapter=adapter, experiment_data=_experiment_data(), n_proc=1, ) assert list(screen["leverage"]) == [0.0, 0.0] assert list(screen["rxn_id"]) == list(model.ec.rxns) @@ -501,7 +501,7 @@ def test_cmaes_tuning_runs_all_generations_on_a_flat_objective(tmp_path, monkeyp model = _build_toy(adapter) params = EvotuneParams(source_groups=GROUPS, max_generations=6, rmse_threshold=-1.0) result = cmaes_kcat_tuning( - model, adapter=adapter, params=params, evotune_data=_evotune_data(), + model, adapter=adapter, params=params, experiment_data=_experiment_data(), n_proc=1, seed=0, verbose=False, ) assert result.n_generations == 6 @@ -514,7 +514,7 @@ def test_screen_warns_when_the_model_has_no_formulas(tmp_path): met.formula = None with pytest.warns(UserWarning, match="no metabolite formulas"): screen_kcat_leverage( - model, adapter=adapter, evotune_data=_evotune_data(), n_proc=1, + model, adapter=adapter, experiment_data=_experiment_data(), n_proc=1, ) @@ -526,7 +526,7 @@ def test_default_groups_label_kcats_by_group_name_through_the_tuning_context(tmp model = _build_toy(_adapter(tmp_path)) model.ec.source = ["combined (sd=0.10, n=2)", "ec_tree"] with caplog.at_level("WARNING"): - out = _tunable_context(model, EvotuneParams(), _evotune_data(), "biomass", None) + out = _tunable_context(model, EvotuneParams(), _experiment_data(), "biomass", None) groups, sigma0_log = out[3], out[4] assert list(groups) == ["combined", "ec_tree"] assert list(sigma0_log) == [0.4, 0.3] @@ -535,7 +535,7 @@ def test_default_groups_label_kcats_by_group_name_through_the_tuning_context(tmp model.ec.source = ["standard", "standard"] caplog.clear() with caplog.at_level("WARNING"): - out = _tunable_context(model, EvotuneParams(), _evotune_data(), "biomass", None) + out = _tunable_context(model, EvotuneParams(), _experiment_data(), "biomass", None) assert list(out[3]) == ["unlabelled", "unlabelled"] assert list(out[4]) == [0.5, 0.5] assert "2 of 2 tunable kcats match no evotune source group and get sigma0_log_default (0.5); origins: standard" in caplog.text diff --git a/tests/test_evotune_data.py b/tests/test_evotune_data.py deleted file mode 100644 index 94e1422e..00000000 --- a/tests/test_evotune_data.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Tests for kcat_tuning.evotune.data.""" -from pathlib import Path - -from geckopy import ModelAdapter -from geckopy.kcat_tuning.evotune import ( - EvotuneData, - load_evotune_data, -) - - -def _adapter(tmp_path: Path) -> ModelAdapter: - (tmp_path / "model_adapter.toml").write_text( - 'conv_gem = "dummy.xml"\n' - 'org_name = "test"\n' - ) - return ModelAdapter.from_folder(tmp_path) - - -_FLUX_DATA_TSV = ( - "Condition\tPtot\tgrRate\tglucose (r_1714)\tethanol (r_1761)\t" - "evotuneRMSEweight\tsource\n" - "glucose\tNaN\t0.36\t-13.33\t19.83\t1\tDLKcat\n" - "ethanol\tNaN\t0.12\tNaN\t-8.5\t1\tbrenda\n" -) - -_MAX_GROWTH_TSV = ( - "Condition\tPtot\tgrRate\tglucose (r_1714)\tfructose (r_1709)\t" - "evotuneRMSEweight\tsource\n" - "glucose\tNaN\t0.41\t-1000\tNaN\t1\tDLKcat\n" - "fructose\tNaN\t0.338\tNaN\t-1000\t1\tDLKcat\n" -) - -_ZERO_EXCH_TSV = "Rxns\nr_1542\nr_1545\nr_1546\n" - - -def test_load_evotune_data_all_files_present(tmp_path): - adapter = _adapter(tmp_path) - data_dir = tmp_path / "data" - data_dir.mkdir() - (data_dir / "evotuneFluxData.tsv").write_text(_FLUX_DATA_TSV) - (data_dir / "evotuneMaxGrowth.tsv").write_text(_MAX_GROWTH_TSV) - (data_dir / "evotuneZeroExch.tsv").write_text(_ZERO_EXCH_TSV) - - evotune_data = load_evotune_data(adapter) - - assert isinstance(evotune_data, EvotuneData) - - assert evotune_data.flux_data is not None - assert evotune_data.flux_data.conds == ["glucose", "ethanol"] - assert evotune_data.flux_data.exch_rxn_ids == ["r_1714", "r_1761"] - assert evotune_data.flux_data.source == ["DLKcat", "brenda"] - assert list(evotune_data.flux_data.evotune_rmse_weight) == [1.0, 1.0] - - assert evotune_data.max_grate is not None - assert evotune_data.max_grate.conds == ["glucose", "fructose"] - assert evotune_data.max_grate.exch_rxn_ids == ["r_1714", "r_1709"] - - assert evotune_data.zero_flux == ["r_1542", "r_1545", "r_1546"] - - -def test_load_evotune_data_all_files_missing(tmp_path): - adapter = _adapter(tmp_path) - - evotune_data = load_evotune_data(adapter) - - assert evotune_data.flux_data is None - assert evotune_data.max_grate is None - assert evotune_data.zero_flux == [] - - -def test_load_evotune_data_partial(tmp_path): - """Only flux_data present: max_grate stays None (not a bogus - partial struct), matching MATLAB's *intended* behaviour for a - missing optional file -- unlike the asymmetric-guard bug flagged - in the MATLAB REVIEW.md, both fields are treated identically.""" - adapter = _adapter(tmp_path) - data_dir = tmp_path / "data" - data_dir.mkdir() - (data_dir / "evotuneFluxData.tsv").write_text(_FLUX_DATA_TSV) - - evotune_data = load_evotune_data(adapter) - - assert evotune_data.flux_data is not None - assert evotune_data.max_grate is None - assert evotune_data.zero_flux == [] diff --git a/tests/test_evotune_simulate_distance.py b/tests/test_experiment_conditions.py similarity index 83% rename from tests/test_evotune_simulate_distance.py rename to tests/test_experiment_conditions.py index 08f21610..7707da5d 100644 --- a/tests/test_evotune_simulate_distance.py +++ b/tests/test_experiment_conditions.py @@ -1,4 +1,4 @@ -"""Tests for kcat_tuning.evotune.simulate / .distance. +"""Tests for experiments.conditions and experiments.distance. Styled like test_sensitivity_tuning.py's hand-built toy EcModel: a tiny two-carbon-source model with a known enzyme cap, so every @@ -13,16 +13,16 @@ from geckopy import EcModel, ModelAdapter from geckopy.databases.flux_data import FluxData -from geckopy.kcat_tuning.evotune.data import EvotuneData -from geckopy.kcat_tuning.evotune.distance import ( +from geckopy.experiments.data import ExperimentData +from geckopy.experiments.distance import ( BIOMASS_CARBON_EQUIV, - evotune_distance, + experiment_distance, compute_excarbon, dataset_rmse, ) -from geckopy.kcat_tuning.evotune.simulate import ( +from geckopy.experiments.conditions import ( ConditionSimResult, - simulate_evotune_dataset, + simulate_conditions, ) @@ -133,12 +133,12 @@ def _flux_data(*, ethanol_grrate: float) -> FluxData: ), exch_mets=["glucose", "ethanol"], exch_rxn_ids=["EX_glc", "EX_eth"], - evotune_rmse_weight=np.array([1.0, 1.0]), + weight=np.array([1.0, 1.0]), source=["test", "test"], ) -def _max_grate_data() -> FluxData: +def _max_growth_data() -> FluxData: """Max-growth dataset: uptake fully opened, so both conditions hit the same enzyme-capped growth of 36.""" return FluxData( @@ -157,7 +157,7 @@ def _max_grate_data() -> FluxData: # --------------------------------------------------------------------------- # -# simulate_evotune_dataset +# simulate_conditions # --------------------------------------------------------------------------- # def test_simulate_flux_data_enzyme_vs_carbon_limited(tmp_path): @@ -165,7 +165,7 @@ def test_simulate_flux_data_enzyme_vs_carbon_limited(tmp_path): model = _build_toy(adapter) flux_data = _flux_data(ethanol_grrate=20.0) - results = simulate_evotune_dataset( + results = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) @@ -192,18 +192,18 @@ def test_simulate_flux_data_enzyme_vs_carbon_limited(tmp_path): assert model.reactions.get_by_id("EX_eth").bounds == (-1000.0, 0.0) -def test_simulate_max_grate_opens_uptake_fully(tmp_path): +def test_simulate_max_growth_opens_uptake_fully(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - max_grate = _max_grate_data() + max_growth = _max_growth_data() - results = simulate_evotune_dataset( - model, max_grate, + results = simulate_conditions( + model, max_growth, constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", ) assert [r.growth for r in results] == pytest.approx([_ENZYME_CAP, _ENZYME_CAP]) - # max_grate rows carry no exchange-flux measurements to report back. + # max_growth rows carry no exchange-flux measurements to report back. assert results[0].exch_fluxes == {} assert results[0].block_fluxes == {} @@ -215,7 +215,7 @@ def test_simulate_unmatched_condition_name_raises(tmp_path): flux_data.conds = ["glucose", "propionate"] with pytest.raises(ValueError, match="propionate"): - simulate_evotune_dataset( + simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=[], bio_rxn_id="biomass", ) @@ -259,7 +259,7 @@ def test_compute_excarbon_zero_carbon_clamps_to_one(tmp_path): # --------------------------------------------------------------------------- # -# dataset_rmse / evotune_distance +# dataset_rmse / experiment_distance # --------------------------------------------------------------------------- # def test_dataset_rmse_measured_terms_match_only_byproduct_deviates(tmp_path): @@ -271,7 +271,7 @@ def test_dataset_rmse_measured_terms_match_only_byproduct_deviates(tmp_path): # nonzero term left is the unmeasured byproduct (assumed # zero-flux, but nonzero in the simulation). flux_data = _flux_data(ethanol_grrate=20.0) - sims = simulate_evotune_dataset( + sims = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) @@ -307,7 +307,7 @@ def test_dataset_rmse_growth_mismatch_is_hand_computable(tmp_path): # (18.0 vs the actual carbon-limited 20.0) -- a known, hand-provable # deviation on top of the byproduct term. flux_data = _flux_data(ethanol_grrate=18.0) - sims = simulate_evotune_dataset( + sims = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) @@ -340,15 +340,15 @@ def test_dataset_rmse_infeasible_condition_gets_penalty(tmp_path): assert rmse == pytest.approx(99.0) -def test_dataset_rmse_max_grate_scores_growth_only(): - max_grate = _max_grate_data() +def test_dataset_rmse_max_growth_scores_growth_only(): + max_growth = _max_growth_data() sims = [ ConditionSimResult(feasible=True, growth=_ENZYME_CAP), ConditionSimResult(feasible=True, growth=30.0), # deliberately off ] rmse, per_cond = dataset_rmse( - max_grate, sims, + max_growth, sims, constrain=False, excarbon={}, bio_rxn_id="biomass", ) @@ -358,59 +358,59 @@ def test_dataset_rmse_max_grate_scores_growth_only(): assert rmse == pytest.approx(np.mean([0.0, expected_second])) -def test_evotune_distance_combines_both_datasets(tmp_path): +def test_experiment_distance_combines_both_datasets(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) flux_data = _flux_data(ethanol_grrate=20.0) - max_grate = _max_grate_data() - evotune_data = EvotuneData(flux_data=flux_data, max_grate=max_grate, zero_flux=["EX_byp"]) + max_growth = _max_growth_data() + experiment_data = ExperimentData(flux_data=flux_data, max_growth=max_growth, zero_flux=["EX_byp"]) - flux_sims = simulate_evotune_dataset( + flux_sims = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) - max_grate_sims = simulate_evotune_dataset( - model, max_grate, + max_growth_sims = simulate_conditions( + model, max_growth, constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", ) excarbon = compute_excarbon( model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", ) - rmse, detail = evotune_distance( - evotune_data, - flux_sims=flux_sims, max_grate_sims=max_grate_sims, + rmse, detail = experiment_distance( + experiment_data, + flux_sims=flux_sims, max_growth_sims=max_growth_sims, excarbon=excarbon, bio_rxn_id="biomass", ) expected_flux_rmse, _ = dataset_rmse( flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id="biomass", ) - expected_max_grate_rmse, _ = dataset_rmse( - max_grate, max_grate_sims, constrain=False, excarbon=excarbon, + expected_max_growth_rmse, _ = dataset_rmse( + max_growth, max_growth_sims, constrain=False, excarbon=excarbon, bio_rxn_id="biomass", ) assert rmse == pytest.approx( - np.mean([expected_flux_rmse, expected_max_grate_rmse]) + np.mean([expected_flux_rmse, expected_max_growth_rmse]) ) - assert "flux_data" in detail and "max_grate" in detail + assert "flux_data" in detail and "max_growth" in detail -def test_evotune_distance_max_growth_weight_scales_the_max_growth_term(tmp_path): +def test_experiment_distance_max_growth_weight_scales_the_max_growth_term(tmp_path): """``max_growth_weight`` scales the max-growth term, so 2 makes the eight max-growth conditions count double against the 33 flux ones.""" adapter = _adapter(tmp_path) model = _build_toy(adapter) flux_data = _flux_data(ethanol_grrate=20.0) - max_grate = _max_grate_data() - evotune_data = EvotuneData(flux_data=flux_data, max_grate=max_grate, zero_flux=["EX_byp"]) + max_growth = _max_growth_data() + experiment_data = ExperimentData(flux_data=flux_data, max_growth=max_growth, zero_flux=["EX_byp"]) - flux_sims = simulate_evotune_dataset( + flux_sims = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) - max_grate_sims = simulate_evotune_dataset( - model, max_grate, + max_growth_sims = simulate_conditions( + model, max_growth, constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", ) excarbon = compute_excarbon( @@ -420,47 +420,47 @@ def test_evotune_distance_max_growth_weight_scales_the_max_growth_term(tmp_path) expected_flux_rmse, _ = dataset_rmse( flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id="biomass", ) - expected_max_grate_rmse, _ = dataset_rmse( - max_grate, max_grate_sims, constrain=False, excarbon=excarbon, + expected_max_growth_rmse, _ = dataset_rmse( + max_growth, max_growth_sims, constrain=False, excarbon=excarbon, bio_rxn_id="biomass", ) - rmse, _ = evotune_distance( - evotune_data, - flux_sims=flux_sims, max_grate_sims=max_grate_sims, + rmse, _ = experiment_distance( + experiment_data, + flux_sims=flux_sims, max_growth_sims=max_growth_sims, excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=2.0, ) assert rmse == pytest.approx( - (expected_flux_rmse + 2.0 * expected_max_grate_rmse) / 3.0 + (expected_flux_rmse + 2.0 * expected_max_growth_rmse) / 3.0 ) # 0.5 reproduces MATLAB's maxGrowthWeight = 2, which weights flux. - rmse_matlab, _ = evotune_distance( - evotune_data, - flux_sims=flux_sims, max_grate_sims=max_grate_sims, + rmse_matlab, _ = experiment_distance( + experiment_data, + flux_sims=flux_sims, max_growth_sims=max_growth_sims, excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=0.5, ) assert rmse_matlab == pytest.approx( - (2.0 * expected_flux_rmse + expected_max_grate_rmse) / 3.0 + (2.0 * expected_flux_rmse + expected_max_growth_rmse) / 3.0 ) # A lone dataset carries the whole score whatever the weight. - solo = EvotuneData(flux_data=flux_data, max_grate=None, zero_flux=["EX_byp"]) - rmse_solo, _ = evotune_distance( + solo = ExperimentData(flux_data=flux_data, max_growth=None, zero_flux=["EX_byp"]) + rmse_solo, _ = experiment_distance( solo, - flux_sims=flux_sims, max_grate_sims=None, + flux_sims=flux_sims, max_growth_sims=None, excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=2.0, ) assert rmse_solo == pytest.approx(expected_flux_rmse) -def test_evotune_distance_missing_dataset_contributes_nothing(tmp_path): +def test_experiment_distance_missing_dataset_contributes_nothing(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) flux_data = _flux_data(ethanol_grrate=20.0) - evotune_data = EvotuneData(flux_data=flux_data, max_grate=None, zero_flux=["EX_byp"]) + experiment_data = ExperimentData(flux_data=flux_data, max_growth=None, zero_flux=["EX_byp"]) - flux_sims = simulate_evotune_dataset( + flux_sims = simulate_conditions( model, flux_data, constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", ) @@ -468,9 +468,9 @@ def test_evotune_distance_missing_dataset_contributes_nothing(tmp_path): model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", ) - rmse, detail = evotune_distance( - evotune_data, - flux_sims=flux_sims, max_grate_sims=None, + rmse, detail = experiment_distance( + experiment_data, + flux_sims=flux_sims, max_growth_sims=None, excarbon=excarbon, bio_rxn_id="biomass", ) diff --git a/tests/test_experiment_data.py b/tests/test_experiment_data.py new file mode 100644 index 00000000..8b4c176b --- /dev/null +++ b/tests/test_experiment_data.py @@ -0,0 +1,85 @@ +"""Tests for experiments.data.""" +from pathlib import Path + +from geckopy import ModelAdapter +from geckopy.experiments import ( + ExperimentData, + load_experiment_data, +) + + +def _adapter(tmp_path: Path) -> ModelAdapter: + (tmp_path / "model_adapter.toml").write_text( + 'conv_gem = "dummy.xml"\n' + 'org_name = "test"\n' + ) + return ModelAdapter.from_folder(tmp_path) + + +_FLUX_DATA_TSV = ( + "Condition\tPtot\tgrRate\tglucose (r_1714)\tethanol (r_1761)\t" + "weight\tsource\n" + "glucose\tNaN\t0.36\t-13.33\t19.83\t1\tDLKcat\n" + "ethanol\tNaN\t0.12\tNaN\t-8.5\t1\tbrenda\n" +) + +_MAX_GROWTH_TSV = ( + "Condition\tPtot\tgrRate\tglucose (r_1714)\tfructose (r_1709)\t" + "weight\tsource\n" + "glucose\tNaN\t0.41\t-1000\tNaN\t1\tDLKcat\n" + "fructose\tNaN\t0.338\tNaN\t-1000\t1\tDLKcat\n" +) + +_ZERO_EXCH_TSV = "Rxns\nr_1542\nr_1545\nr_1546\n" + + +def test_load_experiment_data_all_files_present(tmp_path): + adapter = _adapter(tmp_path) + data_dir = tmp_path / "data" + data_dir.mkdir() + (data_dir / "fluxData.tsv").write_text(_FLUX_DATA_TSV) + (data_dir / "maxGrowth.tsv").write_text(_MAX_GROWTH_TSV) + (data_dir / "zeroFlux.tsv").write_text(_ZERO_EXCH_TSV) + + experiment_data = load_experiment_data(adapter) + + assert isinstance(experiment_data, ExperimentData) + + assert experiment_data.flux_data is not None + assert experiment_data.flux_data.conds == ["glucose", "ethanol"] + assert experiment_data.flux_data.exch_rxn_ids == ["r_1714", "r_1761"] + assert experiment_data.flux_data.source == ["DLKcat", "brenda"] + assert list(experiment_data.flux_data.weight) == [1.0, 1.0] + + assert experiment_data.max_growth is not None + assert experiment_data.max_growth.conds == ["glucose", "fructose"] + assert experiment_data.max_growth.exch_rxn_ids == ["r_1714", "r_1709"] + + assert experiment_data.zero_flux == ["r_1542", "r_1545", "r_1546"] + + +def test_load_experiment_data_all_files_missing(tmp_path): + adapter = _adapter(tmp_path) + + experiment_data = load_experiment_data(adapter) + + assert experiment_data.flux_data is None + assert experiment_data.max_growth is None + assert experiment_data.zero_flux == [] + + +def test_load_experiment_data_partial(tmp_path): + """Only flux_data present: max_growth stays None (not a bogus + partial struct), matching MATLAB's *intended* behaviour for a + missing optional file -- unlike the asymmetric-guard bug flagged + in the MATLAB REVIEW.md, both fields are treated identically.""" + adapter = _adapter(tmp_path) + data_dir = tmp_path / "data" + data_dir.mkdir() + (data_dir / "fluxData.tsv").write_text(_FLUX_DATA_TSV) + + experiment_data = load_experiment_data(adapter) + + assert experiment_data.flux_data is not None + assert experiment_data.max_growth is None + assert experiment_data.zero_flux == [] diff --git a/tests/test_load_flux_data.py b/tests/test_load_flux_data.py index 7c325417..d83d9093 100644 --- a/tests/test_load_flux_data.py +++ b/tests/test_load_flux_data.py @@ -57,6 +57,28 @@ def test_no_exchange_columns_raises(tmp_path): # Basic happy path # --------------------------------------------------------------------------- # +def test_old_weight_column_name_is_rejected(tmp_path): + f = tmp_path / "fluxData.tsv" + _write_tsv(f, [ + ["Condition", "Ptot", "grRate", "glucose (r_1714)", "evotuneRMSEweight"], + ["c1", "0.5", "0.4", "-10", "1"], + ]) + with pytest.raises(ValueError, match="now called 'weight'"): + load_flux_data(f) + + +def test_weight_column_is_read_and_removed_from_the_fluxes(tmp_path): + f = tmp_path / "fluxData.tsv" + _write_tsv(f, [ + ["Condition", "Ptot", "grRate", "glucose (r_1714)", "weight", "source"], + ["c1", "0.5", "0.4", "-10", "0", "x"], + ["c2", "0.5", "0.3", "-8", "2", "y"], + ]) + data = load_flux_data(f) + assert data.weight.tolist() == [0.0, 2.0] + assert data.exch_rxn_ids == ["r_1714"] + + def test_basic_single_condition(tmp_path): p = tmp_path / "fd.tsv" _write_tsv(p, [ @@ -159,21 +181,21 @@ def test_no_optional_columns_yields_none(tmp_path): ["c1", "0.5", "0.4", "-10.0"], ]) fd = load_flux_data(p) - assert fd.evotune_rmse_weight is None + assert fd.weight is None assert fd.source is None -def test_evotune_rmse_weight_column_loaded(tmp_path): +def test_weight_column_loaded(tmp_path): p = tmp_path / "fd.tsv" _write_tsv(p, [ ["condition", "Ptot", "grRate", - "glc (EX_glc)", "evotuneRMSEweight"], + "glc (EX_glc)", "weight"], ["c1", "0.5", "0.4", "-10.0", "1.5"], ["c2", "0.6", "0.5", "-8.5", "0.7"], ]) fd = load_flux_data(p) - assert fd.evotune_rmse_weight is not None - np.testing.assert_array_equal(fd.evotune_rmse_weight, [1.5, 0.7]) + assert fd.weight is not None + np.testing.assert_array_equal(fd.weight, [1.5, 0.7]) # Tuning-weight column dropped from exch_fluxes. assert fd.exch_fluxes.shape == (2, 1) assert fd.exch_rxn_ids == ["EX_glc"] @@ -195,12 +217,12 @@ def test_both_optional_columns(tmp_path): p = tmp_path / "fd.tsv" _write_tsv(p, [ ["condition", "Ptot", "grRate", - "glc (EX_glc)", "evotuneRMSEweight", "source"], + "glc (EX_glc)", "weight", "source"], ["c1", "0.5", "0.4", "-10.0", "1.5", "PMID:12345"], ]) fd = load_flux_data(p) - assert fd.evotune_rmse_weight is not None - np.testing.assert_array_equal(fd.evotune_rmse_weight, [1.5]) + assert fd.weight is not None + np.testing.assert_array_equal(fd.weight, [1.5]) assert fd.source == ["PMID:12345"] assert fd.exch_fluxes.shape == (1, 1) @@ -211,13 +233,13 @@ def test_optional_columns_in_arbitrary_position(tmp_path): p = tmp_path / "fd.tsv" _write_tsv(p, [ ["condition", "Ptot", "grRate", - "source", "glc (EX_glc)", "evotuneRMSEweight"], + "source", "glc (EX_glc)", "weight"], ["c1", "0.5", "0.4", "PMID:1", "-10.0", "1.5"], ]) fd = load_flux_data(p) assert fd.source == ["PMID:1"] - assert fd.evotune_rmse_weight is not None - np.testing.assert_array_equal(fd.evotune_rmse_weight, [1.5]) + assert fd.weight is not None + np.testing.assert_array_equal(fd.weight, [1.5]) np.testing.assert_array_equal(fd.exch_fluxes, [[-10.0]]) assert fd.exch_rxn_ids == ["EX_glc"] diff --git a/tests/test_mask_size.py b/tests/test_mask_size.py index a7f5b8a3..40e26097 100644 --- a/tests/test_mask_size.py +++ b/tests/test_mask_size.py @@ -15,7 +15,7 @@ from geckopy.adapter.params import EvotuneParams, SourceGroupRule from geckopy.databases.flux_data import FluxData from geckopy.ec_model.ec_data import EcData -from geckopy.kcat_tuning.evotune.data import EvotuneData +from geckopy.experiments.data import ExperimentData from geckopy.kcat_tuning.evotune.mask_size import ( MaskSizePoint, recommend_target_impact_share, @@ -111,8 +111,8 @@ def _build_toy(adapter: ModelAdapter) -> EcModel: return model -def _evotune_data() -> EvotuneData: - max_grate = FluxData( +def _experiment_data() -> ExperimentData: + max_growth = FluxData( conds=["glucose", "ethanol"], p_tot=np.array([np.nan, np.nan]), gr_rate=np.array([_TRUE_GROWTH, _TRUE_GROWTH]), @@ -120,7 +120,7 @@ def _evotune_data() -> EvotuneData: exch_mets=["glucose", "ethanol"], exch_rxn_ids=["EX_glc", "EX_eth"], ) - return EvotuneData(flux_data=None, max_grate=max_grate, zero_flux=[]) + return ExperimentData(flux_data=None, max_growth=max_growth, zero_flux=[]) # --------------------------------------------------------------------------- # @@ -131,11 +131,11 @@ def _evotune_data() -> EvotuneData: def test_sweep_reports_one_point_per_share_sorted(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=10, rmse_threshold=1e-6) points = sweep_tunable_mask_size( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, target_impact_shares=[1.0, 0.7, 0.9], seeds=[0], n_proc=1, verbose=False, ) @@ -150,12 +150,12 @@ def test_sweep_reports_one_point_per_share_sorted(tmp_path): def test_sweep_does_not_mutate_input_model(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=10, rmse_threshold=1e-6) before = model.ec.kcat.copy() sweep_tunable_mask_size( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, target_impact_shares=[1.0], seeds=[0], n_proc=1, verbose=False, ) @@ -165,11 +165,11 @@ def test_sweep_does_not_mutate_input_model(tmp_path): def test_sweep_runs_every_seed_at_every_share(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=10, rmse_threshold=1e-6) points = sweep_tunable_mask_size( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, target_impact_shares=[1.0], seeds=[0, 1, 2], n_proc=1, verbose=False, ) @@ -185,10 +185,10 @@ def test_sweep_runs_every_seed_at_every_share(tmp_path): def test_sweep_reuses_a_given_screen(tmp_path): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=10, rmse_threshold=1e-6) screen = screen_kcat_leverage( - model, adapter=adapter, evotune_data=evotune_data, n_proc=1, + model, adapter=adapter, experiment_data=experiment_data, n_proc=1, ) # select_tunable_mask reads cum_leverage_share directly; dropping it @@ -198,7 +198,7 @@ def test_sweep_reuses_a_given_screen(tmp_path): stripped = screen.drop(columns=["cum_leverage_share"]) with pytest.raises(KeyError): sweep_tunable_mask_size( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, screen=stripped, target_impact_shares=[1.0], seeds=[0], n_proc=1, verbose=False, ) @@ -304,7 +304,7 @@ def test_sweep_uses_rmse_trace_minimum_not_last_entry(tmp_path, monkeypatch): adapter = _adapter(tmp_path) model = _build_toy(adapter) - evotune_data = _evotune_data() + experiment_data = _experiment_data() params = EvotuneParams(source_groups=GROUPS, max_generations=10, rmse_threshold=1e-6) # A later generation can improve the (prior-penalised) objective @@ -326,7 +326,7 @@ def fake_cmaes_kcat_tuning(run_model, **kwargs): monkeypatch.setattr(mask_size_module, "cmaes_kcat_tuning", fake_cmaes_kcat_tuning) points = sweep_tunable_mask_size( - model, adapter=adapter, params=params, evotune_data=evotune_data, + model, adapter=adapter, params=params, experiment_data=experiment_data, target_impact_shares=[1.0], seeds=[0], n_proc=1, verbose=False, ) diff --git a/tutorials/full_ecModel/data/evotuneFluxData.tsv b/tutorials/full_ecModel/data/evotuneFluxData.tsv deleted file mode 100644 index 7364bea2..00000000 --- a/tutorials/full_ecModel/data/evotuneFluxData.tsv +++ /dev/null @@ -1,34 +0,0 @@ -Condition Ptot grRate glucose (r_1714) fructose (r_1709) sucrose (r_2058) xylose (r_1718) CO2 (r_1672) oxygen (r_1992) pyruvate (r_2033) ethyl acetate (r_1765) glycerol (r_1808) acetate (r_1634) ethanol (r_1761) succinate (r_2056) evotuneRMSEweight source -glucose NaN 0.36 -13.33333333 0 0 0 NaN NaN 0 0 0.38 NaN 19.82608696 NaN 1 DLKcat -glucose NaN 0.39 -15.44444444 0 0 0 NaN NaN 0 0 0.37 NaN 22.4068323 NaN 1 DLKcat -glucose NaN 0.37 -19.55555556 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat -glucose NaN 0.37 -14.8 0 0 0 NaN NaN 0 0 1.1 0.4 22 NaN 1 DLKcat -glucose NaN 0.38 -15.05555556 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat -glucose NaN 0.35 -17.66666667 0 0 0 NaN NaN 0 0 0.34 NaN 23.51778656 NaN 1 DLKcat -glucose NaN 0.37 -11.2 0 0 0 19.58 NaN 0 0 0.12 0.43 15.74 NaN 1 DLKcat -glucose NaN 0.37 -13.66666667 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat -glucose NaN 0.38 -17.55555556 0 0 0 NaN NaN 0 0 0.41 NaN 28.22463768 NaN 1 DLKcat -glucose NaN 0.32 -25.38888889 0 0 0 NaN NaN 0 0 0.43 NaN 42.73291925 NaN 1 DLKcat -fructose NaN 0.1 0 -6.888888889 0 0 NaN 0 0 0 0.41 NaN 11.14130435 NaN 1 DLKcat -fructose NaN 0.25 0 -12.44444444 0 0 NaN 0 0 0 0.41 NaN 20.256917 NaN 1 DLKcat -sucrose NaN 0.32 0 0 -6.076541046 0 NaN 0 0 0 NaN NaN 19.94202899 NaN 1 DLKcat -glucose NaN 0.025 -0.3 0 0 0 0.75198 -0.73102 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.05 -0.6 0 0 0 1.3697 -1.3251 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.1 -1.1 0 0 0 2.4007 -2.3167 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.15 -1.7 0 0 0 3.6379 -3.5066 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.2 -2.3 0 0 0 4.8751 -4.6966 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.25 -2.8 0 0 0 5.906 -5.6881 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.28 -3.4 0 0 0 7.0413 -6.6773 0 0 0 0.08 0.11 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.3 -4.5 0 0 0 8.5667 -6.012 0 0 0 0.41 2.3 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.35 -8.6 0 0 0 15.2091 -5.3809 0 0 0 0.62 9.5 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.4 -11.1 0 0 0 19.3207 -5.0171 0 0 0 0.6 13.9 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 -glucose NaN 0.4 -20.2 0 0 0 34.4 NaN 0 0 0 1 30 NaN 1 DLKcat -glucose NaN 0.41 -14.22222222 0 0 0 NaN NaN 0 0 0 NaN 25.625 NaN 1 DLKcat -glucose NaN 0.1 -6.44 0 0 0 NaN 0 0 0 1.08 0 9.26 NaN 1 doi:10.1186/1752-0509-2-60 -glucose NaN 0.2449 -10.173 0 0 0 19.109 NaN 0 0 1.2908 0.212 15.494 NaN 1 doi:10.1002/bit.25447 -glucose NaN 0.2287 -13.492 0 0 0 27.31 0 0 0 2.9849 0.328 20.636 NaN 1 doi:10.1002/bit.25447 -glucose NaN 0.1 -6.0184 0 0 0 9.8220288 0 0.0120368 0 1.0351648 0.0240736 8.9734344 0.0270828 1 doi:10.1099/00221287-143-1-203 -glucose NaN 0.2 -11.5927 0 0 0 18.9888426 0 0.0463708 0 2.1098714 0.0695562 17.2499376 0.05216715 1 doi:10.1099/00221287-143-1-203 -glucose NaN 0.3 -17.0704 0 0 0 27.3467808 0 0.1024224 0 3.243376 0.2048448 25.2983328 0.0512112 1 doi:10.1099/00221287-143-1-203 -glucose NaN 0.4 -22.5483 0 0 0 35.3106378 0 0.1803864 0 4.9155294 0.450966 33.6195153 0.0676449 1 doi:10.1099/00221287-143-1-203 -glucose 0.46 0.1 -1.0709285 0 0 0 2.627555 -2.446473 NaN NaN 0.0008575 0 0 NaN 1 doi:10.1016/j.cels.2017.03.003 diff --git a/tutorials/full_ecModel/data/fluxData.tsv b/tutorials/full_ecModel/data/fluxData.tsv index fd31424d..cae5f8fb 100644 --- a/tutorials/full_ecModel/data/fluxData.tsv +++ b/tutorials/full_ecModel/data/fluxData.tsv @@ -1,2 +1,35 @@ -Condition Ptot grRate glucose (r_1714) CO2 (r_1672) oxygen (r_1992) pyruvate (r_2033) succinate (r_2056) glycerol (r_1808) acetate (r_1634) ethanol (r_1761) -Std 0.4228 0.1 -1.1 2.7 -2.5 8.06E-05 1.00E-05 0.001893379 0.089483471 1.00E-05 +Condition Ptot grRate glucose (r_1714) fructose (r_1709) sucrose (r_2058) xylose (r_1718) CO2 (r_1672) oxygen (r_1992) pyruvate (r_2033) ethyl acetate (r_1765) glycerol (r_1808) acetate (r_1634) ethanol (r_1761) succinate (r_2056) weight source +Std 0.4228 0.1 -1.1 NaN NaN NaN 2.7 -2.5 8.06E-05 NaN 0.001893379 0.089483471 1.00E-05 1.00E-05 0 Standard condition of the protein-constraint step; not used for tuning +glucose NaN 0.36 -13.33333333 0 0 0 NaN NaN 0 0 0.38 NaN 19.82608696 NaN 1 DLKcat +glucose NaN 0.39 -15.44444444 0 0 0 NaN NaN 0 0 0.37 NaN 22.4068323 NaN 1 DLKcat +glucose NaN 0.37 -19.55555556 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat +glucose NaN 0.37 -14.8 0 0 0 NaN NaN 0 0 1.1 0.4 22 NaN 1 DLKcat +glucose NaN 0.38 -15.05555556 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat +glucose NaN 0.35 -17.66666667 0 0 0 NaN NaN 0 0 0.34 NaN 23.51778656 NaN 1 DLKcat +glucose NaN 0.37 -11.2 0 0 0 19.58 NaN 0 0 0.12 0.43 15.74 NaN 1 DLKcat +glucose NaN 0.37 -13.66666667 0 0 0 NaN NaN 0 0 NaN NaN NaN NaN 1 DLKcat +glucose NaN 0.38 -17.55555556 0 0 0 NaN NaN 0 0 0.41 NaN 28.22463768 NaN 1 DLKcat +glucose NaN 0.32 -25.38888889 0 0 0 NaN NaN 0 0 0.43 NaN 42.73291925 NaN 1 DLKcat +fructose NaN 0.1 0 -6.888888889 0 0 NaN 0 0 0 0.41 NaN 11.14130435 NaN 1 DLKcat +fructose NaN 0.25 0 -12.44444444 0 0 NaN 0 0 0 0.41 NaN 20.256917 NaN 1 DLKcat +sucrose NaN 0.32 0 0 -6.076541046 0 NaN 0 0 0 NaN NaN 19.94202899 NaN 1 DLKcat +glucose NaN 0.025 -0.3 0 0 0 0.75198 -0.73102 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.05 -0.6 0 0 0 1.3697 -1.3251 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.1 -1.1 0 0 0 2.4007 -2.3167 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.15 -1.7 0 0 0 3.6379 -3.5066 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.2 -2.3 0 0 0 4.8751 -4.6966 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.25 -2.8 0 0 0 5.906 -5.6881 0 0 0 0 0 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.28 -3.4 0 0 0 7.0413 -6.6773 0 0 0 0.08 0.11 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.3 -4.5 0 0 0 8.5667 -6.012 0 0 0 0.41 2.3 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.35 -8.6 0 0 0 15.2091 -5.3809 0 0 0 0.62 9.5 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.4 -11.1 0 0 0 19.3207 -5.0171 0 0 0 0.6 13.9 NaN 1 doi:10.1128/AEM.64.11.4226-4233.1998 +glucose NaN 0.4 -20.2 0 0 0 34.4 NaN 0 0 0 1 30 NaN 1 DLKcat +glucose NaN 0.41 -14.22222222 0 0 0 NaN NaN 0 0 0 NaN 25.625 NaN 1 DLKcat +glucose NaN 0.1 -6.44 0 0 0 NaN 0 0 0 1.08 0 9.26 NaN 1 doi:10.1186/1752-0509-2-60 +glucose NaN 0.2449 -10.173 0 0 0 19.109 NaN 0 0 1.2908 0.212 15.494 NaN 1 doi:10.1002/bit.25447 +glucose NaN 0.2287 -13.492 0 0 0 27.31 0 0 0 2.9849 0.328 20.636 NaN 1 doi:10.1002/bit.25447 +glucose NaN 0.1 -6.0184 0 0 0 9.8220288 0 0.0120368 0 1.0351648 0.0240736 8.9734344 0.0270828 1 doi:10.1099/00221287-143-1-203 +glucose NaN 0.2 -11.5927 0 0 0 18.9888426 0 0.0463708 0 2.1098714 0.0695562 17.2499376 0.05216715 1 doi:10.1099/00221287-143-1-203 +glucose NaN 0.3 -17.0704 0 0 0 27.3467808 0 0.1024224 0 3.243376 0.2048448 25.2983328 0.0512112 1 doi:10.1099/00221287-143-1-203 +glucose NaN 0.4 -22.5483 0 0 0 35.3106378 0 0.1803864 0 4.9155294 0.450966 33.6195153 0.0676449 1 doi:10.1099/00221287-143-1-203 +glucose 0.46 0.1 -1.0709285 0 0 0 2.627555 -2.446473 NaN NaN 0.0008575 0 0 NaN 1 doi:10.1016/j.cels.2017.03.003 diff --git a/tutorials/full_ecModel/data/evotuneMaxGrowth.tsv b/tutorials/full_ecModel/data/maxGrowth.tsv similarity index 89% rename from tutorials/full_ecModel/data/evotuneMaxGrowth.tsv rename to tutorials/full_ecModel/data/maxGrowth.tsv index 9f67bd45..8cdb5fa8 100644 --- a/tutorials/full_ecModel/data/evotuneMaxGrowth.tsv +++ b/tutorials/full_ecModel/data/maxGrowth.tsv @@ -1,4 +1,4 @@ -Condition Ptot grRate glucose (r_1714) fructose (r_1709) sucrose (r_2058) acetate (r_1634) ethanol (r_1761) galactose (r_1710) maltose (r_1931) sucrose (r_2058) mannose (r_1715) evotuneRMSEweight source +Condition Ptot grRate glucose (r_1714) fructose (r_1709) sucrose (r_2058) acetate (r_1634) ethanol (r_1761) galactose (r_1710) maltose (r_1931) sucrose (r_2058) mannose (r_1715) weight source fructose NaN 0.338 NaN -1000 NaN NaN NaN NaN NaN NaN NaN 1 DLKcat glucose NaN 0.41 -1000 NaN NaN NaN NaN NaN NaN NaN NaN 1 DLKcat ethanol NaN 0.12 NaN NaN NaN NaN -1000 NaN NaN NaN NaN 1 DLKcat diff --git a/tutorials/full_ecModel/data/evotuneZeroExch.tsv b/tutorials/full_ecModel/data/zeroFlux.tsv similarity index 100% rename from tutorials/full_ecModel/data/evotuneZeroExch.tsv rename to tutorials/full_ecModel/data/zeroFlux.tsv From 28008de4f27e6a5806ba67623374863e846ebf1f Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Fri, 20 Nov 2026 21:53:45 +0100 Subject: [PATCH 2/5] refactor: take the experiment data and the condition scoring from raven-toolbox FluxData, load_flux_data, load_experiment_data, simulate_conditions, experiment_distance and compute_carbon_weights are in raven_toolbox.experiments, and apply_flux_data_constraints calls the function of the same name there and adds the biomass reaction and the carbon source of the model adapter. load_experiment_data takes the data folder instead of the adapter. --- CHANGELOG.md | 2 + src/geckopy/databases/__init__.py | 2 +- src/geckopy/databases/flux_data.py | 259 ------- src/geckopy/databases/mw.py | 122 ---- src/geckopy/databases/pubchem.py | 249 ------- src/geckopy/experiments/__init__.py | 30 - src/geckopy/experiments/conditions.py | 204 ------ src/geckopy/experiments/data.py | 94 --- src/geckopy/experiments/distance.py | 252 ------- src/geckopy/kcat_tuning/evotune/mask_size.py | 2 +- src/geckopy/kcat_tuning/evotune/tuning.py | 56 +- .../limit_proteins/constrain_flux_data.py | 152 +--- src/geckopy/solver_support.py | 674 ------------------ tests/test_constrain_flux_data.py | 394 ++-------- tests/test_constrain_from_flux_file.py | 67 ++ tests/test_evotune.py | 3 +- tests/test_experiment_conditions.py | 481 ------------- tests/test_experiment_data.py | 85 --- tests/test_load_flux_data.py | 339 --------- tests/test_mask_size.py | 3 +- tests/test_mw.py | 169 ----- tests/test_optimize_model.py | 62 -- tests/test_pubchem.py | 389 ---------- 23 files changed, 181 insertions(+), 3909 deletions(-) delete mode 100644 src/geckopy/databases/flux_data.py delete mode 100644 src/geckopy/databases/mw.py delete mode 100644 src/geckopy/databases/pubchem.py delete mode 100644 src/geckopy/experiments/__init__.py delete mode 100644 src/geckopy/experiments/conditions.py delete mode 100644 src/geckopy/experiments/data.py delete mode 100644 src/geckopy/experiments/distance.py delete mode 100644 src/geckopy/solver_support.py create mode 100644 tests/test_constrain_from_flux_file.py delete mode 100644 tests/test_experiment_conditions.py delete mode 100644 tests/test_experiment_data.py delete mode 100644 tests/test_load_flux_data.py delete mode 100644 tests/test_mw.py delete mode 100644 tests/test_optimize_model.py delete mode 100644 tests/test_pubchem.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8146cc57..bcdc6693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* The experimental data (`load_experiment_data`, `ExperimentData`, `FluxData`, `load_flux_data`), the simulation of a condition (`simulate_conditions`) and its distance to the measurements (`experiment_distance`, `compute_carbon_weights`) are in `raven_toolbox.experiments`; geckopy imports them and `geckopy.databases` still exports `FluxData` and `load_flux_data`. `load_experiment_data` takes the data folder instead of the adapter. The evotune functions take `experiment_data` (was `evotune_data`) and `max_growth` (was `max_grate`), `FluxData.weight` is the per-condition weight (was `evotune_rmse_weight`) and the file column is `weight` (was `evotuneRMSEweight`, which now raises an error naming the new column). The files are `fluxData.tsv`, `maxGrowth.tsv` and `zeroFlux.tsv` (were `evotuneFluxData.tsv`, `evotuneMaxGrowth.tsv` and `evotuneZeroExch.tsv`); in the tutorial `fluxData.tsv` holds the condition of the protein step as its first row, with weight 0. +* `apply_flux_data_constraints` takes the constraint logic from `raven_toolbox.experiments.apply_flux_data_constraints` and keeps the adapter defaults for `bio_rxn` and `c_source`. * `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. diff --git a/src/geckopy/databases/__init__.py b/src/geckopy/databases/__init__.py index 2b08f9d1..e6a97123 100644 --- a/src/geckopy/databases/__init__.py +++ b/src/geckopy/databases/__init__.py @@ -8,7 +8,7 @@ load_complex_portal_json, ) from .dlkcat_ignore_lists import DLKcatIgnoreLists, load_dlkcat_ignore_lists -from .flux_data import FluxData, load_flux_data +from raven_toolbox.experiments import FluxData, load_flux_data from .kegg_download import download_kegg from .kegg_loader import KeggDB, load_kegg_tsv from .mw import calculate_mw diff --git a/src/geckopy/databases/flux_data.py b/src/geckopy/databases/flux_data.py deleted file mode 100644 index 7000cb57..00000000 --- a/src/geckopy/databases/flux_data.py +++ /dev/null @@ -1,259 +0,0 @@ -"""FluxData container for proteomics-paired flux measurements, -plus a ``load_flux_data`` parser for the GECKO `fluxData.tsv` format. -""" -from __future__ import annotations - -import logging -from dataclasses import dataclass, field -from pathlib import Path -from typing import Optional - -import numpy as np - -logger = logging.getLogger(__name__) - - -# ` ()` -> capture met_name and rxn_id. - - -@dataclass -class FluxData: - """Per-condition flux measurements with per-reaction exchange data. - - Attributes - ---------- - conds - Names of the experimental conditions / samples. - p_tot - Total protein content per condition, g/gDCW. Shape - ``(n_conds,)``. - gr_rate - Measured growth rate per condition, 1/h. Shape ``(n_conds,)``. - exch_fluxes - Exchange flux per (condition, reaction), mmol/gDCW/h. Shape - ``(n_conds, n_rxns)``. NaN means "no measurement"; ``+/-1000`` - means unconstrained (used by - ``apply_flux_data_constraints``). - exch_mets - Metabolite names matching the columns of ``exch_fluxes``. - exch_rxn_ids - Exchange reaction IDs in the model, matching the columns of - ``exch_fluxes`` and ``exch_mets``. - weight - Optional per-condition weights of the kcat-tuning - RMSE term (0 leaves the condition out). ``None`` if the source file did not have the - ``weight`` column. - source - Optional per-condition free-text describing where the data - came from. ``None`` if the source file did not have a - ``source`` column. - """ - - conds: list[str] = field(default_factory=list) - p_tot: np.ndarray = field( - default_factory=lambda: np.empty(0, dtype=float) - ) - gr_rate: np.ndarray = field( - default_factory=lambda: np.empty(0, dtype=float) - ) - exch_fluxes: np.ndarray = field( - default_factory=lambda: np.empty((0, 0), dtype=float) - ) - exch_mets: list[str] = field(default_factory=list) - exch_rxn_ids: list[str] = field(default_factory=list) - weight: Optional[np.ndarray] = None - source: Optional[list[str]] = None - - -def load_flux_data(path: str | Path) -> FluxData: - """Parse a GECKO ``fluxData.tsv`` into a ``FluxData``. - - File format (tab-delimited, one header row): - - condition Ptot grRate () () ... - c1 0.5 0.4 -10.0 5.0 ... - c2 0.6 0.5 -8.5 4.2 ... - - Optional extra columns (in any position): - - * ``weight`` (float): weight of the condition in the kcat tuning. - * ``source`` (str): free-text describing the data origin. - - An exchange column header has the form `` ()``, split at - the parenthesis that matches the final closing one, so a reaction id - may contain parentheses (``X (EX_a(e))`` gives ``X`` and - ``EX_a(e)``). - - Both columns are dropped from the matrix and surfaced on the - returned ``FluxData`` as ``weight`` and ``source``. - - Ported from GECKO MATLAB: - src/io/loadFluxData.m. - - Parameters - ---------- - path - Path to the TSV file. - - Returns - ------- - FluxData - - Raises - ------ - FileNotFoundError - If ``path`` does not exist. - ValueError - If the file has no header row, no exchange columns, or a row - with a different number of cells than the header. - """ - path = Path(path) - if not path.is_file(): - raise FileNotFoundError(f"fluxData file not found: {path}") - - rows: list[list[str]] = [] - for line in path.read_text(encoding="utf-8").splitlines(): - if not line.strip(): - continue - rows.append(line.rstrip("\n").split("\t")) - - if not rows: - raise ValueError(f"{path} is empty.") - if len(rows[0]) < 3: - raise ValueError( - f"{path}: header has only {len(rows[0])} column(s); " - f"expected at least 'condition Ptot grRate'." - ) - - header = rows[0] - data = rows[1:] - for line_no, r in enumerate(data, start=2): - if len(r) != len(header): - raise ValueError( - f"{path}: row {line_no} has {len(r)} cell(s), the header " - f"has {len(header)}." - ) - - # Pull out optional columns first; collect indices to drop. - optional_indices: list[int] = [] - if _index_of(header, "evotuneRMSEweight") is not None: - raise ValueError( - f"{path}: the column 'evotuneRMSEweight' is now called 'weight'." - ) - tuning_idx = _index_of(header, "weight") - source_idx = _index_of(header, "source") - - tuning_weights: Optional[np.ndarray] = None - source_values: Optional[list[str]] = None - - if tuning_idx is not None: - tuning_weights = np.array( - [_parse_float(r[tuning_idx]) for r in data], - dtype=float, - ) - optional_indices.append(tuning_idx) - if source_idx is not None: - source_values = [r[source_idx] for r in data] - optional_indices.append(source_idx) - - # Build the kept-column index list (preserving order). - kept = [j for j in range(len(header)) if j not in optional_indices] - kept_header = [header[j] for j in kept] - - # First three columns are condition / Ptot / grRate. Remaining are exchanges. - if len(kept_header) < 4: - raise ValueError( - f"{path}: no exchange-flux columns found after the first three." - ) - - exch_headers = kept_header[3:] - exch_mets, exch_rxn_ids = _split_exch_headers(exch_headers) - - conds = [r[kept[0]] for r in data] - p_tot = np.array([_parse_float(r[kept[1]]) for r in data], dtype=float) - gr_rate = np.array([_parse_float(r[kept[2]]) for r in data], dtype=float) - exch_fluxes = np.full((len(data), len(exch_headers)), np.nan, dtype=float) - for i, r in enumerate(data): - for ci, j in enumerate(kept[3:]): - exch_fluxes[i, ci] = _parse_float(r[j]) - - return FluxData( - conds=conds, - p_tot=p_tot, - gr_rate=gr_rate, - exch_fluxes=exch_fluxes, - exch_mets=exch_mets, - exch_rxn_ids=exch_rxn_ids, - weight=tuning_weights, - source=source_values, - ) - - -def _index_of(header: list[str], name: str) -> Optional[int]: - """Return the index of `name` in `header`, or None if absent.""" - for j, h in enumerate(header): - if h.strip() == name: - return j - return None - - -def _parse_float(s: str) -> float: - """Parse a numeric cell; empty / non-numeric -> NaN.""" - s = s.strip() - if not s: - return float("nan") - try: - return float(s) - except ValueError: - return float("nan") - - -def _split_header(header: str) -> Optional[tuple[str, str]]: - """Split ``' ()'`` at the parenthesis that matches the final - closing one; return None for a header of another form.""" - if not header.endswith(")"): - return None - depth = 0 - for k in range(len(header) - 1, -1, -1): - if header[k] == ")": - depth += 1 - elif header[k] == "(": - depth -= 1 - if depth == 0: - rxn = header[k + 1:-1].strip() - if not rxn: - return None - return header[:k].strip(), rxn - return None - - -def _split_exch_headers(headers: list[str]) -> tuple[list[str], list[str]]: - """Split each header `' ()'` into (met, rxn) pairs. - - Headers without parentheses pass through with the rxn_id equal - to the full header (matching MATLAB's regex behaviour: the - capture-group falls through unchanged). - """ - mets: list[str] = [] - rxns: list[str] = [] - no_paren: list[str] = [] - for h in headers: - parts = _split_header(h.strip()) - if parts is not None: - mets.append(parts[0]) - rxns.append(parts[1]) - else: - mets.append(h.strip()) - rxns.append(h.strip()) - no_paren.append(h.strip()) - if no_paren: - # The reaction id is taken from the header, so a column without the - # ` ()` form will be matched against a reaction whose id is - # the metabolite name -- almost certainly wrong. Surface it. - logger.warning( - "load_flux_data: %d exchange column header(s) lack the " - "' ()' form; their reaction id falls back to the full " - "header text: %s", - len(no_paren), no_paren[:5], - ) - return mets, rxns diff --git a/src/geckopy/databases/mw.py b/src/geckopy/databases/mw.py deleted file mode 100644 index 55035a2d..00000000 --- a/src/geckopy/databases/mw.py +++ /dev/null @@ -1,122 +0,0 @@ -"""Compute molecular weight of a protein from its amino acid sequence. - -Ported from GECKO MATLAB: -src/get_enzyme_data/calculateMW.m. -""" -from __future__ import annotations - -import logging - -logger = logging.getLogger(__name__) - - -# Average mass of water (g/mol). Added once to account for the two -# H atoms and one OH that remain after peptide bond condensation. -# Matches GECKO MATLAB's calculateMW.m exactly. -_WATER_MASS = 18.01528 - - -# Average residue masses (g/mol). Taken from GECKO MATLAB -# `calculateMW.m`. Standard amino acids agree with widely-cited -# references (e.g., ExPASy ProtParam). Non-standard codes: -# - B = average(D, N). -# - J = same as L and I, which both happen to equal 113.16. -# - O (pyrrolysine) = 255.31. -# - U (selenocysteine) = 150.04. -# - Z = average(E, Q). -# -# X (any amino acid): the unweighted mean of the 20 standard residues -# above, 118.885 -- matches GECKO MATLAB's calculateMW.m exactly, which -# computes the same live mean of its own table rather than a fixed -# constant. -_STANDARD_RESIDUE_MASSES: dict[str, float] = { - "A": 71.08, - "C": 103.14, - "D": 115.09, - "E": 129.11, - "F": 147.17, - "G": 57.05, - "H": 137.14, - "I": 113.16, - "K": 128.17, - "L": 113.16, - "M": 131.20, - "N": 114.10, - "P": 97.12, - "Q": 128.13, - "R": 156.19, - "S": 87.08, - "T": 101.10, - "V": 99.13, - "W": 186.21, - "Y": 163.17, -} - -_X_MASS = sum(_STANDARD_RESIDUE_MASSES.values()) / len(_STANDARD_RESIDUE_MASSES) - -_RESIDUE_MASSES: dict[str, float] = { - **_STANDARD_RESIDUE_MASSES, - "B": (115.09 + 114.10) / 2, # D-or-N - "J": 113.16, # L-or-I (both 113.16) - "O": 255.31, # pyrrolysine - "U": 150.04, # selenocysteine - "X": _X_MASS, # any standard residue - "Z": (129.11 + 128.13) / 2, # E-or-Q -} - - -def calculate_mw(sequence: str) -> float: - """Compute the molecular weight of a protein from its sequence. - - Ported from GECKO MATLAB: - 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 - one un-eliminated water relative to N residues + N water masses). - - Recognized amino acid codes (the sequence is uppercased first, so - matching is case-insensitive): - - ACDEFGHIKLMNPQRSTVWY - the 20 standard residues - BJOZUX - non-standard / ambiguous codes - - Whitespace and digits are skipped silently. Any other unrecognized - character is also skipped, but triggers one warning per call - listing every distinct offending character. - - Parameters - ---------- - sequence - Protein sequence as a string. May contain whitespace, digits, - and other non-letter characters; those are ignored. - - Returns - ------- - float - Molecular weight in g/mol (Da). Returns ``nan`` for an - empty/all-skipped sequence (no residues), so a missing sequence - is not mistaken for an 18 Da "protein". - """ - upper = sequence.upper() if sequence else "" - mw = _WATER_MASS - n_residues = 0 - - unknown_chars: set[str] = set() - for ch in upper: - if ch in _RESIDUE_MASSES: - mw += _RESIDUE_MASSES[ch] - n_residues += 1 - elif not ch.isspace() and not ch.isdigit(): - unknown_chars.add(ch) - - if unknown_chars: - logger.warning( - "calculate_mw: skipped %d unknown character(s): %s", - len(unknown_chars), - ", ".join(repr(c) for c in sorted(unknown_chars)), - ) - - if n_residues == 0: - return float("nan") - return mw diff --git a/src/geckopy/databases/pubchem.py b/src/geckopy/databases/pubchem.py deleted file mode 100644 index 32688f0b..00000000 --- a/src/geckopy/databases/pubchem.py +++ /dev/null @@ -1,249 +0,0 @@ -"""SMILES lookup via PubChem with a local TSV cache. - -Ported from GECKO MATLAB: -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 -resume gracefully. -""" -from __future__ import annotations - -import logging -import re -import time -from pathlib import Path -from typing import TYPE_CHECKING, Optional -from urllib.parse import quote - -import requests -from requests.adapters import HTTPAdapter -from urllib3.util.retry import Retry - -if TYPE_CHECKING: - import cobra - - -logger = logging.getLogger(__name__) - -# TODO: progress reporting (tqdm) deferred per design discussion. - -# PubChem deprecated the `CanonicalSMILES` property in 2025 in favour of -# `SMILES` (its canonical/absolute SMILES); `ConnectivitySMILES` replaces -# the old `IsomericSMILES`. -_PUBCHEM_URL = ( - "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" - "{name}/property/SMILES/TXT" -) -_TIMEOUT_SECONDS = 30 -_NUM_RETRIES = 10 -_INTER_REQUEST_SLEEP = 0.05 # seconds; PubChem allows ~5 req/s -_PROT_PATTERN = re.compile(r"^prot_") -_FIRST_LINE_PATTERN = re.compile(r"^(\S*)\n") - - -def met_smiles(metabolite) -> str: - """The metabolite's SMILES, whatever shape its annotation is in. - - cobra stores an annotation value as the object it was assigned, and - returns a *list* for a model read back from a file: a metabolite - annotated in memory by :func:`find_met_smiles` has ``"CCO"``, - and the same metabolite after it is saved and loaded has - ``["CCO"]``. Readers that assume a string get a list that compares - equal to nothing and is not hashable. - - Returns the first non-empty entry, or ``""`` when there is none. - """ - value = metabolite.annotation.get("smiles", "") - if isinstance(value, (list, tuple, set)): - return next((str(v) for v in value if v), "") - return str(value) if value else "" - - -def find_met_smiles( - model: "cobra.Model", - *, - cache_path: Optional[Path] = None, -) -> None: - """Populate ``metabolite.annotation['smiles']`` from PubChem. - - Ported from GECKO MATLAB: - src/change_model/findMetSmiles.m. - - For every metabolite in the model: - - 1. Skip if the name starts with ``prot_`` (enzyme pseudometabolite). - 2. Skip, with a warning, if the metabolite has no name. - 3. Skip if ``annotation['smiles']`` is already set. - 4. Look up the metabolite name in the local TSV cache. When the cache - holds a name more than once, the last row is used. - 5. If still not found, query PubChem; cache the result. - - The same metabolite name may appear on many ``cobra.Metabolite`` - instances (different compartments). Lookup is done once per unique - name and the SMILES is applied to every metabolite sharing that name. - - Parameters - ---------- - model - A cobra.Model. Mutated in place: each metabolite gains - ``annotation['smiles']`` when a SMILES is found. - cache_path - Path to the SMILES cache TSV. If None, defaults to - ``model.adapter.params.path / "data" / "smilesDB.tsv"`` if the - model is an EcModel with adapter; otherwise raises ValueError. - The file is created if missing and appended to as new SMILES - are downloaded. - """ - if cache_path is None: - adapter = getattr(model, "adapter", None) - if adapter is None: - raise ValueError( - "cache_path is required when model has no adapter." - ) - cache_path = adapter.params.path / "data" / "smilesDB.tsv" - cache_path = Path(cache_path) - cache_path.parent.mkdir(parents=True, exist_ok=True) - - # Build map: metabolite name -> list of metabolite objects sharing it. - name_to_mets: dict[str, list] = {} - unnamed = [] - for met in model.metabolites: - name = met.name - if not name: - unnamed.append(met.id) - continue - if _PROT_PATTERN.match(name): - continue - if met.annotation.get("smiles"): - continue - name_to_mets.setdefault(name, []).append(met) - - if unnamed: - logger.warning( - "find_met_smiles: %d metabolite(s) have no name and get no SMILES: %s", - len(unnamed), ", ".join(unnamed), - ) - - if not name_to_mets: - logger.info("find_met_smiles: nothing to look up.") - return - - # Load existing cache. - cache = _load_cache(cache_path) - logger.info( - "find_met_smiles: %d unique metabolite name(s) need SMILES; " - "%d already in cache.", - len(name_to_mets), - sum(1 for n in name_to_mets if n in cache), - ) - - # Apply cached SMILES first (no network). - for name in list(name_to_mets): - if name in cache: - smiles = cache[name] - for met in name_to_mets[name]: - if smiles: - met.annotation["smiles"] = smiles - del name_to_mets[name] - - if not name_to_mets: - logger.info("find_met_smiles: all matches resolved from cache.") - return - - # Hit PubChem for the rest. - session = _make_session() - n_found = 0 - n_missing = 0 - - for name in sorted(name_to_mets): - smiles = _fetch_one_smiles(session, name) - # Append to cache immediately, including empty results so we - # do not retry next time. - _append_cache(cache_path, name, smiles) - - if smiles: - for met in name_to_mets[name]: - met.annotation["smiles"] = smiles - n_found += 1 - else: - n_missing += 1 - logger.debug("No SMILES for: %s", name) - - time.sleep(_INTER_REQUEST_SLEEP) - - total = n_found + n_missing - if total > 0: - pct = 100.0 * n_found / total - logger.info( - "find_met_smiles: SMILES found for %d/%d unique names (%.0f%%); " - "%d missing.", - n_found, total, pct, n_missing, - ) - - -def _load_cache(path: Path) -> dict[str, str]: - """Load the two-column TSV cache. Returns an empty dict if missing.""" - cache: dict[str, str] = {} - if not path.is_file(): - return cache - with open(path, "r", encoding="utf-8") as f: - for line in f: - parts = line.rstrip("\n").split("\t") - if len(parts) >= 2: - cache[parts[0]] = parts[1] - elif len(parts) == 1 and parts[0]: - cache[parts[0]] = "" - return cache - - -def _append_cache(path: Path, name: str, smiles: str) -> None: - """Append one row to the TSV cache. Open and close per call so - interrupted runs leave a consistent file.""" - with open(path, "a", encoding="utf-8") as f: - f.write(f"{name}\t{smiles}\n") - - -def _make_session() -> requests.Session: - """A requests.Session with retry/backoff for transient errors.""" - session = requests.Session() - retry = Retry( - total=_NUM_RETRIES, - backoff_factor=0.5, - status_forcelist=(429, 500, 502, 503, 504), - allowed_methods=("GET",), - ) - adapter = HTTPAdapter(max_retries=retry) - session.mount("https://", adapter) - session.mount("http://", adapter) - return session - - -def _fetch_one_smiles(session: requests.Session, name: str) -> str: - """Fetch SMILES from PubChem for one metabolite name. Returns - empty string if unfound or unrecoverable error.""" - url = _PUBCHEM_URL.format(name=quote(name, safe="")) - try: - response = session.get(url, timeout=_TIMEOUT_SECONDS) - except requests.RequestException as e: - logger.warning("PubChem request failed for %r: %s", name, e) - return "" - - if response.status_code in (400, 404, 500): - return "" - if response.status_code != 200: - logger.warning( - "PubChem returned HTTP %d for %r; treating as no match.", - response.status_code, name, - ) - return "" - - text = response.text - if not text: - return "" - - # Multiple SMILES may come on separate lines; take only the first. - match = _FIRST_LINE_PATTERN.match(text) - if match: - return match.group(1).strip() - return text.split("\n", 1)[0].strip() diff --git a/src/geckopy/experiments/__init__.py b/src/geckopy/experiments/__init__.py deleted file mode 100644 index c4be65e3..00000000 --- a/src/geckopy/experiments/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Experimental data on growth and fluxes, and how a model is scored against it. - -Several functions compare a model with measured conditions: the kcat tuning -of :mod:`geckopy.kcat_tuning` and the flux constraints of -:mod:`geckopy.limit_proteins`. They share the files that hold the -measurements (:func:`load_experiment_data`), the setup of one condition in -the model and its simulation (:func:`simulate_conditions`) and the distance -between simulated and measured values (:func:`experiment_distance`). -""" -from .conditions import ConditionSimResult, simulate_conditions -from .data import ExperimentData, load_experiment_data -from .distance import ( - BIOMASS_CARBON_EQUIV, - INFEASIBLE_PENALTY, - compute_excarbon, - dataset_rmse, - experiment_distance, -) - -__all__ = [ - "BIOMASS_CARBON_EQUIV", - "INFEASIBLE_PENALTY", - "ConditionSimResult", - "ExperimentData", - "compute_excarbon", - "dataset_rmse", - "experiment_distance", - "load_experiment_data", - "simulate_conditions", -] diff --git a/src/geckopy/experiments/conditions.py b/src/geckopy/experiments/conditions.py deleted file mode 100644 index d38adba2..00000000 --- a/src/geckopy/experiments/conditions.py +++ /dev/null @@ -1,204 +0,0 @@ -"""Simulate the measured conditions in a model. - -This module only does the FBA half of the distance: given an already kcat-constrained -``EcModel`` and one :class:`~geckopy.databases.flux_data.FluxData` -dataset (either ``experiment_data.flux_data`` or ``experiment_data.max_growth``), -simulate every condition and report growth + exchange fluxes. -:mod:`.distance` turns those raw numbers into an RMSE against the -matching experimental values -- kept separate so the RMSE math is -testable with hand-built inputs and no ``EcModel``/FBA at all. -""" -from __future__ import annotations - -from dataclasses import dataclass, field -from typing import TYPE_CHECKING, Callable, Optional - -import numpy as np -from ..solver_support import optimize_model - -if TYPE_CHECKING: - from ..databases.flux_data import FluxData - from ..ec_model.ec_model import EcModel - - -@dataclass -class ConditionSimResult: - """One condition's simulated outcome. - - ``feasible=False`` (LP infeasible/unbounded, or no biomass flux) - means ``growth``/``exch_fluxes``/``block_fluxes`` are meaningless; - :mod:`.distance` applies the fixed infeasibility penalty for that - row instead of reading them. - """ - - feasible: bool - growth: float = float("nan") - exch_fluxes: dict[str, float] = field(default_factory=dict) - block_fluxes: dict[str, float] = field(default_factory=dict) - - -def simulate_conditions( - model: "EcModel", - flux_data: "FluxData", - *, - constrain: bool, - zero_flux_rxns: list[str], - bio_rxn_id: str, - make_anaerobic: Optional[Callable[["EcModel"], None]] = None, - change_protein_biomass: Optional[Callable[["EcModel", float], None]] = None, -) -> list[ConditionSimResult]: - """Simulate every condition in one tuning dataset. - - The steps are: - - 1. Every exchange reaction that appears as a *condition name* - anywhere in ``flux_data.conds`` (matched by name against - ``flux_data.exch_mets``) is blocked (``lower_bound = 0``) up - front, across the board -- not just the reaction for the row - being solved -- so one row's carbon source can't leak flux into - another row's simulation. - 2. For row ``i``, that row's own carbon-source reaction is - unblocked: fixed at the measured uptake rate if ``constrain`` - is True (``experiment_data.flux_data``), or fully opened to ``-1000`` - if ``constrain`` is False (``experiment_data.max_growth`` -- "what's - the *best possible* growth on this carbon source", not "growth - at this measured rate"). - 3. Optional per-condition adjustments run if the corresponding - callable is supplied: anaerobic switch when the oxygen exchange - column reads exactly 0, protein-content rescaling when - ``Ptot`` is given and nonzero. - 4. Solve; record growth, plus (``constrain`` datasets only, since - ``max_growth`` has no exchange measurements to compare against) - every non-NaN measured exchange's simulated flux and every - ``zero_flux_rxns`` entry's simulated flux. - - All mutation happens inside ``with model:``, which cobra reverts - on exit -- only bounds are touched here, so this is safe to call - repeatedly against one persistent (per-worker) model instance; a - per-particle ``EcModel.copy()`` is not used (prohibitively - expensive on a real-scale model). - - Parameters - ---------- - model - EcModel with the current particle's kcats already applied - (via ``apply_kcat_constraints``). - flux_data - The dataset to simulate. - constrain - True for flux data (fix uptake at the measured rate), False - for max-growth data (open uptake fully). - zero_flux_rxns - Reaction IDs assumed zero-flux in every condition - (``experiment_data.zero_flux``); the row's own carbon source is - skipped even if it's also listed here. - bio_rxn_id - The biomass reaction ID (``adapter.params.bio_rxn``). - make_anaerobic, change_protein_biomass - 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 - ------- - list[ConditionSimResult] - One entry per row of ``flux_data.conds``, in order. - - Raises - ------ - ValueError - If any ``flux_data.conds`` entry can't be matched by name to - an exchange metabolite in ``flux_data.exch_mets``. - """ - cond_to_exch_idx = _match_conditions_to_exch(flux_data) - all_condition_rxn_ids = sorted( - {flux_data.exch_rxn_ids[j] for j in set(cond_to_exch_idx)} - ) - o2_col = _find_col(flux_data.exch_mets, "oxygen") - measured_cols = [ - j for j in range(len(flux_data.exch_rxn_ids)) - ] if constrain else [] - - results: list[ConditionSimResult] = [] - for i, exch_idx in enumerate(cond_to_exch_idx): - carbon_rxn_id = flux_data.exch_rxn_ids[exch_idx] - with model: - for rxn_id in all_condition_rxn_ids: - model.reactions.get_by_id(rxn_id).lower_bound = 0.0 - - carbon_rxn = model.reactions.get_by_id(carbon_rxn_id) - if constrain: - carbon_rxn.lower_bound = float(flux_data.exch_fluxes[i, exch_idx]) - else: - carbon_rxn.lower_bound = -1000.0 - - if o2_col is not None and flux_data.exch_fluxes[i, o2_col] == 0.0: - if make_anaerobic is not None: - make_anaerobic(model) - - ptot = float(flux_data.p_tot[i]) - if not (np.isnan(ptot) or ptot == 0.0): - if change_protein_biomass is not None: - change_protein_biomass(model, ptot) - - sol = optimize_model(model) - if ( - sol.status != "optimal" - or sol.objective_value is None - or np.isnan(sol.objective_value) - ): - results.append(ConditionSimResult(feasible=False)) - continue - - growth = float(sol.fluxes.get(bio_rxn_id, 0.0)) - exch_fluxes: dict[str, float] = {} - block_fluxes: dict[str, float] = {} - if constrain: - for j in measured_cols: - if np.isnan(flux_data.exch_fluxes[i, j]): - continue - rxn_id = flux_data.exch_rxn_ids[j] - exch_fluxes[rxn_id] = float(sol.fluxes.get(rxn_id, 0.0)) - for rxn_id in zero_flux_rxns: - if rxn_id == carbon_rxn_id: - continue - block_fluxes[rxn_id] = float(sol.fluxes.get(rxn_id, 0.0)) - results.append( - ConditionSimResult( - feasible=True, - growth=growth, - exch_fluxes=exch_fluxes, - block_fluxes=block_fluxes, - ) - ) - return results - - -def _match_conditions_to_exch(flux_data: "FluxData") -> list[int]: - """Column index into ``exch_mets``/``exch_rxn_ids`` for each - condition, matched by name. Mirrors MATLAB's - ``ismember(data.conds, data.exchMets)`` plus its "cannot be - matched" error.""" - met_to_idx: dict[str, int] = {} - for j, met in enumerate(flux_data.exch_mets): - met_to_idx.setdefault(met, j) - missing = sorted({c for c in flux_data.conds if c not in met_to_idx}) - if missing: - raise ValueError( - 'Carbon source(s) "' - + "; ".join(missing) - + '" in the provided fluxData or maxGrowth cannot be matched ' - "by name with an exchange reaction." - ) - return [met_to_idx[c] for c in flux_data.conds] - - -def _find_col(exch_mets: list[str], name: str) -> Optional[int]: - try: - return exch_mets.index(name) - except ValueError: - return None diff --git a/src/geckopy/experiments/data.py b/src/geckopy/experiments/data.py deleted file mode 100644 index 9240050b..00000000 --- a/src/geckopy/experiments/data.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Load the measured growth rates and fluxes of the conditions. - -The flux and max-growth files, ``fluxData.tsv`` and ``maxGrowth.tsv``, -share the column layout -``Condition Ptot grRate () ... weight source``, so both use -:func:`~geckopy.databases.flux_data.load_flux_data`; only ``zeroFlux.tsv`` -(a single ``Rxns`` id column) needs a loader of its own. The functions that -constrain a model with one condition read the same ``fluxData.tsv``; the -kcat tuning uses every condition in it, where a ``weight`` of 0 leaves a -condition out of the tuning. - -All three files are optional: a missing file yields ``None`` (or an empty -list for ``zero_flux``), never an exception. -""" -from __future__ import annotations - -from dataclasses import dataclass, field -from pathlib import Path -from typing import TYPE_CHECKING, Optional - -from ..databases.flux_data import FluxData, load_flux_data - -if TYPE_CHECKING: - from ..adapter import ModelAdapter - -_FLUX_DATA_FILENAME = "fluxData.tsv" -_MAX_GROWTH_FILENAME = "maxGrowth.tsv" -_ZERO_EXCH_FILENAME = "zeroFlux.tsv" - - -@dataclass -class ExperimentData: - """Measured conditions, as the tuning functions use them. - - Attributes - ---------- - flux_data - Per-condition growth/exchange-flux measurements, from - ``fluxData.tsv``. ``None`` if that file isn't present. - max_growth - Per-condition maximum-growth-rate measurements (one active - carbon source per condition, at ``-1000`` i.e. unconstrained), - from ``maxGrowth.tsv``. ``None`` if that file isn't - present. - zero_flux - Reaction IDs assumed to carry zero flux, from - ``zeroFlux.tsv``. Empty if that file isn't present. - """ - - flux_data: Optional[FluxData] = None - max_growth: Optional[FluxData] = None - zero_flux: list[str] = field(default_factory=list) - - -def load_experiment_data(adapter: "ModelAdapter") -> ExperimentData: - """Load the three experimental data files. - - Each file is optional and resolved as - ``/data/``; a missing file yields - ``None`` (``flux_data``/``max_growth``) or an empty list - (``zero_flux``) rather than raising. - - Parameters - ---------- - adapter - A ``ModelAdapter`` whose ``params.path`` locates the ``data/`` - folder. - - Returns - ------- - ExperimentData - """ - data_dir = Path(adapter.params.path) / "data" - return ExperimentData( - flux_data=_load_optional_flux_data(data_dir / _FLUX_DATA_FILENAME), - max_growth=_load_optional_flux_data(data_dir / _MAX_GROWTH_FILENAME), - zero_flux=_load_zero_exch(data_dir / _ZERO_EXCH_FILENAME), - ) - - -def _load_optional_flux_data(path: Path) -> Optional[FluxData]: - if not path.is_file(): - return None - return load_flux_data(path) - - -def _load_zero_exch(path: Path) -> list[str]: - """Parse ``zeroFlux.tsv``: a single ``Rxns`` header, then - one reaction ID per line.""" - if not path.is_file(): - return [] - lines = path.read_text(encoding="utf-8").splitlines() - rxn_ids = [line.strip() for line in lines[1:] if line.strip()] - return rxn_ids diff --git a/src/geckopy/experiments/distance.py b/src/geckopy/experiments/distance.py deleted file mode 100644 index fa390183..00000000 --- a/src/geckopy/experiments/distance.py +++ /dev/null @@ -1,252 +0,0 @@ -"""Carbon- and condition-weighted RMSE distance between a model and the data. - -:mod:`.conditions` does the FBA half; this module is pure numpy (plus one -small model-reading helper for the carbon weights) so the RMSE math is -directly testable against hand-computed expected values, no -``EcModel``/FBA involved. - -The biomass reaction is passed explicitly as ``bio_rxn_id`` -(``adapter.params.bio_rxn``) rather than matched by a literal name, and -:func:`dataset_rmse` builds its reaction-id-to-column lookup once. - -Every zero-carbon exchange metabolite (O2, water, ...) is clamped up to -a carbon weight of 1, not 0, so those reactions' flux mismatches still -count toward the RMSE at unit weight rather than being zeroed out of it. -See :func:`compute_excarbon`. -""" -from __future__ import annotations - -import warnings -from typing import TYPE_CHECKING, Iterable - -import numpy as np - -from .conditions import ConditionSimResult - -if TYPE_CHECKING: - from ..ec_model.ec_model import EcModel - from .data import ExperimentData - from ..databases.flux_data import FluxData - -#: MATLAB's hardcoded assumption of the biomass reaction's carbon -#: content, in Cmmol/gDCW, used to put growth-rate deviations on the -#: same carbon-weighted scale as exchange-flux deviations. -BIOMASS_CARBON_EQUIV = 41.0 - -#: Fixed penalty substituted for an infeasible/failed condition's RMSE -#: ("clearly worse than any real RMSE" -- MATLAB never documents why -#: 99 specifically, just that it must dominate the mean). -INFEASIBLE_PENALTY = 99.0 - - -def compute_excarbon( - model: "EcModel", rxn_ids: Iterable[str], bio_rxn_id: str, -) -> dict[str, float]: - """Per-reaction carbon-weight lookup for RMSE weighting. - - The biomass reaction gets :data:`BIOMASS_CARBON_EQUIV`. Every - other reaction gets the carbon count of its single exchanged - metabolite (via cobrapy's parsed ``Metabolite.elements``, not a - hand-rolled formula parser). A missing/unparseable formula, or a - computed carbon count of exactly 0 (e.g. O2, water), both fall - back to 1 -- matching MATLAB's ``Ematrix(isnan(Ematrix)) = 1`` and - ``excarbon(excarbon == 0) = 1`` respectively. A model in which no - metabolite has a formula gives every exchange the weight 1 and - emits a warning. - - Parameters - ---------- - model - Model providing the metabolite formulas. - rxn_ids - Reaction IDs to compute weights for (typically every - reaction referenced by a tuning dataset: its exchange - columns, its zero-flux list, and the biomass reaction). - bio_rxn_id - The biomass reaction ID. - - Returns - ------- - dict[str, float] - """ - excarbon: dict[str, float] = {} - if not any(m.formula for m in model.metabolites): - warnings.warn( - "The model has no metabolite formulas; every exchange " - "reaction gets the carbon weight 1.", - UserWarning, stacklevel=2, - ) - for rxn_id in rxn_ids: - if rxn_id == bio_rxn_id: - excarbon[rxn_id] = BIOMASS_CARBON_EQUIV - continue - rxn = model.reactions.get_by_id(rxn_id) - mets = list(rxn.metabolites) - carbon = mets[0].elements.get("C") if len(mets) == 1 else None - value = float(carbon) if carbon is not None else 1.0 - excarbon[rxn_id] = value if value != 0.0 else 1.0 - return excarbon - - -def dataset_rmse( - flux_data: "FluxData", - sims: list[ConditionSimResult], - *, - constrain: bool, - excarbon: dict[str, float], - bio_rxn_id: str, - penalty: float = INFEASIBLE_PENALTY, -) -> tuple[float, np.ndarray]: - """RMSE (mean, per-condition array) for one tuning dataset. - - Parameters - ---------- - flux_data - The dataset being scored (``experiment_data.flux_data`` or - ``experiment_data.max_growth``). - sims - One :class:`~.conditions.ConditionSimResult` per - ``flux_data.conds`` row, e.g. from - :func:`~.conditions.simulate_conditions`. - constrain - Must match the value passed to - :func:`~.conditions.simulate_conditions` for these - ``sims``: True includes carbon-weighted exchange-flux terms - (flux data), False scores growth alone (max-growth data). - excarbon - Carbon-weight lookup from :func:`compute_excarbon`, covering - every reaction referenced by ``sims``' ``exch_fluxes`` / - ``block_fluxes`` plus ``bio_rxn_id``. A reaction missing from - the lookup falls back to a weight of 1. - bio_rxn_id - The biomass reaction ID (for reading ``flux_data.gr_rate``'s - carbon-equivalent counterpart). - penalty - RMSE substituted for an infeasible condition. - - Returns - ------- - rmse : float - Weighted (by ``flux_data.weight``, if present) - mean of the per-condition RMSEs. - rmse_list : numpy.ndarray - The per-condition RMSEs (post-penalty, pre-weighting), shape - ``(len(flux_data.conds),)``. - """ - n = len(flux_data.conds) - if len(sims) != n: - raise ValueError( - f"sims has {len(sims)} entries; expected {n} to match " - f"flux_data.conds." - ) - rxn_to_col = {r: j for j, r in enumerate(flux_data.exch_rxn_ids)} - - rmse_list = np.empty(n, dtype=float) - for i, sim in enumerate(sims): - if not sim.feasible: - rmse_list[i] = penalty - continue - - bio_meas = float(flux_data.gr_rate[i]) * BIOMASS_CARBON_EQUIV - bio_sim = sim.growth * BIOMASS_CARBON_EQUIV - - if not constrain: - rmse_list[i] = abs(bio_meas - bio_sim) - continue - - measured = [bio_meas] - simulated = [bio_sim] - for rxn_id, sim_flux in sim.exch_fluxes.items(): - w = excarbon.get(rxn_id, 1.0) - measured.append(w * float(flux_data.exch_fluxes[i, rxn_to_col[rxn_id]])) - simulated.append(w * sim_flux) - for rxn_id, sim_flux in sim.block_fluxes.items(): - w = excarbon.get(rxn_id, 1.0) - weighted_sim = w * sim_flux - if weighted_sim == 0.0: - # Already-correct zero predictions don't dilute the RMSE - # (MATLAB: `blockSim(blockSim == 0) = []`). - continue - measured.append(0.0) - simulated.append(weighted_sim) - - diff = np.asarray(measured, dtype=float) - np.asarray(simulated, dtype=float) - rmse_list[i] = float(np.sqrt(np.mean(diff ** 2))) - - weighted = rmse_list - if flux_data.weight is not None: - weighted = rmse_list * flux_data.weight - return float(np.mean(weighted)), rmse_list - - -def experiment_distance( - experiment_data: "ExperimentData", - *, - flux_sims: list[ConditionSimResult] | None, - max_growth_sims: list[ConditionSimResult] | None, - excarbon: dict[str, float], - bio_rxn_id: str, - penalty: float = INFEASIBLE_PENALTY, - max_growth_weight: float = 1.0, -) -> tuple[float, dict[str, np.ndarray]]: - """Combine both datasets' RMSE into one score. - - ``flux_sims`` must be given (non-``None``) iff ``experiment_data.flux_data`` - is not ``None``, and likewise for ``max_growth_sims`` / - ``experiment_data.max_growth``. A dataset absent from ``experiment_data`` doesn't - contribute to the combined RMSE (MATLAB: a missing dataset's - ``rmse_*`` stays ``[]`` and drops out of ``validIdx``). - - Parameters - ---------- - max_growth_weight - Relative weight of the max-growth dataset against the flux - dataset, giving ``(rmse_flux + w * rmse_max_growth) / (w + 1)``. - At 2 the eight max-growth conditions have twice the weight of - the 33 flux conditions, which is the intended emphasis here: - the max-growth set is where the carbon-source diversity lives, - while 30 of the 33 flux conditions are glucose. - - - Returns - ------- - rmse : float - Weighted mean of whichever dataset RMSEs are present. ``NaN`` - if neither dataset is present. - detail : dict[str, numpy.ndarray] - Per-condition RMSE arrays, keyed ``"flux_data"`` / - ``"max_growth"`` for whichever datasets were scored. - """ - parts: list[float] = [] - weights: list[float] = [] - detail: dict[str, np.ndarray] = {} - - if experiment_data.flux_data is not None: - if flux_sims is None: - raise ValueError("experiment_data.flux_data is set but flux_sims is None.") - rmse, per_cond = dataset_rmse( - experiment_data.flux_data, flux_sims, - constrain=True, excarbon=excarbon, bio_rxn_id=bio_rxn_id, - penalty=penalty, - ) - parts.append(rmse) - weights.append(1.0) - detail["flux_data"] = per_cond - - if experiment_data.max_growth is not None: - if max_growth_sims is None: - raise ValueError("experiment_data.max_growth is set but max_growth_sims is None.") - rmse, per_cond = dataset_rmse( - experiment_data.max_growth, max_growth_sims, - constrain=False, excarbon=excarbon, bio_rxn_id=bio_rxn_id, - penalty=penalty, - ) - parts.append(rmse) - weights.append(float(max_growth_weight)) - detail["max_growth"] = per_cond - - if parts: - rmse = float(np.dot(parts, weights) / np.sum(weights)) - else: - rmse = float("nan") - return rmse, detail diff --git a/src/geckopy/kcat_tuning/evotune/mask_size.py b/src/geckopy/kcat_tuning/evotune/mask_size.py index 801109f0..0e83fd39 100644 --- a/src/geckopy/kcat_tuning/evotune/mask_size.py +++ b/src/geckopy/kcat_tuning/evotune/mask_size.py @@ -37,7 +37,7 @@ from ...adapter import ModelAdapter from ...adapter.params import EvotuneParams from ...ec_model.ec_model import EcModel - from ...experiments.data import ExperimentData + from raven_toolbox.experiments import ExperimentData @dataclass(frozen=True) diff --git a/src/geckopy/kcat_tuning/evotune/tuning.py b/src/geckopy/kcat_tuning/evotune/tuning.py index 6507bf5e..f42dd2f1 100644 --- a/src/geckopy/kcat_tuning/evotune/tuning.py +++ b/src/geckopy/kcat_tuning/evotune/tuning.py @@ -61,6 +61,7 @@ from contextlib import nullcontext from dataclasses import dataclass, field from itertools import combinations +from pathlib import Path from typing import TYPE_CHECKING, Callable, Optional, Sequence import cobra @@ -71,11 +72,16 @@ from ...ec_model.pipeline.apply_kcat import apply_kcat_constraints from ...gather_kcats.kcat_source_format import format_kcat_source, parse_kcat_source from ..corrections import Correction, corrections as build_corrections -from ...experiments.data import ExperimentData, load_experiment_data -from ...experiments.distance import experiment_distance, compute_excarbon +from raven_toolbox.experiments import ( + ExperimentData, + compute_carbon_weights, + experiment_distance, + load_experiment_data, + simulate_conditions, +) from .parsimony import fold_change, n_changed from .priors import build_sigma0_log, classify_kcat_sources, warn_unlabelled -from ...experiments.conditions import simulate_conditions +from ...solver_support import optimize_model from .tying import isozyme_tie_map with warnings.catch_warnings(): @@ -176,7 +182,7 @@ def _resolve_context(model, adapter, params, experiment_data, bio_rxn): raise ValueError( "experiment_data not given and model has no adapter to load it from." ) - experiment_data = load_experiment_data(adapter) + experiment_data = load_experiment_data(Path(adapter.params.path) / "data") if experiment_data.flux_data is None and experiment_data.max_growth is None: raise ValueError( "experiment_data has neither flux_data nor max_growth -- nothing to " @@ -197,7 +203,7 @@ def _tunable_context( model, params, experiment_data, bio_rxn, tunable_mask, ): """The tunable subset plus everything derived from it: tie groups, - trust weights, and the excarbon table. Shared by every function in + trust weights, and the carbon_weights table. Shared by every function in this module so a screen and the run it feeds are always looking at exactly the same parameters.""" is_tunable = model.ec.kcat > 0 @@ -223,19 +229,19 @@ def _tunable_context( warn_unlabelled(groups, sources, params) sigma0_log = build_sigma0_log(groups, params) - excarbon_rxn_ids: set[str] = {bio_rxn} + weighted_rxn_ids: set[str] = {bio_rxn} for data in (experiment_data.flux_data, experiment_data.max_growth): if data is not None: - excarbon_rxn_ids.update(data.exch_rxn_ids) - excarbon_rxn_ids.update(experiment_data.zero_flux) - excarbon = compute_excarbon(model, excarbon_rxn_ids, bio_rxn_id=bio_rxn) + weighted_rxn_ids.update(data.exch_rxn_ids) + weighted_rxn_ids.update(experiment_data.zero_flux) + carbon_weights = compute_carbon_weights(model, weighted_rxn_ids, bio_rxn_id=bio_rxn) tie_map = ( isozyme_tie_map(ec_rxn_ids_tunable, kcat0, list(groups)) if params.tie_isozymes else np.arange(len(kcat0)) ) return (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, - excarbon, tie_map) + carbon_weights, tie_map) def screen_kcat_leverage( @@ -292,7 +298,7 @@ def screen_kcat_leverage( """ params, experiment_data, bio_rxn = _resolve_context( model, adapter, params, experiment_data, bio_rxn) - (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, excarbon, + (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, carbon_weights, tie_map) = _tunable_context( model, params, experiment_data, bio_rxn, tunable_mask) @@ -316,7 +322,7 @@ def screen_kcat_leverage( if n_proc == 1: rmses = [ _score_kcat_vector( - model, tunable_idx, ec_rxn_ids_tunable, experiment_data, excarbon, + model, tunable_idx, ec_rxn_ids_tunable, experiment_data, carbon_weights, bio_rxn, v, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, max_growth_weight=params.max_growth_weight, @@ -327,7 +333,7 @@ def screen_kcat_leverage( with ProcessPool( n_proc, initializer=_init_worker, initargs=(model, tunable_idx, ec_rxn_ids_tunable, experiment_data, - excarbon, bio_rxn, make_anaerobic, change_protein_biomass, + carbon_weights, bio_rxn, make_anaerobic, change_protein_biomass, params.max_growth_weight, 0.0, None, None), ) as pool: chunk = max(1, len(vectors) // (n_proc * 4)) @@ -460,7 +466,7 @@ def cmaes_kcat_tuning( verbose Whether per-generation progress is logged at INFO. make_anaerobic, change_protein_biomass - Forwarded to ``simulate.simulate_conditions`` -- see its + Forwarded to ``raven_toolbox.experiments.simulate_conditions`` -- see its docstring; geckopy has no generic organism-agnostic implementation of these yet. See the module docstring's "Parallel scoring" section for a picklability caveat when @@ -497,7 +503,7 @@ def cmaes_kcat_tuning( tunable_mask = select_tunable_mask( model, screen, target_impact_share=target_impact_share) - (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, excarbon, + (tunable_idx, ec_rxn_ids_tunable, kcat0, groups, sigma0_log, carbon_weights, tie_map) = _tunable_context( model, params, experiment_data, bio_rxn, tunable_mask) @@ -543,7 +549,7 @@ def expand(xm: np.ndarray) -> np.ndarray: ProcessPool( n_proc, initializer=_init_worker, initargs=(model, tunable_idx, ec_rxn_ids_tunable, experiment_data, - excarbon, bio_rxn, make_anaerobic, change_protein_biomass, + carbon_weights, bio_rxn, make_anaerobic, change_protein_biomass, params.max_growth_weight, params.prior_penalty_weight, kcat0, sigma0_log), ) @@ -554,7 +560,7 @@ def _score_batch(kcat_matrix, pool): return np.array([ _score_kcat_vector( model, tunable_idx, ec_rxn_ids_tunable, experiment_data, - excarbon, bio_rxn, kcat_matrix[:, j], + carbon_weights, bio_rxn, kcat_matrix[:, j], make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, max_growth_weight=params.max_growth_weight, @@ -845,7 +851,7 @@ def _score_kcat_vector( tunable_idx: np.ndarray, ec_rxn_ids_tunable: list[str], experiment_data: ExperimentData, - excarbon: dict[str, float], + carbon_weights: dict[str, float], bio_rxn_id: str, kcat_vec: np.ndarray, *, @@ -881,6 +887,7 @@ def _score_kcat_vector( bio_rxn_id=bio_rxn_id, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, + optimize=optimize_model, ) max_growth_sims = None if experiment_data.max_growth is not None: @@ -890,10 +897,11 @@ def _score_kcat_vector( bio_rxn_id=bio_rxn_id, make_anaerobic=make_anaerobic, change_protein_biomass=change_protein_biomass, + optimize=optimize_model, ) rmse, _ = experiment_distance( experiment_data, flux_sims=flux_sims, max_growth_sims=max_growth_sims, - excarbon=excarbon, bio_rxn_id=bio_rxn_id, + carbon_weights=carbon_weights, bio_rxn_id=bio_rxn_id, max_growth_weight=max_growth_weight, ) # The search may run on a penalised objective, but the plain RMSE @@ -915,7 +923,7 @@ def _score_kcat_vector( _WORKER_TUNABLE_IDX: Optional[np.ndarray] = None _WORKER_EC_RXN_IDS_TUNABLE: Optional[list[str]] = None _WORKER_EXPERIMENT_DATA: Optional[ExperimentData] = None -_WORKER_EXCARBON: Optional[dict[str, float]] = None +_WORKER_CARBON_WEIGHTS: Optional[dict[str, float]] = None _WORKER_BIO_RXN: Optional[str] = None _WORKER_MAKE_ANAEROBIC = None _WORKER_CHANGE_PROTEIN_BIOMASS = None @@ -930,7 +938,7 @@ def _init_worker( tunable_idx: np.ndarray, ec_rxn_ids_tunable: list[str], experiment_data: ExperimentData, - excarbon: dict[str, float], + carbon_weights: dict[str, float], bio_rxn_id: str, make_anaerobic, change_protein_biomass, @@ -943,7 +951,7 @@ def _init_worker( (deserialised by ``ProcessPool``, not by us) and everything else needed to score a candidate.""" global _WORKER_MODEL, _WORKER_TUNABLE_IDX, _WORKER_EC_RXN_IDS_TUNABLE - global _WORKER_EXPERIMENT_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN + global _WORKER_EXPERIMENT_DATA, _WORKER_CARBON_WEIGHTS, _WORKER_BIO_RXN global _WORKER_MAKE_ANAEROBIC, _WORKER_CHANGE_PROTEIN_BIOMASS global _WORKER_MAX_GROWTH_WEIGHT, _WORKER_PRIOR_PENALTY_WEIGHT global _WORKER_KCAT0, _WORKER_SIGMA0_LOG @@ -951,7 +959,7 @@ def _init_worker( _WORKER_TUNABLE_IDX = tunable_idx _WORKER_EC_RXN_IDS_TUNABLE = ec_rxn_ids_tunable _WORKER_EXPERIMENT_DATA = experiment_data - _WORKER_EXCARBON = excarbon + _WORKER_CARBON_WEIGHTS = carbon_weights _WORKER_BIO_RXN = bio_rxn_id _WORKER_MAKE_ANAEROBIC = make_anaerobic _WORKER_CHANGE_PROTEIN_BIOMASS = change_protein_biomass @@ -965,7 +973,7 @@ def _score_worker(kcat_vec: np.ndarray) -> tuple[float, float]: assert _WORKER_MODEL is not None, "_score_worker called before _init_worker" return _score_kcat_vector( _WORKER_MODEL, _WORKER_TUNABLE_IDX, _WORKER_EC_RXN_IDS_TUNABLE, - _WORKER_EXPERIMENT_DATA, _WORKER_EXCARBON, _WORKER_BIO_RXN, kcat_vec, + _WORKER_EXPERIMENT_DATA, _WORKER_CARBON_WEIGHTS, _WORKER_BIO_RXN, kcat_vec, make_anaerobic=_WORKER_MAKE_ANAEROBIC, change_protein_biomass=_WORKER_CHANGE_PROTEIN_BIOMASS, max_growth_weight=_WORKER_MAX_GROWTH_WEIGHT, diff --git a/src/geckopy/limit_proteins/constrain_flux_data.py b/src/geckopy/limit_proteins/constrain_flux_data.py index 94e8057b..7eb2ac96 100644 --- a/src/geckopy/limit_proteins/constrain_flux_data.py +++ b/src/geckopy/limit_proteins/constrain_flux_data.py @@ -1,27 +1,18 @@ """Set model exchange-flux bounds from an experimental dataset. -In a typical chemostat experiment you measure exchange fluxes -(substrate uptake, byproduct secretion, biomass growth rate) -under one or more growth conditions. This function takes a -``FluxData`` (parsed from ``fluxData.tsv``), selects one condition, -and applies those measurements as bounds on the matching -exchange reactions in the model. - -The ``loose_strict_flux`` argument controls how tightly: ``"loose"`` -gives the model room to wiggle around the measurement; a numeric -percentage clamps it within that band. - -Ported from GECKO MATLAB: -src/limit_proteins/constrainFluxData.m. +The constraint logic is +:func:`raven_toolbox.experiments.apply_flux_data_constraints`; this +wrapper takes the biomass reaction and the carbon source from the model +adapter when they are not given. """ from __future__ import annotations from typing import TYPE_CHECKING, Literal, Optional, Union -import numpy as np +from raven_toolbox.experiments import apply_flux_data_constraints as _apply if TYPE_CHECKING: - from ..databases.flux_data import FluxData + from raven_toolbox.experiments import FluxData from ..ec_model.ec_model import EcModel @@ -44,14 +35,8 @@ def apply_flux_data_constraints( each exchange reaction in ``flux_data.exch_rxn_ids`` to its measured flux for the chosen condition. Also zeros out the adapter's preferred carbon source so the data determines the choice. - - Ported from GECKO MATLAB: - src/limit_proteins/constrainFluxData.m. - - A ``+/-1000`` measured flux means unconstrained, regardless of - ``loose_strict_flux``: - ``-1000`` -> ``lb=-1000, ub=0`` (free uptake), ``+1000`` -> - ``lb=0, ub=1000`` (free excretion). + See :func:`raven_toolbox.experiments.apply_flux_data_constraints` for + the bounds each setting gives. Parameters ---------- @@ -59,22 +44,19 @@ def apply_flux_data_constraints( EcModel with the exchange reactions referenced by ``flux_data`` already in place. Mutated in place. flux_data - Pre-loaded FluxData (typically from a future - ``load_flux_data`` utility). + Flux data, for example from ``load_flux_data``. condition Either the 0-indexed condition number, or the condition name (matched against ``flux_data.conds``). max_min_growth - ``"max"`` sets ``bio_rxn`` upper bound to the measured - growth rate (and lower bound to 0); ``"min"`` sets the lower + ``"max"`` sets the ``bio_rxn`` upper bound to the measured + growth rate (and the lower bound to 0); ``"min"`` sets the lower bound (suitable when minimising ``prot_pool_exchange``). loose_strict_flux - ``"loose"`` keeps one bound at 0 and the other at the - measured value (allows zero flux). A numeric percentage - ``p`` brackets the measured value with hard bounds: - ``lb = val * (1 - p/200)``, ``ub = val * (1 + p/200)``, - swapped as needed to keep ``lb <= ub`` when ``val`` is - negative. ``p=10`` allows 10% total variance (i.e. +/-5%). + ``"loose"`` keeps one bound at 0 and the other at the measured + value. A number ``p`` in [0, 100] brackets the measured value + with hard bounds of ``p`` percent in total (``p=10`` allows + +/-5%). bio_rxn Biomass reaction ID. Defaults to the adapter's configured biomass reaction (``model.adapter.params.bio_rxn``) when @@ -82,8 +64,7 @@ def apply_flux_data_constraints( c_source Reaction ID of the preferred carbon-source uptake reaction; its bounds are set to ``(0, 0)`` before applying the flux - data so that the data alone determines the carbon source - used. Defaults to ``model.adapter.params.c_source`` (or no + data. Defaults to ``model.adapter.params.c_source`` (or no blocking if there is no adapter) when ``None``; pass ``""`` to skip blocking any reaction. Silently ignored if the reaction id is not found in the model. @@ -91,104 +72,25 @@ def apply_flux_data_constraints( Raises ------ ValueError - If ``model.adapter`` is None, ``max_min_growth`` / - ``loose_strict_flux`` is invalid, ``condition`` (as string) - is not in ``flux_data.conds``, or any + If ``bio_rxn`` is ``None`` and ``model.adapter`` is None, + ``max_min_growth`` / ``loose_strict_flux`` is invalid, + ``condition`` (as string) is not in ``flux_data.conds``, or any ``flux_data.exch_rxn_ids`` is missing from the model. IndexError If ``condition`` (as int) is out of range. """ from ..adapter import resolve_param - if max_min_growth not in ("max", "min"): - raise ValueError( - f"max_min_growth must be 'max' or 'min', got {max_min_growth!r}" - ) - if not ( - loose_strict_flux == "loose" - or (isinstance(loose_strict_flux, (int, float)) - and not isinstance(loose_strict_flux, bool) - and 0 <= loose_strict_flux <= 100) - ): - raise ValueError( - f"loose_strict_flux must be 'loose' or a number in [0, 100]; " - f"got {loose_strict_flux!r}" - ) - - if isinstance(condition, str): - if condition not in flux_data.conds: - raise ValueError( - f"Condition {condition!r} not found in flux_data.conds " - f"({flux_data.conds})" - ) - cond_idx = flux_data.conds.index(condition) - else: - if not 0 <= condition < len(flux_data.conds): - raise IndexError( - f"condition index {condition} out of range " - f"(have {len(flux_data.conds)} condition(s))" - ) - cond_idx = condition - - fluxes = flux_data.exch_fluxes[cond_idx, :] - cobra_rxn_ids = {r.id for r in model.reactions} - missing = [r for r in flux_data.exch_rxn_ids if r not in cobra_rxn_ids] - if missing: - preview = missing[:5] - raise ValueError( - f"{len(missing)} exchange reaction ID(s) from flux_data are not " - f"present in the model (examples: {preview})" - ) - # c_source is optional: use the explicit value, else the adapter's if one - # is attached, else none (no adapter required for a c-source-free run). if c_source is None: - _adapter = getattr(model, "adapter", None) - c_source = _adapter.params.c_source if _adapter is not None else "" - if c_source: - try: - c_source_rxn = model.reactions.get_by_id(c_source) - c_source_rxn.lower_bound = 0.0 - c_source_rxn.upper_bound = 0.0 - except KeyError: - pass # c_source not in model; silent - + adapter = getattr(model, "adapter", None) + c_source = adapter.params.c_source if adapter is not None else "" bio_rxn_id = resolve_param( model, bio_rxn, "bio_rxn", purpose="apply_flux_data_constraints needs the biomass reaction id", ) - bio_rxn = model.reactions.get_by_id(bio_rxn_id) - gr = float(flux_data.gr_rate[cond_idx]) - if max_min_growth == "max": - bio_rxn.lower_bound = 0.0 - bio_rxn.upper_bound = gr - else: - bio_rxn.lower_bound = gr - bio_rxn.upper_bound = 1000.0 - - for rxn_id, flux in zip(flux_data.exch_rxn_ids, fluxes): - if np.isnan(flux): - continue - rxn = model.reactions.get_by_id(rxn_id) - - if abs(flux) == 1000.0: - if flux == -1000.0: - rxn.lower_bound = -1000.0 - rxn.upper_bound = 0.0 - else: - rxn.lower_bound = 0.0 - rxn.upper_bound = 1000.0 - continue - - if loose_strict_flux == "loose": - if flux < 0: - rxn.lower_bound = float(flux) - rxn.upper_bound = 0.0 - else: - rxn.lower_bound = 0.0 - rxn.upper_bound = float(flux) - else: - pct = float(loose_strict_flux) - lo = flux * (1 - pct / 200.0) - hi = flux * (1 + pct / 200.0) - rxn.lower_bound = float(min(lo, hi)) - rxn.upper_bound = float(max(lo, hi)) + _apply( + model, flux_data, + bio_rxn_id=bio_rxn_id, condition=condition, + max_min_growth=max_min_growth, loose_strict_flux=loose_strict_flux, + c_source=c_source, + ) diff --git a/src/geckopy/solver_support.py b/src/geckopy/solver_support.py deleted file mode 100644 index 50937e84..00000000 --- a/src/geckopy/solver_support.py +++ /dev/null @@ -1,674 +0,0 @@ -"""Support for optlang's scipy (HiGHS) solver interface. - -The scipy interface of optlang differs from the other interfaces in ways that matter for an -ecModel, and this module works around them. - -Index map after a removal - ``scipy_interface.Problem`` keeps a map from variable name to column. After a variable is - removed it renumbers the columns with ``islice(variables, start, len(constraints))``, so - the last ``len(variables) - len(constraints)`` names keep their old column whenever the - problem has more variables than constraints. Primal values, and so every flux that cobra - reads, then come from the wrong columns or raise an ``IndexError``, and a later removal - of such a variable deletes the wrong column of the coefficient matrix. Variables are - removed lazily, by the next ``solver.update()``, which any ``add`` or ``optimize`` runs, so - the damage shows up in code that never removed anything itself. - - :func:`repair_scipy_problem` moves the problem to a subclass that renumbers correctly, so - every removal after it is right, and rebuilds the coefficient matrix from the constraint - expressions if the map was already stale. :class:`~geckopy.ec_model.ec_model.EcModel` - calls it whenever ``model.solver`` is read, so an EcModel is covered whatever removes the - variables: geckopy, cobra, or user code that calls ``model.remove_reactions``, - ``reaction.delete`` or ``model.remove_cons_vars``. geckopy builds ecModels on a different - interface and converts them with :func:`switch_to_scipy`, so no removal happens on an - unrepaired scipy problem inside geckopy. For a plain ``cobra.Model`` on the scipy solver - that you edit yourself, call ``repair_scipy_problem(model)`` before the first removal; a - call after the removals still restores the map and the matrix, but anything read in - between was wrong. - -Coefficient matrix - optlang stores the matrix densely and copies it on every added or removed variable or - constraint, so building or editing a genome-scale model (thousands of reactions) takes - hours. The subclass installed by :func:`repair_scipy_problem` keeps the coefficients in - dictionaries and hands HiGHS a sparse matrix, built once per structural change. - ``problem.A`` is then a ``scipy.sparse.csr_matrix`` (a dense array when there are no rows). - -Solution after a solve - ``cobra.Model.optimize`` sets the objective direction again after the solve, and the scipy - interface discards the solution on every direction assignment, also one that changes - nothing. ``Reaction.flux`` and ``Solver.primal_values`` then find no values after - ``model.optimize()``. The subclass discards the solution only when the direction changes. - - After a solve that finds no optimum (infeasible, unbounded) the scipy interface returns - ``None`` for every primal value and raises for the objective, so ``cobra.Model.optimize`` - fails with a ``TypeError`` instead of returning a solution with that status. The subclass - returns NaN for both, so the solution has the status and NaN values. - -Duals - The scipy interface raises for ``Constraint.dual`` and ``Variable.dual`` but keeps the - whole ``linprog`` result in ``problem._solution``. :func:`constraint_shadow_prices` reads - the row marginals from it, so the shadow prices of a scipy model equal those of the glpk - and gurobi interfaces. The stored result is used only after it is shown to be an optimal - primal and dual solution of the problem as it is now, so a result left over from before - a bound or an objective changed is never read. -""" -from __future__ import annotations - -import contextlib -from itertools import islice -from typing import Iterable - -import numpy as np -from scipy import sparse - -try: - from optlang import scipy_interface as _scipy_interface - from optlang.util import SolverTolerances - from scipy.optimize import linprog -except ImportError: # pragma: no cover - optlang always includes the interface - _scipy_interface = None - -__all__ = [ - "repair_scipy_problem", - "switch_to_scipy", - "fast_solver_while_building", - "edit_on_fast_solver", - "is_scipy_interface", - "constraint_shadow_prices", - "optimize_model", -] - -def optimize_model(model, **kwargs): - """``model.optimize()`` that returns the solution of an infeasible LP on every solver. - - Gurobi holds no primal values for an LP that it presolved as infeasible and optlang's - scipy interface returns ``None`` for them, and cobra reads them while it builds the - solution, so ``model.optimize()`` raises ``GurobiError`` or ``TypeError`` there. When - the solver status is not optimal this function returns a ``Solution`` with that status, - a NaN objective and NaN fluxes instead; with an optimal status every error is raised. - """ - try: - return model.optimize(**kwargs) - except Exception: - if model.solver.status == "optimal": - raise - import pandas as pd - from cobra.core import Solution - - return Solution( - objective_value=float("nan"), - status=model.solver.status, - fluxes=pd.Series(float("nan"), index=[r.id for r in model.reactions]), - ) - - -# Relative tolerance of the optimality certificate, scaled by the size of the quantity checked. -_CERTIFICATE_TOL = 1e-6 - - -if _scipy_interface is not None: - - class _SparseProblem(_scipy_interface.Problem): - """``scipy_interface.Problem`` with a sparse coefficient matrix. - - Renumbers every variable column after a removal and keeps its solution when the - direction is set to the value it has. - """ - - def __init__(self): - super().__init__() - self._init_sparse() - - # Feasibility and optimality tolerance of HiGHS; None keeps its default (1e-7). - _feasibility = None - _optimality = None - - def optimize(self, method="highs", verbosity=False, tolerance=1e-9, **kwargs): - """``linprog`` on the problem, with the tolerances that were set on the model.""" - c = np.array([self.objective.get(name, 0) for name in self._variables], dtype=float) - if self.direction == "max": - c *= -1 - options = {"maxiter": 10000, "disp": verbosity} - bounds = list(self.bounds.values()) - - def solve(options): - return linprog(c, self.A, self.upper_bounds, bounds=bounds, method=method, options=options, **kwargs) - - tight = dict(options) - if self._feasibility is not None: - tight["primal_feasibility_tolerance"] = self._feasibility - if self._optimality is not None: - tight["dual_feasibility_tolerance"] = self._optimality - solution = solve(tight) - # HiGHS reports numerical difficulties (status 4) on some LPs at a tolerance - # below its default; those are solved again at the default. - if solution.status == 4 and tight != options: - solution = solve(options) - self._solution = solution - self._status = solution.status - if self._status == 0: - self._var_primals = solution.x - self._slacks = solution.slack - else: - self._var_primals = None - self._slacks = None - self._f = solution.fun - - @property - def status(self): - """The solver status; a solve that ended with numerical difficulties is ``numeric``.""" - if self._status is None: - raise RuntimeError("Problem has not been optimized yet") - return _scipy_interface.SCIPY_STATUS.get(self._status, _scipy_interface.interface.NUMERIC) - - def _init_sparse(self): - # _rows[row name] = {variable name: coefficient}; _cols[variable name] = {row names} - self._rows = {} - self._cols = {name: set() for name in self._variables} - self._matrix = None - self._A = None - - @property - def direction(self): - return self._direction - - @direction.setter - def direction(self, value): - if value != self._direction: - self._direction = value - self._reset_solution() - - @property - def A(self): - """The linear coefficient matrix: sparse, or a dense array without rows.""" - n, m = len(self._variables), len(self._constraints) - if m == 0: - return np.zeros((0, n)) - if self._matrix is None: - data, rows, cols = [], [], [] - columns = self._variables - for row_name, row_index in self._constraints.items(): - for name, coefficient in self._rows[row_name].items(): - data.append(coefficient) - rows.append(row_index) - cols.append(columns[name]) - self._matrix = sparse.csr_matrix((data, (rows, cols)), shape=(m, n), dtype=float) - return self._matrix - - def get_var_primal(self, name): - """The primal value; NaN after a solve that found no optimum, None before any solve.""" - if self._var_primals is None: - return None if self._status is None else float("nan") - return self._var_primals[self._get_var_index(name)] - - @property - def objective_value(self): - """The optimum; NaN after a solve that found none.""" - if self._f is None and self._status is not None: - return float("nan") - return super().objective_value - - def add_variable(self, name): - if name in self._variables: - raise ValueError("A variable named " + name + " already exists.") - self._variables[name] = len(self._variables) - self.bounds[name] = (0, None) - self._cols[name] = set() - self._matrix = None - self._reset_solution() - - def add_constraint(self, name, coefficients={}, ub=0): - if name in self._constraints: - raise ValueError("A constraint named " + name + " already exists.") - self._constraints[name] = len(self._constraints) - self.upper_bounds = np.append(self.upper_bounds, ub) - row = {v: c for v, c in coefficients.items() if c != 0 and v in self._variables} - self._rows[name] = row - for v in row: - self._cols[v].add(name) - self._matrix = None - self._reset_solution() - - def remove_variable(self, name): - index = self._get_var_index(name) - for row_name in self._cols.pop(name): - del self._rows[row_name][name] - del self.bounds[name] - del self._variables[name] - self._update_variable_indices(index) - self._matrix = None - self._reset_solution() - - def remove_constraint(self, name): - index = self._get_constraint_index(name) - self.upper_bounds = np.delete(self.upper_bounds, index) - for v in self._rows.pop(name): - self._cols[v].discard(name) - del self._constraints[name] - self._update_constraint_indices(index) - self._matrix = None - self._reset_solution() - - def change_var_name(self, name, value): - super().change_var_name(name, value) - if name != value: - for row_name in self._cols[name]: - self._rows[row_name][value] = self._rows[row_name].pop(name) - self._cols[value] = self._cols.pop(name) - - def change_constraint_name(self, name, value): - super().change_constraint_name(name, value) - if name != value: - self._rows[value] = self._rows.pop(name) - for v in self._rows[value]: - self._cols[v].discard(name) - self._cols[v].add(value) - - def _update_variable_indices(self, start=0): - i = start - for name in islice(self._variables, start, None): - self._variables[name] = i - i += 1 - - -if _scipy_interface is not None: - - class _SparseModel(_scipy_interface.Model): - """``scipy_interface.Model`` on a `_SparseProblem` that copies and pickles sparsely. - - optlang pickles a model as JSON and restores it by adding the variables and - constraints to a fresh dense problem, which takes tens of minutes for a genome-scale - model. This class restores into a `_SparseProblem`, so ``copy.deepcopy``, - ``cobra.Model.copy`` and the worker processes of a pool get their copy in seconds. - """ - - # optlang finds its interface module (the scipy one) through this attribute. - __module__ = _scipy_interface.__name__ - - def _initialize_problem(self): - self.problem = _SparseProblem() - - def _remove_constraints(self, constraints): - # optlang's scipy interface calls the generic removal inside its loop, once per - # constraint with the whole list, so removing two or more constraints at once - # (up to 350) raises ``LookupError: Constraint ... not in solver``. - for constraint in constraints: - if constraint.lb is not None: - self.problem.remove_constraint(constraint.lower_constraint_name) - if constraint.ub is not None: - self.problem.remove_constraint(constraint.upper_constraint_name) - _scipy_interface.interface.Model._remove_constraints(self, constraints) - - def _initialize_configuration(self): - super()._initialize_configuration() - _install_tolerances(self) - - def __reduce__(self): - problem = self.problem - return (_restore_sparse_model, (self.to_json(), problem._feasibility, problem._optimality)) - - def _restore_sparse_model(state, feasibility, optimality): - model = _SparseModel() - model._init_from_json(state) - model.problem._feasibility = feasibility - model.problem._optimality = optimality - return model - - def _install_tolerances(solver): - """Let ``solver.configuration.tolerances`` (and so ``cobra.Model.tolerance``) set the - feasibility and optimality tolerance of HiGHS, which optlang's scipy interface ignores.""" - - def setter(attribute): - return lambda value: setattr(solver.problem, attribute, float(value)) - - solver.configuration.tolerances = SolverTolerances( - { - "feasibility": (lambda: solver.problem._feasibility or 1e-7, setter("_feasibility")), - "optimality": (lambda: solver.problem._optimality or 1e-7, setter("_optimality")), - } - ) - - -def is_scipy_interface(interface) -> bool: - """True if ``interface`` (a solver name, an optlang interface module or a model's - ``problem`` attribute) is optlang's scipy interface.""" - if _scipy_interface is None: - return False - if isinstance(interface, str): - return interface.lower() == "scipy" - return interface is _scipy_interface - - -def _is_scipy_problem(problem) -> bool: - return _scipy_interface is not None and isinstance(problem, _scipy_interface.Problem) - - -def _map_is_stale(problem) -> bool: - return list(problem._variables.values()) != list(range(len(problem._variables))) - - -def _adopt_dense_matrix(problem) -> None: - """Fill the dictionaries of a `_SparseProblem` from the dense matrix it was swapped from.""" - dense = problem._A - names = list(problem._variables) - row_names = list(problem._constraints) - problem._init_sparse() - problem._rows = {name: {} for name in row_names} - for r, c in zip(*np.nonzero(dense)): - problem._rows[row_names[r]][names[c]] = float(dense[r, c]) - problem._cols[names[c]].add(row_names[r]) - - -def _rebuild_from_expressions(problem, solver) -> None: - """Fill the dictionaries of a `_SparseProblem` from the constraint expressions of ``solver``. - - The expressions are the source the matrix is derived from, so this is correct whatever - happened to the matrix itself. - """ - problem._init_sparse() - problem._rows = {name: {} for name in problem._constraints} - for i, name in enumerate(problem._variables): - problem._variables[name] = i - for constraint in solver.constraints: - coefficients = None - for row_name, sign in ( - (constraint.upper_constraint_name, 1.0), - (constraint.lower_constraint_name, -1.0), - ): - if row_name not in problem._rows: - continue - if coefficients is None: - coefficients = constraint.coefficient_dict() - row = {v: sign * c for v, c in coefficients.items() if c != 0 and v in problem._variables} - problem._rows[row_name] = row - for v in row: - problem._cols[v].add(row_name) - - -def repair_scipy_problem(model) -> bool: - """Make the scipy problem of ``model`` correct and fast to edit. - - Does nothing and returns False when the solver of ``model`` is not optlang's scipy - interface. Otherwise it moves the problem to a class that renumbers the variable columns - correctly after a removal and stores the coefficients sparsely (see the module docstring), - applies the pending changes, restores the column map and the coefficient matrix if a - removal had already left the map stale, and returns True. Once the problem is switched, a - call is a type check. - - Parameters - ---------- - model - A ``cobra.Model`` (or EcModel). Reading ``model.solver`` of an EcModel calls this - function already; call it yourself for a plain ``cobra.Model`` before you remove - variables (``remove_reactions``, ``Reaction.delete``, ``remove_cons_vars``). - """ - solver = getattr(model, "_solver", None) - problem = getattr(solver, "problem", None) - if not _is_scipy_problem(problem): - return False - if isinstance(problem, _SparseProblem): - return True - stale = _map_is_stale(problem) - problem.__class__ = _SparseProblem - solver.__class__ = _SparseModel - _install_tolerances(solver) - if stale: - problem._init_sparse() - problem._rows = {name: {} for name in problem._constraints} - else: - _adopt_dense_matrix(problem) - solver.update() - if stale: - _rebuild_from_expressions(problem, solver) - return True - - -def switch_to_scipy(model) -> None: - """Move ``model`` to the scipy solver, fast, with the sparse problem of this module. - - ``model.solver = "scipy"`` clones the problem through optlang, which grows a dense matrix - row by row. This builds the problem the way ``cobra.Model`` does when it is created, on - the sparse class, so a genome-scale model switches in seconds. The objective and its - direction are kept. A model whose solver holds anything besides one forward and one - reverse variable per reaction and one constraint per metabolite (an extra constraint or - variable of your own) is switched with optlang's clone instead and then repaired. - """ - if repair_scipy_problem(model): - return - solver = model.solver - plain = len(solver.constraints) == len(model.metabolites) and len(solver.variables) == 2 * len( - model.reactions - ) - if _scipy_interface is None or not plain: - from cobra import Model - - Model.solver.fset(model, "scipy") - repair_scipy_problem(model) - return - from cobra.util.solver import linear_reaction_coefficients - from optlang.symbolics import Zero - - coefficients = linear_reaction_coefficients(model) - direction = model.objective.direction - new = _SparseModel() - new.objective = _scipy_interface.Objective(Zero) - model._solver = new - _populate_sparse(model, new) - model.objective = coefficients - model.objective.direction = direction - if getattr(model, "_tolerance", None) is not None: - try: - model.tolerance = model._tolerance - except Exception: # the scipy interface has no tolerance settings to receive it - pass - - -def _populate_sparse(model, solver) -> None: - """Give ``solver`` one forward and one reverse variable per reaction of ``model`` and one - constraint per metabolite, as ``cobra.Model._populate_solver`` does. - - ``_populate_solver`` sets the coefficients of a constraint one at a time, and the scipy - interface removes and adds the rows of a constraint for each call, which takes hours for a - genome-scale model. Here every constraint is created with its whole expression and added - once. - """ - from optlang.symbolics import Zero, add - - problem = solver.interface - variables = {} - for reaction in model.reactions: - variables[reaction.id] = (problem.Variable(reaction.id), problem.Variable(reaction.reverse_id)) - solver.add([v for pair in variables.values() for v in pair]) - terms = {metabolite.id: [] for metabolite in model.metabolites} - for reaction in model.reactions: - forward, reverse = variables[reaction.id] - for metabolite, coefficient in reaction.metabolites.items(): - terms[metabolite.id].append(coefficient * forward) - terms[metabolite.id].append(-coefficient * reverse) - solver.add( - [ - problem.Constraint(add(*expressions) if expressions else Zero, name=name, lb=0, ub=0) - for name, expressions in terms.items() - ] - ) - for reaction in model.reactions: - reaction.update_variable_bounds() - - -@contextlib.contextmanager -def edit_on_fast_solver(model): - """Run structural edits of ``model`` on glpk when it is on the scipy solver. - - Removing reactions and metabolites, or rewriting the coefficients of thousands of - reactions, is quick on glpk and slow on the scipy interface, where optlang rewrites the - constraint expressions (and the rows) for each edit. Inside the block a scipy model is on - glpk; on exit it is converted back with :func:`switch_to_scipy`. The block changes nothing - for a model on another solver, for a model inside a ``with model:`` context (its - rollbacks hold the solver objects) and when glpk is unavailable. The optlang solver of - the model is a different object inside the block and after it. - """ - from cobra import Model - - solver = getattr(model, "_solver", None) - if not _is_scipy_problem(getattr(solver, "problem", None)) or getattr(model, "_contexts", None): - yield False - return - try: - Model.solver.fset(model, "glpk") - except Exception: - yield False - return - try: - yield True - finally: - switch_to_scipy(model) - - -@contextlib.contextmanager -def fast_solver_while_building(): - """Make models created inside the block on a solver that builds large models quickly. - - When cobra's default solver is scipy the block runs with glpk as the default, which - builds a genome-scale model in seconds; the default is restored on exit. Models created - inside are on glpk; convert them with :func:`switch_to_scipy`. With any other default - solver, or when glpk is unavailable, the block changes nothing. - """ - import cobra - - config = cobra.Configuration() - previous = config.solver - switched = False - if getattr(previous, "__name__", str(previous)).endswith("scipy_interface"): - try: - config.solver = "glpk" - switched = True - except Exception: - switched = False - try: - yield switched - finally: - if switched: - config.solver = previous - - -def _objective_vector(problem) -> np.ndarray: - """Cost vector ``linprog`` minimises: the objective, negated for a maximisation.""" - cost = np.array([problem.objective.get(name, 0.0) for name in problem._variables], dtype=float) - if problem.direction == "max": - cost = -cost - return cost - - -def _certified_result(problem): - """The stored ``linprog`` result if it is an optimum of ``problem`` as it is now, else None. - - Checks primal feasibility against the current rows and bounds, stationarity of the cost - vector against the stored marginals, and equality of the primal and dual objective. A - result that passes is an optimal primal-dual pair of the current problem, whatever solve - produced it. - """ - result = getattr(problem, "_solution", None) - try: - x = np.asarray(result.x, dtype=float) - y = np.asarray(result.ineqlin.marginals, dtype=float) - z_low = np.asarray(result.lower.marginals, dtype=float) - z_up = np.asarray(result.upper.marginals, dtype=float) - fun = float(result.fun) - matrix = sparse.csr_matrix(problem.A, dtype=float) - rhs = np.asarray(problem.upper_bounds, dtype=float) - bounds = list(problem.bounds.values()) - except (AttributeError, TypeError, ValueError): - return None - n, m = len(problem._variables), len(problem._constraints) - if matrix.shape != (m, n) or not (x.shape == z_low.shape == z_up.shape == (n,)) or y.shape != (m,): - return None - if len(bounds) != n or not np.all(np.isfinite(x)): - return None - low = np.array([-np.inf if b[0] is None else b[0] for b in bounds], dtype=float) - up = np.array([np.inf if b[1] is None else b[1] for b in bounds], dtype=float) - cost = _objective_vector(problem) - tol = _CERTIFICATE_TOL - - def close(a, b, scale): - return abs(a - b) <= tol * (1.0 + scale) - - if m and np.any(matrix @ x - rhs > tol * (1.0 + np.abs(rhs))): - return None - if np.any(x < low - tol * (1.0 + np.abs(low))) or np.any(x > up + tol * (1.0 + np.abs(up))): - return None - reduced = cost - (matrix.T @ y if m else 0.0) - if np.max(np.abs(reduced - (z_low + z_up)), initial=0.0) > tol * (1.0 + np.max(np.abs(cost), initial=0.0)): - return None - primal = float(cost @ x) - dual = float(rhs @ y) if m else 0.0 - dual += float(np.sum(low[np.isfinite(low)] * z_low[np.isfinite(low)])) - dual += float(np.sum(up[np.isfinite(up)] * z_up[np.isfinite(up)])) - if not (close(primal, fun, abs(fun)) and close(primal, dual, abs(primal))): - return None - return result - - -def constraint_shadow_prices(model, constraint_ids: Iterable[str]) -> np.ndarray: - """Shadow prices of the named constraints, on every solver, as one array. - - The shadow price of a constraint is the change of the optimum, in the direction of the - model objective, per unit rise of its right-hand side: for a metabolite balance - ``S v = 0`` it is the optimum gained per unit of the metabolite supplied. It is the value - of ``cobra.Metabolite.shadow_price`` for the glpk, gurobi and cplex interfaces, and for the - scipy interface (which raises) it is computed from the HiGHS row marginals of the last - solve. - - The model must have been solved. On the scipy interface the stored result is used only if - it is an optimum of the problem as it is now; otherwise the model is solved once more. If - that solve is not optimal, the function raises. - - Parameters - ---------- - model - An EcModel or ``cobra.Model`` that has been optimized. - constraint_ids - IDs of constraints of the model, for example ``prot_``. - - Returns - ------- - numpy.ndarray - One shadow price per ID, in the order given. - - Raises - ------ - RuntimeError - If the scipy problem has no optimal solution to read the marginals from. - KeyError - If an ID is not a constraint of the model. - """ - ids = list(constraint_ids) - constraints = model.constraints - repair_scipy_problem(model) - problem = getattr(model.solver, "problem", None) - if _scipy_interface is None or not isinstance(problem, _scipy_interface.Problem): - return np.array([float(constraints[cid].dual) for cid in ids], dtype=float) - - result = _certified_result(problem) - if result is None: - model.slim_optimize() - result = _certified_result(problem) - if result is None: - raise RuntimeError( - "shadow prices need an optimal solution of the scipy problem; the model has none " - f"(solver status {model.solver.status!r})" - ) - marginals = np.asarray(result.ineqlin.marginals, dtype=float) - rows = problem._constraints - # The rows hold `A x <= ub` and `-A x <= -lb`. The marginal of a row is d(minimised value)/d(rhs - # of that row), so d/d(ub) is the upper row's marginal and d/d(lb) is minus the lower row's. - # A maximisation minimises the negated objective. - sign = -1.0 if problem.direction == "max" else 1.0 - prices = np.zeros(len(ids), dtype=float) - for k, cid in enumerate(ids): - constraint = constraints[cid] - value = 0.0 - if constraint.ub is not None: - value += marginals[rows[constraint.upper_constraint_name]] - if constraint.lb is not None: - value -= marginals[rows[constraint.lower_constraint_name]] - prices[k] = sign * value - return prices diff --git a/tests/test_constrain_flux_data.py b/tests/test_constrain_flux_data.py index ea6ec7eb..10adde71 100644 --- a/tests/test_constrain_flux_data.py +++ b/tests/test_constrain_flux_data.py @@ -1,385 +1,89 @@ -"""Tests for apply_flux_data_constraints.""" +"""Tests for apply_flux_data_constraints: the defaults taken from the adapter. + +The bounds each setting gives are tested with the function in +raven_toolbox.experiments. +""" from pathlib import Path import cobra import numpy as np import pytest -from scipy import sparse from geckopy import EcModel, ModelAdapter -from geckopy.databases import FluxData -from geckopy.ec_model.ec_data import EcData from geckopy.limit_proteins import apply_flux_data_constraints +from raven_toolbox.experiments import FluxData -# --------------------------------------------------------------------------- # -# Fixture builders -# --------------------------------------------------------------------------- # - -def _adapter( - tmp_path: Path, - *, - bio_rxn: str = "biomass", - c_source: str = "EX_glc", -) -> ModelAdapter: +def _adapter(tmp_path: Path, *, c_source: str = "EX_eth") -> ModelAdapter: (tmp_path / "model_adapter.toml").write_text( - f'conv_gem = "dummy.xml"\n' - f'org_name = "test"\n' - f'bio_rxn = "{bio_rxn}"\n' + 'conv_gem = "dummy.xml"\n' + 'org_name = "test"\n' + 'bio_rxn = "biomass"\n' f'c_source = "{c_source}"\n' ) return ModelAdapter.from_folder(tmp_path) -def _build_simple_model(adapter: ModelAdapter) -> EcModel: - """Tiny model with biomass + a few exchange rxns.""" +def _build_model(adapter: ModelAdapter) -> EcModel: model = EcModel("toy", adapter=adapter) - glc = cobra.Metabolite("glc_e", compartment="e") - O2 = cobra.Metabolite("O2_e", compartment="e") eth = cobra.Metabolite("eth_e", compartment="e") bio_met = cobra.Metabolite("bio", compartment="c") - model.add_metabolites([glc, O2, eth, bio_met]) - - EX_glc = cobra.Reaction("EX_glc") - EX_glc.add_metabolites({glc: -1.0}) - EX_glc.lower_bound = -1000.0 - EX_glc.upper_bound = 1000.0 - - EX_O2 = cobra.Reaction("EX_O2") - EX_O2.add_metabolites({O2: -1.0}) - EX_O2.lower_bound = -1000.0 - EX_O2.upper_bound = 1000.0 - - EX_eth = cobra.Reaction("EX_eth") - EX_eth.add_metabolites({eth: -1.0}) - EX_eth.lower_bound = -1000.0 - EX_eth.upper_bound = 1000.0 - - BIO = cobra.Reaction("biomass") - BIO.add_metabolites({bio_met: 1.0}) - BIO.lower_bound = 0.0 - BIO.upper_bound = 1000.0 - - model.add_reactions([EX_glc, EX_O2, EX_eth, BIO]) - - model.ec = EcData( - rxns=[], - kcat=np.empty(0, dtype=float), - source=[], - notes=[], - eccodes=[], - rxn_enz_mat=sparse.csr_matrix((0, 0), dtype=float), - ) + model.add_metabolites([glc, eth, bio_met]) + rxns = [] + for rxn_id, met, coeff in ( + ("EX_glc", glc, -1.0), ("EX_eth", eth, -1.0), ("biomass", bio_met, 1.0), + ): + rxn = cobra.Reaction(rxn_id) + rxn.add_metabolites({met: coeff}) + rxn.bounds = (-1000.0, 1000.0) + rxns.append(rxn) + model.add_reactions(rxns) return model -def _flux_data( - *, - conds: list[str], - p_tot: list[float], - gr_rate: list[float], - exch_fluxes: list[list[float]], - exch_rxn_ids: list[str], -) -> FluxData: +def _flux_data() -> FluxData: return FluxData( - conds=list(conds), - p_tot=np.array(p_tot, dtype=float), - gr_rate=np.array(gr_rate, dtype=float), - exch_fluxes=np.array(exch_fluxes, dtype=float), - exch_mets=list(exch_rxn_ids), - exch_rxn_ids=list(exch_rxn_ids), - ) - - -# --------------------------------------------------------------------------- # -# Pre-flight -# --------------------------------------------------------------------------- # - -def test_no_adapter_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - model.adapter = None - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - with pytest.raises(ValueError, match="adapter"): - apply_flux_data_constraints(model, fd) - - -def test_invalid_max_min_growth_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - with pytest.raises(ValueError, match="max_min_growth"): - apply_flux_data_constraints(model, fd, max_min_growth="invalid") - - -def test_invalid_loose_strict_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - with pytest.raises(ValueError, match="loose_strict_flux"): - apply_flux_data_constraints(model, fd, loose_strict_flux=200) - - -def test_unknown_condition_name_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - with pytest.raises(ValueError, match="Condition"): - apply_flux_data_constraints(model, fd, condition="nonexistent") - - -def test_out_of_range_condition_index_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - with pytest.raises(IndexError, match="condition index"): - apply_flux_data_constraints(model, fd, condition=5) - - -def test_unknown_rxn_id_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_nonexistent"], - ) - with pytest.raises(ValueError, match="not present in the model"): - apply_flux_data_constraints(model, fd) - - -# --------------------------------------------------------------------------- # -# Growth bounds -# --------------------------------------------------------------------------- # - -def test_max_growth_sets_upper_bound(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, max_min_growth="max") - bio = model.reactions.get_by_id("biomass") - assert bio.lower_bound == 0.0 - assert bio.upper_bound == pytest.approx(0.4) - - -def test_min_growth_sets_lower_bound(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, max_min_growth="min") - bio = model.reactions.get_by_id("biomass") - assert bio.lower_bound == pytest.approx(0.4) - assert bio.upper_bound == 1000.0 - - -# --------------------------------------------------------------------------- # -# c_source reset -# --------------------------------------------------------------------------- # - -def test_c_source_zeroed(tmp_path): - adapter = _adapter(tmp_path, c_source="EX_eth") - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd) - eth_rxn = model.reactions.get_by_id("EX_eth") - assert eth_rxn.lower_bound == 0.0 - assert eth_rxn.upper_bound == 0.0 - - -def test_empty_c_source_skipped(tmp_path): - adapter = _adapter(tmp_path, c_source="") - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - # Just shouldn't raise; bounds for non-data rxns left untouched. - apply_flux_data_constraints(model, fd) - - -# --------------------------------------------------------------------------- # -# Loose mode -# --------------------------------------------------------------------------- # - -def test_loose_negative_flux_caps_lb(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux="loose") - glc_rxn = model.reactions.get_by_id("EX_glc") - assert glc_rxn.lower_bound == -5.0 - assert glc_rxn.upper_bound == 0.0 - - -def test_loose_positive_flux_caps_ub(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[3.0]], exch_rxn_ids=["EX_eth"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux="loose") - eth_rxn = model.reactions.get_by_id("EX_eth") - assert eth_rxn.lower_bound == 0.0 - assert eth_rxn.upper_bound == 3.0 - - -def test_loose_nan_flux_skipped(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - original_lb = model.reactions.get_by_id("EX_O2").lower_bound - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[np.nan]], exch_rxn_ids=["EX_O2"], - ) - apply_flux_data_constraints(model, fd) - # NaN means "no measurement"; bounds untouched. - assert model.reactions.get_by_id("EX_O2").lower_bound == original_lb - - -# --------------------------------------------------------------------------- # -# Percentage variance mode -# --------------------------------------------------------------------------- # - -def test_percentage_brackets_positive_value(tmp_path): - """pct=10 -> lb = val*0.95, ub = val*1.05.""" - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[3.0]], exch_rxn_ids=["EX_eth"], + conds=["c1"], + p_tot=np.array([0.5]), + gr_rate=np.array([0.4]), + exch_fluxes=np.array([[-5.0]]), + exch_mets=["EX_glc"], + exch_rxn_ids=["EX_glc"], ) - apply_flux_data_constraints(model, fd, loose_strict_flux=10) - eth_rxn = model.reactions.get_by_id("EX_eth") - assert eth_rxn.lower_bound == pytest.approx(2.85) - assert eth_rxn.upper_bound == pytest.approx(3.15) -def test_percentage_handles_negative_value(tmp_path): - """For negative val, lb and ub are swapped to keep lb <= ub.""" - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0]], exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux=10) - glc_rxn = model.reactions.get_by_id("EX_glc") - # val=-5, pct=10 -> raw [-5*0.95, -5*1.05] = [-4.75, -5.25] - # After swap: lb = -5.25, ub = -4.75. - assert glc_rxn.lower_bound == pytest.approx(-5.25) - assert glc_rxn.upper_bound == pytest.approx(-4.75) +def test_biomass_and_carbon_source_come_from_the_adapter(tmp_path): + model = _build_model(_adapter(tmp_path)) + apply_flux_data_constraints(model, _flux_data()) -# --------------------------------------------------------------------------- # -# Extreme +/-1000 sentinel -# --------------------------------------------------------------------------- # + assert model.reactions.get_by_id("biomass").upper_bound == pytest.approx(0.4) + eth = model.reactions.get_by_id("EX_eth") + assert (eth.lower_bound, eth.upper_bound) == (0.0, 0.0) -def test_minus_thousand_is_unconstrained_uptake(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-1000.0]], exch_rxn_ids=["EX_O2"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux=10) - o2_rxn = model.reactions.get_by_id("EX_O2") - assert o2_rxn.lower_bound == -1000.0 - assert o2_rxn.upper_bound == 0.0 +def test_empty_c_source_blocks_nothing(tmp_path): + model = _build_model(_adapter(tmp_path)) -def test_plus_thousand_is_unconstrained_excretion(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[1000.0]], exch_rxn_ids=["EX_eth"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux=10) - eth_rxn = model.reactions.get_by_id("EX_eth") - assert eth_rxn.lower_bound == 0.0 - assert eth_rxn.upper_bound == 1000.0 + apply_flux_data_constraints(model, _flux_data(), c_source="") + eth = model.reactions.get_by_id("EX_eth") + assert (eth.lower_bound, eth.upper_bound) == (-1000.0, 1000.0) -# --------------------------------------------------------------------------- # -# Condition selection -# --------------------------------------------------------------------------- # -def test_condition_by_index(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1", "c2"], - p_tot=[0.5, 0.6], - gr_rate=[0.4, 0.7], - exch_fluxes=[[-5.0], [-8.0]], - exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, condition=1) - bio = model.reactions.get_by_id("biomass") - assert bio.upper_bound == pytest.approx(0.7) - glc_rxn = model.reactions.get_by_id("EX_glc") - assert glc_rxn.lower_bound == -8.0 +def test_explicit_biomass_reaction_overrides_the_adapter(tmp_path): + model = _build_model(_adapter(tmp_path)) + model.adapter = None + apply_flux_data_constraints(model, _flux_data(), bio_rxn="biomass") -def test_condition_by_name(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1", "c2"], - p_tot=[0.5, 0.6], - gr_rate=[0.4, 0.7], - exch_fluxes=[[-5.0], [-8.0]], - exch_rxn_ids=["EX_glc"], - ) - apply_flux_data_constraints(model, fd, condition="c2") - glc_rxn = model.reactions.get_by_id("EX_glc") - assert glc_rxn.lower_bound == -8.0 + assert model.reactions.get_by_id("biomass").upper_bound == pytest.approx(0.4) -# --------------------------------------------------------------------------- # -# Multi-rxn flux scenarios -# --------------------------------------------------------------------------- # +def test_no_adapter_and_no_biomass_reaction_raises(tmp_path): + model = _build_model(_adapter(tmp_path)) + model.adapter = None -def test_multiple_rxns_constrained_correctly(tmp_path): - adapter = _adapter(tmp_path) - model = _build_simple_model(adapter) - fd = _flux_data( - conds=["c1"], p_tot=[0.5], gr_rate=[0.4], - exch_fluxes=[[-5.0, -2.0, 3.0]], - exch_rxn_ids=["EX_glc", "EX_O2", "EX_eth"], - ) - apply_flux_data_constraints(model, fd, loose_strict_flux="loose") - glc = model.reactions.get_by_id("EX_glc") - o2 = model.reactions.get_by_id("EX_O2") - eth = model.reactions.get_by_id("EX_eth") - assert (glc.lower_bound, glc.upper_bound) == (-5.0, 0.0) - assert (o2.lower_bound, o2.upper_bound) == (-2.0, 0.0) - assert (eth.lower_bound, eth.upper_bound) == (0.0, 3.0) + with pytest.raises(ValueError, match="adapter"): + apply_flux_data_constraints(model, _flux_data()) diff --git a/tests/test_constrain_from_flux_file.py b/tests/test_constrain_from_flux_file.py new file mode 100644 index 00000000..deb16619 --- /dev/null +++ b/tests/test_constrain_from_flux_file.py @@ -0,0 +1,67 @@ +"""A fluxData.tsv file feeds apply_flux_data_constraints.""" +from pathlib import Path + +import numpy as np +import pytest + +from raven_toolbox.experiments import load_flux_data + + +def _write_tsv(path: Path, rows: list[list[str]]) -> None: + path.write_text( + "\n".join("\t".join(r) for r in rows) + "\n", + encoding="utf-8", + ) + + +def test_loaded_data_can_feed_apply_flux_data_constraints(tmp_path): + """End-to-end: load a fluxData.tsv, constrain a model with it.""" + import cobra + from scipy import sparse + + from geckopy import EcModel, ModelAdapter + from geckopy.ec_model.ec_data import EcData + from geckopy.limit_proteins import apply_flux_data_constraints + + (tmp_path / "model_adapter.toml").write_text( + 'conv_gem = "dummy.xml"\n' + 'org_name = "test"\n' + 'bio_rxn = "biomass"\n' + 'c_source = "EX_glc"\n' + ) + adapter = ModelAdapter.from_folder(tmp_path) + + # Build a tiny model with one exchange and biomass. + model = EcModel("test", adapter=adapter) + glc = cobra.Metabolite("glc_e", compartment="e") + bio = cobra.Metabolite("bio", compartment="c") + model.add_metabolites([glc, bio]) + EX_glc = cobra.Reaction("EX_glc") + EX_glc.add_metabolites({glc: -1.0}) + EX_glc.lower_bound = -1000.0 + EX_glc.upper_bound = 0.0 + BIO = cobra.Reaction("biomass") + BIO.add_metabolites({bio: 1.0}) + BIO.lower_bound = 0.0 + BIO.upper_bound = 1000.0 + model.add_reactions([EX_glc, BIO]) + model.ec = EcData( + rxns=[], kcat=np.empty(0), source=[], notes=[], eccodes=[], + rxn_enz_mat=sparse.csr_matrix((0, 0)), + ) + + # Write a fluxData.tsv with a different exchange (so c_source gets zeroed). + p = tmp_path / "fd.tsv" + _write_tsv(p, [ + ["condition", "Ptot", "grRate", "glucose (EX_glc)"], + ["c1", "0.5", "0.4", "-5.0"], + ]) + fd = load_flux_data(p) + apply_flux_data_constraints(model, fd) + assert model.reactions.get_by_id("EX_glc").lower_bound == -5.0 + assert model.reactions.get_by_id("biomass").upper_bound == pytest.approx(0.4) + + +# --------------------------------------------------------------------------- # +# Malformed rows and headers +# --------------------------------------------------------------------------- # diff --git a/tests/test_evotune.py b/tests/test_evotune.py index 6e3dbcde..e5895106 100644 --- a/tests/test_evotune.py +++ b/tests/test_evotune.py @@ -19,7 +19,7 @@ import cobra from geckopy import EcModel, ModelAdapter from geckopy.adapter.params import EvotuneParams, SourceGroupRule -from geckopy.databases.flux_data import FluxData +from raven_toolbox.experiments import ExperimentData, FluxData from geckopy.ec_model.ec_data import EcData from geckopy.kcat_tuning.evotune.tuning import ( cmaes_kcat_tuning, @@ -27,7 +27,6 @@ select_tunable_mask, tune_prior_penalty_weight, ) -from geckopy.experiments.data import ExperimentData from geckopy.kcat_tuning.evotune.tuning import EvotuneResult GROUPS = { diff --git a/tests/test_experiment_conditions.py b/tests/test_experiment_conditions.py deleted file mode 100644 index 7707da5d..00000000 --- a/tests/test_experiment_conditions.py +++ /dev/null @@ -1,481 +0,0 @@ -"""Tests for experiments.conditions and experiments.distance. - -Styled like test_sensitivity_tuning.py's hand-built toy EcModel: a -tiny two-carbon-source model with a known enzyme cap, so every -simulated growth/exchange-flux value used below is hand-predictable -from the model's stoichiometry rather than treated as a black box. -""" -from pathlib import Path - -import cobra -import numpy as np -import pytest - -from geckopy import EcModel, ModelAdapter -from geckopy.databases.flux_data import FluxData -from geckopy.experiments.data import ExperimentData -from geckopy.experiments.distance import ( - BIOMASS_CARBON_EQUIV, - experiment_distance, - compute_excarbon, - dataset_rmse, -) -from geckopy.experiments.conditions import ( - ConditionSimResult, - simulate_conditions, -) - - -# --------------------------------------------------------------------------- # -# Toy two-carbon-source ecModel fixture -# --------------------------------------------------------------------------- # -# -# glucose and ethanol each feed a shared pool metabolite A_c, which the -# single enzyme-gated reaction R turns into biomass at an enzyme-capped -# rate of kcat*3600/mw = 1*3600/100 = 36 (pool size 1 mg/gDW). R also -# co-produces a small fixed amount of a "byproduct" the experimental -# data assumes is never made (byp_c, 0.1 mol per mol of R flux) -- this -# exercises `zero_flux_rxns`' nonzero-simulated-flux case. -# -# - "glucose" condition: uptake unconstrained relative to the enzyme -# cap (measured flux -36 exactly matches TR's capacity), so growth -# is enzyme-limited: 36. -# - "ethanol" condition: uptake capped at -20 (below the enzyme cap), -# so growth is carbon-limited instead: 20. - -_KCAT = 1.0 -_MW = 100.0 -_ENZYME_CAP = _KCAT * 3600.0 / _MW # 36.0 -_BYPRODUCT_YIELD = 0.1 - - -def _adapter(tmp_path: Path) -> ModelAdapter: - (tmp_path / "model_adapter.toml").write_text( - 'conv_gem = "dummy.xml"\n' - 'org_name = "test"\n' - 'bio_rxn = "biomass"\n' - ) - return ModelAdapter.from_folder(tmp_path) - - -def _build_toy(adapter: ModelAdapter) -> EcModel: - model = EcModel("toy", adapter=adapter) - - glc_e = cobra.Metabolite("glc_e", compartment="e", formula="C6H12O6") - eth_e = cobra.Metabolite("eth_e", compartment="e", formula="C2H6O") - byp_c = cobra.Metabolite("byp_c", compartment="c", formula="C1") - A_c = cobra.Metabolite("A_c", compartment="c") - pool = cobra.Metabolite("prot_pool", compartment="c") - prot_E = cobra.Metabolite("prot_E", compartment="c") - bio_met = cobra.Metabolite("bio_met", compartment="c") - model.add_metabolites([glc_e, eth_e, byp_c, A_c, pool, prot_E, bio_met]) - - EX_glc = cobra.Reaction("EX_glc") - EX_glc.add_metabolites({glc_e: -1.0}) - EX_glc.bounds = (-1000.0, 0.0) - - EX_eth = cobra.Reaction("EX_eth") - EX_eth.add_metabolites({eth_e: -1.0}) - EX_eth.bounds = (-1000.0, 0.0) - - EX_byp = cobra.Reaction("EX_byp") - EX_byp.add_metabolites({byp_c: -1.0}) - EX_byp.bounds = (0.0, 1000.0) - - TR_glc = cobra.Reaction("TR_glc") - TR_glc.add_metabolites({glc_e: -1.0, A_c: 1.0}) - TR_glc.bounds = (0.0, 1000.0) - - TR_eth = cobra.Reaction("TR_eth") - TR_eth.add_metabolites({eth_e: -1.0, A_c: 1.0}) - TR_eth.bounds = (0.0, 1000.0) - - coeff = _MW / (_KCAT * 3600.0) # mg/mmol per unit flux - R = cobra.Reaction("R") - R.add_metabolites( - {A_c: -1.0, prot_E: -coeff, bio_met: 1.0, byp_c: _BYPRODUCT_YIELD} - ) - R.bounds = (0.0, 1000.0) - - BIO = cobra.Reaction("biomass") - BIO.add_metabolites({bio_met: -1.0}) - BIO.bounds = (0.0, 1000.0) - - pool_ex = cobra.Reaction("prot_pool_exchange") - pool_ex.add_metabolites({pool: 1.0}) - pool_ex.bounds = (0.0, 1.0) # 1 mg/gDW pool - - usage_E = cobra.Reaction("usage_prot_E") - usage_E.add_metabolites({pool: -1.0, prot_E: 1.0}) - usage_E.bounds = (0.0, 1000.0) - - model.add_reactions( - [EX_glc, EX_eth, EX_byp, TR_glc, TR_eth, R, BIO, pool_ex, usage_E] - ) - model.objective = "biomass" - return model - - -def _flux_data(*, ethanol_grrate: float) -> FluxData: - """Two conditions: glucose (enzyme-limited, everything matches - exactly) and ethanol (carbon-limited; ``ethanol_grrate`` controls - whether the measured growth matches the simulated 20.0 exactly or - deliberately deviates from it).""" - return FluxData( - conds=["glucose", "ethanol"], - p_tot=np.array([np.nan, np.nan]), - gr_rate=np.array([_ENZYME_CAP, ethanol_grrate]), - exch_fluxes=np.array( - [ - [-_ENZYME_CAP, np.nan], - [np.nan, -20.0], - ] - ), - exch_mets=["glucose", "ethanol"], - exch_rxn_ids=["EX_glc", "EX_eth"], - weight=np.array([1.0, 1.0]), - source=["test", "test"], - ) - - -def _max_growth_data() -> FluxData: - """Max-growth dataset: uptake fully opened, so both conditions hit - the same enzyme-capped growth of 36.""" - return FluxData( - conds=["glucose", "ethanol"], - p_tot=np.array([np.nan, np.nan]), - gr_rate=np.array([_ENZYME_CAP, _ENZYME_CAP]), - exch_fluxes=np.array( - [ - [-1000.0, np.nan], - [np.nan, -1000.0], - ] - ), - exch_mets=["glucose", "ethanol"], - exch_rxn_ids=["EX_glc", "EX_eth"], - ) - - -# --------------------------------------------------------------------------- # -# simulate_conditions -# --------------------------------------------------------------------------- # - -def test_simulate_flux_data_enzyme_vs_carbon_limited(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - flux_data = _flux_data(ethanol_grrate=20.0) - - results = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - - assert len(results) == 2 - glucose, ethanol = results - - # Glucose: enzyme-limited growth of 36, full carbon uptake used. - assert glucose.feasible - assert glucose.growth == pytest.approx(_ENZYME_CAP) - assert glucose.exch_fluxes["EX_glc"] == pytest.approx(-_ENZYME_CAP) - assert glucose.block_fluxes["EX_byp"] == pytest.approx( - _ENZYME_CAP * _BYPRODUCT_YIELD - ) - - # Ethanol: carbon-limited to the -20 uptake cap, growth = 20. - assert ethanol.feasible - assert ethanol.growth == pytest.approx(20.0) - assert ethanol.exch_fluxes["EX_eth"] == pytest.approx(-20.0) - assert ethanol.block_fluxes["EX_byp"] == pytest.approx(20.0 * _BYPRODUCT_YIELD) - - # Bounds fully revert after the call (both `with model:` blocks exited). - assert model.reactions.get_by_id("EX_glc").bounds == (-1000.0, 0.0) - assert model.reactions.get_by_id("EX_eth").bounds == (-1000.0, 0.0) - - -def test_simulate_max_growth_opens_uptake_fully(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - max_growth = _max_growth_data() - - results = simulate_conditions( - model, max_growth, - constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", - ) - - assert [r.growth for r in results] == pytest.approx([_ENZYME_CAP, _ENZYME_CAP]) - # max_growth rows carry no exchange-flux measurements to report back. - assert results[0].exch_fluxes == {} - assert results[0].block_fluxes == {} - - -def test_simulate_unmatched_condition_name_raises(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - flux_data = _flux_data(ethanol_grrate=20.0) - flux_data.conds = ["glucose", "propionate"] - - with pytest.raises(ValueError, match="propionate"): - simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=[], bio_rxn_id="biomass", - ) - - -# --------------------------------------------------------------------------- # -# compute_excarbon -# --------------------------------------------------------------------------- # - -def test_compute_excarbon(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - assert excarbon["EX_glc"] == pytest.approx(6.0) # C6H12O6 - assert excarbon["EX_eth"] == pytest.approx(2.0) # C2H6O - assert excarbon["EX_byp"] == pytest.approx(1.0) # C1 - assert excarbon["biomass"] == pytest.approx(BIOMASS_CARBON_EQUIV) - - -def test_compute_excarbon_zero_carbon_clamps_to_one(tmp_path): - """A genuinely zero-carbon exchanged metabolite (e.g. O2) still - gets weight 1, not 0 -- MATLAB's `excarbon(excarbon==0)=1` quirk, - ported deliberately so those reactions' mismatches aren't silently - excluded from the RMSE.""" - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - o2_e = cobra.Metabolite("o2_e", compartment="e", formula="O2") - model.add_metabolites([o2_e]) - EX_o2 = cobra.Reaction("EX_o2") - EX_o2.add_metabolites({o2_e: -1.0}) - EX_o2.bounds = (-1000.0, 0.0) - model.add_reactions([EX_o2]) - - excarbon = compute_excarbon(model, ["EX_o2"], bio_rxn_id="biomass") - - assert excarbon["EX_o2"] == pytest.approx(1.0) - - -# --------------------------------------------------------------------------- # -# dataset_rmse / experiment_distance -# --------------------------------------------------------------------------- # - -def test_dataset_rmse_measured_terms_match_only_byproduct_deviates(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - # ethanol_grrate=20.0 matches the ethanol condition's actual - # carbon-limited growth exactly, and both conditions' own measured - # exchange fluxes match the simulation exactly too -- the only - # nonzero term left is the unmeasured byproduct (assumed - # zero-flux, but nonzero in the simulation). - flux_data = _flux_data(ethanol_grrate=20.0) - sims = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - rmse, per_cond = dataset_rmse( - flux_data, sims, - constrain=True, excarbon=excarbon, bio_rxn_id="biomass", - ) - - # The byproduct term is nonzero-simulated-vs-zero-measured, so RMSE - # is not zero overall -- but every *measured* exchange/growth term - # matches exactly, and the byproduct term is hand-computable. - glucose_byp = _ENZYME_CAP * _BYPRODUCT_YIELD * 1.0 # carbon weight 1 - ethanol_byp = 20.0 * _BYPRODUCT_YIELD * 1.0 - # 3 terms per condition: growth (matches exactly -> 0), the - # condition's own exchange flux (matches exactly -> 0), and the - # byproduct block term (measured 0 vs the nonzero simulated value). - expected_glucose_rmse = np.sqrt(np.mean([0.0, 0.0, glucose_byp ** 2])) - expected_ethanol_rmse = np.sqrt(np.mean([0.0, 0.0, ethanol_byp ** 2])) - - assert per_cond[0] == pytest.approx(expected_glucose_rmse) - assert per_cond[1] == pytest.approx(expected_ethanol_rmse) - assert rmse == pytest.approx(np.mean([expected_glucose_rmse, expected_ethanol_rmse])) - - -def test_dataset_rmse_growth_mismatch_is_hand_computable(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - # Deliberately wrong measured growth for the ethanol condition - # (18.0 vs the actual carbon-limited 20.0) -- a known, hand-provable - # deviation on top of the byproduct term. - flux_data = _flux_data(ethanol_grrate=18.0) - sims = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - _, per_cond = dataset_rmse( - flux_data, sims, - constrain=True, excarbon=excarbon, bio_rxn_id="biomass", - ) - - growth_diff = (18.0 - 20.0) * BIOMASS_CARBON_EQUIV # bio_meas - bio_sim - byp_diff = 0.0 - (20.0 * _BYPRODUCT_YIELD) # measured 0 vs simulated - expected = np.sqrt(np.mean([growth_diff ** 2, 0.0, byp_diff ** 2])) - assert per_cond[1] == pytest.approx(expected) - - -def test_dataset_rmse_infeasible_condition_gets_penalty(tmp_path): - flux_data = _flux_data(ethanol_grrate=20.0) - sims = [ConditionSimResult(feasible=False), ConditionSimResult(feasible=False)] - excarbon = {} - - rmse, per_cond = dataset_rmse( - flux_data, sims, - constrain=True, excarbon=excarbon, bio_rxn_id="biomass", penalty=99.0, - ) - - assert list(per_cond) == [99.0, 99.0] - assert rmse == pytest.approx(99.0) - - -def test_dataset_rmse_max_growth_scores_growth_only(): - max_growth = _max_growth_data() - sims = [ - ConditionSimResult(feasible=True, growth=_ENZYME_CAP), - ConditionSimResult(feasible=True, growth=30.0), # deliberately off - ] - - rmse, per_cond = dataset_rmse( - max_growth, sims, - constrain=False, excarbon={}, bio_rxn_id="biomass", - ) - - assert per_cond[0] == pytest.approx(0.0) - expected_second = abs((_ENZYME_CAP - 30.0) * BIOMASS_CARBON_EQUIV) - assert per_cond[1] == pytest.approx(expected_second) - assert rmse == pytest.approx(np.mean([0.0, expected_second])) - - -def test_experiment_distance_combines_both_datasets(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - flux_data = _flux_data(ethanol_grrate=20.0) - max_growth = _max_growth_data() - experiment_data = ExperimentData(flux_data=flux_data, max_growth=max_growth, zero_flux=["EX_byp"]) - - flux_sims = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - max_growth_sims = simulate_conditions( - model, max_growth, - constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", - ) - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - rmse, detail = experiment_distance( - experiment_data, - flux_sims=flux_sims, max_growth_sims=max_growth_sims, - excarbon=excarbon, bio_rxn_id="biomass", - ) - - expected_flux_rmse, _ = dataset_rmse( - flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id="biomass", - ) - expected_max_growth_rmse, _ = dataset_rmse( - max_growth, max_growth_sims, constrain=False, excarbon=excarbon, - bio_rxn_id="biomass", - ) - assert rmse == pytest.approx( - np.mean([expected_flux_rmse, expected_max_growth_rmse]) - ) - assert "flux_data" in detail and "max_growth" in detail - - -def test_experiment_distance_max_growth_weight_scales_the_max_growth_term(tmp_path): - """``max_growth_weight`` scales the max-growth term, so 2 makes the - eight max-growth conditions count double against the 33 flux ones.""" - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - flux_data = _flux_data(ethanol_grrate=20.0) - max_growth = _max_growth_data() - experiment_data = ExperimentData(flux_data=flux_data, max_growth=max_growth, zero_flux=["EX_byp"]) - - flux_sims = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - max_growth_sims = simulate_conditions( - model, max_growth, - constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", - ) - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - expected_flux_rmse, _ = dataset_rmse( - flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id="biomass", - ) - expected_max_growth_rmse, _ = dataset_rmse( - max_growth, max_growth_sims, constrain=False, excarbon=excarbon, - bio_rxn_id="biomass", - ) - - rmse, _ = experiment_distance( - experiment_data, - flux_sims=flux_sims, max_growth_sims=max_growth_sims, - excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=2.0, - ) - assert rmse == pytest.approx( - (expected_flux_rmse + 2.0 * expected_max_growth_rmse) / 3.0 - ) - - # 0.5 reproduces MATLAB's maxGrowthWeight = 2, which weights flux. - rmse_matlab, _ = experiment_distance( - experiment_data, - flux_sims=flux_sims, max_growth_sims=max_growth_sims, - excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=0.5, - ) - assert rmse_matlab == pytest.approx( - (2.0 * expected_flux_rmse + expected_max_growth_rmse) / 3.0 - ) - - # A lone dataset carries the whole score whatever the weight. - solo = ExperimentData(flux_data=flux_data, max_growth=None, zero_flux=["EX_byp"]) - rmse_solo, _ = experiment_distance( - solo, - flux_sims=flux_sims, max_growth_sims=None, - excarbon=excarbon, bio_rxn_id="biomass", max_growth_weight=2.0, - ) - assert rmse_solo == pytest.approx(expected_flux_rmse) - - -def test_experiment_distance_missing_dataset_contributes_nothing(tmp_path): - adapter = _adapter(tmp_path) - model = _build_toy(adapter) - flux_data = _flux_data(ethanol_grrate=20.0) - experiment_data = ExperimentData(flux_data=flux_data, max_growth=None, zero_flux=["EX_byp"]) - - flux_sims = simulate_conditions( - model, flux_data, - constrain=True, zero_flux_rxns=["EX_byp"], bio_rxn_id="biomass", - ) - excarbon = compute_excarbon( - model, ["EX_glc", "EX_eth", "EX_byp", "biomass"], bio_rxn_id="biomass", - ) - - rmse, detail = experiment_distance( - experiment_data, - flux_sims=flux_sims, max_growth_sims=None, - excarbon=excarbon, bio_rxn_id="biomass", - ) - - expected_flux_rmse, _ = dataset_rmse( - flux_data, flux_sims, constrain=True, excarbon=excarbon, bio_rxn_id="biomass", - ) - assert rmse == pytest.approx(expected_flux_rmse) - assert set(detail) == {"flux_data"} diff --git a/tests/test_experiment_data.py b/tests/test_experiment_data.py deleted file mode 100644 index 8b4c176b..00000000 --- a/tests/test_experiment_data.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Tests for experiments.data.""" -from pathlib import Path - -from geckopy import ModelAdapter -from geckopy.experiments import ( - ExperimentData, - load_experiment_data, -) - - -def _adapter(tmp_path: Path) -> ModelAdapter: - (tmp_path / "model_adapter.toml").write_text( - 'conv_gem = "dummy.xml"\n' - 'org_name = "test"\n' - ) - return ModelAdapter.from_folder(tmp_path) - - -_FLUX_DATA_TSV = ( - "Condition\tPtot\tgrRate\tglucose (r_1714)\tethanol (r_1761)\t" - "weight\tsource\n" - "glucose\tNaN\t0.36\t-13.33\t19.83\t1\tDLKcat\n" - "ethanol\tNaN\t0.12\tNaN\t-8.5\t1\tbrenda\n" -) - -_MAX_GROWTH_TSV = ( - "Condition\tPtot\tgrRate\tglucose (r_1714)\tfructose (r_1709)\t" - "weight\tsource\n" - "glucose\tNaN\t0.41\t-1000\tNaN\t1\tDLKcat\n" - "fructose\tNaN\t0.338\tNaN\t-1000\t1\tDLKcat\n" -) - -_ZERO_EXCH_TSV = "Rxns\nr_1542\nr_1545\nr_1546\n" - - -def test_load_experiment_data_all_files_present(tmp_path): - adapter = _adapter(tmp_path) - data_dir = tmp_path / "data" - data_dir.mkdir() - (data_dir / "fluxData.tsv").write_text(_FLUX_DATA_TSV) - (data_dir / "maxGrowth.tsv").write_text(_MAX_GROWTH_TSV) - (data_dir / "zeroFlux.tsv").write_text(_ZERO_EXCH_TSV) - - experiment_data = load_experiment_data(adapter) - - assert isinstance(experiment_data, ExperimentData) - - assert experiment_data.flux_data is not None - assert experiment_data.flux_data.conds == ["glucose", "ethanol"] - assert experiment_data.flux_data.exch_rxn_ids == ["r_1714", "r_1761"] - assert experiment_data.flux_data.source == ["DLKcat", "brenda"] - assert list(experiment_data.flux_data.weight) == [1.0, 1.0] - - assert experiment_data.max_growth is not None - assert experiment_data.max_growth.conds == ["glucose", "fructose"] - assert experiment_data.max_growth.exch_rxn_ids == ["r_1714", "r_1709"] - - assert experiment_data.zero_flux == ["r_1542", "r_1545", "r_1546"] - - -def test_load_experiment_data_all_files_missing(tmp_path): - adapter = _adapter(tmp_path) - - experiment_data = load_experiment_data(adapter) - - assert experiment_data.flux_data is None - assert experiment_data.max_growth is None - assert experiment_data.zero_flux == [] - - -def test_load_experiment_data_partial(tmp_path): - """Only flux_data present: max_growth stays None (not a bogus - partial struct), matching MATLAB's *intended* behaviour for a - missing optional file -- unlike the asymmetric-guard bug flagged - in the MATLAB REVIEW.md, both fields are treated identically.""" - adapter = _adapter(tmp_path) - data_dir = tmp_path / "data" - data_dir.mkdir() - (data_dir / "fluxData.tsv").write_text(_FLUX_DATA_TSV) - - experiment_data = load_experiment_data(adapter) - - assert experiment_data.flux_data is not None - assert experiment_data.max_growth is None - assert experiment_data.zero_flux == [] diff --git a/tests/test_load_flux_data.py b/tests/test_load_flux_data.py deleted file mode 100644 index d83d9093..00000000 --- a/tests/test_load_flux_data.py +++ /dev/null @@ -1,339 +0,0 @@ -"""Tests for load_flux_data.""" -from pathlib import Path - -import numpy as np -import pytest - -from geckopy.databases import FluxData, load_flux_data - - -# --------------------------------------------------------------------------- # -# Fixture builders -# --------------------------------------------------------------------------- # - -def _write_tsv(path: Path, rows: list[list[str]]) -> None: - path.write_text( - "\n".join("\t".join(r) for r in rows) + "\n", - encoding="utf-8", - ) - - -# --------------------------------------------------------------------------- # -# Pre-flight -# --------------------------------------------------------------------------- # - -def test_missing_file_raises(tmp_path): - with pytest.raises(FileNotFoundError, match="fluxData file"): - load_flux_data(tmp_path / "missing.tsv") - - -def test_empty_file_raises(tmp_path): - p = tmp_path / "empty.tsv" - p.write_text("", encoding="utf-8") - with pytest.raises(ValueError, match="empty"): - load_flux_data(p) - - -def test_short_header_raises(tmp_path): - """Header with fewer than 3 columns -> error.""" - p = tmp_path / "short.tsv" - _write_tsv(p, [["only", "two"]]) - with pytest.raises(ValueError, match="header has"): - load_flux_data(p) - - -def test_no_exchange_columns_raises(tmp_path): - """Header with exactly 3 columns (no exchanges) -> error.""" - p = tmp_path / "no_exch.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate"], - ["c1", "0.5", "0.4"], - ]) - with pytest.raises(ValueError, match="no exchange-flux columns"): - load_flux_data(p) - - -# --------------------------------------------------------------------------- # -# Basic happy path -# --------------------------------------------------------------------------- # - -def test_old_weight_column_name_is_rejected(tmp_path): - f = tmp_path / "fluxData.tsv" - _write_tsv(f, [ - ["Condition", "Ptot", "grRate", "glucose (r_1714)", "evotuneRMSEweight"], - ["c1", "0.5", "0.4", "-10", "1"], - ]) - with pytest.raises(ValueError, match="now called 'weight'"): - load_flux_data(f) - - -def test_weight_column_is_read_and_removed_from_the_fluxes(tmp_path): - f = tmp_path / "fluxData.tsv" - _write_tsv(f, [ - ["Condition", "Ptot", "grRate", "glucose (r_1714)", "weight", "source"], - ["c1", "0.5", "0.4", "-10", "0", "x"], - ["c2", "0.5", "0.3", "-8", "2", "y"], - ]) - data = load_flux_data(f) - assert data.weight.tolist() == [0.0, 2.0] - assert data.exch_rxn_ids == ["r_1714"] - - -def test_basic_single_condition(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glucose (EX_glc)"], - ["c1", "0.5", "0.4", "-10.0"], - ]) - fd = load_flux_data(p) - assert isinstance(fd, FluxData) - assert fd.conds == ["c1"] - np.testing.assert_array_equal(fd.p_tot, [0.5]) - np.testing.assert_array_equal(fd.gr_rate, [0.4]) - np.testing.assert_array_equal(fd.exch_fluxes, [[-10.0]]) - assert fd.exch_mets == ["glucose"] - assert fd.exch_rxn_ids == ["EX_glc"] - - -def test_multi_condition_multi_exchange(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", - "glucose (EX_glc)", "oxygen (EX_O2)", "ethanol (EX_eth)"], - ["c1", "0.5", "0.4", "-10.0", "-2.0", "5.0"], - ["c2", "0.6", "0.5", "-8.5", "-3.0", "7.0"], - ]) - fd = load_flux_data(p) - assert fd.conds == ["c1", "c2"] - np.testing.assert_array_equal(fd.p_tot, [0.5, 0.6]) - np.testing.assert_array_equal(fd.gr_rate, [0.4, 0.5]) - np.testing.assert_array_equal( - fd.exch_fluxes, - [[-10.0, -2.0, 5.0], [-8.5, -3.0, 7.0]], - ) - assert fd.exch_mets == ["glucose", "oxygen", "ethanol"] - assert fd.exch_rxn_ids == ["EX_glc", "EX_O2", "EX_eth"] - - -# --------------------------------------------------------------------------- # -# Exchange header parsing -# --------------------------------------------------------------------------- # - -def test_exchange_header_with_spaces_in_met_name(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "carbon dioxide (EX_co2)"], - ["c1", "0.5", "0.4", "5.0"], - ]) - fd = load_flux_data(p) - assert fd.exch_mets == ["carbon dioxide"] - assert fd.exch_rxn_ids == ["EX_co2"] - - -def test_exchange_header_without_parens_passes_through(tmp_path): - """Header without `(...)` falls back to met=rxn=full header.""" - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "EX_glc"], - ["c1", "0.5", "0.4", "-10.0"], - ]) - fd = load_flux_data(p) - assert fd.exch_mets == ["EX_glc"] - assert fd.exch_rxn_ids == ["EX_glc"] - - -# --------------------------------------------------------------------------- # -# NaN handling -# --------------------------------------------------------------------------- # - -def test_empty_flux_cell_yields_nan(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glc (EX_glc)", "o2 (EX_O2)"], - ["c1", "0.5", "0.4", "-10.0", ""], - ["c2", "0.6", "0.5", "", "-3.0"], - ]) - fd = load_flux_data(p) - assert np.isnan(fd.exch_fluxes[0, 1]) - assert np.isnan(fd.exch_fluxes[1, 0]) - assert fd.exch_fluxes[0, 0] == -10.0 - assert fd.exch_fluxes[1, 1] == -3.0 - - -def test_non_numeric_flux_yields_nan(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glc (EX_glc)"], - ["c1", "0.5", "0.4", "NA"], - ]) - fd = load_flux_data(p) - assert np.isnan(fd.exch_fluxes[0, 0]) - - -# --------------------------------------------------------------------------- # -# Optional columns -# --------------------------------------------------------------------------- # - -def test_no_optional_columns_yields_none(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glc (EX_glc)"], - ["c1", "0.5", "0.4", "-10.0"], - ]) - fd = load_flux_data(p) - assert fd.weight is None - assert fd.source is None - - -def test_weight_column_loaded(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", - "glc (EX_glc)", "weight"], - ["c1", "0.5", "0.4", "-10.0", "1.5"], - ["c2", "0.6", "0.5", "-8.5", "0.7"], - ]) - fd = load_flux_data(p) - assert fd.weight is not None - np.testing.assert_array_equal(fd.weight, [1.5, 0.7]) - # Tuning-weight column dropped from exch_fluxes. - assert fd.exch_fluxes.shape == (2, 1) - assert fd.exch_rxn_ids == ["EX_glc"] - - -def test_source_column_loaded(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glc (EX_glc)", "source"], - ["c1", "0.5", "0.4", "-10.0", "PMID:12345"], - ["c2", "0.6", "0.5", "-8.5", "in-house"], - ]) - fd = load_flux_data(p) - assert fd.source == ["PMID:12345", "in-house"] - assert fd.exch_fluxes.shape == (2, 1) - - -def test_both_optional_columns(tmp_path): - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", - "glc (EX_glc)", "weight", "source"], - ["c1", "0.5", "0.4", "-10.0", "1.5", "PMID:12345"], - ]) - fd = load_flux_data(p) - assert fd.weight is not None - np.testing.assert_array_equal(fd.weight, [1.5]) - assert fd.source == ["PMID:12345"] - assert fd.exch_fluxes.shape == (1, 1) - - -def test_optional_columns_in_arbitrary_position(tmp_path): - """Optional columns can appear before, between, or after the - exchange columns; they are pulled out independently of position.""" - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", - "source", "glc (EX_glc)", "weight"], - ["c1", "0.5", "0.4", "PMID:1", "-10.0", "1.5"], - ]) - fd = load_flux_data(p) - assert fd.source == ["PMID:1"] - assert fd.weight is not None - np.testing.assert_array_equal(fd.weight, [1.5]) - np.testing.assert_array_equal(fd.exch_fluxes, [[-10.0]]) - assert fd.exch_rxn_ids == ["EX_glc"] - - -# --------------------------------------------------------------------------- # -# Blank lines -# --------------------------------------------------------------------------- # - -def test_blank_lines_skipped(tmp_path): - p = tmp_path / "fd.tsv" - p.write_text( - "condition\tPtot\tgrRate\tglc (EX_glc)\n" - "\n" - "c1\t0.5\t0.4\t-10.0\n" - "\n", - encoding="utf-8", - ) - fd = load_flux_data(p) - assert fd.conds == ["c1"] - - -# --------------------------------------------------------------------------- # -# Integration with apply_flux_data_constraints -# --------------------------------------------------------------------------- # - -def test_loaded_data_can_feed_apply_flux_data_constraints(tmp_path): - """End-to-end: load a fluxData.tsv, constrain a model with it.""" - import cobra - from scipy import sparse - - from geckopy import EcModel, ModelAdapter - from geckopy.ec_model.ec_data import EcData - from geckopy.limit_proteins import apply_flux_data_constraints - - (tmp_path / "model_adapter.toml").write_text( - 'conv_gem = "dummy.xml"\n' - 'org_name = "test"\n' - 'bio_rxn = "biomass"\n' - 'c_source = "EX_glc"\n' - ) - adapter = ModelAdapter.from_folder(tmp_path) - - # Build a tiny model with one exchange and biomass. - model = EcModel("test", adapter=adapter) - glc = cobra.Metabolite("glc_e", compartment="e") - bio = cobra.Metabolite("bio", compartment="c") - model.add_metabolites([glc, bio]) - EX_glc = cobra.Reaction("EX_glc") - EX_glc.add_metabolites({glc: -1.0}) - EX_glc.lower_bound = -1000.0 - EX_glc.upper_bound = 0.0 - BIO = cobra.Reaction("biomass") - BIO.add_metabolites({bio: 1.0}) - BIO.lower_bound = 0.0 - BIO.upper_bound = 1000.0 - model.add_reactions([EX_glc, BIO]) - model.ec = EcData( - rxns=[], kcat=np.empty(0), source=[], notes=[], eccodes=[], - rxn_enz_mat=sparse.csr_matrix((0, 0)), - ) - - # Write a fluxData.tsv with a different exchange (so c_source gets zeroed). - p = tmp_path / "fd.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "glucose (EX_glc)"], - ["c1", "0.5", "0.4", "-5.0"], - ]) - fd = load_flux_data(p) - apply_flux_data_constraints(model, fd) - assert model.reactions.get_by_id("EX_glc").lower_bound == -5.0 - assert model.reactions.get_by_id("biomass").upper_bound == pytest.approx(0.4) - - -# --------------------------------------------------------------------------- # -# Malformed rows and headers -# --------------------------------------------------------------------------- # - -def test_ragged_row_raises(tmp_path): - p = tmp_path / "ragged.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "Glc (EX_a)", "O2 (EX_b)"], - ["c1", "0.5", "0.4", "-1", "2"], - ["c2", "0.5", "0.4", "-1"], - ]) - with pytest.raises(ValueError, match="row 3 has 4 cell"): - load_flux_data(p) - - -def test_nested_parentheses_in_reaction_id(tmp_path): - p = tmp_path / "nested.tsv" - _write_tsv(p, [ - ["condition", "Ptot", "grRate", "X (EX_a(e))", "O2 (EX_b)"], - ["c1", "0.5", "0.4", "-1", "2"], - ]) - fd = load_flux_data(p) - assert fd.exch_mets == ["X", "O2"] - assert fd.exch_rxn_ids == ["EX_a(e)", "EX_b"] diff --git a/tests/test_mask_size.py b/tests/test_mask_size.py index 40e26097..f0251099 100644 --- a/tests/test_mask_size.py +++ b/tests/test_mask_size.py @@ -13,9 +13,8 @@ import cobra from geckopy import EcModel, ModelAdapter from geckopy.adapter.params import EvotuneParams, SourceGroupRule -from geckopy.databases.flux_data import FluxData +from raven_toolbox.experiments import ExperimentData, FluxData from geckopy.ec_model.ec_data import EcData -from geckopy.experiments.data import ExperimentData from geckopy.kcat_tuning.evotune.mask_size import ( MaskSizePoint, recommend_target_impact_share, diff --git a/tests/test_mw.py b/tests/test_mw.py deleted file mode 100644 index a6cb2b30..00000000 --- a/tests/test_mw.py +++ /dev/null @@ -1,169 +0,0 @@ -"""Tests for calculate_mw.""" -import logging -import math - -import pytest - -from geckopy.databases import calculate_mw -from geckopy.databases.mw import ( - _RESIDUE_MASSES, - _STANDARD_RESIDUE_MASSES, - _WATER_MASS, - _X_MASS, -) - - -# --------------------------------------------------------------------------- # -# Trivial cases -# --------------------------------------------------------------------------- # - -def test_empty_sequence_returns_nan(): - # An empty / all-skipped sequence has no residues; NaN avoids being - # mistaken for an 18 Da "protein". - assert math.isnan(calculate_mw("")) - assert math.isnan(calculate_mw(" ")) - assert math.isnan(calculate_mw("123")) - - -def test_single_residue(): - assert calculate_mw("A") == pytest.approx(_WATER_MASS + 71.08) - - -def test_dipeptide(): - expected = _WATER_MASS + 71.08 + 137.14 - assert calculate_mw("AH") == pytest.approx(expected) - - -# --------------------------------------------------------------------------- # -# Known reference cases -# --------------------------------------------------------------------------- # - -def test_methionine_only(): - """Met has mass 131.20.""" - assert calculate_mw("M") == pytest.approx(_WATER_MASS + 131.20) - - -def test_repeated_residues_count_correctly(): - expected = _WATER_MASS + 5 * 71.08 - assert calculate_mw("AAAAA") == pytest.approx(expected) - - -def test_all_20_standard_residues(): - """A sequence of one of each of the 20 standard residues should - give water + sum of all 20 masses.""" - seq = "".join(sorted(_STANDARD_RESIDUE_MASSES.keys())) - expected = _WATER_MASS + sum(_STANDARD_RESIDUE_MASSES.values()) - assert calculate_mw(seq) == pytest.approx(expected) - - -# --------------------------------------------------------------------------- # -# Non-standard codes -# --------------------------------------------------------------------------- # - -def test_b_is_average_of_d_and_n(): - expected = (_RESIDUE_MASSES["D"] + _RESIDUE_MASSES["N"]) / 2 - assert _RESIDUE_MASSES["B"] == pytest.approx(expected) - - -def test_z_is_average_of_e_and_q(): - expected = (_RESIDUE_MASSES["E"] + _RESIDUE_MASSES["Q"]) / 2 - assert _RESIDUE_MASSES["Z"] == pytest.approx(expected) - - -def test_j_equals_l_and_i(): - assert _RESIDUE_MASSES["J"] == _RESIDUE_MASSES["L"] == _RESIDUE_MASSES["I"] - - -def test_o_pyrrolysine(): - assert _RESIDUE_MASSES["O"] == pytest.approx(255.31) - - -def test_u_selenocysteine(): - assert _RESIDUE_MASSES["U"] == pytest.approx(150.04) - - -def test_x_is_mean_of_20_standard_residues(): - expected = sum(_STANDARD_RESIDUE_MASSES.values()) / 20 - assert _X_MASS == pytest.approx(expected) - # Sanity: it should differ from the MATLAB historical 126.50. - assert abs(_X_MASS - 126.50) > 0.5 - - -# --------------------------------------------------------------------------- # -# Case insensitivity -# --------------------------------------------------------------------------- # - -def test_lowercase_is_treated_same_as_uppercase(): - assert calculate_mw("ahkc") == calculate_mw("AHKC") - - -def test_mixed_case_works(): - assert calculate_mw("AhKc") == calculate_mw("AHKC") - - -# --------------------------------------------------------------------------- # -# Whitespace and digits ignored silently -# --------------------------------------------------------------------------- # - -def test_whitespace_ignored(): - assert calculate_mw("A H") == calculate_mw("AH") - assert calculate_mw("\tA\nH\n") == calculate_mw("AH") - - -def test_digits_ignored(): - assert calculate_mw("A1H2") == calculate_mw("AH") - - -def test_whitespace_and_digits_do_not_warn(caplog): - with caplog.at_level(logging.WARNING): - calculate_mw("A H 1 2 K") - assert "skipped" not in caplog.text - - -# --------------------------------------------------------------------------- # -# Unknown characters: warn but do not raise -# --------------------------------------------------------------------------- # - -def test_unknown_character_warns(caplog): - with caplog.at_level(logging.WARNING): - calculate_mw("A@H") - assert "skipped" in caplog.text - assert "@" in caplog.text - - -def test_unknown_character_does_not_contribute_to_mass(caplog): - with caplog.at_level(logging.WARNING): - result = calculate_mw("A@H") - assert result == calculate_mw("AH") - - -def test_multiple_unknown_chars_warned_together(caplog): - with caplog.at_level(logging.WARNING): - calculate_mw("A@H#K?L") - assert "skipped" in caplog.text - assert "@" in caplog.text - assert "#" in caplog.text - assert "?" in caplog.text - - -# --------------------------------------------------------------------------- # -# Realistic sequence -# --------------------------------------------------------------------------- # - -def test_short_real_protein_mass(): - """A snippet of a real protein sequence should yield a plausible - mass in the low-kDa range, not just any positive number.""" - seq = "MENTGTLAGCDVRFGRSGGSGSGSGSAATAACK" - mw = calculate_mw(seq) - assert mw > 0 - # Approximately 33 residues at average ~110 g/mol = 3630 + water. - assert 3000 < mw < 4500 - - -# --------------------------------------------------------------------------- # -# Water mass precision -# --------------------------------------------------------------------------- # - -def test_water_mass_is_precise(): - """The water mass used is the precise average, 18.01528 Da.""" - assert _WATER_MASS == pytest.approx(18.01528) diff --git a/tests/test_optimize_model.py b/tests/test_optimize_model.py deleted file mode 100644 index b09ed428..00000000 --- a/tests/test_optimize_model.py +++ /dev/null @@ -1,62 +0,0 @@ -"""Tests for optimize_model.""" -import math - -import cobra -import pytest - -from geckopy.solver_support import optimize_model - - -def _infeasible_model(solver: str) -> cobra.Model: - model = cobra.Model("m") - a = cobra.Metabolite("A", compartment="c") - rxn = cobra.Reaction("R1") - rxn.add_metabolites({a: 1.0}) - rxn.bounds = (0.0, 10.0) - sink = cobra.Reaction("S1") - sink.add_metabolites({a: -1.0}) - sink.bounds = (20.0, 30.0) - model.add_reactions([rxn, sink]) - model.objective = "R1" - model.solver = solver - return model - - -@pytest.mark.parametrize("solver", ["glpk", "scipy"]) -def test_infeasible_lp_gives_an_infeasible_solution(solver): - solution = optimize_model(_infeasible_model(solver)) - assert solution.status == "infeasible" - - -def test_feasible_lp_matches_model_optimize(): - model = _infeasible_model("glpk") - model.reactions.get_by_id("S1").bounds = (0.0, 5.0) - solution = optimize_model(model) - assert solution.status == "optimal" - assert solution.objective_value == pytest.approx(5.0) - - -def test_gurobi_presolved_infeasible_lp_gives_an_infeasible_solution(): - """Gurobi holds no primal values for an infeasible LP that it presolved, which makes - ``model.optimize()`` raise ``GurobiError``.""" - try: - model = _infeasible_model("gurobi") - except Exception as exc: - pytest.skip(f"gurobi unusable: {exc}") - model.solver.problem.Params.Presolve = 2 - solution = optimize_model(model) - assert solution.status == "infeasible" - assert math.isnan(solution.objective_value) - assert set(solution.fluxes.index) == {"R1", "S1"} - - -def test_other_errors_are_raised(): - class Broken: - class solver: - status = "optimal" - - def optimize(self, **kwargs): - raise RuntimeError("boom") - - with pytest.raises(RuntimeError, match="boom"): - optimize_model(Broken()) diff --git a/tests/test_pubchem.py b/tests/test_pubchem.py deleted file mode 100644 index 3a4e41da..00000000 --- a/tests/test_pubchem.py +++ /dev/null @@ -1,389 +0,0 @@ -"""Tests for find_met_smiles (network mocked).""" -import logging - -import cobra - -from geckopy.databases import find_met_smiles - - -# --------------------------------------------------------------------------- # -# Fake HTTP layer -# --------------------------------------------------------------------------- # - -class _FakeResponse: - def __init__(self, status_code: int, text: str = ""): - self.status_code = status_code - self.text = text - - -def _install_fake_session(monkeypatch, name_to_response: dict): - """Replace _make_session so URL fetches return canned responses. - - `name_to_response` maps URL-escaped metabolite names to - (status_code, text). Names not in the map return 404. - """ - from geckopy.databases import pubchem as mod - - class _FakeSession: - def get(self, url, timeout=None): - for fragment, (status, text) in name_to_response.items(): - if fragment in url: - return _FakeResponse(status, text) - return _FakeResponse(404, "") - - monkeypatch.setattr(mod, "_make_session", lambda: _FakeSession()) - # Skip the inter-request sleep in tests. - monkeypatch.setattr(mod, "_INTER_REQUEST_SLEEP", 0.0) - - -def _build_model(metabolites: list[tuple[str, str]]) -> cobra.Model: - """Build a cobra.Model from (id, name) tuples.""" - model = cobra.Model("test") - for met_id, met_name in metabolites: - m = cobra.Metabolite(met_id, name=met_name, compartment="c") - model.add_metabolites([m]) - return model - - -def _smiles(model: cobra.Model, met_id: str) -> str: - return model.metabolites.get_by_id(met_id).annotation.get("smiles", "") - - -# --------------------------------------------------------------------------- # -# Cache hits -# --------------------------------------------------------------------------- # - -def test_uses_cache_without_network(monkeypatch, tmp_path): - """If a SMILES is in the cache, no network call happens.""" - cache_path = tmp_path / "smilesDB.tsv" - cache_path.write_text("glucose\tOC1OC(CO)C(O)C(O)C1O\n") - - model = _build_model([("glc_c", "glucose")]) - - def _fail(): - raise AssertionError("network should not be called") - - from geckopy.databases import pubchem as mod - monkeypatch.setattr(mod, "_make_session", _fail) - - find_met_smiles(model, cache_path=cache_path) - assert _smiles(model, "glc_c") == "OC1OC(CO)C(O)C(O)C1O" - - -def test_cache_with_empty_smiles_skips_network(monkeypatch, tmp_path): - """A cached empty result means 'we already know there is no SMILES'. - Should not retry the network for it.""" - cache_path = tmp_path / "smilesDB.tsv" - cache_path.write_text("unknown_compound\t\n") - - model = _build_model([("u_c", "unknown_compound")]) - - def _fail(): - raise AssertionError("network should not be called") - - from geckopy.databases import pubchem as mod - monkeypatch.setattr(mod, "_make_session", _fail) - - find_met_smiles(model, cache_path=cache_path) - assert _smiles(model, "u_c") == "" - - -# --------------------------------------------------------------------------- # -# Network lookups -# --------------------------------------------------------------------------- # - -def test_fetches_from_network_when_cache_missing(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "OC1OC(CO)C(O)C(O)C1O\n"), - }) - - model = _build_model([("glc_c", "glucose")]) - find_met_smiles(model, cache_path=cache_path) - - assert _smiles(model, "glc_c") == "OC1OC(CO)C(O)C(O)C1O" - - -def test_takes_first_line_when_multiple_returned(monkeypatch, tmp_path): - """PubChem may return multiple SMILES separated by newlines; the - function should keep only the first.""" - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "ambiguous": (200, "C1CCCCC1\nCCCCCCC\nCCCC\n"), - }) - - model = _build_model([("a_c", "ambiguous")]) - find_met_smiles(model, cache_path=cache_path) - - assert _smiles(model, "a_c") == "C1CCCCC1" - - -def test_404_means_no_smiles(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, {}) # everything 404s - - model = _build_model([("x_c", "unknown_compound")]) - find_met_smiles(model, cache_path=cache_path) - - assert _smiles(model, "x_c") == "" - - -def test_500_treated_as_no_smiles(monkeypatch, tmp_path): - """A 500 (often returned for slashes etc. in names) is treated - as 'no match' rather than a crash.""" - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "weird/name": (500, ""), - }) - model = _build_model([("w_c", "weird/name")]) - find_met_smiles(model, cache_path=cache_path) - assert _smiles(model, "w_c") == "" - - -# --------------------------------------------------------------------------- # -# Cache write-back -# --------------------------------------------------------------------------- # - -def test_appends_to_cache_after_fetch(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "OC1OC(CO)C(O)C(O)C1O\n"), - }) - model = _build_model([("glc_c", "glucose")]) - find_met_smiles(model, cache_path=cache_path) - - text = cache_path.read_text() - assert "glucose\tOC1OC(CO)C(O)C(O)C1O\n" in text - - -def test_caches_empty_result_too(monkeypatch, tmp_path): - """Even when nothing is found, the empty result is cached so we - do not retry next time.""" - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, {}) - - model = _build_model([("x_c", "unknown")]) - find_met_smiles(model, cache_path=cache_path) - - text = cache_path.read_text() - assert "unknown\t\n" in text - - -def test_existing_cache_preserved_when_appending(monkeypatch, tmp_path): - """Existing cache entries should not be lost.""" - cache_path = tmp_path / "smilesDB.tsv" - cache_path.write_text("water\tO\n") - - _install_fake_session(monkeypatch, { - "ethanol": (200, "CCO\n"), - }) - model = _build_model([ - ("h2o_c", "water"), - ("etoh_c", "ethanol"), - ]) - find_met_smiles(model, cache_path=cache_path) - - text = cache_path.read_text() - assert "water\tO\n" in text - assert "ethanol\tCCO\n" in text - - -# --------------------------------------------------------------------------- # -# Filters and skip conditions -# --------------------------------------------------------------------------- # - -def test_skips_prot_metabolites(monkeypatch, tmp_path): - """Metabolite names starting with 'prot_' must not be looked up.""" - cache_path = tmp_path / "smilesDB.tsv" - - requested_urls: list[str] = [] - - class _CaptureSession: - def get(self, url, timeout=None): - requested_urls.append(url) - return _FakeResponse(404, "") - - from geckopy.databases import pubchem as mod - monkeypatch.setattr(mod, "_make_session", lambda: _CaptureSession()) - monkeypatch.setattr(mod, "_INTER_REQUEST_SLEEP", 0.0) - - model = _build_model([ - ("prot_P1_c", "prot_P1"), - ("prot_pool_c", "prot_pool"), - ]) - find_met_smiles(model, cache_path=cache_path) - - assert requested_urls == [] - - -def test_skips_metabolites_with_existing_smiles(monkeypatch, tmp_path): - """If a metabolite already has annotation['smiles'], do not overwrite.""" - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "should_not_be_used\n"), - }) - - model = _build_model([("glc_c", "glucose")]) - model.metabolites.get_by_id("glc_c").annotation["smiles"] = "PRESERVE_ME" - - find_met_smiles(model, cache_path=cache_path) - - assert _smiles(model, "glc_c") == "PRESERVE_ME" - - -def test_same_name_in_multiple_compartments_gets_same_smiles( - monkeypatch, tmp_path, -): - """Glucose in cytoplasm and extracellular compartments share a - name and therefore one PubChem lookup; both metabolites should - receive the SMILES.""" - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "OC1OC(CO)C(O)C(O)C1O\n"), - }) - - model = cobra.Model("test") - m_c = cobra.Metabolite("glc_c", name="glucose", compartment="c") - m_e = cobra.Metabolite("glc_e", name="glucose", compartment="e") - model.add_metabolites([m_c, m_e]) - - find_met_smiles(model, cache_path=cache_path) - - assert _smiles(model, "glc_c") == "OC1OC(CO)C(O)C(O)C1O" - assert _smiles(model, "glc_e") == "OC1OC(CO)C(O)C(O)C1O" - - -def test_empty_model_does_not_crash(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - model = cobra.Model("empty") - find_met_smiles(model, cache_path=cache_path) - - -# --------------------------------------------------------------------------- # -# Cache file creation -# --------------------------------------------------------------------------- # - -def test_creates_cache_file_if_missing(monkeypatch, tmp_path): - cache_path = tmp_path / "subdir" / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "C\n"), - }) - model = _build_model([("glc_c", "glucose")]) - find_met_smiles(model, cache_path=cache_path) - assert cache_path.is_file() - - -# --------------------------------------------------------------------------- # -# URL escaping -# --------------------------------------------------------------------------- # - -def test_url_escapes_special_characters(monkeypatch, tmp_path): - """A name with spaces or slashes should be URL-encoded before being - inserted into the URL.""" - cache_path = tmp_path / "smilesDB.tsv" - captured: list[str] = [] - - class _CaptureSession: - def get(self, url, timeout=None): - captured.append(url) - return _FakeResponse(404, "") - - from geckopy.databases import pubchem as mod - monkeypatch.setattr(mod, "_make_session", lambda: _CaptureSession()) - monkeypatch.setattr(mod, "_INTER_REQUEST_SLEEP", 0.0) - - model = _build_model([("x", "L-alanyl-L-glycine")]) - find_met_smiles(model, cache_path=cache_path) - - assert len(captured) == 1 - # Hyphens are URL-safe; we just check the name is in the URL exactly - # (no spaces, no broken escaping). - assert "L-alanyl-L-glycine" in captured[0] - - -def test_url_escapes_spaces_and_slashes(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - captured: list[str] = [] - - class _CaptureSession: - def get(self, url, timeout=None): - captured.append(url) - return _FakeResponse(404, "") - - from geckopy.databases import pubchem as mod - monkeypatch.setattr(mod, "_make_session", lambda: _CaptureSession()) - monkeypatch.setattr(mod, "_INTER_REQUEST_SLEEP", 0.0) - - model = _build_model([("x", "weird name/here")]) - find_met_smiles(model, cache_path=cache_path) - - assert len(captured) == 1 - assert " " not in captured[0] - # Unescaped slash would still be valid in a URL but PubChem would - # interpret it as a path separator. Our quote(safe='') should - # escape it. - assert "weird%20name%2Fhere" in captured[0] - - -# --------------------------------------------------------------------------- # -# Logging -# --------------------------------------------------------------------------- # - -def test_logs_summary_after_run(monkeypatch, tmp_path, caplog): - cache_path = tmp_path / "smilesDB.tsv" - _install_fake_session(monkeypatch, { - "glucose": (200, "C\n"), - }) - model = _build_model([ - ("glc_c", "glucose"), - ("u_c", "unknown_compound"), - ]) - - with caplog.at_level(logging.INFO): - find_met_smiles(model, cache_path=cache_path) - - assert "SMILES found for" in caplog.text - - -# --------------------------------------------------------------------------- # -# Reading the annotation back -# --------------------------------------------------------------------------- # - -def test_met_smiles_accepts_the_shape_a_loaded_model_carries(): - """cobra returns a list for a model read from a file, a string for one - annotated in memory. Both mean the same molecule.""" - import cobra - - from geckopy.databases import met_smiles - - met = cobra.Metabolite("etoh") - met.annotation["smiles"] = "CCO" - assert met_smiles(met) == "CCO" - - met.annotation["smiles"] = ["CCO"] - assert met_smiles(met) == "CCO" - - met.annotation["smiles"] = [] - assert met_smiles(met) == "" - - del met.annotation["smiles"] - assert met_smiles(met) == "" - - -def test_duplicate_cache_name_uses_the_last_row(monkeypatch, tmp_path): - cache_path = tmp_path / "smilesDB.tsv" - cache_path.write_text("glucose\tFIRST\nglucose\tLAST\n") - model = _build_model([("glc_c", "glucose")]) - find_met_smiles(model, cache_path=cache_path) - assert _smiles(model, "glc_c") == "LAST" - - -def test_metabolite_without_name_is_skipped_with_a_warning(monkeypatch, tmp_path, caplog): - cache_path = tmp_path / "smilesDB.tsv" - cache_path.write_text("m1_c\tFROM_ID\nglucose\tCCO\n") - model = _build_model([("m1_c", ""), ("glc_c", "glucose")]) - with caplog.at_level(logging.WARNING): - find_met_smiles(model, cache_path=cache_path) - assert _smiles(model, "m1_c") == "" - assert _smiles(model, "glc_c") == "CCO" - assert any("m1_c" in r.getMessage() and "no name" in r.getMessage() for r in caplog.records) From ed26e19fb403f1d662274a4be81a6c40d1832bbc Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Fri, 20 Nov 2026 21:53:45 +0100 Subject: [PATCH 3/5] refactor: take the scipy solver support from raven-toolbox The functions of geckopy.solver_support are in raven_toolbox.utils.solver. EcModel still repairs the scipy problem whenever model.solver is read. --- CHANGELOG.md | 1 + src/geckopy/ec_model/ec_model.py | 6 +- src/geckopy/ec_model/enzyme.py | 2 +- src/geckopy/ec_model/make_ec_model.py | 4 +- src/geckopy/ec_model/pipeline/apply_kcat.py | 2 +- src/geckopy/kcat_tuning/evotune/tuning.py | 2 +- src/geckopy/kcat_tuning/sensitivity_tuning.py | 2 +- src/geckopy/kcat_tuning/sigma_fitter.py | 2 +- .../limit_proteins/flexibilize_enz_concs.py | 2 +- .../limit_proteins/get_conc_control_coeffs.py | 2 +- src/geckopy/limit_proteins/relax_enz_concs.py | 2 +- .../limit_proteins/relax_proteomics_greedy.py | 2 +- src/geckopy/utilities/bottlenecks.py | 2 +- src/geckopy/utilities/ec_fseof.py | 2 +- src/geckopy/utilities/ec_fva.py | 2 +- src/geckopy/utilities/get_subset_ec_model.py | 2 +- .../utilities/load_conventional_gem.py | 2 +- src/geckopy/utilities/load_ec_model.py | 2 +- tests/test_scipy_solver_support.py | 189 +----------------- 19 files changed, 29 insertions(+), 201 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcdc6693..069ea10e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * The experimental data (`load_experiment_data`, `ExperimentData`, `FluxData`, `load_flux_data`), the simulation of a condition (`simulate_conditions`) and its distance to the measurements (`experiment_distance`, `compute_carbon_weights`) are in `raven_toolbox.experiments`; geckopy imports them and `geckopy.databases` still exports `FluxData` and `load_flux_data`. `load_experiment_data` takes the data folder instead of the adapter. The evotune functions take `experiment_data` (was `evotune_data`) and `max_growth` (was `max_grate`), `FluxData.weight` is the per-condition weight (was `evotune_rmse_weight`) and the file column is `weight` (was `evotuneRMSEweight`, which now raises an error naming the new column). The files are `fluxData.tsv`, `maxGrowth.tsv` and `zeroFlux.tsv` (were `evotuneFluxData.tsv`, `evotuneMaxGrowth.tsv` and `evotuneZeroExch.tsv`); in the tutorial `fluxData.tsv` holds the condition of the protein step as its first row, with weight 0. * `apply_flux_data_constraints` takes the constraint logic from `raven_toolbox.experiments.apply_flux_data_constraints` and keeps the adapter defaults for `bio_rxn` and `c_source`. +* `geckopy.solver_support` is removed. Its functions are in `raven_toolbox.utils.solver` (`optimize_model`, `repair_scipy_problem`, `switch_to_scipy`, `edit_on_fast_solver`, `fast_solver_while_building`, `is_scipy_interface`, `constraint_shadow_prices`). * `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. diff --git a/src/geckopy/ec_model/ec_model.py b/src/geckopy/ec_model/ec_model.py index f2e58ea2..79b42aa7 100644 --- a/src/geckopy/ec_model/ec_model.py +++ b/src/geckopy/ec_model/ec_model.py @@ -25,7 +25,7 @@ import cobra -from ..solver_support import is_scipy_interface, repair_scipy_problem, switch_to_scipy +from raven_toolbox.utils.solver import is_scipy_interface, repair_scipy_problem, switch_to_scipy from .ec_data import EcData if TYPE_CHECKING: @@ -75,7 +75,7 @@ def __init__( @property def solver(self): """The optlang model. With the scipy interface, reading it also repairs the - problem's variable index map (see :mod:`geckopy.solver_support`), so values read + problem's variable index map (see :mod:`raven_toolbox.utils.solver`), so values read after any removal of variables come from the right columns.""" repair_scipy_problem(self) return cobra.Model.solver.fget(self) @@ -83,7 +83,7 @@ def solver(self): @solver.setter def solver(self, value) -> None: """Set the solver. Moving a model to the scipy interface goes through - :func:`geckopy.solver_support.switch_to_scipy`: optlang's clone grows the dense + :func:`raven_toolbox.utils.solver.switch_to_scipy`: optlang's clone grows the dense coefficient matrix row by row, which takes hours for a genome-scale model.""" if not self._contexts and is_scipy_interface(value) and not is_scipy_interface(self.problem): switch_to_scipy(self) diff --git a/src/geckopy/ec_model/enzyme.py b/src/geckopy/ec_model/enzyme.py index e82f11cc..d27effe7 100644 --- a/src/geckopy/ec_model/enzyme.py +++ b/src/geckopy/ec_model/enzyme.py @@ -36,7 +36,7 @@ import cobra -from ..solver_support import constraint_shadow_prices +from raven_toolbox.utils.solver import constraint_shadow_prices from .constants import PROT_PREFIX, USAGE_PREFIX from .pipeline.populate_ec import split_light_rxn_id diff --git a/src/geckopy/ec_model/make_ec_model.py b/src/geckopy/ec_model/make_ec_model.py index 39e22c5e..5048496b 100644 --- a/src/geckopy/ec_model/make_ec_model.py +++ b/src/geckopy/ec_model/make_ec_model.py @@ -31,7 +31,7 @@ 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 raven_toolbox.utils.solver import repair_scipy_problem, switch_to_scipy from .ec_model import EcModel from .pipeline import ( add_protein_pool_exchange_reaction, @@ -216,7 +216,7 @@ def make_ec_model( model = model.copy() # On the scipy solver the pipeline runs on glpk and the result is converted at the end: # optlang grows the scipy matrix densely, so the thousands of edits of a genome-scale - # build take hours there (see geckopy.solver_support). + # build take hours there (see raven_toolbox.utils.solver). on_scipy = repair_scipy_problem(model) if on_scipy: try: diff --git a/src/geckopy/ec_model/pipeline/apply_kcat.py b/src/geckopy/ec_model/pipeline/apply_kcat.py index 7dc3208e..ca25d4be 100644 --- a/src/geckopy/ec_model/pipeline/apply_kcat.py +++ b/src/geckopy/ec_model/pipeline/apply_kcat.py @@ -20,7 +20,7 @@ from ..ec_model import EcModel -from ...solver_support import edit_on_fast_solver +from raven_toolbox.utils.solver import edit_on_fast_solver from ..constants import POOL_ID, PROT_PREFIX from .populate_ec import split_light_rxn_id diff --git a/src/geckopy/kcat_tuning/evotune/tuning.py b/src/geckopy/kcat_tuning/evotune/tuning.py index f42dd2f1..acdd2119 100644 --- a/src/geckopy/kcat_tuning/evotune/tuning.py +++ b/src/geckopy/kcat_tuning/evotune/tuning.py @@ -81,7 +81,7 @@ ) from .parsimony import fold_change, n_changed from .priors import build_sigma0_log, classify_kcat_sources, warn_unlabelled -from ...solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from .tying import isozyme_tie_map with warnings.catch_warnings(): diff --git a/src/geckopy/kcat_tuning/sensitivity_tuning.py b/src/geckopy/kcat_tuning/sensitivity_tuning.py index fd32c480..b333945f 100644 --- a/src/geckopy/kcat_tuning/sensitivity_tuning.py +++ b/src/geckopy/kcat_tuning/sensitivity_tuning.py @@ -30,7 +30,7 @@ import numpy as np -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.constants import ( POOL_ID, PROT_PREFIX, diff --git a/src/geckopy/kcat_tuning/sigma_fitter.py b/src/geckopy/kcat_tuning/sigma_fitter.py index c47ecb51..f59f5e68 100644 --- a/src/geckopy/kcat_tuning/sigma_fitter.py +++ b/src/geckopy/kcat_tuning/sigma_fitter.py @@ -23,7 +23,7 @@ import numpy as np -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.pipeline.protein_pool import set_prot_pool_size if TYPE_CHECKING: diff --git a/src/geckopy/limit_proteins/flexibilize_enz_concs.py b/src/geckopy/limit_proteins/flexibilize_enz_concs.py index a9717fcc..a29a1bef 100644 --- a/src/geckopy/limit_proteins/flexibilize_enz_concs.py +++ b/src/geckopy/limit_proteins/flexibilize_enz_concs.py @@ -33,7 +33,7 @@ import numpy as np -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.constants import ( POOL_EXCHANGE_ID, USAGE_PREFIX, diff --git a/src/geckopy/limit_proteins/get_conc_control_coeffs.py b/src/geckopy/limit_proteins/get_conc_control_coeffs.py index b8eb9e6f..40dfd74b 100644 --- a/src/geckopy/limit_proteins/get_conc_control_coeffs.py +++ b/src/geckopy/limit_proteins/get_conc_control_coeffs.py @@ -14,7 +14,7 @@ from ..ec_model.ec_model import EcModel -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.constants import USAGE_PREFIX logger = logging.getLogger(__name__) diff --git a/src/geckopy/limit_proteins/relax_enz_concs.py b/src/geckopy/limit_proteins/relax_enz_concs.py index 794cd8fd..180557b9 100644 --- a/src/geckopy/limit_proteins/relax_enz_concs.py +++ b/src/geckopy/limit_proteins/relax_enz_concs.py @@ -24,7 +24,7 @@ import numpy as np -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.constants import POOL_EXCHANGE_ID, USAGE_PREFIX from .flexibilize_enz_concs import ( FlexEnzResult, diff --git a/src/geckopy/limit_proteins/relax_proteomics_greedy.py b/src/geckopy/limit_proteins/relax_proteomics_greedy.py index 1406721e..2c9e7e25 100644 --- a/src/geckopy/limit_proteins/relax_proteomics_greedy.py +++ b/src/geckopy/limit_proteins/relax_proteomics_greedy.py @@ -28,7 +28,7 @@ import numpy as np -from ..solver_support import constraint_shadow_prices +from raven_toolbox.utils.solver import constraint_shadow_prices if TYPE_CHECKING: from ..ec_model.ec_model import EcModel diff --git a/src/geckopy/utilities/bottlenecks.py b/src/geckopy/utilities/bottlenecks.py index d7d96dab..b7f9c373 100644 --- a/src/geckopy/utilities/bottlenecks.py +++ b/src/geckopy/utilities/bottlenecks.py @@ -15,7 +15,7 @@ import pandas as pd from ..ec_model.constants import POOL_EXCHANGE_ID -from ..solver_support import constraint_shadow_prices, optimize_model +from raven_toolbox.utils.solver import constraint_shadow_prices, optimize_model if TYPE_CHECKING: from ..ec_model.ec_model import EcModel diff --git a/src/geckopy/utilities/ec_fseof.py b/src/geckopy/utilities/ec_fseof.py index 7911f16a..5bf781b1 100644 --- a/src/geckopy/utilities/ec_fseof.py +++ b/src/geckopy/utilities/ec_fseof.py @@ -51,7 +51,7 @@ 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 +from raven_toolbox.utils.solver import optimize_model if TYPE_CHECKING: from ..ec_model.ec_model import EcModel diff --git a/src/geckopy/utilities/ec_fva.py b/src/geckopy/utilities/ec_fva.py index 2cf6ea85..5645e2a2 100644 --- a/src/geckopy/utilities/ec_fva.py +++ b/src/geckopy/utilities/ec_fva.py @@ -34,7 +34,7 @@ import pandas as pd from cobra.util import ProcessPool -from ..solver_support import optimize_model +from raven_toolbox.utils.solver import optimize_model from ..ec_model.constants import canonicalize_rxn_id try: diff --git a/src/geckopy/utilities/get_subset_ec_model.py b/src/geckopy/utilities/get_subset_ec_model.py index 7bde3877..d207de3a 100644 --- a/src/geckopy/utilities/get_subset_ec_model.py +++ b/src/geckopy/utilities/get_subset_ec_model.py @@ -13,7 +13,7 @@ import numpy as np from cobra.manipulation import remove_genes -from ..solver_support import edit_on_fast_solver +from raven_toolbox.utils.solver import edit_on_fast_solver from ..ec_model.constants import ( POOL_EXCHANGE_ID, PROT_PREFIX, diff --git a/src/geckopy/utilities/load_conventional_gem.py b/src/geckopy/utilities/load_conventional_gem.py index e3d99725..8ffe1379 100644 --- a/src/geckopy/utilities/load_conventional_gem.py +++ b/src/geckopy/utilities/load_conventional_gem.py @@ -24,7 +24,7 @@ from raven_toolbox.io import read_yaml_model from raven_toolbox.utils.solver import set_raven_tolerance -from ..solver_support import fast_solver_while_building, switch_to_scipy +from raven_toolbox.utils.solver import fast_solver_while_building, switch_to_scipy if TYPE_CHECKING: from ..adapter import ModelAdapter diff --git a/src/geckopy/utilities/load_ec_model.py b/src/geckopy/utilities/load_ec_model.py index 24a379c6..d39e140f 100644 --- a/src/geckopy/utilities/load_ec_model.py +++ b/src/geckopy/utilities/load_ec_model.py @@ -56,7 +56,7 @@ from raven_toolbox.io import EcData, read_yaml_model from ..ec_model.ec_model import EcModel -from ..solver_support import fast_solver_while_building, switch_to_scipy +from raven_toolbox.utils.solver import fast_solver_while_building, switch_to_scipy from ..upgrade import upgrade_ec_model if TYPE_CHECKING: diff --git a/tests/test_scipy_solver_support.py b/tests/test_scipy_solver_support.py index 8e4c102a..be632096 100644 --- a/tests/test_scipy_solver_support.py +++ b/tests/test_scipy_solver_support.py @@ -2,7 +2,7 @@ optlang's scipy interface renumbers its variable columns wrongly after a variable is removed, which makes cobra read fluxes from the wrong columns, and it raises for duals. geckopy repairs -the first and reads the HiGHS marginals for the second (see ``geckopy.solver_support``). +the first and reads the HiGHS marginals for the second (see ``raven_toolbox.utils.solver``). """ from pathlib import Path @@ -13,7 +13,7 @@ from geckopy import ModelAdapter, make_ec_model from geckopy.ec_model.pipeline import apply_kcat_constraints from geckopy.limit_proteins import constrain_enz_concs, relax_proteomics_greedy -from geckopy.solver_support import ( +from raven_toolbox.utils.solver import ( constraint_shadow_prices, edit_on_fast_solver, repair_scipy_problem, @@ -106,44 +106,6 @@ def test_removing_reactions_keeps_the_fluxes_right(scipy_default): assert model.reactions.get_by_id("R3").flux == pytest.approx(solution.objective_value, rel=1e-7) -def _ring(solver): - """A plain cobra model on ``solver`` with more variables than constraints.""" - config = cobra.Configuration() - previous = config.solver - config.solver = solver - try: - model = cobra.Model("plain") - mets = [cobra.Metabolite(f"m{i}") for i in range(3)] - rxns = [] - for i in range(6): - rxn = cobra.Reaction(f"r{i}") - rxn.bounds = (0, 10) - rxn.add_metabolites({mets[i % 3]: 1.0, mets[(i + 1) % 3]: -1.0} if i else {mets[0]: 1.0}) - rxns.append(rxn) - model.add_reactions(rxns) - finally: - config.solver = previous - model.objective = "r5" - return model - - -def test_a_plain_cobra_model_is_repaired_on_request(): - model, reference = _ring("scipy"), _ring("glpk") - for m in (model, reference): - m.remove_reactions(["r1", "r2"]) - m.reactions.get_by_id("r4").bounds = (0, 3) - assert repair_scipy_problem(model) - assert _columns_are_consistent(model) - assert model.slim_optimize() == pytest.approx(reference.slim_optimize()) - assert model.reactions.get_by_id("r5").flux == pytest.approx(reference.reactions.get_by_id("r5").flux) - - -def test_repair_does_nothing_on_other_solvers(): - model = cobra.Model("m") - model.solver = "glpk" - assert repair_scipy_problem(model) is False - - @pytest.mark.parametrize("reference", REFERENCE_SOLVERS) def test_ec_fva_on_scipy_equals_the_reference(reference, scipy_default): ref_model, ref_conv = _on(reference) @@ -219,76 +181,6 @@ def test_a_stored_result_from_before_a_change_is_not_read(scipy_default): assert before.shape == after.shape -def _columns_ring(solver): - """Three metabolites, twelve reactions: removals leave late variables with stale columns.""" - config = cobra.Configuration() - previous = config.solver - config.solver = solver - try: - model = cobra.Model("ring") - mets = [cobra.Metabolite(f"m{i}") for i in range(3)] - rxns = [] - for i in range(12): - rxn = cobra.Reaction(f"r{i}") - rxn.bounds = (0, 10 + i) - if i == 0: - rxn.add_metabolites({mets[0]: 1.0}) - else: - rxn.add_metabolites({mets[i % 3]: -1.0, mets[(i + 1) % 3]: 0.5 + 0.1 * i}) - rxns.append(rxn) - model.add_reactions(rxns) - finally: - config.solver = previous - model.objective = "r11" - return model - - -def test_removals_after_the_install_keep_every_column_right(): - model, reference = _columns_ring("scipy"), _columns_ring("glpk") - assert repair_scipy_problem(model) - for rid in ("r3", "r7", "r9", "r10"): - model.reactions.get_by_id(rid).delete() - reference.reactions.get_by_id(rid).delete() - assert _columns_are_consistent(model) - assert model.slim_optimize() == pytest.approx(reference.slim_optimize()) - solution = model.optimize() - expected = reference.optimize() - assert solution.objective_value == pytest.approx(expected.objective_value) - for rid in solution.fluxes.index: - assert model.reactions.get_by_id(rid).flux == pytest.approx(solution.fluxes[rid]) - - -def test_a_map_that_is_already_stale_is_rebuilt_from_the_expressions(): - model, reference = _columns_ring("scipy"), _columns_ring("glpk") - model.reactions.get_by_id("r5").delete() - reference.reactions.get_by_id("r5").delete() - model.solver.update() - assert not _columns_are_consistent(model) - assert repair_scipy_problem(model) - assert _columns_are_consistent(model) - assert model.slim_optimize() == pytest.approx(reference.slim_optimize()) - - -def test_the_sparse_problem_hands_highs_the_matrix_of_the_dense_problem(): - model = _columns_ring("scipy") - dense = model.solver.problem.A.copy() - assert repair_scipy_problem(model) - assert (model.solver.problem.A.toarray() == dense).all() - - -def test_switch_to_scipy_keeps_the_objective_and_its_direction(): - model, reference = _columns_ring("glpk"), _columns_ring("glpk") - for m in (model, reference): - m.objective = "r8" - m.objective_direction = "min" - m.reactions.get_by_id("r1").lower_bound = 2.0 - switch_to_scipy(model) - assert model.solver.interface.__name__.endswith("scipy_interface") - assert model.objective_direction == "min" - assert model.slim_optimize() == pytest.approx(reference.slim_optimize()) - assert model.reactions.get_by_id("r8").flux == pytest.approx(model.slim_optimize()) - - def test_load_conventional_gem_and_make_ec_model_end_on_scipy(scipy_default): from geckopy import load_conventional_gem @@ -313,40 +205,8 @@ def test_shadow_prices_need_an_optimal_solution(scipy_default): constraint_shadow_prices(model, [model.enzymes.get_by_id(model.ec.enzymes[0]).prot_metabolite_id]) -@pytest.mark.parametrize("status_case", ["infeasible", "unbounded"]) -def test_a_solve_without_optimum_returns_a_solution_with_its_status(status_case): - config = cobra.Configuration() - previous = config.solver - config.solver = "scipy" - try: - model = cobra.Model("chain") - met = cobra.Metabolite("m") - source, sink = cobra.Reaction("source"), cobra.Reaction("sink") - source.add_metabolites({met: 1.0}) - sink.add_metabolites({met: -1.0}) - source.bounds = (0, 10) if status_case == "infeasible" else (0, 1000.0) - sink.bounds = (20, 30) if status_case == "infeasible" else (0, 1000.0) - model.add_reactions([source, sink]) - finally: - config.solver = previous - model.objective = "sink" - if status_case == "unbounded": - source.upper_bound = float("inf") - sink.upper_bound = float("inf") - assert repair_scipy_problem(model) - if status_case == "unbounded": - # cobra raises for this status on every solver - with pytest.raises(cobra.exceptions.OptimizationError, match="unbounded"): - model.optimize(raise_error=False) - return - solution = model.optimize(raise_error=False) - assert solution.status == status_case - assert np.isnan(solution.objective_value) - assert solution.fluxes.isna().all() - - def test_setting_the_solver_of_an_ec_model_to_scipy_gives_the_sparse_problem(): - from geckopy.solver_support import _SparseProblem + from raven_toolbox.utils.solver import _SparseProblem model, _ = _build("glpk") expected = model.slim_optimize() @@ -357,7 +217,7 @@ def test_setting_the_solver_of_an_ec_model_to_scipy_gives_the_sparse_problem(): def test_load_ec_model_ends_on_the_sparse_scipy_problem(scipy_default, tmp_path): from geckopy import load_ec_model, save_ec_model - from geckopy.solver_support import _SparseProblem + from raven_toolbox.utils.solver import _SparseProblem model, _ = _build("glpk") expected = model.slim_optimize() @@ -374,7 +234,7 @@ def test_a_scipy_ec_model_pickles_and_copies_into_the_sparse_problem(how): import copy import pickle - from geckopy.solver_support import _SparseProblem + from raven_toolbox.utils.solver import _SparseProblem model, _ = _build("glpk") expected = model.slim_optimize() @@ -409,7 +269,7 @@ def spy(*args, **kwargs): return real(*args, **kwargs) for m in (model, pickle.loads(pickle.dumps(model)), model.copy()): - monkeypatch.setattr("geckopy.solver_support.linprog", spy) + monkeypatch.setattr("raven_toolbox.utils.solver.linprog", spy) m.slim_optimize() assert len(seen) == 3 assert all(o["primal_feasibility_tolerance"] == 1e-9 for o in seen) @@ -431,14 +291,14 @@ def numerical_trouble_when_tight(*args, **kwargs): return optimize.OptimizeResult(status=4, fun=None, x=None, slack=None) return real(*args, **kwargs) - monkeypatch.setattr("geckopy.solver_support.linprog", numerical_trouble_when_tight) + monkeypatch.setattr("raven_toolbox.utils.solver.linprog", numerical_trouble_when_tight) assert model.slim_optimize() == pytest.approx(expected) assert len(calls) == 2 def always_numerical_trouble(*args, **kwargs): return optimize.OptimizeResult(status=4, fun=None, x=None, slack=None) - monkeypatch.setattr("geckopy.solver_support.linprog", always_numerical_trouble) + monkeypatch.setattr("raven_toolbox.utils.solver.linprog", always_numerical_trouble) assert model.optimize(raise_error=False).status == "numeric" @@ -474,36 +334,3 @@ def test_get_subset_ec_model_on_scipy_equals_glpk(light): assert len(on_scipy.solver.constraints) == len(on_scipy.metabolites) assert on_scipy.slim_optimize() == pytest.approx(on_glpk.slim_optimize()) assert on_scipy.slim_optimize() > 0 - - -def test_removing_several_constraints_at_once_on_the_sparse_problem(): - model = _columns_ring("scipy") - repair_scipy_problem(model) - ids = [m.id for m in list(model.metabolites)[:3]] - model.remove_cons_vars([model.constraints[i] for i in ids]) - model.solver.update() - assert len(model.solver.problem._constraints) == 2 * len(model.constraints) - assert not set(ids) & set(model.constraints) - - -def test_edit_on_fast_solver_returns_to_scipy_with_the_same_problem(): - model = _columns_ring("scipy") - repair_scipy_problem(model) - before = model.slim_optimize() - with edit_on_fast_solver(model) as fast: - assert fast - assert model.solver.interface.__name__.endswith("glpk_interface") - model.reactions.get_by_id("r1").bounds = (0.0, 5.0) - assert model.solver.interface.__name__.endswith("scipy_interface") - assert model.reactions.get_by_id("r1").upper_bound == 5.0 - assert model.slim_optimize() <= before - reference = _columns_ring("glpk") - reference.reactions.get_by_id("r1").bounds = (0.0, 5.0) - assert model.slim_optimize() == pytest.approx(reference.slim_optimize()) - - -def test_edit_on_fast_solver_changes_nothing_on_glpk(): - model = _columns_ring("glpk") - with edit_on_fast_solver(model) as fast: - assert not fast - assert model.solver.interface.__name__.endswith("glpk_interface") From c015684c1081ded449523d8af52c767b25847e8c Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Fri, 20 Nov 2026 21:53:45 +0100 Subject: [PATCH 4/5] refactor: take calculate_mw and find_met_smiles from raven-toolbox They are in raven_toolbox.utils.protein and raven_toolbox.annotation, and geckopy.databases exports them from there. find_met_smiles needs cache_path. --- CHANGELOG.md | 1 + src/geckopy/databases/__init__.py | 4 ++-- src/geckopy/databases/kegg_download.py | 2 +- .../gather_kcats/open_kinetics_predictor/parse_output.py | 2 +- src/geckopy/gather_kcats/write_dlkcat_input.py | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 069ea10e..86b9614e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * The experimental data (`load_experiment_data`, `ExperimentData`, `FluxData`, `load_flux_data`), the simulation of a condition (`simulate_conditions`) and its distance to the measurements (`experiment_distance`, `compute_carbon_weights`) are in `raven_toolbox.experiments`; geckopy imports them and `geckopy.databases` still exports `FluxData` and `load_flux_data`. `load_experiment_data` takes the data folder instead of the adapter. The evotune functions take `experiment_data` (was `evotune_data`) and `max_growth` (was `max_grate`), `FluxData.weight` is the per-condition weight (was `evotune_rmse_weight`) and the file column is `weight` (was `evotuneRMSEweight`, which now raises an error naming the new column). The files are `fluxData.tsv`, `maxGrowth.tsv` and `zeroFlux.tsv` (were `evotuneFluxData.tsv`, `evotuneMaxGrowth.tsv` and `evotuneZeroExch.tsv`); in the tutorial `fluxData.tsv` holds the condition of the protein step as its first row, with weight 0. * `apply_flux_data_constraints` takes the constraint logic from `raven_toolbox.experiments.apply_flux_data_constraints` and keeps the adapter defaults for `bio_rxn` and `c_source`. * `geckopy.solver_support` is removed. Its functions are in `raven_toolbox.utils.solver` (`optimize_model`, `repair_scipy_problem`, `switch_to_scipy`, `edit_on_fast_solver`, `fast_solver_while_building`, `is_scipy_interface`, `constraint_shadow_prices`). +* `calculate_mw`, `find_met_smiles` and `met_smiles` are in raven-toolbox (`raven_toolbox.utils.protein` and `raven_toolbox.annotation`) and `geckopy.databases` exports them from there. `find_met_smiles` needs `cache_path`; it no longer defaults to `data/smilesDB.tsv` of the adapter. * `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. diff --git a/src/geckopy/databases/__init__.py b/src/geckopy/databases/__init__.py index e6a97123..708fcb0d 100644 --- a/src/geckopy/databases/__init__.py +++ b/src/geckopy/databases/__init__.py @@ -8,14 +8,14 @@ load_complex_portal_json, ) from .dlkcat_ignore_lists import DLKcatIgnoreLists, load_dlkcat_ignore_lists +from raven_toolbox.annotation import find_met_smiles, met_smiles from raven_toolbox.experiments import FluxData, load_flux_data +from raven_toolbox.utils.protein import calculate_mw from .kegg_download import download_kegg from .kegg_loader import KeggDB, load_kegg_tsv -from .mw import calculate_mw from .pax_db_loader import ProtData, load_pax_db from .phyl_dist import PhylDist, download_phyl_dist, load_phyl_dist from .prot_data_loader import load_prot_data -from .pubchem import find_met_smiles, met_smiles from .uniprot_download import download_uniprot from .uniprot_loader import UniprotDB, load_uniprot_tsv diff --git a/src/geckopy/databases/kegg_download.py b/src/geckopy/databases/kegg_download.py index 1bc44408..bbb7b03d 100644 --- a/src/geckopy/databases/kegg_download.py +++ b/src/geckopy/databases/kegg_download.py @@ -28,7 +28,7 @@ import requests -from .mw import calculate_mw +from raven_toolbox.utils.protein import calculate_mw logger = logging.getLogger(__name__) 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 8bc2e24e..969a2bf4 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/parse_output.py @@ -18,7 +18,7 @@ import numpy as np import pandas as pd -from ...databases.pubchem import met_smiles as read_met_smiles +from raven_toolbox.annotation import met_smiles as read_met_smiles if TYPE_CHECKING: from ...ec_model.ec_model import EcModel diff --git a/src/geckopy/gather_kcats/write_dlkcat_input.py b/src/geckopy/gather_kcats/write_dlkcat_input.py index 4ab4c5e7..6bd3f81f 100644 --- a/src/geckopy/gather_kcats/write_dlkcat_input.py +++ b/src/geckopy/gather_kcats/write_dlkcat_input.py @@ -16,7 +16,7 @@ import pandas as pd from scipy import sparse -from ..databases.pubchem import met_smiles as read_met_smiles +from raven_toolbox.annotation import met_smiles as read_met_smiles if TYPE_CHECKING: from ..databases.dlkcat_ignore_lists import DLKcatIgnoreLists From a7289c229c78a07dbbe08fd6fe9651ee18630002 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Fri, 20 Nov 2026 23:10:12 +0100 Subject: [PATCH 5/5] test: score the experiment data of the full_ecModel tutorial The first condition of the tutorial fluxData.tsv has weight 0 and matches no exchange metabolite, and scoring leaves it out. --- tests/test_tutorial_experiment_data.py | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/test_tutorial_experiment_data.py diff --git a/tests/test_tutorial_experiment_data.py b/tests/test_tutorial_experiment_data.py new file mode 100644 index 00000000..38bb4804 --- /dev/null +++ b/tests/test_tutorial_experiment_data.py @@ -0,0 +1,75 @@ +"""The experiment data of the full_ecModel tutorial can be scored. + +Its first condition, ``Std``, has weight 0: the protein step reads it, and the +tuning leaves it out. ``Std`` matches no exchange metabolite, so scoring has to skip it +before the conditions are matched to exchange reactions. +""" +import warnings +from pathlib import Path + +import cobra +import numpy as np +import pytest + +from raven_toolbox.experiments import ( + compute_carbon_weights, + dataset_rmse, + load_experiment_data, + simulate_conditions, +) + +TUTORIAL_DATA = Path(__file__).parents[1] / "tutorials" / "full_ecModel" / "data" + + +def _model_with_exchanges(rxn_ids): + """Every exchange reaction supplies its own metabolite, which a transport + reaction turns into the intermediate that biomass consumes.""" + model = cobra.Model("tutorial_exchanges") + inter = cobra.Metabolite("A_c", compartment="c") + reactions = [] + for rxn_id in rxn_ids: + met = cobra.Metabolite(f"m_{rxn_id}", compartment="e") + exchange = cobra.Reaction(rxn_id) + exchange.add_metabolites({met: -1.0}) + exchange.bounds = (-1000.0, 1000.0) + transport = cobra.Reaction(f"T_{rxn_id}") + transport.add_metabolites({met: -1.0, inter: 1.0}) + transport.bounds = (0.0, 1000.0) + reactions += [exchange, transport] + biomass = cobra.Reaction("biomass") + biomass.add_metabolites({inter: -1.0}) + biomass.bounds = (0.0, 1000.0) + model.add_reactions(reactions + [biomass]) + model.objective = "biomass" + return model + + +@pytest.mark.skipif(not TUTORIAL_DATA.is_dir(), reason="tutorial data not in this checkout") +def test_the_tutorial_data_is_scored_without_its_weight_zero_condition(): + data = load_experiment_data(TUTORIAL_DATA) + assert data.flux_data.conds[0] == "Std" and data.flux_data.weight[0] == 0 + rxn_ids = sorted(set(data.flux_data.exch_rxn_ids) | set(data.max_growth.exch_rxn_ids)) + model = _model_with_exchanges(rxn_ids) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + carbon_weights = compute_carbon_weights(model, rxn_ids + ["biomass"], bio_rxn_id="biomass") + + flux_sims = simulate_conditions( + model, data.flux_data, constrain=True, zero_flux_rxns=data.zero_flux, + bio_rxn_id="biomass", + ) + max_growth_sims = simulate_conditions( + model, data.max_growth, constrain=False, zero_flux_rxns=[], bio_rxn_id="biomass", + ) + rmse, per_cond = dataset_rmse( + data.flux_data, flux_sims, constrain=True, carbon_weights=carbon_weights, + bio_rxn_id="biomass", + ) + max_growth_rmse, _ = dataset_rmse( + data.max_growth, max_growth_sims, constrain=False, carbon_weights=carbon_weights, + bio_rxn_id="biomass", + ) + + assert flux_sims[0].skipped and not any(s.skipped for s in flux_sims[1:]) + assert np.isnan(per_cond[0]) and not np.isnan(per_cond[1:]).any() + assert np.isfinite(rmse) and np.isfinite(max_growth_rmse)