reactions
RL4CRN.iocrns.reactions
Reaction primitives for IOCRNs.
This module defines the core reaction objects used throughout the package:
-
Reactionis the abstract base class that stores reaction structure (reactants/products), parameter slots (possibly unknown), optional input-channel associations for parameters, and context hooks used when embedding reactions in an IOCRN or registering them in a reaction library. -
MassActionimplements standard mass-action kinetics with an optional scalar input modulation of the rate constant. -
HillProductionimplements regulated production from the empty complex using Hill-type activation/repression.
Reactions are designed to be:
- library-friendly: each reaction can be registered and retrieved by ID,
while equality is typically based on a structure-only
signature. - CRN-friendly: once a reaction is placed in an IOCRN, it can precompute index mappings (species/input labels → integer indices) for fast simulation.
Conventions:
- Stoichiometry is represented implicitly by repeated labels in
reactant_labels/product_labels(e.g.['A','A']means 2A). - Parameter vectors are aligned with
input_channelsandparams_controllability; unknown parameters are represented byNone.
Reaction
__init__(reactant_labels, product_labels, input_channels=[None], params=[None], params_controllability=[False], ID=None, signature=None)
Base class for reactions used in IOCRNs.
A Reaction defines:
- a structure (reactants/products, optional "inputs" that modulate parameters),
- a parameter vector (with optional unknown entries),
- bookkeeping fields used when the reaction is placed inside a CRN (species/input indices) and inside a library (reaction ID).
The class is intended to be subclassed by specific kinetic laws (e.g.
MassAction, HillProduction). Subclasses must implement
propensity and to_reaction_format.
Parameters are stored in parallel lists:
params[j]: numerical value (float-like) orNoneif unknown.params_controllability[j]: whether the parameter is controllable.input_channels[j]: name of the input signal multiplying/modulating that parameter, orNoneif the parameter is not input-dependent.
These lists must have identical length.
Notes
- The constructor sorts reactants/products and de-duplicates + sorts
input_labels(excludingNone) for deterministic behavior. - Equality (
==) is defined via the reactionsignatureonly. A signature is meant to identify the topology/structure of a reaction, ignoring parameter values.
| PARAMETER | DESCRIPTION |
|---|---|
reactant_labels
|
Labels of reactant species (can be empty for ∅).
|
product_labels
|
Labels of product species (can be empty for ∅).
|
input_channels
|
Input channel per parameter slot. Can contain
DEFAULT:
|
params
|
Parameter values per slot.
DEFAULT:
|
params_controllability
|
Boolean flag per slot indicating controllability.
DEFAULT:
|
ID
|
Optional integer identifier used when the reaction is registered in a
DEFAULT:
|
signature
|
Optional string uniquely identifying the structure of the reaction.
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
reactant_labels |
Sorted list of reactant labels.
|
product_labels |
Sorted list of product labels.
|
input_channels |
Input channel list, aligned with
|
params |
Parameter list (may contain
|
params_controllability |
Controllability list, aligned with
|
num_parameters |
Number of parameters (length of
|
input_labels |
Sorted list of unique non-
|
ID |
Reaction ID in a library context (or
|
signature |
Structural signature (or
|
crn |
Set by
|
get_ID()
Return the reaction's library ID (or None if unset).
set_ID(ID)
Set the reaction's library ID.
get_num_controllable_parameters()
Return the number of controllable parameters.
get_num_unknown_params()
Return the number of unknown parameters (None entries).
propensity(x, u)
Compute the propensity/rate contribution of this reaction.
Subclasses must implement this.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Full CRN state vector (species concentrations/counts).
|
u
|
Full input vector (input signal values).
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Propensity of the reaction under the provided state and inputs. |
__call__(x, u)
Alias for propensity.
set_crn_context(crn)
Attach the reaction to an IOCRN context.
Subclasses typically override this to precompute index arrays for fast propensity evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
crn
|
IOCRN instance providing label->index maps.
|
__eq__(other)
Check structural equivalence via signature.
Two reactions are considered equal if their signatures are equal.
set_library_context(reaction_library)
Resolve and set this reaction's ID from a reaction library.
Finds the first reaction in reaction_library with the same signature
(via __eq__) and sets ID accordingly.
| PARAMETER | DESCRIPTION |
|---|---|
reaction_library
|
A
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no matching reaction is found. |
to_reaction_format()
Serialize the reaction to the project's DSL/CRN text format.
Subclasses must implement this.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A line of text such as |
MassAction
Bases: Reaction
__init__(reactant_labels, product_labels, input_channels=[None], params=[None], params_controllability=[True])
Mass-action reaction with optional input modulation.
A mass-action reaction has the form:
with propensity
where:
- \(k\) is the (possibly unknown) rate constant,
- \(x_i\) are the reactant concentrations/counts,
- \(g(u)\) is an optional scalar input modulation.
In the current implementation:
- if the single
input_channelisNonethen \(g(u)=1\), - otherwise \(g(u)=u_{c}\) for the corresponding input channel/index.
- if the single
The parameter layout is fixed to a single scalar parameter:
- ``params = [k]
| PARAMETER | DESCRIPTION |
|---|---|
reactant_labels
|
Reactant species labels. Empty means a 0th-order reaction (∅ → ...).
|
product_labels
|
Product species labels. Empty means degradation (... → ∅).
|
input_channels
|
A single-element list
DEFAULT:
|
params
|
A single-element list
DEFAULT:
|
params_controllability
|
A single-element list indicating controllability of
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
rate_constant |
The mass-action rate constant
|
signature |
Structural signature (depends only on reactants/products).
|
num_continuous_parameters |
Always 1.
|
num_discrete_parameters |
Always 0.
|
num_unknown_params |
Number of unknown parameters (0 or 1).
|
set_parameters(params)
Set the rate constant.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Single-element list
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If |
get_involved_species()
Return sorted unique species involved in the reaction (reactants ∪ products).
get_involved_inputs()
Return input channel labels used by the reaction (excluding None).
get_stoichiometry_dict()
Return stoichiometry coefficients as a dictionary.
Reactants contribute -1 per occurrence, products +1 per occurrence.
| RETURNS | DESCRIPTION |
|---|---|
|
dict[str, int]: Mapping species label -> net stoichiometric coefficient. |
set_crn_context(crn)
Precompute indices for fast propensity evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
crn
|
IOCRN instance providing label->index maps.
|
propensity(x, u)
Compute the mass-action propensity.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Full species state vector of the parent IOCRN.
|
u
|
Full input vector of the parent IOCRN.
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Reaction propensity. |
Notes
This implementation multiplies all reactant entries via np.prod.
For repeated reactants, the state is indexed with repeated indices,
so the product includes appropriate powers.
__str__()
Human-readable string representation.
to_reaction_format()
Serialize to the CRN DSL.
| RETURNS | DESCRIPTION |
|---|---|
str
|
DSL line, e.g. |
Notes
The DSL formatting here distinguishes the "zero-order input generation" special-case when there are no reactants but an input channel exists.
get_ID()
Return the reaction's library ID (or None if unset).
set_ID(ID)
Set the reaction's library ID.
get_num_controllable_parameters()
Return the number of controllable parameters.
get_num_unknown_params()
Return the number of unknown parameters (None entries).
__call__(x, u)
Alias for propensity.
__eq__(other)
Check structural equivalence via signature.
Two reactions are considered equal if their signatures are equal.
set_library_context(reaction_library)
Resolve and set this reaction's ID from a reaction library.
Finds the first reaction in reaction_library with the same signature
(via __eq__) and sets ID accordingly.
| PARAMETER | DESCRIPTION |
|---|---|
reaction_library
|
A
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no matching reaction is found. |
HillProduction
Bases: Reaction
__init__(product_labels, activator_labels, repressor_labels, input_channels=[None], params=[None], params_controllability=[True])
Hill-regulated production reaction (Hill coefficients fixed to 1).
This reaction represents regulated production from the empty complex:
with a propensity of the form:
where: - \(b\) is the basal production rate, - \(V_{\max}\) is the maximal regulated production rate, - \(A\) is the set of activators, \(R\) the set of repressors, - Hill coefficients are fixed to 1 in this implementation.
Parameter layout (after sorting activator/repressor labels):
params = [b, Vmax, Ka_1, ..., Ka_|A|, Kr_1, ..., Kr_|R|]
Note
Each parameter slot may be associated with an input_channel label.
In the current implementation, the input vector u is collected but not
applied inside propensity. This is a placeholder for future
extensions where parameters may be modulated by inputs.
| PARAMETER | DESCRIPTION |
|---|---|
product_labels
|
Product species labels (non-empty).
|
activator_labels
|
Activator species labels (may be empty).
|
repressor_labels
|
Repressor species labels (may be empty).
|
input_channels
|
Input channel list aligned with
DEFAULT:
|
params
|
Parameter list aligned with
DEFAULT:
|
params_controllability
|
Controllability flags aligned with
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
basal_rate |
Basal rate
|
maximal_rate |
Maximal rate
|
activator_dissociation_constants |
List of
|
repressor_dissociation_constants |
List of
|
signature |
Structural signature independent of parameter values.
|
num_continuous_parameters |
|
num_discrete_parameters |
0 (Hill coefficients fixed to 1 here).
|
num_unknown_params |
Number of unknown parameters (
|
set_parameters(params)
Set the full parameter vector.
Layout
[b, Vmax, Ka_1, ..., Ka_|A|, Kr_1, ..., Kr_|R|]
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Parameter vector matching the layout above.
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the provided vector has the wrong length. |
get_involved_species()
Return sorted unique species involved (products ∪ activators ∪ repressors).
get_involved_inputs()
Returns a list of all inputs involved in the reaction. Returns: - input_labels: list of strings representing the labels of the involved inputs.
get_stoichiometry_dict()
Return stoichiometry coefficients as a dictionary (products only).
| RETURNS | DESCRIPTION |
|---|---|
|
dict[str, int]: Mapping product label -> stoichiometric coefficient. |
set_crn_context(crn)
Precompute indices for fast propensity evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
crn
|
IOCRN instance providing label->index maps.
|
propensity(x, u)
Compute Hill-regulated production propensity.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Full species state vector of the parent IOCRN.
|
u
|
Full input vector of the parent IOCRN.
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Reaction propensity. |
Notes
Input modulations of Hill coefficients are fixed to 1. This method currently does not
apply input modulation to parameters even if input_channels are set.
__str__()
Human-readable string representation.
to_reaction_format()
Serialize to the CRN DSL.
| RETURNS | DESCRIPTION |
|---|---|
str
|
DSL line, e.g. |
Notes
This method emits a hill(...) construct assumed to be understood by
your SSA/DSL backend. If the backend expects a different argument
order or syntax, adapt accordingly.
get_ID()
Return the reaction's library ID (or None if unset).
set_ID(ID)
Set the reaction's library ID.
get_num_controllable_parameters()
Return the number of controllable parameters.
get_num_unknown_params()
Return the number of unknown parameters (None entries).
__call__(x, u)
Alias for propensity.
__eq__(other)
Check structural equivalence via signature.
Two reactions are considered equal if their signatures are equal.
set_library_context(reaction_library)
Resolve and set this reaction's ID from a reaction library.
Finds the first reaction in reaction_library with the same signature
(via __eq__) and sets ID accordingly.
| PARAMETER | DESCRIPTION |
|---|---|
reaction_library
|
A
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no matching reaction is found. |
ActiveDegradation
Bases: Reaction
__init__(substrate_label, enzyme_label, input_channels=[None], params=[None], params_controllability=[True])
Active degradation reaction (Hill coefficients fixed to 1).
This reaction represents regulated degradation of a substrate species by an enzyme:
with a propensity of the form:
where: - \(a\) is the maximal degradation rate per degradation enzyme, - \(D\) is the degradation enzyme concentration (picked from the species), - \(K\) is the Michaelis constant for degradation, - Hill coefficients are fixed to 1 in this implementation.
Parameter layout (after sorting activator/repressor labels):
params = [a, K]
Note
Each parameter slot may be associated with an input_channel label.
In the current implementation, the input vector u is collected but not
applied inside propensity. This is a placeholder for future
extensions where parameters may be modulated by inputs.
| PARAMETER | DESCRIPTION |
|---|---|
substrate_label
|
Substrate species label (non-empty).
|
enzyme_label
|
Enzyme species label (non-empty).
|
input_channels
|
Input channel list aligned with
DEFAULT:
|
params
|
Parameter list aligned with
DEFAULT:
|
params_controllability
|
Controllability flags aligned with
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
maximal_rate |
Maximal rate
|
michaelis_constant |
Michaelis constant
|
signature |
Structural signature independent of parameter values.
|
num_continuous_parameters |
|
num_discrete_parameters |
0 (Hill coefficients fixed to 1 here).
|
num_unknown_params |
Number of unknown parameters (
|
set_parameters(params)
Set the full parameter vector.
Layout
[a, K]
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Parameter vector matching the layout above.
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the provided vector has the wrong length. |
get_involved_species()
Return sorted unique species involved (substrate ∪ enzyme).
get_involved_inputs()
Returns a list of all inputs involved in the reaction. Returns: - input_labels: list of strings representing the labels of the involved inputs.
get_stoichiometry_dict()
Return stoichiometry coefficients as a dictionary (reactant only).
| RETURNS | DESCRIPTION |
|---|---|
|
dict[str, int]: Mapping reactant label -> stoichiometric coefficient. |
set_crn_context(crn)
Precompute indices for fast propensity evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
crn
|
IOCRN instance providing label->index maps.
|
propensity(x, u)
Compute Active Degradation propensity.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Full species state vector of the parent IOCRN.
|
u
|
Full input vector of the parent IOCRN.
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Reaction propensity. |
__str__()
Human-readable string representation.
get_ID()
Return the reaction's library ID (or None if unset).
set_ID(ID)
Set the reaction's library ID.
get_num_controllable_parameters()
Return the number of controllable parameters.
get_num_unknown_params()
Return the number of unknown parameters (None entries).
__call__(x, u)
Alias for propensity.
__eq__(other)
Check structural equivalence via signature.
Two reactions are considered equal if their signatures are equal.
set_library_context(reaction_library)
Resolve and set this reaction's ID from a reaction library.
Finds the first reaction in reaction_library with the same signature
(via __eq__) and sets ID accordingly.
| PARAMETER | DESCRIPTION |
|---|---|
reaction_library
|
A
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no matching reaction is found. |
to_reaction_format()
Serialize the reaction to the project's DSL/CRN text format.
Subclasses must implement this.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A line of text such as |