These are chat archives for nextflow-io/nextflow

28th
Apr 2016
Evan Floden
@evanfloden
Apr 28 2016 08:31

How is the best way to handle boolean parameters at the command line in NF?

For example, I want a parameter to be given to a program only if it is specified as a NF parameter (default is that it is not given to the program)

I was thinking something like this:

params.expr    =    FASLE

log.info "calculate expression (cRPKMs)  : ${params.expr}"

expression_flag = ${params.expr}
if ( expresssion_flag ) { 
    expr = '--expr' 
} 
else {
    expr = ''
}

prcocess xyz {
    script:
        someprogram --reads ${reads}  ${expr}
}
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:32
Boolean values are true and false
then conditional expression operator (like in C) can be useful, for example:
expr = expression_flag ? '--expr' : ''
or even better
Evan Floden
@evanfloden
Apr 28 2016 08:34
Nice, much better!
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:34
expr = params.expr ? '--expr' : ''
Evan Floden
@evanfloden
Apr 28 2016 08:34
Wow!
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:35
or even better
params.expr    =    false

log.info "calculate expression (cRPKMs)  : ${params.expr}"


prcocess xyz {
    script:
        someprogram --reads ${reads}  ${params.expr ? '--expr' : ''}
}
Evan Floden
@evanfloden
Apr 28 2016 08:36
great! So if the parameter ' --expr' is given with the NF command, params.expr with be true?
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:36
yes
Evan Floden
@evanfloden
Apr 28 2016 08:36
:thumbsup:
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:37
in some cases can be also useful the elvis operator that's a shortcut for the conditional one
for example
x = statement ?: default_value
that is equivalent to
x = statement ? statement : default_value
Evan Floden
@evanfloden
Apr 28 2016 08:38
Perfect, I think I wil be using this in all my workflows!
Paolo Di Tommaso
@pditommaso
Apr 28 2016 08:39
enjoy it
Paolo Di Tommaso
@pditommaso
Apr 28 2016 13:47
not sure the cross is doing exactly what you need
please try to isolate an example