Additional helper functions for the optlang solvers.
All functions integrate well with the context manager, meaning that
all operations defined here are automatically reverted when used in a
with model: block.
The functions defined here together with the existing model functions
should allow you to implement custom flux analysis methods with ease.
Retrieve coefficient for the reactions in a linear objective.
Parameters:
model (cobra.Model) – The cobra model defining the linear objective.
reactions (list of cobra.Reaction, optional) – An optional list of the reactions to get the coefficients for.
By default, all reactions are considered (default None).
Returns:
A dictionary where the keys are the reaction objects and the values
are the corresponding coefficient. Empty dictionary if there are no
linear terms in the objective.
model (cobra.Model) – The model to set the objective for.
value (optlang.interface.Objective, optlang.symbolics.Basic, dict) – If the model objective is linear, then the value can be a new
optlang.interface.Objective or a dictionary with linear
coefficients where each key is a reaction and the corresponding
value is the new coefficient (float).
If the objective is non-linear and additive is True, then only
values of class optlang.interface.Objective, are accepted.
additive (bool) – If True, add the terms to the current objective, otherwise start with
an empty objective.
Raises:
ValueError – If model objective is non-linear and the value is a dict.
TypeError – If the type of value is not one of the accepted ones.
Add variables and constraints to a model’s solver object.
Useful for variables and constraints that can not be expressed with
reactions and lower/upper bounds. It will integrate with the model’s
context manager in order to revert changes upon leaving the context.
Parameters:
model (cobra.Model) – The model to which to add the variables and constraints.
what (list or tuple of optlang.interface.Variable or) – optlang.interface.Constraint
The variables and constraints to add to the model.
Add the absolute value of an expression to the model.
Also defines a variable for the absolute value that can be used in
other objectives or constraints.
Parameters:
model (cobra.Model) – The model to which to add the absolute expression.
expression (str) – Must be a valid symbolic expression within the model’s solver object.
The absolute value is applied automatically on the expression.
name (str, optional) – The name of the newly created variable (default “abs_var”).
ub (positive float, optional) – The upper bound for the variable (default None).
difference (positive float, optional) – The difference between the expression and the variable
(default 0.0).
add (bool, optional) – Whether to add the variable to the model at once (default True).
Returns:
A named tuple with variable and two constraints (upper_constraint,
lower_constraint) describing the new variable and the constraints
that assign the absolute value of the expression to it.
Fix current objective as an additional constraint.
When adding constraints to a model, such as done in pFBA which
minimizes total flux, these constraints can become too powerful,
resulting in solutions that satisfy optimality but sacrifices too
much for the original objective function. To avoid that, we can fix
the current objective value as a constraint to ignore solutions that
give a lower (or higher depending on the optimization direction)
objective value than the original model.
When done with the model as a context, the modification to the
objective will be reverted when exiting that context.
fraction (float, optional) – The fraction of the optimum the objective is allowed to reach
(default 1.0).
bound (float, optional) – The bound to use instead of fraction of maximum optimal value.
If not None, fraction is ignored (default None).
name (str, optional) – Name of the objective. May contain one “{}” placeholder which is
filled with the name of the old objective
(default “fixed_objective_{}”).
Add a new objective and variables to ensure a feasible solution.
The optimized objective will be zero for a feasible solution and
otherwise represent the distance from feasibility (please see [1]_
for more information).
Parameters:
model (cobra.Model) – The model whose feasibility is to be tested.
Return type:
None
References
“DFBAlab: A Fast and Reliable MATLAB Code for Dynamic Flux Balance
Analysis.” BMC Bioinformatics 15, no. 1 (December 18, 2014): 409.
https://doi.org/10.1186/s12859-014-0409-8.
Successively optimize separate targets in a specific order.
For each objective, optimize the model and set the optimal value as a
constraint. Proceed in the order of the objectives given. Due to the
specific order this is called lexicographic FBA [1]_. This procedure
is useful for returning unique solutions for a set of important
fluxes. Typically this is applied to exchange fluxes.
objectives (list of cobra.Reaction) – A list of reactions (or objectives) in the model for which unique
fluxes are to be determined.
objective_direction (str or list of str, optional) – The desired objective direction for each reaction (if a list) or
the objective direction to use for all reactions (default “max”).
Returns:
A pandas Series containing the optimized fluxes for each of the
given reactions in objectives.
Return type:
pandas.Series
References
“DFBAlab: A Fast and Reliable MATLAB Code for Dynamic Flux Balance
Analysis.” BMC Bioinformatics 15, no. 1 (December 18, 2014): 409.
https://doi.org/10.1186/s12859-014-0409-8.