-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
136 lines (107 loc) · 3.09 KB
/
main.nf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
splitter_jar = file("${moduleDir}/StreamSplitter/build/StreamSplitter.jar")
workflow scatter {
take:
data // meta, fastq
n // number of splits
scatterer // an optional splitting process
main:
if(scatterer)
scattered = scatterer.&run( data, n )
else
scattered = split_fastq( data, n, splitter_jar )
emit:
scattered
}
workflow mapper_wf_single {
take:
x
main:
mapper_process_single(x)
emit:
mapper_process_single.out
}
process mapper_process_single {
input:
tuple val(id), path(part)
output:
tuple val(id), path(part)
script:
"echo hi"
}
//keep
process gather_fastqs {
input:
tuple val(id), path("part*")
output:
tuple val(id), path("out")
script:
"cat part* > out"
}
workflow gather {
take:
x
gatherer // gather_fastqs
keyCounts // map id -> count
partIdKey // 'uuid'
main:
grouped = x.map{ k, partIdx, fq -> [ k, partIdx, fq ] }
.combine( keyCounts )
.map{ k, partIdx, fq, count -> [ groupKey( k, count[k] ), partIdx, fq ] }
.groupTuple()
.map{ k, indices, fqs ->
ordered_fqs = [ indices, fqs ].transpose()
.sort{ a_idx_fq, b_idx_fq -> a_idx_fq[0] <=> b_idx_fq[0] }
.collect{ idx, fq -> fq }
[ k, ordered_fqs ]
}
if( gatherer )
combined = gatherer.&run(grouped)
else
combined = gather_fastqs(grouped)
emit:
out = combined
}
// apply
// combine
// combine-key-fun: a function to apply to meta to use as an index for grouping prior to combining.
process split_fastq {
cpus 8
input:
tuple val(meta), path('in.fq.gz')
val(n_split)
path('StreamSplitter.jar')
output:
tuple( val(meta), path('split*'), emit: split )
script:
"""
java -jar StreamSplitter.jar --lines-per-record 4 --num-split ${n_split} --basename split --gunzip-input --gzip-output in.fq.gz
"""
}
workflow scattergather {
take:
x
n
mapper
options // map[ keyFun : ( meta -> id ) ]
main:
keyFun = options.keyFun ?: { meta -> meta.id }
def partIdKey = options.partIdKey ?: 'uuid'
keyCounts = x.map{ meta, fq -> keyFun.&call(meta) }
.reduce([:],{ acc, v ->
acc[v] = ( acc[v] ?: 0 ) + n
acc
})
keyToMeta = x.map{ meta, fq -> [ keyFun.&call(meta), meta ] }
scatter( x, n, options.scatterer )
to_map = scatter.out
.flatMap{ meta, parts ->
parts.withIndex().collect{ part, idx -> [ meta + [ (partIdKey): idx ], part ] }
}
mapper_out = mapper.&run( to_map )
gather( mapper_out.map{ meta, fq -> [ keyFun.&call(meta), meta[partIdKey], fq ] }, options.gatherer, keyCounts, partIdKey )
gathered = gather.out
.combine( keyToMeta, by: 0 )
.map{ id, fq, meta -> [ meta, fq ] }
emit:
gathered
}