forked from ppoulin91/tractoinferno_tracking_flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
341 lines (286 loc) · 11.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
#!/usr/bin/env nextflow
import groovy.json.*
params.input = false
params.help = false
if(params.help) {
usage = file("$baseDir/USAGE")
cpu_count = Runtime.runtime.availableProcessors()
bindings = [
"pft_seeding_mask_type":"$params.pft_seeding_mask_type",
"pft_fa_seeding_mask_theshold":"$params.pft_fa_seeding_mask_theshold",
"pft_algo":"$params.pft_algo",
"pft_seeding":"$params.pft_seeding",
"pft_nbr_seeds":"$params.pft_nbr_seeds",
"pft_step":"$params.pft_step",
"pft_theta":"$params.pft_theta",
"pft_min_len":"$params.pft_min_len",
"pft_max_len":"$params.pft_max_len",
"pft_compress_streamlines":"$params.pft_compress_streamlines",
"pft_compress_value":"$params.pft_compress_value",
"local_seeding_mask_type":"$params.local_seeding_mask_type",
"local_fa_seeding_mask_theshold":"$params.local_fa_seeding_mask_theshold",
"local_tracking_mask_type":"$params.local_tracking_mask_type",
"local_fa_tracking_mask_theshold":"$params.local_fa_tracking_mask_theshold",
"local_compress_streamlines":"$params.local_compress_streamlines",
"pft_random_seed":"$params.pft_random_seed",
"local_seeding":"$params.local_seeding",
"local_nbr_seeds":"$params.local_nbr_seeds",
"local_step":"$params.local_step",
"local_theta":"$params.local_theta",
"local_sfthres":"$params.local_sfthres",
"local_sfthres_init":"$params.local_sfthres_init",
"local_min_len":"$params.local_min_len",
"local_max_len":"$params.local_max_len",
"local_compress_value":"$params.local_compress_value",
"local_det_random_seed":"$params.local_det_random_seed",
"local_prob_random_seed":"$params.local_prob_random_seed",
"cpu_count":"$cpu_count"]
engine = new groovy.text.SimpleTemplateEngine()
template = engine.createTemplate(usage.text).make(bindings)
print template.toString()
return
}
log.info "TractoInferno Tracking pipeline"
log.info "==================="
log.info ""
log.info "Start time: $workflow.start"
log.info ""
workflow.onComplete {
log.info "Pipeline completed at: $workflow.complete"
log.info "Execution status: ${ workflow.success ? 'OK' : 'failed' }"
log.info "Execution duration: $workflow.duration"
}
if (params.input){
log.info "Input: $params.input"
root = file(params.input)
// fODFs
Channel
.fromPath("$root/**/FODF_Metrics/*__fodf.nii.gz",
maxDepth: 2)
.map{[it.parent.parent.name, it]}
.into{fodf_for_pft_tracking; fodf_for_local_tracking; check_subjects_number}
// PFT Maps
pft_maps_for_pft_tracking = Channel
.fromFilePairs("$root/**/PFT_Maps/*{map_exclude.nii.gz,map_include.nii.gz}",
size:2,
maxDepth: 2,
flat: true) {it.parent.parent.name}
// WM mask
Channel
.fromPath("$root/**/Segment_Tissues/*__mask_wm.nii.gz",
maxDepth: 2)
.map{[it.parent.parent.name, it].flatten()}
.into{wm_mask_for_pft_tracking; wm_mask_for_local_tracking_mask; wm_mask_for_local_seeding_mask}
// FA map
Channel
.fromPath("$root/**/DTI_Metrics/*fa.nii.gz",
maxDepth: 2)
.map{[it.parent.parent.name, it].flatten()}
.into{fa_for_pft_tracking; fa_for_local_tracking_mask; fa_for_local_seeding_mask}
// PFT Interface mask
interface_for_pft_seeding_mask = Channel
.fromPath("$root/**/PFT_Maps/*interface.nii.gz",
maxDepth: 2)
.map{[it.parent.parent.name, it].flatten()}
// PFT seeding mask
wm_mask_for_pft_tracking
.join(fa_for_pft_tracking)
.join(interface_for_pft_seeding_mask)
.set{wm_fa_int_for_pft}
}
else {
error "Error ~ Please use --input for the input data."
}
check_subjects_number.count().into{ number_subj_for_null_check; number_subj_for_compare }
number_subj_for_null_check
.subscribe{a -> if (a == 0)
error "Error ~ No subjects found. Please check the naming convention, your --input path or your BIDS folder."}
if (params.pft_random_seed instanceof String){
pft_random_seed = params.pft_random_seed?.tokenize(',')
}
else{
pft_random_seed = params.pft_random_seed
}
if (params.local_det_random_seed instanceof String){
local_det_random_seed = params.local_det_random_seed?.tokenize(',')
}
else{
local_det_random_seed = params.local_det_random_seed
}
if (params.local_prob_random_seed instanceof String){
local_prob_random_seed = params.local_prob_random_seed?.tokenize(',')
}
else{
local_prob_random_seed = params.local_prob_random_seed
}
process README {
cpus 1
publishDir = params.Readme_Publish_Dir
tag = "README"
output:
file "readme.txt"
script:
String list_options = new String();
for (String item : params) {
list_options += item + "\n"
}
"""
echo "TractoFlow pipeline\n" >> readme.txt
echo "Start time: $workflow.start\n" >> readme.txt
echo "[Command-line]\n$workflow.commandLine\n" >> readme.txt
echo "[Git Info]\n" >> readme.txt
echo "$workflow.repository - $workflow.revision [$workflow.commitId]\n" >> readme.txt
echo "[Options]\n" >> readme.txt
echo "$list_options" >> readme.txt
"""
}
process PFT_Seeding_Mask {
cpus 1
input:
set sid, file(wm), file(fa), file(interface_mask) from wm_fa_int_for_pft
output:
set sid, "${sid}__pft_seeding_mask.nii.gz" into seeding_mask_for_pft
script:
if (params.pft_seeding_mask_type == "wm")
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
scil_image_math.py union $wm $interface_mask ${sid}__pft_seeding_mask.nii.gz\
--data_type uint8
"""
else if (params.pft_seeding_mask_type == "interface")
"""
mv $interface_mask ${sid}__pft_seeding_mask.nii.gz
"""
else if (params.pft_seeding_mask_type == "fa")
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
mrcalc $fa $params.pft_fa_seeding_mask_theshold -ge ${sid}__pft_seeding_mask.nii.gz
"""
}
fodf_for_pft_tracking
.join(pft_maps_for_pft_tracking)
.join(seeding_mask_for_pft)
.set{fodf_maps_for_pft_tracking}
process PFT_Tracking {
cpus 2
input:
set sid, file(fodf), file(exclude), file(include), file(seed)\
from fodf_maps_for_pft_tracking
each curr_seed from pft_random_seed
output:
file "${sid}__pft_tracking_${params.pft_algo}_${params.pft_seeding_mask_type}_seed_${curr_seed}.trk"
script:
compress =\
params.pft_compress_streamlines ? '--compress ' + params.pft_compress_value : ''
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
scil_compute_pft.py $fodf $seed $include $exclude\
${sid}__pft_tracking_${params.pft_algo}_${params.pft_seeding_mask_type}_seed_${curr_seed}.trk\
--algo $params.pft_algo --$params.pft_seeding $params.pft_nbr_seeds\
--seed $curr_seed --step $params.pft_step --theta $params.pft_theta\
--sfthres $params.pft_sfthres --sfthres_init $params.pft_sfthres_init\
--min_length $params.pft_min_len --max_length $params.pft_max_len\
--particles $params.pft_particles --back $params.pft_back\
--forward $params.pft_front $compress --sh_basis $params.basis
"""
}
wm_mask_for_local_tracking_mask
.join(fa_for_local_tracking_mask)
.set{wm_fa_for_local_tracking_mask}
process Local_Tracking_Mask {
cpus 1
input:
set sid, file(wm), file(fa) from wm_fa_for_local_tracking_mask
output:
set sid, "${sid}__local_tracking_mask.nii.gz" into tracking_mask_for_local
script:
if (params.local_tracking_mask_type == "wm")
"""
mv $wm ${sid}__local_tracking_mask.nii.gz
"""
else if (params.local_tracking_mask_type == "fa")
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
mrcalc $fa $params.local_fa_tracking_mask_theshold -ge ${sid}__local_tracking_mask.nii.gz
"""
}
wm_mask_for_local_seeding_mask
.join(fa_for_local_seeding_mask)
.set{wm_fa_for_local_seeding_mask}
process Local_Seeding_Mask {
cpus 1
input:
set sid, file(wm), file(fa) from wm_fa_for_local_seeding_mask
output:
set sid, "${sid}__local_seeding_mask.nii.gz" into tracking_seeding_mask_for_local
script:
if (params.local_seeding_mask_type == "wm")
"""
mv $wm ${sid}__local_seeding_mask.nii.gz
"""
else if (params.local_seeding_mask_type == "fa")
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
mrcalc $fa $params.pft_fa_seeding_mask_theshold -ge ${sid}__local_seeding_mask.nii.gz
"""
}
fodf_for_local_tracking
.join(tracking_mask_for_local)
.join(tracking_seeding_mask_for_local)
.into{fodf_maps_for_local_det_tracking; fodf_maps_for_local_prob_tracking}
process Local_Det_Tracking {
cpus 2
input:
set sid, file(fodf), file(tracking_mask), file(seed)\
from fodf_maps_for_local_det_tracking
each curr_seed from local_det_random_seed
output:
file "${sid}__local_tracking_det_${params.local_seeding_mask_type}_seeding_${params.local_tracking_mask_type}_mask_seed_${curr_seed}.trk"
script:
compress =\
params.local_compress_streamlines ? '--compress ' + params.local_compress_value : ''
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
scil_compute_local_tracking.py $fodf $seed $tracking_mask\
${sid}__local_tracking_det_${params.local_seeding_mask_type}_seeding_${params.local_tracking_mask_type}_mask_seed_${curr_seed}.trk\
--algo det --$params.local_seeding $params.local_nbr_seeds\
--seed $curr_seed --step $params.local_step --theta $params.local_theta\
--sfthres $params.local_sfthres --min_length $params.local_min_len\
--max_length $params.local_max_len $compress --sh_basis $params.basis
"""
}
process Local_Prob_Tracking {
cpus 2
input:
set sid, file(fodf), file(tracking_mask), file(seed)\
from fodf_maps_for_local_prob_tracking
each curr_seed from local_prob_random_seed
output:
file "${sid}__local_tracking_prob_${params.local_seeding_mask_type}_seeding_${params.local_tracking_mask_type}_mask_seed_${curr_seed}.trk"
script:
compress =\
params.local_compress_streamlines ? '--compress ' + params.local_compress_value : ''
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
scil_compute_local_tracking.py $fodf $seed $tracking_mask\
${sid}__local_tracking_prob_${params.local_seeding_mask_type}_seeding_${params.local_tracking_mask_type}_mask_seed_${curr_seed}.trk\
--algo prob --$params.local_seeding $params.local_nbr_seeds\
--seed $curr_seed --step $params.local_step --theta $params.local_theta\
--sfthres $params.local_sfthres --min_length $params.local_min_len\
--max_length $params.local_max_len $compress --sh_basis $params.basis
"""
}