annotate DESeq_render.R @ 2:cf6012738737 draft

Uploaded
author mingchen0919
date Mon, 07 Aug 2017 18:10:42 -0400
parents dfe3cb3f3b39
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
1 ##======= Handle arguments from command line ========
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
2 # setup R error handline to go to stderr
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
3 options(show.error.messages=FALSE,
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
4 error=function(){
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
5 cat(geterrmessage(), file=stderr())
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
6 quit("no", 1, F)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
7 })
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
8
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
9 # we need that to not crash galaxy with an UTF8 error on German LC settings.
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
11
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
12 # suppress warning
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
13 options(warn = -1)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
14
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
16 args = commandArgs(trailingOnly=TRUE)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
17
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
18 suppressPackageStartupMessages({
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
19 library(getopt)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
20 library(tools)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
21 })
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
22
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
23 # column 1: the long flag name
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
24 # column 2: the short flag alias. A SINGLE character string
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
25 # column 3: argument mask
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
26 # 0: no argument
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
27 # 1: argument required
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
28 # 2: argument is optional
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
29 # column 4: date type to which the flag's argument shall be cast.
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
30 # possible values: logical, integer, double, complex, character.
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
31 spec_list=list()
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
32
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
33 ##------- 1. input data ---------------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
34 spec_list$COUNT_FILES = c('count_files', 'c', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
35 spec_list$ECHO = c('echo', 'e', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
36 spec_list$SAMPLE_TABLE = c('sample_table', 's', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
37 spec_list$DESIGN_FORMULA = c('design_formula', 'p', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
38
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
39 ##--------2. output report and report site directory --------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
40 spec_list$OUTPUT_HTML = c('deseq_html', 'o', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
41 spec_list$OUTPUT_DIR = c('deseq_dir', 'd', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
42 spec_list$WORKSPACE = c('deseq_workspace', 'w', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
43
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
44 ##--------3. Rmd templates sitting in the tool directory ----------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
45
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
46 spec_list$DESEQ_RMD = c('deseq_rmd', 'D', '1', 'character')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
47
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
48
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
49
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
50 ##------------------------------------------------------------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
51
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
52 spec = t(as.data.frame(spec_list))
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
53 opt = getopt(spec)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
54 # arguments are accessed by long flag name (the first column in the spec matrix)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
55 # NOT by element name in the spec_list
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
56 # example: opt$help, opt$expression_file
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
57 ##====== End of arguments handling ==========
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
58
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
59 #------ Load libraries ---------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
60 library(rmarkdown)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
61 library(plyr)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
62 library(stringr)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
63 library(dplyr)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
64 library(highcharter)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
65 library(DT)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
66 library(reshape2)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
67 # library(Kmisc)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
68 library(plotly)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
69 library(formattable)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
70 library(htmltools)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
71
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
72
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
73 #----- 1. create the report directory ------------------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
74 system(paste0('mkdir -p ', opt$deseq_dir))
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
75
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
76
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
77 #----- 2. generate Rmd files with Rmd templates --------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
78 # a. templates without placeholder variables:
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
79 # copy templates from tool directory to the working directory.
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
80 # b. templates with placeholder variables:
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
81 # substitute variables with user input values and place them in the working directory.
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
82
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
83
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
84 #----- 01 DESeq.Rmd -----------------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
85 readLines(opt$deseq_rmd) %>%
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
86 (function(x) {
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
87 gsub('ECHO', opt$echo, x)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
88 }) %>%
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
89 (function(x) {
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
90 gsub('DESEQ_WORKSPACE', opt$deseq_workspace, x)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
91 }) %>%
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
92 (function(x) {
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
93 gsub('DESIGN_FORMULA', opt$design_formula, x)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
94 }) %>%
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
95 (function(x) {
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
96 gsub('OUTPUT_DIR', opt$deseq_dir, x)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
97 }) %>%
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
98 (function(x) {
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
99 fileConn = file('DESeq.Rmd')
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
100 writeLines(x, con=fileConn)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
101 close(fileConn)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
102 })
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
103
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
104
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
105 #------ 3. render all Rmd files --------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
106 render('DESeq.Rmd', output_file = opt$deseq_html)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
107
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
108
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
109 #-------4. manipulate outputs -----------------------------
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
110 # document file
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
111 # file.copy('DESeq.html', opt$deseq_html, recursive = TRUE)
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
112
dfe3cb3f3b39 Uploaded
mingchen0919
parents:
diff changeset
113