Return a stoichiometric array representation of the given model.
The the columns represent the reactions and rows represent
metabolites. S[i,j] therefore contains the quantity of metabolite i
produced (negative for consumed) by reaction j.
Parameters:
model (cobra.Model) – The cobra model to construct the matrix for.
array_type ({"dense", "dok", "lil", "DataFrame"}) – The type of array to construct. “dense” will return a standard
numpy.ndarray. “dok”, or “lil” will construct a sparse array using
scipy of the corresponding type. “DataFrame” will give a
pandas.DataFrame with metabolite as indices and reaction as
columns.
dtype (numpy.dtype, optional) – The desired numpy data type for the array (default numpy.float64).
Returns:
The stoichiometric matrix for the given model.
Return type:
matrix of class dtype
Raises:
ValueError – If sparse matrix is used and scipy is not installed.
.. deprecated: – 0.18.1: “DataFrame” option for array_type will be replaced with
“frame” in future versions.
Compute an approximate basis for the nullspace of A.
The algorithm used by this function is based on the Singular Value
Decomposition (SVD) of A.
Parameters:
A (numpy.ndarray) – A should be at most 2-D. 1-D array with length k will be treated
as a 2-D with shape (1, k).
atol (float, optional) – The absolute tolerance for a zero singular value. Singular values
smaller than atol are considered to be zero (default 1e-13).
rtol (float, optional) – The relative tolerance. Singular values less than rtol * smax are
considered to be zero, where smax is the largest singular value
(default 0.0).
Returns:
If A is an array with shape (m, k), then ns will be an array
with shape (k, n), where n is the estimated dimension of the
nullspace of A. The columns of ns are a basis for the
nullspace; each element in numpy.dot(A, ns) will be approximately
zero.
This is used for alternative solution approaches that do not use
“optlang”. The function will construct the equality matrix,
inequality matrix and bounds for the complete problem.
Parameters:
model (cobra.Model) – The model from which to obtain the LP problem.
array_type ({"dense", "dok", "lil", "DataFrame"}) – The type of array to construct. “dense” will return a standard
numpy.ndarray. “dok”, or “lil” will construct a sparse array using
scipy of the corresponding type. “DataFrame” will give a
pandas.DataFrame with metabolite as indices and reaction as
columns.
zero_tol (float, optional) – The zero tolerance used to judge whether two bounds are the same
(default 1e-6).
Returns:
A named tuple consisting of 6 matrices and 2 vectors:
- “equalities” is a matrix S such that S * vars = b. It
includes a row for each constraint and one column for each
variable.
”b” is the right side of the equality equation such that
S * vars = b.
”inequalities” is a matrix M such that lb <= M * vars <= ub.
It contains a row for each inequality and as many columns as
variables.
”bounds” is a compound matrix [lb ub] containing the lower and
upper bounds for the inequality constraints in M.
”variable_fixed” is a boolean vector indicating whether the
variable at that index is fixed (lower bound == upper_bound)
and is thus bounded by an equality constraint.
”variable_bounds” is a compound matrix [lb ub] containing the
lower and upper bounds for all variables.
Return type:
NamedTuple
Notes
To accomodate non-zero equalities, the problem will add the variable
“const_one” which is a variable that equals one.
Deprecated since version 0.18.1: “DataFrame” option for array_type will be replaced with
“frame” in future versions.
It records a list of actions to be taken at a later time.
This is used to implement context managers that allow temporary
changes to a cobra.core.Model.
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.