Does a knockout change which genes respond to a dose, or how strongly? This guide tests a dose series run in wild-type (+/+) and knockout (-/-) mice, each genotype’s response and the difference between the two, with one model per cell type: dose_response(..., by="genotype"). The data are simulated, so the answers are known (the simulated_genotype_dose_series dataset of the Datasets page): the vehicle and six doses from 0.03 to 10 ug/kg, 3 mice per dose and genotype, hepatocytes and Kupffer cells, 1,500 genes. A fifth of the genes respond in the wild types; in the knockouts, 30% of those lose their response, 20% keep half of it and the rest keep all of it.
Dose crossed with time, genotype or drug explains the model and its tests, and Validation gives their agreement with limma, R’s lm and variancePartition.
import json
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
import cytepool
from cytepool import Design, DoseAxis, check_dose_design, dose_response, read_cube
from cytepool.plotting import plot_dose_response
# The cube: one profile per mouse and cell type, and the truth of each gene.
cube = read_cube("simulated_genotype_dose_series.cube.zarr")
with open("simulated_genotype_dose_series_truth.json") as file:
truth = json.load(file)
print(f"CytePool {cytepool.__version__}")
print(cube)CytePool 0.1.1.dev31+g7516769f3.d20261202
Cube with 42 samples x 2 groups x 1,500 features
layers: counts (int32, lazy)
filled: 84 of 84 sample x group entries have cells (100.0%)
samples: sample_id, genotype, dose
groups: group_id
features: feature_id
Two genotypes, one dose series each¶
How is the study laid out? Each genotype is a dose series of its own, with its own vehicle mice: genotype holds +/+ or -/-, and dose the dose in ug/kg. The truth sorts each gene of a cell type into four kinds: flat in both genotypes, or responsive in the wild types and kept, halved or lost in the knockouts.
# The mice of each genotype at each dose.
genotypes = np.asarray(cube.samples["genotype"])
doses = np.asarray(cube.samples["dose"])
for genotype in ("+/+", "-/-"):
mice = Counter(doses[genotypes == genotype].tolist())
print(
f"{genotype}: " + ", ".join(f"{d:g} ug/kg {n}" for d, n in sorted(mice.items()))
)
# Each gene's kind in the truth.
KINDS = ("kept", "halved", "lost", "flat")
def kinds_of(cell_type):
table = truth["tables"][cell_type]
shape, factor = np.array(table["shape"]), np.array(table["knockout_factor"])
kind = np.where(factor == 0, "lost", np.where(factor == 0.5, "halved", "kept"))
return dict(zip(table["feature_id"], np.where(shape == "flat", "flat", kind)))
kinds = {group: kinds_of(group) for group in cube.groups["group_id"].tolist()}
for group, kind in kinds.items():
counts = Counter(kind.values())
print(f"{group}: " + ", ".join(f"{counts[k]} {k}" for k in KINDS))+/+: 0 ug/kg 3, 0.03 ug/kg 3, 0.1 ug/kg 3, 0.3 ug/kg 3, 1 ug/kg 3, 3 ug/kg 3, 10 ug/kg 3
-/-: 0 ug/kg 3, 0.03 ug/kg 3, 0.1 ug/kg 3, 0.3 ug/kg 3, 1 ug/kg 3, 3 ug/kg 3, 10 ug/kg 3
Hepatocyte: 141 kept, 61 halved, 98 lost, 1200 flat
Kupffer cell: 153 kept, 61 halved, 86 lost, 1200 flat
Check what each genotype supports¶
Can each genotype’s series support tests in each cell type? check_dose_design with by reads one model of all of a cell type’s samples, with an indicator of each genotype and of each dose within a genotype, and gives each genotype its own status. The design lists the genotypes in order: the first, the wild types, is the reference that the interaction tests compare the knockouts with. Text sorts by Unicode code point, where +/+ comes first anyway, since the plus sign precedes the hyphen; giving the levels says so in the code:
axis = DoseAxis("dose", unit="ug/kg", metric="administered")
design = Design(cube.samples, "~ 1", levels={"genotype": ["+/+", "-/-"]})
report = check_dose_design(cube, design, dose=axis, by="genotype")
print(report)DoseDesignReport: 2 cell types, 7 doses from 0 to 10 ug/kg, 2 series of genotype
(+/+ and -/-)
Hepatocyte, genotype +/+: inferential; 7 of 7 dose groups keep a sample, 3
each; 28 residual df
Hepatocyte, genotype -/-: inferential; 7 of 7 dose groups keep a sample, 3
each; 28 residual df
Kupffer cell, genotype +/+: inferential; 7 of 7 dose groups keep a sample, 3
each; 28 residual df
Kupffer cell, genotype -/-: inferential; 7 of 7 dose groups keep a sample, 3
each; 28 residual df
Each line is a cell type and genotype: its status, the dose groups that keep a sample with their replicates, and the residual degrees of freedom of the cell type’s one model, which the two genotypes share. A genotype that lost its vehicle mice in a cell type would be refused there while the other is tested.
Which genes respond in each genotype¶
Which genes respond to the dose in the wild types, and which in the knockouts? dose_response fits each cell type once, so that both genotypes share one moderated variance per gene, and runs each genotype’s tests, gate and each dose against its own vehicle, as it does for one series:
result = dose_response(cube, design, dose=axis, by="genotype")
print(result)DoseResponseResult (limma_voom): 2 cell types, 2 tested; 2 series of genotype,
+/+ and -/-; tests: spline, nonlinearity, linear_trend, dose_groups
dose: ug/kg (administered) on a log10 axis, the vehicle in a group of its own;
spline of 2 columns, knots at 0.548 ug/kg, boundary knots at 0.03 and 10
ug/kg; linear_trend slopes per log10 unit of dose
gate: spline test, Benjamini-Hochberg within each cell type, at 5% FDR
Hepatocyte, genotype +/+: 7 dose groups, 21 samples, 1,325 genes; 148 pass the
gate; calls at 5% FDR: spline 148, nonlinearity 45,
linear_trend 150, dose_groups 136
Hepatocyte, genotype -/-: 7 dose groups, 21 samples, 1,325 genes; 87 pass the
gate; calls at 5% FDR: spline 87, nonlinearity 22,
linear_trend 73, dose_groups 70
Hepatocyte, interactions with genotype +/+: calls at 5% FDR:
spline_interaction 36, nonlinearity_interaction 5,
linear_trend_interaction 34, dose_groups_interaction 32
Kupffer cell, genotype +/+: 7 dose groups, 21 samples, 859 genes; 78 pass the
gate; calls at 5% FDR: spline 78, nonlinearity 16,
linear_trend 69, dose_groups 68
Kupffer cell, genotype -/-: 7 dose groups, 21 samples, 859 genes; 45 pass the
gate; calls at 5% FDR: spline 45, nonlinearity 14,
linear_trend 41, dose_groups 41
Kupffer cell, interactions with genotype +/+: calls at 5% FDR:
spline_interaction 16, nonlinearity_interaction 1,
linear_trend_interaction 15, dose_groups_interaction 12
Each genotype has its line of calls, and interactions with genotype +/+ gives the calls of the interaction tests, which compare the knockouts with the wild types and which the next section reads. A genotype’s tables are read with series: result.table("Hepatocyte", "spline", series="-/-"). The genes each genotype’s gate passes, by their kind in the truth:
for group, kind in kinds.items():
for genotype in ("+/+", "-/-"):
gate = result.table(group, "gate", series=genotype)
passed = Counter(
kind[g] for g in gate["feature_id"][gate["passes_gate"]].tolist()
)
print(f"{group}, {genotype}: " + ", ".join(f"{passed[k]} {k}" for k in KINDS))Hepatocyte, +/+: 60 kept, 33 halved, 50 lost, 5 flat
Hepatocyte, -/-: 63 kept, 21 halved, 0 lost, 3 flat
Kupffer cell, +/+: 40 kept, 15 halved, 20 lost, 3 flat
Kupffer cell, -/-: 39 kept, 5 halved, 0 lost, 1 flat
The knockouts’ gate passes none of the genes whose response they lost, and fewer of those they halved: halving a change of 0.5 to 2 log2 units leaves many below what 3 mice per dose detect.
Which genes respond differently in the knockouts¶
Which genes respond differently in the two genotypes? The interaction tests compare the knockouts’ response with the wild types’ over the same doses: spline_interaction whether the two fitted splines differ anywhere, linear_trend_interaction whether their slopes differ and dose_groups_interaction whether any dose’s change from the vehicle differs. Each is a moderated test of the cell type’s one fit, adjusted by Benjamini-Hochberg within the cell type:
for group, kind in kinds.items():
table = result.table(group, "spline_interaction")
called = table["feature_id"][table["adjusted_p_value"] <= 0.05].tolist()
found = Counter(kind[g] for g in called)
print(
f"{group}: {len(called)} genes at 5% FDR: "
+ ", ".join(f"{found[k]} {k}" for k in KINDS)
)Hepatocyte: 36 genes at 5% FDR: 0 kept, 5 halved, 30 lost, 1 flat
Kupffer cell: 16 genes at 5% FDR: 0 kept, 1 halved, 12 lost, 3 flat
Most genes called lost their response in the knockouts, a few kept half of it, and none kept all of it: the test compares the genotypes’ responses with each other, not each genotype with the vehicle. Losing a response is the largest difference a knockout can make here, so it is the easiest to detect; halving one is harder, as the gates showed. Of the 52 genes called in the two cell types, 4 are flat genes, false discoveries: 1 of 36 in hepatocytes and 3 of 16 in Kupffer cells. A 5% FDR bounds the share of false discoveries on average over studies, not in each one, so one cell type’s share can exceed 5%, as the Kupffer cells’ does. The kept genes, which respond alike in both genotypes, are null for these tests too, and none of them was called.
linear_trend_interaction gives the size and sign of a difference: its log2_fold_change is the knockouts’ slope minus the wild types’, in log2 units per log10 unit of dose. Both slopes weigh each dose by its mice pooled over the genotypes, which here keep 3 mice per dose each, so the difference is that of the genotypes’ own slopes. A gene that loses a rising response has a negative difference:
trend = result.table("Hepatocyte", "linear_trend_interaction")
wild = result.table("Hepatocyte", "linear_trend", series="+/+")
order = np.argsort(np.asarray(trend["p_value"]))[:6]
print(f"{'gene':<11}{'slope +/+':>10}{'difference':>12}{'adjusted p':>12} truth")
for row in order.tolist():
gene = trend["feature_id"][row]
print(
f"{gene:<11}{wild['log2_fold_change'][row]:>10.2f}"
f"{trend['log2_fold_change'][row]:>12.2f}{trend['adjusted_p_value'][row]:>12.1e}"
f" {kinds['Hepatocyte'][gene]}"
)gene slope +/+ difference adjusted p truth
gene_1326 -0.80 0.87 8.7e-11 lost
gene_1283 0.91 -0.87 3.2e-08 lost
gene_1092 -0.87 0.81 1.1e-07 lost
gene_1026 -0.81 0.82 8.1e-06 lost
gene_1332 -0.95 0.96 1.7e-05 lost
gene_1042 0.77 -0.79 2.9e-05 lost
See a gene in both genotypes¶
What does a lost response look like? plot_dose_response draws one genotype’s estimates at each dose and fitted spline with its band; drawn into the same Axes, the second genotype takes the next colour:
# The hepatocyte gene of a Hill curve whose loss the test calls with the smallest
# p-value.
table = result.table("Hepatocyte", "spline_interaction")
shape = dict(
zip(
truth["tables"]["Hepatocyte"]["feature_id"],
truth["tables"]["Hepatocyte"]["shape"],
)
)
best = [
table["feature_id"][r] for r in np.argsort(np.asarray(table["p_value"])).tolist()
]
gene = next(g for g in best if kinds["Hepatocyte"][g] == "lost" and shape[g] == "hill")
fig, ax = plt.subplots(figsize=(6.5, 4.0))
for genotype in ("+/+", "-/-"):
plot_dose_response(result, "Hepatocyte", genes=[gene], series=genotype, ax=ax)
ax.set_title("Hepatocytes")
plt.show()
In the wild types the gene falls with dose, as its Hill curve does, to about 1.7 log2 units below the vehicle at 10 ug/kg; in the knockouts it stays within half a log2 unit of the vehicle at every dose.
What to read where¶
| Question | Where |
|---|---|
| Can each genotype be tested in each cell type? | report.status(group, series=...), report.summary |
| Which genes respond in one genotype? | result.table(group, "spline", series=...) and the other tests, "gate" |
| From which dose does a gene respond in one genotype? | result.lowest_effective_doses(group, series=...) |
| Do the genotypes’ responses differ? | result.table(group, "spline_interaction"), "dose_groups_interaction" |
| By how much does the slope differ? | result.table(group, "linear_trend_interaction") |
Curves of named models, benchmark doses and EC50s run on one genotype at a time: dose_response on that genotype’s samples, with a design of them.