-
Notifications
You must be signed in to change notification settings - Fork 22
/
makeJSON.sh
291 lines (268 loc) · 7.48 KB
/
makeJSON.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
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
#!/bin/bash
#$ -cwd
#$ -j y
#$ -S /bin/bash
#$ -V
function usage()
{
echo "makeJSON.sh"
echo "Kami E. Chiotti 10.15.18"
echo
echo "Takes user-edited template for CWL Workflow Tool input options and converts to JSON file"
echo
echo "Usage: $0 [-i INPUT_TEMPLATE] [-p SAMPLE_LIST] [-o OUTPUT_PREFIX] "
echo
echo "Required:"
echo " [-i INPUT_TEMPLATE] - Full path to CWL Workflow Tool file."
echo
echo "Optional:"
echo " [-o OUTPUT_PREFIX] - Prefix for output JSON filename [defaults to 'input' when \$SAMPLE_LIST is NOT provided,"
echo " ignored when \$SAMPLE_LIST is provided]"
echo " [-p SAMPLE_LIST] - Required if using \$SAMPLE_LIST; full path to a tab-delimited file"
exit
}
INPUT_TEMPLATE=""
OUTPUT_PREFIX=""
SAMPLE_LIST=""
while getopts ":i:o:p:h" Option
do
case $Option in
i ) INPUT_TEMPLATE="$OPTARG" ;;
o ) OUTPUT_PREFIX="$OPTARG" ;;
p ) SAMPLE_LIST="$OPTARG" ;;
h ) usage ;;
* ) echo "unrecognized argument. use '-h' for usage information."; exit -1 ;;
esac
done
shift $(($OPTIND - 1))
if [ "$INPUT_TEMPLATE" == "" ]
then
usage
fi
if [ ! -f "$INPUT_TEMPLATE" ]
then
echo "ERROR: Input template file does not exist."
exit 1
fi
if [[ "$SAMPLE_LIST" != "" && ! -f "$SAMPLE_LIST" ]]
then
echo "ERROR: Sample file does not exist."
exit 1
fi
if [[ -z "$OUTPUT_PREFIX" && "$SAMPLE_LIST" == "" ]]
then
OUTPUT_PREFIX=input
else
OUTPUT_PREFIX=tmp
fi
ABS_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ignore=("class" "path")
num=`wc -l $INPUT_TEMPLATE | cut -d' ' -f1`
#snum=`wc -l $SAMPLE_LIST | cut -d' ' -f1`
##########################################################################
###### FUNCTIONS #########################################################
##########################################################################
function chky()
{
eval "declare -A cklist="${2#*=} # work around for bash version 4.2.46
# local -n cklist=$2 # for bash version 4.3 or higher
mtch=0
for ele in "${cklist[@]}"
do
((mtch++))
if [[ $1 == "$ele" ]]
then
echo $mtch
fi
done
}
function keyval()
{
if [[ $2 =~ ^[0-9]+([.][0-9]+)?$ || $2 == [\[{] ]]
then
echo "\"$1\" : $2"
elif [[ $2 == "true" || $2 == "false" || $2 == "null" || ${2:0:1} == "\$" ]]
then
if [[ $3 == $num ]]
then
echo "\"$1\" : $2"
else
echo "\"$1\" : $2,"
fi
else
if [[ $3 == $num ]]
then
echo "\"$1\" : \"$2\""
else
echo "\"$1\" : \"$2\","
fi
fi
}
function loop_ary()
{
cls=$1
IFS=' ' read -a eles <<< `(echo $2 | sed 's/,/ /g')`
idx=`expr ${#eles[@]} - 1`
i=0
echo " {"
for ele in ${eles[@]}
do
echo " \"class\" : \"$cls\","
echo " \"path\" : $ele"
if [[ $i == $idx ]]
then
echo " }"
else
echo " },"
fi
((i++))
done
IFS=$'\n'
}
function single_json() # args: template file, output filename
{
tmplt=$1; outjsn=$2; lnum=0; varnum=1; ary_type=''; all_keys=()
echo "{" > $outjsn
IFS=$'\n'
for LINE in $(cat $tmplt) # LINE=`sed "${lnum}q;d" $tmplt`
do
((lnum++))
line=`echo $LINE | sed 's/ //g' | cut -d# -f1`
ky=`echo $line | cut -d: -f1`
vl=`echo $line | cut -d: -f2`
if [[ $vl == "<>" ]]
then
vl=\$$varnum
((varnum++))
fi
if [[ ${LINE:0:1} == "#" ]] # don't write these lines
then
continue
elif [[ (-z $vl && ${LINE:0:1} != "#") || (-z $ky && ${LINE:0:1} != "#") ]] # error if $ky or $vl is empty
then
echo "Template format error on line $lnum of template"
exit 1
elif [[ $ky == "-class" ]]
then
ary_type=$vl # set $ary_type for expected next input key="-path"
elif [[ $ky == "-path" ]]
then
if [[ ! -z $ary_type ]] # $ary_type = Directory/File
then
loop_ary $ary_type $vl # write array of File or Directory
ary_type=''
if [[ $lnum == $num ]]
then
echo " ]"
else
echo " ],"
fi
else
echo "Template format error on line $lnum; '- class' must be provided before '- path'" # "-path" found but $ary_type not set
exit 1
fi
elif [[ ${ky:0:2} == "-\"" ]]
then
echo " {"
echo " ${ky:1:${#ky}}"
echo " }"
if [[ $lnum == $num ]]
then
echo " ]"
else
echo " ],"
fi
elif [[ -z `chky $ky "$(declare -p ignore)"` ]] # work around for bash version 4.2.46
# elif [[ -z `chky $ky ignore` ]] # for bash version 4.3 or higher
then
echo " `keyval $ky $vl $lnum`"
all_keys+=($ky)
elif [[ `chky $ky "$(declare -p ignore)"` > 0 ]] # work around for bash version 4.2.46
# elif [[ `chky $ky ignore` > 0 ]] # for bash version 4.3 or higher
then
if [[ ($ky == "path") ]]
then
echo " `keyval $ky $vl $num`"
if [[ $lnum == $num ]]
then
echo " }"
else
echo " },"
fi
else
echo " `keyval $ky $vl $lnum`"
fi
else
echo "what am I missing at line $lnum or variable $varnum?"
fi
done
echo "}"
unset IFS
}
function write_script()
{
eval "declare -A args="${2#*=} # work around for bash version 4.2.46
# local -n args=$2 # for bash version 4.3 or higher
basejsn=$1
arglen=`echo ${#args[@]}`
echo '#!/bin/bash'
echo
echo "cat <<EOF"
cat $basejsn
echo "EOF"
}
function multi_json()
{
mkdir -p json_queue
cd json_queue
tmplt=../$1; slist=../$2; outjsn=$3
single_json $tmplt $outjsn >> $outjsn # write tmp.json with variables to fill from sample list
IFS=' ' read -a hdr <<< `(head -n 1 $slist | sed 's/://g')`
IFS=$'\n'
idxs=()
for key in "${all_keys[@]}" # get order in which keys were read from template
do
idx=`chky $key "$(declare -p hdr)"` # work around for bash version 4.2.46
# idx=`chky $key hdr` # for bash version 4.3 or higher
if [[ ! -z $idx ]]
then
idxs+=(\$$idx) # place arguments in order for running popJSON.sh
fi
done
write_script $outjsn "$(declare -p idxs)" > popJSON.sh # work around for bash version 4.2.46
# write_script $outjsn idxs > popJSON.sh # write script to fill from sample list; for bash version 4.3 or higher
slnum=0
for LINE in $(cat $slist)
do
((slnum++))
if [[ $slnum == 1 ]]
then
continue
else
arg_list=()
rid=`awk 'FNR=='$slnum' {print $1}' < $slist`
for i in ${idxs[@]}
do
fld=`echo $i`
arg_list+=(`awk 'FNR=='$slnum' {print '$fld'}' < $slist`)
done
sh popJSON.sh ${arg_list[@]} > $rid.json # run script for each input sample in list
fi
done
rm -f popJSON.sh tmp.json
cd ..
unset IFS
}
##########################################################################
###### MAIN ##############################################################
##########################################################################
uid=`date +%Y%m%d | sed 's/://g'`_makeJSON
logfile="$uid.log.txt"
{
if [[ "$SAMPLE_LIST" != "" ]]
then
multi_json $INPUT_TEMPLATE $SAMPLE_LIST $OUTPUT_PREFIX.json
else
single_json $INPUT_TEMPLATE $OUTPUT_PREFIX.json > $OUTPUT_PREFIX.json
fi
} > $logfile 2>&1