#!/bin/sh
#Avoiding DTI-TK.
#This could be UWRMAC-DTI271_SL
tensors=UWRMAC-DTI271_Paxinos

#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. (NEW)
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.1 -curvethresh 60 -outputdatatype float -tracker euler -interpolator tend_prob_nn -iterations 2 -outputfile ${tensors}_fb_tracts.Bfloat

#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.
target_file=grey_matter_ROI_file.nii.gz #We have an internal version of the file that we are in the process of publication but if interested please contact us directly and we can figure out a way to share.

cat ${tensors}.Bdouble | fa | voxel2image -outputroot ${tensors}_fa -header ${tensors}.nii.gz
#Camino's conmat using ${tensors}_fa.nii.gz
#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_

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