diff --git a/app/server.r b/app/server.r index c311000..c8ddb2f 100644 --- a/app/server.r +++ b/app/server.r @@ -1,4 +1,3 @@ - # Done TODOs ------------------------------------------------------------------- #1 Only show download tabs and button after the data or plot has been generated so the user won't get an error (DONE!) @@ -9,7 +8,7 @@ #6 maybe start splitting the server into chuncks inside the server folder and source the chuncks (almost DONE!) #7 Check for shiny as a R package (it's doable, need to understand how packages are organized first) #8 Having some global variables so I won't need to fetch from DeepBlue many times (DONE!) -#9 Change all the plots to be in Plotly instead of ggplot +#9 Change all the plots to be in Plotly instead of ggplot (Done!!) #10 Advanced option for plotting (choosing which PCAs to plot and filtering option) (DONE!) #11 metadata to be interactive upon clicking on list of experiments (DONE!) #12 RUV for batch effect @@ -18,22 +17,25 @@ #15 genomic ranges R package #16 Have a download button for the adjusted matrix (Done !!) #17 Need to have the back button only in the calculate matrix page and the next is added once the calculation is done (Done!!) -#18 Fix the corrplot download +#18 Fix the corrplot download (done!!) #19 Score matrices and tables other than the first, lines don't need to be selected (Done!) #20 Filter coverage files from the beginning with a check box (Done!) +#21 Make batch effect analysis matrices as a list +#22 Notification to choose a chromosome (Done!) # TODOs ------------------------------------------------------------------- -#9 Change all the plots to be in Plotly instead of ggplot #7 Check for shiny as a R package #12 RUV for batch effect (from Markus' scripts to understand how it works) #13 select expression, for gene expression data -#15 genomic ranges R package -#18 Fix the corrplot download - #It's working, but for some reason the PDFs are taking long time to show - #The plot maybe a bit too big, even thought the pdf file has a small size #21 Make batch effect analysis matrices as a list -#22 Notification to choose a chromosome +#22 I can't retrieve chromosomes anymore? + + +#Problems need to be solved +#1 Can't find chromosomes for each genome anymore (solved!!) +#2 I can't get the rna-seq for most of the projects +#3 #New UI with shiny dashboard library(foreach) @@ -58,22 +60,24 @@ library(shinydashboard) #importing functions for(f in list.files("../functions/")){ - source(paste0('../functions/',f), local = TRUE) + source(file.path('../functions/',f), local = TRUE) } # Server function --------------------------------------------------------- function(input, output, session) { #identifying some variables and reactive variables + #Need to review some of these variables and get rid of some inserted_inputs <- c() echo <- c() to <- c() user_key <- c() - genomes <- c() + # genomes <- c() listed_experiments <- reactiveVal(FALSE) calculated_matrix <- reactiveVal(FALSE) calculated_adjusted_matrix <- reactiveVal(FALSE) logged_in <- reactiveVal("") + batch_adjusted_matrices <- reactiveVal(list()) observe(hideTab(inputId = "plot_box", target = "Download Plot")) observe(hideTab(inputId = "corr_plot_box", target = "Downlad Plot")) @@ -86,7 +90,6 @@ function(input, output, session) { # # listing genomes --------------------------------------------------------- source("../server/listing_genomes.r", local = TRUE) - # Listing Experiments ----------------------------------------------------- source("../server/listing_experiments.r", local = TRUE) @@ -159,13 +162,28 @@ function(input, output, session) { #update the keys for batch effects once we calculate the score matrix observeEvent(filtered_score_matrix(),{ - updateSelectInput(session, inputId = "batch", + updateSelectInput(session, inputId = "batch_combat", choices = colnames(attr(filtered_score_matrix(), "meta"))) - updateSelectInput(session, inputId = "adj_var", + updateSelectInput(session, inputId = "adj_var_combat", + choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) + + updateSelectInput(session, inputId = "interest_var_combat", choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) - updateSelectInput(session, inputId = "interest_var", + updateSelectInput(session, inputId = "adj_var_sva", + choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) + + updateSelectInput(session, inputId = "interest_var_sva", + choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) + + updateSelectInput(session, inputId = "batch_supervised_sva", + choices = colnames(attr(filtered_score_matrix(), "meta"))) + + updateSelectInput(session, inputId = "adj_var_supervised_sva", + choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) + + updateSelectInput(session, inputId = "interest_var_supervised_sva", choices = c("",colnames(attr(filtered_score_matrix(), "meta")))) updateSelectInput(session, inputId = "color_by", @@ -177,22 +195,66 @@ function(input, output, session) { selected = "biosource_name") }) -# ComBat matrix ----------------------------------------------------- +# Batch adjusted matrix ----------------------------------------------------- #calculating the new matrix after batch effect batch_adjusted_matrix <- eventReactive(input$calculate_batch_matrix,{ - batch_adjusted_matrix <- combat_batch_effect(filtered_score_matrix = filtered_score_matrix(), - batch = input$batch, - adjustment_var = input$adj_var, - interest_var = input$interest_var) - + isolate({ + if(input$batch_effect_choice == "combat"){ + temp_matrices <- batch_adjusted_matrices() + temp_matrices[[paste(input$batch_effect_choice, input$batch_combat, + input$adj_var_combat, input$interest_var_combat, collapse = "-")]]<- + combat_batch_effect(filtered_score_matrix = filtered_score_matrix(), + batch = input$batch_combat, + adjustment_var = input$adj_var_combat, + interest_var = input$interest_var_combat) + + batch_adjusted_matrices(temp_matrices) + # batch_adjusted_matrices[[paste(input$batch_effect_choice, input$batch_combat, + # input$adj_var_combat, + # input$interest_var_combat, collapse = "-")]] <- + # + # combat_batch_effect(filtered_score_matrix = filtered_score_matrix(), + # batch = input$batch_combat, + # adjustment_var = input$adj_var_combat, + # interest_var = input$interest_var_combat) + + }else if(input$batch_effect_choice == "sva"){ + temp_matrices <- batch_adjusted_matrices() + + temp_matrices[[paste(input$batch_effect_choice, input$batch_sva, + input$adj_var_sva, input$interest_var_sva, collapse = "-")]]<- + + sva_batch_effect(filtered_score_matrix = filtered_score_matrix(), + adjustment_var = input$adj_var_sva, + interest_var = input$interest_var_sva) + + batch_adjusted_matrices(temp_matrices) + + # batch_adjusted_matrices[[paste(input$batch_effect_choice, input$batch_sva, + # input$adj_var_sva, + # input$interest_var_sva, collapse = "-")]] <- + # + # sva_batch_effect(filtered_score_matrix = filtered_score_matrix(), + # adjustment_var = input$adj_var_sva, + # interest_var = input$interest_var_sva) + } + updateSelectInput(session, "corrected_matrices", choices = names(temp_matrices)) + return(batch_adjusted_matrices()) + }) + }) - output$batch_matrix_summary <- DT::renderDataTable({ - matrix_summary_table(matrix_summary = summary(batch_adjusted_matrix()), - col_names = colnames(batch_adjusted_matrix())) + observeEvent(input$corrected_matrices,{ + if(is.null(input$corrected_matrices)){ + output$batch_matrix_summary <- NULL + } + output$batch_matrix_summary <- DT::renderDataTable({ + matrix_summary_table(matrix_summary = summary(batch_adjusted_matrix()[[input$corrected_matrices]]), + col_names = colnames(batch_adjusted_matrix()[[input$corrected_matrices]])) + }) }) - + #Show the download button observeEvent(batch_adjusted_matrix(), { if(calculated_adjusted_matrix() == FALSE){ diff --git a/app/ui.r b/app/ui.r index d0b24be..98c6dea 100644 --- a/app/ui.r +++ b/app/ui.r @@ -229,32 +229,31 @@ dashboardPage(title = "Batch Effect Analysis and Visualization", selectInput("key_pie", "Select Key to check the value distribution", c("")), plotlyOutput("batch_pie_plot", width = "auto", height = "auto"))), br(),br(), + selectInput("corrected_matrices", "Batch Corrected Matrices", choices = NULL), actionButton("calculate_batch_matrix", "Adjust matrix using ComBat") ), shinydashboard::box(width = 6, conditionalPanel( condition = "input.batch_effect_choice == 'combat'", - selectInput('batch', "Select a batch to adjust for",c("")), - selectInput("adj_var", "Adjustment Variable",c("")), - selectInput("interest_var", "Variable of interest", c("")) + selectInput('batch_combat', "Select a batch to adjust for",c("")), + selectInput("adj_var_combat", "Adjustment Variable",c("")), + selectInput("interest_var_combat", "Variable of interest", c("")) ), conditionalPanel( condition = "input.batch_effect_choice == 'sva'", - selectInput("adj_var", "Adjustment Variable",c("")), - selectInput("interest_var", "Variable of interest", c("")) + selectInput("adj_var_sva", "Adjustment Variable",c("")), + selectInput("interest_var_sva", "Variable of interest", c("")) ), conditionalPanel( condition = "input.batch_effect_choice == 'supervised_sva'", - selectInput('batch', "Select a batch to adjust for",c("")), - selectInput("adj_var", "Adjustment Variable",c("")), - selectInput("interest_var", "Variable of interest", c("")) + selectInput('batch_supervised_sva', "Select a batch to adjust for",c("")), + selectInput("adj_var_supervised_sva", "Adjustment Variable",c("")), + selectInput("interest_var_supervised_sva", "Variable of interest", c("")) ), downloadButton("downloadAdjustedMatrix", "Download Data") - - ) ), diff --git a/functions/inserted_chr_list.r b/functions/inserted_chr_list.r index ddc5490..e50bb12 100644 --- a/functions/inserted_chr_list.r +++ b/functions/inserted_chr_list.r @@ -3,8 +3,7 @@ inserted_chr_list <- function(user_key, id_genome, genomes){ where = "afterEnd", ui = tags$div( selectInput('chr', 'Select a Chromosome, or nothign for the whole genome', - c("",deepblue_info(id = genomes$id[grep(id_genome, genomes$name)], - user_key = user_key)$chromosomes$name),selected = "", + c("",deepblue_chromosomes(genome = input$genome, user_key = user_key)),selected = "", multiple = TRUE), id = id_genome ) diff --git a/functions/rna_seq_data.r b/functions/rna_seq_data.r new file mode 100644 index 0000000..e56ece8 --- /dev/null +++ b/functions/rna_seq_data.r @@ -0,0 +1,43 @@ +rna_seq_data <- function(project, + experiments_info_meta, + user_key){ + + all_genes <- readr::read_tsv("../genecode_v23_genes.tsv") + protein_coding_genes <- all_genes[all_genes$gene_type == "protein_coding" & + all_genes$source == "HAVANA",] + protein_coding_genes_id <- protein_coding_genes$gene_id + + if(project == "TEPIC reprocessed IHEC data"){ + output_format <- "CHROMOSOME,START,END,@STRAND(gencode v23),@GENE_NAME(gencode v23),@SAMPLE_ID,TPM" + }else if(project == "DEEP"){ + output_format <- "CHROMOSOME,START,END,@STRAND(gencode v23),@GENE_NAME(gencode v23),@SAMPLE_ID,FPKM" + } + + gene_expr_query <- deepblue_select_expressions( + expression_type = "gene", + gene_model = "gencode v23", + identifiers = protein_coding_genes_id, + project = project, + user_key = user_key) + + gene_expr_request_id <- deepblue_get_regions( + query_id = gene_expr_query, + output_format ="CHROMOSOME,START,END,@STRAND(gencode v23),@GENE_NAME(gencode v23),@SAMPLE_ID,TPM,FPKM", + user_key = user_key) + + while(deepblue_info(gene_expr_request_id)$state == "running"){ + Sys.sleep(10) + } + + gene_expr_regions <- deepblue_download_request_data(gene_expr_request_id) + + gene_expr_df <- as.data.frame(gene_expr_regions) %>% + spread(key = `X.SAMPLE_ID`, value = TPM) + + sample_cols <- grep(colnames(gene_expr_df), pattern = "s[0-9].", value = TRUE) + gene_expr_matrix <- data.matrix(gene_expr_df[,sample_cols]) + rownames(gene_expr_matrix) <- gene_expr_df$X.GENE_NAME.gencode.v23. + + attr(gene_expr_df, "meta") <- experiments_info_meta + return(gene_expr_df) +} \ No newline at end of file