Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ po/*~
rsconnect/
docs
inst/doc

# Mac things
.DS_Store

# Temporary output files
tmp.*
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(importPhyloseq)
export(importQIIME2)
export(importTreeSE)
export(importTreeSummarizedExperiment)
export(mbiodb_html_format)
export(rankedAbundance)
export(selfCorrelation)
export(updateCollectionName)
Expand Down
89 changes: 89 additions & 0 deletions R/utils-markdown.R
Original file line number Diff line number Diff line change
@@ -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,
...
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions inst/rmarkdown/resources/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.kable-table {
overflow-x: auto;
text-transform: capitalize;
}
45 changes: 45 additions & 0 deletions inst/rmarkdown/templates/alpha_div_mkdn/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: "Alpha Diversity Output Rmd"
output: MicrobiomeDB:::mbiodb_html_format
---

<img src="inst/rmarkdown/resources/MicrobiomeDB_hex_final.png" width=95 height=100 style="position:absolute;top:10px;right:38px;" />

```{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
```
3 changes: 3 additions & 0 deletions inst/rmarkdown/templates/alpha_div_mkdn/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "alpha_div_mkdn"
description: "Alpha Diversity Output Rmd Template"
create_dir: true
44 changes: 44 additions & 0 deletions inst/rmarkdown/templates/beta_div_mkdn/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: "Beta Diversity Output Rmd"
output: MicrobiomeDB:::mbiodb_html_format
---

<img src="inst/rmarkdown/resources/MicrobiomeDB_hex_final.png" width=95 height=100 style="position:absolute;top:10px;right:38px;" />

```{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
```
3 changes: 3 additions & 0 deletions inst/rmarkdown/templates/beta_div_mkdn/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "beta_div_mkdn"
description: "Beta Diversity Output Rmd Template"
create_dir: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: "Ranked Abundance Output Rmd"
output: MicrobiomeDB:::mbiodb_html_format
---

<img src="inst/rmarkdown/resources/MicrobiomeDB_hex_final.png" width=95 height=100 style="position:absolute;top:10px;right:38px;" />

```{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)
```
3 changes: 3 additions & 0 deletions inst/rmarkdown/templates/rankedAbundance_mkdn/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: "beta_div_mkdn"
description: "Beta Diversity Output Rmd Template"
create_dir: false
27 changes: 27 additions & 0 deletions man/createRMarkdownFromComputeResult.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/mbiodb_html_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.