Mercurial > repos > mingchen0919 > elastic_bowtie2
comparison elastic_tool.Rmd @ 0:b503fbd571a6 draft default tip
planemo upload
author | mingchen0919 |
---|---|
date | Tue, 27 Mar 2018 10:05:29 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b503fbd571a6 |
---|---|
1 --- | |
2 title: 'Tool Report' | |
3 output: html_document | |
4 --- | |
5 | |
6 <style> | |
7 pre code, pre, code { | |
8 white-space: pre !important; | |
9 overflow-x: scroll !important; | |
10 word-break: keep-all !important; | |
11 word-wrap: initial !important; | |
12 } | |
13 </style> | |
14 | |
15 ```{r setup, include=FALSE, warning=FALSE, message=FALSE} | |
16 knitr::opts_chunk$set(error = TRUE) | |
17 ``` | |
18 | |
19 ## User input | |
20 | |
21 ```{r, 'display user input'} | |
22 # get user input and save it into a data frame. | |
23 df = read.table(paste0(Sys.getenv('REPORT_FILES_PATH'), '/options_and_arguments.txt'), | |
24 sep = '|', header = TRUE) | |
25 | |
26 # if the input type is 'path_relative_to_a_tool', prepend A_TOOL_OUTPUT_PATH to the value to make | |
27 # the value a full path. | |
28 if (nrow(df[df$type == 'path_relative_to_a_tool', ]) > 0) { | |
29 for (i in 1:nrow(df[df$type == 'path_relative_to_a_tool', ])) { | |
30 root_path = readLines(df[df$type == 'path_relative_to_a_tool', ][i, 'path_type'])[1] | |
31 df[df$type == 'path_relative_to_a_tool', ][i, 'value'] = paste(root_path, | |
32 df[df$type == 'path_relative_to_a_tool', ][i, 'value'], | |
33 sep = '/') | |
34 } | |
35 } | |
36 | |
37 ## display user input as a table | |
38 knitr::kable(df) | |
39 ``` | |
40 | |
41 | |
42 ```{r, 'build script', echo=FALSE} | |
43 ##-------- build script files ----------- | |
44 | |
45 # get tool name, the first line of the script is always the tool name. | |
46 tool_name = df[df$type == 'tool_name', 'value'] | |
47 | |
48 # if the number of option/argument pairs is larger than 0, build script file | |
49 df2 = df[df$type != 'tool_name', ] | |
50 if (nrow(df2) > 0) { | |
51 # write tool name as the first line of the script.sh | |
52 # before running the job, cd into the ${REPORT_FILES_PATH} directory | |
53 write(paste0(tool_name, ' \\'), | |
54 paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh')) | |
55 df2 = df[df$type != 'tool_name', ] | |
56 write(paste(' ', df2$flag, df2$value, '\\', sep = ' '), | |
57 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh'), | |
58 append = TRUE ) | |
59 # remember that after writing option/argument lines to the script.sh, the last line | |
60 # has an extra newline character '\' which causes a problem. We can either remove that extra | |
61 # '\' or add a new line to the end. We choose to add a new line. | |
62 | |
63 # add an extra line to the end to redirect stdout to stdout.txt and stderr to stderr.txt | |
64 write(' > ${REPORT_FILES_PATH}/stdout.txt 2>${REPORT_FILES_PATH}/stderr.txt', | |
65 paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh'), append = TRUE) | |
66 } else { | |
67 # if no option/argument input, simply display the help message | |
68 write(paste0(tool_name, ' -h'), | |
69 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh')) | |
70 } | |
71 ``` | |
72 | |
73 | |
74 ```{r, 'create paths if they do not exist', echo=FALSE} | |
75 ## if the input type is 'path_relative_to_this_tool', that means | |
76 ## we need to create a directory or file path. | |
77 | |
78 # create paths before running the job script | |
79 df_paths = df[df$type == 'path_relative_to_this_tool', ] | |
80 | |
81 if (nrow(df_paths) > 0) { | |
82 for (i in 1:nrow(df_paths)) { | |
83 path = paste0(Sys.getenv('REPORT_FILES_PATH'), '/', df_paths[i, 'value']) | |
84 path_type = df_paths[i, 'path_type'] | |
85 | |
86 # create file paths | |
87 if ((path_type == 'file_path') & !file.exists(path)) { | |
88 dir_path = paste(head(strsplit(path, '/')[[1]], -1), collapse = '/' ) | |
89 if (!dir.exists(dir_path)) { | |
90 dir.create(dir_path, recursive = TRUE) | |
91 } | |
92 file.create(path) | |
93 } | |
94 | |
95 # create dir paths | |
96 if ((path_type == 'dir_path') & !dir.exists(path)) { | |
97 dir.create(path, recursive = TRUE) | |
98 } | |
99 } | |
100 } | |
101 ``` | |
102 | |
103 | |
104 ```{bash, 'run jobs', echo=FALSE} | |
105 # run job script, always use absolute path. | |
106 # we want to run all jobs within the working path. | |
107 sh ${REPORT_FILES_PATH}/script.sh | |
108 ``` | |
109 | |
110 | |
111 ```{bash, 'display script', results='asis', echo=FALSE} | |
112 echo '## Job script' | |
113 echo '' | |
114 echo '' | |
115 echo '```bash' | |
116 cat ${REPORT_FILES_PATH}/script.sh | |
117 echo '```' | |
118 ``` | |
119 | |
120 | |
121 ```{r, 'display output directory contents', results='asis', echo=FALSE} | |
122 ## after the job is done, we list all files from the output directory. | |
123 ## full relative path to the output directory needs to be displayed. | |
124 | |
125 cat('##All output files') | |
126 cat('\n\n') | |
127 all_files = list.files(path = Sys.getenv('REPORT_FILES_PATH'), | |
128 full.names = TRUE, | |
129 recursive = TRUE) | |
130 | |
131 for (f in sub(Sys.getenv('REPORT_FILES_PATH'), '.', all_files) ) { | |
132 cat('* [', f, '](', f, ')\n') | |
133 } | |
134 cat('\n') | |
135 ``` | |
136 | |
137 | |
138 ```{r, 'save output directory of this tool', echo=FALSE} | |
139 ## each elastic tool has a galaxy history output which contains the REPORT_FILES_PATH of this tool | |
140 ## so that other tools can reference the outputs from this tool. | |
141 | |
142 ## obtain REPORT_FILES_PAHT and save it to a galaxy output. | |
143 database_root = paste(head(strsplit(Sys.getenv('TOOL_LOG'), '/')[[1]], -1), collapse = '/') | |
144 tool_output_dir_id = tail(strsplit(Sys.getenv('REPORT_FILES_PATH'), '/')[[1]], 1) | |
145 tool_output_dir = paste0(database_root, '/', tool_output_dir_id) | |
146 write(tool_output_dir, Sys.getenv('TOOL_OUTPUT_DIR')) | |
147 ``` |