annotate DESeq_visualization_render.R @ 8:3f84334c3de8 draft

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