Skip to content

Normalize with frozen controls

Normalization changes the reference for a relative count comparison. The main analysis uses the 997 non-targeting guides that pass the day-14 DMSO count filter. Their identities remain fixed in every comparison, including the median-normalization sensitivity run.

MAGeCK 0.5.9.5 calculates control normalization as its median normalization procedure restricted to the supplied controls. The preparation calls that same upstream function and checks its full precision factors against the actual engine log. The examples read those preserved factors rather than estimate factors from rounded report values.

The factor shown here multiplies an RRA input count. The pinned MLE reader adds one to each count before normalization, then uses the reciprocal of its own factor in the model. Its separate factors remain in the execution record. A positive guide log2 ratio means greater normalized representation in drug cultures. The descriptive ratio adds one after the culture mean; it is not a MAGeCK p-value.

The median sensitivity changes only the normalization reference. It uses the same count rows, sample order, non-targeting list, and RRA permutation settings. Keeping --control-sgrna matters because that argument also supplies the RRA null distribution. Removing it would change two analysis choices at once.

library(ggplot2)
library(jsonlite)
dir.create("outputs", showWarnings = FALSE)
counts <- read.delim("/fixtures/main_counts.tsv", check.names = FALSE)
controls <- readLines("/fixtures/control_guides.txt")
primary <- read.delim("/fixtures/rra_primary_normalization.tsv")
median_norm <- read.delim("/fixtures/rra_median_normalization.tsv")
samples <- names(counts)[-(1:2)]
stopifnot(identical(primary$sample, samples))
stopifnot(identical(median_norm$sample, samples))
stopifnot(setequal(counts$sgRNA[counts$Gene == "Control"], controls))
values <- as.matrix(counts[, samples])
normalized <- sweep(values, 2, primary$count_multiplier, "*")
median_values <- sweep(values, 2, median_norm$count_multiplier, "*")
control_lfc <- log2((rowMeans(normalized[, 3:4]) + 1) /
(rowMeans(normalized[, 1:2]) + 1))
median_lfc <- log2((rowMeans(median_values[, 3:4]) + 1) /
(rowMeans(median_values[, 1:2]) + 1))
selected <- counts$sgRNA %in% controls
summary <- data.frame(sgRNA = counts$sgRNA[selected],
control_lfc = control_lfc[selected],
median_lfc = median_lfc[selected])
write.table(summary, "outputs/summary_r.tsv", sep = "\t",
quote = FALSE, row.names = FALSE)
metrics <- list(controls = nrow(summary),
control_center = median(summary$control_lfc),
median_center = median(summary$median_lfc),
factor_difference = max(abs(primary$count_multiplier -
median_norm$count_multiplier)))
write_json(metrics, "outputs/metrics_r.json", auto_unbox = TRUE, digits = 16)
cat(sprintf("Frozen non-targeting guides: %d\n", nrow(summary)))
cat(sprintf("Control-normalized median log2 ratio: %.6f\n",
metrics$control_center))
cat(sprintf("Median-normalized median log2 ratio: %.6f\n",
metrics$median_center))
print(data.frame(sample = samples, control = primary$count_multiplier,
median = median_norm$count_multiplier), row.names = FALSE)
plot_data <- data.frame(
method = rep(c("Control", "Median"), each = nrow(summary)),
log2_ratio = c(summary$control_lfc, summary$median_lfc))
figure <- ggplot(plot_data, aes(method, log2_ratio, fill = method)) +
geom_violin(alpha = 0.5) + geom_boxplot(width = 0.15, outlier.shape = NA) +
geom_hline(yintercept = 0, linetype = 2) + theme_bw(base_size = 12) +
guides(fill = "none") +
labs(x = "Normalization reference", y = "Control guide log2 drug/DMSO")
ggsave("outputs/control_distribution_r.png", figure,
width = 6.6, height = 4.6, dpi = 160)
Frozen non-targeting guides: 997
Control-normalized median log2 ratio: 0.034103
Median-normalized median log2 ratio: -0.299888
sample control median
C1D14 0.6864403 0.7646623
C2D14 0.7430563 0.8120529
P1D14 1.3128881 1.1523092
P2D14 1.4942026 1.3057843

The distributions compare control-guide log2 ratios under control-based and median normalization.

The same 997 guides estimate the control factors and supply the displayed control distribution, so normalization partly imposes its near-zero center. The plot checks the transformation and cannot independently establish that the reference is appropriate. It also cannot prove that drug treatment leaves every control culture biologically unchanged. The MAGeCK lesson keeps the primary discovery family separate from its method comparison.

The MAGeCK 0.5.9.5 source archive contains mageckCountNorm.py and mleinstanceio.py. The first defines the normalization factors; the second adds one to counts for the MLE reader. These files explain why the RRA and MLE factors require separate checks in this example.