- Run and modify a Nextflow script
- Recognise
process
definition - Recognise
workflow
definition - Understand and use pipeline
params
- Understand and use workflow caching (
-resume
)
- hello_world.nf is a simple Nextflow script
// -------- (1) process definition -------- // process GREET { input: val(x) output: stdout script: "echo -n Hello $x" // (2) String interpolation } // -------- (3) workflow definition -------- // workflow { input_ch = Channel.of('world') // create input channel greet_ch = GREET(input_ch) // run process GREET greet_ch.view() // view the output of GREET }
- Nextflow processes define units of computations carried out by other software. The
script:
section defines a bash script to be run. - String interpolation:
$x
is replaced with the value of the variablex
- Workflows define connections bewteen
channels
,processes
andoperators
.
- Run
hello_world.nf
nextflow run ~/wehi-nextflow-training/module_1/hello_world.nf
- Add additional items to the input channel (e.g.
input_ch = Channel.of('world', 'WEHI')
) and runhello_world.nf
. This will create additional inputs for the processes to be run on.
- Nextflow workflows contain a special variable
params
which is a 'map' (equivalent to a named list in R or a dict in python) - We can set default values for
params
in the Nextflow script, e.g.:params.greeting = 'Hello' process GREET { input: val(x) output: stdout script: "echo -n $params.greeting $x" }
- We can then override parameters with command line arguments, e.g.:
nextflow run ~/wehi-nextflow-training/module_1/hello_world.nf --greeting 'Hey'
- See https://www.nextflow.io/docs/latest/cli.html#pipeline-parameters
- Convert
greeting
to an input paramter inhello_world.nf
as in the example above. - Run
hello_world.nf
, providing--greeting
as a command line arguemnt.
Here will add another process ASK_QUESTION
to run on the output of GREET
.
-
Add the parameter and process definition below to
hello_world.nf
params.question = 'how are you?' process ASK_QUESTION { input: val(x) output: stdout script: "echo -n $x, $params.question" }
-
Create a channel
ask_question_ch
by runningASK_QUESTION
ongreet_ch
-
Replace
greet_ch.view()
withask_question_ch.view()
to view the output ofASK_QUESTION
-
Run
hello_world.nf
with the parameter--question
, e.g.:nextflow run ~/wehi-nextflow-training/module_1/hello_world.nf --question 'what time is it?'
Solution
params.greeting = 'Hello' params.question = 'how are you?' process GREET { input: val(x) output: stdout script: "echo -n $params.greeting $x" } process ASK_QUESTION { input: val(x) output: stdout script: "echo -n $x, $params.question" } workflow { input_ch = Channel.of('world', 'WEHI') greet_ch = GREET(input_ch) ask_question_ch = ASK_QUESTION(greet_ch) ask_question_ch.view() }
- Nextflow provides a mechanism to reuse results from previously run workflows, potentially saving costly processes from being recomputed.
- This works by creating a unique hash from all process inputs, and only running the process if a matching hash is not present in the work directory. If a match if found the existing outputs are used.
- To use this feature, provide the
-resume
argument at the command line:nextflow run ~/wehi-nextflow-training/module_1/hello_world.nf -resume
- In general, we should always use this option
- Experiment by running
hello_world.nf
with and without-resume
, and with different parameters--greeting
and--question
.