# 15.1.1.1.1.7. cobra.core.reaction¶

## 15.1.1.1.1.7.1. Module Contents¶

class cobra.core.reaction.Reaction(id=None, name="", subsystem="", lower_bound=0.0, upper_bound=1000.0, objective_coefficient=0.0)[source]

Reaction is a class for holding information regarding a biochemical reaction in a cobra.Model object.

Parameters: id (string) – The identifier to associate with this reaction name (string) – A human readable name for the reaction subsystem (string) – Subsystem where the reaction is meant to occur lower_bound (float) – The lower flux bound upper_bound (float) – The upper flux bound
__init__(id=None, name="", subsystem="", lower_bound=0.0, upper_bound=1000.0, objective_coefficient=0.0)[source]
_set_id_with_model(value)[source]
reverse_id()

Generate the id of reverse_variable from the reaction’s id.

flux_expression()

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 sympy expression
forward_variable()

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. optlang.interface.Variable
reverse_variable()

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. optlang.interface.Variable
objective_coefficient()

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.

objective_coefficient(value)
__copy__()[source]
__deepcopy__(memo)[source]
lower_bound()

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.

lower_bound(value)
upper_bound()

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.

upper_bound(value)
bounds()

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.

bounds(value)
flux()

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

reduced_cost()

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

metabolites()
genes()
gene_reaction_rule()
gene_reaction_rule(new_rule)
gene_name_reaction_rule()

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.

functional()

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. bool
x()

The flux through the reaction in the most recent solution.

Flux values are computed from the primal values of the variables in the solution.

y()

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.

reversibility()

Whether the reaction can proceed in both directions (reversible)

This is computed from the current upper and lower bounds.

reversibility(value)
boundary()

Whether or not this reaction is an exchange reaction.

Returns True if the reaction has either no products or reactants.

model()

returns the model the reaction is a part of

_update_awareness()[source]

Make sure all metabolites and genes that are associated with this reaction are aware of it.

remove_from_model(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(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
__setstate__(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

copy()[source]

Copy a reaction

The referenced metabolites and genes are also copied.

__add__(other)[source]

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

__iadd__(other)[source]
__sub__(other)[source]
__isub__(other)[source]
__imul__(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.

__mul__(coefficient)[source]
reactants()

Return a list of reactants for the reaction.

products()

Return a list of products for the reaction

get_coefficient(metabolite_id)[source]

Return the stoichiometric coefficient of a metabolite.

Parameters: metabolite_id (str or cobra.Metabolite) –
get_coefficients(metabolite_ids)[source]

Return the stoichiometric coefficients for a list of metabolites.

Parameters: metabolite_ids (iterable) – Containing str or cobra.Metabolites.
add_metabolites(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(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).
reaction()

reaction(value)
build_reaction_string(use_metabolite_names=False)[source]

Generate a human readable reaction string

check_mass_balance()[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.

compartments()

lists compartments the metabolites are in

get_compartments()[source]

lists compartments the metabolites are in

_associate_gene(cobra_gene)[source]

Associates a cobra.Gene object with a cobra.Reaction.

Parameters: cobra_gene (cobra.core.Gene.Gene) –
_dissociate_gene(cobra_gene)[source]

Dissociates a cobra.Gene object with a cobra.Reaction.

Parameters: cobra_gene (cobra.core.Gene.Gene) –
knock_out()[source]

Knockout reaction by setting its bounds to zero.

build_reaction_from_string(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
__str__()[source]
_repr_html_()[source]
cobra.core.reaction.separate_forward_and_reverse_bounds(lower_bound, upper_bound)[source]

Split a given (lower_bound, upper_bound) interval into a negative component and a positive component. Negative components are negated (returns positive ranges) and flipped for usage with forward and reverse reactions bounds

Parameters: lower_bound (float) – The lower flux bound upper_bound (float) – The upper flux bound
cobra.core.reaction.update_forward_and_reverse_bounds(reaction, direction="both")[source]

For the given reaction, update the bounds in the forward and reverse variable bounds.

Parameters: reaction (cobra.Reaction) – The reaction to operate on direction (string) – Either ‘both’, ‘upper’ or ‘lower’ for updating the corresponding flux bounds.