-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_tools.R
More file actions
360 lines (306 loc) · 17.1 KB
/
mod_tools.R
File metadata and controls
360 lines (306 loc) · 17.1 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
mod_tools_UI <- function(id) {
ns <- NS(id)
tagList(
useShinyjs(),
tags$div(class = "alert alert-success", role = "alert",
strong("Message:"), "Welcome to the Toolbox, our online analysis tool for candidate gene research. You can upload and analyze your own data online."),
br(),
card(
layout_column_wrap(
1/2,
# input file----
card(
fileInput(
inputId = ns("up_file_vcf"),width = "100%",placeholder = "Please upload a VCF file for a gene in wheat",
label = "Upload your Gene Type file(vcf)",multiple = F,accept = c(".vcf",".vcf.gz")
),
downloadLink(ns("down_test_vcf"),label = "Click here to download the example VCF file"),
fileInput(
inputId = ns("up_file_phe"),width = "100%",placeholder = "Please upload the matching phenotype file",
label = "Upload your Trait file(xlsx)",multiple = F,accept = ".xlsx"
),
downloadLink(ns("down_test_phe"),label = "Click here to download the example Trait file")
),
card(
card_header("Tip"),
card_body("Wheat is one of the most important grain crops. Currently, the global wheat production is threatened by the potential of wheat stripe rust. The best measure to control wheat stripe rust is to cultivate disease-resistant varieties with excellent resistance to wheat stripe rust."),
p("Sample names and phenotype names must not contain special characters, use letters as the first character, and use NA to fill in missing values."),
p("You need to upload the file in standard format (please download to see example data) first and then analyze it.")
)
),
# run init----
actionButton(ns("run_init"),"Please upload the file first and then click here to initialize the analysis",
icon("magnifying-glass"), class = "btn-success m-2",width = "100%"),
card(
tabsetPanel(
# summary ----
tabPanel("Summary",icon = icon("circle-info"),br(),
uiOutput(ns("sumary_stat")),br(),
card(
card_header("VCF file table"),
DTOutput(ns("vcf_table")),height = 650
)
),
# SGAT UI----
tabPanel("SGAT",icon = icon("arrow-right"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "This tool is based on the R language rMVP package implementation,Provide correlation analysis of different models."),
br(),
layout_column_wrap(
1/4,
selectInput(ns("SGAT_Model"),"Choice model",
selected = "MLM",
choices = c("GLM","MLM","FarmCPU"),multiple = F),
selectInput(ns("SGAT_trait"),"Choice trait",
choices = NULL,multiple = F),
numericInput(ns("SGAT_mythrehold"),label = "threshold",
value = 0.05,min = 0,max = 1,step = 100),
textInput(ns("mygeneid"),label = "Gene ID",placeholder = "TraesCS1A03G0949800")
),
actionButton(ns("run_SGAT"),label = "Run SGAT",icon = icon("arrow-right")),
p("Note :After each parameter change, please click the button to analyze it."),
br(),
uiOutput(ns("SGAT_plot")),
card(
card_header("Manhattan plot of candidate gene association analysis results"),
plotlyOutput(ns("SGAT_mdh_plotly"),width = "100%",height = 300),
full_screen = T
),
card(
DTOutput(ns("SGAT_table")),height = 650,
full_screen = T
),
downloadBttn(ns("downlaod_SGAT"),label = "Download SGAT Result",icon = icon("download"),color="success")
),
# singlemark UI----
tabPanel("SingleMarker",icon = icon("star"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "The use of single marker analysis tools enables multiple testing of variant site phenotypes."),
br(),
# 调用python实现
actionButton(ns("run_singlemark"),label = "Run Single Mark Tool",icon = icon("arrow-right")),
br(),
card(
DTOutput(ns("DT_singlemark"),height = 650)
)
),
tabPanel("LDBlock", icon = icon("magnet"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "The use of single marker analysis tools enables multiple testing of variant site phenotypes."),
br(),
# 调用linux实现
actionButton(ns("run_LD"),label = "Run LD Block Tool",icon = icon("arrow-right")),
br(),
),
tabPanel("PheBoxPlot",icon = icon("palette"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "."),
br(),
),
tabPanel("GeneHAP",icon = icon("compass"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "The use of single marker analysis tools enables multiple testing of variant site phenotypes."),
br(),
),
tabPanel("RNAseqTPM", icon = icon("magnet"),br(),
tags$div(class = "alert alert-success", role = "alert",
strong("Abstract :"), "The use of single marker analysis tools enables multiple testing of variant site phenotypes."),
br(),
)
),
textOutput(ns("userid"))
)
)
)
}
mod_tools_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
userUID <- str_sub(uuid::UUIDgenerate(),26,36)
output$userid <- renderText({paste0("Your visite ID :",userUID," , Server Time:",Sys.time())})
# example vcf ----
output$down_test_vcf <- downloadHandler(
filename = function(){
"Example_GeneType.filter.vcf"
},
content = function(file){
file.copy(from = "www/download/Example_GeneType.filter.vcf",to = file)
}
)
# example trait----
output$down_test_phe <- downloadHandler(
filename = function(){
"Example_trait_file.xlsx"
},
content = function(file){
file.copy(from = "www/download/Example_trait_file.xlsx",to = file)
}
)
# run init----
observeEvent(input$run_init,{
# dev test
req(input$up_file_vcf)
req(input$up_file_phe)
dir.create(paste0("www/OUT/",userUID))
# # if not vcf , use gunzip convert to vcf
# if(substr(input$up_file_vcf$datapath, nchar(input$up_file_vcf$datapath), nchar(input$up_file_vcf$datapath)) == "z"){
# file.copy(input$up_file_vcf$datapath,paste0("www/OUT/",userUID,"/user_upload.vcf.gz"))
# gunzip(paste0("www/OUT/",userUID,"/user_upload.vcf.gz"),remove = FALSE)
# }else{
# file.copy(input$up_file_vcf$datapath,paste0("www/OUT/",userUID,"/user_upload.vcf"))
# }
#
# user_upload_vcf_file_name <- paste0("www/OUT/",userUID,"/user_upload.vcf")
#
shinyalert("Parsing data", "Please do not close the page...It will take about 1 minute. If there is no response after the timeout, please refresh the page",
type = "info", closeOnEsc = FALSE, closeOnClickOutside = FALSE, showConfirmButton = FALSE)
# get data ----
# data_vcf <- get_vcf_df() # dev test
# data_trait <- get_trait_df() # dev test
data_vcf <- get_vcf_df(input$up_file_vcf$datapath)
data_trait <- get_trait_df(input$up_file_phe$datapath)
write_tsv(data_trait$df_phe,file = paste0("www/OUT/",userUID,"/user_upload_trait.tsv"),col_names = T,na = "NA")
# summary stat ----
output$sumary_stat <- renderUI(
list(
tags$div(class = "alert alert-success", role = "alert",
strong("Tip:"), "Hello, your uploaded file has been automatically parsed by the cloud platform, you can preview part of the VCF file and phenotype file below. Please make sure the data is correct and click the tab above to analyze."),
layout_column_wrap(
width=1/3,
value_box(
title = "Sample Number",
value = length(data_vcf$sample_list),
showcase = bs_icon("list-check")
),
value_box(
title = "Marker Number",
value = length(data_vcf$SNP_list),
showcase = bs_icon("list-check")
),
value_box(
title = "Trait Number",
value = length(data_trait$phe_list),
showcase = bs_icon("list-check")
)
),
tags$div(class = "alert alert-info", role = "alert",
"Only the first 8 columns of the VCF file (genotype marker locus information) and the first 20 rows of the phenotype are shown below, for checking and confirming the data format only")
)
)
# vcf table ----
output$vcf_table <- renderDT(data_vcf$df_table)
# phe table ----
output$phe_table <- renderDT(data_trait$df_phe_view)
updateSelectInput(session, "SGAT_trait", choices = data_trait$phe_list)
# init SGAT data ----
SGAT_init(file_vcf = input$up_file_vcf$datapath,
file_phe = paste0("www/OUT/",userUID,"/user_upload_trait.tsv"),
userUID = userUID)
log_file <- list.files(paste0("www/OUT/",userUID,"/SGAT_OUT/"),pattern = "MVP.Data.*.log")
shinyjs::runjs("swal.close();")
showModal(
modalDialog(
title = "Data reading log",
size = "l",
easyClose = T,
tagList(
tags$pre(style = "font-family: monospace; width: 100%;",
getFileContent(paste0("www/OUT/",userUID,"/SGAT_OUT/",log_file)))
)
)
)
})
## SGAT ----
observeEvent(input$run_SGAT,{
req(input$SGAT_trait)
shinyalert("SGAT Running", "Please do not close the page...", type = "info", closeOnEsc = FALSE, closeOnClickOutside = FALSE, showConfirmButton = FALSE)
SGAT_OUT <- SGAT_rMVP(which_trait = input$SGAT_trait,which_model = input$SGAT_Model,
userUID = userUID,mythrehold=input$SGAT_mythrehold,mygeneid = input$mygeneid)
output$SGAT_plot <- renderUI(
card(
layout_column_wrap(
1/3,
tags$img(src= paste0("OUT/",userUID,"/SGAT_OUT/",input$SGAT_trait,".",input$SGAT_Model,".QQplot.jpg") , style = "width: 100%;"),
tags$img(src= paste0("OUT/",userUID,"/SGAT_OUT/",input$SGAT_trait,".Phe_Dist.jpg") , style = "width: 100%;"),
tags$img(src= paste0("OUT/",userUID,"/SGAT_OUT/",input$SGAT_trait,".PCA_2D.jpg") , style = "width: 100%;")
)
)
)
# plotly mamhatun
output$SGAT_mdh_plotly <- renderPlotly({
p_mdh <- ggplot(SGAT_OUT$p_mdh_plotly_data,aes(POS,P,text = SNP))+
geom_point(aes(color= Effect),size=4,alpha=0.8)+
scale_color_gradient(low = "#c3fae8",high = "#087f5b")+
ylab("MLM")+
xlab(str_c("Physical Postion (IWGSC 2.1)"))+
theme_bw()
plotly::ggplotly(p_mdh)
})
output$SGAT_table <- renderDT({
vroom(paste0("www/OUT/",userUID,"/SGAT_OUT/",input$SGAT_trait,".",input$SGAT_Model,".csv"))
})
shinyjs::runjs("swal.close();")
# log_file <- list.files(paste0("www/OUT/",userUID,"/SGAT_OUT/"),pattern = "MVP.*.log") %>% sort()
# log_file_new <- log_file[length(log_file)-1]
#
# showModal(
# modalDialog(
# title = "Data reading log",
# size = "l",
# easyClose = T,
# tagList(
# tags$pre(style = "font-family: monospace; white-space: pre-wrap; width: 100%;",
# getFileContent(paste0("www/OUT/",userUID,"/SGAT_OUT/",log_file)))
# )
# )
# )
down_file_SGAT <- SGAT_OUT$out_zip_file
print(down_file_SGAT)
output$downlaod_SGAT <- downloadHandler(
filename = function(){
down_file_SGAT
},
content = function(file){
file.copy(down_file_SGAT,file)
}
)
})
#singlemark ----
observeEvent(input$run_singlemark,{
req(input$up_file_vcf)
req(input$up_file_phe)
shinyalert("SGAT Running", "Please do not close the page...", type = "info", closeOnEsc = FALSE, closeOnClickOutside = FALSE, showConfirmButton = FALSE)
# 进度条
withProgress(message = 'Running task', value = 0, {
n <- 10 # 任务的步骤数
for (i in 1:n) {
# 模拟长时间运行的任务
Sys.sleep(0.5)
# 更新进度条
incProgress(1/n, detail = paste("Step", i, "of", n))
}
})
output$DT_singlemark <- renderDT(
phe
)
shinyjs::runjs("swal.close();")
})
# 当用户退出60秒后删除临时目录
onSessionEnded(function() {
later(function() {
unlink("rm.txt", recursive = TRUE)
},delay = 600) # 600秒后删除文件
})
# # 指定要删除文件的目录
# temp_directory <- paste0("www/OUT/",userUID)
#
# # 在用户会话结束时删除目录下的所有文件
# session$onSessionEnded(function() {
# files <- list.files(temp_directory, full.names = TRUE)
# file.remove(files)
# # 自定义删除
# })
})
}