Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions docs/optional-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,31 @@ One or more processes have an optional input file.

## Solution

Use a special file name to mark the absence of the file parameter.

Create an empty file in `assets`:
```
touch assets/NO_FILE
```
Pass an empty array to the optional input. The empty array will be evaluated to `false`.

## Code

```groovy
params.inputs = "$projectDir/data/prots/*{1,2,3}.fa"
params.filter = "$projectDir/assets/NO_FILE"
// pass a empty array. this evaluates to false
params.filter = []

process foo {
debug true
debug true
input:
path seq
path opt

script:
def filter = opt.name != 'NO_FILE' ? "--filter $opt" : ''
def filter = opt ? "--filter ${opt}" : ""
"""
echo your_command --input $seq $filter
"""
}

workflow {
prots_ch = Channel.fromPath(params.inputs, checkIfExists:true)
opt_file = file(params.filter, checkIfExists:true)
opt_file = file(params.filter)

foo(prots_ch, opt_file)
}
Expand Down
5 changes: 3 additions & 2 deletions optional-input.nf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*/

params.inputs = "$projectDir/data/prots/*{1,2,3}.fa"
params.filter = "$projectDir/assets/NO_FILE"
// pass a empty array. this evaluates to false
params.filter = []

process foo {
debug true
Expand All @@ -36,7 +37,7 @@ process foo {
path opt

script:
def filter = opt.name != 'NO_FILE' ? "--filter $opt" : ''
def filter = opt ? "--filter ${opt}" : ""
"""
echo your_command --input $seq $filter
"""
Expand Down