Commit 9e535dfc authored by Holger Brandl's avatar Holger Brandl

appify explorer app without bash wrapper

parent 788904dd
......@@ -463,7 +463,6 @@ dge_create_explorer_app() {
files='tpms_by_replicate.txt fpkms_by_replicate.txt de_results.txt basic_design.txt'
ls $files 2>/dev/null || { echo "Can not create app, because not all required data files ($files) exist in the current directory" 1>&2; return; }
appify ${NGS_TOOLS}/dge_workflow/expression_explorer/run_expression_explorer.sh
appify ${NGS_TOOLS}/dge_workflow/expression_explorer/expression_explorer.R "expression_explorer"
}
export -f dge_create_explorer_app
\ No newline at end of file
#!/usr/bin/env Rscript
#+ include=FALSE
#--------------------------------------------------------------------
# Title : Expression Explorer
# Objective : shinyApp to explore differentially expressed genes
# run shiny app from a bash file: Rscript -e "shiny::runApp('expression_explorer/', launch.browser=TRUE)"
# https://shiny.rstudio.com/reference/shiny/1.0.5/
# http://rstudio.github.io/DT/
#--------------------------------------------------------------------
# LOAD packages --------------------------------------------------------------------------------------------------------
devtools::source_url("https://raw.githubusercontent.com/holgerbrandl/datautils/v1.44/R/core_commons.R")
library(shiny)
library(shinyFiles)
library(DT)
library(plotly)
library(shinyjqui)
# FUNCTIONS ------------------------------------------------------------------------------------------------------------
calc_ci = function(df, variable, ci_interval=0.95){
variable <- enquo(variable)
# http://dplyr.tidyverse.org/articles/programming.html
mean_name <- paste0( quo_name(variable), "_mean")
ci_name <- paste0(quo_name(variable), "_ci")
# echo(glue::glue("varname is {ci_name}"))
df %>% summarize(
mean=mean(!!variable),
sd=sd(!!variable),
N = n(),
se=sd/sqrt(N),
!!ci_name := qt(ci_interval/2+0.5, N-1)*se,
!!mean_name :=mean
) %>% select(-c(mean, sd, N, se, mean))
}
# LOAD data ------------------------------------------------------------------------------------------------------------
# data %>% head()
de_res <- read_tsv("de_results.txt") %>%
transmute(external_gene_name, condition_1, condition_2, logfc = c1_over_c2_logfc, pvalue, padj, is_hit, c1_overex)
de_res[,c(4:6)] <- round(de_res[,c(4:6)], 3)
design <- read_tsv("basic_design.txt") %>% select(-batch)
fpkms <- read_tsv("fpkms_by_replicate.txt") %>%
mutate(method = "fpkm") %>%
gather(replicate, rep_values, -c(ensembl_gene_id, gene_name, gene_description, method)) %>%
left_join(design, by = "replicate")
tpms <- read_tsv("tpms_by_replicate.txt") %>%
mutate(method = "tpm") %>%
gather(replicate, rep_values, -c(ensembl_gene_id, gene_name, gene_description, method)) %>%
left_join(design, by = "replicate")
all <- rbind(fpkms, tpms)
#calculate confidence intervals:
ci <- all %>% group_by(gene_name, condition, method) %>% calc_ci(., rep_values)
all %<>% left_join(ci)
# all %>% select(-gene_name, -gene_description) %>% arrange(ensembl_gene_id) %>% head()
#extract gene_names
gene_names <- sort(unique(all$gene_name))
first_entry <- gene_names[1]
sample_names <- unique(design$condition)
# USER INTERFACE -------------------------------------------------------------------------------------------------------
ui <- fixedPage(
headerPanel("Expression Explorer"),
fixedPage("",
fixedRow(
column(12, style = "height:20px")
),
fixedRow(
column(4, style = "height:100px",
selectInput(inputId = "gene_name", label = "Gene name", choices = gene_names, multiple = TRUE, selected = first_entry, selectize=TRUE)
),
column(4, style = "height:100px",
radioButtons("method", label = "Normalization method", choiceNames = c("FPKM", "TPM"), choiceValues = c("fpkm", "tpm"))
),
column(4, style = "height:100px",
checkboxGroupInput("options", label = "Optional", choiceNames = c("show confidence interval", "show line"), choiceValues = c("plot_ci", "plot_line"))
)
),
fixedRow(
column(12,
orderInput("sample", label = "Change sample order:", items = sample_names)#,
#verbatimTextOutput('order')
)
),
hr(),
fixedRow(
column(6, HTML(paste('<br/>'))
# htmlOutput("text", style = "font-size: 80%; width=75%")
)
),
# fixedRow(div(style = "margin_top:50px;"),
fixedRow(
column(6, HTML(paste('<br/>', '<br/>')),
# plotlyOutput(outputId = "points"),
plotOutput(outputId = "points")
),
column(6, HTML(paste('<br/>', '<br/>')),
tabPanel("table", DT::dataTableOutput("table"), style = "font-size: 75%; width: 75%")
)
)
)
)
# shinyApp(ui = ui, server = server)
# SERVER FUNCTIONS -----------------------------------------------------------------------------------------------------
server <- function(input, output) {
# prepare output data
table_data <- reactive({
de_res %>% filter(external_gene_name %in% input$gene_name) %>% select(-is_hit, -c1_overex)
})
# text_data <- reactive({
# lapply(as.list(input$gene_name), function(x){data <- all %>% filter(gene_name == x) %>% select(gene_name, gene_description, ensembl_gene_id) %>% filter(!duplicated(gene_name))})
# })
# plot_data <- reactive({
# lapply(as.list(input$gene_name), function(x){data <- all %>% filter(gene_name == x & grepl(input$method, method) & grepl(input$sample, sample))})
# })
# table_data <- reactive({
# lapply(as.list(input$gene_name), function(x){data <- de_res %>% filter(external_gene_name == x) %>% select(-external_gene_name)})
# })
# prepare variable for text output
str_gene <- tags$b("Gene name: ")
str_desc <- tags$b("Gene description: ")
str_ens <- tags$b("Ensembl gene ID: ")
texting <- function(x) {
HTML(paste('<br/>', str_gene, x$gene_name, '<br/>', str_ens, x$ensembl_gene_id, '<br/>', str_desc, x$gene_description, '<br/>', sep = ''))
}
# http://ggplot2.tidyverse.org/reference/stat_summary.html
output$text <- renderUI({ texting(text_data()) })
# output$points <- renderPlotly({
output$points <- renderPlot({
pd <- position_dodge(0.3)
plot_data <- all %>% filter(gene_name %in% input$gene_name & grepl(input$method, method))
# plot_data <- all %>% filter(gene_name %in% c("Gnai3", "Cdc45") & grepl("fpkm", method))
plot_data$condition <- factor(plot_data$condition, levels = input$sample_order)
plot <- ggplot(plot_data, aes(condition, rep_values, color = gene_name)) +
geom_point(position=pd) +
theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
ylab(paste(input$method, "s", sep = "")) +
xlab("condition")
if (is.element("plot_line", input$options)) {
plot <- plot + geom_line(aes(condition, rep_values_mean, group = gene_name), data = plot_data, position=pd)
}
if (is.element("plot_ci", input$options)) {
plot <- plot + geom_errorbar(aes(ymin=rep_values_mean-rep_values_ci, ymax=rep_values_mean+rep_values_ci), size = 0.2, position=pd)
}
if (is.element("plot_line", input$options) || is.element("plot_ci", input$options)) {
} else {
plot <- plot + stat_summary(fun.y="mean", geom="point", pch = "_", size=8, position = pd)
}
# stat_summary(fun.data = "mean_cl_boot", color = "black", size = 1,
# geom = "errorbar", width = 1, fun.args = list(conf.int = 0.9), aes(group = gene_name),
# position=position_dodge(width=0.4))
plot(plot)
# ggplotly(plot) %>% layout(margin = list(b = 95), legend = list(orientation = "h", y = 10))
})
output$table <- DT::renderDataTable({ DT::datatable(table_data()) })
}
shinyApp(ui = ui, server = server)
###############################################
#ALTERNATIVE: try to plot results for more than one gene (i.e. multiple plots and multiple tables)
# ui <- fixedPage(
#
# tabsetPanel(
#
# headerPanel("Exploring differentially expressed genes"),
#
# fixedPage("",
# fixedRow(
# column(12, style = "height:20px")
# ),
#
# fixedRow(
# column(4, style = "height:100px; background-color:lightblue",
# selectInput(inputId = "gene_name", label = "Gene name", choices = gene_names, multiple = TRUE, selected = first_entry, selectize=TRUE)
# ),
# column(4, style = "height:100px; background-color:lightblue",
# radioButtons("method", label = "Normalization method", choiceNames = c("FPKM", "TPM", "display both"), choiceValues = c("fpkm", "tpm", "fpkm|tpm"))
# ),
# column(4, style = "height:100px; background-color:lightblue",
# radioButtons("sample", label = "Sample level", choiceNames = c("by condition (con)", "by replicate (rep)", "display both"), choiceValues = c("con", "rep", "con|rep"))
# )
# ),
#
# fixedRow(
# column(6, uiOutput(outputId = "plots")
# ),
# column(6, uiOutput(outputId = "tables")
# )
# )
# )
# )
# )
#
# server <- function(input, output) {
#
#
# output$plots <- renderUI({
# plot_output_list <- lapply(1:length(input$gene_name), function(i) {
# plotname <- paste("plot", i, sep="")
# plotOutput(plotname, height = 280, width = 250)
# })
# do.call(tagList, plot_output_list)
# })
#
# output$tables <- renderUI({
# table_output_list <- lapply(1:length(input$gene_name), function(i) {
# tablename <- paste("table", i, sep="")
# dataTableOutput(tablename)
# })
# })
#
#
# for (i in 1:5) {
# local({
# my_i <- i
# plotname <- paste("plot", my_i, sep="")
# tablename <- paste("table", my_i, sep="")
#
# output[[tablename]] <- renderDataTable({
# de_res %>% filter(external_gene_name == input$gene_name[my_i]) %>% select(-external_gene_name)
# })
#
# output[[plotname]] <- renderPlot({
# all %>% filter(gene_name == input$gene_name[my_i] & grepl(input$method, method) & grepl(input$sample, sample)) %>%
# ggplot(aes(condition, value, shape = method, color = sample, alpha = 0.5)) +
# geom_point() +
# # geom_jitter(width = 0.1)+
# theme(axis.text.x = element_text(angle = 50, hjust = 1))
# })
#
# })
# }
#
# }
#
# shinyApp(ui = ui, server = server)
# only TPMs or FPKMs
# always rep and con (con = dash)
# allow more than one gene (position_dodge)
# optional: confidence intervals
# optional: progession lines
# optional: config file for sorting
# footer: "https://www.mpi-cbg.de/en/services-facilities/core-facilities/scientific-computing-facility/services/"
\ No newline at end of file
#!/usr/bin/env bash
#https://christopher.su/2012/creating-mac-applications-shell-scripts/
export SCRIPT_DIRECTORY="$(dirname "$0")/"
#https://stackoverflow.com/questions/26465240/how-to-execute-a-shell-script-from-within-an-app-and-show-it-in-the-terminal
#osascript -e 'display notification "Starting expression explorer... Please be patient, it may require some minutes to install required dependencies on first launch'
# To use Rscript as interpreter see https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
/usr/local/bin/Rscript -<<"EOF" ${SCRIPT_DIRECTORY}
#!/usr/bin/env Rscript
# run shiny app from a bash file: Rscript -e "shiny::runApp('expression_explorer/', launch.browser=TRUE)"
# https://shiny.rstudio.com/reference/shiny/1.0.5/
......@@ -30,8 +19,14 @@ load_pack(shinyjqui)
# GET path--------------------------------------------------------------------------------------------------------------
#Sys.getenv("DA_BIN_ICH")
dataPath= args[1] %>% dirname()%>% dirname()%>% dirname()
# dataPath= args[1] %>% dirname()%>% dirname()%>% dirname()
# https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
initial.options <- commandArgs(trailingOnly = FALSE)
file.arg.name <- "--file="
script.name <- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)])
# script.name="/Volumes/cerebral-organoids/RNA-Seq/data/hsap/dge_analysis/expression_explorer.app/Contents/MacOS/expression_explorer"
dataPath= script.name %>% dirname() %>% dirname()%>% dirname() %>% dirname()
# FUNCTIONS ------------------------------------------------------------------------------------------------------------
......@@ -204,6 +199,4 @@ app <- shinyApp(
#runApp(app)
runApp(app, launch.browser=TRUE)
#shinyApp(ui = ui, server = server)
EOF
\ No newline at end of file
#shinyApp(ui = ui, server = server)
\ No newline at end of file
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