#!/bin/sh -xu

# print commands with -x and exit if any shell variables are undefined (-u)

# All tutorial commands, commented out commands that are listed in the tutorial but are not run for time reasons. 

date 

LIBREOFFICE="libreoffice"
if [ "$(uname)" = "Darwin" ]; then
   LIBREOFFICE="/Applications/LibreOffice.app/Contents/MacOS/soffice"
fi

# FOR DEV BRANCH
# -no-auto-load option does not exist in 6.0
# FREEVIEW="freeview -no-auto-load"

# 6.0.0 RELEASE: Note that 6.0.0 version of freeview always exits with status 0 when using -quit option
FREEVIEW="freeview -quit"
# FREEVIEW="strace -e trace=file freeview"

# Use AppImage to run latest dev version of freeview - which can exit with non-zero status even with -quit
# AppImage file needs -no-auto-load else issues error it cannot find sphere.reg files ?
# FREEVIEW="/home/fsuser3/freesurfer/bin/freeview-dev20190624-x86_64.AppImage -no-auto-load"
# FREEVIEW="$TUTORIAL_DATA/freeview-dev20190709-x86_64.AppImage -no-auto-load"
# FREEVIEW="strace -e trace=file /home/fsuser3/freesurfer/bin/freeview-dev20190624-x86_64.AppImage -no-auto-load"

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs
cd $SUBJECTS_DIR

set -e

# https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/OutputData_freeview

echo "Freeview Command 1: load several output volumes"
$FREEVIEW -v \
good_output/mri/T1.mgz \
good_output/mri/wm.mgz \
good_output/mri/brainmask.mgz \
good_output/mri/aseg.mgz:colormap=lut:opacity=0.2 \
-f good_output/surf/lh.white:edgecolor=blue \
good_output/surf/lh.pial:edgecolor=red \
good_output/surf/rh.white:edgecolor=blue \
good_output/surf/rh.pial:edgecolor=red

echo "Freeview Command 2: view surface overlays"
$FREEVIEW -f  good_output/surf/lh.pial:annot=aparc.annot:name=pial_aparc:visible=0 \
good_output/surf/lh.pial:annot=aparc.a2009s.annot:name=pial_aparc_des:visible=0 \
good_output/surf/lh.inflated:overlay=lh.thickness:overlay_threshold=0.1,3::name=inflated_thickness:visible=0 \
good_output/surf/lh.inflated:visible=0 \
good_output/surf/lh.white:visible=0 \
good_output/surf/lh.pial \
--viewport 3d

# ADDED 9/2018 from Matt
echo "Freeview Command 2.5"
$FREEVIEW -v 004/mri/aparc+aseg.mgz:colormap=lut

# ADDED 9/2018 from Matt
echo "Freeview Command 2.6"
$FREEVIEW -f 004/surf/lh.pial:overlay=lh.thickness:overlay_threshold=1,2 --viewport 3d

# ADDED 9/2018 from Matt
echo "Freeview Command 2.7"
$FREEVIEW -v 004/mri/brainmask.mgz 004/mri/wm.mgz:colormap=jet:opacity=.2 -f 004/surf/lh.pial:edgecolor=blue 004/surf/lh.white:edgecolor=red 004/surf/rh.pial:edgecolor

# https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/Practice

# FINDING THE CORECT INPUT FOR THE FS PIPELINE

export SUBJECTS_DIR=$TUTORIAL_DATA/practice_with_data
cd $SUBJECTS_DIR

# Create scan.log from dicoms
cd $TUTORIAL_DATA/practice_with_data/dicoms
dcmunpack -src . -scanonly scan.log
cat scan.log
echo $SUBJECTS_DIR

# export SUBJECTS_DIR=$TUTORIAL_DATA/practice_with_data
### takes a long time
# recon-all -all -i I50 -s  Subj001

# ADDED
cd $TUTORIAL_DATA/dicom_challenge/dicoms 
dcmunpack -src . -scanonly scan.log

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs
cd $SUBJECTS_DIR

# https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/TroubleshootingDataV6.0

echo "Freeview Command 3: open pial_edits_before"
$FREEVIEW -v  pial_edits_before/mri/T1.mgz  \
pial_edits_before/mri/brainmask.mgz  \
-f pial_edits_before/surf/lh.white:edgecolor=yellow \
pial_edits_before/surf/lh.pial:edgecolor=red \
pial_edits_before/surf/rh.white:edgecolor=yellow \
pial_edits_before/surf/rh.pial:edgecolor=red


echo "Freeview Command 4: open pial_edits_after"
$FREEVIEW -v  pial_edits_after/mri/T1.mgz  \
pial_edits_after/mri/brainmask.mgz  \
-f pial_edits_after/surf/lh.white:edgecolor=yellow \
pial_edits_after/surf/lh.pial:edgecolor=red \
pial_edits_after/surf/rh.white:edgecolor=yellow \
pial_edits_after/surf/rh.pial:edgecolor=red

echo "Freeview Command 5: look at next subject, wm1_edits_before"
$FREEVIEW -v wm1_edits_before/mri/brainmask.mgz \
wm1_edits_before/mri/wm.mgz:colormap=heat:opacity=0.4 \
-f wm1_edits_before/surf/lh.white:edgecolor=yellow \
wm1_edits_before/surf/lh.pial:edgecolor=red \
wm1_edits_before/surf/rh.white:edgecolor=yellow \
wm1_edits_before/surf/rh.pial:edgecolor=red \
wm1_edits_before/surf/rh.inflated:visible=0 \
wm1_edits_before/surf/lh.inflated:visible=0

echo "Freeview Command 6: view problem with white matter segmentation map"
$FREEVIEW -v wm1_edits_after/mri/T1.mgz  \
wm1_edits_after/mri/brainmask.mgz  \
-f wm1_edits_after/surf/lh.white:edgecolor=yellow \
wm1_edits_after/surf/lh.pial:edgecolor=red \
wm1_edits_after/surf/rh.white:edgecolor=yellow \
wm1_edits_after/surf/rh.pial:edgecolor=red
:
echo "Freeview Command 7: view topo_defect_before"
$FREEVIEW -v topo_defect_before/mri/brainmask.mgz \
topo_defect_before/mri/wm.mgz:colormap=heat:opacity=0.4 \
-f topo_defect_before/surf/lh.white:edgecolor=yellow \
topo_defect_before/surf/lh.pial:edgecolor=red \
topo_defect_before/surf/rh.white:edgecolor=yellow \
topo_defect_before/surf/rh.pial:edgecolor=red

echo "Freeview Command 8: view topo_defect_after"
$FREEVIEW -v topo_defect_after/mri/brainmask.mgz \
topo_defect_after/mri/wm.mgz:colormap=heat:opacity=0.4 \
-f topo_defect_after/surf/lh.white:edgecolor=yellow \
topo_defect_after/surf/lh.pial:edgecolor=red \
topo_defect_after/surf/rh.white:edgecolor=yellow \
topo_defect_after/surf/rh.pial:edgecolor=red

echo "Freeview Command 9: view skullstrip_before"
$FREEVIEW -v skullstrip_before/mri/T1.mgz \
skullstrip_before/mri/brainmask.mgz:visible=false \
-f skullstrip_before/surf/lh.white:edgecolor=yellow \
skullstrip_before/surf/lh.pial:edgecolor=red \
skullstrip_before/surf/rh.white:edgecolor=yellow \
skullstrip_before/surf/rh.pial:edgecolor=red

echo "Freeview Command 10: view skullstrip_after"
$FREEVIEW -v skullstrip_after/mri/T1.mgz \
skullstrip_after/mri/brainmask.mgz \
-f skullstrip_after/surf/lh.white:edgecolor=yellow \
skullstrip_after/surf/lh.pial:edgecolor=red \
skullstrip_after/surf/rh.white:edgecolor=yellow \
skullstrip_after/surf/rh.pial:edgecolor=red

# Adding control points (intensity normalization)

echo "Freeview Command 11: view cp_before"
$FREEVIEW -v cp_before/mri/brainmask.mgz \
cp_before/mri/T1.mgz \
-f cp_before/surf/lh.white:edgecolor=blue \
cp_before/surf/lh.pial:edgecolor=red \
cp_before/surf/rh.white:edgecolor=blue \
cp_before/surf/rh.pial:edgecolor=red

echo "Freeview Command 12: view cp_after"
$FREEVIEW -v cp_after/mri/brainmask.mgz \
cp_after/mri/T1.mgz \
-f cp_after/surf/lh.white:edgecolor=blue \
cp_after/surf/lh.pial:edgecolor=red \
cp_after/surf/rh.white:edgecolor=blue \
cp_after/surf/rh.pial:edgecolor=red

echo "Freeview Command 13"
#SUB PAGES OF THIS TUTORIAL:
$FREEVIEW -v pial_edits_before/mri/T1.mgz \
pial_edits_before/mri/brainmask.mgz \
-f pial_edits_before/surf/lh.white:edgecolor=yellow \
pial_edits_before/surf/lh.pial:edgecolor=red \
pial_edits_before/surf/rh.white:edgecolor=yellow \
pial_edits_before/surf/rh.pial:edgecolor=red

#recon-all -autorecon-pial -subjid pial_edits_before
#cd <subjid>/mri
#cp brain.finalsurfs.mgz brain.finalsurfs.manedit.mgz
#recon-all -autorecon-pial -subjid pial_edits_before

echo "Freeview Command 14"
$FREEVIEW -v wm1_edits_before/mri/brainmask.mgz \
wm1_edits_before/mri/wm.mgz:colormap=heat:opacity=0.4 \
-f wm1_edits_before/surf/lh.white:edgecolor=yellow \
wm1_edits_before/surf/lh.pial:edgecolor=red \
wm1_edits_before/surf/rh.white:edgecolor=yellow \
wm1_edits_before/surf/rh.pial:edgecolor=red \
wm1_edits_before/surf/rh.inflated:visible=0 \
wm1_edits_before/surf/lh.inflated:visible=0

echo "Freeview Command 15"
$FREEVIEW -v wm1_edits_after/mri/brainmask.mgz \
wm1_edits_after/mri/wm.mgz:colormap=heat:opacity=0.4 \
-f wm1_edits_after/surf/lh.white:edgecolor=yellow \
wm1_edits_after/surf/lh.pial:edgecolor=red \
wm1_edits_after/surf/rh.white:edgecolor=yellow \
wm1_edits_after/surf/rh.pial:edgecolor=red \
wm1_edits_after/surf/rh.inflated:visible=0 \
wm1_edits_after/surf/lh.inflated:visible=0

#recon-all -autorecon2-wm -autorecon3 -subjid wm1_edits_before

echo "Freeview Command 16"
$FREEVIEW -v topo_defect_before/mri/brainmask.mgz \
topo_defect_before/mri/wm.mgz:colormap=heat:opacity=0.4:visible=0 \
-f topo_defect_before/surf/lh.white:edgecolor=yellow \
topo_defect_before/surf/lh.pial:edgecolor=red \
topo_defect_before/surf/rh.white:edgecolor=yellow \
topo_defect_before/surf/rh.pial:edgecolor=red \
topo_defect_before/surf/lh.smoothwm.nofix:visible=0

echo "Freeview Command 17"
$FREEVIEW -v topo_defect_after/mri/brainmask.mgz \
topo_defect_after/mri/wm.mgz:colormap=heat:opacity=0.4:visible=0 \
-f topo_defect_after/surf/lh.white:edgecolor=yellow \
topo_defect_after/surf/lh.pial:edgecolor=red \
topo_defect_after/surf/rh.white:edgecolor=yellow \
topo_defect_after/surf/rh.pial:edgecolor=red \
topo_defect_after/surf/lh.smoothwm.nofix:visible=0

#recon-all -autorecon2-wm -autorecon3 -subjid topo_defect_before

echo "Freeview Command 18"
$FREEVIEW -v skullstrip_before/mri/T1.mgz \
skullstrip_before/mri/brainmask.mgz:colormap=heat:visible=false \
skullstrip_before/mri/aparc+aseg.mgz:colormap=lut:opacity=0.2 \
-f skullstrip_before/surf/lh.white:edgecolor=yellow \
skullstrip_before/surf/lh.pial:edgecolor=red \
skullstrip_before/surf/rh.white:edgecolor=yellow \
skullstrip_before/surf/rh.pial:edgecolor=red

#recon-all -skullstrip -wsthresh <h> -clean-bm -subjid <subject name>
#recon-all -skullstrip -wsthresh <h> -clean-bm -no-wsgcaatlas -subjid skullstrip_before

echo "Freeview Command 19"
$FREEVIEW -v skullstrip_after/mri/T1.mgz \
skullstrip_after/mri/brainmask.mgz:colormap=heat

#recon-all -skullstrip -clean-bm -gcut -subjid <subject name>
#recon-all -autorecon-pial -subjid <subject name>
#recon-all -all -subjid <subject name>

echo "Freeview Command 20"
$FREEVIEW -v cp_before/mri/brainmask.mgz \
-f cp_before/surf/lh.white:edgecolor=yellow \
cp_before/surf/lh.pial:edgecolor=red \
cp_before/surf/rh.white:edgecolor=yellow \
cp_before/surf/rh.pial:edgecolor=red

#recon-all -autorecon2-cp -autorecon3 -subjid cp_before

# Assembling data, mris_preproc

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial
cd $SUBJECTS_DIR/glm

# takes a while

# mris_preproc --fsgd gender_age.fsgd \
#  --target fsaverage \
#  --hemi lh \
#  --meas thickness \
#  --out lh.gender_age.thickness.00.mgh

# resample the subject left hemisphere data to fsaverage

# mri_surf2surf --hemi lh \
#   --s fsaverage \
#   --sval lh.gender_age.thickness.00.mgh \
#   --fwhm 10 \
#   --cortex \
#   --tval lh.gender_age.thickness.10.mgh

# GLM Analysis

mri_glmfit \
  --y lh.gender_age.thickness.10.mgh \
  --fsgd gender_age.fsgd dods \
  --C lh-Avg-thickness-age-Cor.mtx \
  --surf fsaverage lh \
  --cortex \
  --glmdir lh.gender_age.glmdir

ls lh.gender_age.glmdir
ls lh.gender_age.glmdir/lh-Avg-thickness-age-Cor
cd $SUBJECTS_DIR/glm

echo "Freeview Command 21: view GLM analysis data"
$FREEVIEW -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/sig.mgh:overlay_threshold=4,5 -viewport 3d -layout 1

echo "Freeview Command 22: display F ratio"
$FREEVIEW -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/F.mgh:overlay_threshold=20,50 -viewport 3d -layout 1

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial_perm
cd $SUBJECTS_DIR/glm

# Same command as before to get initial anaylsis which produces uncorrected results
mri_glmfit --y lh.gender_age.thickness.10.mgh --fsgd gender_age.fsgd dods \
--C lh-Avg-thickness-age-Cor.mtx --surf fsaverage lh --cortex --glmdir lh.gender_age.glmdir \
--eres-save

# takes a while

#mri_glmfit-sim \
#  --glmdir lh.gender_age.glmdir \
#  --perm 1000 4.0 abs \
#  --cwp  0.05\
#  --2spaces \
#  --bg 1

# ADDED FOR MATT 9/2019

# output file produced that will prevent a 2bd run if it exists
# rm -f $TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial_perm/glm/glm_challenge/csd/perm.th40.abs.j001-challenge-Cor.csd 

rm -f challenge-Cor.mtx 
touch challenge-Cor.mtx 
# gedit challenge-Cor.mtx 
echo '1 -1 0 0' >> challenge-Cor.mtx
cat challenge-Cor.mtx 
mri_glmfit --y lh.gender_age.thickness.10.mgh --fsgd gender_age.fsgd dods --C challenge-Cor.mtx --surf fsaverage lh --cortex --glmdir glm_challenge 
echo "Freeview Command 21.1"
$FREEVIEW -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:overlay=glm_challenge/challenge-Cor/sig.mgh:overlay_threshold=4,5 -viewport 3d -layout 1

# ADDED FOR MATT 9/2019
mri_glmfit --y lh.gender_age.thickness.10.mgh --fsgd gender_age.fsgd dods --C challenge-Cor.mtx --surf fsaverage lh --cortex --glmdir glm_challenge --eres-save 

# old laptops may be too slow to run and even 1 job will exit uncelanly
# mri_glmfit-sim --glmdir glm_challenge --perm 10 4.0 abs --cwp 0.05 --2spaces --bg 1 

echo "Freeview Command 21.2"
$FREEVIEW -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:overlay=glm_challenge/challenge-Cor/perm.th40.abs.sig.cluster.mgh:overlay_threshold=2,5:annot=glm_challenge/challenge-Cor/perm.th40.abs.sig.ocn.annot -viewport 3d -layout 1 


# look at corrected results
ls lh.gender_age.glmdir/lh-Avg-thickness-age-Cor
# less lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/perm.th40.abs.sig.cluster.summary
cat lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/perm.th40.abs.sig.cluster.summary

echo "Freeview Command 23: load the cluster annotation"
$FREEVIEW -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:overlay=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/perm.th40.abs.sig.cluster.mgh:overlay_threshold=2,5:annot=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/perm.th40.abs.sig.ocn.annot -viewport 3d -layout 1

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial
cd $SUBJECTS_DIR
# recon-all -s <subjid> -qcache

# No more links outside of ./tutorial_data
# ln -sf $FREESURFER_HOME/subjects/fsaverage
export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial

cd $SUBJECTS_DIR
# qdec &
mri_label2label --srclabel lh.supramarg --srcsubject fsaverage --trgsubject 004 --trglabel lh.supramarg --regmethod surface --hemi lh

cd $SUBJECTS_DIR
mris_anatomical_stats -l lh.supramarg.label \
  -t lh.thickness -b -f 004/stats/lh.supramarg.stats 004 lh

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial
cd $SUBJECTS_DIR

# relationship between segmentation, parcellation and LookUp Table (LUT)

echo "Freeview Command 24: open orig and aprc+aseg segmentation volumes"
$FREEVIEW -v 004/mri/orig.mgz \
004/mri/aparc+aseg.mgz:colormap=lut:opacity=0.4 \
-f 004/surf/lh.white:annot=aparc.annot

# less $FREESURFER_HOME/FreeSurferColorLUT.txt
cat $FREESURFER_HOME/FreeSurferColorLUT.txt

# map a label to indicidal subjects 
cd $SUBJECTS_DIR
mri_label2label \
  --srcsubject fsaverage \
  --srclabel $FREESURFER_HOME/subjects/fsaverage/label/lh.BA45_exvivo.label \
  --trgsubject 004 \
  --trglabel 004/label/lh.BA45_exvivo.label \
  --hemi lh \
  --regmethod surface

echo "Freeview Command 25: view the label for the subject"
$FREEVIEW -v 004/mri/orig.mgz

echo "Freeview Command 26: load subjects inflated surface"
$FREEVIEW -f 004/surf/lh.inflated

# look at aseg stats
cd $SUBJECTS_DIR/004/stats
# less aseg.stats
cat aseg.stats

# look at aparc stats
cd $SUBJECTS_DIR/004/stats
# less lh.aparc.stats
cat lh.aparc.stats

# create a stats file for BA45_exvivo.label
cd $SUBJECTS_DIR/004
mris_anatomical_stats \
-l label/lh.BA45_exvivo.label \
-f stats/lh.BA45_exvivo.stats \
004 \
lh

# GROUP STATS FILES

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs/group_analysis_tutorial
cd $SUBJECTS_DIR

# Table of segmentation volumes

# create a segmentation table for 6 subjects
asegstats2table --subjects 004 021 040 067 080 092 \
  --segno 11 17 18 \
  --tablefile aseg.vol.table

# less aseg.vol.table
cat aseg.vol.table

# ADDED for Matt 9/2018
asegstats2table --subjects 004 021 092 --meas mean --tablefile mean.practice.table 
less mean.practice.table

# gedit aseg.vol.table
# open -e aseg.vol.table

# Table of white matter parcelation volumes

# change the segmentation atlas for stats
# asegstats2table \
#  --subjects 004 021 040 067 080 092 \
#  --segno 11 17 18 \
#  --meas mean \
#  --tablefile aseg.mean-intensity.table

# change the segmentation atlas for stats
asegstats2table \
  --subjects 004 021 040 067 080 092 \
  --segno  3007 3021 3022 4022\
  --stats wmparc.stats \
  --tablefile wmparc.vol.table

# create a table of the surface area of each cortical parcellation in the Desikan/Killiany atlas

aparcstats2table --hemi lh \
  --subjects 004 021 040 067 080 092 \
  --tablefile lh.aparc.area.table


aparcstats2table --hemi lh \
  --subjects 004 021 040 067 080 092 \
  --meas thickness \
  --parc aparc.a2009s \
  --tablefile lh.aparc.a2009.thickness.table

#aparcstats2table --help
#asegstats2table --help

# ADDED
aparcstats2table --subjects 004 021 040 --hemi lh --meas thickness --parc aparc.a2009s --tablefile lh.aparc.a2009s.thickness.table 
less mean.practice.table
# soffice --calc lh.aparc.a2009s.thickness.table


export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs
cd $SUBJECTS_DIR/multimodal/fmri/fbirn-101

echo "Freeview Command 27"
$FREEVIEW -v template.nii \
    $SUBJECTS_DIR/fbirn-anat-101.v6/mri/orig.mgz:visible=0 -f \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/lh.white \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/rh.white \
    -viewport cor -transform-volume
  bbregister --mov template.nii --bold --s fbirn-anat-101.v6 --lta register.lta
cat register.lta
cat register.dat.mincost

echo "Freeview Command 28"
$FREEVIEW -v $SUBJECTS_DIR/fbirn-anat-101.v6/mri/orig.mgz:visible=0 \
    template.nii:reg=register.lta -f \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/lh.white \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/rh.white \
    -viewport cor -transform-volume

export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs
cd $SUBJECTS_DIR/multimodal/fmri/fbirn-101

echo "Freeview Command 29"
$FREEVIEW -v $SUBJECTS_DIR/fbirn-anat-101.v6/mri/orig.mgz:visible=0 \
    template.nii:reg=register.lta -f \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/lh.white \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/rh.white \
    -viewport cor -transform-volume

echo "Freeview Command 30"
$FREEVIEW -v $SUBJECTS_DIR/fbirn-anat-101.v6/mri/orig.mgz \
    $SUBJECTS_DIR/fbirn-anat-101.v6/mri/aparc+aseg.mgz:colormap=lut:opacity=0.2 \
    sig.nii:reg=register.lta:colormap=heat:heatscale=2,2,4 -colorscale \
    -viewport coronal
mri_vol2surf --mov sig.nii \
    --reg register.lta \
    --projfrac 0.5 --interp nearest \
    --hemi lh --o lh.sig.mgh
mri_info lh.sig.mgh

echo "Freeview Command 31"
$FREEVIEW -f $SUBJECTS_DIR/fbirn-anat-101.v6/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.sig.mgh:overlay_threshold=2,5 \
    -viewport 3d

mri_vol2vol --mov ces.nii \
    --reg register.lta \
    --fstarg --interp nearest \
    --o ces.anat.bb.mgh
mri_info ces.anat.bb.mgh
mri_segstats \
   --seg $SUBJECTS_DIR/fbirn-anat-101.v6/mri/aparc+aseg.mgz \
   --ctab $FREESURFER_HOME/FreeSurferColorLUT.txt \
   --id 1021 --id 1022 --id 1030  --id 17 \
   --i ces.anat.bb.mgh --sum ces.bb.stats
mri_vol2vol --mov sig.nii \
    --reg register.lta \
    --fstarg --interp nearest \
    --o sig.anat.bb.mgh
mri_segstats \
   --seg $SUBJECTS_DIR/fbirn-anat-101.v6/mri/aparc+aseg.mgz \
   --ctab $FREESURFER_HOME/FreeSurferColorLUT.txt \
   --id 1021 --id 1022 --id 1030  --id 17 \
   --i ces.anat.bb.mgh --sum ces.abs-masked.bb.stats \
   --mask sig.anat.bb.mgh --maskthresh 2 --masksign abs
mri_vol2vol --mov sig.nii \
    --reg register.lta \
    --fstarg --interp nearest \
    --o sig.anat.bb.mgh
mri_segstats \
   --seg $SUBJECTS_DIR/fbirn-anat-101.v6/mri/aparc+aseg.mgz \
   --ctab $FREESURFER_HOME/FreeSurferColorLUT.txt \
   --id 1021 --id 1022 --id 1030  --id 17 \
   --i ces.anat.bb.mgh --sum ces.pos-masked.bb.stats \
   --mask sig.anat.bb.mgh --maskthresh 2 --masksign pos


export SUBJECTS_DIR=$TUTORIAL_DATA/buckner_data/tutorial_subjs
cd $SUBJECTS_DIR/multimodal/fmri

echo "Freeview Command 32"
$FREEVIEW -v $SUBJECTS_DIR/fbirn-anat-101.v6/mri/orig.mgz:visible=0 \
    fbirn-101/template.nii:reg=fbirn-101/register.lta -f \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/lh.white \
    $SUBJECTS_DIR/fbirn-anat-101.v6/surf/rh.white \
    -viewport cor
mris_preproc --target fsaverage --hemi lh \
  --iv  fbirn-101/ces.nii fbirn-101/register.lta \
  --iv  fbirn-103/ces.nii fbirn-103/register.lta \
  --iv  fbirn-104/ces.nii fbirn-104/register.lta \
  --iv  fbirn-105/ces.nii fbirn-105/register.lta \
  --iv  fbirn-106/ces.nii fbirn-106/register.lta \
  --projfrac 0.5 \
  --out lh.ces.mgh
mri_info lh.ces.mgh
mri_surf2surf --hemi lh --s fsaverage --fwhm 5 --cortex \
  --sval lh.ces.mgh --tval lh.ces.sm05.mgh
mri_glmfit --y lh.ces.sm05.mgh --surf fsaverage lh \
  --osgm --glmdir lh.ces.sm05.osgm --cortex

echo "Freeview Command 33"
$FREEVIEW -f \
    $SUBJECTS_DIR/fsaverage/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.ces.sm05.osgm/osgm/sig.mgh:overlay_threshold=2,5 \
    -viewport 3d
asegstats2table \
  --meas volume \
  --tablefile ces.pos-masked.vol.stats \
  --i fbirn-101/ces.pos-masked.bb.stats \
  fbirn-103/ces.pos-masked.bb.stats \
  fbirn-104/ces.pos-masked.bb.stats \
  fbirn-105/ces.pos-masked.bb.stats \
  fbirn-106/ces.pos-masked.bb.stats
cat ces.pos-masked.vol.stats
asegstats2table \
  --meas mean \
  --tablefile ces.abs-masked.mean.stats \
  --i fbirn-101/ces.abs-masked.bb.stats \
  fbirn-103/ces.abs-masked.bb.stats \
  fbirn-104/ces.abs-masked.bb.stats \
  fbirn-105/ces.abs-masked.bb.stats \
  fbirn-106/ces.abs-masked.bb.stats
cat ces.abs-masked.mean.stats
asegstats2table \
  --meas mean \
  --tablefile ces.pos-masked.mean.stats \
  --i fbirn-101/ces.pos-masked.bb.stats \
  fbirn-103/ces.pos-masked.bb.stats \
  fbirn-104/ces.pos-masked.bb.stats \
  fbirn-105/ces.pos-masked.bb.stats \
  fbirn-106/ces.pos-masked.bb.stats
cat ces.pos-masked.mean.stats

export SUBJECTS_DIR=$TUTORIAL_DATA/long-tutorial
cd $SUBJECTS_DIR
# recon-all -subjid OAS2_0001_MR1 -all
# recon-all -subjid OAS2_0001_MR2 -all
# recon-all -base OAS2_0001 -tp OAS2_0001_MR1 -tp OAS2_0001_MR2 -all
# recon-all -long OAS2_0001_MR1 OAS2_0001 -all
# recon-all -long OAS2_0001_MR2 OAS2_0001 -all

echo "Freeview Command 34"
$FREEVIEW -v OAS2_0001/mri/norm.mgz \
         -f OAS2_0001/surf/lh.pial:edgecolor=red \
            OAS2_0001/surf/rh.pial:edgecolor=red \
            OAS2_0001/surf/lh.white:edgecolor=blue \
            OAS2_0001/surf/rh.white:edgecolor=blue

echo "Freeview Command 35"
$FREEVIEW -v OAS2_0001_MR1.long.OAS2_0001/mri/norm.mgz \
            OAS2_0001_MR2.long.OAS2_0001/mri/norm.mgz \
         -f OAS2_0001_MR1.long.OAS2_0001/surf/lh.pial:edgecolor=red \
            OAS2_0001_MR1.long.OAS2_0001/surf/lh.white:edgecolor=blue \
            OAS2_0001_MR2.long.OAS2_0001/surf/lh.pial:edgecolor=255,128,128 \
            OAS2_0001_MR2.long.OAS2_0001/surf/lh.white:edgecolor=lightblue
# libreoffice -calc qdec/long.qdec.table.dat
# $LIBREOFFICE -calc qdec/long.qdec.table.dat

# coment out unless can automatically close
# gedit qdec/long.qdec.table.dat

# long_mris_slopes --qdec ./qdec/long.qdec.table.dat --meas thickness --hemi lh --do-avg --do-rate --do-pc1 --do-spc --do-stack --do-label --time years --qcache fsaverage --sd $SUBJECTS_DIR
cd qdec 
long_qdec_table --qdec ./long.qdec.table.dat --split fsid-base  
cd ..

echo "Freeview Command 36"
$FREEVIEW -f OAS2_0001/surf/lh.pial:overlay=OAS2_0001/surf/lh.long.thickness-avg.fwhm15.mgh:overlay_threshold=0,3.5:overlay=OAS2_0001/surf/lh.long.thickness-stack.mgh:annot=OAS2_0001/label/lh.aparc.annot:annot_outline=1 --timecourse --colorscale --viewport 3d

# Cannot pass quit option
# tksurfer OAS2_0001 lh pial -overlay $SUBJECTS_DIR/OAS2_0001/surf/lh.long.thickness-avg.fwhm15.mgh -timecourse $SUBJECTS_DIR/OAS2_0001/surf/lh.long.thickness-stack.mgh -aparc

echo "Freeview Command 37"
$FREEVIEW -f $FREESURFER_HOME/subjects/fsaverage/surf/lh.pial:overlay=$SUBJECTS_DIR/OAS2_0001/surf/lh.long.thickness-spc.fwhm15.fsaverage.mgh:overlay_threshold=2,5
long_qdec_table --qdec ./qdec/long.qdec.table.dat --cross --out ./qdec/cross.qdec.table.dat

# time ?
# qdec --table ./qdec/cross.qdec.table.dat

export SUBJECTS_DIR=$TUTORIAL_DATA/long-tutorial/skullstrip
cd $SUBJECTS_DIR

# set +e

$FREEVIEW -v OAS2_0004/mri/T1.mgz \
            OAS2_0004/mri/brainmask.mgz \
         -f OAS2_0004/surf/lh.pial:edgecolor=red \
            OAS2_0004/surf/rh.pial:edgecolor=red \
            OAS2_0004/surf/lh.white:edgecolor=blue \
            OAS2_0004/surf/rh.white:edgecolor=blue

$FREEVIEW -v OAS2_0004_MR1/mri/T1.mgz \
            OAS2_0004_MR1/mri/brainmask.mgz \
         -f OAS2_0004_MR1/surf/lh.pial:edgecolor=red \
            OAS2_0004_MR1/surf/rh.pial:edgecolor=red \
            OAS2_0004_MR1/surf/lh.white:edgecolor=blue \
            OAS2_0004_MR1/surf/rh.white:edgecolor=blue

$FREEVIEW -v OAS2_0004_MR2/mri/T1.mgz \
            OAS2_0004_MR2/mri/brainmask.mgz \
         -f OAS2_0004_MR2/surf/lh.pial:edgecolor=red \
            OAS2_0004_MR2/surf/rh.pial:edgecolor=red \
            OAS2_0004_MR2/surf/lh.white:edgecolor=blue \
            OAS2_0004_MR2/surf/rh.white:edgecolor=blue

# set -e

# recon-all -subjid OAS2_0004_MR1 -skullstrip -wsthresh 20 -clean-bm -no-wsgcaatlas
# recon-all -subjid OAS2_0004_MR2 -skullstrip -wsthresh 55 -clean-bm -no-wsgcaatlas
# recon-all -subjid OAS2_0004_MR1 -autorecon2 -autorecon3
# recon-all -subjid OAS2_0004_MR2 -autorecon2 -autorecon3

echo "Freeview Command 41"
$FREEVIEW -v OAS2_0004_MR1_fixed/mri/T1.mgz OAS2_0004_MR1_fixed/mri/brainmask.mgz

echo "Freeview Command 42"
$FREEVIEW -v OAS2_0004_MR2_fixed/mri/T1.mgz OAS2_0004_MR2_fixed/mri/brainmask.mgz
# recon-all -skullstrip -subjid OAS2_0004_MR2
# recon-all -autorecon2 -autorecon3 -subjid OAS2_0004_MR2
# recon-all -base OAS2_0004 -tp OAS2_0004_MR1 -tp OAS2_0004_MR2 -all
# recon-all -long OAS2_0004_MR1 OAS2_0004 -all
# recon-all -long OAS2_0004_MR2 OAS2_0004 -all

echo "Freeview Command 43"
$FREEVIEW -v OAS2_0004_fixed/mri/T1.mgz \
            OAS2_0004_fixed/mri/brainmask.mgz \
         -f OAS2_0004_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0004_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0004_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0004_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 44"
$FREEVIEW -v OAS2_0004_MR1.long.OAS2_0004_fixed/mri/T1.mgz \
            OAS2_0004_MR1.long.OAS2_0004_fixed/mri/brainmask.mgz \
         -f OAS2_0004_MR1.long.OAS2_0004_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0004_MR1.long.OAS2_0004_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0004_MR1.long.OAS2_0004_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0004_MR1.long.OAS2_0004_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 45"
$FREEVIEW -v OAS2_0004_MR2.long.OAS2_0004_fixed/mri/T1.mgz \
            OAS2_0004_MR2.long.OAS2_0004_fixed/mri/brainmask.mgz \
         -f OAS2_0004_MR2.long.OAS2_0004_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0004_MR2.long.OAS2_0004_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0004_MR2.long.OAS2_0004_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0004_MR2.long.OAS2_0004_fixed/surf/rh.white:edgecolor=blue
export SUBJECTS_DIR=$TUTORIAL_DATA/long-tutorial/
cd $SUBJECTS_DIR

echo "Freeview Command 46"
$FREEVIEW -v OAS2_0057/mri/T1.mgz \
            OAS2_0057/mri/brainmask.mgz \
         -f OAS2_0057/surf/lh.pial:edgecolor=red \
            OAS2_0057/surf/rh.pial:edgecolor=red \
            OAS2_0057/surf/lh.white:edgecolor=blue \
            OAS2_0057/surf/rh.white:edgecolor=blue

echo "Freeview Command 47"
$FREEVIEW -v OAS2_0057_MR1/mri/T1.mgz \
            OAS2_0057_MR1/mri/brainmask.mgz \
         -f OAS2_0057_MR1/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR1/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR1/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR1/surf/rh.white:edgecolor=blue

echo "Freeview Command 48"
$FREEVIEW -v OAS2_0057_MR2/mri/T1.mgz \
            OAS2_0057_MR2/mri/brainmask.mgz \
         -f OAS2_0057_MR2/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR2/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR2/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR2/surf/rh.white:edgecolor=blue

echo "Freeview Command 49"
$FREEVIEW -v OAS2_0057_MR1.long.OAS2_0057/mri/T1.mgz \
            OAS2_0057_MR1.long.OAS2_0057/mri/brainmask.mgz \
         -f OAS2_0057_MR1.long.OAS2_0057/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR1.long.OAS2_0057/surf/rh.white:edgecolor=blue

echo "Freeview Command 50"
$FREEVIEW -v OAS2_0057_MR2.long.OAS2_0057/mri/T1.mgz \
            OAS2_0057_MR2.long.OAS2_0057/mri/brainmask.mgz \
         -f OAS2_0057_MR2.long.OAS2_0057/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR2.long.OAS2_0057/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR2.long.OAS2_0057/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR2.long.OAS2_0057/surf/rh.white:edgecolor=blue
# recon-all -base OAS2_0057 -autorecon-pial
# recon-all -long OAS2_0057_MR1 OAS2_0057 -all
# recon-all -long OAS2_0057_MR2 OAS2_0057 -all

echo "Freeview Command 51"
$FREEVIEW -v OAS2_0057_MR1.long.OAS2_0057/mri/T1.mgz \
            OAS2_0057_MR1.long.OAS2_0057/mri/brainmask.mgz \
         -f OAS2_0057_MR1.long.OAS2_0057/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR1.long.OAS2_0057/surf/rh.white:edgecolor=blue

echo "Freeview Command 52"
$FREEVIEW -v OAS2_0057_MR1.long.OAS2_0057_fixed/mri/T1.mgz \
            OAS2_0057_MR1.long.OAS2_0057_fixed/mri/brainmask.mgz \
         -f OAS2_0057_MR1.long.OAS2_0057_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0057_MR1.long.OAS2_0057_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0057_MR1.long.OAS2_0057_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 53"
$FREEVIEW -v OAS2_0121_MR1/mri/T1.mgz \
            OAS2_0121_MR1/mri/brainmask.mgz \
         -f OAS2_0121_MR1/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR1/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR1/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR1/surf/rh.white:edgecolor=blue

echo "Freeview Command 54"
$FREEVIEW -v OAS2_0121_MR2/mri/T1.mgz \
            OAS2_0121_MR2/mri/brainmask.mgz \
         -f OAS2_0121_MR2/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR2/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR2/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR2/surf/rh.white:edgecolor=blue

echo "Freeview Command 55"
$FREEVIEW -v OAS2_0121/mri/T1.mgz \
            OAS2_0121/mri/brainmask.mgz \
         -f OAS2_0121/surf/lh.pial:edgecolor=red \
            OAS2_0121/surf/rh.pial:edgecolor=red \
            OAS2_0121/surf/lh.white:edgecolor=blue \
            OAS2_0121/surf/rh.white:edgecolor=blue

echo "Freeview Command 56"
$FREEVIEW -v OAS2_0121_MR1.long.OAS2_0121/mri/T1.mgz \
            OAS2_0121_MR1.long.OAS2_0121/mri/brainmask.mgz \
         -f OAS2_0121_MR1.long.OAS2_0121/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR1.long.OAS2_0121/surf/rh.white:edgecolor=blue

echo "Freeview Command 57"
$FREEVIEW -v OAS2_0121_MR2.long.OAS2_0121/mri/T1.mgz \
            OAS2_0121_MR2.long.OAS2_0121/mri/brainmask.mgz \
         -f OAS2_0121_MR2.long.OAS2_0121/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR2.long.OAS2_0121/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR2.long.OAS2_0121/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR2.long.OAS2_0121/surf/rh.white:edgecolor=blue
# recon-all -subjid OAS2_0121_MR1 -autorecon2 -autorecon3
# recon-all -subjid OAS2_0121_MR2 -autorecon2 -autorecon3

echo "Freeview Command 58"
$FREEVIEW -v OAS2_0121_MR1_fixed/mri/T1.mgz \
            OAS2_0121_MR1_fixed/mri/brainmask.mgz \
         -f OAS2_0121_MR1_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR1_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR1_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR1_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 59"
$FREEVIEW -v OAS2_0121_MR2_fixed/mri/T1.mgz \
            OAS2_0121_MR2_fixed/mri/brainmask.mgz \
         -f OAS2_0121_MR2_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR2_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR2_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR2_fixed/surf/rh.white:edgecolor=blue
# recon-all -base OAS2_0121 -tp OAS2_0121_MR1 -tp OAS2_0121_MR2 -all

echo "Freeview Command 60"
$FREEVIEW -v OAS2_0121_intermediate/mri/T1.mgz \
            OAS2_0121_intermediate/mri/brainmask.mgz \
         -f OAS2_0121_intermediate/surf/lh.pial:edgecolor=red \
            OAS2_0121_intermediate/surf/rh.pial:edgecolor=red \
            OAS2_0121_intermediate/surf/lh.white:edgecolor=blue \
            OAS2_0121_intermediate/surf/rh.white:edgecolor=blue
# recon-all -base OAS2_0121 -autorecon2 -autorecon3
# recon-all -long OAS2_0121_MR1 OAS2_0121 -all
# recon-all -long OAS2_0121_MR2 OAS2_0121 -all

echo "Freeview Command 61"
$FREEVIEW -v OAS2_0121_MR1.long.OAS2_0121/mri/T1.mgz \
            OAS2_0121_MR1.long.OAS2_0121/mri/brainmask.mgz \
         -f OAS2_0121_MR1.long.OAS2_0121/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR1.long.OAS2_0121/surf/rh.white:edgecolor=blue

echo "Freeview Command 62"
$FREEVIEW -v OAS2_0121_MR1.long.OAS2_0121_fixed/mri/T1.mgz \
            OAS2_0121_MR1.long.OAS2_0121_fixed/mri/brainmask.mgz \
         -f OAS2_0121_MR1.long.OAS2_0121_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0121_MR1.long.OAS2_0121_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0121_MR1.long.OAS2_0121_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 63"
$FREEVIEW -v OAS2_0185/mri/wm.mgz \
            OAS2_0185/mri/brainmask.mgz \
         -f OAS2_0185/surf/lh.pial:edgecolor=red \
            OAS2_0185/surf/rh.pial:edgecolor=red \
            OAS2_0185/surf/lh.white:edgecolor=blue \
            OAS2_0185/surf/rh.white:edgecolor=blue

echo "Freeview Command 64"
$FREEVIEW -v OAS2_0185_MR1/mri/wm.mgz \
            OAS2_0185_MR1/mri/brainmask.mgz \
         -f OAS2_0185_MR1/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR1/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR1/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR1/surf/rh.white:edgecolor=blue

echo "Freeview Command 65"
$FREEVIEW -v OAS2_0185_MR2/mri/wm.mgz \
            OAS2_0185_MR2/mri/brainmask.mgz \
         -f OAS2_0185_MR2/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR2/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR2/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR2/surf/rh.white:edgecolor=blue

echo "Freeview Command 66"
$FREEVIEW -v OAS2_0185_MR1.long.OAS2_0185/mri/wm.mgz \
            OAS2_0185_MR1.long.OAS2_0185/mri/brainmask.mgz \
         -f OAS2_0185_MR1.long.OAS2_0185/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR1.long.OAS2_0185/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR1.long.OAS2_0185/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR1.long.OAS2_0185/surf/rh.white:edgecolor=blue

echo "Freeview Command 67"
$FREEVIEW -v OAS2_0185_MR2.long.OAS2_0185/mri/wm.mgz \
            OAS2_0185_MR2.long.OAS2_0185/mri/brainmask.mgz \
         -f OAS2_0185_MR2.long.OAS2_0185/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR2.long.OAS2_0185/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR2.long.OAS2_0185/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR2.long.OAS2_0185/surf/rh.white:edgecolor=blue
# recon-all -base OAS2_0185 -autorecon2-wm -autorecon3

echo "Freeview Command 68"
$FREEVIEW -v OAS2_0185_fixed/mri/wm.mgz \
            OAS2_0185_fixed/mri/brainmask.mgz \
         -f OAS2_0185_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0185_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0185_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0185_fixed/surf/rh.white:edgecolor=blue
# recon-all -long OAS2_0185_MR1 OAS2_0185 -all
# recon-all -long OAS2_0185_MR2 OAS2_0185 -all

echo "Freeview Command 69"
$FREEVIEW -v OAS2_0185_MR1.long.OAS2_0185_fixed/mri/wm.mgz \
            OAS2_0185_MR1.long.OAS2_0185_fixed/mri/brainmask.mgz \
         -f OAS2_0185_MR1.long.OAS2_0185_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR1.long.OAS2_0185_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR1.long.OAS2_0185_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR1.long.OAS2_0185_fixed/surf/rh.white:edgecolor=blue

echo "Freeview Command 70"
$FREEVIEW -v OAS2_0185_MR2.long.OAS2_0185_fixed/mri/wm.mgz \
            OAS2_0185_MR2.long.OAS2_0185_fixed/mri/brainmask.mgz \
         -f OAS2_0185_MR2.long.OAS2_0185_fixed/surf/lh.pial:edgecolor=red \
            OAS2_0185_MR2.long.OAS2_0185_fixed/surf/rh.pial:edgecolor=red \
            OAS2_0185_MR2.long.OAS2_0185_fixed/surf/lh.white:edgecolor=blue \
            OAS2_0185_MR2.long.OAS2_0185_fixed/surf/rh.white:edgecolor=blue

export SUBJECTS_DIR=$TUTORIAL_DATA/diffusion_recons
export TUTORIAL_DIR=$TUTORIAL_DATA/diffusion_tutorial
subj=Diff001
cd $TUTORIAL_DIR/$subj/dtrecon
# dt_recon --b bvals.dat bvecs.dat --i $TUTORIAL_DIR/$subj/orig/6-1.dcm --s $subj --o $TUTORIAL_DIR/$subj/dtrecon
ls

echo "Freeview Command 71"
$FREEVIEW dwi.nii.gz

echo "Freeview Command 72"
$FREEVIEW -v $SUBJECTS_DIR/$subj/mri/orig.mgz \
            lowb.nii:reg=register.dat \
         -f $SUBJECTS_DIR/$subj/surf/lh.white:edgecolor=green \
            $SUBJECTS_DIR/$subj/surf/rh.white:edgecolor=green \
         -viewport coronal

echo "Freeview Command 73"
$FREEVIEW -v fa.nii adc.nii

echo "Freeview Command 74"
$FREEVIEW -v $SUBJECTS_DIR/$subj/mri/brain.mgz \
            $SUBJECTS_DIR/$subj/mri/orig.mgz \
            $SUBJECTS_DIR/$subj/mri/wmparc.mgz:colormap=lut:opacity=0.2 \
            fa.nii:reg=register.dat:colormap=heat:heatscale=.2,.2,1 -colorscale -viewport coronal
mri_vol2vol --mov lowb.nii \
            --targ $SUBJECTS_DIR/$subj/mri/wmparc.mgz \
            --inv --interp nearest --o $SUBJECTS_DIR/$subj/mri/wmparc2diff.mgz \
            --reg register.dat --no-save-reg

echo "Freeview Command 75"
$FREEVIEW -v lowb.nii \
            $SUBJECTS_DIR/$subj/mri/wmparc2diff.mgz:colormap=lut \
         -f $SUBJECTS_DIR/$subj/surf/lh.white:edgecolor=green \
            $SUBJECTS_DIR/$subj/surf/rh.white:edgecolor=green \
         -viewport coronal

echo "Freeview Command 76"
$FREEVIEW -v fa.nii \
            $SUBJECTS_DIR/$subj/mri/aparc+aseg2diff.mgz:colormap=lut
mri_mask fa.nii \
         $SUBJECTS_DIR/$subj/mri/wmparc2diff.mgz \
         fa-masked.mgz

echo "Freeview Command 77"
$FREEVIEW -v fa.nii \
            fa-masked.mgz
mri_segstats \
    --seg $SUBJECTS_DIR/$subj/mri/wmparc2diff.mgz \
    --ctab $FREESURFER_HOME/FreeSurferColorLUT.txt \
    --id 251 --id 3021 --id 3024 --id 3030 --id 12 --id 4 \
    --i fa.nii --sum fa.stats
# mri_vol2vol --targ $FREESURFER_HOME/subjects/cvs_avg35/mri/norm.mgz \
#            --m3z $SUBJECTS_DIR/$subj/cvs/combined_tocvs_avg35_elreg_afteraseg-norm.m3z \
#            --noDefM3zPath \
#            --reg $TUTORIAL_DIR/$subj/dtrecon/register.dat \
#            --mov fa-masked.mgz \
#            --o fa-masked.ANAT+CVS-to-avg35.mgz \
#            --interp trilin --no-save-reg

echo "Freeview Command 78"
$FREEVIEW -v $FREESURFER_HOME/subjects/cvs_avg35/mri/norm.mgz \
            fa-masked.ANAT+CVS-to-avg35.mgz


export SUBJECTS_DIR=$TUTORIAL_DATA/diffusion_recons
cd $TUTORIAL_DATA/diffusion_tutorial

# gedit $TUTORIAL_DATA/diffusion_tutorial/dmrirc.tutorial &
# trac-all -prep -c $TUTORIAL_DATA/diffusion_tutorial/dmrirc.tutorial
# trac-all -bedp -c $TUTORIAL_DATA/diffusion_tutorial/dmrirc.tutorial
# trac-all -path -c $TUTORIAL_DATA/diffusion_tutorial/dmrirc.tutorial
# cat $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/dwi_motion.txt

cd $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri

echo "Freeview Command 79"
$FREEVIEW $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/dtifit_FA.nii.gz &
cd $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/mni/

echo "Freeview Command 80"
$FREEVIEW -v $FSL_DIR/data/standard/MNI152_T1_1mm_brain.nii.gz \
            $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/mni/dtifit_FA.bbr.nii.gz &

# set +e

# echo "Freeview Command 81"
# $FREEVIEW -v $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/dtifit_FA.nii.gz \
#            $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/rh.ilf_AS_avg33_mni_bbr/path.pd.nii.gz:colormap=heat:isosurface=0,0:color='Red':name=rh.ilf \
#            $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/lh.ilf_AS_avg33_mni_bbr/path.pd.nii.gz:colormap=heat:isosurface=0,0:color='Red':name=lh.ilf

# Remove :color sub-option (which fails in dev branch freeview)

echo "Freeview Command 81"
$FREEVIEW -v $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/dtifit_FA.nii.gz \
            $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/rh.ilf_AS_avg33_mni_bbr/path.pd.nii.gz:colormap=heat:isosurface=0,0:name=rh.ilf \
            $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/lh.ilf_AS_avg33_mni_bbr/path.pd.nii.gz:colormap=heat:isosurface=0,0:name=lh.ilf


# set -e

echo "Freeview Command 82"
$FREEVIEW -tv $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/merged_avg33_mni_bbr.mgz \
         -v $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dmri/dtifit_FA.nii.gz &

# gedit $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/lh.ilf_AS_avg33_mni_bbr/pathstats.overall.txt &

tractstats2table --inputs $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/lh.ilf_AS_avg33_mni_bbr/pathstats.overall.txt --overall --tablefile $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.elmo.2012.All.table

# gedit $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.elmo.2012.All.table &
# gedit $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.list &

# NEED TO FIND lh.ilf.list before run these commands
## tractstats2table --load-pathstats-from-file $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.list --overall --tablefile $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.All.table

# gedit $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.All.table &

## tractstats2table --load-pathstats-from-file $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.list --overall --only-measures FA_Avg --tablefile $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.FA_Avg.table
# localc $TUTORIAL_DATA/diffusion_tutorial/lh.ilf.All.table

# gedit $TUTORIAL_DATA/diffusion_tutorial/elmo.2012/dpath/lh.ilf_AS_avg33_mni_bbr/pathstats.byvoxel.txt &

trac-all -stat -c $TUTORIAL_DATA/diffusion_tutorial/dmrirc.tutorial

# gedit $TUTORIAL_DATA/diffusion_tutorial/stats/lh.ilf_AS.avg33_mni_bbr.FA_Avg.txt &
# gedit $TUTORIAL_DATA/diffusion_tutorial/stats/lh.ilf_AS.avg33_mni_bbr.log &

echo "Freeview Command 83"
$FREEVIEW -v $FSL_DIR/data/standard/MNI152_T1_1mm_brain.nii.gz \
         -w $TUTORIAL_DATA/diffusion_tutorial/stats/*.path.mean.txt

cd $TUTORIAL_DATA/fsfast-functional
ls
cd $TUTORIAL_DATA/fsfast-functional/sess01.noproc
ls
export SUBJECTS_DIR=$TUTORIAL_DATA/fsfast-tutorial.subjects
cat subjectname
ls $SUBJECTS_DIR
ls rest
ls bold
cd bold/001
ls
mri_info --dim f.nii.gz
mri_info --res f.nii.gz

echo "Freeview Command 84"
$FREEVIEW -v f.nii.gz -timecourse

cat workmem.par
cd $TUTORIAL_DATA/fsfast-functional
cat sessidlist
# preproc-sess -s sess01 -fsd bold -stc up -surface fsaverage lhrh -mni305 -fwhm 5 -per-run
export SUBJECTS_DIR=$TUTORIAL_DATA/fsfast-tutorial.subjects
cd $TUTORIAL_DATA/fsfast-functional
ls $TUTORIAL_DATA/fsfast-functional/sess01/bold/001
tkregister-sess -s sess01 -s sess02 -s sess03 -fsd bold -per-run -bbr-sum

# time ?
# tkregister-sess -s sess02 -fsd bold -per-run

cd $TUTORIAL_DATA/fsfast-functional/sess01/bold/001
ls
ls -ltr
mri_info template.nii.gz
mri_info f.nii.gz

echo "Freeview Command 85"
$FREEVIEW -v template.nii.gz masks/brain.nii.gz:colormap=heat:opacity=0.75 -viewport coronal

echo "Freeview Command 86"
$FREEVIEW -v template.nii.gz masks/brain.e3.nii.gz:colormap=heat:opacity=0.75 -viewport coronal

mri_info --dim fmcpr.up.sm5.mni305.2mm.nii.gz
mri_info --res fmcpr.up.sm5.mni305.2mm.nii.gz
mri_info --dim fmcpr.up.sm5.fsaverage.lh.nii.gz
mri_info --res fmcpr.up.sm5.fsaverage.lh.nii.gz
export SUBJECTS_DIR=$TUTORIAL_DATA/fsfast-tutorial.subjects
cd $TUTORIAL_DATA/fsfast-functional
mkanalysis-sess \
  -fsd bold -stc up  -surface fsaverage lh -fwhm 5  \
  -event-related  -paradigm workmem.par -nconditions 5 \
  -spmhrf 0 -TR 2 -refeventdur 16 -nskip 4 -polyfit 2 \
  -analysis my-workmem.sm05.lh  -per-run -force
ls my-workmem.sm05.lh

# more analysis.info
cat my-workmem.sm05.lh/analysis.info

mkcontrast-sess -analysis my-workmem.sm05.lh -contrast encode-v-base -a 1
ls my-workmem.sm05.lh
mkcontrast-sess -analysis my-workmem.sm05.lh -contrast emot.dist-v-neut.dist -a 2 -c 3
mkcontrast-sess -analysis my-workmem.sm05.lh -contrast distractor.avg-v-base -a 2 -a 3
mkcontrast-sess -analysis my-workmem.sm05.lh -contrast emot.dist-v-base -a 2
mkcontrast-sess -analysis my-workmem.sm05.lh -contrast probe.avg-v-base -a 4 -a 5
mkanalysis-sess \
  -fsd bold -stc up  -surface fsaverage rh -fwhm 5  \
  -event-related  -paradigm workmem.par -nconditions 5 \
  -spmhrf 0 -TR 2 -refeventdur 16 -nskip 4 -polyfit 2 \
  -analysis my-workmem.sm05.rh -force -per-run
mkanalysis-sess \
  -fsd bold -stc up  -mni305 2 -fwhm 5  \
  -event-related  -paradigm workmem.par -nconditions 5 \
  -spmhrf 0 -TR 2 -refeventdur 16 -nskip 4 -polyfit 2 \
  -analysis my-workmem.sm05.mni305 -force -per-run
# selxavg3-sess -s sess01 -analysis workmem.sm05.lh
ls $TUTORIAL_DATA/fsfast-functional/sess01/bold

# Cannot pass quit option
# tksurfer-sess, tkmedit-sess have  no -quit option
# 
# tksurfer-sess -s sess01 \
#   -analysis workmem.sm05.lh \
#   -c encode-v-base \
#   -c emot.dist-v-base \
#   -c probe.avg-v-base \
#   -c emot.dist-v-neut.dist
# tksurfer-sess -s sess01 -analysis workmem.sm05.rh \
#   -c encode-v-base \
#   -c emot.dist-v-base \
#   -c emot.dist-v-neut.dist \
#   -c probe.avg-v-base
# tkmedit-sess -s sess01 -analysis workmem.sm05.mni305 \
#   -c encode-v-base \
#   -c emot.dist-v-base \
#   -c emot.dist-v-neut.dist \
#   -c probe.avg-v-base

cd sess01/bold
ls
cd workmem.sm05.lh
ls
cd encode-v-base
ls
export SUBJECTS_DIR=$TUTORIAL_DATA/fsfast-tutorial.subjects
cd $TUTORIAL_DATA/fsfast-functional
# miissing nmask.dat file for the .lh subdir, so do not run this commamnd
# isxconcat-sess -sf sessidlist -analysis workmem.sm05.lh -contrast encode-v-base -o group
isxconcat-sess -sf sessidlist -analysis workmem.sm05.rh -contrast encode-v-base -o group
isxconcat-sess -sf sessidlist -analysis workmem.sm05.mni305 -contrast encode-v-base -o group
cd $TUTORIAL_DATA/fsfast-functional/group
ls
cd $TUTORIAL_DATA/fsfast-functional/group/workmem.sm05.lh
ls
cd $TUTORIAL_DATA/fsfast-functional/group/workmem.sm05.lh/encode-v-base
ls
mri_glmfit --y ces.nii.gz \
  --osgm \
  --surface fsaverage lh \
  --glmdir my-glm\
  --save-eres \
  --nii.gz

# Cannot pass quit option
# tksurferfv fsaverage lh inflated -aparc -overlay my-glm/osgm/sig.nii.gz -fminmax 2 3
# tksurferfv quit fsaverage lh inflated -aparc -overlay my-glm/osgm/sig.nii.gz -fminmax 2 3

# mri_glmfit-sim --glmdir my-glm --perm 1000 3 pos --cwp .05 --3spaces
cat my-glm/osgm/perm.th30.pos.sig.cluster.summary

# Cannot pass quit option
# tksurferfv fsaverage lh inflated \
# tksurferfv quit fsaverage lh inflated \
#  -overlay my-glm/osgm/perm.th30.pos.sig.cluster.nii.gz \
#  -annot ./my-glm/osgm/perm.th30.pos.sig.ocn.annot -fminmax 1.3 3

cd $TUTORIAL_DATA/fsfast-functional/group/workmem.sm05.rh/encode-v-base
mri_glmfit --y ces.nii.gz  --osgm \
  --surface fsaverage rh --glmdir my-glm --save-eres --nii.gz
# mri_glmfit-sim --glmdir my-glm --perm 1000 3 pos --cwp 0.05 --3spaces
cat my-glm/osgm/perm.th30.pos.sig.cluster.summary
cd $TUTORIAL_DATA/fsfast-functional/group/workmem.sm05.mni305/encode-v-base
ls
cd $TUTORIAL_DATA/fsfast-functional/group/workmem.sm05.mni305/encode-v-base
mri_glmfit --y ces.nii.gz --osgm  \
  --glmdir my-glm --nii.gz --save-eres

# Cannot pass quit option
# tkmeditfv fsaverage orig.mgz -aparc+aseg -overlay my-glm/osgm/sig.nii.gz -fminmax 2 3
# tkmeditfv quit fsaverage orig.mgz -aparc+aseg -overlay my-glm/osgm/sig.nii.gz -fminmax 2 3

# mri_glmfit-sim --glmdir my-glm --perm 1000 3 pos --cwp 0.05 --3spaces
cat my-glm/osgm/perm.th30.pos.sig.cluster.summary

# Cannot pass quit option
# tkmeditfv fsaverage orig.mgz \
# tkmeditfv quit fsaverage orig.mgz \
#  -ov my-glm/osgm/perm.th30.pos.sig.cluster.nii.gz \
#  -seg ./my-glm/osgm/perm.th30.pos.sig.ocn.nii.gz  \
#       ./my-glm/osgm/perm.th30.pos.sig.ocn.lut \
#  -fminmax 1.3 5

cd $TUTORIAL_DATA/fsfast-functional/group
cat workmem.sm05.lh/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.summary
cat workmem.sm05.rh/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.summary
cat workmem.sm05.mni305/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.summary
vlrmerge --o encode.merged.nii.gz \
  --lh workmem.sm05.lh/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.nii.gz \
  --rh workmem.sm05.rh/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.nii.gz \
  --vol workmem.sm05.mni305/encode-v-base/my-glm/osgm/perm.th30.pos.sig.cluster.nii.gz \
  --scm workmem.sm05.mni305/subcort.mask.nii.gz

# cannot pass quit option
# tkmeditfv quit fsaverage orig.mgz -aparc+aseg -ov encode.merged.nii.gz -fminmax 1.3 5 -surfs


date 

