Skip to content

Commit bd99bc9

Browse files
committed
Wip refactor nextflow runner
1 parent ee134ea commit bd99bc9

1 file changed

Lines changed: 50 additions & 37 deletions

File tree

src/main/resources/io/viash/runners/nextflow/VDSL3Helper.nf

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,20 @@ def _vdsl3ProcessFactory(Map workflowArgs, Map meta, String rawScript) {
204204

205205
// create dirs for output files (based on BashWrapper.createParentFiles)
206206
def createParentStr = meta.config.allArguments
207-
.findAll { it.type == "file" && it.direction == "output" && it.create_parent }
207+
.findAll { par -> par.type == "file" && par.direction == "output" && par.create_parent }
208208
.collect { par ->
209209
def contents = "args[\"${par.plainName}\"] instanceof List ? args[\"${par.plainName}\"].join('\" \"') : args[\"${par.plainName}\"]"
210210
"\${ args.containsKey(\"${par.plainName}\") ? \"mkdir_parent '\" + escapeText(${contents}) + \"'\" : \"\" }"
211211
}
212212
.join("\n")
213213

214-
// construct inputFileExports
215-
def inputFileExports = meta.config.allArguments
216-
.findAll { it.type == "file" && it.direction.toLowerCase() == "input" }
214+
// construct input file additions to viashPar (convert Path objects to strings, keep lists as arrays)
215+
def inputFileAdditions = meta.config.allArguments
216+
.findAll { par -> par.type == "file" && par.direction.toLowerCase() == "input" }
217217
.collect { par ->
218-
def contents = "viash_par_${par.plainName} instanceof List ? viash_par_${par.plainName}.join(\"${par.multiple_sep}\") : viash_par_${par.plainName}"
219-
"\n\${viash_par_${par.plainName}.empty ? \"\" : \"export VIASH_PAR_${par.plainName.toUpperCase()}='\" + escapeText(${contents}) + \"'\"}"
218+
def varName = "viash_par_${par.plainName}"
219+
// Convert Path objects to strings, keep lists as arrays
220+
"\n |if (!${varName}.empty) { viashPar[\"${par.plainName}\"] = ${varName} instanceof List ? ${varName}.collect{it.toString()} : ${varName}.toString() }"
220221
}
221222

222223
// NOTE: if using docker, use /tmp instead of tmpDir!
@@ -253,6 +254,7 @@ def _vdsl3ProcessFactory(Map workflowArgs, Map meta, String rawScript) {
253254
def procStr =
254255
"""nextflow.enable.dsl=2
255256
|
257+
|import groovy.json.JsonOutput
256258
|def escapeText = { s -> s.toString().replaceAll("'", "'\\\"'\\\"'") }
257259
|process $procKey {$drctvStrs
258260
|input:
@@ -265,36 +267,39 @@ def _vdsl3ProcessFactory(Map workflowArgs, Map meta, String rawScript) {
265267
|$stub
266268
|\"\"\"
267269
|script:$assertStr
268-
|def parInject = args
269-
| .findAll{key, value -> value != null}
270-
| .collect{key, value -> "export VIASH_PAR_\${key.toUpperCase()}='\${escapeText(value)}'"}
271-
| .join("\\n")
272-
|\"\"\"
273-
|# meta exports
274-
|export VIASH_META_RESOURCES_DIR="\${resourcesDir}"
275-
|export VIASH_META_TEMP_DIR="${['docker', 'podman', 'charliecloud'].any{ it == workflow.containerEngine } ? '/tmp' : tmpDir}"
276-
|export VIASH_META_NAME="${meta.config.name}"
277-
|# export VIASH_META_EXECUTABLE="\\\$VIASH_META_RESOURCES_DIR/\\\$VIASH_META_NAME"
278-
|export VIASH_META_CONFIG="\\\$VIASH_META_RESOURCES_DIR/.config.vsh.yaml"
279-
|\${task.cpus ? "export VIASH_META_CPUS=\$task.cpus" : "" }
280-
|\${task.memory?.bytes != null ? "export VIASH_META_MEMORY_B=\$task.memory.bytes" : "" }
281-
|if [ ! -z \\\${VIASH_META_MEMORY_B+x} ]; then
282-
| export VIASH_META_MEMORY_KB=\\\$(( (\\\$VIASH_META_MEMORY_B+999) / 1000 ))
283-
| export VIASH_META_MEMORY_MB=\\\$(( (\\\$VIASH_META_MEMORY_KB+999) / 1000 ))
284-
| export VIASH_META_MEMORY_GB=\\\$(( (\\\$VIASH_META_MEMORY_MB+999) / 1000 ))
285-
| export VIASH_META_MEMORY_TB=\\\$(( (\\\$VIASH_META_MEMORY_GB+999) / 1000 ))
286-
| export VIASH_META_MEMORY_PB=\\\$(( (\\\$VIASH_META_MEMORY_TB+999) / 1000 ))
287-
| export VIASH_META_MEMORY_KIB=\\\$(( (\\\$VIASH_META_MEMORY_B+1023) / 1024 ))
288-
| export VIASH_META_MEMORY_MIB=\\\$(( (\\\$VIASH_META_MEMORY_KIB+1023) / 1024 ))
289-
| export VIASH_META_MEMORY_GIB=\\\$(( (\\\$VIASH_META_MEMORY_MIB+1023) / 1024 ))
290-
| export VIASH_META_MEMORY_TIB=\\\$(( (\\\$VIASH_META_MEMORY_GIB+1023) / 1024 ))
291-
| export VIASH_META_MEMORY_PIB=\\\$(( (\\\$VIASH_META_MEMORY_TIB+1023) / 1024 ))
292-
|fi
293-
|
294-
|# meta synonyms
295-
|export VIASH_TEMP="\\\$VIASH_META_TEMP_DIR"
296-
|export TEMP_DIR="\\\$VIASH_META_TEMP_DIR"
270+
|// Construct meta map
271+
|def viashMeta = [
272+
| "resources_dir": "\${resourcesDir}",
273+
| "temp_dir": "${['docker', 'podman', 'charliecloud'].any{ it == workflow.containerEngine } ? '/tmp' : tmpDir}",
274+
| "name": "${meta.config.name}",
275+
| "config": "\${resourcesDir}/.config.vsh.yaml"
276+
|]
277+
|if (task.cpus) { viashMeta["cpus"] = task.cpus }
278+
|if (task.memory?.bytes != null) {
279+
| def memB = task.memory.bytes
280+
| viashMeta["memory_b"] = memB
281+
| viashMeta["memory_kb"] = (long)((memB + 999) / 1000)
282+
| viashMeta["memory_mb"] = (long)((viashMeta["memory_kb"] + 999) / 1000)
283+
| viashMeta["memory_gb"] = (long)((viashMeta["memory_mb"] + 999) / 1000)
284+
| viashMeta["memory_tb"] = (long)((viashMeta["memory_gb"] + 999) / 1000)
285+
| viashMeta["memory_pb"] = (long)((viashMeta["memory_tb"] + 999) / 1000)
286+
| viashMeta["memory_kib"] = (long)((memB + 1023) / 1024)
287+
| viashMeta["memory_mib"] = (long)((viashMeta["memory_kib"] + 1023) / 1024)
288+
| viashMeta["memory_gib"] = (long)((viashMeta["memory_mib"] + 1023) / 1024)
289+
| viashMeta["memory_tib"] = (long)((viashMeta["memory_gib"] + 1023) / 1024)
290+
| viashMeta["memory_pib"] = (long)((viashMeta["memory_tib"] + 1023) / 1024)
291+
|}
292+
|// Define args
293+
|def viashPar = args + [:]${inputFileAdditions.join()}
297294
|
295+
|// Construct full params object
296+
|def viashParams = [
297+
| "par": viashPar,
298+
| "meta": viashMeta,
299+
| "dep": [:]
300+
|]
301+
|def paramsJson = JsonOutput.prettyPrint(JsonOutput.toJson(viashParams))
302+
|\"\"\"
298303
|# create output dirs if need be
299304
|function mkdir_parent {
300305
| for file in "\\\$@"; do
@@ -303,8 +308,16 @@ def _vdsl3ProcessFactory(Map workflowArgs, Map meta, String rawScript) {
303308
|}
304309
|$createParentStr
305310
|
306-
|# argument exports${inputFileExports.join()}
307-
|\$parInject
311+
|# Write params.json file
312+
|cat > .viash_params.json << 'VIASH_PARAMS_JSON'
313+
|\$paramsJson
314+
|VIASH_PARAMS_JSON
315+
|export VIASH_WORK_PARAMS=".viash_params.json"
316+
|
317+
|# Also export VIASH_META_TEMP_DIR for backwards compatibility
318+
|export VIASH_META_TEMP_DIR="${['docker', 'podman', 'charliecloud'].any{ it == workflow.containerEngine } ? '/tmp' : tmpDir}"
319+
|export VIASH_TEMP="\\\$VIASH_META_TEMP_DIR"
320+
|export TEMP_DIR="\\\$VIASH_META_TEMP_DIR"
308321
|
309322
|# process script
310323
|${escapedScript}

0 commit comments

Comments
 (0)