Skip to content

Outputs

Learning objectives

  1. Utlizie Nextflow process output blocks
  2. Publish results from your pipeline with directives

Instead of printing 'Hello World!' to the standard output it can be saved to a file. In a "real-world" pipeline, this is like having a command that specifies an output file as part of its normal syntax.

Here you're going to update the script and the output definition blocks to save the 'Hello World!' as an output.

Redirecting outputs

The script block will need to be updated to redirect the 'Hello World!' output to a file.

The > operator can be used for output redirection.

Exercise

Redirect 'Hello World!' to a file named 'output.txt' in the script block and add a comment to annotate your change.

Solution
hello-world.nf
// Use echo to print 'Hello World!' and redirect to output.txt
process SAYHELLO {
    debug true

    output:
    stdout

    script:
    """
    echo 'Hello World!' > output.txt
    """
}

Outputs blocks

Outputs in the output definition block typically require an output qualifier and a output name:

<output qualifier> <output name>

The output qualifier defines the type of data to be received. This information is used by Nextflow to apply the semantic rules associated with each qualifier, and handle it properly.

Common output qualifiers include val and path:

  • val: Emit the variable with the specified name
  • For example, val 'Hello World!'
  • path: Emit a file produced by the process with the specified name
  • For example, path 'output.txt'

See the Nextflow documentation for a full list of output qualifiers.

Warning

If you set the wrong qualifier the pipeline will likely throw errors.

The output name is a name given to the output variable. If a specific file is being produced it can be named in single quotes:

hello-world.nf
output:
path 'output.txt'

The output name and the file generated by the script must match (or be picked up by a glob pattern).

Exercise

Add path 'output.txt' in the SAYHELLO output block.

Solution
hello-world.nf
// Use echo to print 'Hello World!' and redirect to output.txt
process SAYHELLO {
    debug true

    output:
    path 'output.txt'

    script:
    """
    echo 'Hello World!' > output.txt
    """
}

Warning

This example is brittle because the output filename is hardcoded in two separate places (the script and the output definition blocks). If you change one but not the other, the script will break.

Publishing directory

Without a publishing strategy any files that are created by a process will only exist in the work directory.

Realistically, you may want to capture a set of outputs and save them in a specific directory.

The publishDir directive can be used to specify where and how output files should be saved. For example:

publishDir 'results'

By adding the above to a process, all output files would be saved in a new folder called results in the current working directory. The process directive is process specific.

Exercise

Replace debug true with publishDir 'results' in the SAYHELLO process block. Execute the pipeline again. View your new results folder in the working directory.

Solution
hello-world.nf
// Use echo to print 'Hello World!' and redirect to output.txt
process SAYHELLO {
    publishDir 'results'

    output:
    path 'output.txt'

    script:
    """
    echo 'Hello World!' > output.txt
    """
}

Summary

In this step you have learned:

  1. How to redirect outputs
  2. How to use output block
  3. How to publish results