forked from treanus/KUL_NIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KUL_anat_biascorrect.sh
executable file
·214 lines (169 loc) · 4.71 KB
/
KUL_anat_biascorrect.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# Bash shell script to register BIDS data to the T1w
#
# Requires ants
#
# @ Stefan Sunaert - UZ/KUL - [email protected]
# 20/02/2022
version="0.1"
kul_main_dir=$(dirname "$0")
script=$(basename "$0")
source $kul_main_dir/KUL_main_functions.sh
# $cwd & $log_dir is made in main_functions
# FUNCTIONS --------------
# function Usage
function Usage {
cat <<USAGE
`basename $0` corrects the signal bias in structural BIDS data
Usage:
`basename $0` <OPT_ARGS>
Example:
`basename $0` -p JohnDoe
Required arguments:
-p: participant name
Optional arguments:
-s: session
-v: show output from commands (0=silent, 1=normal, 2=verbose; default=1)
USAGE
exit 1
}
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
silent=1 # default if option -v is not given
ants_verbose=1
verbose_level=1
# Set required options
p_flag=0
d_flag=0
if [ "$#" -lt 1 ]; then
Usage >&2
exit 1
else
while getopts "p:v:" OPT; do
case $OPT in
p) #participant
participant=$OPTARG
p_flag=1
;;
t) #type
type=$OPTARG
;;
v) #verbose
verbose_level=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo
Usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo
Usage >&2
exit 1
;;
esac
done
fi
# check for required options
if [ $p_flag -eq 0 ] ; then
echo
echo "Option -p is required: give the BIDS name of the participant." >&2
echo
exit 2
fi
KUL_LOG_DIR="KUL_LOG/${script}/sub-${participant}"
mkdir -p $KUL_LOG_DIR
# MRTRIX and others verbose or not?
if [ $verbose_level -lt 2 ] ; then
ants_verbose=0
elif [ $verbose_level -eq 2 ] ; then
ants_verbose=1
fi
# Functions
function KUL_check_data {
echo -e "\n\nAn overview of the BIDS data:"
bidsdir="BIDS/sub-$participant"
T1w=($(find $bidsdir -name "*T1w.nii.gz" ! -name "*gadolinium*" -type f ))
nT1w=${#T1w[@]}
echo " number of non-contrast T1w: $nT1w"
cT1w=($(find $bidsdir -name "*T1w.nii.gz" -name "*gadolinium*" -type f ))
ncT1w=${#cT1w[@]}
echo " number of contrast enhanced T1w: $ncT1w"
FLAIR=($(find $bidsdir -name "*FLAIR.nii.gz" -type f ))
nFLAIR=${#FLAIR[@]}
echo " number of FLAIR: $nFLAIR"
FGATIR=($(find $bidsdir -name "*FGATIR.nii.gz" -type f ))
nFGATIR=${#FGATIR[@]}
echo " number of FGATIR: $nFGATIR"
T2w=($(find $bidsdir -name "*T2w.nii.gz" -type f ))
nT2w=${#T2w[@]}
echo " number of T2w: $nT2w"
SWI=($(find $bidsdir -name "*run-01_SWI.nii.gz" -type f ))
nSWI=${#SWI[@]}
SWIp=($(find $bidsdir -name "*run-02_SWI.nii.gz" -type f ))
nSWIp=${#SWIp[@]}
echo " number of SWI magnitude: $nSWI"
echo " number of SWI phase: $nSWIp"
echo -e "\n\n"
}
function KUL_biascorrect {
bias_output=$kulderivativesdir/${source_mri_label}_bc.nii.gz
if [ ! -f $bias_output ]; then
echo " doing biascorrection on the ${td}"
N4BiasFieldCorrection --verbose $ants_verbose \
-d 3 \
-i $source_mri \
-o $bias_output
else
echo " biascorrection of the ${td} already done"
fi
}
function KUL_biascorrect_anat_images {
if [ $ncT1w -gt 0 ];then
source_mri_label="cT1w"
source_mri=$cT1w
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
if [ $nT1w -gt 0 ];then
source_mri_label="T1w"
source_mri=$T1w
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
if [ $nT2w -gt 0 ];then
source_mri_label="T2w"
source_mri=$T2w
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
if [ $nFLAIR -gt 0 ];then
source_mri_label="FLAIR"
source_mri=$FLAIR
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
if [ $nFGATIR -gt 0 ];then
source_mri_label="FGATIR"
source_mri=$FGATIR
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
if [ $nSWI -gt 0 ];then
source_mri_label="SWI"
source_mri=$SWI
task_in="KUL_biascorrect"
KUL_task_exec $verbose_level "Bias correcting the $source_mri_label" "anat_biascorrect"
fi
}
# --- MAIN ---
# STEP 1 - SETUP
KUL_check_data
kulderivativesdir=$cwd/BIDS/derivatives/KUL_compute/sub-$participant/KUL_anat_biascorrect
mkdir -p $kulderivativesdir
# STEP 2 - biascorrect all anatomical images
KUL_biascorrect_anat_images
echo "Finished"