comparison DESeq_render.R @ 6:2f8ddef8d545 draft

update deseq2
author mingchen0919
date Tue, 07 Nov 2017 13:50:32 -0500
parents 7231d7e8d3ed
children
comparison
equal deleted inserted replaced
5:fd3514267506 6:2f8ddef8d545
1 ##======= Handle arguments from command line ======== 1 library(getopt)
2 # setup R error handline to go to stderr 2 library(rmarkdown)
3 options(show.error.messages=FALSE, 3 library(htmltools)
4 error=function(){ 4 library(dplyr)
5 cat(geterrmessage(), file=stderr()) 5 library(stringi)
6 quit("no", 1, F) 6 library(DESeq2)
7 }) 7 library(pheatmap)
8 library(RColorBrewer)
9 library(DT)
8 10
9 # we need that to not crash galaxy with an UTF8 error on German LC settings. 11 ##============ Sink warnings and errors to a file ==============
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") 12 ## use the sink() function to wrap all code within it.
11 13 ##==============================================================
12 # suppress warning 14 zz = file('warnings_and_errors.txt')
13 options(warn = -1) 15 sink(zz)
14 16 sink(zz, type = 'message')
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE) 17 ##---------below is the code for rendering .Rmd templates-----
16 args = commandArgs(trailingOnly=TRUE) 18
17 19 ##=============STEP 1: handle command line arguments==========
18 suppressPackageStartupMessages({ 20 ##
19 library(getopt) 21 ##============================================================
20 library(tools) 22 # column 1: the long flag name
21 }) 23 # column 2: the short flag alias. A SINGLE character string
22 24 # column 3: argument mask
23 # column 1: the long flag name 25 # 0: no argument
24 # column 2: the short flag alias. A SINGLE character string 26 # 1: argument required
25 # column 3: argument mask 27 # 2: argument is optional
26 # 0: no argument 28 # column 4: date type to which the flag's argument shall be cast.
27 # 1: argument required 29 # possible values: logical, integer, double, complex, character.
28 # 2: argument is optional 30 #-------------------------------------------------------------
29 # column 4: date type to which the flag's argument shall be cast. 31 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++
30 # possible values: logical, integer, double, complex, character. 32 # 1. short flag alias should match the flag in the command section in the XML file.
31 spec_list=list() 33 # 2. long flag name can be any legal R variable names
32 34 # 3. two names in args_list can have common string but one name should not be a part of another name.
33 ##------- 1. input data --------------------- 35 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems.
34 spec_list$COUNT_FILES = c('count_files', 'c', '1', 'character') 36 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
35 spec_list$ECHO = c('echo', 'e', '1', 'character') 37 args_list=list()
36 spec_list$SAMPLE_TABLE = c('sample_table', 's', '1', 'character') 38 ##------- 1. input data ---------------------
37 spec_list$DESIGN_FORMULA = c('design_formula', 'p', '1', 'character') 39 args_list$ECHO = c('echo', 'e', '1', 'character')
38 40 args_list$COUNT_FILE_PATHS = c('count_file_paths', 'P', '1', 'character')
39 ##--------2. output report and report site directory -------------- 41 args_list$COUNT_FILE_NAMES = c('count_file_names', 'N', '1', 'character')
40 spec_list$OUTPUT_HTML = c('deseq_html', 'o', '1', 'character') 42 args_list$SAMPLE_TABLE = c('sample_table', 'S', '1', 'character')
41 spec_list$OUTPUT_DIR = c('deseq_dir', 'd', '1', 'character') 43 args_list$DESIGN_FORMULA = c('design_formula', 'p', '1', 'character')
42 spec_list$WORKSPACE = c('deseq_workspace', 'w', '1', 'character') 44 ##--------2. output report and outputs --------------
43 45 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
44 ##--------3. Rmd templates sitting in the tool directory ---------- 46 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
45 47 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
46 spec_list$DESEQ_RMD = c('deseq_rmd', 'D', '1', 'character') 48 args_list$WORKSPACE = c('deseq_workspace', 'w', '1', 'character')
49 ##--------3. .Rmd templates in the tool directory ----------
50 args_list$deseq_RMD = c('deseq_rmd', 't', '1', 'character')
51 ##-----------------------------------------------------------
52 opt = getopt(t(as.data.frame(args_list)))
47 53
48 54
49 55
50 ##------------------------------------------------------------------ 56 ##=======STEP 2: create report directory (optional)==========
51 57 ##
52 spec = t(as.data.frame(spec_list)) 58 ##===========================================================
53 opt = getopt(spec) 59 dir.create(opt$report_dir)
54 # arguments are accessed by long flag name (the first column in the spec matrix) 60
55 # NOT by element name in the spec_list 61 ##=STEP 3: replace placeholders in .Rmd with argument values=
56 # example: opt$help, opt$expression_file 62 ##
57 ##====== End of arguments handling ========== 63 ##===========================================================
58 64 #++ need to replace placeholders with args values one by one+
59 #------ Load libraries --------- 65 readLines(opt$deseq_rmd) %>%
60 library(rmarkdown)
61 library(plyr)
62 library(stringr)
63 library(dplyr)
64 library(highcharter)
65 library(DT)
66 library(reshape2)
67 # library(Kmisc)
68 library(plotly)
69 library(formattable)
70 library(htmltools)
71
72
73 #----- 1. create the report directory ------------------------
74 system(paste0('mkdir -p ', opt$deseq_dir))
75
76
77 #----- 2. generate Rmd files with Rmd templates --------------
78 # a. templates without placeholder variables:
79 # copy templates from tool directory to the working directory.
80 # b. templates with placeholder variables:
81 # substitute variables with user input values and place them in the working directory.
82
83
84 #----- 01 DESeq.Rmd -----------------------
85 readLines(opt$deseq_rmd) %>%
86 (function(x) { 66 (function(x) {
87 gsub('ECHO', opt$echo, x) 67 gsub('ECHO', opt$echo, x)
88 }) %>% 68 }) %>%
89 (function(x) { 69 (function(x) {
90 gsub('DESEQ_WORKSPACE', opt$deseq_workspace, x) 70 gsub('DESEQ_WORKSPACE', opt$deseq_workspace, x)
91 }) %>% 71 }) %>%
92 (function(x) { 72 (function(x) {
93 gsub('DESIGN_FORMULA', opt$design_formula, x) 73 gsub('DESIGN_FORMULA', opt$design_formula, x)
94 }) %>% 74 }) %>%
95 (function(x) { 75 (function(x) {
96 gsub('OUTPUT_DIR', opt$deseq_dir, x) 76 gsub('REPORT_DIR', opt$report_dir, x)
97 }) %>% 77 }) %>%
98 (function(x) { 78 (function(x) {
99 fileConn = file('DESeq.Rmd') 79 fileConn = file('deseq.Rmd')
100 writeLines(x, con=fileConn) 80 writeLines(x, con=fileConn)
101 close(fileConn) 81 close(fileConn)
102 }) 82 })
83
84
85 ##=============STEP 4: render .Rmd templates=================
86 ##
87 ##===========================================================
88 render('deseq.Rmd', output_file = opt$report_html)
103 89
104 90
105 #------ 3. render all Rmd files -------- 91 ##--------end of code rendering .Rmd templates----------------
106 render('DESeq.Rmd', output_file = opt$deseq_html) 92 sink()
107 93 ##=========== End of sinking output=============================
108
109 #-------4. manipulate outputs -----------------------------
110 # document file
111 # file.copy('DESeq.html', opt$deseq_html, recursive = TRUE)
112
113