# Tutorial on Data Sanity and Integrity Checks

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

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

#### January 08, 2025

    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::makeTreeSummarizedExperimentFromPhyloseq(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 Under development (unstable) (2024-11-20 r87352)
    Platform: aarch64-apple-darwin20
    Running under: macOS Ventura 13.7.1

    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.0

    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     rngtools_1.5.2  foreach_1.5.2   DT_0.33
     [5] lubridate_1.9.4 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4
     [9] purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1
    [13] ggplot2_3.5.1   tidyverse_2.0.0 ANCOMBC_2.9.1

    loaded via a namespace (and not attached):
      [1] splines_4.5.0                   cellranger_1.1.0
      [3] DirichletMultinomial_1.49.0     rpart_4.1.24
      [5] lifecycle_1.0.4                 Rdpack_2.6.2
      [7] doParallel_1.0.17               lattice_0.22-6
      [9] MASS_7.3-64                     MultiAssayExperiment_1.33.4
     [11] crosstalk_1.2.1                 backports_1.5.0
     [13] magrittr_2.0.3                  Hmisc_5.2-1
     [15] sass_0.4.9                      rmarkdown_2.29
     [17] jquerylib_0.1.4                 yaml_2.3.10
     [19] gld_2.6.6                       DBI_1.2.3
     [21] minqa_1.2.8                     ade4_1.7-22
     [23] multcomp_1.4-26                 abind_1.4-8
     [25] expm_1.0-0                      Rtsne_0.17
     [27] GenomicRanges_1.59.1            BiocGenerics_0.53.3
     [29] phyloseq_1.51.0                 yulab.utils_0.1.9
     [31] nnet_7.3-20                     TH.data_1.1-2
     [33] sandwich_3.1-1                  GenomeInfoDbData_1.2.13
     [35] IRanges_2.41.2                  S4Vectors_0.45.2
     [37] ggrepel_0.9.6                   irlba_2.3.5.1
     [39] tidytree_0.4.6                  vegan_2.6-8
     [41] rbiom_1.0.3                     microbiome_1.29.0
     [43] DelayedMatrixStats_1.29.0       permute_0.9-7
     [45] codetools_0.2-20                DelayedArray_0.33.3
     [47] scuttle_1.17.0                  energy_1.7-12
     [49] tidyselect_1.2.1                UCSC.utils_1.3.0
     [51] farver_2.1.2                    viridis_0.6.5
     [53] ScaledMatrix_1.15.0             lme4_1.1-35.5
     [55] gmp_0.7-5                       matrixStats_1.5.0
     [57] stats4_4.5.0                    base64enc_0.1-3
     [59] jsonlite_1.8.9                  BiocNeighbors_2.1.2
     [61] multtest_2.63.0                 e1071_1.7-16
     [63] decontam_1.27.0                 mia_1.15.6
     [65] Formula_1.2-5                   survival_3.8-3
     [67] scater_1.35.0                   iterators_1.0.14
     [69] tools_4.5.0                     treeio_1.31.0
     [71] DescTools_0.99.58               Rcpp_1.0.13-1
     [73] glue_1.8.0                      gridExtra_2.3
     [75] SparseArray_1.7.2               xfun_0.50
     [77] mgcv_1.9-1                      MatrixGenerics_1.19.0
     [79] GenomeInfoDb_1.43.2             TreeSummarizedExperiment_2.15.0
     [81] withr_3.0.2                     numDeriv_2016.8-1.1
     [83] fastmap_1.2.0                   bluster_1.17.0
     [85] boot_1.3-31                     rhdf5filters_1.19.0
     [87] rsvd_1.0.5                      digest_0.6.37
     [89] timechange_0.3.0                R6_2.5.1
     [91] colorspace_2.1-1                lpSolve_5.6.23
     [93] gtools_3.9.5                    generics_0.1.3
     [95] DECIPHER_3.3.2                  data.table_1.16.4
     [97] class_7.3-23                    CVXR_1.0-15
     [99] httr_1.4.7                      htmlwidgets_1.6.4
    [101] S4Arrays_1.7.1                  pkgconfig_2.0.3
    [103] gtable_0.3.6                    Exact_3.3
    [105] Rmpfr_1.0-0                     SingleCellExperiment_1.29.1
    [107] XVector_0.47.2                  htmltools_0.5.8.1
    [109] biomformat_1.35.0               scales_1.3.0
    [111] Biobase_2.67.0                  lmom_3.2
    [113] knitr_1.49                      rstudioapi_0.17.1
    [115] tzdb_0.4.0                      reshape2_1.4.4
    [117] checkmate_2.3.2                 nlme_3.1-166
    [119] nloptr_2.1.1                    proxy_0.4-27
    [121] cachem_1.1.0                    zoo_1.8-12
    [123] rhdf5_2.51.2                    rootSolve_1.8.2.4
    [125] vipor_0.4.7                     parallel_4.5.0
    [127] foreign_0.8-87                  pillar_1.10.1
    [129] grid_4.5.0                      vctrs_0.6.5
    [131] slam_0.1-55                     BiocSingular_1.23.0
    [133] beachmat_2.23.6                 cluster_2.1.8
    [135] beeswarm_0.4.0                  htmlTable_2.4.3
    [137] evaluate_1.0.1                  mvtnorm_1.3-2
    [139] cli_3.6.3                       compiler_4.5.0
    [141] rlang_1.1.4                     crayon_1.5.3
    [143] labeling_0.4.3                  mediation_4.5.0
    [145] ggbeeswarm_0.7.2                plyr_1.8.9
    [147] fs_1.6.5                        stringi_1.8.4
    [149] viridisLite_0.4.2               BiocParallel_1.41.0
    [151] lmerTest_3.1-3                  munsell_0.5.1
    [153] Biostrings_2.75.3               gsl_2.1-8
    [155] lazyeval_0.2.2                  Matrix_1.7-1
    [157] hms_1.1.3                       sparseMatrixStats_1.19.0
    [159] bit64_4.5.2                     Rhdf5lib_1.29.0
    [161] SummarizedExperiment_1.37.0     haven_2.5.4
    [163] rbibutils_2.3                   igraph_2.1.3
    [165] RcppParallel_5.1.9              bslib_0.8.0
    [167] bit_4.5.0.1                     readxl_1.4.3
    [169] 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.