forked from danjonpeterson/dti_preproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdti_preproc.sh
executable file
·213 lines (160 loc) · 7.15 KB
/
dti_preproc.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
#! /bin/sh
usage_exit() {
cat <<EOF
Preprocess DTI Data
Examples:
for fieldmap-based unwarping:
dti_preproc.sh -k raw_diffusion.nii.gz -b bval.txt -r bvec.txt -M brain_mask.nii.gz \\
-f fieldmap_phase.nii.gz -m fieldmap_magnitude.nii.gz \\
-p fieldmap_magnitude_brain_mask -e 93.46 -t 0.567
for blipup-blipbdown based unwarping:
dti_preproc.sh -k raw_diffusion.nii.gz -b bval.txt -r bvec.txt -M brain_mask.nii.gz \\
-q acqparams.txt -i index.txt
Required:
-k <img> : DTI 4D data
-b <bval.txt> : a text file containing a list of b-values
(fsl format - one line)
-r <bvec.txt> : a text file containing a list of b-vectors
(fsl format - three lines)
-M <img> : mask file
And either:
-f <img> : fieldmap image (radian/sec)
-m <img> : fieldmap magnitude image
-e <num> : DTI TE (in ms)
-t <num> : DTI dwell time (in ms)
-p <img> : mask for magnitude image
Or:
-q <file> : acqpars file for topup
-i <file> : index file for topup
Optional:
-c <file> : topup configuration file
-s <num> : % signal loss threshold for fieldmap-based unwarping (default: 10)
-o <dir> : output directory (defaut: current working directory)
-E : don't run the commands, just echo them
-F : fast mode for testing (minimal iterations)
On github: https://github.com/danjonpeterson/dti_preproc.git
EOF
exit 1;
}
#---------variables and defaults---------#
diffusion="PARSE_ERROR_dti" # input raw diffusion file
dph="PARSE_ERROR_dph" # fieldmap phase map
mag="PARSE_ERROR_mag" # fieldmap magnitude image
bval="PARSE_ERROR_bval" # b-values file (in FSL format)
bvec="PARSE_ERROR_vec" # b-vectors file (in FSL format)
mask="PARSE_ERROR_mask" # brain mask file for diffusion data
method="PARSE_ERROR_method" # topup or fugue
bvec_rotation=y # rotate bvecs according to motion correction transforms
configfile=b02b0.cnf # config file. b02b0.cnf actually lives in ${FSLDIR}/etc/flirtsch/
SL=10 # signal loss threshold
outdir=out # output directory
LF=dti_preprocess.log # default log filename
mode=normal # run mode (normal,echo)
fast_testing=n # run with minimal processing for testing
scriptdir=`dirname $0` # directory where dti_preproc scripts live
other_opts="" # flags to pass onto the sub-commands
#------------- parsing parameters ----------------#
if [ "$6" = "" ]; then usage_exit; fi #show help message if fewer than six args
while getopts k:b:r:M:f:m:e:t:p:q:i:c:s:o:EF OPT
do
case "$OPT" in
"k" ) diffusion="$OPTARG";;
"b" ) bval="$OPTARG";;
"r" ) bvec="$OPTARG";;
"M" ) mask="$OPTARG";;
"f" ) dph="$OPTARG"
method="fugue";;
"m" ) mag="$OPTARG";;
"p" ) mag_mask="$OPTARG";;
"t" ) esp="$OPTARG";;
"e" ) te="$OPTARG";;
"q" ) acqparams="$OPTARG"
method="topup";;
"i" ) index="$OPTARG";;
"c" ) configfile="$OPTARG";;
"s" ) SL="$OPTARG";;
"o" ) outdir="$OPTARG";;
"E" ) mode=echo;;
"F" ) fast_testing=y;;
* ) usage_exit;;
esac
done;
#------------- Utility functions ----------------#
T () { # main shell commands are run through here
E=0
if [ "$1" = "-e" ] ; then # just outputting and logging a message with T -e
E=1; shift
fi
cmd="$*"
echo $* | tee -a $LF # echo the command into the console, and the log file
if [ "$E" != "1" ] && [ "$mode" != "echo" ] ; then
$cmd 2>&1 | tee -a $LF # run the command. redirect the output into the log file. Stderr is not directed to the logfile
fi
echo | tee -a $LF # write an empty line to the console and log file
}
error_exit (){
echo "$1" >&2 # Send message to stderr
echo "$1" > $LF # send message to log file
exit "${2:-1}" # Return a code specified by $2 or 1 by default.
}
test_varimg (){ # test if a string is a valid image file
var=$1
if [ "x$var" = "x" ]; then test=0; else test=`imtest $1`; fi
echo $test
}
test_varfile (){ # test if a string is a valid file
var=$1
if [ "x$var" = "x" ]; then test=0 ; elif [ ! -f $var ]; then test=0; else test=1; fi
echo $test
}
#------------- Setting things up ----------------#
## clear, then make the logfile
if [ -e $LF ]; then /bin/rm -f $LF ;fi
touch $LF
## make the output directory
T mkdir -p $outdir
if [ "$mode" = "echo" ]; then
T -e "Running in echo mode - no actual processing done"
fi
if [ "$fast_testing" = "y" ]; then
other_opts=`echo $other_opts -F`
fi
#------------- verifying inputs ----------------#
if [ `test_varimg $diffusion` -eq 0 ]; then
error_exit "ERROR: cannot find image for 4D raw diffusion data: $diffusion"
else
dtidim4=`fslval $diffusion dim4`
fi
if [ `test_varfile $bvec` -eq 0 ]; then error_exit "ERROR: $bvec is not a valid bvec file"; fi
bvecl=`cat $bvec | awk 'END{print NR}'`
bvecw=`cat $bvec | wc -w`
if [ $bvecl != 3 ]; then error_exit "ERROR: bvecs file contains $bvecl lines, it should be 3 lines, each for x, y, z"; fi
if [ "$bvecw" != "`expr 3 \* $dtidim4`" ]; then error_exit "ERROR: bvecs file contains $bvecw words, it should be 3 x $dtidim4 = `expr 3 \* $dtidim4` words"; fi
if [ `test_varfile $bval` -eq 0 ]; then error_exit "ERROR: $bval is not a valid bvals file"; fi
bvall=`cat $bval | awk 'END{print NR}'`; bvalw=`cat $bval | wc -w`
if [ $bvall != 1 ]; then error_exit "ERROR: bvals file contains $bvall lines, it should be 1 lines"; fi
if [ $bvalw != $dtidim4 ]; then error_exit "ERROR: bval file contains $bvalw words, it should be $dtidim4 words"; fi
if [ `test_varimg $mask` -eq 0 ]; then
error_exit "ERROR: cannot find mask image: $mask"
fi
#------------- Motion and Distortion correction ----------------#
if [ "$method" = "topup" ]; then
T -e "Unwarping distortions based on blipup-blipbdown data"
T $scriptdir/unwarp_bupbdown.sh -k $diffusion -a $acqparams -M $mask -c $configfile -o $outdir -b $bval $other_opts
T $scriptdir/motion_correct.sh -k $diffusion -b $bval -r $bvec -M $mask -i $index -a $acqparams -t temp-unwarp_bupbdown/topup_out -o $outdir $other_opts
diffusion=$outdir/mc_unwarped_$diffusion
elif [ "$method" = "fugue" ]; then
T -e "Unwarping distortions based on an acquired fieldmap"
T $scriptdir/motion_correct.sh -k $diffusion -b $bval -r $bvec -o $outdir -M $mask -m eddy $other_opts
T $scriptdir/unwarp_fieldmap.sh -k $outdir/mc_$diffusion -f $dph -m $mag -M $mask -p $mag_mask -o $outdir -s $SL -t $esp -e $te $other_opts
diffusion=$outdir/unwarped_mc_$diffusion
else
error_exit "ERROR: method \"$method\" is neither topup nor fugue"
fi
#-------------- fitting the tensor ------------------#
if [ "$bvec_rotation" = "y" ]; then
bvec=$outdir/bvec_mc.txt
fi
T $scriptdir/fit_tensor.sh -k $diffusion -b $bval -r $bvec -M $outdir/unwarped_brain_mask.nii.gz -o $outdir $other_opts
T -e "Inspect results with the following command:"
T -e "firefox $outdir/*html "