-
Notifications
You must be signed in to change notification settings - Fork 12
/
main_surface.sh
executable file
·1235 lines (1141 loc) · 50.7 KB
/
main_surface.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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Main surface script for SCRIPTS
# Launch with:
# bash path_to_scripts/main_surface.sh -c path_to_config/config.sh
# if you want to write the output instead of a log file, use:
# bash path_to_scripts/main_surface.sh -c path_to_config/config.sh > logfile.txt
# style guide:
# https://google.github.io/styleguide/shell.xml
# TODO: add explicits echo for what to check in the figures
# TODO: add the missing mrviews
# TOCHECK: test/retest
#### Checks and preset variables
# import and check config
while getopts "c:eqf" opt; do
case $opt in
c)
CONFIG=$OPTARG
if [ ! -f "$CONFIG" -a "$CONFIG" != "test" ];then
echo "config file "$CONFIG" does not exist" >&2
exit 1
elif [ $CONFIG = "test" ]; then
echo "test mode"
else
echo "Using config file $CONFIG." >&2
source "$CONFIG"
fi
;;
e)
set -e
;;
q)
QUIET="yes"
;;
f)
FORCE="yes"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ -z "$CONFIG" ]; then
echo "You must provide a config file."
exit 1
fi
# check mandatory variables
if [ -z "$PRD" ]; then
echo "PRD path missing"
exit 1
fi
if [ -z "$SUBJ_ID" ]; then
echo "SUBJ_ID path missing"
exit 1
fi
if [ -z "$SUBJECTS_DIR" ]; then
echo "you have to set the SUBJECTS_DIR environnement variable for FreeSurfer" >> "$PRD"/log_processing_parameters.txt
exit 1
else
export FS="$SUBJECTS_DIR"
fi
# set default parameters if not set in config file
echo "##### $( date ) #####" | tee -a "$PRD"/log_processing_parameters.txt
if [ -z "$FORCE" ] || [ "$FORCE" != "no" -a "$FORCE" != "yes" ]; then
echo "set FORCE parameter to no" | tee -a "$PRD"/log_processing_parameters.txt
FORCE="no"
else
echo "FORCE parameter is "$FORCE"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$QUIET" ] || [ "$QUIET" != "no" -a "$QUIET" != "yes" ]; then
echo "set QUIET parameter to no" | tee -a "$PRD"/log_processing_parameters.txt
export QUIET="no"
else
echo "QUIET parameter is "$QUIET"" | tee -a "$PRD"/log_processing_parameters.txt
export MRTRIX_QUIET=1
fi
if [ -z "$FSL" ] || [ "$FSL" != "fsl5.0" ]; then
echo "set FSL parameter to empty" | tee -a "$PRD"/log_processing_parameters.txt
FSL=""
else
echo "FSL parameter is "$FSL"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$HCP" ] || [ "$HCP" != "no" -a "$HCP" != "yes" ]; then
echo "set HCP parameter to no" | tee -a "$PRD"/log_processing_parameters.txt
HCP="no"
else
echo "HCP parameter is "$HCP"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$CHECK" ] || [ "$CHECK" != "no" -a "$CHECK" != "yes" -a "$CHECK" != "force" ]; then
echo "set CHECK parameter to no"| tee -a "$PRD"/log_processing_parameters.txt
export CHECK="no"
else
echo "CHECK parameter is "$CHECK""| tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$REGISTRATION" ]; then
echo "set REGISTRATION parameter to regular"| tee -a "$PRD"/log_processing_parameters.txt
REGISTRATION="regular"
else
echo "REGISTRATION parameter is "$REGISTRATION""| tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$REGION_MAPPING_CORR" ]; then
echo "set REGION_MAPPING_CORR parameter to 0.42"| tee -a "$PRD"/log_processing_parameters.txt
export REGION_MAPPING_CORR=0.42
else
echo "REGION_MAPPING_CORR parameter is "$REGION_MAPPING_CORR""| tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$NUMBER_TRACKS" ] || ! [[ "$NUMBER_TRACKS" =~ ^[0-9]+$ ]]; then
echo "set NUMBER_TRACKS parameter to 10.000.000" | tee -a "$PRD"/log_processing_parameters.txt
NUMBER_TRACKS=10000000
else
echo "NUMBER_TRACKS parameter is "$NUMBER_TRACKS"" | tee -a "$PRD"/log_processing_parameters.txt
fi
# TODO: check if list of integers
if [ -z "$N_SUBREGIONS_LIST" ]; then
echo "set N_SUBREGIONS_LIST parameter to empty" | tee -a "$PRD"/log_processing_parameters.txt
K_LIST=""
else
echo "N_SUBREGIONS_LIST parameter is "$N_SUBREGIONS_LIST"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$PARCEL" ] || [ "$PARCEL" != "desikan" -a "$PARCEL" != "destrieux" -a "$PARCEL" != "HCP-MMP" -a "$PARCEL" != "Yeo-7nets" -a "$PARCEL" != "Yeo-17nets" ]; then
echo "set PARCEL parameter to desikan" | tee -a "$PRD"/log_processing_parameters.txt
export PARCEL="desikan"
else
echo "PARCEL parameter is "$PARCEL"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$TOPUP" ] || [ "$TOPUP" != "no" -a "$TOPUP" != "eddy_correct" ]; then
echo "set TOPUP parameter to no" | tee -a "$PRD"/log_processing_parameters.txt
TOPUP="no"
else
echo "TOPUP parameter is "$TOPUP"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$ACT" ] || [ "$ACT" != "no" -a "$ACT" != "yes" ]; then
echo "set ACT parameter to yes" | tee -a "$PRD"/log_processing_parameters.txt
ACT="yes"
else
echo "ACT parameter is "$ACT"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$SIFT" ] || [ "$SIFT" != "no" -a "$SIFT" != "sift" -a "$SIFT" != "sift2" ]; then
echo "set SIFT parameter to sift2" | tee -a "$PRD"/log_processing_parameters.txt
SIFT="sift2"
else
echo "SIFT parameter is "$SIFT"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$SIFT_MULTIPLIER" ] || ! [[ "$NUMBER_TRACKS" =~ ^[0-9]+$ ]]; then
echo "set SIFT_MULTIPLIER parameter to 10" | tee -a "$PRD"/log_processing_parameters.txt
SIFT_MULTIPLIER=10
else
echo "SIFT_MULTIPLIER parameter is "$SIFT_MULTIPLIER"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$SEED" ] || [ "$SEED" != "gmwmi" -a "$SEED" != "dynamic" ]; then
echo "set SEED parameter to dynamic" | tee -a "$PRD"/log_processing_parameters.txt
SEED="dynamic"
else
echo "SEED parameter is "$SEED"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$ASEG" ] || [ "$ASEG" != "fs" -a "$ASEG" != "fsl" ]; then
echo "set ASEG parameter to fsl" | tee -a "$PRD"/log_processing_parameters.txt
ASEG="fsl"
else
echo "ASEG parameter is "$ASEG"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$FTTGEN" ] || [ "$FTTGEN" != "fs" -a "$FTTGEN" != "fsl" ]; then
echo "set FTTGEN parameter to fsl" | tee -a "$PRD"/log_processing_parameters.txt
FTTGEN="fsl"
else
echo "FTTGEN parameter is "$FTTGEN"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$FORWARD_MODEL" ] || [ "$FORWARD_MODEL" != "yes" -a "$FORWARD_MODEL" != "no" ]; then
echo "set FORWARD_MODEL parameter to yes" | tee -a "$PRD"/log_processing_parameters.txt
FORWARD_MODEL="yes"
else
echo "FORWARD_MODEL parameter is "$FORWARD_MODEL"" | tee -a "$PRD"/log_processing_parameters.txt
fi
if [ -z "$NB_THREADS" ] || ! [[ "$NUMBER_TRACKS" =~ ^[0-9]+$ ]]; then
if [ -f ~/.mrtrix.conf ]; then
number_threads_mrtrix_conf=$(grep 'NumberOfThreads' ~/.mrtrix.conf | cut -f 2 -d " ")
if [ -n "$number_threads_mrtrix_conf" ]; then
echo "set number of threads to \
"$number_threads_mrtrix_conf" according to ~/.mrtrix.conf file" | tee -a "$PRD"/log_processing_parameters.txt
NB_THREADS="$number_threads_mrtrix_conf"
else
echo "set number of threads to 1" | tee -a "$PRD"/log_processing_parameters.txt
NB_THREADS=1
fi
else
echo "set number of threads to 1" | tee -a "$PRD"/log_processing_parameters.txt
NB_THREADS=1
fi
else
echo "number of threads is "$NB_THREADS"" | tee -a "$PRD"/log_processing_parameters.txt
fi
view_step=0
######## HCP pre_scripts
if [ "$HCP" = "yes" ]; then
#if [ ! -d "$FS"/"$SUBJ_ID"/ ]; then
echo "running HCP pre_scripts"
bash util/HCP_pre_scripts.sh
#fi
fi
######### build cortical surface and region mapping
if [ ! -f "$PRD"/data/T1/T1.nii.gz ]; then
echo "generating T1 from DICOM"
mrconvert $PRD/data/T1/ $PRD/data/T1/T1.nii.gz -nthreads "$NB_THREADS"
fi
###################### freesurfer
if [ ! -d "$FS"/"$SUBJ_ID" ] ; then
echo "running recon-all of freesurfer"
recon-all -i $PRD/data/T1/T1.nii.gz -s $SUBJ_ID -all
fi
###################################### left hemisphere
# export pial into text file
mkdir -p "$PRD"/surface
if [ ! -f "$PRD"/surface/lh.pial.asc ]; then
echo "importing left pial surface from freesurfer"
mris_convert "$FS"/"$SUBJ_ID"/surf/lh.pial "$PRD"/surface/lh.pial.asc
# take care of the c_(ras) shift which is not done by FS (thks FS!)
mris_info "$FS"/"$SUBJ_ID"/surf/lh.pial >& "$PRD"/surface/lhinfo.txt
fi
# triangles and vertices high
if [ ! -f "$PRD"/surface/lh_vertices_high.txt ]; then
echo "extracting left vertices and triangles"
python util/extract_high.py lh
fi
# decimation using remesher
if [ ! -f $PRD/surface/lh_vertices_low.txt ]; then
echo "left decimation using remesher"
# -> to mesh
python util/txt2off.py $PRD/surface/lh_vertices_high.txt $PRD/surface/lh_triangles_high.txt $PRD/surface/lh_high.off
# decimation
./remesher/cmdremesher/cmdremesher $PRD/surface/lh_high.off $PRD/surface/lh_low.off
# export to list vertices triangles
python util/off2txt.py $PRD/surface/lh_low.off $PRD/surface/lh_vertices_low.txt $PRD/surface/lh_triangles_low.txt
fi
# create the left region mapping
if [ ! -f "$PRD"/surface/lh_region_mapping_low_not_corrected.txt ]; then
echo "generating the left region mapping on the decimated surface"
python util/region_mapping.py lh
fi
# correct
if [ ! -f "$PRD"/surface/lh_region_mapping_low.txt ]; then
echo "correct the left region mapping"
python util/correct_region_mapping.py lh
echo "check left region mapping"
python util/check_region_mapping.py lh
fi
###################################### right hemisphere
# export pial into text file
if [ ! -f "$PRD"/surface/rh.pial.asc ]; then
echo "importing right pial surface from freesurfer"
mris_convert $FS/$SUBJ_ID/surf/rh.pial $PRD/surface/rh.pial.asc
# take care of the c_(ras) shift which is not done by FS (thks FS!)
mris_info $FS/$SUBJ_ID/surf/rh.pial >& $PRD/surface/rhinfo.txt
fi
# triangles and vertices high
if [ ! -f "$PRD"/surface/rh_vertices_high.txt ]; then
echo "extracting right vertices and triangles"
python util/extract_high.py rh
fi
# decimation using brainvisa
if [ ! -f "$PRD"/surface/rh_vertices_low.txt ]; then
echo "right decimation using remesher"
# -> to mesh
python util/txt2off.py $PRD/surface/rh_vertices_high.txt $PRD/surface/rh_triangles_high.txt $PRD/surface/rh_high.off
# decimation
./remesher/cmdremesher/cmdremesher $PRD/surface/rh_high.off $PRD/surface/rh_low.off
# export to list vertices triangles
python util/off2txt.py $PRD/surface/rh_low.off $PRD/surface/rh_vertices_low.txt $PRD/surface/rh_triangles_low.txt
fi
# create the right region mapping
if [ ! -f "$PRD"/surface/rh_region_mapping_low_not_corrected.txt ]; then
echo "generating the right region mapping on the decimated surface"
python util/region_mapping.py rh
fi
# correct
if [ ! -f "$PRD"/surface/rh_region_mapping_low.txt ]; then
echo " correct the right region mapping"
python util/correct_region_mapping.py rh
echo "check right region mapping"
python util/check_region_mapping.py rh
fi
###################################### both hemisphere
# prepare final directory
mkdir -p $PRD/$SUBJ_ID
mkdir -p $PRD/$SUBJ_ID/surface
# reunify both region_mapping, vertices and triangles
if [ ! -f "$PRD"/"$SUBJ_ID"/surface/region_mapping.txt ]; then
echo "reunify both region mappings"
python util/reunify_both_regions.py
fi
# zip to put in final format
pushd . > /dev/null
cd $PRD/$SUBJ_ID/surface > /dev/null
zip $PRD/$SUBJ_ID/surface.zip vertices.txt triangles.txt -q
cp region_mapping.txt ..
popd > /dev/null
########################### subcortical surfaces
# extract subcortical surfaces
if [ ! -f "$PRD"/surface/subcortical/aseg_058_vert.txt ]; then
echo "generating subcortical surfaces"
./util/aseg2srf -s $SUBJ_ID
mkdir -p $PRD/surface/subcortical
cp $FS/$SUBJ_ID/ascii/* $PRD/surface/subcortical
python util/list_subcortical.py
fi
########################## build connectivity using mrtrix 3
mkdir -p $PRD/connectivity
mkdir -p $PRD/$SUBJ_ID/connectivity
## preprocessing
# See: http://mrtrix.readthedocs.io/en/0.3.16/workflows/DWI_preprocessing_for_quantitative_analysis.html
# handle encoding scheme
if [ ! -f "$PRD"/connectivity/predwi.mif ]; then
view_step=1
select_images="n"
i_im=1
echo "generate dwi mif file"
echo "if asked, please select a series of images by typing a number"
mrconvert $PRD/data/DWI/ $PRD/connectivity/predwi_"$i_im".mif \
-export_pe_table $PRD/connectivity/pe_table \
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_init \
-datatype float32 -stride 0,0,0,1 -force -nthreads "$NB_THREADS"
cp $PRD/connectivity/predwi_1.mif $PRD/connectivity/predwi.mif
if [ "$FORCE" = "no" ]; then
echo "Do you want to add another image serie (different phase encoding)? [y, n]"
read select_images
while [ "$select_images" != "y" ] && [ "$select_images" != "n" ]; do
echo " please answer y or n"
read select_images
done
while [ "$select_images" == "y" ]; do
i_im=$(($i_im + 1))
mrconvert $PRD/data/DWI/ $PRD/connectivity/predwi_"$i_im".mif \
-export_pe_table $PRD/connectivity/pe_table \
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_init \
-datatype float32 -stride 0,0,0,1 -force -nthreads "$NB_THREADS"
mrcat $PRD/connectivity/predwi.mif $PRD/connectivity/predwi_"$i_im".mif \
$PRD/connectivity/predwi.mif -axis 3 -nthreads "$NB_THREADS" -force
echo "Do you want to add another image serie (different phase encoding)? [y, n]"
read select_images
while [ "$select_images" != "y" ] && [ "$select_images" != "n" ]; do
echo " please answer y or n"
read select_images
done
done
fi
mrinfo $PRD/connectivity/predwi.mif \
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_init \
-export_pe_table $PRD/connectivity/pe_table -force
fi
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
view_step=0
echo "check predwi_*.mif files"
mrview $PRD/connectivity/predwi_*.mif
fi
# denoising the volumes
if [ ! -f "$PRD"/connectivity/predwi_denoised.mif ]; then
# denoising the combined-directions file is preferable to denoising \
# predwi1 and 2 separately because of a higher no of volumes
# see: https://github.com/MRtrix3/mrtrix3/issues/747
echo "denoising dwi data"
view_step=1
dwidenoise $PRD/connectivity/predwi.mif \
$PRD/connectivity/predwi_denoised.mif \
-noise $PRD/connectivity/noise.mif -force -nthreads "$NB_THREADS"
if [ ! -f $PRD/connectivity/noise_res.mif ]; then
# calculate residuals noise
mrcalc $PRD/connectivity/predwi.mif \
$PRD/connectivity/predwi_denoised.mif \
-subtract $PRD/connectivity/noise_res.mif -nthreads "$NB_THREADS"
fi
fi
# check noise file: lack of anatomy is a marker of accuracy
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
# noise.mif can also be used for SNR calculation
echo "check noise/predwi_*_denoised.mif files"
echo "lack of anatomy in noise_res is a marker of accuracy"
view_step=0
mrview $PRD/connectivity/predwi.mif \
$PRD/connectivity/predwi_denoised.mif \
$PRD/connectivity/noise.mif \
$PRD/connectivity/noise_res.mif
fi
# topup/eddy corrections
if [ ! -f "$PRD"/connectivity/predwi_denoised_preproc.mif ]; then
view_step=1
if [ "$TOPUP" = "eddy_correct" ]; then
# eddy and maybe topup corrections depending of the encoding scheme
# TODO: removed repol option for now, as it is not in current FSL debian release
echo "apply eddy and maybe topup if reverse phase-encoding scheme"
dwipreproc $PRD/connectivity/predwi_denoised.mif \
$PRD/connectivity/predwi_denoised_preproc.mif \
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_final \
-rpe_header -force -nthreads "$NB_THREADS"
else # no topup/eddy
echo "no topup/eddy applied"
mrconvert $PRD/connectivity/predwi_denoised.mif \
$PRD/connectivity/predwi_denoised_preproc.mif \
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_final \
-force -nthreads "$NB_THREADS"
fi
fi
# check preproc files
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check preprocessed mif file (no topup/no eddy)"
view_step=0
mrview $PRD/connectivity/predwi.mif \
$PRD/connectivity/predwi_denoised.mif \
$PRD/connectivity/predwi_denoised_preproc.mif
fi
# TOCHECK: Masking step before or after biascorrect?
# Native-resolution mask creation
if [ ! -f "$PRD"/connectivity/mask_native.mif ]; then
echo "create dwi mask"
view_step=1
dwi2mask $PRD/connectivity/predwi_denoised_preproc.mif \
$PRD/connectivity/mask_native.mif -nthreads "$NB_THREADS"
fi
# check mask file
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check native mask mif file"
view_step=0
mrview "$PRD"/connectivity/predwi_denoised_preproc.mif \
-overlay.load $PRD/connectivity/mask_native.mif \
-overlay.opacity 0.5
fi
# Bias field correction
if [ ! -f "$PRD"/connectivity/predwi_denoised_preproc_bias.mif ]; then
# ANTS seems better than FSL
# see http://mrtrix.readthedocs.io/en/0.3.16/workflows/DWI_preprocessing_for_quantitative_analysis.html
if [ -n "$ANTSPATH" ]; then
echo "bias correct using ANTS"
dwibiascorrect $PRD/connectivity/predwi_denoised_preproc.mif \
$PRD/connectivity/predwi_denoised_preproc_bias.mif \
-mask $PRD/connectivity/mask_native.mif \
-bias $PRD/connectivity/B1_bias.mif -ants -force \
-nthreads "$NB_THREADS"
else
echo "bias correct using FSL"
dwibiascorrect $PRD/connectivity/predwi_denoised_preproc.mif \
$PRD/connectivity/predwi_denoised_preproc_bias.mif \
-mask $PRD/connectivity/mask_native.mif \
-bias $PRD/connectivity/B1_bias.mif -fsl -force \
-nthreads "$NB_THREADS"
fi
fi
# check bias field correction
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check bias field correction"
mrview $PRD/connectivity/predwi.mif \
$PRD/connectivity/predwi_denoised_preproc.mif \
$PRD/connectivity/predwi_denoised_preproc_bias.mif
fi
# Upsampling to vox=1.25 as recommended in mrtrix:
# http://mrtrix.readthedocs.io/en/latest/fixel_based_analysis/mt_fibre_density_cross-section.html
# upsampling and reorienting a la fsl
# reorienting from mrtrix to FSL, RAS to LAS, means -stride -1,+2,+3,+4
# see: http://mrtrix.readthedocs.io/en/latest/getting_started/image_data.html
# upsampling (Dyrby TB. Neuroimage. 2014 Dec;103:202-13.) can help registration
# with structural and is common with mrtrix3 fixel analysis pipeline
# see: http://community.mrtrix.org/t/upsampling-dwi-vs-tckgen-defaults/998/2
if [ ! -f "$PRD"/connectivity/dwi.mif ]; then
native_voxelsize=$(mrinfo $PRD/connectivity/mask_native.mif -spacing \
| cut -f 1 -d " " | xargs printf "%.3f")
upsampling=$(echo ""$native_voxelsize">1.25" | bc)
if [ "$upsampling" = 1 ]; then
echo "upsampling dwi"
scale_factor=$( bc -l <<< "$native_voxelsize"/1.25 )
echo "scale factor for upsampling is "$scale_factor""
mrresize $PRD/connectivity/predwi_denoised_preproc_bias.mif - -scale "$scale_factor" -force | \
mrconvert - -datatype float32 -stride -1,+2,+3,+4 $PRD/connectivity/dwi.mif -force
else
echo "no upsampling of dwi"
mrconvert $PRD/connectivity/predwi_denoised_preproc_bias.mif -datatype float32 -stride -1,+2,+3,+4 $PRD/connectivity/dwi.mif -force
fi
fi
if [ ! -f "$PRD"/connectivity/mask.mif ]; then
# for dwi2fod step, a permissive, dilated mask can be used to minimize
# streamline premature termination, see BIDS protocol:
# https://github.com/BIDS-Apps/MRtrix3_connectome/blob/master/run.py
view_step=1
native_voxelsize=$(mrinfo $PRD/connectivity/mask_native.mif -spacing \
| cut -f 1 -d " " | xargs printf "%.3f")
upsampling=$(echo ""$native_voxelsize">1.25" | bc)
if [ "$upsampling" = 1 ]; then
echo "upsampling mask"
scale_factor=$( bc -l <<< "$native_voxelsize"/1.25 )
echo "scale factor for upsampling is "$scale_factor""
mrresize $PRD/connectivity/mask_native.mif - -scale "$scale_factor" -force | \
mrconvert - $PRD/connectivity/mask.mif -datatype bit -stride -1,+2,+3 \
-force -nthreads "$NB_THREADS"
else
echo "no upsampling of the mask"
mrconvert $PRD/connectivity/mask_native.mif $PRD/connectivity/mask.mif -datatype bit -stride -1,+2,+3 \
-force -nthreads "$NB_THREADS"
fi
maskfilter $PRD/connectivity/mask.mif dilate \
$PRD/connectivity/mask_dilated.mif -npass 2 -force \
-nthreads "$NB_THREADS"
fi
# check upsampled files
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check upsampled mif files"
view_step=0
mrview $PRD/connectivity/dwi.mif \
-overlay.load $PRD/connectivity/mask.mif \
-overlay.load $PRD/connectivity/mask_dilated.mif \
-overlay.opacity 0.5 -norealign
fi
## FLIRT registration
# a comparison of registration methods is available in:
# Ou Y, et al. IEEE Trans Med Imaging. 2014 Oct;33(10):2039-65
# FLIRT -bbr (Kerstin's protocol):
# http://community.mrtrix.org/t/registration-of-structural-and-diffusion-weighted-data/203/8
# generating FSl brain.mgz
if [ ! -f "$PRD"/connectivity/brain.nii.gz ]; then
# brain.mgz seems to be superior to diff to T1
# as the main problem for registration is the wmgm interface that we want to
# remove and BET stripping is unfortunate in many situations,
# and FS pial eddited volumes already present
# stride from FS to FSL: RAS to LAS
# see: http://www.grahamwideman.com/gw/brain/fs/coords/fscoords.htm
# we could do
# mrconvert $FS/$SUBJ_ID/mri/brain.mgz $PRD/connectivity/brain.nii.gz \
# -datatype float32 -stride -1,+2,+3,+4 -force -nthreads "$NB_THREADS"
# instead we use the pure brain from aparc+aseg:
echo "generating masked brain in FSL orientation"
mri_binarize --i $FS/$SUBJ_ID/mri/aparc+aseg.mgz \
--o $FS/$SUBJ_ID/mri/aparc+aseg_mask.mgz --min 0.5 --dilate 1
mri_mask $FS/$SUBJ_ID/mri/brain.mgz $FS/$SUBJ_ID/mri/aparc+aseg_mask.mgz \
$FS/$SUBJ_ID/mri/brain_masked.mgz
mrconvert $FS/$SUBJ_ID/mri/brain_masked.mgz $PRD/connectivity/brain.nii.gz \
-force -datatype float32 -stride -1,+2,+3
fi
# low b extraction to FSL
if [ ! -f "$PRD"/connectivity/lowb.nii.gz ]; then
view_step=1
if [ "$REGISTRATION" = "regular" ] || [ "$REGISTRATION" = "boundary" ]; then
echo "extracting b0 vols for registration"
dwiextract $PRD/connectivity/dwi.mif $PRD/connectivity/lowb.mif \
-bzero -force -nthreads "$NB_THREADS"
# stride from mrtrix to FSL, RAS to LAS
# see: http://mrtrix.readthedocs.io/en/latest/getting_started/image_data.html
mrconvert $PRD/connectivity/lowb.mif $PRD/connectivity/lowb.nii.gz \
-stride -1,+2,+3,+4 -force -nthreads "$NB_THREADS"
# for visualization
mrmath $PRD/connectivity/lowb.mif mean $PRD/connectivity/meanlowb.mif \
-axis 3 -force -nthreads "$NB_THREADS"
elif [ "$REGISTRATION" = "pseudo" ]; then
# lowb-pseudo brain for pseudo registration
echo "generate lowb-pseudo vols for pseudo registration"
# Generate transform image (dwi) for pseudo registration method:
# see: Bhushan C, et al. Neuroimage. 2015 Jul 15;115:269-8
# used in: https://github.com/BIDS-Apps/MRtrix3_connectome/blob/master/run.py
dwiextract $PRD/connectivity/dwi.mif -bzero - \
| mrmath - mean - -axis 3 \
| mrcalc 1 - -divide $PRD/connectivity/mask.mif -multiply - \
| mrconvert - - -stride -1,+2,+3 \
| mrhistmatch - $PRD/connectivity/brain.nii.gz \
$PRD/connectivity/lowb.nii.gz
fi
fi
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check lowb image"
view_step=0
mrview $PRD/connectivity/lowb.mif \
-overlay.load $PRD/connectivity/dwi.mif \
-overlay.opacity 1. -norealign
fi
# aparc+aseg to FSL
if [ ! -f "$PRD"/connectivity/aparc+aseg.nii.gz ]; then
echo "generating FSL orientation for aparc+aseg"
# stride from FS to FSL: RAS to LAS
if [ $PARCEL = "desikan" ]; then
mrconvert $FS/$SUBJ_ID/mri/aparc+aseg.mgz \
$PRD/connectivity/aparc+aseg.nii.gz -stride -1,+2,+3 -force \
-nthreads "$NB_THREADS"
elif [ $PARCEL = "destrieux" ]; then
mrconvert $FS/$SUBJ_ID/mri/aparc.a2009s+aseg.mgz \
$PRD/connectivity/aparc+aseg.nii.gz -stride -1,+2,+3 -force \
-nthreads "$NB_THREADS"
fi
fi
# check orientations
if [ ! -f $PRD/connectivity/aparc+aseg_reorient.nii.gz ]; then
echo "reorienting the region parcellation"
view_step=1
"$FSL"fslreorient2std $PRD/connectivity/aparc+aseg.nii.gz \
$PRD/connectivity/aparc+aseg_reorient.nii.gz
fi
# check parcellation to brain.mgz
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
# TODO: mrview discrete colour scheme?
echo "check parcellation"
echo "if it's correct, just close the window."
echo "Otherwise... well, it should be correct anyway"
view_step=0
mrview $PRD/connectivity/brain.nii.gz \
-overlay.load $PRD/connectivity/aparc+aseg_reorient.nii.gz \
-overlay.opacity 0.5 -norealign
fi
# aparcaseg to diff by inverse transform
if [ ! -f "$PRD"/connectivity/aparcaseg_2_diff.nii.gz ]; then
view_step=1
if [ "$REGISTRATION" = "regular" ] || [ "$REGISTRATION" = "pseudo" ]; then
# 6 dof; see:
# http://web.mit.edu/fsl_v5.0.8/fsl/doc/wiki/FLIRT(2f)FAQ.html#What_cost_function_and.2BAC8-or_degrees_of_freedom_.28DOF.29_should_I_use_in_FLIRT.3F
echo "register aparc+aseg to diff"
"$FSL"flirt -in $PRD/connectivity/lowb.nii.gz \
-out $PRD/connectivity/lowb_2_struct.nii.gz \
-ref $PRD/connectivity/brain.nii.gz \
-omat $PRD/connectivity/diffusion_2_struct.mat -dof 6 \
-searchrx -180 180 -searchry -180 180 -searchrz -180 180 \
-cost mutualinfo
elif [ "$REGISTRATION" = "boundary" ]; then
echo "register aparc+aseg to diff using bbr cost function in FLIRT"
# as per http://community.mrtrix.org/t/registration-of-structural-and-diffusion-weighted-data/203/8
"$FSL"fast -N -o $PRD/connectivity/brain_fast $PRD/connectivity/brain.nii.gz
"$FSL"fslmaths $PRD/connectivity/brain_fast_pve_2.nii.gz -thr 0.5 \
-bin $PRD/connectivity/brain_fast_wmmask.nii.gz
# first flirt to get an init transform mat
"$FSL"flirt -in $PRD/connectivity/lowb.nii.gz \
-ref $PRD/connectivity/brain.nii.gz \
-omat $PRD/connectivity/flirt_bbr_tmp.mat -dof 6
# flirt using bbr cost
"$FSL"flirt -in $PRD/connectivity/lowb.nii.gz \
-out $PRD/connectivity/lowb_2_struct.nii.gz \
-ref $PRD/connectivity/brain.nii.gz \
-omat $PRD/connectivity/diffusion_2_struct.mat \
-wmseg $PRD/connectivity/brain_fast_wmmask.nii.gz \
-init $PRD/connectivity/flirt_bbr_tmp.mat \
-schedule $FSLDIR/etc/flirtsch/bbr.sch -dof 6 \
-searchrx -180 180 -searchry -180 180 -searchrz -180 180 \
-cost bbr
fi
transformconvert $PRD/connectivity/diffusion_2_struct.mat \
$PRD/connectivity/lowb.nii.gz \
$PRD/connectivity/brain.nii.gz \
flirt_import $PRD/connectivity/diffusion_2_struct_mrtrix.txt \
-force
mrtransform $PRD/connectivity/aparc+aseg_reorient.nii.gz \
$PRD/connectivity/aparcaseg_2_diff.nii.gz \
-linear $PRD/connectivity/diffusion_2_struct_mrtrix.txt \
-inverse -datatype uint32 -force -nthreads "$NB_THREADS"
fi
# brain to diff by inverse transform
if [ ! -f "$PRD"/connectivity/brain_2_diff.nii.gz ]; then
echo "register brain to diff"
view_step=1
mrtransform $PRD/connectivity/brain.nii.gz \
$PRD/connectivity/brain_2_diff.nii.gz \
-linear $PRD/connectivity/diffusion_2_struct_mrtrix.txt \
-inverse -force
fi
# check brain and parcellation to diff
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check parcellation registration to diffusion space"
echo "if it's correct, just close the window."
echo "Otherwise you will have to do the registration by hand"
view_step=0
mrview $PRD/connectivity/brain_2_diff.nii.gz \
$PRD/connectivity/lowb.nii.gz \
-overlay.load $PRD/connectivity/aparcaseg_2_diff.nii.gz \
-overlay.opacity 0.5 -norealign
fi
# prepare file for act
if [ "$ACT" = "yes" ] && [ ! -f $PRD/connectivity/act.mif ]; then
echo "prepare files for act"
view_step=1
if [ "$FTTGEN" = "fsl" ]; then
5ttgen fsl $PRD/connectivity/brain_2_diff.nii.gz $PRD/connectivity/act.mif \
-premasked -force -nthreads "$NB_THREADS"
elif [ "$FTTGEN" = "fs" ]; then
5ttgen freesurfer $PRD/connectivity/aparcaseg_2_diff.nii.gz $PRD/connectivity/act.mif \
-force -nthreads "$NB_THREADS"
fi
5tt2vis $PRD/connectivity/act.mif $PRD/connectivity/act_vis.mif -force \
-nthreads "$NB_THREADS"
fi
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check tissue segmented image"
view_step=0
mrview $PRD/connectivity/act_vis.mif -colourmap 4
fi
# Response function estimation
# Check if multi or single shell
shells=$(mrinfo -shell_bvalues $PRD/connectivity/dwi.mif)
echo "shell b values are $shells"
nshells=($shells)
no_shells=${#nshells[@]}
echo "no of shells are $no_shells"
if [ "$no_shells" -gt 2 ]; then
# Multishell
if [ ! -f "$PRD"/connectivity/response_wm.txt ]; then
view_step=1
if [ "$ACT" = "yes" ]; then
echo "estimating response using msmt algorithm"
dwi2response msmt_5tt $PRD/connectivity/dwi.mif \
$PRD/connectivity/act.mif \
$PRD/connectivity/response_wm.txt \
$PRD/connectivity/response_gm.txt \
$PRD/connectivity/response_csf.txt \
-voxels $PRD/connectivity/RF_voxels.mif \
-mask $PRD/connectivity/mask.mif -force \
-nthreads "$NB_THREADS"
else
echo "estimating response using dhollander algorithm"
dwi2response dhollander $PRD/connectivity/dwi.mif \
$PRD/connectivity/response_wm.txt \
$PRD/connectivity/response_gm.txt \
$PRD/connectivity/response_csf.txt \
-voxels $PRD/connectivity/RF_voxels.mif \
-mask $PRD/connectivity/mask.mif -force \
-nthreads "$NB_THREADS"
fi
fi
else
# Single shell only
if [ ! -f "$PRD"/connectivity/response_wm.txt ]; then
echo "estimating response using dhollander algorithm"
view_step=1
# dwi2response tournier $PRD/connectivity/dwi.mif $PRD/connectivity/response.txt -force -voxels $PRD/connectivity/RF_voxels.mif -mask $PRD/connectivity/mask.mif
dwi2response dhollander $PRD/connectivity/dwi.mif \
$PRD/connectivity/response_wm.txt \
$PRD/connectivity/response_gm.txt \
$PRD/connectivity/response_csf.txt \
-voxels $PRD/connectivity/RF_voxels.mif \
-mask $PRD/connectivity/mask.mif -force \
-nthreads "$NB_THREADS"
fi
fi
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check ODF image"
view_step=0
mrview $PRD/connectivity/meanlowb.mif \
-overlay.load $PRD/connectivity/RF_voxels.mif \
-overlay.opacity 0.5
fi
# Fibre orientation distribution estimation
if [ ! -f "$PRD"/connectivity/wm_CSD.mif ]; then
# Both for multishell and single shell since we use dhollander in the
# single shell case
# see: http://community.mrtrix.org/t/wm-odf-and-response-function-with-dhollander-option---single-shell-versus-multi-shell/572/4
echo "calculating fod on multishell or single shell data"
view_step=1
dwi2fod msmt_csd $PRD/connectivity/dwi.mif \
$PRD/connectivity/response_wm.txt \
$PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/response_gm.txt \
$PRD/connectivity/gm_CSD.mif \
$PRD/connectivity/response_csf.txt \
$PRD/connectivity/csf_CSD.mif \
-mask $PRD/connectivity/mask_dilated.mif -force \
-nthreads "$NB_THREADS"
fi
if [ "$view_step" = 1 -a "$CHECK" = "yes" ] || [ "$CHECK" = "force" ] && [ -n "$DISPLAY" ]; then
echo "check ODF image"
view_step=0
mrconvert $PRD/connectivity/wm_CSD.mif - -coord 3 0 \
-nthreads "$NB_THREADS" -force \
| mrcat $PRD/connectivity/csf_CSD.mif \
$PRD/connectivity/gm_CSD.mif - \
$PRD/connectivity/tissueRGB.mif -axis 3 -nthreads "$NB_THREADS" \
-force
mrview $PRD/connectivity/tissueRGB.mif \
-odf.load_sh $PRD/connectivity/wm_CSD.mif
fi
# tractography
if [ ! -f "$PRD"/connectivity/whole_brain.tck ]; then
if [ "$SIFT" = "sift" ]; then
# temporarily change number of tracks for sift
number_tracks=$(($NUMBER_TRACKS*$SIFT_MULTIPLIER))
fi
native_voxelsize=$(mrinfo $PRD/connectivity/mask_native.mif -spacing \
| cut -f 1 -d " " | xargs printf "%.3f")
upsampling=$(echo ""$native_voxelsize">1.25" | bc)
if [ "$upsampling" = 1 ]; then
scale_factor=$( bc -l <<< "$native_voxelsize"/1.25 )
else
scale_factor=1
fi
echo "scale factor is "$scale_factor""
stepsize=$( bc -l <<< "scale="$scale_factor"; "$native_voxelsize"/2" )
echo "stepsize parameter for tckgen is $stepsize"
angle=$( bc -l <<< "scale="$scale_factor"; 90*"$stepsize"/"$native_voxelsize"" )
echo "angle parameter for tckgen is $angle"
if [ "$ACT" = "yes" ]; then
# when using msmt_csd in conjunction with ACT, the cutoff threshold
# can be reduced to 0.06
# see: https://github.com/MRtrix3/mrtrix3/blob/master/docs/quantitative_structural_connectivity/ismrm_hcp_tutorial.rst#connectome-generation
echo "generating tracks using act"
if [ "$SEED" = "gmwmi" ]; then
echo "seeding from gmwmi"
5tt2gmwmi $PRD/connectivity/act.mif \
$PRD/connectivity/gmwmi_mask.mif -force \
-nthreads "$NB_THREADS"
# TODO: min length check andreas paper
tckgen $PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/whole_brain.tck \
-seed_gmwmi $PRD/connectivity/gmwmi_mask.mif \
-act $PRD/connectivity/act.mif -select "$NUMBER_TRACKS" \
-seed_unidirectional -crop_at_gmwmi -backtrack \
-minlength 4 -maxlength 250 -step "$stepsize" -angle "$angle" \
-cutoff 0.06 -force -nthreads "$NB_THREADS"
elif [ "$SEED" = "dynamic" ]; then
# -dynamic seeding may work slightly better than gmwmi,
# see Smith RE Neuroimage. 2015 Oct 1;119:338-51.
echo "seeding dynamically"
tckgen $PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/whole_brain.tck \
-seed_dynamic $PRD/connectivity/wm_CSD.mif \
-act $PRD/connectivity/act.mif -select "$NUMBER_TRACKS" \
-crop_at_gmwmi -backtrack -minlength 4 -maxlength 250 \
-step "$stepsize" -angle "$angle" -cutoff 0.06 -force \
-nthreads "$NB_THREADS"
fi
else
echo "generating tracks without using act"
echo "seeding dynamically"
tckgen $PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/whole_brain.tck \
-seed_dynamic $PRD/connectivity/wm_CSD.mif \
-mask $PRD/connectivity/mask.mif -select "$NUMBER_TRACKS" \
-maxlength 250 -step "$stepsize" -angle "$angle" -cutoff 0.1 \
-force -nthreads "$NB_THREADS"
fi
fi
# postprocessing
if [ ! -f "$PRD"/connectivity/whole_brain_post.tck -a ! -h "$PRD"/connectivity/whole_brain_post.tck ]; then
echo "$PRD"/connectivity/whole_brain_post.tck
if [ "$SIFT" = "sift" ]; then
echo "using sift"
number_tracks=$(($NUMBER_TRACKS/$SIFT_MULTIPLIER))
if [ "$ACT" = "yes" ]; then
echo "trimming tracks using sift/act"
tcksift $PRD/connectivity/whole_brain.tck \
$PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/whole_brain_post.tck \
-act $PRD/connectivity/act.mif \
-out_mu $PRD/connectivity/mu.txt \
-term_number $NUMBER_TRACKS -fd_scale_gm -force \
-nthreads "$NB_THREADS"
else
echo "trimming tracks using sift/without act"
tcksift $PRD/connectivity/whole_brain.tck \
$PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/whole_brain_post.tck \
-out_mu $PRD/connectivity/mu.txt \
-term_number $NUMBER_TRACKS -force \
-nthreads "$NB_THREADS"
fi
elif [ "$SIFT" = "sift2" ]; then
echo "running sift2"
ln -s $PRD/connectivity/whole_brain.tck $PRD/connectivity/whole_brain_post.tck
if [ "$ACT" = "yes" ]; then
echo "using act"
tcksift2 $PRD/connectivity/whole_brain.tck \
$PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/streamline_weights.csv\
-act $PRD/connectivity/act.mif \
-out_mu $PRD/connectivity/mu.txt \
-out_coeffs $PRD/connectivity/streamline_coeffs.csv \
-fd_scale_gm -force -nthreads "$NB_THREADS"
else
tcksift2 $PRD/connectivity/whole_brain.tck \
$PRD/connectivity/wm_CSD.mif \
$PRD/connectivity/streamline_weights.csv \
-out_mu $PRD/connectivity/mu.txt \
-out_coeffs $PRD/connectivity/streamline_coeffs.csv \
-force -nthreads "$NB_THREADS"
fi
else
echo "not using sift2"
ln -s $PRD/connectivity/whole_brain.tck \
$PRD/connectivity/whole_brain_post.tck
fi
fi
## now compute connectivity and length matrix
if [ ! -f "$PRD"/connectivity/aparcaseg_2_diff_"$ASEG".mif ]; then
echo "compute parcellation labels"
python util/compute_luts.py
labelconvert $PRD/connectivity/aparcaseg_2_diff.nii.gz \
$PRD/connectivity/lut_in.txt $PRD/connectivity/lut_out.txt\
$PRD/connectivity/aparcaseg_2_diff_fs.mif \
-force -nthreads "$NB_THREADS"
echo "$ASEG"
if [ "$ASEG" = "fsl" ]; then
# FS derived subcortical parcellation is too variable and prone to
# errors => labelsgmfix) was generated,
# see Smith RE Neuroimage. 2015 Jan 1;104:253-65.
# TODO: check effect on region mapping
# TODO; -sgm_amyg_hipp option to consider
echo "fix FS subcortical labels to generate FSL labels"
labelsgmfix $PRD/connectivity/aparcaseg_2_diff_fs.mif \
$PRD/connectivity/brain_2_diff.nii.gz $PRD/connectivity/lut_out.txt \
$PRD/connectivity/aparcaseg_2_diff_fsl.mif -premasked \
-force -nthreads "$NB_THREADS"
fi
fi
if [ ! -f "$PRD"/connectivity/weights.csv ]; then
echo "compute connectivity matrix weights"
if [ "$SIFT" = "sift2" ]; then
# -tck_weights_in flag only needed for sift2 but not for sift/no processing
tck2connectome $PRD/connectivity/whole_brain_post.tck \
$PRD/connectivity/aparcaseg_2_diff_"$ASEG".mif \
$PRD/connectivity/weights.csv -assignment_radial_search 2 \
-out_assignments $PRD/connectivity/edges_2_nodes.csv \
-tck_weights_in $PRD/connectivity/streamline_weights.csv \
-force -nthreads "$NB_THREADS"
else
tck2connectome $PRD/connectivity/whole_brain_post.tck \
$PRD/connectivity/aparcaseg_2_diff_"$ASEG".mif \
$PRD/connectivity/weights.csv -assignment_radial_search 2 \
-out_assignments $PRD/connectivity/edges_2_nodes.csv \
-force -nthreads "$NB_THREADS"
fi
fi
if [ ! -f "$PRD"/connectivity/tract_lengths.csv ]; then
echo "compute connectivity matrix edge lengths"
view_step=1
# mean length result: weight by the length, then average
# see: http://community.mrtrix.org/t/tck2connectome-edge-statistic-sift2-questions/1059/2