forked from KirstieJane/DTI_PROCESSING
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freesurfer_extract_rois.sh
executable file
·272 lines (226 loc) · 11.6 KB
/
freesurfer_extract_rois.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
#!/bin/bash
#==============================================================================
# Extract DTI and MPM measures from freesurfer ROIs
# along with surface parameters from various parcellations
# Created by Kirstie Whitaker
# Contact kw401@cam.ac.uk
#==============================================================================
#==============================================================================
# Define the usage function
#==============================================================================
function usage {
echo "USAGE: freesurfer_extract_rois.sh <data_dir> <subid>"
echo "Note that data dir expects to find SUB_DATA within it"
echo "and then the standard NSPN directory structure"
echo ""
echo "DESCRIPTION: This code will register the DTI B0 file to freesurfer space,"
echo "apply this registration to the DTI measures in the <dti_dir>/FDT folder,"
echo "transform the MPM files to freesurfer space,"
echo "and then create the appropriate <measure>_wmparc.stats and "
echo "<measure>_aseg.stats files for each subject separately"
echo "Finally, it will also extract surface stats from the parcellation schemes"
exit
}
#=============================================================================
# CHECK INPUTS
#=============================================================================
data_dir=$1
sub=$2
# This needs to be in the same directory as this script
# Fine if you download the git repository but not fine
# if you've only take the script itself!
lobes_ctab=`dirname ${0}`/LobesStrictLUT.txt
parc500_ctab=`dirname ${0}`/parc500LUT.txt
if [[ ! -d ${data_dir} ]]; then
echo "${data_dir} is not a directory, please check"
print_usage=1
fi
if [[ -z ${sub} ]]; then
echo "No subject id provided"
print_usage=1
fi
if [[ ! -f ${lobes_ctab} ]]; then
echo "Can't find lobes color look up table file"
echo "Check that LobesStrictLUT.txt is in the same directory"
echo "as this script"
print_usage=1
fi
if [[ ${print_usage} == 1 ]]; then
usage
fi
#=============================================================================
# START A LOOP OVER TIMEPOINTS
#=============================================================================
for occ in 0; do
#=============================================================================
# SET A COUPLE OF USEFUL VARIABLES
#=============================================================================
surfer_dir=${data_dir}/SUB_DATA/${sub}/SURFER/MRI${occ}/
dti_dir=${data_dir}/SUB_DATA/${sub}/DTI/MRI${occ}/
reg_dir=${data_dir}/SUB_DATA/${sub}/REG/MRI${occ}/
mpm_dir=${data_dir}/SUB_DATA/${sub}/MPM/MRI${occ}/
SUBJECTS_DIR=${surfer_dir}/../
surf_sub=`basename ${surfer_dir}`
#=============================================================================
# REGISTER B0 TO FREESURFER SPACE
#=============================================================================
# The first step is ensuring that the dti_ec (B0) file
# has been registered to freesurfer space
if [[ ! -f ${reg_dir}/diffB0_TO_surf.dat ]]; then
bbregister --s ${surf_sub} \
--mov ${dti_dir}/dti_ec.nii.gz \
--init-fsl \
--reg ${reg_dir}/diffB0_TO_surf.dat \
--t2
fi
#=============================================================================
# TRANSFORM DTI MEASURES FILES TO FREESURFER SPACE
#=============================================================================
# If the dti measure file doesn't exist yet in the <surfer_dir>/mri folder
# then you have to make it
for measure in FA MD MO L1 L23 sse; do
measure_file_dti=`ls -d ${dti_dir}/FDT/*_${measure}.nii.gz 2> /dev/null`
if [[ ! -f ${measure_file_dti} ]]; then
echo "${measure} file doesn't exist in dti_dir, please check"
usage
fi
# If the measure file has particularly small values
# then multiply this file by 1000 first
if [[ "MD L1 L23" =~ ${measure} ]]; then
if [[ ! -f ${measure_file_dti/.nii/_mul1000.nii} ]]; then
fslmaths ${measure_file_dti} -mul 1000 ${measure_file_dti/.nii/_mul1000.nii}
fi
measure_file_dti=${measure_file_dti/.nii/_mul1000.nii}
fi
# Now transform this file to freesurfer space
if [[ ! -f ${surfer_dir}/mri/${measure}.mgz ]]; then
echo " Registering ${measure} file to freesurfer space"
mri_vol2vol --mov ${measure_file_dti} \
--targ ${surfer_dir}/mri/T1.mgz \
--o ${surfer_dir}/mri/${measure}.mgz \
--reg ${reg_dir}/diffB0_TO_surf.dat \
--no-save-reg
else
echo " ${measure} file already in freesurfer space"
fi
done
#=============================================================================
# TRANSFORM MPM MEASURES FILES TO FREESURFER SPACE
#=============================================================================
# If the mpm measure file doesn't exist yet in the <surfer_dir>/mri folder
# then you have to make it
# Loop through the mpm outputs that you're interested in
for mpm in R1 MT R2s A; do
mpm_file=`ls -d ${mpm_dir}/${mpm}_head.nii.gz 2> /dev/null`
# If the measure file has particularly small values
# then multiply this file by 1000 first
if [[ ${mpm} == "R2s" ]]; then
if [[ ! -f ${mpm_file/.nii/_mul1000.nii} ]]; then
fslmaths ${mpm_file} -mul 1000 ${mpm_file/.nii/_mul1000.nii}
fi
mpm_file=${mpm_file/.nii/_mul1000.nii}
fi
if [[ ! -f ${surfer_dir}/mri/${mpm}.mgz ]]; then
# Align the mgz file to "freesurfer" anatomical space
mri_vol2vol --mov ${mpm_file} \
--targ ${surfer_dir}/mri/T1.mgz \
--regheader \
--o ${surfer_dir}/mri/${mpm}.mgz \
--no-save-reg
fi
done
#=============================================================================
# EXTRACT THE STATS FROM THE SEGMENTATION FILES
#=============================================================================
# Specifically this will loop through the following segmentations:
# wmparc
# aseg
# lobesStrict
# 500.aparc_cortical_consecutive
# 500.aparc_cortical_expanded_consecutive_WMoverlap
#=============================================================================
for measure in R1 MT R2s A FA MD MO L1 L23 sse; do
if [[ -f ${surfer_dir}/mri/${measure}.mgz ]]; then
#=== wmparc
if [[ ! -f ${surfer_dir}/stats/${measure}_wmparc.stats ]]; then
mri_segstats --i ${surfer_dir}/mri/${measure}.mgz \
--seg ${surfer_dir}/mri/wmparc.mgz \
--ctab ${FREESURFER_HOME}/WMParcStatsLUT.txt \
--sum ${surfer_dir}/stats/${measure}_wmparc.stats \
--pv ${surfer_dir}/mri/norm.mgz
fi
#=== aseg
if [[ ! -f ${surfer_dir}/stats/${measure}_aseg.stats ]]; then
mri_segstats --i ${surfer_dir}/mri/${measure}.mgz \
--seg ${surfer_dir}/mri/aseg.mgz \
--sum ${surfer_dir}/stats/${measure}_aseg.stats \
--pv ${surfer_dir}/mri/norm.mgz \
--ctab ${FREESURFER_HOME}/ASegStatsLUT.txt
fi
#=== lobesStrict
if [[ ! -f ${surfer_dir}/stats/${measure}_lobesStrict.stats ]]; then
mri_segstats --i ${surfer_dir}/mri/${measure}.mgz \
--seg ${surfer_dir}/mri/lobes+aseg.mgz \
--sum ${surfer_dir}/stats/${measure}_lobesStrict.stats \
--pv ${surfer_dir}/mri/norm.mgz \
--ctab ${lobes_ctab}
fi
#=== 500.aparc_cortical_consecutive.nii.gz
# Extract measures from the cortical regions in the 500 parcellation
# if [[ ! -f ${surfer_dir}/stats/${measure}_500cortConsec.stats
# && -f ${surfer_dir}/parcellation/500.aparc_cortical_consecutive.nii.gz ]]; then
mri_segstats --i ${surfer_dir}/mri/${measure}.mgz \
--seg ${surfer_dir}/parcellation/500.aparc_cortical_consecutive.nii.gz \
--sum ${surfer_dir}/stats/${measure}_500cortConsec.stats \
--pv ${surfer_dir}/mri/norm.mgz \
--ctab ${parc500_ctab}
# fi
#=== 500.aparc_cortical_expanded_consecutive_WMoverlap
# Only run this if there is a 500 cortical parcellation
# if [[ ! -f ${surfer_dir}/stats/${measure}_500cortExpConsecWMoverlap.stats \
# && -f ${surfer_dir}/parcellation/500.aparc_cortical_expanded_consecutive.nii.gz ]]; then
# Create the overlap file if it doesn't already exist
if [[ ! -f ${surfer_dir}/parcellation/500.aparc_cortical_expanded_consecutive_WMoverlap.nii.gz ]]; then
fslmaths ${surfer_dir}/parcellation/500.aparc_whiteMatter.nii.gz \
-bin \
-mul ${surfer_dir}/parcellation/500.aparc_cortical_expanded_consecutive.nii.gz \
${surfer_dir}/parcellation/500.aparc_cortical_expanded_consecutive_WMoverlap.nii.gz
fi
mri_segstats --i ${surfer_dir}/mri/${measure}.mgz \
--seg ${surfer_dir}/parcellation/500.aparc_cortical_expanded_consecutive_WMoverlap.nii.gz \
--sum ${surfer_dir}/stats/${measure}_500cortExpConsecWMoverlap.stats \
--pv ${surfer_dir}/mri/norm.mgz \
--ctab ${parc500_ctab}
# fi
else
echo "${measure} file not transformed to Freesurfer space"
fi
done
#=============================================================================
# EXTRACT THE STATS FROM THE SURFACE PARCELLATION FILES
#=============================================================================
# Specifically this will loop through the following segmentations:
# aparc
# 500.aparc
# lobesStrict
#=============================================================================
# Loop over both left and right hemispheres
for hemi in lh rh; do
# Loop over parcellations
for parc in aparc 500.aparc lobesStrict; do
if [[ ! -f ${surfer_dir}/stats/${hemi}.${parc}.stats \
&& -f ${surfer_dir}/label/${hemi}.${parc}.annot ]]; then
mris_anatomical_stats -a ${surfer_dir}/label/${hemi}.${parc}.annot \
-f ${surfer_dir}/stats/${hemi}.${parc}.stats \
${surf_sub} \
${hemi}
fi
done # Close parcellation loop
done # Close hemi loop
#=============================================================================
# CLOSE THE OCC LOOP
#=============================================================================
done
#=============================================================================
# Well done. You're all finished :)
#=============================================================================