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