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