comparison DESeq_results_render.R @ 6:2f8ddef8d545 draft

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