forked from bglnelissen/slideToolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slideMacro
executable file
·325 lines (293 loc) · 10.3 KB
/
slideMacro
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
#
# Description: Create scaled mask and macro from a virtual slide
#
# The MIT License (MIT)
# Copyright (c) 2014-2021, Bas G.L. Nelissen, Sander W. van der Laan,
# UMC Utrecht, Utrecht, the Netherlands.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Variables
SCRIPTNAME=$(basename $0)
DESCRIPTIONSHORT="Create *.png from a virtual slide."
DEPENDENCIES=("convert")
SUPPORTED_FORMATS=("TIFF" "Aperio" "Hamamatsu" "iScan" "iScanHT" "Leica")
DEFAULT_MACROSUFFIX=".macro"
DEFAULT_MACRO_THUMBSIZE="" #"2000x2000" # Maximum size of macro image
DEFAULT_IM_ARGS="" # convert arguments # -verbose
DEFAULT_SLURMARGS="--time 00:05:00 --mem=8G" # Maximum values: rt=runtime HH:MM:SS vmem=GB
# Errors go to stderr
err() {
echo "${SCRIPTNAME} ERROR: $@" >&2
}
# usage message
usage() {
cat <<- EOF
$SCRIPTNAME --help for more information.
EOF
}
# version info
version() {
cat << EOF
slideToolKit v1.2
($SCRIPTNAME is part of the slideToolKit)
The MIT License (MIT) <http://opensource.org/licenses/MIT>
Copyright (c) 2014-2020, Bas G.L. Nelissen & Sander W. van der Laan, UMC Utrecht, Utrecht, the Netherlands.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
There is NO WARRANTY, to the extent permitted by law.
Written by Bas G.L. Nelissen | https://github.com/bglnelissen.
Edits by Sander W. van der Laan | https://github.com/swvanderlaan.
EOF
}
# help message
helpMessage() {
cat <<- EOF
${SCRIPTNAME}: ${DESCRIPTIONSHORT}
usage:
$SCRIPTNAME [options] -f <filename>
options:
-f, --file <filename> virtual slide to create mask from
[--macrosuffix] <text> macro suffix ["$DEFAULT_MACROSUFFIX"]
[-l, --layer] <integer> force specific layer by layer id
[--convert-args]='<arguments>' convert arguments ('=' sign and 'quotes' required)
[--ignore-dependencies] ignores all dependencies, but gives warning
--help display this help
--version display version and license information
examples:
$SCRIPTNAME "file.TIF"
$SCRIPTNAME --file="file.TIF"
$SCRIPTNAME -f "file.TIF"
$SCRIPTNAME -f "file.TIF --macrosuffix=".layer5"
print a job list:
find "\$(pwd)" -iname "*.TIF" -printf 'slideMacro is processing file [ %p ].\n' -exec $(command -v "$SCRIPTNAME") --file "{}" \;
print a job list for 'slurm':
find "\$(pwd)" -iname "*.TIF" -type f \\
-exec printf "sbatch $DEFAULT_SLURMARGS --chdir="\$(pwd)/" " \; \\
-exec printf "\$(command -v "$SCRIPTNAME") " \; \\
-exec echo "--file=\""{}"\" " \;
dependencies: ${DEPENDENCIES[@]}
supported formats: ${SUPPORTED_FORMATS[@]}
You can extract a layer to quickly inspect the image. This program will
use 'convert' to create a '*.macro.png'-file for you which you can
inspect. The 'macro'-image is a layer within the original TIF file.
Pipe the 'multiple folders at once' output in 'parallel' for parallel
processing.
For some slides, the virtual slide format is not known. For these
instances you need to run slideInfo to find the correct layer manually.
When the layer is identified (approximately 2000x2000 pixels), use
the --layer flag.
NOTE #1: by default we set -quiet to remove warnings such as those described
here: https://stackoverflow.com/questions/27608124/imagemagick-how-to-get-rid-of-tiffwarnings-768-message-about-unknown-field-wh
The slideToolKit and all its tools are released under the terms of the MIT license.
The slideToolKit (C) 2014-2021, Bas G.L. Nelissen, UMC Utrecht, the Netherlands;
Sander W. van der Laan | s.w.vanderlaan [at] gmail [dot] com, UMC Utrecht,
the Netherlands.
Report issues at https://github.com/swvanderlaan/slideToolKit/issues.
EOF
}
# Menu
# Empty variables
FILE=""
LAYER=""
MACROSUFFIX=""
# illegal option
illegalOption() {
cat <<- EOF
$SCRIPTNAME: illegal option $1
$(usage)
EOF
exit 1
}
# loop through options
while :
do
case $1 in
-h)
usage
exit 0 ;;
--help | -\?)
helpMessage
exit 0 ;;
--version )
version
exit 0 ;;
-f | --file)
FILE=$2
shift 2 ;;
--file=*)
FILE=${1#*=}
shift ;;
-l | --layer)
LAYER=$2
shift 2 ;;
--layer=*)
LAYER=${1#*=}
shift ;;
--macrosuffix)
MACROSUFFIX=$2
shift 2 ;;
--macrosuffix=*)
MACROSUFFIX=${1#*=}
shift ;;
--convert-args=*) # Equal sign and quotes are required for this option. If not, the convert arguments are seen as slideRename options.
IM_ARGS=${1#*=}
shift ;;
--ignore-dependencies)
IGNOREDEPENDENCIES=TRUE
shift ;;
--) # End of all options
shift
break ;;
-*)
illegalOption "$1"
shift ;;
*) # no more options. Stop while loop
break ;;
esac
done
# DEFAULTS
# set FILE
if [ "$FILE" != "" ]; then
FILE="$FILE"
else
FILE="$1"
fi
# set MACROSUFFIX default
if [ "$MACROSUFFIX" != "$DEFAULT_MACROSUFFIX" ]; then
MACROSUFFIX="$MACROSUFFIX"
else
MACROSUFFIX="$DEFAULT_MACROSUFFIX"
fi
# set IM_ARGS default
if [ "$IM_ARGS" != "" ]; then
IM_ARGS="$IM_ARGS"
else
IM_ARGS="$DEFAULT_IM_ARGS"
fi
# set IGNOREDEPENDENCIES
if [[ "$IGNOREDEPENDENCIES" =~ (true|TRUE|YES|yes) ]] ; then
IGNOREDEPENDENCIES=true
else
IGNOREDEPENDENCIES=false
fi
# requirements
checkRequirements() {
if ! [[ -f "$FILE" ]] ; then
err "No such file: $FILE"
usage
exit 1
fi
}
# Dependencies
checkDependencies(){
DEPS=""
DEPS_FAIL="0"
for DEP in ${DEPENDENCIES[@]}; do
if [[ 0 != "$(command -v "$DEP" >/dev/null ;echo "$?")" ]]; then
err "Missing dependency: $DEP"
DEPS_FAIL=$(($DEPS_FAIL + 1))
fi
done
if [[ "$DEPS_FAIL" > 0 ]]; then
# only warning when --ignore-dependencies is set.
if [[ "$IGNOREDEPENDENCIES" == "true" ]]; then
err "Ignoring missing dependencies (${DEPS_FAIL}), continue..."
else
err "Fix missing dependencies (${DEPS_FAIL}), exit."
usage
exit 1
fi
fi
}
# get mask function for each type of scanner
getMacro(){
# get thumb by layer id
LAYER="$LAYER"
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroiScanHT(){
# layer 8 contains the macro
LAYER=8
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroAperio(){
# the third to last layer contains the macro
NR_LAYERS=`identify "$FILE" | wc -l`
LAYER="$(($NR_LAYERS - 3))"
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroLeica(){
LAYER=5 # 5 determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroHamamatsu(){
LAYER=5 # 5 determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
# convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% "$MACRO"
}
getMacroiScan(){
LAYER=7 # 7 determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
convert -quiet $IM_ARGS "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
# actual program
programOutput(){
# set variables
LAYER="$LAYER"
FILEFULL="$(echo "$(cd "$(dirname "$FILE")"; pwd)"/"$(basename "$FILE")")" # full path $FULL
BASENAME="$(basename "$FILEFULL")" # basename
EXTENSION="${BASENAME##*.}" # extension
FILEPATH="${FILEFULL%.*}" # full path, no extension
FILENAME="${BASENAME%.*}" # filename, no extension"
MACRO="${FILEPATH}${MACROSUFFIX}.png"
# check metadata for scanner data
if [ "$LAYER" != "" ]; then
getMacro # layer is set, do it the easy way
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "iScanHT" | wc -l | awk '{print $1}' )" ]]; then
getMacroiScanHT # iScanHT found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Aperio" | wc -l | awk '{print $1}')" ]]; then
getMacroAperio # Aperio found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Leica" | wc -l | awk '{print $1}')" ]]; then
getMacroLeica # Leica found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Hamamatsu" | wc -l | awk '{print $1}')" ]]; then
getMacroHamamatsu # Hamamatsu found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "iScan" | wc -l | awk '{print $1}')" ]]; then
getMacroiScan # Ventana iScan found
else err "Unknown virtual slide format: $FILEFULL"
err "You probably want to set --layer manually."
err "Run slideInfo for layer information."
usage
exit 1
fi
}
# all check?
checkRequirements
checkDependencies
# lets go!
# actual program
programOutput