Author: Menglu Liang^1, Huang Lin^1
Date: 2026-04-28

# Tutorial on Data Sanity and Integrity Checks

#### Menglu Liang\(^1\), Huang Lin\(^1\)

#### \(^1\)University of Maryland, College Park, MD 20742

#### April 28, 2026

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

# 1. Introduction

The `data_sanity_check` function performs essential validations on the input data to ensure its integrity before further processing. It verifies data types, confirms the structure of the input data, and checks for consistency between sample names in the metadata and the feature table, safeguarding against common data input errors.

# 2. Installation

Download package.

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

Load the package.

    library(ANCOMBC)

# 3. Examples

## 3.1 Import a `phyloseq` object

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.

    data(atlas1006, package = "microbiome")

    atlas1006

    phyloseq-class experiment-level object
    otu_table()   OTU Table:         [ 130 taxa and 1151 samples ]
    sample_data() Sample Data:       [ 1151 samples by 10 sample variables ]
    tax_table()   Taxonomy Table:    [ 130 taxa by 3 taxonomic ranks ]

List the taxonomic levels available for data aggregation.

    phyloseq::rank_names(atlas1006)

    [1] "Phylum" "Family" "Genus"

List the variables available in the sample metadata.

    colnames(microbiome::meta(atlas1006))

     [1] "age"                   "sex"                   "nationality"
     [4] "DNA_extraction_method" "project"               "diversity"
     [7] "bmi_group"             "subject"               "time"
    [10] "sample"

Data sanity and integrity check.

    # With `group` variable
    check_results = data_sanity_check(data = atlas1006,
                                      tax_level = "Family",
                                      fix_formula = "age + sex + bmi_group",
                                      group = "bmi_group",
                                      struc_zero = TRUE,
                                      global = TRUE,
                                      verbose = TRUE)

    Checking the input data type ...

    The input data is of type: phyloseq

    PASS

    Checking the sample metadata ...

    The specified variables in the formula: age, sex, bmi_group

    The available variables in the sample metadata: age, sex, nationality, DNA_extraction_method, project, diversity, bmi_group, subject, time, sample

    PASS

    Checking other arguments ...

    The number of groups of interest is: 6

    The sample size per group is: underweight = 21, lean = 484, overweight = 197, obese = 222, severeobese = 99, morbidobese = 22

    PASS

    # Without `group` variable
    check_results = data_sanity_check(data = atlas1006,
                                      tax_level = "Family",
                                      fix_formula = "age + sex + bmi_group",
                                      group = NULL,
                                      struc_zero = FALSE,
                                      global = FALSE,
                                      verbose = TRUE)

    Checking the input data type ...

    The input data is of type: phyloseq

    PASS

    Checking the sample metadata ...

    The specified variables in the formula: age, sex, bmi_group

    The available variables in the sample metadata: age, sex, nationality, DNA_extraction_method, project, diversity, bmi_group, subject, time, sample

    PASS

    Checking other arguments ...

    PASS

## 3.2 Import a `tse` object

    tse = mia::convertFromPhyloseq(atlas1006)

List the taxonomic levels available for data aggregation.

    mia::taxonomyRanks(tse)

    [1] "Phylum" "Family" "Genus"

List the variables available in the sample metadata.

    colnames(SummarizedExperiment::colData(tse))

     [1] "age"                   "sex"                   "nationality"
     [4] "DNA_extraction_method" "project"               "diversity"
     [7] "bmi_group"             "subject"               "time"
    [10] "sample"

Data sanity and integrity check.

    check_results = data_sanity_check(data = tse,
                                      assay_name = "counts",
                                      tax_level = "Family",
                                      fix_formula = "age + sex + bmi_group",
                                      group = "bmi_group",
                                      struc_zero = TRUE,
                                      global = TRUE,
                                      verbose = TRUE)

    Checking the input data type ...

    The input data is of type: TreeSummarizedExperiment

    PASS

    Checking the sample metadata ...

    The specified variables in the formula: age, sex, bmi_group

    The available variables in the sample metadata: age, sex, nationality, DNA_extraction_method, project, diversity, bmi_group, subject, time, sample

    PASS

    Checking other arguments ...

    The number of groups of interest is: 6

    The sample size per group is: underweight = 21, lean = 484, overweight = 197, obese = 222, severeobese = 99, morbidobese = 22

    PASS

## 3.3 Import a `matrix` or `data.frame`

Both abundance data and sample metadata are required for this import method.

Note that aggregating taxa to higher taxonomic levels is not supported in this method. Ensure that the data is already aggregated to the desired taxonomic level before proceeding. If aggregation is needed, consider creating a `phyloseq` or `tse` object for importing.

    abundance_data = microbiome::abundances(atlas1006)
    meta_data = microbiome::meta(atlas1006)

Ensure that the `rownames` of the metadata correspond to the `colnames` of the abundance data.

    all(rownames(meta_data) %in% colnames(abundance_data))

    [1] TRUE

List the variables available in the sample metadata.

    colnames(meta_data)

     [1] "age"                   "sex"                   "nationality"
     [4] "DNA_extraction_method" "project"               "diversity"
     [7] "bmi_group"             "subject"               "time"
    [10] "sample"

Data sanity and integrity check.

    check_results = data_sanity_check(data = abundance_data,
                                      assay_name = "counts",
                                      tax_level = "Family",
                                      meta_data = meta_data,
                                      fix_formula = "age + sex + bmi_group",
                                      group = "bmi_group",
                                      struc_zero = TRUE,
                                      global = TRUE,
                                      verbose = TRUE)

    Checking the input data type ...

    The input data is of type: matrix

    The imported data is in a generic 'matrix'/'data.frame' format.

    PASS

    Checking the sample metadata ...

    The specified variables in the formula: age, sex, bmi_group

    The available variables in the sample metadata: age, sex, nationality, DNA_extraction_method, project, diversity, bmi_group, subject, time, sample

    PASS

    Checking other arguments ...

    The number of groups of interest is: 6

    The sample size per group is: underweight = 21, lean = 484, overweight = 197, obese = 222, severeobese = 99, morbidobese = 22

    PASS

# Session information

    sessionInfo()

    R version 4.6.0 Patched (2026-04-24 r89963)
    Platform: aarch64-apple-darwin23
    Running under: macOS Tahoe 26.3.1

    Matrix products: default
    BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

    locale:
    [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    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   DT_0.34.0
     [5] lubridate_1.9.5 forcats_1.0.1   stringr_1.6.0   dplyr_1.2.1
     [9] purrr_1.2.2     readr_2.2.0     tidyr_1.3.2     tibble_3.3.1
    [13] ggplot2_4.0.3   tidyverse_2.0.0 ANCOMBC_2.14.0

    loaded via a namespace (and not attached):
      [1] splines_4.6.0                   cellranger_1.1.0
      [3] DirichletMultinomial_1.54.0     rpart_4.1.27
      [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.38.0
     [11] crosstalk_1.2.2                 backports_1.5.1
     [13] magrittr_2.0.5                  Hmisc_5.2-5
     [15] sass_0.4.10                     rmarkdown_2.31
     [17] jquerylib_0.1.4                 yaml_2.3.12
     [19] otel_0.2.0                      gld_2.6.8
     [21] DBI_1.3.0                       minqa_1.2.8
     [23] RColorBrewer_1.1-3              ade4_1.7-24
     [25] multcomp_1.4-30                 abind_1.4-8
     [27] expm_1.0-0                      Rtsne_0.17
     [29] quadprog_1.5-8                  GenomicRanges_1.64.0
     [31] BiocGenerics_0.58.0             phyloseq_1.56.0
     [33] yulab.utils_0.2.4               nnet_7.3-20
     [35] TH.data_1.1-5                   rappdirs_0.3.4
     [37] sandwich_3.1-1                  IRanges_2.46.0
     [39] S4Vectors_0.50.0                ggrepel_0.9.8
     [41] irlba_2.3.7                     tidytree_0.4.7
     [43] vegan_2.7-3                     microbiome_1.34.0
     [45] DelayedMatrixStats_1.34.0       permute_0.9-10
     [47] codetools_0.2-20                DelayedArray_0.38.0
     [49] scuttle_1.22.0                  energy_1.7-12
     [51] tidyselect_1.2.1                farver_2.1.2
     [53] viridis_0.6.5                   ScaledMatrix_1.20.0
     [55] lme4_2.0-1                      matrixStats_1.5.0
     [57] stats4_4.6.0                    base64enc_0.1-6
     [59] Seqinfo_1.2.0                   jsonlite_2.0.0
     [61] BiocNeighbors_2.6.0             multtest_2.68.0
     [63] e1071_1.7-17                    decontam_1.32.0
     [65] mia_1.20.0                      Formula_1.2-5
     [67] survival_3.8-6                  scater_1.40.0
     [69] iterators_1.0.14                tools_4.6.0
     [71] treeio_1.36.0                   DescTools_0.99.60
     [73] Rcpp_1.1.1-1.1                  glue_1.8.1
     [75] gridExtra_2.3                   SparseArray_1.12.0
     [77] xfun_0.57                       mgcv_1.9-4
     [79] MatrixGenerics_1.24.0           TreeSummarizedExperiment_2.20.0
     [81] withr_3.0.2                     numDeriv_2016.8-1.1
     [83] fastmap_1.2.0                   bluster_1.22.0
     [85] boot_1.3-32                     rsvd_1.0.5
     [87] digest_0.6.39                   timechange_0.4.0
     [89] R6_2.6.1                        colorspace_2.1-2
     [91] gtools_3.9.5                    dichromat_2.0-0.1
     [93] ecodive_2.2.6                   generics_0.1.4
     [95] DECIPHER_3.8.0                  data.table_1.18.2.1
     [97] class_7.3-23                    httr_1.4.8
     [99] htmlwidgets_1.6.4               S4Arrays_1.12.0
    [101] pkgconfig_2.0.3                 gtable_0.3.6
    [103] Exact_3.3                       S7_0.2.2
    [105] SingleCellExperiment_1.34.0     XVector_0.52.0
    [107] htmltools_0.5.9                 biomformat_1.40.0
    [109] scales_1.4.0                    Biobase_2.72.0
    [111] lmom_3.3                        reformulas_0.4.4
    [113] knitr_1.51                      rstudioapi_0.18.0
    [115] tzdb_0.5.0                      reshape2_1.4.5
    [117] checkmate_2.3.4                 nlme_3.1-169
    [119] nloptr_2.2.1                    proxy_0.4-29
    [121] cachem_1.1.0                    zoo_1.8-15
    [123] rootSolve_1.8.2.4               vipor_0.4.7
    [125] parallel_4.6.0                  foreign_0.8-91
    [127] pillar_1.11.1                   grid_4.6.0
    [129] vctrs_0.7.3                     BiocSingular_1.28.0
    [131] beachmat_2.28.0                 cluster_2.1.8.2
    [133] beeswarm_0.4.0                  htmlTable_2.5.0
    [135] evaluate_1.0.5                  mvtnorm_1.3-7
    [137] cli_3.6.6                       compiler_4.6.0
    [139] rlang_1.2.0                     crayon_1.5.3
    [141] labeling_0.4.3                  ggbeeswarm_0.7.3
    [143] plyr_1.8.9                      fs_2.1.0
    [145] stringi_1.8.7                   viridisLite_0.4.3
    [147] BiocParallel_1.46.0             lmerTest_3.2-1
    [149] Biostrings_2.80.0               gsl_2.1-9
    [151] lazyeval_0.2.3                  Matrix_1.7-5
    [153] hms_1.1.4                       sparseMatrixStats_1.24.0
    [155] SummarizedExperiment_1.42.0     haven_2.5.5
    [157] rbibutils_2.4.1                 igraph_2.3.0
    [159] bslib_0.10.0                    readxl_1.4.5
    [161] ape_5.8-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, et al. 2017. “Tools for Microbiome Analysis in r.” Version 1: 10013.
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.