Citation

Note: if you use animalcules in published research, please cite:

Zhao, Y., Federico, A., Faits, T. et al. (2021) animalcules: interactive microbiome analytics and visualization in R Microbiome, 9(1), 1-16. 10.1186/s40168-021-01013-0

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.

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")

Load Packages

Load the packages needed into R session.

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

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/TB_example_dataset.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)

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.

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("SRR1204622"),
                            filter_type = "By Metadata",
                            sample_condition = "age_s")
p

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("SRR1204622"),
                                filter_type = "By Metadata",
                                sample_condition = "sex_s")
p

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_s",
                            new_label="AGE_GROUP",
                            bin_breaks=c(0,30,40,100),
                            bin_labels=c('a','b',"c"))
head(result$sam_table)
#            isolate_s Disease age_s sex_s   tissue_s AGE_GROUP
# SRR1204622 Patient 1      TB    45     F     Sputum         c
# SRR1204623 Patient 1      TB    45     F      Nasal         c
# SRR1204624 Patient 1      TB    45     F Oropharynx         c
# SRR1204625 Patient 2      TB    29     M     Sputum         a
# SRR1204626 Patient 2      TB    29     M      Nasal         a
# SRR1204627 Patient 2      TB    29     M Oropharynx         a
result$plot.unbinned
result$plot.binned

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.

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="genus",
                    sort_by="conditions",
                    sample_conditions=c('Disease'),
                    show_legend=TRUE)
p

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_s", "age_s"))
p

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("Streptococcus", "Staphylococcus"),
                    condition="sex_s",
                    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', 'computed', 'paper_bgcolor', 'plot_bgcolor', 'separators', 'hidesources', 'showlegend', 'colorway', 'datarevision', 'uirevision', 'editrevision', 'selectionrevision', 'template', 'modebar', 'newshape', 'activeshape', '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'

Diversity

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")

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.006682257             0.006483754

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')

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')

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)
#           Df SumOfSqs      R2     F Pr(>F)   
# condition  1   1.1942 0.13939 4.535  0.002 **
# Residual  28   7.3730 0.86061                
# Total     29   8.5671 1.00000                
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Dimensionality Reduction

PCA

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

result <- dimred_pca(MAE,
                     tax_level="genus",
                     color="age_s",
                     shape="Disease",
                     pcx=1,
                     pcy=2,
                     datatype="logcpm")
result$plot
head(result$table)
#      PC Standard Deviation Variance Explained Cumulative Variance
# PC1 PC1               5.76           18.8810%              18.88%
# PC2 PC2               4.54           11.7320%              30.61%
# PC3 PC3               3.63            7.4750%              38.09%
# PC4 PC4               3.53            7.0660%              45.15%
# PC5 PC5               3.24            5.9720%              51.12%
# PC6 PC6               2.95            4.9430%              56.07%

PCoA

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

result <- dimred_pcoa(MAE,
                      tax_level="genus",
                      color="age_s",
                      shape="Disease",
                      axx=1,
                      axy=2,
                      method="bray")
result$plot
head(result$table)
#          Axis Eigenvalue Variance Explained Cumulative Variance
# Axis.1 Axis.1      3.550            30.456%              30.46%
# Axis.2 Axis.2      1.590            14.187%              44.64%
# Axis.3 Axis.3      1.120            10.303%              54.95%
# Axis.4 Axis.4      0.751             7.224%              62.17%
# Axis.5 Axis.5      0.381             4.155%              66.32%
# Axis.6 Axis.6      0.304             3.521%              69.85%

UMAP

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

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

t-SNE

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

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

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
#          microbe   padj pValue log2FoldChange    TB Control prevalence
# 1     Firmicutes 0.0936 0.0170          1.010 18/18   12/12    100.00%
# 2 Proteobacteria 0.0936 0.0109          1.970 17/18   12/12     96.67%
# 3  Bacteroidetes 0.1130 0.0307         -2.000 17/18   12/12     96.67%
# 4 Actinobacteria 0.1330 0.0484         -0.929 18/18   12/12    100.00%
# 5    Tenericutes 0.3350 0.1520         -2.610  5/18    3/12     26.67%
# 6  Cyanobacteria 0.4290 0.2340         -2.400  3/18    4/12     23.33%
#   Group Size adjusted fold change
# 1                            1.00
# 2                            1.06
# 3                            1.06
# 4                            1.00
# 5                            1.11
# 6                            2.00

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.

Train biomarker

p <- find_biomarker(MAE,
                    tax_level = "genus",
                    input_select_target_biomarker = c("Disease"),
                    nfolds = 3,
                    nrepeats = 3,
                    seed = 99,
                    percent_top_biomarker = 0.2,
                    model_name = "logistic regression")
# biomarker
p$biomarker
#             biomarker_list
# 1        Pseudoalteromonas
# 2           Pectobacterium
# 3                  Erwinia
# 4          Streptobacillus
# 5          Bifidobacterium
# 6          Glutamicibacter
# 7               Sebaldella
# 8               Filifactor
# 9           Mageeibacillus
# 10              Prevotella
# 11         Arcanobacterium
# 12            Ruminococcus
# 13           Mycobacterium
# 14        Methylobacterium
# 15            Sphingomonas
# 16        Phenylobacterium
# 17        Paraburkholderia
# 18               Weissella
# 19            Blastococcus
# 20               Spirosoma
# 21           Mesorhizobium
# 22           Chelativorans
# 23            Acidiphilium
# 24       Pseudarthrobacter
# 25         Cryptobacterium
# 26               Moraxella
# 27             Caulobacter
# 28            Thiobacillus
# 29            Anaerostipes
# 30             Geobacillus
# 31            Methylomonas
# 32 Pseudopropionibacterium
# 33         Novosphingobium
# 34          Herbaspirillum
# 35           Streptococcus
# importance plot
p$importance_plot

# ROC plot
p$roc_plot

Session Info

# R version 4.1.1 Patched (2021-10-12 r81044)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS Big Sur 10.16
# 
# Matrix products: default
# BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
# 
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
# [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
# [8] methods   base     
# 
# other attached packages:
#  [1] caret_6.0-88                ggplot2_3.3.3              
#  [3] lattice_0.20-45             SummarizedExperiment_1.23.0
#  [5] Biobase_2.53.0              GenomicRanges_1.45.0       
#  [7] GenomeInfoDb_1.29.0         IRanges_2.27.0             
#  [9] S4Vectors_0.31.0            BiocGenerics_0.39.0        
# [11] MatrixGenerics_1.5.0        matrixStats_0.58.0         
# [13] animalcules_1.9.3           testthat_3.0.2             
# [15] BiocStyle_2.21.0           
# 
# loaded via a namespace (and not attached):
#   [1] systemfonts_1.0.3           plyr_1.8.6                 
#   [3] lazyeval_0.2.2              splines_4.1.1              
#   [5] crosstalk_1.1.1             BiocParallel_1.27.0        
#   [7] usethis_2.0.1               digest_0.6.27              
#   [9] foreach_1.5.1               htmltools_0.5.1.1          
#  [11] fansi_0.5.0                 magrittr_2.0.1             
#  [13] memoise_2.0.0               covr_3.5.1                 
#  [15] cluster_2.1.2               limma_3.49.0               
#  [17] remotes_2.3.0               recipes_0.1.16             
#  [19] Biostrings_2.61.0           annotate_1.71.0            
#  [21] gower_0.2.2                 askpass_1.1                
#  [23] pkgdown_1.6.1               prettyunits_1.1.1          
#  [25] colorspace_2.0-1            blob_1.2.1                 
#  [27] textshaping_0.3.6           xfun_0.23                  
#  [29] dplyr_1.0.6                 callr_3.7.0                
#  [31] crayon_1.4.1                RCurl_1.98-1.3             
#  [33] jsonlite_1.7.2              genefilter_1.75.0          
#  [35] ape_5.5                     survival_3.2-13            
#  [37] iterators_1.0.13            glue_1.4.2                 
#  [39] gtable_0.3.0                ipred_0.9-11               
#  [41] zlibbioc_1.39.0             XVector_0.33.0             
#  [43] DelayedArray_0.19.0         pkgbuild_1.2.0             
#  [45] Rhdf5lib_1.15.0             GUniFrac_1.2               
#  [47] shape_1.4.6                 rentrez_1.2.3              
#  [49] scales_1.1.1                DBI_1.1.1                  
#  [51] Rcpp_1.0.6                  plotROC_2.2.1              
#  [53] viridisLite_0.4.0           xtable_1.8-4               
#  [55] reticulate_1.20             bit_4.0.4                  
#  [57] umap_0.2.7.0                DT_0.18                    
#  [59] tsne_0.1-3                  lava_1.6.9                 
#  [61] prodlim_2019.11.13          glmnet_4.1-1               
#  [63] rex_1.2.0                   htmlwidgets_1.5.3          
#  [65] httr_1.4.2                  RColorBrewer_1.1-2         
#  [67] ellipsis_0.3.2              farver_2.1.0               
#  [69] pkgconfig_2.0.3             XML_3.99-0.6               
#  [71] nnet_7.3-16                 sass_0.4.0                 
#  [73] locfit_1.5-9.4              utf8_1.2.1                 
#  [75] labeling_0.4.2              tidyselect_1.1.1           
#  [77] rlang_0.4.11                reshape2_1.4.4             
#  [79] later_1.2.0                 AnnotationDbi_1.55.0       
#  [81] munsell_0.5.0               tools_4.1.1                
#  [83] cachem_1.0.5                cli_2.5.0                  
#  [85] generics_0.1.0              RSQLite_2.2.7              
#  [87] devtools_2.4.1              biomformat_1.21.0          
#  [89] evaluate_0.14               stringr_1.4.0              
#  [91] fastmap_1.1.0               yaml_2.2.1                 
#  [93] ragg_1.1.3                  ModelMetrics_1.2.2.2       
#  [95] processx_3.5.2              knitr_1.33                 
#  [97] bit64_4.0.5                 fs_1.5.0                   
#  [99] purrr_0.3.4                 KEGGREST_1.33.0            
# [101] nlme_3.1-153                mime_0.10                  
# [103] compiler_4.1.1              rstudioapi_0.13            
# [105] plotly_4.9.3                png_0.1-7                  
# [107] tibble_3.1.2                geneplotter_1.71.0         
# [109] bslib_0.2.5.1               stringi_1.6.2              
# [111] highr_0.9                   ps_1.6.0                   
# [113] RSpectra_0.16-0             desc_1.3.0                 
# [115] forcats_0.5.1               Matrix_1.3-4               
# [117] permute_0.9-5               vegan_2.5-7                
# [119] shinyjs_2.0.0               vctrs_0.3.8                
# [121] rhdf5filters_1.5.0          pillar_1.6.1               
# [123] lifecycle_1.0.0             BiocManager_1.30.15        
# [125] jquerylib_0.1.4             data.table_1.14.0          
# [127] bitops_1.0-7                httpuv_1.6.1               
# [129] R6_2.5.0                    bookdown_0.22              
# [131] promises_1.2.0.1            sessioninfo_1.1.1          
# [133] codetools_0.2-18            MASS_7.3-54                
# [135] assertthat_0.2.1            pkgload_1.2.1              
# [137] rhdf5_2.37.0                openssl_1.4.4              
# [139] DESeq2_1.33.1               rprojroot_2.0.2            
# [141] withr_2.4.2                 GenomeInfoDbData_1.2.6     
# [143] MultiAssayExperiment_1.19.1 mgcv_1.8-38                
# [145] grid_4.1.1                  rpart_4.1-15               
# [147] timeDate_3043.102           tidyr_1.1.3                
# [149] class_7.3-19                rmarkdown_2.8              
# [151] pROC_1.17.0.1               shiny_1.6.0                
# [153] lubridate_1.7.10