-
Notifications
You must be signed in to change notification settings - Fork 12
/
KUL_dwiprep_group_fba.sh
executable file
·856 lines (566 loc) · 26.8 KB
/
KUL_dwiprep_group_fba.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#!/bin/bash -e
#set -x
# Bash shell script to process diffusion & structural 3D-T1w MRI data
#
# Requires Mrtrix3
#
# @ Stefan Sunaert - UZ/KUL - [email protected]
# @ Ahmed Radwan - KUL - [email protected]
#
# v1.0 - dd 19/11/2021 - beta version
v="v1.0 - dd 19/11/2021"
# Changes made by AR:
# 1- Updated to use new MRTrix3
# 2- Made the suffix an optional argument
# 3- Template construction and following steps now use the wmfod_norm rather than wmfod
# 4- Use same masks generated by KUL_dwiprep rather than generate new ones
# 5- algorithms can now be single shell single tissue, single shell multi-tissue, or multi-shell multi-tissue
# ----------------------------------- MAIN ---------------------------------------------
# this script defines a few functions:
# - Usage (for information to the novice user)
kul_main_dir=`dirname "$0"`
source $kul_main_dir/KUL_main_functions.sh
cwd=$(pwd)
ncpu_foreach=6
# suffix="_reg2T1w"
#suffix=""
#select_shells="0 700 1000 2000"
select_shells=""
# FUNCTIONS --------------
# function Usage
function Usage {
cat <<USAGE
`basename $0` performs a group fixel based analysis
Usage:
`basename $0` -g group_name <OPT_ARGS>
Example:
`basename $0` -g group_first_32 -n 6 -t "pat01 pat02 pat03 pat04 pat05 con01 con02 con03 con04 con05"
Required arguments:
-g: group_name
Optional arguments:
-a: algorithm: single shell single tissue (ssst), single shell multi tissue (ssmt), multi-shell multi-tissue (default=msmt)
-t: subjects used for population_template (useful if you have more than 30 to 40 subjects, otherwise the template building takes very long)
-n: number of cpu for parallelisation
-s: suffix of input dwis. Options are: 1- to leave it blank (to use native dMRI space processed dMRIs), 2- _reg2T1w (to use the processed dMRIs in native T1 space)
-v: show output from mrtrix commands
USAGE
exit 1
}
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
ncpu=6 # default if option -n is not given
silent=1 # default if option -v is not given
algo=msmt
# Set required options
g_flag=0
t_flag=0
if [ "$#" -lt 1 ]; then
Usage >&2
exit 1
else
while getopts "n:g:t:a:v" OPT; do
case $OPT in
n) #ncpu
ncpu=$OPTARG
;;
g) #group_name
group_name="$OPTARG"
g_flag=1
;;
t) #templatesubjects
templatesubjects="$OPTARG"
t_flag=1
;;
a) #algorithm
algo="$OPTARG"
;;
s) #suffix
suffix="$OPTARG"
;;
v) #verbose
silent=0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo
Usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo
Usage >&2
exit 1
;;
esac
done
fi
# check for required options
if [ $g_flag -eq 0 ] ; then
echo
echo "Option -p is required: give the BIDS name of the participant." >&2
echo
exit 2
fi
# MRTRIX verbose or not?
if [ $silent -eq 1 ] ; then
export MRTRIX_QUIET=1
fi
# check suffix and use blank if not set
# Add a third condition here if needed
if [ -z $suffix ]; then
suffix=""
echo "No suffix is specified for dMRI data, a blank suffix will be used"
echo "We will use the processed dMRIs in native diffusion space"
else
echo "dMRI suffix is specified as ${suffix}"
echo "We will use the processed dMRIs in native T1 space"
fi
# REST OF SETTINGS ---
# timestamp
start=$(date +%s)
# Some parallelisation
FSLPARALLEL=$ncpu; export FSLPARALLEL
OMP_NUM_THREADS=$ncpu; export OMP_NUM_THREADS
d=$(date "+%Y-%m-%d_%H-%M-%S")
log=log/log_${d}.txt
# --- MAIN ----------------
# make dirs
mkdir -p dwiprep/${group_name}/fba/subjects
if [ "$algo" = "ssst" ]; then
mkdir -p dwiprep/${group_name}/fba/dwiintensitynorm/dwi_input
mkdir -p dwiprep/${group_name}/fba/dwiintensitynorm/mask_input
fi
cd dwiprep/${group_name}/fba
# find the preproced mifs
if [ ! -f data_prep.done ]; then
echo " Preparing data in dwiprep/${group_name}/fba/"
search_subjects=($(find ${cwd}/dwiprep/sub-* -type f | grep dwi_preproced${suffix}.mif | sort ))
num_sessions=${#search_subjects[@]}
for i in ${search_subjects[@]}
do
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
#echo $i
#echo $sub
#echo $ses
s=${sub}_${ses}
# if [[ -z ${ses} ]]; then
# s=${sub}_${ses}
# else
# s=${sub}
# fi
mkdir -p ${cwd}/dwiprep/${group_name}/fba/subjects/${s}
ln -sfn $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dwi_preproced${suffix}.mif
if [ "$algo" = "ssst" ]; then
ln -sfn $i ${cwd}/dwiprep/${group_name}/fba/dwiintensitynorm/dwi_input/${s}_dwi_preproced${suffix}.mif
fi
done
# find the preproced masks
# need to make sure this is correct in case KUL_dwiprep_anat.sh is used in advance
search_subjects=($(find ${cwd}/dwiprep/sub-* -type f | grep dwi_mask${suffix}.nii.gz | sort ))
num_subjects=${#search_subjects[@]}
for i in ${search_subjects[@]}
do
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
if [ "$algo" = "ssst" ]; then
mrconvert $i ${cwd}/dwiprep/${group_name}/fba/dwiintensitynorm/mask_input/${s}_dwi_preproced${suffix}.mif -force
fi
done
echo "Algo: $algo"
if [ "$algo" = "ssmt" ] || [ "$algo" = "msmt" ]; then
# find the response functions
search_subjects=($(find ${cwd}/dwiprep -type f | grep dhollander_csf_response.txt | sort ))
num_subjects=${#search_subjects[@]}
for i in ${search_subjects[@]}
do
#s=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
ln -sfn $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dhollander_csf_response.txt
done
# find the response functions
search_subjects=($(find ${cwd}/dwiprep -type f | grep dhollander_gm_response.txt | sort ))
num_subjects=${#search_subjects[@]}
for i in ${search_subjects[@]}
do
#s=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
ln -sfn $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dhollander_gm_response.txt
done
# find the response functions
search_subjects=($(find ${cwd}/dwiprep -type f | grep dhollander_wm_response.txt | sort ))
num_subjects=${#search_subjects[@]}
for i in ${search_subjects[@]}
do
#s=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
ln -sfn $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dhollander_wm_response.txt
done
fi
echo "done" > data_prep.done
else
echo " Preparing data in dwiprep/${group_name}/fba/ already done"
fi
# Option to select certain shells from the data
if [ "$select_shells" = "" ]; then
echo "No shell selection, just continue"
else
echo "Shells $select_shells will now be used in further analysis"
echo "NOT YET IMPLEMENTED!!!!"
fi
# STEP 1 - Intensity Normalisation (only for ST data)
#dwiintensitynorm ../dwiintensitynorm/dwi_input/ ../dwiintensitynorm/mask_input/ ../dwiintensitynorm/dwi_output/ ../dwiintensitynorm/fa_template.mif ../dwiintensitynorm/fa_template_wm_mask.mif
if [ "$algo" = "ssst" ]; then
if [ ! -f dwiintensitynorm/fa_template_wm_mask.mif ]; then
echo " Doing Intensity Normalisation"
dwinromalise group dwiintensitynorm/dwi_input/ dwiintensitynorm/mask_input/ \
dwiintensitynorm/dwi_output/ dwiintensitynorm/fa_template.mif \
dwiintensitynorm/fa_template_wm_mask.mif -nthreads $ncpu -force
mrinfo dwiintensitynorm/dwi_output/* -property dwi_norm_scale_factor > CHECK_dwi_norm_scale_factor.txt
else
echo " Intensity Normalisation already done"
fi
# Adding a subject
# dwi2tensor new_subject/dwi_denoised_unringed_preproc_unbiased.mif -mask new_subject/dwi_temp_mask.mif - | tensor2metric - -fa - | mrregister -force \
# ../dwiintensitynorm/fa_template.mif - -mask2 new_subject/dwi_temp_mask.mif -nl_scale 0.5,0.75,1.0 -nl_niter 5,5,15 -nl_warp - /tmp/dummy_file.mif | \
# mrtransform ../dwiintensitynorm/fa_template_wm_mask.mif -template new_subject/dwi_denoised_unringed_preproc_unbiased.mif -warp - - | dwinormalise \
# new_subject/dwi_denoised_unringed_preproc_unbiased.mif - ../dwiintensitynorm/dwi_output/new_subject.mif
fi
# Link back the normalised data
cd ${cwd}/dwiprep/${group_name}/fba/subjects
if [ "$algo" = "ssst" ]; then
for_each -force * : ln -sfn ${cwd}/dwiprep/${group_name}/fba/dwiintensitynorm/dwi_output/PRE_dwi_preproced${suffix}.mif \
${cwd}/dwiprep/${group_name}/fba/subjects/IN/dwi_preproced${suffix}_normalised.mif
fi
# STEP 2 - Computing an (average) white matter response function
# foreach * : dwi2response tournier IN/dwi_denoised_unringed_preproc_unbiased_normalised.mif IN/response.txt
if [ ! -f ../average_response.done ]; then
echo " Computing an (average) white matter response function"
if [ "$algo" = "ssst" ]; then
for_each -force * : dwi2response tournier IN/dwi_preproced${suffix}_normalised.mif \
IN/response.txt -nthreads $ncpu -force
responsemean */response.txt ../group_average_response.txt
else
responsemean */dhollander_wm_response.txt ../group_average_response_wm.txt
responsemean */dhollander_gm_response.txt ../group_average_response_gm.txt
responsemean */dhollander_csf_response.txt ../group_average_response_csf.txt
fi
if [ $? -eq 0 ]; then
echo "done" > ../average_response.done
fi
else
echo " Computing of an (average) white matter response function alrady done"
fi
# Use same masks generated by KUL_dwiprep
# foreach * : dwi2mask IN/dwi_denoised_unringed_preproc_unbiased_normalised_upsampled.mif IN/dwi_mask_upsampled.mif
search_subjects=($(find ${cwd}/dwiprep -type f | grep dwi_mask${suffix}.nii.gz | sort ))
num_subjects=${#search_subjects[@]}
if [ ! -f ../mask.done ]; then
echo " Compute new brain mask images"
# if [ "$algo" = "ssst" ]; then
# for_each -force -nthreads ${ncpu_foreach} * : dwi2mask IN/dwi_preproced${suffix}_normalised.mif IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu -force
# else
for i in ${search_subjects[@]}; do
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
# this mask isn't really normalized but shouldn't matter so much
mrconvert $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dwi_preproced${suffix}_mask.mif -force
done
# fi
if [ $? -eq 0 ]; then
echo "done" > ../mask.done
fi
else
echo " Computing of new brain mask images already done"
fi
# STEP 3 - Fibre Orientation Distribution estimation (spherical deconvolution)
# see https://mrtrix.readthedocs.io/en/latest/fixel_based_analysis/st_fibre_density_cross-section.html
# Note that dwi2fod csd can be used, however here we use dwi2fod msmt_csd (even with single shell data) to benefit from the hard non-negativity constraint,
# which has been observed to lead to more robust outcomes
# foreach * : dwiextract IN/dwi_denoised_unringed_preproc_unbiased_normalised_upsampled.mif - \| dwi2fod msmt_csd - ../group_average_response.txt IN/wmfod.mif -mask IN/dwi_mask_upsampled.mif
if [ ! -f ../fod_estimation.done ]; then
echo " Performing FOD estimation"
if [ "$algo" = "ssst" ]; then
for_each -force -nthreads ${ncpu_foreach} * : dwiextract IN/dwi_preproced${suffix}_normalised.mif - \
\| dwi2fod msmt_csd - ../group_average_response.txt IN/wmfod.mif \
-mask IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu -force
elif [ "$algo" = "ssmt" ]; then
for_each -force -nthreads ${ncpu_foreach} * : dwi2fod msmt_csd IN/dwi_preproced${suffix}.mif \
../group_average_response_wm.txt IN/wmfod_nogm.mif \
../group_average_response_csf.txt IN/csf_nogm.mif \
-mask IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu -force
elif [ "$algo" = "msmt" ]; then
for_each -force -nthreads ${ncpu_foreach} * : dwi2fod msmt_csd IN/dwi_preproced${suffix}.mif \
../group_average_response_wm.txt IN/wmfod.mif \
../group_average_response_gm.txt IN/gm.mif \
../group_average_response_csf.txt IN/csf.mif \
-mask IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu -force
fi
if [ $? -eq 0 ]; then
echo "done" > ../fod_estimation.done
fi
fi
# STEP 3B - for multi-tissue only - Joint bias field correction and intensity normalisation
if [ "$algo" = "msmt" ]; then
if [ ! -f ../mtnormalise.done ]; then
for_each -force -nthreads ${ncpu_foreach} * : mtnormalise IN/wmfod.mif IN/wmfod_norm.mif \
IN/gm.mif IN/gm_norm.mif IN/csf.mif IN/csf_norm.mif \
-mask IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu
if [ $? -eq 0 ]; then
echo "done" > ../mtnormalise.done
fi
fi
elif [ "$algo" = "ssmt" ]; then
if [ ! -f ../mtnormalise.done ]; then
for_each -force -nthreads ${ncpu_foreach} * : mtnormalise IN/wmfod_nogm.mif IN/wmfod_norm.mif \
IN/csf_nogm.mif IN/csf_norm.mif \
-mask IN/dwi_preproced${suffix}_mask.mif -nthreads $ncpu
if [ $? -eq 0 ]; then
echo "done" > ../mtnormalise.done
fi
fi
fi
# STEP 4 - Generate a study-specific unbiased FOD template
mkdir -p ../template/fod_input
mkdir -p ../template/mask_input
# declare -a links
templatesubjects_a=(${templatesubjects})
echo ${templatesubjects_a[@]}
if [ ! -f ../template/wmfod_template.mif ]; then
echo " Generating FOD template"
# search_sessions=($(find ${cwd}/dwiprep/${group_name}/fba/subjects | grep wmfod_norm.mif | sort ))
search_sessions=($(ls -f ${cwd}/dwiprep/${group_name}/fba/subjects/*/wmfod_norm.mif | sort ))
for t in ${!search_sessions[@]}; do
#links[$bb]=1
# s=$(echo $t | awk -F 'subjects/' '{print $2}' | awk -F '/' '{print $1}')
s=$(echo ${search_sessions[$t]} | rev | cut -d '/' -f2 | rev)
# echo $s
# echo $t
if [ $t_flag -eq 1 ]; then
# Don't link subjects not given in -t
for hb in ${!templatesubjects_a[@]}; do
# echo "${templatesubjects_a[$hb]}"
if [[ "${s}" == "${templatesubjects_a[$hb]}" ]]; then
ln -sfn ${search_sessions[$t]} ${cwd}/dwiprep/${group_name}/fba/template/fod_input/${s}_wmfod_norm.mif
ln -sfn ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dwi_preproced${suffix}_*mask.mif \
${cwd}/dwiprep/${group_name}/fba/template/mask_input/${s}_mask.mif
fi
done
else
ln -sfn ${search_sessions[$t]} ${cwd}/dwiprep/${group_name}/fba/template/fod_input/${s}_wmfod_norm.mif
ln -sfn ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/dwi_preproced${suffix}_*mask.mif \
${cwd}/dwiprep/${group_name}/fba/template/mask_input/${s}_mask.mif
fi
done
population_template ../template/fod_input -mask_dir ../template/mask_input ../template/wmfod_template.mif \
-voxel_size 1.3 -nthreads $ncpu
else
echo " FOD template already generated"
fi
# Register all subject FOD images to the FOD template
# foreach -${ncpu_foreach} * : mrregister IN/wmfod.mif -mask1 IN/dwi_mask_upsampled.mif ../template/wmfod_template.mif -nl_warp IN/subject2template_warp.mif IN/template2subject_warp.mif
if [ ! -f ../fod_reg2template.done ]; then
echo " Registering all subject FOD images to the FOD template"
# at this point ssst, ssmt, and msmt are the same no?
for_each -force -nthreads ${ncpu_foreach} * : mrregister IN/wmfod_norm.mif -mask1 IN/dwi_preproced${suffix}_mask.mif \
../template/wmfod_template.mif \
-nl_warp IN/subject2template_warp.mif IN/template2subject_warp.mif -nthreads $ncpu -force
if [ $? -eq 0 ]; then
echo "done" > ../fod_reg2template.done
fi
else
echo " Registration of all subject FOD images to the FOD template already done"
fi
# Compute the template mask (intersection of all subject masks in template space)
#foreach * : mrtransform IN/dwi_mask_upsampled.mif -warp IN/subject2template_warp.mif -interp nearest -datatype bit IN/dwi_mask_in_template_space.mif
if [ ! -f ../template/template_mask.mif ]; then
echo " Compute the template mask"
if [ "$algo" = "ssst" ]; then
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/dwi_preproced${suffix}_normalised_mask.mif -warp IN/subject2template_warp.mif \
-interp nearest -datatype bit IN/dwi_mask_in_template_space.mif -nthreads $ncpu -force
else
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/dwi_preproced${suffix}_mask.mif -warp IN/subject2template_warp.mif \
-interp nearest -datatype bit IN/dwi_mask_in_template_space.mif -nthreads $ncpu -force
fi
mrmath */dwi_mask_in_template_space.mif min ../template/template_mask.mif -datatype bit -nthreads $ncpu
else
echo " Computation of the template mask already done"
fi
# Compute a white matter template analysis fixel mask
# fod2fixel -mask ../template/template_mask.mif -fmls_peak_value 0.10 ../template/wmfod_template.mif ../template/fixel_mask
if [ ! -d ../template/fixel_mask ]; then
echo " Compute a white matter template analysis fixel mask"
fod2fixel -mask ../template/template_mask.mif -fmls_peak_value 0.10 ../template/wmfod_template.mif ../template/fixel_mask -nthreads $ncpu -force
else
echo " Computation of a white matter template analysis fixel mask already done"
fi
# Warp FOD images to template space
#foreach * : mrtransform IN/wmfod.mif -warp IN/subject2template_warp.mif -noreorientation IN/fod_in_template_space_NOT_REORIENTED.mif
if [ ! -f ../fod_warp.done ]; then
# if [ $mrtrix3new ]
echo " Warping FOD images to template space"
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/wmfod_norm.mif -warp IN/subject2template_warp.mif \
IN/fod_in_template_space_NOT_REORIENTED.mif -reorient_fod 0 -nthreads $ncpu -force
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/wmfod_norm.mif -warp IN/subject2template_warp.mif \
IN/fod_in_template_space_REORIENTED.mif -reorient_fod 1 -nthreads $ncpu -force
if [ $? -eq 0 ]; then
echo "done" > ../fod_warp.done
fi
else
echo " Warping FOD images to template space already done"
fi
# Make FA/ADC images in template space
if [ ! -f ../fa_adc_warp.done ]; then
# find the FA in subject space
search_sessions=($(find ${cwd}/dwiprep/sub-* -type f | grep qa/fa${suffix}.nii.gz | sort ))
num_sessions=${#search_sessions[@]}
for i in ${search_sessions[@]}
do
# s=$(echo $i | awk -F 'subjects/' '{print $2}' | awk -F '/' '{print $1}')
# s=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
echo $i
echo $s
mrconvert $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/FA_subj_space.mif -force
done
# exit 2
# find the ADC in subject space
search_sessions=($(find ${cwd}/dwiprep/sub-* -type f | grep qa/adc${suffix}.nii.gz | sort ))
num_sessions=${#search_sessions[@]}
for i in ${search_sessions[@]}
do
# s=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
# s=$(echo $i | awk -F 'subjects/' '{print $2}' | awk -F '/' '{print $1}')
sub=$(echo $i | awk -F 'sub-' '{print $2}' | awk -F '/' '{print $1}')
ses=$(echo $i | awk -F 'ses-' '{print $2}' | awk -F '/' '{print $1}')
s=${sub}_${ses}
mrconvert $i ${cwd}/dwiprep/${group_name}/fba/subjects/${s}/ADC_subj_space.mif -force
done
echo " Warping FA/ADC images to template space"
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/FA_subj_space.mif -warp IN/subject2template_warp.mif \
IN/FA_in_template_space.nii.gz -nthreads $ncpu -force
for_each -force -nthreads ${ncpu_foreach} * : mrtransform IN/ADC_subj_space.mif -warp IN/subject2template_warp.mif \
IN/ADC_in_template_space.nii.gz -nthreads $ncpu -force
if [ $? -eq 0 ]; then
echo "done" > ../fa_adc_warp.done
fi
mkdir -p ../template/fa
#ln -sfn ${cwd}/dwiprep/${group_name}/fba/dwiintensitynorm/dwi_output
for_each -force -nthreads ${ncpu_foreach} * : ln -sfn ${cwd}/dwiprep/${group_name}/fba/subjects/IN/FA_in_template_space.nii.gz ${cwd}/dwiprep/${group_name}/fba/template/fa/sub_IN_FA.nii.gz
mkdir -p ../template/adc
for_each -force -nthreads ${ncpu_foreach} * : ln -sfn ${cwd}/dwiprep/${group_name}/fba/subjects/IN/ADC_in_template_space.nii.gz ${cwd}/dwiprep/${group_name}/fba/template/adc/sub_IN_ADC.nii.gz
else
echo " Warping FA/ADC images to template space already done"
fi
# Segment FOD images to estimate fixels and their apparent fibre density (FD)
#foreach * : fod2fixel -mask ../template/template_mask.mif IN/fod_in_template_space_NOT_REORIENTED.mif IN/fixel_in_template_space_NOT_REORIENTED -afd fd.mif
if [ ! -f ../fod_segment.done ]; then
echo " Segment FOD images to estimate fixels and their apparent fibre density (FD)"
for_each -force -nthreads ${ncpu_foreach} * : fod2fixel -mask ../template/template_mask.mif IN/fod_in_template_space_NOT_REORIENTED.mif \
IN/fixel_in_template_space_NOT_REORIENTED -afd fd.mif -nthreads $ncpu -force
if [ $? -eq 0 ]; then
echo "done" > ../fod_segment.done
fi
else
echo " Segmenting of FOD images to estimate fixels and their apparent fibre density (FD) already done"
fi
# Reorient fixels
#foreach * : fixelreorient IN/fixel_in_template_space_NOT_REORIENTED IN/subject2template_warp.mif IN/fixel_in_template_space
if [ ! -f ../fod_reor_fixels.done ]; then
echo " Reorient fixels"
for_each -force -nthreads ${ncpu_foreach} * : fixelreorient IN/fixel_in_template_space_NOT_REORIENTED IN/subject2template_warp.mif \
IN/fixel_in_template_space -nthreads $ncpu -force
if [ $? -eq 0 ]; then
echo "done" > ../fod_reor_fixels.done
fi
else
echo " Reorient fixels already done"
fi
# Assign subject fixels to template fixels
# foreach * : fixelcorrespondence IN/fixel_in_template_space/fd.mif ../template/fixel_mask ../template/fd PRE.mif
# Note: do NOT run in PARALLEL
if [ ! -f ../assign_fixels.done ]; then
echo " Assign subject fixels to template fixels"
for_each -force -nthreads ${ncpu_foreach} * : fixelcorrespondence IN/fixel_in_template_space/fd.mif \
../template/fixel_mask ../template/fd PRE.mif -force
if [ $? -eq 0 ]; then
echo "done" > ../assign_fixels.done
fi
else
echo " Assign subject fixels to template fixels already done"
fi
# Compute the fibre cross-section (FC) metric
# IT IS possible that the script will exit at this stage with an error
# This seems to relate to the presence of an index file in the template/fc folder
# IT runs just fine if simply restarted and doesn't quit with fd or fdc
# THESE steps are in accordance with the guide on https://mrtrix.readthedocs.io/en/latest/fixel_based_analysis/st_fibre_density_cross-section.html
if [ ! -f ../compute_fc.done ]; then
echo " Compute the fibre cross-section (FC) metric"
for_each -force -nthreads ${ncpu_foreach} * : warp2metric IN/subject2template_warp.mif -fc ../template/fixel_mask ../template/fc IN.mif -force
if [ $? -eq 0 ]; then
echo "done" > ../compute_fc.done
fi
else
echo " Compute the fibre cross-section (FC) metric already done"
fi
if [ ! -f ../compute_log_fc.done ]; then
echo " Compute the fibre cross-section LOG-(FC) metric"
mkdir -p ../template/log_fc
cp ../template/fc/index.mif ../template/fc/directions.mif ../template/log_fc
for_each -force * : mrcalc ../template/fc/IN.mif -log ../template/log_fc/IN.mif -force
if [ $? -eq 0 ]; then
echo "done" > ../compute_log_fc.done
fi
else
echo " Compute the fibre cross-section LOG-(FC) metric already done"
fi
# Compute a combined measure of fibre density and cross-section (FDC)
if [ ! -f ../compute_fdc.done ]; then
echo " Compute a combined measure of fibre density and cross-section (FDC)"
mkdir -p ../template/fdc
cp ../template/fc/index.mif ../template/fdc
cp ../template/fc/directions.mif ../template/fdc
for_each -nthreads ${ncpu_foreach} * : mrcalc ../template/fd/IN.mif ../template/fc/IN.mif -mult ../template/fdc/IN.mif -force
if [ $? -eq 0 ]; then
echo "done" > ../compute_fdc.done
fi
else
echo " Compute the fibre cross-section LOG-(FC) metric already done"
fi
# Perform whole-brain fibre tractography on the FOD template
cd ../template
if [ ! -f ../tckgen.done ]; then
n=20000000
echo " Perform whole-brain fibre tractography on the FOD template"
tckgen -angle 22.5 -maxlen 250 -minlen 10 -power 1.0 wmfod_template.mif -seed_image template_mask.mif \
-mask template_mask.mif -select $n -cutoff 0.10 tracks_20_million.tck
if [ $? -eq 0 ]; then
echo "done" > ../tckgen.done
fi
else
echo " Whole brain fibre tractography already done"
fi
# Reduce biases in tractogram densities
# tcksift tracks_20_million.tck wmfod_template.mif tracks_2_million_sift.tck -term_number 200000
if [ ! -f ../tckshift.done ]; then
echo " Reduce biases in tractogram densities"
n=2000000
tcksift tracks_20_million.tck wmfod_template.mif tracks_2_million_sift.tck -term_number $n
if [ $? -eq 0 ]; then
echo "done" > ../tckshift.done
fi
else
echo " Reduce biases in tractogram densities already done"
fi