Mercurial > repos > mingchen0919 > dynamic_skewer
comparison dynamic_tool.Rmd @ 0:4cd5e5e9b030 draft
planemo upload
| author | mingchen0919 |
|---|---|
| date | Sat, 24 Mar 2018 13:52:24 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4cd5e5e9b030 |
|---|---|
| 1 --- | |
| 2 title: 'Dynamic tool' | |
| 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, echo=FALSE} | |
| 22 ##-------- build script files ----------- | |
| 23 | |
| 24 # build script file: script.sh | |
| 25 df = read.table(paste0(Sys.getenv('REPORT_FILES_PATH'), '/options_and_arguments.txt'), | |
| 26 sep = '|', header = TRUE) | |
| 27 | |
| 28 # prepend A_TOOL_OUTPUT_PATH | |
| 29 if (nrow(df[df$type == 'path_relative_to_a_tool', ]) > 0) { | |
| 30 for (i in 1:nrow(df[df$type == 'path_relative_to_a_tool', ])) { | |
| 31 root_path = readLines(df[df$type == 'path_relative_to_a_tool', ][i, 'path_type'])[1] | |
| 32 df[df$type == 'path_relative_to_a_tool', ][i, 'value'] = paste(root_path, | |
| 33 df[df$type == 'path_relative_to_a_tool', ][i, 'value'], | |
| 34 sep = '/') | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 # get tool name | |
| 39 tool_name = df[df$type == 'tool_name', 'value'] | |
| 40 # build script for displaying help messages (this probably should be tool specific) | |
| 41 write(paste0(tool_name, ' -h'), | |
| 42 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/help.sh')) | |
| 43 | |
| 44 knitr::kable(df) | |
| 45 | |
| 46 # if the number of option/argument pairs is larger than 0, build script file | |
| 47 df2 = df[df$type != 'tool_name', ] | |
| 48 if (nrow(df2) > 0) { | |
| 49 # write tool name as the first line of the script.sh | |
| 50 # before running the job, cd into the ${REPORT_FILES_PATH} directory | |
| 51 write(paste0(tool_name, ' \\'), | |
| 52 paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh')) | |
| 53 df2 = df[df$type != 'tool_name', ] | |
| 54 write(paste(' ', df2$flag, df2$value, '\\', sep = ' '), | |
| 55 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh'), | |
| 56 append = TRUE ) | |
| 57 # remember that after writing option/argument lines to the script.sh, the last line | |
| 58 # has an extra newline character '\' which causes a problem. We can either remove that extra | |
| 59 # '\' or add a new line to the end. We choose to add a new line. | |
| 60 | |
| 61 # add an extra line to the end to redirect stdout to stdout.txt and stderr to stderr.txt | |
| 62 write(' > ${REPORT_FILES_PATH}/stdout.txt 2>${REPORT_FILES_PATH}/stderr.txt', | |
| 63 paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh'), append = TRUE) | |
| 64 } else { | |
| 65 # if no option/argument input, simply display the help message | |
| 66 write(paste0(tool_name, ' -h'), | |
| 67 file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/script.sh')) | |
| 68 } | |
| 69 | |
| 70 ``` | |
| 71 | |
| 72 | |
| 73 ```{bash, echo=FALSE} | |
| 74 ## code to open help documentation | |
| 75 sh ${REPORT_FILES_PATH}/help.sh > ${TOOL_HELP_DOC} | |
| 76 ``` | |
| 77 | |
| 78 | |
| 79 ```{r, echo=FALSE} | |
| 80 # create paths before running the job script | |
| 81 df_paths = df[df$type == 'path_relative_to_this_tool', ] | |
| 82 | |
| 83 if (nrow(df_paths) > 0) { | |
| 84 for (i in 1:nrow(df_paths)) { | |
| 85 path = paste0(Sys.getenv('REPORT_FILES_PATH'), '/', df_paths[i, 'value']) | |
| 86 path_type = df_paths[i, 'path_type'] | |
| 87 | |
| 88 # create file paths | |
| 89 if ((path_type == 'file_path') & !file.exists(path)) { | |
| 90 dir_path = paste(head(strsplit(path, '/')[[1]], -1), collapse = '/' ) | |
| 91 if (!dir.exists(dir_path)) { | |
| 92 dir.create(dir_path, recursive = TRUE) | |
| 93 } | |
| 94 file.create(path) | |
| 95 } | |
| 96 | |
| 97 # create dir paths | |
| 98 if ((path_type == 'dir_path') & !dir.exists(path)) { | |
| 99 dir.create(path, recursive = TRUE) | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 ``` | |
| 104 | |
| 105 | |
| 106 ```{bash, echo=FALSE} | |
| 107 # run job script | |
| 108 # it's important to run the job within the REPORT_FILES_PATH | |
| 109 cd ${REPORT_FILES_PATH} && sh script.sh | |
| 110 ``` | |
| 111 | |
| 112 | |
| 113 | |
| 114 ```{bash, results='asis', echo=FALSE} | |
| 115 echo '## Job script' | |
| 116 echo '' | |
| 117 echo '' | |
| 118 echo '```bash' | |
| 119 cat ${REPORT_FILES_PATH}/script.sh | |
| 120 echo '```' | |
| 121 ``` | |
| 122 | |
| 123 | |
| 124 ```{r, echo=FALSE} | |
| 125 ## obtain REPORT_FILES_PAHT and save it to a galaxy output. | |
| 126 database_root = paste(head(strsplit(Sys.getenv('TOOL_LOG'), '/')[[1]], -1), collapse = '/') | |
| 127 tool_output_dir_id = tail(strsplit(Sys.getenv('REPORT_FILES_PATH'), '/')[[1]], 1) | |
| 128 tool_output_dir = paste0(database_root, '/', tool_output_dir_id) | |
| 129 write(tool_output_dir, Sys.getenv('TOOL_OUTPUT_DIR')) | |
| 130 ``` | |
| 131 | |
| 132 | |
| 133 ```{r, results='asis', echo=FALSE} | |
| 134 cat('##All output files') | |
| 135 cat('\n\n') | |
| 136 all_files = list.files(path = Sys.getenv('REPORT_FILES_PATH'), | |
| 137 full.names = TRUE, | |
| 138 recursive = TRUE) | |
| 139 | |
| 140 for (f in sub(Sys.getenv('REPORT_FILES_PATH'), '.', all_files) ) { | |
| 141 cat('* [', f, '](', f, ')\n') | |
| 142 } | |
| 143 cat('\n') | |
| 144 ``` | |
| 145 |
