Spaces:
Running
Running
Eachan Johnson
commited on
Commit
·
997150d
0
Parent(s):
Initial commit
Browse files- .github/workflows/hf-sync.yml +20 -0
- .gitignore +3 -0
- README.md +180 -0
- app.py +333 -0
- requirements.txt +4 -0
- sources/growth-curve-intro.md +29 -0
- sources/growth-curve-t-independent.md +30 -0
- sources/header.md +1 -0
- sources/read-counts-expansion.md +51 -0
- sources/read-counts-intro.md +19 -0
- sources/read-counts-spike.md +30 -0
- sources/read-counts-stationary.md +65 -0
.github/workflows/hf-sync.yml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Sync to Hugging Face hub
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
branches: [main]
|
| 5 |
+
|
| 6 |
+
# to run this workflow manually from the Actions tab
|
| 7 |
+
workflow_dispatch:
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
sync-to-hub:
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
steps:
|
| 13 |
+
- uses: actions/checkout@v4
|
| 14 |
+
with:
|
| 15 |
+
fetch-depth: 0
|
| 16 |
+
lfs: true
|
| 17 |
+
- name: Push to hub
|
| 18 |
+
env:
|
| 19 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 20 |
+
run: git push https://HF_USERNAME:[email protected]/spaces/scbirlab/tutorial-seq-fitness main
|
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.venv/
|
| 3 |
+
.gradio/
|
README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
---
|
| 2 |
+
title: Tutorial - Fitness estimation from pooled growth and NGS
|
| 3 |
+
emoji: 🧮
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.22.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: How do strains grow when competing with each other? And how can we infer their fitness from next-generation sequencing data?
|
| 12 |
+
tags:
|
| 13 |
+
- biology
|
| 14 |
+
- sequencing
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Tutorial – Fitness estimation from pooled growth
|
| 18 |
+
|
| 19 |
+
[](https://huggingface.co/spaces/scbirlab/tutorial-seq-fitness)
|
| 20 |
+
|
| 21 |
+
This is the repository for the interactive tutorial, acccessed [here](https://huggingface.co/spaces/scbirlab/tutorial-seq-fitness). Non-interactive text from the tutorial is below.
|
| 22 |
+
|
| 23 |
+
## Multiplex growth curves
|
| 24 |
+
|
| 25 |
+
How do strains grow when competing with each other?
|
| 26 |
+
|
| 27 |
+
That's given by the [Lotka–Volterra competition model](https://en.wikipedia.org/wiki/Competitive_Lotka%E2%80%93Volterra_equations).
|
| 28 |
+
For two strains:
|
| 29 |
+
|
| 30 |
+
$$
|
| 31 |
+
\frac{dn_{wt}}{dt} = w_{wt} n_{wt} \left( 1 - \frac{n_{wt} + n_{1}}{K} \right)
|
| 32 |
+
$$
|
| 33 |
+
|
| 34 |
+
$$
|
| 35 |
+
\frac{dn_1}{dt} = w_{1} n_1 \left( 1 - \frac{n_{wt} + n_{1}}{K} \right)
|
| 36 |
+
$$
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
- $n_i(t)$: abundance of species (or strain) $i$ at time $t$.
|
| 40 |
+
- $w_i$: intrinsic (exponential) growth rate of species $i$.
|
| 41 |
+
- $K$: carrying capacity.
|
| 42 |
+
|
| 43 |
+
We can generalize to many strains. For each one:
|
| 44 |
+
|
| 45 |
+
$$
|
| 46 |
+
\frac{dn_i}{dt} = w_{i} n_i \left( 1 - \frac{\Sigma_j n_j}{K} \right)
|
| 47 |
+
$$
|
| 48 |
+
|
| 49 |
+
It's not possible to algebraically integrate these equations, since they are
|
| 50 |
+
circularly dependent on each other. But we can numerically integrate, to simulate
|
| 51 |
+
multiplexed growth curves.
|
| 52 |
+
|
| 53 |
+
### Removing time dependence
|
| 54 |
+
|
| 55 |
+
It can be difficult to get absolute fitness out of these curves, because
|
| 56 |
+
when the pool approaches the carrying capacity, all the strains growth rates
|
| 57 |
+
mutually affect each other.
|
| 58 |
+
|
| 59 |
+
However, if we're only interested in the relative fitness of multiplexed strains relative
|
| 60 |
+
to a reference (e.g. wild-type) strain, then we can make this simplification:
|
| 61 |
+
|
| 62 |
+
$$
|
| 63 |
+
\frac{dn_{i}}{dt} / \frac{dn_{wt}}{dt} = \frac{dn_{i}}{dn_{wt}} = \frac{w_{i} n_i}{w_{wt} n_{wt}}
|
| 64 |
+
$$
|
| 65 |
+
|
| 66 |
+
The interdependency term cancels out, and time is removed, with the reference strain's
|
| 67 |
+
growth acting as the clock. Unlike the time-dependent Lotka-Volterra equations, this
|
| 68 |
+
has a closed-form integral:
|
| 69 |
+
|
| 70 |
+
$$
|
| 71 |
+
\log n_i(t) = \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)} + \log{n_i(0)}
|
| 72 |
+
$$
|
| 73 |
+
|
| 74 |
+
So now the log of the number of cells of a mutant at any moment ($n_1(t)$) is dependent only
|
| 75 |
+
on its inoculum ($n_1(0)$), how much the reference strain has grown (i.e. fold-expansion,
|
| 76 |
+
$\frac{n_{wt}(t)}{n_{wt}(0)}$), and the ratio of fitness between the mutant and the
|
| 77 |
+
reference ($\frac{n_{wt}(t)}{n_{wt}(0)}$).
|
| 78 |
+
|
| 79 |
+
## Read counts from next-generation sequencing
|
| 80 |
+
|
| 81 |
+
But we don't actually measure the number of cells directly. Instead, we're measuring the
|
| 82 |
+
number of reads (or UMIs) which represent a random sampling of the population followed by
|
| 83 |
+
molecular biology handling and uneven sequencing per lane which decouples the relative
|
| 84 |
+
abundances for each timepoint.
|
| 85 |
+
|
| 86 |
+
Below, you can simulate read counts for technical replicates of the growth curves above.
|
| 87 |
+
The simulation:
|
| 88 |
+
|
| 89 |
+
1. Randomly samples a defined fraction of the cell population (without replacement, i.e.
|
| 90 |
+
the [Hypergeometric distribution](https://en.wikipedia.org/wiki/Hypergeometric_distribution)).
|
| 91 |
+
Smaller samples from smaller populations are noisier.
|
| 92 |
+
2. Calculates the resulting proportional representation of every strain in every sample.
|
| 93 |
+
3. Multiplies that proportion by read depth.
|
| 94 |
+
4. Randomly samples sequencing read counts resulting from variations in library construction
|
| 95 |
+
and other stochasticity, according to the
|
| 96 |
+
[Negative Binomial distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution),
|
| 97 |
+
an established noise model for sequencing counts.
|
| 98 |
+
|
| 99 |
+
### Accounting for sequencing subsampling per sample
|
| 100 |
+
|
| 101 |
+
Each sequencing sample $s$ could be over- or under-sampling the population relative to the first
|
| 102 |
+
timepoint by some factor $\phi_s$.
|
| 103 |
+
|
| 104 |
+
$$\log \frac{c_i(t)}{c_i(0)} = \log \phi_s\frac{n_i(t)}{n_i(0)} = \log \phi_s + \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)}$$
|
| 105 |
+
|
| 106 |
+
Variables:
|
| 107 |
+
- $c_i(t)$: Read (or UMI) count of strain $i$ at time $t$
|
| 108 |
+
- $\phi_s$: The ratio of sampling depth at time $t$ to that at time $0$ for sample $s$
|
| 109 |
+
|
| 110 |
+
The factor $\phi_s$ is the ratio of _the ratio of read counts between samples_
|
| 111 |
+
and _the ratio of cell counts between samples_ for any strain (assuming each strain
|
| 112 |
+
is sampled without bias):
|
| 113 |
+
|
| 114 |
+
$$\log \phi_s = \log \frac{c_i(t)}{c_i(0)} - \log \frac{n_i(0)}{n_i(0)}$$
|
| 115 |
+
|
| 116 |
+
We can get rid of the nuisance parameter $\phi_s$ (which is difficult to measure becuase
|
| 117 |
+
we don't know the true number of cells for each strain and sample) using the following trick.
|
| 118 |
+
|
| 119 |
+
We have the equation for read counts for mutant $i$ (same as above):
|
| 120 |
+
|
| 121 |
+
$$
|
| 122 |
+
\log \frac{c_i(t)}{c_i(0)} = \log \phi_s + \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 123 |
+
$$
|
| 124 |
+
|
| 125 |
+
And for the reference strain (relative fitness is 1):
|
| 126 |
+
|
| 127 |
+
$$
|
| 128 |
+
\log \frac{c_{wt}(t)}{c_{wt}(0)} = \log \phi_s + \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 129 |
+
$$
|
| 130 |
+
|
| 131 |
+
We can make $\phi_s$ disappear by taking the difference:
|
| 132 |
+
|
| 133 |
+
$$
|
| 134 |
+
\log \frac{c_i(t)}{c_i(0)} - \log \frac{c_{wt}(t)}{c_{wt}(0)} = \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)} - \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 135 |
+
$$
|
| 136 |
+
|
| 137 |
+
This is equivalent to:
|
| 138 |
+
|
| 139 |
+
$$
|
| 140 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 141 |
+
$$
|
| 142 |
+
|
| 143 |
+
So the ratio of _the count ratio of a strain to the reference strain at time t_ to
|
| 144 |
+
_the count ratio of a strain to the reference strain at time 0_ is
|
| 145 |
+
dependent only on the relative fitness and the true fold-expansion of the reference strain.
|
| 146 |
+
|
| 147 |
+
Plotting the ratio of _the count ratio of a strain to the reference strain at time t_ to
|
| 148 |
+
_the count ratio of a strain to the reference strain at time 0_
|
| 149 |
+
should give a straight line (on a log-log) plot, with intercept 0 and gradient equal to the relative fitness minus 1.
|
| 150 |
+
|
| 151 |
+
### Using spike-in counts
|
| 152 |
+
|
| 153 |
+
But we don't actually know the true fold-expansion of the reference strain, since
|
| 154 |
+
it's not directly observed. However, a non-growing fitness-zero control can help,
|
| 155 |
+
such as a heat-killed strain or a spike-in plasmid.
|
| 156 |
+
|
| 157 |
+
We start with the equation before,
|
| 158 |
+
|
| 159 |
+
$$
|
| 160 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 161 |
+
$$
|
| 162 |
+
|
| 163 |
+
But for the fitness-zero control, $w_{spike} = 0$, so:
|
| 164 |
+
|
| 165 |
+
$$
|
| 166 |
+
\log \left( \frac{c_{spike}(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_{spike}(0)} \right) = -\log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 167 |
+
$$
|
| 168 |
+
|
| 169 |
+
This means that, although we don't know how the reference strain grows directly, its
|
| 170 |
+
growth is given from the ratio of the spike counts to the reference counts, normalized
|
| 171 |
+
to the same ratio at time 0.
|
| 172 |
+
|
| 173 |
+
This leaves us with the overall equation:
|
| 174 |
+
|
| 175 |
+
$$
|
| 176 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(1 - \frac{w_i}{w_{wt}} \right) \log \left( \frac{c_{spike}(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_{spike}(0)} \right)
|
| 177 |
+
$$
|
| 178 |
+
|
| 179 |
+
If we plot the left hand side against the right, we should get a straight line for
|
| 180 |
+
each strain with intercept zero and gradient $1 - \frac{w_i}{w_{wt}}$.
|
app.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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8 |
+
import matplotlib as mpl
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from scipy.integrate import solve_ivp
|
| 11 |
+
from scipy.stats import multivariate_hypergeom, nbinom
|
| 12 |
+
|
| 13 |
+
# Set the default color cycle
|
| 14 |
+
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(
|
| 15 |
+
color=("lightgrey", "dimgrey") + colorblind_palette()[1:],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
SEED: int = 42
|
| 19 |
+
MAX_TIME: float = 5.
|
| 20 |
+
SOURCES_DIR: str = "sources"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def inject_markdown(filename):
|
| 24 |
+
with open(os.path.join(SOURCES_DIR, filename), 'r') as f:
|
| 25 |
+
md = f.read()
|
| 26 |
+
return gr.Markdown(
|
| 27 |
+
md,
|
| 28 |
+
latex_delimiters=[
|
| 29 |
+
{"left": "$$", "right": "$$", "display": True},
|
| 30 |
+
{"left": "$", "right": "$", "display": False},
|
| 31 |
+
],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def lotka_volterra(t, y, w, K):
|
| 36 |
+
remaining_capacity = np.sum(y) / K
|
| 37 |
+
dy = w * y.flatten() * (1. - remaining_capacity)
|
| 38 |
+
return dy
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def grow(t, n0, w, K=None):
|
| 42 |
+
"""Deterministic population size at time t.
|
| 43 |
+
|
| 44 |
+
n0 : initial cells
|
| 45 |
+
w : growth rate
|
| 46 |
+
t : time (arbitrary units)
|
| 47 |
+
K : carrying capacity (None → pure exponential)
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
if K is None:
|
| 51 |
+
return n0 * np.exp(w[None] * t[:,None])
|
| 52 |
+
# logistic with shared K
|
| 53 |
+
else:
|
| 54 |
+
ode_solution = solve_ivp(
|
| 55 |
+
lotka_volterra,
|
| 56 |
+
t_span=sorted(set([0, max(t)])),
|
| 57 |
+
t_eval=sorted(t),
|
| 58 |
+
y0=n0,
|
| 59 |
+
vectorized=True,
|
| 60 |
+
args=(w, K),
|
| 61 |
+
)
|
| 62 |
+
# print(ode_solution)
|
| 63 |
+
return ode_solution.y
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def plotter_t(x, growths, scatter=False, **kwargs):
|
| 67 |
+
fig, axes = grid(aspect_ratio=1.5)
|
| 68 |
+
plotter_f = partial(axes.scatter, s=5.) if scatter else axes.plot
|
| 69 |
+
for i, y in enumerate(growths):
|
| 70 |
+
plotter_f(
|
| 71 |
+
x.flatten(), y.flatten(),
|
| 72 |
+
label=f"Mutant {i-1}" if i > 1 else "_none",
|
| 73 |
+
)
|
| 74 |
+
axes.set(
|
| 75 |
+
xlabel="Time",
|
| 76 |
+
yscale="log",
|
| 77 |
+
**kwargs,
|
| 78 |
+
)
|
| 79 |
+
add_legend(axes)
|
| 80 |
+
return fig
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def plotter_ref(x, growths, scatter=False, fitlines=None, text=None, **kwargs):
|
| 84 |
+
fig, axes = grid(aspect_ratio=1.5 if text is None else 1.7)
|
| 85 |
+
plotter_f = partial(axes.scatter, s=5.) if scatter else axes.plot
|
| 86 |
+
for i, y in enumerate(growths):
|
| 87 |
+
plotter_f(
|
| 88 |
+
x.flatten(), y.flatten(),
|
| 89 |
+
label=f"Mutant {i-1}" if i > 1 else "_none",
|
| 90 |
+
)
|
| 91 |
+
if fitlines is not None:
|
| 92 |
+
fit_x, fit_y = fitlines
|
| 93 |
+
for i, b in enumerate(fit_y.flatten()):
|
| 94 |
+
y = np.exp(np.log(fit_x) @ b[None])
|
| 95 |
+
print(fit_x.shape, y.shape, b)
|
| 96 |
+
axes.plot(
|
| 97 |
+
fit_x.flatten(), y.flatten(),
|
| 98 |
+
label="_none",
|
| 99 |
+
)
|
| 100 |
+
if text is not None:
|
| 101 |
+
axes.text(
|
| 102 |
+
1.05, .1,
|
| 103 |
+
text,
|
| 104 |
+
fontsize=10,
|
| 105 |
+
transform=axes.transAxes,
|
| 106 |
+
)
|
| 107 |
+
axes.set(
|
| 108 |
+
xscale="log",
|
| 109 |
+
yscale="log",
|
| 110 |
+
**kwargs,
|
| 111 |
+
)
|
| 112 |
+
add_legend(axes)
|
| 113 |
+
return fig
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def calculate_growth_curves(inoculum, inoculum_var, carrying_capacity, fitness, n_timepoints=100):
|
| 117 |
+
inoculum_var = inoculum + inoculum_var * np.square(inoculum)
|
| 118 |
+
p = inoculum / inoculum_var
|
| 119 |
+
n = (inoculum ** 2.) / (inoculum_var - inoculum)
|
| 120 |
+
w = [1., 0.] + list(fitness)
|
| 121 |
+
n0 = nbinom.rvs(n, p, size=len(w), random_state=SEED)
|
| 122 |
+
t = np.linspace(0., MAX_TIME, num=int(n_timepoints))
|
| 123 |
+
growths = grow(t, n0, w, inoculum * carrying_capacity)
|
| 124 |
+
ref_expansion = growths[0] / n0[0]
|
| 125 |
+
return t, ref_expansion, growths
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def growth_plotter(inoculum, inoculum_var, carrying_capacity, *fitness):
|
| 129 |
+
t, ref_expansion, growths = calculate_growth_curves(inoculum, inoculum_var, carrying_capacity, fitness, n_timepoints=100)
|
| 130 |
+
return [
|
| 131 |
+
plotter_t(
|
| 132 |
+
t,
|
| 133 |
+
growths,
|
| 134 |
+
ylabel="Number of cells per strain",
|
| 135 |
+
),
|
| 136 |
+
plotter_ref(
|
| 137 |
+
ref_expansion,
|
| 138 |
+
growths,
|
| 139 |
+
xlabel="Fold-expansion of wild-type",
|
| 140 |
+
ylabel="Number of cells per strain",
|
| 141 |
+
),
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def reads_sampler(population, sample_frac, seq_depth, reps, variance):
|
| 146 |
+
samples = []
|
| 147 |
+
for i, timepoint_pop in enumerate(np.split(population.astype(int), population.shape[-1], axis=-1)):
|
| 148 |
+
sample_size = np.floor(timepoint_pop.sum() * sample_frac).astype(int)
|
| 149 |
+
samples.append(
|
| 150 |
+
multivariate_hypergeom.rvs(
|
| 151 |
+
m=timepoint_pop.flatten(),
|
| 152 |
+
n=sample_size,
|
| 153 |
+
size=reps,
|
| 154 |
+
random_state=SEED + i,
|
| 155 |
+
).T
|
| 156 |
+
)
|
| 157 |
+
samples = np.stack(samples, axis=-2)
|
| 158 |
+
read_means = np.floor(seq_depth * samples.shape[0] * samples / samples.sum(axis=0, keepdims=True))
|
| 159 |
+
variance = read_means + variance * np.square(read_means)
|
| 160 |
+
p = read_means / variance
|
| 161 |
+
n = (read_means ** 2.) / (variance - read_means)
|
| 162 |
+
return np.stack([
|
| 163 |
+
nbinom.rvs(n[...,i], p[...,i], random_state=SEED + i)
|
| 164 |
+
for i in range(reps)
|
| 165 |
+
], axis=-1)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def fitness_fitter(read_counts, ref_expansion):
|
| 169 |
+
|
| 170 |
+
read_count_expansion = read_counts / np.mean(read_counts[:,:1], axis=-1, keepdims=True)
|
| 171 |
+
read_count_expansion_ref = read_count_expansion[:1]
|
| 172 |
+
log_read_count_correction = np.log(read_count_expansion) - np.log(read_count_expansion_ref)
|
| 173 |
+
|
| 174 |
+
ref_expansion = np.tile(
|
| 175 |
+
np.log(ref_expansion)[:,None],
|
| 176 |
+
(1, log_read_count_correction.shape[-1]),
|
| 177 |
+
).reshape((-1, 1))
|
| 178 |
+
betas = []
|
| 179 |
+
for i, log_strain_counts_corrected in enumerate(log_read_count_correction):
|
| 180 |
+
ols_fit = np.linalg.lstsq(a=ref_expansion, b=log_strain_counts_corrected.flatten())
|
| 181 |
+
betas.append(ols_fit[0])
|
| 182 |
+
|
| 183 |
+
return log_read_count_correction, np.asarray(betas)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def fitness_fitter_spike(log_read_count_corrected):
|
| 187 |
+
log_spike_count_corrected = log_read_count_corrected[1,...].flatten()[...,None]
|
| 188 |
+
betas = []
|
| 189 |
+
for i, log_strain_counts_corrected in enumerate(log_read_count_corrected):
|
| 190 |
+
ols_fit = np.linalg.lstsq(
|
| 191 |
+
a=log_spike_count_corrected,
|
| 192 |
+
b=log_strain_counts_corrected.flatten(),
|
| 193 |
+
)
|
| 194 |
+
betas.append(ols_fit[0])
|
| 195 |
+
|
| 196 |
+
return log_spike_count_corrected, np.asarray(betas)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def reads_plotter(
|
| 200 |
+
sample_frac, seq_reps, seq_depth, read_var,
|
| 201 |
+
inoculum, inoculum_var, carrying_capacity, *fitness
|
| 202 |
+
):
|
| 203 |
+
t, ref_expansion, growths = calculate_growth_curves(inoculum, inoculum_var, carrying_capacity, fitness, n_timepoints=10)
|
| 204 |
+
read_counts = reads_sampler(growths, sample_frac, seq_depth, seq_reps, read_var)
|
| 205 |
+
log_read_count_correction, betas = fitness_fitter(read_counts, ref_expansion)
|
| 206 |
+
plot_text = "\n".join(
|
| 207 |
+
f"Mutant {i-1}: $w_{i-1}/w_{'{wt}'}={1. + b:.2f}$"
|
| 208 |
+
for i, b in enumerate(betas.flatten()) if i > 1
|
| 209 |
+
)
|
| 210 |
+
log_spike_count_corrected, spike_betas = fitness_fitter_spike(log_read_count_correction)
|
| 211 |
+
plot_text_spike = "\n".join(
|
| 212 |
+
f"Mutant {i-1}: $w_{i-1}/w_{'{wt}'}={1. - b:.2f}$"
|
| 213 |
+
for i, b in enumerate(spike_betas.flatten()) if i > 1
|
| 214 |
+
)
|
| 215 |
+
read_count_correction = np.exp(log_read_count_correction)
|
| 216 |
+
return growth_plotter(inoculum, inoculum_var, carrying_capacity, *fitness) + [
|
| 217 |
+
plotter_t(
|
| 218 |
+
np.tile(t[:,None], (1, seq_reps)),
|
| 219 |
+
read_counts,
|
| 220 |
+
scatter=True,
|
| 221 |
+
ylabel="Read counts per strain",
|
| 222 |
+
),
|
| 223 |
+
plotter_ref(
|
| 224 |
+
np.tile(ref_expansion[:,None], (1, seq_reps)),
|
| 225 |
+
read_count_correction,
|
| 226 |
+
scatter=True,
|
| 227 |
+
fitlines=(ref_expansion[:,None], betas),
|
| 228 |
+
text=plot_text,
|
| 229 |
+
xlabel="Fold-expansion of wild-type",
|
| 230 |
+
ylabel="$\\frac{c_1(t)}{c_{wt}(t)} / \\frac{c_1(0)}{c_{wt}(0)}$",
|
| 231 |
+
),
|
| 232 |
+
plotter_ref(
|
| 233 |
+
read_count_correction[1:2,...],
|
| 234 |
+
read_count_correction,
|
| 235 |
+
scatter=True,
|
| 236 |
+
fitlines=(read_count_correction[1:2,...].flatten()[...,None], spike_betas),
|
| 237 |
+
text=plot_text_spike,
|
| 238 |
+
xlabel="$\\frac{c_{spike}(t)}{c_{wt}(t)} / \\frac{c_{spike}(0)}{c_{wt}(0)}$",
|
| 239 |
+
ylabel="$\\frac{c_1(t)}{c_{wt}(t)} / \\frac{c_1(0)}{c_{wt}(0)}$",
|
| 240 |
+
),
|
| 241 |
+
]
|
| 242 |
+
|
| 243 |
+
with gr.Blocks() as demo:
|
| 244 |
+
inject_markdown("header.md")
|
| 245 |
+
# Growth curves
|
| 246 |
+
inject_markdown("growth-curve-intro.md")
|
| 247 |
+
mut_fitness_defaults = [.5, 2., .2]
|
| 248 |
+
with gr.Row():
|
| 249 |
+
relative_fitness = [
|
| 250 |
+
gr.Slider(0., 3., step=.1, value=w, label=f"Relative fitness, mutant {i + 1}")
|
| 251 |
+
for i, w in enumerate(mut_fitness_defaults)
|
| 252 |
+
]
|
| 253 |
+
with gr.Row():
|
| 254 |
+
n_mutants = len(mut_fitness_defaults)
|
| 255 |
+
inoculum = gr.Slider(
|
| 256 |
+
10, 1_000_000,
|
| 257 |
+
step=10,
|
| 258 |
+
value=1000,
|
| 259 |
+
label="Average inoculum per strain",
|
| 260 |
+
)
|
| 261 |
+
inoculum_var = gr.Slider(
|
| 262 |
+
.001, 1.,
|
| 263 |
+
step=.001,
|
| 264 |
+
value=.001,
|
| 265 |
+
label="Inoculum variance between strains",
|
| 266 |
+
)
|
| 267 |
+
carrying_capacity = gr.Slider(
|
| 268 |
+
len(mut_fitness_defaults) + 1, 10_000,
|
| 269 |
+
step=1, value=10,
|
| 270 |
+
label="Total carrying capacity ($\times$ inoculum)",
|
| 271 |
+
)
|
| 272 |
+
plot_growth = gr.Button("Plot growth curves")
|
| 273 |
+
growth_curves_t = gr.Plot(label="Growth vs time", format="png")
|
| 274 |
+
|
| 275 |
+
inject_markdown("growth-curve-t-independent.md")
|
| 276 |
+
growth_curves_ref = gr.Plot(label="Growth vs WT expansion", format="png")
|
| 277 |
+
growth_curves = [growth_curves_t, growth_curves_ref]
|
| 278 |
+
|
| 279 |
+
# Read counts
|
| 280 |
+
inject_markdown("read-counts-intro.md")
|
| 281 |
+
with gr.Row():
|
| 282 |
+
sample_frac = gr.Slider(
|
| 283 |
+
.001, 1., step=.001,
|
| 284 |
+
value=.1,
|
| 285 |
+
label="Fraction of population per sample",
|
| 286 |
+
)
|
| 287 |
+
seq_reps = gr.Slider(
|
| 288 |
+
1, 10,
|
| 289 |
+
step=1,
|
| 290 |
+
value=3,
|
| 291 |
+
label="Technical replicates",
|
| 292 |
+
)
|
| 293 |
+
with gr.Row():
|
| 294 |
+
seq_depth = gr.Slider(
|
| 295 |
+
10, 10_000,
|
| 296 |
+
step=10,
|
| 297 |
+
value=10_000,
|
| 298 |
+
label="Average reads per strain per sample",
|
| 299 |
+
)
|
| 300 |
+
read_var = gr.Slider(
|
| 301 |
+
.001, 1.,
|
| 302 |
+
step=.001,
|
| 303 |
+
value=.001,
|
| 304 |
+
label="Sequencing variance",
|
| 305 |
+
)
|
| 306 |
+
plot_reads = gr.Button("Plot read counts")
|
| 307 |
+
read_curves_t = gr.Plot(label="Read counts vs time", format="png")
|
| 308 |
+
|
| 309 |
+
inject_markdown("read-counts-expansion.md")
|
| 310 |
+
read_curves_ref = gr.Plot(label="Read count diff vs WT expansion", format="png")
|
| 311 |
+
|
| 312 |
+
inject_markdown("read-counts-spike.md")
|
| 313 |
+
read_curves_t2 = gr.Plot(label="Read count diff vs spike count diff", format="png")
|
| 314 |
+
|
| 315 |
+
read_curves = [
|
| 316 |
+
read_curves_t,
|
| 317 |
+
read_curves_ref,
|
| 318 |
+
read_curves_t2,
|
| 319 |
+
]
|
| 320 |
+
|
| 321 |
+
# Events
|
| 322 |
+
plot_growth.click(
|
| 323 |
+
fn=growth_plotter,
|
| 324 |
+
inputs=[inoculum, inoculum_var, carrying_capacity, *relative_fitness],
|
| 325 |
+
outputs=growth_curves,
|
| 326 |
+
)
|
| 327 |
+
plot_reads.click(
|
| 328 |
+
fn=reads_plotter,
|
| 329 |
+
inputs=[sample_frac, seq_reps, seq_depth, read_var] + [inoculum, inoculum_var, carrying_capacity, *relative_fitness],
|
| 330 |
+
outputs=growth_curves + read_curves,
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
carabiner-tools[pd,mpl]>=0.0.4
|
| 2 |
+
numpy<2
|
| 3 |
+
gradio==5.23.3
|
| 4 |
+
scipy
|
sources/growth-curve-intro.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Multiplex growth curves
|
| 2 |
+
|
| 3 |
+
How do strains grow when competing with each other?
|
| 4 |
+
|
| 5 |
+
That's given by the [Lotka–Volterra competition model](https://en.wikipedia.org/wiki/Competitive_Lotka%E2%80%93Volterra_equations).
|
| 6 |
+
For two strains:
|
| 7 |
+
|
| 8 |
+
$$
|
| 9 |
+
\frac{dn_{wt}}{dt} = w_{wt} n_{wt} \left( 1 - \frac{n_{wt} + n_{1}}{K} \right)
|
| 10 |
+
$$
|
| 11 |
+
|
| 12 |
+
$$
|
| 13 |
+
\frac{dn_1}{dt} = w_{1} n_1 \left( 1 - \frac{n_{wt} + n_{1}}{K} \right)
|
| 14 |
+
$$
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
- $n_i(t)$: abundance of species (or strain) $i$ at time $t$.
|
| 18 |
+
- $w_i$: intrinsic (exponential) growth rate of species $i$.
|
| 19 |
+
- $K$: carrying capacity.
|
| 20 |
+
|
| 21 |
+
We can generalize to many strains. For each one:
|
| 22 |
+
|
| 23 |
+
$$
|
| 24 |
+
\frac{dn_i}{dt} = w_{i} n_i \left( 1 - \frac{\Sigma_j n_j}{K} \right)
|
| 25 |
+
$$
|
| 26 |
+
|
| 27 |
+
It's not possible to algebraically integrate these equations, since they are
|
| 28 |
+
circularly dependent on each other. But we can numerically integrate, to simulate
|
| 29 |
+
multiplexed growth curves.
|
sources/growth-curve-t-independent.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Removing time dependence
|
| 2 |
+
|
| 3 |
+
It can be difficult to get absolute fitness out of these curves, because
|
| 4 |
+
when the pool approaches the carrying capacity, all the strains growth rates
|
| 5 |
+
mutually affect each other.
|
| 6 |
+
|
| 7 |
+
However, if we're only interested in the relative fitness of multiplexed strains relative
|
| 8 |
+
to a reference (e.g. wild-type) strain, then we can make this simplification:
|
| 9 |
+
|
| 10 |
+
$$
|
| 11 |
+
\frac{dn_{i}}{dt} / \frac{dn_{wt}}{dt} = \frac{dn_{i}}{dn_{wt}} = \frac{w_{i} n_i}{w_{wt} n_{wt}}
|
| 12 |
+
$$
|
| 13 |
+
|
| 14 |
+
The interdependency term cancels out, and time is removed, with the reference strain's
|
| 15 |
+
growth acting as the clock. Unlike the time-dependent Lotka-Volterra equations, this
|
| 16 |
+
has a closed-form integral:
|
| 17 |
+
|
| 18 |
+
$$
|
| 19 |
+
\log n_i(t) = \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)} + \log{n_i(0)}
|
| 20 |
+
$$
|
| 21 |
+
|
| 22 |
+
So now the log of the number of cells of a mutant at any moment ($n_1(t)$) is dependent only
|
| 23 |
+
on its inoculum ($n_1(0)$), how much the reference strain has grown (i.e. fold-expansion,
|
| 24 |
+
$\frac{n_{wt}(t)}{n_{wt}(0)}$), and the ratio of fitness between the mutant and the
|
| 25 |
+
reference ($\frac{n_{wt}(t)}{n_{wt}(0)}$).
|
| 26 |
+
|
| 27 |
+
Plotting the number of cells of a mutant against the reference fold-expansion gives
|
| 28 |
+
a straight line (on log scales), with intercept being the mutant inoculum and gradient
|
| 29 |
+
being its relative fitness with respect to the reference strain's fitness.
|
| 30 |
+
|
sources/header.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Fitness estimation from pooled growth
|
sources/read-counts-expansion.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Accounting for sequencing subsampling per sample
|
| 2 |
+
|
| 3 |
+
Each sequencing sample $s$ could be over- or under-sampling the population relative to the first
|
| 4 |
+
timepoint by some factor $\phi_s$.
|
| 5 |
+
|
| 6 |
+
$$\log \frac{c_i(t)}{c_i(0)} = \log \phi_s\frac{n_i(t)}{n_i(0)} = \log \phi_s + \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)}$$
|
| 7 |
+
|
| 8 |
+
Variables:
|
| 9 |
+
- $c_i(t)$: Read (or UMI) count of strain $i$ at time $t$
|
| 10 |
+
- $\phi_s$: The ratio of sampling depth at time $t$ to that at time $0$ for sample $s$
|
| 11 |
+
|
| 12 |
+
The factor $\phi_s$ is the ratio of _the ratio of read counts between samples_
|
| 13 |
+
and _the ratio of cell counts between samples_ for any strain (assuming each strain
|
| 14 |
+
is sampled without bias):
|
| 15 |
+
|
| 16 |
+
$$\log \phi_s = \log \frac{c_i(t)}{c_i(0)} - \log \frac{n_i(0)}{n_i(0)}$$
|
| 17 |
+
|
| 18 |
+
We can get rid of the nuisance parameter $\phi_s$ (which is difficult to measure becuase
|
| 19 |
+
we don't know the true number of cells for each strain and sample) using the following trick.
|
| 20 |
+
|
| 21 |
+
We have the equation for read counts for mutant $i$ (same as above):
|
| 22 |
+
|
| 23 |
+
$$
|
| 24 |
+
\log \frac{c_i(t)}{c_i(0)} = \log \phi_s + \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 25 |
+
$$
|
| 26 |
+
|
| 27 |
+
And for the reference strain (relative fitness is 1):
|
| 28 |
+
|
| 29 |
+
$$
|
| 30 |
+
\log \frac{c_{wt}(t)}{c_{wt}(0)} = \log \phi_s + \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 31 |
+
$$
|
| 32 |
+
|
| 33 |
+
We can make $\phi_s$ disappear by taking the difference:
|
| 34 |
+
|
| 35 |
+
$$
|
| 36 |
+
\log \frac{c_i(t)}{c_i(0)} - \log \frac{c_{wt}(t)}{c_{wt}(0)} = \frac{w_i}{w_{wt}} \log \frac{n_{wt}(t)}{n_{wt}(0)} - \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 37 |
+
$$
|
| 38 |
+
|
| 39 |
+
This is equivalent to:
|
| 40 |
+
|
| 41 |
+
$$
|
| 42 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 43 |
+
$$
|
| 44 |
+
|
| 45 |
+
So the ratio of _the count ratio of a strain to the reference strain at time t_ to
|
| 46 |
+
_the count ratio of a strain to the reference strain at time 0_ is
|
| 47 |
+
dependent only on the relative fitness and the true fold-expansion of the reference strain.
|
| 48 |
+
|
| 49 |
+
Plotting the ratio of _the count ratio of a strain to the reference strain at time t_ to
|
| 50 |
+
_the count ratio of a strain to the reference strain at time 0_
|
| 51 |
+
should give a straight line (on a log-log) plot, with intercept 0 and gradient equal to the relative fitness minus 1.
|
sources/read-counts-intro.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Read counts from next-generation sequencing
|
| 2 |
+
|
| 3 |
+
But we don't actually measure the number of cells directly. Instead, we're measuring the
|
| 4 |
+
number of reads (or UMIs) which represent a random sampling of the population followed by
|
| 5 |
+
molecular biology handling and uneven sequencing per lane which decouples the relative
|
| 6 |
+
abundances for each timepoint.
|
| 7 |
+
|
| 8 |
+
Below, you can simulate read counts for technical replicates of the growth curves above.
|
| 9 |
+
The simulation:
|
| 10 |
+
|
| 11 |
+
1. Randomly samples a defined fraction of the cell population (without replacement, i.e.
|
| 12 |
+
the [Hypergeometric distribution](https://en.wikipedia.org/wiki/Hypergeometric_distribution)).
|
| 13 |
+
Smaller samples from smaller populations are noisier.
|
| 14 |
+
2. Calculates the resulting proportional representation of every strain in every sample.
|
| 15 |
+
3. Multiplies that proportion by read depth.
|
| 16 |
+
4. Randomly samples sequencing read counts resulting from variations in library construction
|
| 17 |
+
and other stochasticity, according to the
|
| 18 |
+
[Negative Binomial distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution),
|
| 19 |
+
an established noise model for sequencing counts.
|
sources/read-counts-spike.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Using spike-in counts
|
| 2 |
+
|
| 3 |
+
But we don't actually know the true fold-expansion of the reference strain, since
|
| 4 |
+
it's not directly observed. However, a non-growing fitness-zero control can help,
|
| 5 |
+
such as a heat-killed strain or a spike-in plasmid.
|
| 6 |
+
|
| 7 |
+
We start with the equation before,
|
| 8 |
+
|
| 9 |
+
$$
|
| 10 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 11 |
+
$$
|
| 12 |
+
|
| 13 |
+
But for the fitness-zero control, $w_{spike} = 0$, so:
|
| 14 |
+
|
| 15 |
+
$$
|
| 16 |
+
\log \left( \frac{c_{spike}(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_{spike}(0)} \right) = -\log \frac{n_{wt}(t)}{n_{wt}(0)}
|
| 17 |
+
$$
|
| 18 |
+
|
| 19 |
+
This means that, although we don't know how the reference strain grows directly, its
|
| 20 |
+
growth is given from the ratio of the spike counts to the reference counts, normalized
|
| 21 |
+
to the same ratio at time 0.
|
| 22 |
+
|
| 23 |
+
This leaves us with the overall equation:
|
| 24 |
+
|
| 25 |
+
$$
|
| 26 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(1 - \frac{w_i}{w_{wt}} \right) \log \left( \frac{c_{spike}(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_{spike}(0)} \right)
|
| 27 |
+
$$
|
| 28 |
+
|
| 29 |
+
If we plot the left hand side against the right, we should get a straight line for
|
| 30 |
+
each strain with intercept zero and gradient $1 - \frac{w_i}{w_{wt}}$.
|
sources/read-counts-stationary.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
But we don't actually know the true fold-expansion of the reference strain, since
|
| 2 |
+
it's not directly observed.
|
| 3 |
+
|
| 4 |
+
But we _do_ know some things about it. For example, when the pool is far from carrying
|
| 5 |
+
capacity, the fold-expansion will be close to exponential:
|
| 6 |
+
|
| 7 |
+
$$
|
| 8 |
+
\log \frac{n_{wt}(t)}{n_{wt}(0)} = w_{wt} t
|
| 9 |
+
$$
|
| 10 |
+
|
| 11 |
+
And at the carrying capacity, the fold-expansion stops changing with time, arrested
|
| 12 |
+
at the carrying capacity minus the other cells in the pool:
|
| 13 |
+
|
| 14 |
+
$$
|
| 15 |
+
\log \frac{n_{wt}(t)}{n_{wt}(0)} = \log \frac{K - \Sigma_j n_j}{n_{wt}(0)}
|
| 16 |
+
$$
|
| 17 |
+
|
| 18 |
+
So at very early timepoints, long before carrying capacity is reached,
|
| 19 |
+
|
| 20 |
+
$$
|
| 21 |
+
\log \left( \frac{c_i(t)}{c_{wt}(t)}\frac{c_{wt}(0)}{c_i(0)} \right) = \left(\frac{w_i}{w_{wt}} - 1 \right) w_{wt} t
|
| 22 |
+
$$
|
| 23 |
+
|
| 24 |
+
or equivalently,
|
| 25 |
+
|
| 26 |
+
$$
|
| 27 |
+
\log \frac{c_i(t)}{c_{wt}(t)} = \log \frac{c_i(0)}{c_{wt}(0)} + (w_i - w_{wt}) t
|
| 28 |
+
$$
|
| 29 |
+
|
| 30 |
+
So at early timepoints, the log-ratio of a strain's counts to reference counts increases linearly
|
| 31 |
+
over time with the fitness difference between the strain and the reference. The fitness difference
|
| 32 |
+
is useful, but the ratio would be better. (Alternatively, we could use a known value of the reference
|
| 33 |
+
fitness measured separately).
|
| 34 |
+
|
| 35 |
+
And after carrying capacity is reached,
|
| 36 |
+
|
| 37 |
+
$$
|
| 38 |
+
\log \frac{c_i(t)}{c_{wt}(t)} = \log \frac{c_i(0)}{c_{wt}(0)} + \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{K - \Sigma_j n_j}{n_{wt}(0)}
|
| 39 |
+
$$
|
| 40 |
+
|
| 41 |
+
So in this regime, the log-ratio of a strain's counts to reference counts is fixed.
|
| 42 |
+
Subtracting the log-ratio of a strain's counts to reference counts in the input leaves:
|
| 43 |
+
|
| 44 |
+
$$
|
| 45 |
+
\log \frac{c_i(t)}{c_{wt}(t)} - \log \frac{c_i(0)}{c_{wt}(0)} = \left(\frac{w_i}{w_{wt}} - 1 \right) \log \frac{K - \Sigma_j n_j}{n_{wt}(0)}
|
| 46 |
+
$$
|
| 47 |
+
|
| 48 |
+
But we still can't get the relative finess without dividing by the constant
|
| 49 |
+
|
| 50 |
+
$$
|
| 51 |
+
\log \frac{K - \Sigma_j n_j}{n_{wt}(0)}
|
| 52 |
+
$$
|
| 53 |
+
|
| 54 |
+
which we don't know directly. However, the final trick is to use a non-growing control, so that
|
| 55 |
+
|
| 56 |
+
$$\frac{w_i}{w_{wt}} = 0$$
|
| 57 |
+
|
| 58 |
+
That means that for this control,
|
| 59 |
+
|
| 60 |
+
$$
|
| 61 |
+
\log \frac{c_i(t)}{c_{wt}(t)} - \log \frac{c_i(0)}{c_{wt}(0)} = - \log \frac{K - \Sigma_j n_j}{n_{wt}(0)}
|
| 62 |
+
$$
|
| 63 |
+
|
| 64 |
+
We can then get the relative fitness ratio for the other strains directly.
|
| 65 |
+
|