0
|
1 library(getopt)
|
|
2 library(rmarkdown)
|
|
3 library(htmltools)
|
|
4 library(dplyr)
|
1
|
5 library(DT)
|
|
6 library(DESeq2)
|
0
|
7
|
|
8 ##============ Sink warnings and errors to a file ==============
|
|
9 ## use the sink() function to wrap all code within it.
|
|
10 ##==============================================================
|
|
11 zz = file('warnings_and_errors.txt')
|
|
12 sink(zz)
|
|
13 sink(zz, type = 'message')
|
|
14 ##---------below is the code for rendering .Rmd templates-----
|
|
15
|
|
16 ##=============STEP 1: handle command line arguments==========
|
|
17 ##
|
|
18 ##============================================================
|
|
19 # column 1: the long flag name
|
|
20 # column 2: the short flag alias. A SINGLE character string
|
|
21 # column 3: argument mask
|
|
22 # 0: no argument
|
|
23 # 1: argument required
|
|
24 # 2: argument is optional
|
|
25 # column 4: date type to which the flag's argument shall be cast.
|
|
26 # possible values: logical, integer, double, complex, character.
|
|
27 #-------------------------------------------------------------
|
|
28 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++
|
|
29 # 1. short flag alias should match the flag in the command section in the XML file.
|
|
30 # 2. long flag name can be any legal R variable names
|
|
31 # 3. two names in args_list can have common string but one name should not be a part of another name.
|
|
32 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems.
|
|
33 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
34 args_list=list()
|
|
35 ##------- 1. input data ---------------------
|
1
|
36 args_list$ECHO = c('echo', 'e', '1', 'logical')
|
0
|
37 args_list$c = c('count_data', 'c', '1', 'character')
|
1
|
38 args_list$n = c('count_matrix_column_names', 'n', '1', 'character')
|
|
39 args_list$n = c('count_matrix_column_names', 'n', '1', 'character')
|
0
|
40 args_list$C = c('col_data', 'C', '1', 'character')
|
|
41 args_list$D = c('design_formula', 'D', '1', 'character')
|
|
42 args_list$k = c('contrast_condition', 'k', '1', 'character')
|
|
43 args_list$T = c('treatment', 'T', '1', 'character')
|
|
44 args_list$K = c('control', 'K', '1', 'character')
|
|
45 ##--------2. output report and outputs --------------
|
|
46 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
|
|
47 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
|
|
48 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
|
|
49 ##--------3. .Rmd templates in the tool directory ----------
|
|
50 args_list$TOOL_TEMPLATE_RMD = c('tool_template_rmd', 't', '1', 'character')
|
|
51 ##-----------------------------------------------------------
|
|
52 opt = getopt(t(as.data.frame(args_list)))
|
|
53
|
|
54
|
|
55
|
|
56 ##=======STEP 2: create report directory (optional)==========
|
|
57 ##
|
|
58 ##===========================================================
|
|
59 dir.create(opt$report_dir)
|
|
60
|
|
61 ##=STEP 3: replace placeholders in .Rmd with argument values=
|
|
62 ##
|
|
63 ##===========================================================
|
|
64 #++ need to replace placeholders with args values one by one+
|
|
65 readLines(opt$tool_template_rmd) %>%
|
|
66 # (function(x) {
|
|
67 # gsub('ECHO', opt$echo, x)
|
|
68 # }) %>%
|
|
69 # (function(x) {
|
|
70 # gsub('REPORT_DIR', opt$report_dir, x)
|
|
71 # }) %>%
|
|
72 (function(x) {
|
|
73 fileConn = file('tool_template.Rmd')
|
|
74 writeLines(x, con=fileConn)
|
|
75 close(fileConn)
|
|
76 })
|
|
77
|
|
78
|
|
79 ##=============STEP 4: render .Rmd templates=================
|
|
80 ##
|
|
81 ##===========================================================
|
|
82 render('tool_template.Rmd', output_file = opt$report_html)
|
|
83
|
|
84
|
|
85 ##--------end of code rendering .Rmd templates----------------
|
|
86 sink()
|
|
87 ##=========== End of sinking output=============================
|