# animalcules

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

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

#### October 29, 2019

#### Package

animalcules 1.2.0

# 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 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.675
    # 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          Veillonella
    # 2      Deltaretrovirus
    # 3       Herbaspirillum
    # 4            Dialister
    # 5          Escherichia
    # 6         Cladosporium
    # 7          Hepacivirus
    # 8      Aggregatibacter
    # 9          Haemophilus
    # 10           Treponema
    # 11 Alphapapillomavirus
    # 12         Cupriavidus
    # 13          Malassezia

    # importance plot
    p$importance_plot

    # ROC plot
    p$roc_plot

# 12 Session Info

    sessionInfo()

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