-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldfsl.sh
executable file
·288 lines (254 loc) · 9.36 KB
/
ldfsl.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
#!/bin/sh
# Shell script to automate running of fsl to process DTI (diffusion) and
# MPRAGE (structural) data from DICOMS, for probabilistic tractography.
# Requires fsl, dcmtk, condor, fslview, dcn2nii
# Expected command line inputs are <MPRAGE directory>, <dti directory>,
# <output directory>. Each input directory should contain only DICOMs.
# test correct number of command line arguments and 1st 2 are directories
if [ $# -ne 3 ] || ! [ -d $1 ] || ! [ -d $2 ]
then
echo "Usage: $0 <MPRAGE dir> <DTI dir> <output dir>" >&2
exit 1
fi
str_dir=$1
dti_dir=$2
out_dir=$3
# get directory of this script
scriptdir=$(dirname $0);
# check that directories contain dicom files
if ! dcmftest $str_dir/* >/dev/null
then
echo "$0: $str_dir contains one or more non-DICOM files" >&2
exit 1
fi
if ! dcmftest $dti_dir/* >/dev/null
then
echo "$0: $dti_dir contains one or more non-DICOM files" >&2
exit 1
fi
# if destination directory exists, but is not a directory, exit
if [ -e $out_dir ] && ! [ -d $out_dir ]
then
echo "$0: $out_dir exists and is not a directory" >&2
exit 1
fi
# if destination directory doesn't exist, create it
if ! [ -d $out_dir ]
then
if ! mkdir $out_dir
then
echo "$0: Couldn't create $out_dir" >&2
exit 1
fi
fi
# if destination directory is non-empty, prompt to delete
if [ "`ls -A $out_dir`" ]
then
echo "$out_dir is not empty - delete contents? (y/n)"
read answer
if [ "$answer" = 'y' ]
then
rm -r $out_dir/*
fi
fi
# start a log file
if [ -e $out_dir/ldfsl.log ]
then
echo "Recommencing processing `date`" >> $out_dir/ldfsl.log
else
echo "Started processing `date`" > $out_dir/ldfsl.log
fi
# time starts now
start_time=`date +%s`
# check log and do if necessary
if ! grep 'finished nii conversion' $out_dir/ldfsl.log > /dev/null
then
# convert structural dicom to nii
echo "$0: Converting structural DICOM to nii"
dcm2nii -o $out_dir $str_dir/*
# rename and remove files
mv $out_dir/co*.nii.gz $out_dir/mprage
rm $out_dir/*.nii.gz
# convert dti dicom to nii
echo "$0: Converting dti DICOM to nii"
dcm2nii -o $out_dir $dti_dir/*
# rename and remove files
mv $out_dir/*.nii.gz $out_dir/diff.nii.gz
mv $out_dir/mprage $out_dir/mprage.nii.gz
mv $out_dir/*bvec $out_dir/bvecs
mv $out_dir/*bval $out_dir/bvals
# write log
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished nii conversion at $elapsed min" >> $out_dir/ldfsl.log
fi
# now we want to do some background work
export out_dir
export start_time
if ! grep 'finished structural segmentation' $out_dir/ldfsl.log > /dev/null
then
(
echo "$0: cropping structural volume..."
robustfov -r $out_dir/mprage_ax -i $out_dir/mprage
echo "$0: brain-segmenting structural volume..."
bet $out_dir/mprage_ax $out_dir/mprage_brain -R -f 0.5 -g 0
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished structural segmentation at $elapsed min" >> $out_dir/ldfsl.log
)&
str_process=$!
echo "$0: structural analysis started with pid $str_process"
fi
if ! grep 'finished dti segmentation' $out_dir/ldfsl.log > /dev/null
then
(
echo "$0: extracting b0 volume..."
fslroi $out_dir/diff $out_dir/nodif 0 1
echo "$0: segmenting b0 volume..."
bet $out_dir/nodif $out_dir/nodif_brain -f 0.3 -g 0 -m
echo "$0: performing motion and eddy correction..."
eddy_correct $out_dir/diff $out_dir/data 0
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished dti segmentation at $elapsed min" >> $out_dir/ldfsl.log
)&
dti_process=$!
echo "$0: diffusion analysis started with pid $dti_process"
fi
# dti process must be complete before crossing-fibre analysis
wait $dti_process
# peform crossing fibre analysis
if ! grep 'finished crossing fibre analysis' $out_dir/ldfsl.log > /dev/null
then
echo "$0: performing crossing fibre analysis (may take hours)"
FSLPARALLEL=condor
export FSLPARALLEL
if [ -d "$out_dir.bedpostX" ]
then
if [ -e "$out_dir.bedpostX/cancel" ]
then
echo "Cancel all condor jobs before continuing? (recommended) (y/n)";
read answer
if [ "$answer" = 'y' ]
then
condor_rm -all
fi
fi
rm -rf "$out_dir.bedpostX"
fi
bedpostx $out_dir --nf=2 --fudge=1 --bi=1000
# wait for bedpost to complete
$scriptdir/monlog.pl $out_dir
# write to log
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished crossing fibre analysis at $elapsed min" >> $out_dir/ldfsl.log
fi
# both processes must be complete before continuing
wait $str_process
# generate registration transforms
std_space=/usr/share/fsl/data/standard/MNI152_T1_2mm_brain
bedpost_dir=$out_dir.bedpostX
if ! grep 'finished registration transforms' $out_dir/ldfsl.log > /dev/null
then
echo "$0: performing registration transforms..."
flirt -in $bedpost_dir/nodif_brain -ref $out_dir/mprage_brain.nii.gz \
-omat $bedpost_dir/xfms/diff2str.mat -searchrx -90 90 -searchry -90 90
convert_xfm -omat $bedpost_dir/xfms/str2diff.mat \
-inverse $bedpost_dir/xfms/diff2str.mat
flirt -in $out_dir/mprage_brain.nii.gz -ref $std_space \
-omat $bedpost_dir/xfms/str2standard.mat -searchrx -90 90 -searchry -90 90 \
-searchrz -90 90 -dof 12 -cost corratio
convert_xfm -omat $bedpost_dir/xfms/standard2str.mat \
-inverse $bedpost_dir/xfms/str2standard.mat
convert_xfm -omat $bedpost_dir/xfms/diff2standard.mat \
-concat $bedpost_dir/xfms/str2standard.mat $bedpost_dir/xfms/diff2str.mat
convert_xfm -omat $bedpost_dir/xfms/standard2diff.mat \
-inverse $bedpost_dir/xfms/diff2standard.mat
# transform structural to standard space
flirt -in $out_dir/mprage_brain.nii.gz -applyxfm \
-init $bedpost_dir/xfms/str2standard.mat -out $out_dir/mprage_std.nii.gz \
-paddingsize 0.0 -interp trilinear -ref $std_space
# write to log
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished registration transforms at $elapsed min" >> $out_dir/ldfsl.log
fi
# perform probabilistic tracking corticospinal tract
# NB change template directory as required
template_dir=/home/brain/ldfsl/templates
if ! grep 'finished tracking' $out_dir/ldfsl.log > /dev/null
then
(
[ -d $out_dir/cst ] && rm -r $out_dir/cst
mkdir -p $out_dir/cst
midbrain_mask=$template_dir/std_midbrain.nii.gz
waypoints_list=$template_dir/waypoints.txt
probtrackx2 -x $midbrain_mask -l --onewaycondition -c 0.2 -S 2000 \
--steplength=0.5 -P 5000 --fibthresh=0.01 --distthresh=0.0 --sampvox=0.0 \
--xfm=$bedpost_dir/xfms/standard2diff.mat --forcedir --opd \
-s $bedpost_dir/merged -m $bedpost_dir/nodif_brain_mask --dir=$out_dir/cst \
--waypoints=$waypoints_list --waycond=AND
)&
cst_process=$!
# perform probabilistic tracking right optic radiation
(
[ -d $out_dir/right_or ] && rm -r $out_dir/right_or
mkdir -p $out_dir/right_or
r_lat_gen_bod=$template_dir/right_lateral_geniculate_body.nii.gz
r_or_waypoints_list=$template_dir/r_or_waypoints.txt
probtrackx2 -x $r_lat_gen_bod -l --onewaycondition -c 0.2 -S 2000 \
--steplength=0.5 -P 5000 --fibthresh=0.01 --distthresh=0.0 --sampvox=0.0 \
--xfm=$bedpost_dir/xfms/standard2diff.mat --forcedir --opd \
-s $bedpost_dir/merged -m $bedpost_dir/nodif_brain_mask \
--dir=$out_dir/right_or --waypoints=$r_or_waypoints_list --waycond=AND
)&
ror_process=$!
# perform probabilistic tracking left optic radiation
(
[ -d $out_dir/left_or ] && rm -r $out_dir/left_or
mkdir -p $out_dir/left_or
l_lat_gen_bod=$template_dir/left_lateral_geniculate_body.nii.gz
l_or_waypoints_list=$template_dir/l_or_waypoints.txt
probtrackx2 -x $l_lat_gen_bod -l --onewaycondition -c 0.2 -S 2000 \
--steplength=0.5 -P 5000 --fibthresh=0.01 --distthresh=0.0 --sampvox=0.0 \
--xfm=$bedpost_dir/xfms/standard2diff.mat --forcedir --opd \
-s $bedpost_dir/merged -m $bedpost_dir/nodif_brain_mask \
--dir=$out_dir/left_or --waypoints=$l_or_waypoints_list --waycond=AND
)&
lor_process=$!
# perform probabilistic tracking left arcuate fasciculus
(
[ -d $out_dir/left_arcuate ] && rm -r $out_dir/left_arcuate
mkdir -p $out_dir/left_arcuate
l_sup_temp=$template_dir/std_left_sup_temporal_gyrus_posterior.nii.gz
l_arcuate_waypoints_list=$template_dir/l_arcuate_waypoints.txt
probtrackx2 -x $l_sup_temp -l --onewaycondition -c 0.2 -S 2000 \
--steplength=0.5 -P 5000 --fibthresh=0.01 --distthresh=0.0 --sampvox=0.0 \
--xfm=$bedpost_dir/xfms/standard2diff.mat --forcedir --opd \
-s $bedpost_dir/merged -m $bedpost_dir/nodif_brain_mask \
--dir=$out_dir/left_arcuate --waypoints=$l_arcuate_waypoints_list \
--waycond=AND
)&
larcuate_process=$!
# wait for finish
wait $cst_process
wait $ror_process
wait $lor_process
wait $larcuate_process
# write to log
end_time=`date +%s`
elapsed=`expr \( $end_time - $start_time \) / 60`
echo "finished tracking at $elapsed min" >> $out_dir/ldfsl.log
fi
# print elapsed time
end_time=`date +%s`
hours_elapsed=`expr \( $end_time - $start_time \) / 3600`
mins_elapsed=`expr \( $end_time - $start_time - $hours_elapsed \* 3600 \) / 60`
echo "$0 has taken $hours_elapsed h $mins_elapsed m"
echo "finished all at $hours_elapsed h $mins_elapsed m" >> $out_dir/ldfsl.log
# launch fslview
fslview $out_dir/mprage_std -b 0,1000 $out_dir/cst/fdt_paths -b 1000,5000 \
$out_dir/right_or/fdt_paths -b 1000,5000 \
$out_dir/left_or/fdt_paths -b 1000,5000 \
$out_dir/left_arcuate/fdt_paths -b 1000,5000