-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradientDistortionUnwarpOnly.sh
executable file
·258 lines (210 loc) · 7.55 KB
/
GradientDistortionUnwarpOnly.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
#!/bin/bash
#
# Function description
# Setup the environment
#
setup_environment() {
node_indicator=`uname -n | cut -c1-6`
export FSLDIR=/nrgpackages/tools.release/fsl-5.0.6-centos6_64
export HCPPIPEDIR=/home/NRG/tbrown01/projects/Pipelines
if [ "${node_indicator}" == "ubuntu" ] ; then
export FSLDIR=/usr/share/fsl/5.0
export HCPPIPEDIR=/home/tbb/projects/Pipelines
fi
. ${FSLDIR}/etc/fslconf/fsl.sh
export HCPPIPEDIR_Global=${HCPPIPEDIR}/global/scripts
export HCPPIPEDIR_Config=${HCPPIPEDIR}/global/config
}
#
# Function description
# Show usage information for this script
#
usage() {
local scriptName=$(basename ${0})
echo ""
echo " Usage: ${scriptName} --project=<project-id> --subject=<subject-id> --outdir=<output-directory> [--structuralsonly] [--keepworkingdirs]"
echo ""
}
#
# Function description
# Get the command line options for this script
#
# Global output variables
# ${project} - project id
# ${subject} - subject id
# ${outdir} - output directory
# ${structuralsOnly} - "TRUE" or "FALSE"
# ${keepWorkingDirs} - "TRUE" or "FALSE"
#
get_options() {
local scriptName=$(basename ${0})
local arguments=($@)
# initialize global output variables
unset project
unset subject
unset outdir
structuralsOnly="FALSE"
keepWorkingDirs="FALSE"
# parse arguments
local index=0
local numArgs=${#arguments[@]}
local argument
while [ ${index} -lt ${numArgs} ]; do
argument=${arguments[index]}
case ${argument} in
--help)
usage
exit 1
;;
--structuralsonly)
structuralsOnly="TRUE"
index=$(( index + 1 ))
;;
--project=*)
project=${argument/*=/""}
index=$(( index + 1 ))
;;
--subject=*)
subject=${argument/*=/""}
index=$(( index + 1 ))
;;
--outdir=*)
outdir=${argument/*=/""}
index=$(( index + 1 ))
;;
--keepworkingdirs)
keepWorkingDirs="TRUE"
index=$(( index + 1 ))
;;
*)
usage
echo "ERROR: Unrecognized Option: ${argument}"
exit 1
;;
esac
done
# check required parameters
if [ -z ${project} ]; then
usage
echo "ERROR: <project-id> not specified"
exit 1
fi
if [ -z ${subject} ]; then
usage
echo "ERROR: <subject-id> not specified"
exit 1
fi
if [ -z ${outdir} ]; then
usage
echo "ERROR: <output-directory> not specified"
exit 1
fi
# report
echo "-- ${scriptName}: Specified command-line options - Start --"
echo " project: ${project}"
echo " subject: ${subject}"
echo " outdir: ${outdir}"
echo " structuralsOnly: ${structuralsOnly}"
echo " keepWorkingDirs: ${keepWorkingDirs}"
echo "-- ${scriptName}: Specified command-line options - End --"
}
#
# Unwarp images in directory list
#
unwarp_images() {
resource_spec=${1}
resources=`ls -d ${resource_spec}`
for resource_dir in ${resources} ; do
# copy files that need to be unwarped
resource_name=${resource_dir##*/}
scan_name=${resource_name%_unproc}
copy_to_dir=${outdir}/${subject}/unprocessed/3T/${scan_name}
log_Msg "copying image files from ${resource_dir}"
mkdir -p ${copy_to_dir}
cp --dereference ${resource_dir}/*.nii.gz ${copy_to_dir}
# copy associated files
if [ -d ${resource_dir}/LINKED_DATA ] ; then
log_Msg "copying LINKED_DATA"
cp --recursive --dereference ${resource_dir}/LINKED_DATA ${copy_to_dir}
fi
if [ "${scan_name}" == "Diffusion" ] ; then
log_Msg "copying bval"
cp --dereference ${resource_dir}/*.bval ${copy_to_dir}
log_Msg "copying bec"
cp --dereference ${resource_dir}/*.bvec ${copy_to_dir}
fi
# unwarp image files
image_dir=${copy_to_dir}
working_dir=${image_dir}/gdc
mkdir -p ${working_dir}/xfms
image_files=`ls ${image_dir}/*.nii.gz`
for image_file in ${image_files} ; do
image_file_base=${image_file%.nii.gz}
image_file_base=${image_file_base##*/}
coeffs_file=${HCPPIPEDIR_Config}/coeff_SC72C_Skyra.grad
in_file=${image_dir}/${image_file_base}
out_file=${working_dir}/${image_file_base}_gdc
out_warpfile=${working_dir}/xfms/${image_file_base}_gdc_warp
log_Msg "working_dir: ${working_dir}"
log_Msg "coeffs_file: ${coeffs_file}"
log_Msg "in_file: ${in_file}"
log_Msg "out_file: ${out_file}"
log_Msg "out_warpfile: ${out_warpfile}"
${HCPPIPEDIR_Global}/GradientDistortionUnwarp.sh \
--workingdir=${working_dir} \
--coeffs=${coeffs_file} \
--in=${in_file} \
--out=${out_file} \
--owarp=${out_warpfile}
if [ "${scan_name}" == "Diffusion" ] ; then
if [[ ${image_file_base} == *DWI* && ${image_file_base} != *SBRef* ]] ; then
echo "Computing gradient coil tensor"
${FSLDIR}/bin/calc_grad_perc_dev --fullwarp=${out_warpfile} -o ${working_dir}/grad_dev
${FSLDIR}/bin/fslmerge -t ${working_dir}/grad_dev ${working_dir}/grad_dev_x ${working_dir}/grad_dev_y ${working_dir}/grad_dev_z
${FSLDIR}/bin/fslmaths ${working_dir}/grad_dev -div 100 ${working_dir}/grad_dev # Convert from % deviation to absolute
${FSLDIR}/bin/imrm ${working_dir}/grad_dev_?
${FSLDIR}/bin/imrm ${working_dir}/trilinear
${FSLDIR}/bin/imrm ${working_dir}/data_warped_vol1
fi
fi
done
# move the generated _gdc.nii.gz files out of the gdc subdirectory up to the main directory
mv -v ${working_dir}/*_gdc.nii.gz ${image_dir}
# remove the working directory with all the unneeded intermediate files
if [ "${keepWorkingDirs}" != "TRUE" ] ; then
rm -rfv ${working_dir}
fi
done
}
#
# Main processing
#
main() {
get_options $@
resources_dir="/data/hcpdb/archive/${project}/arc001/${subject}_3T/RESOURCES"
# gradient unwarp T1w images
log_Msg "gradient unwarp T1w images"
unwarp_images "${resources_dir}/T1w*_unproc"
# gradient unwarp T2w images
log_Msg "gradient unwarp T2w images"
unwarp_images "${resources_dir}/T2w*_unproc"
if [ "${structuralsOnly}" != "TRUE" ] ; then
# gradient unwarp functional resting state images
log_Msg "gradient unwarp functional resting state images"
unwarp_images "${resources_dir}/rfMRI*_unproc"
# gradient unwarp functional task images
log_Msg "gradient unwarp functional task images"
unwarp_images "${resources_dir}/tfMRI*_unproc"
# gradient unwarp diffusion images
log_Msg "gradient unwarp diffusion images"
unwarp_images "${resources_dir}/Diffusion_unproc"
fi
log_Msg "Complete"
}
# Setup environment
setup_environment
# Load function libraries
source ${HCPPIPEDIR}/global/scripts/log.shlib # log_ functions
log_SetToolName $(basename ${0})
# Invoke the main function
main $@