#!/bin/tcsh -f
#
# @preprocess_fsl: pre-processes Philips 32-direction diffusion 
# datasets using the FSL FDT diffusion pipeline. After this script
# is run, probabilistic tracking can be performed via GUI or command 
# line: 
#
# 1) eddy current distortion correction and motion correction to B0 volume
# 2) bedpostx preprocessing steps to prepare for probabilistic tractography
# 3) computation of tensor, FA, and color-coded eigenvalue images
# 4) registration of hi-res structural and diff images to MNI standard space volume
#
# Needs as arguments: 1) a subject id, 2) an afni directory with a *_DMaps+orig.* and *anatr1+orig.* datasets, 3) a directory with gradient information 
#
# This program assumes you have FSL installed in default location: /usr/local/fsl/bin
#
# Example usage:
# /Volumes/data6/matlab/scripts/bin/@preprocess_fsl TA409 /Volumes/data6/TA409/afni /Volumes/data6/matlab/dti/gradients/After122107
# /Volumes/data6/matlab/scripts/bin/@preprocess_fsl TA202 /Volumes/data6/TA202/afni /Volumes/data6/matlab/dti/gradients/Before122107
#
# Timothy.Ellmore@uth.tmc.edu, Dec 14 2009
#

set ec = $1
set afni_dir = $2
set grad_dir = $3

if ($#argv == 3) then
 echo "@preprocess_fsl is processing $ec ..."
 
 # go to subject afni directory
 cd $afni_dir

 # make fsl directory to keep processing results
 mkdir fsl

 # go into fsl directory and copy gradient information in there
 cd fsl
 cp {$grad_dir}/bvecs .
 cp {$grad_dir}/bvals .
 pwd
 ls ../{$ec}_DMaps+orig.*

 # copy un-registered DW images from the afni directory into the fsl directory
 cp ../{$ec}_DMaps+orig.* .
 # make nifti version of DW images
 3dcopy {$ec}_DMaps+orig.BRIK {$ec}_DMaps_temp.nii.gz
 fslswapdim {$ec}_DMaps_temp.nii.gz LR PA IS {$ec}_DMaps.nii.gz

 # remove copy of the DW afni brik (not needed now in fsl directory)
 rm {$ec}_DMaps+orig.* {$ec}_DMaps_temp.nii.gz

 # eddy current correction and registration to first (B0) DW images
 /usr/local/fsl/bin/eddy_correct {$ec}_DMaps.nii.gz data 0

 # extract nodif (B0) image volume
 3dbucket data.nii.gz'[0]' -prefix nodif.nii.gz

 # make diffusion image brain mask using BET
 /usr/local/fsl/bin/bet nodif nodif_brain  -f 0.5 -g 0 -m

 # check to see if everything is ready for bedpostx
 /usr/local/fsl/bin/bedpostx_datacheck .

 # run bedpostx to prepare for probabilistic tractography (this takes a long time!)
 cd $afni_dir
 /usr/local/fsl/bin/bedpostx fsl -n 2 -w 1  -b 1000

 # compute tensor information for visual inspection
 /usr/local/fsl/bin/dtifit --data=./fsl/data --out=./fsl/dti --mask=./fsl/nodif_brain_mask --bvecs=./fsl/bvecs --bvals=./fsl/bvals 0 128 0 128 0 70

 # prepare for registration by copying afni files into new directory called fsl.bedpostx
 cp *anatr1+orig* fsl.bedpostX
 cd fsl.bedpostX
 
 # skull strip the hi-res structural volume using BET (may need to adjust -f 0.55 for each subject)
 #/usr/local/fsl/bin/bet struct struct_brain  -f 0.50 -g 0 -m

 # skull strip using afni approach
 3dcopy {$ec}anatr1+orig.BRIK struct_temp.nii.gz
 fslswapdim struct_temp.nii.gz LR PA IS struct.nii.gz
 3dSkullStrip -debug 1 -orig_vol -input  struct.nii.gz -prefix struct_brain.nii.gz
 #rm {$ec}anatr1+orig.*
 rm struct_temp.nii.gz

 # run registrations to figure out all possible transforms
 echo "Computing all possible transforms for $ec ..."
 # diff to struct xform
 /usr/local/fsl/bin/flirt -in nodif_brain -ref struct_brain -omat ./xfms/diff2str.mat -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12 -cost mutualinfo
 /usr/local/fsl/bin/convert_xfm -omat ./xfms/str2diff.mat -inverse ./xfms/diff2str.mat

 # struct to standard xform
 /usr/local/fsl/bin/flirt -in struct_brain -ref /usr/local/fsl/data/standard/MNI152_T1_2mm_brain -omat ./xfms/str2standard.mat -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12 -cost corratio
 /usr/local/fsl/bin/convert_xfm -omat ./xfms/standard2str.mat -inverse ./xfms/str2standard.mat

 # diff to standard and standard to diff xforms
 /usr/local/fsl/bin/convert_xfm -omat ./xfms/diff2standard.mat -concat ./xfms/str2standard.mat ./xfms/diff2str.mat
 /usr/local/fsl/bin/convert_xfm -omat ./xfms/standard2diff.mat -inverse ./xfms/diff2standard.mat
 echo "Done computing transforms for for $ec"
else

 echo "@preprocess_fsl: need subject id, afni dir, and grad dir as command args"

endif




