Top-level (Python)#

geckopy objects in geckopy.

Functions#

Function

Summary

cmd_brenda_refresh

cmd_init

cmd_kegg_download

cmd_uniprot_download

cmd_upgrade

List, or with --apply carry out, the steps that bring a GECKO 3

constraint_shadow_prices

Shadow prices of the named constraints, on every solver, as one array.

convert_custom_kcats

Convert data/customKcats.tsv into data/kcats/custom.tsv.

edit_on_fast_solver

Run structural edits of model on glpk when it is on the scipy solver.

fast_solver_while_building

Make models created inside the block on a solver that builds large models quickly.

is_scipy_interface

True if interface (a solver name, an optlang interface module or a model's

main

optimize_model

model.optimize() that returns the solution of an infeasible LP on every solver.

repair_scipy_problem

Make the scipy problem of model correct and fast to edit.

switch_to_scipy

Move model to the scipy solver, fast, with the sparse problem of this module.

upgrade_ec_model

Turn the protein usage and pool reactions of a GECKO 3 ecModel forward.

upgrade_project

Bring a GECKO 3 project to the GECKO 4 layout (geckopy upgrade).

Reference#

cmd_brenda_refresh#

cmd_brenda_refresh(args: argparse.Namespace)

cmd_init#

cmd_init(args: argparse.Namespace)

cmd_kegg_download#

cmd_kegg_download(args: argparse.Namespace)

cmd_uniprot_download#

cmd_uniprot_download(args: argparse.Namespace)

cmd_upgrade#

cmd_upgrade(args: argparse.Namespace)

List, or with --apply carry out, the steps that bring a GECKO 3

project to the GECKO 4 layout (see geckopy.upgrade.upgrade_project).

constraint_shadow_prices#

constraint_shadow_prices(model, constraint_ids: Iterable[str])

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_<uniprot>.

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.

convert_custom_kcats#

convert_custom_kcats(model: 'EcModel', *, custom_kcats: Optional[str | Path]=None, out_file: Optional[str | Path]=None, adapter: Optional['ModelAdapter']=None)

Convert data/customKcats.tsv into data/kcats/custom.tsv.

The kcats are those geckopy.ec_model.pipeline.apply_custom_kcats gives the ecModel, including which of two entries for the same reaction wins: one row per reaction, keyed by conventional reaction and enzyme set (see geckopy.gather_kcats.ec_rxn_keys), with the entry's notes in the note column. The stoicho column is left out: complex stoichiometry comes from apply_complex_data. Unlike apply_custom_kcats, custom.tsv also applies to light ecModels.

Parameters

model

The full ecModel the custom kcats are for.

custom_kcats, out_file

The file to convert and the file to write (default: data/customKcats.tsv and data/kcats/custom.tsv in the project). An existing out_file is not overwritten.

adapter

Model adapter (default: model.adapter).

Returns

(path, report)

The file written, and a report with rows (rows written).

edit_on_fast_solver#

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 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.

fast_solver_while_building#

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 switch_to_scipy. With any other default solver, or when glpk is unavailable, the block changes nothing.

is_scipy_interface#

is_scipy_interface(interface)

True if interface (a solver name, an optlang interface module or a model's

problem attribute) is optlang's scipy interface.

main#

main(argv: list[str] | None=None)

optimize_model#

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.

repair_scipy_problem#

repair_scipy_problem(model)

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).

switch_to_scipy#

switch_to_scipy(model)

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.

upgrade_ec_model#

upgrade_ec_model(model: 'EcModel')

Turn the protein usage and pool reactions of a GECKO 3 ecModel forward.

GECKO 3 defined usage_prot_<id> (prot_<id> -> prot_pool) and prot_pool_exchange (prot_pool ->) in the reverse direction, used with negative flux; GECKO 4 defines them forward. Each reaction that is still reverse gets the opposite stoichiometry, bounds and objective coefficient; calling it again changes nothing. In place. geckopy.load_ec_model already does this for the files it reads; this is for models from other sources, such as SBML.

Returns

list of str

The reactions that were turned.

upgrade_project#

upgrade_project(project: str | Path, *, dry_run: bool=True, ec_model: Optional['EcModel']=None)

Bring a GECKO 3 project to the GECKO 4 layout (geckopy upgrade).

Lists, and with dry_run=False carries out:

  1. custom kcats: data/customKcats.tsv becomes data/kcats/custom.tsv (convert_custom_kcats);

  2. stored predictions: data/DLKcat.tsv and stored OpenKineticsPredictor results become kcat source files (kcats_from_dlkcat, kcats_from_okp with use_stored), without running anything;

  3. saved ecModels: models/*.yml ecModels made with GECKO 3 get their protein reactions turned forward (upgrade_ec_model);

  4. project_format = 4 in model_adapter.toml;

  5. scripts: lines of the project's .py files that call renamed or replaced functions are listed, not changed.

Steps 1 and 2 need the full ecModel the custom kcats and predictions are for (ec_model); without it they are listed and skipped. A project with an adapter class instead of model_adapter.toml is converted by GECKO's upgradeGECKOproject. A file that is replaced or converted is first moved to legacy/<yyyy-mm-dd>/, keeping its place in the folder tree. Steps already done are left out, so it can run again.

Returns

pandas.DataFrame

One row per step: step, status (to do, done, skipped, failed, by hand) and note.