-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextflowTool.groovy
251 lines (223 loc) · 11.5 KB
/
NextflowTool.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// This file holds several functions used to perform JSON parameter validation, help and summary rendering.
//
// Modified from NF-Core's template: https://github.com/nf-core/tools
import org.yaml.snakeyaml.Yaml
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import nextflow.extension.FilesEx
class NextflowTool {
//
// Dump pipeline parameters in a json file
//
public static void dump_parameters(workflow, params) {
def timestamp = new java.util.Date().format( 'yyyy-MM-dd_HH-mm-ss')
def filename = "params_${timestamp}.json"
def temp_pf = new File(workflow.launchDir.toString(), ".${filename}")
def jsonStr = JsonOutput.toJson(params)
temp_pf.text = JsonOutput.prettyPrint(jsonStr)
FilesEx.copyTo(temp_pf.toPath(), "${params.results_dir}/pipeline_info/params_${timestamp}.json")
temp_pf.delete()
}
public static void help_message(pipeline_schema, schema_path_list, monochrome_logs, log) {
Map colors = logColours(monochrome_logs)
def indent = " "
//master schema is pipeline specific it only contains what needs to be there other stuff can be imported from the path_list
//master schema contains a overwrite section which is not printed and instead used to overwrite defaults or whatever in the
//general imported schema
def master_in = new File(pipeline_schema).text
def master_schema = new JsonSlurper().parseText(master_in)
for (schema_path in schema_path_list) {
def schema_in = new File(schema_path).text
def json = new JsonSlurper().parseText(schema_in)
//create a list of keys you want to supress in the json
def banned_keylist = master_schema.overwrite_param.keySet() as Set
json.params.each{
if (!banned_keylist.contains(it.key)) {
log.info "${colors.purple} ${it.key} ${colors.reset}"
it.value.each {
if (it.key in master_schema.overwrite_param) {
// A groovy json path cannot be queried using a variable unless you force it using eval
// set up the query and then use eval.x to append the search term onto the end of the json object used to overwrite
// this then replaces the default value with the input
def overwrite_path = 'overwrite_param.' + it.key
def overwrite_param = Eval.x( master_schema, 'x.' + overwrite_path)
if (overwrite_param.help_text != "") {
log.info indent + "--" + it.key
log.info indent + indent + "default: " + overwrite_param.default
log.info indent + indent + overwrite_param.help_text
}
} else {
if (it.key.toString().contains('header')) {
if (it.value.title){
log.info indent
log.info "${colors.red} ${it.value.title} ${colors.reset}"
}
log.info indent + it.value.subtext
log.info indent
} else {
//if nothing needs to be overwritten just print what is there
log.info indent + "--" + it.key
log.info indent + indent + "default: " + it.value.default
log.info indent + indent + it.value.help_text
}
}
}
//put a line to seperate
log.info dashedLine(monochrome_logs)
}
}
}
//finally print the params in the master manifest
master_schema.params.each {
log.info "${colors.purple} ${it.key} ${colors.reset}"
it.value.each {
if (it.key.toString().contains('header')) {
if (it.value.title){
log.info indent
log.info "${colors.red} ${it.value.title} ${colors.reset}"
}
log.info indent + it.value.subtext
log.info indent
} else {
log.info indent + "--" + it.key
log.info indent + indent + "default: " + it.value.default
log.info indent + indent + it.value.help_text
log.info indent
}
}
//put a line to seperate
log.info dashedLine(monochrome_logs)
}
}
//
// Print pipeline summary on completion
//
public static void summary(workflow, params, log) {
Map colors = logColours(params.monochrome_logs)
if (workflow.success) {
if (workflow.stats.ignoredCount == 0) {
log.info "-${colors.purple}[$workflow.manifest.name]${colors.green} Pipeline completed successfully${colors.reset}-"
} else {
log.info "-${colors.purple}[$workflow.manifest.name]${colors.yellow} Pipeline completed successfully, but with errored process(es) ${colors.reset}-"
}
} else {
log.info "-${colors.purple}[$workflow.manifest.name]${colors.red} Pipeline completed with errors${colors.reset}-"
}
}
//
// ANSII Colours used for terminal logging
//
public static Map logColours(Boolean monochrome_logs) {
Map colorcodes = [:]
// Reset / Meta
colorcodes['reset'] = monochrome_logs ? '' : "\033[0m"
colorcodes['bold'] = monochrome_logs ? '' : "\033[1m"
colorcodes['dim'] = monochrome_logs ? '' : "\033[2m"
colorcodes['underlined'] = monochrome_logs ? '' : "\033[4m"
colorcodes['blink'] = monochrome_logs ? '' : "\033[5m"
colorcodes['reverse'] = monochrome_logs ? '' : "\033[7m"
colorcodes['hidden'] = monochrome_logs ? '' : "\033[8m"
// Regular Colors
colorcodes['black'] = monochrome_logs ? '' : "\033[0;30m"
colorcodes['red'] = monochrome_logs ? '' : "\033[0;31m"
colorcodes['green'] = monochrome_logs ? '' : "\033[0;32m"
colorcodes['yellow'] = monochrome_logs ? '' : "\033[0;33m"
colorcodes['blue'] = monochrome_logs ? '' : "\033[0;34m"
colorcodes['purple'] = monochrome_logs ? '' : "\033[0;35m"
colorcodes['cyan'] = monochrome_logs ? '' : "\033[0;36m"
colorcodes['white'] = monochrome_logs ? '' : "\033[0;37m"
// Bold
colorcodes['bblack'] = monochrome_logs ? '' : "\033[1;30m"
colorcodes['bred'] = monochrome_logs ? '' : "\033[1;31m"
colorcodes['bgreen'] = monochrome_logs ? '' : "\033[1;32m"
colorcodes['byellow'] = monochrome_logs ? '' : "\033[1;33m"
colorcodes['bblue'] = monochrome_logs ? '' : "\033[1;34m"
colorcodes['bpurple'] = monochrome_logs ? '' : "\033[1;35m"
colorcodes['bcyan'] = monochrome_logs ? '' : "\033[1;36m"
colorcodes['bwhite'] = monochrome_logs ? '' : "\033[1;37m"
// Underline
colorcodes['ublack'] = monochrome_logs ? '' : "\033[4;30m"
colorcodes['ured'] = monochrome_logs ? '' : "\033[4;31m"
colorcodes['ugreen'] = monochrome_logs ? '' : "\033[4;32m"
colorcodes['uyellow'] = monochrome_logs ? '' : "\033[4;33m"
colorcodes['ublue'] = monochrome_logs ? '' : "\033[4;34m"
colorcodes['upurple'] = monochrome_logs ? '' : "\033[4;35m"
colorcodes['ucyan'] = monochrome_logs ? '' : "\033[4;36m"
colorcodes['uwhite'] = monochrome_logs ? '' : "\033[4;37m"
// High Intensity
colorcodes['iblack'] = monochrome_logs ? '' : "\033[0;90m"
colorcodes['ired'] = monochrome_logs ? '' : "\033[0;91m"
colorcodes['igreen'] = monochrome_logs ? '' : "\033[0;92m"
colorcodes['iyellow'] = monochrome_logs ? '' : "\033[0;93m"
colorcodes['iblue'] = monochrome_logs ? '' : "\033[0;94m"
colorcodes['ipurple'] = monochrome_logs ? '' : "\033[0;95m"
colorcodes['icyan'] = monochrome_logs ? '' : "\033[0;96m"
colorcodes['iwhite'] = monochrome_logs ? '' : "\033[0;97m"
// Bold High Intensity
colorcodes['biblack'] = monochrome_logs ? '' : "\033[1;90m"
colorcodes['bired'] = monochrome_logs ? '' : "\033[1;91m"
colorcodes['bigreen'] = monochrome_logs ? '' : "\033[1;92m"
colorcodes['biyellow'] = monochrome_logs ? '' : "\033[1;93m"
colorcodes['biblue'] = monochrome_logs ? '' : "\033[1;94m"
colorcodes['bipurple'] = monochrome_logs ? '' : "\033[1;95m"
colorcodes['bicyan'] = monochrome_logs ? '' : "\033[1;96m"
colorcodes['biwhite'] = monochrome_logs ? '' : "\033[1;97m"
return colorcodes
}
//
// Does what is says on the tin
//
public static String dashedLine(monochrome_logs) {
Map colors = logColours(monochrome_logs)
return "-" + "${colors.dim}-${colors.reset}"*63 + "-"
}
//
// pam-info logo
//
public static String logo(workflow, monochrome_logs) {
Map colors = logColours(monochrome_logs)
String.format(
"""\n
${dashedLine(monochrome_logs)}
${colors.blue} _____________________ ___ ____________ _________________ ${colors.reset}
${colors.blue} ___ __ \\__ |__ |/ / ____ _/__ | / /__ ____/_ __ \\ ${colors.reset}
${colors.blue} __ /_/ /_ /| |_ /|_/ /_________ / __ |/ /__ /_ _ / / / ${colors.reset}
${colors.blue} _ ____/_ ___ | / / /_/_____/_/ / _ /| / _ __/ / /_/ / ${colors.reset}
${colors.blue} /_/ /_/ |_/_/ /_/ /___/ /_/ |_/ /_/ \\____/ ${colors.reset}
\n
${colors.purple} ${workflow.manifest.name} ${workflow.manifest.version} ${colors.reset}
${dashedLine(monochrome_logs)}
""".stripIndent()
)
}
public static Map getCommandLineParams(String commandLine) {
def commandLineTokens = commandLine.tokenize('--')
def commandLineParams = [:]
commandLineTokens[1..-1].each { cmd ->
def keyVal = cmd.trim().split(' ', 2)
if (keyVal.size() >= 1 && keyVal[0]) {
def key = keyVal[0].trim()
def value = keyVal.size() == 2 ? keyVal[1].trim() : null
commandLineParams[key] = value ?: true
}
}
return commandLineParams
}
public static void commandLineParams(String commandLine, log, monochrome_logs) {
def indent = " "
Map colors = logColours(monochrome_logs)
def commandLineParams = getCommandLineParams(commandLine)
if (commandLineParams.isEmpty()) {
log.info "${colors.purple} No parameters supplied: running with defaults. ${colors.reset}"
} else {
log.info "${colors.purple} The following parameters were provided on the command line: ${colors.reset}"
log.info indent
commandLineParams.each { key, value ->
log.info indent + "- ${key}: ${value}"
}
}
log.info indent //whitespace after params
}
}