Link Search Menu Expand Document

Step 18 - Add the output filename to the triplet

The other approach to take is to add the output filename to the triplet provided as input to the process. This can be done similarly to what we did with the input filename, i.e.:

// Step - 18
process process_step18 {
    publishDir "output"
    input:
        tuple val(id), file(input), val(config)
    output:
        tuple val("${id}"), file("${config.output}"), val("${config}")
    script:
        """
        a=`cat $input`
        let result="\$a + ${config.term}"
        echo "\$result" > ${config.output}
        """
}
workflow step18 {
    Channel.fromPath( params.input ) \
        | map{ el -> [
            el.baseName.toString(),
            el,
            [
                "output" : "output_from_${el.baseName}.txt",
                "id": el.baseName,
                "operator" : "-",
                "term" : 10
            ]
          ]} \
        | process_step18 \
        | view{ [ it[0], it[1] ] }
}

In order to make a bit more sense of the (gradually growing) configuration map that is sent to the process, we tuned the layout a bit. In this case, the output filename that is configured contains an identifier for the input as well. In this way, the output is always unique.

Since we have configured params.input in nextflow.config, we are able to just run our new pipeline:

> nextflow -q run . -entry step18
WARN: DSL 2 IS AN EXPERIMENTAL FEATURE UNDER DEVELOPMENT -- SYNTAX MAY CHANGE IN FUTURE RELEASE
[input3, <...>/work/9b/7a08a98e8ebf69b0781e06ac95d4d6/output_from_input3.txt]
[input1, <...>/work/e1/2a333f9af260daaad4b86636426004/output_from_input1.txt]
[input2, <...>/work/30/90bc0238055daacc9fdeea3f232c27/output_from_input2.txt]
> ls -1 output/output_from*
output/output_from_input1.txt
output/output_from_input2.txt
output/output_from_input3.txt

In other words, this allows to distinguish between parallel branches in the pipeline.

Please note that if we add steps to the pipeline, because the output is reported as input for the next process, it automatically points to the correct filename even though the next process is not aware of the way the output filename has been specified. That’s nice.