annotate elastic_tool.Rmd @ 0:869ad6fe76d0 draft default tip

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