biocrnpyler.core.GeneralPropensity

class biocrnpyler.core.GeneralPropensity(propensity_function: str, propensity_species: List[Species], propensity_parameters: List[ParameterEntry])[source]

Bases: Propensity

Propensity with user-defined formula string.

A GeneralPropensity allows specification of arbitrary kinetic rate laws using a formula string. The formula can reference species and parameters that are validated and tracked.

Parameters:
  • propensity_function (str) – Valid mathematical formula as a string (e.g., ‘k*S1*S2’). Must contain all referenced species and parameters.

  • propensity_species (list of Species) – List of Species objects used in the formula. Each species must appear in the propensity_function string.

  • propensity_parameters (list of ParameterEntry) – List of ParameterEntry objects used in the formula. Each parameter name must appear in the propensity_function string.

Attributes:
  • propensity_function (str) – The mathematical formula defining the rate law.

  • name (str) – Set to ‘general’ for this propensity type.

Raises:
  • TypeError – If propensity_species or propensity_parameters contain invalid types.

  • ValueError – If species or parameters in lists do not appear in the formula.

See also

MassAction

Mass action kinetics propensity.

Hill

Hill-type propensities.

Notes

The propensity_function string must be a valid mathematical expression that can be parsed by libsbml.parseL3Formula(). It can include:

  • Arithmetic operators: +, -, *, /, ^

  • Mathematical functions: exp, log, sin, cos, etc.

  • Species names (as strings matching their representation)

  • Parameter names (matching ParameterEntry.parameter_name)

Examples

Create a custom Michaelis-Menten propensity:

>>> S = bcp.Species('S')
>>> E = bcp.Species('E')
>>> kcat = bcp.ParameterEntry('kcat', 0.1)
>>> Km = bcp.ParameterEntry('Km', 10.0)
>>> prop = bcp.GeneralPropensity(
...     propensity_function='kcat*E*S/(Km + S)',
...     propensity_species=[S, E],
...     propensity_parameters=[kcat, Km]
... )

Create a custom regulatory function:

>>> X = bcp.Species('X')
>>> Y = bcp.Species('Y')
>>> k1 = bcp.ParameterEntry('k1', 1.0)
>>> k2 = bcp.ParameterEntry('k2', 0.5)
>>> prop = bcp.GeneralPropensity(
...     propensity_function='k1*X + k2*Y^2',
...     propensity_species=[X, Y],
...     propensity_parameters=[k1, k2]
... )

Methods

create_kinetic_law

Create SBML kinetic law using the propensity_function string.

from_dict

Create a propensity from a dictionary.

get_available_propensities

Get all available propensity subclasses.

is_valid_propensity

Check if an object is a valid Propensity subclass instance.

pretty_print

Generate human-readable string representation of propensity.

pretty_print_parameters

Generate formatted string of all propensity parameters.

pretty_print_rate

Return the propensity function formula string.

__eq__(other)[source]

Test equality between propensities.

Parameters:

other (Propensity) – Other propensity to compare with.

Returns:

True if propensities have the same class and propensity_dict.

Return type:

bool

create_kinetic_law(model, sbml_reaction, **kwargs)[source]

Create SBML kinetic law using the propensity_function string.

Translates species and parameter names to SBML identifiers and creates the kinetic law from the formula string.

Parameters:
  • model (libsbml.Model) – SBML model object.

  • sbml_reaction (libsbml.Reaction) – SBML reaction to add kinetic law to.

  • **kwargs – Additional keyword arguments (unused for GeneralPropensity).

Returns:

Created SBML kinetic law object.

Return type:

libsbml.KineticLaw

Raises:

ValueError – If the propensity_function cannot be parsed as valid SBML formula.

classmethod from_dict(propensity_dict)[source]

Create a propensity from a dictionary.

Parameters:

propensity_dict (dict) – Dictionary with ‘parameters’ and ‘species’ keys containing parameter and species values.

Returns:

New instance of the propensity class.

Return type:

Propensity

static get_available_propensities() Set[source]

Get all available propensity subclasses.

Returns:

Set of all Propensity subclass types available in BioCRNpyler.

Return type:

set

Examples

>>> propensities = bcp.Propensity.get_available_propensities()
>>> bcp.MassAction in propensities
True
property is_reversible

Whether the propensity represents a reversible reaction.

Default is False. Subclasses override this for reversible kinetics.

Type:

bool

static is_valid_propensity(propensity_type) bool[source]

Check if an object is a valid Propensity subclass instance.

Recursively checks all subclasses of Propensity to determine if the given object is a valid propensity type.

Parameters:

propensity_type (object) – Object to check for Propensity validity.

Returns:

True if propensity_type is an instance of Propensity or any of its subclasses, False otherwise.

Return type:

bool

Examples

>>> prop = bcp.MassAction(k_forward=100.0)
>>> bcp.Propensity.is_valid_propensity(prop)
True
>>> bcp.Propensity.is_valid_propensity("not a propensity")
False
property k_forward

Forward rate constant.

Raises:

NotImplementedError – Must be implemented by subclasses that use rate constants.

Type:

Float

property k_reverse

Reverse rate constant for reversible reactions.

Raises:

NotImplementedError – Must be implemented by subclasses that use rate constants.

Type:

Float or None

pretty_print(show_parameters=True, **kwargs)[source]

Generate human-readable string representation of propensity.

Parameters:
  • show_parameters (bool, default True) – If True, includes parameter values in output.

  • **kwargs – Additional keyword arguments passed to formatting methods.

Returns:

Formatted string showing rate formula and optionally parameters.

Return type:

str

pretty_print_parameters(show_keys=True, **kwargs)[source]

Generate formatted string of all propensity parameters.

Parameters:
  • show_keys (bool, default True) – If True, shows search and found keys for ModelParameter objects (useful for debugging parameter lookup).

  • **kwargs – Additional formatting keyword arguments.

Returns:

Formatted string listing all parameters and their values.

Return type:

str

pretty_print_rate(**kwargs)[source]

Return the propensity function formula string.

Returns:

The propensity_function formula.

Return type:

str

property species: List

All species used in the propensity function.

Type:

List of Species