7 Discrete dynamic models: Boolean networks
ODE models need rate laws and parameters, and for gene regulatory and signalling networks these are rarely known. Often all we know is who activates or represses whom, and whether a gene is on or off. Boolean networks work at exactly that level: each gene is either ON (1) or OFF (0), time moves in discrete steps, and each gene’s next state is a logical function of the current states of its regulators. Such models have no parameters to estimate, and yet they can reproduce the qualitative behaviour of quite complex systems.
7.1 Introduction
There is no code for this section; see the book.
7.2 Boolean networks: transfer functions
A Boolean network of \(n\) variables is defined by a transfer function for each variable, built from AND, OR and NOT:
\[ X_i(t) = F_i\big(X_1(t-1), X_2(t-1), \ldots, X_n(t-1)\big). \]
With synchronous updating, as written here, all variables are updated at once from the previous state. The network then has \(2^n\) possible states, and each state has exactly one successor. Starting anywhere, the network must eventually revisit a state, and from then on it repeats itself. The states it repeats form an attractor: a single state that maps to itself (a steady state, or point attractor), or a cycle of states. The states that lead to an attractor form its basin.
For small networks, we can simply examine every state. Our first example is the three-gene network of the book’s Fig. 7.1:
\[ g_1^* = g_2 \text{ AND NOT } g_3, \qquad g_2^* = \text{NOT } g_1, \qquad g_3^* = (\text{NOT } g_3) \text{ OR } g_2, \]
where \(g_i^*\) is the next state of \(g_i\). States are written as three digits, \(g_1 g_2 g_3\); state 011, for example, has \(g_1\) OFF and \(g_2\), \(g_3\) ON.
Core MATLAB only.
%% A three-gene Boolean network (book Fig. 7.1, Eqs. 7.3)
% g1* = g2 AND NOT g3
% g2* = NOT g1
% g3* = NOT g3 OR g2
% With synchronous updating, all genes are updated at once from the
% previous state. We list the successor of each of the 2^3 = 8 states,
% and find the attractors (the states the network ends up cycling
% through) and their basins (the states that lead to them).
% Requires: core MATLAB only.
n = 3;
update = @(x) [x(2) && ~x(3), ~x(1), ~x(3) || x(2)];
% State number <-> vector of 0/1, first gene as the leftmost digit
to_bits = @(s) bitget(s, n:-1:1);
to_number = @(x) sum(x .* 2.^(n-1:-1:0));
label = @(s) sprintf('%d', to_bits(s));
% Successor of every state (states are numbered 0 to 2^n - 1)
succ = zeros(1, 2^n);
for s = 0:2^n - 1
succ(s + 1) = to_number(double(update(to_bits(s))));
fprintf('%s => %s\n', label(s), label(succ(s + 1)));
end
% Attractors: follow each state until a state repeats; the repeating part
% is the attractor. Every state is assigned to the attractor it reaches.
attractors = {};
reaches = zeros(1, 2^n);
for s0 = 0:2^n - 1
path = [];
s = s0;
while ~ismember(s, path)
path(end + 1) = s; %#ok<AGROW>
s = succ(s + 1);
end
cycle = path(find(path == s):end);
key = min(cycle);
if ~any(cellfun(@(a) min(a) == key, attractors))
i = find(cycle == key); % start each cycle at
attractors{end + 1} = cycle([i:end, 1:i - 1]); %#ok<SAGROW>
end
reaches(s0 + 1) = key;
end
basin_sizes = cellfun(@(a) sum(reaches == min(a)), attractors);
for k = 1:numel(attractors)
a = attractors{k};
fprintf('Attractor of period %d: %s; basin of %d states\n', numel(a), ...
strjoin(arrayfun(label, a, 'UniformOutput', false), ' -> '), ...
basin_sizes(k));
end
%% Plot the state transition map. Fixed positions (the same in Python)
% keep every edge clear of the other nodes. The steady state's self-loop
% is not drawn, but labelled.
labels = arrayfun(label, 0:2^n - 1, 'UniformOutput', false);
moving = succ ~= 0:2^n - 1;
G = digraph(find(moving), succ(moving) + 1, [], labels); % nodes from 1
% Positions of states 000, 001, ..., 111
x = [2 1 2 3 0 1 0 2];
y = [0 1.5 2 0 1.5 0 0 1];
in_attractor = ismember(0:2^n - 1, [attractors{:}]);
colours = ones(2^n, 3); % white ...
colours(in_attractor, :) = repmat([0.902 0.624 0.000], ...
nnz(in_attractor), 1); % ... or orange
fig = figure('Position', [100 100 700 350]);
plot(G, 'XData', x, 'YData', y, 'NodeLabel', {}, 'Marker', 'none', ...
'EdgeColor', 'k', 'EdgeAlpha', 1, 'LineWidth', 1, ...
'ArrowSize', 12, 'ArrowPosition', 0.6);
hold on
scatter(x, y, 1100, colours, 'filled', 'MarkerEdgeColor', 'k');
text(x, y, labels, 'HorizontalAlignment', 'center', 'FontSize', 11);
text(3, -0.3, {'steady state', '(011 => 011)'}, ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', ...
'FontSize', 9);
xlim([-0.4 3.4]); ylim([-0.8 2.3]);
title('State transitions (orange: states in an attractor)');
axis off
sbp.save_figure(fig, mfilename('fullpath'), 'fig-three-genes');
results.successor = arrayfun(label, succ, 'UniformOutput', false);
results.attractors = cellfun(@(a) arrayfun(label, a, ...
'UniformOutput', false), attractors, 'UniformOutput', false);
results.basin_sizes = basin_sizes;
sbp.save_results(mfilename('fullpath'), results);
Needs NetworkX (for drawing the state transitions).
"""A three-gene Boolean network (book Fig. 7.1, Eqs. 7.3).
g1* = g2 AND NOT g3
g2* = NOT g1
g3* = NOT g3 OR g2
With synchronous updating, all genes are updated at once from the
previous state. We list the successor of each of the 2^3 = 8 states,
and find the attractors (the states the network ends up cycling
through) and their basins (the states that lead to them).
"""
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import sbp
genes = ["g1", "g2", "g3"]
n = len(genes)
def update(x):
g1, g2, g3 = x
return (g2 and not g3, not g1, (not g3) or g2)
def to_bits(s):
"""State number -> tuple of 0/1, first gene as the leftmost digit."""
return tuple((s >> (n - 1 - i)) & 1 for i in range(n))
def to_number(x):
return sum(int(b) << (n - 1 - i) for i, b in enumerate(x))
def label(s):
return "".join(map(str, to_bits(s)))
# Successor of every state
succ = [to_number(update(to_bits(s))) for s in range(2 ** n)]
for s in range(2 ** n):
print(f"{label(s)} => {label(succ[s])}")
# Attractors: follow each state until a state repeats; the repeating part
# is the attractor. Every state is assigned to the attractor it reaches.
attractor_of = {}
attractors = []
for s in range(2 ** n):
path = []
while s not in path:
path.append(s)
s = succ[s]
cycle = path[path.index(s):]
key = min(cycle)
if key not in [min(a) for a in attractors]:
i = cycle.index(key) # start each cycle at its
attractors.append(cycle[i:] + cycle[:i]) # smallest state
attractor_of[path[0]] = key
basins = {min(a): sum(1 for v in attractor_of.values() if v == min(a))
for a in attractors}
for a in attractors:
print(f"Attractor of period {len(a)}: "
f"{' -> '.join(label(s) for s in a)}; basin of {basins[min(a)]} "
f"states")
# --- Plot the state transition map. Fixed positions (the same in MATLAB)
# keep every edge clear of the other nodes. The steady state's self-loop
# is not drawn, but labelled.
G = nx.DiGraph([(label(s), label(succ[s])) for s in range(2 ** n)
if succ[s] != s])
pos = {"000": (2, 0), "001": (1, 1.5), "010": (2, 2), "011": (3, 0),
"100": (0, 1.5), "101": (1, 0), "110": (0, 0), "111": (2, 1)}
in_attractor = {label(s) for a in attractors for s in a}
sbp.style()
fig, ax = plt.subplots(figsize=(7, 3.5))
nx.draw_networkx_edges(G, pos, ax=ax, node_size=1100, arrowsize=18)
nx.draw_networkx_nodes(G, pos, ax=ax, node_size=1100, edgecolors="k",
node_color=["#E69F00" if v in in_attractor else "white"
for v in G.nodes])
nx.draw_networkx_labels(G, pos, ax=ax, font_size=11)
ax.text(3, -0.3, "steady state\n(011 => 011)", ha="center", va="top",
fontsize=9)
ax.set_xlim(-0.4, 3.4)
ax.set_ylim(-0.8, 2.3)
ax.set_title("State transitions (orange: states in an attractor)")
ax.set_axis_off()
sbp.save_figure(fig, __file__, "fig-three-genes")
sbp.save_results(__file__, {
"successor": [label(s) for s in succ],
"attractors": [[label(s) for s in a] for a in attractors],
"basin_sizes": [basins[min(a)] for a in attractors],
})
sbp.show()
The transitions are exactly those listed in the book (§7.2.1). Note that the network has two attractors, each with a basin of four states: a steady state, 011, in which \(g_1\) stays off while \(g_2\) and \(g_3\) stay on; and a cycle of period three, 001 → 010 → 111 → 001, in which the genes switch on and off in a repeating pattern. Which of the two the network ends up in depends only on where it starts, and no state can reach both.
Following each state forward until it repeats, as our code does, finds every attractor of a synchronous network. With \(n\) variables there are \(2^n\) states to examine, which is quick up to \(n \approx 20\); beyond that, attractors must be sought by sampling starting states, or by specialised algorithms.
7.2.1 Characterising Boolean network dynamics
The three-gene example in §7.2 does exactly this: it lists every state transition of the network, and then finds the attractors and their basins.
7.2.2 Synchronous vs. asynchronous updates
The second example is a model of the mammalian cell cycle by Fauré and colleagues (Fauré et al. 2006), with ten variables: the cyclins CycD, CycE, CycA and CycB, the retinoblastoma protein Rb, the transcription factor E2F, the inhibitor p27, and the regulators Cdc20, Cdh1 and UbcH10 (Ubc). Its transfer functions are the book’s Eq. 7.4. CycD stands for the growth signal: it has no regulators in the model, so it simply keeps its initial value.
With ten variables there are 1024 states, still few enough to examine every one. We do so twice: first with synchronous updating, and then with asynchronous updating, which drops the assumption that every process takes exactly one time step: at each step, any one of the variables whose transfer function calls for a change may change, in any order. A state can then have several possible successors, so the states and their possible transitions form a directed graph, the state transition graph. An attractor is now a set of states that, once entered, can never be left: a strongly connected component of that graph with no transitions leading out of it, which graph libraries find directly.
Core MATLAB only.
%% A Boolean model of the mammalian cell cycle (Fauré et al., 2006)
% Ten genes and proteins; the update rules are the book's Eq. 7.4. With
% 2^10 = 1024 possible states, we can examine every one of them:
% 1. synchronous updating, as in the previous example;
% 2. asynchronous updating, where one variable changes at a time, in
% any order. The states then form a 'state transition graph', and the
% attractors are the groups of states that, once entered, are never
% left: its terminal strongly connected components.
% Requires: core MATLAB only.
names = {'CycD', 'Rb', 'E2F', 'CycE', 'CycA', 'p27', 'Cdc20', 'Cdh1', ...
'Ubc', 'CycB'};
n = numel(names);
to_bits = @(s) double(bitget(s, n:-1:1));
to_number = @(x) sum(x .* 2.^(n-1:-1:0));
N = 2^n;
target = zeros(1, N);
for s = 0:N - 1
target(s + 1) = to_number(double(update(to_bits(s))));
end
%% 1. Synchronous updating: follow every state to its attractor
keys = []; periods = []; reaches = zeros(1, N);
for s0 = 0:N - 1
path = [];
s = s0;
while ~ismember(s, path)
path(end + 1) = s; %#ok<AGROW>
s = target(s + 1);
end
cycle = path(find(path == s):end);
if ~ismember(min(cycle), keys)
keys(end + 1) = min(cycle); %#ok<AGROW>
periods(end + 1) = numel(cycle); %#ok<AGROW>
end
reaches(s0 + 1) = min(cycle);
end
[keys, order] = sort(keys);
periods = periods(order);
fprintf('Synchronous updating:\n');
for k = 1:numel(keys)
b = to_bits(keys(k));
sync_summary(k) = struct('period', periods(k), ...
'basin', sum(reaches == keys(k)), 'CycD', b(1)); %#ok<SAGROW>
fprintf(' attractor of period %d (CycD = %d), basin of %d states\n', ...
periods(k), b(1), sync_summary(k).basin);
end
%% 2. Asynchronous updating: from each state, any one variable whose
% rule says it should change may change next
src = []; dst = [];
for s = 0:N - 1
x = to_bits(s);
fx = to_bits(target(s + 1));
for i = find(x ~= fx)
y = x;
y(i) = fx(i);
src(end + 1) = s + 1; %#ok<AGROW>
dst(end + 1) = to_number(y) + 1; %#ok<AGROW>
end
end
G = digraph(src, dst, [], N);
comp = conncomp(G, 'Type', 'strong'); % component of each state
C = condensation(G); % one node per component
terminal = find(outdegree(C) == 0)';
sizes = zeros(size(terminal)); cycd = zeros(size(terminal));
for k = 1:numel(terminal)
members = find(comp == terminal(k)) - 1; % back to states 0..N-1
sizes(k) = numel(members);
b = to_bits(min(members));
cycd(k) = b(1);
end
[~, order] = sortrows([cycd' sizes']);
fprintf('Asynchronous updating:\n');
for k = 1:numel(order)
async_summary(k) = struct('size', sizes(order(k)), ...
'CycD', cycd(order(k))); %#ok<SAGROW>
fprintf(' attractor of %d states (CycD = %d)\n', sizes(order(k)), ...
cycd(order(k)));
end
%% A synchronous trajectory: start from a G1-like state, with CycD on
x0 = double(ismember(names, {'CycD', 'Rb', 'p27', 'Cdh1'}));
traj = zeros(21, n);
traj(1, :) = x0;
for t = 2:21
traj(t, :) = double(update(traj(t - 1, :)));
end
fig = figure('Position', [100 100 800 360]);
imagesc(0:20, 1:n, traj', [0 1]);
colormap([0.97 0.98 1; 0.00 0.45 0.70]); % OFF: light, ON: dark
set(gca, 'YTick', 1:n, 'YTickLabel', names, 'XTick', 0:2:20, 'Box', 'off');
xlabel('Time step');
title('Synchronous trajectory from a G1-like state (dark: ON, light: OFF)');
sbp.save_figure(fig, mfilename('fullpath'), 'fig-cell-cycle');
results.synchronous = sync_summary;
results.asynchronous = async_summary;
results.trajectory = traj;
sbp.save_results(mfilename('fullpath'), results);
%% Local function
function y = update(x)
% Book Eqs. 7.4 (Fauré et al., 2006)
x = num2cell(logical(x));
[CycD, Rb, E2F, CycE, CycA, p27, Cdc20, Cdh1, Ubc, CycB] = x{:};
y = [CycD, ...
(~CycA && ~CycB && ~CycD && ~CycE) || (p27 && ~CycB && ~CycD), ...
(~Rb && ~CycA && ~CycB) || (p27 && ~Rb && ~CycB), ...
E2F && ~Rb, ...
(E2F && ~Rb && ~Cdc20 && ~(Cdh1 && Ubc)) || ...
(CycA && ~Rb && ~Cdc20 && ~(Cdh1 && Ubc)), ...
(~CycD && ~CycE && ~CycA && ~CycB) || ...
(p27 && ~(CycE && CycA) && ~CycB && ~CycD), ...
CycB, ...
(~CycA && ~CycB) || Cdc20 || (p27 && ~CycB), ...
~Cdh1 || (Cdh1 && Ubc && (Cdc20 || CycA || CycB)), ...
~Cdc20 && ~Cdh1];
end
Needs NetworkX.
"""A Boolean model of the mammalian cell cycle (Fauré et al., 2006).
Ten genes and proteins; the update rules are the book's Eq. 7.4. With
2^10 = 1024 possible states, we can examine every one of them:
1. synchronous updating, as in the previous example;
2. asynchronous updating, where one variable changes at a time, in
any order. The states then form a 'state transition graph', and the
attractors are the groups of states that, once entered, are never
left: its terminal strongly connected components.
"""
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import sbp
names = ["CycD", "Rb", "E2F", "CycE", "CycA", "p27", "Cdc20", "Cdh1",
"Ubc", "CycB"]
n = len(names)
def update(x):
CycD, Rb, E2F, CycE, CycA, p27, Cdc20, Cdh1, Ubc, CycB = x
return (
CycD,
(not CycA and not CycB and not CycD and not CycE)
or (p27 and not CycB and not CycD),
(not Rb and not CycA and not CycB) or (p27 and not Rb and not CycB),
E2F and not Rb,
(E2F and not Rb and not Cdc20 and not (Cdh1 and Ubc))
or (CycA and not Rb and not Cdc20 and not (Cdh1 and Ubc)),
(not CycD and not CycE and not CycA and not CycB)
or (p27 and not (CycE and CycA) and not CycB and not CycD),
CycB,
(not CycA and not CycB) or Cdc20 or (p27 and not CycB),
(not Cdh1) or (Cdh1 and Ubc and (Cdc20 or CycA or CycB)),
not Cdc20 and not Cdh1,
)
def to_bits(s):
return tuple((s >> (n - 1 - i)) & 1 for i in range(n))
def to_number(x):
return sum(int(b) << (n - 1 - i) for i, b in enumerate(x))
states = range(2 ** n)
target = [to_number(update(to_bits(s))) for s in states]
# --- 1. Synchronous updating: follow every state to its attractor
sync_attractors = {} # smallest state of the cycle -> cycle
reaches = []
for s in states:
path = []
while s not in path:
path.append(s)
s = target[s]
cycle = path[path.index(s):]
sync_attractors.setdefault(min(cycle), cycle)
reaches.append(min(cycle))
sync_summary = [{"period": len(c), "basin": reaches.count(k),
"CycD": to_bits(k)[0]}
for k, c in sorted(sync_attractors.items())]
print("Synchronous updating:")
for a in sync_summary:
print(f" attractor of period {a['period']} (CycD = {a['CycD']}), "
f"basin of {a['basin']} states")
# --- 2. Asynchronous updating: from each state, any one variable whose
# rule says it should change may change next
G = nx.DiGraph()
G.add_nodes_from(states)
for s in states:
x, fx = to_bits(s), to_bits(target[s])
for i in range(n):
if x[i] != fx[i]:
y = list(x)
y[i] = fx[i]
G.add_edge(s, to_number(y))
C = nx.condensation(G) # one node per strongly connected component
terminal = [c for c in C.nodes if C.out_degree(c) == 0]
async_summary = sorted(
({"size": len(C.nodes[c]["members"]),
"CycD": to_bits(min(C.nodes[c]["members"]))[0]} for c in terminal),
key=lambda a: (a["CycD"], a["size"]))
print("Asynchronous updating:")
for a in async_summary:
print(f" attractor of {a['size']} states (CycD = {a['CycD']})")
# --- A synchronous trajectory: start from a G1-like state, with CycD on
x0 = dict.fromkeys(names, 0)
x0.update(CycD=1, Rb=1, p27=1, Cdh1=1)
traj = [tuple(x0[g] for g in names)]
for _ in range(20):
traj.append(tuple(int(v) for v in update(traj[-1])))
traj = np.array(traj)
sbp.style()
fig, ax = plt.subplots(figsize=(8, 3.6))
ax.imshow(traj.T, aspect="auto", vmin=0, vmax=1,
cmap=ListedColormap(["#F7FAFF", "#0072B2"])) # OFF: light, ON: dark
ax.set_yticks(range(n), names)
ax.set_xticks(range(0, 21, 2))
ax.set_xlabel("Time step")
ax.set_title("Synchronous trajectory from a G1-like state "
"(dark: ON, light: OFF)")
for spine in ax.spines.values():
spine.set_visible(False)
sbp.save_figure(fig, __file__, "fig-cell-cycle")
sbp.save_results(__file__, {
"synchronous": sync_summary,
"asynchronous": async_summary,
"trajectory": traj,
})
sbp.show()
With synchronous updating, each half of the state space (CycD off, or CycD on) leads to a single attractor. Without the growth signal, the network settles into a steady state, a quiescent cell with Rb, p27 and Cdh1 active. With the growth signal, it settles into a cycle of seven states, in which E2F, then cyclin E, cyclin A and cyclin B rise and fall in turn, much as they do through the phases of a real cell cycle, as the figure shows. These are exactly the attractors reported by Fauré and colleagues.
Asynchronous updating keeps the quiescent steady state, but the seven-state cycle becomes a complex attractor of 112 states: the same sequence of events can now happen with many different relative timings, and the network wanders among all of them without ever leaving. Synchronous updating is easy to simulate and to reason about, but its rigid timing can produce cycles that depend on every process taking exactly one step, and that a real cell, with its variable timing, would not show; comparing both, as here, is a useful check on any conclusion drawn from a Boolean model (book, §7.5).
7.3 Other paradigms
7.3.1 Probabilistic Boolean networks
We turn the three-gene network of §7.2 into a probabilistic Boolean network. Gene \(g_3\) now follows its usual rule, \(g_3^* = (\text{NOT } g_3) \text{ OR } g_2\), with probability 0.8, and an alternative rule, \(g_3^* = \text{NOT } g_1\), with probability 0.2. Following Shmulevich and colleagues, we also let every gene flip at random with a small probability, \(p = 0.01\), as if by noise. A PBN is then a Markov chain on the \(2^3 = 8\) states, and for so small a network we need not simulate it at all: we can write down its transition matrix, and solve for its stationary distribution, the long-run probability of each state, exactly.
Core MATLAB only.
%% A probabilistic Boolean network (PBN), analysed exactly
% We start from the three-gene network of §7.2 (book Fig. 7.1):
% g1* = g2 AND NOT g3, g2* = NOT g1, g3* = NOT g3 OR g2,
% whose attractors are the steady state 011 and the cycle 001 -> 010 -> 111.
% In the PBN (book §7.3.1), g3 follows its rule with probability 0.8, and
% an alternative rule, g3* = NOT g1, with probability 0.2; at each step,
% one of the two is picked at random. Also, as proposed by Shmulevich and
% colleagues, each gene may be flipped at random with probability
% p = 0.01, as if by noise. The PBN is then a Markov chain on the 2^3 = 8
% states, and we can compute its transition matrix, and the long-run
% probability of each state (the stationary distribution), exactly. For
% comparison, we do the same for the original network with noise alone.
n = 3;
p_flip = 0.01;
original = @(g1, g2, g3) ~g3 || g2;
alternative = @(g1, g2, g3) ~g1;
P_noise = transition_matrix({original}, 1.0, n, p_flip);
P_pbn = transition_matrix({original, alternative}, [0.8, 0.2], n, p_flip);
pi_noise = stationary(P_noise);
pi_pbn = stationary(P_pbn);
labels = cellstr(dec2bin(0:2 ^ n - 1, n))';
cycle = find(ismember(labels, {'001', '010', '111'}));
steady = find(strcmp(labels, '011'));
fprintf('State noise only PBN\n');
for s = 1:2 ^ n
fprintf(' %s %.4f %.4f\n', labels{s}, pi_noise(s), pi_pbn(s));
end
fprintf('Noise only: steady state 011 %.3f; cycle %.3f\n', ...
pi_noise(steady), sum(pi_noise(cycle)));
fprintf('PBN: steady state 011 %.3f; cycle %.3f\n', ...
pi_pbn(steady), sum(pi_pbn(cycle)));
%% Plot
fig = figure('Position', [100 100 700 400]);
b = bar(categorical(labels, labels), [pi_noise; pi_pbn]', ...
'EdgeColor', 'none');
b(1).FaceColor = [0.6 0.6 0.6]; b(2).FaceColor = [0.000 0.447 0.698];
set(gca, 'TickDir', 'out'); % no ticks between the bars of a group
xlabel('State (g1 g2 g3)'); ylabel('Long-run probability');
legend({'noise only', 'PBN'}, 'Box', 'off'); box off
sbp.save_figure(fig, mfilename('fullpath'), 'fig-probabilistic');
results.states = labels;
results.transition = P_pbn;
results.stationary_noise = pi_noise;
results.stationary_pbn = pi_pbn;
sbp.save_results(mfilename('fullpath'), results);
%% Local functions
function P = transition_matrix(rules, probs, n, p_flip)
% Markov chain of the PBN, for the rules for g3 and their probabilities.
% States are numbered 0 to 2^n - 1, first gene as the leftmost digit;
% row/column s + 1 of P is state s.
T = zeros(2 ^ n);
for s = 0:2 ^ n - 1
g = bitget(s, n:-1:1); % [g1, g2, g3]
for r = 1:numel(rules)
nxt = [g(2) && ~g(3), ~g(1), rules{r}(g(1), g(2), g(3))];
t = nxt * 2 .^ (n - 1:-1:0)';
T(s + 1, t + 1) = T(s + 1, t + 1) + probs(r);
end
end
% With probability (1 - p)^n no gene flips and the network acts;
% otherwise, the genes that flip are flipped instead
P = (1 - p_flip) ^ n * T;
for s = 0:2 ^ n - 1
for flips = 1:2 ^ n - 1 % which genes flip (as bits)
k = sum(bitget(flips, 1:n));
t = bitxor(s, flips);
P(s + 1, t + 1) = P(s + 1, t + 1) ...
+ p_flip ^ k * (1 - p_flip) ^ (n - k);
end
end
assert(all(abs(sum(P, 2) - 1) < 1e-12));
end
function p = stationary(P)
% pi P = pi, with the probabilities summing to 1.
A = [P' - eye(size(P)); ones(1, size(P, 1))];
b = [zeros(size(P, 1), 1); 1];
p = (A \ b)';
end
Needs NumPy only.
"""A probabilistic Boolean network (PBN), analysed exactly.
We start from the three-gene network of §7.2 (book Fig. 7.1):
g1* = g2 AND NOT g3, g2* = NOT g1, g3* = NOT g3 OR g2,
whose attractors are the steady state 011 and the cycle 001 -> 010 -> 111.
In the PBN (book §7.3.1), g3 follows its rule with probability 0.8, and
an alternative rule, g3* = NOT g1, with probability 0.2; at each step,
one of the two is picked at random. Also, as proposed by Shmulevich and
colleagues, each gene may be flipped at random with probability
p = 0.01, as if by noise. The PBN is then a Markov chain on the 2^3 = 8
states, and we can compute its transition matrix, and the long-run
probability of each state (the stationary distribution), exactly. For
comparison, we do the same for the original network with noise alone.
"""
import numpy as np
import matplotlib.pyplot as plt
import sbp
n = 3
p_flip = 0.01
def bits(s):
"""State number -> (g1, g2, g3), first gene as the leftmost digit."""
return [(s >> (n - 1 - i)) & 1 for i in range(n)]
def number(x):
return sum(int(b) << (n - 1 - i) for i, b in enumerate(x))
def transition_matrix(rules_g3):
"""Markov chain of the PBN, for a list of (rule for g3, probability)."""
T = np.zeros((2 ** n, 2 ** n))
for s in range(2 ** n):
g1, g2, g3 = bits(s)
for rule, prob in rules_g3:
T[s, number([g2 and not g3, not g1, rule(g1, g2, g3)])] += prob
# With probability (1 - p)^n no gene flips and the network acts;
# otherwise, the genes that flip are flipped instead
P = (1 - p_flip) ** n * T
for s in range(2 ** n):
for flips in range(1, 2 ** n): # which genes flip (as bits)
k = bin(flips).count("1")
P[s, s ^ flips] += p_flip ** k * (1 - p_flip) ** (n - k)
return P
def stationary(P):
"""pi P = pi, with the probabilities summing to 1."""
A = np.vstack([P.T - np.eye(len(P)), np.ones(len(P))])
b = np.r_[np.zeros(len(P)), 1]
return np.linalg.lstsq(A, b, rcond=None)[0]
original = lambda g1, g2, g3: (not g3) or g2
alternative = lambda g1, g2, g3: not g1
P_noise = transition_matrix([(original, 1.0)])
P_pbn = transition_matrix([(original, 0.8), (alternative, 0.2)])
pi = {"noise": stationary(P_noise), "pbn": stationary(P_pbn)}
labels = ["".join(map(str, bits(s))) for s in range(2 ** n)]
cycle = [labels.index(x) for x in ("001", "010", "111")]
steady = labels.index("011")
print("State noise only PBN")
for s in range(2 ** n):
print(f" {labels[s]} {pi['noise'][s]:.4f} {pi['pbn'][s]:.4f}")
for key, name in (("noise", "Noise only"), ("pbn", "PBN")):
print(f"{name}: steady state 011 {pi[key][steady]:.3f}; cycle "
f"{pi[key][cycle].sum():.3f}")
# --- Plot
sbp.style()
fig, ax = plt.subplots(figsize=(7, 4))
x = np.arange(2 ** n)
ax.bar(x - 0.2, pi["noise"], 0.4, color="#999999", label="noise only")
ax.bar(x + 0.2, pi["pbn"], 0.4, color="#0072B2", label="PBN")
ax.set_xticks(x, labels)
ax.set_xlabel("State (g1 g2 g3)")
ax.set_ylabel("Long-run probability")
ax.legend(frameon=False)
sbp.save_figure(fig, __file__, "fig-probabilistic")
sbp.save_results(__file__, {"states": labels, "transition": P_pbn,
"stationary_noise": pi["noise"],
"stationary_pbn": pi["pbn"]})
sbp.show()
With noise alone, the network spends about half its time in the steady state 011, and half cycling through 001, 010 and 111: noise lets it hop between the two basins, roughly equally often in each direction. The alternative rule changes this dramatically. From state 111, \(g_3^* = \text{NOT } g_1\) switches \(g_3\) off, the network passes through 000, and lands in the steady state; nothing leads back out of 011 except noise. In the PBN, therefore, the steady state captures 83% of the long-run probability. Remember, the alternative rule is used only one time in five, and yet it reshapes the long-run behaviour of the network completely.
7.3.2 Logical interaction hypergraphs
There is no code for this section; see the book.
7.3.3 Generalised logical networks
There is no code for this section; see the book.
7.3.4 Petri nets
There is no code for this section; see the book.
7.4 Applications
There is no code for this section; see the book.
7.5 Troubleshooting
There is no code for this section; see the book.
7.6 Software tools
There is no code for this section; see the book.
Exercises
- In the three-gene network, change the rule for \(g_3\) to \(g_3^* = (\text{NOT } g_3) \text{ AND } g_2\). Find the new attractors and their basins.
- Simulate a knock-out in the cell-cycle model: fix one variable at 0 (for example, Cdh1) and find the attractors again, with both kinds of updating. Which knock-outs abolish the cycle? (This is book Exercise 7.6, done here without BooleSim or BooleanNet.)
- Start the cell-cycle model in its quiescent steady state, switch CycD on, and update synchronously. Through which states does the network pass before it joins the seven-state cycle, and how many steps does that take?
- Write a function that generates a random Boolean network (Kauffman’s “NK” network): \(n\) genes, each with \(K\) randomly chosen regulators and a random truth table. For \(n = 12\) and \(K = 1, 2, 3, 4\), find the number of attractors and their periods, averaged over many networks. How does the behaviour change with \(K\)?
- Book Exercise 7.3: write Boolean transfer functions for the repressilator (LacI represses TetR, TetR represses λcI, λcI represses LacI) and simulate them with synchronous and asynchronous updates. Compare the result with the ODE model of Chapter 12.
- Book Exercise 7.1: build the four-gene network described there, compute all its state transitions and identify the attractors.
Going further
Boolean networks lie at one end of a spectrum of models. Between them and ODE models sit multi-valued logical models and Petri nets (book, §7.3). The section on stochastic simulation at the end of Chapter 5 treats the small numbers of molecules inside a single cell as random events, another way of modelling discrete behaviour.