<p>Is this inside the <code>main.nf</code> file or inside the <code>process.nf</code> file? Anyway, the <code>param.anything</code> variable should already be available in the <code>nextflow</code> context when executed, thus, you could simply do:</p>
<pre class="lang-bash prettyprint-override"><code>if (params.aligner == &quot;bowtie&quot;) {
    align_bowtie2(get_fq_info.out.fq_info)
} else {
    align_bwa(get_fq_info.out.fq_info)
}
</code></pre>
<p>Check out this <a href="https://github.com/fmalmeida/bacannot/blob/master/main.nf" rel="nofollow noreferrer"><code>main.nf</code></a> of a nextflow pipeline of mine, it contains some working if/else snippets.</p>
<blockquote>
<p>Here it is <a href="https://stackoverflow.com/questions/50443545/conditional-pipeline-in-nextflow">another StackOverflow question on conditional</a> nextflow pipelines that might help.</p>
</blockquote>
<p>Hope it helps.</p>

CC BY-SA 4.0
2021-04-15T18:28:48.163Z
2021-04-15T18:33:55.127Z
2021-04-15T18:33:55.127Z
Felipe Almeida
<p>You can move that into your <a href="https://www.nextflow.io/docs/latest/dsl2.html#workflow-definition" rel="nofollow noreferrer">workflow definition</a>. There's an example in the docs of how best to use <a href="https://www.nextflow.io/docs/latest/dsl2.html#workflow-parameters" rel="nofollow noreferrer">workflow parameters</a>:</p>
<blockquote>
<p>A workflow component can access any variable and parameter defined in
the outer scope:</p>
<pre><code>params.data = '/some/data/file'

workflow my_pipeline {
    if( params.data )
        bar(params.data)
    else
        bar(foo()) }
</code></pre>
</blockquote>
<p>Your example workflow would look like:</p>
<pre><code>workflow {

    fq_info = get_fq_info.out.fq_info

    if( params.aligner == &quot;bowtie&quot; ) {
        align_bowtie2( fq_info )
    }
    else {
        align_bwa( fq_info )
    }

    ...
}
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2021-04-16T03:15:39.450Z
2021-04-16T03:15:39.450Z
Steve
<p>How should I do:</p>
<pre><code>...
if ($(params.aligner) == &quot;bowtie&quot;) {
    align_bowtie2(get_fq_info.out.fq_info)
}
else {
    align_bwa(get_fq_info.out.fq_info)
}
...
</code></pre>

<nextflow>
Use conditional in workflow in Nextflow dsl2
# Use conditional in workflow in Nextflow dsl2

(Asked by: username_0 on 2021-04-15)

How should I do:
...
if ($(params.aligner) == "bowtie") {
    align_bowtie2(get_fq_info.out.fq_info)
}
else {
    align_bwa(get_fq_info.out.fq_info)
}
...

**Tags:** nextflow

---

## 2 answers

### Answer by username_2 on 2021-04-16. Score: 2 | Accepted answer

You can move that into your workflow definition. There's an example in the docs of how best to use workflow parameters:

A workflow component can access any variable and parameter defined in
the outer scope:
params.data = '/some/data/file'

workflow my_pipeline {
    if( params.data )
        bar(params.data)
    else
        bar(foo()) }


Your example workflow would look like:
workflow {

    fq_info = get_fq_info.out.fq_info

    if( params.aligner == "bowtie" ) {
        align_bowtie2( fq_info )
    }
    else {
        align_bwa( fq_info )
    }

    ...
}
```


---

### Answer by username_1 on 2021-04-15. Score: 2

Is this inside the main.nf file or inside the process.nf file? Anyway, the param.anything variable should already be available in the nextflow context when executed, thus, you could simply do:
if (params.aligner == "bowtie") {
    align_bowtie2(get_fq_info.out.fq_info)
} else {
    align_bwa(get_fq_info.out.fq_info)
}

Check out this main.nf of a nextflow pipeline of mine, it contains some working if/else snippets.

Here it is another StackOverflow question on conditional nextflow pipelines that might help.

Hope it helps.


---

