-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNSPN_TransformQuantitativeMaps.sh
executable file
·202 lines (173 loc) · 7.35 KB
/
NSPN_TransformQuantitativeMaps.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
#!/bin/bash
#====================================================================
# Created by Kirstie Whitaker on 25th April 2016
#
# DESCRIPTION:
# This code takes a freesurfer directory and it's fellow MPM
# directory and transforms all MPM and DTI measures so they
# are all in the same space.
#
# INPUTS:
# study_dir : The directory containing the SUB_DATA folder which
# itself contains directories named by sub_id.
# sub_id : Subject ID. These folders should be inside SUB_DATA
# and themselves contain directories called SURFER
# and MPM.
# occ : The scan occasion. One of baseline, CBSU, UCL and
# WBIC. This directory contains the output of
# recon-all and is found inside the subject's SURFER
# directory.
#
# EXPECTS:
# Recon-all, trac-all and quality control edits must have been
# completed.
# NSPN_mpm_bet_mask.sh must also have been completed and the MPM
# directory should be inside the subject's directory at the same
# level as the SURFER dir.
#
# OUTPUTS:
# At the end the ${sub_id}SURFER/${occ}/mri directory will contain:
# A.mgz MT.mgz R1.mgz R2s.mgz
# FA.mgz L1.mgz L23.mgz MD.mgz MO.mgz
#
#====================================================================
#====================================================================
# USAGE: NSPN_TransformQuantitativeMaps.sh <study_dir> <sub> <occ>
#====================================================================
function usage {
echo "USAGE: NSPN_TransformQuantitativeMaps.sh <study_dir> <sub> <occ>"
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."
exit
}
#====================================================================
# CHECK INPUTS
#====================================================================
data_dir=$1
sub=$2
occ=$3
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 [[ -z ${occ} ]]; then
echo "No occ provided"
print_usage=1
fi
if [[ ${print_usage} == 1 ]]; then
usage
fi
#=============================================================================
# SET A COUPLE OF USEFUL VARIABLES
#=============================================================================
surfer_dir=${data_dir}/SUB_DATA/${sub}/SURFER/${occ}/
mpm_dir=${data_dir}/SUB_DATA/${sub}/MPM/${occ}/
SUBJECTS_DIR=${surfer_dir}/../
surf_sub=`basename ${surfer_dir}`
#====================================================================
# PRINT TO SCREEN WHAT WE'RE DOING
#====================================================================
echo "==== Transform Quantitative Maps to Freesurfer Space ===="
#=============================================================================
# DON'T BOTHER IF THERE'S NO DATA!
#=============================================================================
if [[ ! -f ${surfer_dir}/mri/T1.mgz ]]; then
echo "No T1.mgz file in surfer directory. Exiting"
exit
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; do
measure_file_dti=${surfer_dir}/dmri/dtifit_${measure}.nii.gz
# If the file doesn't exist, then just skip this whole
# section!
if [[ ! -f ${measure_file_dti} && ${measure} != "L23" ]]; then
echo "No ${measure} file in dmri folder - skipping"
continue
# If the measure you're looking for is L23 then you need to make
# that one from L2 and L23. The first step is checking that those
# two files exist!
elif [[ ! -f ${measure_file_dti} && ${measure} == "L23" ]]; then
if [[ -f ${measure_file_dti/L23.nii/L2.nii} && -f ${measure_file_dti/L23.nii/L3.nii} ]]; then
fslmaths ${measure_file_dti/L23.nii/L2.nii} \
-add ${measure_file_dti/L23.nii/L3.nii} \
-div 2 \
${measure_file_dti}
else
echo "Either L2 or L3 file in dmri folder is missing - skipping"
continue
fi
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 \
--fsl ${surfer_dir}/dmri/xfms/diff2anatorig.bbr.mat \
--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=${mpm_dir}/${mpm}_head.nii.gz
# If the file doesn't exist, then just skip this whole
# section!
if [[ ! -f ${mpm_file} ]]; then
echo "No ${mpm}_head.nii.gz file in MPM folder - skipping"
continue
else
# If the measure file has particularly small values
# then multiply this file by 1000 first
if [[ ${mpm} == "R2s" || ${mpm} == "MT" ]]; 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
echo " Registering ${mpm} file to freesurfer space"
# 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
else
echo " ${mpm} file already in freesurfer space"
fi
fi
done
#=============================================================================
# Well done. You're all finished :)
#=============================================================================