-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_data.sh
108 lines (91 loc) · 3.68 KB
/
preprocess_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
#!/bin/bash
#
# Preprocess data. From raw images, preceeds to resampling, reorientation and gradient distortion correction.
# Usage:
# ./preprocess_data.sh <SUBJECT> <PATH_GRADCORR_FILE>
#
#
# Authors: Sandrine Bédard, Julien Cohen-Adad
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
PATH_GRADCORR_FILE=$2
# get starting time:
start=`date +%s`
# 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 list of participants in processed data folder
if [[ ! -f "participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv .
fi
# Copy list of participants in resutls folder
if [[ ! -f $PATH_RESULTS/"participants.tsv" ]]; then
rsync -avzh $PATH_DATA/participants.tsv $PATH_RESULTS/"participants.tsv"
fi
# Copy source images
rsync -avzh $PATH_DATA/$SUBJECT .
# Go to anat folder where all structural data are located
cd ${SUBJECT}/anat/
# T1w
# ------------------------------------------------------------------------------
file_t1="${SUBJECT}_T1w"
# Rename the raw image
mv ${file_t1}.nii.gz ${file_t1}_raw.nii.gz
file_t1="${file_t1}_raw"
# Reorient to RPI and resample to 1 mm iso (supposed to be the effective resolution)
sct_image -i ${file_t1}.nii.gz -setorient RPI -o ${file_t1}_RPI.nii.gz
sct_resample -i ${file_t1}_RPI.nii.gz -mm 1x1x1 -o ${file_t1}_RPI_r.nii.gz
file_t1="${file_t1}_RPI_r"
# Gradient distortion correction
gradient_unwarp.py ${file_t1}.nii.gz ${file_t1}_gradcorr.nii.gz siemens -g ${PATH_GRADCORR_FILE}/coeff.grad -n
file_t1="${file_t1}_gradcorr"
# Rename gradcorr file
mv ${file_t1}.nii.gz ${SUBJECT}_T1w.nii.gz
# Delete raw, resampled and reoriented to RPI images and warp file from gradient distortion correction
rm -f ${SUBJECT}_T1w_raw.nii.gz ${SUBJECT}_T1w_raw_RPI.nii.gz ${SUBJECT}_T1w_raw_RPI_r.nii.gz fullWarp_abs.nii.gz
# T2w FLAIR
# ------------------------------------------------------------------------------
file_t2="${SUBJECT}_T2w"
# Rename raw file
mv ${file_t2}.nii.gz ${file_t2}_raw.nii.gz
file_t2="${file_t2}_raw"
# Reorient to RPI and resample to 1mm iso (supposed to be the effective resolution)
sct_image -i ${file_t2}.nii.gz -setorient RPI -o ${file_t2}_RPI.nii.gz
sct_resample -i ${file_t2}_RPI.nii.gz -mm 1x1x1 -o ${file_t2}_RPI_r.nii.gz
file_t2="${file_t2}_RPI_r"
# Gradient distortion correction
gradient_unwarp.py ${file_t2}.nii.gz ${file_t2}_gradcorr.nii.gz siemens -g ${PATH_GRADCORR_FILE}/coeff.grad -n
file_t2="${file_t2}_gradcorr"
# Rename gradcorr file
mv ${file_t2}.nii.gz ${SUBJECT}_T2w.nii.gz
# Delete raw, resampled and reoriented to RPI images and warp file from gradient distortion correction
rm -f ${SUBJECT}_T2w_raw.nii.gz ${SUBJECT}_T2w_raw_RPI.nii.gz ${SUBJECT}_T2w_raw_RPI_r.nii.gz fullWarp_abs.nii.gz
# Verify presence of output files and write log file if error
# ------------------------------------------------------------------------------
FILES_TO_CHECK=(
"${SUBJECT}_T1w.nii.gz"
"${SUBJECT}_T2w.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 "~~~"