diff --git a/.gitignore b/.gitignore index 6c949a1..23c3a33 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,9 @@ po/*~ rsconnect/ docs inst/doc + +# Mac things +.DS_Store + +# Temporary output files +tmp.* diff --git a/NAMESPACE b/NAMESPACE index b85fde1..366915e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(importPhyloseq) export(importQIIME2) export(importTreeSE) export(importTreeSummarizedExperiment) +export(mbiodb_html_format) export(rankedAbundance) export(selfCorrelation) export(updateCollectionName) diff --git a/R/utils-markdown.R b/R/utils-markdown.R new file mode 100644 index 0000000..1f1d393 --- /dev/null +++ b/R/utils-markdown.R @@ -0,0 +1,89 @@ +# Helper functions for creating the R Markdown files. + +#' Create R Markdown file from compute result +#' This function will create an R Markdown file that from a ComputeResult object. The markdown +#' file will describe how to access the data in the ComputeResult object and create a plot. Finally, +#' it will render the R Markdown file to HTML. Templates for these mardown files are stored in the +#' inst/rmarkdown/templates directory. +#' @param result A compute result object +#' @param rmd_file_name The name of the R Markdown file to be created. Default is 'tmp.Rmd' +#' @return A list containing the plots created for the R markdown file. +createRMarkdownFromComputeResult <- function(result, rmd_file_name = "tmp.Rmd"){ + + dt <- getComputeResult(result) + plot_list <- list() + + if (result@name == 'betaDiv') { + # We'll create a pcoa scatterplot + axis1_name <- result@computedVariableMetadata[[1]]@displayName + axis2_name <- result@computedVariableMetadata[[2]]@displayName + p <- ggplot2::ggplot(dt) + + aes(x=Axis1, y=Axis2) + + geom_point() + + labs(y= axis2_name, x =axis1_name, + title="PCoA plot of beta diversity results", + caption=paste0("produced on ", Sys.time())) + + theme_bw() + + template <- "inst/rmarkdown/templates/beta_div_mkdn/skeleton/skeleton.Rmd" + plot_list[[1]] <- p + + } else if (result@name == 'alphaDiv') { + + # Show the distribution of alpha diversity values with a histogram + alpha_div_display_name <- result@computedVariableMetadata[[1]]@displayName + breaks <- pretty(range(dt$alphaDiversity), + n = nclass.Sturges(dt$alphaDiversity), + min.n = 1) + p <- ggplot2::ggplot(dt) + + aes(x=alphaDiversity) + + geom_histogram(breaks=breaks, color=1, fill='white') + + labs(y= "Frequency", x =alpha_div_display_name, + title="Distribution of alpha diversity values", + caption=paste0("produced on ", Sys.time())) + + theme_bw() + + template <- "inst/rmarkdown/templates/alpha_div_mkdn/skeleton/skeleton.Rmd" + plot_list[[1]] <- p + } else { + stop(paste0("This function does not support ComputeResult objects with name=", data@name)) + } + + # Create the R Markdown file + file.copy(template, rmd_file_name, overwrite = TRUE) + rmarkdown::render(rmd_file_name) + + return(plot_list) + +} + + +#' Custom HTML template, based on the custom +#' +#' Loads additional style and template file +#' +#' @param toc should a table of contents be displayed? +#' @param ... additional arguments provided to \@code{html_document} +#' @export +#' +mbiodb_html_format <- function(toc = TRUE, ...) { + + # locations of resource files in the package + pkg_resource <- function(...) { + system.file(..., package = "MicrobiomeDB") + } + + css <- pkg_resource("rmarkdown/resources/styles.css") + + # call the base html_document function + rmarkdown::html_document( + toc = toc, + toc_float = TRUE, + fig_width = 6.5, + fig_height = 4, + theme = "lumen", + df_print = "kable", + number_sections = TRUE, + ... + ) +} diff --git a/inst/rmarkdown/resources/MicrobiomeDB_hex_final.png b/inst/rmarkdown/resources/MicrobiomeDB_hex_final.png new file mode 100644 index 0000000..76f5d7d Binary files /dev/null and b/inst/rmarkdown/resources/MicrobiomeDB_hex_final.png differ diff --git a/inst/rmarkdown/resources/styles.css b/inst/rmarkdown/resources/styles.css new file mode 100644 index 0000000..dcd9707 --- /dev/null +++ b/inst/rmarkdown/resources/styles.css @@ -0,0 +1,4 @@ +.kable-table { + overflow-x: auto; + text-transform: capitalize; +} \ No newline at end of file diff --git a/inst/rmarkdown/templates/alpha_div_mkdn/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/alpha_div_mkdn/skeleton/skeleton.Rmd new file mode 100644 index 0000000..9c11419 --- /dev/null +++ b/inst/rmarkdown/templates/alpha_div_mkdn/skeleton/skeleton.Rmd @@ -0,0 +1,45 @@ +--- +title: "Alpha Diversity Output Rmd" +output: MicrobiomeDB:::mbiodb_html_format +--- + + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## Examine the ComputationResult object +The `ComputationResult` object contains useful information about the the alpha diversity calculation. +```{r computation_result, echo=TRUE} +# Input parameters of the computation +print(result@parameters) + +``` + +## Inspect the returned data +```{r example_data, echo=TRUE} +# Extract the output of the alpha diversity computation +dt <- getComputeResult(result, format='data.table') +head(dt) +``` + +## Example plot +```{r example_plot, echo=TRUE} +# Grab nice names for the axes +alpha_div_display_name <- result@computedVariableMetadata[[1]]@displayName + +# Create histogram +alpha_div_display_name <- result@computedVariableMetadata[[1]]@displayName +breaks <- pretty(range(dt$alphaDiversity), + n = nclass.Sturges(dt$alphaDiversity), + min.n = 1) +p <- ggplot2::ggplot(dt) + + aes(x=alphaDiversity) + + geom_histogram(breaks=breaks, color=1, fill='white') + + labs(y= "Frequency", x =alpha_div_display_name, + title="Distribution of alpha diversity values", + caption=paste0("produced on ", Sys.time())) + + theme_bw() +p +``` \ No newline at end of file diff --git a/inst/rmarkdown/templates/alpha_div_mkdn/template.yaml b/inst/rmarkdown/templates/alpha_div_mkdn/template.yaml new file mode 100644 index 0000000..72073dd --- /dev/null +++ b/inst/rmarkdown/templates/alpha_div_mkdn/template.yaml @@ -0,0 +1,3 @@ +name: "alpha_div_mkdn" +description: "Alpha Diversity Output Rmd Template" +create_dir: true \ No newline at end of file diff --git a/inst/rmarkdown/templates/beta_div_mkdn/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/beta_div_mkdn/skeleton/skeleton.Rmd new file mode 100644 index 0000000..7ce94ff --- /dev/null +++ b/inst/rmarkdown/templates/beta_div_mkdn/skeleton/skeleton.Rmd @@ -0,0 +1,44 @@ +--- +title: "Beta Diversity Output Rmd" +output: MicrobiomeDB:::mbiodb_html_format +--- + + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## Examine the ComputationResult object +The `ComputationResult` object contains useful information about the the beta diversity calculation. +```{r computation_result, echo=TRUE} +# Input parameters of the computation +print(result@parameters) + +# The computed variable display names include the percent variance captured by each axis. +print(paste0(result@computedVariableMetadata[[1]]@displayName, ', ', result@computedVariableMetadata[[2]]@displayName)) +``` + +## Inspect the returned data +```{r example_data, echo=TRUE} +# Extract the output of the beta diversity computation +dt <- getComputeResult(result, format='data.table') +head(dt) +``` + +## Example plot +```{r example_plot, echo=TRUE} +# Grab nice names for the axes +axis1_name <- result@computedVariableMetadata[[1]]@displayName +axis2_name <- result@computedVariableMetadata[[2]]@displayName + +# Create the PCoA scatterplot +p <- ggplot2::ggplot(dt) + + aes(x=Axis1, y=Axis2) + + geom_point() + + labs(y= axis2_name, x = axis1_name, + title='Beta diversity PCoA plot', + caption=paste0('produced on ', Sys.time())) + + theme_bw() +p +``` \ No newline at end of file diff --git a/inst/rmarkdown/templates/beta_div_mkdn/template.yaml b/inst/rmarkdown/templates/beta_div_mkdn/template.yaml new file mode 100644 index 0000000..4c465f9 --- /dev/null +++ b/inst/rmarkdown/templates/beta_div_mkdn/template.yaml @@ -0,0 +1,3 @@ +name: "beta_div_mkdn" +description: "Beta Diversity Output Rmd Template" +create_dir: false \ No newline at end of file diff --git a/inst/rmarkdown/templates/rankedAbundance_mkdn/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/rankedAbundance_mkdn/skeleton/skeleton.Rmd new file mode 100644 index 0000000..33d096d --- /dev/null +++ b/inst/rmarkdown/templates/rankedAbundance_mkdn/skeleton/skeleton.Rmd @@ -0,0 +1,64 @@ +--- +title: "Ranked Abundance Output Rmd" +output: MicrobiomeDB:::mbiodb_html_format +--- + + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## Examine the ComputationResult object +The `ComputationResult` object contains useful information about the ranked Abundance calculation. +```{r computation_result, echo=TRUE} +# Input parameters of the computation +print(result@parameters) + +# The rankedAbundance function will return only the top n (default n=10) most abundant taxa (pathways, etc.), +# while ignoring any taxa with {method} = 0 (for example, if method = 'median', we ignore taxa with median = 0). +# The computationDetails slot lets us know if the rankedAbundance function had to cutoff the list of taxa at n. +print(result@computationDetails) +``` + +## Inspect the returned data +```{r example_data, echo=TRUE} +# Extract the output of the ranked abundance computation +dt <- getComputeResult(result, format='data.table') +ancestorIdCols <- result@ancestorIdColumns + +# Show the names of the top taxa (pathways, etc.) by abundance +print(names(dt)[(length(ancestorIdCols)+2):ncol(dt)]) + +# Finally, check out the data +head(dt) +``` + +## Example plot +```{r example_plot, echo=TRUE, fig.height=9} + +# Create a boxplot showing the taxa with the top abundance values. +# Prep data +rankedAbund_pivot <- tidyr::pivot_longer(dt, # dataframe to be pivoted + cols = (length(result@ancestorIdColumns)+2):ncol(dt), # column names to be stored as a SINGLE variable + names_to = "taxa", # name of that new variable (column) + values_to = "abundance") # name of new variable (column) storing all the values (data) +rankedAbund_pivot$taxa <- factor(rankedAbund_pivot$taxa, levels = names(dt)[ncol(dt):(length(result@ancestorIdColumns)+2)]) +method <- gsub(".*method = ([^, ]+).*", "\\1", rabund@parameters) + +# Plot +p <- ggplot2::ggplot(rankedAbund_pivot) + + ggplot2::aes(x=taxa, y=abundance) + + ggplot2::geom_boxplot() + + ggplot2::labs(y= "Abundance", x = "Taxa", + title=paste0("Ranked abundance output, decending by ", method), + caption=paste0("produced on ", Sys.time())) + + ggplot2::coord_flip() + + ggplot2::theme_bw() +p +``` + +## Interactive plot +```{r interactive_plot, echo=TRUE, fig.height=9} +plotly::ggplotly(p) +``` \ No newline at end of file diff --git a/inst/rmarkdown/templates/rankedAbundance_mkdn/template.yaml b/inst/rmarkdown/templates/rankedAbundance_mkdn/template.yaml new file mode 100644 index 0000000..4c465f9 --- /dev/null +++ b/inst/rmarkdown/templates/rankedAbundance_mkdn/template.yaml @@ -0,0 +1,3 @@ +name: "beta_div_mkdn" +description: "Beta Diversity Output Rmd Template" +create_dir: false \ No newline at end of file diff --git a/man/createRMarkdownFromComputeResult.Rd b/man/createRMarkdownFromComputeResult.Rd new file mode 100644 index 0000000..6eb2f27 --- /dev/null +++ b/man/createRMarkdownFromComputeResult.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-markdown.R +\name{createRMarkdownFromComputeResult} +\alias{createRMarkdownFromComputeResult} +\title{Create R Markdown file from compute result +This function will create an R Markdown file that from a ComputeResult object. The markdown +file will describe how to access the data in the ComputeResult object and create a plot. Finally, +it will render the R Markdown file to HTML. Templates for these mardown files are stored in the +inst/rmarkdown/templates directory.} +\usage{ +createRMarkdownFromComputeResult(result, rmd_file_name = "tmp.Rmd") +} +\arguments{ +\item{result}{A compute result object} + +\item{rmd_file_name}{The name of the R Markdown file to be created. Default is 'tmp.Rmd'} +} +\value{ +A list containing the plots created for the R markdown file. +} +\description{ +Create R Markdown file from compute result +This function will create an R Markdown file that from a ComputeResult object. The markdown +file will describe how to access the data in the ComputeResult object and create a plot. Finally, +it will render the R Markdown file to HTML. Templates for these mardown files are stored in the +inst/rmarkdown/templates directory. +} diff --git a/man/mbiodb_html_format.Rd b/man/mbiodb_html_format.Rd new file mode 100644 index 0000000..e7bb0e5 --- /dev/null +++ b/man/mbiodb_html_format.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-markdown.R +\name{mbiodb_html_format} +\alias{mbiodb_html_format} +\title{Custom HTML template, based on the custom} +\usage{ +mbiodb_html_format(toc = TRUE, ...) +} +\arguments{ +\item{toc}{should a table of contents be displayed?} + +\item{...}{additional arguments provided to \@code{html_document}} +} +\description{ +Loads additional style and template file +}