Last updated: 2026-07-28
Checks: 7 0
Knit directory:
immgenT-GP-analysis/analysis/
This reproducible R Markdown analysis was created with workflowr (version 1.7.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(1) was run prior to running the
code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 0f5b5da. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .claude/
Ignored: analysis/.DS_Store
Ignored: analysis/.Rhistory
Ignored: analysis/assets/.DS_Store
Ignored: captions/
Ignored: code/.DS_Store
Ignored: code/other/topic_flashier_20250212.R
Ignored: code/other/topic_wrapper_20250215_alldata_backfit.sh
Ignored: data
Ignored: experiments/
Ignored: figures/.DS_Store
Ignored: figures/Previous/.DS_Store
Ignored: figures/Previous/bits/.DS_Store
Ignored: figures/Previous/bits/Figure 1/.DS_Store
Ignored: figures/Previous/bits/Figure 2/.DS_Store
Ignored: figures/Previous/bits/Figure 3/.DS_Store
Ignored: figures/Previous/bits/Figure 4/.DS_Store
Ignored: figures/Previous/bits/Figure 6/.DS_Store
Ignored: figures/Previous/bits/Figure 7/.DS_Store
Ignored: figures/Previous/bits/Figure S1/.DS_Store
Ignored: figures/Previous/bits/Figure S2/.DS_Store
Ignored: figures/Previous/bits/Figure S3/.DS_Store
Ignored: figures/Previous/bits/Figure S6/.DS_Store
Ignored: figures/Previous/bits/Figure S7/.DS_Store
Ignored: figures/final-selected/.DS_Store
Ignored: figures/final-selected/Figure 1/.DS_Store
Ignored: figures/final-selected/Figure 2/.DS_Store
Ignored: figures/final-selected/Figure S4/.DS_Store
Ignored: log/
Ignored: output/Figure2/
Ignored: output/FigureS5/S5_cell_metadata.csv.gz
Ignored: plan/
Ignored: reorder/
Ignored: tables/
Ignored: tmp/
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/FigureS1.Rmd) and HTML
(docs/FigureS1.html) files. If you’ve configured a remote
Git repository (see ?wflow_git_remote), click on the
hyperlinks in the table below to view the files as they were in that
past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | 0f5b5da | Ziang Zhang | 2026-07-28 | Align all figure captions with captions_20260728_final.docx |
| html | 3fc3789 | Ziang Zhang | 2026-07-27 | Republish all 24 pages |
| html | 1390a03 | Ziang Zhang | 2026-07-27 | Republish all 24 pages |
| html | 8a62cc5 | Ziang Zhang | 2026-07-27 | Build site: S1C/S1D on the shared 18-IGT basis |
| Rmd | 39de9e0 | Ziang Zhang | 2026-07-27 | Compute S1C and S1D from one matrix over the 18 >=500-cell IGTs |
| html | 6c1a613 | Ziang Zhang | 2026-07-27 | Build site: S1D now follows S1C |
| Rmd | bf7afcf | Ziang Zhang | 2026-07-27 | Make S1D consistent with S1C, and drop the legacy pages from docs/ |
| html | adaef21 | Ziang Zhang | 2026-07-27 | Build site: panel fixes and PDF-derived assets |
| Rmd | 9e032a7 | Ziang Zhang | 2026-07-27 | Fix four panels that diverged from the published figures; make renders reproducible |
| html | 5b19858 | Ziang Zhang | 2026-07-27 | Build site. |
| Rmd | 91ee059 | Ziang Zhang | 2026-07-26 | Select each panel’s code block by name, not by line number |
| html | 91ee059 | Ziang Zhang | 2026-07-26 | Select each panel’s code block by name, not by line number |
| Rmd | c655a83 | Ziang Zhang | 2026-07-16 | Fig S1E: use empirical active-gene/cell counts (match Figure 2) |
| html | c655a83 | Ziang Zhang | 2026-07-16 | Fig S1E: use empirical active-gene/cell counts (match Figure 2) |
| Rmd | c2b3360 | Ziang Zhang | 2026-07-13 | Use log-scale axes for Fig 1E/1F and add Fig S1E gene-vs-cell sparsity scatter |
| html | c2b3360 | Ziang Zhang | 2026-07-13 | Use log-scale axes for Fig 1E/1F and add Fig S1E gene-vs-cell sparsity scatter |
| html | 92021bf | Ziang Zhang | 2026-07-02 | Build site. |
| html | 827c89b | Ziang Zhang | 2026-07-02 | Build site. |
| Rmd | 8ac7f9f | Ziang Zhang | 2026-07-02 | Add data provenance notes to each script; remove conversational |
| Rmd | f9db962 | Ziang Zhang | 2026-07-02 | Simplify layout: drop old code/script folders, rename |
| html | c6e5086 | Ziang Zhang | 2026-07-02 | Build site. |
| html | 5a79883 | Ziang Zhang | 2026-07-02 | Build site. |
| Rmd | 2b0e445 | Ziang Zhang | 2026-07-02 | Fix GitHub source links to point at the new |
| html | cf1d0ac | Ziang Zhang | 2026-07-02 | Build site. |
| Rmd | 06b2461 | Ziang Zhang | 2026-07-02 | Initial commit: immgenT-GP-analysis |
| html | 06b2461 | Ziang Zhang | 2026-07-02 | Initial commit: immgenT-GP-analysis |
Published as Extended Data Figure 1.
All panels are produced by script/FigureS1.R.
The code below is shown for reference (not re-executed on this page);
the images are its pre-rendered output. Panels A/B reuse a cached
per-IGT cosine-similarity score matrix
(data/igt_specific_cosine_scores.csv); see
code/pipeline/05_igt_validation.R for how that matrix
itself is produced (a much heavier, cluster-scale computation).
# Figure S1. GP reproducibility across IGTs.
#
# Panels produced (see figures/Previous/bits/Figure S1/FigureS1_caption.md
# for the full caption text):
# S1A Cumulative number of GPs validated (cosine >= threshold, thresholds
# 0.2-0.8) as IGTs are added one at a time, in IGT index order.
# S1B Number of GPs validated by at least X IGTs, vs X (log-log), for the
# same thresholds.
# S1C Between-IGT variability: each GP's mean-of-per-IGT-mean-loading (x)
# vs. variance-of-per-IGT-mean-loading (y).
# S1D Heatmap of per-IGT mean loading for S1C's 10 highest-variance GPs.
#
# S1C and S1D share one per-IGT mean-loading matrix, over the
# standard-spleen subset restricted to the 18 IGTs with >= 500 such
# cells. Figure_batch.R built it twice on different IGT sets (S1C over
# all 35, S1D over the 18), which made the published panels disagree --
# see the note in the "Load data for S1C/S1D" section.
# S1E Scatter of the NUMBER of active genes (x) vs. proportion of active
# cells (y) per GP, using the same hard-threshold definitions as Figure 2
# (|normalized score| > 0.25 for genes; normalized loading > 0.1 for
# cells), over non-thymocyte cells -- not the EBMF sparsity prior. One
# dot per GP.
#
# Source: S1A/S1B ported from Figure_Saturation.R; S1C/S1D from
# Figure_batch.R (panels a/b only -- that script's `plot_gp_loading()`
# helper is defined but never called for a saved output, so it's dropped).
#
# S1A/S1B reuse the per-IGT cosine-matching score matrix
# (data/igt_specific_cosine_scores.csv) rather than recomputing it here --
# recomputing requires Hungarian-matching each of the ~80 per-IGT
# refactorizations in data/igt_specific/*.qs against the full model, which is
# the job of code/pipeline/05_igt_validation.R (run once upstream).
#
# Required inputs (data/) -- see code/README.md's "Data provenance" table
# for the full picture:
# igt_specific_cosine_scores.csv [code/pipeline/05_igt_validation.R]
# L_pm_filtered.rds [code/pipeline/01b_filter_cells.R]
# igt1_96_..._ADTonly.Rds [primary input Seurat object]
library(dplyr)
library(tidyr)
library(ggplot2)
library(pheatmap)
Panels A and B both use the cached per-IGT cosine score matrix:
data_path <- "data/"
figure_path <- "figures/final-selected/Figure S1/"
gp_label <- function(x) sub("^K(\\d+)$", "GP\\1", x)
# ============================================================
# S1A/S1B: load the cached per-IGT cosine score matrix
# (GPs x IGTs; produced by code/pipeline/05_igt_validation.R)
# ============================================================
score_mat <- as.matrix(read.csv(paste0(data_path, "igt_specific_cosine_scores.csv"), row.names = 1, check.names = FALSE))
# ============================================================
# S1A: cumulative number of GPs validated as IGTs are added, in IGT-index order
# ============================================================
igt_idx <- as.integer(gsub("^IGT", "", colnames(score_mat)))
o <- order(igt_idx)
score_mat_ord <- score_mat[, o, drop = FALSE]
cum_validated_counts <- function(score_mat_ord, threshold) {
validated <- score_mat_ord >= threshold
ever_validated <- t(apply(validated, 1, cummax)) # 200 x nIGT logical
colSums(ever_validated)
}
plot_df_a <- lapply(thresholds, function(t) {
y <- cum_validated_counts(score_mat_ord, t)
data.frame(n_IGTs_included = seq_along(y), validated_GPs = y, threshold = factor(t))
}) %>% bind_rows()
p_S1A <- ggplot(plot_df_a, aes(x = n_IGTs_included, y = validated_GPs, color = threshold)) +
geom_line(linewidth = 1) +
geom_point(size = 1) +
labs(x = "Number of IGTs included (in IGT index order)", y = "Number of validated GPs (cumulative union)", color = "Threshold") +
theme_minimal() +
scale_color_brewer(palette = "Set1") +
scale_x_continuous(breaks = seq(0, ncol(score_mat_ord), by = 5)) +
scale_y_continuous(breaks = seq(0, max(plot_df_a$validated_GPs), by = 20))
ggsave(paste0(figure_path, "S1A.pdf"), plot = p_S1A, width = 6, height = 4)

Fig. S1A. Cumulative number of GPs, out of the 200 identified in the full immgenT solution, reproduced in at least one dataset (IGT). For each dataset an EBMF factorization was computed and its factors were matched to the 200 immgenT GPs by Hungarian assignment using the cosine similarity of gene-score vectors (restricted to shared genes, with columns scaled); a GP was considered reproduced in a dataset when this cosine similarity exceeded the threshold indicated for each curve. Adding IGTs one at a time in index order, the curves show the cumulative count for cosine-similarity thresholds of 0.2-0.8.
# ============================================================
# S1B: number of GPs validated by at least X IGTs, vs X (log-log)
# ============================================================
thresholds <- seq(0.2, 0.8, by = 0.1)
X_grid <- 1:50
plot_df_b <- tidyr::crossing(threshold = thresholds, X = X_grid) %>%
mutate(n_GP = purrr::map2_int(threshold, X, \(t, x) {
rowSums(score_mat >= t, na.rm = TRUE) |> (\(v) sum(v >= x))()
}))
p_S1B <- ggplot(plot_df_b, aes(x = X, y = n_GP, group = factor(threshold))) +
geom_line() +
geom_point(size = 1) +
scale_y_log10() +
scale_x_log10() +
labs(x = "X (validated by at least X IGTs)", y = "Number of GPs", color = "Threshold") +
aes(color = factor(threshold)) +
theme_minimal() +
scale_color_brewer(palette = "Set1")
ggsave(paste0(figure_path, "S1B.pdf"), plot = p_S1B, width = 6, height = 4)

Fig. S1B. Distribution of GP reproducibility across datasets. Using the same per-IGT cosine matching, the curves show the number of GPs reproduced in at least that many datasets, for cosine-similarity thresholds ranging from 0.2 to 0.8 (both axes log-scaled).
# ============================================================
# Load data for S1C/S1D
# ============================================================
L_pm_filtered <- readRDS(paste0(data_path, "L_pm_filtered.rds"))
seurat_meta <- readRDS(paste0(data_path, "igt1_96_withtotalvi20260206_clean_ADTonly.Rds"))@meta.data
seurat_meta_filtered <- seurat_meta[rownames(L_pm_filtered), ]
seurat_meta_filtered_spleen <- seurat_meta_filtered %>% filter(spleen_standard == TRUE)
# S1C and S1D are both computed from ONE per-IGT mean-loading matrix
# (IGTs x GPs) over the standard-spleen subset, restricted to the IGTs with
# >= 500 such cells -- an IGT mean over a handful of cells is too noisy to
# either rank on or draw. Both panels therefore describe the same 18 IGTs, and
# S1C's ten labelled GPs are exactly S1D's ten rows.
#
# Figure_batch.R built this matrix twice instead: S1C over all 35 spleen IGTs,
# S1D over the >= 500-cell subset. The two rankings disagree -- GP2 is 7th on
# the subset but 11th over all 35, GP25 is 6th over all 35 but 23rd on the
# subset -- so the published S1C labels GP25 while the published S1D shows GP2,
# and "the top ten from (C)" could not be followed across the two panels.
# Computing it once removes the inconsistency and the chance of the two
# drifting apart again.
spleen_cells <- intersect(rownames(L_pm_filtered), rownames(seurat_meta_filtered_spleen))
igt_vec <- seurat_meta_filtered_spleen[spleen_cells, "IGT"]
selected_igts <- names(table(igt_vec))[table(igt_vec) >= 500]
igt_mean_mat <- do.call(rbind, lapply(selected_igts, function(igt) {
colMeans(L_pm_filtered[spleen_cells[igt_vec == igt], , drop = FALSE])
}))
rownames(igt_mean_mat) <- selected_igts
gp_igt_var <- apply(igt_mean_mat, 2, var)
gp_overall <- colMeans(igt_mean_mat)
# ============================================================
# S1C: GP mean-of-IGT-mean-loading vs. variance-of-IGT-mean-loading (spleen)
# ============================================================
gp_stats <- data.frame(GP = colnames(igt_mean_mat), x = gp_overall, y = gp_igt_var) %>%
arrange(desc(y)) %>%
mutate(label = ifelse(row_number() <= 10, gp_label(as.character(GP)), ""))
p_S1C <- ggplot(gp_stats, aes(x = x, y = y, label = label)) +
geom_point(size = 1.5, alpha = 0.7, color = "steelblue") +
ggrepel::geom_text_repel(seed = 42, size = 3, box.padding = 0.4, max.overlaps = Inf, segment.color = "grey50") +
cowplot::theme_cowplot() +
labs(
title = "GP Mean of IGT Mean Loading vs. Between-IGT VAR",
x = "Mean of IGT Mean Loading",
y = "Variance of IGT Mean Loading"
)
ggsave(paste0(figure_path, "S1C.pdf"), plot = p_S1C, width = 6, height = 5, dpi = 300)

Fig. S1C. Batch-effect evaluation using the spleen standards spiked into each dataset (IGT): variance versus mean GP loading across datasets, considering only spleen-standard cells, in the 18 datasets contributing at least 500 such cells – so that no per-IGT mean rests on a handful of cells. Each GP’s mean loading is computed within every one of those 18 IGTs; each point is a GP, plotting the mean across IGTs of these per-IGT mean loadings (x-axis) against their variance across IGTs (y-axis). The ten GPs with the highest between-dataset variance are labeled, and are the ten shown in (D).
# ============================================================
# S1D: heatmap of per-IGT mean loading for S1C's ten highest-variance GPs
# ============================================================
# Same `igt_mean_mat` and same `gp_igt_var` as S1C, so these ten rows are the
# ten GPs S1C labels -- see the note where that matrix is built.
top10_var_gps <- names(sort(gp_igt_var, decreasing = TRUE))[1:10]
plot_mat <- t(igt_mean_mat[, top10_var_gps, drop = FALSE])
plot_mat[plot_mat < 0] <- 0
rownames(plot_mat) <- gp_label(rownames(plot_mat))
pdf(paste0(figure_path, "S1D.pdf"), width = 5, height = 5)
pheatmap(
plot_mat,
cluster_rows = TRUE,
cluster_cols = FALSE,
main = "Top 10 GPs by Variance of IGT Mean Loading",
color = colorRampPalette(c("white", "red"))(100),
border_color = "white",
fontsize_row = 8,
angle_col = 45
)
dev.off()

Fig. S1D. Heatmap showing the mean loading of these same ten GPs across the same 18 datasets – the same ten GPs labelled in (C), over the same IGTs, from the same matrix. Rows (GPs) are hierarchically clustered; color runs from white (low) to red (high mean loading).
# ============================================================
# S1E: active-gene vs active-cell scatter per GP, using the SAME hard-threshold
# definitions as Figure 2 (per-GP-normalized): number of active genes = count of
# genes with |score| > 0.25 of the GP's max; proportion of active cells = fraction
# of cells with loading > 0.1 of the GP's max. Non-thymocyte cells, matching
# Figure 2. (Replaces the earlier EBMF-sparsity-prior version.)
# ============================================================
non_thymo_s1e <- seurat_meta_filtered$cellID[seurat_meta_filtered$annotation_level1 != "thymocyte"]
L_s1e <- L_pm_filtered[non_thymo_s1e, ]
L_norm_s1e <- L_s1e / matrix(apply(L_s1e, 2, max), nrow = nrow(L_s1e), ncol = ncol(L_s1e), byrow = TRUE)
prop_cells <- colSums(L_norm_s1e > 0.1) / nrow(L_norm_s1e) # proportion of active cells per GP
F_s1e <- readRDS(paste0(data_path, "F_pm_filtered.rds"))
F_norm_s1e <- F_s1e / matrix(apply(F_s1e, 2, function(x) max(abs(x))), nrow = nrow(F_s1e), ncol = ncol(F_s1e), byrow = TRUE)
n_genes_act <- colSums(abs(F_norm_s1e) > 0.25) # number of active genes per GP
scatter_df_s1e <- data.frame(n_genes = n_genes_act, prop_cells = prop_cells)
pct_breaks <- c(0.0001, 0.001, 0.01, 0.05, 0.1, 0.3, 0.5, 1)
p_S1E <- ggplot(scatter_df_s1e, aes(x = n_genes, y = prop_cells)) +
geom_point(size = 2, alpha = 0.7, color = "steelblue") +
scale_x_log10(labels = scales::label_comma()) +
scale_y_log10(breaks = pct_breaks, labels = function(x) paste0(x * 100, "%")) +
annotation_logticks(sides = "bl") +
labs(
x = "Number of active genes per GP (log scale)",
y = "Proportion of active cells per GP (log scale)",
title = "Active genes vs. active-cell proportion per GP"
) +
theme_minimal(base_size = 13)
ggsave(paste0(figure_path, "S1E.pdf"), plot = p_S1E, width = 6, height = 5, dpi = 300)

Fig. S1E. For each GP, the number of active genes (x-axis) versus the fraction of active cells (y-axis), using the same thresholds as Figure 2: a gene is active in a GP if its per-GP-normalized score exceeds 0.25 in absolute value, and a cell is active if its per-GP-normalized loading exceeds 0.1; computed over non-thymocyte cells. Both axes on log scale; each dot is one of the 200 GPs.
sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.6.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
locale:
[1] en_CA/en_CA/en_CA/C/en_CA/en_CA
time zone: America/Chicago
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] vctrs_0.7.3 cli_3.6.6 knitr_1.50 rlang_1.2.0
[5] xfun_0.55 stringi_1.8.7 otel_0.2.0 promises_1.5.0
[9] jsonlite_2.0.0 workflowr_1.7.2 glue_1.8.1 rprojroot_2.1.1
[13] git2r_0.36.2 htmltools_0.5.9 httpuv_1.6.16 sass_0.4.10
[17] rmarkdown_2.30 evaluate_1.0.5 jquerylib_0.1.4 tibble_3.3.0
[21] fastmap_1.2.0 yaml_2.3.12 lifecycle_1.0.5 whisker_0.4.1
[25] stringr_1.6.0 compiler_4.5.1 fs_1.6.6 Rcpp_1.1.1-1.1
[29] pkgconfig_2.0.3 later_1.4.4 digest_0.6.39 R6_2.6.1
[33] pillar_1.11.1 magrittr_2.0.5 bslib_0.9.0 tools_4.5.1
[37] cachem_1.1.0