-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01_tractography.sh
137 lines (103 loc) · 6.01 KB
/
01_tractography.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
set -e
MAINDIR=$(pwd)
display_usage() {
echo ""
tput bold
echo "Description"
tput sgr0
echo ""
echo "This script uses MRTrix and FSL to analyze diffusion data using a certain atlas."
echo ""
tput bold
echo "Usage"
tput sgr0
echo ""
echo "./$(basename $0) [Subject and type of session]"
echo "It requires 2 argument: Subject DWI Directory (eg: sub-control019_ses-midcycle) and the name of the atlas (either AAL116 or schaefer100cersubcort)."
}
if [ $# -l 2 ] # if there are less than 2 arguments
then
display_usage
exit 1
fi
########################### STEP 1 ###################################
# Prepare data and directories #
######################################################################
DIR=$1 #example name: sub-control019_ses-midcycle
SUB=${DIR:0:14} # example name: sub-control019
ATLASNAME=$2
if [ $ATLASNAME == "AAL116" ]; then
ATLAS="AAL116.nii.gz"
else
ATLAS="schaefer100cersubcort.nii.gz"
fi
SUBDIR="${MAINDIR}/data/${DIR}" #example name: sub-control019_ses-midcycle
ANATDIR="${MAINDIR}/data/${SUB}" #example name: sub-control019
ANAT="${ANATDIR}/${SUB}_restored-MPRAGE_brain.nii.gz"
cd $SUBDIR
DWI="${SUBDIR}/*clean.nii.gz"
DWIMASK="${SUBDIR}/*clean_mask.nii.gz"
BVEC="${SUBDIR}/*rotated_bvecs.bvec"
BVAL="bvals_132dir.bval"
mkdir -p mrtrix_outputs
cd mrtrix_outputs
########################### STEP 2 ###################################
# Convert data to .mif format #
######################################################################
# Convert data do mif format
mrconvert $DWI dwi.mif -fslgrad $BVEC $BVAL -force
mrconvert $ANAT anat.mif -force
# Convert mask do mif format
mrconvert $DWIMASK mask.mif -force
########################## STEP 3 ###################################
# Basis function for each tissue type #
#####################################################################
#Create a basis function from the subject's DWI data. The "dhollander" function is best used for multi-shell acquisitions; it will estimate different basis functions for each tissue type. For single-shell acquisition, use the "tournier" function instead
dwi2response dhollander dwi.mif wm.txt gm.txt csf.txt -force
# Performs multishell-multitissue constrained spherical deconvolution, using the basis functions estimated above
dwi2fod msmt_csd dwi.mif -mask mask.mif wm.txt wmfod.mif gm.txt gmfod.mif csf.txt csffod.mif -force
# Now normalize the FODs to enable comparison between subjects
mtnormalise wmfod.mif wmfod_norm.mif gmfod.mif gmfod_norm.mif csffod.mif csffod_norm.mif -mask mask.mif -force
########################### STEP 4 ###################################
# Create a GM/WM boundary for seed analysis #
######################################################################
# Extract all five tissue categories (1=GM; 2=Subcortical GM; 3=WM; 4=CSF; 5=Pathological tissue)
5ttgen fsl anat.mif 5tt_nocoreg.mif -premasked -nocrop -force
#The following series of commands will take the average of the b0 images (which have the best contrast), convert them and the 5tt image to NIFTI format, and use it for coregistration.
dwiextract dwi.mif - -bzero | mrmath - mean mean_b0_processed.mif -axis 3 -force
mrconvert mean_b0_processed.mif mean_b0_processed.nii.gz -force
mrconvert 5tt_nocoreg.mif 5tt_nocoreg.nii.gz -force
# Uses FSL commands fslroi and flirt to create a transformation matrix for registration between the tissue map and the b0 images
fslroi 5tt_nocoreg.nii.gz 5tt_vol0.nii.gz 0 1 #Extract the first volume of the 5tt dataset (since flirt can only use 3D images, not 4D images)
flirt -in mean_b0_processed.nii.gz -ref 5tt_vol0.nii.gz -interp nearestneighbour -dof 6 -omat diff2struct_fsl.mat
transformconvert diff2struct_fsl.mat mean_b0_processed.nii.gz 5tt_nocoreg.nii.gz flirt_import diff2struct_mrtrix.txt -force
mrtransform 5tt_nocoreg.mif -linear diff2struct_mrtrix.txt -inverse 5tt_coreg.mif -force
#Create a seed region along the GM/WM boundary
5tt2gmwmi 5tt_coreg.mif gmwmSeed_coreg.mif -force
rm -f 5tt_nocoreg.nii.gz 5tt_nocoreg.mif mean_b0_processed.mif mean_b0_processed.nii.gz 5tt_vol0.nii.gz anat.mif gmfod.mif gmfod_norm.mif csffod.mif csffod_norm.mif mask.mif wmfod.mif dwi.mif wm.txt gm.txt csf.txt
########################## STEP 5 ###################################
# Coregister atlas to the data #
#####################################################################
# Coregister atlas to struct space and convert to mrtrix format
applywarp -i $ATLAS -r $ANAT --out=atlas_2struct --warp="${ANATDIR}/reg_nonlinear_invwarp_T1tostandard_2mm.nii.gz"
mrconvert atlas_2struct.nii.gz atlas_2struct.mif -force
# Coregister atlas to diff space
mrtransform atlas_2struct.mif -linear diff2struct_mrtrix.txt -inverse atlas_coreg.mif -force
# Make sure the values of the atlas are integer
mrcalc atlas_coreg.mif -round atlas_${ATLASNAME}.mif -force
########################### STEP 6 ###################################
# Run the streamline analysis #
######################################################################
# Create streamlines: maxlength=250mm, 10M seeds
tckgen -act 5tt_coreg.mif -seed_gmwmi gmwmseed_coreg.mif -maxlength 250 -select 10000000 wmfod_norm.mif tracks.tck -force
# Reduce the number of streamlines with tcksift
tcksift2 -act 5tt_coreg.mif tracks.tck wmfod_norm.mif sift.txt -force
########################### STEP 7 ###################################
# Creating the connectome #
######################################################################
#Creating the connectome
tck2connectome -symmetric -zero_diagonal -scale_invnodevol -tck_weights_in sift.txt tracks.tck atlas_${ATLASNAME}.mif "${MAINDIR}/matrix_data/${ATLASNAME}/${DIR}_matrix_mrtrix.csv" -force
# Remove unneeded data from storage because it is not needed anymore
rm -f 5tt_nocoreg.nii.gz 5tt_nocoreg.mif atlas_2struct.mif atlas_2struct.nii.gz mean_b0_processed.mif mean_b0_processed.nii.gz 5tt_vol0.nii.gz anat.mif atlas_coreg.mif
cd $MAINDIR