# animalcules

Yue Zhao1,2, Anthony Federico1,2 and Evan Johnson1,2

1Boston University School of Medicine, Boston, MA
2Bioinformatics Program, Boston University, Boston, MA

#### March 16, 2020

#### Package

animalcules 1.2.1

# Contents

# 1 Introduction

animalcules is an R package for utilizing up-to-date data analytics, visualization methods, and machine learning models to provide users an easy-to-use interactive microbiome analysis framework. It can be used as a standalone software package or users can explore their data with the accompanying interactive R Shiny application. Traditional microbiome analysis such as alpha/beta diversity and differential abundance analysis are enhanced, while new methods like biomarker identification are introduced by animalcules. Powerful interactive and dynamic figures generated by animalcules enable users to understand their data better and discover new insights.

# 2 Installation

Install the development version of the package from Bioconductor.

    if (!requireNamespace("BiocManager", quietly=TRUE))
      install.packages("BiocManager")
    BiocManager::install("compbiomed/animalcules")

Or install the development version of the package from Github.

    if (!requireNamespace("devtools", quietly=TRUE))
      install.packages("devtools")
    devtools::install_github("compbiomed/animalcules")

# 3 Load Packages

Load the packages needed into R session.

    library(animalcules)
    library(SummarizedExperiment)

# 4 Run Shiny App

This command is to start the animalcules shiny app. For shiny app tutorials, please go to our website, and click the tab “Interactive Shiny Analysis”.

The rest of the vignette is for the command line version of animalcules, which could also be found in our website, and click the tab “Command Line Analysis”.

    run_animalcules()

# 5 Load Toy Dataset

This toy dataset contains 50 simulated samples. Throughout the vignette, we will use the data structure MultiAssayExperiment (MAE) as the input to all functions.

    data_dir = system.file("extdata/MAE.rds", package = "animalcules")
    MAE = readRDS(data_dir)

For users who would like to use their own dataset, please follow the following steps:

- Use the shiny app to upload your dataset. check shiny tutorial
- Go to tab 2 “Data Summary and Filtering”, and click the button “Download Animalcules File”. check shiny tutorial
- Load the downloaded .rds animalcules file and rename it as MAE in R.

    data_dir = "PATH_TO_THE_ANIMALCULES_FILE"
    MAE = readRDS(data_dir)

# 6 Summary and Categorize

One of the first steps in the data analysis process involves summarizing the data and looking for outliers or other obvious data issues that may cause issue with downstream analysis.

## 6.1 Summary Plot (Pie Chart or Box plot)

One type of summarization plot returns either a box plot or pie chart for continous or categorical data respectively.

    p <- filter_summary_pie_box(MAE,
                                samples_discard = c("subject_2", "subject_4"),
                                filter_type = "By Metadata",
                                sample_condition = "AGE")
    p

## 6.2 Summary Plot (Density Plot or Bar Plot)

One type of summarization plot returns either a density plot or bar plot for continous or categorical data respectively.

    p <- filter_summary_bar_density(MAE,
                                    samples_discard = c("subject_2", "subject_4"),
                                    filter_type = "By Metadata",
                                    sample_condition = "SEX")
    p

## 6.3 Categorize

It is often necessary to bin continuous data into categories when performing analyses that require categorical input. To help ease this process, users can automatically categorize categorical data and provide custom bin breaks and labels in doing so.

    microbe <- MAE[['MicrobeGenetics']]
    samples <- as.data.frame(colData(microbe))
    result <- filter_categorize(samples,
                                sample_condition="AGE",
                                new_label="AGE_GROUP",
                                bin_breaks=c(0,55,75,100),
                                bin_labels=c('Young','Adult',"Elderly"))
    head(result$sam_table)

    #           AGE    SEX  DISEASE GROUP AGE_GROUP
    # subject_1  34 Female positive     A     Young
    # subject_2  61   Male positive     A     Adult
    # subject_3  62   Male positive     A     Adult
    # subject_4  95 Female positive     B   Elderly
    # subject_5  30 Female positive     A     Young
    # subject_6  80 Female positive     B   Elderly

    result$plot.unbinned

    result$plot.binned

# 7 Visualization

A typical analysis involves visualization of microbe abundances across samples or groups of samples. Animalcules implements three common types of visualization plots including stacked bar plots, heat map, and box plots generated with Plotly.

## 7.1 Relative Abundance Stacked Bar Plot

The stacked bar plots are used to visualize the relative abundance of microbes at a given taxonomical level in each sample represented as a single bar.

    p <- relabu_barplot(MAE,
                        tax_level="family",
                        order_organisms=c('Retroviridae'),
                        sort_by="organisms",
                        sample_conditions=c('SEX', 'AGE'),
                        show_legend=TRUE)
    p

## 7.2 Relative Abundance Heatmap

The heatmap represents a sample by organisms matrix that can be visualized at different taxonomic levels.

    p <- relabu_heatmap(MAE,
                       tax_level="genus",
                       sort_by="conditions",
                       sample_conditions=c("SEX", "AGE"))
    p

## 7.3 Relative Abundance Boxplot

The boxplot visualization allows users to compare the abundance of one or more organisms between categorical attributes.

    p <- relabu_boxplot(MAE,
                        tax_level="genus",
                        organisms=c("Escherichia", "Actinomyces"),
                        condition="SEX",
                        datatype="logcpm")
    p

    # Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

    # Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

    # Warning: 'layout' objects don't have these attributes: 'boxmode'
    # Valid attributes include:
    # 'font', 'title', 'uniformtext', 'autosize', 'width', 'height', 'margin', 'paper_bgcolor', 'plot_bgcolor', 'separators', 'hidesources', 'showlegend', 'colorway', 'datarevision', 'uirevision', 'editrevision', 'selectionrevision', 'template', 'modebar', 'meta', 'transition', '_deprecated', 'clickmode', 'dragmode', 'hovermode', 'hoverdistance', 'spikedistance', 'hoverlabel', 'selectdirection', 'grid', 'calendar', 'xaxis', 'yaxis', 'ternary', 'scene', 'geo', 'mapbox', 'polar', 'radialaxis', 'angularaxis', 'direction', 'orientation', 'editType', 'legend', 'annotations', 'shapes', 'images', 'updatemenus', 'sliders', 'colorscale', 'coloraxis', 'metasrc', 'barmode', 'bargap', 'mapType'

# 8 Diversity

## 8.1 Alpha Diversity Boxplot

Alpha diversity, which describes the richness and evenness of sample microbial community, is a vital indicator in the microbiome analysis. animalcules provides the interactive boxplot comparison of alpha diversity between selected groups of samples. Both taxonomy levels and alpha diversity metrics (Shannon, Gini Simpson, Inverse Simpson) can be changed. Users can also conduct alpha diversity statistical tests including Wilcoxon rank sum test, T test and Kruskal-Wallis test.

Plot the alpha diversity boxplot between the levels in selected condition.

    alpha_div_boxplot(MAE = MAE,
                      tax_level = "genus",
                      condition = "DISEASE",
                      alpha_metric = "shannon")

## 8.2 Alpha Diversity Statistical Test

Conduct statistical test on the alpha diversity between the levels in selected condition.

    do_alpha_div_test(MAE = MAE,
                      tax_level = "genus",
                      condition = "DISEASE",
                      alpha_metric = "shannon",
                      alpha_stat = "T-test")

    #         Wilcoxon rank sum test Welch Two Sample t-test
    # P-value              0.3267236               0.4235008

## 8.3 Beta Diversity Heatmap

On the other hand, by defining distances between each sample, beta diversity is another key metric to look at. Users can plot the beta diversity heatmap by selecting different beta diversity dissimilarity metrics including Bray-Curtis and Jaccard. Users can also conduct beta diversity statistical testing between groups including PERMANOVA, Wilcoxon rank sum test and Kruskal-Wallis.

Plot the beta diversity heatmap with selected condition.

    diversity_beta_heatmap(MAE = MAE,
                           tax_level = 'genus',
                           input_beta_method = "bray",
                           input_bdhm_select_conditions = 'DISEASE',
                           input_bdhm_sort_by = 'condition')

## 8.4 Beta Diversity Boxplot

Plot the beta diversity boxplot within and between conditions.

    diversity_beta_boxplot(MAE = MAE,
                           tax_level = 'genus',
                           input_beta_method = "bray",
                           input_select_beta_condition = 'DISEASE')

## 8.5 Beta Diversity Test

Conduct statistical test on the beta diversity between the levels in selected condition.

    diversity_beta_test(MAE = MAE,
                        tax_level = 'genus',
                        input_beta_method = "bray",
                        input_select_beta_condition =  'DISEASE',
                        input_select_beta_stat_method = 'PERMANOVA',
                        input_num_permutation_permanova = 999)

    # Permutation test for adonis under reduced model
    # Terms added sequentially (first to last)
    # Permutation: free
    # Number of permutations: 999
    #
    # vegan::adonis2(formula = dist.mat ~ condition, data = sam_table, permutations = input_num_permutation_permanova, strata = "PLOT")
    #           Df SumOfSqs      R2      F Pr(>F)
    # condition  1   0.2286 0.01583 0.7722  0.621
    # Residual  48  14.2108 0.98417
    # Total     49  14.4394 1.00000

# 9 Dimensionality Reduction

## 9.1 PCA

A wrapper for conduction 2D and 3D Principal Component Analysis.

    result <- dimred_pca(MAE,
                         tax_level="genus",
                         color="AGE",
                         shape="DISEASE",
                         pcx=1,
                         pcy=2,
                         datatype="logcpm")
    result$plot

    head(result$table)

    #      PC Standard Deviation Variance Explained Cumulative Variance
    # PC1 PC1               2.44             9.622%                 10%
    # PC2 PC2               2.17             7.598%                 17%
    # PC3 PC3               1.98             6.317%                 24%
    # PC4 PC4               1.88             5.688%                 29%
    # PC5 PC5                1.8             5.247%                 34%
    # PC6 PC6               1.69             4.620%                 39%

## 9.2 PCoA

A wrapper for conduction 2D and 3D Principal Coordinate Analysis.

    result <- dimred_pcoa(MAE,
                          tax_level="genus",
                          color="AGE",
                          shape="DISEASE",
                          axx=1,
                          axy=2,
                          method="bray")
    result$plot

    head(result$table)

    #          Axis Eigenvalue Variance Explained Cumulative Variance
    # Axis.1 Axis.1      3.640                20%                 20%
    # Axis.2 Axis.2      2.860                16%                 35%
    # Axis.3 Axis.3      1.720                10%                 45%
    # Axis.4 Axis.4      0.861                 5%                 50%
    # Axis.5 Axis.5      0.846                 5%                 55%
    # Axis.6 Axis.6      0.709                 4%                 59%

## 9.3 UMAP

A wrapper for conduction 2D and 3D Uniform Manifold Approximation and Projection.

    result <- dimred_umap(MAE,
                          tax_level="genus",
                          color="AGE",
                          shape="DISEASE",
                          cx=1,
                          cy=2,
                          n_neighbors=15,
                          metric="euclidean",
                          datatype="logcpm")
    result$plot

## 9.4 t-SNE

A wrapper for conduction 2D and 3D t-distributed stochastic neighbor embedding.

    result <- dimred_tsne(MAE,
                          tax_level="phylum",
                          color="AGE",
                          shape="GROUP",
                          k="3D",
                          initial_dims=30,
                          perplexity=10,
                          datatype="logcpm")
    result$plot

# 10 Differential Analysis

Here in animalcules, we provide a DESeq2-based differential abundance analysis. Users can choose the target variable, covariate variable, taxonomy level, minimum count cut-off, and an adjusted p-value threshold. The analysis report will output not only the adjusted p-value and log2-fold-change of the microbes, but also the percentage, prevalence, and the group size-adjusted fold change.

    p <- differential_abundance(MAE,
                                tax_level="phylum",
                                input_da_condition=c("DISEASE"),
                                min_num_filter = 2,
                                input_da_padj_cutoff = 0.5)
    p

    #      [,1]
    # [1,] "No differentially abundant items found!"

# 11 Biomarker

One unique feature of animalcules is the biomarker identification module built on machine learning models. Users can choose one classification model from logistic regression, gradient boosting machine, or random forest to identify a microbes biomarker list. The feature importance score for each microbe will be provided. To evaluate the biomarker performance, the ROC plot and the AUC value using cross-validation outputs are shown to users. Note: Results may vary each run.

## 11.1 Train biomarker

    p <- find_biomarker(MAE,
                        tax_level = "genus",
                        input_select_target_biomarker = c("SEX"),
                        nfolds = 3,
                        nrepeats = 3,
                        seed = 99,
                        percent_top_biomarker = 0.2,
                        model_name = "logistic regression")
    # biomarker
    p$biomarker

    #         biomarker_list
    # 1          Xanthomonas
    # 2           Tannerella
    # 3          Aspergillus
    # 4  Alphapapillomavirus
    # 5          Escherichia
    # 6          Veillonella
    # 7           Malassezia
    # 8            Neisseria
    # 9          Haemophilus
    # 10         Cupriavidus

    # importance plot
    p$importance_plot

    # ROC plot
    p$roc_plot

# 12 Session Info

    sessionInfo()

    # R version 3.6.3 (2020-02-29)
    # Platform: x86_64-apple-darwin15.6.0 (64-bit)
    # Running under: OS X El Capitan 10.11.6
    #
    # Matrix products: default
    # BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
    # LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
    #
    # locale:
    # [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    #
    # attached base packages:
    #  [1] grid      parallel  stats4    stats     graphics  grDevices utils
    #  [8] datasets  methods   base
    #
    # other attached packages:
    #  [1] DMwR_0.4.1                  caret_6.0-85
    #  [3] ggplot2_3.3.0               lattice_0.20-40
    #  [5] SummarizedExperiment_1.16.1 DelayedArray_0.12.2
    #  [7] BiocParallel_1.20.1         matrixStats_0.56.0
    #  [9] Biobase_2.46.0              GenomicRanges_1.38.0
    # [11] GenomeInfoDb_1.22.0         IRanges_2.20.2
    # [13] S4Vectors_0.24.3            BiocGenerics_0.32.0
    # [15] animalcules_1.2.1           testthat_2.3.2
    # [17] BiocStyle_2.14.4
    #
    # loaded via a namespace (and not attached):
    #   [1] reticulate_1.14             tidyselect_1.0.0
    #   [3] RSQLite_2.2.0               AnnotationDbi_1.48.0
    #   [5] htmlwidgets_1.5.1           covr_3.5.0
    #   [7] pROC_1.16.1                 devtools_2.2.2
    #   [9] munsell_0.5.0               codetools_0.2-16
    #  [11] umap_0.2.5.0                DT_0.12
    #  [13] rentrez_1.2.2               withr_2.1.2
    #  [15] colorspace_1.4-1            knitr_1.28
    #  [17] rstudioapi_0.11             ROCR_1.0-7
    #  [19] TTR_0.23-6                  labeling_0.3
    #  [21] GenomeInfoDbData_1.2.2      plotROC_2.2.1
    #  [23] farver_2.0.3                bit64_0.9-7
    #  [25] rhdf5_2.30.1                rprojroot_1.3-2
    #  [27] vctrs_0.2.4                 generics_0.0.2
    #  [29] ipred_0.9-9                 xfun_0.12
    #  [31] R6_2.4.1                    locfit_1.5-9.1
    #  [33] rex_1.1.2                   bitops_1.0-6
    #  [35] assertthat_0.2.1            promises_1.1.0
    #  [37] scales_1.1.0                nnet_7.3-13
    #  [39] gtable_0.3.0                Cairo_1.5-11
    #  [41] processx_3.4.2              timeDate_3043.102
    #  [43] rlang_0.4.5                 genefilter_1.68.0
    #  [45] splines_3.6.3               lazyeval_0.2.2
    #  [47] ModelMetrics_1.2.2.1        acepack_1.4.1
    #  [49] checkmate_2.0.0             BiocManager_1.30.10
    #  [51] yaml_2.2.1                  reshape2_1.4.3
    #  [53] abind_1.4-5                 crosstalk_1.1.0.1
    #  [55] backports_1.1.5             httpuv_1.5.2
    #  [57] quantmod_0.4-16             Hmisc_4.3-1
    #  [59] tools_3.6.3                 lava_1.6.7
    #  [61] usethis_1.5.1               bookdown_0.18
    #  [63] ellipsis_0.3.0              gplots_3.0.3
    #  [65] biomformat_1.14.0           RColorBrewer_1.1-2
    #  [67] sessioninfo_1.1.1           MultiAssayExperiment_1.12.4
    #  [69] Rcpp_1.0.3                  plyr_1.8.6
    #  [71] base64enc_0.1-3             zlibbioc_1.32.0
    #  [73] purrr_0.3.3                 RCurl_1.98-1.1
    #  [75] ps_1.3.2                    prettyunits_1.1.1
    #  [77] openssl_1.4.1               rpart_4.1-15
    #  [79] zoo_1.8-7                   cluster_2.1.0
    #  [81] fs_1.3.2                    magrittr_1.5
    #  [83] magick_2.3                  RSpectra_0.16-0
    #  [85] data.table_1.12.8           pkgload_1.0.2
    #  [87] shinyjs_1.1                 mime_0.9
    #  [89] reactable_0.1.0.1           evaluate_0.14
    #  [91] xtable_1.8-4                XML_3.99-0.3
    #  [93] jpeg_0.1-8.1                gridExtra_2.3
    #  [95] shape_1.4.4                 compiler_3.6.3
    #  [97] tibble_2.1.3                KernSmooth_2.23-16
    #  [99] crayon_1.3.4                htmltools_0.4.0
    # [101] mgcv_1.8-31                 later_1.0.0
    # [103] Formula_1.2-3               tidyr_1.0.2
    # [105] geneplotter_1.64.0          lubridate_1.7.4
    # [107] DBI_1.1.0                   MASS_7.3-51.5
    # [109] Matrix_1.2-18               permute_0.9-5
    # [111] cli_2.0.2                   gdata_2.18.0
    # [113] gower_0.2.1                 forcats_0.5.0
    # [115] pkgconfig_2.0.3             foreign_0.8-76
    # [117] plotly_4.9.2                recipes_0.1.9
    # [119] foreach_1.4.8               annotate_1.64.0
    # [121] XVector_0.26.0              prodlim_2019.11.13
    # [123] stringr_1.4.0               callr_3.4.2
    # [125] digest_0.6.25               tsne_0.1-3
    # [127] vegan_2.5-6                 rmarkdown_2.1
    # [129] htmlTable_1.13.3            curl_4.3
    # [131] shiny_1.4.0.2               gtools_3.8.1
    # [133] lifecycle_0.2.0             nlme_3.1-145
    # [135] jsonlite_1.6.1              Rhdf5lib_1.8.0
    # [137] askpass_1.1                 desc_1.2.0
    # [139] viridisLite_0.3.0           limma_3.42.2
    # [141] fansi_0.4.1                 pillar_1.4.3
    # [143] fastmap_1.0.1               httr_1.4.1
    # [145] pkgbuild_1.0.6              survival_3.1-11
    # [147] glue_1.3.2                  xts_0.12-0
    # [149] remotes_2.1.1               png_0.1-7
    # [151] iterators_1.0.12            glmnet_3.0-2
    # [153] bit_1.1-15.2                class_7.3-15
    # [155] stringi_1.4.6               blob_1.2.1
    # [157] DESeq2_1.26.0               latticeExtra_0.6-29
    # [159] caTools_1.18.0              memoise_1.1.0
    # [161] dplyr_0.8.5                 ape_5.3