Intro
Linear dynamic systems can describe a number of phenomena including system of first-order chemical reactions. An equation describing such systems is as follows:
\( \frac{d\mathbf{x}}{dt} = \mathbf{A}\mathbf{x} \)
Where \(\mathbf{x}\) is the vector of reactant concentrations, \(\mathbf{A}\) is the coefficient matrix corresponding to chemical rate constants.
Simple Case: X1 -> X2 -> X3
Encoding The Reactions As a Graph With igraph
For the sake of simplicity we’ll set all the chemical reaction rate constants to 1. The ODEs corresponding to this simple chain of chemical reactions are:
\( \frac{dX1}{dt} = 1 \cdot X1 + 0 \cdot X2 + 0 \cdot X3 \)
\( \frac{dX2}{dt} = 1 \cdot X1 - 1 \cdot X2 + 0 \cdot X3 \)
\( \frac{dX3}{dt} = 0 \cdot X1 + 1 \cdot X2 + 0 \cdot X3 \)
Firts, let’s encode the reaction graph.
Simulation With deSolve
Extracting matrix with ODE coefficients from the weighted directed graph.
X1 | X2 | X3 | |
---|---|---|---|
X1 | -1 | 0 | 0 |
X2 | 1 | -1 | 0 |
X3 | 0 | 1 | 0 |
The core of the simulation.
Plotting Simulation Results
More Complicated Example
Generating Random Reaction Graph
Now we randomize the kinetic rate constants. Note we set them to follow the log-normal distribution. That is all of them will be positive. In the coefficient matrix, the edge weights correspond to kinetic constants of the edges point to a given node. Therefore they will reflect the influx and thus can be only positive. For negative kinetic constant we’ll have to invert the directionality of the edge.
Initial concentrations of reactants can be distributed across the species anyway we want. Here we’ll place all the initial concentration into the node with the mose outflux. The code below defines which one is the “source” node for plotting purposes.
Plotting reaction graph with edge weights reflecting the fluxes.
Alternative plotting with ggraph
Simulation
Extracting the coefficient matrix.
X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | X9 | X10 | |
---|---|---|---|---|---|---|---|---|---|---|
X1 | -1.06 | 0.00 | 0.00 | 0.00 | 1.95 | 0.00 | 1.15 | 0.00 | 0.00 | 0 |
X2 | 0.00 | -0.46 | 6.46 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0 |
X3 | 0.00 | 0.00 | -6.87 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.68 | 0 |
X4 | 0.00 | 0.00 | 0.00 | -6.14 | 0.22 | 0.45 | 1.50 | 0.00 | 0.00 | 0 |
X5 | 0.00 | 0.00 | 0.41 | 0.00 | -2.17 | 0.00 | 0.00 | 0.00 | 0.04 | 0 |
X6 | 1.06 | 0.00 | 0.00 | 0.54 | 0.00 | -1.28 | 0.00 | 0.00 | 0.00 | 0 |
X7 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.83 | -4.33 | 0.00 | 0.00 | 0 |
X8 | 0.00 | 0.46 | 0.00 | 4.82 | 0.00 | 0.00 | 0.00 | -1.63 | 2.24 | 0 |
X9 | 0.00 | 0.00 | 0.00 | 0.78 | 0.00 | 0.00 | 1.14 | 1.63 | -2.96 | 0 |
X10 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.55 | 0.00 | 0.00 | 0 |
Setting up initial concentrations, defining time interval and finally simulation.
Plotting The Simulation Results
Visualizing the results of the simulation.
Visualizing trajectory of each specie individually.