annotate fastqc_report_render.R @ 14:2efa46ce2c4c draft

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