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

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.643
# 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")
# 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    Escherichia
# 2    Veillonella
# 3    Xanthomonas
# 4    Haemophilus
# 5      Neisseria
# 6    Cupriavidus
# 7     Malassezia
# importance plot
p$importance_plot

# ROC plot
p$roc_plot

12 Session Info

sessionInfo()
# R version 4.0.0 (2020-04-24)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 18.04.4 LTS
# 
# Matrix products: default
# BLAS:   /home/biocbuild/bbs-3.11-bioc/R/lib/libRblas.so
# LAPACK: /home/biocbuild/bbs-3.11-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.0               lattice_0.20-41            
#  [5] SummarizedExperiment_1.18.0 DelayedArray_0.14.0        
#  [7] matrixStats_0.56.0          Biobase_2.48.0             
#  [9] GenomicRanges_1.40.0        GenomeInfoDb_1.24.0        
# [11] IRanges_2.22.0              S4Vectors_0.26.0           
# [13] BiocGenerics_0.34.0         animalcules_1.4.0          
# [15] testthat_2.3.2              BiocStyle_2.16.0           
# 
# loaded via a namespace (and not attached):
#   [1] reticulate_1.15             tidyselect_1.0.0           
#   [3] RSQLite_2.2.0               AnnotationDbi_1.50.0       
#   [5] htmlwidgets_1.5.1           BiocParallel_1.22.0        
#   [7] covr_3.5.0                  pROC_1.16.2                
#   [9] devtools_2.3.0              munsell_0.5.0              
#  [11] codetools_0.2-16            DT_0.13                    
#  [13] umap_0.2.5.0                rentrez_1.2.2              
#  [15] withr_2.2.0                 colorspace_1.4-1           
#  [17] knitr_1.28                  rstudioapi_0.11            
#  [19] ROCR_1.0-7                  TTR_0.23-6                 
#  [21] labeling_0.3                GenomeInfoDbData_1.2.3     
#  [23] plotROC_2.2.1               bit64_0.9-7                
#  [25] farver_2.0.3                rhdf5_2.32.0               
#  [27] rprojroot_1.3-2             vctrs_0.2.4                
#  [29] generics_0.0.2              ipred_0.9-9                
#  [31] xfun_0.13                   R6_2.4.1                   
#  [33] locfit_1.5-9.4              rex_1.2.0                  
#  [35] bitops_1.0-6                assertthat_0.2.1           
#  [37] promises_1.1.0              scales_1.1.0               
#  [39] nnet_7.3-14                 gtable_0.3.0               
#  [41] Cairo_1.5-12                processx_3.4.2             
#  [43] timeDate_3043.102           rlang_0.4.5                
#  [45] genefilter_1.70.0           splines_4.0.0              
#  [47] lazyeval_0.2.2              ModelMetrics_1.2.2.2       
#  [49] BiocManager_1.30.10         yaml_2.2.1                 
#  [51] reshape2_1.4.4              abind_1.4-5                
#  [53] crosstalk_1.1.0.1           backports_1.1.6            
#  [55] httpuv_1.5.2                quantmod_0.4.17            
#  [57] tools_4.0.0                 lava_1.6.7                 
#  [59] usethis_1.6.0               bookdown_0.18              
#  [61] ellipsis_0.3.0              gplots_3.0.3               
#  [63] biomformat_1.16.0           RColorBrewer_1.1-2         
#  [65] sessioninfo_1.1.1           MultiAssayExperiment_1.14.0
#  [67] Rcpp_1.0.4.6                plyr_1.8.6                 
#  [69] zlibbioc_1.34.0             purrr_0.3.4                
#  [71] RCurl_1.98-1.2              ps_1.3.2                   
#  [73] prettyunits_1.1.1           rpart_4.1-15               
#  [75] openssl_1.4.1               zoo_1.8-7                  
#  [77] cluster_2.1.0               fs_1.4.1                   
#  [79] magrittr_1.5                magick_2.3                 
#  [81] data.table_1.12.8           RSpectra_0.16-0            
#  [83] pkgload_1.0.2               shinyjs_1.1                
#  [85] mime_0.9                    reactable_0.1.0.1          
#  [87] evaluate_0.14               xtable_1.8-4               
#  [89] XML_3.99-0.3                shape_1.4.4                
#  [91] compiler_4.0.0              tibble_3.0.1               
#  [93] KernSmooth_2.23-17          crayon_1.3.4               
#  [95] htmltools_0.4.0             mgcv_1.8-31                
#  [97] later_1.0.0                 tidyr_1.0.2                
#  [99] geneplotter_1.66.0          lubridate_1.7.8            
# [101] DBI_1.1.0                   MASS_7.3-51.6              
# [103] Matrix_1.2-18               permute_0.9-5              
# [105] cli_2.0.2                   gdata_2.18.0               
# [107] gower_0.2.1                 forcats_0.5.0              
# [109] pkgconfig_2.0.3             plotly_4.9.2.1             
# [111] recipes_0.1.10              foreach_1.5.0              
# [113] annotate_1.66.0             XVector_0.28.0             
# [115] prodlim_2019.11.13          stringr_1.4.0              
# [117] callr_3.4.3                 digest_0.6.25              
# [119] tsne_0.1-3                  vegan_2.5-6                
# [121] rmarkdown_2.1               curl_4.3                   
# [123] shiny_1.4.0.2               gtools_3.8.2               
# [125] lifecycle_0.2.0             nlme_3.1-147               
# [127] jsonlite_1.6.1              Rhdf5lib_1.10.0            
# [129] desc_1.2.0                  viridisLite_0.3.0          
# [131] askpass_1.1                 limma_3.44.0               
# [133] fansi_0.4.1                 pillar_1.4.3               
# [135] fastmap_1.0.1               httr_1.4.1                 
# [137] pkgbuild_1.0.7              survival_3.1-12            
# [139] glue_1.4.0                  xts_0.12-0                 
# [141] remotes_2.1.1               iterators_1.0.12           
# [143] glmnet_3.0-2                bit_1.1-15.2               
# [145] class_7.3-17                stringi_1.4.6              
# [147] blob_1.2.1                  DESeq2_1.28.0              
# [149] caTools_1.18.0              memoise_1.1.0              
# [151] dplyr_0.8.5                 ape_5.3