This application is motivated by http://statweb.stanford.edu/~susan/summer12/phyloseq-demosh.html.

One treelapse feature illustrated here is the ability to display only those time series associated with leaves. This is useful in this case since otherwise some of the leaves would be overrepresentated, because many of the internal nodes look like them and not other leaves. This option is set by the display_opts parameter, setting leaves_only = TRUE within the ts parameter.

library("treelapse")
library("phyloseq")
library("ape")
library("knitr")
opts_chunk$set(out.width = "300px", out.height = "300px", cache = TRUE)
data(GlobalPatterns)
gp_subset <- GlobalPatterns %>%
  filter_taxa(function(x) { var(x) > 9.22 * 1e5 }, prune = TRUE) %>%
  transform_sample_counts(asinh)
## Code from http://statweb.stanford.edu/~susan/summer12/phyloseq-demosh.html
GPUF <- UniFrac(gp_subset)
gp_hclust <- hclust(GPUF, method = "complete")

## extract edges from hierarchical clustering
edges <- as.phylo(gp_hclust)$edge
colnames(edges) <- c("parent", "child")
class(edges) <- "character"
sample_names_sub <- sample_names(gp_subset)
names(sample_names_sub) <- seq_len(nsamples(gp_subset))
rownames(edges) <- edges[, "child"]
edges[names(sample_names_sub), "child"] <- sample_names_sub

## Prepare aggregated values
X <- get_taxa(gp_subset)
taxa <- tax_table(gp_subset)
for (i in seq_len(nrow(taxa))) {
  taxa[i, ] <- na.locf(as.character(taxa[i, ]))
}
rownames(X) <- paste0("s", rownames(taxa))
plot_order <- dist(X) %>%
  hclust() %>%
  as.dendrogram() %>%
  order.dendrogram() 
X <- X[plot_order, ]

values <- tree_fun_multi(
  edges,
  X,
  tree_mean
)
values$time <- rownames(X)[values$row]

display_opts <- list(
  "x_axis_rotation" = 45,
  "axis_font_size" = 9,
  "axis_text_anchor" = "start",
  "margin" = list(
    "bottom" = 75
  ),
  "ts" = list(
    "leaves_only" = TRUE
  )
)
timebox_tree(
  values,
  edges,
  display_opts = display_opts
)
treebox(
  values,
  edges,
  display_opts = display_opts
)


krisrs1128/treelapse documentation built on Jan. 3, 2020, 6:29 p.m.