Part 5: Hello Modules¶
See the whole playlist on the Nextflow YouTube channel.
The video transcript is available here.
This section covers how to organize your workflow code to make development and maintenance of your pipeline more efficient and sustainable. Specifically, we are going to demonstrate how to use modules.
In Nextflow, a module is a standalone code file, often encapsulating a single process definition.
To use a module in a workflow, you just add a single-line include statement to your workflow code file; then you can integrate the process into the workflow the same way you normally would.
That makes it possible to reuse process definitions in multiple workflows without producing multiple copies of the code.
When we started developing our workflow, we wrote everything in one single code file.
Now we're going to give the project the standard layout of a Nextflow pipeline: the workflow file becomes main.nf, the processes go into individual modules, and the parameter values go into the configuration file.
This will make our code more shareable, flexible and maintainable.
This part reorganizes the code of the pipeline you built in Parts 1 to 4, without changing what it does:
How to begin from this section
This section of the course assumes you have completed Parts 1-4 of the Hello Nextflow course and have a complete working pipeline.
If you're starting the course from this point, you'll need to copy the nextflow.config file over from the solutions, since it enables Docker for the cowpy process:
0. Warmup: Run hello-modules.nf¶
We're going to use the workflow script hello-modules.nf as a starting point.
It is equivalent to the script produced by working through Part 4 of this training course.
Just to make sure everything is working, run the script once before making any changes:
Command output
N E X T F L O W ~ version 26.10.7
Launching `hello-modules.nf` [hopeful_babbage] revision: 91c8df337b
executor > local (8)
[e3/17c738] sayHello (Celine) | 3 of 3 ✔
[65/f0915c] translateGreeting (es-Celine.txt) | 3 of 3 ✔
[8c/f6000c] collectGreetings (batch) | 1 of 1 ✔
[e8/a30297] cowpy (turkey) | 1 of 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- batch/intermediates/Mateo.txt
- batch/intermediates/Raphael.txt
- batch/intermediates/Celine.txt
translated:
- batch/intermediates/es-Raphael.txt
- batch/intermediates/es-Mateo.txt
- batch/intermediates/es-Celine.txt
collected: batch/intermediates/batch-es-greetings.txt
batch_report: batch/batch-es-report.txt
cowpy_art: batch/cowpy-batch-es-greetings.txt
As previously, you will find the output files under results/batch/.
Directory contents
If that worked for you, you're ready to organize your pipeline code.
1. Organize the code into modules¶
The first step is to give the code the standard file layout: an entry workflow file called main.nf, and one module file per process in a modules/ directory.
1.1. Rename the workflow file to main.nf¶
By convention, the workflow file that holds the entry workflow of a Nextflow project is called main.nf.
It is the entry script of the finished Hello pipeline in Nextflow Run, and it is the file Nextflow looks for when you run a pipeline from a repository without naming a script.
Rename hello-modules.nf to main.nf:
From now on, run the workflow with nextflow run main.nf.
Run it now with the -resume flag:
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [festering_meninsky] revision: 91c8df337b
[45/f75511] sayHello (Raphael) | 3 of 3, cached: 3 ✔
[65/f0915c] translateGreeting (es-Celine.txt) | 3 of 3, cached: 3 ✔
[8c/f6000c] collectGreetings (batch) | 1 of 1, cached: 1 ✔
[e8/a30297] cowpy (turkey) | 1 of 1, cached: 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- batch/intermediates/Celine.txt
- batch/intermediates/Mateo.txt
- batch/intermediates/Raphael.txt
translated:
- batch/intermediates/es-Raphael.txt
- batch/intermediates/es-Mateo.txt
- batch/intermediates/es-Celine.txt
collected: batch/intermediates/batch-es-greetings.txt
batch_report: batch/batch-es-report.txt
cowpy_art: batch/cowpy-batch-es-greetings.txt
Every task is retrieved from the cache: the name of the workflow file doesn't change the work to be done.
1.2. Create a directory to store modules¶
It is best practice to store your modules in a specific directory.
You can call that directory anything you want, but the convention is to call it modules/.
1.3. Create a module for sayHello()¶
In its simplest form, turning an existing process into a module is little more than a copy-paste operation. We're going to create a file stub for the module, copy the relevant code over then delete it from the main workflow file.
Then all we'll need to do is add an include statement so that Nextflow will know to pull in the relevant code at runtime.
1.3.1. Create a file stub for the new module¶
Create an empty file for the module called sayHello.nf.
This gives us a place to put the process code.
1.3.2. Move the sayHello process code to the module file¶
Copy the whole process definition over from the workflow file to the module file.
| modules/sayHello.nf | |
|---|---|
Once that is done, delete the process definition from the workflow file.
1.3.3. Add an include declaration before the params block¶
The syntax for including a process from a module is fairly straightforward:
Insert that above the params block and fill it out appropriately.
You see we've filled in the process name, sayHello, and the path to the file containing the module code, ./modules/sayHello.nf.
Once all four processes are in modules, the workflow file will include each module file this way:
1.3.4. Run the workflow¶
We're running the workflow with essentially the same code and inputs as before, so run it with the -resume flag and see what happens.
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [suspicious_chandrasekhar] revision: 408483af68
[b4/33647f] sayHello (Mateo) | 3 of 3, cached: 3 ✔
[1d/c2cf89] translateGreeting (es-Raphael.txt) | 3 of 3, cached: 3 ✔
[8c/f6000c] collectGreetings (batch) | 1 of 1, cached: 1 ✔
[e8/a30297] cowpy (turkey) | 1 of 1, cached: 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- batch/intermediates/Raphael.txt
- batch/intermediates/Mateo.txt
- batch/intermediates/Celine.txt
translated:
- batch/intermediates/es-Mateo.txt
- batch/intermediates/es-Celine.txt
- batch/intermediates/es-Raphael.txt
collected: batch/intermediates/batch-es-greetings.txt
batch_report: batch/batch-es-report.txt
cowpy_art: batch/cowpy-batch-es-greetings.txt
This should run very quickly because everything is cached. Feel free to check the published outputs.
Nextflow recognized that it's still all the same work to be done, even if the code is split up into multiple files.
1.4. Modularize the remaining processes¶
The translateGreeting, collectGreetings and cowpy processes follow exactly the same pattern as sayHello.
1.4.1. Move the process code into module files¶
Create an empty file for each of the three modules:
Copy each process definition over from the workflow file to its module file:
A module can hold a script block in any language: the Python code of translateGreeting moves over exactly as it was.
The container and conda directives of cowpy move with the process, so the module carries its own software requirements.
Once that is done, delete the three process definitions from the workflow file.
1.4.2. Add the include declarations¶
Add an include declaration for each of the new modules, under the one for sayHello:
The workflow file now contains only the include declarations, the params block, the workflow block and the output block.
Skip the copy-pasting
If you're short on time, this command brings your directory to the end state of sections 1.4.1 and 1.4.2, by copying the four module files and the updated workflow file from the solutions:
1.4.3. Run the workflow¶
Run the workflow with the -resume flag:
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [awesome_mahavira] revision: 794a6d172f
[b4/33647f] sayHello (Mateo) | 3 of 3, cached: 3 ✔
[65/26c109] translateGreeting (es-Mateo.txt) | 3 of 3, cached: 3 ✔
[8c/f6000c] collectGreetings (batch) | 1 of 1, cached: 1 ✔
[e8/a30297] cowpy (turkey) | 1 of 1, cached: 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- batch/intermediates/Mateo.txt
- batch/intermediates/Raphael.txt
- batch/intermediates/Celine.txt
translated:
- batch/intermediates/es-Raphael.txt
- batch/intermediates/es-Celine.txt
- batch/intermediates/es-Mateo.txt
collected: batch/intermediates/batch-es-greetings.txt
batch_report: batch/batch-es-report.txt
cowpy_art: batch/cowpy-batch-es-greetings.txt
Every task is retrieved from the cache once again, including cowpy, which still runs in its container.
Takeaway¶
You know that the entry workflow file of a Nextflow project is conventionally called main.nf, and how to move processes into local modules without breaking the resumability of the workflow.
If you write another pipeline that calls on one of those processes, you just need to type one short include statement to use the relevant module.
This is better than copy-pasting the code, because if later you decide to improve the module, all your pipelines will inherit the improvements.
When you add a new step to a pipeline organized this way, you can write it as a module from the start: create the module file, include it in main.nf, and call it in the workflow.
What's next?¶
Move the parameter values out of the workflow script.
2. Organize the configuration¶
With the code in modules, the next step is to organize the configuration: move the default parameter values into nextflow.config, put the test values in a test profile, and add a profile for running with Conda.
2.1. Move the default values to the configuration¶
The workflow script still sets default values for its parameters in the params block:
| main.nf | |
|---|---|
That was convenient while we were developing the workflow, but these values are not all the same kind of thing:
language = 'es',batch = 'batch'andcharacter = 'turkey'are true defaults: sensible values for anyone who runs the pipeline without specifying them.input = 'data/names.csv'is a test value: it points to the example data provided with the course, which nobody would want to process by default in a real analysis.
True defaults belong in the params section of nextflow.config.
The test value doesn't belong among them; you'll give it its own place in section 2.2.
The workflow script keeps the parameter declarations and their types, so it is still easy to see at a glance which parameters the pipeline accepts.
2.1.1. Remove the values from the workflow script¶
In the params block of the workflow script, make the following code change:
2.1.2. Add the default values to nextflow.config¶
Open nextflow.config and make the following change:
The params section of the configuration file uses = to assign values, without types: the types are declared in the workflow script.
There is no default value for input.
2.1.3. Run the workflow without an input file¶
Try running the workflow the same way as before:
Command output
Since input no longer has a default value, Nextflow stops before running anything and tells you that the parameter is missing.
language, batch and character did not cause an error, because their default values now come from nextflow.config.
The input has no default value on purpose. No input file makes sense for every analysis, so whoever runs the pipeline has to say which file to process.
2.1.4. Provide the input file on the command line¶
Provide the input file with --input:
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [jovial_ekeblad] revision: 6164e6b941
[b4/33647f] sayHello (Mateo) | 3 of 3, cached: 3 ✔
[1d/c2cf89] translateGreeting (es-Raphael.txt) | 3 of 3, cached: 3 ✔
[8c/f6000c] collectGreetings (batch) | 1 of 1, cached: 1 ✔
[e8/a30297] cowpy (turkey) | 1 of 1, cached: 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- batch/intermediates/Celine.txt
- batch/intermediates/Mateo.txt
- batch/intermediates/Raphael.txt
translated:
- batch/intermediates/es-Celine.txt
- batch/intermediates/es-Mateo.txt
- batch/intermediates/es-Raphael.txt
collected: batch/intermediates/batch-es-greetings.txt
batch_report: batch/batch-es-report.txt
cowpy_art: batch/cowpy-batch-es-greetings.txt
The parameter values are the same as before, so every task is retrieved from the cache, and the outputs are still published under results/batch/.
2.2. Create a test profile¶
Typing the path to the input file every time gets tedious when you run the pipeline repeatedly to test it.
You may also want test runs to be easy to tell apart from real ones.
A test profile solves both: it bundles a test input file with any other values you want to use for testing, and you activate it with a single option.
2.2.1. Add the profile to nextflow.config¶
In nextflow.config, make the following change:
| nextflow.config | |
|---|---|
The test profile sets input to the example data file.
It also overrides two default values: it names the batch test, so test outputs are published to results/test/, and it has the ASCII art drawn by the tux character.
When you activate the profile, its values take precedence over the ones in the params section.
2.2.2. Run the workflow with the test profile¶
Run the workflow with -profile test:
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [lethal_shockley] revision: 6164e6b941
executor > local (2)
[e3/17c738] sayHello (Celine) | 3 of 3, cached: 3 ✔
[65/f0915c] translateGreeting (es-Celine.txt) | 3 of 3, cached: 3 ✔
[0c/4ffbad] collectGreetings (test) | 1 of 1 ✔
[91/c6445f] cowpy (tux) | 1 of 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Mateo.txt
- test/intermediates/Raphael.txt
- test/intermediates/Celine.txt
translated:
- test/intermediates/es-Raphael.txt
- test/intermediates/es-Mateo.txt
- test/intermediates/es-Celine.txt
collected: test/intermediates/test-es-greetings.txt
batch_report: test/test-es-report.txt
cowpy_art: test/cowpy-test-es-greetings.txt
The sayHello and translateGreeting tasks are retrieved from the cache, because the profile doesn't change their inputs.
collectGreetings and cowpy run again, because the batch name and the character changed.
The outputs are published under results/test/, separate from the outputs of your earlier runs in results/batch/, and the ASCII art is drawn by tux:
________________
/ Hola, Raphael! \
| Hola, Mateo! |
\ Hola, Celine! /
----------------
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
The run shows how the profile combines with the defaults: it supplies the test input, overrides batch and character, and leaves language at its default value, es.
From now on, run the workflow with -profile test, or provide --input on the command line.
2.3. Add a conda profile¶
In Part 4, you gave the cowpy process both a container and a Conda package, and enabled Docker in nextflow.config.
The finished Hello pipeline in Nextflow Run is set up the same way: it uses Docker by default, and provides a conda profile to use Conda instead.
2.3.1. Add the profile to nextflow.config¶
In the profiles section of nextflow.config, make the following change:
The conda profile turns Docker off and turns Conda on.
Docker stays enabled by default, since the settings in a profile only apply when you activate it.
2.3.2. Run the workflow with the conda profile¶
Activate both the test and conda profiles, separating their names with a comma:
Command output
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [modest_saha] revision: 6164e6b941
executor > local (1)
[45/f75511] sayHello (Raphael) | 3 of 3, cached: 3 ✔
[1d/c2cf89] translateGreeting (es-Raphael.txt) | 3 of 3, cached: 3 ✔
[0c/4ffbad] collectGreetings (test) | 1 of 1, cached: 1 ✔
[b4/b606e2] cowpy (tux) | 1 of 1 ✔
Creating env using conda: conda-forge::cowpy==1.1.5 [cache /workspaces/training/hello-nextflow/work/conda/env-898314d566668b6587ad714ae06b8520]
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Mateo.txt
- test/intermediates/Raphael.txt
- test/intermediates/Celine.txt
translated:
- test/intermediates/es-Celine.txt
- test/intermediates/es-Mateo.txt
- test/intermediates/es-Raphael.txt
collected: test/intermediates/test-es-greetings.txt
batch_report: test/test-es-report.txt
cowpy_art: test/cowpy-test-es-greetings.txt
Nextflow creates a Conda environment from the package declared in the conda directive of cowpy, and runs the task in that environment.
The cowpy task runs again because its software environment changed.
The other processes don't declare any software, so their tasks are retrieved from the cache.
When you run the workflow without the conda profile, it uses Docker again.
To recap, this is where each part of the original script ended up:
Takeaway¶
You know how to separate the parameter declarations in the workflow script from their values in the configuration.
True default values go in the params section of nextflow.config, and the test input, along with any values that should differ for test runs, goes in a test profile.
You also know how to add a profile that switches the pipeline from Docker to Conda.
Your project now has the standard layout of a Nextflow pipeline, the same as the finished Hello pipeline in Nextflow Run: main.nf holds the entry workflow, modules/ holds one module file per process, and nextflow.config holds the default parameter values and the test and conda profiles.
Anyone who knows how a Nextflow pipeline is organized can find their way around it.
What's next?¶
Take a break!
You've completed the Hello Nextflow course. Head to the course summary to review what you learned, share your feedback, and choose where to go next.
Bonus exercises¶
These optional exercises let you practice what you learned in this part.
Each one can be solved with what this part covered; open the hint if you get stuck.
The solutions are also in solutions/5-hello-modules/bonus/.
Exercise 1: Add a profile for French greetings¶
Starting from your main.nf and nextflow.config at the end of this part, add a profile named french that sets the language to French.
Then run the workflow with both the test and french profiles, and check that the collected greetings are in French.
Hint
Write it like the test profile, setting params.language instead of params.input.
To activate two profiles, separate their names with a comma.
Solution
In nextflow.config, add the french profile next to the test profile:
| nextflow.config | |
|---|---|
Then run the workflow with both profiles:
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [suspicious_leavitt] revision: 6164e6b941
executor > local (5)
[45/f75511] sayHello (Raphael) | 3 of 3, cached: 3 ✔
[51/c68f13] translateGreeting (fr-Mateo.txt) | 3 of 3 ✔
[bc/f4f75e] collectGreetings (test) | 1 of 1 ✔
[e1/2fa71d] cowpy (tux) | 1 of 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Mateo.txt
- test/intermediates/Raphael.txt
- test/intermediates/Celine.txt
translated:
- test/intermediates/fr-Celine.txt
- test/intermediates/fr-Raphael.txt
- test/intermediates/fr-Mateo.txt
collected: test/intermediates/test-fr-greetings.txt
batch_report: test/test-fr-report.txt
cowpy_art: test/cowpy-test-fr-greetings.txt
The sayHello tasks are cached, and the translation runs again in French, followed by the steps that depend on it:
Since the collected file, the report and the ASCII art are named after the language, the French results are published next to the Spanish ones instead of overwriting them:
results/test
├── cowpy-test-es-greetings.txt
├── cowpy-test-fr-greetings.txt
├── intermediates
│ ├── Celine.txt
│ ├── es-Celine.txt
│ ├── es-Mateo.txt
│ ├── es-Raphael.txt
│ ├── fr-Celine.txt
│ ├── fr-Mateo.txt
│ ├── fr-Raphael.txt
│ ├── Mateo.txt
│ ├── Raphael.txt
│ ├── test-es-greetings.txt
│ └── test-fr-greetings.txt
├── test-es-report.txt
└── test-fr-report.txt
Exercise 2: Give each module its own folder¶
Larger projects often store each module in its own folder, as modules/<name>/main.nf, so that files related to the module can sit next to it.
Starting from your main.nf at the end of this part, move each module file into a folder of its own, with this layout, and update the workflow so it runs as before:
modules
├── collectGreetings
│ └── main.nf
├── cowpy
│ └── main.nf
├── sayHello
│ └── main.nf
└── translateGreeting
└── main.nf
Hint
Nextflow finds each module from the path in its include declaration.
Solution
After moving the files, update the include declarations in main.nf:
| main.nf | |
|---|---|
Then run the workflow:
N E X T F L O W ~ version 26.10.7
Launching `main.nf` [big_kimura] revision: deff6b13ac
[45/f75511] sayHello (Raphael) | 3 of 3, cached: 3 ✔
[65/f0915c] translateGreeting (es-Celine.txt) | 3 of 3, cached: 3 ✔
[0c/4ffbad] collectGreetings (test) | 1 of 1, cached: 1 ✔
[91/c6445f] cowpy (tux) | 1 of 1, cached: 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Mateo.txt
- test/intermediates/Celine.txt
- test/intermediates/Raphael.txt
translated:
- test/intermediates/es-Mateo.txt
- test/intermediates/es-Raphael.txt
- test/intermediates/es-Celine.txt
collected: test/intermediates/test-es-greetings.txt
batch_report: test/test-es-report.txt
cowpy_art: test/cowpy-test-es-greetings.txt
Every task is retrieved from the cache: moving a module to another file doesn't change the work it does.
Quiz¶
What is the conventional name of the file that holds the entry workflow of a Nextflow project?
What is a module in Nextflow?
What convention is typically used for storing module files?
What is the correct syntax to use a module?
What do you need to change in a process whose script is written in Python when you move it into a module?
What happens to the -resume functionality when using modules?
What are the benefits of using modules? (Select all that apply)
Where should the path to the example input data used for testing go?
What does the conda profile in nextflow.config do when you activate it?