| 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: xcommon.sh |
| 28 |
# |
| 29 |
# Author: Jonathan Kans, Aaron Ucko |
| 30 |
# |
| 31 |
# Version Creation Date: 01/18/2025 |
| 32 |
# |
| 33 |
# ========================================================================== |
| 34 |
|
| 35 |
# environment variable turns on shell tracing |
| 36 |
|
| 37 |
if [ -n "${EDIRECT_TRACE}" ] && [ "${EDIRECT_TRACE}" = true ] |
| 38 |
then |
| 39 |
set -x |
| 40 |
fi |
| 41 |
|
| 42 |
# initialize common flags |
| 43 |
|
| 44 |
dbase="" |
| 45 |
|
| 46 |
ids="" |
| 47 |
rest="" |
| 48 |
input="" |
| 49 |
num="" |
| 50 |
|
| 51 |
mssg="" |
| 52 |
web_env="" |
| 53 |
qry_key="" |
| 54 |
qury="" |
| 55 |
|
| 56 |
reldate="" |
| 57 |
mindate="" |
| 58 |
maxdate="" |
| 59 |
datetype="" |
| 60 |
|
| 61 |
archiveBase="" |
| 62 |
postingsBase="" |
| 63 |
dataBase="" |
| 64 |
sourceBase="" |
| 65 |
|
| 66 |
# set up colors for error report |
| 67 |
|
| 68 |
ColorSetup() { |
| 69 |
|
| 70 |
if [ -z "$TERM" ] || [ ! -t 2 ] |
| 71 |
then |
| 72 |
RED="" |
| 73 |
BLUE="" |
| 74 |
BOLD="" |
| 75 |
FLIP="" |
| 76 |
INIT="" |
| 77 |
elif command -v tput >/dev/null |
| 78 |
then |
| 79 |
RED="$(tput setaf 1)" |
| 80 |
BLUE="$(tput setaf 4)" |
| 81 |
BOLD="$(tput bold)" |
| 82 |
FLIP="$(tput rev)" |
| 83 |
INIT="$(tput sgr0)" |
| 84 |
else |
| 85 |
# assume ANSI |
| 86 |
escape="$(printf '\033')" |
| 87 |
RED="${escape}[31m" |
| 88 |
BLUE="${escape}[34m" |
| 89 |
BOLD="${escape}[1m" |
| 90 |
FLIP="${escape}[7m" |
| 91 |
INIT="${escape}[0m" |
| 92 |
fi |
| 93 |
LOUD="${INIT}${RED}${BOLD}" |
| 94 |
INVT="${LOUD}${FLIP}" |
| 95 |
# clear color on terminal if "export EDIRECT_TRACE=true" has been used |
| 96 |
echo "${INIT}" > /dev/null |
| 97 |
} |
| 98 |
|
| 99 |
ColorSetup |
| 100 |
|
| 101 |
# highlighted error and warning functions |
| 102 |
|
| 103 |
DisplayError() { |
| 104 |
|
| 105 |
if [ $# -gt 0 ] |
| 106 |
then |
| 107 |
msg="$1" |
| 108 |
echo "${INVT} ERROR: ${LOUD} ${msg}${INIT}" >&2 |
| 109 |
fi |
| 110 |
} |
| 111 |
|
| 112 |
DisplayWarning() { |
| 113 |
|
| 114 |
if [ $# -gt 0 ] |
| 115 |
then |
| 116 |
msg="$1" |
| 117 |
echo "${INVT} WARNING: ${LOUD} ${msg}${INIT}" >&2 |
| 118 |
fi |
| 119 |
} |
| 120 |
|
| 121 |
DisplayNote() { |
| 122 |
|
| 123 |
if [ $# -gt 0 ] |
| 124 |
then |
| 125 |
msg="$1" |
| 126 |
echo "${INVT} NOTE: ${LOUD} ${msg}${INIT}" >&2 |
| 127 |
fi |
| 128 |
} |
| 129 |
|
| 130 |
# parse XML Config/File object |
| 131 |
|
| 132 |
ParseConfig() { |
| 133 |
|
| 134 |
mesg=$1 |
| 135 |
objc=$2 |
| 136 |
shift 2 |
| 137 |
|
| 138 |
if [ -z "$mesg" ] |
| 139 |
then |
| 140 |
return 1 |
| 141 |
fi |
| 142 |
|
| 143 |
while [ $# -gt 0 ] |
| 144 |
do |
| 145 |
var=$1 |
| 146 |
fld=$2 |
| 147 |
shift 2 |
| 148 |
value=$( echo "$mesg" | xtract -pattern Rec -ret "" -element "$fld" ) |
| 149 |
if [ -n "$value" ] |
| 150 |
then |
| 151 |
eval "$var=\$value" |
| 152 |
fi |
| 153 |
done |
| 154 |
|
| 155 |
return 0 |
| 156 |
} |
| 157 |
|
| 158 |
# parse ENTREZ_DIRECT object |
| 159 |
|
| 160 |
ParseMessage() { |
| 161 |
|
| 162 |
mesg=$1 |
| 163 |
objc=$2 |
| 164 |
shift 2 |
| 165 |
|
| 166 |
if [ -z "$mesg" ] |
| 167 |
then |
| 168 |
return 1 |
| 169 |
fi |
| 170 |
|
| 171 |
object=$( echo "$mesg" | tr -d '\n' | sed -n "s|.*<$objc>\\(.*\\)</$objc>.*|\\1|p" ) |
| 172 |
if [ -z "$object" ] |
| 173 |
then |
| 174 |
return 2 |
| 175 |
fi |
| 176 |
|
| 177 |
err=$( echo "$object" | sed -n 's|.*<Error>\(.*\)</Error>.*|\1|p' ) |
| 178 |
if [ -z "$err" ] |
| 179 |
then |
| 180 |
while [ $# -gt 0 ] |
| 181 |
do |
| 182 |
var=$1 |
| 183 |
fld=$2 |
| 184 |
shift 2 |
| 185 |
value=$( echo "$object" | sed -n "s|.*<$fld>\\(.*\\)</$fld>.*|\\1|p" ) |
| 186 |
eval "$var=\$value" |
| 187 |
done |
| 188 |
fi |
| 189 |
|
| 190 |
return 0 |
| 191 |
} |
| 192 |
|
| 193 |
# check for ENTREZ_DIRECT object, or list of UIDs, piped from stdin |
| 194 |
|
| 195 |
ParseStdin() { |
| 196 |
|
| 197 |
if [ \( -e /dev/fd/0 -o ! -d /dev/fd \) -a ! -t 0 ] |
| 198 |
then |
| 199 |
mssg=$( cat ) |
| 200 |
ParseMessage "$mssg" ENTREZ_DIRECT \ |
| 201 |
dbase Db web_env WebEnv qry_key QueryKey qury Query \ |
| 202 |
mindate MinDate maxdate MaxDate reldate RelDate \ |
| 203 |
datetype DateType num Count |
| 204 |
if [ "$?" = 2 ] |
| 205 |
then |
| 206 |
# if no ENTREZ_DIRECT message present, support passing raw UIDs via stdin |
| 207 |
rest="$mssg" |
| 208 |
else |
| 209 |
# support for UIDs instantiated within message in lieu of Entrez History |
| 210 |
rest=$( echo "$mssg" | |
| 211 |
xtract -pattern ENTREZ_DIRECT -sep "\n" -element Id | |
| 212 |
grep '.' | sort -n | uniq ) |
| 213 |
fi |
| 214 |
fi |
| 215 |
} |
| 216 |
|
| 217 |
# process common control flags |
| 218 |
|
| 219 |
CheckForArgumentValue() { |
| 220 |
|
| 221 |
tag="$1" |
| 222 |
rem="$2" |
| 223 |
|
| 224 |
if [ "$rem" -lt 2 ] |
| 225 |
then |
| 226 |
DisplayError "Missing ${tag} argument" |
| 227 |
exit 1 |
| 228 |
fi |
| 229 |
} |
| 230 |
|
| 231 |
# ensure date constraint argument consistency |
| 232 |
|
| 233 |
FixDateConstraints() { |
| 234 |
|
| 235 |
if [ -z "$datetype" ] |
| 236 |
then |
| 237 |
datetype="PDAT" |
| 238 |
fi |
| 239 |
|
| 240 |
if [ -z "$reldate" ] || [ "$reldate" -lt 1 ] |
| 241 |
then |
| 242 |
reldate="" |
| 243 |
fi |
| 244 |
|
| 245 |
# set default value for missing date range endpoint |
| 246 |
if [ -n "$mindate" ] && [ -z "$maxdate" ] |
| 247 |
then |
| 248 |
currentDate=$(date +%Y) |
| 249 |
maxdate="$currentDate" |
| 250 |
fi |
| 251 |
if [ -z "$mindate" ] && [ -n "$maxdate" ] |
| 252 |
then |
| 253 |
mindate="1900" |
| 254 |
fi |
| 255 |
|
| 256 |
if [ -z "$mindate" ] || [ -z "$maxdate" ] |
| 257 |
then |
| 258 |
mindate="" |
| 259 |
maxdate="" |
| 260 |
fi |
| 261 |
|
| 262 |
if [ -z "$reldate" ] && [ -z "$mindate" ] && [ -z "$maxdate" ] |
| 263 |
then |
| 264 |
datetype="" |
| 265 |
fi |
| 266 |
} |
| 267 |
|
| 268 |
# set path to Archive or Postings folder |
| 269 |
|
| 270 |
FindLocalArchiveFolder() { |
| 271 |
|
| 272 |
dbs="$1" |
| 273 |
fld="$2" |
| 274 |
|
| 275 |
warn="true" |
| 276 |
if [ "$#" -gt 2 ] |
| 277 |
then |
| 278 |
warn="$3" |
| 279 |
fi |
| 280 |
|
| 281 |
# find selected local archive folder from environment variables or configuration file |
| 282 |
target=$( rchive -local "$dbs" "$fld" ) |
| 283 |
|
| 284 |
if [ -z "$target" ] || [ "$target" = "" ] |
| 285 |
then |
| 286 |
echo "ERROR: Must supply path to local data by setting EDIRECT_LOCAL_ARCHIVE environment variable" >&2 |
| 287 |
exit 1 |
| 288 |
fi |
| 289 |
|
| 290 |
osname=$( uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/' ) |
| 291 |
if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ] |
| 292 |
then |
| 293 |
target=$( cygpath -w "$target" ) |
| 294 |
fi |
| 295 |
|
| 296 |
# remove trailing slash |
| 297 |
target=${target%/} |
| 298 |
|
| 299 |
if [ ! -d "$target" ] && [ "$warn" = "true" ] |
| 300 |
then |
| 301 |
echo "ERROR: ${fld} directory ${target} is not present" >&2 |
| 302 |
exit 1 |
| 303 |
fi |
| 304 |
|
| 305 |
echo "$target" |
| 306 |
} |
| 307 |
|
| 308 |
FindArchiveFolder() { |
| 309 |
|
| 310 |
archiveBase=$( FindLocalArchiveFolder "$dbase" "Archive" ) |
| 311 |
} |
| 312 |
|
| 313 |
FindPostingsFolder() { |
| 314 |
|
| 315 |
postingsBase=$( FindLocalArchiveFolder "$dbase" "Postings" ) |
| 316 |
} |
| 317 |
|
| 318 |
FindDataFolder() { |
| 319 |
|
| 320 |
dataBase=$( FindLocalArchiveFolder "$dbase" "Data" ) |
| 321 |
} |
| 322 |
|
| 323 |
FindSourceFolder() { |
| 324 |
|
| 325 |
sourceBase=$( FindLocalArchiveFolder "$dbase" "Source" ) |
| 326 |
} |
| 327 |
|
| 328 |
# helper for constructing argument arrays |
| 329 |
|
| 330 |
AddIfNotEmpty() { |
| 331 |
|
| 332 |
if [ -n "$2" ] |
| 333 |
then |
| 334 |
ky=$1 |
| 335 |
vl=$2 |
| 336 |
shift 2 |
| 337 |
"$@" "$ky" "$vl" |
| 338 |
else |
| 339 |
shift 2 |
| 340 |
"$@" |
| 341 |
fi |
| 342 |
} |
| 343 |
|
| 344 |
# passes all possible data source arguments to ecollect |
| 345 |
|
| 346 |
CallEcollect() { |
| 347 |
|
| 348 |
# execute query |
| 349 |
res=$( "$@" ) |
| 350 |
|
| 351 |
# use printf percent-s instead of echo to prevent unwanted evaluation of backslash |
| 352 |
printf "%s\n" "$res" | sed -e '${/^$/d;}' |
| 353 |
} |
| 354 |
|
| 355 |
GetFromEUtils() { |
| 356 |
|
| 357 |
set ecollect |
| 358 |
|
| 359 |
AddIfNotEmpty -db "$dbase" \ |
| 360 |
AddIfNotEmpty -web "$web_env" \ |
| 361 |
AddIfNotEmpty -key "$qry_key" \ |
| 362 |
AddIfNotEmpty -query "$qury" \ |
| 363 |
AddIfNotEmpty -reldate "$reldate" \ |
| 364 |
AddIfNotEmpty -mindate "$mindate" \ |
| 365 |
AddIfNotEmpty -maxdate "$maxdate" \ |
| 366 |
AddIfNotEmpty -datetype "$datetype" \ |
| 367 |
AddIfNotEmpty -num "$num" \ |
| 368 |
CallEcollect "$@" |
| 369 |
} |
| 370 |
|
| 371 |
GetUIDs() { |
| 372 |
|
| 373 |
if [ -n "$web_env" ] && [ -n "$qry_key" ] |
| 374 |
then |
| 375 |
GetFromEUtils |
| 376 |
elif [ -n "$qury" ] && [ "$dbase" = "pubmed" ] |
| 377 |
then |
| 378 |
GetFromEUtils |
| 379 |
elif [ -n "$ids" ] |
| 380 |
then |
| 381 |
echo "$ids" | |
| 382 |
# faster version of accn-at-a-time without case transformation |
| 383 |
tr -cs a-zA-Z0-9_. '\n' |
| 384 |
|
| 385 |
elif [ -n "$rest" ] |
| 386 |
then |
| 387 |
# raw UIDs or instantiated UIDs extracted from ENTREZ_DIRECT message |
| 388 |
echo "$rest" | |
| 389 |
tr -cs a-zA-Z0-9_. '\n' |
| 390 |
|
| 391 |
elif [ -n "$input" ] |
| 392 |
then |
| 393 |
# input file of raw UIDs |
| 394 |
cat "$input" | |
| 395 |
tr -cs a-zA-Z0-9_. '\n' |
| 396 |
|
| 397 |
else |
| 398 |
DisplayError "Missing argument describing data source" |
| 399 |
exit 1 |
| 400 |
fi | |
| 401 |
|
| 402 |
if [ "$dbase" = "pmc" ] |
| 403 |
then |
| 404 |
# remove any PMC prefix |
| 405 |
sed -e 's/^PMC//g' -e 's/^pmc//g' |
| 406 |
else |
| 407 |
grep '.' |
| 408 |
fi | |
| 409 |
|
| 410 |
# sort and unique final UID results |
| 411 |
sort -n | uniq |
| 412 |
} |