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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
#!/bin/sh
# ===========================================================================
#
# PUBLIC DOMAIN NOTICE
# National Center for Biotechnology Information (NCBI)
#
# This software/database is a "United States Government Work" under the
# terms of the United States Copyright Act. It was written as part of
# the author's official duties as a United States Government employee and
# thus cannot be copyrighted. This software/database is freely available
# to the public for use. The National Library of Medicine and the U.S.
# Government do not place any restriction on its use or reproduction.
# We would, however, appreciate having the NCBI and the author cited in
# any work or product based on this material.
#
# Although all reasonable efforts have been taken to ensure the accuracy
# and reliability of the software and data, the NLM and the U.S.
# Government do not and cannot warrant the performance or results that
# may be obtained by using this software or data. The NLM and the U.S.
# Government disclaim all warranties, express or implied, including
# warranties of performance, merchantability or fitness for any particular
# purpose.
#
# ===========================================================================
#
# File Name: epost
#
# Author: Jonathan Kans, Aaron Ucko
#
# Version Creation Date: 06/25/2020
#
# ==========================================================================
pth=$( dirname "$0" )
case ":$PATH:" in
*:"$pth":* )
;;
* )
PATH="$PATH:$pth"
export PATH
;;
esac
# conditionally execute original Perl implementation
PERL=""
internal=no
while [ "$#" -ne 0 ]
do
case "$1" in
-internal )
internal=yes
shift
;;
-newmode )
USE_NEW_EDIRECT=1
shift
;;
-oldmode )
USE_NEW_EDIRECT=0
shift
;;
* )
break
;;
esac
done
if [ "$internal" = yes ]
then
set _ -internal "$@"
shift
fi
if [ ! -f "$pth"/ecommon.sh ]
then
USE_NEW_EDIRECT=false
fi
case "${USE_NEW_EDIRECT}" in
[FfNn]* | 0 | [Oo][Ff][Ff] )
# set PERL path if using old EDirect
PERL=perl
case "$( uname -s )" in
CYGWIN_NT* )
# Use a negative match here because the shell treats 0 as success.
if perl -e 'exit $^O !~ /^MSWin/'; then
pth=$( cygpath -w "$pth" )
fi
;;
Darwin )
PERL="/usr/bin/perl"
;;
esac
;;
"" | * )
;;
esac
if [ -n "${PERL}" ]
then
exec "${PERL}" "$pth"/edirect.pl -post "$@"
exit 0
fi
# handle common flags - dot command is equivalent of "source"
. "$pth"/ecommon.sh
# help text
PrintHelp() {
echo "epost $version"
cat << "EOF"
-db Database name
-id Unique identifier(s) or accession number(s)
-format uid or acc
-input Read identifier(s) from file instead of stdin
Examples
echo 3OQZ_a | epost -db protein | efetch -format fasta
epost -db protein -id 3OQZ_a | efetch -format fasta
efetch -db protein -id 3OQZ_a -format fasta
echo GCF_000001405.38 | epost -db assembly | efetch -format docsum
epost -db assembly -id GCF_000001405.38 | efetch -format docsum
efetch -db assembly -id GCF_000001405.38 -format docsum
echo PRJNA257197 | epost -db bioproject | efetch -format docsum
epost -db bioproject -id PRJNA257197 | efetch -format docsum
efetch -db bioproject -id PRJNA257197 -format docsum
EOF
}
# check for isHelp flag
if [ "$isHelp" = true ]
then
PrintHelp
exit 0
fi
# initialize specific flags
format=""
# read command-line arguments
while [ $# -gt 0 ]
do
case "$1" in
-db )
shift
if [ $# -gt 0 ]
then
db="$1"
shift
else
echo "ERROR: Missing -db argument" >&2
exit 1
fi
;;
-id )
shift
if [ $# -gt 0 ]
then
ids="$1"
shift
else
echo "ERROR: Missing -id argument" >&2
exit 1
fi
while [ $# -gt 0 ]
do
case "$1" in
-* )
break
;;
* )
# concatenate run of UIDs with commas
ids="$ids,$1"
shift
;;
esac
done
;;
-format )
# no longer needed to identify non-numeric accessions, provided for backward compatibility
shift
if [ $# -gt 0 ]
then
format="$1"
shift
else
echo "ERROR: Missing -format argument" >&2
exit 1
fi
;;
-h | -help | --help )
PrintHelp
exit 0
;;
-* )
ParseCommonArgs "$@"
if [ "$argsConsumed" -gt 0 ]
then
shift "$argsConsumed"
else
echo "ERROR: Unrecognized option $1" >&2
exit 1
fi
;;
* )
# allows while loop to check for multiple flags
break
;;
esac
done
FinishSetup
# check for missing database argument
if [ -z "$db" ]
then
echo "ERROR: Missing -db argument" >&2
exit 1
fi
# check for piped UIDs unless database and UIDs provided in command line
if [ -z "$ids" ] && [ -z "$input" ]
then
ParseStdin
fi
# needHistory allows reuse of GenerateUidList
if [ -z "$ids$rest$input" ]
then
needHistory=true
fi
# take database from dbase value or -db argument
if [ -z "$dbase" ]
then
dbase="$db"
fi
# check for missing required arguments
if [ -z "$dbase" ]
then
echo "ERROR: Missing -db argument" >&2
exit 1
fi
# helper function adds post-specific arguments (if set)
RunWithPostArgs() {
if [ "$log" = true ]
then
printf "." >&2
fi
AddIfNotEmpty -WebEnv "$web_env" \
RunWithCommonArgs "$@"
}
# convert spaces between UIDs to commas
ids=$( echo "$ids" | sed -e "s/ /,/g; s/,,*/,/g" )
# lookup accessions in -id argument or piped from stdin
LookupSpecialAccessions
# post to history in groups, join if necessary with esearch
wb="$web_env"
PostInGroups() {
if [ "$log" = true ]
then
printf "EPost\n" >&2
fi
GenerateUidList "$dbase" |
join-into-groups-of 10000 |
while read uids
do
err=""
res=$( RunWithPostArgs nquire -url "$base" epost.fcgi -db "$dbase" -id "$uids" )
if [ -n "$res" ]
then
qry_key=""
ParseMessage "$res" ePostResult web_env WebEnv qry_key QueryKey
if [ -n "$err" ]
then
echo "ERROR: epost failed - $err" >&2
exit 1
fi
if [ -z "$web_env" ]
then
echo "WebEnv value not found in epost output - WebEnv1 $wb"
exit 1
fi
if [ -n "$wb" ] && [ "$web_env" != "$wb" ]
then
echo "WebEnv mismatch in epost output - WebEnv1 $wb, WebEnv2 $web_env"
exit 1
fi
WriteEDirectStep "$dbase" "$web_env" "$qry_key" "$err"
fi
done
if [ "$log" = true ]
then
printf "\n" >&2
fi
}
psts=$( PostInGroups )
if [ -n "$psts" ]
then
# extract first database and webenv values, and all key numbers
comps=$( echo "$psts" | xtract -wrp Set,Rec -pattern ENTREZ_DIRECT \
-wrp Web -element WebEnv -wrp Key -element QueryKey )
wbnv=$( echo "$comps" | xtract -pattern Set -first Web )
qrry=$( echo "$comps" | xtract -pattern Set -block Rec -pfx "(#" -sfx ")" -tab " OR " -element Key )
err=""
num=""
# send search command, e.g, "(#1) OR (#2)", along with database and web environment
srch=$( RunWithCommonArgs nquire -get "$base" esearch.fcgi -db "$dbase" \
-WebEnv "$wbnv" -term "$qrry" -retmax 0 -usehistory y )
if [ -n "$srch" ]
then
res=$( echo "$srch" | sed -e 's|<TranslationStack>.*</TranslationStack>||' )
ParseMessage "$srch" eSearchResult web_env WebEnv qry_key QueryKey num Count
fi
WriteEDirect "$dbase" "$web_env" "$qry_key" "$num" "$stp" "$err"
exit 0
fi
# warn on error
echo "ERROR: EPost failure" >&2
exit 1
|