# Chapter 10. Networks and Trees
# Course: Statistical Methods for Modern Biology (following Holmes and Huber 2019,
# Modern Statistics for Modern Biology, Cambridge University Press, Chapter 10).
#
# Reproduces every figure of the chapter that is computed from data or by simulation:
#   Figure 10.1   a small protein-protein (chemokine) network from STRING (small_chemokine.txt)
#   Figure 10.2   the six-node undirected graph g1 (igraph)
#   Figure 10.3   the adjacency matrix of g1 drawn as tiles
#   Figure 10.5   the bipartite finch-island graph (network package)
#   Figure 10.6   g1 drawn with ggraph
#   Figure 10.7   a four-state Markov chain on A, C, G, T drawn as a weighted directed graph
#   Figure 10.8   the perturbed chemokine subnetwork (string_graph.txt)
#   Figure 10.9   20,000 null draws of the number of blue genes among 75 (hypergeometric), exact pmf on top
#   Figure 10.10  quantile-quantile plot of the beta-uniform mixture fitted to the DLBCL p-values
#   Figure 10.11  histogram of the same p-values with the fitted mixture density and pi0 bound
#   Figure 10.12  the maximal-scoring subnetwork found by the fast Heinz heuristic (BioNet)
#   Figure 10.15  the 6-tip tree tree1 used as the true parameter
#   Figure 10.16  60 sites simulated down tree1, shown next to the tree
#   Figure 10.17  a Steiner tree (g1 with the two inner vertices drawn as squares)
#   Figure 10.18  the neighbour-joining tree estimated from the simulated sequences (JC69 distances)
#   Figure 10.20  two spanning trees of six points: a path and the minimum spanning tree
#   Figure 10.21  minimum spanning tree of 512 HIV sequences from 2009 (DNA distances), coloured by country
#   Figure 10.22  the same with repelling labels (Question 10.16)
#   Figure 10.23  the same tree drawn at jittered country centroids
#   Figure 10.24  the one-dimensional two-sample picture behind the Wald-Wolfowitz runs test
#   Figure 10.25  minimum spanning tree of 344 mouse stool samples (Jaccard), by mouse and litter
#   Figure 10.26  permutation null of the number of pure edges (labels: mouse)
#   Figure 10.27  thresholded co-occurrence network (Jaccard dissimilarity at most 0.35)
#   Figure 10.28  permutation null for litter, permuting litters between mice (with the exact null on top)
#   Figure 10.29  the 1-nearest-neighbour graph with pure and mixed edges (litter, nested permutations)
#   Figure 10.32  heatmap of the CCNB1 co-expression adjacency matrix (Exercise 10.6)
#   Figure 10.33  2-nearest-neighbour graph from Bray-Curtis dissimilarities and its permutation null
#
# Skipped (not computed from data):
#   Figure 10.4   bipartite graph schematic (drawing)
#   Figure 10.13  hierarchical clustering tree versus phylogeny (drawing)
#   Figure 10.14  HIV/SIV phylogeny reproduced from Wertheim and Worobey (2009) (published image)
#   Figure 10.19  screenshot of the interactive structSSI browser display (screenshot); the underlying
#                 hierarchical test is computed below (hier_test) and summarised in the notes
#   Figure 10.30  Markov chain of vaginal community state types (DiGiulio et al. 2015): the book's code
#                 is not live and the data are not distributed with the book
#   Figure 10.31  screenshot of the STRING web site (screenshot)
#
# The 16S rRNA workflow of Section 10.4.4 (DECIPHER alignment of 268 sequence variants, a
# neighbour-joining start and a GTR + Gamma(4) + I maximum likelihood tree with stochastic
# rearrangements) takes several minutes, so its result is cached in cache/ch10-fitGTR.rds; so are the
# Heinz module (Figure 10.12) and the hierarchical test (Section 10.5.1).
#
# It also writes the small JSON files used by the browser demonstrations:
#   data/ch10-mice.json   344 stool samples x 389 taxa (counts), mouse and litter labels, display
#                         layouts for three dissimilarities, and reference test statistics
#   data/ch10-gag.json    20 aligned HIV gag sequences (Exercise 10.8) and reference distances
#   data/ch10-tree.json   tree1, the 6 simulated sequences of Figure 10.16, NJ and likelihood references
#
# Run: Rscript ch10_networks_trees.R             (all figures and data)
#      Rscript ch10_networks_trees.R fig_10_25   (one figure)
# The code is my own; it follows the analysis steps of the book's Chapter 10.

.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(igraph)
  library(ggraph)
  library(ape)
  library(phangorn)
  library(phyloseq)
})

CH <- 10
CACHE <- file.path(CODE_DIR, "cache")
dir.create(CACHE, showWarnings = FALSE)

cached <- function(name, expr) {
  f <- file.path(CACHE, paste0("ch10-", name, ".rds"))
  if (file.exists(f)) return(readRDS(f))
  val <- force(expr)
  saveRDS(val, f)
  val
}

# =============================================================================================
# helpers

# == small graphs ==
g1_graph <- function() {
  edges <- matrix(c(1, 3, 2, 3, 3, 4, 4, 5, 4, 6), byrow = TRUE, ncol = 2)
  g <- graph_from_edgelist(edges, directed = FALSE)
  V(g)$name <- as.character(1:6)
  g
}
laplacian <- function(A) diag(rowSums(A)) - A

# the four-state chain of Figure 10.7 (rows: from, columns: to)
T1 <- matrix(c(0.2, 0.1, 0.4, 0.3,
               0,   1,   0,   0,
               0.1, 0.2, 0.2, 0.5,
               0.1, 0.1, 0.8, 0.0), nrow = 4, byrow = TRUE,
             dimnames = list(c("A", "C", "G", "T"), c("A", "C", "G", "T")))

# == the urn of Section 10.3.2 ==
URN <- list(N = 1000, blue = 100, m = 75, obs = 25)
sim_countblue <- function(B = 20000, seed = 10) {
  set.seed(seed)
  universe <- c(rep("Yellow", 500), rep("Blue", 100), rep("Red", 400))
  replicate(B, sum(sample(universe, URN$m) == "Blue"))
}

# == beta-uniform mixture (Pounds and Morris 2003) ==
dbum <- function(x, lambda, a) lambda + (1 - lambda) * a * x^(a - 1)
pbum <- function(x, lambda, a) lambda * x + (1 - lambda) * x^a
qbum <- function(p, lambda, a) vapply(p, function(pp) uniroot(function(x) pbum(x, lambda, a) - pp,
                                                             c(0, 1), tol = 1e-12)$root, 0)
# my own maximum-likelihood fit (several starts, box constraints)
fit_bum <- function(p) {
  nll <- function(th) -sum(log(dbum(p, th[1], th[2])))
  starts <- expand.grid(lambda = c(0.2, 0.5, 0.8), a = c(0.1, 0.4, 0.7))
  fits <- lapply(seq_len(nrow(starts)), function(i)
    optim(unlist(starts[i, ]), nll, method = "L-BFGS-B", lower = c(1e-6, 1e-6), upper = c(1 - 1e-6, 1 - 1e-6)))
  best <- fits[[which.min(sapply(fits, `[[`, "value"))]]
  list(lambda = unname(best$par[1]), a = unname(best$par[2]), loglik = -best$value)
}
# p-value threshold at which the estimated FDR equals fdr (Gap 10.5), and the node scores
bum_threshold <- function(fdr, lambda, a) {
  pihat <- lambda + (1 - lambda) * a
  ((pihat - fdr * lambda) / (fdr * (1 - lambda)))^(1 / (a - 1))
}
bum_score <- function(p, fdr, lambda, a) (a - 1) * (log(p) - log(bum_threshold(fdr, lambda, a)))

dlbcl <- function() {
  suppressPackageStartupMessages({ library(BioNet); library(DLBCL) })
  e <- new.env()
  data("dataLym", package = "DLBCL", envir = e); data("interactome", package = "DLBCL", envir = e)
  pval <- e$dataLym$t.pval; names(pval) <- e$dataLym$label
  logFC <- e$dataLym$diff; names(logFC) <- e$dataLym$label
  list(pval = pval, logFC = logFC, label = e$dataLym$label, interactome = e$interactome)
}
heinz_module <- function() cached("heinz", {
  dl <- dlbcl()
  subnet <- BioNet::rmSelfLoops(BioNet::subNetwork(dl$label, dl$interactome))
  fb <- BioNet::fitBumModel(dl$pval, plot = FALSE)
  scores <- BioNet::scoreNodes(subnet, fb, fdr = 0.001)
  hot <- BioNet::runFastHeinz(subnet, scores)
  list(fb = fb, scores = scores, hot = hot, subnet_nodes = length(graph::nodes(subnet)),
       subnet_edges = graph::numEdges(subnet))
})

# == substitution models ==
Q_jc <- function(alpha = 1) { Q <- matrix(alpha, 4, 4); diag(Q) <- -3 * alpha; dimnames(Q) <- list(c("A", "C", "G", "T"), c("A", "C", "G", "T")); Q }
# Kimura (1980): alpha for transitions (A<->G, C<->T), beta for transversions; state order A, C, G, T
Q_k80 <- function(alpha, beta) {
  Q <- matrix(beta, 4, 4, dimnames = list(c("A", "C", "G", "T"), c("A", "C", "G", "T")))
  Q["A", "G"] <- Q["G", "A"] <- Q["C", "T"] <- Q["T", "C"] <- alpha
  diag(Q) <- 0; diag(Q) <- -rowSums(Q); Q
}
expm_sym <- function(Q, t) {   # exp(Qt) for a rate matrix that is symmetric
  e <- eigen(Q, symmetric = TRUE)
  e$vectors %*% diag(exp(e$values * t)) %*% t(e$vectors)
}
p_jc <- function(d) 0.75 * (1 - exp(-4 * d / 3))       # expected p-distance at d substitutions/site
d_jc <- function(p) -0.75 * log(1 - 4 * p / 3)          # Jukes-Cantor correction
d_k80 <- function(P, Qv) -0.5 * log(1 - 2 * P - Qv) - 0.25 * log(1 - 2 * Qv)

# == the simulated tree data of Section 10.4.2 ==
load_tree1 <- function() { e <- new.env(); load(msmb_data("tree1.RData"), envir = e); e$tree1 }
get_seqs6 <- function() {
  tree1 <- load_tree1()
  set.seed(2019)
  simSeq(tree1, l = 60, type = "DNA", bf = c(1, 1, 3, 3) / 8, rate = 0.1)
}

# my own Felsenstein pruning for a 4-state reversible model with stationary freqs pi:
# tree in ape format, sequences as a character matrix (tips x sites), lower case a c g t
prune_loglik <- function(tree, seqmat, Pfun, pi = rep(0.25, 4)) {
  st <- c("a", "c", "g", "t")
  n <- length(tree$tip.label); nn <- max(tree$edge)
  seqmat <- seqmat[tree$tip.label, , drop = FALSE]
  lik <- vector("list", nn)                # lik[[v]][s, site] = P(data below v | state s at v)
  for (i in 1:n) lik[[i]] <- t(sapply(st, function(s) as.numeric(seqmat[i, ] == s))) * 1
  for (i in 1:n) dim(lik[[i]]) <- c(4, ncol(seqmat))
  tr <- reorder(tree, "postorder")
  for (k in seq_len(nrow(tr$edge))) {
    par <- tr$edge[k, 1]; ch <- tr$edge[k, 2]
    P <- Pfun(max(tr$edge.length[k], 0))    # P[i, j] = P(j | i); a negative NJ length counts as 0, as in phangorn
    msg <- P %*% lik[[ch]]
    lik[[par]] <- if (is.null(lik[[par]])) msg else lik[[par]] * msg
  }
  sum(log(colSums(pi * lik[[n + 1]])))
}

# my own neighbour joining (Saitou and Nei 1987, in the form of Studier and Keppler 1988)
nj_mine <- function(D) {
  D <- as.matrix(D); labs <- rownames(D); n0 <- nrow(D)
  active <- seq_len(n0); node <- n0 + 1; edges <- NULL; lens <- NULL
  ids <- seq_len(n0)                     # current id of each row
  Dm <- D
  steps <- list()
  while (length(active) > 3) {
    m <- length(active); Ds <- Dm[active, active]
    R <- rowSums(Ds)
    Qm <- (m - 2) * Ds - outer(R, R, "+"); diag(Qm) <- Inf
    ij <- which(Qm == min(Qm), arr.ind = TRUE)[1, ]
    a <- active[ij[1]]; b <- active[ij[2]]
    la <- 0.5 * Dm[a, b] + (R[ij[1]] - R[ij[2]]) / (2 * (m - 2))
    lb <- Dm[a, b] - la
    steps[[length(steps) + 1]] <- list(join = c(ids[a], ids[b]), Qmin = min(Qm), la = la, lb = lb)
    newrow <- 0.5 * (Dm[a, ] + Dm[b, ] - Dm[a, b])
    Dm <- rbind(cbind(Dm, newrow), c(newrow, 0)); k <- nrow(Dm)
    ids <- c(ids, node)
    edges <- rbind(edges, c(node, ids[a]), c(node, ids[b])); lens <- c(lens, la, lb)
    active <- c(setdiff(active, c(a, b)), k); node <- node + 1
  }
  # last three: one centre node
  a <- active[1]; b <- active[2]; c3 <- active[3]
  la <- 0.5 * (Dm[a, b] + Dm[a, c3] - Dm[b, c3]); lb <- Dm[a, b] - la; lc <- Dm[a, c3] - la
  edges <- rbind(edges, c(node, ids[a]), c(node, ids[b]), c(node, ids[c3])); lens <- c(lens, la, lb, lc)
  list(edges = edges, lengths = lens, steps = steps, labels = labs, centre = node)
}
# patristic distance matrix of an nj_mine result
nj_patristic <- function(res) {           # path lengths in a tree (negative NJ lengths allowed)
  E <- res$edges; w <- res$lengths; n <- length(res$labels)
  out <- matrix(0, n, n, dimnames = list(res$labels, res$labels))
  for (i in seq_len(n)) {
    dist <- c(); dist[as.character(i)] <- 0; stack <- i
    while (length(stack)) {
      v <- stack[1]; stack <- stack[-1]
      nb <- which(E[, 1] == v | E[, 2] == v)
      for (k in nb) { u <- if (E[k, 1] == v) E[k, 2] else E[k, 1]
        if (is.na(dist[as.character(u)])) { dist[as.character(u)] <- dist[as.character(v)] + w[k]; stack <- c(stack, u) } }
    }
    out[i, ] <- dist[as.character(seq_len(n))]
  }
  out
}

# == mouse microbiome (ps1) ==
load_ps1 <- function() readRDS(msmb_data("ps1.rds"))
ps1_counts <- function(ps) { X <- as(otu_table(ps), "matrix"); if (taxa_are_rows(ps)) X <- t(X); X }
pure_edges <- function(el, lab) sum(lab[el[, 1]] == lab[el[, 2]])
mst_edges <- function(d) {
  g <- graph_from_adjacency_matrix(as.matrix(d), mode = "undirected", weighted = TRUE)
  as_edgelist(igraph::mst(g, algorithm = "prim"))
}
# k-nearest-neighbour graph as in phyloseqGraphTest: edge i--j if j is among the k nearest of i or vice versa
knn_edges <- function(d, k) {
  d <- as.matrix(d)
  nb <- t(apply(d, 1, function(x) { r <- rank(x); ((r > 1) & (r < k + 2)) + 0 }))
  A <- (nb + t(nb)) > 0
  w <- which(A & upper.tri(A), arr.ind = TRUE)
  cbind(rownames(d)[w[, 1]], rownames(d)[w[, 2]])
}
# permutation test of the number of pure edges; grouping permutes labels between blocks
perm_pure <- function(el, lab, block = NULL, nperm = 1000, seed = 1) {
  set.seed(seed)
  obs <- pure_edges(el, lab)
  if (is.null(block)) {
    perm <- replicate(nperm, { l <- sample(lab); names(l) <- names(lab); pure_edges(el, l) })
  } else {
    bl <- tapply(lab, block, `[`, 1)
    perm <- replicate(nperm, { b <- setNames(sample(bl), names(bl)); l <- b[as.character(block)]; names(l) <- names(lab); pure_edges(el, l) })
  }
  list(observed = obs, perm = perm, pval = (sum(perm >= obs) + 1) / (nperm + 1))
}
# exact null when litter labels are permuted between mice: all choose(12, 4) = 495 assignments
exact_nested <- function(el, lab, block) {
  bl <- tapply(lab, block, `[`, 1); mice <- names(bl); lev <- sort(unique(bl))
  n1 <- sum(bl == lev[1])
  cmb <- combn(length(mice), n1)
  S <- apply(cmb, 2, function(ix) {
    b <- setNames(ifelse(seq_along(mice) %in% ix, lev[1], lev[2]), mice)
    l <- b[as.character(block)]; names(l) <- names(lab); pure_edges(el, l)
  })
  obs <- pure_edges(el, lab)
  list(S = S, observed = obs, pval = mean(S >= obs), n = length(S))
}
# moments of the number of pure edges under free relabelling (Gap 10.21)
pure_moments <- function(el, lab) {
  N <- length(lab); nk <- as.numeric(table(lab)); E <- nrow(el)
  f2 <- sum(nk * (nk - 1)) / (N * (N - 1))
  f3 <- sum(nk * (nk - 1) * (nk - 2)) / (N * (N - 1) * (N - 2))
  f4 <- (sum(nk * (nk - 1))^2 - sum(nk * (nk - 1) * (4 * nk - 6))) / (N * (N - 1) * (N - 2) * (N - 3))
  deg <- table(c(el[, 1], el[, 2]))
  C <- sum(deg * (deg - 1) / 2)                 # pairs of edges that share a vertex
  npairs <- E * (E - 1) / 2
  m <- E * f2
  v <- E * f2 + 2 * C * f3 + 2 * (npairs - C) * f4 - m^2
  list(mean = m, var = v, sd = sqrt(v), C = C, E = E)
}

mice_setup <- function() {
  ps <- load_ps1()
  sd <- data.frame(sample_data(ps))
  dj <- as.matrix(phyloseq::distance(ps, method = "jaccard"))
  lab_m <- setNames(as.character(sd$host_subject_id), rownames(sd))
  lab_l <- setNames(as.character(sd$family_relationship), rownames(sd))
  list(ps = ps, sd = sd, dj = dj, lab_m = lab_m, lab_l = lab_l)
}

# == hierarchical testing (Section 10.5.1) ==
hier_test <- function() cached("hfdr", {
  suppressPackageStartupMessages({ library(DESeq2); library(structSSI) })
  ps <- load_ps1()
  dds <- phyloseq_to_deseq2(ps, design = ~ ageBin + family_relationship)
  gm <- function(x) if (all(x == 0)) 0 else exp(mean(log(x[x != 0])))
  dds <- estimateSizeFactors(dds, geoMeans = apply(counts(dds), 1, gm))
  dds <- estimateDispersions(dds, quiet = TRUE)
  abund <- getVarianceStabilizedData(dds)
  full <- rownames(abund)
  rownames(abund) <- make.names(substr(rownames(abund), 1, 5), unique = TRUE)
  tr <- phy_tree(ps)
  el0 <- tr$edge[rev(seq_len(nrow(tr$edge))), ]
  el_names <- c(rownames(abund), seq_len(tr$Nnode))
  el <- cbind(el_names[el0[, 1]], el_names[el0[, 2]])
  unadj <- treePValues(el, abund, sample_data(ps)$ageBin)
  res <- hFDR.adjust(unadj, el, 0.75)
  pv <- res@p.vals
  tax <- data.frame(tax_table(ps)[full, c("Family", "Genus")])
  tax$hypothesisName <- rownames(abund)
  mine <- hfdr_mine(unadj, el, 0.75)
  pm <- data.frame(hypothesisName = names(unadj), unadjp = unname(unadj), adjp = unname(mine[names(unadj)]))
  tips <- merge(tax, pm, by = "hypothesisName")
  tips <- tips[order(tips$adjp), ]
  list(pvals = pv, mine = pm, tips = tips, unadj = unadj, el = el, est = structSSI:::EstimatedHFDRControl(res),
       n_tested_pkg = sum(!is.na(pv$adjp)), sizefactors = sizeFactors(dds))
})
# my own version of the hierarchical procedure (Algorithm 10.2 of the notes): test the root at alpha; for every
# rejected vertex, Benjamini-Hochberg at alpha on the family of its children. Returns adjusted p-values
# (NA = never tested). structSSI 1.2.1's hFDR.adjust stops after the root's children (its recursion looks
# up a column 'original_name' that does not exist), so the full recursion is coded here.
hfdr_mine <- function(unadj, el, alpha) {
  kids <- split(el[, 2], el[, 1]); root <- setdiff(el[, 1], el[, 2])
  adj <- setNames(rep(NA_real_, length(unadj)), names(unadj))
  adj[root] <- unadj[root]
  if (unadj[root] > alpha) return(adj)
  queue <- root
  while (length(queue)) {
    v <- queue[1]; queue <- queue[-1]
    ch <- kids[[v]]; if (is.null(ch)) next
    a <- p.adjust(unadj[ch], "BH"); adj[ch] <- a
    queue <- c(queue, ch[a < alpha])
  }
  adj
}

# == the 16S tree of Section 10.4.4 (slow: cached) ==
fit_16s <- function() cached("fitGTR", {
  suppressPackageStartupMessages({ library(dada2); library(DECIPHER) })
  seqtab <- readRDS(msmb_data("seqtab.rds")); seqs <- getSequences(seqtab); names(seqs) <- seqs
  set.seed(10)
  aln <- AlignSeqs(DNAStringSet(seqs), anchor = NA, verbose = FALSE, processors = 1)
  pa <- phyDat(as(aln, "matrix"), type = "DNA")
  fit <- pml(NJ(dist.ml(pa)), data = pa)
  fitGTR <- update(fit, k = 4, inv = 0.2)
  set.seed(10)
  fitGTR <- optim.pml(fitGTR, model = "GTR", optInv = TRUE, optGamma = TRUE,
                      rearrangement = "stochastic", control = pml.control(trace = 0))
  list(alignment = aln, nj_logLik = fit$logLik, fitGTR = fitGTR)
})

# == HIV 2009 ==
load_hiv <- function() { e <- new.env(); load(msmb_data("dist2009c.RData"), envir = e); e$dist2009c }
hiv_graph <- function() {
  d <- load_hiv()
  m <- ape::mst(d)
  g <- graph_from_adjacency_matrix(unclass(m)[seq_len(nrow(m)), seq_len(ncol(m))], mode = "undirected")
  V(g)$country <- attr(d, "Labels")
  g
}
country_centroids <- function(countries) {
  e <- new.env()
  lazyLoad(file.path(find.package("rworldmap"), "data", "Rdata"), envir = e)   # the package itself needs terra
  cl <- e$countriesLow@data
  m <- match(countries, cl$NAME)
  data.frame(country = countries, lat = cl$LAT[m], lon = cl$LON[m])
}

# =============================================================================================
# figures

theme_net <- function() theme_void(base_size = 11) + theme(legend.key = element_blank())

fig_10_1 <- function() {
  dats <- read.table(msmb_data("small_chemokine.txt"), header = TRUE)
  gr <- graph_from_data_frame(dats[, c("node1", "node2")], directed = FALSE)
  set.seed(4)
  p <- ggraph(gr, layout = "fr") +
    geom_edge_link(colour = "grey35", alpha = 0.6, linewidth = 0.9) +
    geom_node_point(size = 3.2, colour = COL[["orange"]], alpha = 0.8) +
    geom_node_text(aes(label = name), colour = COL[["purple"]], size = 4.2, repel = TRUE, seed = 4) +
    theme_net()
  savefig(p, "fig-10-1", ch = CH, width = 5.2, height = 4)
}

fig_10_2 <- function() {
  g1 <- g1_graph()
  savefig(function() {
    set.seed(3); par(mar = rep(0.2, 4))
    plot(g1, vertex.size = 25, edge.width = 5, vertex.color = "coral", vertex.label.cex = 1.4,
         vertex.label.color = "black", edge.color = "grey40", layout = layout_with_fr(g1))
  }, "fig-10-2", ch = CH, width = 4.2, height = 4.2)
}

adjacency_tiles <- function(A) {
  n <- nrow(A); p <- ncol(A)
  df <- data.frame(i = rep(seq_len(n), p), j = rep(seq_len(p), each = n), value = factor(as.vector(A)))
  ggplot(df, aes(x = j, y = i, fill = value)) +
    geom_tile(colour = "black") +
    scale_fill_manual(values = c(`0` = "white", `1` = "#1f3f8f"), name = "entry") +
    scale_x_continuous(breaks = seq_len(p), position = "top", expand = c(0, 0)) +
    scale_y_reverse(breaks = seq_len(n), expand = c(0, 0)) +
    coord_fixed() + labs(x = NULL, y = NULL) +
    theme_course(13) + theme(panel.grid = element_blank(), axis.ticks = element_blank(), panel.border = element_blank())
}
fig_10_3 <- function() {
  A <- as_adjacency_matrix(g1_graph(), sparse = FALSE)
  savefig(adjacency_tiles(A), "fig-10-3", ch = CH, width = 4.4, height = 3.8)
}

fig_10_5 <- function() {
  suppressPackageStartupMessages(library(network))
  finch <- readr::read_csv(msmb_data("finch.csv"), comment = "#", col_types = "cc")
  nw <- network::as.network(as.data.frame(finch), bipartite = TRUE, directed = FALSE)
  nm <- network::network.vertex.names(nw)
  is_island <- nchar(nm) == 1
  savefig(function() {
    set.seed(7); par(mar = rep(0.3, 4))
    plot(nw, vertex.cex = 2.2, displaylabels = TRUE, label = sub(" finch", "", nm), label.cex = 0.75,
         vertex.col = ifelse(is_island, "forestgreen", "gold3"), edge.col = "grey55")
  }, "fig-10-5", ch = CH, width = 8, height = 5.6)
}

fig_10_6 <- function() {
  set.seed(2)
  p <- ggraph(g1_graph(), layout = "nicely") +
    geom_edge_link(linewidth = 0.9) +
    geom_node_point(size = 8, colour = "#8856a7") +
    geom_node_text(aes(label = name), colour = "white", size = 4.5) +
    theme_net()
  savefig(p, "fig-10-6", ch = CH, width = 3.4, height = 3.2)
}

fig_10_7 <- function() {
  w <- which(T1 > 0, arr.ind = TRUE)
  g <- graph_from_edgelist(cbind(rownames(T1)[w[, 1]], colnames(T1)[w[, 2]]), directed = TRUE)
  E(g)$p <- T1[w]
  el <- as_edgelist(g)
  loop <- el[, 1] == el[, 2]
  # loop directions (radians): each self-loop points away from the centre of the square
  ang <- c(A = 3 * pi / 4, C = pi / 4, G = -3 * pi / 4, T = -pi / 4)[el[, 1]]
  savefig(function() {
    par(mar = rep(0.2, 4))
    lay <- matrix(c(-1, 1, 1, 1, -1, -1, 1, -1), ncol = 2, byrow = TRUE)[match(V(g)$name, c("A", "C", "G", "T")), ]
    plot(g, layout = lay, rescale = FALSE, xlim = c(-1.45, 1.45), ylim = c(-1.45, 1.45),
         vertex.size = 34, vertex.color = "#6a3d9a", vertex.label.color = "white",
         vertex.label.cex = 2, vertex.label.family = "sans", edge.color = "#1f5fbf", edge.width = 1 + 6 * E(g)$p,
         edge.arrow.size = 0.6, edge.curved = ifelse(loop, 0, 0.25), edge.label = format(E(g)$p), edge.label.cex = 1.1,
         edge.label.color = "#b5470b", edge.label.family = "sans", edge.loop.angle = ifelse(loop, ang, 0))
  }, "fig-10-7", ch = CH, width = 5, height = 5)
}

fig_10_8 <- function() {
  datf <- read.table(msmb_data("string_graph.txt"), header = TRUE)
  grs <- graph_from_data_frame(datf[, c("node1", "node2")], directed = FALSE)
  set.seed(8)
  p <- ggraph(grs, layout = "fr") +
    geom_edge_arc(colour = "black", strength = 0.05, alpha = 0.6) +
    geom_node_point(size = 2.5, alpha = 0.6, colour = COL[["orange"]]) +
    geom_node_label(aes(label = name), size = 2.8, alpha = 0.9, colour = "#8856a7", repel = TRUE, seed = 8,
                    label.padding = unit(0.12, "lines")) +
    theme_net()
  savefig(p, "fig-10-8", ch = CH, width = 6.4, height = 4.8)
}

fig_10_9 <- function() {
  cb <- sim_countblue()
  k <- 0:30
  ex <- data.frame(k = k, n = length(cb) * dhyper(k, URN$blue, URN$N - URN$blue, URN$m))
  p <- ggplot(data.frame(cb = cb), aes(x = cb)) +
    geom_histogram(binwidth = 1, colour = "white", fill = COL[["purple"]], boundary = -0.5) +
    geom_point(data = ex, aes(x = k, y = n), colour = COL[["orange"]], size = 1.6) +
    geom_vline(xintercept = URN$obs, colour = COL[["red"]], linetype = 2) +
    annotate("text", x = URN$obs - 0.4, y = max(ex$n) * 0.9, hjust = 1, size = 3.4,
             label = "observed: 25 blue", colour = COL[["red"]]) +
    labs(x = "number of blue genes among 75 drawn", y = "count (20,000 draws)")
  savefig(p, "fig-10-9", ch = CH, width = 6, height = 3.8)
}

fig_10_10 <- function() {
  hz <- heinz_module(); fb <- hz$fb
  n <- length(fb$pvalues)
  probs <- (seq_len(n) - 0.5) / n
  df <- data.frame(fitted = qbum(probs, fb$lambda, fb$a), observed = sort(fb$pvalues))
  p <- ggplot(df, aes(fitted, observed)) +
    geom_abline(intercept = 0, slope = 1, colour = COL[["blue"]]) +
    geom_point(size = 0.5, alpha = 0.35, colour = COL[["red"]]) +
    coord_fixed() + labs(x = "theoretical quantiles (fitted BUM)", y = "observed p-value quantiles")
  savefig(p, "fig-10-10", ch = CH, width = 4.4, height = 4.4)
}

fig_10_11 <- function() {
  hz <- heinz_module(); fb <- hz$fb
  xs <- seq(0.006, 1, length.out = 400)
  pi_up <- fb$lambda + (1 - fb$lambda) * fb$a
  p <- ggplot(data.frame(p = fb$pvalues), aes(x = p)) +
    geom_histogram(aes(y = after_stat(density)), binwidth = 0.02, boundary = 0, fill = COL[["orange"]], alpha = 0.75) +
    geom_hline(yintercept = pi_up, colour = COL[["blue"]]) +
    geom_line(data = data.frame(x = xs, y = dbum(xs, fb$lambda, fb$a)), aes(x, y), colour = COL[["red"]], linewidth = 1.1) +
    annotate("text", x = 0.93, y = pi_up + 0.45, label = "hat(pi)[0]", parse = TRUE, size = 5, colour = COL[["blue"]]) +
    coord_cartesian(ylim = c(0, 8)) + labs(x = "p-value", y = "density")
  savefig(p, "fig-10-11", ch = CH, width = 4.6, height = 4.2)
}

fig_10_12 <- function() {
  hz <- heinz_module(); dl <- dlbcl()
  savefig(function() {
    set.seed(12)
    BioNet::plotModule(hz$hot, layout = igraph::layout_with_dh, scores = hz$scores, diff.expr = dl$logFC,
                       vertex.size = 6, vertex.label.cex = 0.55)
  }, "fig-10-12", ch = CH, width = 8, height = 8)
}

fig_10_15 <- function() {
  suppressPackageStartupMessages(library(ggtree))
  p <- ggtree(load_tree1(), size = 1.4, color = "darkgreen", alpha = 0.8) +
    geom_tiplab(size = 5, offset = 0.05) +
    geom_point(aes(shape = isTip, colour = isTip), size = 4, alpha = 0.7) +
    scale_colour_manual(values = c(`FALSE` = COL[["red"]], `TRUE` = COL[["green"]])) +
    scale_shape_manual(values = c(`FALSE` = 16, `TRUE` = 17)) +
    xlim(0, 3.3) + theme(legend.position = "bottom")
  savefig(p, "fig-10-15", ch = CH, width = 5, height = 4.6)
}

fig_10_16 <- function() {
  suppressPackageStartupMessages(library(ggtree))
  s6 <- as.character(get_seqs6())
  p <- ggtree(load_tree1(), size = 1) + geom_tiplab(aes(x = branch), size = 4, vjust = -0.6)
  tips <- p$data[p$data$isTip, c("label", "y")]
  x0 <- max(p$data$x) + 0.08; w <- 0.045       # gheatmap() fails with the current ggplot2, so draw the tiles directly
  tl <- data.frame(label = rep(rownames(s6), ncol(s6)), site = rep(seq_len(ncol(s6)), each = nrow(s6)),
                   base = as.vector(s6))
  tl$y <- tips$y[match(tl$label, tips$label)]; tl$x <- x0 + (tl$site - 0.5) * w
  p <- p + geom_tile(data = tl, aes(x = x, y = y, fill = base), width = w, height = 0.9, inherit.aes = FALSE) +
    scale_fill_manual(values = c(a = "#1f5fbf", c = "#d9822b", g = "#2e8b57", t = "#c0392b"), name = "base") +
    theme(legend.position = "bottom")
  savefig(p, "fig-10-16", ch = CH, width = 7.6, height = 3.6)
}

fig_10_17 <- function() {
  g1 <- g1_graph()
  savefig(function() {
    set.seed(3); par(mar = rep(0.2, 4))
    plot(g1, vertex.size = 22, edge.width = 5, vertex.color = "coral", vertex.label.cex = 1.4,
         vertex.label.color = "black", edge.color = "grey40", layout = layout_with_fr(g1),
         vertex.shape = c("circle", "square")[c(1, 1, 2, 2, 1, 1)])
  }, "fig-10-17", ch = CH, width = 4.2, height = 4.2)
}

fig_10_18 <- function() {
  suppressPackageStartupMessages(library(ggtree))
  tnj <- nj(dist.ml(get_seqs6(), "JC69"))
  p <- ggtree(tnj, size = 1) + geom_tiplab(size = 5) + xlim(0, max(node.depth.edgelength(tnj)) * 1.25)
  savefig(p, "fig-10-18", ch = CH, width = 4.6, height = 3.8)
}

MST_PTS <- matrix(c(0, 0, 1, 1, 1.5, 2, 0, 1, 1, 0, 0.5, 0.5), ncol = 2)
fig_10_20 <- function() {
  g <- graph_from_adjacency_matrix(as.matrix(dist(MST_PTS)), mode = "undirected", weighted = TRUE)
  m1 <- igraph::mst(g)
  path <- graph_from_edgelist(cbind(1:5, 2:6), directed = FALSE)
  one <- function(gr, col, ttl) ggraph(gr, layout = MST_PTS) +
    geom_edge_link(colour = col, alpha = 0.85, edge_width = 2) +
    geom_node_point(colour = "black", size = 3.5) + coord_fixed() + ggtitle(ttl) +
    theme_void(base_size = 11) + theme(plot.title = element_text(hjust = 0.5))
  p <- patchwork::wrap_plots(
    one(path, COL[["red"]], sprintf("(a) a spanning path, length %.2f", sum(sqrt(rowSums((MST_PTS[1:5, ] - MST_PTS[2:6, ])^2))))),
    one(m1, "steelblue", sprintf("(b) the minimum spanning tree, length %.2f", sum(E(m1)$weight))), ncol = 1)
  savefig(p, "fig-10-20", ch = CH, width = 5, height = 5)
}

hiv_plot <- function(label_fun) {
  g <- hiv_graph()
  set.seed(21)
  ggraph(g, layout = "fr") +
    geom_edge_link(colour = "black", alpha = 0.45) +
    geom_node_point(aes(colour = country), size = 1.8) +
    label_fun +
    theme_net() + guides(colour = guide_legend(title = "Country", keyheight = 0.5, keywidth = 0.5, ncol = 1,
                                                override.aes = list(size = 2.2))) +
    theme(legend.text = element_text(size = 7))
}
fig_10_21 <- function() {
  p <- hiv_plot(geom_node_text(aes(label = country), colour = "black", size = 1.7))
  savefig(p, "fig-10-21", ch = CH, width = 7.5, height = 6)
}
fig_10_22 <- function() {
  p <- hiv_plot(geom_node_label(aes(label = country), colour = "black", size = 1.6, repel = TRUE, seed = 22,
                                label.padding = unit(0.08, "lines"), max.overlaps = 30))
  savefig(p, "fig-10-22", ch = CH, width = 7.5, height = 6.4)
}
fig_10_23 <- function() {
  g <- hiv_graph()
  cc <- country_centroids(V(g)$country)
  set.seed(23)
  lay <- cbind(x = jitter(cc$lon, amount = 15), y = jitter(cc$lat, amount = 8))
  tab <- table(V(g)$country); labc <- names(tab)[tab > 1]
  dfc <- country_centroids(labc)
  p <- ggraph(g, layout = lay) +
    geom_edge_arc(colour = "black", alpha = 0.35, strength = 0.15) +
    geom_node_point(aes(colour = country), size = 2.2, alpha = 0.75) +
    geom_label(data = dfc, aes(x = lon, y = lat, label = country, fill = country), colour = "white", alpha = 0.85,
               size = 2.6, show.legend = FALSE) +
    theme_net() + theme(legend.position = "none")
  savefig(p, "fig-10-23", ch = CH, width = 8, height = 5.4)
}

fig_10_24 <- function() {
  set.seed(24)
  df <- data.frame(measure = c(rnorm(15, 0.9), rnorm(15, 1.8)), group = rep(c("men", "women"), each = 15))
  o <- df$group[order(df$measure)]
  runs <- 1 + sum(o[-1] != o[-length(o)])
  p <- ggplot(df, aes(x = measure, y = 0, colour = group, shape = group)) +
    geom_hline(yintercept = 0, colour = "grey50") + geom_point(size = 4, alpha = 0.65) +
    scale_colour_manual(values = c(men = "blue", women = "red")) + ylim(-0.25, 0.25) +
    labs(x = sprintf("measurement (%d runs among 30 ordered values)", runs), y = NULL) +
    theme_course() + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank(), panel.grid = element_blank(),
                           panel.border = element_blank(), legend.position = "right")
  savefig(p, "fig-10-24", ch = CH, width = 6.4, height = 1.6)
}

mice_mst_graph <- function(ms) {
  g <- graph_from_adjacency_matrix(ms$dj, mode = "undirected", weighted = TRUE)
  net <- igraph::mst(g, algorithm = "prim")
  V(net)$id <- ms$sd[V(net)$name, "host_subject_id"]
  V(net)$litter <- ms$sd[V(net)$name, "family_relationship"]
  net
}
mouse_colours <- function() setNames(c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b",
                                       "#e377c2", "#7f7f7f", "#bcbd22", "#17becf", "#393b79", "#637939"),
                                     c("F003", "F004", "F005", "F006", "F007", "F008", "M001", "M002", "M003", "M004", "M005", "M006"))
fig_10_25 <- function() {
  ms <- mice_setup(); net <- mice_mst_graph(ms)
  set.seed(25)
  p <- ggraph(net, layout = "fr") +
    geom_edge_arc(colour = "darkgray", strength = 0.1) +
    geom_node_point(aes(colour = id, shape = litter), size = 1.8) +
    scale_colour_manual(values = mouse_colours(), name = "mouse") +
    theme_net() + theme(legend.position = "bottom", legend.box = "vertical") +
    guides(colour = guide_legend(nrow = 2, override.aes = list(size = 2.5)))
  savefig(p, "fig-10-25", ch = CH, width = 5.4, height = 6)
}

perm_hist <- function(perm, obs, exact = NULL, xlab = "number of pure edges") {
  p <- ggplot(data.frame(perm = perm), aes(perm)) +
    geom_histogram(binwidth = 1, fill = "grey55", colour = "white", boundary = -0.5) +
    geom_vline(xintercept = obs, colour = COL[["red"]]) +
    annotate("point", x = obs, y = 0, colour = COL[["red"]], size = 2.5) +
    labs(x = xlab, y = "count")
  if (!is.null(exact)) p <- p + geom_point(data = exact, aes(x = x, y = y), colour = COL[["blue"]], size = 1.1)
  p
}
mice_tests <- function() cached("mice-tests", {
  suppressPackageStartupMessages(library(phyloseqGraphTest))
  ps <- load_ps1()
  set.seed(26)
  g_m <- graph_perm_test(ps, "host_subject_id", distance = "jaccard", type = "mst", nperm = 1000)
  set.seed(28)
  g_l <- graph_perm_test(ps, "family_relationship", grouping = "host_subject_id", distance = "jaccard", type = "mst", nperm = 1000)
  set.seed(29)
  g_k1 <- graph_perm_test(ps, "family_relationship", grouping = "host_subject_id", distance = "jaccard", type = "knn", knn = 1)
  set.seed(33)
  g_k2 <- graph_perm_test(ps, "family_relationship", grouping = "host_subject_id", distance = "bray", type = "knn", knn = 2)
  strip <- function(g) list(observed = g$observed, perm = g$perm, pval = g$pval, el = as_edgelist(g$net))
  list(mouse = strip(g_m), litter = strip(g_l), knn1 = strip(g_k1), knn2 = strip(g_k2))
})
fig_10_26 <- function() {
  t <- mice_tests()$mouse
  savefig(perm_hist(t$perm, t$observed), "fig-10-26", ch = CH, width = 5, height = 2.6)
}
fig_10_27 <- function() {
  ps <- load_ps1()
  net <- make_network(ps, max.dist = 0.35)
  sd <- data.frame(sample_data(ps))
  V(net)$id <- sd[V(net)$name, "host_subject_id"]; V(net)$litter <- sd[V(net)$name, "family_relationship"]
  set.seed(27)
  p <- ggraph(net, layout = "fr") +
    geom_edge_link(colour = "darkgray", alpha = 0.7) +
    geom_node_point(aes(colour = id, shape = litter), size = 2) +
    scale_colour_manual(values = mouse_colours(), name = "mouse") +
    theme_net() + guides(colour = guide_legend(ncol = 2, override.aes = list(size = 2.5)))
  savefig(p, "fig-10-27", ch = CH, width = 6.4, height = 4.4)
}
fig_10_28 <- function() {
  ms <- mice_setup(); t <- mice_tests()$litter
  ex <- exact_nested(t$el, ms$lab_l, ms$sd[names(ms$lab_l), "host_subject_id"])
  tab <- table(ex$S)
  exdf <- data.frame(x = as.numeric(names(tab)), y = as.numeric(tab) / ex$n * length(t$perm))
  savefig(perm_hist(t$perm, t$observed, exdf), "fig-10-28", ch = CH, width = 5, height = 2.8)
}
test_network_plot <- function(el, ms, labname = "litter") {
  g <- graph_from_edgelist(el, directed = FALSE)
  lab <- ms$lab_l[V(g)$name]
  E(g)$type <- ifelse(lab[el[, 1]] == lab[el[, 2]], "pure", "mixed")
  V(g)$litter <- lab
  set.seed(29)
  ggraph(g, layout = "fr") +
    geom_edge_link(aes(colour = type), alpha = 0.8) +
    geom_node_point(aes(shape = litter, fill = litter), size = 1.8, colour = "black") +
    scale_edge_colour_manual(values = c(pure = "grey30", mixed = COL[["orange"]]), name = "edge") +
    scale_shape_manual(values = c(Litter1 = 21, Litter2 = 24)) +
    scale_fill_manual(values = c(Litter1 = COL[["teal"]], Litter2 = COL[["purple"]])) +
    theme_net()
}
fig_10_29 <- function() {
  ms <- mice_setup(); t <- mice_tests()$knn1
  savefig(test_network_plot(t$el, ms), "fig-10-29", ch = CH, width = 5.6, height = 4.2)
}

ccnb1 <- function() {
  dat <- read.table(msmb_data("ccnb1datsmall.txt"), header = TRUE, comment.char = "", stringsAsFactors = TRUE)
  v <- levels(unlist(dat[, 1:2])); n <- length(v)
  e <- matrix(match(as.character(unlist(dat[, 1:2])), v), ncol = 2)
  M <- matrix(0, n, n); M[e] <- dat$coexpression; M <- M + t(M); dimnames(M) <- list(v, v)
  list(M = M, A = 1 * (M > 0), v = v, e = e, w = dat$coexpression)
}
fig_10_32 <- function() {
  cc <- ccnb1(); M <- cc$M; v <- cc$v; n <- length(v)
  breaks <- c(0, seq(0.9, 1, length.out = 11))
  cols <- grey(1 - c(0, seq(0.5, 1, length.out = 10)))
  i0 <- which(v == "CCNB1")
  vcols <- rep("white", n); vcols[M[, i0] > 0] <- "red"; vcols[i0] <- "blue"
  savefig(function() {
    stats::heatmap(M, symm = TRUE, ColSideColors = vcols, RowSideColors = vcols, col = cols, breaks = breaks,
                   margins = c(5, 5), cexRow = 0.55, cexCol = 0.55)
    legend("topleft", c("neighbours of CCNB1", "CCNB1"), fill = c("red", "blue"), bty = "n", inset = 0,
           xpd = TRUE, border = FALSE, cex = 0.8)
  }, "fig-10-32", ch = CH, width = 6.4, height = 6.4)
}

fig_10_33 <- function() {
  ms <- mice_setup(); t <- mice_tests()$knn2
  pa <- test_network_plot(t$el, ms) + ggtitle("(a) 2-NN graph, Bray-Curtis")
  pb <- perm_hist(t$perm, t$observed) + ggtitle("(b) permutation null")
  savefig(patchwork::wrap_plots(pa, pb, ncol = 2, widths = c(1.25, 1)), "fig-10-33", ch = CH, width = 9, height = 3.8)
}

# =============================================================================================
# non-figure computations (cached) and the data for the browser demonstrations

tab_16s <- function() {
  f <- fit_16s()
  ps <- load_ps1()
  message(sprintf("16S tree: %d tips, GTR logLik %.2f (NJ start %.2f), shape %.3f, inv %.3f",
                  length(f$fitGTR$tree$tip.label), f$fitGTR$logLik, f$nj_logLik, f$fitGTR$shape, f$fitGTR$inv))
  h <- hier_test()
  message("hierarchical test, top tips:"); print(utils::head(h$tips[, c("Family", "Genus", "hypothesisName", "unadjp", "adjp")], 9))
  invisible(NULL)
}

data_ch10 <- function() {
  ms <- mice_setup()
  X <- ps1_counts(ms$ps)[rownames(ms$dj), ]
  dbray <- as.matrix(vegan::vegdist(X, "bray"))
  djb <- as.matrix(vegan::vegdist(X, "jaccard", binary = TRUE))
  lay <- function(d) {          # display positions: force-directed layout of the MST (unweighted)
    g <- igraph::mst(graph_from_adjacency_matrix(d, mode = "undirected", weighted = TRUE))
    set.seed(25); round(unname(layout_with_fr(g, weights = NA)), 3)
  }
  enc <- apply(X, 1, function(r) paste(ifelse(r == 0, "", r), collapse = ","))
  tests <- mice_tests()
  el_mst <- mst_edges(ms$dj)
  ex_l <- exact_nested(el_mst, ms$lab_l, ms$sd[names(ms$lab_l), "host_subject_id"])
  el_k1 <- knn_edges(ms$dj, 1); el_k2 <- knn_edges(dbray, 2)
  blk <- ms$sd[names(ms$lab_l), "host_subject_id"]
  ex_k1 <- exact_nested(el_k1, ms$lab_l, blk); ex_k2 <- exact_nested(el_k2, ms$lab_l, blk)
  mm <- pure_moments(el_mst, ms$lab_m)
  export_json(list(
    note = "ps1 of Holmes and Huber (2019), Section 10.6: 344 stool samples x 389 taxa; counts are comma-separated, empty = 0",
    samples = rownames(X), mouse = unname(ms$lab_m[rownames(X)]), litter = unname(ms$lab_l[rownames(X)]),
    age = ms$sd[rownames(X), "age"], counts = unname(enc), ntaxa = ncol(X),
    layout = list(jaccard = lay(ms$dj), bray = lay(dbray), jaccard_binary = lay(djb)),
    ref = list(mst_weight_jaccard = sum(ms$dj[el_mst]), mst_pure_mouse = pure_edges(el_mst, ms$lab_m),
               mst_pure_litter = pure_edges(el_mst, ms$lab_l), mst_mouse_null_mean = mm$mean, mst_mouse_null_sd = mm$sd,
               mst_litter_exact_p = ex_l$pval, knn1_edges = nrow(el_k1), knn1_pure_litter = pure_edges(el_k1, ms$lab_l),
               knn1_litter_exact_p = ex_k1$pval, knn2bray_edges = nrow(el_k2), knn2bray_pure_litter = pure_edges(el_k2, ms$lab_l),
               knn2bray_litter_exact_p = ex_k2$pval, mst_pure_mouse_binary = pure_edges(mst_edges(djb), ms$lab_m),
               book_pkg_p_mouse = tests$mouse$pval, book_pkg_p_litter = tests$litter$pval)), "ch10-mice.json", digits = 7)

  gag <- ape::read.dna(msmb_data("DNA_GAG_20.txt"))
  gs <- apply(as.character(gag), 1, paste, collapse = "")
  gs <- gsub("-", "?", gs, fixed = TRUE)    # alignment gaps written as ? (any non-ACGT symbol is skipped)
  dr <- as.matrix(dist.dna(gag, model = "raw", pairwise.deletion = TRUE))
  dj <- as.matrix(dist.dna(gag, model = "JC69", pairwise.deletion = TRUE))
  dk <- as.matrix(dist.dna(gag, model = "K80", pairwise.deletion = TRUE))
  export_json(list(note = "20 aligned HIV gag sequences (Holmes and Huber 2019, Exercise 10.8), gaps written as ?; distances from ape::dist.dna with pairwise deletion",
                   names = rownames(gag), seqs = unname(toupper(gs)),
                   ref = list(raw_1_2 = dr[1, 2], jc_1_2 = dj[1, 2], k80_1_2 = dk[1, 2], raw_max = max(dr), jc_max = max(dj),
                              k80_max = max(dk), jc_mean = mean(dj[upper.tri(dj)]), raw_mean = mean(dr[upper.tri(dr)]),
                              k80_mean = mean(dk[upper.tri(dk)]))), "ch10-gag.json", digits = 8)

  tree1 <- load_tree1(); s6 <- get_seqs6()
  sm <- as.character(s6)
  dml <- as.matrix(dist.ml(s6, "JC69"))
  tnj <- nj(dist.ml(s6, "JC69"))
  ll_true <- pml(tree1, s6)$logLik
  ll_nj <- pml(tnj, s6)$logLik
  export_json(list(note = "tree1 and 60 sites simulated on it (Holmes and Huber 2019, Section 10.4.2), F81 with base frequencies (1,1,3,3)/8, rate 0.1",
                   tips = tree1$tip.label, edge = tree1$edge, length = tree1$edge.length,
                   seqs = unname(apply(sm[tree1$tip.label, ], 1, paste, collapse = "")),
                   ref = list(jc_dist = unname(dml[tree1$tip.label, tree1$tip.label]),
                              nj_patristic = unname(cophenetic(tnj)[tree1$tip.label, tree1$tip.label]),
                              loglik_true_jc = ll_true, loglik_nj_jc = ll_nj,
                              loglik_true_jc_scaled = pml(tree1, s6, rate = 0.1)$logLik)), "ch10-tree.json", digits = 8)
}

# Set .ch10_no_run before sourcing this file to reuse the helpers without plotting
if (!exists(".ch10_no_run")) {
  run_figures(list(fig_10_1 = fig_10_1, fig_10_2 = fig_10_2, fig_10_3 = fig_10_3, fig_10_5 = fig_10_5,
                   fig_10_6 = fig_10_6, fig_10_7 = fig_10_7, fig_10_8 = fig_10_8, fig_10_9 = fig_10_9,
                   fig_10_10 = fig_10_10, fig_10_11 = fig_10_11, fig_10_12 = fig_10_12, fig_10_15 = fig_10_15,
                   fig_10_16 = fig_10_16, fig_10_17 = fig_10_17, fig_10_18 = fig_10_18, fig_10_20 = fig_10_20,
                   fig_10_21 = fig_10_21, fig_10_22 = fig_10_22, fig_10_23 = fig_10_23, fig_10_24 = fig_10_24,
                   fig_10_25 = fig_10_25, fig_10_26 = fig_10_26, fig_10_27 = fig_10_27, fig_10_28 = fig_10_28,
                   fig_10_29 = fig_10_29, fig_10_32 = fig_10_32, fig_10_33 = fig_10_33,
                   tab_16s = tab_16s, data_ch10 = data_ch10))
}
