# Chapter 9, part B: Multivariate methods for heterogeneous data
# (nonlinear ordination of single-cell data, multitable methods, and the chapter's exercises).
#
# Reproduces the computed figures 9.27 to 9.41 of Holmes and Huber (2019), Modern Statistics for Modern
# Biology, Chapter 9. Part A (ch09_heterogeneous_data_a.R) covers Figures 9.1 to 9.25.
# Written for the course "Statistical Methods for Modern Biology"; the analyses follow the book's steps,
# the code is my own.
#
# Figures not reproduced:
#   * Figure 9.26 (the cell-type tree of Moignard et al.): a hand-drawn schematic.
# Figures 9.30, 9.39, 9.40 and 9.41 are screenshots of interactive rgl views in the book; here they are
# redrawn as static 3-d scatterplots of the same computed coordinates (t-SNE, diffusion map, principal
# curve).
#
# The Moignard computations (3,934 cells: distance matrices, eigen-decompositions, t-SNE, kernel MDS,
# diffusion maps) are cached in code/cache/ch09-*.rds; the first full run takes several minutes.
#
# Run:   Rscript ch09_heterogeneous_data_b.R            (all figures of part B)
#        Rscript ch09_heterogeneous_data_b.R fig_9_32   (one figure)

this_dir <- local({
  a <- grep("^--file=", commandArgs(FALSE), value = TRUE)
  if (length(a)) dirname(normalizePath(sub("^--file=", "", a[1]))) else getwd()
})
source(file.path(this_dir, "common.R"))

suppressPackageStartupMessages({
  library(dplyr)
  library(ggrepel)
  library(patchwork)
  library(ade4)
  library(vegan)
})

CACHE <- file.path(CODE_DIR, "cache")
dir.create(CACHE, showWarnings = FALSE)
cached <- function(name, expr) {
  f <- file.path(CACHE, paste0("ch09-", name, ".rds"))
  if (file.exists(f)) return(readRDS(f))
  val <- force(expr)
  saveRDS(val, f)
  val
}

# ===================================================================================================
# Helpers
# ===================================================================================================
scree_plot <- function(values, m = length(values), ylab = "eigenvalue") {
  df <- data.frame(k = factor(seq_len(m), levels = seq_len(m)), v = values[seq_len(m)])
  ggplot(df, aes(k, v)) + geom_col(width = 0.55, fill = COL[["blue"]], colour = "grey25", linewidth = 0.25) +
    geom_hline(yintercept = 0, linewidth = 0.3) + labs(x = "k", y = ylab)
}
pct_scree <- function(eig, m = 10, ylab = "percentage of inertia") {
  pct <- 100 * eig / sum(eig)
  m <- min(m, length(eig))
  df <- data.frame(k = factor(seq_len(m), levels = seq_len(m)), v = pct[seq_len(m)])
  ggplot(df, aes(k, v)) + geom_col(width = 0.6, fill = "#4682b4") + geom_line(aes(group = 1)) +
    geom_point() + labs(x = "dimensions", y = ylab)
}

# Classical MDS for a large distance matrix: all eigenvalues of B = -1/2 H (D*D) H (needed for the
# "percentage of variability" of the book, which divides by their sum) and the leading k eigenvectors.
# D is a 'dist' object or a square matrix of distances (they are squared here, as in cmdscale).
cmds_big <- function(D, k = 4) {
  M <- as.matrix(D)^2
  rm <- rowMeans(M); gm <- mean(rm)
  B <- -0.5 * (M - outer(rm, rep(1, length(rm))) - outer(rep(1, length(rm)), rm) + gm)
  rm(M)
  vals <- eigen(B, symmetric = TRUE, only.values = TRUE)$values
  top <- RSpectra::eigs_sym(B, k = k, which = "LA")
  o <- order(top$values, decreasing = TRUE)
  list(eig = vals, points = top$vectors[, o] %*% diag(sqrt(pmax(top$values[o], 0))))
}

scatter3 <- function(xyz, col, labs, angle = 40, main = "", cex = 0.45) {
  par(mar = c(2.5, 2.5, 2, 1))
  scatterplot3d::scatterplot3d(xyz[, 1], xyz[, 2], xyz[, 3], color = col, pch = 16, cex.symbols = cex,
                               xlab = labs[1], ylab = labs[2], zlab = labs[3], angle = angle, main = main,
                               cex.axis = 0.7, cex.lab = 0.9)
}

# ===================================================================================================
# Moignard et al. (2015) single-cell qPCR data: 3,934 cells x 46 genes
# ===================================================================================================
load_moignard <- function() {
  M <- readRDS(msmb_data("Moignard.rds"))
  rd <- SummarizedExperiment::rowData(M)
  list(blom = SummarizedExperiment::assay(M), type = as.character(rd$celltypes), col = as.character(rd$cellcol))
}
CELL_COLS <- c(PS = "blue", NP = "green3", HF = "orange", `4SG` = "red", `4SGF-` = "purple")

moignard_mds <- function() cached("moignard-mds", {
  m <- load_moignard()
  l1 <- cmds_big(dist(m$blom, "manhattan"), 4)
  l2 <- cmds_big(dist(m$blom), 4)
  raw <- as.matrix(read.csv(msmb_data("nbt.3154-S3-raw.csv"), row.names = 1))
  r1 <- cmds_big(dist(raw, "manhattan"), 2)
  r2 <- cmds_big(dist(raw), 2)
  list(l1 = l1, l2 = l2, raw_l1_eig = r1$eig, raw_l2_eig = r2$eig)
})

fig_9_27 <- function() {
  R <- moignard_mds()
  p <- (scree_plot(R$l1$eig, 4) + ggtitle(expression("(a) " * l[1] * " distance"))) +
    (scree_plot(R$l2$eig, 4) + ggtitle(expression("(b) " * L[2] * " distance")))
  savefig(p, "fig-9-27", ch = 9, width = 7, height = 3.2)
}

cell_scatter <- function(xy, type, xlab, ylab, title = NULL) {
  df <- data.frame(x = xy[, 1], y = xy[, 2], type = factor(type, levels = names(CELL_COLS)))
  ggplot(df, aes(x, y, colour = type)) + geom_point(alpha = 0.55, size = 0.7) +
    scale_colour_manual("cell type", values = CELL_COLS) +
    guides(colour = guide_legend(override.aes = list(size = 2.5, alpha = 1))) +
    labs(x = xlab, y = ylab, title = title)
}

fig_9_28 <- function() {
  R <- moignard_mds(); m <- load_moignard()
  p <- (cell_scatter(R$l1$points, m$type, "L1_PCo1", "L1_PCo2", expression("(a) " * l[1])) +
          cell_scatter(R$l2$points, m$type, "L2_PCo1", "L2_PCo2", expression("(b) " * L[2]))) +
    plot_layout(guides = "collect")
  savefig(p, "fig-9-28", ch = 9, width = 10, height = 4.4, dense = TRUE)
}

moignard_tsne <- function() cached("moignard-tsne", {
  m <- load_moignard()
  set.seed(9)
  t2 <- Rtsne::Rtsne(m$blom, dims = 2, perplexity = 30, max_iter = 900, verbose = FALSE, num_threads = 1)
  set.seed(9)
  t3 <- Rtsne::Rtsne(m$blom, dims = 3, perplexity = 30, max_iter = 900, verbose = FALSE, num_threads = 1)
  list(Y2 = t2$Y, Y3 = t3$Y, cost2 = sum(t2$costs), cost3 = sum(t3$costs))
})

fig_9_29 <- function() {
  T <- moignard_tsne(); m <- load_moignard()
  p <- (cell_scatter(T$Y2, m$type, "axis1", "axis2", "(a) 2-d t-SNE") +
          cell_scatter(T$Y3[, c(3, 2)], m$type, "axis3", "axis2", "(b) 3-d t-SNE, axes 3 and 2")) +
    plot_layout(guides = "collect")
  savefig(p, "fig-9-29", ch = 9, width = 10, height = 4.4, dense = TRUE)
}

fig_9_30 <- function() {
  T <- moignard_tsne(); m <- load_moignard()
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    scatter3(T$Y3, m$col, c("taxis1", "taxis2", "taxis3"), angle = 30, main = "(a)")
    scatter3(T$Y3[, c(1, 3, 2)], m$col, c("taxis1", "taxis3", "taxis2"), angle = 120, main = "(b)")
  }
  savefig(draw, "fig-9-30", ch = 9, width = 10, height = 5, dense = TRUE)
}

fig_9_31 <- function() {
  e <- new.env(); load(msmb_data("ukrainecities.RData"), envir = e)
  set.seed(31)
  ts <- Rtsne::Rtsne(as.matrix(e$ukraine_dists), is_distance = TRUE, perplexity = 8, verbose = FALSE,
                     num_threads = 1)
  df <- data.frame(t1 = ts$Y[, 1], t2 = ts$Y[, 2], city = attr(e$ukraine_dists, "Labels"))
  p <- ggplot(df, aes(t1, t2, label = city)) + geom_point(size = 1.3) +
    geom_text_repel(colour = "#0057b7", size = 2.6, seed = 1, max.overlaps = 50, segment.size = 0.2) +
    coord_fixed() + labs(x = "t-SNE 1", y = "t-SNE 2")
  savefig(p, "fig-9-31", ch = 9, width = 6, height = 4.6)
}

# ===================================================================================================
# 9.6 Multitable methods: Kashyap et al. (2013) microbes and metabolites, 12 samples
# ===================================================================================================
load_kashyap <- function() {
  suppressPackageStartupMessages({ library(phyloseq); library(genefilter) })
  e <- new.env(); load(msmb_data("microbe.rda"), envir = e)
  microbe <- e$microbe
  metab <- as.matrix(read.csv(msmb_data("metabolites.csv"), row.names = 1))
  metab <- metab[rowSums(metab == 0) <= 3, ]                       # metabolites seen in at least 9 samples
  microbe <- prune_taxa(taxa_sums(microbe) > 4, microbe)
  microbe <- filter_taxa(microbe, filterfun(kOverA(3, 2)), TRUE)   # count > 2 in at least 3 samples
  metab <- log10(1 + metab)
  X <- log10(1 + as.matrix(otu_table(microbe)))
  colnames(metab) <- colnames(X)                                   # the two tables list the samples in the same order
  list(X = X, metab = metab)
}

kashyap_scca <- function() cached("kashyap-scca", {
  K <- load_kashyap()
  set.seed(1)
  res <- PMA::CCA(t(K$X), t(K$metab), penaltyx = 0.15, penaltyz = 0.15, typex = "standard",
                  typez = "standard", trace = FALSE)
  list(u = res$u[, 1], v = res$v[, 1], cor = res$cors[1])
})

fig_9_32 <- function() {
  K <- load_kashyap(); S <- kashyap_scca()
  combined <- cbind(t(K$X[S$u != 0, , drop = FALSE]), t(K$metab[S$v != 0, , drop = FALSE]))
  pca <- dudi.pca(combined, scannf = FALSE, nf = 3)
  smp <- data.frame(pca$li, genotype = substr(rownames(pca$li), 1, 2),
                    diet = substr(rownames(pca$li), 3, 4))
  feat <- data.frame(pca$c1, feature = substr(colnames(combined), 1, 6),
                     type = ifelse(grepl("\\.", colnames(combined)), "Metabolite", "OTU"))
  sc <- 5.5
  pe <- 100 * pca$eig / sum(pca$eig)
  p <- ggplot() +
    geom_point(data = smp, aes(Axis1, Axis2, colour = diet, shape = genotype), size = 3) +
    geom_label_repel(data = feat, aes(sc * CS1, sc * CS2, label = feature, fill = type), size = 2.2,
                     label.padding = unit(0.1, "lines"), label.size = 0, segment.size = 0.25, seed = 1,
                     max.overlaps = 50) +
    geom_point(data = feat, aes(sc * CS1, sc * CS2, fill = type), shape = 23, size = 1.4, colour = "grey25") +
    scale_colour_manual("sample: diet", values = c(PD = "#66c2a5", ST = "#fc8d62")) +
    scale_fill_manual("feature type", values = c(Metabolite = "#a6d854", OTU = "#e78ac3")) +
    scale_shape_manual("sample: genotype", values = c(KO = 17, WT = 16)) +
    coord_fixed() +
    labs(x = sprintf("Axis1 [%.0f%% variance]", pe[1]), y = sprintf("Axis2 [%.0f%% variance]", pe[2]))
  savefig(p, "fig-9-32", ch = 9, width = 8, height = 4.4)
}

# Constrained correspondence analysis of the mouse microbiome (ps1, from the dada2 pipeline of Ch. 4).
ccpna <- function() cached("ccpna", {
  suppressPackageStartupMessages(library(phyloseq))
  ps1 <- readRDS(msmb_data("ps1.rds"))
  ps1p <- filter_taxa(ps1, function(x) sum(x) > 0, TRUE)
  fit <- ordinate(ps1p, "CCA", formula = ps1p ~ ageBin + family_relationship)
  sc <- vegan::scores(fit, display = c("sites", "species"))
  tax <- data.frame(tax_table(ps1p), stringsAsFactors = FALSE)
  main <- c("Clostridiales", "Bacteroidales", "Lactobacillales", "Coriobacteriales")
  order <- ifelse(tax$Order %in% main, tax$Order, "Other")
  sd <- as(sample_data(ps1p), "data.frame")
  sites <- data.frame(sc$sites, sd[rownames(sc$sites), c("ageBin", "family_relationship")])
  species <- data.frame(sc$species, Order = factor(order, levels = c(main, "Other")),
                        otu_id = seq_len(nrow(sc$species)))
  list(sites = sites, species = species, ccaeig = fit$CCA$eig, caeig = fit$CA$eig, tot = fit$tot.chi)
})

ccpna_plot <- function(facet) {
  C <- ccpna()
  pt <- 100 * C$ccaeig[1:2] / C$tot          # percentage of the total inertia
  sp <- C$species
  ggplot() +
    geom_point(data = C$sites, aes(CCA2, CCA1), shape = 2, alpha = 0.5, size = 1.2) +
    geom_point(data = sp, aes(CCA2, CCA1, colour = Order), size = 1) +
    geom_text_repel(data = dplyr::filter(sp, CCA2 < -2), aes(CCA2, CCA1, label = otu_id), size = 2,
                    segment.size = 0.15, seed = 1, max.overlaps = 40) +
    facet_grid(stats::as.formula(paste(". ~", facet))) +
    scale_colour_brewer(palette = "Set1") +
    guides(colour = guide_legend(nrow = 2, override.aes = list(size = 2.2))) +
    labs(x = sprintf("Axis2 [%.1f%% of inertia]", pt[2]), y = sprintf("Axis1 [%.1f%% of inertia]", pt[1])) +
    theme(legend.position = "bottom")
}
fig_9_33 <- function() savefig(ccpna_plot("ageBin"), "fig-9-33", ch = 9, width = 7, height = 5.4)
fig_9_34 <- function() savefig(ccpna_plot("family_relationship"), "fig-9-34", ch = 9, width = 6, height = 5.4)

# ===================================================================================================
# Exercises
# ===================================================================================================
# Exercise 9.1: CA of presence/absence Phylochip data
fig_9_35 <- function() {
  chip <- readRDS(msmb_data("vsn28Exprd.rds"))
  assay <- chip[-nrow(chip), ]; day <- factor(chip[nrow(chip), ])
  pres <- ifelse(assay > 8.633, 1, 0)
  pres <- pres[rowSums(pres) > 0, ]        # taxa never present carry no mass and do not change the CA
  ca <- dudi.coa(as.data.frame(pres), scannf = FALSE, nf = 4)
  pe <- 100 * ca$eig / sum(ca$eig)
  df <- data.frame(x = ca$co[, 1], y = ca$co[, 2], day = day, lab = rownames(ca$co))
  hulls <- df |> group_by(day) |> slice(chull(x, y))
  pal <- c("#1b9e77", "#d95f02", "#7570b3")
  pb <- ggplot(df, aes(x, y, colour = day, fill = day, shape = day)) +
    geom_hline(yintercept = 0, linetype = 2, linewidth = 0.3) + geom_vline(xintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_polygon(data = hulls, alpha = 0.12, show.legend = FALSE) + geom_point(size = 2) +
    scale_colour_manual(values = pal) + scale_fill_manual(values = pal) + scale_shape_manual(values = c(16, 17, 15)) +
    coord_fixed() + labs(x = sprintf("Dim1 (%.1f%%)", pe[1]), y = sprintf("Dim2 (%.1f%%)", pe[2]), title = "(b) samples")
  pa <- pct_scree(ca$eig, ylab = "percentage of chi-square") + ggtitle("(a) eigenvalues")
  savefig(pa + pb + plot_layout(widths = c(1, 1.5)), "fig-9-35", ch = 9, width = 9.5, height = 4)
}

colour_table <- function() {
  cs <- read.csv(msmb_data("colorsentiment.csv"))
  tab <- unclass(xtabs(cs[, 3] ~ cs[, 2] + cs[, 1]))
  round(as.data.frame(tab) / 1000)
}

# Exercise 9.2: colours and emotions (counts of search-engine hits, in thousands)
fig_9_36 <- function() {
  tab <- colour_table()
  fit <- vegan::cca(tab)
  s <- vegan::scores(fit, scaling = 3, display = c("sites", "species"))
  emo <- data.frame(s$sites[, 1:2], lab = rownames(s$sites))
  colr <- data.frame(s$species[, 1:2], lab = rownames(s$species))
  fillc <- ifelse(colr$lab == "white", "grey97", ifelse(colr$lab == "grey", "grey50", colr$lab))
  pe <- 100 * fit$CA$eig / fit$tot.chi
  p <- ggplot() +
    geom_hline(yintercept = 0, linetype = 2, linewidth = 0.3) + geom_vline(xintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_point(data = emo, aes(CA1, CA2), shape = 21, fill = "yellow", colour = "red", size = 2.5) +
    geom_text_repel(data = emo, aes(CA1, CA2, label = lab), colour = "red", size = 3.2, seed = 1) +
    geom_point(data = colr, aes(CA1, CA2), shape = 21, fill = fillc, colour = "black", size = 4) +
    geom_text_repel(data = colr, aes(CA1, CA2, label = lab), size = 3.2, seed = 2) +
    coord_fixed() + labs(x = sprintf("CA1 (%.1f%%)", pe[1]), y = sprintf("CA2 (%.1f%%)", pe[2]))
  savefig(p, "fig-9-36", ch = 9, width = 6, height = 5)
}

# Exercise 9.3: Plato's sentence endings
fig_9_37 <- function() {
  plato <- read.table(msmb_data("platof.txt"), header = TRUE)
  ca <- dudi.coa(plato, scannf = FALSE, nf = 2)
  pe <- 100 * ca$eig / sum(ca$eig)
  rows <- data.frame(x = ca$li[, 2], y = ca$li[, 1], lab = rownames(ca$li))
  cols <- data.frame(x = ca$co[, 2], y = ca$co[, 1], lab = rownames(ca$co))
  pa <- ggplot() +
    geom_hline(yintercept = 0, linetype = 2, linewidth = 0.3) + geom_vline(xintercept = 0, linetype = 2, linewidth = 0.3) +
    geom_point(data = rows, aes(x, y), colour = COL[["blue"]], size = 1.3) +
    geom_text_repel(data = rows, aes(x, y, label = lab), colour = COL[["blue"]], size = 3.1, seed = 1, max.overlaps = 40,
                    segment.size = 0.2) +
    geom_point(data = cols, aes(x, y), colour = COL[["red"]], shape = 17, size = 2.2) +
    geom_text_repel(data = cols, aes(x, y, label = lab), colour = COL[["red"]], size = 3.4, seed = 2, fontface = "bold") +
    coord_fixed() + labs(x = sprintf("Dim2 (%.1f%%)", pe[2]), y = sprintf("Dim1 (%.1f%%)", pe[1]), title = "(a) biplot")
  pb <- pct_scree(ca$eig) + ggtitle("(b) eigenvalues")
  savefig(pa + pb + plot_layout(widths = c(1.5, 1)), "fig-9-37", ch = 9, width = 9.5, height = 7)
}

# Exercise 9.6: kernel MDS (the book's kernels and sigma values)
moignard_kernel <- function() cached("moignard-kernel", {
  m <- load_moignard()
  raw <- as.matrix(read.csv(msmb_data("nbt.3154-S3-raw.csv"), row.names = 1))
  lap <- kernlab::laplacedot(sigma = 1 / 3934)
  gau <- kernlab::rbfdot(sigma = (1 / 3934)^2)
  out <- list()
  for (nm in c("lap_raw", "gau_raw", "lap_norm", "gau_norm")) {
    dat <- if (grepl("raw", nm)) raw else m$blom
    K <- kernlab::kernelMatrix(if (grepl("lap", nm)) lap else gau, dat)
    r <- cmds_big(1 - K@.Data, if (nm == "gau_norm") 4 else 2)
    out[[nm]] <- r
  }
  out
})

fig_9_38 <- function() {
  KM <- moignard_kernel(); m <- load_moignard()
  P <- KM$gau_norm$points[, 1:3]
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    scatter3(P, m$col, c("Axis k1", "Axis k2", "Axis k3"), angle = 15, main = "(a)")
    scatter3(P, m$col, c("Axis k1", "Axis k2", "Axis k3"), angle = -70 %% 360, main = "(b)")
  }
  savefig(draw, "fig-9-38", ch = 9, width = 10, height = 5, dense = TRUE)
}

# Exercises 9.7 and 9.8: diffusion maps and local principal curves
moignard_dmap <- function() cached("moignard-dmap", {
  m <- load_moignard()
  set.seed(8)
  d1 <- diffusionMap::diffuse(dist(m$blom, "manhattan"), neigen = 11)
  d2 <- diffusionMap::diffuse(dist(m$blom), neigen = 11)
  X1 <- d1$X
  lpc05 <- LPCM::lpc(X1[, c(1, 3, 4)], scale = FALSE, h = 0.5)
  lpc07 <- LPCM::lpc(X1[, c(1, 3, 4)], scale = FALSE, h = 0.7)
  list(X1 = X1, X2 = d2$X, eps1 = d1$epsilon, eps2 = d2$epsilon, eig1 = d1$eigenvals,
       eig2 = d2$eigenvals, lpc05 = lpc05$LPC, lpc07 = lpc07$LPC)
})

with_curve <- function(xyz, col, curve, labs, angle, main) {
  s <- scatter3(xyz, col, labs, angle = angle, main = main, cex = 0.4)
  if (!is.null(curve)) s$points3d(curve[, 1], curve[, 2], curve[, 3], type = "l", lwd = 4, col = "black")
  invisible(s)
}

fig_9_39 <- function() {
  D <- moignard_dmap(); m <- load_moignard()
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    with_curve(D$X1[, c(1, 3, 4)], m$col, NULL, c("Axis1", "Axis3", "Axis4"), 50, "(a) diffusion coordinates 1, 3, 4")
    with_curve(D$X1[, c(1, 3, 4)], m$col, D$lpc05, c("Axis1", "Axis3", "Axis4"), 50, "(b) with a local principal curve, h = 0.5")
  }
  savefig(draw, "fig-9-39", ch = 9, width = 10, height = 5, dense = TRUE)
}

fig_9_40 <- function() {
  D <- moignard_dmap(); m <- load_moignard()
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    s <- scatter3(D$lpc07, "black", c("Axis1", "Axis3", "Axis4"), angle = 130, main = "(a) principal curve, h = 0.7", cex = 0.6)
    s$points3d(D$lpc07[, 1], D$lpc07[, 2], D$lpc07[, 3], type = "l", lwd = 3)
    with_curve(D$X1[, c(1, 3, 4)], m$col, D$lpc07, c("Axis1", "Axis3", "Axis4"), 130, "(b) curve and cells")
  }
  savefig(draw, "fig-9-40", ch = 9, width = 10, height = 5, dense = TRUE)
}

fig_9_41 <- function() {
  D <- moignard_dmap(); m <- load_moignard()
  draw <- function() {
    op <- par(mfrow = c(1, 2)); on.exit(par(op))
    scatter3(D$X1[, 1:3], m$col, c("Axis1", "Axis2", "Axis3"), angle = 210, main = expression("(a) " * l[1] * " distances"))
    scatter3(D$X2[, 1:3], m$col, c("Axis1", "Axis2", "Axis3"), angle = 210, main = expression("(b) " * L[2] * " distances"))
  }
  savefig(draw, "fig-9-41", ch = 9, width = 10, height = 5, dense = TRUE)
}

# ===================================================================================================
# Data for the canonical-correlation demonstration
# ===================================================================================================
export_demo_data <- function() {
  K <- load_kashyap(); S <- kashyap_scca()
  pca1 <- dudi.pca(t(K$metab), scale = TRUE, scannf = FALSE)
  pca2 <- dudi.pca(t(K$X), scale = TRUE, scannf = FALSE)
  rv <- RV.rtest(pca1$tab, pca2$tab, 99)$obs
  export_json(list(samples = colnames(K$X), otu = rownames(K$X), metab = rownames(K$metab),
                   X = round(t(K$X), 5), Z = round(t(K$metab), 5),
                   ref = list(nzu = which(S$u != 0), nzv = which(S$v != 0), cor = S$cor, rv = rv,
                              penalty = 0.15)),
              "ch09-multitable.json", digits = 7)
}

run_figures(list(fig_9_27 = fig_9_27, fig_9_28 = fig_9_28, fig_9_29 = fig_9_29, fig_9_30 = fig_9_30,
                 fig_9_31 = fig_9_31, fig_9_32 = fig_9_32, fig_9_33 = fig_9_33, fig_9_34 = fig_9_34,
                 fig_9_35 = fig_9_35, fig_9_36 = fig_9_36, fig_9_37 = fig_9_37, fig_9_38 = fig_9_38,
                 fig_9_39 = fig_9_39, fig_9_40 = fig_9_40, fig_9_41 = fig_9_41,
                 export_demo_data = export_demo_data))
