corneto.methods.future.MultiSampleFBA#
- class corneto.methods.future.MultiSampleFBA(lambda_reg=0.0, beta_reg=0.0, flux_indicator_name='edge_has_flux', disable_structured_sparsity=False, backend=None)#
Bases:
FlowMethod
Flux Balance Analysis (FBA) method for multiple samples.
This class implements Flux Balance Analysis for metabolic networks across multiple samples. It allows for regularization to minimize the number of active reactions (sparsity) and optimization based on reaction objectives.
Note
The graph is expected to be a genome scale metabolic network. It can also be imported in SBML format (XML) using the
corneto.io.import_cobra_model
function (requires cobrapy).Flux Balance Analysis is a mathematical approach for analyzing metabolism in living cells, where metabolic fluxes in a metabolic network are calculated by optimizing an objective function (typically biomass production) subject to network stoichiometry constraints.
The MultiSampleFBA class extends traditional FBA by enabling analysis across multiple samples or conditions simultaneously, which allows for comparative metabolic studies.
- lambda_reg#
Regularization parameter to minimize the number of active reactions across samples (only when samples > 1).
- Type:
- beta_reg#
Additional regularization parameter for controlling sparsity individually for each sample.
- Type:
Examples
Basic usage with a single sample:
>>> from corneto.io import import_miom_model >>> from corneto.data import Data >>> from corneto.methods.future.fba import MultiSampleFBA >>> # Load a metabolic model >>> model = import_miom_model("path/to/metabolic_model.miom") >>> # Create data with objective function (typically biomass) >>> data = Data.from_dict({ ... "sample1": { ... "EX_biomass_e": { ... "role": "objective", ... }, ... } ... }) >>> # Initialize FBA and solve >>> fba = MultiSampleFBA() >>> P = fba.build(model, data) >>> P.solve() >>> # Access the flux values >>> biomass_rid = next(iter(model.get_edges_by_attr("id", "EX_biomass_e"))) >>> biomass_flux = P.expr.flow[biomass_rid].value >>> print(f"Biomass flux: {biomass_flux}")
Multi-sample analysis with gene knockouts:
>>> # Create data with two samples - control and knockout >>> data = Data.from_cdict({ ... "control": { ... "EX_biomass_e": { ... "role": "objective", ... }, ... }, ... "knockout": { ... "EX_biomass_e": { ... "role": "objective", ... }, ... "MDHm": { # Malate dehydrogenase knockout ... "lower_bound": 0, ... "upper_bound": 0, ... }, ... } ... }) >>> # Initialize FBA and solve >>> fba = MultiSampleFBA() >>> P = fba.build(model, data) >>> P.solve() >>> # Compare biomass production between conditions >>> rid = next(iter(model.get_edges_by_attr("id", "EX_biomass_e"))) >>> control_flux = P.expr.flow[rid, 0].value >>> knockout_flux = P.expr.flow[rid, 1].value >>> print(f"Control biomass: {control_flux}") >>> print(f"Knockout biomass: {knockout_flux}") >>> print(f"Growth reduction: {(control_flux - knockout_flux) / control_flux * 100:.2f}%")
Sparse FBA to minimize the number of active reactions:
>>> # Create data with biomass lower bound constraint >>> data = Data.from_dict({ ... "sample1": { ... "EX_biomass_e": { ... "type": "objective", ... "lower_bound": 100.80, # Enforce minimum biomass production ... }, ... } ... }) >>> # Initialize FBA with regularization for sparsity >>> fba = MultiSampleFBA(beta_reg=1) >>> P = fba.build(model, data) >>> P.solve() >>> # Count number of active reactions >>> n_active_reactions = np.sum(np.round(P.expr.edge_has_flux.value)) >>> print(f"Number of active reactions: {n_active_reactions}")
- __init__(lambda_reg=0.0, beta_reg=0.0, flux_indicator_name='edge_has_flux', disable_structured_sparsity=False, backend=None)#
Initialize a MultiSampleFBA instance.
- Parameters:
lambda_reg (float, optional) – Regularization parameter for the primary sparsity term. Higher values encourage fewer active reactions. Defaults to 0.0.
beta_reg (float, optional) – Secondary regularization parameter for sparsity. Used when both types of regularization are needed. Defaults to 0.0.
flux_indicator_name (str, optional) – Name for the flux indicator variables. These variables track whether a reaction is active or not. Defaults to “edge_has_flux”.
disable_structured_sparsity (bool, optional) – If True, structured sparsity optimization is disabled. Structured sparsity can improve solution quality but increases computation time. Defaults to False.
backend (Optional[Backend], optional) – The optimization backend to use. If None, the default backend is used. Defaults to None.
Methods
__init__
([lambda_reg, beta_reg, ...])Initialize a MultiSampleFBA instance.
build
(graph, data)Build the complete optimization problem.
create_flow_based_problem
(flow_problem, ...)Create the flow-based optimization problem.
create_problem
(graph, data)Create the optimization problem using a flow-based formulation.
Returns citation keys for this method.
get_flow_bounds
(graph, data)Get the flow bounds for the optimization problem.
preprocess
(graph, data)Preprocess the graph and data before solving.
show_bibtex
()Display raw BibTeX entries in a formatted block for easy copying.
show_citations
()Display formatted citations in a Jupyter notebook.
Attributes
Return the optimization backend being used.
- preprocess(graph, data)#
Preprocess the graph and data before solving.
This method can be extended to implement flux-consistent preprocessing techniques such as removing blocked reactions or dead-end metabolites.
- get_flow_bounds(graph, data)#
Get the flow bounds for the optimization problem.
This method extracts the default lower and upper bounds for each reaction in the metabolic network and sets up the problem dimensions based on the number of samples.
- Parameters:
- Returns:
- Dictionary containing flow bounds information including
lower bounds, upper bounds, number of flows (samples), and whether bounds are shared across samples.
- Return type:
Dict[str, Any]
- create_flow_based_problem(flow_problem, graph, data)#
Create the flow-based optimization problem.
This method sets up the constraints and objectives for the FBA optimization problem by: 1. Creating indicator variables for flux activity 2. Setting reaction-specific bounds for each sample 3. Adding objective functions for each sample 4. Adding regularization terms if requested
- static get_citations()#
Returns citation keys for this method.
- Returns:
A list of citation keys that can be used to lookup BibTeX entries.