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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
|
#!/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.
JLF=antsJointFusion
ANTS=antsRegistration
WARP=antsApplyTransforms
SMOOTH=SmoothDisplacementField
PEXEC=ANTSpexec.sh
SGE=waitForSGEQJobs.pl
PBS=waitForPBSQJobs.pl
XGRID=waitForXGridJobs.pl
SLURM=waitForSlurmJobs.pl
fle_error=0
for FLE in $JLF $ANTS $WARP $SMOOTH $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
function Usage {
cat <<USAGE
This adds some additional utilty to the original antsJointLabelFusion.sh script.
Specifically, it adds the parameters '-a', '-b', '-e', and '-s' for performing
multiple piecewise registration steps. For example, suppose one wants to use
joint label fusion to label thoracic CT where the following atlases have been
supplied with the following labels:
label 1: left lung
label 2: right lung
label 3: spinal cord
label 4: heart
This script allows one to perform a piecewise registration where one can register
the two lungs separately and then "stitch" together the resulting transforms. This
is followed up by a registration refinement step where other labeled structures
can be incorporated into driving the registration. An additional benefit is that
one can use the segmentation labels of the atlases to create fixed masks for the
registration. Example usage for the above would be
antsJointLabelFusion2.sh -d 3 ... \
-b 1 -b 2 -b 3 -b 4 \
-e 1 -e 2 \
-a 4 \
-s 10 \
...
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 = 0).
-k: Keep files: Keep warped atlas and label files (default = 0).
-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 2; -- requires "-c 2").
-r: qsub options
-q: Use quick registration parameters: Either 0 or 1 (default = 1).
-a: isotropic resampling in mm for specifying labels (default = 'NA' = "no resampling").
-b: specify labels to use for dilation and masking for the final registration
-e: subset of labels specified by the '-b' option to be used for the piecewise registration.
If no labels are specified by '-e', it is assumed that all the '-b' labels are to be
used for the initial piecewise registration.
-s: dilation radii for labels specified with option 'b'
-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 = 1) -- 0 == double, 1 == float.
-u: Registration walltime (default = 20:00:00): Option for PBS/SLURM qsub specifying requested time
per pairwise registration.
-v: Registration memory limit (default = 8gb): Option for PBS/SLURM qsub specifying requested memory
per pairwise registration.
-w: JLF walltime (default = 20:00:00): Option for PBS/SLURM qsub specifying requested time
for the joint label fusion call.
-z: JLF Memory limit (default = 8gb): Option for PBS/SLURM qsub specifying requested memory
for the joint label fusion call.
-y: transform type (default = 's')
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 = 'majorityvoting')
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.
For lung-specific applications, cf https://www.ncbi.nlm.nih.gov/pubmed/26222827
Tustison NJ, Qing K, Wang C, Altes TA, Mugler JP 3rd.
Atlas-based estimation of lung and lobar anatomy in proton MRI.
Magn Reson Med. 2016 Jul;76(1):315-20.
--------------------------------------------------------------------------------------
script by Nick Tustison
--------------------------------------------------------------------------------------
USAGE
exit 1
}
function Help {
cat <<HELP
This adds some additional utilty to the original antsJointLabelFusion.sh script.
Specifically, it adds the parameters '-a', '-b', '-e', and '-s' for performing
multiple piecewise registration steps. For example, suppose one wants to use
joint label fusion to label thoracic CT where the following atlases have been
supplied with the following labels:
label 1: left lung
label 2: right lung
label 3: spinal cord
label 4: heart
This script allows one to perform a piecewise registration where one can register
the two lungs separately and then "stitch" together the resulting transforms. This
is followed up by a registration refinement step where other labeled structures
can be incorporated into driving the registration. An additional benefit is that
one can use the segmentation labels of the atlases to create fixed masks for the
registration. Example usage for the above would be
antsJointLabelFusion2.sh -d 3 ... \
-b 1 -b 2 -b 3 -b 4 \
-e 1 -e 2 \
-a 4 \
-s 10 \
...
`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 = 0).
-k: Keep files: Keep warped atlas and label files (default = 0).
-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 2; -- requires "-c 2").
-q: Use quick registration parameters: Either 0 or 1 (default = 1).
-b: specify labels to use for dilation and masking for the final registration
-e: subset of labels specified by the '-b' option to be used for the piecewise registration.
If no labels are specified by '-e', it is assumed that all the '-b' labels are to be
used for the initial piecewise registration.
-s: dilation radii (in mm) for labels specified with option 'b'
-a: isotropic resampling in mm for specifying labels (default = 4).
-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 = 1) -- 0 == double, 1 == float.
-u: Registration walltime (default = 20:00:00): Option for PBS/SLURM qsub specifying requested time
per pairwise registration.
-v: Registration memory limit (default = 8gb): Option for PBS/SLURM qsub specifying requested memory
per pairwise registration.
-w: JLF walltime (default = 20:00:00): Option for PBS/SLURM qsub specifying requested time
for the joint label fusion call.
-z: JLF Memory limit (default = 8gb): Option for PBS/SLURM qsub specifying requested memory
for the joint label fusion call.
-y: Transform type (default = 's')
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 = 'majorityvoting')
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 directory. 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`/
DIM=3
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'
PIECEWISE_LABELS=()
PIECEWISE_LABELS_SUBSET=()
PIECEWISE_DILATION_RADII=()
ISOTROPIC_RESAMPLING='NA'
KEEP_ALL_IMAGES=0
DOQSUB=0
CORES=1
PRECISION=0
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"
##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 [[ "$1" == "-h" ]];
then
Help >&2
fi
MAJORITYVOTE=0
RUNQUICK=1
TRANSFORM_TYPE="s"
# reading command line arguments
while getopts "a:b:c:d:e:f:g:h:j:k:l:m:o:p:q:r:s: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
;;
a)
ISOTROPIC_RESAMPLING=$OPTARG
;;
b)
PIECEWISE_LABELS[${#PIECEWISE_LABELS[@]}]=$OPTARG
;;
e)
PIECEWISE_LABELS_SUBSET[${#PIECEWISE_LABELS_SUBSET[@]}]=$OPTARG
;;
s)
PIECEWISE_DILATION_RADII[${#PIECEWISE_DILATION_RADII[@]}]=$OPTARG
;;
j) #number of cpu cores to use
CORES=$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
if [[ ${#PIECEWISE_LABELS[@]} -gt 0 ]];
then
if [[ ${#PIECEWISE_DILATION_RADII[@]} -eq 1 ]];
then
for (( i = 0; i < ${#PIECEWISE_LABELS[@]}; i++ ))
do
PIECEWISE_DILATION_RADII[${i}]=${PIECEWISE_DILATION_RADII[0]}
done
elif [[ ${#PIECEWISE_LABELS[@]} -ne ${#PIECEWISE_DILATION_RADII[@]} ]];
then
echo "The number of dilation radii does not equal the number of piecewise labels."
exit 1
fi
if [[ ${#PIECEWISE_LABELS_SUBSET[@]} -eq 0 ]];
then
for (( i = 0; i < ${#PIECEWISE_LABELS[@]}; i++ ))
do
PIECEWISE_LABELS_SUBSET[$i]=${PIECEWISE_LABELS[$i]}
done
else
for (( i = 0; i < ${#PIECEWISE_LABELS_SUBSET[@]}; i++ ))
do
LABEL_IS_FOUND=0
for (( j = 0; j < ${#PIECEWISE_LABELS[@]}; j++ ))
do
if [[ ${PIECEWISE_LABELS[$j]} -eq ${PIECEWISE_LABELS_SUBSET[$i]} ]];
then
LABEL_IS_FOUND=1
break
fi
done
if [[ $LABEL_IS_FOUND -eq 0 ]];
then
echo "Subset label ${PIECEWISE_LABELS_SUBSET[$i]} is not found in the labels specified by the -b option."
exit 1
fi
done
fi
fi
ISOTROPIC_RESAMPLING_VECTOR="${ISOTROPIC_RESAMPLING}x${ISOTROPIC_RESAMPLING}"
if [[ $DIM -eq 3 ]];
then
ISOTROPIC_RESAMPLING_VECTOR="${ISOTROPIC_RESAMPLING}x${ISOTROPIC_RESAMPLING}x${ISOTROPIC_RESAMPLING}"
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"
WARPED_ATLAS_LABELS[${#WARPED_ATLAS_LABELS[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz"
if [[ -f ${WARPED_ATLAS_LABELS[${i}]} ]];
then
echo "${WARPED_ATLAS_LABELS[${i}]} already exists."
rm -f $qscript
continue
fi
regcall=antsRegistrationSyN.sh
if [[ $RUNQUICK -eq 1 ]];
then
regcall=antsRegistrationSyNQuick.sh
fi
if [[ ${#PIECEWISE_LABELS[@]} -eq 0 ]];
then
INVERSE_WARPED_ATLAS_IMAGES[${#INVERSE_WARPED_ATLAS_IMAGES[@]}]="${OUTPUT_PREFIX}${BASENAME}_${i}_InverseWarped.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"
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 GenericLabel \
-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
else
TMP_FILES=()
logFile="${OUTPUT_PREFIX}${BASENAME}_${i}_log.txt"
# Dilate regions and create masks
SUBSET_CONFIDENCE_MASK="${OUTPUT_PREFIX}${BASENAME}_${i}_ConfidenceMask.nii.gz"
CONFIDENCE_DILATED_MASK="${OUTPUT_PREFIX}${BASENAME}_${i}_ConfidenceDilatedMask.nii.gz"
PIECEWISE_MASKS=()
PIECEWISE_DILATED_MASKS=()
PIECEWISE_OUTPUT_PREFIX="${OUTPUT_PREFIX}${BASENAME}_${i}_PiecewiseLabel"
INITIAL_LABEL_IS_SPECIFIED=0
for (( j = 0; j < ${#PIECEWISE_LABELS[@]}; j++ ))
do
PIECEWISE_MASKS[$j]="${PIECEWISE_OUTPUT_PREFIX}${PIECEWISE_LABELS[$j]}.nii.gz"
PIECEWISE_DILATED_MASKS[$j]="${PIECEWISE_OUTPUT_PREFIX}Dilated${PIECEWISE_LABELS[$j]}.nii.gz"
thresholdCall="ThresholdImage ${DIM} ${ATLAS_LABELS[$i]} ${PIECEWISE_MASKS[$j]} ${PIECEWISE_LABELS[$j]} ${PIECEWISE_LABELS[$j]} 1 0"
echo "$thresholdCall" >> $qscript
distanceCall="ImageMath ${DIM} ${PIECEWISE_DILATED_MASKS[$j]} MaurerDistance ${PIECEWISE_MASKS[$j]} 1"
thresholdCall="ThresholdImage ${DIM} ${PIECEWISE_DILATED_MASKS[$j]} ${PIECEWISE_DILATED_MASKS[$j]} -1000000 ${PIECEWISE_DILATION_RADII[$j]} 1 0"
echo "$distanceCall" >> $qscript
echo "$thresholdCall" >> $qscript
if [[ $j -eq 0 ]]
then
echo "cp ${PIECEWISE_DILATED_MASKS[$j]} ${CONFIDENCE_DILATED_MASK}" >> $qscript
else
addCall="ImageMath ${DIM} ${CONFIDENCE_DILATED_MASK} max ${CONFIDENCE_DILATED_MASK} ${PIECEWISE_DILATED_MASKS[$j]}"
echo "$addCall" >> $qscript
fi
SUBSET_LABEL_IS_FOUND=0
for (( k = 0; k < ${#PIECEWISE_LABELS_SUBSET[@]}; k++ ))
do
if [[ ${PIECEWISE_LABELS[$j]} -eq ${PIECEWISE_LABELS_SUBSET[$k]} ]];
then
SUBSET_LABEL_IS_FOUND=1
break
fi
done
if [[ $SUBSET_LABEL_IS_FOUND -eq 0 ]];
then
continue
fi
if [[ $INITIAL_LABEL_IS_SPECIFIED -eq 0 ]]
then
echo "cp ${PIECEWISE_MASKS[$j]} ${SUBSET_CONFIDENCE_MASK}" >> $qscript
INITIAL_LABEL_IS_SPECIFIED=1
else
addCall="ImageMath ${DIM} ${SUBSET_CONFIDENCE_MASK} max ${SUBSET_CONFIDENCE_MASK} ${PIECEWISE_MASKS[$j]}"
echo "$addCall" >> $qscript
fi
done
if [[ ${ISOTROPIC_RESAMPLING} != 'NA' ]];
then
resampleCall="ResampleImage ${DIM} ${SUBSET_CONFIDENCE_MASK} ${SUBSET_CONFIDENCE_MASK} $ISOTROPIC_RESAMPLING_VECTOR 0 1"
echo "$resampleCall" >> $qscript
fi
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_MASKS[@]} ${PIECEWISE_DILATED_MASKS[@]} ${SUBSET_CONFIDENCE_MASK} ${CONFIDENCE_DILATED_MASK} )
# Perform initial linear registration of the whole images
# Note that we're registering the target image to the atlas image since the atlas
# image has the masks (i.e., labels)
INIT_OUTPUT_PREFIX=${OUTPUT_PREFIX}${BASENAME}_${i}_Init
registrationCallInit="antsRegistration -d 3 -v 1 -o $INIT_OUTPUT_PREFIX -r [ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},0 ]"
registrationCallInit="$registrationCallInit -t Rigid[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x40x10,1e-6,10 ] -f 6x4x3 -s 3x2x1"
registrationCallInit="$registrationCallInit -t Similarity[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x40x10,1e-6,10 ] -f 6x4x3 -s 3x2x1"
registrationCallInit="$registrationCallInit -t Affine[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x40x10,1e-6,10 ] -f 6x4x3 -s 3x2x1 >> $logFile"
echo "$registrationCallInit" >> $qscript
# Register piecewise labels separately
PIECEWISE_TRANSFORM_WARPS=""
for (( j = 0; j < ${#PIECEWISE_LABELS[@]}; j++ ))
do
SUBSET_LABEL_IS_FOUND=0
for (( k = 0; k < ${#PIECEWISE_LABELS_SUBSET[@]}; k++ ))
do
if [[ ${PIECEWISE_LABELS[$j]} -eq ${PIECEWISE_LABELS_SUBSET[$k]} ]];
then
SUBSET_LABEL_IS_FOUND=1
break
fi
done
if [[ $SUBSET_LABEL_IS_FOUND -eq 0 ]];
then
continue
fi
PIECEWISE_OUTPUT_PREFIX=${OUTPUT_PREFIX}${BASENAME}_${i}_PiecewiseLabel${PIECEWISE_LABELS[$j]}_
registrationCallPiecewise="antsRegistration -d 3 -v 1 -z 0 -o $PIECEWISE_OUTPUT_PREFIX -r ${INIT_OUTPUT_PREFIX}0GenericAffine.mat -x ${PIECEWISE_DILATED_MASKS[$j]}"
registrationCallPiecewise="$registrationCallPiecewise -t Rigid[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x100x50,1e-6,10 ] -f 6x4x3 -s 3x2x1"
registrationCallPiecewise="$registrationCallPiecewise -t Similarity[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x100x50,1e-6,10 ] -f 6x4x3 -s 3x2x1"
registrationCallPiecewise="$registrationCallPiecewise -t Affine[ 0.15 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x100x50,1e-6,10 ] -f 6x4x3 -s 3x2x1"
registrationCallPiecewise="$registrationCallPiecewise -t BSplineSyN[ 0.15,40,0 ] -m MI[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,32,Regular,0.25 ] -c [ 100x40x0,1e-6,10 ] -f 6x4x1 -s 3x2x0 >> $logFile"
echo "$registrationCallPiecewise" >> $qscript
composeCallPiecewise="$WARP \
-d ${DIM} \
-v 1 \
-o [ ${PIECEWISE_OUTPUT_PREFIX}TotalWarp.nii.gz,1 ] \
-r ${ATLAS_IMAGES[$i]} \
-t ${PIECEWISE_OUTPUT_PREFIX}4Warp.nii.gz \
-t ${PIECEWISE_OUTPUT_PREFIX}3Affine.mat \
-t ${PIECEWISE_OUTPUT_PREFIX}2Similarity.mat \
-t ${PIECEWISE_OUTPUT_PREFIX}1Rigid.mat >> $logFile"
echo "$composeCallPiecewise" >> $qscript
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}1Rigid.mat )
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}2Similarity.mat )
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}3Affine.mat )
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}4Warp.nii.gz )
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}4InverseWarp.nii.gz )
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}TotalWarp.nii.gz )
componentCall="ConvertImage ${DIM} ${PIECEWISE_OUTPUT_PREFIX}TotalWarp.nii.gz ${PIECEWISE_OUTPUT_PREFIX}TotalWarp 10"
echo "$componentCall" >> $qscript
components=( 'xvec' 'yvec' 'zvec' )
for (( d = 0; d < ${DIM}; d++ ))
do
maskingCall="ImageMath 3 ${PIECEWISE_OUTPUT_PREFIX}TotalWarp${components[$d]}.nii.gz m ${PIECEWISE_OUTPUT_PREFIX}TotalWarp${components[$d]}.nii.gz ${PIECEWISE_MASKS[$j]}"
echo "$maskingCall" >> $qscript
if [[ ${ISOTROPIC_RESAMPLING} != 'NA' ]];
then
resampleCall="ResampleImage 3 ${PIECEWISE_OUTPUT_PREFIX}TotalWarp${components[$d]}.nii.gz ${PIECEWISE_OUTPUT_PREFIX}TotalWarp${components[$d]}.nii.gz $ISOTROPIC_RESAMPLING_VECTOR 0 0"
echo "$resampleCall" >> $qscript
fi
TMP_FILES=( ${TMP_FILES[@]} ${PIECEWISE_OUTPUT_PREFIX}TotalWarp${components[$d]}.nii.gz )
done
componentCall="ConvertImage ${DIM} ${PIECEWISE_OUTPUT_PREFIX}TotalWarp ${PIECEWISE_OUTPUT_PREFIX}TotalWarp.nii.gz 9"
echo "$componentCall" >> $qscript
PIECEWISE_TRANSFORM_WARPS="${PIECEWISE_TRANSFORM_WARPS} -t ${PIECEWISE_OUTPUT_PREFIX}TotalWarp.nii.gz"
done
# Compose all the piecewise warps
composeCall="${WARP} -d ${DIM} -v 1 \
-o [ ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz,1 ] \
-r ${SUBSET_CONFIDENCE_MASK} \
${PIECEWISE_TRANSFORM_WARPS} >> $logFile"
echo "$composeCall" >> $qscript
# Smooth the displacement field and estimate the inverse
smoothCall="SmoothDisplacementField ${DIM} ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz 4x4x4 5 3 0 ${SUBSET_CONFIDENCE_MASK} >> $logFile"
echo "$smoothCall" >> $qscript
inverseSmoothCall="SmoothDisplacementField ${DIM} ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz 4x4x4 5 3 1 >> $logFile"
echo "$inverseSmoothCall" >> $qscript
# Resample to the full size
xfrmForwardCall="${WARP} -d ${DIM} -v 1 \
-o [ ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz,1 ] \
-r ${ATLAS_IMAGES[$i]} \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz >> $logFile"
echo "$xfrmForwardCall" >> $qscript
xfrmInverseCall="${WARP} -d ${DIM} -v 1 \
-o [ ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz,1 ] \
-r ${ATLAS_IMAGES[$i]} \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz >> $logFile"
echo "$xfrmInverseCall" >> $qscript
# Now do a more refined registration
registrationCallRefined="antsRegistration -d 3 -v 1 -z 0 -o ${OUTPUT_PREFIX}${BASENAME}_${i}_ -r ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz -r ${INIT_OUTPUT_PREFIX}0GenericAffine.mat -x ${CONFIDENCE_DILATED_MASK}"
registrationCallRefined="$registrationCallRefined -t BSplineSyN[ 0.15,40,0 ] -m CC[ ${ATLAS_IMAGES[$i]},${TARGET_IMAGE},1,2 ] -c [ 100x40x0,1e-6,10 ] -f 6x4x1 -s 2x1x0 >> $logFile"
echo "$registrationCallRefined" >> $qscript
atlasXfrmCall="antsApplyTransforms \
-d ${DIM} \
--float 1 \
-i ${ATLAS_IMAGES[$i]} \
-r ${TARGET_IMAGE} \
-o ${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz \
-n Linear \
-t [ ${INIT_OUTPUT_PREFIX}0GenericAffine.mat,1 ] \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_2InverseWarp.nii.gz >> ${OUTPUT_PREFIX}${BASENAME}_${i}_log.txt"
echo "$atlasXfrmCall" >> $qscript
labelXfrmCall="antsApplyTransforms \
-d ${DIM} \
--float 1 \
-i ${ATLAS_LABELS[$i]} \
-r ${TARGET_IMAGE} \
-o ${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz \
-n GenericLabel \
-t [ ${INIT_OUTPUT_PREFIX}0GenericAffine.mat,1 ] \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz \
-t ${OUTPUT_PREFIX}${BASENAME}_${i}_2InverseWarp.nii.gz >> ${OUTPUT_PREFIX}${BASENAME}_${i}_log.txt"
echo "$labelXfrmCall" >> $qscript
TMP_FILES=( ${TMP_FILES[@]} ${INIT_OUTPUT_PREFIX}0GenericAffine.mat )
TMP_FILES=( ${TMP_FILES[@]} ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalWarp.nii.gz ${OUTPUT_PREFIX}${BASENAME}_${i}_TotalInverseWarp.nii.gz )
TMP_FILES=( ${TMP_FILES[@]} ${OUTPUT_PREFIX}${BASENAME}_${i}_2Warp.nii.gz ${OUTPUT_PREFIX}${BASENAME}_${i}_2InverseWarp.nii.gz )
copyImageHeaderCall="CopyImageHeaderInformation \
${TARGET_IMAGE} \
${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz \
${OUTPUT_PREFIX}${BASENAME}_${i}_Warped.nii.gz 1 1 1"
echo "$copyImageHeaderCall" >> $qscript
copyLabelsHeaderCall="CopyImageHeaderInformation \
${TARGET_IMAGE} \
${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz \
${OUTPUT_PREFIX}${BASENAME}_${i}_WarpedLabels.nii.gz 1 1 1"
echo "$copyLabelsHeaderCall" >> $qscript
if [[ $KEEP_ALL_IMAGES -eq 0 ]];
then
for f in ${TMP_FILES[@]}
do
echo "rm -f $f" >> $qscript
done
fi
fi
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_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 -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
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
sh $qscript2
fi
if [[ $DOQSUB -eq 3 ]];
then
# Run jobs on XGrid and wait to finish
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF on XGrid cluster. Submitted $count jobs "
echo "--------------------------------------------------------------------------------------"
waitForXGridJobs.pl -xgridflags "$XGRID_OPTS" -verbose -delay 30 $jobIDs
# Returns 1 if there are errors
if [[ ! $? -eq 0 ]];
then
echo "XGrid 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
sh $qscript2
fi
if [[ $DOQSUB -eq 5 ]];
then
# Run jobs on SLURM and wait to finish
echo
echo "--------------------------------------------------------------------------------------"
echo " Starting JLF on SLURM cluster. "
echo "--------------------------------------------------------------------------------------"
waitForSlurmJobs.pl 1 600 $jobIDs
# Returns 1 if there are errors
if [[ ! $? -eq 0 ]];
then
echo "SLURM submission failed - jobs went into error state"
exit 1;
fi
# Remove the SLURM output files (which are likely to be empty)
rm -f ${OUTPUT_DIR}/slurm-*.out
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 "#!/bin/sh" > $qscript2
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=`sbatch --job-name=antsJlf $QSUB_OPTS --nodes=1 --cpus-per-task=1 --time=${JLF_WALLTIME} --mem=${JLF_MEMORY} $qscript2 | rev | cut -f1 -d\ | rev`
waitForSlurmJobs.pl 1 600 $jobIDs
fi
# clean up
rm -f ${OUTPUT_DIR}/job_*.sh
if [[ $KEEP_ALL_IMAGES -eq 0 ]];
then
rm -f ${WARPED_ATLAS_IMAGES[@]}
rm -f ${INVERSE_WARPED_ATLAS_IMAGES[@]}
rm -f ${WARPED_ATLAS_LABELS[@]}
rm -f ${AFFINE_FILES[@]}
rm -f ${WARP_FIELDS[@]}
rm -f ${INVERSE_WARP_FIELDS[@]}
rm -f $qscript
rm -f $qscript2
rm -f ${OUTPUT_DIR}/slurm-*.out
fi
time_end=`date +%s`
time_elapsed=$((time_end - time_start))
echo
echo "--------------------------------------------------------------------------------------"
if [[ $MAJORITYVOTE -eq 1 ]];
then
echo " Done creating: ${OUTPUT_PREFIX}MajorityVotingLabels.nii.gz"
else
echo " Done creating: ${OUTPUT_PREFIX}Labels.nii.gz"
fi
echo " Script executed in $time_elapsed seconds"
echo " $(( time_elapsed / 3600 ))h $(( time_elapsed %3600 / 60 ))m $(( time_elapsed % 60 ))s"
echo "--------------------------------------------------------------------------------------"
exit 0
|