-
Notifications
You must be signed in to change notification settings - Fork 2
/
ENIGMA_pre.sh
executable file
·346 lines (283 loc) · 9.57 KB
/
ENIGMA_pre.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
#!/bin/bash -l
##############################################################################
# Distortion Correction
#
# INPUT
# 1) Subject
# 2) Non-linear registration method
# 3) BET Method
#
#
###############################################################################
#set -x
set -e
module load civet/1.1.10
module load minc-tools/2011.11
module load fsl/5.0.6
module load R/3.0.2-precise64
module load ENIGMA/master
# HORRIBLE HACK
PYTHONPATH=/usr/lib/pymodules/python2.7:${PYTHONPATH}
# Initialize Arguments
function usage() {
echo
echo "###############################################################"
echo " Usage: $0 [OPTIONS]"
echo " -h print help."
echo " -s Subject ID to process."
echo " -m Non-Linear Registration Method: ANTS, TRAC, FNIRT."
echo " -b BET Method (not-required), methods are equivalent."
echo ""
echo "Req:"
echo " ./T2.nii.gz"
echo " ./DTI_DIR60/DTI.nii.gz"
echo " ./DTI_DIR60/DTI.bvecs"
echo " ./DTI_DIR60/DTI.bvals"
echo ""
echo "###############################################################"
exit
}
function help() {
echo
echo "Apply Distortion Correction: Snippet"
echo "This script requires two inputs:"
echo " "
echo " 1. The name of the current subject to process"
echo " "
echo " 2. The name of non-linear registration method."
echo " ANTS, FNIRT, TRAC."
echo " "
exit
}
function transpose() {
awk '
{
if (max_nf<NF)
max_nf=NF
max_nr=NR
for (x=1; x<=NF; ++x)
vector[x, NR]=$x
}
END {
for (x=1; x<=max_nf; ++x) {
for (y=1; y<=max_nr; ++y)
printf("%s ", vector[x, y])
printf("\n")
}
}' ${1}
}
while getopts ":m:s:b:h" OPT; do
case ${OPT} in
m)
METHOD=${OPTARG}
;;
s)
SUB=${OPTARG}
;;
b)
BET=${OPTARG}
;;
h)
help
exit 1
;;
*)
echo "Unknown option: ${OPT}"
usage
exit 1
;;
esac
done
if [ ${OPTIND} -eq 0 ]; then
echo "No options were passed"
shift ${OPTIND}
echo "$# non-option arguments"
usage
exit 1
else
echo "$# option arguments"
fi
args="$@"
BET=FSL
if [ ${METHOD} == "ANTS" ]; then
echo "####################################################################"
echo "MINCANTS Distortion Correction, with ${BET} skull stripping"
echo "####################################################################"
elif [ ${METHOD} == "TRAC" ]; then
echo "####################################################################"
echo "MINCTRACC Distortion Correction, with ${BET} skull stripping"
echo "Warning: No restricted transformation for minctracc!"
echo "####################################################################"
elif [ ${METHOD} == "FNIRT" ]; then
echo "####################################################################"
echo "FNIRT Distortion Correction, with ${BET} skull stripping"
echo "####################################################################"
else
echo "####################################################################"
echo "A distortion correction METHOD should be specified"
echo "WARNING: ANTS will be selected by default!"
echo "####################################################################"
fi
echo "Distortion Correction ${METHOD}: Subject ${SUB}"
echo "####################################################################"
cd ${SUB}
DIRNAME=Distortion_Corrected_T_"${METHOD}"
echo ${DIRNAME}
mkdir ${DIRNAME}
###############################################################################
# DTI
#
cp DTI_DIR60/*.bv* ${DIRNAME}
cp DTI_DIR60/*.nii.gz ${DIRNAME}
cd ${DIRNAME}
# Split DTI volume
fslsplit *.nii.gz
# Conversion to MINC
for VOL in vol*nii*; do
gunzip ${VOL}
VOLUME=$(basename ${VOL} .nii.gz)
# if [ ${BET} == "FSL" ]; then
bet "${VOL}" "${VOLUME}"_bet -R -f 0.1
gunzip "${VOLUME}"_bet.nii.gz
nii2mnc "${VOLUME}"_bet.nii "${VOLUME}"_M.mnc
# elif [ ${BET} = "MINC" ]; then
# nii2mnc "${VOL}".nii "${VOLUME}"_MINC.mnc
# mincbet "${VOLUME}"_MINC.mnc "${VOLUME}"_M
# else
# echo "FSL BET Used as Default!"
# bet "${VOL}" "${VOLUME}"_bet -R -f 0.1
# gunzip "${VOLUME}"_bet.nii.gz
# nii2mnc "${VOLUME}"_bet.nii "${VOLUME}"_M.mnc
# fi
done
###############################################################################
# T2
cp ../T2.nii.gz ./T2.nii.gz
gunzip T2.nii.gz
bet T2.nii T2_brain.nii -R
###############################################################################
# Register T2 to B-ZERO
flirt -interp sinc \
-sincwidth 7 \
-noresample \
-noresampblur \
-sincwindow blackman \
-in T2_brain.nii \
-ref vol0000.nii.gz \
-nosearch \
-o T2_REG \
-omat T2_Trans.txt \
-paddingsize 1
gunzip T2_REG.nii.gz
nii2mnc T2_REG.nii T2_REG.mnc
cp T2_REG.nii T2_REG_F.nii
cp vol0000_M.mnc average_blur.mnc
# Run Non-Linear Regsitration Algorithm
if [ ${METHOD} == "ANTS"]; then
mincANTS 3 \
-m MI[average_blur.mnc,T2_REG.mnc,1,4] \
--number-of-affine-iterations 20x20x10 \
--MI-option 32x16000 \
--Restrict-Deformation 0x1x0 \
--affine-gradient-descent-option 0.5x0.95x1.e-4x1.e-4 \
--use-Histogram-Matching \
-r Gauss[3,0] \
-t SyN[0.5] \
-o Average_OUTPUT.xfm \
-i 10x10
elif [ ${METHOD} == "TRAC" ]; then
nlfit_smr_modelless_trans_mod \
T2_REG.mnc average_blur.mnc \
Average_OUTPUT.xfm
elif [ ${METHOD} == "FNIRT" ]; then
fnirt \
--ref=T2_REG_F.nii \
--in=vol0000.nii \
--iout=FNIRT_NL.nii \
--fout=field_xyz.nii
fslsplit field_xyz.nii NL
mv NL0001.nii.gz Y_deform.nii.gz
gunzip Y_deform.nii.gz
nii2mnc Y_deform.nii
cp Y_deform.mnc X.mnc
cp Y_deform.mnc Z.mnc
minccalc -expression "A[0]*0" X.mnc X_ZERO.mnc
minccalc -expression "A[0]*0" Z.mnc Z_ZERO.mnc
minccalc -expression "A[0]*-1" Y_deform.mnc Y_NEG.mnc
mincconcat \
-concat_dimension t \
X_ZERO.mnc Y_NEG.mnc Z_ZERO.mnc \
FNIRT_TRANSFORM.mnc
fi
###############################################################################
# Collect and clean transforms
# compute the actual linear registrations with MINC tools
parallel --gnu "bestlinreg \
-clobber \
-lsq12 {1} \
vol0000_M.mnc {1/.}.xfm ::: vol*_M.mnc"
# calculate some FSL linear registrations for QC purposesse
parallel --gnu "flirt \
-in {1/.}_bet.nii \
-ref vol0000_bet.nii \
-out {1/.}_flirted.nii.gz \
-paddingsize 1 \
-omat {1/.}_flirt.txt" ::: vol????.nii
# create full/formatted transform files
for VOL in vol*_M.mnc; do
BASE=$(basename ${VOL} .mnc)
echo "MNI Transform File" > ${BASE}_transform.xfm
cat ${BASE}.xfm | tail -5 >> ${BASE}_transform.xfm
echo "Transform_Type = Grid_Transform;" >> ${BASE}_transform.xfm
echo "Displacement_Volume = FNIRT_TRANSFORM.mnc;" >> ${BASE}_transform.xfm
done
# Apply transforms, convert to NIFTI.GZ format
parallel --gnu "mincresample \
-sinc \
-width 2 \
-tfm \
-transformation {1/.}_transform.xfm {1} \
EPI_Trans_{1/.}.mnc" ::: vol????_M.mnc
parallel --gnu "mnc2nii \
EPI_Trans_{1/.}.mnc \
{1/.}_S.nii" ::: vol????_M.mnc
echo "########################################################################"
echo "Transformation Complete: Merging Files"
echo "########################################################################"
cat vol????_flirt.txt > transform.txt
transpose *.bvec > dirs_60.dat
## we need to have this accept an arbitrary number of directions
matlab -nojvm < /quarantine/ENIGMA/finitestrain.m
perl /quarantine/ENIGMA/dattonrrd.pl newdirs.dat newdirs.nhdr
cat newdirs.nhdr |cut -d " " -f 2 | tr '\n' ' ' > bvecs
echo '' >> bvecs
cat newdirs.nhdr | cut -d " " -f 3 | tr '\n' ' ' >> bvecs
echo '' >> bvecs
cat newdirs.nhdr | cut -d " " -f 4 | tr '\n' ' ' >> bvecs
fslmerge -t Distortion_Corrected vol????_M_S.nii
gunzip Distortion_Corrected.nii.gz
cp *.bval DWI.bval
cp bvecs DWI.bvec
# this is super ugly -- I'm calling a version of python that isn't in the minc
# module this script depends on so I can import nibabel appropriately. The
# best work-around will likely be a re-do of the minc-tools module.
#
# For now, uglyness. -- jdv
/usr/bin/python2.7 /quarantine/ENIGMA/nifti2nrrd -i Distortion_Corrected.nii
bet Distortion_Corrected.nii CORR -m -f 0.1
dtifit -k Distortion_Corrected.nii -m CORR_mask.nii.gz -b DWI.bval -r DWI.bvec -o CORR
# Exit distortion correction directory
echo "########################################################################"
echo "Making Sub Directories"
echo "########################################################################"
mkdir FA
mv CORR_FA.nii.gz FA/
cd FA
echo "########################################################################"
echo "Cleaning Temporary Files"
echo "########################################################################"
rm -rf vol*
#cd ..
# Submit script to queue.
# qsub -q main.q -cwd -V -N DIST_"$base" epi2epi.sh
#!/bin/bash