tb-host-dashboard/server.R

263 lines
12 KiB
R

library(shiny)
library(shinyjs)
library(shinydashboard)
#library("wesanderson") # ayyyy lmao hipster af
library(dplyr)
library(DT)
library(ggplot2)
library(grid) # for the info box
library(plotly)
library(shinycssloaders)
library(NGLVieweR)
library(httr)
library(readr)
library(RCurl)
shinyServer(function(input, output){
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
### NGLViewer ####
# Structure Viewer WebGL/NGLViewR window
output$structure <- renderNGLVieweR({
selected_gene=input$gene
ngl_gene=as.character(genes[genes$Gene==selected_gene,"PDB"])
NGLVieweR(ngl_gene) %>%
addRepresentation("cartoon",
param = list(name = "cartoon",
#color="tan",
colorScheme = "chainid",
opacity = 1
)
) %>%
stageParameters(backgroundColor = "white") %>%
setQuality("high") %>%
setFocus(0) %>%
setSpin(FALSE)
})
output$af_structure <- renderNGLVieweR({
selected_af_gene=input$gene
ngl_af_gene=as.character(genes[genes$Gene==selected_af_gene,"AF_PDB"])
#af_pdb=cat(content(GET(paste0(alphafold_url,"AF-",ngl_af_gene,"-F1-model_v4.pdb")), as="text"), "\n")
af_pdb=content(GET(paste0(alphafold_url,"AF-",ngl_af_gene,"-F1-model_v4.pdb")), as="text")
#print(af_pdb)
#ngl_pdb_file=paste0(load_dir, "Data/", ngl_drug, '/output/depth/', ngl_gene, '_complex.pdb')
NGLVieweR(af_pdb, format="pdb") %>%
addRepresentation("cartoon",
param = list(name = "cartoon",
#color="tan"
colorScheme = "bfactor",
opacity = 1
)
) %>%
stageParameters(backgroundColor = "white") %>%
setQuality("high") %>%
setFocus(0) %>%
setSpin(FALSE)
})
# output$table <- DT::renderDataTable({
# selected_gene=input$gene
# gene=as.character(genes[genes$Gene==selected_gene,"Gene"])
# #unique_missense_genes[unique_missense_genes$gene_name==gene,]
# unique_missense_genes
# },
# selection = "single",
# search = list(search="alr"))
output$table <- DT::renderDataTable(
unique_missense_genes,
selection = "single",
options=list(
search = list(search=as.character(genes[genes$Gene==input$gene,"Gene"]))
)
)
observeEvent(input$table_rows_selected,{
#req(length(input$table_row_selected) > 0)
mutation_pdb = as.character(unique_missense_genes[input$table_rows_selected,"position_pdb"])
mutation_af = as.character(unique_missense_genes[input$table_rows_selected,"position_af"])
mutation = as.character(unique_missense_genes[input$table_rows_selected,"hgvd_p"])
chain = as.character(unique_missense_genes[input$table_rows_selected,"chain"])
# what the absolute FUCK is this mess? I JUST WANT THE MATCH ASDLASJASDASHFKLJASDFK
# coming down from the trees was a mistake, and abandoning Perl doubly so.
#clicked_position = regmatches(mutation, regexpr("[0-9]+", mutation, perl=TRUE))
# Now update the 3D structure to highlight the clicked thing and then zoom in.
NGLVieweR_proxy("structure") %>%
addSelection('spacefill'
, param = list(
name = "Pos"
, sele = trimws(paste0(mutation_pdb,':', chain))
, color = "red"
#, colorValue="00ff00"
#, colorScheme="element"
)
)
NGLVieweR_proxy("af_structure") %>%
addSelection('spacefill'
, param = list(
name = "Pos"
, sele = mutation_af
, color = "red"
#, colorValue="00ff00"
#, colorScheme="element"
)
)
NGLVieweR_proxy("af_structure") %>% updateZoomMove(
center = mutation_af,
zoom = mutation_af,
duration = 1000, # animation time in ms
z_offSet = -1
)
NGLVieweR_proxy("structure") %>% updateZoomMove(
center = trimws(paste0(mutation_pdb,':', chain)),
zoom = trimws(paste0(mutation_pdb,':', chain)),
duration = 1000, # animation time in ms
z_offSet = -1
)
output$information <- renderUI({
selected_gene=input$gene
HTML(paste0("<strong>Mutation: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"mutationinformation_pdb"]),"<br/>",
"<strong>PDB ID: </strong>", as.character(genes[genes$Gene==selected_gene,"PDB"]),"<br/>",
"<strong>AlphaFold ID: </strong>", as.character(genes[genes$Gene==selected_gene,"AF_PDB"]), "<br/>",
"<strong>Ligand Distance: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"ligand_distance"]), "<br/>",
"<strong>mCSM Lig: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"mcsm_lig"]), "<br/>",
"<strong>mmCSM Lig: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"mmcsm_lig"]), "<br/>",
"<strong>mCSM DUET: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"ddg_duet"]), "<br/>",
"<strong>FoldX: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"ddg_foldx"]), "<br/>",
"<strong>DeepDDG: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"ddg_deepddg"]), "<br/>",
"<strong>Dynamut2: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"ddg_dynamut2"]), "<br/>",
"<strong>mCSM PPI2: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"mcsm_ppi2_affinity"]), "<br/>",
"<strong>Interface Distance: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"interface_dist"]), "<br/>",
"<strong>mCSM NA: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"mcsm_na_affinity"]), "<br/>",
"<strong>Consurf: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"consurf_score"]), "<br/>",
#"<strong>SNAP2: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"snap2_score"]), "<br/>",
"<strong>SNAP2 Outcome: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"snap2_outcome"]), "<br/>",
#"<strong>PROVEAN: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"provean_score"]), "<br/>",
"<strong>PROVEAN Outcome: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"provean_outcome"]), "<br/>",
"<strong>RSA: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"rsa"]), "<br/>",
"<strong>Residue Depth: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"rd_values"]), "<br/>",
"<strong>Total Samples: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"total_sample_count"]), "<br/>",
"<strong>Distinct Lineages: </strong>", as.character(unique_missense_genes[input$table_rows_selected,"distinct_lineage_count"]), "<br/>"
)
)
})
# output$debug <- renderPrint({
# print(c(mutation, clicked_position))
# })
})
observeEvent(
{
input$clear_ngl
}, {
NGLVieweR_proxy("structure") %>%
removeSelection("Pos") %>%
removeSelection("all_mutations_surface") %>%
updateVisibility("cartoon", TRUE) %>%
removeSelection("all_mutations")
NGLVieweR_proxy("af_structure") %>%
removeSelection("Pos") %>%
removeSelection("all_mutations_surface") %>%
updateVisibility("cartoon", TRUE) %>%
removeSelection("all_mutations")
})
# add a surface representation and highlight all mutations on it
observeEvent(
{
input$all_mutations
}, {
gene = input$gene
mutations = paste0(":",
as.matrix(unique_missense_genes[unique_missense_genes$gene_name == gene,c("chain")])[1],
" and (",
paste0(
apply(
unique_missense_genes[unique_missense_genes$gene_name == gene,c("position_pdb","chain")],
1,
function(x){
paste0(trimws(x[1])
)}
),
collapse=", "
), ")"
)
mutations_af = paste0(":A and (",
paste0(
apply(
unique_missense_genes[unique_missense_genes$gene_name == gene,c("position_af","chain")],
1,
function(x){
paste0(trimws(x[1])
)}
),
collapse=", "
), ")"
)
#print(mutations)
NGLVieweR_proxy("structure") %>%
updateVisibility("cartoon", FALSE) %>%
addSelection(type="cartoon",
param = list(name = "all_mutations_surface",
sele = "all",
#color="tan",
colorScheme="chainid",
opacity = 0.2
)
) %>%
addSelection(type="spacefill", #spacefill
param = list(name = "all_mutations",
color="orange",
sele = mutations
#colorScheme = "bfactor",
#opacity = 1
)
)
NGLVieweR_proxy("af_structure") %>%
updateVisibility("cartoon", FALSE) %>%
addSelection(type="cartoon",
param = list(name = "all_mutations_surface",
sele = "all",
#color="tan",
colorScheme = "bfactor",
opacity = 0.2
)
) %>%
addSelection(type="spacefill", #spacefill
param = list(name = "all_mutations",
color="orange",
sele = mutations_af
#colorScheme = "bfactor",
#opacity = 1
)
)
output$information <- renderUI({
selected_gene=input$gene
HTML(paste0("<strong>PDB ID: </strong>", as.character(genes[genes$Gene==selected_gene,"PDB"]),"<br/>",
"<strong>AlphaFold ID: </strong>", as.character(genes[genes$Gene==selected_gene,"AF_PDB"]), "<br/>",
"<strong>Gene: </strong>", selected_gene, "<br/>",
"<strong>Mutations Observed: </strong>",as.character(as.matrix(unique_missense_genes[unique_missense_genes[,'gene_name']==selected_gene,"total_muts_per_gene"])[1]), "<br/>"
)
)
})
})
})