forked from VANDAlab/Ex_vivo_Pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DWIpreproc.sh.old
142 lines (104 loc) · 7.9 KB
/
DWIpreproc.sh.old
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
138
139
140
141
142
#DWI preprocessing and DTI computation script
#Zaki Alasmar (zklsmr) - Dec 07 2023
#TODO: plenty of things, not totally happy with it
#!/bin/bash
src_raw="SOMEPATH/niix_Files/"
out_raw="SOMEOTHERPATH/all_subs/raw_diff/"
src_mask="ANOTHERPATH/BISON_Native/"
out_dti="SOMEOTHERPATH/all_subs/proc/"
previous_DTI="MOREPATHS/diff_data/"
my_src_raw="JUSTABACKUPPATH/diff_data_raw_backup/"
for sub_mask in "$src_mask"/*; do
# Get subject ID & session date
sub_id=$(basename "$sub_mask" | cut -d '_' -f 1)
sub_sess_date=$(basename "$sub_mask" | cut -d '_' -f 2)
echo "$sub_id -----> "
# Identify a path for preprocessed date, and check if it has FA maps
checking=($(ls "$previous_DTI$sub_id"/tensor*/FA.nii* 2>/dev/null))
if [ ${#checking[@]} -gt 0 ]; then
echo "Subject has already been preprocessed"
echo "---------------------------------------------------------------------------------------------"
else
echo "New subject"
# Check the subject raw directory in dadmah exists
sub_dir=($(ls -d "$my_src_raw"*"DH${sub_id#DH}"* 2>/dev/null))
if [ ${#sub_dir[@]} -ge 1 ]; then
echo "Subject Dir = $(
test -d "${sub_dir[0]}"
echo $?
) -----> "
# If the directory exists, check that a diffusion image exists
diff_dwi_image=($(ls "${sub_dir[0]}"/*DWI_sag*nii 2>/dev/null))
# If not, stop
if [ ${#diff_dwi_image[@]} -eq 0 ]; then
echo -e "\n-=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-"
echo "${diff_dwi_image[@]} No DWI volume"
echo "-=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-"
else
echo "DWI image = $(
test -f "${diff_dwi_image[0]}"
echo $?
) -----> "
# Check if the subject has B0 images
b0_images=($(ls "${sub_dir[0]}"/*B0*nii 2>/dev/null))
# There should be two B0 images in opposite encoding directions
if [ ${#b0_images[@]} -eq 2 ]; then
# Get the paths for the AP or RL
b0_images_encDWI=($(ls "${sub_dir[0]}"/*DWI-B0_*nii 2>/dev/null))
b0_images_encDWI_INV=($(ls "${sub_dir[0]}"/*DWI-B03_*nii 2>/dev/null))
echo "B0 images = True -----> "
# Concatenate the two B0 images in the correct way as expected by MRtrix, then output to the raw directory of images
bzero_out_dir="$my_src_raw$sub_id"
mrcat -axis 3 ${b0_images_encDWI[0]} ${b0_images_encDWI_INV[0]} ${bzero_out_dir}/Bzero_${b0_images_encDWI[0]##*/sag_??}${b0_images_encDWI_INV[0]##*/sag_??}.mif
# Convert the mask to nii and output it to the subject's raw directory along with their DWI images
mnc2nii "${sub_mask}" "${my_src_raw}${sub_id}/${sub_mask%.*}.nii"
# From the nii mask that we just converted, create separate binary masks for the GM, WM, and subcortex, then combine them into one binarized whole brain mask
fslmaths "${my_src_raw}${sub_id}/${sub_mask%.*}.nii" -thr 0.9 -uthr 1.2 -bin "${my_src_raw}${sub_id}/${sub_mask%.*}_GM_mask.nii.gz"
fslmaths "${my_src_raw}${sub_id}/${sub_mask%.*}.nii" -thr 8.9 -uthr 9.1 -bin "${my_src_raw}${sub_id}/${sub_mask%.*}_WM_mask.nii.gz"
fslmaths "${my_src_raw}${sub_id}/${sub_mask%.*}.nii" -thr 1.9 -uthr 5.1 -bin "${my_src_raw}${sub_id}/${sub_mask%.*}_subcortex_mask.nii.gz"
fslmaths "${my_src_raw}${sub_id}/${sub_mask%.*}_GM_mask.nii.gz" -add "${my_src_raw}${sub_id}/${sub_mask%.*}_WM_mask.nii.gz" -add "${my_src_raw}${sub_id}/${sub_mask%.*}_subcortex_mask.nii.gz" "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask.nii.gz"
# =========================================================#
# Preprocessing starts here #
# =========================================================#
# Concatenate the DWI images, the b-vectors, and the b-values into one image, then denoise it
bvals_path=($(ls "${sub_dir[0]}"/*DWI_sag*bval))
bvecs_path=($(ls "${sub_dir[0]}"/*DWI_sag*bvec))
if [ "${bvals_path%.*}" == "${diff_dwi_image[0]%.*}" ]; then
# New basename to save images as
new_basename="${diff_dwi_image[0]##*/}"
# Resample the combined brain mask to use in the preprocessing
itk_resample "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask.nii.gz" --like "${b0_images_encDWI[0]}" --labels --clobber "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask_resampled.nii.gz"
mrconvert "${diff_dwi_image[0]}" "${previous_DTI}${sub_id}/denoised_data/${new_basename}_concat.mif" -fslgrad "${bvecs_path[0]}" "${bvals_path[0]}"
dwidenoise "${previous_DTI}${sub_id}/denoised_data/${new_basename}_concat.mif" "${previous_DTI}${sub_id}/denoised_data/${new_basename}_concat_denoised.mif" \
-noise "${previous_DTI}${sub_id}/denoised_data/${new_basename}_noise.mif" \
-mask "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask_resampled.nii.gz"
mrdegibbs "${previous_DTI}${sub_id}/denoised_data/${new_basename}_concat_denoised.mif" "${previous_DTI}${sub_id}/unrang_data/${new_basename}_concat_denoised_unrang.mif" \
-axes "1,2"
dwifslpreproc "${previous_DTI}${sub_id}/unrang_data/${new_basename}_concat_denoised_unrang.mif" "${previous_DTI}${sub_id}/fslpreproc_data/${new_basename}_concat_denoised_unrang_preproc.mif" \
-pe_dir "${b0_images_encDWI[0]##*/sag_??}" -nthreads 20
-eddy_mask "${my_src_raw}${sub_id}/${sub_mask%.}wholebrain_mask_resampled.nii.gz"
-rpe_pair -se_epi "${bzero_out_dir}/Bzero${b0_images_encDWI[0]##/sag_??}${b0_images_encDWI_INV[0]##*/sag_??}.mif"
-eddy_options "--slm=linear"
dwibiascorrect fsl "${previous_DTI}${sub_id}/fslpreproc_data/${new_basename}_concat_denoised_unrang_preproc.mif" "${previous_DTI}${sub_id}/unbias_data/${new_basename}_concat_denoised_unrang_preproc_unbiased.mif" \
-bias "${previous_DTI}${sub_id}/unbias_data/${new_basename}_concat_denoised_unrang_preproc_bias.mif" \
-mask "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask_resampled.nii.gz"
dwi2tensor -mask "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask_resampled.nii.gz" \
"${previous_DTI}${sub_id}/unbias_data/${new_basename}_concat_denoised_unrang_preproc_unbiased.mif" \
"${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI.nii"
tensor2metric -mask "${my_src_raw}${sub_id}/${sub_mask%.*}_wholebrain_mask_resampled.nii.gz" \
-fa "${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI_FA.nii" \
-ad "${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI_AD.nii" \
-rd "${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI_RD.nii" \
-adc "${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI_MD.nii" \
"${previous_DTI}${sub_id}/tensor_data/${new_basename}_concat_denoised_unrang_preproc_unbiased_DTI.nii"
echo -e "\n-=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-"
echo "Finished DTI metric extraction"
echo -e "\n-=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-"
fi
else
echo "Subject ${sub_id} Does not have DWI data"
fi
fi
echo -e "\n============================================================================================"
fi
done