Here is what I came with, which gives the expected behavior:
// Parameters default values
params.optionalFiles=false
// Channel creation
optionalFiles=Channel.empty()
// Fill channel with file(s) (exits if file does not exist)
if(params.optionalFiles){
optionalFiles=Channel.from(params.optionalFiles.tokenize()).flatMap{if(!(f=file(it)).exists())
exit(1, "Parameter 'optionalFiles': file '${f.getName()}' does not
exist"); return(f)}
}
process P01_test {
input:
file fileToProcess from optionalFiles.ifEmpty{'any invalid file ID'}.collect() // replace by any integer value ?
output:
stdout result
script:
if(fileToProcess.every{it.exists()}){
"""
echo 'Starting process using file(s) : ${fileToProcess.each{it.getName()}.join(' -&- ')}'
"""
} else {
"""
echo 'Starting process without reference file'
"""
}
}
result.subscribe{ println(it) }
Supposing 'test.txt' and '
test.nf' files exist in the directory:
>nextflow myScript.nf --optionalFiles 'test.txt
test.nf'
[56/537220] Submitted process > P01_test
Starting process using file(s) : test.txt -&-
test.nf>nextflow myScript.nf --optionalFiles 'test.txt lala.txt'
ERROR ~ Parameter 'optionalFiles': file 'lala.txt' does not exist
>nextflow myScript.nf
Starting process without reference file
This
is the best I could think of with my limited knowledge but I'm sure
there is room for optimization and a cleaner implementation.
Could
you please comment anything that could be made better on this piece of
code (specially the file handling mechanism) ? I am still learning and
any comment is appreciated :)
I feel that the "ifEmpty" in input channel is too indirect approach...
Thank you !
Romain.