annotate DESeq_results_render.R @ 7:a5fdd120b2c7 draft

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