annotate rmarkdown_samtools_flagstat_render.R @ 5:2a244f3c48a9 draft default tip

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