1#!/bin/tcsh 2 3@global_parse `basename $0` "$*" ; if ($status) exit 0 4 5# make surface spec files from the surface files 6# 7# usage @SUMA_Make_Spec_FS [options] 8# 9# options: 10# -sid subject_id : specify subject ID 11# -fspath FreeSurfer_path : specify path to FreeSurfer files 12# -neuro : use neurological orientation 13# -debug level : display extra output 14# 15 16# [Dec 30, 2016]: PT+RCR updated. 17# + new renumbering of aparc/aseg files for both 2000 and 2009 18# atlases 19# + replaces functionality of 'rank' data sets 20# + produce tissue maps based on hopefully good guesses of FS 21# labeling 22# [Aug 20, 2019] PT : indent properly, for readability 23# [Aug 20, 2019] PT : update help 24# [Sep 4, 2019] RCR: check for valid 'mris_convert --help' output 25# [Sep 4, 2019] PT : add in new opt: '-extra_fs_dsets ..' 26# + so more $fsdsets can be created with surfs (for F. Lalonde) 27# [Nov 14, 2019] PT : new default: *rank* dsets will not be output 28# + have new option to create rank dsets: -make_rank_dsets 29# [Nov 19, 2019] RCR: new option: -fs_setup 30# [Jan 27, 2020] RCR: compress *.nii, but not SV, new opt: -no_compress 31# [Feb 3, 2020] RCR: add -extra_annot_labels 32# [Feb 27, 2020] RCR: add -fsannot_ver (for older version of files) 33# + also, -extra_annot_labels output is passed on to -extra_fs_dsets 34# [Mar 5, 2020] RCR/DRG: for extra annot's, pass along without colormap 35# + use mris_convert/3dcopy for now, instead of FSread_annot/ConvertDset 36# [Jan 27, 2021] PT : add another driving script in L_MAKE_DRIVE_SCRIPT block 37# + has both AFNI+SUMA run, plus talk 38# [Mar 3, 2021] PT : add some functionality in new L_MASK_QC_ROIS block 39# + make a nice WB mask 40# + make some QC images of mask(s), segmentations and ROIs 41# + make some tables of ROI info 42# [May 12, 2021] PT : remove old NIFTI message about needing even matrix 43# dimensions for anatomical input to recon-all. Yikes, that wasn't 44# need, just the way we were viewing it... 45 46set ver = 2.2.4 47 48#---------------------------------------------------------------------- 49goto L_INIT_VARS 50 L_INIT_VARS_DONE: 51 52goto L_CHECK_USAGE 53 L_CHECK_USAGE_DONE: 54 55goto L_PARSE_COMMAND 56 L_PARSE_COMMAND_DONE: 57 58goto L_VERIFY_PROGRAMS 59 L_VERIFY_PROGRAMS_DONE: 60 61goto L_SET_SURF_DIRS 62 L_SET_SURF_DIRS_DONE: 63 64goto L_CHECK_FOR_OVERWRITE 65 L_CHECK_FOR_OVERWRITE_DONE: 66 67goto L_LOOK_FOR_SURF 68 L_LOOK_FOR_SURF_DONE: 69 70goto L_CREATE_BRICK 71 L_CREATE_BRICK_DONE: 72 73goto L_CONVERT_SURFACES 74 L_CONVERT_SURFACES_DONE: 75 76goto L_CREATE_SPEC 77 L_CREATE_SPEC_DONE: 78 79goto L_ICO 80 L_ICO_DONE: 81 82goto L_TEST_SURF_VOL 83 L_TEST_SURF_VOL_DONE: 84 85# PT added [Jan 27, 2021] 86goto L_MAKE_DRIVE_SCRIPT 87 L_MAKE_DRIVE_SCRIPT_DONE: 88 89# PT added [Dec 30, 2016] 90goto L_RENUMBER_ROIS 91 L_RENUMBER_ROIS_DONE: 92 93# PT added [Mar 3, 2021] 94goto L_MASK_QC_ROIS 95 L_MASK_QC_ROIS_DONE: 96 97# [27 Jan 2020 rickr] 98goto L_COMPRESS_FILES 99 L_COMPRESS_FILES_DONE: 100 101goto L_GOOD_END # finished, woohooo! 102 103 104#################################################################### 105# variable initialization 106 107L_INIT_VARS: 108 109 set DO_MAKE_RANK_DSETS = 0 # [PT: Nov 14, 2019] the new normal 110 set USING_NIFTI = 0 111 set prog_name = $0:t 112 set endstr = "$prog_name ... finished" 113 set debug = 0 114 set ldlist = (141 60) 115 set do_compress = 1 116 #set fsdsets = (thickness.gii.dset) 117 #set fsdsets = (thickness curv sulc $fsdsets_extra ) 118 set inflates = () 119 120 set surf_attribs = ( \ 121 smoothwm \ 122 pial \ 123 inflated \ 124 occip.patch.3d \ 125 occip.patch.flat \ 126 occip.flat.patch.3d \ 127 fusiform.patch.flat \ 128 full.patch.3d \ 129 full.patch.flat \ 130 full.flat.patch.3d \ 131 full.flat \ 132 flat.patch \ 133 sphere \ 134 white \ 135 sphere.reg \ 136 rh.sphere.reg \ 137 lh.sphere.reg \ 138 pial-outer-smoothed \ 139 ) 140 141goto L_INIT_VARS_DONE 142 143 144#################################################################### 145# check usage, and possibly print help 146 147L_CHECK_USAGE: 148 149 if ( $#argv == 0 ) then 150 # show help on no args 28 Feb 2017 [rickr] 151 152 # echo "usage: $prog_name [options] -sid SUBJECT_ID" 153 # echo "usage: $prog_name -help" 154 # set endstr = "" 155 goto L_HELP_END 156 endif 157 158goto L_CHECK_USAGE_DONE 159 160 161#################################################################### 162# parse the command line 163 164L_PARSE_COMMAND: 165 166 echo "++ Running @SUMA_Make_Spec_FS version: $ver" 167 168 # init command line arg values 169 170 set fs_dir = "." 171 set subj_id = "" 172 set neuro_ori = 0 173 set sfieldname = 'FreeSurferSurface' 174 set sex = 'asc' 175 set stp = 'FreeSurfer' 176 set ldu = () 177 set ldpref = () 178 set use_mgz = 0 179 set set_space = '' 180 set args = $#argv 181 set count = 1 182 set extra_annot_labels = ( ) # 3 Feb 2020 [rickr] 183 set fsdsets_extra = ( ) 184 set fsannot_ver = "" # 27 Feb 2020 [rickr] 185 186 while ( $count <= $args ) 187 switch ( "$argv[$count]" ) 188 189 # ---------------------------------------------------------- 190 # usage: -help 191 case "-h": 192 case "-help": 193 194 goto L_HELP_END # and don't ya' come back, neither 195 196 breaksw 197 198 # ---------------------------------------------------------- 199 # usage: -sid SUBJECT_ID 200 case "-sid": 201 202 if ( $count >= $args ) then 203 set endstr = "arg usage: -sid SUBJECT_ID" 204 goto L_BAD_END 205 endif 206 207 @ count ++ 208 set subj_id = $argv[$count] 209 210 breaksw 211 212 # ---------------------------------------------------------- 213 # usage: -fsannot_ver YEAR (e.g. 2009) 214 case "-fsannot_ver": 215 216 if ( $count >= $args ) then 217 set endstr = "arg usage: -fsannot_ver YEAR" 218 goto L_BAD_END 219 endif 220 221 @ count ++ 222 set fsannot_ver = $argv[$count] 223 224 breaksw 225 226 # ---------------------------------------------------------- 227 # usage: -fspath FREESURFER_PATH 228 case "-fspath": 229 230 if ( $count >= $args ) then 231 set endstr = "arg usage: -fspath FREESURFER_PATH" 232 goto L_BAD_END 233 endif 234 235 @ count ++ 236 set fs_dir = $argv[$count] 237 238 if ( ! -d $fs_dir ) then 239 set endstr = "failure: directory not found - '$fs_dir'" 240 goto L_BAD_END 241 endif 242 243 breaksw 244 245 # ---------------------------------------------------------- 246 # usage: -fs_setup : source SetUpFreeSurfer.csh 247 248 case "-fs_setup": 249 250 # if $FREESURFER_HOME is set 251 # and $FREESURFER_HOME/SetUpFreeSurfer.csh exists, 252 # source it 253 254 if ( ! $?FREESURFER_HOME ) then 255 echo '** -fs_setup requires $FREESURFER_HOME to be set' 256 set endstr = "failure: cannot apply -fs_setup" 257 goto L_BAD_END 258 endif 259 260 set fs_setupstr = '$FREESURFER_HOME/SetUpFreeSurfer.csh' 261 set fs_setupfile = $FREESURFER_HOME/SetUpFreeSurfer.csh 262 263 if ( ! -f $fs_setupfile ) then 264 echo "** missing $fs_setupstr" 265 set endstr = "failure: cannot apply -fs_setup" 266 goto L_BAD_END 267 endif 268 269 # do the deed 270 echo "++ sourcing $fs_setupstr" 271 source $fs_setupfile 272 273 breaksw 274 275 # ---------------------------------------------------------- 276 # usage : -neuro 277 case "-neuro": 278 279 # do not allow overlapping options 15 Jun 2017 [rickr] 280 if ( $neuro_ori != 0 ) then 281 echo '** cannot use any -*IFTI options with -neuro' 282 goto L_BAD_END 283 endif 284 285 set neuro_ori = 1 286 breaksw 287 288 # ---------------------------------------------------------- 289 # usage : -no_compress 27 Jan 2020 [rickr] 290 case "-no_compress": 291 292 # then turn off compression 293 set do_compress = 0 294 breaksw 295 296 # ---------------------------------------------------------- 297 # usage : -nocor 298 case "-nocor": 299 set endstr = "Option -nocor obsolete. " 300 set endstr = "$endstr See -help for important details." 301 goto L_BAD_END 302 303 #set neuro_ori = -1 304 #set sfieldname = 'SurfaceName' 305 #set sex = 'gii' 306 #set stp = 'GIFTI' 307 308 breaksw 309 310 # ---------------------------------------------------------- 311 # usage : -GNIFTI 312 case "-GNIFTI": 313 case "-NIFTI": 314 case "-GIFTI": 315 case "-IFTI": 316 317 # do not allow overlapping options 15 Jun 2017 [rickr] 318 if ( $neuro_ori != 0 ) then 319 echo '** cannot use any -*IFTI options with -neuro' 320 goto L_BAD_END 321 endif 322 323 set neuro_ori = -1 324 set sfieldname = 'SurfaceName' 325 set sex = 'gii' 326 set stp = 'GIFTI' 327 set USING_NIFTI = 1 # [PT: Feb,2017] really useful... 328 329 breaksw 330 331 # ---------------------------------------------------------- 332 # [PT: Nov 14, 2019] the *rank* dsets have been superceded 333 # by the safer/more uniform *REN* ones; now only make the former 334 # if users explicitly ask for it 335 # usage : -make_rank_dsets 336 case "-make_rank_dsets": 337 338 set DO_MAKE_RANK_DSETS = 1 339 340 breaksw 341 342 # ---------------------------------------------------------- 343 # usage : -set_space SPACE 344 case "-set_space": 345 346 if ( $count >= $args ) then 347 set endstr = "arg usage: -set_space SPACE" 348 goto L_BAD_END 349 endif 350 351 @ count ++ 352 set set_space = $argv[$count] 353 354 breaksw 355 356 # ---------------------------------------------------------- 357 # usage : -neuro 358 case "-use_mgz": 359 360 set use_mgz = 1 361 362 breaksw 363 364 # ---------------------------------------------------------- 365 # usage : -debug DEBUG_LEVEL 366 case "-debug": 367 368 if ( $count >= $args ) then 369 set endstr = "arg usage: -debug DEBUG_LEVEL" 370 goto L_BAD_END 371 endif 372 373 @ count ++ 374 set debug = $argv[$count] 375 376 if ( "$debug" > 2 ) then 377 set debug = 2 378 endif 379 if ( "$debug" < 0 ) then 380 set debug = 0 381 endif 382 383 breaksw 384 385 # ---------------------------------------------------------- 386 # usage : -inflate VAL 387 case "-inflate": 388 389 if ( $count >= $args ) then 390 set endstr = "arg usage: -inflate INF" 391 goto L_BAD_END 392 endif 393 394 @ count ++ 395 set ff = 0 396 foreach inf ($inflates) 397 if ($inf == "$argv[$count]") then 398 set ff = 1 399 endif 400 end 401 if ($ff == 0) then 402 set inflates = ($inflates $argv[$count]) 403 endif 404 405 breaksw 406 407 408 # ---------------------------------------------------------- 409 # usage : -extra_annot_labels AA BB CC ... 410 case "-extra_annot_labels": 411 412 if ( $count >= $args ) then 413 set endstr = "arg usage: -extra_annot_labels AA BB CC ..." 414 goto L_BAD_END 415 endif 416 417 @ i0 = $count + 1 # $i0 will always lead $count by one 418 419 # keep track of first char of next arg (not '-') 420 set c0 = `echo $argv[$i0] | cut -b 1` 421 422 while ( ( $i0 <= $#argv ) && ( "$c0" != "-" ) ) 423 set extra_annot_labels = ($extra_annot_labels "$argv[$i0]") 424 425 @ i0 ++ 426 @ count ++ 427 if ( $i0 <= $#argv ) then 428 # get first char of next arg, to be check in while() 429 set c0 = `echo $argv[$i0] | cut -b 1` 430 endif 431 end 432 433 if ( $#extra_annot_labels == 0 ) then 434 echo "** ERROR: need at least one dset after $argv[$count]" 435 goto BAD_EXIT 436 else 437 echo "++ will track $#extra_annot_labels extra annot labels" 438 endif 439 440 breaksw 441 442 # ---------------------------------------------------------- 443 # usage : -extra_fs_dsets AA BB CC ... 444 case "-extra_fs_dsets": 445 446 if ( $count >= $args ) then 447 set endstr = "arg usage: -extra_fs_dsets AA BB CC ..." 448 goto L_BAD_END 449 endif 450 451 @ i0 = $count + 1 # $i0 will always lead $count by one 452 set c0 = "" 453 while ( ( $i0 <= $#argv ) && ( "$c0" != "-" ) ) 454 set fsdsets_extra = ( $fsdsets_extra "$argv[$i0]" ) 455 456 @ i0 ++ 457 @ count ++ 458 if ( $i0 <= $#argv ) then 459 # get first char of next arg, to be check in while() 460 set c0 = `echo $argv[$i0] \ 461 | awk '{print substr($0,1,1)}'` 462 endif 463 end 464 465 if ( $#fsdsets_extra == 0 ) then 466 echo "** ERROR: need at least one dset after $argv[$count]" 467 goto BAD_EXIT 468 else 469 echo "++ User has listed $#fsdsets_extra 'extra' FS dsets" 470 endif 471 472 breaksw 473 474 # ---------------------------------------------------------- 475 # usage : -ld VAL 476 case "-ld": 477 478 if ( $count >= $args ) then 479 set endstr = "arg usage: -ld LD" 480 goto L_BAD_END 481 endif 482 483 @ count ++ 484 set ldu = ($ldu $argv[$count]) 485 486 breaksw 487 488 # ---------------------------------------------------------- 489 # usage : -ldpref LDpref 490 case "-ldpref": 491 492 if ( $count >= $args ) then 493 set endstr = "arg usage: -ldpref LDpref" 494 goto L_BAD_END 495 endif 496 497 @ count ++ 498 set ldpref = ($ldpref $argv[$count]) 499 500 breaksw 501 502 # ---------------------------------------------------------- 503 # usage : -no_ld 504 case "-no_ld": 505 506 set ldlist = () 507 508 breaksw 509 510 511 # ---------------------------------------------------------- 512 # bad argument 513 default: 514 515 set endstr = "invalid option: '$argv[$count]'" 516 apsearch -popt `basename $0` -word $argv[$count] 517 goto L_BAD_END 518 519 breaksw 520 endsw 521 522 @ count ++ 523 end 524 525 if ( $subj_id == "" ) then 526 set endstr = "missing required option: -sid" 527 goto L_BAD_END 528 endif 529 530 531 # [PT: Sep 4, 2019] Put this array definition here, to allow users 532 # to input other dsets from surf/ dir 533 set fsdsets = (thickness curv sulc $fsdsets_extra ) 534 535 if ($#inflates == 0 && $neuro_ori == -1) then 536 set inflates = ($inflates 200) 537 endif 538 539 if ( $debug ) echo "-- usage okay" 540 if ( $debug > 1 ) then 541 set echo 542 endif 543 if ( $#ldu ) then 544 set ldlist = ($ldu) 545 endif 546 if ($#ldpref == 0 && $#ldlist != 0) then 547 set ldpref = () 548 foreach lldd ($ldlist) 549 set ldpref = ($ldpref std.$lldd.) 550 end 551 endif 552 if ($#ldpref != 0 && $#ldpref != $#ldlist) then 553 set endstr = "Not enough -ldpref options for $#ldlist values" 554 goto L_BAD_END 555 endif 556 557 set spec_files = ({$subj_id}_lh.spec {$subj_id}_rh.spec ) 558 set sample_spec = $spec_files[1] 559 set afni_prefix = ${subj_id}_SurfVol 560 if ( $neuro_ori != -1 ) then 561 set afni_dataset = $afni_prefix+orig 562 else 563 set afni_dataset = $afni_prefix 564 endif 565 # set and go to the base directory 566 cd $fs_dir 567 set start_dir = $cwd 568 569 if ( $debug ) echo "-- using start_dir '$start_dir'" 570 571goto L_PARSE_COMMAND_DONE 572 573 574#################################################################### 575# make sure programs exist 576 577L_VERIFY_PROGRAMS: 578 579 set failed_pgms = 0 580 581 # check that programs are in the path 582 foreach test_command ( afni to3d suma mris_convert ) 583 584 (which $test_command) >& /dev/null 585 586 if ( $status ) then 587 echo "program not found in path: $test_command" 588 @ failed_pgms ++ 589 continue 590 endif 591 592 # $test_command was found, but maybe additional checks are warranted 593 594 if ( $test_command == mris_convert ) then 595 # Unfortunately, all -help variants seem to set $status, 596 # so parse the actual text, looking for 'surface'. 597 set nl = `$test_command --help |& \grep surface | wc -l` 598 if ( "$nl" == 0 ) then 599 echo "** apparent failure to run $test_command" 600 echo " $test_command --help starts with:" 601 echo "" 602 $test_command --help |& head -n 10 603 echo "" 604 echo "---------------------------------------------------------" 605 echo "** if this requires SetUpFreeSurfer.csh, consider option:" 606 echo " -fs_setup" 607 echo "" 608 @ failed_pgms ++ 609 continue # in case more checks are added 610 endif 611 endif 612 end 613 614 if ( $failed_pgms ) then 615 set endstr = "$failed_pgms program(s) not found" 616 goto L_BAD_END 617 endif 618 619 # check that a couple of commands actually work 620 foreach test_command ( afni mris_convert ) 621 $test_command --version >& /dev/null 622 623 if ( $status ) then 624 echo "program failure: $test_command" 625 @ failed_pgms ++ 626 endif 627 end 628 629 if ( $failed_pgms ) then 630 set endstr = "$failed_pgms program(s) not working correctly" 631 goto L_BAD_END 632 endif 633 634 if ( $debug ) echo "-- all programs found" 635 636goto L_VERIFY_PROGRAMS_DONE 637 638 639#################################################################### 640# 1. set surf_dir and orig_dir - check cwd and parent 641# 2. create SUMA directory at the same level as surf_dir and store the 642# results there 643 644 645L_SET_SURF_DIRS: 646 647 # find surf directory 648 649 if ( -d surf ) then 650 set surf_dir = ./surf 651 set label_dir = ./label 652 else if ( $cwd:t == surf ) then 653 set surf_dir = . 654 set label_dir = ../label 655 else if ( -d ../surf ) then 656 set surf_dir = ../surf 657 set label_dir = ../label 658 else 659 # this is a general failure case, even if we find one 660 set surf_dirs = ( `find . -maxdepth 4 -type d -name surf -print` ) 661 662 if ( $#surf_dirs == 0 ) then 663 echo "failure: cannot find directory 'surf' under '$fs_dir'" 664 echo "(subject to a maximum search depth of 4 subdirectories)" 665 666 set endstr = "" 667 else if ( $#surf_dirs == 1 ) then 668 echo "surf directory found at '$surf_dirs[1]'" 669 set endstr = "consider running program from '$surf_dirs[1]:h'" 670 else 671 echo "multiple surf directories found:" 672 set count = 1 673 while ( $count <= $#surf_dirs ) 674 echo " $surf_dirs[$count]" 675 @ count ++ 676 end 677 678 set endstr = ( "consider running program from one of the" \ 679 "surf directories" ) 680 endif 681 682 goto L_BAD_END 683 endif 684 685 # verify surf dir permissions 686 if ( ! -w $surf_dir ) then 687 set endstr = "failure: no write permissions for directory '$surf_dir'" 688 goto L_BAD_END 689 endif 690 691 if ( $debug ) echo "-- using surf directory '$surf_dir'..." 692 693 # now check for orig dir 694 695 set orig_dir = "" 696 foreach test_dir ( $surf_dir/orig \ 697 $surf_dir/mri/orig \ 698 orig \ 699 mri/orig \ 700 ../orig \ 701 ../mri/orig ) 702 if ( -d $test_dir ) then 703 set orig_dir = $test_dir 704 break 705 endif 706 end 707 708 set orig_mgz = "" 709 foreach test_mgz ( $surf_dir/orig.mgz \ 710 $surf_dir/mri/orig.mgz \ 711 orig.mgz \ 712 mri/orig.mgz \ 713 ../orig.mgz \ 714 ../mri/orig.mgz ) 715 if ( -f $test_mgz ) then 716 set orig_mgz = $test_mgz 717 break 718 endif 719 end 720 721 set other_mgz = () 722 set other_candidates = ( T1 aparc+aseg aparc.a2005s+aseg \ 723 aparc.a2009s+aseg aseg.auto aseg \ 724 brain.finalsurfs brain brainmask.auto \ 725 brainmask norm nu nu_noneck lh.ribbon \ 726 rh.ribbon wm.asegedit wm wm.seg ) 727 728 foreach candidate ($other_candidates) 729 foreach dcand ($surf_dir/ $surf_dir/mri/ ./ mri/ ../ ../mri/ ) 730 if ( -f $dcand$candidate.mgz ) then 731 set other_mgz = ($other_mgz $dcand$candidate.mgz) 732 break 733 endif 734 end 735 end 736 737 if ( $orig_dir == "" && $orig_mgz == "") then 738 set endstr = "failure: cannot find directory 'orig' or file 'orig.mgz' under $fs_dir" 739 goto L_BAD_END 740 endif 741 742 if ( $orig_dir == "" ) then 743 set endstr = "failure: script expects an 'orig' directory even if it is empty." 744 goto L_BAD_END 745 endif 746 747 # verify orig dir permissions 748 if ( ! -w $orig_dir ) then 749 set endstr = "failure: no write permissions for directory '$orig_dir'" 750 goto L_BAD_END 751 endif 752 753 if ( $debug ) echo "-- using orig directory '$orig_dir'..." 754 755 # decide whether we need to make surf/SUMA directory 756 757 set suma_dir = ./SUMA 758 759 if ( -d $suma_dir ) then 760 if ( $debug ) echo "-- $suma_dir already exists, continuing..." 761 else 762 echo "++ creating directory '$suma_dir' for results..." 763 mkdir $suma_dir 764 if ( $status ) then 765 set endstr = "failure: cannot create directory '$suma_dir'" 766 goto L_BAD_END 767 endif 768 endif 769 770goto L_SET_SURF_DIRS_DONE 771 772 773#################################################################### 774# verify non-existence of spec files and AFNI files 775 776L_CHECK_FOR_OVERWRITE: 777 778 set test_failures = 0 779 780 foreach test_file ( $suma_dir/$spec_files[1] \ 781 $suma_dir/$spec_files[2] \ 782 $suma_dir/$afni_dataset.HEAD \ 783 $suma_dir/$afni_dataset.BRIK \ 784 $orig_dir/$afni_dataset.HEAD \ 785 $orig_dir/$afni_dataset.BRIK \ 786 $orig_dir/$afni_prefix'+orig.HEAD' \ 787 $orig_dir/$afni_prefix'+orig.BRIK' \ 788 $suma_dir/$afni_prefix'+orig.HEAD' \ 789 $suma_dir/$afni_prefix'+orig.BRIK' \ 790 $suma_dir/$afni_prefix'.nii' \ 791 $suma_dir/$afni_prefix'.nii.gz') 792 if ( -f $test_file ) then 793 if ( $test_failures == 0 ) then 794 echo "failure: will not overwrite files: " 795 set test_failures = 1 796 endif 797 798 echo " '$test_file'" 799 endif 800 end 801 802 if ( $test_failures ) then 803 set endstr = "please remove these files if you want to rerun the script." 804 goto L_BAD_END 805 endif 806 807goto L_CHECK_FOR_OVERWRITE_DONE 808 809#################################################################### 810# find surface files 811 812L_LOOK_FOR_SURF: 813 814 cd $surf_dir 815 816 set list_lh = "X" # init to something useless - allows nice empty check 817 set list_rh = "X" 818 819 foreach attrib ( $surf_attribs ) 820 if ( -f lh.$attrib ) then 821 set list_lh = ( $list_lh lh.$attrib ) 822 endif 823 if ( -f rh.$attrib ) then 824 set list_rh = ( $list_rh rh.$attrib ) 825 endif 826 end 827 828 set list_lh = ( $list_lh[2-] ) # now remove the leading "X" 829 set list_rh = ( $list_rh[2-] ) 830 831 if ( $#list_lh == 0 && $#list_rh == 0 ) then 832 set endstr = "found no LH or RH surface files under '$surf_dir'" 833 goto L_BAD_END 834 endif 835 836 if ( $#list_lh > 0 ) then 837 echo "-- found $#list_lh LH surfaces" 838 if ( $debug ) echo " --" $list_lh 839 endif 840 841 if ( $#list_rh > 0 ) then 842 echo "-- found $#list_rh RH surfaces" 843 if ( $debug ) echo " --" $list_rh 844 endif 845 846 cd $start_dir 847 848goto L_LOOK_FOR_SURF_DONE 849 850 851#################################################################### 852# convert to ascii via mris_convert (found earlier) 853 854L_CONVERT_SURFACES: 855 856 cd $surf_dir 857 foreach surf ( $list_lh $list_rh ) 858 # run mris_convert and verify 859 if ( $surf =~ *patch* || $surf =~ *.flat) then 860 echo "-- running 'mris_convert -p $surf $surf.asc'" 861 mris_convert -p $surf $surf.asc 862 else 863 echo "-- running 'mris_convert $surf $surf.asc'" 864 mris_convert $surf $surf.asc >& ___out 865 tail ___out ; \rm -f ___out 866 endif 867 868 if ( $neuro_ori == -1 ) then 869 if ( ! -f xmatras.1D ) then 870 set c_ras = (`mri_info --cras --ras_good $start_dir/$orig_mgz`) 871 set minus = `ccalc "$c_ras[3]*-1"` 872 echo 1 0 0 $c_ras[1] 0 1 0 $c_ras[2] 0 0 1 $minus > xmatras.1D 873 endif 874 #If using new conversion, change surfaces to gifti with proper 875 #coordinate shift. 876 if ( $surf =~ *.white* || \ 877 $surf =~ *.smoothwm* || \ 878 $surf =~ *.pial*) then 879 # hmmm, identical commands suggest the second 880 # -ixmat_1D does not belong (currently left for future 881 # bickering) 882 ConvertSurface \ 883 -sv $start_dir/$suma_dir/$afni_dataset.nii \ 884 -i $surf.asc \ 885 -ixmat_1D xmatras.1D -overwrite -o $surf.gii 886 else 887 ConvertSurface \ 888 -sv $start_dir/$suma_dir/$afni_dataset.nii \ 889 -i $surf.asc \ 890 -ixmat_1D xmatras.1D -overwrite -o $surf.gii 891 endif 892 endif 893 894 if ( ! -f $surf.${sex} ) then 895 echo "failure: could not create surface $surf.${sex}" 896 897 if ( ! -w . ) then 898 set endstr = "--> no write permissions in this directory" 899 else 900 set endstr = "--> is your FreeSurfer license installed?" 901 endif 902 903 goto L_BAD_END 904 endif 905 906 if ( $debug ) then 907 echo "++ $surf.${sex} created" 908 if ( -f $start_dir/$suma_dir/$surf.${sex} ) then 909 echo "++ overwriting '$suma_dir/$surf.${sex}'" 910 endif 911 endif 912 913 #tmp fix for rh.lh.sphere.reg.gii side 914 if ( "$surf.${sex}" == rh.lh.sphere.reg.gii) then 915 echo "++ Fixing side for rh.lh.sphere.reg.gii" 916 sed 's:CortexLeft:CortexRight:' rh.lh.sphere.reg.gii > __tmpfix 917 if ($status) then 918 echo "** Failed to fix side for rh.lh.sphere.reg.gii" 919 else 920 mv __tmpfix rh.lh.sphere.reg.gii 921 endif 922 endif 923 924 \mv -f $surf.${sex} $start_dir/$suma_dir # how barbaric! 925 926 if ( $status ) then 927 set endstr = ( "failure: cannot write" \ 928 "'$suma_dir/$surf.${sex}'" ) 929 goto L_BAD_END 930 endif 931 end # foreach surf ... 932 933 cd $start_dir 934 935 #and do the labels 936 if ( -d $label_dir) then 937 cd $label_dir 938 if ( $fsannot_ver != "" ) then 939 set fsannot_opt = "-FSversion $fsannot_ver" 940 else 941 set fsannot_opt = "" 942 endif 943 944 # ------------------------------------------------------------ 945 # process any extra annotation labels - use mris_convert 946 # (convert and create GIFTI versions to pass to MapIcosahedron) 947 if ( $#extra_annot_labels > 0 ) then 948 echo "++ have $#extra_annot_labels extra labels, " \ 949 "including as if -extra_fs_dsets" 950 951 # find a surface to use as input to mris_convert 952 set sinput = '' 953 set surf_names = ( white smoothwm pial ) 954 foreach stmp ( $surf_names ) 955 if ( -f ../$surf_dir/lh.$stmp && -f ../$surf_dir/rh.$stmp ) then 956 set sinput = $stmp 957 break 958 endif 959 end 960 961 if ( $sinput != "" ) then 962 echo "-- using $sinput as surf input to mris_convert" 963 964 foreach exlab ( $extra_annot_labels ) 965 # convert and make a GIFTI version 966 foreach hand ( lh rh ) 967 set efile = ${hand}.$exlab.annot 968 if ( ! -f $efile ) then 969 echo "" 970 echo "** annot label '$exlab', missing file: $efile" 971 echo "" 972 continue 973 endif 974 975 set isurf = ../$surf_dir/$hand.$sinput 976 set ddd = $start_dir/$suma_dir 977 978 # Until we fix FSread_annot 979 # 980 # FSread_annot -input $efile \ 981 # $fsannot_opt \ 982 # -roi_1D $ddd/${hand}.$exlab.annot.1D.roi \ 983 # -dset $ddd/${hand}.$exlab.annot.niml.dset \ 984 # -cmap_1D $ddd/${hand}.$exlab.annot.1D.cmap 985 986 mris_convert --annot $efile $isurf \ 987 $ddd/${hand}.$exlab.FS.annot.gii 988 989 # This has a color map, but suma does not properly use it. 990 # Use 3dcopy instead, to just get color index values (and 991 # essentially throw away the colormap). 992 # 993 # ConvertDset -i $ddd/${hand}.$exlab.FS.annot.gii \ 994 # -o $ddd/${hand}.$exlab.annot.niml.dset 995 996 3dcopy $ddd/${hand}.$exlab.FS.annot.gii \ 997 $ddd/${hand}.$exlab.annot.niml.dset 998 end 999 end 1000 else 1001 echo "** missing surf input to mris_convert for -extra_fs_dsets" 1002 endif 1003 endif 1004 1005 # ------------------------------------------------------------ 1006 # convert standard annot files 1007 foreach hand ( lh rh ) 1008 if ( -f ${hand}.aparc.a2005s.annot ) then 1009 set ddd = $start_dir/$suma_dir 1010 FSread_annot -input ${hand}.aparc.a2005s.annot \ 1011 $fsannot_opt \ 1012 -roi_1D $ddd/${hand}.aparc.a2005s.annot.1D.roi \ 1013 -dset $ddd/${hand}.aparc.a2005s.annot.niml.dset \ 1014 -cmap_1D $ddd/${hand}.aparc.a2005s.annot.1D.cmap 1015 endif 1016 1017 if ( -f ${hand}.aparc.a2009s.annot ) then 1018 set ddd = $start_dir/$suma_dir 1019 FSread_annot -input ${hand}.aparc.a2009s.annot \ 1020 $fsannot_opt \ 1021 -roi_1D $ddd/${hand}.aparc.a2009s.annot.1D.roi \ 1022 -dset $ddd/${hand}.aparc.a2009s.annot.niml.dset \ 1023 -cmap_1D $ddd/${hand}.aparc.a2009s.annot.1D.cmap 1024 endif 1025 1026 foreach pp (prob predict) 1027 if ( -f ${hand}.v1.${pp}.label ) then 1028 sed '1,2s/^/#/' ${hand}.v1.${pp}.label \ 1029 > ${hand}.v1.${pp}.label.1D 1030 ConvertDset \ 1031 -input ${hand}.v1.${pp}.label.1D'[$]' \ 1032 -node_index_1D ${hand}.v1.${pp}.label.1D'[0]' \ 1033 -o_niml \ 1034 -prefix $start_dir/$suma_dir/${hand}.v1.${pp} 1035 \rm -f ${hand}.v1.${pp}.label.1D 1036 cd $start_dir/$suma_dir/ 1037 3drefit -sublabel 0 v1_${pp} ${hand}.v1.${pp}.niml.dset 1038 cd - 1039 endif 1040 end 1041 1042 end 1043 cd $start_dir 1044 endif 1045 1046 #and the thickness data 1047 cd $surf_dir 1048 foreach hand ( lh rh ) 1049 foreach tp ($fsdsets) 1050 if ( -f ${hand}.${tp} ) then 1051 echo "++ Converting surf dsets: ${hand}.${tp}" 1052 mris_convert -c ${hand}.${tp} ${hand}.white ${hand}.${tp}.gii 1053 mv ${hand}.${tp}.gii \ 1054 $start_dir/$suma_dir/${hand}.${tp}.gii.dset 1055 endif 1056 end 1057 end 1058 cd $start_dir 1059 1060 #and make semi inflated surfaces 1061 cd $suma_dir 1062 if ($#inflates > 0) then 1063 foreach inf ($inflates) 1064 set surf_attribs = ($surf_attribs inf_${inf}) 1065 end 1066 foreach hand ( lh rh ) 1067 foreach inf ($inflates) 1068 if ( -f ${hand}.white.asc ) then 1069 set iis = ${hand}.white.asc 1070 else if ( -f ${hand}.white.gii ) then 1071 set iis = ${hand}.white.gii 1072 else 1073 set endstr = ( "failure: Did not find white for inflation" ) 1074 goto L_BAD_END 1075 endif 1076 SurfSmooth \ 1077 -i ${iis} -met NN_geom \ 1078 -Niter $inf -o_gii -surf_out ${hand}.inf_${inf} \ 1079 -match_vol 0.01 1080 if ( $hand == lh ) then 1081 set list_lh = ($list_lh ${hand}.inf_${inf}) 1082 else 1083 set list_rh = ($list_rh ${hand}.inf_${inf}) 1084 endif 1085 end 1086 end 1087 endif 1088 cd $start_dir 1089 1090goto L_CONVERT_SURFACES_DONE 1091 1092 1093#################################################################### 1094# actually create the spec file 1095 1096L_CREATE_SPEC: 1097 1098 cd $suma_dir 1099 1100 foreach hand ( lh rh ) 1101 if ( $hand == lh ) then 1102 set list_cur = ( $list_lh ) # get a current list copy 1103 else 1104 set list_cur = ( $list_rh ) 1105 endif 1106 1107 set spec_file = {$subj_id}_$hand.spec 1108 1109 if ( $debug ) echo "++ creating spec file '$spec_file'..." 1110 1111 (echo "# delimits comments" > $spec_file) >& /dev/null 1112 1113 if ( $status ) then 1114 set endstr = ( "failure: no permissions to create spec file" \ 1115 "'$suma_dir/$spec_file'" ) 1116 goto L_BAD_END 1117 endif 1118 1119 # note user, date, machine, pwd, command line 1120 echo "" >> $spec_file 1121 echo "# Creation information:" >> $spec_file 1122 echo "# user : $user" >> $spec_file 1123 echo "# date : `date`" >> $spec_file 1124 echo "# machine : `uname -n`" >> $spec_file 1125 echo "# pwd : $cwd" >> $spec_file 1126 echo "# command : $prog_name $argv" >> $spec_file 1127 echo "" >> $spec_file 1128 1129 # define the group 1130 echo "# define the group" >> $spec_file 1131 echo " Group = $subj_id" >> $spec_file 1132 echo "" >> $spec_file 1133 1134 # define the states 1135 echo "# define various States" >> $spec_file 1136 foreach attrib ( $surf_attribs ) 1137 echo " StateDef = $attrib" >> $spec_file 1138 end 1139 echo "" >> $spec_file 1140 1141 foreach surf ( $list_cur ) 1142 set s_head = `echo $surf | cut -d. -f1` # up to first '.' 1143 set s_state = `echo $surf | cut -d. -f2-` # after first '.' 1144 1145 set label_dset = "" 1146 # check for SAME mapping ref 1147 if ( "$surf.${sex}" == "$s_head.smoothwm.${sex}" ) then 1148 set map_ref = SAME 1149 if ( -f ${hand}.aparc.a2005s.annot.niml.dset ) then 1150 set label_dset = ${hand}.aparc.a2005s.annot.niml.dset 1151 endif 1152 if ( -f ${hand}.aparc.a2009s.annot.niml.dset ) then 1153 set label_dset = ${hand}.aparc.a2009s.annot.niml.dset 1154 endif 1155 else 1156 set map_ref = $s_head.smoothwm.${sex} 1157 endif 1158 1159 # check for SAME mapping ref case for pial-outer 1160 if ( "$surf.${sex}" == "$s_head.pial-outer-smoothed.${sex}" ) then 1161 set map_ref = SAME 1162 else 1163 #abide by previous setting 1164 endif 1165 1166 # EmbedDimension is 2 for .flat surfaces, else 3 1167 if ( $surf =~ *.flat* ) then 1168 set embed_ref = 2 1169 else 1170 set embed_ref = 3 1171 endif 1172 1173 # Anatomical flag 1174 if ( $surf =~ *.white* ||\ 1175 $surf =~ *.smoothwm* ||\ 1176 $surf =~ *.pial*) then 1177 set anatomical = Y 1178 else 1179 set anatomical = N 1180 endif 1181 1182 echo "NewSurface" >> $spec_file 1183 echo " SurfaceFormat = ASCII" >> $spec_file 1184 echo " SurfaceType = ${stp}" >> $spec_file 1185 echo " ${sfieldname} = $surf.${sex}" >> $spec_file 1186 echo " LocalDomainParent = $map_ref" >> $spec_file 1187 echo " SurfaceState = $s_state" >> $spec_file 1188 echo " EmbedDimension = $embed_ref" >> $spec_file 1189 echo " Anatomical = $anatomical" >> $spec_file 1190 if ("$label_dset" != "") \ 1191 echo " LabelDset = $label_dset" >> $spec_file 1192 echo "" >> $spec_file 1193 end # foreach surf 1194 1195 echo "++ created spec file'$suma_dir/$spec_file'" 1196 end # foreach hand 1197 1198 # Create both.spec file 1199 set spec_file = {$subj_id}_both.spec 1200 set sample_spec = ${subj_id}_lh.spec 1201 1202 if ( $debug ) echo "++ creating spec file '$spec_file'..." 1203 1204 (echo "# delimits comments" > $spec_file) >& /dev/null 1205 1206 if ( $status ) then 1207 set endstr = ( "failure: no permissions to create spec file" \ 1208 "'$suma_dir/$spec_file'" ) 1209 goto L_BAD_END 1210 endif 1211 1212 # note user, date, machine, pwd, command line 1213 echo "" >> $spec_file 1214 echo "# Creation information:" >> $spec_file 1215 echo "# user : $user" >> $spec_file 1216 echo "# date : `date`" >> $spec_file 1217 echo "# machine : `uname -n`" >> $spec_file 1218 echo "# pwd : $cwd" >> $spec_file 1219 echo "# command : $prog_name $argv" >> $spec_file 1220 echo "" >> $spec_file 1221 1222 # define the group 1223 echo "# define the group" >> $spec_file 1224 echo " Group = $subj_id" >> $spec_file 1225 echo "" >> $spec_file 1226 1227 # define the states 1228 echo "# define various States" >> $spec_file 1229 foreach attrib ( $surf_attribs ) 1230 if ( $attrib =~ *sphere* || \ 1231 $attrib =~ *flat* || \ 1232 $attrib =~ *infla*) then 1233 echo " StateDef = ${attrib}_lh" >> $spec_file 1234 echo " StateDef = ${attrib}_rh" >> $spec_file 1235 else 1236 echo " StateDef = $attrib" >> $spec_file 1237 endif 1238 end 1239 echo "" >> $spec_file 1240 foreach hand ( lh rh ) 1241 if ( $hand == lh ) then 1242 set list_cur = ( $list_lh ) # get a current list copy 1243 else 1244 set list_cur = ( $list_rh ) 1245 endif 1246 1247 foreach surf ( $list_cur ) 1248 set s_head = `echo $surf | cut -d. -f1` # up to first '.' 1249 set s_state = `echo $surf | cut -d. -f2-` # after first '.' 1250 1251 if ( $s_state =~ *sphere* || \ 1252 $s_state =~ *flat* || \ 1253 $s_state =~ *infla*) then 1254 set s_state = "${s_state}_${hand}" 1255 endif 1256 1257 set label_dset = "" 1258 # check for SAME mapping ref 1259 if ( "$surf.${sex}" == "$s_head.smoothwm.${sex}" ) then 1260 set map_ref = SAME 1261 if ( -f ${hand}.aparc.a2005s.annot.niml.dset ) then 1262 set label_dset = ${hand}.aparc.a2005s.annot.niml.dset 1263 endif 1264 if ( -f ${hand}.aparc.a2009s.annot.niml.dset ) then 1265 set label_dset = ${hand}.aparc.a2009s.annot.niml.dset 1266 endif 1267 else 1268 set map_ref = $s_head.smoothwm.${sex} 1269 endif 1270 1271 # EmbedDimension is 2 for .flat surfaces, else 3 1272 if ( $surf =~ *.flat* ) then 1273 set embed_ref = 2 1274 else 1275 set embed_ref = 3 1276 endif 1277 1278 # Anatomical flag 1279 if ( $surf =~ *.white* ||\ 1280 $surf =~ *.smoothwm* ||\ 1281 $surf =~ *.pial*) then 1282 set anatomical = Y 1283 else 1284 set anatomical = N 1285 endif 1286 1287 echo "NewSurface" >> $spec_file 1288 echo " SurfaceFormat = ASCII" >> $spec_file 1289 echo " SurfaceType = ${stp}" >> $spec_file 1290 echo " ${sfieldname} = $surf.${sex}" >> $spec_file 1291 echo " LocalDomainParent = $map_ref" >> $spec_file 1292 echo " SurfaceState = $s_state" >> $spec_file 1293 echo " EmbedDimension = $embed_ref" >> $spec_file 1294 echo " Anatomical = $anatomical" >> $spec_file 1295 if ("$label_dset" != "") \ 1296 echo " LabelDset = $label_dset" >> $spec_file 1297 echo "" >> $spec_file 1298 end 1299 end # foreach hand 1300 1301 echo "++ created spec file'$suma_dir/$spec_file'" 1302 1303 cd $start_dir 1304 1305goto L_CREATE_SPEC_DONE 1306 1307 1308#################################################################### 1309# actually create an AFNI dataset 1310 1311L_CREATE_BRICK: 1312 1313 cd $orig_dir 1314 # verify existence of 256 COR files, Checking for 256 is now 1315 # obsolete ZSS Oct 14 04 1316 set cor_files = ( `echo COR-???` ) >& /dev/null 1317 1318 if ( $status || $use_mgz == 1) then 1319 if ($orig_mgz != "") then 1320 #Try the orig.mgz business 1321 echo "No COR files, converting $orig_mgz ..." 1322 cd $start_dir 1323 mri_convert -ot cor $orig_mgz $orig_dir 1324 cd $orig_dir 1325 else 1326 if ($use_mgz == 0) then 1327 set endstr = "failure: No COR files and no $orig.mgz" 1328 else 1329 set endstr = "failure: No mgz files" 1330 endif 1331 goto L_BAD_END 1332 endif 1333 else 1334 if ($orig_mgz != "" && $neuro_ori != -1) then 1335 echo "" 1336 echo "Notice: Found COR files and $orig_mgz" 1337 echo "******** Will use COR files." 1338 echo "" 1339 else if ($orig_mgz == "" && $neuro_ori == -1) then 1340 set endstr = "No orig_mgz found for -nocor option" 1341 goto L_BAD_END 1342 endif 1343 endif 1344 1345 #check again 1346 set cor_files = ( `echo COR-???` ) >& /dev/null 1347 if ( $status ) then 1348 set endstr = "failure: did not find COR files under '$orig_dir'" 1349 goto L_BAD_END 1350 #else if ( $#cor_files != 256 ) then 1351 #set endstr = "failure: 256 COR files required under '$orig_dir'" 1352 #goto L_BAD_END 1353 endif 1354 1355 # create BRIK with to3d 1356 1357 #SLAB changed by ZSS Mar 12 04. was 0.5 mm off. Bug pointed 1358 #out by A. Thomas. Good info found here: 1359 #www.wideman-one.com/gw/brain/fs/coords/fscoords.htm 1360 1361 #New block added for reading COR images that are no longer 1362 #256x256x256 / 1mm cubic voxels ZSS Oct 20 04 1363 1364 set Sr = `\grep -w imnr0 COR-.info` 1365 set i0 = $Sr[$#Sr] 1366 set Sr = `\grep -w imnr1 COR-.info` 1367 set i1 = $Sr[$#Sr] 1368 set Sr = `\grep -w fov COR-.info` 1369 set FOV = `ccalc -eval $Sr[$#Sr] \* 1000` 1370 set Sr = `\grep -w x COR-.info | \grep -v xform` 1371 set nx = $Sr[$#Sr] 1372 set Sr = `\grep -w y COR-.info` 1373 set ny = $Sr[$#Sr] 1374 set Sr = `\grep -w thick COR-.info` 1375 set zthick = `ccalc -eval $Sr[$#Sr] \* 1000` 1376 set Sr = `\grep -w psiz COR-.info` 1377 set psize = `ccalc -eval $Sr[$#Sr] \* 1000` 1378 1379 set xSLAB0 = `ccalc -eval $nx \* $psize / 2` 1380 set xSLAB1 = `ccalc -eval $xSLAB0 - $psize` 1381 set ySLAB0 = `ccalc -eval $ny \* $psize / 2` 1382 set ySLAB1 = `ccalc -eval $ySLAB0 - $psize` 1383 set zSLAB0 = `ccalc -eval '('$i1 - $i0 + 1')' \* $zthick / 2` 1384 set zSLAB1 = `ccalc -eval $zSLAB0 - $zthick` 1385 1386 1387 if ( $neuro_ori == 1 ) then 1388 to3d \ 1389 -prefix $afni_prefix \ 1390 -xSLAB $xSLAB0'L'-$xSLAB1'R' \ 1391 -ySLAB $ySLAB0'S'-$ySLAB1'I' \ 1392 -zSLAB $zSLAB0'P'-$zSLAB1'A' '3Db:0:0:'$nx':'$ny':1:COR-???' 1393 #to3d -prefix $afni_prefix -xSLAB 127L-128R -ySLAB 128S-127I \ 1394 # -zSLAB 128P-127A $cor_files 1395 else if ( $neuro_ori == 0 ) then 1396 to3d \ 1397 -prefix $afni_prefix \ 1398 -xSLAB $xSLAB0'R'-$xSLAB1'L' \ 1399 -ySLAB $ySLAB0'S'-$ySLAB1'I' \ 1400 -zSLAB $zSLAB0'P'-$zSLAB1'A' '3Db:0:0:'$nx':'$ny':1:COR-???' 1401 #to3d -prefix $afni_prefix -xSLAB 128R-127L -ySLAB 128S-127I \ 1402 # -zSLAB 128P-127A $cor_files 1403 else if ( $neuro_ori == -1 ) then 1404 if ( "$orig_mgz" == '' ) then 1405 set endstr = "Could not locate orig_mgz volume" 1406 goto L_BAD_END 1407 endif 1408 mri_convert -ot nii $start_dir/$orig_mgz \ 1409 $start_dir/$suma_dir/${orig_mgz:t:r}".nii" 1410 else 1411 set endstr = "Bad value for neuro_ori of $neuro_ori" 1412 goto L_BAD_END 1413 endif 1414 1415 if ( $neuro_ori == -1 ) then 1416 mv \ 1417 $start_dir/$suma_dir/${orig_mgz:t:r}".nii" \ 1418 $start_dir/$suma_dir/$afni_dataset.nii 1419 echo "++ created AFNI file '$suma_dir/$afni_dataset.nii'" 1420 else 1421 mv $afni_dataset.HEAD $afni_dataset.BRIK* $start_dir/$suma_dir 1422 1423 echo "++ created AFNI file '$suma_dir/$afni_dataset.HEAD'" 1424 echo "++ created AFNI file '$suma_dir/$afni_dataset.BRIK'" 1425 endif 1426 1427 cd $start_dir 1428 1429 if ($#other_mgz > 0) then 1430 # cat /Applications/freesurfer/FreeSurferColorLUT.txt | grep -v "^#" | sed '/^[[:space:]]*$/d' > fslut.txt 1431 # first unranked original datasets - reliably constant across subjects 1432 @FS_roi_label -name ALL -labeltable $start_dir/$suma_dir/fs_table 1433 if -e $start_dir/$suma_dir/fs_table.niml.lt then 1434 echo "Made roi label table for AFNI - fs_table.niml.lt" 1435 else 1436 echo "ERROR: failed to make roi label table fs_table.niml.lt" 1437 endif 1438 1439 #Do the other .mgz files 1440 foreach other ($other_mgz) 1441 #@SUMA_FSvolToBRIK $other $start_dir/$suma_dir/$other:t:r 1442 #rm -f $start_dir/$suma_dir/COR-??? $start_dir/$suma_dir/COR-.info \ 1443 # >& /dev/null 1444 mri_convert -ot nii $other $start_dir/$suma_dir/${other:t:r}".nii" 1445 #Now shift the damned volumes to match the surface volumes 1446 cd $start_dir/$suma_dir/ 1447 if ($neuro_ori != -1 ) then 1448 @Align_Centers \ 1449 -base $afni_dataset \ 1450 -dset ${other:t:r}".nii" \ 1451 -no_cp 1452 endif 1453 if ( "${other:t:r}" == "aparc.a2005s+aseg" || \ 1454 "${other:t:r}" == "aparc.a2009s+aseg" || \ 1455 "${other:t:r}" == "aseg" || \ 1456 "${other:t:r}" == "aparc+aseg" ) then 1457 # put atlas labels into FreeSurfer segmentation datasets 1458 # first use generic FS table that's the same for all datasets 1459 # [PT] ??? Put parentheses here??? 1460 if ( -e $start_dir/$suma_dir/fs_table.niml.lt ) then 1461 3drefit -labeltable $start_dir/$suma_dir/fs_table.niml.lt \ 1462 "${other:t:r}.nii" 1463 @MakeLabelTable -replace \ 1464 -atlasize_labeled_dset "${other:t:r}.nii" 1465 endif 1466 1467 if ( $DO_MAKE_RANK_DSETS ) then 1468 1469 echo "++ Caveat emptor: *rank* dsets coming your way" 1470 # now ranked datasets-removing empty indices - easier 1471 # to color in AFNI 1472 3dmerge -1rank -prefix "${other:t:r}_rank.nii" \ 1473 ${other:t:r}".nii" 1474 if (0) then 1475 # kill this block soon. 1476 set kk = `echo ${other:t:r} | cut -f 1 -d '+'` 1477 mv ${kk}.rankmap.1D ${other:t:r}.rankmap.1D 1478 endif 1479 @FS_roi_label \ 1480 -name ALL \ 1481 -rankmap ${other:t:r}_rank.rankmap.1D \ 1482 -labeltable ${other:t:r}_rank \ 1483 > ${other:t:r}_rank.niml.lt.log 1484 if ($status) then 1485 echo "Failed to make roi labels for $other" 1486 else 1487 3drefit \ 1488 -labeltable ${other:t:r}_rank.niml.lt \ 1489 "${other:t:r}_rank.nii" 1490 @MakeLabelTable \ 1491 -replace \ 1492 -atlasize_labeled_dset "${other:t:r}_rank.nii" 1493 endif 1494 1495 endif 1496 1497 endif 1498 cd - 1499 end 1500 endif 1501 1502 #Change from orig space 1503 if ( ${set_space} != '') then 1504 cd $start_dir/$suma_dir/ 1505 3drefit -space ${set_space} *.nii* *.HEAD 1506 cd - 1507 endif 1508 1509goto L_CREATE_BRICK_DONE 1510 1511#################################################################### 1512# echo run MapIcosahedron 1513 1514L_ICO: 1515 1516 if ($#ldlist < 1) goto L_ICO_DONE 1517 1518 echo "" 1519 echo "------------------------------------------------------------------" 1520 echo "Running MapIcosahedron for both hemispheres at ld values of $ldlist" 1521 cd $start_dir/$suma_dir/ 1522 set ild = 0 1523 foreach ld ($ldlist) 1524 @ ild ++ 1525 set both = () 1526 foreach hem (lh rh) 1527 set sp = {$subj_id}_${hem}.spec 1528 set dm = () 1529 if ($#fsdsets > 0) then 1530 foreach fsd ($fsdsets) 1531 if ( -f ${hem}.${fsd}.gii.dset ) then 1532 set dm = ($dm "-dset_map" ${hem}.${fsd}.gii.dset) 1533 endif 1534 end 1535 endif 1536 1537 if ($#extra_annot_labels > 0) then 1538 foreach alab ($extra_annot_labels) 1539 set aset = ${hem}.${alab}.annot.niml.dset 1540 if ( -f $aset ) then 1541 set dm = ($dm "-NN_dset_map" $aset) 1542 endif 1543 end 1544 endif 1545 1546 if ( -f $sp) then 1547 # possibly add -verb 19 Mar 2017 [rickr] 1548 if ( $debug ) then 1549 set vb = "-verb" 1550 else 1551 set vb = "" 1552 endif 1553 MapIcosahedron $vb -spec $sp -ld $ld $dm -prefix $ldpref[$ild] 1554 set both = ($both ${ldpref[$ild]}{$subj_id}_${hem}.spec) 1555 endif 1556 end # foreach hem 1557 if ($#both == 2) then 1558 inspec -LRmerge $both -prefix ${ldpref[$ild]}{$subj_id}_both.spec 1559 endif 1560 end # foreach ld 1561 # pass a better sample spec file along 1562 set sample_spec = $ldpref[1]${subj_id}_lh.spec 1563 cd - 1564 1565goto L_ICO_DONE 1566 1567#################################################################### 1568# echo details for the user to launch suma and afni, in order to 1569# check the alignment 1570 1571L_TEST_SURF_VOL: 1572 1573 echo "" 1574 echo "------------------------------------------------------------------" 1575 echo "Please verify that the datasets are aligned properly in both" 1576 echo "afni and suma. You may do this by running the following commands:" 1577 echo "" 1578 echo " cd $suma_dir" 1579 echo " afni -niml &" 1580 if ( -f ${suma_dir}/${afni_dataset}.nii ) then 1581 set ovol = ${afni_dataset}.nii 1582 else if ( -f ${suma_dir}/${afni_dataset}.nii.gz ) then 1583 set ovol = ${afni_dataset}.nii.gz 1584 else 1585 set ovol = $afni_dataset 1586 endif 1587 echo " suma -spec $spec_file -sv $ovol" 1588 1589 echo "suma -spec $sample_spec -sv $ovol" > $suma_dir/run_suma_00_basic.txt 1590 1591goto L_TEST_SURF_VOL_DONE 1592 1593#################################################################### 1594# [PT, Jan 26, 2020] 1595# have an AFNI+suma driving script made 1596 1597L_MAKE_DRIVE_SCRIPT: 1598 1599printf "" > $suma_dir/run_suma_01_with_afni.tcsh 1600cat << EOF >> $suma_dir/run_suma_01_with_afni.tcsh 1601#!/bin/tcsh 1602 1603# This is an example script for driving AFNI+SUMA and having them talk 1604 1605set suma_spec = std.141.*_both.spec 1606set all_sv = ( *SurfVol*{nii,HEAD}* ) 1607set dset_sv = \${all_sv[1]} 1608 1609if ( \$status || "\${?suma_spec}" == "0" ) then 1610 echo "** Some badness with finding files" 1611 exit 1 1612endif 1613 1614# --------------------- preliminary settings ----------------------- 1615 1616# you can set other env vars if you want 1617setenv AFNI_ENVIRON_WARNINGS NO 1618setenv SUMA_DriveSumaMaxCloseWait 6 1619setenv SUMA_DriveSumaMaxWait 6 1620 1621set portnum = `afni -available_npb_quiet` 1622 1623# ------------------------- open the GUIs -------------------------- 1624 1625afni -npb \${portnum} -niml & 1626 1627sleep 2 1628 1629suma \ 1630 -onestate \ 1631 -npb \${portnum} \ 1632 -spec \${suma_spec} \ 1633 -sv \${dset_sv} & 1634 1635sleep 2 1636 1637DriveSuma \ 1638 -npb \${portnum} \ 1639 -com viewer_cont -key 't' -key '.' 1640 1641# ------------------------- Finish up --------------------------- 1642 1643if ( \$status ) then 1644 echo "** Some badness with driving SUMA" 1645 exit 1 1646endif 1647 1648echo "" 1649echo "++ AFNI+SUMA should be up and talking now..." 1650echo "" 1651 1652EOF 1653 1654goto L_MAKE_DRIVE_SCRIPT_DONE 1655 1656#################################################################### 1657# [PT, Dec 30, 2016] 1658# finally replacing rank utility. 1659 1660L_RENUMBER_ROIS: 1661 1662 echo "\n\n++ Going to make renumbered ROIs now." 1663 echo " --> done for colorbar and making tissue maps." 1664 echo " See '@SUMA_renumber_FS -help' for description.\n\n" 1665 1666 @SUMA_renumber_FS $suma_dir 1667 1668goto L_RENUMBER_ROIS_DONE 1669 1670#################################################################### 1671# [PT, Mar 3 2021] 1672# Make a useful WB mask and QC images; also make tables of ROI info. 1673 1674L_MASK_QC_ROIS: 1675 1676 echo "\n\n++ Going to make a nice WB mask." 1677 echo " ... and some useful QC images." 1678 echo " ... and some useful ROI info tables.\n\n" 1679 1680 adjunct_suma_fs_mask_and_qc \ 1681 -sid ${subj_id} \ 1682 -suma_dir $suma_dir 1683 1684 adjunct_suma_fs_roi_info \ 1685 -sid ${subj_id} \ 1686 -suma_dir $suma_dir 1687 1688goto L_MASK_QC_ROIS_DONE 1689 1690#################################################################### 1691# [27 Jan 2020 rickr] 1692# compress SUMA/*.nii, except for SurfVol 1693 1694L_COMPRESS_FILES: 1695 1696 # maybe the user does not want to compress 1697 if ( ! $do_compress ) then 1698 if ( $debug ) echo "-- skipping NIFTI compression" 1699 goto L_COMPRESS_FILES_DONE 1700 endif 1701 1702 # be sure gzip is in the path 1703 (which gzip) >& /dev/null 1704 if ( $status ) then 1705 echo "program not found in path: gzip, skipping compress" 1706 goto L_COMPRESS_FILES_DONE 1707 endif 1708 1709 cd $suma_dir 1710 1711 # do this very simply, for clarity 1712 # first zip everything, then unzip SurfVol 1713 if ( -f $afni_dataset.nii ) then 1714 echo "\n\n++ Compressing all remaining .nii files, except for SurfVol.\n" 1715 1716 gzip *.nii 1717 gunzip $afni_dataset.nii.gz 1718 endif 1719 1720 cd - 1721 1722 1723goto L_COMPRESS_FILES_DONE 1724 1725#################################################################### 1726# display help and exit 1727 1728L_HELP_END: 1729 1730cat <<EOF 1731 1732OVERVIEW ~1~ 1733 1734$prog_name - prepare for surface viewing in SUMA 1735 1736This script goes through the following steps: 1737 + verify existence of necessary programs 1738 (afni, to3d, suma, mris_convert) 1739 + determine the location of surface and COR files 1740 + creation of ascii surface files via 'mris_convert' 1741 + creation of left and right hemisphere SUMA spec files 1742 + creation of an AFNI dataset from the COR files via 'to3d' 1743 + creation of AFNI datasets from various .mgz volumes created 1744 by FreeSurfer. The segmentation volumes with aseg in the 1745 name are best viewed in AFNI with the FreeSurfer_Seg_255 1746 colormap. See bottom of @SUMA_FSvolToBRIK -help for more 1747 info. 1748 + renumbered data sets output, to replace old '*rank*' file data 1749 sets. Also new tissue maps based on FS functions and 1750 labels. Collectively, these are the '*REN*' dsets in the output 1751 directory. (Rank dsets are no longer output by default, from 1752 Nov, 2019; instead, use -make_rank_dsets if really needed.) 1753 + convenience dsets for afni_proc.py processing with tissue-based 1754 regressors (fs_ap_* files: subset of ventricle and WM maps) 1755 1756 + all created files are stored in a new SUMA directory 1757 1758USAGE ~1~ 1759 1760 $prog_name [options] -sid SUBJECT_ID 1761 1762 examples ('-NIFTI' is really useful-- see below!): 1763 1764 $prog_name -help 1765 $prog_name -NIFTI -sid subject1 1766 $prog_name -NIFTI -fspath subject1/surface_stuff -sid subject1 1767 $prog_name -NIFTI -sid 3.14159265 -debug 1 1768 1769OPTIONS ~1~ 1770 1771 -help : show this help information 1772 1773 -debug LEVEL : print debug information along the way 1774 e.g. -debug 1 1775 the default level is 0, max is 2 1776 1777 A debug level of 2 will "set echo", so one can follow the actual 1778 commands executed in the script. 1779 1780 -fs_setup : source \$FREESURFER_HOME/SetUpFreeSurfer.csh 1781 1782 This might be useful on OS X, as FreeSurfer uses DYLD_LIBRARY_PATH, 1783 which does not propagate to child shells. Then this program would 1784 require them to source SetUpFreeSurfer.csh from .cshrc, which might 1785 happen too often and could be irrirating. 1786 1787 With -fs_setup, that will happen from within this script, so it is 1788 not necessary to do from the .cshrc file. 1789 1790 -fspath PATH : path to 'surf' and 'orig' directories 1791 e.g. -fspath subject1/surface_info 1792 the default PATH value is './', the current directory 1793 1794 This is generally the location of the 'surf' directory, 1795 though having PATH end in surf is OK. The mri/orig 1796 directory should also be located here. 1797 1798 Note: when this option is provided, all file/path 1799 messages will be with respect to this directory. 1800 1801 -extra_annot_labels L1 L2 ... : convert extra annot files into ROI dsets 1802 e.g. -extra_annot_labels aparc 1803 1804 FS typically outputs annotation files: 1805 lh.aparc.a2005s.annot 1806 rh.aparc.a2005s.annot 1807 lh.aparc.a2009s.annot 1808 rh.aparc.a2009s.annot 1809 from each of which Make_Spec extracts a colormap, ROI and a 1810 displayable surface dataset. 1811 1812 Use this option to specify other labels to extract. 1813 1814 If LABEL is specified, then expected annotation files will be: 1815 lh.LABEL.annot 1816 rh.LABEL.annot 1817 1818 -extra_fs_dsets AA BB CC ... 1819 : FS calculates many types of data on the surface, 1820 listed in their surf/ output directory. By default, this 1821 program brings the following ones into the SUMA surface realm: 1822 thickness curv sulc 1823 which get turned into 'std.141.{l,r}h.curv.niml.dset' and 1824 '{l,r}h.curv.gii.dset' files, for example. 1825 This option allows the user to list *other* dsets to include, 1826 as well. Ones that users have selected are, for example: 1827 volume area area.pial curv.pial 1828 1829 -make_rank_dsets : before Nov 14, 2019, *rank* dsets used to be 1830 created by this command by default; these dsets mapped the 1831 FreeSurfer ROI numbering to a 1..N set of labels, where N 1832 was the number of ROIs. Because this number might *not* be 1833 constant across a group (though, the ROI string labels in 1834 each would be), we don't recommend using these; the *REN* 1835 dsets are renumbered in a consistent, mapped way, and so 1836 those are more useful. The present option is purely for 1837 backward compatability, for Zome Special Scientists out 1838 there who might still use these dsets. 1839 1840 -use_mgz : use MGZ volumes even if COR volumes are there 1841 1842 -neuro : use neurological orientation 1843 e.g. -neuro 1844 the default is radiological orientation 1845 1846 In the default radiological orientation, the subject's 1847 right is on the left side of the image. In the 1848 neurological orientation, left is really left. 1849 1850 * This is not compatible with -NIFTI. 1851 1852 -nocor: This option is no longer supported because it created 1853 GIFTI surfaces with coordinates in RAI, rather than LPI 1854 which is the GIFTI standard. While using RAI surfaces 1855 within AFNI/SUMA is not problematic, the resultant GIFTI 1856 surfaces do not port well to other software. 1857 The replacement option for -nocor is -GNIFTI but the 1858 surfaces will have negated coordinates along the x and y 1859 compared to those with -nocor. 1860 GIFTI surfaces produced with SUMA programs compiled before 1861 August 1st 2013 will have their X and Y coordinates 1862 negated and will no longer line up with the anatomy. 1863 Correcting such surfaces can be done with ConvertSurface 1864 with the following command: 1865 1866 ConvertSurface \ 1867 -i lh.smoothwm.gii \ 1868 -o_gii lh.smoothwm \ 1869 -overwrite \ 1870 -xmat_1D NegXY 1871 1872 or for an entire SUMA directory: 1873 1874 cd SUMA 1875 tcsh 1876 foreach ss (*.gii) 1877 ConvertSurface \ 1878 -i \$ss \ 1879 -o_gii \$ss \ 1880 -overwrite \ 1881 -xmat_1D NegXY 1882 end 1883 1884 -GNIFTI/-GIFTI/-IFTI: same as -NIFTI 1885 1886 -NIFTI :Produce files in exchangeable formats. With this option 1887 :COR volumes are no longer used and output volumes 1888 :and surfaces are in alignment with the original 1889 :volume used to create the surface. All volumes are 1890 written out NIFTI format, and all surfaces are 1891 in GIFTI format. 1892 1893 This option is incompatible with -neuro or -use_mgz 1894 1895 ** Note: from 22 Feb 2013 through 20 Mar 2017, use of -NIFTI 1896 would distort standard mesh surfaces. To evaluate 1897 effects of this, consider: MapIcosahedron -write_dist. 1898 1899 * If you are seeing this message, that problem was fixed 1900 years ago. 1901 1902 -inflate INF: Create modereately inflated surfaces using 1903 SurfSmooth. INF controls the amount of smoothness 1904 in the final image. It is the number of iterations 1905 in the command such as: 1906 SurfSmooth \ 1907 -i lh.white.asc \ 1908 -met NN_geom \ 1909 -Niter 200 \ 1910 -o_gii \ 1911 -surf_out lh.inf_200 \ 1912 -match_vol 0.01 1913 1914 You can use multiple instances of -inflate to create 1915 inflations of various levels. 1916 -set_space SPACE: Set the space flag of all volumes to 1917 SPACE (orig, MNI, TLRC, MNIa). The default is 1918 orig space. 1919 You should only use this option when the volume you 1920 passed to FreeSurfer was not in 'orig' space. 1921 Use '3dinfo -space YOUR_DATASET' to find the space 1922 of a certain dataset. 1923 1924 -sid SUBJECT_ID : required subject ID for file naming 1925 1926 -ld LD : Create standard mesh surfaces with mesh density 1927 linear depth (see MapIcosahedron -help, option -ld) 1928 set to LD. You can use multiple -ld options. 1929 By default the script will run ld values of 141 and 1930 60. 1931 -ldpref LDpref: Supply what ends up being the -prefix option 1932 for MapIcosahedron. By default it is std.LD. 1933 You need as many -ldpref as you have -ld 1934 -no_ld: Do not run MapIcosahedron. 1935 1936NOTES ~1~ 1937 1938 Making use of FreeSurfer's -contrasurfreg output with MapIcosahedron: 1939 This script will create SUMA versions of lh.rh.sphere.reg and 1940 rh.lh.sphere.reg but in this current state, MapIcosahedron does 1941 not attempt to use them for backward compatibility. 1942 Should you want to create standard mesh surfaces with node 1943 index correspondence across the hemispheres you will need to run 1944 MapIcosahedron manually in the output SUMA/ directory. 1945 1946 For example: 1947 MapIcosahedron \ 1948 -spec SUBJ_rh.spec -ld 60 \ 1949 -dset_map rh.thickness.gii.dset \ 1950 -dset_map rh.curv.gii.dset \ 1951 -dset_map rh.sulc.gii.dset \ 1952 -morph rh.lh.sphere.reg.gii \ 1953 -prefix std.60.lhreg. 1954 1955 This command is very similar to the one use to create the default 1956 output spec file std.60.SUBJ_rh.spec (look at the top of the spec 1957 file for a record of the command that created it), except for the 1958 last two options -morph and -prefix. By using -morph 1959 rh.lh.sphere.reg.gii the resultant standard-mesh right hemispheres 1960 (std.60.lhreg.rh.*.gii) will have node index correspondence with 1961 std.60.lh.*.gii surfaces. To verify visually the correspondence, 1962 run the following: 1963 1964 count -column 0 36001 > std.60.lh.rh.nodeindex.1D.dset 1965 suma -noniml -spec std.60.SUBJ_lh.spec & 1966 suma -noniml -spec std.60.SUBJ_rh.spec & 1967 suma -noniml -spec std.60.lhreg.SUBJ_rh.spec & 1968 1969 Then load std.60.lh.rh.nodeindex.1D.dset into each of the three SUMA 1970 windows. Note how the color pattern (node indices) matches between 1971 SUBJ_lh and lhreg.SUBJ_rh surfaces, but NOT between SUBJ_lh and 1972 SUBJ_rh surfaces. 1973 1974COMMENTS ~1~ 1975 1976 0. More help may be found at: 1977 https://afni.nimh.nih.gov/pub/dist/doc/htmldoc/SUMA/main_toc.html 1978 1. Surface file names should look like 'lh.smoothwm'. 1979 2. Patches of surfaces need the word patch in their name, in 1980 order to use the correct option for 'mris_convert'. 1981 3. Flat surfaces must have .flat in their name. 1982 4. You can tailor the script to your needs. Just make sure you 1983 rename it or risk having your modifications overwritten with 1984 the next SUMA version you install. 1985 1986 Authors to pester: 1987 R. Reynolds (reynoldr@mail.nih.gov) 1988 Z. Saad (saadz@mail.nih.gov) 1989 M. Beauchamp (michael.beauchamp@bcm.edu) 1990 1991EOF 1992 1993 exit 1994 1995 1996#################################################################### 1997# failure! 1998 1999L_BAD_END: 2000 2001 echo "" 2002 if ( "$endstr" != "" ) echo "$endstr" 2003 echo program failure: exiting... 2004 echo "" 2005 2006 exit 2007 2008#################################################################### 2009# finished! 2010 2011L_GOOD_END: 2012 2013 if ( $USING_NIFTI == 0 ) then 2014 echo "\n\n" 2015 echo "WARNING!" 2016 echo " You didn't use the '-NIFTI' or '-GIFTI' or similarly-named" 2017 echo " option flag! Are you *sure* you didn't want to? Learning about" 2018 echo " this option changed my life-- it might just change yours, too." 2019 echo " Please read the helpfile entry for '-NIFTI' if you don't know" 2020 echo " what I'm talking about here." 2021 echo "\n\n" 2022 endif 2023 2024 if ( $debug > 1 ) unset echo 2025 2026 echo "" 2027 if ( "$endstr" != "" ) then 2028 echo "$endstr" 2029 echo "" 2030 endif 2031 2032 exit # just to be consistent 2033 2034