Skip to content

Dynamic multi-species metabolic system

Two E. coli core strains that differ in maximal glucose uptake on one medium.

  • Class: DynamicMultispeciesFBASystem
  • Import: from skcomplex.systems.biology.ecology import DynamicMultispeciesFBASystem
  • Field: biology/ecology
  • Topics: dynamic flux balance, competition, linear program, metabolism, co-simulation
  • References:
    • Raman 2021
    • Zhuang et al. 2011

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.biology.ecology import DynamicMultispeciesFBASystem

trajectory = DynamicMultispeciesFBASystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
fast_vmax 10.0
slow_vmax 8.0
biomass 0.01
glucose 10.0
km 0.015
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time continuous
Space none
Laws equation, rate
Randomness unknown
Domains real
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class DynamicMultispeciesFBASystem(BaseSystem):
    """Two E. coli core strains that differ in maximal glucose uptake on one medium.

    It needs the extra `metabolism`.

    Each strain has its own program, uptake kinetics, and biomass, and they read one glucose pool (Raman 2021, Section
    11.1.3.4). The strain with the larger uptake grows faster, so its share rises until the glucose runs out. The two
    strains and their uptakes of 10 and 8 are an illustration of the framework, whose example in the source is the
    competition between Rhodoferax and Geobacter (Zhuang et al. 2011).

    Args:
        fast_vmax:
            The largest glucose uptake of the fast strain, in mmol per gDW per h.

        slow_vmax:
            The largest glucose uptake of the slow strain, in mmol per gDW per h.

        biomass:
            The initial biomass of each strain in gDW per L.

        glucose:
            The initial glucose in mM.

        km:
            The half-saturation concentration of glucose in mM.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Raises:
        MissingExtraError:
            The extra is not installed.

    Examples:
        >>> from skcomplex.systems.biology.ecology import DynamicMultispeciesFBASystem
        >>> [element.label for element in DynamicMultispeciesFBASystem().prepare().system_.systems[1].elements]
        ['biomass_fast', 'biomass_slow', 'glucose']
    """

    field = 'biology/ecology'
    topics = ('dynamic flux balance', 'competition', 'linear program', 'metabolism', 'co-simulation')
    references = ('Raman 2021', 'Zhuang et al. 2011')
    until = 8.0
    dt = SYNCHRONISATION

    def __init__(
        self,
        *,
        fast_vmax: float = 10.0,
        slow_vmax: float = 8.0,
        biomass: float = 0.01,
        glucose: float = 10.0,
        km: float = 0.015,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.fast_vmax = fast_vmax
        self.slow_vmax = slow_vmax
        self.biomass = biomass
        self.glucose = glucose
        self.km = km

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of two E. coli core strains that differ in glucose uptake.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        cells = []
        networks = []
        interactions = []
        for name, vmax in (('fast', self.fast_vmax), ('slow', self.slow_vmax)):
            network = load_network(
                'textbook',
                f'cell_{name}',
                label=f'fba_{name}',
                needed_by='dynamic_multispecies_fba',
            ).set_params(parsimonious=True, infeasible='zero')
            cells.append(MetabolicCellElement(network.cell, values=network.get_values()))
            networks.append(network)
            interactions += [
                ExchangeInteraction(
                    f'uptake_{name}',
                    cell=f'fba/cell_{name}',
                    biomass=f'culture/biomass_{name}',
                    substrate='culture/glucose',
                    reaction=GLUCOSE_EXCHANGE,
                    vmax=vmax,
                    km=self.km,
                    interval=SYNCHRONISATION,
                ),
                GrowthInteraction(
                    f'growth_{name}',
                    cell=f'fba/cell_{name}',
                    biomass=f'culture/biomass_{name}',
                    reaction=GROWTH,
                ),
            ]
        self.system_ = System(
            self.label,
            systems=[
                System('fba', elements=cells, interactions=networks),
                System(
                    'culture',
                    elements=[
                        BiomassElement('biomass_fast', x=self.biomass),
                        BiomassElement('biomass_slow', x=self.biomass),
                        SubstrateElement('glucose', c=self.glucose),
                    ],
                ),
            ],
            interactions=interactions,
            solver=CoSimulationSolver(sync_dt=SYNCHRONISATION, solvers={'culture': RK4Solver(dt=ODE_STEP)}),
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • ExchangeInteraction, from Interaction
  • GrowthInteraction, from Interaction
  • FluxBalanceInteraction, from Interaction
class ExchangeInteraction(Interaction):
    """Exchange of a metabolite between a metabolic network and its medium.

    The exchange is bounded by Michaelis-Menten uptake.

    The law bounds the uptake, the negative flux of the exchange reaction, by `vmax c / (km + c)` and, with an interval,
    by what the medium holds per biomass over the interval (Raman 2021, Section 9.6). The medium changes at the rate
    `dc/dt = v x`, and a depleted medium loses nothing more.

    Args:
        label:
            The label of the interaction.

        cell:
            The path of the cell whose values hold the fluxes of the network.

        biomass:
            The path of the biomass.

        substrate:
            The path of the metabolite.

        reaction:
            The name of the exchange reaction.

        vmax:
            The largest uptake.

        km:
            The half-saturation concentration.

        interval:
            The time over which the medium must last, or `None` for no such bound.

    Examples:
        >>> from skcomplex.systems.biology.common.metabolism import ExchangeInteraction
        >>> ExchangeInteraction('uptake', reaction='EX_glc__D_e').vmax
        10.0
    """

    def __init__(
        self,
        label: str,
        *,
        cell: Role = 'cell',
        biomass: Role = 'biomass',
        substrate: Role = 'substrate',
        reaction: str = 'EX_glc__D_e',
        vmax: Annotated[float, POSITIVE] = 10.0,
        km: Annotated[float, POSITIVE] = 0.015,
        interval: float | None = None,
    ) -> None:
        super().__init__(label)
        self.cell = cell
        self.biomass = biomass
        self.substrate = substrate
        self.reaction = reaction
        self.vmax = vmax
        self.km = km
        self.interval = interval

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the bounds of the uptake and the rate of the medium.

        Args:
            context:
                The namespace of the roles and parameters.
        """
        super()._prepare(context)
        flux = getattr(context.cell, FluxBalanceInteraction.get_flux_name(self.reaction))
        amount, biomass = context.substrate.c, context.biomass.x
        held = where(amount > 0, amount, 0)
        laws: list[BaseLaw] = [ConstraintLaw(flux >= -context.vmax * held / (context.km + held), target=flux)]
        if self.interval is not None:
            laws.append(ConstraintLaw(flux >= -held / (biomass * float(self.interval)), target=flux))
        laws.append(RateLaw(amount, flux * biomass * where(either(amount > 0, flux > 0), 1, 0)))
        self.laws_ = laws

class GrowthInteraction(Interaction):
    """Growth of biomass at the rate a reaction of a metabolic network gives.

    The biomass follows `dx/dt = v x`.

    Args:
        label:
            The label of the interaction.

        cell:
            The path of the cell whose values hold the fluxes of the network.

        biomass:
            The path of the biomass.

        reaction:
            The name of the reaction of growth.

    Examples:
        >>> from skcomplex.systems.biology.common.metabolism import GrowthInteraction
        >>> GrowthInteraction('growth', cell='cell', biomass='culture', reaction='grow').reaction
        'grow'
    """

    def __init__(
        self,
        label: str,
        *,
        cell: Role = 'cell',
        biomass: Role = 'biomass',
        reaction: str = 'Biomass_Ecoli_core',
    ) -> None:
        super().__init__(label)
        self.cell = cell
        self.biomass = biomass
        self.reaction = reaction

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the rate of the biomass.

        Args:
            context:
                The namespace of the cell and the biomass.
        """
        super()._prepare(context)
        flux = getattr(context.cell, FluxBalanceInteraction.get_flux_name(self.reaction))
        self.laws_: list[BaseLaw] = [RateLaw(context.biomass.x, flux * context.biomass.x)]

class MetabolicCellElement(Element):
    """Cell whose values hold the fluxes, the bounds, and the gene switches of a metabolic network.

    It is drawn as the linear program of its network, and `FluxBalanceInteraction.get_values` gives its values.

    Examples:
        >>> from skcomplex.systems.biology.common.metabolism import FluxBalanceInteraction, MetabolicCellElement
        >>> from skcomplex.systems.biology.common.metabolism import ReactionSpec
        >>> network = FluxBalanceInteraction('fba', reactions={'r': ReactionSpec({'A': -1.0})})
        >>> MetabolicCellElement('cell', values=network.get_values()).values['upper_r']
        1000.0
    """

    visual = VisualSpec('linear_program')

class FluxBalanceInteraction(Interaction):
    """Flux balance of a metabolic network, which sets its fluxes to the best ones its balances and bounds allow.

    A network is a set of reactions, each with the coefficients of its metabolites, its bounds, and the rule of the
    genes that make its enzymes. The interaction sets the flux `v_<reaction>` of its cell, one per reaction, to the
    optimum of a linear program: every metabolite balances, `sum_r S_mr v_r = 0`, every flux stays between the values
    `lower_<reaction>` and `upper_<reaction>` of the cell, and the weighted sum of the fluxes of `objective` is
    greatest (Raman 2021, Eq. 8.9). A reaction whose rule is false, read from the switches `g_<gene>` of the cell,
    carries no flux. The program is solved at every time of a run, so the network also drives a culture that changes
    in time, and `get_values` gives the values the cell starts with.

    Args:
        label:
            The label of the interaction.

        cell:
            The path of the cell whose values hold the fluxes, the bounds, and the switches.

        reactions:
            The reactions by name.

        objective:
            The coefficient of every reaction in the objective, or nothing for a network whose fluxes another objective
            chooses, such as the growth of a community.

        parsimonious:
            Whether to minimise the total absolute flux after the objective.

        infeasible:
            What a run does when the balances and bounds cannot all hold: `'raise'`, `'zero'` to set the fluxes to
            zero, or `'hold'` to keep their last values.

        name:
            The name of the network, or `None` for none.

        biomass:
            The path of the biomass that `threshold` reads, or `None` for none.

        threshold:
            The biomass above which the program is solved, or `None` for always. The fluxes are zero elsewhere, as at
            the empty boxes of a plate.

    Examples:
        >>> from skcomplex.core.components.elements import Element
        >>> from skcomplex.core.systems import System
        >>> from skcomplex.systems.biology.common.metabolism import FluxBalanceInteraction, ReactionSpec
        >>> reactions = {'uptake': ReactionSpec({'A': 1.0}, upper=4.0), 'use': ReactionSpec({'A': -1.0})}
        >>> fba = FluxBalanceInteraction('fba', cell='cell', reactions=reactions, objective={'use': 1.0})
        >>> cell = Element('cell', values=fba.get_values())
        >>> trajectory = System(elements=[cell], interactions=[fba]).prepare().simulate(until=0.0)
        >>> float(trajectory.get_values('cell.v_use')[0])
        4.0
    """

    def __init__(
        self,
        label: str = 'fba',
        *,
        cell: Role = 'cell',
        reactions: Mapping[str, ReactionSpec] = NO_REACTIONS,
        objective: Mapping[str, float] = NO_OBJECTIVE,
        parsimonious: bool = False,
        infeasible: str = 'raise',
        name: str | None = None,
        biomass: Role | None = None,
        threshold: float | None = None,
    ) -> None:
        super().__init__(label)
        self.cell = cell
        self.reactions = reactions
        self.objective = objective
        self.parsimonious = parsimonious
        self.infeasible = infeasible
        self.name = name
        self.biomass = biomass
        self.threshold = threshold

    @staticmethod
    @functools.cache
    def _get_rule_genes(rule: str) -> tuple[str, ...]:
        """Return the gene names in a rule, read once per rule.

        Compiling reads the rules of a network often.
        """
        if not rule.strip():
            return ()
        return tuple(node.id for node in ast.walk(ast.parse(rule, mode='eval')) if isinstance(node, ast.Name))

    @staticmethod
    def _get_identifier(name: str) -> str:
        """Return a name as a part of an identifier."""
        return re.sub(r'\W', '_', name)

    @classmethod
    def get_flux_name(cls, reaction: str) -> str:
        """Return the name of the value of the cell that holds the flux of a reaction.

        Args:
            reaction:
                The name of the reaction.

        Returns:
            name:
                The name of the value, such as `v_PGI` for the reaction `PGI`.
        """
        return f'v_{cls._get_identifier(reaction)}'

    @classmethod
    def get_gene_name(cls, gene: str) -> str:
        """Return the name of the value of the cell that holds the switch of a gene.

        Args:
            gene:
                The name of the gene.

        Returns:
            name:
                The name of the value, such as `g_b4025` for the gene `b4025`.
        """
        return f'g_{cls._get_identifier(gene)}'

    def get_genes_of(self) -> tuple[str, ...]:
        """Return the names of the genes that the rules of the reactions read.

        Returns:
            genes:
                The names, in the order the rules name them.
        """
        names = [name for reaction in self.reactions.values() for name in self._get_rule_genes(reaction.rule)]
        return tuple(dict.fromkeys(names))

    def get_values(self) -> dict[str, float]:
        """Return the values the cell of the network starts with.

        They are, for every reaction in order, its flux at zero and its bounds, and then the switch of every gene, on
        at 1.

        Returns:
            values:
                The values by name, such as `{'v_r': 0.0, 'lower_r': 0.0, 'upper_r': 1000.0, 'g_g': 1.0}`.

        Examples:
            >>> from skcomplex.systems.biology.common.metabolism import FluxBalanceInteraction, ReactionSpec
            >>> reactions = {'r': ReactionSpec({'A': -1.0}, upper=4.0, rule='g')}
            >>> FluxBalanceInteraction('fba', reactions=reactions).get_values()
            {'v_r': 0.0, 'lower_r': 0.0, 'upper_r': 4.0, 'g_g': 1.0}
        """
        values = {}
        for reaction, spec in self.reactions.items():
            identifier = self._get_identifier(reaction)
            values[f'v_{identifier}'] = 0.0
            values[f'lower_{identifier}'] = float(spec.lower)
            values[f'upper_{identifier}'] = float(spec.upper)
        return values | dict.fromkeys(map(self.get_gene_name, self.get_genes_of()), 1.0)

    def _get_gate(self, cell: SimpleNamespace, rule: str) -> Expression | int:
        """Return the condition of a rule on the switches of its genes.

        A reaction without genes has the condition 1.
        """
        if not rule.strip():
            return 1
        return both(self._get_condition(cell, ast.parse(rule, mode='eval').body))

    def _get_condition(self, cell: SimpleNamespace, node: ast.expr) -> Expression:
        """Return the condition of a node of a rule."""
        if isinstance(node, ast.BoolOp):
            combine = both if isinstance(node.op, ast.And) else either
            return combine(*(self._get_condition(cell, item) for item in node.values))
        if isinstance(node, ast.Name):
            return getattr(cell, self.get_gene_name(node.id)) > SWITCH_THRESHOLD
        error_msg = f'A gene rule of {self.label} holds {ast.dump(node)}. Use gene names, `and`, `or`, and parentheses.'
        raise ValidationError(error_msg)

    def _get_balances(self, cell: SimpleNamespace) -> list[ConstraintLaw]:
        """Return the balance of every metabolite."""
        terms: dict[str, list[Expression]] = {}
        for reaction, spec in self.reactions.items():
            flux = getattr(cell, self.get_flux_name(reaction))
            for metabolite, coefficient in spec.coefficients.items():
                terms.setdefault(metabolite, []).append(float(coefficient) * flux)
        return [ConstraintLaw(sum(items[1:], items[0]) == 0) for items in terms.values()]

    def _get_bounds(self, cell: SimpleNamespace) -> list[ConstraintLaw]:
        """Return the lower and upper bound of every reaction, gated by its rule."""
        bounds = []
        for reaction, spec in self.reactions.items():
            flux = getattr(cell, self.get_flux_name(reaction))
            gate = self._get_gate(cell, spec.rule)
            identifier = self._get_identifier(reaction)
            bounds.append(ConstraintLaw(flux >= getattr(cell, f'lower_{identifier}') * gate))
            bounds.append(ConstraintLaw(flux <= getattr(cell, f'upper_{identifier}') * gate))
        return bounds

    def _get_objective(self, cell: SimpleNamespace, biomass: SimpleNamespace | None) -> ObjectiveLaw:
        """Return the objective of the network over its fluxes.

        It is the greatest weighted sum of `objective`, then the smallest total absolute flux for a parsimonious
        network, or any point that the balances and bounds allow for a network without an objective. With a threshold,
        it is sought only where the biomass lies above it.
        """
        active = None if self.threshold is None or biomass is None else biomass.x > float(self.threshold)
        fluxes = [getattr(cell, self.get_flux_name(name)) for name in self.reactions]
        values = []
        if self.objective:
            terms = [float(weight) * getattr(cell, self.get_flux_name(name)) for name, weight in self.objective.items()]
            values.append(-sum(terms[1:], terms[0]))
            if self.parsimonious:
                absolutes = [abs(flux) for flux in fluxes]
                values.append(sum(absolutes[1:], absolutes[0]))
        return ObjectiveLaw(values, fluxes, active=active, infeasible=self.infeasible)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the objective of the network with its balances and bounds.

        Args:
            context:
                The namespace of the cell and the biomass.
        """
        super()._prepare(context)
        laws: list[BaseLaw] = [*self._get_balances(context.cell), *self._get_bounds(context.cell)]
        self.laws_ = [*laws, self._get_objective(context.cell, None if self.biomass is None else context.biomass)]

class BiomassElement(Element):
    """Biomass of a culture, in gDW per L.

    Args:
        label:
            The label of the element.

        x:
            The biomass in gDW per L.

        values:
            Further values by name.

        size:
            The number of members, or `None` for one.

        space:
            The space of the members, such as the plate of a culture with one biomass per cell, or `None`.

    Examples:
        >>> from skcomplex.systems.biology.common.metabolism import BiomassElement
        >>> BiomassElement('culture', x=0.01).x
        0.01
    """

    def __init__(
        self,
        label: str,
        *,
        x: float | npt.ArrayLike = 0.01,
        values: Mapping[str, object] | None = None,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, values=values, size=size, space=space)
        self.x = x

class SubstrateElement(Element):
    """Concentration of a metabolite of a medium, in mM.

    Args:
        label:
            The label of the element.

        c:
            The concentration in mM.

        values:
            Further values by name.

        size:
            The number of members, or `None` for one.

        space:
            The space of the members, such as the plate of a culture with one concentration per cell, or `None`.

    Examples:
        >>> from skcomplex.systems.biology.common.metabolism import SubstrateElement
        >>> SubstrateElement('glucose', c=10.0).c
        10.0
    """

    def __init__(
        self,
        label: str,
        *,
        c: float | npt.ArrayLike = 10.0,
        values: Mapping[str, object] | None = None,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, values=values, size=size, space=space)
        self.c = c