diff --git a/msa/.global.R.swp b/msa/.global.R.swp deleted file mode 100644 index ae9e887..0000000 Binary files a/msa/.global.R.swp and /dev/null differ diff --git a/msa/global.R b/msa/global.R index 92e2cc4..9873d8b 100644 --- a/msa/global.R +++ b/msa/global.R @@ -1,17 +1,47 @@ +# *************************** +# ** I M P O R T A N T ** +# *************************** + +# DO NOT USE OR MODIFY THIS. +# USE THE ONE IN THE 'Dashboards' +# REPO + + + + + library(shinycssloaders) library(DT) library(NGLVieweR) library(hash) +# FIXME This is slow and should happen *once only* +#source("~/git/LSHTM_analysis/scripts/Header_TT.R") + +# FIXME: these are needed but slow to load every time +# source("~/git/LSHTM_analysis/config/alr.R") +# source("~/git/LSHTM_analysis/config/gid.R") + +# source("~/git/LSHTM_analysis/config/pnca.R") +# source("~/git/LSHTM_analysis/config/rpob.R") +# source("~/git/LSHTM_analysis/config/katg.R") + +# TODO: this is TEMPORARY and will shortly get replaced with a target picker +# that'll reload everything when changing targets. the lapply() is *much* quicker +# than previous approaches + +# system.time({ +source("~/git/LSHTM_analysis/scripts/Header_TT.R") + load_target_globals=function(target){ cat(paste0("Reloading Target: ", target)) - source(paste0("/srv/shiny-server/git/LSHTM_analysis/config/", target, ".R")) # load per-target config file - - invisible(assign(paste0(target, "_merged_df3"), read.csv(paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/",target,"-merged_df3.csv")), envir = .GlobalEnv)) - invisible(assign(paste0(target, "_merged_df2"), read.csv(paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/",target,"-merged_df2.csv")), envir = .GlobalEnv)) - invisible(assign(paste0(target, "_corr_df_m3_f"), read.csv(paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/",target,"-corr_df_m3_f.csv")), envir = .GlobalEnv)) - invisible(assign(paste0(target, "_lin_lf"), read.csv(paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/",target,"-lin_lf.csv")), envir = .GlobalEnv)) - invisible(assign(paste0(target, "_lin_wf"), read.csv(paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/",target,"-lin_wf.csv")), envir = .GlobalEnv)) + source(paste0("~/git/LSHTM_analysis/config/", target, ".R")) # load per-target config file + + invisible(assign(paste0(target, "_merged_df3"), read.csv(paste0("~/git/Misc/shiny_dashboard/data/",target,"-merged_df3.csv")), envir = .GlobalEnv)) + invisible(assign(paste0(target, "_merged_df2"), read.csv(paste0("~/git/Misc/shiny_dashboard/data/",target,"-merged_df2.csv")), envir = .GlobalEnv)) + invisible(assign(paste0(target, "_corr_df_m3_f"), read.csv(paste0("~/git/Misc/shiny_dashboard/data/",target,"-corr_df_m3_f.csv")), envir = .GlobalEnv)) + invisible(assign(paste0(target, "_lin_lf"), read.csv(paste0("~/git/Misc/shiny_dashboard/data/",target,"-lin_lf.csv")), envir = .GlobalEnv)) + invisible(assign(paste0(target, "_lin_wf"), read.csv(paste0("~/git/Misc/shiny_dashboard/data/",target,"-lin_wf.csv")), envir = .GlobalEnv)) lapply( c( "duet", @@ -26,12 +56,12 @@ load_target_globals=function(target){ "mcsm_ppi2"#, #"mcsm_na" ), function(x){ - wf_filename=paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/", tolower(gene), "-wf_", x ,".csv") + wf_filename=paste0("~/git/Misc/shiny_dashboard/data/", tolower(gene), "-wf_", x ,".csv") wf_var=paste0("wf_",x) if (file.exists(wf_filename)){ invisible(assign(wf_var,read.csv(wf_filename), envir = .GlobalEnv)) # FILTH } - lf_filename=paste0("/srv/shiny-server/git/Misc/shiny_dashboard/data/", tolower(gene), "-lf_", x ,".csv") + lf_filename=paste0("~/git/Misc/shiny_dashboard/data/", tolower(gene), "-lf_", x ,".csv") lf_var=paste0(target, "_lf_",x) if (file.exists(lf_filename)){ invisible(assign(lf_var,read.csv(lf_filename), envir = .GlobalEnv)) # FILTH @@ -42,37 +72,24 @@ load_target_globals=function(target){ # populate target-specific *_unified_msa vars load_msa_global=function(gene){ drug=target_map[[gene]] - in_filename_msa = paste0(tolower(gene), "_msa.csv") - infile_msa = paste0("/srv/shiny-server/git/Data/", drug, "/output/", in_filename_msa) + in_filename_msa = paste0(tolower(gene), "_msa.csv") + infile_msa = paste0("~/git/Data/", drug, "/output/", in_filename_msa) print(infile_msa) msa1 = read.csv(infile_msa, header = F) msa_seq = msa1$V1 - - infile_fasta = paste0("/srv/shiny-server/git/Data/", drug, "/input/", tolower(gene), "2_f2.fasta") + + infile_fasta = paste0("~/git/Data/", drug, "/input/", tolower(gene), "2_f2.fasta") print(infile_fasta) msa2 = read.csv(infile_fasta, header = F) wt_seq = msa2$V1 - + target_name=paste0(gene, '_unified_msa') #print(target_name) invisible(assign(target_name, list(msa_seq = msa_seq, wt_seq = wt_seq), envir = .GlobalEnv)) } #### Local Functions #### -# Generate a conditionalPanel() for a given graph function and sidebar name combination -generate_conditionalPanel = function(tab_name, plot_function, plot_df){ - # e.g.: list("lin_count_bp_diversity", "Lineage diversity count") - cond=paste0("input.sidebar == '", tab_name, "'") - conditionalPanel(condition=cond, box( - title=tab_name - , status = "info" - , width=NULL - , plotOutput(plot_function - , click = "plot_click") %>% withSpinner(color="#0dc5c1") - # , plotOutput(plot_function, click = "plot_click") - ) - ) -} + # FIXME: passing in the per-plot params is broken lin_sc=function(x, all_lineages = F, ...){ @@ -180,7 +197,7 @@ table_columns = c( "snap2_outcome", "consurf_outcome", "provean_outcome", - "rsa", + "rsa", "kd_values" , "rd_values" ) @@ -197,26 +214,6 @@ dm_om_methods = c("DUET ΔΔG" , "Ligand affinity (log fold change)" , "mCSM-NA affinity ΔΔG" , "SNAP2") -dm_om_map = hash(c( - "DUET ΔΔG" - , "Consurf" - , "Deepddg ΔΔG" - , "Dynamut2 ΔΔG" - , "FoldX ΔΔG" - , "Ligand affinity (log fold change)" - , "mCSM-NA affinity ΔΔG" - , "SNAP2" -), c( - "lf_duet" - ,"lf_consurf" - ,"lf_deepddg" - ,"lf_dynamut2" - ,"lf_foldx" - ,"lf_mcsm_lig" - ,"lf_mcsm_na" - ,"lf_snap2" -) -) #### target_map: handy gene/drug mapping hash #### target_map = hash( c( @@ -235,7 +232,7 @@ target_map = hash( "isoniazid") ) -# load E V E R Y T H I N G +#load E V E R Y T H I N G lapply(c( "alr", "embb", @@ -245,57 +242,188 @@ lapply(c( "rpob" ),function(x){ invisible(load_target_globals(x)) - invisible(load_msa_global(x)) # turn off to speed up start time at the expense of "LogoP ED" + invisible(load_msa_global(x)) } ) -consurf_palette1 = c("0" = "yellow2" - , "1" = "cyan1" - , "2" = "steelblue2" - , "3" = "cadetblue2" - , "4" = "paleturquoise2" - , "5" = "thistle3" - , "6" = "thistle2" - , "7" = "plum2" - , "8" = "maroon" - , "9" = "violetred2") - -consurf_palette2 = c("0" = "yellow2" - , "1" = "forestgreen" - , "2" = "seagreen3" - , "3" = "palegreen1" - , "4" = "darkseagreen2" - , "5" = "thistle3" - , "6" = "lightpink1" - , "7" = "orchid3" - , "8" = "orchid4" - , "9" = "darkorchid4") - -# decreasing levels mess legend -# consurf_colours_LEVEL = c( -# "0" = rgb(1.00,1.00,0.59) -# , "9" = rgb(0.63,0.16,0.37) -# , "8" = rgb(0.94,0.49,0.67) -# , "7" = rgb(0.98,0.78,0.86) -# , "6" = rgb(0.98,0.92,0.96) -# , "5" = rgb(1.00,1.00,1.00) -# , "4" = rgb(0.84,0.94,0.94) -# , "3" = rgb(0.65,0.86,0.90) -# , "2" = rgb(0.29,0.69,0.75) -# , "1" = rgb(0.04,0.49,0.51) -# ) - -consurf_colours = c( - "0" = rgb(1.00,1.00,0.59) - , "1" = rgb(0.04,0.49,0.51) - , "2" = rgb(0.29,0.69,0.75) - , "3" = rgb(0.65,0.86,0.90) - , "4" = rgb(0.84,0.94,0.94) - , "5" = rgb(1.00,1.00,1.00) - , "6" = rgb(0.98,0.92,0.96) - , "7" = rgb(0.98,0.78,0.86) - , "8" = rgb(0.94,0.49,0.67) - , "9" = rgb(0.63,0.16,0.37) +#### Shiny UI ##### +if(interactive()){ +ui <- dashboardPage( + #dashboardHeader(title = paste0(gene, "/", drug)), + dashboardHeader(title = "Sequence Alignment"), + + dashboardSidebar( + sidebarMenu( id = "sidebar", + selectInput( + "switch_target", + label="Target", + choices = c( + "alr", + "embb", + "gid", + "katg", + "pnca", + "rpob" + ), + selected="embb"), + menuItem("LogoP ED", tabName="LogoP ED"), + + sliderInput( + "display_position_full_range" + , "Display Positions" + , min=1, max=150, value=c(1,150) + ), + + conditionalPanel( + condition=" + input.sidebar == 'LogoP SNP' || + input.sidebar == 'LogoP OR' || + input.sidebar == 'LogoP ED'", + selectInput( + "logoplot_colour_scheme", + label="Logo Plot Colour Scheme", + choices = logoPlotSchemes, + selected="chemistry" + ) + ) + ) + ), + body <- dashboardBody( + + tabItems( + tabItem(tabName = "dashboard", + h2("Dashboard tab content") + ), + + tabItem(tabName = "widgets", + h2("Widgets tab content") + ) + ), + # creates a 'Conditional Panel' containing a plot object from each of our + # ggplot plot functions (and its associated data frame) + fluidRow( + column( + width=12, + plotOutput("LogoPlotMSA", + click = "plot_click") %>% withSpinner(color="#0dc5c1") + ) + ) + ) ) +#### Shiny Backend Server ##### +server <- function(input, output, session) { + observeEvent( + { + input$display_position_full_range #special-purpose for MSA + input$logoplot_colour_scheme + input$switch_target + }, + { + # C O M P A T I B I L I T Y + #gene=input$switch_target + #drug=target_map[[gene]] + merged_df3=cbind(get(paste0(input$switch_target, '_merged_df3'))) + + position_max=max(merged_df3[['position']]) + position_min=min(merged_df3[['position']]) + min_ligand_distance=min(merged_df3$ligand_distance) + max_ligand_distance=max(merged_df3$ligand_distance) + # FIXME: these are IMPORTANT + # # add "pos_count" position count column + # merged_df3=merged_df3 %>% dplyr::add_count(position) + # merged_df3$pos_count=merged_df3$n + # merged_df3$n=NULL + # + mutable_df3 = cbind(merged_df3) + unified_msa = get(paste0(input$switch_target, '_unified_msa')) + # + # # re-sort the dataframe according to position count + sorted_df = cbind(merged_df3) + sorted_df = sorted_df %>% arrange(pos_count) + + # + outdir = paste0("~/git/Data/", drug, '/output/') + indir = paste0("~/git/Data/", drug , "/input/") + # + # source("~/git/LSHTM_analysis/scripts/plotting/logo_data_msa.R") # probably unnecessary... + + # source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R") + + #### nasty special-purpose merged_df3 variants #### + # FIXME: SLOW + # corr_plotdf = corr_data_extract( + # merged_df3 + # , gene = gene + # , drug = drug + # , extract_scaled_cols = F + # ) + + #input$stability_snp_param + + updateSliderInput( + session, + "display_position_range", + min = position_min, + max = position_max + #, value = c(position_min, position_min+150) + ) + updateSliderInput( + session, + "display_position_full_range", + min = 1, + max = position_max #, + # value = c(position_min, position_min+150) + ) + updateNumericInput(session, "selected_logop_snp_position", min = position_min, max = position_max, value = position_min) + updateNumericInput(session, "selected_logop_ed_position", min = position_min, max = position_max, value = position_min) + updateNumericInput(session, "corr_lig_dist", min = min_ligand_distance, max = max_ligand_distance, value = min_ligand_distance) + + stability_colname = stability_boxes_df[stability_boxes_df$stability_type==input$stability_snp_param,"stability_colname"] + outcome_colname = stability_boxes_df[stability_boxes_df$stability_type==input$stability_snp_param,"outcome_colname"] + + display_position_range = input$display_position_range + plot_min=display_position_range[1] + plot_max=display_position_range[2] + + display_position_full_range = input$display_position_full_range + full_range_min=display_position_full_range[1] + full_range_max=display_position_full_range[2] + + logoplot_colour_scheme = input$logoplot_colour_scheme + omit_snp_count = input$omit_snp_count + + print(paste0('Plotting positions between: ', plot_min, ' and ', plot_max)) + + subset_mutable_df3=mutable_df3[(mutable_df3$position>=plot_min & mutable_df3$position <=plot_max),] + + subset_mutable_df3=mutable_df3[(mutable_df3$position>=plot_min & mutable_df3$position <=plot_max),] + subset_sorted_df=sorted_df[(sorted_df$position>=plot_min & sorted_df$position <=plot_max),] + + + + #### LogoPlotMSA/Logo Plot ED #### + output$LogoPlotMSA = renderPlot( + LogoPlotMSA(target=input$switch_target, + plot_positions=full_range_min:full_range_max, + my_logo_col = logoplot_colour_scheme, + aa_pos_drug = aa_pos_drug, + active_aa_pos = active_aa_pos, + aa_pos_lig1 = aa_pos_lig1, + aa_pos_lig2 = aa_pos_lig2, + aa_pos_lig3 = aa_pos_lig3 + ) + ) + } + ) + + #### EOF Shiny Server #### + + + +} +################ Running Server ############## +app <- shinyApp(ui, server) +runApp(app) +} \ No newline at end of file diff --git a/msa/server.R b/msa/server.R index e69de29..542fe34 100644 --- a/msa/server.R +++ b/msa/server.R @@ -0,0 +1,105 @@ +function(input, output, session) { + observeEvent( + { + input$display_position_full_range #special-purpose for MSA + input$logoplot_colour_scheme + input$switch_target + }, + { + # C O M P A T I B I L I T Y + #gene=input$switch_target + #drug=target_map[[gene]] + merged_df3=cbind(get(paste0(input$switch_target, '_merged_df3'))) + + position_max=max(merged_df3[['position']]) + position_min=min(merged_df3[['position']]) + min_ligand_distance=min(merged_df3$ligand_distance) + max_ligand_distance=max(merged_df3$ligand_distance) + # FIXME: these are IMPORTANT + # # add "pos_count" position count column + # merged_df3=merged_df3 %>% dplyr::add_count(position) + # merged_df3$pos_count=merged_df3$n + # merged_df3$n=NULL + # + mutable_df3 = cbind(merged_df3) + unified_msa = get(paste0(input$switch_target, '_unified_msa')) + # + # # re-sort the dataframe according to position count + sorted_df = cbind(merged_df3) + sorted_df = sorted_df %>% arrange(pos_count) + + # + outdir = paste0("~/git/Data/", drug, '/output/') + indir = paste0("~/git/Data/", drug , "/input/") + # + # source("~/git/LSHTM_analysis/scripts/plotting/logo_data_msa.R") # probably unnecessary... + + # source("~/git/LSHTM_analysis/scripts/plotting/get_plotting_dfs.R") + + #### nasty special-purpose merged_df3 variants #### + # FIXME: SLOW + # corr_plotdf = corr_data_extract( + # merged_df3 + # , gene = gene + # , drug = drug + # , extract_scaled_cols = F + # ) + + #input$stability_snp_param + + updateSliderInput( + session, + "display_position_range", + min = position_min, + max = position_max + #, value = c(position_min, position_min+150) + ) + updateSliderInput( + session, + "display_position_full_range", + min = 1, + max = position_max #, + # value = c(position_min, position_min+150) + ) + updateNumericInput(session, "selected_logop_snp_position", min = position_min, max = position_max, value = position_min) + updateNumericInput(session, "selected_logop_ed_position", min = position_min, max = position_max, value = position_min) + updateNumericInput(session, "corr_lig_dist", min = min_ligand_distance, max = max_ligand_distance, value = min_ligand_distance) + + stability_colname = stability_boxes_df[stability_boxes_df$stability_type==input$stability_snp_param,"stability_colname"] + outcome_colname = stability_boxes_df[stability_boxes_df$stability_type==input$stability_snp_param,"outcome_colname"] + + display_position_range = input$display_position_range + plot_min=display_position_range[1] + plot_max=display_position_range[2] + + display_position_full_range = input$display_position_full_range + full_range_min=display_position_full_range[1] + full_range_max=display_position_full_range[2] + + logoplot_colour_scheme = input$logoplot_colour_scheme + omit_snp_count = input$omit_snp_count + + print(paste0('Plotting positions between: ', plot_min, ' and ', plot_max)) + + subset_mutable_df3=mutable_df3[(mutable_df3$position>=plot_min & mutable_df3$position <=plot_max),] + + subset_mutable_df3=mutable_df3[(mutable_df3$position>=plot_min & mutable_df3$position <=plot_max),] + subset_sorted_df=sorted_df[(sorted_df$position>=plot_min & sorted_df$position <=plot_max),] + + + + #### LogoPlotMSA/Logo Plot ED #### + output$LogoPlotMSA = renderPlot( + LogoPlotMSA(target=input$switch_target, + plot_positions=full_range_min:full_range_max, + my_logo_col = logoplot_colour_scheme, + aa_pos_drug = aa_pos_drug, + active_aa_pos = active_aa_pos, + aa_pos_lig1 = aa_pos_lig1, + aa_pos_lig2 = aa_pos_lig2, + aa_pos_lig3 = aa_pos_lig3 + ) + ) + } + ) +} \ No newline at end of file diff --git a/msa/ui.R b/msa/ui.R index e69de29..2fe5bc6 100644 --- a/msa/ui.R +++ b/msa/ui.R @@ -0,0 +1,62 @@ +dashboardPage( + #dashboardHeader(title = paste0(gene, "/", drug)), + dashboardHeader(title = "Sequence Alignment"), + + dashboardSidebar( + sidebarMenu( id = "sidebar", + selectInput( + "switch_target", + label="Target", + choices = c( + "alr", + "embb", + "gid", + "katg", + "pnca", + "rpob" + ), + selected="embb"), + menuItem("LogoP ED", tabName="LogoP ED"), + + sliderInput( + "display_position_full_range" + , "Display Positions" + , min=1, max=150, value=c(1,150) + ), + + conditionalPanel( + condition=" + input.sidebar == 'LogoP SNP' || + input.sidebar == 'LogoP OR' || + input.sidebar == 'LogoP ED'", + selectInput( + "logoplot_colour_scheme", + label="Logo Plot Colour Scheme", + choices = logoPlotSchemes, + selected="chemistry" + ) + ) + ) + ), + body <- dashboardBody( + + tabItems( + tabItem(tabName = "dashboard", + h2("Dashboard tab content") + ), + + tabItem(tabName = "widgets", + h2("Widgets tab content") + ) + ), + # creates a 'Conditional Panel' containing a plot object from each of our + # ggplot plot functions (and its associated data frame) + fluidRow( + column( + width=12, + plotOutput("LogoPlotMSA", + click = "plot_click") %>% withSpinner(color="#0dc5c1") + ) + ) + ) +) \ No newline at end of file