-
Notifications
You must be signed in to change notification settings - Fork 12
/
KUL_lesion_fs_recall.sh
executable file
·1327 lines (794 loc) · 42.8 KB
/
KUL_lesion_fs_recall.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 [email protected]
# Stefan Sunaert [email protected]
# # v 1.0 - dd 20/03/2019 - dev Alpha
v="1.0 - dd 20/03/2019"
# This script is meant for allowing a decent recon-all output in the presence of a large brain lesion
# It is not a final end-all solution but a rather crude and simplistic work around
# The main idea is to replace the lesion with a hole and fill the hole with information from normal hemisphere # maintains subject specificity and diseased hemisphere information but replaces lesion tissue with sham brain
# should be followed by a loop calculating overlap between fake labels resulting from sham brain with actual lesion
# To do: #
# - A similar approach for bilateral lesions could still work, using a population template perhaps ?
# - improve lesion fill patch ( elastic registration ?)
# - Use also MNI space images in recon-all
# - add N4 bias correction to MNI space images
# - test -a -t -f and -s flags
# - Add QC step similarity measure as follows:
# MeasureImageSimilarity -d 3 --metric MI[../../../BIDS/sub-PT01/anat/sub-PT01_T1w.nii.gz,../../../lesion_wf/lesion_wf_output/sub-PT01/sub-PT01_T1nat_T1_sim_lesionless_Warped.nii.gz,1,64] \
# --masks [../../../lesion_wf/lesion_wf_preproc/sub-PT01/sub-PT01_MNI_brain_mask_in_T1nat_minlesion.nii.gz,../../../lesion_wf/lesion_wf_preproc/sub-PT01/sub-PT01_MNI_mask_min_lesion.nii.gz] \
# -o ./test_im_sim_MI.nii.gz -v
# - add a sharpening step to the hemispheres before generating the lesion_fill
# also do a sharpening step to the image before making the hole and replacing the lesion with the fill patch.
# Description:
# Info and Instructions:
# This functiona in its current state is a WIP for S61759 AR,SK,EC,TT,PD,SS
# - KUL_Lesion_FSrecall takes as input T1, T2, FLAIR and a lesion mask
# - The end result is running Freesurfer recon-all on lesioned brains
# - We start with unprocessed data
# - At least 2 modalities (T1 + * ) are required plus a lesion mask
# - The idea is to use warped healthy subject specific brain tissue to fill the lesioned area.
# - Takes as input: subject_label, BIDS_dir, lesion mask file path & name
# Steps:
# - The input images are rigidly aligned to the T1, T1 affine 2 MNI, then all to MNI
# - The MNI images are flipped in LR # % - The lesion mask in binarized and inverted in MNI + either smoothed or dilated
# - This is followed by a rough brain extraction in MNI
# - The lesion & brain masks are combined with apriori left right split masks in MNI
# - Left & right hemispheres are split from the rough BET in MNI images
# - Determine if lesion is R or L, and warp as follows
# - Use lesioned_side-lesion_mask as target mask (fixed image MNI unflip brain)
# - Use lesioned_side_w_lesion mask as input mask (source image MNI flip brain)
# - Lesion fill is created out of the warped healthy to unhealthy brain tissue
# - Lesion fill is inserted in place of lesion in original MNI images
# - Inverse warp to native space T1
# - Use both native T1&T2 and MNIT1&T2 to run recon-all as if we have two sessions with two modalities each.
# - Calculate percentage volume overlap with each lobe.
# - Move results to KUL_NITs location # % @ Ahmed Radwan ([email protected]) 07/03/2019
# Requires: FSL, Freesurfer, T1 and T2 WIs and lesion mask out of itk-snap
# to add a lesion argument later
# - lesion mask name should include space, modality, and adhere to BIDS naming scheme
# ----------------------------------- 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:
`basename $0` -p subject <OPT_ARGS> -l <OPT_ARGS> -z <OPT_ARGS> -b
or
`basename $0` -p subject <OPT_ARGS> -a <OPT_ARGS> -b <OPT_ARGS> -c <OPT_ARGS> -l <OPT_ARGS> -z <OPT_ARGS>
Examples:
`basename $0` -p pat001 -b -n 6 -l /fullpath/lesion_T1w.nii.gz -z T1 -o /fullpath/output
`basename $0` -p pat001 -n 6 -a /fullpath/T1w.nii.gz -b /fullpath/T2w.nii.gz -l /fullpath/lesion_T2w.nii.gz -z T2
`basename $0` -p pat001 -n 6 -a /fullpath/T1w.nii.gz -c /fullpath/flair.nii.gz -l /fullpath/lesion_flair.nii.gz -z FLAIR
Required arguments:
-p: BIDS 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 (T1, T2, or FLAIR)
-a: full path and file name to T1
-t: full path and file name to T2
-f: full path and file name to T2 FLAIR
-m: full path to intermediate output dir
-o: full path to output dir (if not set reverts to default output ./lesion_wf_output)
(This workflow require at least 2 modalities T1 + T2 or FLAIR)
Optional arguments:
-s: session (of the participant)
-n: number of cpu for parallelisation
-v: show output from mrtrix commands
-h: prints help menu
USAGE
exit 1
}
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
ncpu=6
silent=1
# Set required options
p_flag=0
bids_flag=0
s_flag=0
l_flag=0
l_spaceflag=0
t1_flag=0
t2_flag=0
flair_flag=0
o_flag=0
m_flag=0
n_flag=0
if [ "$#" -lt 1 ]; then
Usage >&2
exit 1
else
while getopts "p:a:t:f:l:z:s:o:m:n:bvh" OPT; do
case $OPT in
p) #subject
p_flag=1
subj=$OPTARG
;;
b) #BIDS or not ?
bids_flag=1
;;
a) #T1 WIs
t1_flag=1
t1_orig=$OPTARG
;;
t) #T2 WIs
t2_flag=1
t2_orig=$OPTARG
;;
f) #Flair WIs
flair_flag=1
flair_orig=$OPTARG
;;
s) #session
s_flag=1
ses=$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
;;
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
# check for required inputs and define your workflow accordingly
if [[ $p_flag -eq 0 || $l_flag -eq 0 || $l_spaceflag -eq 0 ]]; then
echo
echo "Inputs -p -lesion -lesion_space must be set." >&2
echo
exit 2
else
echo "Inputs are -p " $subj " -lesion " $L_mask " -lesion_space " $L_mask_space
fi
if [[ $bids_flag -eq 1 && $s_flag -eq 0 ]]; then
# bids flag defined but not session flag
search_sessions=($(find ${cwd}/BIDS/sub-${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
# here we also need to search for the images
# then also find which modalities are available and set wf accordingly
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
if [[ $search_T2 && ! $search_FLAIR ]]; then
wf=1;
T2_orig=$search_T2;
echo " We found also T2 WIs, but no FLAIR WIs "
elif [[ ! $search_T2 && $search_FLAIR ]]; then
wf=2;
FLAIR_orig=$search_FLAIR;
echo " We found also FLAIR WIs, but no T2 WIs "
elif [[ $search_T2 && $search_FLAIR ]]; then
wf=3;
T2_orig=$search_T2;
FLAIR_orig=$search_FLAIR;
echo " We found also T2 WIs, and FLAIR WIs "
else
echo " This script requires at least T1 WIs + either T2 or FLAIR WIs, exiting."
exit 2
fi
else
echo " There's 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
elif [[ $bids_flag -eq 1 && $s_flag -eq 1 ]]; then
# this is fine
search_sessions=($(find ${cwd}/BIDS/sub-${subj}_ses-${ses} -type d | grep anat));
num_sessions=1;
ses_long=_ses-0${num_sessions};
# here we also need to search for the images
# then also find which modalities are available and set wf accordingly
if [[ $num_sessions -eq 1 ]]; then
echo " One session " $ses " specified in BIDS dir, good."
# now we need to search for the images
# here we also need to search for the images
# then also find which modalities are available and set wf accordingly
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
if [[ $search_T2 && ! $search_FLAIR ]]; then
wf=1;
T2_orig=$search_T2;
echo " We found also T2 WIs, but no FLAIR WIs "
elif [[ ! $search_T2 && $search_FLAIR ]]; then
wf=2;
FLAIR_orig=$search_FLAIR;
echo " We found also FLAIR WIs, but no T2 WIs "
elif [[ $search_T2 && $search_FLAIR ]]; then
wf=3;
T2_orig=$search_T2;
FLAIR_orig=$search_FLAIR;
echo " We found also T2 WIs, and FLAIR WIs "
else
echo " This script requires at least T1 WIs + either T2 or FLAIR WIs, exiting."
exit 2
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
ses_long="";
if [[ $t1_flag ]]; then
T1_orig=$t1_orig
else
echo " No T1 WIs specified, exiting. "
exit 2
fi
if [[ $seach_T1 && $search_T2 && ! $search_FLAIR ]]; then
wf=1;
T2_orig=$search_T2;
echo " We also have T2 WIs, but no FLAIR WIs "
elif [[ $seach_T1 && ! $search_T2 && $search_FLAIR ]]; then
wf=2;
FLAIR_orig=$search_FLAIR;
echo " We also have FLAIR WIs, but no T2 WIs "
elif [[ $seach_T1 && $search_T2 && $search_FLAIR ]]; then
wf=3;
T2_orig=$search_T2;
FLAIR_orig=$search_FLAIR;
echo " We also have T2 WIs, and FLAIR WIs "
else
echo " This script requires at least T1 WIs + either T2 or FLAIR WIs, exiting."
exit 2
fi
elif [[ $bids_flag -eq 0 && $s_flag -eq 1 ]]; then
echo " Wrong optional arguments, we can't have sessions without BIDS, exiting."
exit 2
fi
fi
function_path=($(which KUL_lesion_fs_recall.sh | rev | cut -d"/" -f2- | rev))
# REST OF SETTINGS ---
# timestamp
start_t=$(date +%s)
# Some parallelisation
if [[ $n_flag -eq 0 ]]; then
ncpu=6
echo " -n flag not set, using default 6 threads. "
else
echo " -n flag set, using " ${ncpu} " threads."
fi
# 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 ----------------
# here we give session invariate variables (e.g. template images)
# only in for WIP - R 09-03-2019
# subj=BAC
# The necessary priors
# need to use multiple templates here...
# will need to include a folder with priors
MNI_T1=${function_path}/atlasses/Templates/MNI_T1.nii.gz
MNI_T1_brain=${function_path}/atlasses/Templates/MNI_T1_brain.nii.gz
# MNI_T2=${function_path}/atlasses/Templates/MNI_T2.nii.gz
MNI_T2_brain=${function_path}/atlasses/Templates/MNI_T2_brain.nii.gz
# MNI_FLAIR=${function_path}/atlasses/Templates/MNI_FLAIR.nii.gz
MNI_FLAIR_brain=${function_path}/atlasses/Templates/MNI_FLAIR_brain.nii.gz
MNI_brain_mask=${function_path}/atlasses/Templates/MNI_brain_mask.nii.gz
MNI_rl=${function_path}/atlasses/Templates/MNI_RL1c_labels.nii.gz
# Either a session is given on the command line
# If not the session(s) need to be determined.
# ---- BIG LOOP for processing each session
#
for current_session in `seq 0 $(($num_sessions-1))`; do
cd $cwd
long_bids_subj=${search_sessions[$current_session]}
echo $long_bids_subj
bids_subj=${long_bids_subj%anat}
echo $bids_subj
lesion_wf=${cwd}/lesion_wf
# output
if [[ $o_flag -eq 1 ]]; then
output=$out_dir
else
output=${lesion_wf}/lesion_wf_output/sub-${subj}${ses_long}
fi
# intermediate folder
if [[ $m_flag -eq 1 ]]; then
preproc=$wf_dir
else
preproc=${lesion_wf}/lesion_wf_preproc/sub-${subj}${ses_long}
fi
echo $lesion_wf
ROIs=${output}/ROIs
overlap=${output}/overlap
# make your dirs
mkdir -p ${preproc}
mkdir -p ${output}
mkdir -p ${ROIs}
mkdir -p ${overlap}
# The lesion as provided (this depends on the session)
L_mask_bin=${L_mask::${#L_mask}-7}.nii.gz
fslmaths $L_mask -bin $L_mask_bin
L_mask_orig=${L_mask_bin}
L_mask_binv_in_T1=${preproc}/sub-${subj}${ses_long}_L_mask_binv_T1.nii.gz
L_mask_in_MNI=${preproc}/sub-${subj}${ses_long}_L_mask_in_MNI.nii.gz
L_mask_in_MNI_dil=${preproc}/sub-${subj}${ses_long}_L_mask_dil_in_MNI.nii.gz
L_mask_in_MNI_dil_binv=${preproc}/sub-${subj}${ses_long}_L_mask_dil_binv_in_MNI.nii.gz
MNI_mask_min_lesion=${preproc}/sub-${subj}${ses_long}_MNI_mask_min_lesion.nii.gz
lesion_left_hemi_overlap=${preproc}/sub-${subj}${ses_long}_lesion_left_hemi_overlap.nii.gz
lesion_right_hemi_overlap=${preproc}/sub-${subj}${ses_long}_lesion_right_hemi_overlap.nii.gz
# vars for images
T2_in_T1=${preproc}/sub-${subj}${ses_long}_T2_in_T1_Warped.nii.gz
FLAIR_in_T1=${preproc}/sub-${subj}${ses_long}_FLAIR_in_T1_Warped.nii.gz
T2_to_T1_affine=${preproc}/sub-${subj}${ses_long}_T2_in_T1_0GenericAffine.mat
FLAIR_to_T1_affine=${preproc}/sub-${subj}${ses_long}_FLAIR_in_T1_0GenericAffine.mat
T1_in_MNI=${preproc}/sub-${subj}${ses_long}_T1_in_MNI_Warped.nii.gz
T1_N4BFC=${preproc}/sub-${subj}${ses_long}_T1_N4BFC.nii.gz
T2_N4BFC=${preproc}/sub-${subj}${ses_long}_T2_N4BFC.nii.gz
FLAIR_N4BFC=${preproc}/sub-${subj}${ses_long}_FLAIR_N4BFC.nii.gz
MNI_brain_mask_in_nat=${preproc}/sub-${subj}${ses_long}_MNI_brain_mask_in_T1nat.nii.gz
MNI_brain_mask_in_nat_min_lesion=${preproc}/sub-${subj}${ses_long}_MNI_brain_mask_in_T1nat_minlesion.nii.gz
T1_in_MNI_brain=${preproc}/sub-${subj}${ses_long}_T1_in_MNI_BrainExtractionBrain.nii.gz
subj_brain_mask_in_MNI=${preproc}/sub-${subj}${ses_long}_T1_in_MNI_BrainExtractionMask.nii.gz
T1_in_MNI_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_T1_in_MNI_brain.nii.gz
T1_in_MNI_SyN_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_T1_brain_in_MNI_SyN_Warped.nii.gz
T2_in_MNI=${preproc}/sub-${subj}${ses_long}_T2_in_MNI_Warped.nii.gz
T2_in_MNI_brain=${preproc}/sub-${subj}${ses_long}_T2_in_MNI_brain.nii.gz
T2_in_MNI_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_T2_in_MNI_brain.nii.gz
T2_in_MNI_SyN_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_T2_brain_in_MNI_SyN_Warped.nii.gz
FLAIR_in_MNI=${preproc}/sub-${subj}${ses_long}_FLAIR_in_MNI_Warped.nii.gz
FLAIR_in_MNI_brain=${preproc}/sub-${subj}${ses_long}_FLAIR_in_MNI_brain.nii.gz
FLAIR_in_MNI_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_FLAIR_in_MNI_brain.nii.gz
FLAIR_in_MNI_SyN_brain_flip=${preproc}/sub-${subj}${ses_long}_flip_FLAIR_brain_in_MNI_SyN_Warped.nii.gz
T1_to_MNI_affine=${preproc}/sub-${subj}${ses_long}_T1_in_MNI_0GenericAffine.mat
T1_with_lesion_fill_MNI=${output}/sub-${subj}${ses_long}_MNI_T1_sim_lesionless.nii.gz
T2_with_lesion_fill_MNI=${output}/sub-${subj}${ses_long}_MNI_T2_sim_lesionless.nii.gz
FLAIR_with_lesion_fill_MNI=${output}/sub-${subj}${ses_long}_MNI_FLAIR_sim_lesionless.nii.gz
T1_with_lesion_fill_T1nat=${output}/sub-${subj}${ses_long}_T1nat_T1_sim_lesionless_Warped.nii.gz
T2_with_lesion_fill_T1nat=${output}/sub-${subj}${ses_long}_T1nat_T2_sim_lesionless_Warped.nii.gz
FLAIR_with_lesion_fill_T1nat=${output}/sub-${subj}${ses_long}_T1nat_FLAIR_sim_lesionless_Warped.nii.gz
MNI_left=${preproc}/MNI_left_bin.nii.gz
MNI_right=${preproc}/MNI_right_bin.nii.gz
# kul_e2cl " Start processing $bids_subj" ${preproc}/${log}
cd $preproc
# determine which workflow we need to apply and run it.
# this is distributed in nested if loops
# processing control flags are called wf_mark, listed below.
wf_mark1=${preproc}"/first_part_done.done"
wf_mark2=${preproc}"/second_part_done.done"
wf_mark3=${preproc}"/third_part_done.done"
search_wf_mark1=($(find ${preproc} -type f | grep first_part_done.done));
search_wf_mark2=($(find ${preproc} -type f | grep second_part_done.done));
search_wf_mark3=($(find ${preproc} -type f | grep third_part_done.done));
if [[ ! $search_wf_mark1 ]] ; then
N4BiasFieldCorrection -d 3 -i $T1_orig -o $T1_N4BFC
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T1 -m $T1_N4BFC -t a -o ${preproc}/sub-${subj}${ses_long}_T1_in_MNI_ -n ${ncpu}
# This screws me over big time! let's do a respectable BET
# fslmaths $T1_in_MNI -mas $subj_brain_mask_in_MNI $T1_in_MNI_brain
antsBrainExtraction.sh -d 3 -a ${T1_in_MNI} -e ${MNI_T1} -m ${MNI_brain_mask} -o ${preproc}/sub-${subj}${ses_long}_T1_in_MNI_ -u 1
sleep 2
WarpImageMultiTransform 3 $subj_brain_mask_in_MNI $MNI_brain_mask_in_nat -R $T1_orig -i $T1_to_MNI_affine
fslmaths $MNI_brain_mask_in_nat -bin $MNI_brain_mask_in_nat
fslswapdim $T1_in_MNI_brain -x y z $T1_in_MNI_brain_flip
fslorient -swaporient $T1_in_MNI_brain_flip
echo " first part done. " >> $wf_mark1
else
echo " fist part already done, skipping. "
fi
if [[ ! $search_wf_mark2 ]] ; then
if [[ $wf -eq 1 ]]; then
N4BiasFieldCorrection -d 3 -i $T2_orig -o $T2_N4BFC
antsRegistrationSyNQuick.sh -d 3 -f $T1_orig -m $T2_N4BFC -t a -o $preproc/sub-${subj}${ses_long}_T2_in_T1_ -n ${ncpu}
WarpImageMultiTransform 3 $T2_in_T1 $T2_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $T2_in_MNI -mas $subj_brain_mask_in_MNI $T2_in_MNI_brain
if [[ $L_mask_space == "T1" ]] ; then
L_mask_in_T1=$L_mask_orig
elif [[ $L_mask_space == "T2" ]] ; then
L_mask_in_T2=$L_mask_orig
L_mask_in_T1=$preproc/sub-${subj}${ses_long}_L_mask_in_T2_to_T1.nii.gz
WarpImageMultiTransform 3 $L_mask_in_T2 $L_mask_in_T1 -R $T1_orig $T2_to_T1_affine
fslmaths $L_mask_in_T1 -bin $L_mask_in_T1
fi
# flip T2 in MNI
fslswapdim $T2_in_MNI_brain -x y z $T2_in_MNI_brain_flip
fslorient -swaporient $T2_in_MNI_brain_flip
WarpImageMultiTransform 3 $L_mask_in_T1 $L_mask_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $L_mask_in_MNI -dilM -bin $L_mask_in_MNI_dil
fslmaths $L_mask_in_MNI_dil -binv $L_mask_in_MNI_dil_binv
fslmaths $subj_brain_mask_in_MNI -mas $L_mask_in_MNI_dil_binv $MNI_mask_min_lesion
# SyN warp the flipped brains to the respective MNI template
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T1_brain -m $T1_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_T1_brain_in_MNI_SyN_ -n ${ncpu}
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T2_brain -m $T2_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_T2_brain_in_MNI_SyN_ -n ${ncpu}
elif [[ $wf -eq 2 ]] ; then
N4BiasFieldCorrection -d 3 -i $FLAIR_orig -o $FLAIR_N4BFC
antsRegistrationSyNQuick.sh -d 3 -f $T1_orig -m $FLAIR_N4BFC -t a -o $preproc/sub-${subj}${ses_long}_FLAIR_in_T1_ -n ${ncpu}
WarpImageMultiTransform 3 $FLAIR_in_T1 $FLAIR_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $FLAIR_in_MNI -mas $subj_brain_mask_in_MNI $FLAIR_in_MNI_brain
if [[ $L_mask_space == "T1" ]] ; then
L_mask_in_T1=$L_mask_orig
elif [[ $L_mask_space == "FLAIR" ]] ; then
L_mask_in_FLAIR=$L_mask_orig
L_mask_in_T1=$preproc/sub-${subj}${ses_long}_L_mask_in_FLAIR_to_T1.nii.gz
WarpImageMultiTransform 3 $L_mask_in_FLAIR $L_mask_in_T1 -R $T1_orig $FLAIR_to_T1_affine
fslmaths $L_mask_in_T1 -bin $L_mask_in_T1
fi
# flip FLAIR in MNI
fslswapdim $FLAIR_in_MNI_brain -x y z $FLAIR_in_MNI_brain_flip
fslorient -swaporient $FLAIR_in_MNI_brain_flip
WarpImageMultiTransform 3 $L_mask_in_T1 $L_mask_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $L_mask_in_MNI -dilM -bin $L_mask_in_MNI_dil
fslmaths $L_mask_in_MNI_dil -binv $L_mask_in_MNI_dil_binv
fslmaths $subj_brain_mask_in_MNI -mas $L_mask_in_MNI_dil_binv $MNI_mask_min_lesion
# SyN warp the flipped brains to the respective MNI template
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T1_brain -m $T1_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_T1_brain_in_MNI_SyN_ -n ${ncpu}
antsRegistrationSyNQuick.sh -d 3 -f $MNI_FLAIR_brain -m $FLAIR_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_FLAIR_brain_in_MNI_SyN_ -n ${ncpu}
elif [[ $wf -eq 3 ]] ; then
N4BiasFieldCorrection -d 3 -i $T2_orig -o $T2_N4BFC
N4BiasFieldCorrection -d 3 -i $FLAIR_orig -o $FLAIR_N4BFC
antsRegistrationSyNQuick.sh -d 3 -f $T1_orig -m $FLAIR_N4BFC -t a -o $preproc/sub-${subj}${ses_long}_FLAIR_in_T1_ -n ${ncpu}
antsRegistrationSyNQuick.sh -d 3 -f $T1_orig -m $T2_N4BFC -t a -o $preproc/sub-${subj}${ses_long}_T2_in_T1_ -n ${ncpu}
WarpImageMultiTransform 3 $T2_in_T1 $T2_in_MNI -R $MNI_T1 $T1_to_MNI_affine
WarpImageMultiTransform 3 $FLAIR_in_T1 $FLAIR_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $T1_in_MNI -mas $subj_brain_mask_in_MNI $T1_in_MNI_brain
fslmaths $T2_in_MNI -mas $subj_brain_mask_in_MNI $T2_in_MNI_brain
fslmaths $FLAIR_in_MNI -mas $subj_brain_mask_in_MNI $FLAIR_in_MNI_brain
if [[ $L_mask_space == "T1" ]] ; then
L_mask_in_T1=$L_mask_orig
elif [[ $L_mask_space == "T2" ]] ; then
L_mask_in_T2=$L_mask_orig
L_mask_in_T1=$preproc/sub-${subj}${ses_long}_L_mask_in_T2_to_T1.nii.gz
WarpImageMultiTransform 3 $L_mask_in_T2 $L_mask_in_T1 -R $T1_orig $T2_to_T1_affine
fslmaths $L_mask_in_T1 -bin $L_mask_in_T1
elif [[ $L_mask_space == "FLAIR" ]] ; then
L_mask_in_FLAIR=$L_mask_orig
L_mask_in_T1=$preproc/sub-${subj}${ses_long}_L_mask_in_FLAIR_to_T1.nii.gz
WarpImageMultiTransform 3 $L_mask_in_FLAIR $L_mask_in_T1 -R $T1_orig $FLAIR_to_T1_affine
fslmaths $L_mask_in_T1 -bin $L_mask_in_T1
fi
# flip T2 and FLAIR in MNI
fslswapdim $T2_in_MNI_brain -x y z $T2_in_MNI_brain_flip
fslorient -swaporient $T2_in_MNI_brain_flip
fslswapdim $FLAIR_in_MNI_brain -x y z $FLAIR_in_MNI_brain_flip
fslorient -swaporient $FLAIR_in_MNI_brain_flip
WarpImageMultiTransform 3 $L_mask_in_T1 $L_mask_in_MNI -R $MNI_T1 $T1_to_MNI_affine
fslmaths $L_mask_in_MNI -dilM -bin $L_mask_in_MNI_dil
fslmaths $L_mask_in_MNI_dil -binv $L_mask_in_MNI_dil_binv
fslmaths $subj_brain_mask_in_MNI -mas $L_mask_in_MNI_dil_binv $MNI_mask_min_lesion
# SyN warp the flipped brains to the respective MNI template
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T1_brain -m $T1_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_T1_brain_in_MNI_SyN_ -n ${ncpu}
antsRegistrationSyNQuick.sh -d 3 -f $MNI_T2_brain -m $T2_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_T2_brain_in_MNI_SyN_ -n ${ncpu}
antsRegistrationSyNQuick.sh -d 3 -f $MNI_FLAIR_brain -m $FLAIR_in_MNI_brain_flip -t s \
-x $MNI_brain_mask,$MNI_mask_min_lesion -o $preproc/sub-${subj}${ses_long}_flip_FLAIR_brain_in_MNI_SyN_ -n ${ncpu}
fi
echo " second part done " >> $wf_mark2
else
echo " second part already done, skipping. "
L_mask_in_T1=($(find ${preproc} -type f | grep _to_T1.nii.gz));
fi
# all workflows collapse to this same step at this point
T1_fake_left_hemi=${preproc}/T1_fake_left_hemi.nii.gz
T2_fake_left_hemi=${preproc}/T2_fake_left_hemi.nii.gz
FLAIR_fake_left_hemi=${preproc}/FLAIR_fake_left_hemi.nii.gz
T1_fake_right_hemi=${preproc}/T1_fake_right_hemi.nii.gz
T2_fake_right_hemi=${preproc}/T2_fake_right_hemi.nii.gz
FLAIR_fake_right_hemi=${preproc}/FLAIR_fake_right_hemi.nii.gz
T1_fake_left_hemi_mask=${preproc}/T1_fake_left_hemi_mask.nii.gz
T2_fake_left_hemi_mask=${preproc}/T2_fake_left_hemi_mask.nii.gz
FLAIR_fake_left_hemi_mask=${preproc}/FLAIR_fake_left_hemi_mask.nii.gz
T1_fake_right_hemi_mask=${preproc}/T1_fake_right_hemi_mask.nii.gz
T2_fake_right_hemi_mask=${preproc}/T2_fake_right_hemi_mask.nii.gz
FLAIR_fake_right_hemi_mask=${preproc}/FLAIR_fake_right_hemi_mask.nii.gz
T1_real_left_hemi=${preproc}/T1_real_left_hemi.nii.gz
T2_real_left_hemi=${preproc}/T2_real_left_hemi.nii.gz
FLAIR_real_left_hemi=${preproc}/FLAIR_real_left_hemi.nii.gz
T1_real_right_hemi=${preproc}/T1_real_right_hemi.nii.gz
T2_real_right_hemi=${preproc}/T2_real_right_hemi.nii.gz
FLAIR_real_right_hemi=${preproc}/FLAIR_real_right_hemi.nii.gz
T1_real_left_hemi_mask_min_lesion=${preproc}/T1_real_left_hemi_mask_min_lesion.nii.gz
T2_real_left_hemi_mask_min_lesion=${preproc}/T2_real_left_hemi_mask_min_lesion.nii.gz
FLAIR_real_left_hemi_mask_min_lesion=${preproc}/FLAIR_real_left_hemi_mask_min_lesion.nii.gz
T1_real_right_hemi_mask_min_lesion=${preproc}/T1_real_right_hemi_mask_min_lesion.nii.gz
T2_real_right_hemi_mask_min_lesion=${preproc}/T2_real_right_hemi_mask_min_lesion.nii.gz
FLAIR_real_right_hemi_mask_min_lesion=${preproc}/FLAIR_real_right_hemi_mask_min_lesion.nii.gz
T1_left_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_T1_left_fake_2_real_hemiWarped.nii.gz
T2_left_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_T2_left_fake_2_real_hemiWarped.nii.gz
FLAIR_left_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_FLAIR_left_fake_2_real_hemiWarped.nii.gz
T1_right_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_T1_right_fake_2_real_hemiWarped.nii.gz
T2_right_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_T2_right_fake_2_real_hemiWarped.nii.gz
FLAIR_right_fake_2_real_hemi_SyN=${preproc}/sub-${subj}${ses_long}_FLAIR_right_fake_2_real_hemiWarped.nii.gz
T1_lesion_fill=${preproc}/sub-${subj}${ses_long}_lesion_fill_T1.nii.gz
T2_lesion_fill=${preproc}/sub-${subj}${ses_long}_lesion_fill_T2.nii.gz
FLAIR_lesion_fill=${preproc}/sub-${subj}${ses_long}_lesion_fill_FLAIR.nii.gz
# determine lesion side
if [[ ! $search_wf_mark3 ]]; then
fslmaths $MNI_rl -thr 100 -uthr 100 -bin $MNI_left
fslmaths $MNI_left -mas $L_mask_in_MNI_dil $lesion_left_hemi_overlap
overlap_left="$(fslstats $lesion_left_hemi_overlap -V | head -c 1)"
fslmaths $MNI_rl -thr 1 -uthr 1 -bin $MNI_right
fslmaths $MNI_right -mas $L_mask_in_MNI_dil $lesion_right_hemi_overlap
overlap_right="$(fslstats $lesion_right_hemi_overlap -V | head -c 1)"
fslmaths $L_mask_in_T1 -binv $L_mask_binv_in_T1
fslmaths $MNI_brain_mask_in_nat -mas $L_mask_binv_in_T1 $MNI_brain_mask_in_nat_min_lesion
# generate lesion fill and inject it into images in MNI, the hemisphere we manipulate depends on lesion laterality
if [[ ! $overlap_left -eq 0 ]]; then
echo "it's a left sided lesion"
fslmaths $T1_in_MNI_SyN_brain_flip -mas $MNI_left $T1_fake_left_hemi
fslmaths $T1_fake_left_hemi -bin $T1_fake_left_hemi_mask
fslmaths $T1_in_MNI_brain -mas $MNI_left $T1_real_left_hemi
fslmaths $T1_real_left_hemi -bin -mas $L_mask_in_MNI_dil_binv $T1_real_left_hemi_mask_min_lesion
antsRegistrationSyN.sh -d 3 -f $T1_real_left_hemi -m $T1_fake_left_hemi -t s \
-x $T1_real_left_hemi_mask_min_lesion,$T1_fake_left_hemi_mask -o ${preproc}/sub-${subj}${ses_long}_T1_left_fake_2_real_hemi -n ${ncpu}
fslmaths $T1_left_fake_2_real_hemi_SyN -mas $L_mask_in_MNI_dil $T1_lesion_fill
fslmaths $T1_in_MNI -mas $L_mask_in_MNI_dil_binv -add $T1_lesion_fill $T1_with_lesion_fill_MNI
antsRegistrationSyN.sh -d 3 -f $T1_orig -m $T1_with_lesion_fill_MNI -t a \
-x $MNI_brain_mask_in_nat_min_lesion,$MNI_mask_min_lesion -o ${output}/sub-${subj}${ses_long}_T1nat_T1_sim_lesionless_ -n ${ncpu}
if [[ $wf -eq 1 ]] ; then
fslmaths $T2_in_MNI_SyN_brain_flip -mas $MNI_left $T2_fake_left_hemi
fslmaths $T2_fake_left_hemi -bin $T2_fake_left_hemi_mask
fslmaths $T2_in_MNI_brain -mas $MNI_left $T2_real_left_hemi
fslmaths $T2_real_left_hemi -bin -mas $L_mask_in_MNI_dil_binv $T2_real_left_hemi_mask_min_lesion
antsRegistrationSyN.sh -d 3 -f $T2_real_left_hemi -m $T2_fake_left_hemi -t s \
-x $T2_real_left_hemi_mask_min_lesion,$T2_fake_left_hemi_mask -o ${preproc}/sub-${subj}${ses_long}_T2_left_fake_2_real_hemi -n ${ncpu}
fslmaths $T2_left_fake_2_real_hemi_SyN -mas $L_mask_in_MNI_dil $T2_lesion_fill
fslmaths $T2_in_MNI -mas $L_mask_in_MNI_dil_binv -add $T2_lesion_fill $T2_with_lesion_fill_MNI
antsRegistrationSyN.sh -d 3 -f $T2_in_T1 -m $T2_with_lesion_fill_MNI -t a \
-x $MNI_brain_mask_in_nat_min_lesion,$MNI_mask_min_lesion -o ${output}/sub-${subj}${ses_long}_T1nat_T2_sim_lesionless_ -n ${ncpu}
elif [[ $wf -eq 2 ]] ; then
fslmaths $FLAIR_in_MNI_SyN_brain_flip -mas $MNI_left $FLAIR_fake_left_hemi
fslmaths $FLAIR_fake_left_hemi -bin $FLAIR_fake_left_hemi_mask
fslmaths $FLAIR_in_MNI_brain -mas $MNI_left $FLAIR_real_left_hemi
fslmaths $FLAIR_real_left_hemi -bin -mas $L_mask_in_MNI_dil_binv $FLAIR_real_left_hemi_mask_min_lesion
antsRegistrationSyN.sh -d 3 -f $FLAIR_real_left_hemi -m $FLAIR_fake_left_hemi -t s \
-x $FLAIR_real_left_hemi_mask_min_lesion,$FLAIR_fake_left_hemi_mask -o ${preproc}/sub-${subj}${ses_long}_FLAIR_left_fake_2_real_hemi -n ${ncpu}
fslmaths $FLAIR_left_fake_2_real_hemi_SyN -mas $L_mask_in_MNI_dil $FLAIR_lesion_fill
fslmaths $FLAIR_in_MNI -mas $L_mask_in_MNI_dil_binv -add $FLAIR_lesion_fill $FLAIR_with_lesion_fill_MNI
antsRegistrationSyN.sh -d 3 -f $FLAIR_in_T1 -m $FLAIR_with_lesion_fill_MNI -t a \
-x $MNI_brain_mask_in_nat_min_lesion,$MNI_mask_min_lesion -o ${output}/sub-${subj}${ses_long}_T1nat_FLAIR_sim_lesionless_ -n ${ncpu}
elif [[ $wf -eq 3 ]] ; then
fslmaths $T2_in_MNI_SyN_brain_flip -mas $MNI_left $T2_fake_left_hemi
fslmaths $T2_fake_left_hemi -bin $T2_fake_left_hemi_mask
fslmaths $T2_in_MNI_brain -mas $MNI_left $T2_real_left_hemi
fslmaths $T2_real_left_hemi -bin -mas $L_mask_in_MNI_dil_binv $T2_real_left_hemi_mask_min_lesion
antsRegistrationSyN.sh -d 3 -f $T2_real_left_hemi -m $T2_fake_left_hemi -t s \
-x $T2_real_left_hemi_mask_min_lesion,$T2_fake_left_hemi_mask -o ${preproc}/sub-${subj}${ses_long}_T2_left_fake_2_real_hemi -n ${ncpu}
fslmaths $T2_left_fake_2_real_hemi_SyN -mas $L_mask_in_MNI_dil $T2_lesion_fill
fslmaths $T2_in_MNI -mas $L_mask_in_MNI_dil_binv -add $T2_lesion_fill $T2_with_lesion_fill_MNI
antsRegistrationSyN.sh -d 3 -f $T2_in_T1 -m $T2_with_lesion_fill_MNI -t a \
-x $MNI_brain_mask_in_nat_min_lesion,$MNI_mask_min_lesion -o ${output}/sub-${subj}${ses_long}_T1nat_T2_sim_lesionless_ -n ${ncpu}
fslmaths $FLAIR_in_MNI_SyN_brain_flip -mas $MNI_left $FLAIR_fake_left_hemi
fslmaths $FLAIR_fake_left_hemi -bin $FLAIR_fake_left_hemi_mask
fslmaths $FLAIR_in_MNI_brain -mas $MNI_left $FLAIR_real_left_hemi
fslmaths $FLAIR_real_left_hemi -bin -mas $L_mask_in_MNI_dil_binv $FLAIR_real_left_hemi_mask_min_lesion
antsRegistrationSyN.sh -d 3 -f $FLAIR_real_left_hemi -m $FLAIR_fake_left_hemi -t s \