forked from jerdra/TIGR_PURR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recalculate_confounds.nf
340 lines (258 loc) · 9.79 KB
/
recalculate_confounds.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
nextflow.preview.dsl = 2
usage = file("${workflow.scriptFile.getParent()}/usage/recalculate_confounds")
engine = new groovy.text.SimpleTemplateEngine()
bindings = ["fmriprep": params.fmriprep,
"fmriprep_img": params.fmriprep_img,
"subjects": params.subjects,
"rewrite": params.rewrite,
"fmriprep_img":params.fmriprep_img,
"dump_masks":params.dump_masks
]
toprint = engine.createTemplate(usage.text).make(bindings)
printhelp = params.help
req_param = ["--fmriprep": params.fmriprep]
missing_arg = req_param.grep{ (it.value == null || it.value == "") }
if (missing_arg){
log.error("Missing required arguments(s)!")
missing_arg.each{ log.error("Missing ${it.key}") }
printhelp = true
}
if (printhelp){
print(toprint)
System.exit(0)
}
process denoise_image{
label 'fmriprep'
input:
tuple val(sub), path(t1w), path(mask)
output:
tuple val(sub), path("${sub}_desc-denoised.nii.gz"), emit: denoised
shell:
'''
DenoiseImage -d 3 -i !{t1w} -x !{mask} -o !{sub}_desc-denoised.nii.gz
'''
}
process apply_mask{
label 'fmriprep'
input:
tuple val(sub), path(t1w), path(mask)
output:
tuple val(sub), path("${sub}_desc-masked.nii.gz"), emit: masked
shell:
'''
fslmaths !{t1w} -mas !{mask} !{sub}_desc-masked.nii.gz
'''
}
process fast{
label 'fmriprep'
input:
tuple val(sub), path(t1w)
output:
tuple val(sub),\
path("${sub}_desc-prob_2.nii.gz"), path("${sub}_desc-prob_0.nii.gz"), emit: tpm
shell:
'''
fast -p -g --nobias -o t1 !{t1w}
rename "s/t1_/!{sub}_desc-/g" t1*
'''
}
process gen_confounds{
label 'fmriprep'
input:
tuple val(sub), val(ses),\
path(t1), path(t1_bm),\
path(wm), path(csf),\
path(func), path(func_bm), path(func_json),\
val(base)
output:
tuple val(sub), val(base), path("${base}_new_confounds.tsv"), emit: confounds
tuple val(sub), val(base), path("${base}_new_confounds.json"), emit: confounds_metadata
tuple val(sub), val(base), path("${base}_wm_roi.nii.gz"), emit: wm
tuple val(sub), val(base), path("${base}_csf_roi.nii.gz"), emit: csf
tuple val(sub), val(base), path("${base}_acc_roi.nii.gz"), emit: acc
shell:
'''
PYTHONPATH=/scripts
/scripts/confounds.py $(pwd)/!{t1} $(pwd)/!{t1_bm} $(pwd)/!{wm} $(pwd)/!{csf} \
$(pwd)/!{func} $(pwd)/!{func_bm} $(pwd)/!{func_json} \
--workdir $(pwd) $(pwd)/!{base}
rename 's/_confounds/_new_confounds/g' *confounds*
'''
}
process update_confounds{
label 'fmriprep'
input:
tuple val(sub), val(base), val(ses),\
path(new_confounds), path(confounds)
output:
tuple val(sub), val(base), val(ses),\
path("${base}_merged_confounds.tsv"), emit: confounds
shell:
'''
#!/usr/bin/env python
import numpy as np
import pandas as pd
# Load in TSV files
old_tsv = pd.read_csv("!{confounds}", delimiter="\t")
new_tsv = pd.read_csv("!{new_confounds}", delimiter="\t")
# Calculate derivates and powers
deriv_cols = ["white_matter", "csf"]
for d in deriv_cols:
deriv_name = d + "_derivative1"
sq_name = d + "_power2"
deriv_sq_name = d + "_derivative1_power2"
new_tsv[deriv_name] = new_tsv[d].diff()
new_tsv[sq_name] = new_tsv[d] ** 2
new_tsv[deriv_sq_name] = new_tsv[deriv_name]**2
# Rename heads in
cols = new_tsv.columns
new_tsv.columns = ["{}_fixed".format(c) for c in cols]
# Drop component based columns in old CSV
drop_cols = [c for c in old_tsv.columns if "a_comp_cor" in c.lower()]
old_tsv.drop(columns = drop_cols, inplace=True)
# Drop columns in old tsv
old_tsv.drop(columns = cols, inplace=True, errors="ignore")
out = old_tsv.merge(new_tsv, left_index=True, right_index=True)
out.to_csv("!{base}_merged_confounds.tsv", sep="\\t", index=False)
'''
}
process dump_masks{
publishDir path: "$params.dump_masks",\
pattern: "*.nii.gz",\
mode: 'copy'
input:
tuple val(sub), val(base),\
path(csf), path(wm), path(acc)
output:
tuple path(csf), path(wm), path(acc)
shell:
'''
echo "Dumping !{base} ROIs into !{params.dump_masks}"
'''
}
process update_metadata{
label 'fmriprep'
input:
tuple val(sub), val(base), val(ses),\
path(new_meta), path(meta)
output:
tuple val(sub), val(base), val(ses),\
path("${base}_merged_confounds.json"), emit: metadata
shell:
'''
#!/usr/bin/env python
import json
with open("!{meta}", "r") as f:
meta = json.load(f)
with open("!{new_meta}", "r") as f:
new_meta = json.load(f)
# Remove columns being replaced
cleaned_meta = {k: v for k,v in meta.items() if not (("a_comp" in k) or ("dropped" in k))}
out_meta = {**cleaned_meta, **new_meta}
with open("!{base}_merged_confounds.json", "w") as f:
json.dump(out_meta, f, indent=2)
'''
}
process write_to_fmriprep{
label 'fmriprep'
stageInMode 'copy'
publishDir path: "$params.fmriprep/$sub/$ses/func",\
pattern: "*tsv",\
saveAs: { f -> "${base}_desc-confounds_fixedregressors.tsv" },\
mode: 'copy'
publishDir path: "$params.fmriprep/$sub/$ses/func",\
pattern: "*json",\
saveAs: { f -> "${base}_desc-confounds_fixedregressors.json" },\
mode: 'copy'
input:
tuple val(sub), val(base), val(ses),\
path(confounds), path(metadata)
output:
tuple path(confounds), path(metadata)
shell:
'''
echo "Writing confounds to !{params.fmriprep}/!{sub}/!{ses}/func/!{base}_desc-confound_fixedregressors.tsv"
echo "Writing confounds to !{params.fmriprep}/!{sub}/!{ses}/func/!{base}_desc-confound_fixedregressors.json"
'''
}
// Implement logic to filter subjects
input_channel = Channel.fromPath("$params.fmriprep/sub-*", type: "dir")
.map{ i -> [i.getBaseName(), i] }
// Filter subjects
if (params.subjects){
subjects_channel = Channel.fromPath(params.subjects)
.splitText(){it.strip()}
input_channel = input_channel.join(subjects_channel)
}
def remove_desc = ~/_desc.*/
def remove_space = ~/_space-\p{Alnum}+_?/
def ses_from_bids = ~/(?<=_)ses-.*?(?=_)/
workflow {
// Structural inputs
t1_channel = input_channel.map{i,f -> [i,"$f/anat/${i}_desc-preproc_T1w.nii.gz"]}
mask_channel = input_channel.map{i,f -> [i,"$f/anat/${i}_desc-brain_mask.nii.gz"]}
// Denoise the image, yielding a denoised T1 unmasked
i_denoise_image = t1_channel.join(mask_channel)
denoise_image(i_denoise_image)
// Apply mask to denoised image
i_apply_mask = denoise_image.out.denoised.join(mask_channel)
apply_mask(i_apply_mask)
// Run fast
fast(apply_mask.out.masked)
// There's probably a better way to gather inputs for the confounds file
// Put together inputs for running confound calculation
i_gen_confounds = input_channel
.join(denoise_image.out.denoised)
.join(mask_channel)
.join(fast.out.tpm)
.map{s,f,t,tbm,wm,csf ->
[ s,f,t,tbm,wm,csf,
file("${f}/ses-*/func/${s}_*space-T1w*{preproc,mask}*.nii.gz")]}
.transpose()
.map{s,f,t,tbm,wm,csf,fmri ->
[s,t,tbm,wm,csf,fmri.getName() - remove_desc, fmri]
}
.groupTuple(by: [0,1,2,3,4,5], size: 2)
.map{sub,t,tbm,wm,csf,k,fmri->
[sub,
(k =~ ses_from_bids)[0],
t,tbm,wm,csf,
fmri.sort{a,b -> b.getBaseName()<=>a.getBaseName()},
k - remove_space].flatten()
}
.map{sub,ses,t,tbm,wm,csf,fmri,fbm,base ->
[
sub,ses,t,tbm,wm,csf,fmri,fbm,
fmri.toString().replaceFirst(/nii.gz/, "json"),
base
]}
gen_confounds(i_gen_confounds)
// Operations to pull off:
// 1 - A tag identification mark
// Next extract the confounds file
basenames = i_gen_confounds.map{sub,ses,t,tbm,wm,csf,fmri,fbm,js,base ->
[sub,base,ses,
"${params.fmriprep}/${sub}/${ses}/func/"]
}
i_update_confounds = basenames.join(gen_confounds.out.confounds, by: [0,1])
.map{sub,base,ses,funcp,conf ->
[sub,base,ses,conf,
"${funcp}/${base}_desc-confounds_regressors.tsv"
]}
update_confounds(i_update_confounds)
i_update_metadata = basenames.join(gen_confounds.out.confounds_metadata, by: [0,1])
.map{sub,base,ses,funcp,meta ->
[sub,base,ses,meta,
"${funcp}/${base}_desc-confounds_regressors.json"
]}
update_metadata(i_update_metadata)
i_write_to_fmriprep = update_confounds.out.confounds
.join(update_metadata.out.metadata, by: [0,1,2])
write_to_fmriprep(i_write_to_fmriprep)
if (params.dump_masks){
i_dump_masks = gen_confounds.out.csf
.join(gen_confounds.out.wm, by: [0,1])
.join(gen_confounds.out.acc, by: [0,1])
dump_masks(i_dump_masks)
}
}