-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_data.sh
176 lines (146 loc) · 5.83 KB
/
process_data.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
#!/bin/bash
#
# Process data.
#
# Usage:
# ./process_data.sh <SUBJECT>
#
# Manual segmentations or labels should be located under:
# XXX
#
# Authors: Sandrine Bédard
set -x
# Immediately exit if error
set -e -o pipefail
# Exit if user presses CTRL+C (Linux) or CMD+C (OSX)
trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT
# Retrieve input params
SUBJECT=$1
# Save script path
PATH_SCRIPT=$PWD
# get starting time:
start=`date +%s`
# FUNCTIONS
# ==============================================================================
# Check if manual segmentation already exists. If it does, copy it locally. If it does not, perform segmentation.
segment_if_does_not_exist(){
local file="$1"
local contrast="$2"
# Update global variable with segmentation file name
FILESEG="${file}_seg"
FILESEGMANUAL="${PATH_DATA}/${SUBJECT}/SC_seg.nii.gz"
FILESEGMANUAL2="${PATH_DATA}/${SUBJECT}/${file}_seg.nii.gz"
echo
if [[ -e $FILESEGMANUAL ]]; then
if [ $file == "T1w" ]; then
FILESEGMANUAL2=${FILESEGMANUAL}
fi
fi
echo
echo "Looking for manual segmentation: $FILESEGMANUAL2"
if [[ -e $FILESEGMANUAL2 ]]; then
echo "Found! Using manual segmentation."
rsync -avzh $FILESEGMANUAL2 ${FILESEG}.nii.gz
sct_qc -i ${file}.nii.gz -s ${FILESEG}.nii.gz -p sct_deepseg_sc -qc ${PATH_QC} -qc-subject ${SUBJECT}
else
echo "Not found. Proceeding with automatic segmentation."
# Segment spinal cord
sct_deepseg_sc -i ${file}.nii.gz -c $contrast -qc ${PATH_QC} -qc-subject ${SUBJECT}
fi
}
# SCRIPT STARTS HERE
# ==============================================================================
# Display useful info for the log, such as SCT version, RAM and CPU cores available
sct_check_dependencies -short
# Go to folder where data will be copied and processed
cd ${PATH_DATA_PROCESSED}
# Copy source images
rsync -avzh $PATH_DATA/$SUBJECT .
# Go to anat folder where all structural data are located
cd ${SUBJECT}
# Check if manual label already exists. If it does, copy it locally. If it does
# not, perform labeling.
# NOTE: manual disc labels include C1-2, C2-3 and C3-4.
label_if_does_not_exist(){
local file="$1"
local file_seg="$2"
local contrast="$3"
# Update global variable with labels file name
FILELABEL="${file}_labels"
FILELABELMANUAL="${PATH_DATA}/${SUBJECT}/${FILELABEL}-manual.nii.gz"
echo "Looking for manual label: $FILELABELMANUAL"
if [[ -e $FILELABELMANUAL ]]; then
echo "Found! Using manual labels."
rsync -avzh $FILELABELMANUAL ${FILELABEL}.nii.gz
# Generate labeled segmentation from manual disc labels
sct_label_vertebrae -i ${file}.nii.gz -s ${file_seg}.nii.gz -discfile ${FILELABEL}.nii.gz -c t1
else
echo "Not found. Proceeding with automatic labeling."
# Generate labeled segmentation
sct_label_vertebrae -i ${file}.nii.gz -s ${file_seg}.nii.gz -c $contrast
fi
}
# T1w
# ------------------------------------------------------------------------------
file_t1="T1w"
# Segment spinal cord (only if it does not exist)
segment_if_does_not_exist $file_t1 "t1"
file_t1_seg=$FILESEG
# Create labeled segmentation (only if it does not exist)
label_if_does_not_exist ${file_t1} ${file_t1_seg} "t1"
file_t1_seg_labeled="${file_t1_seg}_labeled"
# Generate QC report to assess vertebral labeling
sct_qc -i ${file_t1}.nii.gz -s ${file_t1_seg_labeled}.nii.gz -p sct_label_vertebrae -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute average cord CSA between C2 and C3
sct_process_segmentation -i ${file_t1_seg}.nii.gz -vert 2:3 -vertfile ${file_t1_seg_labeled}.nii.gz -o ${PATH_RESULTS}/csa-SC_c2c3_T1.csv -append 1
# T2w
# ------------------------------------------------------------------------------
file_t2="T2w"
# Segment spinal cord (only if it does not exist)
segment_if_does_not_exist $file_t2 "t1"
file_t2_seg=$FILESEG
# Create labeled segmentation (only if it does not exist)
label_if_does_not_exist ${file_t2} ${file_t2_seg} "t2"
file_t2_seg_labeled="${file_t2_seg}_labeled"
# Generate QC report to assess vertebral labeling
sct_qc -i ${file_t2}.nii.gz -s ${file_t2_seg_labeled}.nii.gz -p sct_label_vertebrae -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute average cord CSA between C2 and C3
sct_process_segmentation -i ${file_t2_seg}.nii.gz -vert 2:3 -vertfile ${file_t2_seg_labeled}.nii.gz -o ${PATH_RESULTS}/csa-SC_c2c3_T2.csv -append 1
# T2w_flair
# ------------------------------------------------------------------------------
file_t2_flair="T2w_flair"
# Segment spinal cord (only if it does not exist)
segment_if_does_not_exist $file_t2_flair "t1"
file_t2_flair_seg=$FILESEG
# Create labeled segmentation (only if it does not exist)
label_if_does_not_exist ${file_t2_flair} ${file_t2_flair_seg} "t2"
file_t2_flair_seg_labeled="${file_t2_flair_seg}_labeled"
# Generate QC report to assess vertebral labeling
sct_qc -i ${file_t2_flair}.nii.gz -s ${file_t2_flair_seg_labeled}.nii.gz -p sct_label_vertebrae -qc ${PATH_QC} -qc-subject ${SUBJECT}
# Compute average cord CSA between C2 and C3
sct_process_segmentation -i ${file_t2_flair_seg}.nii.gz -vert 2:3 -vertfile ${file_t2_flair_seg_labeled}.nii.gz -o ${PATH_RESULTS}/csa-SC_c2c3_T2_flair.csv -append 1
# Verify presence of output files and write log file if error
# ------------------------------------------------------------------------------
FILES_TO_CHECK=(
"T1w_seg.nii.gz"
"T1w_seg_labeled.nii.gz"
"T2w_seg.nii.gz"
"T2w_seg_labeled.nii.gz"
"T2w_flair_seg.nii.gz"
"T2w_flair_seg_labeled.nii.gz"
)
pwd
for file in ${FILES_TO_CHECK[@]}; do
if [[ ! -e $file ]]; then
echo "${SUBJECT}/anat/${file} does not exist" >> $PATH_LOG/_error_check_output_files.log
fi
done
# Display useful info for the log
end=`date +%s`
runtime=$((end-start))
echo
echo "~~~"
echo "SCT version: `sct_version`"
echo "Ran on: `uname -nsr`"
echo "Duration: $(($runtime / 3600))hrs $((($runtime / 60) % 60))min $(($runtime % 60))sec"
echo "~~~"