-
Notifications
You must be signed in to change notification settings - Fork 4
/
KUL_VBG.sh
executable file
·3342 lines (1905 loc) · 105 KB
/
KUL_VBG.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
#!/bin/bash
# set -x
# Ahmed Radwan ahmed.radwan@kuleuven.be
# Stefan Sunaert stefan.sunaert@kuleuven.be
# this script is in dev for S61759
#####################################
v="0.61_23112021_beta"
# This script is meant to allow a decent recon-all/antsMALF output in the presence of a large brain lesion
# The main idea is to replace the lesion with a hole and fill the hole with information from the a synthetic image
# this maintains subject specificity and diseased hemisphere information but replaces lesioned tissue with sham brain
# to do:
# (1) Add MSBP
# (2) Add a quick parcellation solution using labelpropagation in ANTs
# (3) Add a MALP-EM based parcellation stream
# ----------------------------------- MAIN ---------------------------------------------
# this script uses "preprocessing control", i.e. if some steps are already processed it will skip these
kul_lesion_dir=`dirname "$0"`
script=`basename "$0"`
# source $kul_main_dir/KUL_main_functions.sh
cwd=($(pwd))
# FUNCTIONS --------------
# function Usage
function Usage {
cat <<USAGE
`basename $0` preps structural images with lesions and runs recon-all.
Usage examples:
`basename $0` -S Subject_ID -l /fullpath/lesion_mask.nii.gz -z T1 -b -B (1 or 2)
or
`basename $0` -S Subject_ID -a /fullpath/Subject_ID_T1w.nii.gz -l /fullpath/lesion_mask.nii.gz -z T1 -B (1 or 2) -n 10 -v 1
Purpose:
The purpose of this workflow is to generate a lesion filled image, with healthy looking synthetic tissue in place of the lesion
Essentially excising the lesion and grafting over the brain tissue defect in the MR image space
How to use:
- You need to use the cook_template_4VBG script once for your study - if you have only 1 scanner
- cook_template_4VBG requires two brains with unilateral lesions on opposing sides
- it is meant to facilitate the grafting process and minimize intensity differences
- You need a high resolution T1 WI and a lesion mask in the same space for VBG to run
- If you end up with an empty image, it is possible you have a mismatch between the T1 and lesion mask
Required arguments:
-S: BIDS subject/participant name (anonymised name of the subject without the "sub-" prefix)
-b: if data is in BIDS
-l: full path and file name to lesion mask file per session
-z: space of the lesion mask used (only T1 supported in this version)
-a: Input precontrast T1WIs
Optional arguments:
-s: session (of the participant)
-t: Use the VBG template to derive the fill patch (if used, template tissue is used alongside native tissue to create the donor brain)
-E: Treat as an extra-axial lesion (skip VBG bulk, fill lesion patch with 0s, run FS and subsequent steps)
-B: specify brain extraction method (1 = HD-BET, 2 = ANTs-BET), if not set ANTs-BET will be used by default
-P: Run parcellation (1 = FreeSurfer recon-all, 2 = FastSurfer, 3 = FastSurfer and FreeSurfer hybrid)
-p: In case of pediatric patients - use pediatric template (NKI_under_10 in MNI)
-m: full path to intermediate output dir
-o: full path to output dir (if not set reverts to default output ./VBG_output)
-n: number of cpu for parallelisation (default is 6)
-v: show output from mrtrix commands
-h: prints help menu
Notes:
- You can use -b and the script will find your BIDS files automatically
- If your data is not in BIDS, then use -a without -b
- This version is for validation only.
- In case of trouble with HD-BET see lines (1124 - 1200)
USAGE
exit 1
}
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
# this works for ANTsX scripts and FS
ncpu=8
# Set required options
S_flag=0
b_flag=0
s_flag=0
l_flag=0
l_spaceflag=0
t1_flag=0
t_flag=0
o_flag=0
m_flag=0
n_flag=0
P_flag=0
E_flag=0
p_flag=0
if [ "$#" -lt 1 ]; then
Usage >&2
exit 1
else
while getopts "S:a:l:z:s:o:m:n:B:P:bvhtEp" OPT; do
case $OPT in
S) #subject
S_flag=1
subj="sub-${OPTARG}"
;;
b) #BIDS or not ?
bids_flag=1
;;
a) #T1 WIs
t1_flag=1
t1_orig=$OPTARG
;;
s) #session
s_flag=1
ses=$OPTARG
;;
B) #session
BET_flag=1
BET_m=$OPTARG
;;
l) #lesion_mask
l_flag=1
L_mask=$OPTARG
;;
z) #lesion_mask
l_spaceflag=1
L_mask_space=$OPTARG
;;
m) #intermediate output
m_flag=1
wf_dir=$OPTARG
;;
o) #output
o_flag=1
out_dir=$OPTARG
;;
t) #template flag
t_flag=1
;;
P) #Parcellation flag
P_flag=1
parc_F=$OPTARG
;;
E) #Extra-axial flag
E_flag=1
;;
p) #Pediatric flag
p_flag=1
;;
n) #parallel
n_flag=1
ncpu=$OPTARG
;;
v) #verbose
silent=0
;;
h) #help
Usage >&2
exit 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
lesion_wf="${cwd}/VBG_out"
# output
if [[ "$o_flag" -eq 1 ]]; then
output_m="${out_dir}"
output_d="${output_m}/output_VBG/${subj}${ses_long}"
else
output_d="${lesion_wf}/output_VBG/${subj}${ses_long}"
fi
# intermediate folder
if [[ "$m_flag" -eq 1 ]]; then
preproc_m="${wf_dir}"
preproc="${preproc_m}/proc_VBG/${subj}${ses_long}"
else
preproc="${lesion_wf}/proc_VBG/${subj}${ses_long}"
fi
# timestamp
start_t=$(date +%s)
FSLPARALLEL=$ncpu; export FSLPARALLEL
OMP_NUM_THREADS=$ncpu; export OMP_NUM_THREADS
d=$(date "+%Y-%m-%d_%H-%M-%S");
# handle the dirs
cd $cwd
long_bids_subj="${search_sessions}"
echo $long_bids_subj
bids_subj=${long_bids_subj%anat}
echo $bids_subj
######
# check for required inputs and define your workflow accordingly
# Check if the FreeSurfer License can be found
FS_lic_search[0]="$FREESURFER_HOME/license.txt"
FS_lic_search[1]="$FREESURFER_HOME/.license.txt"
FS_lic_search[3]="$FREESURFER_HOME/.license"
FS_lic_search[4]="$FS_LICENSE"
for s in ${FS_lic_search[@]}; do
if [ ! -z "$s" ]; then
if [[ -f $s ]]; then
# here we set the found FS_lic license file of freesurfer
FS_lic=$s
break
fi
fi
done
if [[ ! -f ${FS_lic} ]]; then
echo "Unable to find a FreeSurfer license file, please make sure it is located in $FREESURFER_HOME with a name of either license.txt or .license, exiting " | tee -a ${prep_log}
exit 2
fi
srch_Lmask_str=($(basename ${L_mask}))
srch_Lmask_dir=($(dirname ${L_mask}))
srch_Lmask_o=($(find ${srch_Lmask_dir} -type f | grep ${srch_Lmask_str}))
if [[ $S_flag -eq 0 ]] || [[ $l_flag -eq 0 ]] || [[ $l_spaceflag -eq 0 ]]; then
echo
echo "Inputs -S -l -z must be set." >&2
echo
exit 2
else
if [[ -z "${srch_Lmask_o}" ]]; then
echo
echo " Incorrect Lesion mask, please check the file path and name "
echo
exit 2
else
echo "Inputs are -S ${subj} -l ${L_mask} -z ${L_mask_space}"
fi
fi
if [[ "$bids_flag" -eq 1 ]] && [[ "$s_flag" -eq 0 ]]; then
# bids flag defined but not session flag
search_sessions=($(find ${cwd}/BIDS/${subj} -type d | grep anat));
num_sessions=${#search_sessions[@]};
ses_long="";
if [[ "$num_sessions" -eq 1 ]]; then
echo " we have one session in the BIDS dir, this is good."
# now we need to search for the images
# then also find which modalities are available and set wf accordingly
### STEFAN NEED TO DO: check what is the best input T1w: with or whitout Gd?
### to select use something like:
### find_T1w=($(find ${cwd}/BIDS/sub-${participant}/anat/ -name "*_T1w.nii.gz" ! -name "*gadolinium*"))
search_T1=($(find $search_sessions -type f | grep T1w.nii.gz));
# search_T2=($(find $search_sessions -type f | grep T2w.nii.gz));
# search_FLAIR=($(find $search_sessions -type f | grep FLAIR.nii.gz));
if [[ $search_T1 ]]; then
T1_orig=$search_T1
echo " We found T1 WIs ${T1_orig}"
else
echo " no T1 WIs found in BIDS dir, exiting"
exit 2
fi
else
echo " There is a problem with sessions in BIDS dir. "
echo " Please double check your data structure &/or specify one session with -s if you have multiple ones. "
exit 2
fi
if [[ "$o_flag" -eq 0 ]]; then
output_d="${cwd}/BIDS/derivatives/output_VBG/${subj}${ses_long}"
fi
if [[ "$m_flag" -eq 0 ]]; then
preproc="${cwd}/BIDS/derivatives/proc_VBG/${subj}${ses_long}"
fi
elif [[ "$bids_flag" -eq 1 ]] && [[ "$s_flag" -eq 1 ]]; then
# this is fine
ses_string="${cwd}/BIDS/${subj}/ses-${ses}"
search_sessions=($(find ${ses_string} -type d | grep anat));
num_sessions=1;
ses_long=_ses-0${num_sessions};
if [[ "$num_sessions" -eq 1 ]]; then
echo " One session " $ses " specified in BIDS dir, good."
### STEFAN NEED TO DO: check what is the best input T1w: with or whitout Gd?
# AR: This can be as added as an optional flag with the bids flag (-b )
# Which is best input to use might require some testing
### to select use something like:
### find_T1w=($(find ${cwd}/BIDS/sub-${participant}/anat/ -name "*_T1w.nii.gz" ! -name "*gadolinium*"))
search_T1=($(find $search_sessions -type f | grep T1w.nii.gz));
# search_T2=($(find $search_sessions -type f | grep T2w.nii.gz));
# search_FLAIR=($(find $search_sessions -type f | grep flair.nii.gz));
if [[ "$search_T1" ]]; then
T1_orig=$search_T1;
echo " We found T1 WIs ${T1_orig}"
else
echo " no T1 WIs found in BIDS dir, exiting "
exit 2
fi
fi
### STEFAN NEED TO DO: prefer to set KUL_compute/sub-participant/VBG as default output?
# AR: this can easily be changed using -o
# no objections to changing default behavior for -o
# We just need to keep FS output stable, as this will be needed for MSBP and other BIDS_apps
if [[ "$o_flag" -eq 0 ]]; then
output_d="${cwd}/BIDS/derivatives/output_VBG/${subj}${ses_long}"
fi
if [[ "$m_flag" -eq 0 ]]; then
preproc="${cwd}/BIDS/derivatives/proc_VBG/${subj}${ses_long}"
fi
elif [[ "$bids_flag" -eq 0 ]] && [[ "$s_flag" -eq 0 ]]; then
# this is fine if T1 and T2 and/or flair are set
# find which ones are set and define wf accordingly
num_sessions=1;
ses_long="";
if [[ "$t1_flag" ]]; then
T1_orig=$t1_orig
echo " T1 images provided as ${t1_orig} "
else
echo " No T1 WIs specified, exiting. "
exit 2
fi
elif [[ "$bids_flag" -eq 0 ]] && [[ "$s_flag" -eq 1 ]]; then
echo " Wrong optional arguments, we cant have sessions without BIDS, exiting."
exit 2
fi
######
ROIs="${output_d}/ROIs"
overlap="${output_d}/overlap"
#####
# make your dirs
mkdir -p ${preproc_m} >/dev/null 2>&1
mkdir -p ${output_m} >/dev/null 2>&1
mkdir -p ${preproc} >/dev/null 2>&1
mkdir -p ${output_d} >/dev/null 2>&1
mkdir -p ${ROIs} >/dev/null 2>&1
mkdir -p ${overlap} >/dev/null 2>&1
# make your log file
prep_log="${preproc}/KUL_VBG_prep_log_${d}.txt";
if [[ ! -f ${prep_log} ]] ; then
touch ${prep_log}
else
echo "${prep_log} already created"
fi
echo " Preproc dir is ${preproc} and output dir is ${output_d}" | tee -a ${prep_log}
echo " You are using KUL_VBG.sh version ${v}" | tee -a ${prep_log}
# deal with ncpu and itk ncpu
# itk default ncpu for antsRegistration
itk_ncpu="export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=${ncpu}"
export $itk_ncpu
silent=1
# decide on BET method
if [[ -z "${BET_flag}" ]]; then
echo
echo " You have not specified a BET method, ANTsBET will be used by default" | tee -a ${prep_log}
echo
BET_m=2
else
if [[ ${BET_m} -eq 1 ]]; then
echo
echo " You have specified HD-BET for brain extraction, please make sure it is called correctly from within KUL_VBG"
echo " You have specified HD-BET for brain extraction, please make sure it is called correctly from within KUL_VBG" | tee -a ${prep_log}
echo " In case of BET problems see lines 1124 - 1200 "
echo
# BET_m=1
elif [[ ${BET_m} -eq 2 ]]; then
echo
echo " You have specified ANTs-BET for brain extraction, please make sure it is called correctly from within KUL_VBG"
echo " You have specified ANTs-BET for brain extraction, please make sure it is called correctly from within KUL_VBG" | tee -a ${prep_log}
echo " In case of BET problems see lines 1124 - 1200 "
echo
# BET_m=2
else
echo
echo " You have specified an incorrect value to the -B option, exiting... "
echo " Correct options for the -B flag are 1 for HD-BET or 2 for ANTs-BET"
exit 2
fi
fi
# set this manually for debugging
function_path=($(which KUL_VBG.sh | rev | cut -d"/" -f2- | rev))
mrtrix_path=($(which mrmath | rev | cut -d"/" -f3- | rev))
FS_path1=($(which recon-all | rev | cut -d"/" -f3- | rev))
if [[ -z ${function_path} ]]; then
echo "update function path to reflect funciton name line 514"
exit 2
else
echo " VBG lives in ${function_path} "
echo " VBG lives in ${function_path} " | tee -a ${prep_log}
fi
# the primary image is the noncontrast T1
prim=${T1_orig}
# this if loop will quit the script if the T1 is not found
if [[ -z "${T1_orig}" ]]; then
echo
echo " Incorrect T1 input, please check the file path and name "
echo
exit 2
else
echo "Inputs are -p ${subj} -T1 ${T1_orig} -lesion ${L_mask} -lesion_space ${L_mask_space}"
echo "Inputs are -p ${subj} -T1 ${T1_orig} -lesion ${L_mask} -lesion_space ${L_mask_space}" | tee -a ${prep_log}
fi
# REST OF SETTINGS ---
# Some parallelisation
if [[ "$n_flag" -eq 0 ]]; then
ncpu=8
echo " -n flag not set, using default 8 threads. "
echo " -n flag not set, using default 8 threads. " | tee -a ${prep_log}
else
echo " -n flag set, using " ${ncpu} " threads."
echo " -n flag set, using " ${ncpu} " threads." | tee -a ${prep_log}
fi
echo "KUL_VBG @ ${d} with parent pid $$ "
echo "KUL_VBG @ ${d} with parent pid $$ " | tee -a ${prep_log}
# --- MAIN ----------------
# Start with your Vars for Part 1
# naming strings
str_pp="${preproc}/${subj}${ses_long}"
str_op="${output_d}/${subj}${ses_long}"
str_overlap="${overlap}/${subj}${ses_long}"
# Template stuff
# check which template to use based on
# is this a pediatric or adult brain and whether we use donor tissue or not
if [[ "${p_flag}" -eq 1 ]] && [[ "${t_flag}" -eq 0 ]]; then
# ADJUST TEMPLATES FOR NKI10U IF P=1 T=0
echo "Working with default pediatric template and priors"
echo "Working with default pediatric template and priors" | tee -a ${prep_log}
MNI_T1="${function_path}/atlasses/Templates_update/VBG_"
MNI_T1_brain="${function_path}/atlasses/Templates_update/NKI10u_temp_brain.nii.gz"
MNI_brain_mask="${function_path}/atlasses/Templates_update/NKI10u_temp_brain_mask.nii.gz"
MNI_brain_pmask="${function_path}/atlasses/Templates_update/ped_PBEM.nii.gz"
MNI_brain_emask="${function_path}/atlasses/Templates_update/ped_BET_mask.nii.gz"
new_priors="${function_path}/atlasses/Templates_update/priors/NKI10U_Prior_%d.nii.gz"
elif [[ "${p_flag}" -eq 1 ]] && [[ "${t_flag}" -eq 1 ]]; then
# ADJUST TEMPLATES FOR VBG_PED IF P=1 T=1
echo "Working with cooked template and priors"
echo "Working with cooked template and priors" | tee -a ${prep_log}
MNI_T1="${function_path}/atlasses/Templates_update/VBG_T1_temp_ped.nii.gz"
MNI_T1_brain="${function_path}/atlasses/Templates_update/VBG_T1_temp_ped_brain.nii.gz"
MNI_brain_mask="${function_path}/atlasses/Templates_update/VBG_T1_temp_ped_brain_mask.nii.gz"
MNI_brain_pmask="${function_path}/atlasses/Templates_update/ped_PBEM.nii.gz"
MNI_brain_emask="${function_path}/atlasses/Templates_update/ped_BET_mask.nii.gz"
new_priors="${function_path}/atlasses/Templates_update/priors/VBG_ped_T_Prior_%d.nii.gz"
elif [[ "${p_flag}" -eq 0 ]] && [[ "${t_flag}" -eq 1 ]]; then
echo "Working with cooked adult template and priors"
echo "Working with cooked adult template and priors" | tee -a ${prep_log}
MNI_T1="${function_path}/atlasses/Templates_update/VBG_T1_temp.nii.gz"
MNI_T1_brain="${function_path}/atlasses/Templates_update/VBG_T1_temp_brain.nii.gz"
MNI_brain_mask="${function_path}/atlasses/Templates_update/VBG_T1_temp_brain_mask.nii.gz"
MNI_brain_pmask="${function_path}/atlasses/Templates_update/adult_PBEM.nii.gz"
MNI_brain_emask="${function_path}/atlasses/Templates_update/adult_BET_mask.nii.gz"
new_priors="${function_path}/atlasses/Templates_update/priors/VBG_adult_T_Prior_%d.nii.gz"
elif [[ "${p_flag}" -eq 0 ]] && [[ "${t_flag}" -eq 0 ]]; then
echo "Working with default adult template and priors"
echo "Working with default adult template and priors" | tee -a ${prep_log}
MNI_T1="${function_path}/atlasses/Templates_update/HR_T1_MNI.nii.gz"
MNI_T1_brain="${function_path}/atlasses/Templates_update/HR_T1_MNI_brain.nii.gz"
MNI_brain_mask="${function_path}/atlasses/Templates_update/HR_T1_MNI_brain_mask.nii.gz"
MNI_brain_pmask="${function_path}/atlasses/Templates_update/adult_PBEM.nii.gz"
MNI_brain_emask="${function_path}/atlasses/Templates_update/adult_BET_mask.nii.gz"
new_priors="${function_path}/atlasses/Templates_update/priors/HRT1_Prior_%d.nii.gz"
fi
MNI2_in_T1="${str_pp}_T1_brain_inMNI2_InverseWarped.nii.gz"
MNI2_in_T1_hm="${str_pp}_T1_brain_inMNI2_InverseWarped_HistMatch.nii.gz"
MNI_r="${function_path}/atlasses/Templates_update/Rt_hemi_mask.nii.gz"
MNI_l="${function_path}/atlasses/Templates_update/Lt_hemi_mask.nii.gz"
MNI_lw="${str_pp}_MNI_L_insubjT1_inMNI1.nii.gz"
MNI_lwr="${str_pp}_MNI_L_insubjT1_inMNI1r.nii.gz"
MNI_rwr="${str_pp}_MNI_R_insubjT1_inMNI1r.nii.gz"
L_hemi_mask="${str_pp}_L_hemi_mask_bin.nii.gz"
H_hemi_mask="${str_pp}_H_hemi_mask_bin.nii.gz"
L_hemi_mask_binv="${str_pp}_L_hemi_mask_binv.nii.gz"
H_hemi_mask_binv="${str_pp}_H_hemi_mask_binv.nii.gz"
# CSF+GMC+GMB+WM and the rest
tmp_s2T1_nCSFGMC="${str_pp}_tmp_s2T1_nCSFGMC.nii.gz"
tmp_s2T1_nCSFGMCB="${str_pp}_tmp_s2T1_nCSFGMCB.nii.gz"
tmp_s2T1_nCSFGMCBWM="${str_pp}_tmp_s2T1_nCSFGMCBWM.nii.gz"
tmp_s2T1_nCSFGMCBWMr="${str_pp}_tmp_s2T1_nCSFGMCBWMr.nii.gz"
tmp_s2T1_CSFGMCBWM="${str_pp}_tmp_s2T1_CSFGMCBWM.nii.gz"
MNI2_in_T1_scaled="${str_pp}_MNI_brain_IW_scaled.nii.gz"
tissues=("CSF" "GMC" "GMBG" "WM");
priors_str="${new_priors::${#new_priors}-9}*.nii.gz"
priors_array=($(ls ${priors_str}))
if [[ -z ${priors_array} ]]; then
echo " priors are not found!"
exit 2
else
echo "priors are ${priors_array}"
fi
# arrays
declare -a Atropos1_posts
declare -a Atropos2_posts
# need also to declare tpm arrays
declare -a atropos1_tpms_Lfill
declare -a atropos2_tpms_filled
declare -a atropos2_tpms_filled_GLC
declare -a atropos2_tpms_filled_GLCbinv
declare -a atropos2_tpms_punched
declare -a NP_arr_rs
declare -a NP_arr_rs_bin
declare -a NP_arr_rs_binv
declare -a NP_arr_rs_bin2
declare -a NP_arr_rs_binv2
declare -a Atropos2_posts_bin
declare -a Atropos2_posts_bin2
declare -a Atropos1_posts_bin
declare -a T1_ntiss_At2masked
declare -a nMNI2_inT1_ntiss_sc2T1MNI1
declare -a MNI2_inT1_ntiss
declare -a Atropos2_Int_finder
declare -a R_nTiss_Norm_mean
declare -a R_nTiss_Int_map_norm
declare -a Atropos1b_ntiss_map
declare -a A1_nTiss_Norm_mean
declare -a A1_nTiss_Int_scaled
declare -a A1_nTiss_Int_scaled_fill
declare -a R_nTiss_map_filled
# input variables
# lesion stuff
Lmask_o=$L_mask
L_mask_reori="${str_pp}_L_mask_reori.nii.gz"
L_mask_reori1="${str_pp}_L_mask_reori1.nii.gz"
Lmask_FS_reori="${str_pp}_Lmask_FS_reori.nii.gz"
L_mask_affMNI1="${str_pp}_L_mask_r_MNI1aff.nii.gz"
L_mask_MNI1c_bin="${str_pp}_L_mask_r_MNI1aff_bin.nii.gz"
L_mask_MNI1c_binv="${str_pp}_L_mask_r_MNI1aff_binv.nii.gz"
L_O_binv="${str_pp}_L_mask_reori_binv.nii.gz"
Lmask_bin="${str_pp}_L_mask_orig_bin.nii.gz"
Lmask_in_T1="${str_pp}_L_mask_in_T1.nii.gz"
Lmask_in_T1_bin="${str_pp}_L_mask_in_T1_bin.nii.gz"
Lmask_in_T1_binv="${str_pp}_L_mask_in_T1_binv.nii.gz"
Lmask_bin_s3="${str_pp}_Lmask_in_T1_bins3.nii.gz"
Lmask_bin_s3_flat="${str_pp}_Lmask_in_T1_bins3_flat.nii.gz"
Lmask_binv_s3_nobrain="${str_pp}_Lmask_in_T1_binvs3_nobrain.nii.gz"
Lmask_binv_s3="${str_pp}_Lmask_in_T1_binvs3.nii.gz"
brain_mask_minL="${str_pp}_antsBET_BrainMask_min_L.nii.gz"
brain_mask_minL_inMNI1="${str_pp}_brainmask_minL_inMNI1.nii.gz"
brain_mask_minL_inMNI2="${str_pp}_brainmask_minL_inMNI2.nii.gz"
brain_mask_minL_atropos2="${str_pp}_brainmask_minL_atropos2.nii.gz"
Lmask_bin_inMNI1="${str_pp}_Lmask_bin_inMNI1.nii.gz"
Lmask_binv_inMNI1="${str_pp}_Lmask_binv_inMNI1.nii.gz"
Lmask_bin_inMNI1_s3="${str_pp}_Lmask_bin_s3_inMNI1.nii.gz"
Lmask_bin_inMNI1_dilx2="${str_pp}_Lmask_bin_inMNI1_dilmx2.nii.gz"
Lmask_binv_inMNI1_dilx2="${str_pp}_Lmask_binv_inMNI1_dilmx2.nii.gz"
Lmask_binv_inMNI1_s3="${str_pp}_Lmask_binv_s3_inMNI1.nii.gz"
Lmask_bin_inMNI2_s3="${str_pp}_Lmask_bin_s3_inMNI2.nii.gz"
Lmask_binv_inMNI2_s3="${str_pp}_Lmask_binv_s3_inMNI2.nii.gz"
Lmask_bin_inMNI2="${str_pp}_Lmask_bin_inMNI2.nii.gz"
fbrain_mask_minL_inMNI1="${str_pp}_fbrainmask_minL_inMNI1.nii.gz"
L_fill_T1="${str_pp}_T1_Lfill_inMNI2.nii.gz"
nat_T1_filled1="${str_pp}_T1inMNI2_fill1.nii.gz"
stitched_T1_temp="${str_pp}_stitched_T1_brain_temp.nii.gz"
stitched_T1_nat="${str_pp}_stitched_T1_brain_nat.nii.gz"
stitched_T1_innat="${str_pp}_stitched_T1_brain_bk2nat.nii.gz"
# stitched_T1_temp_innat="${str_pp}_stitched_T1_brain_temp_bk2nat.nii.gz"
# stitched_T1="${str_pp}_stitched_T1_brain.nii.gz"
T1_bk2nat1_str="${str_pp}_T1_brain_bk2anat1_"
Temp_L_hemi="${str_pp}_Temp_L_hemi_filler.nii.gz"
Temp_L_fill_T1="${str_pp}_Temp_L_fill_T1.nii.gz"
Temp_T1_bilfilled1="${str_pp}_T1_brain_Temp_bil_Lmask_filled1.nii.gz"
Temp_bil_Lmask_fill1="${str_pp}_Temp_bil_Lmask_fill1.nii.gz"
Temp_T1_filled1="${str_pp}_Temp_T1inMNI2_filled1.nii.gz"
T1_filled_bk2nat1="${str_pp}_T1_brain_bk2anat1_InverseWarped.nii.gz"
filled_segm_im="${str_pp}_atropos1_Segmentation_2nat.nii.gz"
real_segm_im="${str_pp}_atropos2_Segmentation_2nat.nii.gz"
Lfill_segm_im="${str_pp}_Lfill_segmentation_im.nii.gz"
atropos2_segm_im_filled="${str_pp}_filled_atropos2_segmentation_im.nii.gz"
atropos2_segm_im_filled_nat="${str_pp}_filled_atropos2_segmentation_im.nii.gz"
lesion_left_overlap="${str_overlap}_L_lt_overlap.nii.gz"
lesion_right_overlap="${str_overlap}_L_rt_overlap.nii.gz"
smoothed_binLmask15="${str_pp}_smoothedLmaskbin15.nii.gz"
smoothed_binvLmask15="${str_pp}_smoothedLmaskbinv15.nii.gz"
# last lesion related vars (hopefully)
L_mask_reori_scaled="${str_pp}_L_mask_reori_scaled99.nii.gz"
bmc_minL_conn="${str_pp}_brain_mask_cleaned_minL_conn.nii.gz"
bmc_minL_true="${str_pp}_brain_mask_cleaned_minL.nii.gz"
L_mask_reori_ero1="${str_pp}_L_mask_reori_ero1.nii.gz"
bmc_minL_ero1="${str_pp}_brain_mask_cleaned_minL_ero1.nii.gz"
L_mask_reori_ero2="${str_pp}_L_mask_reori_ero2.nii.gz"
bmc_minL_ero2="${str_pp}_brain_mask_cleaned_minL_ero2.nii.gz"
# img vars for part 1 and 2
T1_reori_mat="${str_pp}_T1_reori2std_matrix.mat"
T1_reori_mat_inv="${str_pp}_T1_reori2std_matrix_inv.mat"
# T1_pp1="${str_pp}_T1_bfc.nii.gz"
T1_pp1="${str_pp}_T1_dn_thr.nii.gz"
T1_brain="${str_pp}_antsBET_BrainExtractionBrain.nii.gz"
brain_mask="${str_pp}_antsBET_BrainExtractionMask.nii.gz"
T1_inMNI_aff_str="${str_pp}_T1_inMNI_aff"
T1_inMNI_aff="${str_pp}_T1_inMNI_aff_Warped.nii.gz"
KULBETp="${str_pp}_atropos4BET"
rough_mask="${str_pp}_rough_mask.nii.gz"
rough_mask_minL="${str_pp}_rough_mask_minL.nii.gz"
clean_mask_nat_binv="${str_pp}_clean_brain_mask_nat_binv.nii.gz"
T1_brain_clean="${str_pp}_Brain_clean.nii.gz"
MNI_bm_BET_innat="${str_pp}_MNI_BM_inNat.nii.gz"
clean_mask_nat="${str_pp}_Brain_clean_mask.nii.gz"
clean_BM_mgz="${str_pp}_Brain_clean_mask.mgz"
hdbet_str="${str_pp}_Brain_clean"
BET_mask_s2="${str_pp}_antsBET_Mask_s2.nii.gz"
BET_mask_binvs2="${str_pp}_antsBET_Mask_binv_s2.nii.gz"
T1_skull="${str_pp}_T1_skull.nii.gz"
T1_brMNI1_str="${str_pp}_T1_brain_inMNI1_"
T1_brain_inMNI1="${str_pp}_T1_brain_inMNI1_Warped.nii.gz"
# T1_noise_inMNI1="${str_pp}_T1_noise_inMNI1.nii.gz"
# fT1_noise_inMNI1="${str_pp}_fT1_noise_inMNI1.nii.gz"
# T1_noise_H_hemi="${str_pp}_T1_noise_Hhemi_inMNI1.nii.gz"
# stitched_noise_MNI1="${str_pp}_T1_stitched_noise_inMNI1.nii.gz"
# stitched_noise_nat="${str_pp}_T1_stitched_noise_nat.nii.gz"
T1_brMNI2_str="${str_pp}_T1_brain_inMNI2_"
T1_brain_inMNI2="${str_pp}_T1_brain_inMNI2_Warped.nii.gz"
fT1brain_inMNI1="${str_pp}_fT1_brain_inMNI1_Warped.nii.gz"
fT1_brMNI2_str="${str_pp}_fT1_brain_inMNI2_"
fT1brain_inMNI2="${str_pp}_fT1_brain_inMNI2_Warped.nii.gz"
brain_mask_inMNI1="${str_pp}_brain_mask_inMNI1.nii.gz"
# MNI_brain_mask_in_nat="${str_pp}_MNI_brain_mask_in_nat.nii.gz"
T1_sti2fil_str="${str_pp}_stitchT12filled_brain_"