Getting started
You need only one of the two languages to follow this companion. The examples are self-contained scripts that you can run as they are, and then modify.
Getting the code
Download the repository as a zip file from https://github.com/RamanLab/sysbio-code-companion, or clone it with Git:
git clone https://github.com/RamanLab/sysbio-code-companion.git
cd sysbio-code-companionPython
Python 3.10 or newer is needed. From the repository folder, create a virtual environment and install the packages the examples use (NumPy, SciPy, pandas, Matplotlib, NetworkX, COBRApy and the quadratic-programming solver Clarabel), plus a tiny helper package, sbp, that the examples use to save their results and figures:
python -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activate
pip install -r requirements.txt
pip install -e .Then run any example, for instance:
python examples/constraint-based/toy_fba/toy_fba.pyMATLAB
The examples use core MATLAB (R2022a or newer; they are tested with R2024b) and, where noted at the top of a script, standard MathWorks toolboxes such as the Optimization Toolbox. Some constraint-based examples need the COBRA Toolbox; each chapter says so where this applies.
Start MATLAB in the repository folder and run, once per session:
setup_companion % adds the small +sbp helper package to the pathThen run any example, for instance run examples/constraint-based/toy_fba/toy_fba.m. If you are new to MATLAB, the book’s online Appendix E, MATLAB for systems biology, is a short introduction.
What the examples write
Each script prints its main results and writes two kinds of output next to itself, under out/matlab/ or out/python/:
- figures (
fig-*.png), which are the figures shown in this companion; results.json, with the key numbers. These files are what the automated checks compare between the two languages.
MATLAB and Python side by side
If you know one language and want to read the other, this table collects the commands the examples use most. A few differences cause most of the surprises:
- Indexing. MATLAB counts from 1 and includes both ends of a range; Python counts from 0 and excludes the end.
x(1:3)in MATLAB isx[0:3]in Python. - Arrays. MATLAB’s
*is matrix multiplication and.*is element-wise; in NumPy,*is element-wise and@is matrix multiplication. MATLAB vectors are rows or columns; NumPy has true one-dimensional arrays. - Copies. Assigning an array in MATLAB copies it; in Python,
b = amakes another name for the same array, andb = a.copy()makes a copy. - Random numbers. The two languages draw different random numbers even from the same seed, so the stochastic examples agree only on average. Where the two must make exactly the same random choices, the examples use
sbp.uniform, a small generator written out in both languages.
| Task | MATLAB | Python |
|---|---|---|
| Vector of evenly spaced points | linspace(0, 10, 101) |
np.linspace(0, 10, 101) |
| Range with a step | 0:0.5:10 |
np.arange(0, 10.5, 0.5) |
| Matrix and vector | A = [1 2; 3 4]; x = [1; 2]; |
A = np.array([[1, 2], [3, 4]]); x = np.array([1, 2]) |
| Matrix product, element-wise product | A * x, x .* x |
A @ x, x * x |
| Solve \(A\mathbf{x} = \mathbf{b}\) | A \ b |
np.linalg.solve(A, b) |
| Eigenvalues | eig(J) |
np.linalg.eigvals(J) |
| Solve ODEs | [t, y] = ode45(f, tspan, y0) (ode15s if stiff) |
solve_ivp(f, tspan, y0) (method="LSODA" or "Radau" if stiff) |
| Right-hand side of an ODE | f = @(t, y) -k * y; |
def f(t, y): return -k * y |
| Events (zero crossings) | odeset('Events', fn) |
solve_ivp(..., events=fn) |
| Root of a function in an interval | fzero(g, [a b]) |
brentq(g, a, b) |
| Nonlinear least squares | lsqnonlin(res, p0, lb, ub) |
least_squares(res, p0, bounds=(lb, ub)) |
| Linear programme | linprog(c, A, b, Aeq, beq, lb, ub) |
linprog(c, A_ub, b_ub, A_eq, b_eq, bounds) |
| Quadratic programme | quadprog(H, f, A, b, Aeq, beq, lb, ub) |
clarabel.DefaultSolver(...) (see 9 Extending constraint-based approaches) |
| Read a CSV file | readtable('data.csv') |
np.loadtxt('data.csv', delimiter=",", skiprows=1) |
| Graph and degrees | G = graph(s, t); degree(G) |
G = nx.Graph(edges); dict(G.degree()) |
| Centrality, components | centrality(G, 'closeness'), conncomp(G) |
nx.closeness_centrality(G), nx.connected_components(G) |
| Random numbers with a seed | rng(1); rand(3, 1); randi(10) |
rng = np.random.default_rng(1); rng.random(3); rng.integers(1, 11) |
| Constraint-based model | readCbModel(file), optimizeCbModel(model) |
cobra.io.read_sbml_model(file), model.optimize() |
| Plot | plot(t, y); xlabel('t') |
plt.plot(t, y); plt.xlabel("t") |
| Save a figure | exportgraphics(gcf, 'fig.png') |
plt.savefig("fig.png") |