Mercurial > repos > mingchen0919 > rmarkdown_fastqc_report
comparison 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 |
comparison
equal
deleted
inserted
replaced
13:9d3586701985 | 14:2efa46ce2c4c |
---|---|
1 ##======= Handle arguments from command line ======== | 1 library(getopt) |
2 # setup R error handline to go to stderr | 2 library(rmarkdown) |
3 options(show.error.messages=FALSE, | 3 library(htmltools) |
4 error=function(){ | 4 library(plyr) |
5 cat(geterrmessage(), file=stderr()) | 5 library(dplyr) |
6 quit("no", 1, F) | 6 library(stringr) |
7 }) | 7 library(highcharter) |
8 library(DT) | |
9 library(reshape2) | |
10 library(plotly) | |
11 library(formattable) | |
8 | 12 |
9 # we need that to not crash galaxy with an UTF8 error on German LC settings. | 13 ##============ Sink warnings and errors to a file ============== |
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | 14 ## use the sink() function to wrap all code within it. |
11 | 15 ##============================================================== |
12 # suppress warning | 16 zz = file('warnings_and_errors.txt') |
13 options(warn = -1) | 17 sink(zz) |
14 | 18 sink(zz, type = 'message') |
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE) | 19 ##---------below is the code for rendering .Rmd templates----- |
16 args = commandArgs(trailingOnly=TRUE) | 20 |
17 | 21 ##=============STEP 1: handle command line arguments========== |
18 suppressPackageStartupMessages({ | 22 ## |
19 library(getopt) | 23 ##============================================================ |
20 library(tools) | 24 # column 1: the long flag name |
21 }) | 25 # column 2: the short flag alias. A SINGLE character string |
22 | 26 # column 3: argument mask |
23 # column 1: the long flag name | 27 # 0: no argument |
24 # column 2: the short flag alias. A SINGLE character string | 28 # 1: argument required |
25 # column 3: argument mask | 29 # 2: argument is optional |
26 # 0: no argument | 30 # column 4: date type to which the flag's argument shall be cast. |
27 # 1: argument required | 31 # possible values: logical, integer, double, complex, character. |
28 # 2: argument is optional | 32 #------------------------------------------------------------- |
29 # column 4: date type to which the flag's argument shall be cast. | 33 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ |
30 # possible values: logical, integer, double, complex, character. | 34 # 1. short flag alias should match the flag in the command section in the XML file. |
31 spec_list=list() | 35 # 2. long flag name can be any legal R variable names |
32 spec_list$READS = c('reads', 'r', '1', 'character') | 36 # 3. two names in args_list can have common string but one name should not be a part of another name. |
33 spec_list$ECHO = c('echo', 'e', '1', 'character') | 37 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. |
34 spec_list$FASTQC_TPL = c('fastqc_tpl', 'p', 1, 'character') | 38 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
35 spec_list$REPORT = c('report', 'o', '1', 'character') | 39 args_list=list() |
36 spec_list$REPORT_OUTPUT_DIR = c('report_output_dir', 'd', '1', 'character') | 40 ##------- 1. input data --------------------- |
41 args_list$ECHO = c('echo', 'e', '1', 'character') | |
42 args_list$READS = c('reads', 'r', '1', 'character') | |
43 ##--------2. output report and outputs -------------- | |
44 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character') | |
45 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') | |
46 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character') | |
47 ##--------3. .Rmd templates in the tool directory ---------- | |
48 args_list$FASTQC_REPORT_RMD = c('fastqc_report_rmd', 't', '1', 'character') | |
49 ##----------------------------------------------------------- | |
50 opt = getopt(t(as.data.frame(args_list))) | |
37 | 51 |
38 | 52 |
39 spec = t(as.data.frame(spec_list)) | 53 |
54 ##=======STEP 2: create report directory (optional)========== | |
55 ## | |
56 ##=========================================================== | |
57 dir.create(opt$report_dir) | |
58 | |
59 ##=STEP 3: replace placeholders in .Rmd with argument values= | |
60 ## | |
61 ##=========================================================== | |
62 #++ need to replace placeholders with args values one by one+ | |
63 readLines(opt$fastqc_report_rmd) %>% | |
64 (function(x) { | |
65 gsub('ECHO', opt$echo, x) | |
66 }) %>% | |
67 (function(x) { | |
68 gsub('READS', opt$reads, x) | |
69 }) %>% | |
70 (function(x) { | |
71 gsub('REPORT_DIR', opt$output_dir, x) | |
72 }) %>% | |
73 (function(x) { | |
74 fileConn = file('fastqc_report.Rmd') | |
75 writeLines(x, con=fileConn) | |
76 close(fileConn) | |
77 }) | |
78 | |
40 | 79 |
41 opt = getopt(spec) | 80 ##=============STEP 4: render .Rmd templates================= |
42 # arguments are accessed by long flag name (the first column in the spec matrix) | 81 ## |
43 # NOT by element name in the spec_list | 82 ##=========================================================== |
44 # example: opt$help, opt$expression_file | 83 render('fastqc_report.Rmd', output_file = opt$report_html) |
45 ##====== End of arguments handling ========== | |
46 | 84 |
47 | 85 |
48 mgsub = function(pattern, replacement, x) { | 86 ##--------end of code rendering .Rmd templates---------------- |
49 if(length(pattern) != length(replacement) ) { | 87 sink() |
50 stop("pattern and replacement have to be the same in length") | 88 ##=========== End of sinking output============================= |
51 } | |
52 | |
53 result = x | |
54 | |
55 for(i in 1:length(pattern)) { | |
56 result = try( gsub(pattern[i], replacement[i], x = result) ) | |
57 } | |
58 | |
59 result | |
60 } | |
61 | |
62 | |
63 ##====== replace variables in tpl file ====== | |
64 p = c('READS', | |
65 'ECHO', | |
66 'FASTQC_TPL', | |
67 'REPORT_OUTPUT_DIR', | |
68 'REPORT') | |
69 r = c(opt$reads, | |
70 opt$echo, | |
71 opt$fastqc_tpl, | |
72 opt$report_output_dir, | |
73 opt$report) | |
74 | |
75 fastqc_report_tpl = mgsub(p, r, readLines(opt$fastqc_tpl)) | |
76 | |
77 ##====== write replaced text into Rmd file === | |
78 fileConn = file('fastqc_report.Rmd') | |
79 writeLines(fastqc_report_tpl, con=fileConn) | |
80 close(fileConn) | |
81 | |
82 ##====== render Rmd files ==================== | |
83 rmarkdown::render('fastqc_report.Rmd') | |
84 file.copy('fastqc_report.html', opt$report, recursive=TRUE) | |
85 paste0('cp -r ./* ', opt$report_output_dir) %>% | |
86 system() | |
87 |