From 6f98dfd6b7464157630156b32c1b0ee028422314 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Tue, 17 Nov 2026 21:27:37 +0100 Subject: [PATCH 1/4] fix: always split the gene cell of uniprot.tsv into its gene names A gene cell such as 'YPR080W; YBR118W' is expanded into one row per gene name, split on semicolons and whitespace. The split_gene_cells option of load_uniprot_tsv is removed. --- CHANGELOG.md | 1 + src/geckopy/databases/uniprot_loader.py | 23 ++++-------- tests/test_uniprot_loader.py | 50 ++++++++++++------------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608cfc7..e99a6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* `load_uniprot_tsv` always splits a gene cell of uniprot.tsv with several gene names, separated by semicolons and/or whitespace (`YPR080W; YBR118W`), into one row per gene name; the other columns are repeated on every row and an empty cell stays one row. A model gene that is any of the names finds the entry. The `split_gene_cells` option is removed. GECKO's `loadDatabases` does the same. * The user guides (evotune, YAML format, relaxation methods, migration from GECKO MATLAB, kcat sources) moved to [gecko-docs](https://gecko-docs.readthedocs.io/en/latest/). The `docs/` folder is gone; the developer notes are in `dev/`. * `uniprot.id` and `kegg.id` are empty by default instead of the identifiers of Saccharomyces cerevisiae (`"559292"` and `"sce"`), as in GECKO; `adapterParameters.tsv` is the same as GECKO's. `geckopy init` writes both uncommented as `""`, with examples in the comments. `make_ec_model` and `calculate_f_factor` raise `FileNotFoundError` naming `[uniprot] id` when `data/uniprot.tsv` is missing and the identifier is empty, as GECKO's `loadDatabases` raises `loadDatabases:noUniprotID`; an empty `kegg.id` leaves KEGG out with a warning. A project that relied on the yeast defaults sets the identifiers in model_adapter.toml. * `upgrade_project` (`geckopy upgrade`) reports the calls of removed or replaced functions in the project's scripts with their replacement, for example `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values`. diff --git a/src/geckopy/databases/uniprot_loader.py b/src/geckopy/databases/uniprot_loader.py index 9d21d1b..3586d96 100644 --- a/src/geckopy/databases/uniprot_loader.py +++ b/src/geckopy/databases/uniprot_loader.py @@ -15,16 +15,20 @@ """ from __future__ import annotations +import re from dataclasses import dataclass, field from pathlib import Path import numpy as np +# Separates the gene names in a Gene Names cell. +_GENE_SEPARATOR = re.compile(r"[;\s]+") + @dataclass class UniprotDB: """Parsed uniprot.tsv. Arrays are aligned: row i in every field - describes the same database entry (or gene-name row when split). + describes one gene name of a database entry. ``mw`` is in Da, matching the Mass column of the TSV verbatim. """ @@ -55,7 +59,6 @@ def find_by_id(self, uniprot_id: str) -> int | None: def load_uniprot_tsv( path: str | Path, *, - split_gene_cells: bool = False, uniprot_id: str | int | None = None, id_type: str = "taxonomy_id", gene_id_field: str = "gene_oln", @@ -72,15 +75,6 @@ def load_uniprot_tsv( path Path to uniprot.tsv. May not exist if ``uniprot_id`` triggers an auto-download. - split_gene_cells - If True, rows whose Gene Names cell contains whitespace-separated - multiple names are expanded into one row per gene name. All other - fields (Entry, EC, MW, sequence) are duplicated across the - expanded rows. This matters for organisms where the UniProt - ``gene_oln`` field returns multiple space-separated ORF names - per entry: with the default of False, the cell is stored - verbatim (including any embedded whitespace), so an exact-match - lookup against a single ORF name silently misses those genes. uniprot_id UniProt-side identifier (e.g. NCBI taxonomy ID). Required for auto-download; otherwise unused. @@ -165,10 +159,9 @@ def load_uniprot_tsv( except ValueError: mass_da = float("nan") - if split_gene_cells and gene_cell.strip(): - gene_names = gene_cell.split() - else: - gene_names = [gene_cell] + gene_names = [name for name in _GENE_SEPARATOR.split(gene_cell) if name] + if not gene_names: + gene_names = [""] source_row_ids.append(entry) for gene_name in gene_names: diff --git a/tests/test_uniprot_loader.py b/tests/test_uniprot_loader.py index e41496c..e494974 100644 --- a/tests/test_uniprot_loader.py +++ b/tests/test_uniprot_loader.py @@ -143,25 +143,14 @@ def test_raises_on_duplicate_entry(tmp_path): # --------------------------------------------------------------------------- # -# split_gene_cells option +# Gene cell splitting # --------------------------------------------------------------------------- # -def test_split_gene_cells_default_is_false(tmp_path): - """Without splitting (the default), multi-gene cells are stored - verbatim as a single string.""" +def test_multi_gene_cell_is_one_row_per_gene(tmp_path): path = _write_tsv(tmp_path / "u.tsv", [ "P1\tG1 G2\t1.1.1.1\t10000\tMKAL", ]) db = load_uniprot_tsv(path) - assert db.genes == ["G1 G2"] - assert db.ids == ["P1"] - - -def test_split_gene_cells_expands_multi_gene_rows(tmp_path): - path = _write_tsv(tmp_path / "u.tsv", [ - "P1\tG1 G2\t1.1.1.1\t10000\tMKAL", - ]) - db = load_uniprot_tsv(path, split_gene_cells=True) assert db.genes == ["G1", "G2"] assert db.ids == ["P1", "P1"] @@ -170,38 +159,49 @@ def test_split_gene_cells_expands_multi_gene_rows(tmp_path): assert db.sequences == ["MKAL", "MKAL"] -def test_split_gene_cells_with_multiple_whitespace(tmp_path): - """Tabs and multiple spaces in the Gene Names cell all split.""" +def test_semicolon_separated_gene_cell_is_split(tmp_path): + path = _write_tsv(tmp_path / "u.tsv", [ + "P1\tYPR080W; YBR118W\t\t10000\tMKAL", + "P2\tG3;G4 ; G5;\t\t20000\tMNTD", + ]) + db = load_uniprot_tsv(path) + assert db.genes == ["YPR080W", "YBR118W", "G3", "G4", "G5"] + assert db.ids == ["P1", "P1", "P2", "P2", "P2"] + assert db.find_by_gene("YBR118W") == 1 + + +def test_gene_cell_with_multiple_whitespace_is_split(tmp_path): path = _write_tsv(tmp_path / "u.tsv", [ "P1\tG1 G2 G3\t\t10000\tMKAL", ]) - db = load_uniprot_tsv(path, split_gene_cells=True) + db = load_uniprot_tsv(path) assert db.genes == ["G1", "G2", "G3"] -def test_split_gene_cells_empty_cell_stays_empty(tmp_path): +def test_empty_gene_cell_stays_one_row(tmp_path): """An empty Gene Names cell produces a single empty-string row, not zero rows. This preserves alignment with the source TSV.""" path = _write_tsv(tmp_path / "u.tsv", [ "P1\t\t1.1.1.1\t10000\tMKAL", + "P2\t ; \t1.1.1.1\t10000\tMKAL", ]) - db = load_uniprot_tsv(path, split_gene_cells=True) - assert db.genes == [""] - assert db.ids == ["P1"] + db = load_uniprot_tsv(path) + assert db.genes == ["", ""] + assert db.ids == ["P1", "P2"] -def test_split_gene_cells_does_not_flag_repetition_as_duplicate(tmp_path): +def test_repeated_entry_from_splitting_is_not_a_duplicate(tmp_path): """Expanded rows share an Entry value, which is expected, not a duplicate.""" path = _write_tsv(tmp_path / "u.tsv", [ "P1\tG1 G2\t\t10000\tMKAL", "P2\tG3\t\t20000\tMNTD", ]) # Should not raise. - db = load_uniprot_tsv(path, split_gene_cells=True) + db = load_uniprot_tsv(path) assert len(db) == 3 -def test_split_gene_cells_still_detects_cross_row_duplicates(tmp_path): +def test_cross_row_duplicates_are_still_detected(tmp_path): """Duplicate Entry across distinct source rows must still raise.""" path = _write_tsv(tmp_path / "u.tsv", [ "P1\tG1 G2\t\t10000\tMKAL", @@ -209,7 +209,7 @@ def test_split_gene_cells_still_detects_cross_row_duplicates(tmp_path): "P1\tG4\t\t30000\tMSYN", # second occurrence of P1 ]) with pytest.raises(ValueError, match="Duplicate Entry"): - load_uniprot_tsv(path, split_gene_cells=True) + load_uniprot_tsv(path) # --------------------------------------------------------------------------- # @@ -257,7 +257,7 @@ def test_quoted_gene_cell_is_split_without_quotes(tmp_path): path = _write_tsv(tmp_path / "u.tsv", [ 'P1\t"G1 G2"\t\t10000\tMKAL', ]) - db = load_uniprot_tsv(path, split_gene_cells=True) + db = load_uniprot_tsv(path) assert db.genes == ["G1", "G2"] From c52ec8555b42c16d4ec0d580038fa8f2af9c267f Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Tue, 17 Nov 2026 21:27:37 +0100 Subject: [PATCH 2/4] doc: name fillKcatsFromIsozymes in the fill_kcats docstring --- src/geckopy/ec_model/pipeline/fill_kcats.py | 8 ++++---- tests/test_gecko_matlab_parity.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/geckopy/ec_model/pipeline/fill_kcats.py b/src/geckopy/ec_model/pipeline/fill_kcats.py index 584155f..eeeb8ae 100644 --- a/src/geckopy/ec_model/pipeline/fill_kcats.py +++ b/src/geckopy/ec_model/pipeline/fill_kcats.py @@ -1,7 +1,7 @@ """Fill missing kcats by averaging across isozymes. Ported from GECKO MATLAB: -src/change_model/getKcatAcrossIsozymes.m. +src/change_model/fillKcatsFromIsozymes.m. """ from __future__ import annotations @@ -47,9 +47,9 @@ def fill_kcats_from_isozymes( For each reaction with no kcat assigned (``ec.kcat == 0``), find its siblings whose kcat is set (``> 0``). If any exist, take their mean and assign it. Set ``ec.source`` to ``"isozymes"`` for the - filled entries. Only a kcat of 0 is filled, as in ``getKcatAcrossIsozymes``: + filled entries. Only a kcat of 0 is filled, as in ``fillKcatsFromIsozymes``: a NaN kcat is not a missing kcat and is left as it is. A NaN sibling - does not contribute to the mean here; in ``getKcatAcrossIsozymes`` it + does not contribute to the mean here; in ``fillKcatsFromIsozymes`` it does, so there the mean is NaN and the row is not filled. Reactions whose siblings all also lack a kcat stay at 0. Reactions @@ -63,7 +63,7 @@ def fill_kcats_from_isozymes( apply If True, call ``apply_kcat_constraints`` after updating ec.kcat so the new values reflect in the S matrix. Default False, as - in ``getKcatAcrossIsozymes``, which only changes ``ec.kcat``. + in ``fillKcatsFromIsozymes``, which only changes ``ec.kcat``. aggregate How to combine sibling kcats: ``"mean"`` (matches MATLAB GECKO), ``"median"`` (more robust for log-distributed rate constants), or diff --git a/tests/test_gecko_matlab_parity.py b/tests/test_gecko_matlab_parity.py index abbc069..b41941e 100644 --- a/tests/test_gecko_matlab_parity.py +++ b/tests/test_gecko_matlab_parity.py @@ -599,7 +599,7 @@ def test_tc0010_fuzzy_light_subset(light_ec_model): # tc0011 -- testKcats # # Covers writeDLKcatInput, mergeDLKcatAndFuzzyKcats, selectKcatValue, -# applyKcatConstraints, getKcatAcrossIsozymes and applyCustomKcats, plus +# applyKcatConstraints, fillKcatsFromIsozymes and applyCustomKcats, plus # the full-vs-light growth-rate equivalence. # ========================================================================== # From 039eb8d32cdeb783e8746e60fec8228bd0e16ec0 Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Tue, 17 Nov 2026 21:27:56 +0100 Subject: [PATCH 3/4] test: gene cells with several names in the EC and ecModel matching --- tests/test_ec_from_database.py | 35 ++++++++++++++++++++++++++++++++++ tests/test_make_ec_model.py | 26 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/test_ec_from_database.py b/tests/test_ec_from_database.py index 1881912..ea7ccf4 100644 --- a/tests/test_ec_from_database.py +++ b/tests/test_ec_from_database.py @@ -446,3 +446,38 @@ def test_no_kegg_arg_preserves_original_behaviour(tmp_path): uniprot = _uniprot([]) fill_eccodes_from_database(model, uniprot) assert model.ec.eccodes == [""] + + +# --------------------------------------------------------------------------- # +# Gene cells with several names +# --------------------------------------------------------------------------- # + +def test_gene_matches_second_name_of_a_gene_cell(tmp_path): + from geckopy.databases import load_uniprot_tsv + + tsv = tmp_path / "uniprot.tsv" + tsv.write_text( + "Entry\tGene Names\tEC number\tMass\tSequence\n" + "P1\tg0; g1\t1.1.1.1\t100\tMK\n" + ) + adapter = _minimal_adapter(tmp_path) + model = _build_ec_model(adapter, ["r1"], ["g1"], [[0]]) + fill_eccodes_from_database(model, load_uniprot_tsv(tsv)) + assert model.ec.eccodes == ["1.1.1.1"] + + +def test_gene_in_two_entries_is_handled_as_with_separate_rows(tmp_path, caplog): + from geckopy.databases import load_uniprot_tsv + + tsv = tmp_path / "uniprot.tsv" + tsv.write_text( + "Entry\tGene Names\tEC number\tMass\tSequence\n" + "P1\tg0; g1\t1.1.1.1\t100\tMK\n" + "P2\tg1\t2.2.2.2\t200\tMK\n" + ) + adapter = _minimal_adapter(tmp_path) + model = _build_ec_model(adapter, ["r1"], ["g1"], [[0]]) + with caplog.at_level(logging.WARNING): + fill_eccodes_from_database(model, load_uniprot_tsv(tsv)) + assert model.ec.eccodes == ["1.1.1.1"] + assert "g1" in caplog.text diff --git a/tests/test_make_ec_model.py b/tests/test_make_ec_model.py index 9ff944b..b5f968a 100644 --- a/tests/test_make_ec_model.py +++ b/tests/test_make_ec_model.py @@ -420,3 +420,29 @@ def test_make_ec_model_without_kegg_id_leaves_the_gene_unmatched(tmp_path): model, adapter, db = _toy_with_missing_gene(tmp_path) ec_model = make_ec_model(model, adapter, uniprot_db=db) assert sorted(ec_model.ec.enzymes) == ["P1"] + + +def test_make_ec_model_gene_matches_second_name_of_a_gene_cell(tmp_path): + (tmp_path / "model_adapter.toml").write_text( + 'conv_gem = "dummy.xml"\norg_name = "test"\nenzyme_comp = "c"\n' + ) + adapter = ModelAdapter.from_folder(tmp_path) + model = cobra.Model("m") + model.compartments = {"c": "c"} + m_a = cobra.Metabolite("A", compartment="c") + m_b = cobra.Metabolite("B", compartment="c") + model.add_metabolites([m_a, m_b]) + r = cobra.Reaction("r1") + r.add_metabolites({m_a: -1, m_b: 1}) + r.lower_bound, r.upper_bound = 0.0, 1000.0 + r.gene_reaction_rule = "g2" + model.add_reactions([r]) + + tsv = tmp_path / "uniprot.tsv" + tsv.write_text( + "Entry\tGene Names\tEC number\tMass\tSequence\n" + "P1\tg1; g2\t\t10000\tMK\n" + ) + ec_model = make_ec_model(model, adapter, uniprot_db=load_uniprot_tsv(tsv)) + assert ec_model.ec.genes == ["g2"] + assert ec_model.ec.enzymes == ["P1"] From db92e205589da389948969780219206d42efccda Mon Sep 17 00:00:00 2001 From: Eduard Kerkhoven Date: Tue, 17 Nov 2026 22:03:52 +0100 Subject: [PATCH 4/4] refactor: name the OKP setting [kcat_sources.okp] method --- CHANGELOG.md | 8 ++-- src/geckopy/adapter/adapterParameters.tsv | 2 +- src/geckopy/adapter/params.py | 26 ++++++------ src/geckopy/adapter/show.py | 8 ++-- src/geckopy/adapter/template.py | 2 +- src/geckopy/gather_kcats/kcat_assignment.py | 41 +++++++++---------- src/geckopy/gather_kcats/kcat_sources.py | 28 ++++++++----- src/geckopy/gather_kcats/kcats_from.py | 15 ++++--- .../open_kinetics_predictor/run.py | 4 +- tests/test_adapter.py | 21 +++++----- tests/test_cli.py | 2 +- tests/test_combine_kcats.py | 36 ++++++++++------ tests/test_kcat_assignment.py | 20 ++++----- tests/test_kcat_sources.py | 12 +++--- tutorials/full_ecModel/protocol.py | 8 ++-- 15 files changed, 126 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e99a6dd..fc0c76d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,15 @@ * `uniprot.id` and `kegg.id` are empty by default instead of the identifiers of Saccharomyces cerevisiae (`"559292"` and `"sce"`), as in GECKO; `adapterParameters.tsv` is the same as GECKO's. `geckopy init` writes both uncommented as `""`, with examples in the comments. `make_ec_model` and `calculate_f_factor` raise `FileNotFoundError` naming `[uniprot] id` when `data/uniprot.tsv` is missing and the identifier is empty, as GECKO's `loadDatabases` raises `loadDatabases:noUniprotID`; an empty `kegg.id` leaves KEGG out with a warning. A project that relied on the yeast defaults sets the identifiers in model_adapter.toml. * `upgrade_project` (`geckopy upgrade`) reports the calls of removed or replaced functions in the project's scripts with their replacement, for example `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `truncate_values`. * The deprecated aliases with GECKO 3 names are removed from `geckopy`: `run_dlkcat`, `read_dlkcat_output`, `write_dlkcat_input`, `apply_custom_kcats`, `get_kcat_across_isozymes`, `get_standard_kcat`, `select_kcat_value`, `merge_dlkcat_and_fuzzy_kcats`, `get_ec_from_database`, `get_ec_from_gem`, `sigma_fitter` and `constrain_flux_data`, and `find_max_value`, which ports `findMaxValue` that GECKO 4 removed. `upgrade_project` (`geckopy upgrade`) reports each of them in a project's scripts with its replacement, which `geckopy.upgrade.RENAMED` lists. `extract_enzyme_substrate_pairs` stays public. -* The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H alone by default, instead of the eight methods with combination parameters; a project that sets `[kcat_sources.okp] methods` keeps its list. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. -* `okp.method` is removed from model_adapter.toml; `[kcat_sources.okp] methods` is the only setting that names OpenKineticsPredictor methods (default `["KinForm-H"]`). `kcats_from_okp` without `method` uses the first entry of `methods`; it used `okp.method`. `[kcat_sources.okp_] method` is removed: the method of the file `okp_` is the known method whose file name is `okp_` (`okp_` and the method in lower case, each run of other characters than letters and digits as `_`), and `update_kcat_sources` reports any other `okp_` as failed (` is not the file of a known OKP method; the files are ...`). `Unknown OKP methods in [kcat_sources.okp] methods: ...` lists the names comma-separated. `CatPred` and `TurNup` are no longer accepted in `[kcat_sources.okp] methods`: `kcats_from_okp` rejects them, so the configuration now fails early with `Unknown OKP methods`. +* The `okp` source group of the presets is the OpenKineticsPredictor method KinForm-H by default; `[kcat_sources.okp] method` sets another. `update_kcat_sources` then gathers `okp_kinform_h.tsv` only. GECKO uses the same default. +* `[kcat_sources.okp] method` is the only setting that names the OpenKineticsPredictor method (default `"KinForm-H"`); `[okp]` has no `method` key. `kcats_from_okp` without `method` uses it. `[kcat_sources.okp_] method` is removed: the method of the file `okp_` is the known method whose file name is `okp_` (`okp_` and the method in lower case, each run of other characters than letters and digits as `_`), and `update_kcat_sources` reports any other `okp_` as failed (` is not the file of a known OKP method; the files are ...`). `Unknown OKP method in [kcat_sources.okp] method; supported: ` lists the supported methods comma-separated. `CatPred` and `TurNup` are not accepted as `[kcat_sources.okp] method`: `kcats_from_okp` rejects them, so the configuration fails early with `Unknown OKP method`. * Evotune source groups are one table each, `[evotune.source_groups.]` with `sources` and `sigma0_log`, both required. `[evotune.sigma0_log_source]` and `SourceGroupRule.match_okp` are removed, and so is the `okp_method` argument of `classify_kcat_source`, `classify_kcat_sources`, `screen_kcat_leverage`, `cmaes_kcat_tuning`, `sweep_tunable_mask_size` and the other tuning functions; a group covers OpenKineticsPredictor kcats with a pattern such as `okp_*`. `sigma0_log` is a standard deviation of the natural log of the kcat. A `sources` entry ending in `*` is a prefix pattern on the origin (case-insensitive); exact matches over all groups are tried first, then patterns, the longest prefix first, and groups of equal length in file order. Without `[evotune.source_groups]` the groups are `custom` 0.1, `brenda` 0.2, `ec_tree` 0.3 and `combined` 0.4 (they were `dlkcat` 0.4, `brenda` 0.2 and `custom` 0.1); groups in the file replace the defaults entirely. Tuning warns when more than 20 % of the tunable kcats are in no group: ` of tunable kcats match no evotune source group and get sigma0_log_default (); origins: `. * `kcat_aggregate_brenda` is removed from model_adapter.toml (and `ModelParameters`); `[kcat_sources.brenda] aggregation` is the only BRENDA aggregation setting. `kcats_from_brenda` and `fuzzy_kcat_matching` without an explicit aggregation use it as the kcat assignment config resolves it: median for the default preset `brenda_ec_tree` and the other median presets, max for `gecko_classic`, and max when neither the preset nor the file sets it. They used `kcat_aggregate_brenda`, which defaulted to `"max"`, so a call without an aggregation under the default preset now takes the median. * `kcat_aggregate_candidates` and `kcat_aggregate_isozymes` are removed from model_adapter.toml (and `ModelParameters`). `apply_kcat_list` takes `criteria="max"` and `fill_kcats_from_isozymes` takes `aggregate="mean"` as their defaults, as before; they no longer read an adapter value. * `gr_exp` has no default (it was `0.41`, the yeast growth rate). `sensitivity_tuning`, `fit_sigma`, `flexibilize_enz_concs` and `relax_enz_concs` called without a growth rate raise `ValueError: gr_exp is not set in model_adapter.toml` when the file does not set it; `geckopy init` writes it commented out, noting that the functions that tune kcats or fit sigma require it. A project that relied on `0.41` sets `gr_exp`. -* An unknown key in model_adapter.toml is an error, whatever its place: `ModelAdapter.from_folder` raises `ValueError` with `: unknown key in model_adapter.toml`. This includes keys under `evotune.source_groups.`, the `kcat_assignment.stage` entries and `kcat_sources.`. The keys of `[kcat_sources.]` are `aggregation` and `force_wildcard_level` for `brenda`, `k` for `ec_tree`, `k`, `min_identity` and `coverage` for `homologs`, `methods` for `okp`, and none for `custom`, `dlkcat` and `okp_`; another source name is an unknown key. A malformed evotune group raises `: evotune.source_groups. must be a table with sources and sigma0_log`, `... needs both sources and sigma0_log`, `... .sources must be a list of strings` or `... .sigma0_log must be a positive number`. The removed keys are unknown keys: no alias or warning remains for them. +* An unknown key in model_adapter.toml is an error, whatever its place: `ModelAdapter.from_folder` raises `ValueError` with `: unknown key in model_adapter.toml`. This includes keys under `evotune.source_groups.`, the `kcat_assignment.stage` entries and `kcat_sources.`. The keys of `[kcat_sources.]` are `aggregation` and `force_wildcard_level` for `brenda`, `k` for `ec_tree`, `k`, `min_identity` and `coverage` for `homologs`, `method` for `okp`, and none for `custom`, `dlkcat` and `okp_`; another source name is an unknown key. A malformed evotune group raises `: evotune.source_groups. must be a table with sources and sigma0_log`, `... needs both sources and sigma0_log`, `... .sources must be a list of strings` or `... .sigma0_log must be a positive number`. The removed keys are unknown keys: no alias or warning remains for them. * `geckopy adapter show [FOLDER]` (default: the current folder) and `show_adapter_params(adapter)` print one line per item in the order of `adapterParameters.tsv`: ` = []` with the origin `file`, `default`, `preset ` or `not set` (` = [not set]`), without a header. The kcat assignment stages (`kcat_assignment.stage[]`), the `kcat_sources..` settings and the evotune source groups with their `sigma0_log` are shown as resolved. GECKO has `showAdapterParams`. -* `ModelAdapter.from_folder` checks more of model_adapter.toml when it loads it: `[kcat_sources.okp] methods` must be a list of strings and name known OKP methods (`Unknown OKP methods in [kcat_sources.okp] methods: ; methods: .`), a `[kcat_sources]` entry must be a table (`: kcat_sources. must be a table`), `[kcat_sources.brenda] aggregation` must be `max` or `median`, an evotune group's `sources` must be a non-empty list of strings, and `sigma0_log` must be finite and positive. A function given another BRENDA aggregation raises `aggregation must be max or median`. The unlabelled-kcat warning of evotune tuning is also issued as a `UserWarning`. `geckopy adapter show` prints whole-valued numbers without decimals and lists the `kcat_sources` settings by source (brenda, ec_tree, homologs, okp, then others alphabetically) and key. +* `ModelAdapter.from_folder` checks more of model_adapter.toml when it loads it: `[kcat_sources.okp] method` must be a string and name a known OKP method (`kcat_sources.okp.method must be a string`, `Unknown OKP method in [kcat_sources.okp] method; supported: `), a `[kcat_sources]` entry must be a table (`: kcat_sources. must be a table`), `[kcat_sources.brenda] aggregation` must be `max` or `median`, an evotune group's `sources` must be a non-empty list of strings, and `sigma0_log` must be finite and positive. A function given another BRENDA aggregation raises `aggregation must be max or median`. The unlabelled-kcat warning of evotune tuning is also issued as a `UserWarning`. `geckopy adapter show` prints whole-valued numbers without decimals and lists the `kcat_sources` settings by source (brenda, ec_tree, homologs, okp, then others alphabetically) and key. * `submit_open_kinetics_predictor`, `fetch_open_kinetics_predictor`, `build_okp_input_csv` and `parse_okp_output` are private (`_submit_open_kinetics_predictor` and so on) and no longer exported from `geckopy`, `geckopy.gather_kcats` or `geckopy.gather_kcats.open_kinetics_predictor`: `kcats_from_okp` and `kcats_from_dlkcat(via="okp")` submit and fetch OpenKineticsPredictor jobs. GECKO moves its OKP engine functions into `kcatsFromOKP` in the same way. `OKPClient` and `OKPError` stay public. * `convert_custom_kcats` (or `geckopy upgrade`) turns `data/customKcats.tsv` into `data/kcats/custom.tsv`. `collect_kcats` and `assign_kcats` do not apply `data/customKcats.tsv`: when a stage lists custom, `data/kcats/custom.tsv` does not exist and `data/customKcats.tsv` does, they raise `ValueError` naming `convert_custom_kcats`, before they change the model. The decisions have no stage `custom`. GECKO's `collectKcats` does the same. * `download_uniprot` and `load_uniprot_tsv` download reviewed and unreviewed entries by default (`reviewed=False`), as the adapter default `uniprot.reviewed = false`; they downloaded reviewed entries only. diff --git a/src/geckopy/adapter/adapterParameters.tsv b/src/geckopy/adapter/adapterParameters.tsv index 6b9f29b..575796d 100644 --- a/src/geckopy/adapter/adapterParameters.tsv +++ b/src/geckopy/adapter/adapterParameters.tsv @@ -32,6 +32,6 @@ kcat_assignment.preset Kcat assignment preset: brenda_ec_tree (used when neith kcat_assignment.isozymes true Fill reactions without a kcat from their isozymes after the stages kcat_assignment.standard true Give the remaining reactions the standard kcat kcat_assignment.stage Stages instead of a preset: action (select or combine), sources, criteria and BRENDA limits ([[kcat_assignment.stage]]) -kcat_sources Settings per kcat source file: [kcat_sources.brenda] aggregation and force_wildcard_level, [kcat_sources.ec_tree] k, [kcat_sources.homologs] k, min_identity and coverage, [kcat_sources.okp] methods (the OKP methods of the okp source group; default KinForm-H) +kcat_sources Settings per kcat source file: [kcat_sources.brenda] aggregation and force_wildcard_level, [kcat_sources.ec_tree] k, [kcat_sources.homologs] k, min_identity and coverage, [kcat_sources.okp] method (the OKP method of the okp source group; default KinForm-H) matlab.make_model_anaerobic matlab.makeModelAnaerobic MATLAB function that constrains the ecModel to anaerobic growth matlab.change_protein_biomass matlab.changeProteinBiomass MATLAB function that changes the protein content of the biomass diff --git a/src/geckopy/adapter/params.py b/src/geckopy/adapter/params.py index e963b4d..703dceb 100644 --- a/src/geckopy/adapter/params.py +++ b/src/geckopy/adapter/params.py @@ -118,8 +118,8 @@ class KcatStageParams(BaseModel): sources: list[str] = Field( min_length=1, description=( - "Source file names (custom, ec_tree, dlkcat, okp_catapro, ...), the OKP " - "methods of [kcat_sources.okp] as okp, and for select the BRENDA tiers " + "Source file names (custom, ec_tree, dlkcat, okp_unikp, ...), the OKP " + "method of [kcat_sources.okp] as okp, and for select the BRENDA tiers " "database_exact, database_top and database_bottom, which read brenda.tsv; " "for select the first listed wins." ), @@ -271,7 +271,7 @@ class EvotuneParams(BaseModel): class OkpParams(BaseModel): """OpenKineticsPredictor settings (used by kcats_from_okp). - The OKP methods are set in ``[kcat_sources.okp] methods``. + The OKP method is set in ``[kcat_sources.okp] method``. The API key is intentionally NOT here (it is a secret): provide it as a function argument, the OKP_API_KEY environment variable, or the file @@ -352,7 +352,7 @@ class ModelParameters(BaseModel): "brenda": ("aggregation", "force_wildcard_level"), "ec_tree": ("k",), "homologs": ("k", "min_identity", "coverage"), - "okp": ("methods",), + "okp": ("method",), "dlkcat": (), "custom": (), } @@ -402,14 +402,16 @@ def _check_table(cls: type[BaseModel], data: dict, prefix: str, file: Path | str def _check_source_values(name: str, table: dict, file: Path | str) -> None: - """The values of ``[kcat_sources.okp] methods`` and ``[kcat_sources.brenda] aggregation``.""" - if name == "okp" and "methods" in table: - methods = table["methods"] - if not isinstance(methods, list) or not all(isinstance(m, str) for m in methods): - raise ValueError(f"{file}: kcat_sources.okp.methods must be a list of strings") - from ..gather_kcats.kcat_sources import check_okp_methods - - check_okp_methods(methods) + """The values of ``[kcat_sources.okp] method`` and ``[kcat_sources.brenda] aggregation``.""" + if name == "okp" and "method" in table: + method = table["method"] + if not isinstance(method, str) and not ( + isinstance(method, list) and method and all(isinstance(m, str) for m in method) + ): + raise ValueError(f"{file}: kcat_sources.okp.method must be a string") + from ..gather_kcats.kcat_sources import _okp_method_names + + _okp_method_names(method) if name == "brenda" and "aggregation" in table and table["aggregation"] not in ("max", "median"): raise ValueError(f"{file}: kcat_sources.brenda.aggregation must be max or median") diff --git a/src/geckopy/adapter/show.py b/src/geckopy/adapter/show.py index 9c29401..e716609 100644 --- a/src/geckopy/adapter/show.py +++ b/src/geckopy/adapter/show.py @@ -57,7 +57,7 @@ def _stage_line(i: int, stage, shown: list[str], origin: str) -> str: def format_adapter_params(adapter: "ModelAdapter") -> str: """The resolved parameters of ``adapter`` as text (see :func:`show_adapter_params`).""" - from ..gather_kcats.kcat_assignment import DEFAULT_OKP_METHODS, kcat_assignment_config + from ..gather_kcats.kcat_assignment import DEFAULT_OKP_METHOD, kcat_assignment_config params = adapter.params config = kcat_assignment_config(adapter) @@ -84,8 +84,8 @@ def format_adapter_params(adapter: "ModelAdapter") -> str: out.append(_stage_line(i, stage, shown, "file" if params.kcat_assignment.stage else preset_origin)) elif key == "kcat_sources": sources = {name: dict(keys) for name, keys in config.sources.items()} - if "methods" not in sources.get("okp", {}): - sources.setdefault("okp", {})["methods"] = list(DEFAULT_OKP_METHODS) + if "method" not in sources.get("okp", {}): + sources.setdefault("okp", {})["method"] = DEFAULT_OKP_METHOD order = ["brenda", "ec_tree", "homologs", "okp"] for name in sorted(sources, key=lambda n: (order.index(n), "") if n in order else (len(order), n)): keys = sources[name] @@ -93,7 +93,7 @@ def format_adapter_params(adapter: "ModelAdapter") -> str: v = keys[k] if k in params.kcat_sources.get(name, {}): where = "file" - elif name == "okp" and k == "methods": + elif name == "okp" and k == "method": where = "default" else: where = preset_origin diff --git a/src/geckopy/adapter/template.py b/src/geckopy/adapter/template.py index 9acd688..39d2026 100644 --- a/src/geckopy/adapter/template.py +++ b/src/geckopy/adapter/template.py @@ -52,7 +52,7 @@ "# min_identity = 0.4", "# coverage = 0.8", "# [kcat_sources.okp]", - '# methods = ["KinForm-H"] # the first is the default method of kcats_from_okp', + '# method = "KinForm-H" # the default method of kcats_from_okp', "", ] diff --git a/src/geckopy/gather_kcats/kcat_assignment.py b/src/geckopy/gather_kcats/kcat_assignment.py index 37f6293..6427b6b 100644 --- a/src/geckopy/gather_kcats/kcat_assignment.py +++ b/src/geckopy/gather_kcats/kcat_assignment.py @@ -22,8 +22,8 @@ from .get_standard_kcat import assign_standard_kcat from .kcat_source_format import format_kcat_source, is_standard_source from .kcat_sources import ( - DEFAULT_OKP_METHODS, - check_okp_methods, + DEFAULT_OKP_METHOD, + _okp_method_names, load_kcat_source, okp_file, okp_method_of_file, @@ -115,11 +115,9 @@ def _source_file(token: str) -> str: return "brenda" if token.startswith("database_") else token -def okp_methods(config: KcatAssignmentConfig) -> list[str]: - """The OKP methods of the ``okp`` source group (``[kcat_sources.okp] methods``).""" - methods = config.sources.get("okp", {}).get("methods") or list(DEFAULT_OKP_METHODS) - check_okp_methods(methods) - return list(methods) +def _okp_names(config: KcatAssignmentConfig) -> list[str]: + """The OKP method of the ``okp`` source group, ``[kcat_sources.okp] method``.""" + return _okp_method_names(config.sources.get("okp", {}).get("method") or DEFAULT_OKP_METHOD) def brenda_aggregation(adapter: "ModelAdapter") -> str: @@ -134,15 +132,15 @@ def brenda_aggregation(adapter: "ModelAdapter") -> str: def default_okp_method(adapter: "ModelAdapter") -> str: - """The OKP method used when none is given: the first of ``[kcat_sources.okp] methods``.""" - return okp_methods(kcat_assignment_config(adapter))[0] + """The OKP method used when none is given: ``[kcat_sources.okp] method``.""" + return _okp_names(kcat_assignment_config(adapter))[0] def _expand(tokens: list[str], config: KcatAssignmentConfig) -> list[str]: - """Stage tokens with the ``okp`` group replaced by its methods' files.""" + """Stage tokens with the ``okp`` group replaced by the file of its OKP method.""" out: list[str] = [] for t in tokens: - out.extend(okp_file(m) for m in okp_methods(config)) if t == "okp" else out.append(t) + out.extend(okp_file(m) for m in _okp_names(config)) if t == "okp" else out.append(t) return list(dict.fromkeys(out)) @@ -314,22 +312,22 @@ def collect_kcats( :mod:`geckopy.gather_kcats.combine_kcats`); ``ec.source`` reads ``combined (sd=, n=)``. - In both, ``okp`` stands for the OKP methods of ``[kcat_sources.okp] - methods`` (default: KinForm-H). + In both, ``okp`` stands for the file of the OKP method of + ``[kcat_sources.okp] method`` (default: KinForm-H). The presets: - ``brenda_ec_tree`` (the default): custom kcats, the BRENDA median of measurements of the organism on the reaction's substrate (origin 1, without EC wildcards), the EC tree (:func:`kcats_from_ec_tree`); then - the OKP methods combined for the reactions left (without an EC number). + the OKP predictions for the reactions left (without an EC number). - ``ec_tree_homologs``: custom kcats, the organism's own BRENDA value; then the EC tree and homologs (:func:`kcats_from_homologs`) combined; - then the OKP methods combined. + then the OKP predictions. - ``ec_tree_okp``: custom kcats, the organism's own BRENDA value; then - the EC tree, homologs and the OKP methods combined; then the OKP - methods combined. - - ``okp``: custom kcats; then the OKP methods combined. + the EC tree, homologs and the OKP predictions combined; then the OKP + predictions. + - ``okp``: custom kcats; then the OKP predictions. - ``gecko_median``: custom kcats, the BRENDA median of matches without EC wildcards from the organism or other organisms, with or without the substrate (origin 1 to 4), DLKcat, then the other BRENDA matches (EC @@ -340,10 +338,9 @@ def collect_kcats( Against measured kcats hidden from the search, the EC tree was closer than the BRENDA median at origins 2 and 4 and with EC wildcards and as - close at origins 1 and 3; the EC tree combined with the OKP methods was - slightly closer than the EC tree, and the OKP methods combined as close. - That comparison combined the eight OKP methods with combination - parameters; the default ``okp`` group is now KinForm-H alone. In ecModels, values combined with predictions set high-turnover enzymes + close at origins 1 and 3; the EC tree combined with OKP predictions was + slightly closer than the EC tree. The default OKP method is KinForm-H. + In ecModels, values combined with predictions set high-turnover enzymes such as ATP synthase too low, which is why the default uses predictions only where there is no value from measurements. The BRENDA aggregation is ``[kcat_sources.brenda] aggregation`` (see :func:`update_kcat_sources`). diff --git a/src/geckopy/gather_kcats/kcat_sources.py b/src/geckopy/gather_kcats/kcat_sources.py index 246a70f..5685439 100644 --- a/src/geckopy/gather_kcats/kcat_sources.py +++ b/src/geckopy/gather_kcats/kcat_sources.py @@ -40,16 +40,16 @@ _NUMBER_RE = re.compile(r"^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?$|^[-+]?Inf$") _META_RE = re.compile(r"^\s*#\s*([A-Za-z][A-Za-z0-9_]{0,62})\s*:\s?(.*)$", re.ASCII) -#: The OKP methods that ``[kcat_sources.okp] methods`` and ``kcats_from_okp`` accept. +#: The OKP methods that ``[kcat_sources.okp] method`` and ``kcats_from_okp`` accept. #: The reaction methods CatPred and TurNup are left out: they read whole reactions #: (substrate set, and products for TurNup), while ``kcats_from_okp`` submits #: enzyme-substrate pairs. OKP_METHODS = ("CataPro", "CatRange", "DLKcat", "EITLEM", "KinForm-H", "KinForm-L", "OmniESI", "OmniESI-O2DENet", "UniKP") -#: The OKP methods of the ``okp`` source group by default; a project lists others in -#: ``[kcat_sources.okp] methods`` (those that have combination parameters: not +#: The OKP method of the ``okp`` source group by default; a project sets another in +#: ``[kcat_sources.okp] method`` (one that has combination parameters: not #: CatRange, which predicts classes). -DEFAULT_OKP_METHODS = ("KinForm-H",) +DEFAULT_OKP_METHOD = "KinForm-H" def okp_file(method: str) -> str: @@ -61,15 +61,23 @@ def okp_file(method: str) -> str: return "okp_" + re.sub(r"[^A-Za-z0-9]+", "_", method).lower().strip("_") -def check_okp_methods(methods: list[str]) -> None: - """Raise ``ValueError`` naming the entries of ``methods`` that are no known OKP method. +def _okp_method_names(method: Any) -> list[str]: + """The OKP method names of a ``[kcat_sources.okp] method`` value. - The names are in the order given, each once. + Raise ``ValueError`` when the value is not a string or when a name is no known + OKP method. """ - unknown = list(dict.fromkeys(m for m in methods if m not in OKP_METHODS)) + if isinstance(method, str): + names = [method] + elif isinstance(method, list) and method and all(isinstance(m, str) for m in method): + names = list(method) + else: + raise ValueError("kcat_sources.okp.method must be a string") + unknown = list(dict.fromkeys(m for m in names if m not in OKP_METHODS)) if unknown: - raise ValueError(f"Unknown OKP methods in [kcat_sources.okp] methods: {', '.join(unknown)}; " - f"methods: {', '.join(OKP_METHODS)}.") + raise ValueError(f"Unknown OKP method {', '.join(unknown)} in [kcat_sources.okp] method; " + f"supported: {', '.join(OKP_METHODS)}") + return names def okp_method_of_file(name: str) -> str: diff --git a/src/geckopy/gather_kcats/kcats_from.py b/src/geckopy/gather_kcats/kcats_from.py index a4f6433..a31bacc 100644 --- a/src/geckopy/gather_kcats/kcats_from.py +++ b/src/geckopy/gather_kcats/kcats_from.py @@ -409,20 +409,19 @@ def kcats_from_okp( OKP is a web service with a job queue, so this can be called repeatedly: without a job for ``method`` it submits one; with a job it fetches the - result, and writes the file once the job is done. Each method keeps its - own job in ``data/OKP_job_.txt`` and its own result in - ``data/OKP_output_.csv``, so jobs of several methods can run at - the same time. The job file records which job the stored result came - from; ``use_stored`` only uses a result of the current job. An API key - is needed. + result, and writes the file once the job is done. The job of a method is + kept in ``data/OKP_job_.txt`` and its result in + ``data/OKP_output_.csv``. The job file records which job the stored + result came from; ``use_stored`` only uses a result of the current job. An + API key is needed. Parameters ---------- model EcModel with metabolite SMILES and protein sequences. method - OKP method, e.g. ``"CataPro"``, ``"CatRange"``, ``"UniKP"`` (default: the - first of ``[kcat_sources.okp] methods``, ``"KinForm-H"`` unless set). + OKP method, e.g. ``"CataPro"``, ``"CatRange"``, ``"UniKP"`` (default: + ``[kcat_sources.okp] method``, ``"KinForm-H"`` unless set). wait Wait until the job is done. resubmit diff --git a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py index c4457bf..ee51453 100644 --- a/src/geckopy/gather_kcats/open_kinetics_predictor/run.py +++ b/src/geckopy/gather_kcats/open_kinetics_predictor/run.py @@ -70,8 +70,8 @@ def _submit_open_kinetics_predictor( OKP API key. If omitted, resolved from ``OKP_API_KEY`` or ``data/okpApiKey.txt`` (see the module docstring). method - Predictor method. Resolved as: this arg -> the first of - ``[kcat_sources.okp] methods`` (``'KinForm-H'`` unless set). + Predictor method. Resolved as: this arg -> + ``[kcat_sources.okp] method`` (``'KinForm-H'`` unless set). ec_rxns Optional iterable of reaction IDs to include (default all). only_with_smiles diff --git a/tests/test_adapter.py b/tests/test_adapter.py index ba8c4e5..6b1bd6d 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -306,7 +306,7 @@ def test_known_kcat_source_keys_and_free_matlab_params_load(tmp_path): min_identity = 0.4 coverage = 0.5 [kcat_sources.okp] -methods = ["KinForm-H"] +method = "KinForm-H" [kcat_sources.okp_kinform_h] [kcat_sources.custom] [kcat_sources.dlkcat] @@ -345,7 +345,7 @@ def test_show_prints_one_line_per_item_in_the_agreed_format(tmp_path, capsys): "kcat_assignment.stage[2] = combine: okp [preset brenda_ec_tree]", "kcat_sources.brenda.aggregation = max [file]", "kcat_sources.ec_tree.k = 5 [preset brenda_ec_tree]", - "kcat_sources.okp.methods = KinForm-H [default]", + "kcat_sources.okp.method = KinForm-H [default]", "evotune.source_groups.okp = okp_*; sigma0_log 0.25 [file]", ]: assert expected in lines, expected @@ -426,8 +426,9 @@ def test_okp_source_tables_are_limited_to_known_methods(tmp_path): @pytest.mark.parametrize("extra, message", [ - ("[kcat_sources.okp]\nmethods = \"KinForm-H\"\n", "kcat_sources.okp.methods must be a list of strings"), - ("[kcat_sources.okp]\nmethods = [1]\n", "kcat_sources.okp.methods must be a list of strings"), + ("[kcat_sources.okp]\nmethod = 1\n", "kcat_sources.okp.method must be a string"), + ("[kcat_sources.okp]\nmethod = [1]\n", "kcat_sources.okp.method must be a string"), + ("[kcat_sources.okp]\nmethods = \"KinForm-H\"\n", "unknown key kcat_sources.okp.methods in model_adapter.toml"), ("kcat_sources.brenda = 1\n", "kcat_sources.brenda must be a table"), ("kcat_sources.foo = 1\n", "unknown key kcat_sources.foo in model_adapter.toml"), ("[kcat_sources.brenda]\naggregation = \"mean\"\n", "kcat_sources.brenda.aggregation must be max or median"), @@ -439,16 +440,16 @@ def test_kcat_source_values_are_checked_at_load(tmp_path, extra, message): assert str(err.value) == f"{folder.resolve() / 'model_adapter.toml'}: {message}" -def test_unknown_okp_methods_are_an_error_at_load_in_file_order_without_duplicates(tmp_path): +def test_unknown_okp_method_is_an_error_at_load(tmp_path): from geckopy.gather_kcats.kcat_sources import OKP_METHODS folder = _write_adapter_folder( - tmp_path, MINIMAL_TOML + '[kcat_sources.okp]\nmethods = ["Zed", "KinForm-H", "Alpha", "Zed", "TurNup"]\n') + tmp_path, MINIMAL_TOML + '[kcat_sources.okp]\nmethod = "TurNup"\n') with pytest.raises(ValueError) as err: ModelAdapter.from_folder(folder) assert str(err.value) == ( - "Unknown OKP methods in [kcat_sources.okp] methods: Zed, Alpha, TurNup; " - f"methods: {', '.join(OKP_METHODS)}.") + "Unknown OKP method TurNup in [kcat_sources.okp] method; " + f"supported: {', '.join(OKP_METHODS)}") def test_show_number_formats_and_kcat_sources_order(tmp_path, capsys): @@ -459,7 +460,7 @@ def test_show_number_formats_and_kcat_sources_order(tmp_path, capsys): [complex] taxonomic_id = 5592920 [kcat_sources.okp] -methods = ["UniKP"] +method = "UniKP" [kcat_sources.ec_tree] k = 3 [kcat_sources.brenda] @@ -479,5 +480,5 @@ def test_show_number_formats_and_kcat_sources_order(tmp_path, capsys): "kcat_sources.brenda.aggregation = median [file]", "kcat_sources.brenda.force_wildcard_level = 1 [file]", "kcat_sources.ec_tree.k = 3 [file]", - "kcat_sources.okp.methods = UniKP [file]", + "kcat_sources.okp.method = UniKP [file]", ] diff --git a/tests/test_cli.py b/tests/test_cli.py index 7c8fe1d..9335359 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -201,5 +201,5 @@ def fake_download(kid, path, *, gene_id_field): def test_template_lists_the_kcat_source_keys_after_the_sections(): text = generate_template_toml() assert text.index("# [kcat_sources.brenda]") > text.index("# [evotune.source_groups.combined]") - for key in ("aggregation", "force_wildcard_level", "min_identity", "coverage", "methods"): + for key in ("aggregation", "force_wildcard_level", "min_identity", "coverage", "method"): assert f"# {key} = " in text diff --git a/tests/test_combine_kcats.py b/tests/test_combine_kcats.py index 3b3c173..a7e3ba5 100644 --- a/tests/test_combine_kcats.py +++ b/tests/test_combine_kcats.py @@ -7,11 +7,11 @@ from pathlib import Path import cobra -import numpy as np import pandas as pd import pytest from geckopy import ModelAdapter, make_ec_model +from geckopy.adapter import show_adapter_params from geckopy.gather_kcats import collect_kcats, save_kcat_source from geckopy.gather_kcats.combine_kcats import combine_kcats, evidence_bin, load_combine_params from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, okp_file, update_kcat_sources @@ -92,7 +92,7 @@ def _project(tmp_path: Path, preset: str): (project / "data" / "customKcats.tsv").unlink() toml = project / "model_adapter.toml" toml.write_text(toml.read_text(encoding="utf-8") + f'\n[kcat_assignment]\npreset = "{preset}"\n' - '[kcat_sources.okp]\nmethods = ["CataPro", "UniKP"]\n', encoding="utf-8") + '[kcat_sources.okp]\nmethod = "CataPro"\n', encoding="utf-8") adapter = ModelAdapter.from_folder(project) model = make_ec_model(cobra.io.read_sbml_model(str(adapter.params.conv_gem)), adapter) fill_eccodes_from_gem(model) @@ -110,20 +110,17 @@ def test_okp_presets(tmp_path): assert [st.action for st in config.stages] == ["select", "combine"] assert okp_file("KinForm-H") == "okp_kinform_h" report = update_kcat_sources(model, dry_run=True, adapter=adapter) - assert list(report["source"]) == ["custom", "okp_catapro", "okp_unikp"] + assert list(report["source"]) == ["custom", "okp_catapro"] rxns = [r for r in model.ec.rxns if r.startswith("R")][:3] save_kcat_source(model, _okp_list(model, rxns, 100.0, 30, "CataPro"), "okp_catapro", adapter=adapter) - save_kcat_source(model, _okp_list(model, rxns[:1], 10.0, 50, "UniKP"), "okp_unikp", adapter=adapter) decisions, rep, decided = collect_kcats(model, adapter=adapter, write_report=False) d = decisions.set_index("ec_rxn") - # rxns[0]: CataPro 100 (bias 0.2, variance 2) and UniKP 10 (bias -0.3, variance 1): - # (1.8 * 0.5 + 1.3 * 1) / 1.5 = 1.4667; the okp scale is 1. - assert d.loc[rxns[0], "kcat"] == pytest.approx(10 ** ((1.8 * 0.5 + 1.3) / 1.5)) - assert d.loc[rxns[0], "source"] == f"combined (sd={math.sqrt(1 / 1.5 + 0.25):.2f}, n=2)" - assert d.loc[rxns[0], "stage"] == "2" and d.loc[rxns[0], "candidates"] == 2 - assert d.loc[rxns[1], "kcat"] == pytest.approx(10 ** 1.8) - assert np.isfinite(d.loc[rxns[0], "sd_log10"]) + # rxns[0]: CataPro 100 (bias 0.2, variance 2): the corrected value is 1.8; + # sd sqrt(1 / 0.5 + 0.25) with the okp scale 1. + assert d.loc[rxns[0], "kcat"] == pytest.approx(10 ** 1.8) + assert d.loc[rxns[0], "stage"] == "2" + assert d.loc[rxns[0], "sd_log10"] == pytest.approx(math.sqrt(1 / 0.5 + 0.25)) # ec_tree_okp: the EC tree and OKP combined, where both have a value. model, adapter, project = _project(tmp_path / "b", "ec_tree_okp") @@ -138,6 +135,21 @@ def test_okp_presets(tmp_path): assert d.loc[rxns[0], "source"].startswith("combined (") and d.loc[rxns[0], "stage"] == "2" +def test_okp_method_value_forms(tmp_path, capsys): + model, adapter, project = _project(tmp_path, "okp") + adapter.params.kcat_sources["okp"]["method"] = ["CataPro", "UniKP"] + report = update_kcat_sources(model, dry_run=True, adapter=adapter) + assert list(report["source"]) == ["custom", "okp_catapro", "okp_unikp"] + rxns = [r for r in model.ec.rxns if r.startswith("R")][:3] + save_kcat_source(model, _okp_list(model, rxns, 100.0, 30, "CataPro"), "okp_catapro", adapter=adapter) + save_kcat_source(model, _okp_list(model, rxns[:1], 10.0, 50, "UniKP"), "okp_unikp", adapter=adapter) + d = collect_kcats(model, adapter=adapter, write_report=False)[0].set_index("ec_rxn") + assert d.loc[rxns[0], "kcat"] == pytest.approx(10 ** ((1.8 * 0.5 + 1.3) / 1.5)) + assert d.loc[rxns[0], "candidates"] == 2 + show_adapter_params(adapter) + assert "kcat_sources.okp.method = CataPro, UniKP" in capsys.readouterr().out + + def test_exact_homolog_dominates(tmp_path): """EC tree 10 /s (ec4:3-5: bias 0.1, variance 1) and a homolog at 100% identity measured at 100 /s (exact: bias 0, variance 0.01): @@ -158,6 +170,6 @@ def test_exact_homolog_dominates(tmp_path): def test_unknown_okp_method(tmp_path): model, adapter, project = _project(tmp_path, "okp") - adapter.params.kcat_sources["okp"]["methods"] = ["CataPro", "NoSuchMethod"] + adapter.params.kcat_sources["okp"]["method"] = "NoSuchMethod" with pytest.raises(ValueError, match="NoSuchMethod"): update_kcat_sources(model, dry_run=True, adapter=adapter) diff --git a/tests/test_kcat_assignment.py b/tests/test_kcat_assignment.py index 67bb809..054019c 100644 --- a/tests/test_kcat_assignment.py +++ b/tests/test_kcat_assignment.py @@ -262,27 +262,27 @@ def no_key(api_key, adapter): assert "API key" in report.loc["dlkcat", "note"] -def test_default_okp_methods_are_kinform_h_and_a_project_list_is_kept(tmp_path): - from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, okp_methods +def test_default_okp_method_is_kinform_h_and_a_project_method_is_kept(tmp_path): + from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, _okp_names _, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "okp"\n') - assert okp_methods(kcat_assignment_config(adapter)) == ["KinForm-H"] - adapter.params.kcat_sources["okp"] = {"methods": ["CataPro", "UniKP"]} - assert okp_methods(kcat_assignment_config(adapter)) == ["CataPro", "UniKP"] + assert _okp_names(kcat_assignment_config(adapter)) == ["KinForm-H"] + adapter.params.kcat_sources["okp"] = {"method": "UniKP"} + assert _okp_names(kcat_assignment_config(adapter)) == ["UniKP"] @pytest.mark.parametrize("method", ["CatPred", "TurNup"]) -def test_reaction_okp_methods_are_unknown_in_methods(tmp_path, method): - from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, okp_methods +def test_reaction_okp_methods_are_unknown_as_method(tmp_path, method): + from geckopy.gather_kcats.kcat_assignment import kcat_assignment_config, _okp_names _, adapter, _ = _project(tmp_path, '[kcat_assignment]\npreset = "okp"\n') - adapter.params.kcat_sources["okp"] = {"methods": [method]} + adapter.params.kcat_sources["okp"] = {"method": method} from geckopy.gather_kcats.kcat_sources import OKP_METHODS with pytest.raises(ValueError) as err: - okp_methods(kcat_assignment_config(adapter)) + _okp_names(kcat_assignment_config(adapter)) assert str(err.value) == ( - f"Unknown OKP methods in [kcat_sources.okp] methods: {method}; methods: {', '.join(OKP_METHODS)}.") + f"Unknown OKP method {method} in [kcat_sources.okp] method; supported: {', '.join(OKP_METHODS)}") def test_okp_source_file_of_no_known_method_fails_naming_the_file(tmp_path): diff --git a/tests/test_kcat_sources.py b/tests/test_kcat_sources.py index 6febac1..69a22f2 100644 --- a/tests/test_kcat_sources.py +++ b/tests/test_kcat_sources.py @@ -200,8 +200,8 @@ def test_kcats_from_dlkcat_and_okp_write_files(tmp_path, monkeypatch): def test_okp_jobs_per_method(tmp_path, monkeypatch): - """The sequence that mixed up methods: two jobs in flight, one fetched, - then use_stored for the other. Each method keeps its own job and result.""" + """A job is submitted and fetched, then use_stored reads the result of a call + with another method: each method keeps its own job and result.""" adapter = _adapter(tmp_path) full = _model(adapter, light=False) data = tmp_path / "data" @@ -468,9 +468,9 @@ def test_kcats_from_dlkcat_run_is_docker(tmp_path, monkeypatch): assert meta["parameters"] == '{"run":true}' -def test_kcats_from_okp_default_method_is_first_of_methods(tmp_path, monkeypatch): - """Without a method, kcats_from_okp uses the first entry of [kcat_sources.okp] methods - (KinForm-H when the project lists none).""" +def test_kcats_from_okp_default_method_is_the_configured_method(tmp_path, monkeypatch): + """Without a method, kcats_from_okp uses [kcat_sources.okp] method + (KinForm-H when the project sets none).""" adapter = _adapter(tmp_path) full = _model(adapter, light=False) (tmp_path / "data").mkdir() @@ -483,7 +483,7 @@ def fake_submit(model, **kw): monkeypatch.setattr(kcats_from_module, "_submit_open_kinetics_predictor", fake_submit) monkeypatch.setattr(kcats_from_module, "_fetch_open_kinetics_predictor", lambda model, **kw: (False, None)) assert kcats_from_okp(full, save=False) == (None, False) - adapter.params.kcat_sources["okp"] = {"methods": ["UniKP", "CataPro"]} + adapter.params.kcat_sources["okp"] = {"method": "UniKP"} kcats_from_okp(full, save=False, resubmit=True) assert submitted == ["KinForm-H", "UniKP"] diff --git a/tutorials/full_ecModel/protocol.py b/tutorials/full_ecModel/protocol.py index 6709a6c..5c1f6ad 100644 --- a/tutorials/full_ecModel/protocol.py +++ b/tutorials/full_ecModel/protocol.py @@ -205,11 +205,11 @@ # # - `brenda_ec_tree` (the default): the organism's own BRENDA value, else # the median of the EC number's measurements (`kcats_from_ec_tree`); the -# OpenKineticsPredictor (OKP) methods combined for reactions without an -# EC number. -# - `ec_tree_okp`: the EC tree and the OKP methods combined for every +# OpenKineticsPredictor (OKP) predictions combined for reactions without +# an EC number. +# - `ec_tree_okp`: the EC tree and the OKP predictions combined for every # reaction, weighted by how reliable each is. -# - `okp`: the OKP methods combined only, for when the model's EC numbers +# - `okp`: the OKP predictions only, for when the model's EC numbers # are not trusted. # # Custom kcats (`data/kcats/custom.tsv`) come first in every preset.