forked from dama-lab/multi-atlas-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMASHelperFunctions.sh
2906 lines (2610 loc) · 92.8 KB
/
MASHelperFunctions.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
source $HOME/.bashrc
##########################################################
# Multi-Atlas-Segmentation, parcellation, and label fusion
# To report any bugs/request, please contact: Da Ma ([email protected],[email protected])
##########################################################
# -------------------------------------------------
# define some global variables, (if not predefined)
# -------------------------------------------------
mas_script_path="$BASH_SOURCE"
mas_script_dir=${mas_script_path%/*}
mas_script_file="$(basename $mas_script_path)"
# Alternatively: mas_script_name=${mas_script_path##*/}
mas_script_name=$(echo $mas_script_file | cut -d. -f1 )
# define some default global variable value
AtlasListFileName=template_list.cfg
# define FSLDIR explicitely
# export FSLDIR="$HOME/Tools/fsl"
# source ${FSLDIR}/etc/fslconf/fsl.sh
echo """
=======================
[ $mas_script_name ]
mas_script_dir = $mas_script_dir
mas_script_file = $mas_script_file
[ Base functions ]
# - check_image_file
# - check_atlas_file
# - check_mapping_file
# - check_label_fusion_file
# - get_orientation
[ Single processing functions ]
# - mas_masking (prerequisite: NiftyReg)
# - mas_masking_fusion (prerequisite: NiftyReg)
# - mas_mapping (prerequisite: NiftyReg)
# - mas_fusion (prerequisite: NiftySeg)
# - mas_quickcheck (prerequisite: FSL)
# - mas_label_volume (prerequisite: NiftySeg)
# - mas_template_function (template functions for developer)
[ Batch processing functions ]:
# - mas_masking_batch
# - mas_mask_dilate_batch
# - mas_mapping_batch
# - mas_fusion_batch
# - mas_parcellation_batch
# - mas_quickcheck_batch
[ Pre-processing functions ]:
# - mas_fix_header_info
# - mas_smooth_batch
# - mas_N4_batch (prerequisite: ANT)
[ Post-processing functions ]:
# - mas_extract_label
# - mas_extract_label_batch
# - mas_extract_volume
# - mas_extract_volume_batch
# - mas_quickcheck_panorama
=======================
"""
# ---------------------------------
# function: get_orientation
# ---------------------------------
function get_orientation(){
local function_name=${FUNCNAME[0]}
if [ "$#" -lt 1 ]; then
echo "Get nifti file header"
echo "Usage: $function_name [input_img]"
return 1
else
local IMG=$1
fi
ORIENT=`mri_info $IMG | grep Orientation`
ORIENT=${ORIENT##*\ }
echo $ORIENT
}
function get_orientation_batch(){
local function_name=${FUNCNAME[0]}
if [ "$#" -lt 2 ]; then
echo "Get nifti file header"
echo "Usage: $function_name [input_dir] [targetlist]"
return 1
else
local input_dir=$1
local targetlist=$2
fi
echo "input_dir=$input_dir"
echo "targetlist=$targetlist"
local target_id
for target_id in $(cat $targetlist); do
echo $target_id $(get_orientation $input_dir/$target_id.nii.gz)
done
}
# ---------------------------------
# function: check_image_file
# ---------------------------------
function check_image_file(){
local exist_flag=0
local function_name=${FUNCNAME[0]}
if [[ $# -lt 1 ]]; then
echo "[$function_name] please specify image file to check the existence"
return 1
fi
local file_path=$1 # with or without image extension
local function_name=${FUNCNAME[0]}
# check existence with any valid extension
if [[ -f $file_path ]]; then
exist_flag=1
return 0
fi
ext_array=("" ".nii" ".nii.gz" ".img" ".hdr")
for ext in ${ext_array[@]}; do
if [[ -f $file_path$ext ]]; then
exist_flag=1
break
fi
done
# if find no file with any extension:
if [[ $exist_flag -eq 0 ]]; then
echo "[$function_name] cannot find file ($file_path)"
return 1
fi
return 0
}
# ---------------------------------
# function: check_atlas_file
# ---------------------------------
function check_atlas_file(){
local return_flag=0
local function_name=${FUNCNAME[0]}
if [[ $# -lt 2 ]]; then
echo "[$function_name] <atlas_dir> <atlas_id>"
return_flag=1
return $return_flag
fi
local atlas_dir=$1
local atlas_id=$2
local seg_type
for seg_type in template label mask;do
check_image_file "$atlas_dir/$seg_type/$atlas_id"
if [[ $? -ne 0 ]]; then
echo "[$function_name] ($atlas_id) atlas $seg_type missing "
return_flag=1
return $return_flag
fi
done
return $return_flag
}
# functions to be implemented and used for input/output file checking with accurate control
function check_mapping_input(){
local return_flag=0
local function_name=${FUNCNAME[0]}
}
function check_mapping_output(){
local return_flag=0
local function_name=${FUNCNAME[0]}
}
function check_fusion_input(){
local return_flag=0
local function_name=${FUNCNAME[0]}
}
function check_fusion_output(){
local return_flag=0
local function_name=${FUNCNAME[0]}
}
# ---------------------------------
# function: check_mapping_file
# ---------------------------------
function check_mapping_file(){
local return_flag=0
local function_name=${FUNCNAME[0]}
if [[ $# -lt 4 ]]; then
echo "[$function_name] <target_dir> <target_id> <atlas_dir> <atlas_list> <(Optional)result_dir> <(Optional)targetmask_dir> (Optional)targetmask_suffix>"
return_flag=1
return $return_flag
fi
local target_dir=$1
local target_id=$2
local atlas_dir=$3
local atlas_list=$4
local result_dir=$5
local targetmask_dir=$6
local targetmask_suffix=$7
local target_id
local atlas_name=$(basename $atlas_dir)
local atlas_id
# check if output file already exist
local mapped_file_all_exist=1
for atlas_id in $(cat $atlas_list);do
if [[ ! -f "$result_dir/label/$atlas_name/$target_id.label.$atlas_id.nii.gz" ]] || \
[[ ! -f "$result_dir/mask/$atlas_name/$target_id.mask.$atlas_id.nii.gz" ]] || \
[[ ! -f "$result_dir/mapping/$atlas_name/$atlas_id.$target_id.f3d.nii.gz" ]]; then
# found missing output files
mapped_file_all_exist=0
continue
fi
done
# if all file exist (no missing files found)
if [[ $mapped_file_all_exist -eq 1 ]]; then
echo "[$function_name] all mapping output files exist"
return_flag=2
return $return_flag
fi
# check if input target file missing
check_image_file $target_dir/$target_id
if [[ $? -ne 0 ]]; then
echo "[$function_name] cannot find target ($target_dir/$target_id)"
return_flag=3
return $return_flag
fi
# check if specified target mask file exist
if [[ ! -z $targetmask_dir ]]; then
local targetmask_name="$targetmask_dir/$target_id$targetmask_suffix"
check_image_file $targetmask_name
if [[ $? -ne 0 ]]; then
echo "[$function_name] cannot find target mask $targetmask_name"
return_flag=4
return $return_flag
fi
fi
return $return_flag
}
# ---------------------------------
# function: check_label_fusion_file
# ---------------------------------
function check_label_fusion_file(){
local return_flag=0
local function_name=${FUNCNAME[0]}
if [[ $# -lt 5 ]]; then
echo "[$function_name] <target_dir> <target_id> <atlas_name> <atlas_list> <result_dir>"
return_flag=1
return $return_flag
fi
local target_dir=$1
local target_id=$2
local atlas_name=$3
local atlas_list=$4
local result_dir=$5
# check output file
if [[ -f $result_dir/label/$target_id.label.$atlas_name.nii.gz ]]; then
echo "[$function_name] fusion output exist for target: $target_id, skipping ..."
return_flag=2
return $return_flag
fi
# check if input target file missing
check_image_file $target_dir/$target_id
local target_exist=$?
if [[ $target_exist -ne 0 ]]; then
echo "[$function_name] cannot find target ($target_dir/$target_id)"
return_flag=3
return $return_flag
fi
# check if the input 4D file has already been created
local seg_file
local seg_file_flag=1
for seg_file in mapping label; do
local merged_4d_file="$result_dir/$seg_file/$atlas_name/$target_id.4D.nii.gz"
if [[ ! -f $merged_4d_file ]]; then
seg_file_flag=0
break
fi
done
if [[ $seg_file_flag -eq 1 ]]; then
echo "[$function_name] ($target_id) both 4D mapping and label file, ready for fusion ..."
return_flag=0
return $return_flag
fi
# check if input mapped atlas file missing
local atlas_flag=0
local atlas_seg_flag=0
local atlas_id
for atlas_id in $(cat $atlas_list);do
if [[ ! -f "$result_dir/mapping/$atlas_name/$atlas_id.$target_id.f3d.nii.gz" ]]; then
# echo "[$function_name] ($target_id) Mapping file for atlas ($atlas_id) not pre-exist"
#location: $result_dir/mapping/$atlas_name/$atlas_id.$target_id.f3d.nii.gz"
atlas_flag=1
break
fi
local seg_file
for seg_file in label; do # no need to check mapped mask
if [[ ! -f "$result_dir/$seg_file/$atlas_name/$target_id.$seg_file.$atlas_id.nii.gz" ]]; then
echo "[$function_name] ($target_id) Cannot find mapped $seg_file of atlas ($atlas_id): $result_dir/$seg_file/$atlas_name/$target_id.$seg_file.$atlas_id.nii.gz"
atlas_seg_flag=1
break 2
fi
done
done
# skip current $target_id if input file missing
if [[ $atlas_flag -eq 1 ]]; then
# echo "[$function_name] cannot fine input mapped atlas template file"
return_flag=4
return $return_flag
elif [[ $atlas_seg_flag -eq 1 ]]; then
# echo "[$function_name] cannot fine input mapped $seg_file file"
return_flag=5
return $return_flag
fi
return $return_flag
}
# ------------------------------
# function: lable2mask
# ------------------------------
function label2masks(){
local function_name=${FUNCNAME[0]}
usage() {
echo """binarize label 2 mask
[Usage]: $function_name \$label_dir \$mask_dir [Optional]
"""
}
if [[ $# -lt 2 ]]; then
usage; return 1;
fi
local label_dir=$1
local mask_dir=$2
mkdir -p $mask_dir
for id in $(ls $label_dir); do
echo "create binarized mask for: $id"
seg_maths $label_dir/$id -bin $mask_dir/$id
done
}
# ------------------------------
# function: dilate_mask
# ------------------------------
function dilate_masks(){
local function_name=${FUNCNAME[0]}
usage() {
echo """dilate mask by [dil]
[Usage]: $function_name \$mask_dir \$mask_dil_dir \$dil
"""
}
if [[ $# -lt 3 ]]; then
usage; return 1;
fi
local mask_dir=$(readlink -f $1)
local mask_dil_dir=$(readlink -f $2)
local dil=$3
mkdir -p $mask_dil_dir
for id in $(ls $mask_dir); do
echo "dilate [$dil] for mask: $id"
seg_maths $mask_dir/$id -dil $dil $mask_dil_dir/$id
done
}
# ------------------------------
# function: mas_label_volume
# ------------------------------
function mas_label_volume(){
local function_name=${FUNCNAME[0]}
usage() {
echo """
Multi Atlas Segmentation - Part 4: volume extraction
[$function_name] [-l target_list] [-s seg_dir] [-v vol_csv (file path)]
(optional file name suffix) [-t seg_type] [-a atlas_name]
example usage 1: To extract the label volume for the parcellated results:
mas_label_volume -l target_list -s result_dir/\"label\" -v vol_csv -t \"label\" -a atlas_name
example usage 2: To extract the mask volume for the parcellated results:
mas_label_volume -l target_list -s result_dir/\"mask\" -v vol_csv -t \"mask\" -a atlas_name
"""
return 1
}
local OPTIND
local options
# print a seperate line
echo ""
while getopts ":l:a:s:t:v:c:h" options; do
case $options in
l ) echo "Target list: $OPTARG"
local target_list=$OPTARG;;
a ) echo "Atlas name: $OPTARG"
local atlas_name=$OPTARG;;
s ) echo "Result directory: $OPTARG"
local seg_dir=$OPTARG;;
t ) echo "Segmentation type: $OPTARG"
local seg_type=$OPTARG;;
v ) echo "Volume file: $OPTARG"
local vol_csv=$OPTARG;;
c ) echo "Cleanup flag: $OPTARG"
local cleanup_flag=$OPTARG;;
\?) echo "Unknown option"
usage; return 1;;
h ) usage; return 1;;
: ) usage; return 1;;
esac
done
if [[ $OPTIND -eq 1 ]]; then
echo "[$function_name] no option specified"
usage; return 1
fi
echo "[$function_name] begin volume extraction... "
local error_flag=0
local target_id
local seg_file
# check existance of $target_list
if [[ ! -f $target_list ]]; then
echo "[$function_name] cannot find target_list file: $starget_list"
error_flag=1
return $error_flag
fi
# check existance of $target_id
for target_id in $(cat $target_list); do
seg_file="$seg_dir/$target_id"
if [[ ! -z $seg_type ]]; then
seg_file="$seg_file.$seg_type"
if [[ ! -z $atlas_name ]]; then
seg_file="$seg_file.$atlas_name"
fi
fi
check_image_file $seg_file
if [[ $? -ne 0 ]]; then
echo "[$function_name] cannot find $seg_type file: $seg_file"
error_flag=1
return $error_flag
fi
done
# In case output file exist, warn user before overwrite
if [[ -f $vol_csv ]]; then
echo "Output file: <$vol_csv> exist, overwrite?"
local yn
select yn in "select 1 for Yes" "select 2 for No"; do
case $yn in
"select 1 for Yes" ) rm -f $vol_csv; break;;
"select 2 for No" ) echo "no overwrite, exiting ..."; return 1;;
esac
done
fi
local tmp_dir="$seg_dir/tmp_$RANDOM"
mkdir -p $tmp_dir
for target_id in $(cat $target_list); do
# input image file
seg_file="$seg_dir/$target_id"
if [[ ! -z $seg_type ]]; then
seg_file="$seg_file.$seg_type"
if [[ ! -z $atlas_name ]]; then
seg_file="$seg_file.$atlas_name"
fi
fi
# output csv file
local target_csv="$tmp_dir/$target_id.$seg_type.$atlas_name.csv"
seg_stats $seg_file -Vl $target_csv
# exclude the first colume, which is the background volume
echo "$target_id,$(cat $target_csv | cut -d, -f2-)" >> $vol_csv
done
# remove tmp
rm -rf $tmp_dir
}
# -------------------------
# function mas_quickcheck
# reference:
# [1] Generating Quality Assurance Images using FSL overlay and slicer
# https://faculty.washington.edu/madhyt/2016/12/10/180/
# -------------------------
function mas_quickcheck(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "Multi Atlas Segmentation - Part 3: quickcheck generation"
echo "Usage: $function_name [bg_img] [(optional) overlay_img] [qc_dir] [qc_filename]"
return 1
fi
local bg_img=$1
local overlay_img=$2
local qc_dir=$3
local qc_filename=$4
echo "bg_img = $bg_img"
echo "overlay_img = $overlay_img"
echo "qc_dir = $qc_dir"
echo "qc_filename = $qc_filename"
# check if FSL is installed (by checking variable $FSLDIR)
if [[ -z $FSLDIR ]]; then
echo "[$function_name] variable \$FSLDIR not set, cannot determine FSL installed location, exitng ..."
return 1
fi
local tmp_dir=$qc_dir/tmp_$RANDOM
mkdir -p $tmp_dir
local bg_name=$(basename $bg_img | cut -d. -f1)
local slicer_cmd
if [[ ! -z $overlay_img ]]; then
local overlay_name=$(basename $overlay_img | cut -d. -f1)
local overlay_nan=$tmp_dir/masknan.$bg_name.$overlay_name.nii.gz
local overlay_tmp=$tmp_dir/overlay.$bg_name.$overlay_name.nii.gz
# determine label range
seg_maths $overlay_img -masknan $overlay_img $overlay_nan
local label_range=$(seg_stats $overlay_nan -r)
echo "label_range = $label_range"
# generate overlay nifti file using FSL's overlay
overlay 1 0 $bg_img -a $overlay_nan $label_range $overlay_tmp
# local label_min=$(( $(echo $label_range | cut -d' ' -f1) +1 ))
# local label_max=$(echo $label_range | cut -d' ' -f2)
# echo "label_range = $label_min $label_max"
# # generate overlay nifti file using FSL's overlay
# overlay 1 0 $bg_img -a $overlay_img $label_min $label_max $overlay_tmp
# specify overlay transparency and colormap
local LUT_file="$FSLDIR/etc/luts/renderjet.lut"
if [[ ! -f $LUT_file ]]; then
echo "[$function_name] cannot find color Look-up-table file: $LUT_file"
return 1
fi
slicer_cmd="slicer -t -n -l $LUT_file"
else # no overlay_img
local overlay_tmp=$tmp_dir/$bg_name.nii.gz
ln -s $bg_img $overlay_tmp
slicer_cmd="slicer"
fi
# create png
slicer_cmd="$slicer_cmd $overlay_tmp \
-x 0.10 $tmp_dir/x_11.png -x 0.20 $tmp_dir/x_12.png -x 0.30 $tmp_dir/x_13.png \
-x 0.34 $tmp_dir/x_21.png -x 0.38 $tmp_dir/x_22.png -x 0.42 $tmp_dir/x_23.png \
-x 0.46 $tmp_dir/x_31.png -x 0.50 $tmp_dir/x_32.png -x 0.54 $tmp_dir/x_33.png \
-x 0.58 $tmp_dir/x_41.png -x 0.62 $tmp_dir/x_42.png -x 0.66 $tmp_dir/x_43.png \
-x 0.70 $tmp_dir/x_51.png -x 0.74 $tmp_dir/x_52.png -x 0.78 $tmp_dir/x_53.png \
-x 0.82 $tmp_dir/x_61.png -x 0.86 $tmp_dir/x_62.png -x 0.90 $tmp_dir/x_63.png \
-y 0.15 $tmp_dir/y_11.png -y 0.25 $tmp_dir/y_12.png -y 0.34 $tmp_dir/y_13.png -y 0.42 $tmp_dir/y_14.png \
-y 0.50 $tmp_dir/y_21.png -y 0.55 $tmp_dir/y_22.png -y 0.62 $tmp_dir/y_23.png -y 0.65 $tmp_dir/y_24.png \
-y 0.70 $tmp_dir/y_31.png -y 0.73 $tmp_dir/y_32.png -y 0.78 $tmp_dir/y_33.png -y 0.82 $tmp_dir/y_34.png \
-z 0.15 $tmp_dir/z_11.png -z 0.25 $tmp_dir/z_12.png -z 0.34 $tmp_dir/z_13.png -z 0.42 $tmp_dir/z_14.png \
-z 0.50 $tmp_dir/z_21.png -z 0.55 $tmp_dir/z_22.png -z 0.62 $tmp_dir/z_23.png -z 0.65 $tmp_dir/z_24.png \
-z 0.70 $tmp_dir/z_31.png -z 0.73 $tmp_dir/z_32.png -z 0.78 $tmp_dir/z_33.png -z 0.82 $tmp_dir/z_34.png "
eval $slicer_cmd
# append png files -x
pngappend $tmp_dir/x_11.png + 2 $tmp_dir/x_12.png + 2 $tmp_dir/x_13.png $tmp_dir/x_1.png
pngappend $tmp_dir/x_21.png + 2 $tmp_dir/x_22.png + 2 $tmp_dir/x_23.png $tmp_dir/x_2.png
pngappend $tmp_dir/x_31.png + 2 $tmp_dir/x_32.png + 2 $tmp_dir/x_33.png $tmp_dir/x_3.png
pngappend $tmp_dir/x_41.png + 2 $tmp_dir/x_42.png + 2 $tmp_dir/x_43.png $tmp_dir/x_4.png
pngappend $tmp_dir/x_51.png + 2 $tmp_dir/x_52.png + 2 $tmp_dir/x_53.png $tmp_dir/x_5.png
pngappend $tmp_dir/x_61.png + 2 $tmp_dir/x_62.png + 2 $tmp_dir/x_63.png $tmp_dir/x_6.png
pngappend $tmp_dir/x_1.png + 1 $tmp_dir/x_2.png $tmp_dir/x1.png
pngappend $tmp_dir/x_3.png + 1 $tmp_dir/x_4.png $tmp_dir/x2.png
pngappend $tmp_dir/x_5.png + 1 $tmp_dir/x_6.png $tmp_dir/x3.png
pngappend $tmp_dir/x1.png - 2 $tmp_dir/x2.png - 2 $tmp_dir/x3.png $tmp_dir/x.png
# append png files -y
pngappend $tmp_dir/y_11.png + 2 $tmp_dir/y_12.png + 2 $tmp_dir/y_13.png + 2 $tmp_dir/y_14.png $tmp_dir/y_1.png
pngappend $tmp_dir/y_21.png + 2 $tmp_dir/y_22.png + 2 $tmp_dir/y_23.png + 2 $tmp_dir/y_24.png $tmp_dir/y_2.png
pngappend $tmp_dir/y_31.png + 2 $tmp_dir/y_32.png + 2 $tmp_dir/y_33.png + 2 $tmp_dir/y_34.png $tmp_dir/y_3.png
pngappend $tmp_dir/y_1.png - 2 $tmp_dir/y_2.png - 2 $tmp_dir/y_3.png $tmp_dir/y.png
# append png files -z
pngappend $tmp_dir/z_11.png + 2 $tmp_dir/z_12.png + 2 $tmp_dir/z_13.png + 2 $tmp_dir/z_14.png $tmp_dir/z_1.png
pngappend $tmp_dir/z_21.png + 2 $tmp_dir/z_22.png + 2 $tmp_dir/z_23.png + 2 $tmp_dir/z_24.png $tmp_dir/z_2.png
pngappend $tmp_dir/z_31.png + 2 $tmp_dir/z_32.png + 2 $tmp_dir/z_33.png + 2 $tmp_dir/z_34.png $tmp_dir/z_3.png
pngappend $tmp_dir/z_1.png - 2 $tmp_dir/z_2.png - 2 $tmp_dir/z_3.png $tmp_dir/z.png
# append png files -xyz
pngappend $tmp_dir/x.png - 2 $tmp_dir/y.png + 2 $tmp_dir/z.png $qc_dir/$qc_filename.png
rm -rf $tmp_dir
# To display the output image, type
# xdg-open foo.png
}
#-----------------------------------
# function: mas_masking
# ----------------------------------
function mas_masking(){
local function_name=${FUNCNAME[0]}
usage() {
echo ""
echo "Multi Atlas Segmentation - Preprocssing: Brain masking (brain extraction). Only affine transformation is used"
echo "Usage: $function_name [-T target_dir] [-t target_id] [-A atlas_dir] [-a atlas_id] [-r result_dir] [-p parameter_cfg]"
echo ""
return 1
}
local OPTIND
local options
while getopts ":T:t:A:a:f:r:p:c:h" options; do
case $options in
T ) echo "Target directory: $OPTARG"
local target_dir=$OPTARG;;
t ) echo "Target ID: $OPTARG"
local target_id=$OPTARG;;
A ) echo "Atlas directory: $OPTARG"
local atlas_dir=$OPTARG;;
a ) echo "Atlas ID: $OPTARG"
local atlas_id=$OPTARG;;
f ) echo "Affine parameter: $OPTARG"
local affine_param=$OPTARG;;
r ) echo "Result directory: $OPTARG"
local result_dir=$OPTARG;;
p ) echo "Parameter config file: $OPTARG"
local parameter_cfg=$OPTARG;;
c ) echo "Cleanup flag: $OPTARG"
local cleanup_flag=$OPTARG;;
h ) usage; return 1;;
\?) echo "Unknown option"
usage; return 1;;
: ) usage; return 1;;
esac
done
if [[ $OPTIND -eq 1 ]]; then
echo "[$function_name] no option specified"
usage; return 1
fi
# preload parameter_cfg file
if [[ ! -z $parameter_cfg ]]; then
source $parameter_cfg
fi
echo "[$function_name] begin mapping template $atlas_id to $target_id"
local error_flag=0
# check mapping input/output file existance
# check_mapping_file $target_dir $target_id $atlas_dir $atlas_list $result_dir
# local file_tag=$?
# if [[ $file_tag -eq 2 ]]; then
# echo "[$function_name] Mapping file already exist: $target_dir/$target_id "
# return 0
# elif [[ $file_tag -ne 0 ]]; then
# echo "[$function_name] cannot find target ($target_id)"
# return 1
# fi
# checking atlas_file existance
check_atlas_file $atlas_dir $atlas_id
if [[ $? -ne 0 ]]; then
echo "[$function_name] cannot find atlas $atlas_file_type: $atlas_dir atlas_id: atlas_id"
return 1
fi
# eliminate .ext from id
# local target_id=$(echo $target_id | cut -d. -f1) # to avoid cases where there are dots ('.') in the filename
local atlas_id=$(echo $atlas_id | cut -d. -f1)
# creat folders
local atlas_name=$(basename $atlas_dir)
for subdir in tmp mapping mask label quickcheck; do
local subdir_path="$result_dir/$subdir/$atlas_name"
mkdir -p $subdir_path
done
local mapping_dir="$result_dir/mapping/$atlas_name"
local mask_dir="$result_dir/mask/$atlas_name"
local label_dir="$result_dir/label/$atlas_name"
local tmp_dir="$result_dir/tmp/$atlas_name"
mkdir -p $mapping_dir
mkdir -p $mask_dir
mkdir -p $label_dir
mkdir -p $tmp_dir
# generate affine matrix (atlas >> target), if no affine matrix found
if [[ -f $tmp_dir/$atlas_id.$target_id.aff ]]; then
echo "[$function_name] affine matrix already exist: $tmp_dir/$atlas_id.$target_id.aff, skipping affine step ..."
else
if [[ -z $affine_param ]]; then
local affine_param="" # "-rigOnly"
fi
affine_param="$affine_param -speeeeed" # -ln 4 -lp 4
affine_param="$affine_param -flo $atlas_dir/template/$atlas_id"
affine_param="$affine_param -fmask $atlas_dir/mask/$atlas_id"
affine_param="$affine_param -ref $target_dir/$target_id"
affine_param="$affine_param -res $tmp_dir/$atlas_id.$target_id.aff.nii.gz"
affine_param="$affine_param -aff $tmp_dir/$atlas_id.$target_id.aff"
reg_aladin $affine_param
fi
# check if affine matrix file generated successfully
if [[ ! -f $tmp_dir/$atlas_id.$target_id.aff ]]; then
echo "[$function_name] failed to generate affine matrix file"
return 1
fi
# resample the label, as well as mask
local seg_file
for seg_file in mask; do
local result_file="$result_dir/$seg_file/$atlas_name/$target_id.$seg_file.$atlas_id.affine.nii.gz"
if [[ -f $result_file ]]; then
echo "[$function_name] ($target_id) $seg_file file already exist: $result_file, skipping ..."
continue
fi
local resamp_param=""
resamp_param="$resamp_param -flo $atlas_dir/$seg_file/$atlas_id"
resamp_param="$resamp_param -ref $target_dir/$target_id"
resamp_param="$resamp_param -trans $tmp_dir/$atlas_id.$target_id.aff"
resamp_param="$resamp_param -inter 0"
resamp_param="$resamp_param -res $result_file"
reg_resample $resamp_param
# generating quickcheck for mask and label
mas_quickcheck $target_dir/$target_id $result_file $result_dir/quickcheck/$atlas_name $target_id.$seg_file.affine.$atlas_id
done
# remove tmp files
if [[ $cleanup_flag -eq 1 ]]; then
rm -rf $tmp_dir
error_flag=$?
fi
# unset parameters
unset affine_param
return $error_flag
}
#-----------------------------------
# function: mas_mapping
# ----------------------------------
function mas_mapping(){
local function_name=${FUNCNAME[0]}
usage() {
echo ""
echo "Multi Atlas Segmentation - Part 1: Mapping"
echo "Usage: $function_name [-T target_dir] [-t target_id] [-m target_mask] [-A atlas_dir] [-a atlas_id] [-r result_dir] [-p parameter_cfg]"
echo ""
return 1
}
local OPTIND
local options
while getopts ":T:t:m:A:a:r:p:c:h" options; do
case $options in
T ) echo "Target directory: $OPTARG"
local target_dir=$OPTARG;;
t ) echo "Target ID: $OPTARG"
local target_id=$OPTARG;;
m ) echo "Target mask: $OPTARG"
local target_mask=$OPTARG;;
A ) echo "Atlas directory: $OPTARG"
local atlas_dir=$OPTARG;;
a ) echo "Atlas ID: $OPTARG"
local atlas_id=$OPTARG;;
r ) echo "Result directory: $OPTARG"
local result_dir=$OPTARG;;
p ) echo "Parameter config file: $OPTARG"
local parameter_cfg=$OPTARG;;
c ) echo "Cleanup flag: $OPTARG"
local cleanup_flag=$OPTARG;;
h ) usage; return 1;;
\?) echo "Unknown option"
usage; return 1;;
: ) usage; return 1;;
esac
done
if [[ $OPTIND -eq 1 ]]; then
echo "[$function_name] no option specified"
usage; return 1
fi
# preload parameter_cfg file
if [[ ! -z $parameter_cfg ]]; then
source $parameter_cfg
fi
echo "[mas_mapping] begin mapping template $atlas_id to $target_id"
local error_flag=0
# check mapping input/output file existance
# check_mapping_file $target_dir $target_id $atlas_dir $atlas_list $result_dir
# local file_tag=$?
# if [[ $file_tag -eq 2 ]]; then
# echo "[mas_mapping] Mapping file already exist: $target_dir/$target_id "
# return 0
# elif [[ $file_tag -ne 0 ]]; then
# echo "[mas_mapping] cannot find target ($target_id)"
# return 1
# fi
# check target_mask existance
if [[ ! -z $target_mask ]]; then
check_image_file $target_mask
if [[ $? -ne 0 ]]; then
echo "[mas_mapping] cannot find target mask for ($target_id): $target_mask"
return 1
fi
# else # need to create target_mask first
# mas_masking -T $target_dir -t $target_id -A $atlas_dir -a $atlas_id -r $result_dir -p $parameter_cfg
# local target_mask = $result_dir/mask/$atlas_name/$target_id.mask.$atlas_id.affine.nii.gz
fi
# checking atlas_file existance
check_atlas_file $atlas_dir $atlas_id
if [[ $? -ne 0 ]]; then
echo "[mas_mapping] cannot find atlas $atlas_file_type: $atlas_dir atlas_id: $atlas_id"
return 1
fi
# eliminate .ext from id
# local target_id=$(echo $target_id | cut -d. -f1)
local atlas_id=$(echo $atlas_id | cut -d. -f1)
# creat folders
local atlas_name=$(basename $atlas_dir)
for subdir in tmp mapping mask label quickcheck; do
local subdir_path="$result_dir/$subdir/$atlas_name"
mkdir -p $subdir_path
done
local mapping_dir="$result_dir/mapping/$atlas_name"
local mask_dir="$result_dir/mask/$atlas_name"
local label_dir="$result_dir/label/$atlas_name"
local tmp_dir="$result_dir/tmp/$atlas_name"
mkdir -p $mapping_dir
mkdir -p $mask_dir
mkdir -p $label_dir
mkdir -p $tmp_dir
# generate affine matrix (atlas >> target), if no affine matrix found
if [[ -f $tmp_dir/$atlas_id.$target_id.aff ]]; then
echo "[$function_name] affine matrix already exist: $tmp_dir/$atlas_id.$target_id.aff, skipping affine step ..."
else
# load affine_param from parameter_cfg if specified
if [[ -z $affine_param ]]; then
local affine_param="" # "-rigOnly"
fi
affine_param="$affine_param -ln 4 -lp 4 -speeeeed" #
affine_param="$affine_param -flo $atlas_dir/template/$atlas_id"
affine_param="$affine_param -fmask $atlas_dir/mask/$atlas_id"
affine_param="$affine_param -ref $target_dir/$target_id"
affine_param="$affine_param -res $tmp_dir/$atlas_id.$target_id.aff.nii.gz"
affine_param="$affine_param -aff $tmp_dir/$atlas_id.$target_id.aff"
# use target mask if specified
if [[ ! -z $target_mask ]]; then
affine_param="$affine_param -rmask $target_mask -cog"
else
affine_param="$affine_param -rmask $target_mask -nac"
fi
reg_aladin $affine_param
fi
# generate nrr cpp (atlas >> target), if no cpp found
if [[ -f $tmp_dir/$atlas_id.$target_id.cpp.nii.gz ]]; then
echo "[$function_name] non-rigid control point already exist: $tmp_dir/$atlas_id.$target_id.cpp.nii.gz, skipping non-rigid step ..."
else
# check if affine matrix successfully generated
if [[ ! -f $tmp_dir/$atlas_id.$target_id.aff ]]; then
echo "[mas_mapping] failed to generate affine matrix"
return 1
fi
# load nrr_param from parameter_cfg if specified
if [[ -z $nrr_param ]]; then
local nrr_param="$nrr_param -vel -nogce -vel -smooF 0.04 -smooR 0.04 -ln 4 -lp 4" # -smooR 0.04 -ln 4 -lp 4
fi
nrr_param="$nrr_param -flo $atlas_dir/template/$atlas_id"
nrr_param="$nrr_param -fmask $atlas_dir/mask/$atlas_id"
nrr_param="$nrr_param -ref $target_dir/$target_id"
# use affine transform matrix to initialize non-rigid registration
nrr_param="$nrr_param -aff $tmp_dir/$atlas_id.$target_id.aff"
nrr_param="$nrr_param -res $mapping_dir/$atlas_id.$target_id.f3d.nii.gz"
nrr_param="$nrr_param -cpp $tmp_dir/$atlas_id.$target_id.cpp.nii.gz"
if [[ ! -z $target_mask ]] ; then
# use target mask if specified and exist
nrr_param="$nrr_param -rmask $target_mask"
fi
reg_f3d $nrr_param
fi
# check if cpp file generated successfully
if [[ ! -f $tmp_dir/$atlas_id.$target_id.cpp.nii.gz ]]; then
echo "[mas_mapping] failed to generate control point cpp file"
return 1
fi
# resample the label, as well as mask
local seg_file
for seg_file in label mask; do
local result_file="$result_dir/$seg_file/$atlas_name/$target_id.$seg_file.$atlas_id.nii.gz"
if [[ -f $result_file ]]; then
echo "[$function_name] ($target_id) $seg_file file already exist: $result_file, skipping ..."
continue
fi
local resamp_param=""
resamp_param="$resamp_param -flo $atlas_dir/$seg_file/$atlas_id"
resamp_param="$resamp_param -ref $target_dir/$target_id"
resamp_param="$resamp_param -cpp $tmp_dir/$atlas_id.$target_id.cpp.nii.gz"
resamp_param="$resamp_param -inter 0"
resamp_param="$resamp_param -res $result_file"
reg_resample $resamp_param
# generating quickcheck for mask and label
mas_quickcheck $target_dir/$target_id $result_file $result_dir/quickcheck/$atlas_name $target_id.$seg_file.$atlas_id
done
# remove tmp files
if [[ $cleanup_flag -eq 1 ]]; then
rm -rf $tmp_dir
error_flag=$?
fi
return $error_flag
}
#-----------------------------------
# function: mas_fusion
# ----------------------------------
function mas_fusion(){
local function_name=${FUNCNAME[0]}
usage() {
echo ""
echo "Multi Atlas Segmentation - Part 2: Fusion"
echo "Usage: $function_name [-T target_dir] [-t target_id] [-A atlas_name] [-a atlas_list] [-r result_dir]"
echo " (optional) [-m target_mask] [-p parameter_cfg] [-c cleanup_flag]"
echo ""
return 1
}
local OPTIND
local options
while getopts ":T:t:m:A:a:r:p:c:h" options; do
case $options in
T ) echo "Target directory: $OPTARG"
local target_dir=$OPTARG;;
t ) echo "Target ID: $OPTARG"
local target_id=$OPTARG;;
m ) echo "Target mask: $OPTARG"
local target_mask=$OPTARG;;
A ) echo "Atlas name: $OPTARG"
local atlas_name=$OPTARG;;
a ) echo "Atlas list: $OPTARG"
local atlas_list=$OPTARG;;
r ) echo "Result directory: $OPTARG"
local result_dir=$OPTARG;;
p ) echo "Parameter config file: $OPTARG"
local parameter_cfg=$OPTARG;;
c ) echo "Cleanup flag: $OPTARG"
local cleanup_flag=$OPTARG;;
h ) usage; return 1;;
\?) echo "Unknown option"
usage; return 1;;
: ) usage; return 1;;
esac
done
if [[ $OPTIND -eq 1 ]]; then
echo "[$function_name] no option specified"
usage; return 1
fi
# local target_id=$(echo $target_id | cut -d. -f1)
local mapping_dir="$result_dir/mapping/$atlas_name"
local mask_dir="$result_dir/mask/$atlas_name"