comparison wgcna_construct_network_render.R @ 0:4275479ada3a draft

planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_wgcna commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
author mingchen0919
date Tue, 08 Aug 2017 12:35:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4275479ada3a
1 ##======= Handle arguments from command line ========
2 # setup R error handline to go to stderr
3 options(show.error.messages=FALSE,
4 error=function(){
5 cat(geterrmessage(), file=stderr())
6 quit("no", 1, F)
7 })
8
9 # we need that to not crash galaxy with an UTF8 error on German LC settings.
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
11
12 # suppress warning
13 options(warn = -1)
14
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE)
16 args = commandArgs(trailingOnly=TRUE)
17
18 suppressPackageStartupMessages({
19 library(getopt)
20 library(tools)
21 })
22
23 # column 1: the long flag name
24 # column 2: the short flag alias. A SINGLE character string
25 # column 3: argument mask
26 # 0: no argument
27 # 1: argument required
28 # 2: argument is optional
29 # column 4: date type to which the flag's argument shall be cast.
30 # possible values: logical, integer, double, complex, character.
31 spec_list=list()
32
33 ##------- 1. input data ---------------------
34 spec_list$ECHO = c('echo', 'e', '1', 'character')
35 spec_list$PREPROCESSING_WORKSPACE = c('preprocessing_workspace', 'w', '1', 'character')
36 spec_list$HEIGHT_CUT = c('height_cut', 'h', '2', 'double')
37 spec_list$TRAIT_DATA = c('trait_data', 't', '2', 'character')
38
39
40 ##--------2. output report and report site directory --------------
41 spec_list$OUTPUT_HTML = c('wgcna_construct_network_html', 'o', '1', 'character')
42 spec_list$OUTPUT_DIR = c('wgcna_construct_network_dir', 'd', '1', 'character')
43 spec_list$CONSTRUCT_NETWORK_WORKSPACE = c('construct_network_workspace', 'W', '1', 'character')
44
45
46 ##--------3. Rmd templates in the tool directory ----------
47
48 spec_list$WGCNA_PREPROCESSING_RMD = c('wgcna_construct_network_rmd', 'M', '1', 'character')
49
50
51
52 ##------------------------------------------------------------------
53
54 spec = t(as.data.frame(spec_list))
55 opt = getopt(spec)
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(WGCNA)
64 library(DT)
65 library(htmltools)
66 library(ggplot2)
67
68
69 #----- 1. create the report directory ------------------------
70 system(paste0('mkdir -p ', opt$wgcna_construct_network_dir))
71
72
73 #----- 2. generate Rmd files with Rmd templates --------------
74 # a. templates without placeholder variables:
75 # copy templates from tool directory to the working directory.
76 # b. templates with placeholder variables:
77 # substitute variables with user input values and place them in the working directory.
78
79
80 #----- 01 wgcna_construct_network.Rmd -----------------------
81 readLines(opt$wgcna_construct_network_rmd) %>%
82 (function(x) {
83 gsub('ECHO', opt$echo, x)
84 }) %>%
85 (function(x) {
86 gsub('PREPROCESSING_WORKSPACE', opt$preprocessing_workspace, x)
87 }) %>%
88 (function(x) {
89 gsub('HEIGHT_CUT', opt$height_cut, x)
90 }) %>%
91 (function(x) {
92 gsub('TRAIT_DATA', opt$trait_data, x)
93 }) %>%
94 (function(x) {
95 gsub('OUTPUT_DIR', opt$wgcna_construct_network_dir, x)
96 }) %>%
97 (function(x) {
98 gsub('CONSTRUCT_NETWORK_WORKSPACE', opt$construct_network_workspace, x)
99 }) %>%
100 (function(x) {
101 fileConn = file('wgcna_construct_network.Rmd')
102 writeLines(x, con=fileConn)
103 close(fileConn)
104 })
105
106
107 #------ 3. render all Rmd files --------
108 render('wgcna_construct_network.Rmd', output_file = opt$wgcna_construct_network_html)
109
110 #-------4. manipulate outputs -----------------------------
111
112