annotate fastqc_report_render_ori.R @ 14:2efa46ce2c4c draft

upgrade fastqc_report
author mingchen0919
date Wed, 18 Oct 2017 22:06:39 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
1 ##======= Handle arguments from command line ========
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
2 # setup R error handline to go to stderr
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
3 options(show.error.messages = FALSE,
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
4 error = function(){
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
5 cat(geterrmessage(), file = stderr())
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
6 quit("no", 1, F)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
7 })
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
8
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
9 # we need that to not crash galaxy with an UTF8 error on German LC settings.
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
11
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
12 # suppress warning
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
13 options(warn = - 1)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
14
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
15 options(stringsAsFactors = FALSE, useFancyQuotes = FALSE)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
16 args = commandArgs(trailingOnly = TRUE)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
17
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
18 suppressPackageStartupMessages({
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
19 library(getopt)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
20 library(tools)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
21 })
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
22
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
23 # column 1: the long flag name
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
24 # column 2: the short flag alias. A SINGLE character string
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
25 # column 3: argument mask
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
26 # 0: no argument
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
27 # 1: argument required
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
28 # 2: argument is optional
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
29 # column 4: date type to which the flag's argument shall be cast.
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
30 # possible values: logical, integer, double, complex, character.
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
31 spec_list = list()
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
32 spec_list$READS = c('reads', 'r', '1', 'character')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
33 spec_list$ECHO = c('echo', 'e', '1', 'character')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
34 spec_list$FASTQC_TPL = c('fastqc_tpl', 'p', 1, 'character')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
35 spec_list$REPORT = c('report', 'o', '1', 'character')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
36 spec_list$REPORT_OUTPUT_DIR = c('report_output_dir', 'd', '1', 'character')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
37
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
38
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
39 spec = t(as.data.frame(spec_list))
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
40
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
41 opt = getopt(spec)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
42 # arguments are accessed by long flag name (the first column in the spec matrix)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
43 # NOT by element name in the spec_list
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
44 # example: opt$help, opt$expression_file
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
45 ##====== End of arguments handling ==========
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
46
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
47
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
48 mgsub = function(pattern, replacement, x) {
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
49 if (length(pattern) != length(replacement)) {
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
50 stop("pattern and replacement have to be the same in length")
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
51 }
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
52
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
53 result = x
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
54
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
55 for (i in 1 : length(pattern)) {
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
56 result = try(gsub(pattern[i], replacement[i], x = result))
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
57 }
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
58
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
59 result
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
60 }
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
61
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
62
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
63 ##====== replace variables in tpl file ======
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
64 p = c('READS',
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
65 'ECHO',
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
66 'FASTQC_TPL',
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
67 'REPORT_OUTPUT_DIR',
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
68 'REPORT')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
69 r = c(opt$reads,
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
70 opt$echo,
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
71 opt$fastqc_tpl,
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
72 opt$report_output_dir,
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
73 opt$report)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
74
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
75 fastqc_report_tpl = mgsub(p, r, readLines(opt$fastqc_tpl))
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
76
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
77 ##====== write replaced text into Rmd file ===
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
78 fileConn = file('fastqc_report.Rmd')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
79 writeLines(fastqc_report_tpl, con = fileConn)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
80 close(fileConn)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
81
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
82 ##====== render Rmd files ====================
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
83 rmarkdown::render('fastqc_report.Rmd')
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
84 file.copy('fastqc_report.html', opt$report, recursive = TRUE)
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
85 paste0('cp -r ./* ', opt$report_output_dir) %>%
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
86 system()
2efa46ce2c4c upgrade fastqc_report
mingchen0919
parents:
diff changeset
87