Chapter 5 Microbiome data exploration
Now we have loaded the data set into R. Next, let us walk through some basic operations for data exploration to confirm that the data has all the necessary components.
5.1 Data structure
Let us now investigate how taxonomic profiling data is organized in R.
Dimensionality tells us how many taxa and samples the data contains. As we can see, there are 12613 taxa and 40 samples.
# mae[]: indexing/retrieving the taxonomic data experiment dim(mae[])
##  12613 40
rowData slot contains a taxonomic table. This includes taxonomic
information for each of the 12613 entries. With the
command, we can print just the beginning of the table.
rowData seems to contain information from 7
different taxonomy classes.
::kable(head(rowData(mae[]))) %>% knitr::kable_styling("striped", kableExtralatex_options="scale_down") %>% ::scroll_box(width = "100%") kableExtra
The colData slot contains sample metadata. It contains information for all 40 samples.
However, here only the 6 first samples are shown as we use the
head() command. There
are 6 columns, that contain information, e.g., about patients’ status, and cohort.
# For simplicity, classify all high-fat diets as high-fat, and all the low-fat # diets as low-fat diets colData(mae)$Diet <- ifelse(colData(mae)$Diet == "High-fat" | colData(mae)$Diet == "High-fat + XOS", "High-fat", "Low-fat") ::kable(head(colData(mae))) %>% knitr::kable_styling("striped", kableExtralatex_options="scale_down") %>% ::scroll_box(width = "100%") kableExtra
From here, we can draw summaries of the sample (column) data, for instance to see what is the diet distribution.
colData(mae)$Diet fetches the data from the
table() creates a table that shows how many times each
class is present, and
sort() sorts the table to ascending order.
There are 20 samples from mice having High-fat, and 20 Low-fat.
## ## High-fat Low-fat ## 20 20
Microbial abundances are typically ‘compositional’ (relative) in the current microbiome profiling data sets. This is due to technical aspects of the data generation process (see e.g. Gloor et al., 2017).
The next example calculates relative abundances as these are usually easier to interpret than plain counts. For some statistical models we need to transform the data into other formats as explained in above link (and as we will see later).
# Calculates relative abundances, and stores the table to assays 1]] <- transformCounts(mae[], method = "relabundance")mae[[
A variety of standard transformations for microbiome data are available through mia R package.
Microbial species can be called at multiple taxonomic resolutions. We can easily agglomerate the data based on taxonomic ranks. Here, we agglomerate the data at Phylum level.
<- agglomerateByRank(mae[], rank = "Phylum") se_phylum # Show dimensionality dim(se_phylum)
##  13 40
Now there are 13 taxa and 40
samples, meaning that there are 13 different
Phylum level taxonomic groups. Looking at the
agglomeration shows all Firmicutes are combined together, and all
lower rank information is lost.
From the assay we can see that all abundances of taxa that belong to Firmicutes are summed up.
::kable(head(rowData(se_phylum))) %>% knitr::kable_styling("striped", kableExtralatex_options="scale_down") %>% ::scroll_box(width = "100%") kableExtra
If you are sharp, you have by now noticed that all the aggregated
values in the above example are NA’s (missing data). This is because
the agglomeration is missing abundances for certain taxa, and in that
case the sum is not defined by default (
na.rm = FALSE). We can
ignore the missing values in summing up the data by setting
na.rm = TRUE; then the taxa that do not have information in specified level
will be removed. Those taxa that do not have information in specified
level are agglomerated at lowest possible level that is left after
<- rowData(agglomerateByRank(mae[], rank = "Genus")) temp # Prints those taxa that do not have information at the Genus level (NA) ::kable(head(temp[which(is.na(temp$Genus)),])) %>% knitr::kable_styling("striped", kableExtralatex_options="scale_down") %>% ::scroll_box(width = "100%") kableExtra
Here agglomeration is done similarly, but na.rm = TRUE
<- rowData(agglomerateByRank(mae[], rank = "Genus", na.rm = TRUE)) temp2 print(paste0("Agglomeration with na.rm = FALSE: ", dim(temp), " taxa."))
##  "Agglomeration with na.rm = FALSE: 277 taxa."
print(paste0("Agglomeration with na.rm = TRUE: ", dim(temp2), " taxa."))
##  "Agglomeration with na.rm = TRUE: 262 taxa."
The mia package contains further examples on various data agglomeration and splitting options.
The miaViz package facilitates data visualization. Let us plot the Phylum level abundances.
# Here we specify "relabundance" to be abundance table that we use for plotting. # Note that we can use agglomerated or non-agglomerated mae[] as an input, because # the function agglomeration is built-in option. # Legend does not fit into picture, so its height is reduced. <- plotAbundance(mae[], abund_values="relabundance", rank = "Phylum") + plot_abundance theme(legend.key.height = unit(0.5, "cm")) + scale_y_continuous(label = scales::percent) plot_abundance
Density plot shows the overall abundance distribution for a given taxonomic group. Let us check the relative abundance of Firmicutes across the sample collection. The density plot is a smoothened version of a standard histogram.
The plot shows peak abundances around 30 %.
# Subset data by taking only Firmicutes <- se_phylum["Firmicutes"] se_firmicutes # Gets the abundance table <- assay(se_firmicutes, "relabundance") abundance_firmicutes # Creates a data frame object, where first column includes abundances <- as.data.frame(t(abundance_firmicutes)) firmicutes_abund_df # Rename the first and only column colnames(firmicutes_abund_df) <- "abund" # Creates a plot. Parameters inside feom_density are optional. With # geom_density(bw=1000), it is possible to adjust bandwidth. <- ggplot(firmicutes_abund_df, aes(x = abund)) + firmicutes_abund_plot geom_density(color="darkred", fill="lightblue") + labs(x = "Relative abundance", title = "Firmicutes") + theme_classic() + # Changes the background scale_x_continuous(label = scales::percent) firmicutes_abund_plot
For more visualization options and examples, see the miaViz vignette.