comparison fastqc_report_render_ori.R @ 14:2efa46ce2c4c draft

upgrade fastqc_report
author mingchen0919
date Wed, 18 Oct 2017 22:06:39 -0400
parents
children
comparison
equal deleted inserted replaced
13:9d3586701985 14:2efa46ce2c4c
1 ##======= Handle arguments from command line ========
2 # setup R error handline to go to stderr
3 options(show.error.messages = FALSE,
4 error = function(){
5 cat(geterrmessage(), file = stderr())
6 quit("no", 1, F)
7 })
8
9 # we need that to not crash galaxy with an UTF8 error on German LC settings.
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
11
12 # suppress warning
13 options(warn = - 1)
14
15 options(stringsAsFactors = FALSE, useFancyQuotes = FALSE)
16 args = commandArgs(trailingOnly = TRUE)
17
18 suppressPackageStartupMessages({
19 library(getopt)
20 library(tools)
21 })
22
23 # column 1: the long flag name
24 # column 2: the short flag alias. A SINGLE character string
25 # column 3: argument mask
26 # 0: no argument
27 # 1: argument required
28 # 2: argument is optional
29 # column 4: date type to which the flag's argument shall be cast.
30 # possible values: logical, integer, double, complex, character.
31 spec_list = list()
32 spec_list$READS = c('reads', 'r', '1', 'character')
33 spec_list$ECHO = c('echo', 'e', '1', 'character')
34 spec_list$FASTQC_TPL = c('fastqc_tpl', 'p', 1, 'character')
35 spec_list$REPORT = c('report', 'o', '1', 'character')
36 spec_list$REPORT_OUTPUT_DIR = c('report_output_dir', 'd', '1', 'character')
37
38
39 spec = t(as.data.frame(spec_list))
40
41 opt = getopt(spec)
42 # arguments are accessed by long flag name (the first column in the spec matrix)
43 # NOT by element name in the spec_list
44 # example: opt$help, opt$expression_file
45 ##====== End of arguments handling ==========
46
47
48 mgsub = function(pattern, replacement, x) {
49 if (length(pattern) != length(replacement)) {
50 stop("pattern and replacement have to be the same in length")
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