Flexible publishDir in Nextflow Using the Same Process

One thing I like in Nextflow is how “publishDir” can be made dynamic, avoiding duplicated processes.

In my “FASTQC” workflow, I wanted the same process to handle both raw and trimmed reads while publishing outputs to different folders.

Instead of creating separate logic, I used a conditional publishDir

publishDir(

path: { task.process.contains(‘TRIMMED_FASTQC’)

? “${params.outdir}/shortReads/checkQuality/trimmed/”

: “${params.outdir}/shortReads/checkQuality/raw/”

}, mode: ‘copy’

)

The trick is that I reused the same module with include and assigned it a new process name.

include { FASTQC as TRIMMED_FASTQC } from ‘./modules/fastqc’

Since “task.process” reflects the aliased process name, the same “FASTQC” module automatically publishes results to the correct location..

A small Nextflow trick that keeps pipelines easier to maintain. :smiling_face_with_sunglasses:

1 Like