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