-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcortex-mapping-pipeline.sh
executable file
·443 lines (378 loc) · 13.5 KB
/
cortex-mapping-pipeline.sh
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
## This script will create a midthickness surface, map tensor and NODDI values to this surface, and compute stats for each ROI from Freesurfer parcellation
set -x
set -e
#### Set cores ####
OMP_NUM_THREADS=8
#### make directory and copy metric files to folder ####
echo "making directories"
mkdir cortexmap ./cortexmap/cortexmap/
mkdir metric
mkdir ./cortexmap/cortexmap/label
mkdir ./cortexmap/cortexmap/func
mkdir raw
echo "making directories complete"
#### Variables ####
# parse inputs
echo "parsing inputs"
freesurfer=`jq -r '.freesurfer' config.json`
dwi=`jq -r '.dwi' config.json`
fa=`jq -r '.fa' config.json`
ad=`jq -r '.ad' config.json`
md=`jq -r '.md' config.json`
rd=`jq -r '.rd' config.json`
ndi=`jq -r '.ndi' config.json`
isovf=`jq -r '.isovf' config.json`
odi=`jq -r '.odi' config.json`
warp=`jq -r '.warp' config.json`
inv_warp=`jq -r '.inverse_warp' config.json`
fsurfparc=`jq -r '.fsurfparc' config.json`
echo "parsing inputs complete"
# set sigmas
echo "calculating sigma to use from dwi dimensions"
diffRes="`fslval ${dwi} pixdim1 | awk '{printf "%0.2f",$1}'`"
MappingFWHM="` echo "$diffRes * 2.5" | bc -l`"
MappingSigma=` echo "$MappingFWHM / ( 2 * ( sqrt ( 2 * l ( 2 ) ) ) )" | bc -l`
SmoothingFWHM=$diffRes
SmoothingSigma=` echo "$SmoothingFWHM / ( 2 * ( sqrt ( 2 * l ( 2 ) ) ) )" | bc -l`
echo "sigma set to ${MappingSigma}"
# set hemisphere labels
echo "set hemisphere labels"
HEMI="lh rh"
CARETHemi="L R"
echo "hemisphere labels set"
# set other variables for ease of scripting
echo "setting useful variables"
if [[ ! ${warp} == 'null' ]]; then
SPACES="native mni"
SPACES_DIR=("./cortexmap/cortexmap/surf" "./cortexmap/cortexmap/surf/mni")
else
SPACES="native"
SPACES_DIR=("./cortexmap/cortexmap/surf/")
fi
for spaces in ${SPACES_DIR[*]}
do
mkdir -p ${spaces}
done
FUNC_DIR=("./cortexmap/cortexmap/func/")
surfs="pial.surf.gii white.surf.gii"
echo "variables set"
# parse whether dti and NODDI are included or not
echo "parsing input diffusion metrics"
if [[ $fa == "null" ]];
then
METRIC="ndi isovf odi"
elif [[ $ndi == "null" ]]; then
METRIC="ad fa md rd"
else
METRIC="ad fa md rd ndi isovf odi"
fi
echo "input diffusion metrics set"
#### copy diffusion measures to temporary folder for ease ####
echo "copying over diffusion metric data"
for i in ${METRIC}
do
ii=$(eval "echo \$${i}")
[ ! -f ./metric/${i}.nii.gz ] && cp -v "${ii}" ./metric/
done
echo "diffusion data copied"
#### identify transform between freesurfer space and anat space. See HCP pipeline for more reference ####
if [ ! -f c_ras.mat ]; then
echo "identifying transform between freesurfer and anat space"
MatrixXYZ=`mri_info --cras ${freesurfer}/mri/brain.finalsurfs.mgz`
MatrixX=`echo ${MatrixXYZ} | awk '{print $1;}'`
MatrixY=`echo ${MatrixXYZ} | awk '{print $2;}'`
MatrixZ=`echo ${MatrixXYZ} | awk '{print $3;}'`
echo "1 0 0 ${MatrixX}" > c_ras.mat
echo "0 1 0 ${MatrixY}" >> c_ras.mat
echo "0 0 1 ${MatrixZ}" >> c_ras.mat
echo "0 0 0 1" >> c_ras.mat
fi
echo "transform computed"
#### convert ribbons and surface files ####
# ribbon
echo "converting ribbon files"
[ ! -f ${SPACES_DIR[0]}/ribbon.nii.gz ] && mri_convert ${freesurfer}/mri/ribbon.mgz ${SPACES_DIR[0]}/ribbon.nii.gz
# extract hemispheric ribbons
[ ! -f ./ribbon_lh.nii.gz ] && fslmaths ${SPACES_DIR[0]}/ribbon.nii.gz -thr 3 -uthr 3 -bin ./ribbon_lh.nii.gz
[ ! -f ./ribbon_rh.nii.gz ] && fslmaths ${SPACES_DIR[0]}/ribbon.nii.gz -thr 42 -uthr 42 -bin ./ribbon_rh.nii.gz
echo "converting ribbon files complete"
# hemisphere ribbon and surfaces (obtained directly from HCP PostFreesurfer pipeline and amended for BL: https://github.com/Washington-University/HCPpipelines/blob/master/PostFreeSurfer/scripts/FreeSurfer2CaretConvertAndRegisterNonlinear.sh)
echo "looping through hemispheres and creating appropriate surface files"
for hemi in $HEMI
do
if [[ ${hemi} == 'lh' ]]; then
STRUCTURE="CORTEX_LEFT"
else
STRUCTURE="CORTEX_RIGHT"
fi
for SURFS in $surfs
do
# convert files and set structure
if [ ! -f ${SPACES_DIR[0]}/${hemi}.${SURFS} ]; then
mris_convert ${freesurfer}/surf/${hemi}.${SURFS::-9} ${SPACES_DIR[0]}/${hemi}.${SURFS}
# set up gifti structure
wb_command -set-structure ${SPACES_DIR[0]}/${hemi}.${SURFS} \
${STRUCTURE} \
-surface-type ANATOMICAL
fi
# apply affine
[ ! -f ${SPACES_DIR[0]}/${hemi}.cras.${SURFS} ] && wb_command -surface-apply-affine ${SPACES_DIR[0]}/${hemi}.${SURFS} \
c_ras.mat \
${SPACES_DIR[0]}/${hemi}.cras.${SURFS}
if [[ ! ${warp} == 'null' ]]; then
# apply MNI warp
[ ! -f ${SPACES_DIR[1]}/${hemi}.mni.${SURFS} ] && wb_command -surface-apply-warpfield ${SPACES_DIR[0]}/${hemi}.cras.${SURFS} ${inv_warp} \
${SPACES_DIR[1]}/${hemi}.mni.${SURFS} \
-fnirt \
${warp}
fi
done
# create midthickness surfaces
if [ ! -f ${SPACES_DIR[0]}/${hemi}.midthickness.native.surf.gii ]; then
wb_command -surface-average \
${SPACES_DIR[0]}/${hemi}.midthickness.native.surf.gii \
-surf ${SPACES_DIR[0]}/${hemi}.cras.white.surf.gii \
-surf ${SPACES_DIR[0]}/${hemi}.cras.pial.surf.gii
wb_command -set-structure \
${SPACES_DIR[0]}/${hemi}.midthickness.native.surf.gii \
${STRUCTURE} \
-surface-type ANATOMICAL \
-surface-secondary-type MIDTHICKNESS
if [[ ! ${warp} == 'null' ]]; then
[ ! -f ${SPACES_DIR[1]}/${hemi}.midthickness.mni.surf.gii ] && wb_command -surface-apply-warpfield ${SPACES_DIR[0]}/${hemi}.midthickness.native.surf.gii ${inv_warp} \
${SPACES_DIR[1]}/${hemi}.midthickness.mni.surf.gii \
-fnirt \
${warp}
fi
fi
# identify number of vertices for inflation
NativeVerts=$(wb_command -file-information ${SPACES_DIR[0]}/${hemi}.midthickness.native.surf.gii | grep 'Number of Vertices:' | cut -f2 -d: | tr -d '[:space:]')
NativeInflationScale=$(echo "scale=4; 0.75 * $NativeVerts / 32492" | bc -l)
# inflate surfaces
for spaces in ${SPACES}
do
if [[ ${spaces} == 'native' ]]; then
outdir=${SPACES_DIR[0]}
else
outdir=${SPACES_DIR[1]}
fi
# inflate native
[ ! -f ${outdir}/${hemi}.midthickness.very_inflated.${spaces}.surf.gii ] && wb_command -surface-generate-inflated \
${outdir}/${hemi}.midthickness.${spaces}.surf.gii \
${outdir}/${hemi}.midthickness.inflated.${spaces}.surf.gii \
${outdir}/${hemi}.midthickness.very_inflated.${spaces}.surf.gii \
-iterations-scale $NativeInflationScale
done
# volume-specific operations
volume_name="volume.shape.gii"
outdir=${SPACES_DIR[0]}
if [ ! -f ${outdir}/${hemi}.${volume_name} ]; then
mris_convert -c ${freesurfer}/surf/${hemi}.volume \
${freesurfer}/surf/${hemi}.white \
${outdir}/${hemi}.${volume_name}
wb_command -set-structure ${outdir}/${hemi}.${volume_name} \
${STRUCTURE}
wb_command -set-map-names ${outdir}/${hemi}.${volume_name} \
-map 1 ${hemi}_Volume
wb_command -metric-palette ${outdir}/${hemi}.${volume_name} \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 2 98 \
-palette-name Gray_Interp \
-disp-pos true \
-disp-neg true \
-disp-zero true
wb_command -metric-math "abs(volume)" \
${outdir}/${hemi}.${volume_name} \
-var volume \
${outdir}/${hemi}.${volume_name}
wb_command -metric-palette ${outdir}/${hemi}.${volume_name} \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 4 96 \
-interpolate true \
-palette-name videen_style \
-disp-pos true \
-disp-neg false \
-disp-zero false
fi
# thickness-specific operations
thickness_name="thickness.shape.gii"
outdir=${SPACES_DIR[0]}
if [ ! -f ${outdir}/${hemi}.${thickness_name} ]; then
mris_convert -c ${freesurfer}/surf/${hemi}.thickness \
${freesurfer}/surf/${hemi}.white \
${outdir}/${hemi}.${thickness_name}
wb_command -set-structure ${outdir}/${hemi}.${thickness_name} \
${STRUCTURE}
wb_command -metric-math "var * -1" ${outdir}/${hemi}.${thickness_name} \
-var var\
${outdir}/${hemi}.${thickness_name}
wb_command -set-map-names ${outdir}/${hemi}.${thickness_name} \
-map 1 ${hemi}_Thickness
wb_command -metric-palette ${outdir}/${hemi}.${thickness_name} \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 2 98 \
-palette-name Gray_Interp \
-disp-pos true \
-disp-neg true \
-disp-zero true
wb_command -metric-math "abs(thickness)" \
${outdir}/${hemi}.${thickness_name} \
-var thickness \
${outdir}/${hemi}.${thickness_name}
wb_command -metric-palette ${outdir}/${hemi}.${thickness_name} \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 4 96 \
-interpolate true \
-palette-name videen_style \
-disp-pos true \
-disp-neg false \
-disp-zero false
wb_command -metric-math "thickness > 0" \
${outdir}/${hemi}.roi.shape.gii \
-var thickness \
${outdir}/${hemi}.${thickness_name}
wb_command -metric-fill-holes \
${outdir}/${hemi}.midthickness.native.surf.gii \
${outdir}/${hemi}.roi.shape.gii \
${outdir}/${hemi}.roi.shape.gii
wb_command -metric-remove-islands \
${outdir}/${hemi}.midthickness.native.surf.gii \
${outdir}/${hemi}.roi.shape.gii \
${outdir}/${hemi}.roi.shape.gii
wb_command -set-map-names \
${outdir}/${hemi}.roi.shape.gii \
-map 1 \
"${hemi}"_ROI
fi
# set up aparc.a2009s labels
if [ ! -f ./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii ]; then
mris_convert --annot \
${freesurfer}/label/${hemi}.${fsurfparc}.annot \
${freesurfer}/surf/${hemi}.pial \
./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii
wb_command -set-structure \
./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii \
${STRUCTURE}
wb_command -set-map-names \
./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii \
-map 1 \
"${hemi}"_aparc.a2009s
wb_command -gifti-label-add-prefix \
./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii \
"${hemi}_" \
./cortexmap/cortexmap/label/${hemi}.${fsurfparc}.native.label.gii
fi
done
echo "surface files generated"
#### SNR surface mapping ####
# reslice snr to ribbon
echo "moving snr image to ribbon space"
[ ! -f ./snr_ribbon.nii.gz ] && mri_vol2vol --mov snr.nii.gz \
--targ ${SPACES_DIR[0]}/ribbon.nii.gz \
--regheader \
--o ./snr_ribbon.nii.gz
echo "snr image in ribbon space"
echo "looping through hemispheres and mapping snr"
for hemi in ${HEMI}
do
snr_data="snr_ribbon.nii.gz"
outdir=${SPACES_DIR[0]}
funcdir=${FUNC_DIR[0]}
# map snr
if [ ! -f ${funcdir}/${hemi}.snr.func.gii ]; then
wb_command -volume-to-surface-mapping ${snr_data} \
$outdir/${hemi}.midthickness.native.surf.gii \
${funcdir}/${hemi}.snr.func.gii \
-myelin-style ribbon_${hemi}.nii.gz \
$outdir/${hemi}.thickness.shape.gii \
"$MappingSigma"
# mask out non cortex
wb_command -metric-mask ${funcdir}/${hemi}.snr.func.gii \
${SPACES_DIR[0]}/${hemi}.roi.shape.gii \
${funcdir}/${hemi}.snr.func.gii
# find "good" vertices (snr > 10)
wb_command -metric-math 'x>10' \
${funcdir}/${hemi}.goodvertex.func.gii \
-var x \
${funcdir}/${hemi}.snr.func.gii
# set map name and pallete
wb_command -set-map-names ${funcdir}/${hemi}.snr.func.gii \
-map 1 \
"$hemi"_"$vol"
wb_command -metric-palette ${funcdir}/${hemi}.snr.func.gii \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 2 98 \
-interpolate true \
-palette-name videen_style \
-disp-pos true \
-disp-neg false \
-disp-zero false
fi
# error out if snr not mapped
if [ -f ${funcdir}/${hemi}.snr.func.gii ]; then
echo "${hemi} snr mapped"
else
echo "${hemi} snr failed. check logs"
exit 1
fi
done
echo "snr mapped to cortex"
#### metric surface mapping ####
echo "looping through diffusion metrics and mapping to cortex"
for vol in ${METRIC}
do
echo "mapping ${vol} to cortex"
[ ! -f ./metric/${vol}_ribbon.nii.gz ] && mri_vol2vol --mov ./metric/${vol}.nii.gz \
--targ ${SPACES_DIR[0]}/ribbon.nii.gz \
--regheader \
--o ./metric/${vol}_ribbon.nii.gz
for hemi in ${HEMI}
do
vol_data="./metric/${vol}_ribbon.nii.gz"
outdir=${SPACES_DIR[0]}
funcdir=${FUNC_DIR[0]}
# map volumes to surface
if [ ! -f ${funcdir}/${hemi}.${vol}.func.gii ]; then
wb_command -volume-to-surface-mapping ${vol_data} \
${outdir}/${hemi}.midthickness.native.surf.gii \
${funcdir}/${hemi}.${vol}.func.gii \
-myelin-style ribbon_${hemi}.nii.gz \
${outdir}/${hemi}.thickness.shape.gii \
"$MappingSigma"
# mask surface by good vertices
wb_command -metric-mask ${funcdir}/${hemi}.${vol}.func.gii \
${funcdir}/${hemi}.goodvertex.func.gii \
${funcdir}/${hemi}.${vol}.func.gii
# dilate surface
wb_command -metric-dilate ${funcdir}/${hemi}.${vol}.func.gii \
${outdir}/${hemi}.midthickness.native.surf.gii \
20 \
${funcdir}/${hemi}.${vol}.func.gii \
-nearest
# mask surface by roi.native.shape
wb_command -metric-mask ${funcdir}/${hemi}.${vol}.func.gii \
${outdir}/${hemi}.roi.shape.gii \
${funcdir}/${hemi}.${vol}.func.gii
# set map name and pallete
wb_command -set-map-names ${funcdir}/${hemi}.${vol}.func.gii \
-map 1 \
"$hemi"_"$vol"
wb_command -metric-palette ${funcdir}/${hemi}.${vol}.func.gii \
MODE_AUTO_SCALE_PERCENTAGE \
-pos-percent 4 96 \
-interpolate true \
-palette-name videen_style \
-disp-pos true \
-disp-neg false \
-disp-zero false
fi
# fail out if map is not created
if [ -f ${funcdir}/${hemi}.${vol}.func.gii ]; then
echo "${hemi} ${vol} mapped to cortex"
else
echo "${hemi} ${vol} failed. check logs"
exit 1
fi
done
done