# Generate the expression-matrix toy data used by heatmap recipes.
script_dir <- local({
arg <- grep("^--file=", commandArgs(trailingOnly = FALSE), value = TRUE)
path <- if (length(arg)) sub("^--file=", "", arg[[1L]]) else sys.frame(1)$ofile
dirname(normalizePath(path, mustWork = TRUE))
})
out_csv <- file.path(script_dir, "..", "csv", "expression_heatmap.csv")
dir.create(dirname(out_csv), recursive = TRUE, showWarnings = FALSE)
set.seed(20260902)
pathways <- data.frame(
prefix = c("CC", "IR", "LM"),
pathway = c("Cell cycle", "Immune response", "Lipid metabolism"),
tumor_effect = c(1.60, 0.90, -1.30)
)
genes <- do.call(rbind, lapply(seq_len(nrow(pathways)), function(i) {
data.frame(
gene = sprintf("%s_G%02d", pathways$prefix[i], seq_len(12L)),
pathway = pathways$pathway[i],
gene_baseline = rnorm(12L, mean = 6, sd = 0.65),
tumor_effect = pathways$tumor_effect[i] + rnorm(12L, 0, 0.18)
)
}))
samples <- data.frame(
sample = sprintf("Sample%02d", seq_len(18L)),
type = rep(c("Normal", "Tumor"), each = 9L),
sample_offset = rnorm(18L, mean = 0, sd = 0.28)
)
expression_heatmap <- merge(
genes[, c("gene", "pathway", "gene_baseline", "tumor_effect")],
samples,
by = NULL
)
# Three coherent pathway blocks, with gene-, sample-, and observation-level
# variation. The values are synthetic log-expression, not biological results.
expression_heatmap$expression <- with(
expression_heatmap,
gene_baseline + sample_offset +
ifelse(type == "Tumor", tumor_effect, 0) +
rnorm(nrow(expression_heatmap), mean = 0, sd = 0.45)
)
# Fixed missing cells make NA rendering reproducible without weakening any
# entire gene or sample enough to prevent clustering.
missing_cells <- c(37L, 146L, 255L, 364L, 473L, 582L)
expression_heatmap$expression[missing_cells] <- NA_real_
expression_heatmap <- expression_heatmap[, c(
"gene", "sample", "expression", "pathway", "type"
)]
expression_heatmap <- expression_heatmap[order(
match(expression_heatmap$gene, genes$gene),
match(expression_heatmap$sample, samples$sample)
), ]
expression_heatmap$expression <- round(expression_heatmap$expression, 4)
rownames(expression_heatmap) <- NULL
write.csv(expression_heatmap, out_csv, row.names = FALSE, na = "NA")