Skip to main content

ANCOM-BC

Differential abundance analysis of microbiome count data with correction for sample-specific sampling fraction bias.

Microbiome differential abundance released R Python QIIME 2

ANCOM-BC (analysis of compositions of microbiomes with bias correction) tests for differential abundance of taxa between groups. It estimates the sample-specific sampling fraction, corrects the resulting bias in a log-linear model, and reports effect estimates, standard errors, and adjusted p-values for each taxon.

Platforms

Platform Package Install Documentation
R ANCOMBC (Bioconductor) BiocManager::install("ANCOMBC") Vignette · GitHub
Python scikit-bio (version 0.7.1 or later) pip install scikit-bio skbio.stats.composition.ancombc
QIIME 2 q2-composition Included in the QIIME 2 amplicon distribution qiime composition ancombc · GitHub

Extended tutorial

The R tutorial applies ANCOM-BC to the HITChip Atlas data set of intestinal microbiota profiles from Western adults, distributed with the microbiome package as atlas1006. The analysis tests whether the abundance of each taxon, aggregated at the Family rank of the data set, differs between body mass index (BMI) groups, adjusted for age and geographic region.

Select a platform.

Model

Let \(o_{ij}\) denote the observed abundance of taxon \(i\) in sample \(j\), and let \(\mathbf{x}_j\) denote the covariate vector of sample \(j\), including an intercept. ANCOM-BC assumes

\[ \log o_{ij} = d_j + \mathbf{x}_j^\top \boldsymbol{\beta}_i + \varepsilon_{ij}, \]

where \(d_j\) is the log sampling fraction of sample \(j\) and \(\varepsilon_{ij}\) is a mean-zero error. The sampling fraction is not observed and differs between samples, which biases an unadjusted comparison of observed abundances. ANCOM-BC estimates \(d_j\), and for coefficient \(k\) of taxon \(i\) tests

\[ H_0: \beta_{ik} = 0 \quad \text{with} \quad W_{ik} = \hat{\beta}_{ik} / \widehat{\mathrm{se}}(\hat{\beta}_{ik}). \]

The coefficient \(\beta_{ik}\) is a log fold change in absolute abundance on the natural-log scale.

Data

library(ANCOMBC)
library(tidyverse)

data(atlas1006, package = "microbiome")

# Baseline samples only, so that each subject contributes one sample.
pseq <- phyloseq::subset_samples(atlas1006, time == 0)
meta <- data.frame(phyloseq::sample_data(pseq))

# BMI in three groups, with lean as the reference level.
meta$bmi <- fct_collapse(meta$bmi_group,
                         obese = c("obese", "severeobese", "morbidobese"))
meta$bmi <- factor(meta$bmi, levels = c("lean", "overweight", "obese"))

# Geographic region, with Central Europe as the reference level.
meta$region <- fct_collapse(meta$nationality,
                            CE = "CentralEurope", NE = c("Scandinavia", "UKIE"),
                            SE = "SouthEurope", EE = "EasternEurope", US = "US")

phyloseq::sample_data(pseq) <- meta
tse <- mia::convertFromPhyloseq(pseq)
tse
#> class: TreeSummarizedExperiment 
#> dim: 130 1006 
#> metadata(0):
#> assays(1): counts
#> rownames(130): Actinomycetaceae Aerococcus ... Xanthomonadaceae
#>   Yersinia et rel.
#> rowData names(3): Phylum Family Genus
#> colnames(1006): 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

The number of samples in each BMI group and region:

table(BMI = meta$bmi, Region = meta$region, useNA = "ifany")
#>             Region
#> BMI           CE  EE  NE  SE  US <NA>
#>   lean       268   1 104  49  16    6
#>   overweight  75   0  44  25   7    3
#>   obese      235   0  33   9   8    0
#>   <NA>        39  14  31   3  13   23

Samples with a missing value in any covariate of the model are excluded from the analysis.

Analysis

set.seed(123)
out <- ancombc(data = tse, assay_name = "counts", tax_level = "Family",
               formula = "age + region + bmi",
               p_adj_method = "holm", prv_cut = 0.10, lib_cut = 1000,
               group = "bmi", struc_zero = TRUE, neg_lb = TRUE,
               conserve = TRUE, alpha = 0.05, global = TRUE,
               n_cl = 1, verbose = FALSE)
Argument Role
tax_level Taxonomic rank at which counts are aggregated before testing.
formula Covariates of the log-linear model.
p_adj_method Multiplicity adjustment of the p-values across taxa.
prv_cut Taxa present in a smaller proportion of samples are excluded.
lib_cut Samples with a smaller library size are excluded.
group Grouping variable for the structural zero detection and the global test.
struc_zero, neg_lb Detection of taxa that are absent from a group.
conserve Variance estimator recommended for small samples or many differentially abundant taxa.
global Test of any difference among the levels of group.

Primary results

The element res contains one data frame per quantity, each with one row per taxon and one column per model coefficient.

Element Content
lfc Log fold change \(\hat{\beta}_{ik}\) (natural log)
se Standard error of \(\hat{\beta}_{ik}\)
W Test statistic \(W_{ik}\)
p_val, q_val p-value and adjusted p-value
diff_abn TRUE if the adjusted p-value is below alpha
res <- out$res

tab_obese <- tibble(
  taxon = res$lfc$taxon,
  lfc   = res$lfc$bmiobese,
  se    = res$se$bmiobese,
  W     = res$W$bmiobese,
  p     = res$p_val$bmiobese,
  q     = res$q_val$bmiobese,
  diff  = res$diff_abn$bmiobese
) |>
  arrange(q)

knitr::kable(tab_obese, digits = 3,
             caption = "Obese versus lean, adjusted for age and region.")
Obese versus lean, adjusted for age and region.
taxon lfc se W p q diff
Clostridium cluster IX 0.363 0.115 3.158 0.002 0.033 TRUE
Bacteroidetes 0.457 0.154 2.973 0.003 0.059 FALSE
Uncultured Clostridiales -0.377 0.127 -2.960 0.003 0.059 FALSE
Clostridium cluster XI -0.267 0.096 -2.781 0.005 0.097 FALSE
Verrucomicrobia -0.230 0.105 -2.183 0.029 0.494 FALSE
Actinobacteria -0.200 0.119 -1.675 0.094 1.000 FALSE
Bacilli -0.091 0.106 -0.859 0.390 1.000 FALSE
Clostridium cluster I -0.102 0.081 -1.263 0.207 1.000 FALSE
Clostridium cluster III -0.026 0.098 -0.263 0.793 1.000 FALSE
Clostridium cluster IV -0.062 0.132 -0.473 0.636 1.000 FALSE
Clostridium cluster XIII 0.051 0.089 0.573 0.567 1.000 FALSE
Clostridium cluster XIVa -0.032 0.129 -0.250 0.802 1.000 FALSE
Clostridium cluster XV 0.025 0.096 0.260 0.795 1.000 FALSE
Clostridium cluster XVI -0.057 0.088 -0.649 0.516 1.000 FALSE
Clostridium cluster XVII 0.010 0.117 0.089 0.929 1.000 FALSE
Clostridium cluster XVIII 0.024 0.087 0.273 0.785 1.000 FALSE
Cyanobacteria 0.060 0.121 0.498 0.619 1.000 FALSE
Fusobacteria 0.059 0.083 0.707 0.479 1.000 FALSE
Proteobacteria 0.167 0.090 1.852 0.064 1.000 FALSE
Spirochaetes 0.051 0.106 0.484 0.628 1.000 FALSE
Uncultured Mollicutes -0.059 0.103 -0.578 0.563 1.000 FALSE
tab_obese |>
  mutate(taxon = fct_reorder(taxon, lfc)) |>
  ggplot(aes(x = lfc, y = taxon)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey50") +
  geom_errorbarh(aes(xmin = lfc - 1.96 * se, xmax = lfc + 1.96 * se),
                 height = 0, color = "grey40") +
  geom_point(aes(fill = diff), shape = 21, size = 3, color = "grey20") +
  scale_fill_manual(values = c(`TRUE` = "#f2775f", `FALSE` = "white"),
                    guide = "none") +
  labs(x = "Log fold change (obese versus lean)", y = NULL) +
  theme_minimal(base_size = 12)
Figure 1: Log fold change (natural log) in absolute abundance, obese versus lean, with 95% confidence intervals. Filled points have an adjusted p-value below 0.05.

The same extraction applies to every other coefficient, for example bmioverweight or age.

Global test

The global test evaluates, for each taxon, the null hypothesis that the abundance is equal across all levels of group.

out$res_global |>
  arrange(q_val) |>
  knitr::kable(digits = 3, caption = "Global test across the three BMI groups.")
Global test across the three BMI groups.
taxon W p_val q_val diff_abn
Uncultured Clostridiales 14.038 0.002 0.038 TRUE
Bacteroidetes 11.989 0.005 0.100 FALSE
Clostridium cluster XVII 0.011 0.011 0.208 FALSE
Clostridium cluster IX 9.970 0.014 0.246 FALSE
Clostridium cluster XI 7.737 0.042 0.710 FALSE
Actinobacteria 5.439 0.132 1.000 FALSE
Bacilli 2.121 0.692 1.000 FALSE
Clostridium cluster I 1.800 0.813 1.000 FALSE
Clostridium cluster III 0.504 0.446 1.000 FALSE
Clostridium cluster IV 0.494 0.437 1.000 FALSE
Clostridium cluster XIII 0.329 0.303 1.000 FALSE
Clostridium cluster XIVa 0.227 0.215 1.000 FALSE
Clostridium cluster XV 0.068 0.067 1.000 FALSE
Clostridium cluster XVI 0.431 0.388 1.000 FALSE
Clostridium cluster XVIII 0.264 0.248 1.000 FALSE
Cyanobacteria 0.249 0.234 1.000 FALSE
Fusobacteria 0.533 0.468 1.000 FALSE
Proteobacteria 4.129 0.254 1.000 FALSE
Spirochaetes 0.263 0.246 1.000 FALSE
Uncultured Mollicutes 0.995 0.784 1.000 FALSE
Verrucomicrobia 4.793 0.182 1.000 FALSE

Structural zeros

A taxon is a structural zero in a group if it is absent from that group. Such a taxon is declared differentially abundant between that group and any group in which it is present, without a test.

zero_ind <- out$zero_ind
n_struc <- sum(rowSums(zero_ind[, -1, drop = FALSE]) > 0)
n_struc
#> [1] 0

In this analysis, 0 of the 21 taxa are structural zeros in at least one BMI group.

Bias-corrected abundances

The estimated log sampling fractions are returned in samp_frac. Subtracting them from the log observed abundances gives bias-corrected log abundances, which are comparable between samples.

samp_frac <- out$samp_frac
samp_frac[is.na(samp_frac)] <- 0          # samples excluded from the analysis

log_obs  <- log(out$feature_table + 1)
log_corr <- t(t(log_obs) - samp_frac)

round(log_corr[1:5, 1:4], 2)
#>                         Sample-1 Sample-2 Sample-3 Sample-4
#> Actinobacteria              2.41     1.59     1.61     4.80
#> Bacilli                     1.46     1.48     0.47     4.50
#> Bacteroidetes               3.89     4.61     3.05     7.93
#> Clostridium cluster I      -0.10     0.56     0.73     2.71
#> Clostridium cluster III     0.38    -0.57    -1.35     2.77

Session information

Code
sessionInfo()
#> R version 4.5.3 (2026-03-11)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Sequoia 15.7.2
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
#> 
#> locale:
#> [1] en_US/C.UTF-8/en_US/C/en_US/en_US
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] doRNG_1.8.6.3   rngtools_1.5.2  foreach_1.5.2   lubridate_1.9.5
#>  [5] forcats_1.0.1   stringr_1.6.0   dplyr_1.2.0     purrr_1.2.1    
#>  [9] readr_2.2.0     tidyr_1.3.2     tibble_3.3.1    ggplot2_4.0.2  
#> [13] tidyverse_2.0.0 ANCOMBC_2.15.1 
#> 
#> loaded via a namespace (and not attached):
#>   [1] splines_4.5.3                   cellranger_1.1.0               
#>   [3] DirichletMultinomial_1.52.0     rpart_4.1.24                   
#>   [5] lifecycle_1.0.5                 Rdpack_2.6.6                   
#>   [7] doParallel_1.0.17               lattice_0.22-9                 
#>   [9] MASS_7.3-65                     MultiAssayExperiment_1.36.1    
#>  [11] backports_1.5.0                 magrittr_2.0.4                 
#>  [13] Hmisc_5.2-5                     rmarkdown_2.30                 
#>  [15] yaml_2.3.12                     otel_0.2.0                     
#>  [17] gld_2.6.8                       DBI_1.3.0                      
#>  [19] minqa_1.2.8                     RColorBrewer_1.1-3             
#>  [21] ade4_1.7-23                     multcomp_1.4-30                
#>  [23] abind_1.4-8                     Rtsne_0.17                     
#>  [25] expm_1.0-0                      quadprog_1.5-8                 
#>  [27] GenomicRanges_1.62.1            fillpattern_1.0.3              
#>  [29] BiocGenerics_0.56.0             phyloseq_1.54.2                
#>  [31] yulab.utils_0.2.4               nnet_7.3-20                    
#>  [33] TH.data_1.1-5                   rappdirs_0.3.4                 
#>  [35] sandwich_3.1-1                  IRanges_2.44.0                 
#>  [37] S4Vectors_0.48.0                ggrepel_0.9.7                  
#>  [39] irlba_2.3.7                     tidytree_0.4.7                 
#>  [41] vegan_2.7-3                     microbiome_1.32.0              
#>  [43] rbiom_2.2.1                     parallelly_1.46.1              
#>  [45] DelayedMatrixStats_1.32.0       permute_0.9-10                 
#>  [47] codetools_0.2-20                DelayedArray_0.36.0            
#>  [49] xml2_1.5.2                      ggtext_0.1.2                   
#>  [51] scuttle_1.20.0                  energy_1.7-12                  
#>  [53] tidyselect_1.2.1                farver_2.1.2                   
#>  [55] lme4_2.0-1                      ScaledMatrix_1.18.0            
#>  [57] viridis_0.6.5                   matrixStats_1.5.0              
#>  [59] stats4_4.5.3                    base64enc_0.1-6                
#>  [61] Seqinfo_1.0.0                   jsonlite_2.0.0                 
#>  [63] BiocNeighbors_2.4.0             multtest_2.66.0                
#>  [65] e1071_1.7-17                    decontam_1.30.0                
#>  [67] mia_1.18.0                      Formula_1.2-5                  
#>  [69] emmeans_2.0.2                   survival_3.8-6                 
#>  [71] scater_1.38.0                   iterators_1.0.14               
#>  [73] systemfonts_1.3.2               ggnewscale_0.5.2               
#>  [75] tools_4.5.3                     ragg_1.5.1                     
#>  [77] treeio_1.34.0                   DescTools_0.99.60              
#>  [79] Rcpp_1.1.1                      glue_1.8.0                     
#>  [81] BiocBaseUtils_1.12.0            gridExtra_2.3                  
#>  [83] SparseArray_1.10.9              xfun_0.56                      
#>  [85] mgcv_1.9-4                      MatrixGenerics_1.22.0          
#>  [87] TreeSummarizedExperiment_2.18.0 withr_3.0.2                    
#>  [89] numDeriv_2016.8-1.1             fastmap_1.2.0                  
#>  [91] bluster_1.20.0                  boot_1.3-32                    
#>  [93] rhdf5filters_1.22.0             digest_0.6.39                  
#>  [95] rsvd_1.0.5                      estimability_1.5.1             
#>  [97] timechange_0.4.0                R6_2.6.1                       
#>  [99] textshaping_1.0.5               colorspace_2.1-2               
#> [101] gtools_3.9.5                    generics_0.1.4                 
#> [103] DECIPHER_3.6.0                  data.table_1.18.2.1            
#> [105] class_7.3-23                    httr_1.4.8                     
#> [107] htmlwidgets_1.6.4               S4Arrays_1.10.1                
#> [109] pkgconfig_2.0.3                 gtable_0.3.6                   
#> [111] Exact_3.3                       S7_0.2.1                       
#> [113] SingleCellExperiment_1.32.0     XVector_0.50.0                 
#> [115] htmltools_0.5.9                 biomformat_1.38.0              
#> [117] scales_1.4.0                    Biobase_2.70.0                 
#> [119] lmom_3.2                        reformulas_0.4.4               
#> [121] knitr_1.51                      rstudioapi_0.18.0              
#> [123] tzdb_0.5.0                      reshape2_1.4.5                 
#> [125] coda_0.19-4.1                   checkmate_2.3.4                
#> [127] nlme_3.1-168                    nloptr_2.2.1                   
#> [129] proxy_0.4-29                    zoo_1.8-15                     
#> [131] rhdf5_2.54.1                    rootSolve_1.8.2.4              
#> [133] parallel_4.5.3                  vipor_0.4.7                    
#> [135] foreign_0.8-91                  pillar_1.11.1                  
#> [137] grid_4.5.3                      vctrs_0.7.1                    
#> [139] slam_0.1-55                     BiocSingular_1.26.0            
#> [141] beachmat_2.26.0                 xtable_1.8-8                   
#> [143] cluster_2.1.8.2                 beeswarm_0.4.0                 
#> [145] htmlTable_2.4.3                 evaluate_1.0.5                 
#> [147] mvtnorm_1.3-5                   cli_3.6.5                      
#> [149] compiler_4.5.3                  rlang_1.1.7                    
#> [151] crayon_1.5.3                    labeling_0.4.3                 
#> [153] plyr_1.8.9                      fs_1.6.7                       
#> [155] ggbeeswarm_0.7.3                stringi_1.8.7                  
#> [157] viridisLite_0.4.3               BiocParallel_1.44.0            
#> [159] lmerTest_3.2-1                  Biostrings_2.78.0              
#> [161] gsl_2.1-9                       lazyeval_0.2.2                 
#> [163] Matrix_1.7-4                    patchwork_1.3.2                
#> [165] hms_1.1.4                       sparseMatrixStats_1.22.0       
#> [167] Rhdf5lib_1.32.0                 SummarizedExperiment_1.40.0    
#> [169] haven_2.5.5                     rbibutils_2.4.1                
#> [171] gridtext_0.1.6                  igraph_2.2.2                   
#> [173] readxl_1.4.5                    ape_5.8-1

The code in this tab is shown for reference and is not executed on this page.

ANCOM-BC is available in scikit-bio from version 0.7.1.

pip install scikit-bio
import pandas as pd
from skbio.stats.composition import ancombc

# Counts with samples in rows and taxa in columns; metadata with one row per sample.
table = pd.read_csv("counts.tsv", sep="\t", index_col=0)
metadata = pd.read_csv("metadata.tsv", sep="\t", index_col=0)

res = ancombc(table, metadata, formula="age + region + bmi",
              p_adjust="holm", alpha=0.05)

The arguments and the structure of the result are listed in the function reference.

The command in this tab is shown for reference and is not executed on this page.

ANCOM-BC is an action of the q2-composition plugin, which is included in the QIIME 2 amplicon distribution.

qiime composition ancombc \
  --i-table table.qza \
  --m-metadata-file metadata.tsv \
  --p-formula 'bmi' \
  --o-differentials differentials.qza

A web tool for this method is in development. This tab will hold its link and a walk-through at release.

Citation

Lin H, Peddada SD. Analysis of compositions of microbiomes with bias correction. Nature Communications 11:3514 (2020). doi:10.1038/s41467-020-17041-7

For the Python implementation, also cite: Wu Z, Lin H, Morton JT, Zhu Q. Efficient and scalable Python implementation of ANCOM-BC and ANCOM-BC2 for omics differential abundance testing. Bioinformatics 42(10):btag767 (2026). doi:10.1093/bioinformatics/btag767