flowchart LR
subgraph sp["Species"]
direction TB
s1["<i>Roseburia intestinalis</i><br/>6"]
s2["<i>Roseburia hominis</i><br/>3"]
s3["<i>Blautia obeum</i><br/>4"]
s4["<i>Bacteroides fragilis</i><br/>5"]
end
subgraph ge["Genus"]
direction TB
g1["<i>Roseburia</i><br/>6 + 3 = 9"]
g2["<i>Blautia</i><br/>4"]
g3["<i>Bacteroides</i><br/>5"]
end
subgraph fa["Family"]
direction TB
f1["Lachnospiraceae<br/>9 + 4 = 13"]
f2["Bacteroidaceae<br/>5"]
end
s1 --> g1
s2 --> g1
s3 --> g2
s4 --> g3
g1 --> f1
g2 --> f1
g3 --> f2
classDef leaf fill:#ffffff,stroke:#6c757d,color:#070707
classDef agg fill:#e6f3f7,stroke:#0087af,color:#070707
class s1,s2,s3,s4 leaf
class g1,g2,g3,f1,f2 agg
9 Agglomeration
In Chapter 8, we covered how to select features from the dataset. Agglomeration, on the other hand, involves combining data points into broader feature categories by summing their values. For instance, if you have counts for individual species, you might agglomerate them into groups based on the family of each species. This means you would add up the counts of all species within a particular family to get a single value that represents that family (Figure 9.1). While this method simplifies your dataset and you lose the specific information about individual species, it can be useful in highlighting overall trends.
The choice between subsetting and agglomeration depends on your research goals and the type of insights you want to gain from your data. For example, if you are interested in comparing compositions of two types of microbiomes (e.g., gut microbiome and soil microbiome), you might want to first examine their taxonomic profiles at the level of phyla or families, rather than species, to get an idea of their broad similarities or differences. Agglomeration is most often used to reduce the number of features, especially in sequencing data, where there may not be enough resolution to reliably differentiate between closely related species. Combining data at higher taxonomic levels allows you to focus on broader patterns in the community while managing the complexity of the dataset. Similarly, in addition to agglomerating features to higher categories, you might want to agglomerate samples within the given sample type, to examine these broad differences across sample types rather than samples.
When you are interested in the total feature abundances of certain taxonomy ranks, the data should first be agglomerated to the specified taxonomy level. Afterward, we can then select the desired taxa from the dataset.
9.1 By taxonomy rank
One of the main applications of taxonomic information in sequencing data is to agglomerate counts (such as ASV counts) to specific taxonomic levels and to model how different sample-specific variables may influence the feature composition at different taxonomic levels. For this, mia contains the agglomerateByRank() function.
At its simplest, the function takes a TreeSE object as input and outputs a TreeSE object agglomerated to a specified taxonomy level using the rank parameter. Note that we can also choose to prune the phylogenetic tree to correspond to the agglomerated data, by setting update.tree = TRUE. The tree can be pruned only if the features of each group form a subtree of their own, i.e., the groups are monophyletic. Otherwise, the tree is removed.
# Agglomerate
tse_phylum <- agglomerateByRank(tse, rank = "Phylum", update.tree = TRUE)
tse_phylum
## class: TreeSummarizedExperiment
## dim: 66 26
## metadata(1): agglomerated_by_rank
## assays(1): counts
## rownames(66): ABY1_OD1 AC1 ... ZB2 ZB3
## rowData names(7): Kingdom Phylum ... Genus Species
## colnames(26): CL3 CC1 ... Even2 Even3
## colData names(7): X.SampleID Primer ... SampleType Description
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):
## rowLinks: NULL
## rowTree: NULL
## colLinks: NULL
## colTree: NULLThe output now contains 66 features, a reduction from the original 19216 rows. Agglomeration has combined features together to higher feature categories, but the samples remain unchanged from the original dataset. Let’s take a look at the rowData to see how it looks.
rowData(tse_phylum)
## DataFrame with 66 rows and 7 columns
## Kingdom Phylum Class Order
## <character> <character> <character> <character>
## ABY1_OD1 Bacteria ABY1_OD1 NA NA
## AC1 Bacteria AC1 NA NA
## AD3 Bacteria AD3 NA NA
## Acidobacteria Bacteria Acidobacteria NA NA
## Actinobacteria Bacteria Actinobacteria NA NA
## ... ... ... ... ...
## WS1 Bacteria WS1 NA NA
## WS2 Bacteria WS2 NA NA
## WS3 Bacteria WS3 NA NA
## ZB2 Bacteria ZB2 NA NA
## ZB3 Bacteria ZB3 NA NA
## Family Genus Species
## <character> <character> <character>
## ABY1_OD1 NA NA NA
## AC1 NA NA NA
## AD3 NA NA NA
## Acidobacteria NA NA NA
## Actinobacteria NA NA NA
## ... ... ... ...
## WS1 NA NA NA
## WS2 NA NA NA
## WS3 NA NA NA
## ZB2 NA NA NA
## ZB3 NA NA NAAs we observe from the taxonomy table, all lower ranks below Phylum now contain NA values. This is expected, as we have agglomerated the data to the Phylum level, meaning that the lowest rank that rows can be uniquely mapped to is the Phylum rank.
Since we specified update.tree = TRUE, the function tried to prune the phylogenetic tree. In this dataset, however, some phyla are not monophyletic: their features are spread across different branches of the tree. The tree could therefore not be agglomerated, and it was removed.
rowTree(tse_phylum)
## NULLIf a tree is needed for the agglomerated data, a hierarchy tree can be created from the taxonomy table instead (see Section 5.4).
Additionally, we can examine the counts assay table to assess how the agglomeration has affected the counts of unique features per sample.
assay(tse_phylum, "counts")[1:5, 1:5]
## CL3 CC1 SV1 M31Fcsw M11Fcsw
## ABY1_OD1 11 33 0 0 0
## AC1 0 0 0 0 0
## AD3 1299 15577 17 2 1
## Acidobacteria 269202 183132 92863 120 115
## Actinobacteria 39601 90280 121703 2540 841The values in the counts assay are considerably larger than in the original data, indicating that the values have been summed during agglomeration.
Now that the data is agglomerated, we can check the abundances of certain phyla across the samples.
# Store features of interest into phyla
phyla <- c("Actinobacteria", "Chlamydiae")
# Subset by feature
tse_sub <- tse_phylum[phyla, ]
# Show counts
assay(tse_sub)
## CL3 CC1 SV1 M31Fcsw M11Fcsw M31Plmr M11Plmr F21Plmr
## Actinobacteria 39601 90280 121703 2540 841 85825 103067 27295
## Chlamydiae 265 702 22 0 1 5 22 0
## M31Tong M11Tong LMEpi24M SLEpi20M AQC1cm AQC4cm AQC7cm NP2
## Actinobacteria 123464 8822 444605 648008 26392 27519 30304 20047
## Chlamydiae 0 0 3 8 9 26 35 6
## NP3 NP5 TRRsed1 TRRsed2 TRRsed3 TS28 TS29 Even1
## Actinobacteria 19592 168762 1058 2290 2351 27569 130158 105541
## Chlamydiae 36 9 2 85 62 2 0 1
## Even2 Even3
## Actinobacteria 79261 89965
## Chlamydiae 1 0As data was agglomerated and then further subsetted, the number of rows should equal the number of phyla used in subsetting (in this case, just 2).
Furthermore, we can observe that the agglomeration will be applied to every assay in the dataset. Let’s add another assay to the original dataset and then perform agglomeration again, this time at the Family level.
# Add another assay
assay(tse, "another_assay", withDimnames = FALSE) <- matrix(
runif(ncol(tse) * nrow(tse), 0, 1),
ncol = ncol(tse), nrow = nrow(tse)
)
# Agglomerate
tse_family <- agglomerateByRank(tse, rank = "Family")
assayNames(tse_family)
## [1] "counts" "another_assay"We can now confirm that the agglomerated dataset contains two assays: “counts” and “another_assay”, consistent with the original data structure.
If the data is agglomerated by features, the ideal location to store the resulting dataset is the alternative experiment, altExp, slot (see Section 3.7). Let’s add the Phylum data there.
altExp(tse, "phylum") <- tse_phylum
altExpNames(tse)
## [1] "phylum"altExp now contains the Phylum-level data under the name phylum. This can be extended to any taxonomic level listed in taxonomyRanks(tse). While it is certainly possible to agglomerate data one taxonomic level at a time, you can also aggregate data across all available ranks in a single step using the agglomerateByRanks() function. This function returns a TreeSE object that includes the agglomerated datasets in the altExp slot.
If you want the data as a list as discussed in Section 6.1, you can achieve this by specifying as.list = TRUE.
tse <- agglomerateByRanks(tse)
altExpNames(tse)
## [1] "phylum" "Kingdom" "Phylum" "Class" "Order" "Family" "Genus"
## [8] "Species"9.2 By variable
The agglomerateByRank() function aggregates data based on rowData, that is, the agglomerates are made based on features, such as taxonomy information. Alternatively, you can use agglomerateByVariable() to aggregate the data.
agglomerateByVariable() is designed to aggregate data based on criteria other than taxa, such as sample groups or clusters. The by parameter specifies whether features (by = "rows") or samples (by = "cols") are aggregated, and group names the rowData or colData variable that defines the groups. Below, we aggregate samples by their sample type.
Note that a feature dataset might contain taxa with the same lower-level rank, even if their higher-level ranks differ. For this reason, agglomerateByVariable() should not be used for aggregating taxonomy ranks.
# Agglomerate samples based on type
tse_sub <- agglomerateByVariable(tse, by = "cols", group = "SampleType")
tse_sub
## class: TreeSummarizedExperiment
## dim: 19216 9
## metadata(0):
## assays(2): counts another_assay
## rownames(19216): 549322 522457 ... 200359 271582
## rowData names(7): Kingdom Phylum ... Genus Species
## colnames(9): Feces Freshwater ... Soil Tongue
## colData names(7): X.SampleID Primer ... SampleType Description
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(8): phylum Kingdom ... Genus Species
## rowLinks: a LinkDataFrame (19216 rows)
## rowTree: 1 phylo tree(s) (19216 leaves)
## colLinks: NULL
## colTree: NULLNow, the data includes as many columns as there are sample types.
In Section 14.1.2, we will explore how cluster information can be used to agglomerate data effectively.
9.3 Choosing a taxonomic level
There is no single correct taxonomic level for an analysis; the choice depends on the data and the research question. Some general considerations:
- Use the finest level that the data supports. Agglomeration discards information, so analyses such as alpha and beta diversity, ordination and PERMANOVA are often done at the finest available level. Phylogenetic measures, such as Faith’s diversity and UniFrac, use the tree and do not need agglomeration.
- Consider the sequencing method. Short-read 16S rRNA amplicon data often cannot reliably resolve species, so the genus level is a common choice, while full-length 16S rRNA sequencing can resolve many species (Johnson et al. 2019). Shotgun metagenomic data are commonly analyzed at the species level.
- Consider interpretation. Higher levels, such as phylum or family, give fewer features that are easier to summarize and report, which is why they are common in composition barplots (Section 11.1). At very fine levels, many features are found only in a few samples, which can make samples look more different from each other.
- Consider multiple testing. Fewer features mean fewer tests, for example in differential abundance analysis, and thus a smaller correction for multiple testing. However, choose the level before looking at the results, not based on which level gives the smallest p-values.
It can be useful to check whether the main results hold at more than one level. For choosing the transformation, see Section 10.4.
9.4 Exercises
We use the Tengeler2020 dataset.
Solve the exercises in order, as each exercise continues from the previous one. Try to solve each exercise yourself before opening the solution.