Commit 0de9c508 authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

new commit to merge files after talk

Merge branch 'master' of https://gitlab.gwdg.de/loosolab/software/i2dash.scrnaseq

# Conflicts:
#	NAMESPACE
#	man/dot-box_plot.Rd
parents 244fb27e f6639ede
......@@ -12,3 +12,8 @@ License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Imports:
stringi,
plotly,
shiny,
knitr
# Generated by roxygen2: do not edit by hand
export(.bar_plot)
export(.box_plot)
export(.scatter_plot)
export(.violin_plot)
export(boxplot)
export(plotly_boxplot)
#' Renders a boxplot for cluster characterization
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param x Numeric observations for the boxplot. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param group_by A factor, by which observations can optionally be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title The title of the components junk.
#'
#' @return A string containing markdown code for the rendered component.
#'
#' @export
boxplot <- function(object, x, group_by, title = NULL) {
# Create random env id
env_id <- paste0("env_", stringi::stri_rand_strings(1, 6, pattern = "[A-Za-z0-9]"))
if(!is.list(x)) x <- list(x)
if(!is.list(group_by)) group_by <- list(group_by)
# Validate input
if(!all(sapply(x, is.numeric))) stop("'x' should only contain numerical values.")
if(!all(sapply(cluster, is.factor))) stop("'cluster' should only contain factorial values.")
# Create component environment
env <- new.env()
env$x_selection <- FALSE
env$group_by_selection <- FALSE
env$x <- x
env$x_selection <- length(env$x) > 1
env$group_by <- group_by
env$group_by_selection <- length(env$group_by) > 1
# save environment object
saveRDS(env, file = file.path(object@workdir, "envs", paste0(env_id, ".rds")))
# Expand component
timestamp <- Sys.time()
expanded_component <- knitr::knit_expand(file = system.file("templates", "boxplot.Rmd", package = "i2dash.scrnaseq"), title = title, env_id = env_id, date = timestamp)
return(expanded_component)
}
#' Renders a boxplot for cluster characterization
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param cluster Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode. If x is NULL then a barplot for "Number of cells" per cluster will be created. If x is not NULL a barplot for "Fraction per cell" per cluster will be created.
#' @param title A title that will be displayed on top.
#'
#' @return A string containing markdown code for the rendered textbox
cluster_characterization_boxplot <- function(object, x, cluster, title = "Characterization of clusters") {
# Create random env id
env_id <- paste0("env_", stringi::stri_rand_strings(1, 6, pattern = "[A-Za-z0-9]"))
# validate input, create environment variables, save environment object
.validate_input_cluster_characterizatio_boxplot(object@workdir, env_id, x, cluster)
timestamp <- Sys.time()
expanded_component <- knitr::knit_expand(file = system.file("templates", "cluster_characterization_boxplot_template.Rmd", package = "i2dash.scrnaseq"), title = title, env_id = env_id, date = timestamp)
return(expanded_component)
}
.validate_input_cluster_characterizatio_boxplot <- function(workdir, env_id, x, cluster) {
env <- new.env()
env$x_selection <- FALSE
env$cluster_selection <- FALSE
# Check existence of cluster
if(is.null(cluster)) stop("cluster is required.")
if(is.null(x)) stop("x is required.")
# Create lists if needed
if(!is.list(x)) x <- list(x)
if(!is.list(cluster)) cluster <- list(cluster)
# should I use magrittr::%<>% ?
# name the lists
library(magrittr)
if(is.null(names(cluster))) x %<>% magrittr::set_names("clustering.1")
if(is.null(names(x))) x %<>% magrittr::set_names("x")
# Check validity
if(!all(sapply(cluster, is.factor))) stop("'cluster' should only contain factorial values.")
if(!all(sapply(x, is.numeric))) stop("'x' should only contain numerical values.")
# Check if lengths in a list are the same and if x and y and label and color_by are the same length
#if(length(unique(sapply(x, length))) != 1) stop("list x should contain elements with the same length.")
#if(length(unique(sapply(y, length))) != 1) stop("list y should contain elements with the same length.")
#if(!identical(length(x[[1]]), length(y[[1]]))) stop("all arguments should be of the the same length.")
#if(!identical(length(x[[1]]), length(colour_by[[1]])) & !is.null(colour_by)) stop("all arguments should be of the the same length.")
#if(!identical(length(x[[1]]), length(labels[[1]])) & !is.null(labels)) stop("all arguments should be of the the same length.")
# Add objects to env
env$x <- x
env$x_selection <- length(env$x) > 1
env$cluster <- cluster
env$cluster_selection <- length(env$cluster) > 1
# save environment as rds-object
saveRDS(env, file = file.path(workdir, "envs", paste0(env_id, ".rds")))
print("validation TRUE")
}
......@@ -26,10 +26,6 @@ sequence_saturation <- function(object, x, y, colour_by = NULL, labels = NULL, t
env$y_selection <- FALSE
env$colour_by_selection <- FALSE
# Check existence of x and y
if(is.null(x)) stop("x is required.")
if(is.null(y)) stop("y is required.")
# Create lists if needed
if(!is.list(x)) x <- list(x)
if(!is.list(y)) y <- list(y)
......
......@@ -126,25 +126,20 @@
}
#' Render a box plot
#' Render a box plot with plotly.
#'
#' @param cluster Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric observations for the boxplot.
#' @param group_by A factor, by which observations can optionally be grouped.
#' @param title A title that describes the observations.
#' @param group_by_title A title that describes the grouping factor.
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @return An object of class \code{plotly}.
#' @export
.box_plot <- function(cluster, x){
df <- data.frame(cluster, x)
# plotly
title <- "Detected genes"
p <- plotly::plot_ly(df, y = df[[1]], x = df[[2]], type = "box", name = df[[1]])
p <- plotly::layout(p,
xaxis = list(title=title, showline = T),
yaxis = list(title="Cluster", showline = T, showticklabels = T),
showlegend = T
)
return(list("plot" = p, "df" = df))
plotly_boxplot <- function(x, group_by = NULL, title = "", group_by_title = NULL){
plotly::plot_ly(x = x, y = group_by, type = "box", name = title) %>%
plotly::layout(xaxis = list(title = title, showline = T),
yaxis = list(title = group_by_title, showline = T, showticklabels = T),
showlegend = T)
}
#' Render a violin plot
......
### {{ title }}
<!-- Component created on {{ date }} -->
```{r}
{{ env_id }} = readRDS("envs/{{ env_id }}.rds")
is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
```{r, eval=!is_shiny}
# set variables
x <- {{ env_id }}$x[[1]]
group_by <- {{ env_id }}$group_by[[1]]
# Provide data download
# data.frame(x, group_by) %>%
# i2dash::embed_var()
# Render plot
i2dash.scrnaseq::plotly_box_plot(x = x, group_by = group_by)
```
```{r, eval=is_shiny}
ui_list <- list()
# selection field for x
if ({{ env_id }}$x_selection) {
ui_list <- rlist::list.append(ui_list,
selectInput("select_x_{{ env_id }}", label = "Select observations:",
choices = names({{ env_id }}$x)))
}
# selection field for group_by
if ({{ env_id }}$group_by_selection) {
ui_list <- rlist::list.append(ui_list,
selectInput("select_group_by_{{ env_id }}", label = "Group observations by:",
choices = names({{ env_id }}$group_by)))
}
#
# Handle inputs
#
if( !{{ env_id }}$x_selection ) {
x_{{ env_id }} <- shiny::reactive({
{{ env_id }}$x[[1]]
})
} else {
x_{{ env_id }} <- shiny::reactive({
{{ env_id }}$x[[input$select_x_{{ env_id }}]]
})
}
if( !{{ env_id }}$group_by_selection ) {
group_by_{{ env_id }} <- shiny::reactive({
{{ env_id }}$group_by[[1]]
})
} else {
group_by_{{ env_id }} <- shiny::reactive({
{{ env_id }}$group_by[[input$select_group_by_{{ env_id }}]]
})
}
#
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly(
i2dash.scrnaseq::plotly_box_plot(x = x_{{ env_id }}(), group_by = group_by_{{ env_id }}())
)
shiny::fillCol(flex = c(NA, 1),
do.call(shiny::inputPanel, ui_list),
plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
)
```
### {{ title }}
<!-- Component created on {{ date }} -->
```{r}
{{ env_id }} = readRDS("envs/{{ env_id }}.rds")
is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
```{r, eval=!is_shiny}
```
```{r, eval=is_shiny}
df <- data.frame({{ env_id }}$cluster[1], {{ env_id }}$x)
#print(df)
title <- "Detected genes"
p <- plotly::plot_ly(df, y = df[[1]], x = df[[2]], type = "box", name = df[[1]])
p <- plotly::layout(p,
xaxis = list(title=title, showline = T),
yaxis = list(title="Cluster", showline = T, showticklabels = T),
showlegend = T
)
p
```
***
Plot description:
Sequencing is called *saturated* when generating more sequencing output from a cDNA library does not substantially increase the number of detected features in a sample. Since the number of detected features can act as a technical confounder, and thereby drive substructure in the data, it is advisable to aim for a saturated sequencing by either adding more sequencing output or decreasing the number of samples until saturation is achieved. [@zhang_one_2018] gives advise on how to choose the optimal cell number given a fixed sequencing budget
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cluster_characterization_boxplot.R
\name{cluster_characterization_boxplot}
\alias{cluster_characterization_boxplot}
% Please edit documentation in R/boxplot.R
\name{boxplot}
\alias{boxplot}
\title{Renders a boxplot for cluster characterization}
\usage{
cluster_characterization_boxplot(object, x, cluster,
title = "Characterization of clusters")
boxplot(object, x, group_by, title = NULL)
}
\arguments{
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{x}{Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode. If x is NULL then a barplot for "Number of cells" per cluster will be created. If x is not NULL a barplot for "Fraction per cell" per cluster will be created.}
\item{x}{Numeric observations for the boxplot. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{cluster}{Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.}
\item{group_by}{A factor, by which observations can optionally be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{title}{A title that will be displayed on top.}
\item{title}{The title of the components junk.}
}
\value{
A string containing markdown code for the rendered textbox
A string containing markdown code for the rendered component.
}
\description{
Renders a boxplot for cluster characterization
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/visualization_functions.R
\name{.box_plot}
\alias{.box_plot}
\title{Render a box plot}
\usage{
.box_plot(cluster, x)
}
\arguments{
\item{cluster}{Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.}
\item{x}{Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.}
}
\value{
A list with 1. the plotly object & 2. the data frame used in the plot
}
\description{
Render a box plot
}
......@@ -4,16 +4,16 @@
\alias{multiplot}
\title{Renders a Sequence saturation plot}
\usage{
multiplot(object, x, y, title = "Multiplot", colour_by = NULL)
multiplot(object, plot_title, x, y, color_by)
}
\arguments{
\item{plot_title}{The title of the Component}
\item{x}{A list with the x-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{y}{A list with the y-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{colour_by}{A list with the color_by values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{plot_title}{The title of the Component}
\item{color_by}{A list with the color_by values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
}
\value{
A string containing markdown code for the rendered textbox
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/visualization_functions.R
\name{plotly_boxplot}
\alias{plotly_boxplot}
\title{Render a box plot with plotly.}
\usage{
plotly_boxplot(x, group_by = NULL, title = "", group_by_title = NULL)
}
\arguments{
\item{x}{Numeric observations for the boxplot.}
\item{group_by}{A factor, by which observations can optionally be grouped.}
\item{title}{A title that describes the observations.}
\item{group_by_title}{A title that describes the grouping factor.}
}
\value{
An object of class \code{plotly}.
}
\description{
Render a box plot with plotly.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dimension_reduction.R, R/sequence_saturation.R
% Please edit documentation in R/sequence_saturation.R
\name{sequence_saturation}
\alias{sequence_saturation}
\title{Renders a Sequence saturation plot}
\usage{
sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
title = "Sequencing saturation")
sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
title = "Sequencing saturation")
}
......@@ -21,23 +18,11 @@ sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
\item{labels}{A list with sample names, that should be of the same length as x and y.}
\item{title}{A title that will be displayed on top.}
\item{red_dim}{List with reduced dimensions as data.frames.}
\item{coulor_by}{A factor that will be mapped to colours.In case of a nested list, a dropdown menu will be provided in the interactive mode.}
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{title}{A title that will be displayed on top.}
}
\value{
A string containing markdown code for the rendered textbox
A string containing markdown code for the rendered textbox
}
\description{
Renders a Sequence saturation plot
Renders a Sequence saturation plot
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment