Compositional Heatmaps

Example 1.1

The following packages are needed to succesfully run the examples in this notebook:

  • mia: tools for microbiome data analysis

  • ComplexHeatmap: plotting heatmaps

To begin with, we import Tengeler2020 from the mia package and store it into a variable.

# load dataset and store it into tse
data("Tengeler2020", package = "mia")
tse <- Tengeler2020

First of all, we transform the counts assay to relative abundances and store the new assay back in the TreeSE.

tse <- transformAssay(tse, method = "relabundance")

Why relative abundances?

Relative abundances are useful because they are not affected by compositionality, which is present in the counts assay due to sequencing bias (the unequal amplification of DNA from two different samples due to random effects).

tse_phylum <- mergeFeaturesByRank(tse, rank = "Phylum")

tse_list <- splitOn(tse_phylum, f = "patient_status")

top_tab_list <- lapply(
    tse_list,
    function(x) round(rowMeans(assay(x, "relabundance")[getTopTaxa(x, top = 4L), ]) * 100, 1)
)

knitr::kable(cbind(top_tab_list[[1]], top_tab_list[[2]]), col.names = c("ADHD", "Control"))
ADHD Control
Bacteroidetes 69.2 61.1
Firmicutes 23.7 30.1
Verrucomicrobia 4.7 6.9
Proteobacteria 2.4 1.8

Example 1.2

Next, we agglomerate the experiment to the order level, so that information is more condensed and therefore easier to visualise and interpret.

tse_order <- mergeFeaturesByRank(tse, rank = "Order")

We then perform a CLR transformation sample-wise and a Z transformation feature-wise. The former creates a more homogeneous (logarithmic) scale for the usually very skewed microbiome data, whereas the latter normalises the features across samples for better comparisons between samples.

# transform relative abundance to clr
tse_order <- transformAssay(tse_order,
                            assay.type = "relabundance",
                            method = "clr",
                            pseudocount = 1,
                            MARGIN = "samples")

# transform clr to z
tse_order <- transformAssay(tse_order,
                            assay.type = "clr", 
                            method = "z",
                            name = "clr_z",
                            MARGIN = "features")

Example 1.3

Finally, we plot the compositional heatmap with the ComplexHeatmap package and illustrate it in Figure 1.

hm1 <- Heatmap(assay(tse_order, "clr_z"), name = "clr-z")
hm1

Figure 1: Heatmap of CLR-Z assay where columns correspond to samples and rows to taxa agglomerated by order.

Why clr-z transformation?

hm2 <- Heatmap(assay(tse_order, "counts"), name = "counts")
hm3 <- Heatmap(assay(tse_order, "relabundance"), name = "relabundance")
hm1 + hm2 + hm3

Exercise 1

  • exercise 9.2
  • refine exercise 9.2 and make a new one with some heatmap functionality