Skip to content

Update optional-input.nf #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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