[c46e49]: / app.R

Download this file

336 lines (287 with data), 15.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
library(shiny)
library(shinydashboard)
library(plotly)
library(reshape2)
library(survminer)
library(survival)
library(data.table)
library(dplyr)
library(scales)
load("LungDemoData.Rdata")
load("Results.Rdata")
load("Means.Rdata")
ui <- dashboardPage(
dashboardHeader(title = "Prognostic models in metastatic lung adenocarcinoma (BETA)",titleWidth = 400),
dashboardSidebar(width = 200,
sidebarMenu(
menuItem("Dashboard", tabName = "fixed", icon = icon("bar-chart")),
menuItem("Risk Group Stratification", tabName = "strat", icon = icon("scissors")),
menuItem("Gene View", tabName = "gene", icon = icon("gears")),
menuItem("Patient View", tabName = "patient", icon = icon("user-circle-o")),
menuItem("Generate Risk Score", tabName = "risk", icon = icon("gears"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "fixed",
sidebarLayout(
sidebarPanel(
width=12,
h2("Prognostic Performance")
# selectInput("StudyName", "Choose a Study :",
# choices = c("Lung"
# )),
# checkboxInput("AddCNV", "Include copy number data", FALSE),
# checkboxInput("OnlyCNV", "Consider only copy number data", FALSE),
#
# selectInput("Method", "Choose a Method :",
# choices = c("Penalized regression"="LASSO")),
# submitButton("Submit")
),
mainPanel(width=12,
#htmlOutput("VariableHeader"),
htmlOutput("RiskHeader"),
plotOutput("RiskHistogram"),
tableOutput("RiskSummary"),
htmlOutput("CIHeader"),
tableOutput("CI"),
htmlOutput("RefitHeader"),
tableOutput("RefitRisk"),
htmlOutput("CommentPval"),
tableOutput("ClinRefit")#,
#htmlOutput("EffectHeader"),
#htmlOutput("FreqHeader")#,
#plotOutput("influencePlot"),
)
)
),
# First tab content
tabItem(tabName = "strat",
#
# sidebarPanel( width = 12,
# checkboxInput("ShowVolvano", "Show Volcano Plot", TRUE),
# checkboxInput("ShowPies", "Show Pie Charts", TRUE),
# submitButton("Submit")
# ),
sidebarLayout(
sidebarPanel(width = 12,
h2("Risk Group Stratification")
),
mainPanel(width = 12,
htmlOutput("predRiskText"),
htmlOutput("KMText"),
plotOutput("KM"),
tableOutput("SurvSum"),
htmlOutput("MutGroupText"),
htmlOutput("MutBPText"),
plotOutput("Mut")
)
)
),
tabItem(tabName = "gene",
h2("Exploratory interactive gene plots"),
# sidebarPanel( width = 12,
# checkboxInput("ShowVolvano", "Show Volcano Plot", TRUE),
# checkboxInput("ShowPies", "Show Pie Charts", TRUE),
# submitButton("Submit")
# ),
sidebarLayout(
sidebarPanel(width = 12,
textInput("GeneListRisk",
"Find gene(s) : ",
value = ""),
submitButton("Submit")),
mainPanel(
htmlOutput("VolcanoHeader"),width = 12,
plotlyOutput("effectPlot"),
htmlOutput("profiletext"),
plotlyOutput("ProfilePie")#, width="800px", height="400px")
)
)
),
tabItem(tabName = "patient",
h2("Predicting survival for an incoming patient"),
sidebarLayout(
sidebarPanel(width = 12,
textInput("GeneList",
"Names of the genes you wish to use to create predictive risk : example STK11,KEAP1,KRAS",
value = ""),
##### FOR LUNG ONLY #####
checkboxGroupInput(inputId = "Demographics", label = "Choose demographic variables :",
choices = c("Male" = "Sex",
"Smoker" = "Smoker",
"Age > 65" = "Age"),
inline = TRUE),
submitButton("Submit")
),
mainPanel(width = 12,
htmlOutput("predtext"),
plotlyOutput("IndSurvKM"),
tableOutput("IndPredTable")
)
)
),
tabItem(tabName = "risk",
h2("Generate risk score in a new cohort"),
# sidebarPanel( width = 12,
# checkboxInput("ShowVolvano", "Show Volcano Plot", TRUE),
# checkboxInput("ShowPies", "Show Pie Charts", TRUE),
# submitButton("Submit")
# ),
sidebarLayout(
sidebarPanel(width = 12,
fileInput(inputId = "File1", label = "Choose your dataset (.csv file)",
accept = c(".csv")
)#,
# textInput("OutName",
# "Name or the output risk file :",
# value = "")#,
#submitButton("Submit")
),
mainPanel(
width = 12,
plotOutput("RiskHistogram.new"),
downloadLink("downloadData", "Download")
)
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# load functions of interest
source("./Scripts/GetResultsVarSelect.R")
source("./Scripts/MakeKM.R")
source("./Scripts/GetKMStuff.R")
# source("./Scripts/PlotTree.R")
source("./Scripts/PredictIncoming.R")
# load("FirstRun.Rdata")
# load("RiskGroupsResults.Rdata")
output$VariableHeader <- renderText({ paste("<h3> <u> <font color=\"black\"><b>","Prognostic performance", "</b></font> </u> </h3>") })
output$RiskHeader <- renderText({paste("<h4> <u> <font color=\"black\"><b>","Histogram of predicted risk score", "</b></font> </u> </h4>")})
output$CIHeader <- renderText({paste("<h4> <u> <font color=\"black\"><b>","Concordance index in predicting overall survival", "</b></font> </u> </h4>")})
output$RefitHeader <- renderText({paste("<h4> <u> <font color=\"black\"><b>","Cox regression estimates and significance P-values", "</b></font> </u> </h4>")})
output$EffectHeader <- renderText({ paste("<h3> <u> <font color=\"black\"><b>","Individual gene effect size and relative importance", "</b></font> </u> </h3>") })
output$FreqHeader <- renderText({paste("<h4> <u> <font color=\"black\"><b>","Gene selection frequency", "</b></font> </u> </h4>")})
output$VolcanoHeader <- renderText({paste("<h4> <u> <font color=\"black\"><b>","Interactive Volcano plot", "</b></font> </u> </h4>")})
output$CommentPval <- renderText({paste("<font color=\"black\">","*Note that the pvalue is 0 here because it goes beyond the precision of the machine.", "</font>")})
output$RiskHistogram <- renderPlot({FirstRun$RiskHistogram})
output$RiskSummary <- renderTable({FirstRun$RiskScoreSummary}, rownames = TRUE)
output$CI <- renderTable({FirstRun$ciSummary}, rownames = TRUE)
output$RefitRisk <- renderTable({FirstRun$RiskRefit}, rownames = TRUE)
output$ClinRefit <- renderTable({FirstRun$ClinRefitTable}, rownames = TRUE)
output$influencePlot <- renderPlot({FirstRun$inflPlot})
output$predRiskText <- renderText({ paste("<h3> <u> <font color=\"black\"><b>","Risk group stratification", "</b></font> </u> </h3>") })
GetResultsReactive <- reactive({getResults(studyType = "Lung",
method="LASSO",
geneList = unlist(strsplit(input$GeneListRisk, split ="," )))})
output$effectPlot <- renderPlotly({GetResultsReactive()$selectInflPlot})
#output$effectPlot <- renderPlotly({FirstRun$selectInflPlot})
KMStuffReactive <- reactive({
KMStuff(FirstRun$data.out,FirstRun$average.risk,
FirstRun$topHits,4,
c(0.25,0.75,0.9),
geneList=unlist(strsplit(input$GeneListRisk, split ="," )))
})
output$KMText <- renderText({ paste("<h4> <u> <font color=\"black\"><b>","Kaplan-Meier plot of overall survival", "</b></font> </u> </h4>") })
output$KM <- renderPlot(FirstRun$KM_Plot)
output$SurvSum <- renderTable(FirstRun$SurvSum,rownames = TRUE)
output$MutGroupText <- renderText({ paste("<h3> <u> <font color=\"black\"><b>","Mutation profiles by risk groups", "</b></font> </u> </h3>") })
output$MutBPText <- renderText({ paste("<h4> <u> <font color=\"black\"><b>","Barplot of mutation frequency", "</b></font> </u> </h4>") })
output$Mut <- renderPlot({
print(FirstRun$mut_Plot)})
## Tab 2
output$profiletext <- renderText({ paste("<h4> <u> <font color=\"black\"><b>","Piechart of most representative mutation profiles : ",
KMStuffReactive()$GenesUsed, "</b></font> </u> </h4>") })
output$ProfilePie <- renderPlotly({
KMStuffReactive()$PieChart
})
makePredictionsReactive <- reactive({
predictIncomingPatient(mutGenes = unlist(strsplit(input$GeneList, split =",")),
clinical=c(input$Demographics),
ClinRefit=FirstRun$ClinRefit,
time.type=FirstRun$time.type,
MD=FirstRun$MD,
LassoFits=FirstRun$LassoFits,
RiskScore=FirstRun$average.risk,
means.train=means.train)
})
output$IndSurvKM <- renderPlotly({makePredictionsReactive()$IndSurvKM})
output$IndPredTable <- renderTable({makePredictionsReactive()$IndPredTable},rownames = TRUE)
#output$IndSurvKM <- renderPlotly({FirstRun$IndSurvKM})
# output$IndPredTable <- renderTable({FirstRun$IndPredTable},rownames = TRUE)
### GENERATING THE RISK SCORE FOR NEW DATA ###
### GENERATING THE RISK SCORE FOR NEW DATA ###
dset <- reactive({
inFile <- input$File1
if(is.null(inFile)) return(NULL)
LassoFits <- as.matrix(Results$LassoFits)
ori.risk <- Results$ori.risk
file.rename(inFile$datapath, paste0(inFile$datapath, ".csv"))
in.data <- read.csv(paste0(inFile$datapath, ".csv"), header = T,row.names = 1)
features <- colnames(LassoFits)
features[match(c("alk","ros1","ret"),features)] <- paste0(features[match(c("alk","ros1","ret"),features)],".fusion")
colnames(LassoFits)[match(c("alk","ros1","ret"),colnames(LassoFits))] <- paste0(colnames(LassoFits)[match(c("alk","ros1","ret"),colnames(LassoFits))],".fusion")
# in.data <- as.data.frame(matrix(rbinom(1100,1,prob=0.5),ncol =11))
# colnames(in.data) <- c("KEAP1","STK11","TP53","EGFR","KRAS","SMARCA4","alk","ros1","BRCA1","AXIN1","noNameTest")
if(!all(is.na(match(colnames(in.data),features)))){
matched.genes <- c(na.omit(match(colnames(in.data),features)))
new.dat <- in.data[,which(!is.na(match(colnames(in.data),features)))]
## ADD ALL MISSING GENES TO BE ALL zero ##
missing <- features[which(is.na(match(features,colnames(new.dat))))]
to.add <- as.data.frame(matrix(0L,nrow=nrow(new.dat),ncol=length(missing)))
colnames(to.add) <- missing
rownames(to.add) <- rownames(new.dat)
new.dat <- as.data.frame(cbind(new.dat,to.add))
new.dat <- new.dat[,match(features,colnames(new.dat))]
#############################################
all.pred <- lapply(1:nrow(LassoFits),function(x){
### Subset to the coefs of that cv ###
coefs <- LassoFits[x,LassoFits[x,] != 0]
new.temp <- select(new.dat,names(coefs))
if(!all(is.na(match(c("alk","ros1","ret"),names(means.train[[x]]))))){
names(means.train[[x]])[na.omit(match(c("alk","ros1","ret"),names(means.train[[x]])))] <-
paste0(names(means.train[[x]])[na.omit(match(c("alk","ros1","ret"),names(means.train[[x]])))],".fusion")
}
## substract mean mutation rate of TRAINING SET !!!###
new.x <- new.temp - rep(means.train[[x]][match(names(coefs),names(means.train[[x]]))], each = nrow(new.temp))
cal.risk.test <- drop(as.matrix(new.x) %*% coefs)
return(cal.risk.test)
})
all.pred <- do.call("cbind",all.pred)
Risk <- apply(all.pred,1,mean)
names(Risk) <- rownames(new.dat)
# Risk.all <- as.matrix(coefs) %*% as.matrix(t(new.dat))
# Risk <- apply(Risk.all,2,mean)
#in.data$Risk <- Risk
##########################################
ori.risk.range <- range(ori.risk)
in.data$OncoCastRiskScore <- rescale(Risk, to = c(0, 10), from = ori.risk.range) #WithOriginal
#in.data$rescaledRisk <- rescale(in.data$Risk, to = c(0, 10), from = range(in.data$Risk, na.rm = TRUE, finite = TRUE))
RiskHistogram.new <- ggplot(in.data, aes(x = OncoCastRiskScore, y = ..density..)) +
geom_histogram(show.legend = FALSE, aes(fill=..x..),
breaks=seq(min(in.data$OncoCastRiskScore,na.rm = T), max(in.data$OncoCastRiskScore,na.rm = T), by=20/nrow(in.data))) +
geom_density(show.legend = FALSE) +
theme_minimal() +
labs(x = "Average risk score", y = "Density") +
scale_fill_gradient(high = "red", low = "green")
return(list("RiskHistogram.new"=RiskHistogram.new,"out.data"=in.data))
}
else{
stop("No gene in your dataset overlapped with the IMPACT platform. Please rename genes or check your dataset.")
}
})
output$RiskHistogram.new <- renderPlot(dset()$RiskHistogram.new)
#data <- renderTable(dset()$out.data)
output$downloadData <- downloadHandler(
filename = function() {
paste("NewRiskData.csv", sep="")
},
content = function(file) {
write.csv(dset()$out.data, file)
}
)
}
shinyApp(ui, server)