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:

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
# Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
# Please use `arrange()` instead.
# See vignette('programming') for more help
# This warning is displayed once every 8 hours.
# Call `lifecycle::last_warnings()` to see where this warning was generated.

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 exact 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.626
# 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%              9.622%
# PC2 PC2               2.17             7.598%             17.220%
# PC3 PC3               1.98             6.317%             23.537%
# PC4 PC4               1.88             5.688%             29.225%
# PC5 PC5                1.8             5.247%             34.472%
# PC6 PC6               1.69             4.620%             39.092%

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           19.6142%              19.61%
# Axis.2 Axis.2      2.860           15.5122%              35.13%
# Axis.3 Axis.3      1.720            9.5218%              44.65%
# Axis.4 Axis.4      0.861            5.0201%              49.67%
# Axis.5 Axis.5      0.846            4.9398%              54.61%
# Axis.6 Axis.6      0.709            4.2222%              58.83%

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")
# Warning in if (class(X) == "dist") {: the condition has length > 1 and only the
# first element will be used
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          Veillonella
# 2            Dialister
# 3          Escherichia
# 4          Hepacivirus
# 5            Neisseria
# 6          Aspergillus
# 7          Haemophilus
# 8       Staphylococcus
# 9        Lactobacillus
# 10         Xanthomonas
# 11 Alphapapillomavirus
# 12          Malassezia
# 13         Cupriavidus
# importance plot
p$importance_plot

# ROC plot
p$roc_plot

12 Session Info

sessionInfo()
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 18.04.5 LTS
# 
# Matrix products: default
# BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
# LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
# 
# locale:
#  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
#  [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       
# 
# 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-86               
#  [3] ggplot2_3.3.2               lattice_0.20-41            
#  [5] SummarizedExperiment_1.20.0 Biobase_2.50.0             
#  [7] GenomicRanges_1.42.0        GenomeInfoDb_1.26.0        
#  [9] IRanges_2.24.0              S4Vectors_0.28.0           
# [11] BiocGenerics_0.36.0         MatrixGenerics_1.2.0       
# [13] matrixStats_0.57.0          animalcules_1.6.0          
# [15] testthat_2.3.2              BiocStyle_2.18.0           
# 
# loaded via a namespace (and not attached):
#   [1] backports_1.1.10            plyr_1.8.6                 
#   [3] lazyeval_0.2.2              splines_4.0.3              
#   [5] crosstalk_1.1.0.1           BiocParallel_1.24.0        
#   [7] usethis_1.6.3               digest_0.6.27              
#   [9] foreach_1.5.1               htmltools_0.5.0            
#  [11] magick_2.5.0                fansi_0.4.1                
#  [13] magrittr_1.5                memoise_1.1.0              
#  [15] covr_3.5.1                  cluster_2.1.0              
#  [17] ROCR_1.0-11                 limma_3.46.0               
#  [19] remotes_2.2.0               recipes_0.1.14             
#  [21] annotate_1.68.0             gower_0.2.2                
#  [23] xts_0.12.1                  askpass_1.1                
#  [25] prettyunits_1.1.1           colorspace_1.4-1           
#  [27] blob_1.2.1                  xfun_0.18                  
#  [29] dplyr_1.0.2                 callr_3.5.1                
#  [31] crayon_1.3.4                RCurl_1.98-1.2             
#  [33] jsonlite_1.7.1              genefilter_1.72.0          
#  [35] zoo_1.8-8                   survival_3.2-7             
#  [37] iterators_1.0.13            ape_5.4-1                  
#  [39] glue_1.4.2                  gtable_0.3.0               
#  [41] ipred_0.9-9                 zlibbioc_1.36.0            
#  [43] XVector_0.30.0              DelayedArray_0.16.0        
#  [45] pkgbuild_1.1.0              Rhdf5lib_1.12.0            
#  [47] shape_1.4.5                 quantmod_0.4.17            
#  [49] rentrez_1.2.2               abind_1.4-5                
#  [51] scales_1.1.1                DBI_1.1.0                  
#  [53] Rcpp_1.0.5                  plotROC_2.2.1              
#  [55] viridisLite_0.3.0           xtable_1.8-4               
#  [57] reticulate_1.18             bit_4.0.4                  
#  [59] umap_0.2.6.0                DT_0.16                    
#  [61] tsne_0.1-3                  lava_1.6.8                 
#  [63] prodlim_2019.11.13          glmnet_4.0-2               
#  [65] htmlwidgets_1.5.2           rex_1.2.0                  
#  [67] httr_1.4.2                  RColorBrewer_1.1-2         
#  [69] ellipsis_0.3.1              farver_2.0.3               
#  [71] pkgconfig_2.0.3             XML_3.99-0.5               
#  [73] nnet_7.3-14                 locfit_1.5-9.4             
#  [75] labeling_0.4.2              tidyselect_1.1.0           
#  [77] rlang_0.4.8                 reshape2_1.4.4             
#  [79] later_1.1.0.1               AnnotationDbi_1.52.0       
#  [81] munsell_0.5.0               tools_4.0.3                
#  [83] cli_2.1.0                   generics_0.0.2             
#  [85] RSQLite_2.2.1               devtools_2.3.2             
#  [87] biomformat_1.18.0           evaluate_0.14              
#  [89] stringr_1.4.0               fastmap_1.0.1              
#  [91] yaml_2.2.1                  ModelMetrics_1.2.2.2       
#  [93] processx_3.4.4              knitr_1.30                 
#  [95] bit64_4.0.5                 fs_1.5.0                   
#  [97] purrr_0.3.4                 nlme_3.1-150               
#  [99] reactable_0.2.3             mime_0.9                   
# [101] compiler_4.0.3              rstudioapi_0.11            
# [103] curl_4.3                    plotly_4.9.2.1             
# [105] tibble_3.0.4                geneplotter_1.68.0         
# [107] stringi_1.5.3               ps_1.4.0                   
# [109] RSpectra_0.16-0             desc_1.2.0                 
# [111] forcats_0.5.0               Matrix_1.2-18              
# [113] shinyjs_2.0.0               vegan_2.5-6                
# [115] permute_0.9-5               vctrs_0.3.4                
# [117] rhdf5filters_1.2.0          pillar_1.4.6               
# [119] lifecycle_0.2.0             BiocManager_1.30.10        
# [121] data.table_1.13.2           bitops_1.0-6               
# [123] httpuv_1.5.4                R6_2.4.1                   
# [125] bookdown_0.21               promises_1.1.1             
# [127] sessioninfo_1.1.1           codetools_0.2-16           
# [129] MASS_7.3-53                 assertthat_0.2.1           
# [131] pkgload_1.1.0               rhdf5_2.34.0               
# [133] openssl_1.4.3               DESeq2_1.30.0              
# [135] rprojroot_1.3-2             withr_2.3.0                
# [137] GenomeInfoDbData_1.2.4      MultiAssayExperiment_1.16.0
# [139] mgcv_1.8-33                 rpart_4.1-15               
# [141] timeDate_3043.102           tidyr_1.1.2                
# [143] class_7.3-17                rmarkdown_2.5              
# [145] Cairo_1.5-12.2              TTR_0.24.2                 
# [147] pROC_1.16.2                 shiny_1.5.0                
# [149] lubridate_1.7.9