#!/bin/sh
tensors=UWRMAC-DTI271_SL

#Converting to Camino's format. my_nii2dt is a copy of nii2dt to allow increased heap-size.
my_nii2dt -inputfile ${tensors}.nii.gz > ${tensors}.Bdouble

#Getting the brain mask for seedfile.
cat ${tensors}.Bdouble | md | voxel2image -outputroot ${tensors}_md -header ${tensors}.nii.gz
fslmaths ${tensors}_md.nii.gz -thr 0.01 -uthr 100 -bin ${tensors}_brain_mask.nii.gz

#Camino's track command. my_track is a copy of track to allow increased heap-size.
my_track -inputmodel dt -inputfile ${tensors}.Bdouble -inputdatatype double -seedfile ${tensors}_brain_mask.nii.gz -anisthresh 0.0 -curvethresh 45 -curveinterval 5 -stepsize 0.1 -outputdatatype float -tracker euler -interpolator tend_prob_nn -iterations 2 -outputfile ${tensors}_fb_tracts.Bfloat
#In the above command I would recommend trying more iterations than 2 after basic sanity checks. Perhaps 20. But that will take up a very long time.

#Have to install camino-trackvis (http://sourceforge.net/projects/camino-trackvis/) Could be 0.2.8!
camino_to_trackvis -i ${tensors}_fb_tracts.Bfloat -o ${tensors}_fb_tracts.trk --nifti ${tensors}.nii.gz --phys-coords

#Connectivity matrix generation using Camino's conmat.
atlasPrefix=Paxinos_to_112RM-SL_20121014_nifti_fsl
target_file=${atlasPrefix}.nii.gz #Please email Alex Converse (akconverse@wisc.edu), Jeff Moriano (moirano@wisc.edu) or Nagesh Adluru (nagesh.adlur@gmail.com) for the file and the SplitROIs subfolder.

cat ${tensors}.Bdouble | fa | voxel2image -outputroot ${tensors}_fa -header ${tensors}.nii.gz
#Camino's conmat using ${tensors}_fa.nii.gz as a scalar file.
#More on using conmat here (http://cmic.cs.ucl.ac.uk/camino//index.php?n=Tutorials.ConnectivityMatrices).
conmat -inputfile ${tensors}_fb_tracts.Bfloat -targetfile ${target_file} -scalarfile ${tensors}_fa.nii.gz -tractstat mean -outputroot ${tensors}_fa_mean_

#Visualizing the connecting tracts between two example pair of ROIs.
roi1=46
roi2=39
fslmaths SplitROIs/${atlasPrefix}_index_${roi1}.nii -add SplitROIs/${atlasPrefix}_index_${roi2}.nii ${roi1}_${roi2}.nii.gz
pairFile=${roi1}_${roi2}.nii.gz
cat ${tensors}_fb_tracts.Bfloat|procstreamlines -outputfile ${tensors}_${roi1}_${roi2}.Bfloat -seedfile ${tensors}_brain_mask.nii.gz -endpointfile ${pairFile}
camino_to_trackvis -i ${tensors}_${roi1}_${roi2}.Bfloat -o ${tensors}_${roi1}_${roi2}.trk --nifti ${tensors}_brain_mask.nii.gz --phys-coords

#Cleaning up for space.
#rm -f ${tensors}.Bdouble
#rm -f ${tensors}_fb_tracts.Bfloat
