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