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 Top

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

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

6.2 Summary Plot Bottom

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

p <- filter_summary_bottom(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', 'autosize', 'width', 'height', 'margin', 'paper_bgcolor', 'plot_bgcolor', 'separators', 'hidesources', 'showlegend', 'colorway', 'colorscale', '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', '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.3667155               0.4879632

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.2066 0.01495 0.7283  0.686
# Residual  48  13.6149 0.98505              
# Total     49  13.8215 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.5599634292793              10.4%               10.4%
# PC2 PC2   2.19272108550489               7.6%               18.0%
# PC3 PC3   2.00513673550526               6.4%               24.4%
# PC4 PC4   1.87545892419787               5.6%               30.0%
# PC5 PC5   1.80253703972772               5.2%               35.2%
# PC6 PC6   1.70630724920304               4.6%               39.8%

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.6608829              21.0%               21.0%
# Axis.2 Axis.2  2.7032256              15.6%               36.5%
# Axis.3 Axis.3  1.6483657               9.7%               46.2%
# Axis.4 Axis.4  0.8576656               5.3%               51.5%
# Axis.5 Axis.5  0.7871770               4.9%               56.4%
# Axis.6 Axis.6  0.7508249               4.7%               61.0%

9.3 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
#          microbe  padj pValue log2FoldChange positive negative prevalence
# 1 Actinobacteria 0.206 0.0716          0.704    28/28    22/22    100.00%
# 2     Ascomycota 0.206 0.1030         -0.372    28/28    22/22    100.00%
# 3  Basidiomycota 0.206 0.0874         -1.460    19/28    15/22     68.00%
# 4     Firmicutes 0.206 0.0850          0.817    28/28    22/22    100.00%
# 5         others 0.206 0.0783         -0.479    28/28    22/22    100.00%
# 6 Proteobacteria 0.260 0.1560         -0.649    28/28    22/22    100.00%
#   Group Size adjusted fold change
# 1                               1
# 2                               1
# 3                               1
# 4                               1
# 5                               1
# 6                               1

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             Serpula
# 2         Citrobacter
# 3         Aspergillus
# 4 Alphapapillomavirus
# 5         Veillonella
# 6           Neisseria
# 7         Haemophilus
# 8          Malassezia
# 9         Cupriavidus
# importance plot
p$importance_plot

# ROC plot
p$roc_plot

12 Session Info

sessionInfo()
# R version 3.6.0 (2019-04-26)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 18.04.2 LTS
# 
# Matrix products: default
# BLAS:   /home/biocbuild/bbs-3.9-bioc/R/lib/libRblas.so
# LAPACK: /home/biocbuild/bbs-3.9-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-84               
#  [3] ggplot2_3.2.0               lattice_0.20-38            
#  [5] SummarizedExperiment_1.14.0 DelayedArray_0.10.0        
#  [7] BiocParallel_1.18.0         matrixStats_0.54.0         
#  [9] Biobase_2.44.0              GenomicRanges_1.36.0       
# [11] GenomeInfoDb_1.20.0         IRanges_2.18.1             
# [13] S4Vectors_0.22.0            BiocGenerics_0.30.0        
# [15] animalcules_1.0.6           BiocStyle_2.12.0           
# 
# loaded via a namespace (and not attached):
#   [1] backports_1.1.4             Hmisc_4.2-0                
#   [3] plyr_1.8.4                  lazyeval_0.2.2             
#   [5] splines_3.6.0               crosstalk_1.0.0            
#   [7] digest_0.6.19               foreach_1.4.4              
#   [9] htmltools_0.3.6             gdata_2.18.0               
#  [11] magrittr_1.5                checkmate_1.9.3            
#  [13] memoise_1.1.0               covr_3.2.1                 
#  [15] cluster_2.1.0               ROCR_1.0-7                 
#  [17] limma_3.40.2                recipes_0.1.5              
#  [19] annotate_1.62.0             gower_0.2.1                
#  [21] xts_0.11-2                  colorspace_1.4-1           
#  [23] blob_1.1.1                  xfun_0.8                   
#  [25] dplyr_0.8.2                 crayon_1.3.4               
#  [27] RCurl_1.95-4.12             jsonlite_1.6               
#  [29] genefilter_1.66.0           survival_2.44-1.1          
#  [31] zoo_1.8-6                   iterators_1.0.10           
#  [33] ape_5.3                     glue_1.3.1                 
#  [35] gtable_0.3.0                ipred_0.9-9                
#  [37] zlibbioc_1.30.0             XVector_0.24.0             
#  [39] quantmod_0.4-15             rentrez_1.2.2              
#  [41] abind_1.4-5                 scales_1.0.0               
#  [43] DBI_1.0.0                   Rcpp_1.0.1                 
#  [45] plotROC_2.2.1               viridisLite_0.3.0          
#  [47] xtable_1.8-4                htmlTable_1.13.1           
#  [49] foreign_0.8-71              bit_1.1-14                 
#  [51] Formula_1.2-3               tsne_0.1-3                 
#  [53] lava_1.6.5                  prodlim_2018.04.18         
#  [55] DT_0.7                      glmnet_2.0-18              
#  [57] htmlwidgets_1.3             rex_1.1.2                  
#  [59] httr_1.4.0                  gplots_3.0.1.1             
#  [61] RColorBrewer_1.1-2          acepack_1.4.1              
#  [63] pkgconfig_2.0.2             XML_3.98-1.20              
#  [65] nnet_7.3-12                 locfit_1.5-9.1             
#  [67] labeling_0.3                tidyselect_0.2.5           
#  [69] rlang_0.4.0                 reshape2_1.4.3             
#  [71] later_0.8.0                 AnnotationDbi_1.46.0       
#  [73] munsell_0.5.0               tools_3.6.0                
#  [75] generics_0.0.2              RSQLite_2.1.1              
#  [77] evaluate_0.14               stringr_1.4.0              
#  [79] yaml_2.2.0                  ModelMetrics_1.2.2         
#  [81] knitr_1.23                  bit64_0.9-7                
#  [83] caTools_1.17.1.2            purrr_0.3.2                
#  [85] nlme_3.1-140                mime_0.7                   
#  [87] compiler_3.6.0              rstudioapi_0.10            
#  [89] plotly_4.9.0                curl_3.3                   
#  [91] tibble_2.1.3                geneplotter_1.62.0         
#  [93] stringi_1.4.3               forcats_0.4.0              
#  [95] Matrix_1.2-17               permute_0.9-5              
#  [97] vegan_2.5-5                 shinyjs_1.0                
#  [99] pillar_1.4.2                BiocManager_1.30.4         
# [101] data.table_1.12.2           bitops_1.0-6               
# [103] httpuv_1.5.1                R6_2.4.0                   
# [105] latticeExtra_0.6-28         bookdown_0.11              
# [107] promises_1.0.1              KernSmooth_2.23-15         
# [109] gridExtra_2.3               codetools_0.2-16           
# [111] MASS_7.3-51.4               gtools_3.8.1               
# [113] assertthat_0.2.1            DESeq2_1.24.0              
# [115] withr_2.1.2                 GenomeInfoDbData_1.2.1     
# [117] mgcv_1.8-28                 MultiAssayExperiment_1.10.4
# [119] rpart_4.1-15                timeDate_3043.102          
# [121] tidyr_0.8.3                 class_7.3-15               
# [123] rmarkdown_1.13              Cairo_1.5-10               
# [125] TTR_0.23-4                  shiny_1.3.2                
# [127] lubridate_1.7.4             base64enc_0.1-3