#!/bin/tcsh
# make_caez_2.2.csh
# Script to convert Eickhoff-Zilles SPM Anatomy Toolbox atlases and 
# templates for AFNI whereami use

# note no change to macrolabels atlas on N27 dataset except for missing
# label for index 116
# manually edited those existing in AFNI distribution
#cp ~/abin/*_ml_18+tlrc.* .
#3dinfo -atlas_points MNI_caez_ml_18+tlrc > ml_18.atlas.niml
#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:ml_18.atlas.niml" MNIa_caez_ml_18+tlrc.
#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:ml_18.atlas.niml" MNI_caez_ml_18+tlrc
#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:ml_18.atlas.niml" TT_caez_ml_18+tlrc.
# copy these to the distribution directory
# need to remove extra spaces at end of labels too
#3dinfo -atlas_points MNI_caez_mpm_22+tlrc > MNI_mpm_22.niml
#3dinfo -atlas_points MNIa_caez_mpm_22+tlrc > MNIa_mpm_22.niml
#3dinfo -atlas_points TT_caez_mpm_22+tlrc > TT_mpm_22.niml

#cat MNI_mpm_22.niml | sed 's/\ \"/\"/g' > MNI_mpm_22_nospace.niml
#cat TT_mpm_22.niml | sed 's/\ \"/\"/g' > TT_mpm_22_nospace.niml
#cat MNIa_mpm_22.niml | sed 's/\ \"/\"/g' > MNIa_mpm_22_nospace.niml

#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:MNI_mpm_22_nospace.niml" MNI_caez_mpm_22+tlrc.HEAD
#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:MNIa_mpm_22_nospace.niml" MNIa_caez_mpm_22+tlrc.HEAD
#3drefit -atrstring "ATLAS_LABEL_TABLE" "file:TT_mpm_22_nospace.niml" TT_caez_mpm_22+tlrc.HEAD

# also manually edit the comments in CustomAtlases.niml to show updated citations

set ez_anat_dir = ../Anatomy
set ez_mpm_labels0 = MPM_labels_orig1.txt

set MPM = MNIa_caez_mpm_22
set MNI_MPM = MNI_caez_mpm_22
set TT_MPM = TT_caez_mpm_22

set ML MNIa_caez_ml_22
setenv AFNI_SUPP_ATLAS_DIR `pwd`/caez_2.2_dist
set TT_N27 = ~/abin/TT_N27+tlrc

mkdir $AFNI_SUPP_ATLAS_DIR
rm $AFNI_SUPP_ATLAS_DIR/CustomAtlases.niml

3dcopy -overwrite $ez_anat_dir/Anatomy_v22c.nii temp_$MPM 
cp $ez_anat_dir/MacroLabels.nii $ML
cp $ez_anat_dir/colin27T1_seg.nii MNIa_caez_colin27_T1_22
cp $ez_anat_dir/AnatMask.nii .
cp $ez_anat_dir/wgrey.nii .
cp $ez_anat_dir/wwhite.nii .

# do pmaps later...

cp $ez_anat_dir/Anatomy_v22c_MPM.txt .
# imported this into LibreOffice Calc
# copied first three columns over with labels and Indices to text file

# process to separate labels and indices
# replace spaces with underscores 
set nl =  `wc -l $ez_mpm_labels0`
set nl = $nl[1]
rm mpm_labels_afni.txt

foreach li (`count 1 $nl`)
    set labline = `head -n $li $ez_mpm_labels0 |tail -1`
    set nv = $#labline
    # if not a blank line  
    if("$nv" != "0") then    
       set val = $labline[$nv]
       # we don't use the next to last column
       set nv = `ccalc -int "$nv-2"`
       set label = `echo "$labline[1-$nv]" | sed 's/\[/(/g'| sed 's/\]/)/g'| \
          sed 's/\ -\ /-/g'|sed 's/\ /_/g' | sed 's/\ \"/\"/g'`
       echo "$val $label" >> mpm_labels_afni.txt
    endif
end

# there are many voxels with voxel values  below any identified region
# threshold here
3dcalc -a temp_${MPM}+tlrc -expr 'a*step(a-101)' -prefix $MPM -overwrite

# compute left and right masks
# mask does not go to edge of MPM regions, so dilate a little
3dmask_tool -inputs AnatMask.nii'<1..1.01>' -dilate_inputs 3 \
   -prefix temp_right -overwrite
3dmask_tool -inputs AnatMask.nii'<2>' -dilate_inputs 3 \
   -prefix temp_left -overwrite
# mask each side without encroaching on the opposite side
3dcalc -a ${MPM}+tlrc -b temp_right+tlrc -c AnatMask.nii'<2>' \
    -expr 'a*step(b)*not(c)' -prefix ${MPM}_right -overwrite
# some rounding problem because these are stored as floats?    
3dcalc -a ${MPM}+tlrc -b temp_left+tlrc -c AnatMask.nii'<1..1.01>' \
    -expr 'a*step(b)*not(c)' -prefix ${MPM}_left -overwrite

# remove any previous copies
rm $AFNI_SUPP_ATLAS_DIR/${MPM}*
rm $AFNI_SUPP_ATLAS_DIR/${MNI_MPM}*
rm $AFNI_SUPP_ATLAS_DIR/${TT_MPM}*

# make MPM atlases
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNIA -space MNI_ANAT  \
   -centers -dset ${MPM}+tlrc -lab_file mpm_labels_afni.txt 1 0     \
   -atlas_description "Eickhoff-Zilles MPM atlas"
# make right side MPM, with centers of mass for right
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNIA_right -space MNI_ANAT \
   -centers -dset ${MPM}_right+tlrc -lab_file mpm_labels_afni.txt 1 0   \
   -atlas_description "Eickhoff-Zilles MPM right only"
# make left side MPM, with centers of mass for left
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNIA_left -space MNI_ANAT \
   -centers -dset ${MPM}_left+tlrc -lab_file mpm_labels_afni.txt 1 0    \
   -atlas_description "Eickhoff-Zilles MPM left only"

# make shifted versions in MNI space
@Shift_Volume -MNI_Anat_to_MNI -prefix $MNI_MPM -dset ${MPM}+tlrc
@Shift_Volume -MNI_Anat_to_MNI -prefix ${MNI_MPM}_left -dset ${MPM}_left+tlrc
@Shift_Volume -MNI_Anat_to_MNI -prefix ${MNI_MPM}_right -dset ${MPM}_right+tlrc

# make MNI versions of MPM atlases
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNI -space MNI  \
   -centers -dset ${MNI_MPM}+tlrc -lab_file mpm_labels_afni.txt 1 0     \
   -atlas_description "Eickhoff-Zilles MPM atlas"
# make right side MPM, with centers of mass for right
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNI_right -space MNI \
   -centers -dset ${MNI_MPM}_right+tlrc -lab_file mpm_labels_afni.txt 1 0   \
   -atlas_description "Eickhoff-Zilles MPM right only"
# make left side MPM, with centers of mass for left
@Atlasize -atlas_type G -atlas_name CA_MPM_22_MNI_left -space MNI \
   -centers -dset ${MNI_MPM}_left+tlrc -lab_file mpm_labels_afni.txt 1 0    \
   -atlas_description "Eickhoff-Zilles MPM left only"

# make TT_N27 Talairach space versions of these MPM atlases too
adwarp -force -resam NN -apar $TT_N27 -force -dpar ${MNI_MPM}+tlrc -prefix ${TT_MPM}
adwarp -force -resam NN -apar $TT_N27 -force -dpar ${MNI_MPM}_left+tlrc -prefix ${TT_MPM}_left
adwarp -force -resam NN -apar $TT_N27 -force -dpar ${MNI_MPM}_right+tlrc -prefix ${TT_MPM}_right

# make TT versions of MPM atlases
@Atlasize -atlas_type G -atlas_name CA_MPM_22_TT -space TT_N27  \
   -centers -dset ${TT_MPM}+tlrc -lab_file mpm_labels_afni.txt 1 0     \
   -atlas_description "Eickhoff-Zilles MPM atlas - Talairach space"
# make right side MPM, with centers of mass for right
@Atlasize -atlas_type G -atlas_name CA_MPM_22_TT_right -space TT_N27 \
   -centers -dset ${TT_MPM}_right+tlrc -lab_file mpm_labels_afni.txt 1 0   \
   -atlas_description "Eickhoff-Zilles MPM right only"
# make left side MPM, with centers of mass for left
@Atlasize -atlas_type G -atlas_name CA_MPM_22_TT_left -space TT_N27 \
   -centers -dset ${TT_MPM}_left+tlrc -lab_file mpm_labels_afni.txt 1 0    \
   -atlas_description "Eickhoff-Zilles MPM left only"

# update MPM atlas with right side centers
3drefit -copytables $AFNI_SUPP_ATLAS_DIR/${MPM}_right+tlrc $AFNI_SUPP_ATLAS_DIR/${MPM}+tlrc
# update MNI_MPM atlas with right side centers
3drefit -copytables $AFNI_SUPP_ATLAS_DIR/${MNI_MPM}_right+tlrc $AFNI_SUPP_ATLAS_DIR/${MNI_MPM}+tlrc
# update TT_MPM atlas with right side centers
3drefit -copytables $AFNI_SUPP_ATLAS_DIR/${TT_MPM}_right+tlrc $AFNI_SUPP_ATLAS_DIR/${TT_MPM}+tlrc

mkdir mpm_surfs
cd mpm_surfs
IsoSurface -input ../MNI_caez_mpm_22+tlrc. -isorois+dsets -o MNI_mpm_2.2.gii -Tsmooth 0.3 60
