You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/syntax.md
+59-46Lines changed: 59 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,15 @@ This page provides a comprehensive description of the Nextflow language.
6
6
7
7
## Comments
8
8
9
-
Nextflow uses Java-style comments: `//`for a line comment, and `/* ... */` for a block comment:
9
+
A line comment starts with `//` and includes the rest of the line.
10
10
11
11
```groovy
12
12
println 'Hello world!' // line comment
13
+
```
14
+
15
+
A block comment starts with `/*` and includes all subsequent characters up to the first `*/`.
13
16
17
+
```groovy
14
18
/*
15
19
* block comment
16
20
*/
@@ -31,11 +35,9 @@ A Nextflow script may contain the following top-level declarations:
31
35
- Enum types
32
36
- Output block
33
37
34
-
These declarations are in turn composed of statements and expressions.
38
+
Script declarations are in turn composed of statements and expressions.
35
39
36
-
Alternatively, a script may contain one or more [statements](#statements), as long as there are no top-level declarations. In this case, the entire script will be treated as an entry workflow.
37
-
38
-
For example, the following script:
40
+
A script may contain one or more [statements](#statements), if there are no top-level declarations. In this case, the entire script will be treated as an entry workflow. For example:
39
41
40
42
```groovy
41
43
println 'Hello world!'
@@ -50,7 +52,7 @@ workflow {
50
52
```
51
53
52
54
:::{warning}
53
-
Top-level declarations and statements can not be mixed at the same level. If your script has top-level declarations, all statements must be contained within top-level declarations such as the entry workflow.
55
+
Statements and top-level declarations can not be mixed at the same level. If your script has top-level declarations, all statements must be contained within top-level declarations such as the entry workflow.
54
56
:::
55
57
56
58
### Shebang
@@ -74,23 +76,27 @@ nextflow.preview.topic = true
74
76
An include declaration consists of an *include source* and one or more *include clauses*:
75
77
76
78
```groovy
77
-
include { foo ; bar as baz } from './some/module'
79
+
include { foo as bar } from './some/module'
78
80
```
79
81
80
-
The include source should be a string literal and should refer to either a local path (e.g. `./module.nf`) or a plugin (e.g. `plugin/nf-hello`).
82
+
The include source should be a string literal and should refer to either a local path (e.g. `./module.nf`) or a plugin (e.g. `plugin/nf-hello`). Each include clause should specify a name, and may also specify an *alias*. In the example above, `foo` is included under the alias `bar`.
81
83
82
-
Each include clause should specify a name, and may also specify an *alias*. In the example above, `bar` is included under the alias `baz`.
83
-
84
-
Include clauses can be separated by newlines or semi-colons, or they can be specified as separate includes:
84
+
Include clauses can be separated by semi-colons or newlines:
85
85
86
86
```groovy
87
+
// semi-colons
88
+
include { foo ; bar as baz } from './some/module'
89
+
87
90
// newlines
88
91
include {
89
92
foo
90
93
bar as baz
91
94
} from './some/module'
95
+
```
96
+
97
+
Include clauses can also be specified as separate includes:
92
98
93
-
// separate includes
99
+
```groovy
94
100
include { foo } from './some/module'
95
101
include { bar as baz } from './some/module'
96
102
```
@@ -115,7 +121,9 @@ Parameters supplied via command line options, params files, and config files tak
115
121
116
122
### Workflow
117
123
118
-
A workflow consists of a name and a body. The workflow body consists of a *main* section, with additional sections for *takes*, *emits*, and *publishers* (shown later):
124
+
A workflow can be a *named workflow* or an *entry workflow*.
125
+
126
+
A *named workflow* consists of a name and a body, and may consist of a *take*, *main*, *emit*, and *publish* section:
119
127
120
128
```groovy
121
129
workflow greet {
@@ -130,15 +138,18 @@ workflow greet {
130
138
}
131
139
```
132
140
133
-
- The take, emit, and publish sections are optional. If they are not specified, the `main:` section label can be omitted.
141
+
- The take, emit, and publish sections are optional. The `main:` section label can be omitted if they are not specified.
134
142
135
143
- The take section consists of one or more parameters.
136
144
137
145
- The main section consists of one or more [statements](#statements).
138
146
139
147
- The emit section consists of one or more *emit statements*. An emit statement can be a [variable name](#variable), an [assignment](#assignment), or an [expression statement](#expression-statement). If an emit statement is an expression statement, it must be the only emit.
140
148
141
-
An alternative workflow form, known as an *entry workflow*, has no name and may only define a main and publish section:
149
+
- The publish section can be specified but is intended to be used in the entry workflow (see below).
150
+
151
+
152
+
An *entry workflow* has no name and may consist of a *main* and *publish* section:
142
153
143
154
```groovy
144
155
workflow {
@@ -158,8 +169,6 @@ workflow {
158
169
159
170
- The publish section consists of one or more *publish statements*. A publish statement is a [right-shift expression](#binary-expressions), where the left-hand side is an expression that refers to a value in the workflow body, and the right-hand side is an expression that returns a string.
160
171
161
-
- The publish section can also be specified in named workflows as a convenience, but is intended mainly to be used in the entry workflow.
162
-
163
172
In order for a script to be executable, it must either define an entry workflow or use the implicit workflow syntax described [above](#top-level-declarations).
164
173
165
174
Entry workflow definitions are ignored when a script is included as a module. This way, the same script can be included as a module or executed as a pipeline.
@@ -205,13 +214,13 @@ process greet {
205
214
}
206
215
```
207
216
208
-
-Each of these additional sections are optional. Directives do not have an explicit section label, but are simply defined first.
217
+
-A process must define a script, shell, or exec section (see below). All other sections are optional. Directives do not have an explicit section label, but must be defined first.
209
218
210
219
- The `script:` section label can be omitted only when there are no other sections in the body.
211
220
212
-
- Sections must be defined in the order shown above, with the exception of the output section, which can alternatively be specified after the script and stub.
221
+
- Sections must be defined in the order shown above, with the exception of the output section, which can also be specified after the script and stub.
213
222
214
-
Each section may contain one or more statements. For directives, inputs, and outputs, these statements must be [function calls](#function-call). Refer to {ref}`process-reference` for the set of available input qualifiers, output qualifiers, and directives.
223
+
Each section may contain one or more statements. For directives, inputs, and outputs, these statements must be [function calls](#function-call). See {ref}`process-reference` for the set of available input qualifiers, output qualifiers, and directives.
215
224
216
225
The script section can be substituted with a shell or exec section:
217
226
@@ -241,7 +250,7 @@ process greetExec {
241
250
242
251
The script, shell, and stub sections must return a string in the same manner as a [function](#function).
243
252
244
-
Refer to {ref}`process-page` for more information on the semantics of each process section.
253
+
See {ref}`process-page` for more information on the semantics of each process section.
245
254
246
255
(syntax-function)=
247
256
@@ -277,7 +286,7 @@ def fib(x) {
277
286
278
287
### Enum type
279
288
280
-
An enum type declaration consists of a name and a body, which consists of a comma-separated list of identifiers:
289
+
An enum type declaration consists of a name and a body. The body consists of a comma-separated list of identifiers:
281
290
282
291
```groovy
283
292
enum Day {
@@ -291,7 +300,7 @@ enum Day {
291
300
}
292
301
```
293
302
294
-
Enum values can be accessed as `Day.MONDAY`, `Day.TUESDAY`, and so on.
303
+
Enum values in the above example can be accessed as `Day.MONDAY`, `Day.TUESDAY`, and so on.
295
304
296
305
:::{note}
297
306
Enum types cannot be included across modules at this time.
@@ -312,11 +321,11 @@ output {
312
321
}
313
322
```
314
323
315
-
Only one output block may be defined in a script. Refer to {ref}`workflow-output-def` for the set of available target directives.
324
+
Only one output block may be defined in a script. See {ref}`workflow-output-def` for the set of available target directives.
316
325
317
326
## Statements
318
327
319
-
Statements should be separated by a newline or semi-colon:
328
+
Statements can be separated by either a newline or a semi-colon:
320
329
321
330
```groovy
322
331
// newline
@@ -335,19 +344,19 @@ Variables can be declared with the `def` keyword:
335
344
def x = 42
336
345
```
337
346
338
-
Multiple variables can be declared in a single statement as long as the initializer is a [list literal](#list) with as many elements as declared variables:
347
+
Multiple variables can be declared in a single statement if the initializer is a [list literal](#list) with the same number of elements and declared variables:
339
348
340
349
```groovy
341
350
def (x, y) = [ 1, 2 ]
342
351
```
343
352
344
-
Every variable has a *scope*, which determines the region of code in which the variable is defined.
353
+
Each variable has a *scope*, which is the region of code in which the variable can be used.
345
354
346
355
Variables declared in a function, as well as the parameters of that function, exist for the duration of that function call. The same applies to closures.
347
356
348
357
Workflow inputs exist for the entire workflow body. Variables declared in the main section exist for the main, emit, and publish sections. Named outputs are not considered variable declarations and therefore do not have any scope.
349
358
350
-
Process input variables exist for the entire process body. Variables declared in the process script, shell, exec, and stub sections exist only in their respective section, with one exception -- in these sections, a variable can be declared with the `def` keyword, in which case it will also exist in the output section.
359
+
Process input variables exist for the entire process body. Variables declared in the process script, shell, exec, and stub sections exist only in their respective section, with one exception -- variables declared without the `def` keyword also exist in the output section.
351
360
352
361
Variables declared in an if or else branch exist only within that branch:
353
362
@@ -363,7 +372,7 @@ if( true )
363
372
println x
364
373
```
365
374
366
-
A variable cannot be declared with the same name as another variable in the same scope or any enclosing scope:
375
+
A variable cannot be declared with the same name as another variable in the same scope or an enclosing scope:
367
376
368
377
```groovy
369
378
def clash(x) {
@@ -385,15 +394,15 @@ map.key = 'value'
385
394
386
395
The target expression must be a [variable](#variable), [index](#binary-expressions), or [property](#binary-expressions) expression. The source expression can be any expression.
387
396
388
-
Multiple variables can be assigned in a single statement as long as the source expression is a [list literal](#list) with as many elements as assigned variables:
397
+
Multiple variables can be assigned in a single statement as long as the source expression is a [list literal](#list) with the same number of elements and assigned variables:
389
398
390
399
```groovy
391
400
(x, y) = [ 1, 2 ]
392
401
```
393
402
394
403
### Expression statement
395
404
396
-
Any [expression](#expressions) can also be a statement.
405
+
Any [expression](#expressions) can be a statement.
397
406
398
407
In general, the only expressions that can have any effect as expression statements are function calls that have side effects (e.g. `println`) or an implicit return statement (e.g. in a function or closure).
399
408
@@ -407,9 +416,9 @@ assert 2 + 2 == 4 : 'The math broke!'
407
416
408
417
If the condition is false, an error will be raised with the given error message.
409
418
410
-
### if / else
419
+
### if/else
411
420
412
-
An if/else statement consists of an *if branch* and an optional *else branch*. Each branch consists of a boolean expression in parentheses, followed by either a single statement or a *block statement* (one or more statements in curly braces).
421
+
An if/else statement consists of an *if branch* and an optional *else branch*. Each branch consists of a boolean expression in parentheses, followed by either a single statement or a *block statement* (one or more statements in curly braces). For example:
413
422
414
423
```groovy
415
424
def x = Math.random()
@@ -423,7 +432,7 @@ else {
423
432
424
433
If the condition is true, the if branch will be executed, otherwise the else branch will be executed.
425
434
426
-
If / else statements can be chained any number of times by making the else branch another if / else statement:
435
+
If/else statements can be chained any number of times by making the else branch another if/else statement:
427
436
428
437
```groovy
429
438
def grade = 89
@@ -439,7 +448,7 @@ else
439
448
println 'You failed.'
440
449
```
441
450
442
-
A more verbose way to write the same code would be:
451
+
A more verbose way to write the same code is:
443
452
444
453
```groovy
445
454
def grade = 89
@@ -500,7 +509,9 @@ def isEven2(n) {
500
509
}
501
510
```
502
511
503
-
Note that if the last statement is not a return or expression statement (implicit return), it is equivalent to appending an empty return.
512
+
:::{note}
513
+
If the last statement is not a return or expression statement (implicit return), it is equivalent to appending an empty return.
514
+
:::
504
515
505
516
### throw
506
517
@@ -517,9 +528,9 @@ error 'something failed!'
517
528
```
518
529
:::
519
530
520
-
### try / catch
531
+
### try/catch
521
532
522
-
A try / catch statement consists of a *try block* followed by any number of *catch clauses*:
533
+
A try/catch statement consists of a *try block* followed by any number of *catch clauses*:
523
534
524
535
```groovy
525
536
def text = null
@@ -531,7 +542,7 @@ catch( IOException e ) {
531
542
}
532
543
```
533
544
534
-
The try block will be executed, and if an error is raised and matches the expected error type of a catch clause, the code in that catch clause will be executed. If no catch clause is matched, the error will be raised to the next enclosing try / catch statement, or to the Nextflow runtime.
545
+
The try block will be executed, and if an error is raised and matches the expected error type of a catch clause, the code in that catch clause will be executed. If no catch clause is matched, the error will be raised to the next enclosing try/catch statement, or to the Nextflow runtime.
535
546
536
547
## Expressions
537
548
@@ -631,11 +642,13 @@ Logic unconfined.
631
642
/
632
643
```
633
644
634
-
Note that a slashy string cannot be empty because it would become a line comment.
645
+
:::{note}
646
+
A slashy string cannot be empty because it would become a line comment.
647
+
:::
635
648
636
649
### Dynamic string
637
650
638
-
Double-quoted strings can be interpolated using the `${}` placeholder, which can contain any expression:
651
+
Double-quoted strings can be interpolated using the `${}` placeholder with an expression:
639
652
640
653
```groovy
641
654
def names = ['Thing 1', 'Thing 2']
@@ -729,7 +742,7 @@ println [1, 2, 3].collect { v -> factor * v }
729
742
// -> [2, 4, 6]
730
743
```
731
744
732
-
And they can declare local variables that exist only for the lifetime of each closure invocation:
745
+
Closures can declare local variables that exist only for the lifetime of each closure invocation:
733
746
734
747
```groovy
735
748
def result = 0
@@ -742,7 +755,7 @@ println result
742
755
// -> 14
743
756
```
744
757
745
-
Refer to the {ref}`standard library <stdlib-page>` and {ref}`operator <operator-page>`reference pages for examples of closures being used in practice.
758
+
See {ref}`standard library <stdlib-page>` and {ref}`operator <operator-page>` for more examples of how closures are used in practice.
746
759
747
760
### Index expression
748
761
@@ -782,15 +795,15 @@ The argument list may contain any number of *positional arguments* and *named ar
782
795
file('hello.txt', checkIfExists: true)
783
796
```
784
797
785
-
The named arguments are collected into a map and provided as the first positional argument to the function. Thus the above function call can be rewritten as:
798
+
The named arguments are collected into a map and provided as the first positional argument to the function. The above function call can be rewritten as:
786
799
787
800
```groovy
788
801
file([checkIfExists: true], 'hello.txt')
789
802
```
790
803
791
804
The argument name must be an identifier or string literal.
792
805
793
-
When the function call is also an [expression statement](#expression-statement) and there is at least one argument, the parentheses can be omitted:
806
+
The parentheses can be omitted when the function call is also an [expression statement](#expression-statement) and there is at least one argument:
794
807
795
808
```groovy
796
809
// positional args
@@ -827,7 +840,7 @@ If the type is implicitly available in the script, the *fully-qualified type nam
827
840
new Date()
828
841
```
829
842
830
-
Refer to {ref}`stdlib-default-imports` for the set of types which are implicitly available in Nextflow scripts.
843
+
See {ref}`stdlib-default-imports` for the set of types which are implicitly available in Nextflow scripts.
0 commit comments