main.R 39.6 KB
Newer Older
jlopez's avatar
jlopez committed
1
2
3
source("tools.R")
library(yaml)

4
path_yml <- "/home/mbb/Documents/waw/workflows/RADseq_ref/sag.yaml"
jlopez's avatar
jlopez committed
5

mmassaviol's avatar
mmassaviol committed
6
#path_tools <- "/home/mbb/Documents/waw/tools"
jlopez's avatar
jlopez committed
7

jlopez's avatar
jlopez committed
8
9
10
11
12
APP <<- yaml.load_file(path_yml)$App

generate_added_styles <- function () {
  path_css = APP$project + "/www/added_styles.css"

jlopez's avatar
jlopez committed
13
  file.replace(path_css)
jlopez's avatar
jlopez committed
14
15
16
17
}

#============================================================

jlopez's avatar
jlopez committed
18
19
20
21
22
generate_page_input <- function(res, cnt) {
  
  if(cnt$type == "button") {
    
    if(cnt$icon == "") {
jlopez's avatar
jlopez committed
23
      res <- res + '\t\tactionButton("' + cnt$name + '", "'+ cnt$label +'", class="'+ cnt$class +'")' 
jlopez's avatar
jlopez committed
24
25
      
    } else {
jlopez's avatar
jlopez committed
26
      res <- res + '\t\tactionButton("' + cnt$name + '", "'+ cnt$label +'",  icon("'+ cnt$icon +'"), class="'+ cnt$class +'")' 
jlopez's avatar
jlopez committed
27
28
    }
    
29
30
  }
  if(cnt$type == "text") {
jlopez's avatar
jlopez committed
31
    
32
    res <- res + '\t\ttextInput("'+ cnt$name +'", label = "'+ cnt$label +'", value = "'+ cnt$value +'", width = "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
33
    
34
35
  }
  if(cnt$type == "numeric") {
36
    
mmassaviol's avatar
mmassaviol committed
37
    res <- res + '\t\tnumericInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = '+ cnt$value +')'
38

39
40
  }
  if(cnt$type == "slider") {
jlopez's avatar
jlopez committed
41
    if(length(cnt$value) == 1) {
jlopez's avatar
jlopez committed
42
      res <- res + '\t\tsliderInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = '+ cnt$value +')'
jlopez's avatar
jlopez committed
43
    } else {
jlopez's avatar
jlopez committed
44
      res <- res + '\t\tsliderInput("'+ cnt$name +'", label = "'+ cnt$label +'", min = '+ cnt$min +', max = '+ cnt$max +', step = '+ cnt$step +', width =  "'+ 'auto' +'", value = c('+ cnt$value[1] + ',' + cnt$value[2] + '))'
jlopez's avatar
jlopez committed
45
    }
46
47
  }
  if(cnt$type == "help") {
jlopez's avatar
jlopez committed
48
    res <- res + '\t\tp("'+cnt$label+'")'
49
50
  }
  if(cnt$type == "link") {
51
    res <- res + '\t\tp("'+cnt$label+'",a(href="'+cnt$href+'","'+cnt$href+'"))'
52
53
  }
  if(cnt$type == "select") {
jlopez's avatar
jlopez committed
54
55
    
    
jlopez's avatar
jlopez committed
56
    res <- res + '\t\tselectInput("'+ cnt$name +'", label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
57
    
mmassaviol's avatar
mmassaviol committed
58
    cpt = 1
jlopez's avatar
jlopez committed
59
    if(length(cnt$choices) > 0) {
mmassaviol's avatar
mmassaviol committed
60
      for(ch in cnt$choices){
jlopez's avatar
jlopez committed
61
        
mmassaviol's avatar
mmassaviol committed
62
63
        if(cpt < length(cnt$choices)) {
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '", '
jlopez's avatar
jlopez committed
64
        } else {
mmassaviol's avatar
mmassaviol committed
65
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '"), '
jlopez's avatar
jlopez committed
66
        }
mmassaviol's avatar
mmassaviol committed
67
        cpt = cpt + 1
jlopez's avatar
jlopez committed
68
69
70
      }
    }
    
71
    res <- res + ' selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
72
    
73
74
  }
  if(cnt$type == "radio") {
jlopez's avatar
jlopez committed
75
    
jlopez's avatar
jlopez committed
76
    res <- res + '\t\tradioButtons("'+ cnt$name +'", label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
77
    
78
    cpt = 1
jlopez's avatar
jlopez committed
79
    if(length(cnt$choices) > 0) {
80
      for(ch in cnt$choices){
jlopez's avatar
jlopez committed
81
        
82
83
        if(cpt < length(cnt$choices)) {
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '", '
jlopez's avatar
jlopez committed
84
        } else {
85
          res <- res + '"' + names(ch) + '" = "' + ch[names(ch)] + '"), '
jlopez's avatar
jlopez committed
86
        }
87
        cpt = cpt + 1
jlopez's avatar
jlopez committed
88
89
90
      }
    }
    
91
    res <- res + ' selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
92
    
93
94
  }
  if(cnt$type == "checkbox") {
jlopez's avatar
jlopez committed
95
    
jlopez's avatar
jlopez committed
96
    res <- res + '\t\tcheckboxInput("'+ cnt$name +'", label = "'+ cnt$label +'", value = ' + cnt$value + ')'  
jlopez's avatar
jlopez committed
97
    
98
99
  }
  if(cnt$type == "chooser") {
jlopez's avatar
jlopez committed
100
101
102
103
104
105
106
107
108
109
110
111
    
    vl <- 'c('
    vr <- 'c('
    
    if(length(cnt$value_left) > 0) {
      for(x in 1:length(cnt$value_left)) {
        vl <- vl + '"' + cnt$value_left[[x]] + '"'
        if(x < length(cnt$value_left)) {
          vl <- vl + ', '
        }
      }
    }
jlopez's avatar
jlopez committed
112
    
jlopez's avatar
jlopez committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    vl <- vl + ')'
    
    if(length(cnt$value_right) > 0) {
      vr <- "c("
      for(x in 1:length(cnt$value_right)) {
        vr <- vr + '"' + cnt$value_right[[x]] + '"'
        if(x < length(cnt$value_right)) {
          vr <- vr + ', '
        }
      }
    }
    
    vr <- vr + ')'
  
    res <- res + '\t\tchooserInput("' + cnt$name + '", "Available", "Selected", ' + vl + ', ' + vr + ', size = ' + cnt$size + ', multiple = ' + cnt$multiple + ')'

129
130
  }
  if(cnt$type == "checkboxGroupInput") {
jlopez's avatar
jlopez committed
131
    
jlopez's avatar
jlopez committed
132
    res <- res + '\t\tcheckboxGroupInput("'+ cnt$name +'",label = "'+ cnt$label +'", choices = list('
jlopez's avatar
jlopez committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    
    if(length(cnt$choices) > 0) {
      for(ch in 1:length(cnt$choices)){
        
        choices = cnt$choices[[ch]]
        
        value = choices$value
        
        if(is.character(choices$value)) {
          value = '"' + choices$value + '"'
        }
        
        if(ch < length(cnt$choices)) {
          res <- res + '"' + choices$label + '"' + ' = ' + value + ', '
        } else {
          res <- res + '"' + choices$label + '"' + ' = ' + value
        }
      }
    }
    
153
    res <- res + '), selected = "'+cnt$value+'"' + ', width =  "'+ 'auto' +'")'
jlopez's avatar
jlopez committed
154
    
155
156
  }
  if(cnt$type == "file") {
jlopez's avatar
jlopez committed
157
    
jlopez's avatar
jlopez committed
158
    res <- res + '\t\tfileInput("'+ cnt$name +'", label = "'+ cnt$label + '"'
jlopez's avatar
jlopez committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    
    if(length(cnt$accept) > 0) {
      
      res <- res + ", accept = c("
      
      for(a in 1:length(cnt$accept)){
        form <- cnt$accept[[a]]
        
        res <- res + '"' + form + '"'
        
        
        if(a < length(cnt$accept)) {
          res <- res + ', '
        } else {
          res <- res + ')'
        }
        
      }
      
    }
    
jlopez's avatar
jlopez committed
180
    res <- res + ', multiple = ' + cnt$multiple + ', width =  "'+ 'auto' + '")'
jlopez's avatar
jlopez committed
181
182
183
184
185
186
    
  }
  
  return(res)
}

jlopez's avatar
jlopez committed
187
188
generate_page_box <- function(panel, box, res) {
  
mmassaviol's avatar
mmassaviol committed
189
  res <- res + 'box(title = "' + box$title + '", width = 12, status = "' + box$status + '", collapsible = TRUE, solidHeader = TRUE'
jlopez's avatar
jlopez committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  
  panel_content = box$content
  
  if(length(panel_content) > 0) {
    
    res <- res + ',\n'
    
    for(z in 1:length(panel_content)) {
      res <- generate_page_input(res, panel_content[[z]])
      
      if(z < length(panel_content)) {
        res <- res + ','
      }
  
      res <- res + '\n\n'
    }
  }
  
mmassaviol's avatar
mmassaviol committed
208
  res <- res + '\t)'
jlopez's avatar
jlopez committed
209
210
211
212
213
214
215
  
  return(res)
  
}

#============================================================

jlopez's avatar
update    
jlopez committed
216
generate_pages_server <- function() {
jlopez's avatar
jlopez committed
217
  resG <- ""
jlopez's avatar
update    
jlopez committed
218
219
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
220
    resG <- resG + "\t# \tPage : " + name + "\n"
jlopez's avatar
update    
jlopez committed
221
    path_page = APP$project+"/pages/pages_def_"+tolower(name)+".R"
jlopez's avatar
jlopez committed
222
    file.replace(path_page)
jlopez's avatar
update    
jlopez committed
223
    
jlopez's avatar
jlopez committed
224
    res <- 'tab'+ name + ' = fluidPage(\n\n'
jlopez's avatar
jlopez committed
225
    
mmassaviol's avatar
mmassaviol committed
226
    res <- res + 'box(title = "Parameters :", width = 12, status = "primary", collapsible = TRUE, solidHeader = TRUE'
jlopez's avatar
jlopez committed
227
     
mmassaviol's avatar
mmassaviol committed
228
    content <- APP$pages[[x]]
jlopez's avatar
update    
jlopez committed
229
230
    
    if(length(content) > 0) {
jlopez's avatar
jlopez committed
231
      res <- res + ',\n\n'
mmassaviol's avatar
mmassaviol committed
232
233

      boxes = content$boxes
jlopez's avatar
jlopez committed
234
      
mmassaviol's avatar
mmassaviol committed
235
      if(length(boxes) > 0) {
jlopez's avatar
jlopez committed
236
        
mmassaviol's avatar
mmassaviol committed
237
        if(length(boxes) > 1){
238
          res <- res + '\tselectInput("select' + content$name + '", label = "Select the tool to use : ", selected = "' + content$default + '", choices = list(' 
jlopez's avatar
jlopez committed
239
          
mmassaviol's avatar
mmassaviol committed
240
241
          for(b in 1:length(boxes)) { # construction list box tools
            res <- res + '"' + boxes[[b]]$name +'" = "' + boxes[[b]]$name + '"'
jlopez's avatar
update    
jlopez committed
242
            
mmassaviol's avatar
mmassaviol committed
243
            if(b < length(boxes)) {
jlopez's avatar
jlopez committed
244
245
246
247
248
              res <- res + ', '
            }
            
          }
          res <- res + ')),\n'
mmassaviol's avatar
mmassaviol committed
249
250
251
252
253
254
        }
        else{
          res <- res + '\thidden(textInput("select' + content$name + '", label = "", value="'+ boxes[[1]]$name +'")),'
        }
        
        for(b in 1:length(boxes)) { # draw boxes
jlopez's avatar
update    
jlopez committed
255
          
mmassaviol's avatar
mmassaviol committed
256
257
258
259
260
261
          box <- boxes[[b]]
          
          if(length(boxes) > 1){
            res <- res + "\nconditionalPanel(condition = \"input.select"+content$name+" == '"+box$name+"'\","
          }

262
263
264
          #if(box$defined == TRUE) {
          #  box <- get_predefined_box(box, APP$pages[[x]]$name)
          #} 
mmassaviol's avatar
mmassaviol committed
265
266
267
268
269
270
271
272
          
          res <- generate_page_box(panel, box, res)

          if(length(boxes) > 1){
            res <- res + ")"
          }
          if(b < length(boxes)){
            res <- res + ","
jlopez's avatar
update    
jlopez committed
273
          }
jlopez's avatar
jlopez committed
274
        }
jlopez's avatar
update    
jlopez committed
275
276
277
      }
    }
    
mmassaviol's avatar
mmassaviol committed
278
    res <- res + '))\n\n'
jlopez's avatar
jlopez committed
279

jlopez's avatar
update    
jlopez committed
280
281
282
    write(res, file=path_page)
    
    path_opt = APP$project+"/server/opt_"+tolower(name)+".R"
283
    #file.replace(path_opt)
jlopez's avatar
update    
jlopez committed
284
285
286
    
    res <- ""
    
mmassaviol's avatar
mmassaviol committed
287
    content <- APP$pages[[x]]
jlopez's avatar
update    
jlopez committed
288
    
289
    # if(length(content) > 0) {
mmassaviol's avatar
mmassaviol committed
290
      
291
    #   boxes = content$boxes
mmassaviol's avatar
mmassaviol committed
292
      
293
    #   if(length(boxes) > 0) {
jlopez's avatar
jlopez committed
294
        
jlopez's avatar
jlopez committed
295
        
296
    #     for(b in 1:length(boxes)) {
jlopez's avatar
jlopez committed
297
          
298
    #       box <- boxes[[b]]
jlopez's avatar
jlopez committed
299
          
300
301
302
    #       #if(box$defined == TRUE) {
    #       #  box <- get_predefined_box(box, APP$pages[[x]]$name)
    #       #} 
jlopez's avatar
jlopez committed
303
          
jlopez's avatar
jlopez committed
304
          
305
    #       panel_content = box$content
mmassaviol's avatar
mmassaviol committed
306
307
  
      
308
    #       if(length(panel_content) > 0) {
mmassaviol's avatar
mmassaviol committed
309
      
310
    #     for(z in 1:length(panel_content)) {
mmassaviol's avatar
mmassaviol committed
311
          
312
    #       cnt <- panel_content[[z]]
mmassaviol's avatar
mmassaviol committed
313
          
314
315
    #       type <- cnt$type
    #       id <- cnt$name
mmassaviol's avatar
mmassaviol committed
316
          
317
318
319
    #       if(type == "button") {
    #           res <- res + '#\' Event when use '+ id +' button\n'
    #           res <- res + 'observeEvent(input$'+id+', {\n\n'
mmassaviol's avatar
mmassaviol committed
320
              
321
322
323
    #           res <- res + '})\n\n'
    #       }
    #     }
mmassaviol's avatar
mmassaviol committed
324
        
325
    #   }
jlopez's avatar
jlopez committed
326
      
327
    #     }
mmassaviol's avatar
mmassaviol committed
328
        
329
330
    #   }
    # }
jlopez's avatar
jlopez committed
331
    
mmassaviol's avatar
mmassaviol committed
332
333
334
    run <- APP$run

    if(length(run$options) > 0) {
jlopez's avatar
jlopez committed
335
      
336
337
      # res <- res + '#\' Event when use '+ run$shiny_button$name +' button\n'
      # res <- res + 'observeEvent(input$'+ run$shiny_button$name+', {\n\n'
jlopez's avatar
jlopez committed
338
      
339
      # page_path <- APP$project + '/params/' + 'params_' + APP$pages[[x]]$name + '.yml'
jlopez's avatar
jlopez committed
340
      
341
      # file.replace(page_path)
jlopez's avatar
jlopez committed
342
343
      
      
344
345
346
      # res <- res + '# Params \n'
      # res <- res + '\tpath_param <- "' + page_path + '"\n\n'
      # res <- res + '\tres <- ""\n'
jlopez's avatar
jlopez committed
347
      
348
      # res <- res + '\n\t# Page : ' + APP$pages[[x]]$name+ '\n'
mmassaviol's avatar
mmassaviol committed
349
350
351
352
      
      boxes = APP$pages[[x]]$boxes
      
      if(length(boxes) > 0) {
353

354
        resG <- resG + '\t\tres = paste0(res , paste("'+APP$pages[[x]]$name + ':", paste0(\'\"\', input$select' + APP$pages[[x]]$name + ', \'\"\'), "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
355
        for(b in 1:length(boxes)) {  # list boxes
jlopez's avatar
jlopez committed
356
          
mmassaviol's avatar
mmassaviol committed
357
358
          box <- boxes[[b]]
          
359
          # res <- res + '\n\t# Tool : ' + box$name+ '\n'
mmassaviol's avatar
mmassaviol committed
360
          
361
362
363
364
365
366
367
368
          # if (length(boxes) > 1){
          #   verif = "input$select" + APP$pages[[x]]$name + ' == "' + box$name + '"'
          # }
          # else{
          #   verif = "TRUE"
          # }

          # res <- res + '\n\tif('+verif+') {\n'
mmassaviol's avatar
mmassaviol committed
369
370
371
372
          
          panel_content = box$content
    
          if(length(panel_content) > 0) {
373

mmassaviol's avatar
mmassaviol committed
374
            for(z in 1:length(panel_content)) {
jlopez's avatar
jlopez committed
375
              
mmassaviol's avatar
mmassaviol committed
376
377
378
379
              cnt <- panel_content[[z]]
              
              type <- cnt$type
              id <- cnt$name
jlopez's avatar
jlopez committed
380
              
mmassaviol's avatar
mmassaviol committed
381
382
383
              genR <- ""
            
              if(type == 'text') {
384
385
386
387
388
389
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
                genR <-  genR + '\t\t} else {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
390
              } else if(type == 'numeric') {
391
392
393
394
395
396
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
                genR <-  genR + '\t\t} else {\n'
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
mmassaviol's avatar
mmassaviol committed
397
398
              } else if(cnt$type == "slider") {
                if(length(cnt$value) == 1) {
399
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))'
jlopez's avatar
jlopez committed
400
                  genR <-  genR + '\t\n\n'
jlopez's avatar
jlopez committed
401
                } else {
402
403
404
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_min:", input$' + id + '[1], "\\n", sep = " "))\n'
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_max:", input$' + id + '[2], "\\n", sep = " "))\n'
                  genR <-  genR + '\t\tres = paste0(res, paste("' + id + '_step:", "' + cnt$step + '", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
405
                  genR <-  genR + '\t\n\n'
jlopez's avatar
jlopez committed
406
                }
mmassaviol's avatar
mmassaviol committed
407
408
              } else if(cnt$type == "select") {
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
409
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
410
                genR <-  genR + '\t\t} else {\n'
411
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
412
413
414
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
              } else if(cnt$type == "file") {
415
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + '$datapath, \'\"\'), "\\n", sep = " "))'
mmassaviol's avatar
mmassaviol committed
416
417
418
                genR <-  genR + '\t\n\n'
              } else if(cnt$type == "checkbox") {
                genR <-  genR + '\t\tif(input$' + id + ') {\n'
419
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", "true", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
420
                genR <-  genR + '\t\t} else {\n'
421
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", "false", "\\n", sep = " "))\n'
mmassaviol's avatar
mmassaviol committed
422
423
424
425
426
427
428
429
430
431
432
433
434
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
              } else if(cnt$type == "chooser") {
                genR <-  genR + '\t\tres <- paste0(res, " ' + id + '" + "_left: [")\n'
                genR <-  genR + '\t\tif(length(input$'+ id +'$left) > 0) {\n' 
                genR <-  genR + '\t\tfor(x in 1:length(input$'+ id +'$left)) {\n'
                genR <-  genR + '\t\t\tres <- paste0(res, \'"\', input$'+ id +'$left[[x]], \'"\')\n'
                genR <-  genR + '\t\t\tif(x < length(input$'+ id +'$left)) {\n'
                genR <-  genR + '\t\t\t\tres <- paste0(res, ", ")\n'
                genR <-  genR + '\t\t\t}\n'
                genR <-  genR + '\t\t}\n'
                genR <-  genR + '\t\t}		\n'
                genR <-  genR + '\t\tres <- paste0(res, "]", "\\n")\n\n'
jlopez's avatar
jlopez committed
435
                
mmassaviol's avatar
mmassaviol committed
436
437
438
439
440
441
442
443
444
445
                genR <-  genR + '\t\tres <- paste0(res, " ' + id + '" + "_right: [")\n'
                genR <-  genR + '\t\tif(length(input$'+ id +'$right) > 0) {\n' 
                genR <-  genR + '\t\tfor(x in 1:length(input$'+ id +'$right)) {\n'
                genR <-  genR + '\t\t\tres <- paste0(res, \'"\', input$'+ id +'$right[[x]], \'"\')\n'
                genR <-  genR + '\t\t\tif(x < length(input$'+ id +'$right)) {\n'
                genR <-  genR + '\t\t\t\tres <- paste0(res, ", ")\n'
                genR <-  genR + '\t\t\t}\n'
                genR <-  genR + '\t\t}\n'
                genR <-  genR + '\t\t}		\n'
                genR <-  genR + '\t\tres <- paste0(res, "]", "\\n")\n\n'
jlopez's avatar
jlopez committed
446
                
447
448
              } else if(cnt$type == "radio") {
                genR <-  genR + '\t\tif(!is.na(as.numeric(input$' + id + '))) {\n'
449
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", input$' + id + ', "\\n", sep = " "))\n'
450
                genR <-  genR + '\t\t} else {\n'
451
                genR <-  genR + '\t\t\tres = paste0(res, paste("' + id + ':", paste0(\'\"\', input$' + id + ', \'\"\'), "\\n", sep = " "))\n'
452
453
454
455
456
                genR <-  genR + '\t\t}'
                genR <-  genR + '\t\n\n'
              }
              
              else {
mmassaviol's avatar
mmassaviol committed
457
                #TODO
jlopez's avatar
jlopez committed
458
              }
mmassaviol's avatar
mmassaviol committed
459
              
460
              # res <- res + genR
mmassaviol's avatar
mmassaviol committed
461
              resG <- resG + genR
jlopez's avatar
jlopez committed
462
            }
mmassaviol's avatar
mmassaviol committed
463
          
jlopez's avatar
jlopez committed
464
          }
mmassaviol's avatar
mmassaviol committed
465
      
466
          # res <- res + '\t}\n'
jlopez's avatar
jlopez committed
467
          
468
          # res <- res + '\n'
mmassaviol's avatar
mmassaviol committed
469
      
jlopez's avatar
jlopez committed
470
        }
mmassaviol's avatar
mmassaviol committed
471
        
jlopez's avatar
jlopez committed
472
473
      }
      
474
      # res <- res + '\twrite(res, file=path_param)'
jlopez's avatar
jlopez committed
475
      
476
      # res <- res + '\n\n'
jlopez's avatar
jlopez committed
477
      
478
      # res <- res + '})\n\n'
jlopez's avatar
update    
jlopez committed
479
480
      
    }
mmassaviol's avatar
mmassaviol committed
481
    #write(res, file=path_opt)
jlopez's avatar
update    
jlopez committed
482
  }
jlopez's avatar
jlopez committed
483
  
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  save_params <- "save_params <- function(path_param){\n"
  save_params <- save_params + '\tres = ""'
  save_params <- save_params + resG
  save_params <- save_params + '\t\tres = paste0(res, paste("final_step:", paste0(\'\"\', input$final_step, \'\"\'), "\\n", sep = " "))\n\n'
  save_params <- save_params + '\ta = yaml.load_file("/workflow/params.total.yml")\n'
  save_params <- save_params + '\tb = yaml.load(res)\n'
  save_params <- save_params + '\tanotb = subset(names(a), !(names(a)%in%names(b)))\n'
  save_params <- save_params + '\tc = c(a[anotb],b)\n'
  save_params <- save_params + '\tlogical = function(x) {\n'
  save_params <- save_params + '\t\tresult <- ifelse(x, "True", "False")\n'
  save_params <- save_params + '\t\tclass(result) <- "verbatim"\n'
  save_params <- save_params + '\t\treturn(result)\n'
  save_params <- save_params + '\t}\n'
  save_params <- save_params + '\tsamples = yaml.load(system(paste0("python3 /workflow/get_samples.py ",input$sample_dir," /samples/groups.csv"),intern = T))\n'
  save_params <- save_params + '\tc$samples = names(samples)\n'
  save_params <- save_params + '\tnames(samples) = NULL\n'
  save_params <- save_params + '\tc$groups = unlist(samples)\n'
  save_params <- save_params + '\twrite_yaml(c,path_param,handlers=list(logical = logical))\n'
  save_params <- save_params + '\t}\n\n'

  res <- save_params

506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  # Compare parameters (give rules that need to be re-runned)
  res <- res + 'compare_params = function(dossierAnalyse){\n'
  res <- res + '\tif (!file.exists(paste0(dossierAnalyse,"/lastrun/params.yml"))){\n'
  res <- res + '\t\treturn(c())\n'
  res <- res + '\t}\n'
  res <- res + '\telse{\n'
  res <- res + '\t\tnew_params = yaml.load_file(paste0(dossierAnalyse,"/params.yml"))\n'
  res <- res + '\t\told_params = yaml.load_file(paste0(dossierAnalyse,"/workflow/params.yml"))\n'
  res <- res + '\t\tchanged = new_params[!(new_params %in% old_params)]\n'
  res <- res + '\t\trules = c()\n'
  res <- res + '\t\tif (length(changed)>=1){\n'
  res <- res + '\t\t\tfor (param in names(changed)){\n'
  res <- res + '\t\t\t\tif (!grepl("_threads$",param)){\n'
  res <- res + '\t\t\t\t\trules = c(rules, new_params$params_info[[param]]$rule)\n'
  res <- res + '\t\t\t\t}\n'
  res <- res + '\t\t\t}\n'
  res <- res + '\t\t}\n'
  res <- res + '\t\treturn(unique(rules))\n'
  res <- res + '\t}\n'
  res <- res + '}\n'

jlopez's avatar
jlopez committed
527
  file.replace(APP$project+"/server/opt_global.R")
528
  #res <- ""
529
530
  # Event RULEGRAPH
  if ("RULEGRAPH" %in% names(APP)){
531
532
533
534
535
536
537
    res <- res + '#\' Event when use RULEGRAPH button\n'
    res <- res + 'observeEvent({c(input$sidebarmenu,input$refresh_rg)}, {\n\n'
    res <- res + '\tif(input$sidebarmenu=="RULEGRAPH"){\n'
    res <- res + '\t\tinput_list <- reactiveValuesToList(input)\n'
    res <- res + '\t\ttoggle_inputs(input_list,F,F)\n'
    res <- res + '\t\tpath_param <- paste0(input$results_dir,"/params.yml")\n\n'
    
538
    res <- res + '\t\tsave_params(path_param)\n'
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

	  res <- res + '\t\ti = sample.int(1000,size = 1)\n\n'
	  res <- res + '\t\tsystem(paste0("rm ",input$results_dir,"/rulegraph*"))\n\n'

    res <- res + '\t\toutUI = tryCatch({\n'
    res <- res + '\t\t\tsystem(paste0("snakemake -s /workflow/Snakefile --configfile ",input$results_dir,"/params.yml -d ",input$results_dir," all --rulegraph > ",input$results_dir,"/rulegraph",i,".dot"),intern=T)\n'
    res <- res + '\t\t\tsystem(paste0("cat ",input$results_dir,"/rulegraph",i,".dot | dot -Tsvg -Gratio=0.75 > ",input$results_dir,"/rulegraph",i,".svg"),intern=T)\n'
    res <- res + '\t\t\ttagList(img(src = paste0("results/rulegraph",i,".svg") ,alt = "Rulegraph of Snakemake jobs",style="max-width: 100%;height: auto;display: block;margin: auto"))},\n'
    res <- res + '\t\terror = function(e){\n'
    res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
    res <- res + '\t\t\treturn(tags$p(paste0("error : ",e$message)))},\n'
    res <- res + '\t\twarning = function(w){\n'
    res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
    res <- res + '\t\t\treturn(tags$p(paste0("error : ",w$message)))})\n'

    res <- res + '\t\taddResourcePath("results", input$results_dir)\n'
    res <- res + '\t\toutput$RULEGRAPH_svg = renderUI(outUI)\n'
    res <- res + '\t\ttoggle_inputs(input_list,T,F)\n'
    res <- res + '}})\n'
558
  }
559
  
mmassaviol's avatar
mmassaviol committed
560
  # Event Run
561
  res <- res + '#\' Event when use '+ run$shiny_button$name +' button\n'
mmassaviol's avatar
mmassaviol committed
562
  res <- res + 'observeEvent(input$'+run$shiny_button$name+', {\n\n'
563

mmassaviol's avatar
mmassaviol committed
564
  res <- res + '\trv$running = T\n'
565
566
  res <- res + '\tinput_list <- reactiveValuesToList(input)\n'
  res <- res + '\ttoggle_inputs(input_list,F,F)\n'
567
  res <- res + '\tupdateTabsetPanel(session, "sidebarmenu", selected = "run_out")\n'
568
  
569
  #panel_path <- APP$project + '/params/' + 'params_' + APP$pages[[x]]$name + '.yml'
mmassaviol's avatar
mmassaviol committed
570
  res <- res + '\tpath_param <- paste0(input$results_dir,"/params.yml")\n\n'
jlopez's avatar
jlopez committed
571
  
572
  res <- res + '\t\tsave_params(path_param)\n'
573
574
  res <- res + '\n\n'

575
  res <- res + '\toutUI = tryCatch({\n'
mmassaviol's avatar
mmassaviol committed
576
  res <- res + '\t\tif (!dir.exists(paste0(input$results_dir,"/logs"))){\n'
577
  res <- res + '\t\t\tdir.create(paste0(input$results_dir,"/logs"))\n'
578
  res <- res + '\t\t}\n'
579
580
  res <- res + '\t\tif (!file.exists(paste0(input$results_dir,"/logs/runlog.txt"))){\n'
  res <- res + '\t\t\tfile.create(paste0(input$results_dir,"/logs/runlog.txt"))\n'
581
  res <- res + '\t\t}\n'
582
583
584
585
586
587
588
589
590
591
592
593
594
595

  res <- res + '\t\tforcerun = compare_params(input$results_dir)\n'
  res <- res + '\t\tif (length(forcerun>1)){\n'
  res <- res + '\t\t\trules = paste(forcerun, collapse=" ")\n'
  res <- res + '\t\t\tforcerun = paste(" --forcerun ",rules)\n'
  res <- res + '\t\t\tshowModal(modalDialog(\n'
  res <- res + '\t\t\t\ttitle = "Params have changed since the last run",\n'
  res <- res + '\t\t\t\tforcerun\n'
  res <- res + '\t\t\t))\n'
  res <- res + '\t\t}\n'
  res <- res + '\t\telse{\n'
  res <- res + '\t\t\tforcerun = ""\n'
  res <- res + '\t\t}\n'

596
  res <- res + '\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow.running"),wait = T)\n'
597
  res <- res + '\t\tsystem2("python3",paste0("-u -m snakemake -s /workflow/Snakefile --configfile ", paste0(input$results_dir,"/params.yml") ,\t" -d ", input$results_dir ,\t" --cores ", input$cores, " all ", forcerun),wait = FALSE, stdout = paste0(input$results_dir,"/logs/runlog.txt"), stderr = paste0(input$results_dir,"/logs/runlog.txt"))\n'
598
599
600
601
602
603
604
605
  res <- res + '\t\ttags$iframe(src="results/multiqc_report.html",width="100%", height="900px")},\n'
  res <- res + '\t\terror = function(e){\n'
  res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
  res <- res + '\t\t\treturn(tags$p(paste0("error : ",e$message)))},\n'
  res <- res + '\t\twarning = function(w){\n'
  res <- res + '\t\t\tsystem(paste0("touch ",input$results_dir,"/logs/workflow_end.error"),wait = T)\n'
  res <- res + '\t\t\treturn(tags$p(paste0("error : ",w$message)))})\n'
  res <- res + '\t\t})'
mmassaviol's avatar
mmassaviol committed
606

jlopez's avatar
jlopez committed
607
  write(res, APP$project+"/server/opt_global.R")
jlopez's avatar
update    
jlopez committed
608
609
610
611
}

#============================================================

jlopez's avatar
jlopez committed
612
613
614
615
generate_menu <- function() {
  
  path_file = APP$project + "/R/menugauche.R"
  
jlopez's avatar
jlopez committed
616
  file.replace(path_file)
jlopez's avatar
jlopez committed
617
618
619
  
  res <- ""
  
jlopez's avatar
jlopez committed
620
  res <- res + 'MenuGauche = sidebarMenu(id="sidebarmenu",\n\n'
jlopez's avatar
jlopez committed
621
  
jlopez's avatar
update    
jlopez committed
622
623
624
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
    icon <- APP$pages[[x]]$icon
jlopez's avatar
jlopez committed
625
    menu <- APP$pages[[x]]$label
mmassaviol's avatar
mmassaviol committed
626
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
jlopez's avatar
update    
jlopez committed
627
  }
628

629
630
  if("RULEGRAPH" %in% names(APP)){
    name <- "RULEGRAPH"
631
    icon <- "gear"
632
    menu <- "Rule Graph"
mmassaviol's avatar
mmassaviol committed
633
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
634
  }
635

mmassaviol's avatar
mmassaviol committed
636
  res <- res + '\ttags$br(),\n\n'
637

mmassaviol's avatar
mmassaviol committed
638
  res <- res + '\tnumericInput("cores", label = "Threads available", min = 1, max = NA, step = 1, width =  "auto", value = 4),\n'
639
  
640
641
  # select step to reach
  choices = "list("
642
  for(step in APP$pages[2:length(APP$pages)]){ # all steps but remove global_params page
643
644
645
    choices <- choices + "'" + step$label + "'='" + step$name + "',"
  }
  res <- res + 'selectInput("final_step", label = "Select the step to reach : ", selected = "all", choices = '+choices+'"All"="all")),'
646
  
647
  #
648
649

  res <- res + '\ttags$br(),\n'
jlopez's avatar
jlopez committed
650
  
mmassaviol's avatar
mmassaviol committed
651
  res <- res + '\tactionButton("'+ APP$run$shiny_button$name +'", "'+ APP$run$shiny_button$label +'",  icon("'+ APP$run$shiny_button$icon +'"), class="'+ APP$run$shiny_button$class +'"),\n\n'
652
653
  res <- res + '\tactionButton("StopPipeline", "Stop pipeline",  icon("stop"), class="btn btn-secondary"),\n'
  
mmassaviol's avatar
mmassaviol committed
654
655
656
  res <- res + '\ttags$br(),\n'
  res <- res + '\ttags$br(),\n'

657
  res <- res + '\tmenuItem("Running Workflow output", tabName="run_out", icon=icon("terminal", lib="font-awesome"), newtab=FALSE),\n'
mmassaviol's avatar
mmassaviol committed
658

659
660
661
  if("Report" %in% names(APP)){
    name <- "Report"
    icon <- "file"
662
    menu <- "Final report"
mmassaviol's avatar
mmassaviol committed
663
    res <- res + '\tmenuItem("'+menu+'", tabName="'+name+'", icon=icon("'+icon+'", lib="font-awesome"), newtab=FALSE),\n\n'
664
  }
665
666
667

  res <- res + '\ttags$br(),\n'
  res <- res + '\tactionButton("close_session", "Close session",  icon("times"), class="btn btn-primary"),\n'
668
  
669
  if("download" %in% names(APP)){
mmassaviol's avatar
mmassaviol committed
670

mmassaviol's avatar
mmassaviol committed
671
    res <- res + '\ttags$br(),\n\n'
672

mmassaviol's avatar
mmassaviol committed
673
    res <- res + '\tdownloadButton("'+ APP$download$shiny_button$name +'", "'+ APP$download$shiny_button$label +'", class="'+ APP$download$shiny_button$class +'", style="color:black;margin: 6px 5px 6px 15px;"),\n\n'
674
675

  }
jlopez's avatar
jlopez committed
676
  
mmassaviol's avatar
mmassaviol committed
677
  res <- res + '\ttags$br(),tags$br(),\n\n'
jlopez's avatar
jlopez committed
678
  
mmassaviol's avatar
mmassaviol committed
679
  res <- res + '\tmenuItem("Powered by mbb", href="http://mbb.univ-montp2.fr/MBB/index.php", newtab=TRUE, icon=icon("book", lib="font-awesome"), selected=NULL)\n\n'
jlopez's avatar
jlopez committed
680
  
jlopez's avatar
jlopez committed
681
  res <- res + ')\n\n'
jlopez's avatar
jlopez committed
682
683
684
685
686
687
688
  
  write(res, file = path_file)
  
}

#============================================================

jlopez's avatar
jlopez committed
689
690
691
692
693
694
dir.create.not.exist <- function(path) {
  if(!dir.exists(path)) {
    dir.create(path)
  }
}

jlopez's avatar
jlopez committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#============================================================

generate_rproj <- function(path) {
  file.replace(path)
  res <- ""
  res <- res + "Version: 1.0\n"
  res <- res + " \n"
  res <- res + "RestoreWorkspace: Default\n"
  res <- res + "SaveWorkspace: Default\n"
  res <- res + "AlwaysSaveHistory: Default\n"
  res <- res + " \n"
  res <- res + "EnableCodeIndexing: Yes\n"
  res <- res + "UseSpacesForTab: Yes\n"
  res <- res + "NumSpacesForTab: 2\n"
  res <- res + "Encoding: UTF-8\n"
  res <- res + " \n"
  res <- res + "RnwWeave: Sweave\n"
  res <- res + "LaTeX: pdfLaTeX\n"
  res <- res + " \n"
  write(res, path)
}

#============================================================

jlopez's avatar
jlopez committed
719
720
721
722
generate <- function() {
  
  print("Generate Start")
  
mmassaviol's avatar
mmassaviol committed
723
  #YML_TOOLS <<- get_predefined_tool(files_tools)
724
  
jlopez's avatar
jlopez committed
725
  if(dir.exists(APP$project)) {
jlopez's avatar
jlopez committed
726
    #unlink(APP$project, recursive=TRUE)
jlopez's avatar
jlopez committed
727
728
  }
  
jlopez's avatar
jlopez committed
729
  dir.create.not.exist(APP$project)
jlopez's avatar
jlopez committed
730
731
  generate_app()
  
jlopez's avatar
jlopez committed
732
  dir.create.not.exist(APP$project + "/R")
jlopez's avatar
jlopez committed
733
734
  generate_menu()
  
jlopez's avatar
jlopez committed
735
736
  dir.create.not.exist(APP$project + "/pages")
  dir.create.not.exist(APP$project + "/server")
737
  #dir.create.not.exist(APP$project + "/params")
jlopez's avatar
update    
jlopez committed
738
739
  
  generate_pages_server()
jlopez's avatar
jlopez committed
740

jlopez's avatar
jlopez committed
741
742
  dir.create.not.exist(APP$project + "/www")
  
jlopez's avatar
jlopez committed
743
744
  path_rproj = APP$project + "/" + basename(APP$project) + ".Rproj"
  
jlopez's avatar
jlopez committed
745
  generate_rproj(path_rproj)
jlopez's avatar
jlopez committed
746
  
jlopez's avatar
jlopez committed
747
748
  generate_added_styles()
  
jlopez's avatar
jlopez committed
749
750
751
  file.copy("./tools/chooser.R", APP$project + "/R/")
  file.copy("./tools/chooser-binding.js", APP$project + "/www/")
  
jlopez's avatar
jlopez committed
752
753
754
755
756
  print("Generate End")
}

#============================================================

jlopez's avatar
jlopez committed
757
758
759
760
761
762
763
764
765
766
file.replace <- function(path) {
  if(file.exists(path)) {
    file.remove(path)
  }
  
  file.create(path)
}

#============================================================

jlopez's avatar
jlopez committed
767
768
769
770
771
generate_app <- function() {
  path_project = APP$project + "/app.R"

  main = APP$general
  
jlopez's avatar
jlopez committed
772
  file.replace(path_project)
jlopez's avatar
jlopez committed
773
  
jlopez's avatar
jlopez committed
774
  res <- "#@author jimmy.lopez@univ-montp2.fr\n\n\n\n"
jlopez's avatar
jlopez committed
775
  
jlopez's avatar
update    
jlopez committed
776
777
778
779
780
781
782
  res <- res + 'library(shiny)\n'
  res <- res + 'library(shinydashboard)\n'
  res <- res + 'library(shinyjs)\n'
  res <- res + 'library(yaml)\n'
  res <- res + 'library(stringr)\n'
  res <- res + 'library(shinyFiles)\n'
  res <- res + 'library(tools)\n'
jlopez's avatar
jlopez committed
783
784
  res <- res + 'library(knitr)\n'
  res <- res + 'library(DT)\n'
jlopez's avatar
update    
jlopez committed
785
  
jlopez's avatar
jlopez committed
786
  res <- res + '\n\n'
jlopez's avatar
jlopez committed
787
  
jlopez's avatar
jlopez committed
788
789
  res <- res + 'source("./R/chooser.R", local=T)\n\n'
  
jlopez's avatar
update    
jlopez committed
790
791
792
793
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
    res <- res + 'source("./pages/pages_def_'+tolower(name)+'.R", local=T)\n'
  }
794
  if("RULEGRAPH" %in% names(APP)){
795
    res <- res + "tabRULEGRAPH = fluidPage(box(title = 'Rule Graph :', width = 12, status = 'primary', collapsible = TRUE, solidHeader = TRUE, uiOutput('RULEGRAPH_svg'),actionButton('refresh_rg', 'Refresh',  icon('sync'), class='btn btn-info')))\n"
796
797
798
  }
  if("Report" %in% names(APP)){
    res <- res + "tabReport = fluidPage(box(title = 'Report :', width = 12, status = 'primary', collapsible = TRUE, solidHeader = TRUE, uiOutput('report_html')))\n"
799
  }
mmassaviol's avatar
mmassaviol committed
800
801

  res <- res + 'tabRUN = fluidPage(box(title = \'Run :\', width = 12 , status = \'primary\', collapsible = TRUE, solidHeader = TRUE, uiOutput(\'run_out\',style = \'overflow-y: scroll; height: 600px\')))\n'
jlopez's avatar
jlopez committed
802
  
jlopez's avatar
jlopez committed
803
  res <- res + 'source("./R/menugauche.R", local=T)\n\n'
jlopez's avatar
jlopez committed
804
  
jlopez's avatar
jlopez committed
805
806
807
808
  
  
  
  
jlopez's avatar
jlopez committed
809
  res <- res + '\n\n'
jlopez's avatar
jlopez committed
810
  
jlopez's avatar
jlopez committed
811
  res <- res + 'style <- tags$style(HTML(readLines("www/added_styles.css")))\n\n'
jlopez's avatar
jlopez committed
812
  
jlopez's avatar
jlopez committed
813
814
815
816
817
818
819
  res <- res + 'UI <- dashboardPage(\n\n'
  res <- res + '    skin="' + main$skin + '",\n\n'
  res <- res + '    dashboardHeader(title="'+ main$title + '", titleWidth='+main$menu_width+'),\n\n'
  res <- res + '    dashboardSidebar(width='+main$menu_width+', MenuGauche),\n\n'
  res <- res + '    dashboardBody(\n\n'
  res <- res + '        shinyjs::useShinyjs(),\n\n'
  res <- res + '                 tags$head(tags$link(rel="stylesheet", type="text/css", href="bootstrap.min.readable.css")),\n\n'
jlopez's avatar
jlopez committed
820
821
  #tags$head(tags$script(src="message-handler.js")),
  
jlopez's avatar
jlopez committed
822
823
  res <- res + 'tags$head(style),\n\n'
  res <- res + '\ttabItems(\n\n'
jlopez's avatar
update    
jlopez committed
824
825
826
827
  
  for(x in 1:length(APP$pages)) {
    name <- APP$pages[[x]]$name
    
jlopez's avatar
jlopez committed
828
    res <- res + '\ttabItem(tabName = "' + name + '",         tab'+ name +')'
jlopez's avatar
update    
jlopez committed
829
830
    
    if(x < length(APP$pages)) {
jlopez's avatar
jlopez committed
831
      res <- res + ",\n\n"
jlopez's avatar
update    
jlopez committed
832
    } else {
jlopez's avatar
jlopez committed
833
      res <- res + "\n\n"
jlopez's avatar
update    
jlopez committed
834
    }
835
  }
836
  if("RULEGRAPH" %in% names(APP)){
837
    res <- res + '\t,tabItem(tabName = "RULEGRAPH",         tabRULEGRAPH)\n'
jlopez's avatar
update    
jlopez committed
838
  }
839
  if("Report" %in% names(APP)){
840
    res <- res + '\t,tabItem(tabName = "Report",         tabReport)\n'
841
  }
842
  res <- res + '\t,tabItem(tabName = "run_out",         tabRUN)\n'
jlopez's avatar
update    
jlopez committed
843
  
jlopez's avatar
jlopez committed
844
845
846
  res <- res + '     )\n\n'
  res <- res + ')\n\n'
  res <- res + ')\n\n'
mmassaviol's avatar
mmassaviol committed
847
848
849
850
851
852

  # function to reload params, rulegraph and report if they exists
  res <- res + 'reload = function(dossierAnalyse,session,output){\n'
  res <- res + '\t# if params exists reload them\n'
  res <- res + '\tif (file.exists(paste0(dossierAnalyse,"/params.yml"))){\n'
  res <- res + '\t\tparams = read_yaml(paste0(dossierAnalyse,"/params.yml"))\n'
853
854
  res <- res + '\t\tfor (param in names(params$params_info)){\n'
  res <- res + '\t\t\tif (params$params_info[[param]]$type == "text"){\n'
mmassaviol's avatar
mmassaviol committed
855
856
  res <- res + '\t\t\t\tupdateTextInput(session, param, value = params[[param]])\n'
  res <- res + '\t\t\t}\n'
857
  res <- res + '\t\t\tif (params$params_info[[param]]$type == "numeric"){\n'
mmassaviol's avatar
mmassaviol committed
858
859
  res <- res + '\t\t\t\tupdateNumericInput(session, param, value = params[[param]])\n'
  res <- res + '\t\t\t}\n'
860
  res <- res + '\t\t\tif (params$params_info[[param]]$type == "radio"){\n'
mmassaviol's avatar
mmassaviol committed
861
862
  res <- res + '\t\t\t\tupdateRadioButtons(session, param, selected = params[[param]])\n'
  res <- res + '\t\t\t}\n'
863
  res <- res + '\t\t\tif (params$params_info[[param]]$type == "select"){\n'
mmassaviol's avatar
mmassaviol committed
864
865
866
  res <- res + '\t\t\t\tupdateSelectInput(session, param, selected = params[[param]])\n'
  res <- res + '\t\t\t}\n'
  res <- res + '\t\t}\n'
867
868
869
  res <- res + '\t\tfor (step in params$steps){\n'
  res <- res + '\t\t\tupdateSelectInput(session, paste0("select",step$name), selected = params[[step$name]])\n'
  res <- res + '\t\t}\n'
mmassaviol's avatar
mmassaviol committed
870
871
872
  res <- res + '\t}\n'
  res <- res + '\t# if rulegraph show it\n'
  res <- res + '\trulegraph = list.files(path=dossierAnalyse,pattern="rulegraph[[:digit:]]+.svg")\n'
873
  res <- res + '\tif (length(rulegraph) == 1){\n'
mmassaviol's avatar
mmassaviol committed
874
  res <- res + '\t\taddResourcePath("results", dossierAnalyse)\n'
875
876
877
878
879
  res <- res + '\t\toutput$RULEGRAPH_svg = renderUI(tagList(img(src = paste0("results/",rulegraph) ,alt = "Rulegraph of Snakemake jobs",style="max-width: 100%;height: auto;display: block;margin: auto")))\n'
  res <- res + '\t}\n'
  res <- res + '\telse{\n'
  res <- res + '\t\t\toutput$RULEGRAPH_svg = renderUI(tagList(h3("No rule graph found, press the rule graph button to generate one.")))\n'
  res <- res + '\t}\n'
mmassaviol's avatar
mmassaviol committed
880
  res <- res + '\t# if report show it\n'
881
  res <- res + '\tif (file.exists(paste0(dossierAnalyse,"/multiqc_report.html"))){\n'
mmassaviol's avatar
mmassaviol committed
882
  res <- res + '\t\taddResourcePath("results", dossierAnalyse)\n'
883
884
885
886
  res <- res + '\t\toutput$report_html = renderUI(tags$iframe(src=paste0("results/multiqc_report.html"),width="100%", height="900px"))\n'
  res <- res + '\t}\n'
  res <- res + '\telse{\n'
  res <- res + '\t\t\toutput$report_html = renderUI(tags$h3("No report found, run the pipeline to produce a report"))\n'
mmassaviol's avatar
mmassaviol committed
887
888
  res <- res + '\t}\n'
  res <- res + '}\n'
jlopez's avatar
jlopez committed
889
  
jlopez's avatar
jlopez committed
890
  res <- res + '\n\n'
jlopez's avatar
jlopez committed
891
  
jlopez's avatar
jlopez committed
892
  res <- res + 'server <- function( input, output, session) {\n\n'
893
  res <- res + '\trv <- reactiveValues(textstream = c(""), running = FALSE, timer = reactiveTimer(1000))\n\n'
894

mmassaviol's avatar
mmassaviol committed
895
896
897
898
899
900
901
902
903
904
905
906
  # Observe query to find user and directory
  res <- res + 'observeEvent(parseQueryString(session$clientData$url_search),{\n'
  res <- res + '\t\tquery <- parseQueryString(session$clientData$url_search)\n'
  res <- res + '\t\tif (!is.null(query[[\'nom\']]) & !is.null(query[[\'prenom\']]) & !is.null(query[[\'dossier\']])) {\n'
  res <- res + '\t\t\tnom\t<<-\tquery$nom\n'
  res <- res + '\t\t\tprenom <<- query$prenom\n'
  res <- res + '\t\t\tdossier <<- query$dossier\n'
  res <- res + '\t\t}\n'
  res <- res + '\t\telse {\n'
  res <- res + '\t\t\tnom\t<<-\t"anonym"\n'
  res <- res + '\t\t\tprenom <<- "anonym"\n'
  res <- res + '\t\t\tdossier <<- format(Sys.time(), "%d-%m-%Y_%H-%M-%S")\n'
907
  res <- res + '\t\t\t \n'