ANCOM Tutorial

Huang Lin\(^1\)

\(^1\)NIEHS, Research Triangle Park, NC 27709, USA

October 24, 2023

knitr::opts_chunk$set(message = FALSE, warning = FALSE, comment = NA,
                      fig.width = 6.25, fig.height = 5)
library(ANCOMBC)
library(tidyverse)

1. Introduction

Analysis of Composition of Microbiomes (ANCOM) (Mandal et al. 2015) is a differential abundance (DA) analysis for microbial absolute abundances. It accounts for the compositionality of microbiome data by performing the additive log ratio (ALR) transformation. ANCOM employs a heuristic strategy to declare taxa that are significantly differentially abundant. For a given taxon, the output W statistic represents the number ALR transformed models where the taxon is differentially abundant with regard to the variable of interest. The larger the value of W, the more likely the taxon is differentially abundant. For more details, please refer to the ANCOM paper.

2. Installation

Download package.

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("ANCOMBC")

Load the package.

library(ANCOMBC)

3. Run ANCOM on a real cross-sectional dataset

3.1 Import example data

The HITChip Atlas dataset contains genus-level microbiota profiling with HITChip for 1006 western adults with no reported health complications, reported in (Lahti et al. 2014). The dataset is available via the microbiome R package (Lahti et al. 2017) in phyloseq (McMurdie and Holmes 2013) format. In this tutorial, we consider the following covariates:

data(atlas1006, package = "microbiome")
tse = mia::makeTreeSummarizedExperimentFromPhyloseq(atlas1006)

# Subset to baseline
tse = tse[, tse$time == 0]

# Re-code the bmi group
tse$bmi = recode(tse$bmi_group,
                 obese = "obese",
                 severeobese = "obese",
                 morbidobese = "obese")
# Subset to lean, overweight, and obese subjects
tse = tse[, tse$bmi %in% c("lean", "overweight", "obese")]

# Note that by default, levels of a categorical variable in R are sorted 
# alphabetically. In this case, the reference level for `bmi` will be 
# `lean`. To manually change the reference level, for instance, setting `obese`
# as the reference level, use:
tse$bmi = factor(tse$bmi, levels = c("obese", "overweight", "lean"))
# You can verify the change by checking:
# levels(sample_data(tse)$bmi)

# Create the region variable
tse$region = recode(as.character(tse$nationality),
                    Scandinavia = "NE", UKIE = "NE", SouthEurope = "SE", 
                    CentralEurope = "CE", EasternEurope = "EE",
                    .missing = "unknown")

# Discard "EE" as it contains only 1 subject
# Discard subjects with missing values of region
tse = tse[, ! tse$region %in% c("EE", "unknown")]

print(tse)
class: TreeSummarizedExperiment 
dim: 130 873 
metadata(0):
assays(1): counts
rownames(130): Actinomycetaceae Aerococcus ... Xanthomonadaceae
  Yersinia et rel.
rowData names(3): Phylum Family Genus
colnames(873): Sample-1 Sample-2 ... Sample-1005 Sample-1006
colData names(12): age sex ... bmi region
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):
rowLinks: NULL
rowTree: NULL
colLinks: NULL
colTree: NULL

3.2 Run ancom function

set.seed(123)
out = ancom(data = tse, assay_name = "counts", 
            tax_level = "Family", phyloseq = NULL, 
            p_adj_method = "holm", prv_cut = 0.10, lib_cut = 1000, 
            main_var = "bmi", adj_formula = "age + region", 
            rand_formula = NULL, lme_control = NULL, struc_zero = TRUE,
            neg_lb = TRUE, alpha = 0.05, n_cl = 2)

res = out$res

# Similarly, if the main variable of interest is continuous, such as age, the
# ancom model can be specified as
# out = ancom(data = tse, assay_name = "counts",
#             tax_level = "Family", phyloseq = NULL,
#             p_adj_method = "holm", prv_cut = 0.10, lib_cut = 1000,
#             main_var = "age", adj_formula = "bmi + region",
#             rand_formula = NULL, lme_control = NULL, struc_zero = FALSE,
#             neg_lb = FALSE, alpha = 0.05, n_cl = 2)

# ancom also supports importing data in phyloseq format
# tse_alt = agglomerateByRank(tse, "Family")
# pseq = makePhyloseqFromTreeSummarizedExperiment(tse_alt)
# out = ancom(data = NULL, assay_name = NULL,
#             tax_level = "Family", phyloseq = pseq,
#             p_adj_method = "holm", prv_cut = 0.10, lib_cut = 1000,
#             main_var = "bmi", adj_formula = "age + region",
#             rand_formula = NULL, lme_control = NULL, struc_zero = TRUE,
#             neg_lb = TRUE, alpha = 0.05, n_cl = 2)

3.3 Scatter plot for W statistics

q_val = out$q_data
beta_val = out$beta_data
# Only consider the effect sizes with the corresponding q-value less than alpha
beta_val = beta_val * (q_val < 0.05) 
# Choose the maximum of beta's as the effect size
beta_pos = apply(abs(beta_val), 2, which.max) 
beta_max = vapply(seq_along(beta_pos), function(i) 
    beta_val[beta_pos[i], i], FUN.VALUE = double(1))
# Number of taxa except structural zeros
n_taxa = ifelse(is.null(out$zero_ind), 
                nrow(tse), 
                sum(apply(out$zero_ind[, -1], 1, sum) == 0))
# Cutoff values for declaring differentially abundant taxa
cut_off = 0.7 * (n_taxa - 1)

df_fig_w = res %>%
  dplyr::mutate(beta = beta_max,
                direct = case_when(
                  detected_0.7 == TRUE & beta > 0 ~ "Positive",
                  detected_0.7 == TRUE & beta <= 0 ~ "Negative",
                  TRUE ~ "Not Significant"
                  )) %>%
  dplyr::arrange(W)
df_fig_w$taxon = factor(df_fig_w$taxon, levels = df_fig_w$taxon)
df_fig_w$W = replace(df_fig_w$W, is.infinite(df_fig_w$W), n_taxa - 1)
df_fig_w$direct = factor(df_fig_w$direct, 
                         levels = c("Negative", "Positive", "Not Significant"))

p_w = df_fig_w %>%
  ggplot(aes(x = taxon, y = W, color = direct)) +
  geom_point(size = 2, alpha = 0.6) +
  labs(x = "Taxon", y = "W") +
  scale_color_discrete(name = NULL) + 
  geom_hline(yintercept = cut_off, linetype = "dotted", 
             color = "blue", size = 1.5) +
  geom_text(aes(x = 2, y = cut_off + 0.5, label = "W[0.7]"), 
            size = 5, vjust = -0.5, hjust = 0, color = "orange", parse = TRUE) +
  theme_bw() +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid.major = element_blank())
p_w

4. Run ANCOM on a real longitudinal dataset

4.1 Import example data

A two-week diet swap study between western (USA) and traditional (rural Africa) diets (Lahti et al. 2014). The dataset is available via the microbiome R package (Lahti et al. 2017) in phyloseq (McMurdie and Holmes 2013) format. In this tutorial, we consider the following fixed effects:

and the following random effects:

data(dietswap, package = "microbiome")
tse = mia::makeTreeSummarizedExperimentFromPhyloseq(dietswap)
print(tse)
class: TreeSummarizedExperiment 
dim: 130 222 
metadata(0):
assays(1): counts
rownames(130): Actinomycetaceae Aerococcus ... Xanthomonadaceae
  Yersinia et rel.
rowData names(3): Phylum Family Genus
colnames(222): Sample-1 Sample-2 ... Sample-221 Sample-222
colData names(8): subject sex ... timepoint.within.group bmi_group
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):
rowLinks: NULL
rowTree: NULL
colLinks: NULL
colTree: NULL

4.2 Run ancom function

set.seed(123)
out = ancom(data = tse, assay_name = "counts", 
            tax_level = "Family", phyloseq = NULL, 
            p_adj_method = "holm", prv_cut = 0.10, lib_cut = 1000, 
            main_var = "group",
            adj_formula = "nationality + timepoint", 
            rand_formula = "(timepoint | subject)", 
            lme_control = lme4::lmerControl(), 
            struc_zero = TRUE, neg_lb = TRUE, alpha = 0.05, n_cl = 2)

res = out$res

4.3 Visualization for W statistics

q_val = out$q_data
beta_val = out$beta_data
# Only consider the effect sizes with the corresponding q-value less than alpha
beta_val = beta_val * (q_val < 0.05) 
# Choose the maximum of beta's as the effect size
beta_pos = apply(abs(beta_val), 2, which.max) 
beta_max = vapply(seq_along(beta_pos), function(i) beta_val[beta_pos[i], i],
                  FUN.VALUE = double(1))
# Number of taxa except structural zeros
n_taxa = ifelse(is.null(out$zero_ind), 
                nrow(tse), 
                sum(apply(out$zero_ind[, -1], 1, sum) == 0))
# Cutoff values for declaring differentially abundant taxa
cut_off = 0.7 * (n_taxa - 1)

df_fig_w = res %>%
  dplyr::mutate(beta = beta_max,
                direct = case_when(
                  detected_0.7 == TRUE & beta > 0 ~ "Positive",
                  detected_0.7 == TRUE & beta <= 0 ~ "Negative",
                  TRUE ~ "Not Significant"
                  )) %>%
  dplyr::arrange(W)
df_fig_w$taxon = factor(df_fig_w$taxon, levels = df_fig_w$taxon)
df_fig_w$W = replace(df_fig_w$W, is.infinite(df_fig_w$W), n_taxa - 1)
df_fig_w$direct = factor(df_fig_w$direct, 
                     levels = c("Negative", "Positive", "Not Significant"))

p_w = df_fig_w %>%
  ggplot(aes(x = taxon, y = W, color = direct)) +
  geom_point(size = 2, alpha = 0.6) +
  labs(x = "Taxon", y = "W") +
  scale_color_discrete(name = NULL) + 
  geom_hline(yintercept = cut_off, linetype = "dotted", 
             color = "blue", size = 1.5) +
  geom_text(aes(x = 2, y = cut_off + 0.5, label = "W[0.7]"), 
            size = 5, vjust = -0.5, hjust = 0, color = "orange", parse = TRUE) +
  theme_bw() +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid.major = element_blank())
p_w

Session information

sessionInfo()
R Under development (unstable) (2023-10-22 r85388)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/New_York
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] TreeSummarizedExperiment_2.11.0 GenomicRanges_1.55.0           
 [3] SummarizedExperiment_1.33.0     SingleCellExperiment_1.25.0    
 [5] IRanges_2.37.0                  S4Vectors_0.41.0               
 [7] phyloseq_1.47.0                 lubridate_1.9.3                
 [9] forcats_1.0.0                   stringr_1.5.0                  
[11] dplyr_1.1.3                     purrr_1.0.2                    
[13] readr_2.1.4                     tidyr_1.3.0                    
[15] tibble_3.2.1                    ggplot2_3.4.4                  
[17] tidyverse_2.0.0                 ANCOMBC_2.5.0                  

loaded via a namespace (and not attached):
  [1] splines_4.4.0               bitops_1.0-7               
  [3] cellranger_1.1.0            rpart_4.1.21               
  [5] DirichletMultinomial_1.45.0 lifecycle_1.0.3            
  [7] Rdpack_2.5                  doParallel_1.0.17          
  [9] lattice_0.22-5              MASS_7.3-60.1              
 [11] MultiAssayExperiment_1.29.0 backports_1.4.1            
 [13] magrittr_2.0.3              Hmisc_5.1-1                
 [15] sass_0.4.7                  rmarkdown_2.25             
 [17] jquerylib_0.1.4             yaml_2.3.7                 
 [19] doRNG_1.8.6                 gld_2.6.6                  
 [21] DBI_1.1.3                   minqa_1.2.6                
 [23] ade4_1.7-22                 multcomp_1.4-25            
 [25] abind_1.4-5                 zlibbioc_1.49.0            
 [27] expm_0.999-7                BiocGenerics_0.49.0        
 [29] RCurl_1.98-1.12             TH.data_1.1-2              
 [31] yulab.utils_0.1.0           nnet_7.3-19                
 [33] sandwich_3.0-2              GenomeInfoDbData_1.2.11    
 [35] ggrepel_0.9.4               irlba_2.3.5.1              
 [37] tidytree_0.4.5              vegan_2.6-4                
 [39] permute_0.9-7               DelayedMatrixStats_1.25.0  
 [41] codetools_0.2-19            DelayedArray_0.29.0        
 [43] scuttle_1.13.0              energy_1.7-11              
 [45] tidyselect_1.2.0            farver_2.1.1               
 [47] lme4_1.1-34                 gmp_0.7-2                  
 [49] ScaledMatrix_1.11.0         viridis_0.6.4              
 [51] matrixStats_1.0.0           stats4_4.4.0               
 [53] base64enc_0.1-3             jsonlite_1.8.7             
 [55] multtest_2.59.0             BiocNeighbors_1.21.0       
 [57] e1071_1.7-13                decontam_1.23.0            
 [59] mia_1.11.0                  Formula_1.2-5              
 [61] survival_3.5-7              scater_1.31.0              
 [63] iterators_1.0.14            foreach_1.5.2              
 [65] tools_4.4.0                 treeio_1.27.0              
 [67] DescTools_0.99.50           Rcpp_1.0.11                
 [69] glue_1.6.2                  gridExtra_2.3              
 [71] SparseArray_1.3.0           xfun_0.40                  
 [73] mgcv_1.9-0                  MatrixGenerics_1.15.0      
 [75] GenomeInfoDb_1.39.0         withr_2.5.1                
 [77] numDeriv_2016.8-1.1         fastmap_1.1.1              
 [79] rhdf5filters_1.15.0         boot_1.3-28.1              
 [81] bluster_1.13.0              fansi_1.0.5                
 [83] digest_0.6.33               rsvd_1.0.5                 
 [85] timechange_0.2.0            R6_2.5.1                   
 [87] colorspace_2.1-0            gtools_3.9.4               
 [89] RSQLite_2.3.1               utf8_1.2.4                 
 [91] generics_0.1.3              data.table_1.14.8          
 [93] DECIPHER_2.31.0             class_7.3-22               
 [95] CVXR_1.0-11                 httr_1.4.7                 
 [97] htmlwidgets_1.6.2           S4Arrays_1.3.0             
 [99] pkgconfig_2.0.3             gtable_0.3.4               
[101] Exact_3.2                   Rmpfr_0.9-3                
[103] blob_1.2.4                  XVector_0.43.0             
[105] htmltools_0.5.6.1           biomformat_1.31.0          
[107] scales_1.2.1                Biobase_2.63.0             
[109] lmom_3.0                    knitr_1.44                 
[111] rstudioapi_0.15.0           tzdb_0.4.0                 
[113] reshape2_1.4.4              checkmate_2.2.0            
[115] nlme_3.1-163                nloptr_2.0.3               
[117] rhdf5_2.47.0                proxy_0.4-27               
[119] cachem_1.0.8                zoo_1.8-12                 
[121] rootSolve_1.8.2.4           parallel_4.4.0             
[123] vipor_0.4.5                 foreign_0.8-85             
[125] pillar_1.9.0                grid_4.4.0                 
[127] vctrs_0.6.4                 BiocSingular_1.19.0        
[129] beachmat_2.19.0             cluster_2.1.4              
[131] beeswarm_0.4.0              htmlTable_2.4.1            
[133] evaluate_0.22               mvtnorm_1.2-3              
[135] cli_3.6.1                   compiler_4.4.0             
[137] rlang_1.1.1                 crayon_1.5.2               
[139] rngtools_1.5.2              labeling_0.4.3             
[141] plyr_1.8.9                  fs_1.6.3                   
[143] ggbeeswarm_0.7.2            stringi_1.7.12             
[145] viridisLite_0.4.2           BiocParallel_1.37.0        
[147] lmerTest_3.1-3              munsell_0.5.0              
[149] Biostrings_2.71.0           gsl_2.1-8                  
[151] lazyeval_0.2.2              Matrix_1.6-1.1             
[153] hms_1.1.3                   sparseMatrixStats_1.15.0   
[155] bit64_4.0.5                 Rhdf5lib_1.25.0            
[157] rbibutils_2.2.15            igraph_1.5.1               
[159] memoise_2.0.1               bslib_0.5.1                
[161] bit_4.0.5                   readxl_1.4.3               
[163] ape_5.7-1                  

References

Lahti, Leo, Jarkko Salojärvi, Anne Salonen, Marten Scheffer, and Willem M De Vos. 2014. “Tipping Elements in the Human Intestinal Ecosystem.” Nature Communications 5 (1): 1–10.

Lahti, Leo, Sudarshan Shetty, T Blake, J Salojarvi, and others. 2017. “Tools for Microbiome Analysis in R.” Version 1: 10013.

Mandal, Siddhartha, Will Van Treuren, Richard A White, Merete Eggesbø, Rob Knight, and Shyamal D Peddada. 2015. “Analysis of Composition of Microbiomes: A Novel Method for Studying Microbial Composition.” Microbial Ecology in Health and Disease 26 (1): 27663.

McMurdie, Paul J, and Susan Holmes. 2013. “Phyloseq: An R Package for Reproducible Interactive Analysis and Graphics of Microbiome Census Data.” PloS One 8 (4): e61217.