| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# =========================================================================== |
| 4 |
# |
| 5 |
# PUBLIC DOMAIN NOTICE |
| 6 |
# National Center for Biotechnology Information (NCBI) |
| 7 |
# |
| 8 |
# This software/database is a "United States Government Work" under the |
| 9 |
# terms of the United States Copyright Act. It was written as part of |
| 10 |
# the author's official duties as a United States Government employee and |
| 11 |
# thus cannot be copyrighted. This software/database is freely available |
| 12 |
# to the public for use. The National Library of Medicine and the U.S. |
| 13 |
# Government do not place any restriction on its use or reproduction. |
| 14 |
# We would, however, appreciate having the NCBI and the author cited in |
| 15 |
# any work or product based on this material. |
| 16 |
# |
| 17 |
# Although all reasonable efforts have been taken to ensure the accuracy |
| 18 |
# and reliability of the software and data, the NLM and the U.S. |
| 19 |
# Government do not and cannot warrant the performance or results that |
| 20 |
# may be obtained by using this software or data. The NLM and the U.S. |
| 21 |
# Government disclaim all warranties, express or implied, including |
| 22 |
# warranties of performance, merchantability or fitness for any particular |
| 23 |
# purpose. |
| 24 |
# |
| 25 |
# =========================================================================== |
| 26 |
# |
| 27 |
# File Name: epost |
| 28 |
# |
| 29 |
# Author: Jonathan Kans, Aaron Ucko |
| 30 |
# |
| 31 |
# Version Creation Date: 06/25/2020 |
| 32 |
# |
| 33 |
# ========================================================================== |
| 34 |
|
| 35 |
pth=$( dirname "$0" ) |
| 36 |
|
| 37 |
case "$pth" in |
| 38 |
/* ) |
| 39 |
;; # already absolute |
| 40 |
* ) |
| 41 |
pth=$(cd "$pth" && pwd) |
| 42 |
;; |
| 43 |
esac |
| 44 |
|
| 45 |
case ":$PATH:" in |
| 46 |
*:"$pth":* ) |
| 47 |
;; |
| 48 |
* ) |
| 49 |
PATH="$PATH:$pth" |
| 50 |
export PATH |
| 51 |
;; |
| 52 |
esac |
| 53 |
|
| 54 |
# handle common flags - dot command is equivalent of "source" |
| 55 |
|
| 56 |
if [ ! -f "$pth"/ecommon.sh ] |
| 57 |
then |
| 58 |
echo "ERROR: Unable to find '$pth/ecommon.sh' file" >&2 |
| 59 |
exit 1 |
| 60 |
fi |
| 61 |
|
| 62 |
. "$pth"/ecommon.sh |
| 63 |
|
| 64 |
# initialize specific flags |
| 65 |
|
| 66 |
internal=false |
| 67 |
|
| 68 |
format="" |
| 69 |
|
| 70 |
# read command-line arguments |
| 71 |
|
| 72 |
while [ $# -gt 0 ] |
| 73 |
do |
| 74 |
tag="$1" |
| 75 |
rem="$#" |
| 76 |
case "$tag" in |
| 77 |
-internal ) |
| 78 |
internal=true |
| 79 |
shift |
| 80 |
;; |
| 81 |
-newmode | -oldmode ) |
| 82 |
shift |
| 83 |
;; |
| 84 |
-db ) |
| 85 |
CheckForArgumentValue "$tag" "$rem" |
| 86 |
shift |
| 87 |
db="$1" |
| 88 |
shift |
| 89 |
;; |
| 90 |
-id ) |
| 91 |
CheckForArgumentValue "$tag" "$rem" |
| 92 |
shift |
| 93 |
ids="$1" |
| 94 |
shift |
| 95 |
while [ $# -gt 0 ] |
| 96 |
do |
| 97 |
case "$1" in |
| 98 |
-* ) |
| 99 |
break |
| 100 |
;; |
| 101 |
* ) |
| 102 |
# concatenate run of UIDs with commas |
| 103 |
ids="$ids,$1" |
| 104 |
shift |
| 105 |
;; |
| 106 |
esac |
| 107 |
done |
| 108 |
;; |
| 109 |
-format ) |
| 110 |
# no longer needed to identify non-numeric accessions, provided for backward compatibility |
| 111 |
CheckForArgumentValue "$tag" "$rem" |
| 112 |
shift |
| 113 |
format="$1" |
| 114 |
shift |
| 115 |
;; |
| 116 |
-h | -help | --help | help ) |
| 117 |
echo "epost $version" |
| 118 |
echo "" |
| 119 |
cat "$pth/help/epost-help.txt" |
| 120 |
echo "" |
| 121 |
exit 0 |
| 122 |
;; |
| 123 |
-* ) |
| 124 |
ParseCommonArgs "$@" |
| 125 |
if [ "$argsConsumed" -gt 0 ] |
| 126 |
then |
| 127 |
shift "$argsConsumed" |
| 128 |
else |
| 129 |
DisplayError "Unrecognized option $1" |
| 130 |
exit 1 |
| 131 |
fi |
| 132 |
;; |
| 133 |
* ) |
| 134 |
DisplayError "Unrecognized argument $1" |
| 135 |
shift |
| 136 |
;; |
| 137 |
esac |
| 138 |
done |
| 139 |
|
| 140 |
FinishSetup |
| 141 |
|
| 142 |
# check for piped UIDs unless database and UIDs provided in command line |
| 143 |
|
| 144 |
if [ -z "$ids" ] && [ -z "$input" ] |
| 145 |
then |
| 146 |
ParseStdin |
| 147 |
fi |
| 148 |
|
| 149 |
if [ -z "$ids$rest$qury$input" ] |
| 150 |
then |
| 151 |
needHistory=true |
| 152 |
fi |
| 153 |
|
| 154 |
# take database from dbase value or -db argument |
| 155 |
|
| 156 |
if [ -z "$dbase" ] |
| 157 |
then |
| 158 |
dbase="$db" |
| 159 |
fi |
| 160 |
|
| 161 |
if [ "$dbase" = "nucleotide" ] |
| 162 |
then |
| 163 |
dbase="nuccore" |
| 164 |
fi |
| 165 |
|
| 166 |
# check for missing required arguments |
| 167 |
|
| 168 |
if [ -z "$dbase" ] |
| 169 |
then |
| 170 |
DisplayError "Missing -db argument" |
| 171 |
exit 1 |
| 172 |
fi |
| 173 |
|
| 174 |
# normalize to lower-case (e.g., SRA -> sra) |
| 175 |
|
| 176 |
dbase=$( echo "$dbase" | tr '[:upper:]' '[:lower:]' ) |
| 177 |
|
| 178 |
# convert spaces between UIDs to commas |
| 179 |
|
| 180 |
ids=$( echo "$ids" | sed -e "s/ /,/g; s/,,*/,/g" ) |
| 181 |
|
| 182 |
# helper function adds post-specific arguments (if set) |
| 183 |
|
| 184 |
RunWithPostArgs() { |
| 185 |
|
| 186 |
if [ "$log" = true ] |
| 187 |
then |
| 188 |
printf "." >&2 |
| 189 |
fi |
| 190 |
|
| 191 |
AddIfNotEmpty -WebEnv "$web_env" \ |
| 192 |
RunWithCommonArgs "$@" |
| 193 |
} |
| 194 |
|
| 195 |
# lookup accessions in -id argument or piped from stdin |
| 196 |
|
| 197 |
LookupSpecialAccessions |
| 198 |
|
| 199 |
# only in epost - lookup of nucleotide accessions to silently exclude retired records |
| 200 |
|
| 201 |
if [ "$needHistory" = false ] |
| 202 |
then |
| 203 |
case "$dbase" in |
| 204 |
nuc* ) |
| 205 |
nucUidList=$( GetUIDs ) |
| 206 |
anyNonInteger=$( echo "$nucUidList" | sed -e 's/[0-9.]//g' ) |
| 207 |
if [ -n "$anyNonInteger" ] |
| 208 |
then |
| 209 |
accn=$( echo "$nucUidList" | grep -v "^[0-9]*$" ) |
| 210 |
lcl=$( echo "$nucUidList" | grep "^[0-9]*$" ) |
| 211 |
if [ -n "$accn" ] |
| 212 |
then |
| 213 |
newids=$( echo "$accn" | |
| 214 |
PrepareAccnQuery "ACCN" "100" | |
| 215 |
RunAccnSearch "1000" ) |
| 216 |
if [ -n "$newids" ] |
| 217 |
then |
| 218 |
ids=$( echo "$newids $lcl" | fmt -w 1 | sort -n | uniq ) |
| 219 |
fi |
| 220 |
fi |
| 221 |
fi |
| 222 |
;; |
| 223 |
esac |
| 224 |
fi |
| 225 |
|
| 226 |
# post to history in groups, join if necessary with esearch |
| 227 |
|
| 228 |
wb="$web_env" |
| 229 |
|
| 230 |
PostInGroups() { |
| 231 |
|
| 232 |
if [ "$log" = true ] |
| 233 |
then |
| 234 |
printf "EPost\n" >&2 |
| 235 |
fi |
| 236 |
|
| 237 |
GetUIDs | |
| 238 |
join-into-groups-of 1000 | |
| 239 |
while read uids |
| 240 |
do |
| 241 |
err="" |
| 242 |
res=$( RunWithPostArgs nquire -url "$base" epost.fcgi -db "$dbase" -id "$uids" ) |
| 243 |
|
| 244 |
if [ -n "$res" ] |
| 245 |
then |
| 246 |
qry_key="" |
| 247 |
ParseMessage "$res" ePostResult web_env WebEnv qry_key QueryKey |
| 248 |
|
| 249 |
if [ -n "$err" ] |
| 250 |
then |
| 251 |
DisplayError "epost failed - $err" |
| 252 |
exit 1 |
| 253 |
fi |
| 254 |
if [ -z "$web_env" ] |
| 255 |
then |
| 256 |
DisplayError "WebEnv value not found in epost output - WebEnv1 $wb${INIT}" |
| 257 |
exit 1 |
| 258 |
fi |
| 259 |
if [ -n "$wb" ] && [ "$web_env" != "$wb" ] |
| 260 |
then |
| 261 |
DisplayError "WebEnv mismatch in epost output - WebEnv1 $wb, WebEnv2 $web_env${INIT}" |
| 262 |
exit 1 |
| 263 |
fi |
| 264 |
|
| 265 |
WriteEDirectStep "$dbase" "$web_env" "$qry_key" "$err" |
| 266 |
fi |
| 267 |
done |
| 268 |
|
| 269 |
if [ "$log" = true ] |
| 270 |
then |
| 271 |
printf "\n" >&2 |
| 272 |
fi |
| 273 |
} |
| 274 |
|
| 275 |
joinByString() { |
| 276 |
|
| 277 |
local separator="$1" |
| 278 |
shift |
| 279 |
local first="$1" |
| 280 |
shift |
| 281 |
printf "%s" "$first" "${@/#/$separator}" |
| 282 |
} |
| 283 |
|
| 284 |
rotateString() { |
| 285 |
|
| 286 |
OLDIFS="$IFS" |
| 287 |
IFS=" OR " read -a qry <<< $1 |
| 288 |
lst="${qry[${#qry[@]}-1]}" |
| 289 |
rst="${qry[@]::${#qry[@]}-1}" |
| 290 |
lst+=(${rst}) |
| 291 |
joinByString ' OR ' ${lst[@]} |
| 292 |
IFS="$OLDIFS" |
| 293 |
} |
| 294 |
|
| 295 |
psts=$( PostInGroups ) |
| 296 |
|
| 297 |
if [ -n "$psts" ] |
| 298 |
then |
| 299 |
# extract first database and webenv values, and all key numbers |
| 300 |
comps=$( echo "$psts" | xtract -wrp Set,Rec -pattern ENTREZ_DIRECT \ |
| 301 |
-wrp Web -element WebEnv -wrp Key -element QueryKey ) |
| 302 |
|
| 303 |
wbnv=$( echo "$comps" | xtract -pattern Set -first Web ) |
| 304 |
qrry=$( echo "$comps" | xtract -pattern Set -block Rec -pfx "(#" -sfx ")" -tab " OR " -element Key ) |
| 305 |
|
| 306 |
if [ "$dbase" = "pubmed" ] |
| 307 |
then |
| 308 |
qrry=$( rotateString "$qrry" ) |
| 309 |
fi |
| 310 |
|
| 311 |
err="" |
| 312 |
num="" |
| 313 |
# send search command, e.g, "(#1) OR (#2)", along with database and web environment |
| 314 |
srch=$( RunWithCommonArgs nquire -get "$base" esearch.fcgi -db "$dbase" \ |
| 315 |
-WebEnv "$wbnv" -term "$qrry" -retmax 0 -usehistory y ) |
| 316 |
|
| 317 |
if [ -n "$srch" ] |
| 318 |
then |
| 319 |
res=$( echo "$srch" | sed -e 's|<TranslationStack>.*</TranslationStack>||' ) |
| 320 |
ParseMessage "$res" eSearchResult web_env WebEnv qry_key QueryKey num Count |
| 321 |
if [ "$qry_key" = "-1" ] |
| 322 |
then |
| 323 |
DisplayError "Failure to combine individual epost components with esearch query '${qrry}'" |
| 324 |
echo "${INVT} RESULT: ${LOUD} $srch${INIT}" >&2 |
| 325 |
fi |
| 326 |
fi |
| 327 |
|
| 328 |
WriteEDirect "$dbase" "$web_env" "$qry_key" "$num" "$stp" "$err" |
| 329 |
|
| 330 |
exit 0 |
| 331 |
fi |
| 332 |
|
| 333 |
# warn on error |
| 334 |
|
| 335 |
DisplayError "EPost failure" |
| 336 |
exit 1 |