-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathashsPrepT1T2Inputs
executable file
·179 lines (150 loc) · 5.3 KB
/
ashsPrepT1T2Inputs
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
#!/bin/bash
#
# Takes T1 and T2 dicom/niftis
#
# If needed converts both inputs to niftis
# T1 input has the neck trimmed
# T1 and T2 files are copied into /tmp/Ashs-(T1|T2)-XXXXXXXX.nii.gz filenames
# so ashs_main won't get upset
#
# Returns
# T1.nii.gz
# T2.nii.gz
# T1-trimmed.nii.gz
# T1-trimmed-sanitized.nii.gz
# T2-sanitized.nii.gz
#
# T1/T2 nifti and nifti trimmed files will have the same base names as the T1/T2 input files.
# T1/T2 sanitized files will be arbitrary file names so they can be assured to be ashs_main.sh acceptable
#
function sys {
[ -n "${opt_n}${opt_v}" ] && echo "$(date): $@" 1>&2
[ -n "$opt_n" ] || "$@"
}
function niftiIfNeeded {
local InputFile="$1"
local NiftiFile
# Convert to nifi if needed
if echo "$InputFile" | grep -q ".nii.gz$"
then
NiftiFile="$InputFile"
elif echo "$InputFile" | grep -q '.nii$'
then
gzip "$InputFile"
NiftiFile="${InputFile}.gz"
elif file "$InputFile" | grep -q "Zip archive data"
then
DicomDir=$(mktemp -d "${TmpDir}/DicomDir-XXXXXXX")
[ -d "$DicomDir" ] || mkdir -p "$DicomDir"
NiftiFile="${TmpDir}/$(basename "$InputFile" | sed 's/.zip$//; s/.dicom//; s/$/.nii.gz/')"
sys unzip -qq -j -d "$DicomDir" "$InputFile"
SeriesID="$(sys c3d -dicom-series-list "$DicomDir" | tail -n 1 | awk '{print $NF}')"
# -dicom takes two arguments so -o follows
sys c3d -dicom-series-read "$DicomDir" "$SeriesID" -o "$NiftiFile"
rm -rf "$DicomDir"
else
echo "$cmd : Unrecognized input type '$InputFile'" 1>&2
exit 1
fi
realpath "$NiftiFile"
}
CmdName=$(basename "$0")
Syntax="${CmdName} {-1 T1} {-2 T2} [-n][-v][-c ConfigJsonFile]"
TrimScript=./trim_neck.sh
if [ -n "$TMPDIR" ]
then
TmpDir="$TMPDIR"
else
TmpDir=$(mktemp -d "/tmp/ashsPrepT1T2Inputs-XXXXXXX")
fi
while getopts 1:2:c:nv arg
do
case "$arg" in
1|2|c|n|v)
eval "opt_${arg}='${OPTARG:=1}'"
;;
esac
done
shift $(($OPTIND - 1))
if [ -n "$opt_1" ]
then
T1InputFile="$opt_1"
else
echo "${CmdName} : Missing -1 T1InputFile" 1>&2
echo "$Syntax" 1>&2
exit 1
fi
if [ -n "$opt_2" ]
then
T2InputFile="$opt_2"
else
echo "${CmdName} : Missing -2 T1InputFile" 1>&2
echo "$Syntax" 1>&2
exit 2
fi
if [ -n "$opt_c" ]
then
ConfigJsonFile="$opt_c"
fi
if [ ! -e "$ConfigJsonFile" ]
then
echo "${CmdName} : '$ConfigJsonFile' does not exist." 1>&2
exit 3
fi
T1NiftiFile=$(niftiIfNeeded "$T1InputFile")
sys c3d "$T1NiftiFile" -info 1>&2
IdString=$(basename "$T1NiftiFile" | sed -r 's/.gz$//; s/.zip$//; s/(.nii|.nifti|.dicom|.dcm)$//')
AcquisitionId=$(jq -r '.inputs.T1w.hierarchy.id' "$ConfigJsonFile")
if jq -r 'if ((.inputs.T1w.location.name == "'"$(basename $T1NiftiFile)"'") and (.inputs.T1w.object.tags | any("Trimmed"))) then true else false end' "$ConfigJsonFile" | grep -q true
then
[ -n "$opt_v" ] && echo "${CmdName} : '$T1NiftiFile' is tagged as 'Trimmed'" 1>&2
T1NiftiFileTrimmed=$T1NiftiFile
else
width=$(sys c3d "$T1NiftiFile" -info-full | grep "Image Dimensions" | awk '{print $4}' | sed 's/[[,]//g')
height=$(sys c3d "$T1NiftiFile" -info-full | grep "Image Dimensions" | awk '{print $5}' | sed 's/[[,]//g')
sx=$(sys c3d "$T1NiftiFile" -info-full | grep "Voxel Spacing" | awk '{print $4}' | sed 's/[][,]//g')
sy=$(sys c3d "$T1NiftiFile" -info-full | grep "Voxel Spacing" | awk '{print $5}' | sed 's/[][,]//g')
sz=$(c3d "$T1NiftiFile" -info-full | grep "Voxel Spacing" | awk '{print $6}' | sed 's/[][,]//g')
smax=$(python -c "print(max([$sx,$sy,$sz]))")
smin=$(python -c "print(min([$sx,$sy,$sz]))")
aspect_ratio_min=$(python -c "print( float($smin)/float($smax))")
aspect_ratio_max=$(python -c "print( float($smax)/float($smin))")
T1NiftiFileTrimmed=$(echo "${T1NiftiFile}" | sed 's/.nii.gz/-trimmed&/')
if (( $width > 40 )) && (( $height > 40 )) ; then
if [[ $(echo "$aspect_ratio_max < 3" | bc) == 1 ]] && [[ $(echo "$aspect_ratio_min > 0.33" | bc) == 1 ]] ; then
MaskDir="$TmpDir"/mask
InterDir="$TmpDir"/inter
[ -d "$MaskDir" ] || mkdir -p "$MaskDir"
[ -d "$InterDir" ] || mkdir -p "$InterDir"
sys "$TrimScript" -m "$MaskDir" -w "$InterDir" "$T1NiftiFile" "$T1NiftiFileTrimmed" 1>&2 || exit "$?"
sys fwuploadfile -p "$AcquisitionId" -t "Trimmed" "$T1NiftiFileTrimmed"
else
echo "Wrong scan input: the aspect ratio of the image is < 0.33 or > 3." 1>&2
exit 4
fi
else
echo "Wrong scan input: the width or/and the height of image is inferior to 40." 1>&2
exit 5
fi
fi
sys c3d "$T1NiftiFileTrimmed" -info 1>&2
T1NiftiFileTrimmedSanitized="${TmpDir}/T1NiftiTrimmedSanitized.nii.gz"
cp "$T1NiftiFileTrimmed" "$T1NiftiFileTrimmedSanitized"
sys c3d "$T1NiftiFileTrimmedSanitized" -info 1>&2
if [ "$T1InputFile" = "$T2InputFile" ]
then
T2NiftiFile="$T1NiftiFile"
T2NiftiFileSanitized="$T1NiftiFileTrimmedSanitized"
else
T2NiftiFile=$(niftiIfNeeded "$T2InputFile")
T2NiftiFileSanitized="${TmpDir}/T2NiftiSanitized.nii.gz"
cp "$T2NiftiFile" "$T2NiftiFileSanitized"
fi
sys c3d "$T2NiftiFileSanitized" -info 1>&2
cat << EOF
$T1NiftiFile
$T2NiftiFile
$T1NiftiFileTrimmed
$T1NiftiFileTrimmedSanitized
$T2NiftiFileSanitized
EOF