-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathantsJointLabelFusion.sh
executable file
·1250 lines (1025 loc) · 43.6 KB
/
antsJointLabelFusion.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
VERSION="0.0.0"
# trap keyboard interrupt (control-c)
trap control_c SIGINT
# Test availability of helper scripts.
# No need to test this more than once. Can reside outside of the main loop.
ANTS=antsRegistration
WARP=antsApplyTransforms
JLF=antsJointFusion
PEXEC=ANTSpexec.sh
SGE=waitForSGEQJobs.pl
PBS=waitForPBSQJobs.pl
XGRID=waitForXGridJobs.pl
SLURM=waitForSlurmJobs.pl
fle_error=0
for FLE in $JLF $ANTS $WARP $PEXEC $SGE $XGRID $PBS $SLURM
do
if ! command -v $FLE &> /dev/null
then
echo
echo "--------------------------------------------------------------------------------------"
echo " FILE $FLE DOES NOT EXIST -- OR -- IS NOT EXECUTABLE !!! $0 will terminate."
echo "--------------------------------------------------------------------------------------"
echo " if the file is not executable, please change its permissions. "
fle_error=1
fi
done
if [[ $fle_error = 1 ]];
then
echo "missing helper script"
exit 1
fi
#assuming .nii.gz as default file type. This is the case for ANTS 1.7 and up
# Initialize variables with defaults, referred to in usage
DIM=3
KEEP_ALL_IMAGES=0
DOQSUB=0
CORES=2
PRECISION=1
XGRID_OPTS=""
SCRIPT_PREPEND=""
QSUB_OPTS=""
TARGET_MASK_IMAGE="majorityvoting"
REGISTRATION_WALLTIME="20:00:00"
REGISTRATION_MEMORY="8gb"
JLF_WALLTIME="20:00:00"
JLF_MEMORY="8gb"
MAJORITYVOTE=0
RUNQUICK=1
TRANSFORM_TYPE="s"
function Usage {
cat <<USAGE
Usage:
`basename $0` -d ImageDimension -o OutputPrefix <other options> <images>
Compulsory arguments (minimal command line requires SGE cluster, otherwise use -c & -j options):
-d: ImageDimension: 2 or 3.
-o: OutputPrefix: A prefix that is prepended to all output files.
-t: TargetImage: Target image to be labeled.
-g: Atlas: Atlas to be warped to target image.
-l: Labels: Labels corresponding to atlas (cf -g).
Optional arguments:
-m: Majority vote: Use majority vote instead of joint label fusion (default = ${MAJORITYVOTE}).
-k: Keep files: Keep warped atlas and label files (default = ${KEEP_ALL_IMAGES}).
-c: Control for parallel computation (default 0) -- 0 == run serially, 1 == SGE qsub,
2 == use PEXEC (localhost), 3 == Apple XGrid, 4 == PBS qsub, 5 == SLURM.
-j: Number of cpu cores to use (default ${CORES}; -- requires "-c 2").
-r: qsub options
-q: Use quick registration parameters: Either 0 or 1 (default = ${RUNQUICK}).
-p: Save posteriors: Save posteriors in specified c-style format e.g. posterior%04d.nii.gz
Need to specify output directory.
-f: Float precision: Use float precision (default = ${PRECISION}) -- 0 == double, 1 == float.
-u: Registration walltime (default = ${REGISTRATION_WALLTIME}): Option for PBS/SLURM qsub specifying requested time
per pairwise registration.
-v: Registration memory limit (default = ${REGISTRATION_MEMORY}): Option for PBS/SLURM qsub specifying requested memory
per pairwise registration.
-w: JLF walltime (default = ${JLF_WALLTIME}): Option for PBS/SLURM qsub specifying requested time
for the joint label fusion call.
-z: JLF Memory limit (default = ${JLF_MEMORY}): Option for PBS/SLURM qsub specifying requested memory
for the joint label fusion call.
-y: transform type (default = \'${TRANSFORM_TYPE}\')
t: translation
r: rigid
a: rigid + affine
s: rigid + affine + deformable syn
sr: rigid + deformable syn
so: deformable syn only
b: rigid + affine + deformable b-spline syn
br: rigid + deformable b-spline syn
bo: deformable b-spline syn only
-x: Target mask image (default = \'${TARGET_MASK_IMAGE}\')
otsu: use otsu thresholding to define foreground/background
or: 'or' all the warped atlas images to defined foreground/background
majorityvoting: perform a voxelwise label voting. If >= 80% of the warped atlases agree at that
voxel, we keep that voted label at that voxel and *do not* perform JLF. Note that
the 80% threshold is hard-coded but can be easily changed in the script.
<filename>: a user-specified mask
none: don't use a mask
Example:
`basename $0` -d 3 -t target.nii.gz -o malf \\
-p malfPosteriors%04d.nii.gz \\
-g atlas1.nii.gz -l labels1.nii.gz \\
-g atlas2.nii.gz -l labels2.nii.gz \\
-g atlas3.nii.gz -l labels3.nii.gz
--------------------------------------------------------------------------------------
JLF was created by:
--------------------------------------------------------------------------------------
Hongzhi Wang and Paul Yushkevich
Penn Image Computing And Science Laboratory
University of Pennsylvania
Please reference http://www.ncbi.nlm.nih.gov/pubmed/22732662 when employing this script
in your studies.
Wang H, Suh JW, Das SR, Pluta J, Craige C, Yushkevich PA.
Multi-Atlas Segmentation with Joint Label Fusion.
IEEE Trans Pattern Anal Mach Intell.
--------------------------------------------------------------------------------------
script by Nick Tustison
--------------------------------------------------------------------------------------
USAGE
exit 1
}
function Help {
cat <<HELP
`basename $0` will propagate labels from a set of pre-labeled atlases using the JLF
algorithm.
Usage:
`basename $0` -d ImageDimension -o OutputPrefix <other options> <images>
Example Case:
`basename $0` -d 3 -t target.nii.gz -o malf \
-p malfPosteriors%04d.nii.gz \
-g atlas1.nii.gz -l labels1.nii.gz \
-g atlas2.nii.gz -l labels2.nii.gz \
-g atlas3.nii.gz -l labels3.nii.gz
Compulsory arguments (minimal command line requires SGE cluster, otherwise use -c & -j options):
-d: ImageDimension: 2 or 3.
-o: OutputPrefix: A prefix that is prepended to all output files.
-t: TargetImage: Target image to be labeled.
-g: Atlas: Atlas to be warped to target image.
-l: Labels: Labels corresponding to atlas (cf -g).
Optional arguments:
-m: Majority vote: Use majority vote instead of joint label fusion (default = ${MAJORITYVOTE}).
-k: Keep files: Keep warped atlas and label files (default = ${KEEP_ALL_IMAGES}).
-c: Control for parallel computation (default 0) -- 0 == run serially, 1 == SGE qsub,
2 == use PEXEC (localhost), 3 == Apple XGrid, 4 == PBS qsub, 5 == SLURM.
-j: Number of cpu cores to use (default ${CORES}; -- requires "-c 2").
-r: qsub options
-q: Use quick registration parameters: Either 0 or 1 (default = ${RUNQUICK}).
-p: Save posteriors: Save posteriors in specified c-style format e.g. posterior%04d.nii.gz
Need to specify output directory.
-f: Float precision: Use float precision (default = ${PRECISION}) -- 0 == double, 1 == float.
-u: Registration walltime (default = ${REGISTRATION_WALLTIME}): Option for PBS/SLURM qsub specifying requested time
per pairwise registration.
-v: Registration memory limit (default = ${REGISTRATION_MEMORY}): Option for PBS/SLURM qsub specifying requested memory
per pairwise registration.
-w: JLF walltime (default = ${JLF_WALLTIME}): Option for PBS/SLURM qsub specifying requested time
for the joint label fusion call.
-z: JLF Memory limit (default = ${JLF_MEMORY}): Option for PBS/SLURM qsub specifying requested memory
for the joint label fusion call.
-y: transform type (default = \'${TRANSFORM_TYPE}\')
t: translation
r: rigid
a: rigid + affine
s: rigid + affine + deformable syn
sr: rigid + deformable syn
so: deformable syn only
b: rigid + affine + deformable b-spline syn
br: rigid + deformable b-spline syn
bo: deformable b-spline syn only
-x: Target mask image (default = \'${TARGET_MASK_IMAGE}\')
otsu: use otsu thresholding to define foreground/background
or: 'or' all the warped atlas images to defined foreground/background
majorityvoting: perform a voxelwise label voting. If >= 80% of the warped atlases agree at that
voxel, we keep that voted label at that voxel and *do not* perform JLF. Note that
the 80% threshold is hard-coded but can be easily changed in the script.
<filename>: a user-specified mask
none: don't use a mask
Requirements:
This scripts relies on the following scripts in your $PATH. The script
will terminate prematurely if these files are not present or are not executable.
- pexec.sh
- waitForSGEQJobs.pl (only for use with Sun Grid Engine)
- waitForPBSQJobs.pl (only for use with Portable Batch System)
- ANTSpexec.sh (only for use with localhost parallel execution)
- waitForXGridJobs.pl (only for use with Apple XGrid)
- waitForSlurmJobs.pl (only for use with SLURM)
- antsRegistrationSyN.sh
- antsRegistrationSyNQuick.sh ( quick parameters )
--------------------------------------------------------------------------------------
Get the latest ANTS version at:
--------------------------------------------------------------------------------------
https://github.com/stnava/ANTs/
--------------------------------------------------------------------------------------
Read the ANTS documentation at:
--------------------------------------------------------------------------------------
http://stnava.github.io/ANTs/
--------------------------------------------------------------------------------------
JLF was created by:
--------------------------------------------------------------------------------------
Hongzhi Wang and Paul Yushkevich
Penn Image Computing And Science Laboratory
University of Pennsylvania
Please reference http://www.ncbi.nlm.nih.gov/pubmed/22732662 when employing this script
in your studies.
Wang H, Suh JW, Das SR, Pluta J, Craige C, Yushkevich PA.
Multi-Atlas Segmentation with Joint Label Fusion.
IEEE Trans Pattern Anal Mach Intell.
--------------------------------------------------------------------------------------
script by Nick Tustison
--------------------------------------------------------------------------------------
HELP
exit 1
}
function reportParameters {
cat <<REPORTPARAMETERS
--------------------------------------------------------------------------------------
Parameters
--------------------------------------------------------------------------------------
Dimensionality: $DIM
Output prefix: $OUTPUT_PREFIX
Posteriors format: $OUTPUT_POSTERIORS_FORMAT
Target image: $TARGET_IMAGE
Atlas images: ${ATLAS_IMAGES[@]}
Atlas labels: ${ATLAS_LABELS[@]}
Transformation: ${TRANSFORM_TYPE}
Keep all images: $KEEP_ALL_IMAGES
Processing type: $DOQSUB
Number of cpu cores: $CORES
--------------------------------------------------------------------------------------
REPORTPARAMETERS
}
cleanup()
{
echo "\n*** Performing cleanup, please wait ***\n"
runningANTSpids=$( ps --ppid $$ -o pid= )
for thePID in $runningANTSpids
do
echo "killing: ${thePID}"
kill ${thePID}
done
return $?
}
control_c()
# run if user hits control-c
{
echo -en "\n*** User pressed CTRL + C ***\n"
cleanup
exit $?
echo -en "\n*** Script cancelled by user ***\n"
}
#initializing variables with global scope
time_start=`date +%s`
CURRENT_DIR=`pwd`/
OUTPUT_DIR=${CURRENT_DIR}/tmp$RANDOM/
OUTPUT_PREFIX=${OUTPUT_DIR}/tmp
OUTPUT_SUFFIX="nii.gz"
OUTPUT_POSTERIORS_FORMAT=''
TARGET_IMAGE=''
ATLAS_IMAGES=()
ATLAS_LABELS=()
TRANSFORM='s'
##Getting system info from linux can be done with these variables.
# RAM=`cat /proc/meminfo | sed -n -e '/MemTotal/p' | awk '{ printf "%s %s\n", $2, $3 ; }' | cut -d " " -f 1`
# RAMfree=`cat /proc/meminfo | sed -n -e '/MemFree/p' | awk '{ printf "%s %s\n", $2, $3 ; }' | cut -d " " -f 1`
# cpu_free_ram=$((${RAMfree}/${cpu_count}))
if [[ ${OSTYPE:0:6} == 'darwin' ]];
then
cpu_count=`sysctl -n hw.physicalcpu`
else
cpu_count=`cat /proc/cpuinfo | grep processor | wc -l`
fi
# Provide output for Help
if [[ $# -eq 0 ]];
then
Usage >&2
fi
# Provide output for Help
if [[ "$1" == "-h" ]];
then
Help >&2
fi
# reading command line arguments
while getopts "c:d:f:g:h:j:k:l:m:o:p:q:r:t:u:v:w:x:y:z:" OPT
do
case $OPT in
h) #help
echo "$USAGE"
exit 0
;;
c) #use SGE cluster
DOQSUB=$OPTARG
if [[ $DOQSUB -gt 5 ]];
then
echo " DOQSUB must be an integer value (0=serial, 1=SGE qsub, 2=try pexec, 3=XGrid, 4=PBS qsub, 5=SLURM ) you passed -c $DOQSUB "
exit 1
fi
;;
d) #dimensions
DIM=$OPTARG
if [[ ${DIM} -ne 2 && $DIM -ne 3 ]];
then
echo " Dimensionality is only valid for 2 or 3. You passed -d $DIM."
exit 1
fi
;;
f)
PRECISION=$OPTARG
;;
g)
ATLAS_IMAGES[${#ATLAS_IMAGES[@]}]=$OPTARG
;;
j) #number of cpu cores to use
CORES=$OPTARG
;;
k)
KEEP_ALL_IMAGES=$OPTARG
;;
m) #majority voting option
MAJORITYVOTE=$OPTARG
;;
p)
OUTPUT_POSTERIORS_FORMAT=$OPTARG
;;
q)
RUNQUICK=$OPTARG
;;
r)
QSUB_OPTS=$OPTARG
;;
l)
ATLAS_LABELS[${#ATLAS_LABELS[@]}]=$OPTARG
;;
o)
OUTPUT_PREFIX=$OPTARG
OUTPUT_DIR=`dirname ${OUTPUT_PREFIX}`
;;
t)
TARGET_IMAGE=$OPTARG
;;
u)
REGISTRATION_WALLTIME=$OPTARG
;;
v)
REGISTRATION_MEMORY=$OPTARG
;;
w)
JLF_WALLTIME=$OPTARG
;;
z)
JLF_MEMORY=$OPTARG
;;
x)
TARGET_MASK_IMAGE=$OPTARG
;;
y)
TRANSFORM_TYPE=$OPTARG
;;
\?) # getopts issues an error message
echo "$USAGE" >&2
exit 1
;;
esac
done
if [[ $DOQSUB -eq 1 || $DOQSUB -eq 4 ]];
then
qq=`which qsub`
if [[ ${#qq} -lt 1 ]];
then
echo "do you have qsub? if not, then choose another c option ... if so, then check where the qsub alias points ..."
exit 1
fi
fi
if [[ $DOQSUB -eq 5 ]];
then
qq=`which sbatch`
if [[ ${#qq} -lt 1 ]];
then
echo "do you have sbatch? if not, then choose another c option ... if so, then check where the sbatch alias points ..."
exit
fi
fi
if [[ ! -f "$TARGET_IMAGE" ]];
then
echo "Target image '$TARGET_IMAGE' does not exist. See usage: '$0 -h 1'"
exit
fi
if [[ ${#ATLAS_IMAGES[@]} -ne ${#ATLAS_LABELS[@]} ]];
then
echo "The number of atlas images does not equal the number of labels. Ensure that a corresponding set of labels exist for each image."
exit 1
fi
PRECISIONFLAG='f'
if [[ ${PRECISION} -eq 0 ]];
then
PRECISIONFLAG='d'
fi
if [[ ${OUTPUT_PREFIX} == */ ]];
then
OUTPUT_DIR=${OUTPUT_PREFIX%/}
else
OUTPUT_DIR=$(dirname $OUTPUT_PREFIX)
fi
if [[ ! -d $OUTPUT_DIR ]];
then
echo "The output directory \"$OUTPUT_DIR\" does not exist. Making it."
mkdir -p $OUTPUT_DIR
fi
##########################################################################
#
# Perform JLF labeling by
# 1) registering all atlases to target image
# 2) call 'jointfusion'
#
##########################################################################
echo
echo "--------------------------------------------------------------------------------------"
echo " Start JLFization"
echo "--------------------------------------------------------------------------------------"
reportParameters
jobIDs=""
WARPED_ATLAS_IMAGES=()
WARPED_ATLAS_LABELS=()
AFFINE_FILES=()
WARP_FIELDS=()
INVERSE_WARP_FIELDS=()
for (( i = 0; i < ${#ATLAS_IMAGES[@]}; i++ ))
do
IMG_BASE=`basename ${ATLAS_IMAGES[$i]}`
BASENAME=` echo ${IMG_BASE} | cut -d '.' -f 1 `
qscript="${OUTPUT_DIR}/job_${BASENAME}_${i}.sh"
WARPED_ATLAS_IMAGES[${#WARPED_ATLAS_IMAGES[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz"
INVERSE_WARPED_ATLAS_IMAGES[${#INVERSE_WARPED_ATLAS_IMAGES[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_InverseWarped.nii.gz"
WARPED_ATLAS_LABELS[${#WARPED_ATLAS_LABELS[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz"
WARP_FIELDS[${#WARP_FIELDS[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_1Warp.nii.gz"
INVERSE_WARP_FIELDS[${#INVERSE_WARP_FIELDS[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_1InverseWarp.nii.gz"
AFFINE_FILES[${#AFFINE_FILES[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_0GenericAffine.mat"
if [[ -f "${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz" ]];
then
echo ${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz already exists.
rm -f $qscript
continue
fi
regcall=antsRegistrationSyN.sh
if [[ $RUNQUICK -eq 1 ]];
then
regcall=antsRegistrationSyNQuick.sh
fi
registrationCall="$regcall \
-d ${DIM} \
-p ${PRECISIONFLAG} \
-j 1 \
-t ${TRANSFORM_TYPE} \
-f ${TARGET_IMAGE} \
-m ${ATLAS_IMAGES[$i]} \
-o ${OUTPUT_PREFIX}${BASENAME}_${i}_ > ${OUTPUT_PREFIX}${BASENAME}_${i}_log.txt"
labelXfrmCall="antsApplyTransforms \
-d ${DIM} \
--float 1 \
-i ${ATLAS_LABELS[$i]} \
-r ${TARGET_IMAGE} \
-o ${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz \
-n NearestNeighbor \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_1Warp.nii.gz \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_0GenericAffine.mat >> ${OUTPUT_PREFIX}${BASENAME}_${i}_log.txt"
copyImageHeaderCall="CopyImageHeaderInformation \
${TARGET_IMAGE} \
${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz \
${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz 1 1 1"
copyLabelsHeaderCall="CopyImageHeaderInformation \
${TARGET_IMAGE} \
${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz \
${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz 1 1 1"
rm -f $qscript
if [[ $DOQSUB -eq 5 ]];
then
# SLURM job scripts must start with a shebang
echo '#!/bin/sh' > $qscript
fi
echo "$registrationCall" >> $qscript
echo "$labelXfrmCall" >> $qscript
echo "$copyImageHeaderCall" >> $qscript
echo "$copyLabelsHeaderCall" >> $qscript
if [[ $DOQSUB -eq 1 ]];
then
id=`qsub -cwd -S /bin/bash -N antsJlfReg $QSUB_OPTS $qscript | awk '{print $3}'`
jobIDs="$jobIDs $id"
sleep 0.5
elif [[ $DOQSUB -eq 4 ]];
then
id=`qsub -N antsJlfReg $QSUB_OPTS -q nopreempt -l nodes=1:ppn=1 -l mem=${REGISTRATION_MEMORY} -l walltime=${REGISTRATION_WALLTIME} $qscript | awk '{print $1}'`
jobIDs="$jobIDs $id"
sleep 0.5
elif [[ $DOQSUB -eq 3 ]];
then
id=`xgrid $XGRID_OPTS -job submit /bin/bash $qscript | awk '{sub(/;/,"");print $3}' | tr '\n' ' ' | sed 's: *: :g'`
jobIDs="$jobIDs $id"
elif [[ $DOQSUB -eq 5 ]];
then
id=`sbatch --job-name=antsJlfReg${i} $QSUB_OPTS --nodes=1 --cpus-per-task=1 --time=${REGISTRATION_WALLTIME} --mem=${REGISTRATION_MEMORY} $qscript | rev | cut -f1 -d\ | rev`
jobIDs="$jobIDs $id"
sleep 0.5
elif [[ $DOQSUB -eq 0 ]];
then
echo $qscript
bash $qscript
fi
done
if [[ $DOQSUB -eq 2 ]];
then
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF on max ${CORES} cpucores. "
echo "--------------------------------------------------------------------------------------"
chmod +x ${OUTPUT_DIR}/job_*.sh
$PEXEC -j ${CORES} "sh" ${OUTPUT_DIR}/job_*.sh
fi
jlfCall="antsJointFusion -d ${DIM} -t $TARGET_IMAGE --verbose 1 "
if [[ $DOQSUB -eq 0 ]];
then
# Run job locally
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF"
echo "--------------------------------------------------------------------------------------"
EXISTING_WARPED_ATLAS_IMAGES=()
EXISTING_WARPED_ATLAS_LABELS=()
for (( i = 0; i < ${#WARPED_ATLAS_IMAGES[@]}; i++ ))
do
echo ${WARPED_ATLAS_IMAGES[$i]}
if [[ -f ${WARPED_ATLAS_IMAGES[$i]} ]] && [[ -f ${WARPED_ATLAS_LABELS[$i]} ]];
then
EXISTING_WARPED_ATLAS_IMAGES[${#EXISTING_WARPED_ATLAS_IMAGES[@]}]=${WARPED_ATLAS_IMAGES[$i]}
EXISTING_WARPED_ATLAS_LABELS[${#EXISTING_WARPED_ATLAS_LABELS[@]}]=${WARPED_ATLAS_LABELS[$i]}
fi
done
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -lt 2 ]];
then
echo "Error: At least 2 warped image/label pairs needs to exist for jointFusion."
exit 1
fi
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -ne ${#WARPED_ATLAS_LABELS[@]} ]];
then
echo "Warning: One or more registrations failed."
fi
maskCall=''
if [[ $MAJORITYVOTE -eq 1 ]];
then
jlfCall="ImageMath ${DIM} ${OUTPUT_PREFIX}MajorityVotingLabels.nii.gz MajorityVoting ${EXISTING_WARPED_ATLAS_LABELS[@]} "
else
for (( i = 0; i < ${#EXISTING_WARPED_ATLAS_IMAGES[@]}; i++ ))
do
jlfCall="${jlfCall} -g ${EXISTING_WARPED_ATLAS_IMAGES[$i]} -l ${EXISTING_WARPED_ATLAS_LABELS[$i]}"
done
if [[ -z "${OUTPUT_POSTERIORS_FORMAT}" ]];
then
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz ]"
else
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz,${OUTPUT_POSTERIORS_FORMAT} ]"
fi
if [[ ${TARGET_MASK_IMAGE} == 'otsu' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOtsu.nii.gz"
maskCall="ThresholdImage ${DIM} ${TARGET_IMAGE} ${TARGET_MASK_IMAGE} Otsu 1;"
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
elif [[ ${TARGET_MASK_IMAGE} == 'or' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOr.nii.gz"
maskCall="ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${EXISTING_WARPED_ATLAS_LABELS[0]} ${EXISTING_WARPED_ATLAS_LABELS[1]};"
for (( i = 2; i < ${#EXISTING_WARPED_ATLAS_LABELS[@]}; i++ ))
do
maskCall="${maskCall} ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${TARGET_MASK_IMAGE} ${EXISTING_WARPED_ATLAS_LABELS[$i]};"
done
maskCall="${maskCall} ThresholdImage ${DIM} ${TARGET_MASK_IMAGE} ${TARGET_MASK_IMAGE} 0 0 0 1"
elif [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
MAJORITY_VOTING_IMAGE="${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
maskCall="ImageMath ${DIM} ${MAJORITY_VOTING_IMAGE} MajorityVoting 0.8 ${EXISTING_WARPED_ATLAS_LABELS[@]};"
jlfCall="${jlfCall} -x ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting_Mask.nii.gz"
elif [[ -f ${TARGET_MASK_IMAGE} ]];
then
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
fi
fi
qscript2="${OUTPUT_PREFIX}JLF.sh"
echo "$maskCall" > $qscript2
echo "$jlfCall" >> $qscript2
if [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
combineCall="ImageMath ${DIM} ${OUTPUT_PREFIX}Labels.nii.gz max ${OUTPUT_PREFIX}Labels.nii.gz ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
echo "$combineCall" >> $qscript2
fi
echo $qscript2
bash $qscript2
fi
if [[ $DOQSUB -eq 1 ]];
then
# Run jobs on SGE and wait to finish
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF on SGE cluster. "
echo "--------------------------------------------------------------------------------------"
waitForSGEQJobs.pl 1 600 $jobIDs
# Returns 1 if there are errors
if [[ ! $? -eq 0 ]];
then
echo "qsub submission failed - jobs went into error state"
exit 1;
fi
EXISTING_WARPED_ATLAS_IMAGES=()
EXISTING_WARPED_ATLAS_LABELS=()
for (( i = 0; i < ${#WARPED_ATLAS_IMAGES[@]}; i++ ))
do
echo ${WARPED_ATLAS_IMAGES[$i]}
if [[ -f ${WARPED_ATLAS_IMAGES[$i]} ]] && [[ -f ${WARPED_ATLAS_LABELS[$i]} ]];
then
EXISTING_WARPED_ATLAS_IMAGES[${#EXISTING_WARPED_ATLAS_IMAGES[@]}]=${WARPED_ATLAS_IMAGES[$i]}
EXISTING_WARPED_ATLAS_LABELS[${#EXISTING_WARPED_ATLAS_LABELS[@]}]=${WARPED_ATLAS_LABELS[$i]}
fi
done
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -lt 2 ]];
then
echo "Error: At least 2 warped image/label pairs needs to exist for jointFusion."
exit 1
fi
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -ne ${#WARPED_ATLAS_LABELS[@]} ]];
then
echo "Warning: One or more registrations failed."
fi
maskCall=''
if [[ $MAJORITYVOTE -eq 1 ]];
then
jlfCall="ImageMath ${DIM} ${OUTPUT_PREFIX}MajorityVotingLabels.nii.gz MajorityVoting ${EXISTING_WARPED_ATLAS_LABELS[@]} "
else
for (( i = 0; i < ${#EXISTING_WARPED_ATLAS_IMAGES[@]}; i++ ))
do
jlfCall="${jlfCall} -g ${EXISTING_WARPED_ATLAS_IMAGES[$i]} -l ${EXISTING_WARPED_ATLAS_LABELS[$i]}"
done
if [[ -z "${OUTPUT_POSTERIORS_FORMAT}" ]];
then
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz ]"
else
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz,${OUTPUT_POSTERIORS_FORMAT}]"
fi
if [[ ${TARGET_MASK_IMAGE} == 'otsu' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOtsu.nii.gz"
maskCall="ThresholdImage ${DIM} ${TARGET_IMAGE} ${TARGET_MASK_IMAGE} Otsu 1;"
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
elif [[ ${TARGET_MASK_IMAGE} == 'or' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOr.nii.gz"
maskCall="ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${EXISTING_WARPED_ATLAS_IMAGES[0]} ${EXISTING_WARPED_ATLAS_IMAGES[1]};"
for (( i = 2; i < ${#EXISTING_WARPED_ATLAS_IMAGES[@]}; i++ ))
do
maskCall="${maskCall} ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${TARGET_MASK_IMAGE} ${EXISTING_WARPED_ATLAS_IMAGES[$i]};"
done
maskCall="${maskCall} ThresholdImage ${DIM} ${TARGET_MASK_IMAGE} ${TARGET_MASK_IMAGE} 0 0 0 1"
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
elif [[ -f ${TARGET_MASK_IMAGE} ]];
then
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
fi
fi
qscript2="${OUTPUT_PREFIX}JLF.sh"
echo "$maskCall" > $qscript2
echo "$jlfCall" >> $qscript2
if [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
combineCall="ImageMath ${DIM} ${OUTPUT_PREFIX}Labels.nii.gz max ${OUTPUT_PREFIX}Labels.nii.gz ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
echo "$combineCall" >> $qscript2
fi
jobIDs=`qsub -cwd -S /bin/bash -N antsJlf $QSUB_OPTS $qscript2 | awk '{print $3}'`
waitForSGEQJobs.pl 1 600 $jobIDs
fi
if [[ $DOQSUB -eq 4 ]];
then
# Run jobs on PBS and wait to finish
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF on PBS cluster. "
echo "--------------------------------------------------------------------------------------"
waitForPBSQJobs.pl 1 600 $jobIDs
# Returns 1 if there are errors
if [[ ! $? -eq 0 ]];
then
echo "qsub submission failed - jobs went into error state"
exit 1;
fi
EXISTING_WARPED_ATLAS_IMAGES=()
EXISTING_WARPED_ATLAS_LABELS=()
for (( i = 0; i < ${#WARPED_ATLAS_IMAGES[@]}; i++ ))
do
echo ${WARPED_ATLAS_IMAGES[$i]}
if [[ -f ${WARPED_ATLAS_IMAGES[$i]} ]] && [[ -f ${WARPED_ATLAS_LABELS[$i]} ]];
then
EXISTING_WARPED_ATLAS_IMAGES[${#EXISTING_WARPED_ATLAS_IMAGES[@]}]=${WARPED_ATLAS_IMAGES[$i]}
EXISTING_WARPED_ATLAS_LABELS[${#EXISTING_WARPED_ATLAS_LABELS[@]}]=${WARPED_ATLAS_LABELS[$i]}
fi
done
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -lt 2 ]];
then
echo "Error: At least 2 warped image/label pairs needs to exist for jointFusion."
exit 1
fi
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -ne ${#WARPED_ATLAS_LABELS[@]} ]];
then
echo "Warning: One or more registrations failed."
fi
maskCall=''
if [[ $MAJORITYVOTE -eq 1 ]];
then
jlfCall="ImageMath ${DIM} ${OUTPUT_PREFIX}MajorityVotingLabels.nii.gz MajorityVoting ${EXISTING_WARPED_ATLAS_LABELS[@]} "
else
for (( i = 0; i < ${#EXISTING_WARPED_ATLAS_IMAGES[@]}; i++ ))
do
jlfCall="${jlfCall} -g ${EXISTING_WARPED_ATLAS_IMAGES[$i]} -l ${EXISTING_WARPED_ATLAS_LABELS[$i]}"
done
if [[ -z "${OUTPUT_POSTERIORS_FORMAT}" ]];
then
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz ]"
else
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz,${OUTPUT_POSTERIORS_FORMAT} ]"
fi
if [[ ${TARGET_MASK_IMAGE} == 'otsu' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOtsu.nii.gz"
maskCall="ThresholdImage ${DIM} ${TARGET_IMAGE} ${TARGET_MASK_IMAGE} Otsu 1;"
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
elif [[ ${TARGET_MASK_IMAGE} == 'or' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOr.nii.gz"
maskCall="ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${EXISTING_WARPED_ATLAS_LABELS[0]} ${EXISTING_WARPED_ATLAS_LABELS[1]};"
for (( i = 2; i < ${#EXISTING_WARPED_ATLAS_LABELS[@]}; i++ ))
do
maskCall="${maskCall} ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${TARGET_MASK_IMAGE} ${EXISTING_WARPED_ATLAS_LABELS[$i]};"
done
maskCall="${maskCall} ThresholdImage ${DIM} ${TARGET_MASK_IMAGE} ${TARGET_MASK_IMAGE} 0 0 0 1"
elif [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
MAJORITY_VOTING_IMAGE="${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
maskCall="ImageMath ${DIM} ${MAJORITY_VOTING_IMAGE} MajorityVoting 0.8 ${EXISTING_WARPED_ATLAS_LABELS[@]};"
jlfCall="${jlfCall} -x ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting_Mask.nii.gz"
elif [[ -f ${TARGET_MASK_IMAGE} ]];
then
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
fi
fi
qscript2="${OUTPUT_PREFIX}JLF.sh"
echo "$maskCall" > $qscript2
echo "$jlfCall" >> $qscript2
if [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
combineCall="ImageMath ${DIM} ${OUTPUT_PREFIX}Labels.nii.gz max ${OUTPUT_PREFIX}Labels.nii.gz ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
echo "$combineCall" >> $qscript2
fi
jobIDs=`qsub -N antsJlf $QSUB_OPTS -q nopreempt -l nodes=1:ppn=1 -l mem=${JLF_MEMORY} -l walltime=${JLF_WALLTIME} $qscript2 | awk '{print $1}'`
waitForPBSQJobs.pl 1 600 $jobIDs
fi
if [[ $DOQSUB -eq 2 ]];
then
EXISTING_WARPED_ATLAS_IMAGES=()
EXISTING_WARPED_ATLAS_LABELS=()
for (( i = 0; i < ${#WARPED_ATLAS_IMAGES[@]}; i++ ))
do
echo ${WARPED_ATLAS_IMAGES[$i]}
if [[ -f ${WARPED_ATLAS_IMAGES[$i]} ]] && [[ -f ${WARPED_ATLAS_LABELS[$i]} ]];
then
EXISTING_WARPED_ATLAS_IMAGES[${#EXISTING_WARPED_ATLAS_IMAGES[@]}]=${WARPED_ATLAS_IMAGES[$i]}
EXISTING_WARPED_ATLAS_LABELS[${#EXISTING_WARPED_ATLAS_LABELS[@]}]=${WARPED_ATLAS_LABELS[$i]}
fi
done
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -lt 2 ]];
then
echo "Error: At least 2 warped image/label pairs needs to exist for jointFusion."
exit 1
fi
if [[ ${#EXISTING_WARPED_ATLAS_LABELS[@]} -ne ${#WARPED_ATLAS_LABELS[@]} ]];
then
echo "Warning: One or more registrations failed."
fi
maskCall=''
if [[ $MAJORITYVOTE -eq 1 ]];
then
jlfCall="ImageMath ${DIM} ${OUTPUT_PREFIX}MajorityVotingLabels.nii.gz MajorityVoting ${EXISTING_WARPED_ATLAS_LABELS[@]} "
else
for (( i = 0; i < ${#EXISTING_WARPED_ATLAS_IMAGES[@]}; i++ ))
do
jlfCall="${jlfCall} -g ${EXISTING_WARPED_ATLAS_IMAGES[$i]} -l ${EXISTING_WARPED_ATLAS_LABELS[$i]}"
done
if [[ -z "${OUTPUT_POSTERIORS_FORMAT}" ]];
then
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz ]"
else
jlfCall="${jlfCall} -o [ ${OUTPUT_PREFIX}Labels.nii.gz,${OUTPUT_PREFIX}Intensity.nii.gz,${OUTPUT_POSTERIORS_FORMAT}]"
fi
if [[ ${TARGET_MASK_IMAGE} == 'otsu' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOtsu.nii.gz"
maskCall="ThresholdImage ${DIM} ${TARGET_IMAGE} ${TARGET_MASK_IMAGE} Otsu 1;"
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
elif [[ ${TARGET_MASK_IMAGE} == 'or' ]];
then
TARGET_MASK_IMAGE="${OUTPUT_PREFIX}TargetMaskImageOr.nii.gz"
maskCall="ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${EXISTING_WARPED_ATLAS_LABELS[0]} ${EXISTING_WARPED_ATLAS_LABELS[1]};"
for (( i = 2; i < ${#EXISTING_WARPED_ATLAS_LABELS[@]}; i++ ))
do
maskCall="${maskCall} ImageMath ${DIM} ${TARGET_MASK_IMAGE} max ${TARGET_MASK_IMAGE} ${EXISTING_WARPED_ATLAS_LABELS[$i]};"
done
maskCall="${maskCall} ThresholdImage ${DIM} ${TARGET_MASK_IMAGE} ${TARGET_MASK_IMAGE} 0 0 0 1"
elif [[ ${TARGET_MASK_IMAGE} == 'majorityvoting' ]];
then
MAJORITY_VOTING_IMAGE="${OUTPUT_PREFIX}TargetMaskImageMajorityVoting.nii.gz"
maskCall="ImageMath ${DIM} ${MAJORITY_VOTING_IMAGE} MajorityVoting 0.8 ${EXISTING_WARPED_ATLAS_LABELS[@]};"
jlfCall="${jlfCall} -x ${OUTPUT_PREFIX}TargetMaskImageMajorityVoting_Mask.nii.gz"
elif [[ -f ${TARGET_MASK_IMAGE} ]];
then
jlfCall="${jlfCall} -x ${TARGET_MASK_IMAGE}"
fi
fi