#! /bin/tcsh -f

set subject1=$1
set subject2=$2
if ( "x${subject1}" == "x" || "x${subject2}" == "x" ) then
    echo "USAGE: asegstatsdiff <subj1> <subj2> [<outdir>]"
    echo ""
    echo "The utility asegstats2table is executed given the two subjects"
    echo "as input, and an output table called asegstats.txt is produced."
    echo "Then, another row is added to that table containing the percent"
    echo "difference between the data of each of the two subjects."
    echo "Mainly this is used to evaluate the effect of a code change on"
    echo "the aseg morphometry data for a given subject (before and after)."
    echo "The return code is the number of structures found to have nonzero"
    echo "percentage differences."
    echo ""
    echo "[<outdir>] - optionally specify a directory to write asegstats.txt"
    echo ""
    exit 1
endif

if ( ! -e $SUBJECTS_DIR/${subject1} ) then
    echo "$SUBJECTS_DIR/${subject1} does not exist!"
    exit 1
endif
if ( ! -e $SUBJECTS_DIR/${subject2} ) then
    echo "$SUBJECTS_DIR/${subject2} does not exist!"
    exit 1
endif
if ( "x${subject1}" == "x${subject2}" ) then
    echo "Subject names must be different."
    exit 1
endif
if ( "x$3" != "x" ) then
    set outfile=$3/asegstats.txt
else
    set outfile=./asegstats.txt
endif

set cmd=(asegstats2table --s ${subject1} --s ${subject2} --t ${outfile})
set tmpfile=(/tmp/asegstats2table-$$.txt)
$cmd >& $tmpfile
if ($status) then
    echo "FAILED: $cmd"
    cat $tmpfile
    exit 1
endif
rm -f $tmpfile >& /dev/null

set labels=`grep "volume " ${outfile}`
set subj1=`grep "${subject1} " ${outfile}`
set subj2=`grep "${subject2} " ${outfile}`

if ( "$#labels" != "$#subj1" ) then
    echo "Label row and Subj1 row have unequal number of columns!" 
    echo "$#labels"
    echo "$#subj1"
    exit 1
endif
if ( "$#subj1" != "$#subj2" ) then
    echo "Subj1 and Subj2 rows have unequal number of columns" 
    echo "$#subj1"
    echo "$#subj2"
    exit 1
endif

set total=$#subj1
@ total++
set idx=2
set newrow=( "pctdiff" )
set diffcount=0
set stdoutfile=(/tmp/asegstatsdiff-$$.txt)
rm -f $stdoutfile >& /dev/null
while ( "$idx" != "$total" )

    set label=$labels[$idx]
    set struct1=$subj1[$idx]
    set struct2=$subj2[$idx]
    set diff=`echo "scale=4; ${struct2} - ${struct1}" | bc`
    if ($status) exit 1
    if ( "$diff" != "0" ) then
      if ( "$struct1" != "0" ) then
        set pctdiff=`echo "scale=4; ${diff} / ${struct1}" | bc`
      else
        set pctdiff=`echo "scale=4; ${diff} / ${struct2}" | bc`
      endif
      if ($status) exit 1
      set pctdiff=`echo "scale=4; ${pctdiff} * 100" | bc`
      if ($status) exit 1
      echo "${pctdiff}\t\t${label}   (s1: ${struct1}, s2: ${struct2})" \
          >> $stdoutfile
      set newrow=( ${newrow} ${pctdiff} )
      @ diffcount++
    else
      set newrow=( ${newrow} 0 )
    endif

    @ idx++

end

# add the new row to table output by asegstats2table
echo "${newrow}" >> ${outfile}

# output the column of non-zero structures, sorted by % change
echo ""
echo "----------------"
echo "Aseg Stats Diff:" 
echo "subject1: ${subject1}, subject2: ${subject2}"
echo "% diff:\t\tlabel:   (subject1:value, subject2:value)"
echo "-------\t\t-----------------------------------------"
if ( -e $stdoutfile ) then
    cat $stdoutfile | sort -n
    rm -f $stdoutfile >& /dev/null
else 
    echo "All labels have equal measures (zero diff)."
endif

exit ${diffcount}
