-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.nf
376 lines (347 loc) · 10.7 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import java.util.stream.Collectors;
// Workaround for allowing comma separated list of rank upper/lower bounds
def rl = params.ranks?.split(',') as List
params.rankslist = rl.collect { it as int }
process biCVSplit {
// Shuffle data matrix and split into 9 submatrices for bi-cross validation
label 'largemem'
input:
path matrix
output:
path 'shuffle_*.npz'
script:
"""
cva_split.py -n $params.num_shuffles -m $matrix -s $params.seed
"""
stub:
"""
for i in \$(seq 1 10);
do
touch shuffle_${i}.npz
done
"""
}
// Rank selection
process rankBiCv {
// Perform bi-fold cross validation for a given rank and matrix
// When multiple inputs, each input must come from a different channel
// Here this is okay, as we will make a channel which emits the desired
// ranks to search.
input:
tuple val(rank), path(x)
// The results file has all the params in it, so process which joins
// can use that instead
output:
path('results.pickle')
script:
"""
cva_rank.py \
--folds $x \
--seed $params.seed \
--rank $rank \
--max_iter $params.max_iter \
--beta_loss $params.beta_loss \
--init $params.init \
--verbose
"""
stub:
"""
touch results.pickle
"""
}
process rankCombineScores {
label 'largemem'
input:
path(results, stageAs: "results*.pickle")
// We will publish this as it is one of the primary outputs
publishDir { params.publish_dir }, mode: 'copy', overwrite: true
// The output file here will have the rank as a column, so we can just pass
// this to a collector process to concatenate without knowledge of the rank
output:
path "rank_combined.pickle", emit: pickle
script:
"""
cva_rank_combine.py $results
"""
stub:
"""
touch "rank_combined.pickle"
"""
}
process publishAnalysis {
label 'largemem'
input:
path "rank_analysis.ipynb"
path "rank_combined.pickle"
output:
path "rank_analysis.ipynb"
path "rank_analysis.html"
path "rank_analysis.tsv"
publishDir { params.publish_dir }, mode: 'copy', overwrite: true
script:
"""
jupyter nbconvert --execute --to html rank_analysis.ipynb
"""
}
process publishRankDecompositions {
label 'largememandcpu'
input:
path "rank_decomposition.ipynb"
path data
val rank
output:
path "decomposition.ipynb"
path "decomposition.html"
path("model", type: "dir")
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
papermill rank_decomposition.ipynb decomposition.ipynb \
-p data ${data} -p rank ${rank} -p top_n 1 \
-p seed ${params.seed} -p max_iter ${params.max_iter}
jupyter nbconvert --to html decomposition.ipynb
"""
}
// Regularisation selection
process reguBiCv {
// Perform bi-fold cross validation for a given rank and matrix
// When multiple inputs, each input must come from a different channel
// Here this is okay, as we will make a channel which emits the desired
// ranks to search.
input:
tuple val(rank), val(alpha), path(x)
// The results file has all the params in it, so process which joins
// can use that instead
output:
tuple val(rank), path('results.pickle')
script:
"""
cva_alpha.py \
--folds $x \
--seed $params.seed \
--rank $rank \
--alpha $alpha \
--max_iter $params.max_iter \
--init $params.init \
--beta_loss $params.beta_loss \
--verbose
"""
stub:
"""
touch results.json
"""
}
process reguCombineScores {
label 'largemem'
input:
tuple val(rank), path(results, stageAs: "results*.pickle")
output:
tuple val(rank), path("regu_combined.pickle")
script:
"""
cva_rank_combine.py -g alpha $results
"""
stub:
"""
touch "regu_combined.pickle"
"""
}
process reguPublishAnalysis {
label 'largemem'
input:
path "regu_analysis.ipynb"
tuple val(rank), path("regu_combined.pickle")
output:
path "regu_analysis.ipynb"
path "regu_analysis.html"
path "regu_analysis.tsv"
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
jupyter nbconvert --execute --to html regu_analysis.ipynb
"""
}
process reguPublishDecomposition {
label 'largememandcpu'
input:
path matrix
tuple val(rank), path(regu_res)
output:
tuple val(rank), path("regularised_model", type: "dir")
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
cva_decompose.py \
--matrix $matrix \
--regu_res $regu_res \
--output regularised_model \
--seed $params.seed \
--rank $rank \
--max_iter $params.max_iter \
--l1_ratio $params.l1_ratio \
--random_starts $params.random_starts \
--beta_loss $params.beta_loss \
--init $params.init \
--stability false
"""
}
process reguPublishNotebook {
input:
path notebook
tuple val(rank), path(model)
output:
path "regularised_decomposition.ipynb"
path "regularised_decomposition.html"
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
papermill ${notebook} regularised_decomposition.ipynb \
-p model ${model}
jupyter nbconvert --to html regularised_decomposition.ipynb
"""
}
// Non-regularised decompositions on full matrix
process publishDecomposition {
label 'largememandcpu'
input:
path matrix
val(rank)
output:
tuple val(rank), path("model", type: "dir")
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
cva_decompose.py \
--matrix $matrix \
--output model \
--seed $params.seed \
--rank $rank \
--max_iter $params.max_iter \
--l1_ratio $params.l1_ratio \
--random_starts $params.random_starts \
--beta_loss $params.beta_loss \
--init $params.init \
--stability $params.stability
"""
}
process publishDecompositionNotebook {
input:
path notebook
tuple val(rank), path(model)
output:
path "decomposition.ipynb"
path "decomposition.html"
publishDir { "${params.publish_dir}/${rank}" }, mode: 'copy', overwrite: true
script:
"""
papermill ${notebook} decomposition.ipynb \
-p model ${model}
jupyter nbconvert --to html decomposition.ipynb
"""
}
process rankCombineStability {
input:
path(model, arity: '1..*', stageAs: "model*")
path notebook
output:
// path "stability_rank_analysis.ipynb"
path "stability_rank_analysis.tsv"
path "stability_rank_analysis.html"
publishDir "${params.publish_dir}", mode: 'copy', overwrite: true
script:
"""
stab_combine.py ${model}
jupyter nbconvert --execute --to html ${notebook} \
--output stability_rank_analysis
"""
}
workflow bicv_regu {
take:
shuffles
main:
// CHANNELS
// Ranks to search across
rank_channel = Channel.from(params.regu_rank)
// Alphas to search across
alpha_channel = Channel.from(params.alpha)
// Make a channel which is a combination of rank, alpha and shuffle
regu_sel_channel = rank_channel
.combine(alpha_channel)
.combine(shuffles.flatten())
// PROCESSES
// Perform BiCV on each shuffle for each rank
// Map to make sure that the second element of the tuple is a list of files,
// rather than all being flattened into a single list
bicv_res = reguBiCv(regu_sel_channel)
// Group runs on the same rank together
grpd_regu_res = bicv_res.groupTuple(
by: 0,
size: params.alpha.size()*params.num_shuffles,
remainder: true
)
comb_res = reguCombineScores(grpd_regu_res)
// Add the analysis notebook to the output
reguPublishAnalysis(file("${projectDir}/resources/cva_regu_analysis.ipynb"), comb_res)
// Make a regularised decomposition for each rank requested
decomp_channel = reguPublishDecomposition(file(params.matrix), comb_res)
reguPublishNotebook(
file("${projectDir}/resources/rank_decomposition.ipynb"),
decomp_channel
)
}
workflow bicv_rank {
take:
shuffles
main:
rank_channel = Channel.of(params.rankslist.get(0)..params.rankslist.get(1))
// PROCESSES
// Perform BiCV on each shuffle for each rank
// Map to make sure that the second element of the tuple is a list of files,
// rather than all being flattened into a single list
bicv_res = rankBiCv(
rank_channel
.combine(shuffles.flatten())
)
// Combine results
bicv_comb = rankCombineScores(bicv_res.collect())
publishAnalysis(file("${projectDir}/resources/cva_rank_analysis.ipynb"), bicv_comb)
}
workflow publish_rank {
main:
rank_channel = Channel.of(params.rankslist.get(0)..params.rankslist.get(1))
// Trying moving decomposition generation out of the notebook, as
// the kernel seems to die with larger decompositions
decomp_channel = publishDecomposition(file(params.matrix), rank_channel)
publishDecompositionNotebook(
file("${projectDir}/resources/rank_decomposition.ipynb"),
decomp_channel
)
// If stability rank selection coefficients have been calculated, combine
// them to make a rank selection report based on stability.
if ( params.stability ) {
model_only_channel = decomp_channel
.map{ it.get(1) }
.collect()
rankCombineStability(
model_only_channel,
file("${projectDir}/resources/stab_rank_analysis.ipynb")
)
}
}
workflow {
// The input data - value channel so it can be consumed endlessly
data_channel = file(params.matrix)
// Shuffle and split matrix n times
splits = biCVSplit(data_channel)
// Regularisation selection
bicv_regu(splits)
// Rank selection
bicv_rank(splits)
// Making decompositions for all the target ranks
// publishRankDecompositions(
// file("${projectDir}/resources/rank_decomposition.ipynb"),
// data_channel,
// Channel.of(params.rankslist.get(0)..params.rankslist.get(1))
// )
publish_rank()
}