-
Notifications
You must be signed in to change notification settings - Fork 9
/
MBSHelperFunctions.sh
306 lines (260 loc) · 9.33 KB
/
MBSHelperFunctions.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
##########################
# Multi-Brain-Seperation #
# Author: Da MA #
# da_ma@sfu.ca #
# d.ma.11@ucl.ac.uk #
##########################
source MASHelperFunctions.sh > /dev/null 2>&1
function getOrientation(){
#gets orientation string using mri_info
if [ "$#" -lt "1" ]; then
return 1
else
IMG=$1
fi
ORIENT=`mri_info $IMG | grep Orientation`
ORIENT=${ORIENT##*\ }
echo $ORIENT
}
function convert_dcm_to_nifti_batch(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "Usage: $function_name [input_dir] [dcm_nii_dir]"
return 1
fi
local input_dir=$1
local dcm_nii_dir=$2
local line
for line in $(cat $targetlist_raw); do
local id=$(echo $line | cut -d',' -f1)
local input_dir=$(echo $line | cut -d',' -f2)
dcm2niix_afni -9 -f $id -t -o $dcm_nii_dir -v 1 -z i $input_dir
mv $input_dir/$id.nii.gz $dcm_nii_dir
done
}
function multi_brain_seperation(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "Usage: $function_name [target_dir] [target_id] [result_dir] (Optional) [brain_no] [threshold]"
return 1
fi
local target_dir=$1
local target_id=$2
local result_dir=$3
## Number of brain to separate
local brain_no
if [[ ! -z $4 ]]; then
brain_no=$4
else
# default number of brain = 3
brain_no=3
fi
## Threshold value to extract the brain region
if [[ ! -z $5 ]]; then
local thr=$5
else
# default threshold = 5000
local thr=5000
fi
echo "target_dir=$target_dir"
echo "target_id=$target_id"
echo "result_dir=$result_dir"
local ero=2
local dil=4
local ero2=2
local tmp_dir=$result_dir/tmp_${RANDOM}
mkdir -p $tmp_dir
# create multi-brain mask
seg_maths $target_dir/$target_id -thr $thr -bin -ero $ero -dil $dil -fill -ero $ero2 $tmp_dir/${target_id}_multimask_1.nii.gz
local i=1
while [[ i -le $brain_no ]]; do
echo "extract ${i}th brain"
# extract ${i}th mask out
seg_maths $tmp_dir/${target_id}_multimask_$i.nii.gz -lconcomp $tmp_dir/${target_id}_mask_$i.nii.gz
# using mask
seg_maths $tmp_dir/${target_id}_mask_$i.nii.gz -dil 1 -mul $target_dir/$target_id $result_dir/${target_id}_${i}.nii.gz
# substract ${i}th extracted mask
if [[ i -lt $brain_no ]]; then
# echo "substract ${i}th extracted mask (-sub)"
seg_maths $tmp_dir/${target_id}_multimask_$i.nii.gz -sub $tmp_dir/${target_id}_mask_$i.nii.gz $tmp_dir/${target_id}_multimask_$(( i + 1 )).nii.gz
fi
i=$(( $i + 1 ))
done
rm -rf $tmp_dir
}
function multi_brain_seperation_batch(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "Usage: $function_name [target_dir] [targetlist] [result_dir]"
return 1
fi
local target_dir=$1
local targetlist=$2
local result_dir=$3
# local thr=5000
# local ero=2
# local dil=6
# local ero2=4
# local tmp_dir=$result_dir/tmp_${RANDOM}
# mkdir -p $tmp_dir
# brain_no=3
for target_id in $(cat $targetlist); do
multi_brain_seperation $target_dir $target_id $result_dir # $brain_no
# seg_maths $target_dir/$target_id -thr $thr -bin -ero $ero -dil $dil -fill -ero $ero2 $tmp_dir/${target_id}_mask_multi.nii.gz
done
}
function fix_header_info(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "$function_name Usage: $function_name [Input_file with wrong header] [orientation] [Output_file] [(Optional) output_type (analyze/nii)]"
return 1
fi
local input_file=$1
local orientation=$2
local output_file=$3
local output_type
if [[ ! -z $4 ]]; then
output_type=$4
else
output_type=nii
fi
mri_convert --in_orientation $orientation --out_orientation $orientation -ot $output_type $input_file $3 # -odt float
}
function reorder_brain(){
# not working properly
local function_name=${FUNCNAME[0]}
if [[ $# -lt 2 ]];then
echo "Usage: $function_name [input_nii] [output_nii] [out_orientation (Optional,default=RAS)]"
return 1
fi
local input_nii=$1
local output_nii=$2
local orient_out=$3 #RAS
local orient_in=$(getOrientation $input_nii)
mri_convert --in_orientation $orient_in $input_nii $output_nii $orient_out
fix_header_info $output_nii $orient_out $output_nii
}
function reorient_brain(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 4 ]]; then
echo "Usage: $function_name [target_dir] [target_id] [location (L/R/S)] [result_dir] [(Optional) convert_RAS]"
echo " convert_RAS: 0) skip (not) convert to LAS MNI152 standatd orientation (no resampling) [Default]"
echo " Otherwise) convert to LAS MNI152 standatd orientation (involve resampling)"
return 1
fi
local target_dir=$1
local target_id=$2
local location=$3
local result_dir=$4
local convert_RAS=$5
local out_orientation="LR PA IS" # = RAS
if [[ "$location" = "S" ]]; then
# orientation=RSA
orientation=RSP
elif [[ "$location" = "R" ]]; then
# orientation=ILA
orientation=ILP
elif [[ "$location" = "L" ]]; then
# orientation=SRA
orientation=SRP
fi
# local
target_file=$(ls $target_dir/${target_id}* | cut -d' ' -f1)
# Fix nifti hearder info using FreeSurfer
fix_header_info $target_file $orientation $result_dir/$target_id.nii.gz
# If FSL is installed, reorient into LAS MNI152 space
# check if FSL is installed (by checking variable $FSLDIR)
if [[ -z $convert_RAS ]]; then convert_RAS=1; fi
echo "convert_RAS = $convert_RAS"
if [[ $convert_RAS -eq 1 ]] && [[ ! -z $FSLDIR ]]; then
echo "using fslswapdim to convert data to RAS"
fslswapdim $result_dir/$target_id.nii.gz $out_orientation $result_dir/$target_id.nii.gz
fi
}
function reorient_brain_batch_3brain(){
local function_name=${FUNCNAME[0]}
if [[ $# -lt 3 ]]; then
echo "Usage: $function_name [target_dir] [scan_list] [result_dir]"
return 1
fi
local target_dir=$1
local scan_list=$2
local result_dir=$3
echo "target_dir=$target_dir"
echo "scan_list=$scan_list"
echo "result_dir=$result_dir"
local scan_id
local target_id
local target_id_oriented
local convert_RAS=1
local location
local orientation
local qc_orientation="LAS"
for scan_id in $(cat $scan_list); do
for brain_id in 1 2 3; do
target_id=${scan_id}_$brain_id
# make three orientation guess
for location in L R S; do
if [[ "$location" = "S" ]]; then
# orientation=RSA
orientation=RSP
elif [[ "$location" = "R" ]]; then
# orientation=ILA
orientation=ILP
elif [[ "$location" = "L" ]]; then
# orientation=SRA
orientation=SRP
fi
echo ".......... reorienting: $target_id to $orientation (guessing location: $location) .........."
if [[ -e $target_dir/$target_id.nii.gz ]]; then
echo ".......... reorienting ......"
reorient_brain $target_dir $target_id $location $result_dir
# rename to add orientation guess behind
target_id_oriented=${target_id}_$location
mv $result_dir/$target_id.nii.gz $result_dir/$target_id_oriented.nii.gz
# generate quickcheck (not working as expected yet, due to the limitation of FSL's slicer command)
# echo ".......... generating quickcheck ......"
# mas_quickcheck $result_dir/$target_id_oriented.nii.gz '' $result_dir $target_id_oriented $qc_orientation
fi
done
done
done
}
function extract_label(){
local function_name=$[FUNCNAME[0]]
if [[ $# -lt 4 ]]; then
echo "Usage: $function_name [target_dir] [target_id] [label] [result_dir]"
return 1
fi
local target_dir=$1
local target_list=$2
local label=$3
local result_dir=$4
seg_maths $target_dir/$target_id.nii.gz -thr $(($label-0.5)) -uthr $(($label+0.5)) $result_dir/$target_id.nii.gz
}
# function masking_batch_nii(){
# local function_name=${FUNCNAME[0]}
# if [[ $# -lt 3 ]]; then
# echo "Usage: $function_name [target_dir] [target_list] [result_dir] [(optional) atlas_dir]"
# return 1
# fi
# local target_dir=$1
# local target_list=$2
# local result_dir=$3
# local atlas_dir=$4
# echo "target_dir=$target_dir"
# echo "target_list=$target_list"
# echo "result_dir=$result_dir"
# echo "atlas_dir=$atlas_dir"
# local target_id
# for target_id in $(cat $target_list); do
# # $$AtlasListFileName=template_list.cfg
# for atlas_id in $(cat $atlas_dir/$AtlasListFileName); do
# # cloud process need to run on rcg-queen
# # local target_result_dir=$result_dir/$target_id
# # mkdir -p $target_result_dir
# mas_masking -T $target_dir -t $target_id -A $atlas_dir -a $atlas_id -r $result_dir
# done
# done
# }