-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfieldmaps.nf
291 lines (216 loc) · 8.38 KB
/
fieldmaps.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
usage = file("${baseDir.getParent()}/usage/fieldmaps_usage"
bindings = [ "rewrite":"$params.rewrite",
"echo1":"$params.echo1",
"echo2":"$params.echo2"]
engine = new groovy.text.SimpleTemplateEngine()
toprint = engine.createTemplate(usage.text).make(bindings)
printhelp = params.help
if (!params.study || !params.out){
println("Insufficient specification")
println("Both --study and --out are required!")
printhelp = true
}
if (params.subjects) {
println("Subject file provided: $params.subjects")
}
if (printhelp){
print(toprint)
System.exit(0)
}
// Main processes
//nifti directory
println("Output directory: $params.out")
nifti_dir="/archive/data/$params.study/$params.nii"
//Inputs
input_sessions_dir = new File(nifti_dir)
input_sessions = input_sessions_dir.list()
//Outputs
output_sessions_dir = new File("$params.out/fieldmaps")
output_sessions = output_sessions_dir.list()
//Get subjects
if (params.subjects){
to_run = new File(params.subjects).readLines()
}else{
to_run = input_sessions
}
//Filter subjects based on outputs if rewrite isn't specified
if (!params.rewrite && output_sessions) {
to_run = to_run.findAll { !(output_sessions.contains(it)) }
}
//Filter for unavailable subjects if specified
if (params.subjects){
//Pull non-empty inputs
input_sub_channel = Channel.from(to_run)
.filter{ (it?.trim()) }
process split_invalid{
publishDir "$params.out/pipeline_logs/$params.application/", \
mode: 'copy', \
saveAs: { 'invalid_subjects.log' }, \
pattern: 'invalid'
input:
val subs from input_sub_channel.collect()
val available_subs from Channel.from(input_sessions).collect()
output:
file 'valid' into valid_subs
file 'invalid' optional true into invalid_subs
"""
#!/usr/bin/env python
import os
print(os.getcwd())
def nflist_2_pylist(x):
x = x.strip('[').strip(']')
x = [x.strip(' ').strip("\\n") for x in x.split(',')]
return x
#Process full BIDS subjects
bids_subs = nflist_2_pylist("$available_subs")
input_subs = nflist_2_pylist("$subs")
print(input_subs)
valid_subs = [x for x in input_subs if x in bids_subs]
invalid_subs = [x for x in input_subs if x not in valid_subs]
with open('valid','w') as f:
f.writelines("\\n".join(valid_subs))
if invalid_subs:
with open('invalid','w') as f:
f.writelines("\\n".join(invalid_subs))
f.write("\\n")
"""
}
input_subs = valid_subs
.splitText() { it.strip() }
}else{
input_subs = Channel.from(to_run)
}
//Pull subjects and scans with ECHO ExportInfo tag
//And process into pairs
fieldmap_input = input_subs
.map { n -> [
n,
new File("$nifti_dir/$n").list()
.findAll { it.matches(".*${params.echo1}.*.nii.gz") }
.sort(),
new File("$nifti_dir/$n").list()
.findAll { it.matches(".*${params.echo2}.*.nii.gz") }
.sort()
] }
.filter { !it[1].isEmpty() }
.transpose()
.map { x,y,z -> [
x,
new File("$nifti_dir/$x/$y").toPath(),
new File("$nifti_dir/$x/$z").toPath()
] }
// Resample if needed
process resample {
stageInMode 'copy'
module "FSL/5.0.11"
input:
set val(sub), file(echo1), file(echo2) from fieldmap_input
output:
set val(sub), file(echo1), file(echo2) into resampled_fieldmaps
shell:
'''
#!/bin/bash
THRES=0.0001
# Grab info from image 1
img1_x=$(fslinfo !{echo1} | grep pixdim1 | awk '{print $2}')
img1_y=$(fslinfo !{echo1} | grep pixdim2 | awk '{print $2}')
img1_z=$(fslinfo !{echo1} | grep pixdim3 | awk '{print $2}')
img1_voxarea=$(echo "$img1_x*$img1_y*$img1_z" | bc -l)
# Grab info from image 1
img2_x=$(fslinfo !{echo2} | grep pixdim1 | awk '{print $2}')
img2_y=$(fslinfo !{echo2} | grep pixdim2 | awk '{print $2}')
img2_z=$(fslinfo !{echo2} | grep pixdim3 | awk '{print $2}')
img2_voxarea=$(echo "$img2_x*$img2_y*$img2_z" | bc -l)
#Calculate difference
diff=$(echo "$img1_voxarea - $img2_voxarea" | bc -l)
#Check image areas then downsample if needed
if (( $(echo "$diff > $THRES" | bc -l) )); then
in=!{echo2}
ref=!{echo1}
elif (( $(echo "$diff < -1*$THRES" | bc -l) )); then
in=!{echo1}
ref=!{echo2}
else
exit 0
fi
#Split images then generate transforms
fslsplit $in image1_ -t
fslsplit $ref image2_ -t
#Resample image
flirt -in image1_0000.nii.gz -ref image2_0000.nii.gz -omat resamp_mat
#Apply transformation
flirt -in $in -ref image2_0000.nii.gz -applyxfm -init resamp_mat -out transformed.nii.gz
#Replace
mv transformed.nii.gz $in
'''
}
// With list of inputs (sub,echo1,echo2) apply fieldmap processing!
process fieldmaps {
module "FSL/5.0.11"
publishDir "$params.out/${params.application}/$sub", \
mode: 'copy', \
pattern: "magnitude.nii.gz" , \
saveAs: { echo1.getName().replace("$params.echo1","MAG") }
publishDir "$params.out/${params.application}/$sub", \
mode: 'copy', \
pattern: "fieldmap.nii.gz" , \
saveAs: { echo1.getName().replace("$params.echo1","FIELDMAP") }
publishDir "$params.out/${params.application}/$sub", \
mode: 'copy', \
pattern: "json", \
saveAs: { echo1.getName().replace("$params.echo1","FIELDMAP").replace('.nii.gz','.json') }
input:
set val(sub), file(echo1), file(echo2) from resampled_fieldmaps
output:
set val(sub), file("fieldmap.nii.gz"), file("magnitude.nii.gz"), file("json") into fieldmap_output
shell:
'''
#!/bin/bash
#Set up logging
logging_dir=!{params.out}/pipeline_logs/!{params.application}
mkdir -p ${logging_dir}
#Get processID
pid=$$
log_out=${logging_dir}/!{sub}_${pid}.out
log_err=${logging_dir}/!{sub}_${pid}.err
echo "TASK ATTEMPT !{task.attempt}" >> ${log_out}
echo "============================" >> ${log_out}
echo "TASK ATTEMPT !{task.attempt}" >> ${log_err}
echo "============================" >> ${log_err}
FM65=!{echo1}
FM85=!{echo2}
echo "Using ECHO1 $FM65" >> ${log_out}
echo "Using ECHO2 $FM85" >> ${log_out}
####split (pre) fieldmap files and log
(
fslsplit ${FM65} split65 -t
bet split650000 65mag -R -f 0.5 -m
fslmaths split650002 -mas 65mag_mask 65realm
fslmaths split650003 -mas 65mag_mask 65imagm
fslsplit ${FM85} split85 -t
bet split850000 85mag -R -f 0.5 -m
fslmaths split850002 -mas 85mag_mask 85realm
fslmaths split850003 -mas 85mag_mask 85imagm
####calc phase difference
fslmaths 65realm -mul 85realm realeq1
fslmaths 65imagm -mul 85imagm realeq2
fslmaths 65realm -mul 85imagm imageq1
fslmaths 85realm -mul 65imagm imageq2
fslmaths realeq1 -add realeq2 realvol
fslmaths imageq1 -sub imageq2 imagvol
####create complex image and extract phase and magnitude
fslcomplex -complex realvol imagvol calcomplex
fslcomplex -realphase calcomplex phasevolume 0 1
fslcomplex -realabs calcomplex magnitude 0 1
####unwrap phase
prelude -a 65mag -p phasevolume -m 65mag_mask -o phasevolume_maskUW
####divide by TE diff in seconds -> radians/sec
fslmaths phasevolume_maskUW -div 0.002 fieldmap
####copy in geometry information
fslcpgeom ${FM65} fieldmap.nii.gz -d
fslcpgeom ${FM65} magnitude.nii.gz -d
) 2>> ${log_err} 1>> ${log_out}
####make a JSON file containing the units
echo ' { "Units": "rad/s" } ' > json
'''
}