TITLE: DTI preprocessing, statistical and cluster analysis
AUTHORS: Maurizio Bergamino, Jennapher Lingo Vangilder

[DESCRIPTION]
This protocol outlines preprocessing, statistical and cluster analyses that were applied in 'Using whole-brain diffusion tensor analysis to  evaluate white matter structural correlates of delayed visuospatial memory and one-week motor skill retention in nondemented older adults: A  preliminary study' (https://doi.org/10.1371/journal.pone.0274955). 


The following software packages are required to follow this protocol: FSL, MRtrix, Advanced Normalization Tools (Github: https://github.com/ANTsX/ANTs). Figures were created via AFNI. 

[GUIDELINES]
Double-check your images after each step!

[STEPS]
SECTION: Preprocessing data
1. Double-check if your native and original images are in FLOAT32 and oriented with the MNI.
-Convert to float-
fslmaths dwidata.nii.gz dwi.nii.gz -odt float
SECTION: Preprocessing data
2. DWI Denoising:
dwidenoise -datatype float32 -noise level_noise.nii.gz [IN] [OUT_denoised]
dwidenoise -datatype float32 -noise level_noise.nii.gz dwi.nii.gz dwi_denoised.nii.gz
SECTION: Preprocessing data
3. mrdegibbs [OUT_denoised] [OUT_Gibbs]
mrdegibbs dwi_denoised.nii.gz dwi_Gibbs.nii.gz
SECTION: Preprocessing data
4. need brain – bet doesn’t work as well as dwi2mask
dwi2mask -fslgrad bvecs.txt bvals.txt dwi_Gibbs.nii.gz temp_brain_mask.nii.gz
SECTION: Preprocessing data
5. Run ‘nodif_brain_mask.sh’ script [e.g.,bash nodif_brain_mask.sh dwi_Gibbs.nii.gz 4214_52 temp_brain_mask.nii.gz] to get temporary nodif brain masks for eddy.
Rename both of these nodif files with the prefix ‘dwi_Gibbs_’ e.g., ‘dwi_Gibbs_brain_mask’ for eddy. Delete the ‘temp_brain’ file.
SECTION: Preprocessing data
6. Run eddy correction and get rotated vector file
eddy --imain=dwi_Gibbs.nii.gz --mask=dwi_Gibbs_brain_mask.nii.gz --index=index.txt --acqp=acqparams.txt --bvecs=bvecs.txt --bvals=bvals.txt --fwhm=0 --flm=quadratic --slm=linear --out=eddy_unwarped_images --data_is_shelled
SECTION: Preprocessing data
7. Nodif and Brain extraction on the data.nii.gz
fslroi data.nii.gznodif_data0 1
dwi2mask -fslgrad eddy_unwarped_images.eddy_rotated_bvecs.txt bvals.txt data.nii.gz temp_brain_mask.nii.gz
SECTION: Preprocessing data
8. Run ‘nodif_brain_mask.sh’ script [e.g.,bash nodif_brain_mask.sh data.nii.gz 4214_52 temp_brain_mask.nii.gz] output is ‘nodif_brain_mask.nii.gz’ and ‘nodif_brain.nii.gz’ rename and add ‘data’ in front, so ‘data_nodif_brain’. Delete ‘temp_brain’ file.
SECTION: Preprocessing data
9. Bias Field correction (via ANTs):
dwibiascorrect ants data.nii.gz data_bias.nii.gz -fslgrad eddy_unwarped_images.eddy_rotated_bvecs.txt bvals.txt -mask data_nodif_brain_mask.nii.gz -bias bias_image.nii.gz -ants.b [100,3]
SECTION: Preprocessing data
10. Upsample DWI images for better coregistration with MNI template
mrgrid -vox 1.25 data_bias.nii.gz regrid data_bias_HR.nii.gz

SECTION: Preprocessing data
11. Fit tensor model
dtifit -k data_bias_HR.nii.gz -o DTI_map_w-linear_ -m nodif_brain_mask.nii.gz -r eddy_unwarped_images.eddy_rotated_bvecs.txt -b bvals.txt -w
SECTION: Preprocessing data
12. Optional: create bash script to generate group template
Copy all the nodif_brain.nii.gz images into one folder and change directory to it. Then run:
bash buildtemplateparallel.sh -d 3 -o template -c 0 -r 1 -n 0 -i 3 *.nii.gz

WarpImageMultiTransform 3 [in: FA map in native space] [out: FA map in template space] -R templatetemplate.nii.gz [warp file] [affine file] 
SECTION: Preprocessing data
13. At this point all participant FA maps should be in template space. Create a 4D (x,y,z + time) file with all your maps:
Insert all your FA in template space in one folder and run:
fslmerge -t [output] *.nii.gz
SECTION: Preprocessing data
14. Create then apply mask from all subjects
fslmaths all_FA.nii.gz -max 0 -Tmin -bin mean_mask -odt char
fslmaths all_FA.nii.gz -mas mean_mask all_FA.nii.gz


SECTION: Preprocessing data
15. Create a mean of the FA over-time and create white matter mask (thresholded at FA>0.20):
fslmaths all_FA -Tmean mean_FA
fslmaths mean_FA -thr 0.20 -bin WM_mask
SECTION: Preprocessing data
16. Apply  your mask to your smoothed maps:
fslmaths all_FA_smooth -mas mean_mask all_FA_smooth
SECTION: Preprocessing data
17. At this point you should have a FA map 'all_FA.nii.gz'  for all subjects.  
SECTION: Linear regression
18. Create linear regression script (that applies model voxel-by-voxel) using the LM.m function in MATLAB by using:
LM (file_ID1, raw_image_subject, mask)

e.g., LM (covariates.txt, all_FA_smooth.nii, WM_mask.nii)
SECTION: Linear regression
19. Resulting files will be the FDR-corrected p-value and tstat maps for each variable.
SECTION: Clusterize and apply white matter atlas to identify clusters
20. Example is for first behavioral variable 'score_1'
3dcalc -ascore_1_FDR.nii-expr 'a*1' -prefixscore_1_FDR_corrected.nii.gz
3dcalc -a score_1_tStat.nii -expr 'a*1' -prefix score_1_tStat_corrected.nii.gz
SECTION: Clusterize and apply white matter atlas to identify clusters
21. Split scores tStat into positive and negative (this provides you with positive and negative correlations):

Fslmathsscore_1_tStat_corrected.nii.gz -thr 0 positive_score1.nii.gz
Fslmathsscore_1_tStat_corrected.nii.gz -uthr 0 negative_score1.nii.gz
*For negative images, need to multiply by -1
Fslmaths negative_score1.nii.gz -mul -1 negative_score1.nii.gz
SECTION: Clusterize and apply white matter atlas to identify clusters
22. Get FDR-corrected p-value clusters that are less than 0.01 and that are at least 100 voxels in size  
3dClusterize -nosum -1Dformat -inset score_1_FDR_corrected.nii.gz -idat 0 -ithr 0 -NN 2 -clust_nvox 100 -1sided RIGHT_TAIL 0.99 -pref_map Clust_mask_motor.nii.gz

fslmaths Clust_mask_motor.nii.gz -bin Clust_mask_motor.nii.gz
SECTION: Clusterize and apply white matter atlas to identify clusters
23. Coregister the template to MNI:

antsRegistrationSyN.sh -d 3 -m /Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/group_template/templatetemplate.nii.gz -f /usr/local/fsl/data/standard/MNI152_T1_1mm_brain.nii.gz -o /Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI -t s
SECTION: Clusterize and apply white matter atlas to identify clusters
24. Check that ROIs are Binarized (use nearest neighbor command when the images are binarized). Behavioral variables are 'score_1' and 'motor'

Transform clusters to MNI:

antsApplyTransforms -d 3 -i Clust_mask_motor.nii.gz -o Clust_mask_motor_MNI.nii.gz -r/usr/local/fsl/data/standard/MNI152_T1_1mm_brain.nii.gz -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI1Warp.nii.gz,0]  -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI0GenericAffine.mat,0] -n NearestNeighbor

SECTION: Clusterize and apply white matter atlas to identify clusters
25. Transform scores tstat (positive and negative) to MNI space:

antsApplyTransforms -d 3 -ipositive_score1.nii.gz-opositive_score1_MNI.nii.gz -r/usr/local/fsl/data/standard/MNI152_T1_1mm_brain.nii.gz -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI1Warp.nii.gz,0]  -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI0GenericAffine.mat,0]

antsApplyTransforms -d 3 -inegative_score1.nii.gz-onegative_score1_MNI.nii.gz -r/usr/local/fsl/data/standard/MNI152_T1_1mm_brain.nii.gz -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI1Warp.nii.gz,0]  -t [/Users/syschaef/Desktop/4214_DTI/MRtrix_preproc/template_to_MNI/templatetemplate_to_MNI0GenericAffine.mat,0]
SECTION: Clusterize and apply white matter atlas to identify clusters
26. Mask the tstat scores with significant cluster files:

Positive:
fslmathspositive_score1_MNI.nii.gz -mas Clust_mask_motor_MNI.nii.gzpositive_score1masked_MNI.nii.gz
fslmathspositive_score1masked_MNI.nii.gz -bin Clust_mask_motor_positive_MNI.nii.gz

Negative:
fslmathsnegative_score1_MNI.nii.gz -mas Clust_mask_motor_MNI.nii.gznegative_score1masked_MNI.nii.gz
fslmathsnegative_score1masked_MNI.nii.gz -bin Clust_mask_motor_negative_MNI.nii.gz
SECTION: Clusterize and apply white matter atlas to identify clusters
27. Custom code extracted significant cluster locations from the JHU white matter atlas, but use the atlas of your choice and identify clusters by using 'fslmaths' (see fslwiki for tutorial)