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