Skip to content

Commit 111a57c

Browse files
committed
Add example and exercise for map
1 parent c1fa34e commit 111a57c

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

docs/nextflow/building_blocks.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,29 @@ b
194194
z
195195
```
196196

197+
- `map`: e.g. when you would like to run your own function on each item in a channel.
198+
- The map operator is expressed as a [closure](https://www.nextflow.io/docs/latest/script.html#script-closure) (`{ ... }`)
199+
- By default, the items in the channel are referenced by the variable `it`. This can be changed by using the `map { item -> ... }` syntax.
200+
- All functions available on the item, are available on the `it` variable within the closure.
201+
- When an element is a list or tuple, you can use the `it[0]`, `it[1]`, etc. syntax to access the individual elements of your item.
202+
203+
```
204+
Channel
205+
.of( 1, 2, 3, 4, 5 )
206+
.map { it * it }
207+
.subscribe onNext: { println it }, onComplete: { println 'Done' }
208+
209+
# outputs
210+
1
211+
4
212+
9
213+
16
214+
25
215+
Done
216+
```
197217

198218
````{tab} Exercise 1.2
199-
Create a channel from a csv-file (`input.csv`). Generate the channel for the `input.csv`-file which you can find in the `exercises/01_building_blocks/` folder and contains the following content:
219+
Create a channel from a csv-file (`input.csv`) and use an operator to view the contents. Generate the channel for the `input.csv`-file which you can find in the `exercises/01_building_blocks/` folder and contains the following content:
200220
201221
| sampleId | Read 1 | Read 2 |
202222
|----------|-------------------------------|-------------------------------|
@@ -218,10 +238,17 @@ samples_ch = Channel
218238
.splitCsv(header:true)
219239
```
220240
221-
Additionally, you can use the following expression to view the contents in a clean way:
222-
```
223-
samples_ch.view{ row -> tuple(row.sampleId, file(row.forward_read), file(row.reverse_read)) }
224-
```
241+
````
242+
---
243+
244+
````{tab} Exercise 1.3
245+
Building on exercise 1.2 and using the `map` operator, create 2 channels, one containing the sampleId and the forward read as a tuple and the second containg the sampleId and reverse read as a tuple. Use the `view` operator to inspect the contents of thsee channels.
246+
247+
````
248+
249+
````{tab} Solution 1.3
250+
The solution is available in the file `exercises/01_building_blocks/solutions/1.3_template-csv-map.nf`
251+
225252
````
226253

227254
---
@@ -405,10 +432,10 @@ executor > local (10)
405432
[4b/aff57f] process > whosfirst (10) [100%] 10 of 10
406433
```
407434

408-
````{tab} Exercise 1.3
435+
````{tab} Exercise 1.4
409436
A `tag` directive can be added at the top of the process definition and allows you to associate each process execution with a custom label. Hence, it is really useful for logging or debugging. Add a tag for `num` and `str` in the process of the script `exercises/01_building_blocks/firstscript.nf` and inspect the output.
410437
````
411-
````{tab} Solution 1.3
438+
````{tab} Solution 1.4
412439
The process should be adapted, containing the following tag line in the directives.
413440
```
414441
// Defining the process that is executed

exercises/01_building_blocks/solutions/1.2_template-csv.nf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ samples_ch = Channel
77

88
// Inspect a channels contents with the operator .view()
99
samples_ch.view()
10-
samples_ch.view{ row -> tuple(row.sampleId, file(row.forward_read), file(row.reverse_read)) }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env nextflow
2+
3+
// Create the channels
4+
samples_ch = Channel
5+
.fromPath('exercises/01_building_blocks/input.csv')
6+
.splitCsv(header:true)
7+
8+
// Inspect a channels contents with the operator .view()
9+
samples_ch.view()
10+
samples_ch.view{ row -> tuple(row.sampleId, file(row.forward_read), file(row.reverse_read)) }
11+
12+
read1_ch = samples_ch.map{ row -> tuple(row.sampleId, file(row.forward_read)) }
13+
read2_ch = samples_ch.map{ row -> tuple(row.sampleId, file(row.reverse_read)) }
14+
15+
read1_ch.view()
16+
read2_ch.view()

0 commit comments

Comments
 (0)