17.1.1.1.1.9. cobra.core.reaction
¶
Define the Reaction class.
17.1.1.1.1.9.1. Module Contents¶
17.1.1.1.1.9.1.1. Classes¶
Reaction is a class for holding information regarding |
-
class
cobra.core.reaction.
Reaction
(id=None, name='', subsystem='', lower_bound=0.0, upper_bound=None)[source]¶ Bases:
cobra.core.object.Object
Reaction is a class for holding information regarding a biochemical reaction in a cobra.Model object.
Reactions are by default irreversible with bounds (0.0, cobra.Configuration().upper_bound) if no bounds are provided on creation. To create an irreversible reaction use lower_bound=None, resulting in reaction bounds of (cobra.Configuration().lower_bound, cobra.Configuration().upper_bound).
- Parameters
-
property
flux_expression
(self)[source]¶ Forward flux expression
- Returns
The expression representing the the forward flux (if associated with model), otherwise None. Representing the net flux if model.reversible_encoding == ‘unsplit’ or None if reaction is not associated with a model
- Return type
sympy expression
-
property
forward_variable
(self)[source]¶ An optlang variable representing the forward flux
- Returns
An optlang variable for the forward flux or None if reaction is not associated with a model.
- Return type
optlang.interface.Variable
-
property
reverse_variable
(self)[source]¶ An optlang variable representing the reverse flux
- Returns
An optlang variable for the reverse flux or None if reaction is not associated with a model.
- Return type
optlang.interface.Variable
-
property
objective_coefficient
(self)[source]¶ Get the coefficient for this reaction in a linear objective (float)
Assuming that the objective of the associated model is summation of fluxes from a set of reactions, the coefficient for each reaction can be obtained individually using this property. A more general way is to use the model.objective property directly.
-
property
lower_bound
(self)[source]¶ Get or set the lower bound
Setting the lower bound (float) will also adjust the associated optlang variables associated with the reaction. Infeasible combinations, such as a lower bound higher than the current upper bound will update the other bound.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting the context.
-
property
upper_bound
(self)[source]¶ Get or set the upper bound
Setting the upper bound (float) will also adjust the associated optlang variables associated with the reaction. Infeasible combinations, such as a upper bound lower than the current lower bound will update the other bound.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting the context.
-
property
bounds
(self)[source]¶ Get or set the bounds directly from a tuple
Convenience method for setting upper and lower bounds in one line using a tuple of lower and upper bound. Invalid bounds will raise an AssertionError.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting the context.
-
property
flux
(self)[source]¶ The flux value in the most recent solution.
Flux is the primal value of the corresponding variable in the model.
Warning
Accessing reaction fluxes through a Solution object is the safer, preferred, and only guaranteed to be correct way. You can see how to do so easily in the examples.
Reaction flux is retrieved from the currently defined self._model.solver. The solver status is checked but there are no guarantees that the current solver state is the one you are looking for.
If you modify the underlying model after an optimization, you will retrieve the old optimization values.
- Raises
RuntimeError – If the underlying model was never optimized beforehand or the reaction is not part of a model.
OptimizationError – If the solver status is anything other than ‘optimal’.
AssertionError – If the flux value is not within the bounds.
Examples
>>> import cobra.test >>> model = cobra.test.create_test_model("textbook") >>> solution = model.optimize() >>> model.reactions.PFK.flux 7.477381962160283 >>> solution.fluxes.PFK 7.4773819621602833
-
property
reduced_cost
(self)[source]¶ The reduced cost in the most recent solution.
Reduced cost is the dual value of the corresponding variable in the model.
Warning
Accessing reduced costs through a Solution object is the safer, preferred, and only guaranteed to be correct way. You can see how to do so easily in the examples.
Reduced cost is retrieved from the currently defined self._model.solver. The solver status is checked but there are no guarantees that the current solver state is the one you are looking for.
If you modify the underlying model after an optimization, you will retrieve the old optimization values.
- Raises
RuntimeError – If the underlying model was never optimized beforehand or the reaction is not part of a model.
OptimizationError – If the solver status is anything other than ‘optimal’.
Examples
>>> import cobra.test >>> model = cobra.test.create_test_model("textbook") >>> solution = model.optimize() >>> model.reactions.PFK.reduced_cost -8.673617379884035e-18 >>> solution.reduced_costs.PFK -8.6736173798840355e-18
-
property
gene_name_reaction_rule
(self)[source]¶ Display gene_reaction_rule with names intead.
Do NOT use this string for computation. It is intended to give a representation of the rule using more familiar gene names instead of the often cryptic ids.
-
property
functional
(self)[source]¶ All required enzymes for reaction are functional.
- Returns
True if the gene-protein-reaction (GPR) rule is fulfilled for this reaction, or if reaction is not associated to a model, otherwise False.
- Return type
-
property
x
(self)[source]¶ The flux through the reaction in the most recent solution.
Flux values are computed from the primal values of the variables in the solution.
-
property
y
(self)[source]¶ The reduced cost of the reaction in the most recent solution.
Reduced costs are computed from the dual values of the variables in the solution.
-
property
reversibility
(self)[source]¶ Whether the reaction can proceed in both directions (reversible)
This is computed from the current upper and lower bounds.
-
property
boundary
(self)[source]¶ Whether or not this reaction is an exchange reaction.
Returns True if the reaction has either no products or reactants.
-
_update_awareness
(self)[source]¶ Make sure all metabolites and genes that are associated with this reaction are aware of it.
-
remove_from_model
(self, remove_orphans=False)[source]¶ Removes the reaction from a model.
This removes all associations between a reaction the associated model, metabolites and genes.
The change is reverted upon exit when using the model as a context.
- Parameters
remove_orphans (bool) – Remove orphaned genes and metabolites from the model as well
-
delete
(self, remove_orphans=False)[source]¶ Removes the reaction from a model.
This removes all associations between a reaction the associated model, metabolites and genes.
The change is reverted upon exit when using the model as a context.
Deprecated, use reaction.remove_from_model instead.
- Parameters
remove_orphans (bool) – Remove orphaned genes and metabolites from the model as well
-
__setstate__
(self, state)[source]¶ Probably not necessary to set _model as the cobra.Model that contains self sets the _model attribute for all metabolites and genes in the reaction.
However, to increase performance speed we do want to let the metabolite and gene know that they are employed in this reaction
-
__add__
(self, other)[source]¶ Add two reactions
The stoichiometry will be the combined stoichiometry of the two reactions, and the gene reaction rule will be both rules combined by an and. All other attributes (i.e. reaction bounds) will match those of the first reaction
-
__imul__
(self, coefficient)[source]¶ Scale coefficients in a reaction by a given value
E.g. A -> B becomes 2A -> 2B.
If coefficient is less than zero, the reaction is reversed and the bounds are swapped.
-
get_coefficient
(self, metabolite_id)[source]¶ Return the stoichiometric coefficient of a metabolite.
- Parameters
metabolite_id (str or cobra.Metabolite) –
-
get_coefficients
(self, metabolite_ids)[source]¶ Return the stoichiometric coefficients for a list of metabolites.
- Parameters
metabolite_ids (iterable) – Containing
str
or ``cobra.Metabolite``s.
-
add_metabolites
(self, metabolites_to_add, combine=True, reversibly=True)[source]¶ Add metabolites and stoichiometric coefficients to the reaction. If the final coefficient for a metabolite is 0 then it is removed from the reaction.
The change is reverted upon exit when using the model as a context.
- Parameters
metabolites_to_add (dict) – Dictionary with metabolite objects or metabolite identifiers as keys and coefficients as values. If keys are strings (name of a metabolite) the reaction must already be part of a model and a metabolite with the given name must exist in the model.
combine (bool) – Describes behavior a metabolite already exists in the reaction. True causes the coefficients to be added. False causes the coefficient to be replaced.
reversibly (bool) – Whether to add the change to the context to make the change reversibly or not (primarily intended for internal use).
-
subtract_metabolites
(self, metabolites, combine=True, reversibly=True)[source]¶ Subtract metabolites from a reaction.
That means add the metabolites with -1*coefficient. If the final coefficient for a metabolite is 0 then the metabolite is removed from the reaction.
Notes
A final coefficient < 0 implies a reactant.
The change is reverted upon exit when using the model as a context.
- Parameters
metabolites (dict) – Dictionary where the keys are of class Metabolite and the values are the coefficients. These metabolites will be added to the reaction.
combine (bool) – Describes behavior a metabolite already exists in the reaction. True causes the coefficients to be added. False causes the coefficient to be replaced.
reversibly (bool) – Whether to add the change to the context to make the change reversibly or not (primarily intended for internal use).
-
build_reaction_string
(self, use_metabolite_names=False)[source]¶ Generate a human readable reaction string
-
check_mass_balance
(self)[source]¶ Compute mass and charge balance for the reaction
returns a dict of {element: amount} for unbalanced elements. “charge” is treated as an element in this dict This should be empty for balanced reactions.
-
_associate_gene
(self, cobra_gene)[source]¶ Associates a cobra.Gene object with a cobra.Reaction.
- Parameters
cobra_gene (cobra.core.Gene.Gene) –
-
_dissociate_gene
(self, cobra_gene)[source]¶ Dissociates a cobra.Gene object with a cobra.Reaction.
- Parameters
cobra_gene (cobra.core.Gene.Gene) –
-
build_reaction_from_string
(self, reaction_str, verbose=True, fwd_arrow=None, rev_arrow=None, reversible_arrow=None, term_split='+')[source]¶ Builds reaction from reaction equation reaction_str using parser
Takes a string and using the specifications supplied in the optional arguments infers a set of metabolites, metabolite compartments and stoichiometries for the reaction. It also infers the reversibility of the reaction from the reaction arrow.
Changes to the associated model are reverted upon exit when using the model as a context.
- Parameters
reaction_str (string) – a string containing a reaction formula (equation)
verbose (bool) – setting verbosity of function
fwd_arrow (re.compile) – for forward irreversible reaction arrows
rev_arrow (re.compile) – for backward irreversible reaction arrows
reversible_arrow (re.compile) – for reversible reaction arrows
term_split (string) – dividing individual metabolite entries
-
summary
(self, solution=None, fva=None)[source]¶ Create a summary of the reaction flux.
- Parameters
solution (cobra.Solution, optional) – A previous model solution to use for generating the summary. If
None
, the summary method will generate a parsimonious flux distribution (default None).fva (pandas.DataFrame or float, optional) – Whether or not to include flux variability analysis in the output. If given, fva should either be a previous FVA solution matching the model or a float between 0 and 1 representing the fraction of the optimum objective to be searched (default None).
- Returns
- Return type
cobra.summary.ReactionSummary
See also
Metabolite.summary()
,Model.summary()