---
title: "analyze_xwas_results"
author: "Saideep Gona"
date: "2024-06-07"
format:
html:
code-fold: true
code-summary: "Show the code"
execute:
freeze: true
warning: false
server: shiny
description: "After running summary predixcan on different epigeneti marks and disease conditions, we can analyze the results to see if these marks help identify associations with disease."
---
```{r}
library (plotly)
library (ggplot2)
library (shiny)
# library(htmlwidgets)
path_to_full_spredixcan_results = "/beagle3/haky/users/saideep/projects/aracena_modeling/SPrediXcan/sumstats_formatted/asthma_eczema_farreira_susie/enformer_preds/spredixcan/spredixcan_results_full.txt"
full_spredixcan_results = read.table (path_to_full_spredixcan_results, header = T, sep = " \t " )
print (head (full_spredixcan_results))
full_spredixcan_results$ neglog10p = - log10 (full_spredixcan_results$ pvalue)
```
```{r}
library (ggplot2)
# ggplot(full_spredixcan_results, aes(x=modality, y=gene, fill=pvalue)) +
# geom_raster() +
# theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue", high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$condition), scales = "free", space="free_y") #facets to add gaps
```
```{r}
# library(ggplot2)
# ggplot(full_spredixcan_results, aes(x=condition, y=gene, fill=pvalue)) +
# geom_raster() +
# theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue", high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$modality), scales = "free", space="free_y") #facets to add gaps
```
```{r}
# ggplot(full_spredixcan_results, aes(x=modality, y=gene, fill=neglog10p)) +
# geom_raster() +
# theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue", high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$condition), scales = "free", space="free_y") #facets to add gaps
```
```{r}
# ggplot(full_spredixcan_results, aes(x=condition, y=gene, fill=neglog10p)) +
# geom_raster() +
# theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue", high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$modality), scales = "free", space="free_y") #facets to add gaps
```
```{r}
# #| context: server
# library(plotly)
# library(ggplot2)
# library(htmlwidgets)
# # start of copy
# inter_plot1 <- ggplot(full_spredixcan_results, aes(x=condition, y=gene, fill=neglog10p)) +
# geom_raster() +
# # theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# # strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue",
# high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$modality), scales = "free", space="free_y") #facets to add gaps
# fig_plotly <- ggplotly(inter_plot1)
# fig_plotly <- fig_plotly %>%
# layout(
# hovermode = "closest",
# hoverlabel = list(bgcolor = "white",
# font = list(family = "Arial", size = 16, color = "black")
# )
# )
# for(i in seq_along(fig_plotly$x$data)){
# fig_plotly <- fig_plotly %>%
# add_trace(
# data = fig_plotly$x$data[[i]],
# x - fig_plotly$x$data[[i]]$x,
# y = fig_plotly$x$data[[i]]$y,
# type = "scatter",
# mode = "markers",
# marker = list(color = fig_plotly$x$data[[i]]$fill),
# hoverinfo = "text",
# text = ~paste("Gene: ", gene, "<br>Condition: ", condition, "<br>Modality: ", modality, "<br>neglog10p: ", neglog10p)
# )
# }
# # end of copy
# # trace delete
# # fig_plotly$x$data <- fig_plotly$x$data[seq_len(length(fig_plotly$x$data) - 1)]
# # fig_plotly$x$data <- fig_plotly$x$data[seq_len(length(fig_plotly$x$data)/2)]
# fig_plotly
```
```{r}
# UI for Shiny app
# ui <- fluidPage(
# titlePanel(“Population by Continent Over Time”),
# sidebarLayout(
# mainPanel(
# plotlyOutput(“plot”)
# )
# )
# )
# ui <- navbarPage("test",
# tabPanel("test_tab"),
# mainPanel(
# plotlyOutput("plot")
# )
# )
# print(ui)
```
```{r, xwas_app_v2_working_version}
# library(shiny)
# library(plotly)
# library(ggplot2)
# library(htmlwidgets)
# ui <- navbarPage("XWAS App",
# tabPanel("XWAS Tab Name"),
# mainPanel(
# plotlyOutput("plot")
# )
# )
# # Server logic for Shiny app
# server <- function(input, output) {
# # Reactive expression for filtered data
# # filtered_data <- reactive({
# # df_cnt %>% filter(year == input$year)
# # })
# #plot1
# output$plot <- renderPlotly({
# inter_plot1 <- ggplot(full_spredixcan_results, aes(x=condition, y=gene,
# text = paste("Gene: ", gene, "<br>Condition: ", condition, "<br>Modality: ", modality, "<br>neglog10p: ", neglog10p),
# # text = paste("Modality: ", modality),
# fill=neglog10p)) +
# geom_raster() +
# # theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# # strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue",
# high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$modality), scales = "free", space="free_y") #facets to add gaps
# fig_plotly <- ggplotly(inter_plot1, tooltip="text")
# })
# }
# # Run the Shiny app
# shinyApp(ui = ui, server = server)
```
```{r, xwas_app_v3}
# # library(shiny)
# # library(plotly)
# # library(ggplot2)
# # library(htmlwidgets)
# library(plotly)
# library(ggplot2)
# library(shiny)
# # library(htmlwidgets)
# path_to_full_spredixcan_results = "/beagle3/haky/users/saideep/projects/aracena_modeling/SPrediXcan/sumstats_formatted/asthma_eczema_farreira_susie/enformer_preds/spredixcan/spredixcan_results_full.txt"
# full_spredixcan_results = read.table(path_to_full_spredixcan_results, header = T, sep = "\t")
# print(head(full_spredixcan_results))
# full_spredixcan_results$neglog10p = -log10(full_spredixcan_results$pvalue)
# # define global variables
# facet_variables <- c("condition", "modality")
# x_variables <- c("condition", "modality")
# ui <- navbarPage("XWAS App",
# tabPanel("XWAS Tab Name",
# sidebarPanel(t
# h4("Change Plot Settings"),
# selectInput("select_facet_variable",
# "Select facet variable",
# choices = facet_variables,
# selected = choices[1]
# ),
# selectInput("select_x_variable",
# "Select x variable",
# choices = x_variables,
# selected = choices[1]
# )
# ),
# mainPanel(
# h3("XWAS of fine-mapped Allergy Eczema Loci"),
# plotlyOutput("plot1")
# )
# )
# )
# # ui <- navbarPage("XWAS App",
# # tabPanel("XWAS Tab Name",
# # sidebarPanel(
# # h4("Change Plot Settings"),
# # selectInput("select_facet_variable",
# # "Select facet variable",
# # choices = facet_variables,
# # selected = choices[1]
# # ),
# # selectInput("select_x_variable",
# # "Select x variable",
# # choices = x_variables,
# # selected = choices[1]
# # )
# # )
# # ),
# # mainPanel(
# # plotlyOutput("plot1")
# # )
# # )
# # Server logic for Shiny app
# server <- function(input, output, session) {
# # Reactive expression for filtered data
# # filtered_data <- reactive({
# # df_cnt %>% filter(year == input$year)
# # })
# #Used to observe the facet and x selection
# observe({
# facet_variable <- input$select_facet_variable
# second_x_variable <- facet_variables[facet_variables != facet_variable]
# updateSelectInput(session,
# 'select_x_variable',
# choices = second_x_variable,
# selected = second_x_variable[1])
# })
# #Plot1 - Facet = Modality, x = condition
# output$plot1 <- renderPlotly({
# inter_plot1 <- ggplot(full_spredixcan_results, aes(x=input$select_x_variable, y=gene,
# text = paste("Gene: ", gene, "<br>Condition: ", condition, "<br>Modality: ", modality, "<br>neglog10p: ", neglog10p),
# # text = paste("Modality: ", modality),
# fill=neglog10p)) +
# geom_raster() +
# # theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# # strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue",
# high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$input$select_facet_variable), scales = "free", space="free_y") #facets to add gaps
# fig_plotly <- ggplotly(inter_plot1, tooltip="text")
# })
# }
# # Run the Shiny app
# shinyApp(ui = ui, server = server)
```
```{r, hover_text_with_R_and_plotly}
# inter_plot1 <- ggplot(full_spredixcan_results, aes(x=condition, y=gene,
# # text = paste("Gene: ", gene, "<br>Condition: ", condition, "<br>Modality: ", modality, "<br>neglog10p: ", neglog10p),
# text = paste("Modality: ", modality),
# fill=neglog10p)) +
# geom_raster() +
# # theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), # lables vertical
# # strip.text.y = element_blank()) + #remove facet bar on y
# scale_fill_gradient(low = "darkblue",
# high = "lightblue") +
# ggtitle("XWAS of fine-mapped Allergy Eczema Loci") +
# facet_grid(cols = vars(full_spredixcan_results$modality), scales = "free", space="free_y") #facets to add gaps
# fig_plotly <- ggplotly(inter_plot1)
# fig_plotly
```
```{r, xwas_app_v4_latest_working_app}
#| context: server
library (shiny)
library (plotly)
# Sample data for select inputs
facet_variables <- c ("condition" , "modality" )
x_variables <- c ("condition" , "modality" )
color_variables <- c ("neglog10p" , "var_g" )
ui <- navbarPage ("XWAS App" ,
tabPanel ("XWAS Tab Name" ,
sidebarPanel (
h4 ("Change Plot Settings" ),
selectInput ("select_facet_variable" ,
"Select facet variable" ,
choices = facet_variables,
selected = facet_variables[1 ]
),
selectInput ("select_x_variable" ,
"Select x variable" ,
choices = x_variables,
selected = x_variables[1 ]
),
selectInput ("select_color_variable" ,
"Select color variable" ,
choices = color_variables,
selected = color_variables[1 ]
)
),
mainPanel (
h3 ("XWAS of fine-mapped Allergy Eczema Loci" ),
plotlyOutput ("plot1" )
)
)
)
server <- function (input, output, session) {
observe ({
facet_variable <- input$ select_facet_variable
second_x_variable <- facet_variables[facet_variables != facet_variable]
updateSelectInput (session,
'select_x_variable' ,
choices = second_x_variable,
selected = second_x_variable[1 ])
})
# Plot1 - Facet = Modality, x = condition
output$ plot1 <- renderPlotly ({
inter_plot1 <- ggplot (full_spredixcan_results, aes_string (x = input$ select_x_variable, y = "gene" ,
# text = paste(
# "Gene: ", full_spredixcan_results$gene,
# "<br>Condition: ", full_spredixcan_results$condition,
# "<br>Modality: ", full_spredixcan_results$modality,
# "<br>neglog10p: ", full_spredixcan_results$neglog10p),
fill = input$ select_color_variable
)) +
geom_raster (aes (
text = paste (
"Gene: " , gene,
"<br>Condition: " , condition,
"<br>Modality: " , modality,
"<br>neglog10p: " , neglog10p)
)) +
scale_fill_gradient (low = "darkblue" , high = "lightblue" ) +
ggtitle ("XWAS of fine-mapped Allergy Eczema Loci" ) +
facet_grid (cols = vars (full_spredixcan_results[[input$ select_facet_variable]]), scales = "free" , space = "free_y" )
fig_plotly <- ggplotly (inter_plot1, tooltip = "text" )
fig_plotly
})
}
shinyApp (ui = ui, server = server)
```