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