annotate elastic_tool.Rmd @ 1:58c172729fd3 draft default tip

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