-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnifmri_prep04_filter
executable file
·355 lines (306 loc) · 12.4 KB
/
lnifmri_prep04_filter
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
#!/bin/bash
#
# LNiF MRI Lab Pipeline for preprocessing of
# structural and functional MRI dataset
# [email protected] - 20200310
#
# Regression and filtering of SD-corrected EPI:
# - WM and CSF regressors
# - Motion parameters
# - Temporal masking
# - Bandpass filtering
# - ICA (ncomp=40) [optional]
#
########################################################################
export LC_ALL=C
shopt -s extglob
UsageFSL() {
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "\tFSLDIR not set!\n\n"
printf "\tRequires:\n\t\tFSL (https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FSL)\n"
printf "\n\n(c) [email protected], 2020.\n"
printf "***************************************************\n\n"
exit 1
}
UsageEPI() {
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "Missing motion de-weighting and ICA options.\n"
printf "Quit."
printf "\n\n(c) [email protected], 2020.\n"
printf "***************************************************\n\n"
exit 1
}
Start() {
stamp=`date +%Y%m%d_%H%M%S`
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "Process functional data (nuisance regression, smoothing, BP filtering)\n"
}
NextStep() {
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "Complete. Exec. Time: ${exectime}\nPreprocessing pipeline complete."
printf "\n\n***************************************************\n\n"
exit 1
}
[ "$FSLDIR" = "" ] && UsageFSL
[ "$1" = "" ] && UsageEPI
runmot=""
runica=""
#Read answer for optional single-subject ICA
while getopts 'MmIi' option; do
case "$option" in
M)
runmot="y"
;;
m)
runmot="n"
;;
I)
runica="y"
;;
i)
runica="n"
;;
esac
done
exectime=0
SECONDS=0
LOGDIR=`pwd`/logs
cursubID="none"
cursdc="none"
Start
printf "\t\nRun motion outliers: $runmot\n\tRun SS-ICA: $runica\n\n"
for epiimg in `cat ${LOGDIR}/lnifmri_prep_sdclist.txt` ; do
epiname=$($FSLDIR/bin/remove_ext ${epiimg})
outpath=$(dirname ${epiimg})
readarray -d / -t fileparts <<< "${epiimg}"
nid=$((${#fileparts[@]}-1))
sid=$((${#fileparts[@]}-4))
did=$((${#fileparts[@]}-2))
nameID=$(echo ${fileparts[${nid}]})
subID=$(echo ${fileparts[${sid}]})
sdcID=$(echo ${fileparts[${did}]})
jsonID=$($FSLDIR/bin/remove_ext ${nameID})
# epivol="${epiname}_onevol"
outname=""
meanimg=${epiname}_meanimg
#define different images/masks to load
printf -v STR "${subID}_ses-01_T1w"
t1struc=${subID}/$STR.anat/T1_biascorr.nii.gz
bstruc=${subID}/$STR.anat/T1_biascorr_brain.nii.gz
wmstruc=${subID}/$STR.anat/T1_wmedge.nii.gz
gmstruc=${subID}/$STR.anat/T1_gmedge.nii.gz
csfstruc=${subID}/$STR.anat/T1_csfedge.nii.gz
brainmask=${subID}/$STR.anat/T1_biascorr_brain_mask.nii.gz
mniwarp=${subID}/${STR}.anat/T1_to_MNI_nonlin_field.nii.gz
lesionmask=${subID}/$STR.anat/lesionmask.nii.gz
lesionmaskinv=${subID}/$STR.anat/lesionmaskinv.nii.gz
stdbrainmask=$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz
std=$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz
# Check to restart the ses-XX counter
if [[ "$cursubID" != "${subID}" || "$cursdc" != "${sdcID}" ]]; then
subcount=0
cursubID=${subID}
cursdc=${sdcID}
fi
printf "\n%s_ses-%02d:\n" "${subID}" "$(( ++subcount ))"
TR=$(awk '$1 ~ /"RepetitionTime"/ {print $2}' ${subID}/func/${jsonID}_parameters.json | tr -d ',')
printf "\tExtracting mean image\n"
$FSLDIR/bin/fslmaths ${epiimg} -Tmean ${meanimg}
printf "\tExtracting WM regressor\n"
$FSLDIR/bin/fslmeants -i ${epiimg} -o ${epiname}_regressor_wm.txt -m ${wmstruc}
printf "\tExtracting CSF regressor\n"
$FSLDIR/bin/fslmeants -i ${epiimg} -o ${epiname}_regressor_csf.txt -m ${csfstruc}
printf "\tExtracting GM regressor\n"
$FSLDIR/bin/fslmeants -i ${epiimg} -o ${epiname}_regressor_gm.txt -m ${gmstruc}
printf "\tExtracting GS regressor\n"
$FSLDIR/bin/fslmeants -i ${epiimg} -o ${epiname}_regressor_gs.txt -m ${brainmask}
# Switch deweighting of motion outliers
case "${runmot}" in
y)
# Compose full matrix of regressors (if motout exists)
if [ -f ${subID}/func/${jsonID}_motout.txt ]; then
printf "\tComposing nuisance regressor (motion, outliers, WM, CSF)\n"
paste -d " " ${subID}/func/${jsonID}.par \
${subID}/func/${jsonID}_motout.txt \
${epiname}_regressor_wm.txt \
${epiname}_regressor_csf.txt > ${epiname}_nuisance.txt;
else
# Use only motion parameters and tissue-based regressors
printf "\tMotion outliers info not available\n"
printf "\tComposing nuisance regressor (motion, WM, CSF)\n"
paste -d " " ${subID}/func/${jsonID}.par \
${epiname}_regressor_wm.txt \
${epiname}_regressor_csf.txt > ${epiname}_nuisance.txt;
fi
;;
n)
# Regress motion and tissue-based regressors
printf "\tMotion outliers deweighting disabled\n"
printf "\tComposing nuisance regressor (motion, WM, CSF)\n"
paste -d " " ${subID}/func/${jsonID}.par \
${epiname}_regressor_wm.txt \
${epiname}_regressor_csf.txt > ${epiname}_nuisance.txt;
;;
esac
# Regression of nuisance matrix
printf "\tRegression of nuisance signals\n"
if [ -f ${epiname}_nuisance.txt ]; then
$FSLDIR/bin/fsl_glm -i ${epiimg} \
-d ${epiname}_nuisance.txt \
-o ${epiname}_nuisance_betas \
-m ${brainmask} \
--demean \
--out_res=${epiname}_res;
fi
#echo "${g}: Noise reduction"
#$FSLDIR/bin/susan $epiimg -1 0.6 3 1 1 ${brainmask} -1 $epiimg
# If it's NOT a task-based scan, apply bandpass filtering
if [[ ${nameID} != *"task"* ]]; then
printf -v sesID "ses-%02d" "${subcount}"
printf -v outname -- "${outpath}/${subID}_${sesID}_bold_rest"
#Compute frequency spectrum
printf "\tCompute Spectrum before BPF\n"
$FSLDIR/bin/fslpspec ${epiname}_res ${epiname}_spectrum_pre
$FSLDIR/bin/fslmeants -i ${epiname}_spectrum_pre -o ${epiname}_spectrum_pre.txt -m ${gmstruc} -w
# Set BPF cutoff values
hp=$( echo "scale=3;1/(2*0.01*${TR})" | bc) #0.01Hz, sigma: 1/(2*f*TR) as reported in fsl mailing list
lp=$( echo "scale=3;1/(2*0.1*${TR})" | bc) #0.1Hz - check it after computing the spectrum
# BP filtering
printf "\tBandpass filtering (hp=%s, lp=%s, TR=%s)\n" "${hp}" "${lp}" "${TR}"
$FSLDIR/bin/fslmaths ${epiname}_res -mas ${brainmask} -bptf ${hp} ${lp} ${outname}
#####################################################################################################
#iCAPS
# lp=$( echo "scale=3;1/(2*0.1*${TR})" | bc) #0.1Hz - check it after computing the spectrum
# # BP filtering
# printf "\tBandpass filtering (hp=%s, lp=%s)\n" "${hp}" "${lp}"
# $FSLDIR/bin/fslmaths $epiimg -mas ${brainmask} -bptf $hp $lp ${outname}_iCAPS
# #####################################################################################################
#Compute frequency spectrum
printf "\tCompute Spectrum after BPF\n"
$FSLDIR/bin/fslpspec ${outname} ${outname}_spectrum
$FSLDIR/bin/fslmeants -i ${outname}_spectrum -o ${outname}_spectrum.txt -m ${gmstruc} -w
dim=$(cat ${outname}_spectrum.txt | wc -l )
nyq=$(echo "0.5/${TR}/${dim}" | bc -l )
printf "${nyq}\n${subID}\n${sesID}\n${TR}" > ${outname}_spectraspecs.txt
paste -d " " ${epiname}_spectrum_pre.txt ${outname}_spectrum.txt > ${outname}_spectra.txt
#otherwise skip this step
else
printf -v outname -- "${outpath}/${subID}_ses-%02d_bold_task" "${subcount}"
$FSLDIR/bin/fslmaths ${epiname}_res -mul 1 ${outname}
printf "\tTask-based timeseries: skipping bandpass filter\n\n"
fi
#####################################################################################################
# Compute tSNR
printf "\tComputing tSNR map\n"
# $FSLDIR/bin/fslmaths ${outname} -add ${meanimg} ${outname}_scaled
# $FSLDIR/bin/bet ${outname}_scaled ${outname}_tSNR -m -n
# $FSLDIR/bin/fslmaths ${outname}_scaled -mul ${outname}_tSNR_mask -Tmean ${outname}_tSNR_mean
# $FSLDIR/bin/fslmaths ${outname}_scaled -mul ${outname}_tSNR_mask -Tstd ${outname}_tSNR_std
# $FSLDIR/bin/fslmaths ${outname}_tSNR_mean -div ${outname}_tSNR_std -nan ${outname}_tSNR
# Add back mean value
$FSLDIR/bin/fslmaths ${outname} -add ${meanimg} ${outname}_scaled
$FSLDIR/bin/fslmaths ${outname}_scaled \
-mul ${brainmask} \
-Tmean ${outname}_tSNR_mean;
$FSLDIR/bin/fslmaths ${outname}_scaled \
-mul ${brainmask} \
-Tstd ${outname}_tSNR_std;
$FSLDIR/bin/fslmaths ${outname}_tSNR_mean \
-div ${outname}_tSNR_std \
-mul ${brainmask} \
-nan ${outname}_tSNR;
printf "\tFinal warping of tSNR map to MNI\n"
$FSLDIR/bin/applywarp -i ${outname}_tSNR \
-r ${std} \
-w ${mniwarp} \
--interp=nn \
-o ${outname}_tSNR_MNI.nii.gz;
#####################################################################################################
#Warping of filtered data to MNI space
printf "\tFinal warping of EPI to MNI\n"
$FSLDIR/bin/applywarp -i ${outname} \
-r ${std} \
-w ${mniwarp} \
--interp=spline \
-o ${outname}-MNI.nii.gz;
#Create smoothed version of subj- and std-space timeseries
printf "\tSpatial smoothing\n"
$FSLDIR/bin/fslmaths ${outname} -kernel gauss 3 -fmean ${outname}-smooth
$FSLDIR/bin/fslmaths ${outname}-MNI -kernel gauss 3 -fmean ${outname}-MNI-smooth
#Set GM mask as weighting file?
# Repeat for non-BPF data
#Warping of filtered data to MNI space
printf "\tFinal warping of EPI to MNI\n"
$FSLDIR/bin/applywarp -i ${epiname}_res \
-r ${std} \
-w ${mniwarp} \
--interp=spline \
-o ${outname}-nonBPF-MNI.nii.gz;
#Create smoothed version of subj- and std-space timeseries
printf "\tSpatial smoothing\n"
$FSLDIR/bin/fslmaths ${epiname}_res -kernel gauss 3 -fmean ${outname}-nonBPF-smooth
$FSLDIR/bin/fslmaths ${outname}-nonBPF-MNI -kernel gauss 3 -fmean ${outname}-nonBPF-MNI-smooth
#####################################################################################################
# Repeat for iCAPS data
# #Warping of filtered data to MNI space
# printf "\tFinal warping of EPI to MNI\n"
# $FSLDIR/bin/applywarp -i ${epiimg} \
# -r ${std} \
# -w ${mniwarp} \
# --interp=spline \
# -o ${outname}-nonBPF-MNI.nii.gz;
# #Create smoothed version of subj- and std-space timeseries
# printf "\tSpatial smoothing\n"
# $FSLDIR/bin/fslmaths ${epiimg} -kernel gauss 3 -fmean ${outname}-nonBPF-smooth
# $FSLDIR/bin/fslmaths ${outname}-nonBPF-MNI -kernel gauss 3 -fmean ${outname}-nonBPF-MNI-smooth
#####################################################################################################
#Run single-subject ICA [otional]
case "${runica}" in
y)
comp=40
in=${outname}-smooth
out=${outname}-smooth.ica
printf "\tRunning ICA (n=%s)\n" "${comp}"
printf "${out}\n" >> ${LOGDIR}/lnifmri_prep_ssicalist.txt
$FSLDIR/bin/melodic -i ${in} \
-o ${out} \
--nobet --tr=${TR} --mmthresh=0.66 \
-d ${comp} \
-m ${brainmask} \
--report &
# 2022-06-08 [email protected]
inMNI=${outname}-MNI-smooth
outMNI=${outname}-MNI-smooth.ica
printf "\tRunning Probabilistic ICA in MNI space\n"
printf "${outMNI}\n" >> ${LOGDIR}/lnifmri_prep_ssicalist-MNI.txt
$FSLDIR/bin/melodic -i ${inMNI} \
-o ${outMNI} \
--nobet --tr=${TR} --mmthresh=0.66 \
-m ${stdbrainmask} \
--report
stamp=`date +%Y%m%d_%H%M%S`
printf "LNiF MRI Lab Preprocessing Pipeline\nDo not remove this file!\nCreated: ${stamp}" > ${LOGDIR}/lnifmri_prep04_greenlight.txt
sleep 2
;;
n)
printf "\tSkipping SS-ICA\n"
stamp=`date +%Y%m%d_%H%M%S`
printf "LNiF MRI Lab Preprocessing Pipeline\nDo not remove this file!\nCreated: ${stamp}" > ${LOGDIR}/lnifmri_prep04_greenlight.txt
sleep 2
;;
esac
printf "\tDONE.\n\n"
done
stamp=`date +%Y%m%d_%H%M%S`
printf "\t%s: Greenlight from 04_filter, moving to QC Report\n\n" "${stamp}"
duration=$SECONDS
printf -v exectime "$(($duration / 60))m:$(($duration % 60))s."
NextStep