comparison rmarkdown_report_render.R @ 1:32210899a3dd draft

planemo upload commit 841d8b22bf9f1aaed6bfe8344b60617f45b275b2-dirty
author mingchen0919
date Sun, 30 Dec 2018 12:45:56 -0500
parents
children
comparison
equal deleted inserted replaced
0:6f94b4b9de44 1:32210899a3dd
1 ##============ Sink warnings and errors to a file ==============
2 ## use the sink() function to wrap all code within it.
3 ##==============================================================
4 zz = file(paste0(Sys.getenv('REPORT_FILES_PATH'), '/.r_rendering.log.txt'))
5 sink(zz)
6 sink(zz, type = 'message')
7
8 #============== preparation ====================================
9 # import libraries
10 #------------------------------------------------------------------
11 # ADD MORE LIBRARIES HERE IF YOUR TOOL DEPENDS ON OTHER R LIBRARIES
12 #------------------------------------------------------------------
13 library('getopt')
14 library('rmarkdown')
15 library('htmltools')
16 library('ggplot2')
17 library('plotly')
18 library('DESeq2')
19 library('pheatmap')
20 #------------------------------------------------------------------
21 options(stringsAsFactors = FALSE)
22
23
24 # define two helper functions
25 #-----: helper function 1
26 #' \code{getopt_specification_matrix} returns a getopt specification matrix.
27 #'
28 #' @param specification_file a cvs file within the \code{galaxy_tool_directory} which stores getopt specification matrix data.
29 #' The first column are short flags, the second column are argument masks, the third column
30 #' is data types. The fourth column are variable names used in the tool XML. These three columns are required.
31 #' @param gtg_name the name of a running GTG.
32 getopt_specification_matrix = function(specification_file,
33 gtg_name = 'gtg',
34 tool_dir = Sys.getenv('TOOL_INSTALL_DIR')) {
35 df = read.csv(
36 paste0(tool_dir, '/', specification_file),
37 header = TRUE,
38 stringsAsFactors = FALSE
39 )
40 # check if there are duplicated short flags
41 short_flags = df[, 1]
42 if (length(unique(short_flags)) < length(short_flags)) {
43 cat('----Duplicated short flags found ----\n')
44 cat('short flags: ', df[, 1][duplicated(df[, 1])], '\n')
45 stop('Duplicated short flags are not allowed.')
46 }
47
48 # use short flags to generate long flags
49 long_flags = paste0('X_', df[, 1])
50
51 # specification matrix
52 df2 = data.frame(
53 long_flags = long_flags,
54 short_flags = df[, 1],
55 argument_mask = df[, 2],
56 data_type = df[, 3]
57 )
58
59 as.matrix(df2)
60 }
61
62 #-----: helper function 2
63 #' \code{file_tree} generate file tree of a directory in the format of HTML lists.
64 #'
65 #' @param dir the path to the directory for generating the file tree.
66 #' @param output_dir the REPORT_FILES_PATH folder name, which has the name style: dataset_NUMBER_files.
67 # define a recursive function to build html string of the file tree
68 file_tree = function(dir = '.') {
69 # get the OUTPUT_DIR folder data: dataset_NUMBER_files
70 report_files_path = Sys.getenv('REPORT_FILES_PATH')
71 output_dir = tail(strsplit(report_files_path, '/')[[1]], 1)
72
73 files = list.files(path = dir,
74 recursive = FALSE,
75 full.names = TRUE)
76 # files also include directorys, need to remove directorys
77 files = files[!dir.exists(files)]
78 dirs = list.dirs(path = dir,
79 recursive = FALSE,
80 full.names = TRUE)
81 tags$ul({
82 if (length(files) > 0) {
83 lapply(files, function(x) {
84 path_end = tail(strsplit(x, '/')[[1]], 1)
85 href_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
86 li_item = tags$li(tags$a(path_end, href = href_path))
87 li_item$attribs = list('data-jstree' = '{"icon":"jstree-file"}')
88 li_item
89 })
90 }
91 },
92 {
93 if (length(dirs) > 0) {
94 lapply(dirs, function(x) {
95 path_end = tail(strsplit(x, '/')[[1]], 1)
96 # hide vakata-jstree-3.3.5 folder
97 if (!(path_end %in% c('vakata-jstree-3.3.5', 'rmarkdown_report_files', 'site_libs'))) {
98 # x_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
99 li_item = tags$li(path_end, file_tree(x))
100 li_item$attribs = list('data-jstree' = '{"icon":"jstree-folder"}')
101 li_item
102 }
103 })
104 }
105 })
106 }
107 #----------------- end of help functions -------------------------
108
109
110 # import getopt specification matrix from a csv file
111 opt = getopt(getopt_specification_matrix('command-line-arguments.csv',
112 tool_dir = Sys.getenv('TOOL_INSTALL_DIR')))
113 # define environment variables for all input values. this is useful when we
114 # want to use input values by other programming language in r markdown
115 do.call(Sys.setenv, opt[-1])
116 #===============================================================
117
118
119 #======================== render Rmd files =========================
120 # copy jstree javascript library to tool output directory
121 file.copy(
122 from = paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/vakata-jstree-3.3.5'),
123 to = Sys.getenv('REPORT_FILES_PATH'),
124 recursive = TRUE
125 )
126
127 # if '_site.yml' file exists, this tool is assumed to render a website.
128 # otherwise, it renders a single html.
129 if (file.exists(paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/_site.yml'))) {
130 # render a website
131 system(command = 'cp -r ${TOOL_INSTALL_DIR}/*.Rmd ${REPORT_FILES_PATH}')
132 system(command = 'cp -r ${TOOL_INSTALL_DIR}/_site.yml ${REPORT_FILES_PATH}')
133 render_site(input = Sys.getenv('REPORT_FILES_PATH'))
134 } else {
135 # render a single html
136 system(command = 'cp -r ${TOOL_INSTALL_DIR}/rmarkdown_report.Rmd ${REPORT_FILES_PATH}')
137 # add a few lines to 'rmarkdown_report.Rmd' to generate file tree outputs
138 jstree_lines = '
139 ## Outputs
140
141 ```{r, echo=FALSE}
142 tags$div(id="jstree", file_tree(Sys.getenv(\'REPORT_FILES_PATH\')))
143 ```'
144 write(
145 x = jstree_lines,
146 append = TRUE,
147 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd')
148 )
149 render(input = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd'))
150 }
151 #===============================================================
152
153
154 #============== expose outputs to galaxy history ===============
155 system(command = 'sh ${TOOL_INSTALL_DIR}/expose-outputs-to-galaxy-history.sh')
156 #===============================================================
157
158
159 ##--------end of code rendering .Rmd templates----------------
160 sink()
161 ##=========== End of sinking output=============================