#!/bin/tcsh # [Dec 30, 2016] v1.1 # [Jan 31, 2017] v1.2: update for FS 6.0 # After using @SUMA_Make_Spec_FS, take standard recon-all's aparc+aseg # and aparc+aseg-2009 to make 1) a renumbered data set of all ROIs # (*RENUM*), and 2) a set of only GM ROIs (*GMonly*); in both cases, a # labeltable is attached. The reduced enumeration is mainly for # colorability in the AFNI viewer at present, and the GM list might be # useful for tracking. if ( $#argv > 1 ) then echo "** Only one command line argument, please!\n\n" goto BAD_EXIT endif if ( ("$1" == "-h") || ("$1" == "-help") || ("$1" == "") ) then goto SHOW_HELP endif set sumadir = $1 set here = $PWD cd $1 # --------- check for known file names from @SUMA_Make_Spec_FS ----------- # either zipped or unzipped files set ugh2000 = `ls aparc+aseg.nii*` if ( $#ugh2000 == "1" ) then echo "++ found $ugh2000" else echo "** Trouble finding 'aparc+aseg.nii*'! Check $PWD" goto BAD_EXIT endif set set2000 = "$ugh2000" set ugh2009 = `ls aparc.a2009s+aseg.nii*` if ( $#ugh2009 == "1" ) then echo "++ found $ugh2009" else echo "** Trouble finding 'aparc+aseg.nii*'! Check $PWD" goto BAD_EXIT endif set set2009 = "$ugh2009" # --------------------- look for reference files ----------------------- # get reffile set afniloc = `which afni` set refloc = `dirname $afniloc` set ref2000 = "${refloc}/afni_fs_aparc+aseg_2000.txt" # for making labeltable set ref2009 = "${refloc}/afni_fs_aparc+aseg_2009.txt" # for making labeltable # temp file for labeltable set tempref2000 = "__tmp_ref_827163zxcv_2000.txt" set tempref2009 = "__tmp_ref_827163zxcv_2009.txt" # temp files for minor checks set tmp0 = "__zarglefizz_070.nii.gz" set tmp1 = "__zarglefizz_071.nii.gz" set tmp2 = "__zarglefizz_072.nii.gz" # --------------------- check for necessary files ------------------------ if ( ! -e $set2000 ) then echo "** Oi! No ${set2000}? Must have been a problem with the" echo " @SUMA_Make_Spec_FS conversion. Exiting!" goto BAD_EXIT endif if ( ! -e $set2009 ) then echo "** Oi! No ${set2009}? Must have been a problem with the" echo " @SUMA_Make_Spec_FS conversion. Exiting!" goto BAD_EXIT endif if ( ! -e $ref2000 ) then echo "** Oi! Can't find ${ref2000}?? Problem! Exiting!" goto BAD_EXIT endif if ( ! -e $ref2009 ) then echo "** Oi! Can't find ${ref2009}?? Problem! Exiting!" goto BAD_EXIT endif # ----------------- 2000 parc/seg: full and tissues ----------------------- # full set s0 = `3dinfo -prefix_noext ${set2000}` set ss = "${s0}_REN" set s2000_renum = "${ss}_all.nii.gz" set s2000_renum_lt = "${ss}_all.niml.lt" # all subcategories at present: set s2000_gm = "${ss}_gm.nii.gz" # tiss__gm set s2000_wmat = "${ss}_wmat.nii.gz" # tiss__wmat set s2000_csf = "${ss}_csf.nii.gz" # tiss__csf set s2000_vent = "${ss}_vent.nii.gz" # tiss__vent set s2000_othr = "${ss}_othr.nii.gz" # tiss__othr set s2000_unkn = "${ss}_unkn.nii.gz" # tiss__unkn and >0 # ---------- >>>>>>>> BEGIN generated by Python script <<<<<<<<<< ---------- echo "++ Renumbering FS whole brain ($set2000)." 3dcalc \ -overwrite \ -a $set2000 \ -expr "equals(a,0)*0+equals(a,2)*1+equals(a,3)*2+equals(a,4)*3+equals(a,5)*4+equals(a,7)*5+equals(a,8)*6+equals(a,10)*7+equals(a,11)*8+equals(a,12)*9+equals(a,13)*10+equals(a,14)*11+equals(a,15)*12+equals(a,16)*13+equals(a,17)*14+equals(a,18)*15+equals(a,24)*16+equals(a,26)*17+equals(a,28)*18+equals(a,30)*19+equals(a,31)*20+equals(a,41)*21+equals(a,42)*22+equals(a,43)*23+equals(a,44)*24+equals(a,46)*25+equals(a,47)*26+equals(a,49)*27+equals(a,50)*28+equals(a,51)*29+equals(a,52)*30+equals(a,53)*31+equals(a,54)*32+equals(a,58)*33+equals(a,60)*34+equals(a,62)*35+equals(a,63)*36+equals(a,72)*37+equals(a,77)*38+equals(a,80)*39+equals(a,85)*40+equals(a,251)*41+equals(a,252)*42+equals(a,253)*43+equals(a,254)*44+equals(a,255)*45+equals(a,1000)*46+equals(a,1001)*47+equals(a,1002)*48+equals(a,1003)*49+equals(a,1005)*50+equals(a,1006)*51+equals(a,1007)*52+equals(a,1008)*53+equals(a,1009)*54+equals(a,1010)*55+equals(a,1011)*56+equals(a,1012)*57+equals(a,1013)*58+equals(a,1014)*59+equals(a,1015)*60+equals(a,1016)*61+equals(a,1017)*62+equals(a,1018)*63+equals(a,1019)*64+equals(a,1020)*65+equals(a,1021)*66+equals(a,1022)*67+equals(a,1023)*68+equals(a,1024)*69+equals(a,1025)*70+equals(a,1026)*71+equals(a,1027)*72+equals(a,1028)*73+equals(a,1029)*74+equals(a,1030)*75+equals(a,1031)*76+equals(a,1032)*77+equals(a,1033)*78+equals(a,1034)*79+equals(a,1035)*80+equals(a,2000)*81+equals(a,2001)*82+equals(a,2002)*83+equals(a,2003)*84+equals(a,2005)*85+equals(a,2006)*86+equals(a,2007)*87+equals(a,2008)*88+equals(a,2009)*89+equals(a,2010)*90+equals(a,2011)*91+equals(a,2012)*92+equals(a,2013)*93+equals(a,2014)*94+equals(a,2015)*95+equals(a,2016)*96+equals(a,2017)*97+equals(a,2018)*98+equals(a,2019)*99+equals(a,2020)*100+equals(a,2021)*101+equals(a,2022)*102+equals(a,2023)*103+equals(a,2024)*104+equals(a,2025)*105+equals(a,2026)*106+equals(a,2027)*107+equals(a,2028)*108+equals(a,2029)*109+equals(a,2030)*110+equals(a,2031)*111+equals(a,2032)*112+equals(a,2033)*113+equals(a,2034)*114+equals(a,2035)*115" \ -prefix $s2000_renum echo "++ ... getting gm..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,2,6,7,8,9,10,13,14,15,17,18,22,26,27,28,29,30,31,32,33,34,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)+amongst(a,76,77,78,79,80,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))" \ -prefix $s2000_gm echo "++ ... getting wmat..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,1,5,21,25,38,41,42,43,44,45))" \ -prefix $s2000_wmat echo "++ ... getting csf..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,16))" \ -prefix $s2000_csf echo "++ ... getting vent..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,3,4,11,12,20,23,24,36,37))" \ -prefix $s2000_vent echo "++ ... getting othr..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,19,35,39,40))" \ -prefix $s2000_othr echo "++ ... getting unkn..." 3dcalc \ -overwrite \ -a $s2000_renum \ -expr "a*(amongst(a,46,81))" \ -prefix $s2000_unkn # ---------- >>>>>>>> END generated by Python script <<<<<<<<<< --------- # --------------------- checks! ------------------------------- echo "\n++ Running checks now.\n" # Did we find all we should have? 3dcalc \ -overwrite \ -a $set2000 \ -b $s2000_renum \ -expr 'step(a)-step(b)' \ -prefix $tmp0 set minv = `3dinfo -minus $tmp0` set maxv = `3dinfo -maxus $tmp0` # should be zero: if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #0 for 2000 dsets!" echo " ---> incomplete selection somehow." goto BAD_EXIT endif 3dcalc \ -overwrite \ -a $s2000_renum \ -b $s2000_gm \ -c $s2000_wmat \ -d $s2000_csf \ -e $s2000_vent \ -f $s2000_othr \ -g $s2000_unkn \ -expr 'step(a)-step(b+c+d+e+f+g)' \ -prefix $tmp1 set minv = `3dinfo -minus $tmp1` set maxv = `3dinfo -maxus $tmp1` if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #1 for 2000 dsets!" echo " ---> over/under-matching." goto BAD_EXIT endif # Check that ROIs are accounted for! 3dcalc \ -overwrite \ -a $s2000_gm \ -b $s2000_wmat \ -c $s2000_csf \ -d $s2000_vent \ -e $s2000_othr \ -f $s2000_unkn \ -expr 'step(a)*step(b)*step(c)*step(d)*step(e)*step(f)' \ -prefix $tmp2 set minv = `3dinfo -minus $tmp2` set maxv = `3dinfo -maxus $tmp2` if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #2 for 2000 dsets!" echo " ---> overlapping tissues." goto BAD_EXIT endif # clean 'em up \rm $tmp0 $tmp1 $tmp2 # @MakeLabelTable can't handle the comment... grep -v "#" "${ref2000}" > "${tempref2000}" # make+apply labeltables, based on new enumeration foreach ff ( $s2000_renum $s2000_gm $s2000_wmat $s2000_csf $s2000_vent $s2000_othr $s2000_unkn ) echo "++ Attaching labeltable to: $ff" @MakeLabelTable \ -lab_file "${tempref2000}" 1 3 \ -labeltable $s2000_renum_lt \ -dset $ff end \rm ${tempref2000} echo "\n\n++ OK through '2000' dsets.\n\n" # ----------------- 2009 parc/seg: full and GM --------------------------- # full set s0 = `3dinfo -prefix_noext ${set2009}` set ss = "${s0}_REN" set s2009_renum = "${ss}_all.nii.gz" set s2009_renum_lt = "${ss}_all.niml.lt" # all subcategories at present: set s2009_gm = "${ss}_gm.nii.gz" # tiss__gm set s2009_wmat = "${ss}_wmat.nii.gz" # tiss__wmat set s2009_csf = "${ss}_csf.nii.gz" # tiss__csf set s2009_vent = "${ss}_vent.nii.gz" # tiss__vent set s2009_othr = "${ss}_othr.nii.gz" # tiss__othr set s2009_unkn = "${ss}_unkn.nii.gz" # tiss__unkn and >0 # ----------- >>>>>>>> BEGIN generated by Python script <<<<<<<<<< --------- echo "++ Renumbering FS whole brain ($set2009)." 3dcalc \ -overwrite \ -a $set2009 \ -expr "equals(a,0)*0+equals(a,2)*1+equals(a,3)*2+equals(a,4)*3+equals(a,5)*4+equals(a,7)*5+equals(a,8)*6+equals(a,10)*7+equals(a,11)*8+equals(a,12)*9+equals(a,13)*10+equals(a,14)*11+equals(a,15)*12+equals(a,16)*13+equals(a,17)*14+equals(a,18)*15+equals(a,24)*16+equals(a,26)*17+equals(a,28)*18+equals(a,30)*19+equals(a,31)*20+equals(a,41)*21+equals(a,42)*22+equals(a,43)*23+equals(a,44)*24+equals(a,46)*25+equals(a,47)*26+equals(a,49)*27+equals(a,50)*28+equals(a,51)*29+equals(a,52)*30+equals(a,53)*31+equals(a,54)*32+equals(a,58)*33+equals(a,60)*34+equals(a,62)*35+equals(a,63)*36+equals(a,72)*37+equals(a,77)*38+equals(a,80)*39+equals(a,85)*40+equals(a,251)*41+equals(a,252)*42+equals(a,253)*43+equals(a,254)*44+equals(a,255)*45+equals(a,1000)*46+equals(a,2000)*47+equals(a,11100)*48+equals(a,11101)*49+equals(a,11102)*50+equals(a,11103)*51+equals(a,11104)*52+equals(a,11105)*53+equals(a,11106)*54+equals(a,11107)*55+equals(a,11108)*56+equals(a,11109)*57+equals(a,11110)*58+equals(a,11111)*59+equals(a,11112)*60+equals(a,11113)*61+equals(a,11114)*62+equals(a,11115)*63+equals(a,11116)*64+equals(a,11117)*65+equals(a,11118)*66+equals(a,11119)*67+equals(a,11120)*68+equals(a,11121)*69+equals(a,11122)*70+equals(a,11123)*71+equals(a,11124)*72+equals(a,11125)*73+equals(a,11126)*74+equals(a,11127)*75+equals(a,11128)*76+equals(a,11129)*77+equals(a,11130)*78+equals(a,11131)*79+equals(a,11132)*80+equals(a,11133)*81+equals(a,11134)*82+equals(a,11135)*83+equals(a,11136)*84+equals(a,11137)*85+equals(a,11138)*86+equals(a,11139)*87+equals(a,11140)*88+equals(a,11141)*89+equals(a,11143)*90+equals(a,11144)*91+equals(a,11145)*92+equals(a,11146)*93+equals(a,11147)*94+equals(a,11148)*95+equals(a,11149)*96+equals(a,11150)*97+equals(a,11151)*98+equals(a,11152)*99+equals(a,11153)*100+equals(a,11154)*101+equals(a,11155)*102+equals(a,11156)*103+equals(a,11157)*104+equals(a,11158)*105+equals(a,11159)*106+equals(a,11160)*107+equals(a,11161)*108+equals(a,11162)*109+equals(a,11163)*110+equals(a,11164)*111+equals(a,11165)*112+equals(a,11166)*113+equals(a,11167)*114+equals(a,11168)*115+equals(a,11169)*116+equals(a,11170)*117+equals(a,11171)*118+equals(a,11172)*119+equals(a,11173)*120+equals(a,11174)*121+equals(a,11175)*122+equals(a,12100)*123+equals(a,12101)*124+equals(a,12102)*125+equals(a,12103)*126+equals(a,12104)*127+equals(a,12105)*128+equals(a,12106)*129+equals(a,12107)*130+equals(a,12108)*131+equals(a,12109)*132+equals(a,12110)*133+equals(a,12111)*134+equals(a,12112)*135+equals(a,12113)*136+equals(a,12114)*137+equals(a,12115)*138+equals(a,12116)*139+equals(a,12117)*140+equals(a,12118)*141+equals(a,12119)*142+equals(a,12120)*143+equals(a,12121)*144+equals(a,12122)*145+equals(a,12123)*146+equals(a,12124)*147+equals(a,12125)*148+equals(a,12126)*149+equals(a,12127)*150+equals(a,12128)*151+equals(a,12129)*152+equals(a,12130)*153+equals(a,12131)*154+equals(a,12132)*155+equals(a,12133)*156+equals(a,12134)*157+equals(a,12135)*158+equals(a,12136)*159+equals(a,12137)*160+equals(a,12138)*161+equals(a,12139)*162+equals(a,12140)*163+equals(a,12141)*164+equals(a,12143)*165+equals(a,12144)*166+equals(a,12145)*167+equals(a,12146)*168+equals(a,12147)*169+equals(a,12148)*170+equals(a,12149)*171+equals(a,12150)*172+equals(a,12151)*173+equals(a,12152)*174+equals(a,12153)*175+equals(a,12154)*176+equals(a,12155)*177+equals(a,12156)*178+equals(a,12157)*179+equals(a,12158)*180+equals(a,12159)*181+equals(a,12160)*182+equals(a,12161)*183+equals(a,12162)*184+equals(a,12163)*185+equals(a,12164)*186+equals(a,12165)*187+equals(a,12166)*188+equals(a,12167)*189+equals(a,12168)*190+equals(a,12169)*191+equals(a,12170)*192+equals(a,12171)*193+equals(a,12172)*194+equals(a,12173)*195+equals(a,12174)*196+equals(a,12175)*197" \ -prefix $s2009_renum echo "++ ... getting gm..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,2,6,7,8,9,10,13,14,15,17,18,22,26,27,28,29,30,31,32,33,34,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)+amongst(a,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,124,125,126,127,128)+amongst(a,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)+amongst(a,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197))" \ -prefix $s2009_gm echo "++ ... getting wmat..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,1,5,21,25,38,41,42,43,44,45))" \ -prefix $s2009_wmat echo "++ ... getting csf..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,16))" \ -prefix $s2009_csf echo "++ ... getting vent..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,3,4,11,12,20,23,24,36,37))" \ -prefix $s2009_vent echo "++ ... getting othr..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,19,35,39,40))" \ -prefix $s2009_othr echo "++ ... getting unkn..." 3dcalc \ -overwrite \ -a $s2009_renum \ -expr "a*(amongst(a,46,47,48,123))" \ -prefix $s2009_unkn # ----------- >>>>>>>> END generated by Python script <<<<<<<<<< -------- # --------------------- checks! ------------------------------- echo "\n++ Running checks now.\n" # Did we find all we should have? 3dcalc \ -overwrite \ -a $set2009 \ -b $s2009_renum \ -expr 'step(a)-step(b)' \ -prefix $tmp0 set minv = `3dinfo -minus $tmp0` set maxv = `3dinfo -maxus $tmp0` # should be zero: if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #0 for 2009 dsets!" echo " ---> incomplete selection somehow." goto BAD_EXIT endif 3dcalc \ -overwrite \ -a $s2009_renum \ -b $s2009_gm \ -c $s2009_wmat \ -d $s2009_csf \ -e $s2009_vent \ -f $s2009_othr \ -g $s2009_unkn \ -expr 'step(a)-step(b+c+d+e+f+g)' \ -prefix $tmp1 set minv = `3dinfo -minus $tmp1` set maxv = `3dinfo -maxus $tmp1` if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #1 for 2009 dsets!" echo " ---> over/under-matching." goto BAD_EXIT endif # Check that ROIs are accounted for! 3dcalc \ -overwrite \ -a $s2009_gm \ -b $s2009_wmat \ -c $s2009_csf \ -d $s2009_vent \ -e $s2009_othr \ -f $s2009_unkn \ -expr 'step(a)*step(b)*step(c)*step(d)*step(e)*step(f)' \ -prefix $tmp2 set minv = `3dinfo -minus $tmp2` set maxv = `3dinfo -maxus $tmp2` if ( $minv != $maxv ) then echo "** ERROR: Badness in Check #2 for 2009 dsets!" echo " ---> overlapping tissues." goto BAD_EXIT endif # clean 'em up \rm $tmp0 $tmp1 $tmp2 # @MakeLabelTable can't handle the comment... grep -v "#" "${ref2009}" > "${tempref2009}" # make+apply labeltables, based on new enumeration foreach ff ( $s2009_renum $s2009_gm $s2009_wmat $s2009_csf $s2009_vent $s2009_othr $s2009_unkn ) echo "++ Attaching labeltable to: $ff" @MakeLabelTable \ -lab_file "${tempref2009}" 1 3 \ -labeltable $s2009_renum_lt \ -dset $ff end \rm ${tempref2009} echo "\n\n++ OK through '2009' dsets.\n\n" goto GOOD_EXIT # ------------------------------------------------------------------- SHOW_HELP: cat << EOF --------------------------------------------------------------------- This script is now run at the end of modern @SUMA_Make_Spec_FS commands, or it can be run separately for data that had been processed using older versions of AFNI. Originally written and tested on FreeSurfer (FS) v5.3 output from default running of 'recon-all'. This should now work for FS v6.0 default running of 'recon-all', as well. Written by PA Taylor (NIMH, NIH; 2016-7). PURPOSE: This program will take the aparc+aseg.nii.gz and aparc.a2009s+aseg.nii.gz parcellation files produced by FreeSurfer (FS) and converted to NIFTI by @SUMA_Make_Spec_FS, and make the following related data sets (with the same prefix) for each: + A copy of the whole parcellation/segmentation and renumber the ROIs to be smaller (for colorbar representation); this file is called "*_REN_all.nii.gz". + Tissue segmentation maps (not binary, but containing the renumbered ROI values), based on our best guesses of of what each is, from both the 'mri_binarize' command in FS and our own supplementary reading of the ROI names. The following files are output: *_REN_gm.nii.gz :gray matter *_REN_wmat.nii.gz :white matter *_REN_csf.nii.gz :cerebrospinal fluid *_REN_vent.nii.gz :ventricles and choroid plexus *_REN_othr.nii.gz :optic chiasm, non-WM-hypointens, etc. *_REN_unkn.nii.gz :FS-defined "unknown", with voxel value >0 + A labeltable of the new ROI values: "*_REN_all.niml.lt". This labeltable is attached to each of the *_REN_*.nii.gz files. RUNNING: At the moment, the function just takes a single, required argument, which is the location of the 'SUMA/' directory created by @SUMA_Make_Spec_FS. The program also requires being able to see the two 'afni_fs_aparc+aseg_*.txt' files in the AFNI binary directory: that is where the information on renumbering the FS ROIs is). \$ @SUMA_renumber_FS SUMA_DIR where SUMA_DIR is either the full or relative path to the 'SUMA/' directory (including that directory name). EXAMPLE: \$ @SUMA_renumber_FS /data/study/SUBJ_01/FS/SUMA ___________________________________________________________________________ EOF goto GOOD_EXIT BAD_EXIT: exit 1 GOOD_EXIT: exit 0