annotate fastqc_report_render.R @ 3:1ed094d8871c draft

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