forked from swvanderlaan/slideToolKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slideSQLheader
executable file
·232 lines (207 loc) · 5.97 KB
/
slideSQLheader
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
#!/bin/bash
#
# Description: Extract SQL-file column headers created by cellprofiler
#
# The MIT License (MIT)
# Copyright (c) 2014-2016, Bas G.L. Nelissen, UMC 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="Extract SQL-file column names created by cellprofiler"
DEPENDENCIES=("perl")
SUFFIX=".head"
# usage message
usage() {
cat <<- EOF
$SCRIPTNAME --help for more information.
EOF
}
# version info
version() {
cat << EOF
slideToolkit 0.1 beta
($SCRIPTNAME is part of the slideToolkit)
The MIT License (MIT) <http://opensource.org/licenses/MIT>
Copyright (c) 2014-2016, Bas G.L. Nelissen, UMC 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.
EOF
}
# Errors go to stderr
err() {
echo "${SCRIPTNAME} ERROR: $@" >&2
}
#help message
helpMessage() {
cat <<- EOF
${SCRIPTNAME}: ${DESCRIPTIONSHORT}
usage:
$SCRIPTNAME [options] -f <filename>
options:
-f, --file <filename> "SQL_SETUP.SQL" file
[--add-head] <filename> "file.csv" file
[--ignore-dependencies] ignores all dependencies, but gives warning
--help display this help
--version display version and license information
examples:
$SCRIPTNAME "SQL_SETUP.SQL"
$SCRIPTNAME --file="SQL_SETUP.SQL" --add-head="file.csv"
dependencies: ${DEPENDENCIES[@]}
Extract SQL-file column names created by cellprofiler. These headers
can be added to a .csv file using the --add-head option, a copy
(.head.csv) will be created
The slideToolkit and all its tools are released under the terms of the MIT license.
The slideToolkit (C) 2014-2016, Bas G.L. Nelissen, UMC Utrecht, the Netherlands.
Report issues at https://github.com/bglnelissen/slideToolkit/issues.
EOF
}
#Menu
#Empty variables
FILE=""
ADDHEAD=""
#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 ;;
--add-head)
ADDHEAD=$2
shift 2 ;;
--add-head=*)
ADDHEAD=${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 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">&2;
usage
exit 1
fi
if ! [[ -z "$ADDHEAD" ]]; then
if ! [[ -f "$ADDHEAD" ]] ; then
err "No such file: $ADDHEAD">&2;
usage
exit 1
fi
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
}
# actual program
programOutput(){
# Find line with "CREATE TABLE Per_Image (" and print this and '9999' lines after)
# Find line with "PRIMARY KEY (ImageNumber) )" and print this and '9999' lines before)
# Remove First and last line, fetch 1st column only, replace newlines with comma's.
# Remove trailing comma
HEAD=$(cat "$FILE" | \
grep -A 9999 -w "CREATE TABLE Per_Image (" | \
grep -B 9999 -w "PRIMARY KEY (ImageNumber) )" | \
tail -n+2 | head -n-1 | awk '{print $1}' | tr '\n' ',' | \
perl -p -e 's/^(.*).$/\1/')
# add head, yes -> do it, no -> echo head to stdout
if ! [[ -z "$ADDHEAD" ]]; then
echo "$(echo $HEAD; cat $ADDHEAD)" > "${ADDHEAD%.*}${SUFFIX}.${ADDHEAD##*.}"
else
echo "$HEAD"
fi
}
#all check?
checkRequirements
checkDependencies
#lets go!
programOutput