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')
|
|
43 ##--------2. output report and outputs --------------
|
|
44 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
|
|
45 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
|
|
46 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
|
|
47 ##--------3. .Rmd templates in the tool directory ----------
|
|
48 args_list$FASTQC_REPORT_RMD = c('fastqc_report_rmd', 't', '1', 'character')
|
|
49 ##-----------------------------------------------------------
|
|
50 opt = getopt(t(as.data.frame(args_list)))
|
1
|
51
|
|
52
|
14
|
53
|
|
54 ##=======STEP 2: create report directory (optional)==========
|
|
55 ##
|
|
56 ##===========================================================
|
|
57 dir.create(opt$report_dir)
|
|
58
|
|
59 ##=STEP 3: replace placeholders in .Rmd with argument values=
|
|
60 ##
|
|
61 ##===========================================================
|
|
62 #++ need to replace placeholders with args values one by one+
|
|
63 readLines(opt$fastqc_report_rmd) %>%
|
|
64 (function(x) {
|
|
65 gsub('ECHO', opt$echo, x)
|
|
66 }) %>%
|
|
67 (function(x) {
|
|
68 gsub('READS', opt$reads, x)
|
|
69 }) %>%
|
|
70 (function(x) {
|
|
71 gsub('REPORT_DIR', opt$output_dir, x)
|
|
72 }) %>%
|
|
73 (function(x) {
|
|
74 fileConn = file('fastqc_report.Rmd')
|
|
75 writeLines(x, con=fileConn)
|
|
76 close(fileConn)
|
|
77 })
|
|
78
|
1
|
79
|
14
|
80 ##=============STEP 4: render .Rmd templates=================
|
|
81 ##
|
|
82 ##===========================================================
|
|
83 render('fastqc_report.Rmd', output_file = opt$report_html)
|
1
|
84
|
|
85
|
14
|
86 ##--------end of code rendering .Rmd templates----------------
|
|
87 sink()
|
|
88 ##=========== End of sinking output============================= |