Challenges¶
The bonus exercises at the end of each part can be solved with what that part covers. The challenges on this page go further on purpose: each one asks you to extend the Hello pipeline with a Nextflow feature that the course does not cover.
Each challenge starts from the end state of one part of the course.
Copy the solution file for that state to hello-challenge.nf in the hello-nextflow directory, as shown in each challenge, and make your changes in that file.
Your own course scripts stay as they are.
The hint for each challenge introduces the new feature and links to the Nextflow documentation. Each solution ends with a pointer to the side quest that covers the feature in depth.
Here is what each challenge covers:
| Challenge | Starts from the end of | New feature | Side quest |
|---|---|---|---|
| 1. Read a CSV file with a header row | Part 2 | splitCsv() with a header row |
Splitting and Grouping |
| 2. Greet only the French names | Part 2 | The filter() operator |
Splitting and Grouping |
| 3. Greet each name in its own language | Part 3 | Tuples in channels and process inputs and outputs | Metadata and Meta Maps |
| 4. Give each name its own character | Part 5 | Tuples across modules | Metadata and Meta Maps |
| 5. Package the greeting steps as a reusable workflow | Part 5 | Named workflows with take and emit |
Workflows of Workflows |
| 6. Translate into two languages in one run | Part 5 | Including a module under an alias | Workflows of Workflows |
The solution files are in solutions/challenges/, one directory per challenge.
To run a solution directly, launch it from the hello-nextflow directory.
Challenges 4 to 6 also need the nextflow.config from the end of Part 5 in that directory, which you already have if you completed the course; otherwise, copy it with cp solutions/5-hello-modules/nextflow.config . first.
Challenge 1: Read a CSV file with a header row¶
Start from hello-channels.nf as it stands at the end of Part 2:
Many CSV files start with a header row that names the columns.
Create a copy of data/names.csv called data/names-header.csv, and add the header row name,language,character at the top.
Then adapt hello-challenge.nf so that it reads this file and picks the name by its column name instead of its position.
The workflow should still greet the same three names.
Hint
The splitCsv() operator has a header option.
With header: true, Nextflow reads the column names from the first row, and returns each following row as a set of named values.
You can then pick a value by its column name, as in row.name, instead of by its position, as in row[0].
Solution
Point the input parameter to the new file:
Then parse the header and pick the column by name:
| hello-challenge.nf | |
|---|---|
Run the workflow:
After splitCsv: [name:Mateo, language:es, character:squirrel]
After splitCsv: [name:Raphael, language:it, character:turtle]
After splitCsv: [name:Celine, language:fr, character:moose]
After map: Mateo
After map: Raphael
After map: Celine
Each row is now a set of named values, and the header row itself is not passed on as a row.
The solution directory also contains names-header.csv.
To run the solution directly, point the input parameter to that copy:
nextflow run solutions/challenges/challenge-1/hello-challenge.nf --input solutions/challenges/challenge-1/names-header.csv
To learn more about reading CSV files with a header row, see the Splitting and Grouping side quest.
Challenge 2: Greet only the French names¶
Start from hello-channels.nf as it stands at the end of Part 2:
Change the workflow so that it only greets the names whose language, in the second column of data/names.csv, is fr.
Only one greeting file should be produced.
Hint
Use the filter() operator.
Like map(), it takes a closure, and keeps only the elements for which the closure returns true.
Solution
Add a filter() step between splitCsv() and map(), while each row still holds all its columns:
| hello-challenge.nf | |
|---|---|
Run the workflow:
[19/1c5874] sayHello (Celine) | 1 of 1 ✔
Before splitCsv: /workspaces/training/hello-nextflow/data/names.csv
After splitCsv: [Mateo, es, squirrel]
After splitCsv: [Raphael, it, turtle]
After splitCsv: [Celine, fr, moose]
After map: Celine
All three rows are read, but only Celine's row passes the filter, so sayHello runs once and writes Hello, Celine!.
To learn more about filtering channel contents, see the Splitting and Grouping side quest.
Challenge 3: Greet each name in its own language¶
Start from hello-workflow.nf as it stands at the end of Part 3:
The second column of data/names.csv gives a language for each name.
Translate each greeting into the language of its own row, instead of one language for the whole batch.
Run it with --batch mixed and check that the collected file contains Hola, Mateo!, Ciao, Raphael! and Bonjour, Celine!.
Hint
Each item now needs to carry two values, the name and its language, through sayHello to translateGreeting.
A channel element can be a list of values, and a process can accept and emit such an element as a single tuple:
.map { row -> [row[0], row[1]] } // channel elements: [name, language]
input:
tuple val(name), val(language) // in sayHello
output:
tuple path("${name}.txt"), val(language)
translateGreeting then takes tuple path(input_file), val(language) as its only input.
collectGreetings still takes a language for its file names; give it a label such as 'native', since each name is greeted in its own language.
Solution
Make sayHello accept the pair and pass the language along with its output:
| hello-challenge.nf | |
|---|---|
Make translateGreeting take that pair as a single input:
In the workflow, keep both columns in the channel, call translateGreeting with the output of sayHello alone, and give collectGreetings a fixed language label:
The greetings are now in several languages, so params.language no longer describes them.
collectGreetings still needs a language for its file names, so it gets the label 'native' instead.
The language parameter is no longer used, so remove it from the params block.
sayHello now emits pairs, so publish only the file from each pair:
| hello-challenge.nf | |
|---|---|
In the map closure, the underscore at the start of _language tells Nextflow that the variable is deliberately unused, so nextflow lint doesn't warn about it.
Run the workflow:
Command output
N E X T F L O W ~ version 26.10.7
Launching `hello-challenge.nf` [loving_stallman] revision: 14b81b681a
executor > local (7)
[84/0c2d79] sayHello (Raphael) | 3 of 3 ✔
[45/83cea3] translateGreeting (it-Raphael.txt) | 3 of 3 ✔
[13/3a83f8] collectGreetings (mixed) | 1 of 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- mixed/intermediates/Mateo.txt
- mixed/intermediates/Celine.txt
- mixed/intermediates/Raphael.txt
translated:
- mixed/intermediates/fr-Celine.txt
- mixed/intermediates/es-Mateo.txt
- mixed/intermediates/it-Raphael.txt
collected: mixed/intermediates/mixed-native-greetings.txt
batch_report: mixed/mixed-native-report.txt
Each translated file is named after its own language, and the collected file contains one greeting in each language:
To learn more about passing several values together through a process, see the Metadata and Meta Maps side quest.
Challenge 4: Give each name its own character¶
Start from main.nf and its modules as they stand at the end of Part 5:
At the end of Part 5, the pipeline project consists of main.nf, the modules in modules/, and the parameter values and profiles in nextflow.config.
This challenge changes the modules, which main.nf and Challenges 5 and 6 also use, so the second command copies them to challenge-modules/.
Point the include declarations in hello-challenge.nf at ./challenge-modules/... and edit only those copies, so that main.nf and the other challenges keep working.
As in Part 5 Exercise 2, Nextflow finds each module from the path in its include declaration.
hello-challenge.nf uses the same configuration as main.nf.
The third column of data/names.csv assigns a character to each name: squirrel for Mateo, turtle for Raphael and moose for Celine.
Make the workflow produce one ASCII art file per name, each drawn by the character from its row, instead of a single drawing of all the greetings.
Keep the collected file and the report as they are.
Hint
Carry the character along with each greeting, in a tuple.
Map each CSV row to [row[0], row[2]], declare process inputs like tuple val(name), val(character) and outputs like tuple path("${name}.txt"), val(character), and call cowpy on the output of translateGreeting rather than on the collected file.
Where only the file is needed, remove the character with map.
Challenge 3 uses the same approach.
Solution
In hello-challenge.nf, include the modules from challenge-modules/:
| hello-challenge.nf | |
|---|---|
In the workflow, build [name, character] pairs, pass them through the processes, and call cowpy on each translated greeting:
In the map closures, the underscore at the start of _character tells Nextflow that the variable is deliberately unused, so nextflow lint doesn't warn about it.
The character parameter is no longer used, so you can remove it from the params block of hello-challenge.nf.
Keep its values in nextflow.config, since main.nf still uses them.
The tux value from the test profile has no effect on hello-challenge.nf: each drawing uses the character from its own row.
In the modules in challenge-modules/, the inputs and outputs carry the character alongside the file:
| challenge-modules/sayHello.nf | |
|---|---|
| challenge-modules/translateGreeting.nf | |
|---|---|
Run the workflow:
Command output
N E X T F L O W ~ version 26.10.7
Launching `hello-challenge.nf` [loving_varahamihira] revision: 1a882b4ad2
executor > local (10)
[20/26f5c5] sayHello (Mateo) | 3 of 3 ✔
[78/a33880] translateGreeting (es-Mateo.txt) | 3 of 3 ✔
[bc/0c52b4] collectGreetings (test) | 1 of 1 ✔
[42/b4b93b] cowpy (squirrel) | 3 of 3 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Raphael.txt
- test/intermediates/Celine.txt
- test/intermediates/Mateo.txt
translated:
- test/intermediates/es-Raphael.txt
- test/intermediates/es-Celine.txt
- test/intermediates/es-Mateo.txt
collected: test/intermediates/test-es-greetings.txt
batch_report: test/test-es-report.txt
cowpy_art:
- test/cowpy-es-Raphael.txt
- test/cowpy-es-Celine.txt
- test/cowpy-es-Mateo.txt
cowpy now runs three times, once per name, and each file is drawn by that name's character:
______________
< Hola, Mateo! >
--------------
\
\
_ _
| \__/| .~ ~.
/oo `./ .'
{o__, \ {
/ . . ) \
`-` '-' \ }
.( _( )_.'
'---.~_ _ _|
________________
< Hola, Raphael! >
----------------
\ ___-------___
\ _-~~ ~~-_
\ _-~ /~-_
/^\__/^\ /~ \ / \
/| O|| O| / \_______________/ \
| |___||__| / / \ \
| \ / / \ \
| (_______) /______/ \_________ \
| / / \ / \
\ \^\\ \ / \ /
\ || \______________/ _-_ //\__//
\ ||------_-~~-_ ------------- \ --/~ ~\ || __/
~-----||====/~ |==================| |/~~~~~
(_(__/ ./ / \_\ \.
(_(___/ \_____)_)
_______________
< Hola, Celine! >
---------------
\
\ \_\_ _/_/
\ \__/
(oo)\_______
(__)\ )\/\
||----w |
|| ||
The solution directory contains its own challenge-modules/ with the modified modules, so you can run the solution directly:
The Metadata and Meta Maps side quest covers this pattern in depth.
Its section 1.4 groups several values into one tuple input, as you did here.
Section 1.5 then solves a problem much like this one, with a cowpy character per row, using a meta map: a single map that holds all of a sample's metadata next to its file, so that adding a field doesn't change the input and output declarations of every process.
Challenge 5: Package the greeting steps as a reusable workflow¶
Start from main.nf as it stands at the end of Part 5:
The copy uses the same modules and configuration as main.nf.
This challenge doesn't change them.
Move the three greeting steps, sayHello, translateGreeting and collectGreetings, into a named workflow called GREET, in a new file workflows/greet.nf.
GREET takes the channel of names, the language and the batch name as inputs, and outputs the greetings, the translated greetings, the collected file and the report.
The entry workflow in hello-challenge.nf then calls GREET, passes the collected file to cowpy, and publishes the same outputs as before.
Hint
A named workflow declares its inputs in a take: section and its outputs in an emit: section, around the main: section that holds its logic (see Takes and emits):
workflow GREET {
take:
names_ch
main:
greetings_ch = sayHello(names_ch)
emit:
greetings = greetings_ch
}
You include a named workflow the same way as a process, and call it the same way too: with greet_out = GREET(...), its named outputs are available as greet_out.greetings and so on.
The paths in include declarations are relative to the file that contains them, so workflows/greet.nf includes the modules from ../modules/.
Solution
Create workflows/greet.nf with the three greeting steps:
The include paths start with ../ because the file is in workflows/, one level below the modules directory.
Inside GREET, the language and the batch name come from the take: inputs instead of params, and the language goes to both translateGreeting and collectGreetings.
In hello-challenge.nf, include GREET instead of the three modules, call it, and publish its outputs:
The output block stays as it is.
Only the entry workflow can publish outputs, so GREET emits everything that needs to be published, including the intermediate greetings and translations, and the entry workflow publishes them from greet_out.
Run the workflow:
Command output
N E X T F L O W ~ version 26.10.7
Launching `hello-challenge.nf` [wise_colden] revision: 40b89c26d3
executor > local (8)
[41/6b7df0] GREET:sayHello (Raphael) | 3 of 3 ✔
[86/ffbe78] GREET:translateGreeting (es-Raphael.txt) | 3 of 3 ✔
[4d/3ff323] GREET:collectGreetings (test) | 1 of 1 ✔
[ee/b62338] cowpy (tux) | 1 of 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-Celine.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
The processes called inside GREET are listed as GREET:sayHello and so on, and the published outputs are the same as with main.nf.
The solution directory contains its own copy of the modules and the configuration, so you can run the solution directly:
To learn more about composing workflows, see the Workflows of Workflows side quest.
Challenge 6: Translate into two languages in one run¶
Start from main.nf as it stands at the end of Part 5:
The copy uses the same modules and configuration as main.nf.
translateGreeting takes the language as an input, so translating into a second language looks like a matter of calling it a second time.
However, a process can only be called once in a workflow.
A second call to translateGreeting fails with this error:
Process 'translateGreeting' was called twice -- include it with a different alias or call it in a different workflow
Add a second_language parameter with the default value fr, and make the workflow translate the greetings into both languages.
Collect the second-language greetings into their own file and report, and publish them alongside the existing outputs.
Keep cowpy on the greetings in the first language.
Hint
Include the same process a second time under a different name with include { ... as ... } (see Calling processes and workflows):
translateSecond is then a separate process that runs the same code, and you can call it with its own inputs.
collectGreetings needs the same treatment.
Since it includes the language in its file names, the second collection doesn't clash with the first.
Solution
Add the default value of the new parameter to the params section of nextflow.config:
| nextflow.config | |
|---|---|
main.nf doesn't declare second_language, and still runs as before with this configuration.
In hello-challenge.nf, include translateGreeting and collectGreetings a second time under aliases:
Declare the new parameter:
| hello-challenge.nf | |
|---|---|
In the workflow, call the aliased processes on the same greetings, and publish their outputs:
Add the three new outputs to the output block, in the same directories as their first-language counterparts:
| hello-challenge.nf | |
|---|---|
Run the workflow:
Command output
N E X T F L O W ~ version 26.10.7
Launching `hello-challenge.nf` [ecstatic_mirzakhani] revision: db63a39de6
executor > local (12)
[20/f7231c] sayHello (Raphael) | 3 of 3 ✔
[ad/90c9dc] translateGreeting (es-Raphael.txt) | 3 of 3 ✔
[b0/2bcfc6] collectGreetings (test) | 1 of 1 ✔
[1c/782a6d] translateSecond (fr-Mateo.txt) | 3 of 3 ✔
[8d/3764ae] collectSecond (test) | 1 of 1 ✔
[ae/d6690a] cowpy (tux) | 1 of 1 ✔
Outputs:
/workspaces/training/hello-nextflow/results
greetings:
- test/intermediates/Celine.txt
- test/intermediates/Raphael.txt
- test/intermediates/Mateo.txt
translated:
- test/intermediates/es-Mateo.txt
- test/intermediates/es-Celine.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
translated_second:
- test/intermediates/fr-Celine.txt
- test/intermediates/fr-Raphael.txt
- test/intermediates/fr-Mateo.txt
collected_second: test/intermediates/test-fr-greetings.txt
batch_report_second: test/test-fr-report.txt
translateSecond and collectSecond are listed as separate processes, and the second collected file contains the French greetings:
Aliases work for workflows too.
With the GREET workflow from Challenge 5, you could include it twice, as in include { GREET as GREET_SECOND } from './workflows/greet.nf', and call each one with its own language.
The solution directory contains its own copy of the modules and the configuration, so you can run the solution directly:
To learn more about including workflows, with or without an alias, see the Workflows of Workflows side quest.
What's next?¶
Return to the course summary to choose a course for further study.