Einar
Einar

Reputation: 4933

nextflow: change part of the script basing on a parameter

I have a Nextflow workflow that's like this (reduced):

params.filter_pass = true
// ... more stuff

process concatenate_vcf {

cpus 6

input:
file(vcf_files) from source_vcf.collect()
file(tabix_files) from source_vcf_tbi.collect()

output:
file("assembled.vcf.gz") into decompose_ch

script:
"""
echo ${vcf_files} | tr " " "\n" > vcflist

bcftools merge  \
    -l vcflist \
    -m none \
    -f PASS,. \
    --threads ${task.cpus} \
    -O z \
    -o assembled.vcf.gz

rm -f vcflist
"""

}

Now, I want to add the -f PASS,. part of the command in the script in the bcftools merge call only if params.filter_pass is true.

In other words, the script would be executed like this, if params.filter_pass is true (other lines removed for clarity):

bcftools merge  \
    -l vcflist \
    -m none \
    -f PASS,. \
    --threads ${task.cpus} \
    -O z \
    -o assembled.vcf.gz

and if it instead params.filter_pass is false:

bcftools merge  \
    -l vcflist \
    -m none \
    --threads ${task.cpus} \
    -O z \
    -o assembled.vcf.gz

I know I can use conditional scripts but that would mean replicating the whole script stanza just to change one parameter.

Is this use case possible with Nextflow?

Upvotes: 2

Views: 453

Answers (1)

Steve
Steve

Reputation: 54502

The general pattern is to use a local variable in the 'script' block and a ternary operator to add the -f PASS,. filter option when params.filter_pass is true:

process concatenate_vcf {

    ...

    script:
    def filter_pass = params.filter_pass ? '-f PASS,.' : ''

    """
    echo "${vcf_files.join('\n')}" > vcf.list

    bcftools merge  \\
        -l vcf.list \\
        -m none \\
        ${filter_pass} \\
        --threads ${task.cpus} \\
        -O z \\
        -o assembled.vcf.gz
    """
}

An if/else statement could also be used in place of the ternary operator if preferred.

Upvotes: 4

Related Questions