forked from miykael/3dprintyourbrain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_3d_brain_docker.sh
175 lines (149 loc) · 6.06 KB
/
create_3d_brain_docker.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
#!/bin/bash -e
# 3dprintyourbrain, adapted script by skjerns, original by miykael
# usage: create_3d_brain_docker.sh subject_name.nii.gz
###############################################
set -e # exit on error
# Default values
smooth=75
decimate=290000
# Parse additional arguments
# Flag to check if the input file is set
input_file_set=false
# Collect input parameters and additional recon-all parameters
params=()
recon_all_params=()
while [[ "$#" -gt 0 ]]; do
case $1 in
--smooth)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
smooth="$2"
shift 2
else
echo "Error: --smooth expects a numeric argument."
exit 1
fi
;;
--decimate)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
decimate="$2"
shift 2
else
echo "Error: --decimate expects a numeric argument."
exit 1
fi
;;
-*)
# Collect any other options as recon-all parameters
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
recon_all_params+=("$1" "$2")
shift 2
else
recon_all_params+=("$1")
shift
fi
;;
*)
if $input_file_set; then
echo "Error: Multiple input files provided or unrecognized arguments."
exit 1
else
params+=("$1")
input_file_set=true
shift
fi
;;
esac
done
# Reassign parsed positional arguments
set -- "${params[@]}"
if [ "$input_file_set" = false ]; then
echo "Error: No input file provided."
exit 1
fi
export FSLOUTPUTTYPE=NIFTI_GZ
# Main folder for the whole project
if [ -f "$1" ]; then
subjT1="$1"
elif [ -f "/opt/share/$1" ]; then
subjT1="/opt/share/$1"
else
echo "Error: File $1 not found in current directory or in /opt/share."
exit 1
fi
echo "input file: $subjT1, postprocess: smooth: $smooth, decimate: $decimate"
echo
echo "running process recon-all -subjid 'output' -all -time -log logfile -nuintensitycor -sd '$MAIN_DIR/${subject}/' -parallel '${recon_all_params[@]} "
echo
echo "-----------------------------------------------"
echo "STARTING RECONSTRUCTION, CAN TAKE SEVERAL HOURS"
echo "-----------------------------------------------"
echo
sleep 3
start_time=$(date +%s)
export subjT1
export MAIN_DIR=$HOME/3dbrains
# Name of the subject
export subject=$(echo "$subjT1" | rev | cut -f 1 -d '/' | rev | cut -f 1 -d '.')
# Path to the subject (output folder)
export SUBJECTS_DIR=$MAIN_DIR/${subject}/output
#==========================================================================================
# 2. Create Surface Model with FreeSurfer
#==========================================================================================
mkdir -p $MAIN_DIR/${subject}/
mkdir -p $SUBJECTS_DIR/mri/orig
mri_convert ${subjT1} $SUBJECTS_DIR/mri/orig/001.mgz
# Pass additional recon-all parameters
recon-all -subjid "output" -all -time -log logfile -nuintensitycor -sd "$MAIN_DIR/${subject}/" -parallel "${recon_all_params[@]}"
#==========================================================================================
#3. Create 3D Model of Cortical and Subcortical Areas
#==========================================================================================
# CORTICAL
# Convert output of step (2) to fsl-format
mris_convert --combinesurfs $SUBJECTS_DIR/surf/lh.pial $SUBJECTS_DIR/surf/rh.pial \
$SUBJECTS_DIR/cortical.stl
# SUBCORTICAL
mkdir -p $SUBJECTS_DIR/subcortical
# First, convert aseg.mgz into NIfTI format
mri_convert $SUBJECTS_DIR/mri/aseg.mgz $SUBJECTS_DIR/subcortical/subcortical.nii
# Second, binarize all areas that you're not interested and inverse the binarization
mri_binarize --i $SUBJECTS_DIR/subcortical/subcortical.nii \
--match 2 3 24 31 41 42 63 72 77 51 52 13 12 43 50 4 11 26 58 49 10 17 18 53 54 44 5 80 14 15 30 62 \
--inv \
--o $SUBJECTS_DIR/subcortical/bin.nii
# Third, multiply the original aseg.mgz file with the binarized files
fslmaths.fsl $SUBJECTS_DIR/subcortical/subcortical.nii \
-mul $SUBJECTS_DIR/subcortical/bin.nii \
$SUBJECTS_DIR/subcortical/subcortical.nii.gz
# Fourth, copy original file to create a temporary file
cp $SUBJECTS_DIR/subcortical/subcortical.nii.gz $SUBJECTS_DIR/subcortical/subcortical_tmp.nii.gz
# Fifth, unzip this file
gunzip -f $SUBJECTS_DIR/subcortical/subcortical_tmp.nii.gz
# Sixth, check all areas of interest for wholes and fill them out if necessary
for i in 7 8 16 28 46 47 60 251 252 253 254 255
do
mri_pretess $SUBJECTS_DIR/subcortical/subcortical_tmp.nii \
$i \
$SUBJECTS_DIR/mri/norm.mgz \
$SUBJECTS_DIR/subcortical/subcortical_tmp.nii
done
# Seventh, binarize the whole volume
fslmaths.fsl $SUBJECTS_DIR/subcortical/subcortical_tmp.nii -bin $SUBJECTS_DIR/subcortical/subcortical_bin.nii
# Eighth, create a surface model of the binarized volume with mri_tessellate
mri_tessellate $SUBJECTS_DIR/subcortical/subcortical_bin.nii.gz 1 $SUBJECTS_DIR/subcortical/subcortical
# Ninth, convert binary surface output into stl format
mris_convert $SUBJECTS_DIR/subcortical/subcortical $SUBJECTS_DIR/subcortical.stl
# last, apply preprocessing by smoothing and combining the meshes
# and decimating to 290.000 vertices (tinkercad limit is 300.000)
python3 /opt/post_process_mesh.py $SUBJECTS_DIR --smooth $smooth --decimate $decimate
cp $SUBJECTS_DIR/final.stl /opt/share/$subject.stl
#==========================================================================================
# Cleanup
#==========================================================================================
rm -R -- $SUBJECTS_DIR/*/
rm $SUBJECTS_DIR/logfile
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
formatted_time=$(printf "%02d:%02d:%02d" $((elapsed_time/3600)) $((elapsed_time%3600/60)) $((elapsed_time%60)))
echo
echo "n---------------------------------"
echo "Finished after $formatted_time. Output can be found at $subject.stl"