Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring in changes from RSE fork #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33,986 changes: 33,986 additions & 0 deletions data/csv/metaphewas_model1a_extracted.csv

Large diffs are not rendered by default.

33,775 changes: 33,775 additions & 0 deletions data/csv/metaphewas_model1b_extracted.csv

Large diffs are not rendered by default.

Binary file added data/rds/metaphewas_model1a_extracted.RDS
Binary file not shown.
Binary file added data/rds/metaphewas_model1b_extracted.RDS
Binary file not shown.
9 changes: 9 additions & 0 deletions rse_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## EPoCH results R Shiny app

To run the app launch an R shell from the top level repo directory and invoke the following commands:

```
> library(shiny)
> runApp("results_app")
```

183 changes: 183 additions & 0 deletions rse_app/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@

# Load required packages -------------------------------
library(shiny)
library(shinyjs)
library(shinycssloaders)
library(shinyTree)
library(tidyverse)
library(plotly)
library(bslib)

source("data.R")
source("plot.R")

addResourcePath(prefix="img", directoryPath = "./img")

ui <- function(request) {
fluidPage(title = "EPoCH data analysis tool", id = 'epoch',
theme = bs_theme(version = 4, bootswatch = "minty"),#, heading_font = font_google("Segoe UI")),
hr(),
# User Input Section -------------------------------
sidebarLayout(
sidebarPanel( width = 3,
# Java scriptlet to get window dimensions
tags$head(tags$script('
var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
$(window).resize(function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
')),
# Logo
tags$img(src="img/app_logo.png",
width = 185, height = 60, label = tags$h4("logo")),
hr(),
actionButton("load_results","Click to load results"),
hr(),
selectizeInput(inputId = "exposure_choice",
label = tags$h4("Exposure type:"),
choices = NULL,
selected = NULL,
multiple = T,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "outcome_choice",
label = tags$h4("Outcome:"),
choices = NULL,
selected = NULL,
multiple = T,
width = "1200px",
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "model_choice",
label = tags$h4("Model:"),
choices = NULL,
selected = NULL,
multiple = T,
options = list(placeholder = '----------', maxItems = 1)),
hr(),
actionButton("plot_data","Visualise data"),
hr(),
p(HTML("<p>The Exploring Prenatal influences on Childhood Health, or EPoCH, project investigates how parents’
lifestyles in the important prenatal period might affect the health of their children. Please visit
the <a href='https://epoch.blogs.bristol.ac.uk'>EPoCH website</a> for more information. </p>"),
style = "font-size: 85%")
),
# Output of Plot, Data, and Summary -------------------------------
mainPanel( width = 9,
tabsetPanel(id = 'main_tabs',
tabPanel("Plot by exposure", icon = icon("chart-simple"),
tabsetPanel(
tabPanel("Manhattan", icon = icon("chart-simple"),
textOutput('Text1'),
plotlyOutput("exposureManhattanPlot", height="100%")
),
tabPanel("Volcano",
plotlyOutput("exposureVolcanoPlot")
)
)
),
tabPanel("Plot by Outcome", icon = icon("chart-simple"),
tabsetPanel(
tabPanel("Manhattan",
textOutput('Text2'),
plotlyOutput("outcomeManhattanPlot")
),
tabPanel("Volcano",
plotlyOutput("outcomeVolcanoPlot")
)
)
),
tabPanel("Plot by exposure coefficient", icon = icon("chart-simple"),
tabsetPanel(
tabPanel("Select comparisons",
inputPanel(
selectizeInput(inputId = "coeff_person",
label = tags$h4("Person exposed:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "coeff_subclass",
label = tags$h4("Exposure level:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "coeff_exptime",
label = tags$h4("Exposure time:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1))),
tags$div(
selectInput(width = "80%", inputId = "coeff_explink",
label = tags$h4("Exposure linker:"),
choices = NULL,
selected = NULL,
multiple = F)),
actionButton("add_comp","Add comparison"),
hr(),
uiOutput("showActiveLinkers"),
actionButton("clear_comps","Clear comparisons")),
tabPanel("Plots", icon = icon("chart-simple"),
plotlyOutput("exposureCoeffPlot"))
)),
tabPanel("Forest plots", icon = icon("chart-simple"),
tabsetPanel(
tabPanel("Select comparisons",
inputPanel(selectizeInput(inputId = "forest_person",
label = tags$h4("Person exposed:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "forest_subclass",
label = tags$h4("Exposure level:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "forest_exptime",
label = tags$h4("Exposure time:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1)),
selectizeInput(inputId = "forest_outcometype",
label = tags$h4("Outcome type:"),
choices = NULL,
selected = NULL,
options = list(placeholder = '----------', maxItems = 1))
),
tags$div(
selectInput(width = "80%", inputId = "forest_explink",
label = tags$h4("Exposure linker:"),
choices = NULL,
selected = NULL,
multiple = F),
selectInput(width = "80%", inputId = "forest_outlink",
label = tags$h4("Outcome linker:"),
choices = NULL,
selected = NULL,
multiple = F))),
tabPanel("Plots", icon = icon("chart-simple"),plotlyOutput("forestPlot"),
hr(),
uiOutput("showForestLinkers")),
)
)
)
)
)
)
}

server <- function(input, output, session) {

global_data <- reactiveValues(data = NULL, data_is_loaded = FALSE,
coeff_linkers = NULL)

source("data_server.R",local=T)$value
source("plot_server.R",local=T)$value
}

# Load UI and server controls
shinyApp(ui = ui, server = server)
142 changes: 142 additions & 0 deletions rse_app/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
library(shiny)
library(tidyverse)
library(plotly)
library(shinycssloaders)
library(shinyjs)
library(shinyTree)
library(RCurl)


format_loaded_data <- function(global_data, loaded_data){
# a bit of tidying will happen (stick it all together, make lists of unique exposure and outcome classes, make a tibble of unique combinations, put everything in a list and name the objects)
loaded_data <- bind_rows(loaded_data)
global_data$exp_classes <- unique(loaded_data$exposure_class)
global_data$out_classes <- unique(loaded_data$outcome_class)
all_res_tib <- as_tibble(unique(loaded_data[,c("exposure_class", "exposure_subclass", "person_exposed", "exposure_time", "exposure_type", "exposure_source", "exposure_dose", "model")]))
formatted_data <- list(loaded_data,global_data$exp_classes,global_data$out_classes,all_res_tib)
names(formatted_data) <- c("all_res","exp_classes","out_classes","all_res_tib")

formatted_data
}

import_data_dropbox <- function(global_data){
dropbox_links <- c("https://www.dropbox.com/s/amagzojd4xmj66l/metaphewas_model1a_extracted.RDS?dl=1",
"https://www.dropbox.com/s/3ey11hxpqlo6i2h/metaphewas_model1b_extracted.RDS?dl=1"
)
all_res <- lapply(dropbox_links,function(x) readRDS(url(x)))
imported_data <- format_loaded_data(global_data, all_res)

return(imported_data)
}

import_data_local <- function(global_data){
file_paths <- c("../data/rds/metaphewas_model1a_extracted.RDS",
"../data/rds/metaphewas_model1b_extracted.RDS"
)
all_res <- lapply(file_paths,function(x) readRDS(x))
imported_data <- format_loaded_data(global_data, all_res)

return(imported_data)
}

create_exposure_dfs <- function(exposureclass, dat){

if (exposureclass == "all") {
df <- dat[dat$person_exposed!="child",]
} else {
df <- dat[dat$exposure_class==exposureclass&dat$person_exposed!="child",]
}

df$exposure_dose_ordered <- factor(df$exposure_dose,ordered=T,levels=c("light","moderate","heavy"))
df <- df[order(df$exposure_subclass,df$exposure_time,df$exposure_dose_ordered),]
df$exposure_subclass_time_dose <- paste(df$exposure_subclass,df$exposure_time,df$exposure_dose)
df$exposure_subclass_time_dose<-factor(df$exposure_subclass_time_dose,ordered=T,levels=unique(df$exposure_subclass_time_dose))
# to convert ln(OR) to cohen's d (SDM), multiply ln(OR) by sqrt(3)/pi (which is 0.5513)
# to convert SDM to ln(OR), multiply SDM by pi/sqrt(3) (which is 1.814)
# https://www.meta-analysis.com/downloads/Meta-analysis%20Converting%20among%20effect%20sizes.pdf
# https://onlinelibrary.wiley.com/doi/abs/10.1002/1097-0258(20001130)19:22%3C3127::AID-SIM784%3E3.0.CO;2-M
df$est_SDM <- df$est
df$est_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$est[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df$se_SDM <- df$se
df$se_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$se[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df
}

create_outcome_dfs <- function(outcomeclass, dat){

if (outcomeclass == "all") {
df <- dat[dat$person_exposed!="child",]
} else {
df <- dat[dat$outcome_class==outcomeclass&dat$person_exposed!="child",]
}

df$outcome_time_ordered <- factor(df$outcome_time,ordered=T,levels=c("pregnancy","delivery","first year", "age 1-2","age 3-4","age 5-7","age 8-11","anytime in childhood"))
df <- df[order(df$outcome_subclass1,df$outcome_subclass2,df$outcome_time_ordered),]
df$outcome_subclass_time <- paste(df$outcome_subclass1,df$outcome_subclass2,df$outcome_time)
df$outcome_subclass_time<-factor(df$outcome_subclass_time,ordered=T,levels=unique(df$outcome_subclass_time))
# to convert ln(OR) to cohen's d (SDM), multiply ln(OR) by sqrt(3)/pi (which is 0.5513)
# to convert SDM to ln(OR), multiply SDM by pi/sqrt(3) (which is 1.814)
# https://www.meta-analysis.com/downloads/Meta-analysis%20Converting%20among%20effect%20sizes.pdf
# https://onlinelibrary.wiley.com/doi/abs/10.1002/1097-0258(20001130)19:22%3C3127::AID-SIM784%3E3.0.CO;2-M
df$est_SDM <- df$est
df$est_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$est[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df$se_SDM <- df$se
df$se_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$se[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df
}

create_exposure_dfs <- function(exposureclass, dat){

if (exposureclass == "all") {
df <- dat[dat$person_exposed!="child",]
} else {
df <- dat[dat$exposure_class==exposureclass&dat$person_exposed!="child",]
}

df$exposure_dose_ordered <- factor(df$exposure_dose,ordered=T,levels=c("light","moderate","heavy"))
df <- df[order(df$exposure_subclass,df$exposure_time,df$exposure_dose_ordered),]
df$exposure_subclass_time_dose <- paste(df$exposure_subclass,df$exposure_time,df$exposure_dose)
df$exposure_subclass_time_dose<-factor(df$exposure_subclass_time_dose,ordered=T,levels=unique(df$exposure_subclass_time_dose))
# to convert ln(OR) to cohen's d (SDM), multiply ln(OR) by sqrt(3)/pi (which is 0.5513)
# to convert SDM to ln(OR), multiply SDM by pi/sqrt(3) (which is 1.814)
# https://www.meta-analysis.com/downloads/Meta-analysis%20Converting%20among%20effect%20sizes.pdf
# https://onlinelibrary.wiley.com/doi/abs/10.1002/1097-0258(20001130)19:22%3C3127::AID-SIM784%3E3.0.CO;2-M
df$est_SDM <- df$est
df$est_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$est[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df$se_SDM <- df$se
df$se_SDM[df$outcome_type=="binary"|df$outcome_type=="ordinal"]<-df$se[df$outcome_type=="binary"|df$outcome_type=="ordinal"]*0.5513
df
}

create_forest_dfs <- function(dat, exp_linker, out_linker){
extracted_res <- dat[which(dat$exposure_linker==exp_linker &
dat$outcome_linker==out_linker),]

# which cohorts contributed to the meta-analysis
cohorts <- unlist(strsplit(extracted_res$cohorts,split=","))
cohorts <-str_remove(cohorts," ")

#prepare data:
df <- data.frame(exposure=rep(extracted_res$exposure_linker, length(cohorts)+1),
outcome=rep(extracted_res$outcome_linker, length(cohorts)+1),
cohort=c("meta",cohorts),
binary=c(extracted_res$outcome_type=="binary"),
est=c(extracted_res$est,#meta-analysis estimate
unlist(extracted_res[,paste0("est_",cohorts)])), #estimates for each cohort
se=c(extracted_res$se,#meta-analysis standard errror
unlist(extracted_res[,paste0("se_",cohorts)])), #standard errors for each cohort
n=c(extracted_res$total_n,#meta-analysis sample size
unlist(extracted_res[,paste0("n_",cohorts)])), #sample size for each cohort
P=c(extracted_res$p,#meta-analysis P-value
unlist(extracted_res[,paste0("p_",cohorts)])) #P-value for each cohort
)
df$lci <- df$est-(1.96*df$se) #lower 95% CI
df$uci <- df$est+(1.96*df$se) #upper 95% CI

df$cohort<-factor(df$cohort,ordered=T,levels=c("meta",sort(cohorts,decreasing = T))) #ordering so that meta-analysis statistic comes bottom
df$point_size <- c(1, #size 1 for meta-analysis results
sqrt(sqrt(1/df$se[-1]/100)) #then all other cohorts will have points sized smaller than 1, but proportionate to their contribution to the meta-analysis (i.e. the inverse of the variance)

)
df
}
Loading