-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathensembletracking.sh
executable file
·468 lines (411 loc) · 16.4 KB
/
ensembletracking.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/bin/bash
#make the script to fail if any of the command fails.
set -e
#DEBUG - echo commands executed (to stderr)
set -x
cat config.json
rm -f tmp.tck
DOPROB=`jq -r '.do_probabilistic' config.json`
DOSTREAM=`jq -r '.do_deterministic' config.json`
DOTENSOR=`jq -r '.do_tensor' config.json`
PROB_CURVS=`jq -r '.prob_curvs' config.json`
DETR_CURVS=`jq -r '.detr_curvs' config.json`
STEPSIZE=`jq -r '.stepsize' config.json`
MINLENGTH=`jq -r '.minlength' config.json`
MAXLENGTH=`jq -r '.maxlength' config.json`
NUMFIBERS=`jq -r '.num_fibers' config.json`
NUMCCFIBERS=`jq -r '.num_cc_fibers' config.json`
NUMORFIBERS=`jq -r '.num_or_fibers' config.json`
NUMMTFIBERS=`jq -r '.num_mt_fibers' config.json`
NUMVZFIBERS=`jq -r '.num_vz_fibers' config.json`
MAXNUMFIBERSATTEMPTED=$(($NUMFIBERS*50))
MAXNUMCCFIBERS=$(($NUMCCFIBERS*50))
MAXNUMORFIBERS=$(($NUMORFIBERS*250000))
MAXNUMMTFIBERS=$(($NUMMTFIBERS*250000))
MAXNUMVZFIBERS=$(($NUMVZFIBERS*250000))
dwi=`jq -r '.dwi' config.json`
if [ $dwi != "null" ]; then
export input_nii_gz=$dwi
export BVECS=`jq -r '.bvecs' config.json`
export BVALS=`jq -r '.bvals' config.json`
fi
dtiinit=`jq -r '.dtiinit' config.json`
if [ $dtiinit != "null" ]; then
export input_nii_gz=$dtiinit/`jq -r '.files.alignedDwRaw' $dtiinit/dt6.json`
export BVECS=$dtiinit/`jq -r '.files.alignedDwBvecs' $dtiinit/dt6.json`
export BVALS=$dtiinit/`jq -r '.files.alignedDwBvals' $dtiinit/dt6.json`
fi
#if max_lmax is empty, auto calculate
MAXLMAX=`jq -r '.max_lmax' config.json`
if [[ $MAXLMAX == "null" || -z $MAXLMAX ]]; then
echo "max_lmax is empty... determining which lmax to use from .bvals"
MAXLMAX=`./calculatelmax.py $BVALS`
echo "Using MAXLMAX: $MAXLMAX"
fi
###################################################################################################
#
# precompute the expected output count by stepping through the tracking logic
#
TOTAL=0
if [ $DOTENSOR == "true" ]; then
TOTAL=$(($TOTAL+$NUMCCFIBERS+$NUMFIBERS))
fi
if [ $DOPROB == "true" ] ; then
for (( i_lmax=2; i_lmax<=$MAXLMAX; i_lmax+=2 )); do
for i_curv in $PROB_CURVS; do
if [ $i_lmax -le 2 ]; then
TOTAL=$(($TOTAL+$NUMORFIBERS+$NUMORFIBERS))
fi
TOTAL=$(($TOTAL+$NUMCCFIBERS+$NUMMTFIBERS+$NUMMTFIBERS+$NUMVZFIBERS+$NUMFIBERS))
done
done
fi
if [ $DOSTREAM == "true" ] ; then
for (( i_lmax=2; i_lmax<=$MAXLMAX; i_lmax+=2 )); do
for i_curv in $DETR_CURVS; do
TOTAL=$(($TOTAL+$NUMCCFIBERS+$NUMMTFIBERS+$NUMMTFIBERS+$NUMVZFIBERS+$NUMFIBERS))
done
done
fi
echo "Expecting $TOTAL streamlines in final ensemble."
#
#
###################################################################################################
#generate grad.b from bvecs/bvals
bvals=$(cat $BVALS | tr , ' ')
bvecs_x=$(cat $BVECS | tr , ' ' | head -1)
bvecs_y=$(cat $BVECS | tr , ' ' | head -2 | tail -1)
bvecs_z=$(cat $BVECS | tr , ' ' | tail -1)
#convert strings to array of numbers
bvecs_x=($bvecs_x)
bvecs_y=($bvecs_y)
bvecs_z=($bvecs_z)
#output grad.b
i=0
true > grad.b
for bval in $bvals; do
echo ${bvecs_x[$i]} ${bvecs_y[$i]} ${bvecs_z[$i]} $bval >> grad.b
i=$((i+1))
done
if [ ! -f convertmif.success ]; then
echo "converting various nii.gz to mif"
rm -f *.mif #if this safe?
mrconvert --quiet wm_anat.nii.gz wm.mif
mrconvert --quiet mask_anat.nii.gz brainmask.mif
mrconvert --quiet cc_anat.nii.gz cc.mif
mrconvert --quiet wm_full.nii.gz tm.mif
mrconvert --quiet wm_lh.nii.gz wm_lh.mif
mrconvert --quiet wm_rh.nii.gz wm_rh.mif
## create extra wm rois
mrconvert --quiet lh_thalamus.nii.gz lh_thalamus.mif
mrconvert --quiet rh_thalamus.nii.gz rh_thalamus.mif
mrconvert --quiet lh_occipital.nii.gz lh_occipital.mif
mrconvert --quiet rh_occipital.nii.gz rh_occipital.mif
mrconvert --quiet lh_motor.nii.gz lh_motor.mif
mrconvert --quiet rh_motor.nii.gz rh_motor.mif
mrconvert --quiet br_stem.nii.gz br_stem.mif
mrconvert --quiet wm_vis.nii.gz wm_vis.mif
mrconvert --quiet wm_fh.nii.gz wm_fh.mif
## create extra seed masks
mradd -quiet lh_thalamus.mif lh_occipital.mif lh_or_seed.mif
mradd -quiet rh_thalamus.mif rh_occipital.mif rh_or_seed.mif
mradd -quiet lh_motor.mif br_stem.mif lh_motor_seed.mif
mradd -quiet rh_motor.mif br_stem.mif rh_motor_seed.mif
mrconvert --quiet $input_nii_gz dwi.mif
echo "done converting"
touch convertmif.success
fi
if [ ! -f dt.mif ] && [ $DOTENSOR == "true" ]; then
echo "fit tensor model (takes about 16 minutes)"
time dwi2tensor dwi.mif -grad grad.b dt.mif
fi
if [ ! -f response.txt ]; then
echo "creating response.txt"
time estimate_response dwi.mif cc.mif -grad grad.b response.txt
fi
for (( i_lmax=2; i_lmax<=$MAXLMAX; i_lmax+=2 )); do
lmaxout=lmax${i_lmax}.mif
if [ ! -f $lmaxout ]; then
echo "csdeconv $lmaxout"
time csdeconv dwi.mif -grad grad.b response.txt -lmax $i_lmax -mask brainmask.mif $lmaxout
fi
done
###################################################################################################
#
# DT_STREAM
#
if [ $DOTENSOR == "true" ]; then
if [ ! -f cc_tensor.tck ] && [ $NUMCCFIBERS -gt 0 ]; then
echo "streamtrack DT_STREAM cc_tensor.tck - number:$NUMCCFIBERS"
timeout 3600 time streamtrack -quiet DT_STREAM dwi.mif tmp.tck \
-seed cc.mif \
-mask tm.mif \
-grad grad.b \
-number $NUMCCFIBERS \
-maxnum $MAXNUMCCFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck cc_tensor.tck
fi
if [ ! -f wm_tensor.tck ] && [ $NUMFIBERS -gt 0 ]; then
echo "streamtrack DT_STREAM wm_tensor.tck - number:$NUMFIBERS"
timeout 3600 time streamtrack -quiet DT_STREAM dwi.mif tmp.tck \
-seed wm.mif \
-mask tm.mif \
-grad grad.b \
-number $NUMFIBERS \
-maxnum $MAXNUMFIBERSATTEMPTED \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck wm_tensor.tck
fi
fi
###################################################################################################
#
# SD_PROB
#
if [ $DOPROB == "true" ]; then
for (( i_lmax=2; i_lmax<=$MAXLMAX; i_lmax+=2 )); do
for i_curv in $PROB_CURVS; do
prefix=csd_lmax${i_lmax}_wm_SD_PROB_curv${i_curv}
out=${prefix}_lo.tck
if [ $i_lmax -le 2 ] && [ ! -f $out ] && [ $NUMORFIBERS -gt 0 ]; then
echo "streamtrack SD_PROB $out"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed lh_or_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMORFIBERS \
-maxnum $MAXNUMORFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include lh_thalamus.mif \
-include lh_occipital.mif \
-exclude wm_fh.mif \
-exclude wm_rh.mif \
-exclude br_stem.mif
mv tmp.tck $out
fi
out=${prefix}_ro.tck
if [ ${i_lmax} -le 2 ] && [ ! -f $out ] && [ $NUMORFIBERS -gt 0 ] ; then
echo "streamtrack SD_PROB $out - number:$NUMORFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed rh_or_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMORFIBERS \
-maxnum $MAXNUMORFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include rh_thalamus.mif \
-include rh_occipital.mif \
-exclude wm_fh.mif \
-exclude wm_lh.mif \
-exclude br_stem.mif
mv tmp.tck $out
fi
out=${prefix}_cc.tck
if [ ! -f $out ] && [ $NUMCCFIBERS -gt 0 ]; then
echo "streamtrack SD_PROB $out - number:$NUMCCFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed cc.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMCCFIBERS \
-maxnum $MAXNUMCCFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
#TODO this times out too often (NUMMTFIBERS now default to 0)
out=${prefix}_lm.tck
if [ ! -f $out ] && [ $NUMMTFIBERS -gt 0 ]; then
echo "streamtrack SD_PROB $out - number:$NUMMTFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed lh_motor_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMMTFIBERS \
-maxnum $MAXNUMMTFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include lh_motor.mif \
-include br_stem.mif
mv tmp.tck $out
fi
#TODO this times out too often (NUMMTFIBERS now default to 0)
out=${prefix}_rm.tck
if [ ! -f $out ] && [ $NUMMTFIBERS -gt 0 ]; then
echo "streamtrack SD_PROB $out - number:$NUMMTFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed rh_motor_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMMTFIBERS \
-maxnum $MAXNUMMTFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include rh_motor.mif \
-include br_stem.mif
mv tmp.tck $out
fi
out=${prefix}_vz.tck
if [ ! -f $out ] && [ $NUMVZFIBERS -gt 0 ] ; then
echo "streamtrack SD_PROB $out - number:$NUMVZFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed wm_vis.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMVZFIBERS \
-maxnum $MAXNUMVZFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
out=${prefix}_wb.tck
if [ ! -f $out ] && [ $NUMFIBERS -gt 0 ]; then
echo "streamtrack SD_PROB $out - number:$NUMFIBERS"
timeout 3600 time streamtrack -quiet SD_PROB lmax${i_lmax}.mif tmp.tck \
-seed wm.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMFIBERS \
-maxnum $MAXNUMFIBERSATTEMPTED \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
done
done
fi
###################################################################################################
#
# SD_STREAM
#
if [ $DOSTREAM == "true" ] ; then
for (( i_lmax=2; i_lmax<=$MAXLMAX; i_lmax+=2 )); do
for i_curv in $DETR_CURVS; do
prefix=csd_lmax${i_lmax}_wm_SD_STREAM_curv${i_curv}
out=${prefix}_cc.tck
if [ ! -f $out ] && [ $NUMCCFIBERS -gt 0 ]; then
echo "streamtrack SD_STREAM $out - number:$NUMCCFIBERS"
timeout 3600 time streamtrack -quiet SD_STREAM lmax${i_lmax}.mif tmp.tck \
-seed cc.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMCCFIBERS \
-maxnum $MAXNUMCCFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
#TODO this times out too often (NUMMTFIBERS now default to 0)
out=${prefix}_lm.tck
if [ ! -f $out ] && [ $NUMMTFIBERS -gt 0 ]; then
echo "streamtrack SD_STREAM $out - number:$NUMMTFIBERS"
timeout 3600 time streamtrack -quiet SD_STREAM lmax${i_lmax}.mif tmp.tck \
-seed lh_motor_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMMTFIBERS \
-maxnum $MAXNUMMTFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include lh_motor.mif \
-include br_stem.mif
mv tmp.tck $out
fi
#TODO this times out too often (NUMMTFIBERS now default to 0)
out=${prefix}_rm.tck
if [ ! -f $out ] && [ $NUMMTFIBERS -gt 0 ]; then
echo "streamtrack SD_STREAM $out - number:$NUMMTFIBERS"
timeout 3600 time streamtrack -quiet SD_STREAM lmax${i_lmax}.mif tmp.tck \
-seed rh_motor_seed.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMMTFIBERS \
-maxnum $MAXNUMMTFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH \
-include rh_motor.mif \
-include br_stem.mif
mv tmp.tck $out
fi
out=${prefix}_vz.tck
if [ ! -f $out ] && [ $NUMVZFIBERS -gt 0 ]; then
echo "streamtrack SD_STREAM $out - number:$NUMVZFIBERS"
timeout 3600 time streamtrack -quiet SD_STREAM lmax${i_lmax}.mif tmp.tck \
-seed wm_vis.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMVZFIBERS \
-maxnum $MAXNUMVZFIBERS \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
out=${prefix}_wb.tck
if [ ! -f $out ] && [ $NUMFIBERS -gt 0 ]; then
echo "streamtrack SD_STREAM $out - number:$NUMFIBERS"
timeout 3600 time streamtrack -quiet SD_STREAM lmax${i_lmax}.mif tmp.tck \
-seed wm.mif \
-mask tm.mif \
-grad grad.b \
-curvature $i_curv \
-number $NUMFIBERS \
-maxnum $MAXNUMFIBERSATTEMPTED \
-step $STEPSIZE \
-minlength $MINLENGTH \
-length $MAXLENGTH
mv tmp.tck $out
fi
done
done
fi
###################################################################################################
echo "done tracking. creating the final track.tck"
holder=(*.tck*)
cat_tracks track.tck ${holder[*]}
track_info track.tck > track_info.txt
## hard check of count
COUNT=`track_info track.tck | grep -w 'count' | awk '{print $2}'`
echo "Ensemble tractography generated $COUNT of a requested $TOTAL"
## adding product.json /w streamline count
echo "{\"count\": $COUNT}" > product.json
#if [ $COUNT -ne $TOTAL ]; then
# echo "Incorrect count. Tractography failed."
# rm track.tck
# exit 1
#fi
# reduce storage load
rm csd*.tck
rm *tensor.tck
rm *.mif
#remove all .nii.gz except wm_full (as mask.nii.gz)
#cp wm_full.nii.gz wm_full.nii.gz.backup
#rm *.nii.gz
#cp wm_full.nii.gz.backup mask.nii.gz