#!/usr/bin/env bash

#All steps in this script are written to run the commands on a cohort of image data. We use the 'foreach' script for simplified batch processing.
#We assume that the dataset is organised with one directory identifying each subject (e.g. S01...S99), and structured into subfolders and/or files identifying the data (e.g. 5_dwi).
#Path example:'study_name/subjects/S01/5_dwi/dwi_den_unr_pre_unbia_up.mif'
#Further references can be found here: https://mrtrix.readthedocs.io/en/latest/index.html) and here: https://doi.org/10.1016/j.neuroimage.2019.116137.

#Tensor based metrics
#Calculate tensors
foreach * : dwi2tensor IN/5_dwi/dwi_den_unr_pre_unbia_up.mif -mask IN/5_dwi/mask_up.mif IN/5_dwi/tensor_up.mif;

#Calculate fa
foreach * : tensor2metric IN/5_dwi/tensor_up.mif -mask IN/5_dwi/mask_up.mif -fa IN/5_dwi/fa_up.mif;

#Calculate adc
foreach * : tensor2metric IN/5_dwi/tensor_up.mif -mask IN/5_dwi/mask_up.mif -adc IN/5_dwi/adc_up.mif;

#Sample resampled tractograms with fa and write as '.csv'-file 
foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/fa_up.mif IN/5_dwi/fa_tms_hand_l_resampled_100.csv;
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/fa_up.mif IN/5_dwi/fa_tms_hand_r_resampled_100.csv;

#sample resampled tractograms with adc and write as '.csv'-file
foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/adc_up.mif IN/5_dwi/adc_tms_hand_l_resampled_100.csv;
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/adc_up.mif IN/5_dwi/adc_tms_hand_r_resampled_100.csv;


#FOD based metrics
#Perform segmentation of continuous Fibre Orientation Distributions (FODs) to produce discrete fixels with use of option flag '-afd image'
foreach * : fod2fixel -afd fd.mif IN/5_dwi/wm.mif IN/5_dwi/fd_output;

#Compute fixel map from tractogram,left hemisphere
foreach * : tck2fixel IN/5_dwi/tms_hand_l_norm.tck IN/5_dwi/fd_output IN/5_dwi/fd_output/tck2fixel_hand_l_mean_fd.mif;

#Generate a fixel mask, remove all fixels where tck2fixel.mif==0
foreach * : fixelcrop IN/5_dwi/fd_output IN/5_dwi/fd_output/tck2fixel_hand_l_mean_fd.mif IN/5_dwi/mean_fd_l;

#Convert a fixel-based sparse-data image into a scalar image
foreach * : fixel2voxel IN/5_dwi/mean_fd_l mean IN/5_dwi/mean_fd_l/fixel2voxel_hand_l_mean_fd.mif;

#Sample values obtained from scalar image along resampled tractogram
foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/mean_fd_l/fixel2voxel_hand_l_mean_fd_weighted.mif IN/5_dwi/mean_fd_l/hand_l_norm_resampled_100_mean_fd_weighted.csv;


#Repeat above commands tck2fixel,fixelcrop,fixel2voxel and tcksample for right hemisphere
foreach * : tck2fixel IN/5_dwi/tms_hand_r_norm.tck IN/5_dwi/mean_fd_output IN/5_dwi/mean_fd_output tck2fixel_hand_r_mean_fd.mif;

foreach * : fixelcrop IN/5_dwi/mean_fd_output IN/5_dwi/mean_fd_output/tck2fixel_hand_r_mean_fd.mif IN/5_dwi/mean_fd_r;

foreach * : fixel2voxel IN/5_dwi/mean_fd_r/fd.mif mean IN/5_dwi/mean_fd_r/fixel2voxel_hand_r_mean_fd.mif;

foreach * : fixel2voxel IN/5_dwi/mean_fd_r/fd.mif mean IN/5_dwi/mean_fd_r/fixel2voxel_hand_r_mean_fd_weighted.mif -weighted IN/5_dwi/mean_fd_r/fd.mif;

foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/mean_fd_r/fixel2voxel_hand_r_mean_fd_weighted.mif IN/5_dwi/mean_fd_r/hand_r_norm_resampled_100_mean_fd_weighted.csv
