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