#! /bin/tcsh -f

#
# run-qdec-glm
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/03/14 04:04:44 $
#    $Revision: 1.9 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA).
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#

set version = '$Id: run-qdec-glm,v 1.9 2007/03/14 04:04:44 nicks Exp $';

if($#argv != 1) then
  echo "run-qdec-glm qdecdir"
  echo $version
  exit 1;
endif

set qdecdir = $argv[1];
if(! -e $qdecdir) then
  echo "ERROR: cannot fin $qdecdir"
  exit 1;
endif

set cfg = $qdecdir/qdec.cfg
if(! -e $cfg) then
  echo "ERROR: cannot fin $cfg"
  exit 1;
endif

set fsgdf = $qdecdir/qdec.fsgd
if(! -e $fsgdf) then
  echo "ERROR: cannot fin $fsgdf"
  exit 1;
endif

set cdir = $qdecdir/contrasts
if(! -e $cdir) then
  echo "ERROR: cannot fin $cdir"
  exit 1;
endif

set measure = `grep \#MEASURE $cfg | awk '{print $2}'`;
if($#measure != 1) then
  echo "ERROR: $cfg, MEASURE"
  exit 1;
endif

set hemi = `grep \#HEMI $cfg | awk '{print $2}'`;
if($#hemi != 1) then
  echo "ERROR: $cfg, HEMI"
  exit 1;
endif

set fwhm = `grep \#FWHM $cfg | awk '{print $2}'`;
if($#fwhm != 1) then
  echo "ERROR: $cfg, FWHM"
  exit 1;
endif

set contrasts = `grep \#CONTRAST $cfg | awk '{print $2}'`;

foreach c ($contrasts)
  set cmat = $cdir/$c.mat
  if(! -e $cmat) then
    echo "ERROR: cannot find $cmat"
    exit 1;
  endif
end

echo setenv SUBJECTS_DIR $SUBJECTS_DIR

set StartDate = `date`;

echo measure $measure
echo hemi $hemi
echo fwhm $fwhm
echo nContrasts $#contrasts
foreach c ($contrasts)
  echo $c
end

if (-e $SUBJECTS_DIR/average) then
    set avgsubj="average"
    set glmflag=""
else if (-e $SUBJECTS_DIR/fsaverage) then
    set avgsubj="fsaverage"
    set glmflag=""
else if (-e $SUBJECTS_DIR/average7) then
    set avgsubj="average7"
    set glmflag="--really-use-average7"
else
    echo "An average subject was not found in $SUBJECTS_DIR"
    exit 1;
endif

#----------- Concatenate inputs ------------------#
echo "Concatenating Inputs"
set subjlist = `grep Input $fsgdf | awk '{print $2}'`;
set y = $qdecdir/y.mgh
set cmd = (mri_concat --o $y)
foreach subj ($subjlist)
  set infile = $SUBJECTS_DIR/$subj/surf/$hemi.$measure.fwhm$fwhm.${avgsubj}.mgh
  if(! -e $infile) then
    echo "ERROR: $infile does not exist"
    exit 1;
  endif
  set cmd = ($cmd --i $infile);
end
pwd
$cmd
if($status) exit 1;

mri_info --nframes $y --o /tmp/nframes.$$.dat
set nsubjects = `cat /tmp/nframes.$$.dat`;

# ----------- Run the GLM -------------------------
set cmd = (mri_glmfit --y $y --fsgd $fsgdf dods --glmdir $qdecdir \
  ${glmflag} --surf ${avgsubj} $hemi)
foreach c ($contrasts)
  set cmat = $cdir/$c.mat
  set cmd = ($cmd --C $cmat)
end
pwd
$cmd
if($status) exit 1;


#-------------------------------------------------------------
# Concatenate into one file with multiple frames
set concatfile = $qdecdir/contrasts.sig.mgh
set cmd = (mri_concat --o $concatfile)
foreach c ($contrasts)
  set sigfile = $qdecdir/$c/sig.mgh
  if(! -e $sigfile) then
    echo "ERROR: cannot find $sigfile"
    exit 1;
  endif
  set cmd = ($cmd --i $sigfile);
end
pwd
$cmd
if($status) exit 1;
set EndDate = `date`;

echo Started at $StartDate
echo Ended   at $EndDate


#-------------------------------------------------------------------#
set tclfile = $qdecdir/qdec.show.tcl
rm -f $tclfile

echo toplevel .infoDlog >> $tclfile
echo wm title .infoDlog \"QDEC Contrasts\" >> $tclfile
echo frame .infoDlog.fw >> $tclfile
echo tixScrolledText .infoDlog.fw.st -scrollbar y >> $tclfile
echo [.infoDlog.fw.st subwidget text] config -wrap word -relief ridge -bd 1 >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"nSubjects $nsubjects \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"Measure $measure \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"FWHM $fwhm \\n\" >> $tclfile

echo [.infoDlog.fw.st subwidget text] insert end \" \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \" Overlay Layer 1: Contrasts\\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \" Overlay Layer 2: Residual Error StdDev\\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \" Overlay Layer 3: Regression Coefficients\\n\" >> $tclfile

echo [.infoDlog.fw.st subwidget text] insert end \" \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"TimePoint \\t Contrast \\n\" >> $tclfile
@ nthc = 0;
foreach c ($contrasts)
  set nthcpad = `printf \#Q%03d $nthc`;
  set q = `grep $nthcpad $cfg| sed 's/\#Q//'`;
  echo [.infoDlog.fw.st subwidget text] insert end \"$nthc \\t "$q" \\n\" >> $tclfile
  @ nthc = $nthc + 1;
end

echo [.infoDlog.fw.st subwidget text] insert end \" \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"To change contrast, change the TimePoint \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"using the View-\>Config-\>Overlay panel.\\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \" \\n\" >> $tclfile

echo [.infoDlog.fw.st subwidget text] insert end \" \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"SUBJECTS_DIR $SUBJECTS_DIR \\n\" >> $tclfile
echo [.infoDlog.fw.st subwidget text] insert end \"qdecdir $qdecdir \\n\" >> $tclfile

echo pack .infoDlog.fw.st -expand yes -fill both >> $tclfile
echo pack .infoDlog.fw -expand yes -fill both >> $tclfile

echo "open_window" >> $tclfile
echo "sclv_read_from_volume 2 beta.mgh 2" >> $tclfile
echo "sclv_read_from_volume 1 rstd.mgh 2" >> $tclfile
echo "sclv_read_from_volume 0 contrasts.sig.mgh 2" >> $tclfile
echo "GDF_Load y.fsgd 0" >> $tclfile
echo "set gaLinkedVars(currentvaluefield) 0"  >> $tclfile
echo "set curv $hemi.curv" >> $tclfile
echo "read_binary_curv; " >> $tclfile
echo "set curvflag 1; " >> $tclfile
echo "redraw" >> $tclfile

echo "cd $qdecdir"
echo "tksurfer ${avgsubj} $hemi inflated -tcl qdec.show.tcl"

cd $qdecdir
tksurfer ${avgsubj} $hemi inflated -tcl qdec.show.tcl





exit 0

