Debian Codegraph @

archive-taxonomy

raw
1 #!/bin/bash
2
3 # Public domain notice for all NCBI EDirect scripts is located at:
4 # https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 # archive-taxonomy
7
8 total_start=$(date "+%s")
9
10 pth=$( dirname "$0" )
11
12 case "$pth" in
13 /* )
14 ;; # already absolute
15 * )
16 pth=$(cd "$pth" && pwd)
17 ;;
18 esac
19
20 case ":$PATH:" in
21 *:"$pth":* )
22 ;;
23 * )
24 PATH="$PATH:$pth"
25 export PATH
26 ;;
27 esac
28
29 # initialize specific flags
30
31 hasgo=$( command -v go )
32 if [ ! -x "$hasgo" ]
33 then
34 echo "ERROR: The Go (golang) compiler must be installed locally in order to process the data, EXITING" >&2
35 exit 1
36 fi
37
38 # database-specific parameters
39
40 dbase="taxonomy"
41 recname="TaxonInfo"
42 dotmaxIdx="500"
43 dotmaxInv="10"
44 fields="SCIN RANK GNSP COMN TXDV TXSY TAXA LNGE TREE MODS PROP VCHR GC HGC MGC PGC UID"
45
46 # control flags set by command-line arguments
47
48 useFtp=true
49 useHttps=false
50 noAspera=false
51
52 info=false
53
54 clean=false
55 scrub=false
56 scour=false
57 erase=false
58 zap=false
59
60 datafiles=true
61 download=true
62 populate=true
63
64 e2index=false
65 e2invert=false
66 e2collect=false
67 e2merge=false
68 e2post=false
69
70 while [ $# -gt 0 ]
71 do
72 case "$1" in
73 daily | -daily )
74 e2index=true
75 e2invert=true
76 datafiles=true
77 shift
78 ;;
79 index | -index | reindex | -reindex )
80 e2index=true
81 e2invert=true
82 e2collect=true
83 e2merge=true
84 e2post=true
85 datafiles=true
86 shift
87 ;;
88 clean | -clean | clear | -clear )
89 # delete Indices contents and Increment files
90 clean=true
91 shift
92 ;;
93 scrub | -scrub )
94 clean=true
95 # and delete Postings directories
96 scrub=true
97 shift
98 ;;
99 scour | -scour )
100 clean=true
101 scrub=true
102 # and delete Data, Archive, and Sentinels directories
103 scour=true
104 shift
105 ;;
106 erase | -erase )
107 clean=true
108 scrub=true
109 scour=true
110 # and delete Extras directory contents
111 erase=true
112 shift
113 ;;
114 zap | -zap )
115 clean=true
116 scrub=true
117 scour=true
118 # and delete Source records and all remaining directories
119 zap=true
120 shift
121 ;;
122 -info )
123 info=true
124 shift
125 ;;
126 -ftp )
127 useFtp=true
128 useHttps=false
129 noAspera=true
130 export EDIRECT_NO_ASPERA=true
131 shift
132 ;;
133 -http | -https )
134 useFtp=false
135 useHttps=true
136 shift
137 ;;
138 * )
139 break
140 ;;
141 esac
142 done
143
144 if [ "$clean" == true ] && [ "$e2index" == true ]
145 then
146 echo "ERROR: Cleaning and indexing must be done in separate commands, EXITING" >&2
147 exit 1
148 fi
149
150 if [ "$zap" = true ]
151 then
152 pm-clean -db "$dbase" -zap
153 exit 0
154 fi
155
156 if [ "$erase" = true ]
157 then
158 pm-clean -db "$dbase" -erase
159 exit 0
160 fi
161
162 if [ "$scour" = true ]
163 then
164 pm-clean -db "$dbase" -scour
165 exit 0
166 fi
167
168 if [ "$scrub" = true ]
169 then
170 pm-clean -db "$dbase" -scrub
171 exit 0
172 fi
173
174 if [ "$clean" = true ]
175 then
176 pm-clean -db "$dbase" -clean
177 exit 0
178 fi
179
180 # get path to local folder
181
182 osname=$( uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/' )
183
184 GetLocalArchiveFolder() {
185
186 dbs="$1"
187 fld="$2"
188
189 # find selected local archive folder from environment variables or configuration file
190 target=$( rchive -local "$dbs" "$fld" )
191
192 if [ -z "$target" ] || [ "$target" = "" ]
193 then
194 echo "ERROR: Must supply path to local data by setting EDIRECT_LOCAL_ARCHIVE environment variable" >&2
195 exit 1
196 fi
197
198 if [ -n "$osname" ] && [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
199 then
200 target=$( cygpath -w "$target" )
201 fi
202
203 # remove trailing slash
204 target=${target%/}
205
206 echo "$target"
207 }
208
209 date
210 echo "" >&2
211
212 pm-setup -db "$dbase"
213
214 echo "Preparing Drives" >&2
215 pm-prepare -db "$dbase"
216 echo "" >&2
217
218 archiveBase=$( GetLocalArchiveFolder "$dbase" "Archive" )
219 dataBase=$( GetLocalArchiveFolder "$dbase" "Data" )
220 extrasBase=$( GetLocalArchiveFolder "$dbase" "Extras" )
221 indexBase=$( GetLocalArchiveFolder "$dbase" "Index" )
222 invertBase=$( GetLocalArchiveFolder "$dbase" "Invert" )
223 mergedBase=$( GetLocalArchiveFolder "$dbase" "Merged" )
224 postingsBase=$( GetLocalArchiveFolder "$dbase" "Postings" )
225
226 DAT=""
227 DWN=""
228 POP=""
229
230 IDX=""
231 INV=""
232 COL=""
233 MRG=""
234 PST=""
235
236 if [ "$datafiles" = true ]
237 then
238 seconds_start=$(date "+%s")
239 echo "Downloading Taxonomy Data Files" >&2
240
241 if [ -d "${extrasBase}" ]
242 then
243 cd "${extrasBase}"
244
245 if [ ! -f "new_taxdump.tar.gz" ]
246 then
247 if [ "$useFtp" = true ]
248 then
249 # uses Aspera Connect (if installed), otherwise FTP
250 if [ "$noAspera" = true ]
251 then
252 nquire -dwn ftp.ncbi.nlm.nih.gov "pub/taxonomy/new_taxdump" "new_taxdump.tar.gz"
253 else
254 nquire -asp ftp.ncbi.nlm.nih.gov "pub/taxonomy/new_taxdump" "new_taxdump.tar.gz"
255 fi
256 elif [ "$useHttps" = true ]
257 then
258 nquire -bulk -get https://ftp.ncbi.nlm.nih.gov pub/taxonomy/new_taxdump new_taxdump.tar.gz > new_taxdump.tar.gz
259 fi
260 fi
261
262 if [ -f "new_taxdump.tar.gz" ]
263 then
264 rm -f *.dmp
265 gunzip -c new_taxdump.tar.gz |
266 tar xf -
267 fi
268 fi
269
270 seconds_end=$(date "+%s")
271 seconds=$((seconds_end - seconds_start))
272 DAT=$seconds
273 echo "DAT $DAT seconds" >&2
274 echo "" >&2
275 sleep 1
276 fi
277
278 if [ "$download" = true ]
279 then
280 seconds_start=$(date "+%s")
281 echo "Creating Taxonomy Records" >&2
282
283 if [ -d "${extrasBase}" ]
284 then
285 cd "${extrasBase}"
286
287 rm -f taxoninfo.xml
288
289 go run "$pth/extern/prep-taxoninfo.go" "$pth/data/" > taxoninfo.xml
290
291 if [ -f "${extrasBase}/taxoninfo.xml" ]
292 then
293 rm -f "${dataBase}/taxoninfo.xml"
294 cp "${extrasBase}/taxoninfo.xml" "${dataBase}/taxoninfo.xml"
295 fi
296 fi
297
298 seconds_end=$(date "+%s")
299 seconds=$((seconds_end - seconds_start))
300 DWN=$seconds
301 echo "DWN $DWN seconds" >&2
302 echo "" >&2
303 sleep 1
304 fi
305
306 if [ "$populate" = true ]
307 then
308 seconds_start=$(date "+%s")
309 echo "Populating Taxonomy Archive" >&2
310
311 if [ -d "${extrasBase}" ]
312 then
313 cd "${extrasBase}"
314
315 rchive -gzip -db "$dbase" -input "taxoninfo.xml" \
316 -archive "${archiveBase}" "${indexBase}" "${invertBase}" \
317 -index TaxID -pattern "$recname" < /dev/null
318 fi
319
320 seconds_end=$(date "+%s")
321 seconds=$((seconds_end - seconds_start))
322 POP=$seconds
323 echo "POP $POP seconds" >&2
324 echo "" >&2
325 sleep 1
326 fi
327
328 # check archive, printing "OK" from "Ochlandra keralensis" if record is successfully retrieved
329 echo 1949059 |
330 xfetch -db taxonomy |
331 xtract -pattern "$recname" -sep "" -pfx "Archive is " \
332 -upper "Genus[1:1],Species[1:1]"
333
334 echo "" >&2
335
336 # variable contains taxonomy-database-specific xtract indexing instructions
337 read -r -d '' idxtxt <<- EOS
338 xtract -set IdxDocumentSet -rec IdxDocument \
339 -pattern TaxonInfo -UID TaxonInfo/TaxID \
340 -wrp IdxUid -element "&UID" -clr -rst -tab "" \
341 -group TaxonInfo -pkg IdxSearchFields \
342 -block TaxonInfo \
343 -wrp UID -pad "&UID" \
344 -wrp SCIN -element Scientific \
345 -wrp GNSP -element Binomial \
346 -wrp COMN -element Common \
347 -wrp COMN -element GenBank \
348 -wrp TXSY -element Synonym \
349 -wrp TXDV -element Division -element Division@code \
350 -wrp RANK -element Rank \
351 -wrp LNGE -element Lineage \
352 -wrp TREE -element Tree \
353 -wrp GC -element Nuclear \
354 -wrp MGC -element Mitochondrial \
355 -wrp PGC -element Plastid \
356 -wrp HGC -element Hydrogenosome \
357 -wrp TAXA -element Domain \
358 -wrp TAXA -element Kingdom \
359 -wrp TAXA -element Phylum \
360 -wrp TAXA -element Class \
361 -wrp TAXA -element Order \
362 -wrp TAXA -element Family \
363 -wrp TAXA -element Genus \
364 -wrp TAXA -element Species \
365 -wrp MODS -pfx "<MODS>subspecies " -alnum Subspecies \
366 -wrp MODS -pfx "<MODS>serovar " -alnum Serovar \
367 -wrp MODS -pfx "<MODS>strain " -alnum Strain \
368 -wrp MODS -pfx "<MODS>substrain " -alnum Substrain \
369 -wrp MODS -pfx "<MODS>clade " -alnum Clade \
370 -wrp MODS -pfx "<MODS>note " -alnum Note \
371 -wrp VCHR -element SpID \
372 -block TaxonInfo -if Subspecies -wrp PROP -lbl "Has Subspecies" \
373 -block TaxonInfo -if Strain -wrp PROP -lbl "Has Strain" \
374 -block TaxonInfo -if Substrain -wrp PROP -lbl "Has Substrain" \
375 -block TaxonInfo -if Clade -wrp PROP -lbl "Has Clade" \
376 -block TaxonInfo -if Serovar -wrp PROP -lbl "Has Serovar"
377 EOS
378
379 if [ "$e2index" = true ]
380 then
381 seconds_start=$(date "+%s")
382 echo "Incremental Indexing" >&2
383
384 temp=$(mktemp /tmp/INDEX_TEMP.XXXXXXXXX)
385 # generate file with xtract indexing arguments, split onto separate lines, skipping past xtract command itself
386 echo "${idxtxt}" | xargs -n1 echo | tail -n +2 > $temp
387 rchive -db "$dbase" -e2incIndex "${archiveBase}" "${indexBase}" -idxargs "$temp" -dotmax "$dotmaxIdx" -e2index
388 rm "$temp"
389
390 seconds_end=$(date "+%s")
391 seconds=$((seconds_end - seconds_start))
392 IDX=$seconds
393 echo "IDX $IDX seconds" >&2
394 echo "" >&2
395 sleep 1
396 fi
397
398 if [ "$e2invert" = true ]
399 then
400 seconds_start=$(date "+%s")
401 echo "Incremental Inversion" >&2
402
403 rchive -db "$dbase" -dotmax "$dotmaxInv" -e2incInvert "${indexBase}" "${invertBase}"
404
405 if [ -d "${invertBase}" ]
406 then
407 cd "${invertBase}"
408
409 rm -f *.inv.gz
410 fi
411
412 seconds_end=$(date "+%s")
413 seconds=$((seconds_end - seconds_start))
414 INV=$seconds
415 echo "INV $INV seconds" >&2
416 echo "" >&2
417 sleep 1
418 fi
419
420 if [ "$e2collect" = true ]
421 then
422 seconds_start=$(date "+%s")
423 echo "Collect Inverted Sets" >&2
424
425 if [ -d "${invertBase}" ]
426 then
427 cd "${invertBase}"
428
429 rm -f *.inv.gz
430
431 idx=0
432 for dir in "${invertBase}"/*
433 do
434 if [ -d "$dir" ]
435 then
436 cd "$dir"
437 printf "."
438 rchive -gzip -join *.inv.gz > "${invertBase}/${dbase}$(printf %02d $idx).inv.gz"
439 idx=$(( idx + 1 ))
440 fi
441 done
442 printf "\n"
443 fi
444
445 seconds_end=$(date "+%s")
446 seconds=$((seconds_end - seconds_start))
447 COL=$seconds
448 echo "COL $COL seconds" >&2
449 echo "" >&2
450 sleep 1
451 fi
452
453 if [ "$e2merge" = true ]
454 then
455 seconds_start=$(date "+%s")
456 echo "Merging Inverted Indices" >&2
457
458 if [ -d "${invertBase}" ]
459 then
460 cd "${invertBase}"
461
462 rchive -gzip -db "$dbase" -merge "${mergedBase}" *.inv.gz
463 fi
464
465 seconds_end=$(date "+%s")
466 seconds=$((seconds_end - seconds_start))
467 MRG=$seconds
468 echo "MRG $MRG seconds" >&2
469 echo "" >&2
470 sleep 1
471 # taxonomy only goes to zyzzyzus warreni
472 if [ ! -f "${mergedBase}/zy.mrg.gz" ]
473 then
474 echo "ERROR: Merge failed to complete - missing zy.mrg.gz file" >&2
475 echo "" >&2
476 echo "EXITING DUE TO BUILD FAILURE" >&2
477 echo "" >&2
478 # do not continue
479 e2post=false
480 fi
481 fi
482
483 if [ "$e2post" = true ]
484 then
485 seconds_start=$(date "+%s")
486 echo "Producing Postings Files" >&2
487
488 if [ -d "${mergedBase}" ]
489 then
490 cd "${mergedBase}"
491
492 for fl in *.mrg.gz
493 do
494 echo "$fl"
495 done |
496 sort |
497 xargs -n 100 echo |
498 while read files
499 do
500 rchive -db "$dbase" -promote "${postingsBase}" "$fields" $files
501 done
502 fi
503
504 seconds_end=$(date "+%s")
505 seconds=$((seconds_end - seconds_start))
506 PST=$seconds
507 echo "PST $PST seconds" >&2
508 echo "" >&2
509 sleep 1
510 fi
511
512 wait
513
514 # check postings
515 okay=""
516 if [ "$e2post" = true ]
517 then
518 okay=$( xsearch -db "$dbase" -query "ochlandra keralensis [GNSP]" |
519 xfetch -db taxonomy |
520 xtract -pattern "$recname" -sep "" -upper "Genus[1:1],Species[1:1]" )
521 if [ "$okay" = "OK" ]
522 then
523 echo "Archive and Index are $okay" >&2
524 echo "" >&2
525 fi
526 fi
527
528 if [ "$e2post" = true ] && [ "$okay" = "OK" ] && [ -d "${mergedBase}" ]
529 then
530 target="${mergedBase}"
531 find "$target" -name "*.mrg" -delete
532 find "$target" -name "*.mrg.gz" -delete
533
534 if [ -d "${invertBase}" ]
535 then
536 cd "${invertBase}"
537 rm -f *.inv.gz
538 fi
539 fi
540
541 cd
542
543 echo "ARCHIVE-TAXONOMY" >&2
544
545 echo "" >&2
546
547 PrintTime() {
548
549 if [ "$1" = true ]
550 then
551 echo "$2 $3 seconds" >&2
552 fi
553 }
554
555 PrintTime "$datafiles" "DAT" "$DAT"
556 PrintTime "$download" "DWN" "$DWN"
557 PrintTime "$populate" "POP" "$POP"
558
559 PrintTime "$e2index" "IDX" "$IDX"
560 PrintTime "$e2invert" "INV" "$INV"
561 PrintTime "$e2collect" "COL" "$COL"
562 PrintTime "$e2merge" "MRG" "$MRG"
563 PrintTime "$e2post" "PST" "$PST"
564
565 echo "" >&2
566
567 function PrintTotalElapsedTime {
568 local L=$1
569 local T=$2
570 local D=$((T/60/60/24))
571 local H=$((T/60/60%24))
572 local M=$((T/60%60))
573 local S=$((T%60))
574 printf '%s %d second' "$L" $T 1>&2
575 (( $T > 1 )) && printf 's' 1>&2
576 if [ "$T" -gt 59 ]
577 then
578 printf ', or' 1>&2
579 (( $D > 0 )) && printf ' %d day' $D 1>&2
580 (( $D > 1 )) && printf 's' 1>&2
581 (( $H > 0 )) && printf ' %d hour' $H 1>&2
582 (( $H > 1 )) && printf 's' 1>&2
583 (( $M > 0 )) && printf ' %d minute' $M 1>&2
584 (( $M > 1 )) && printf 's' 1>&2
585 (( $S > 0 )) && printf ' %d second' $S 1>&2
586 (( $S > 1 )) && printf 's' 1>&2
587 fi
588 printf '\n' 1>&2
589 }
590
591 total_end=$(date "+%s")
592 total=$((total_end - total_start))
593 TOT=$total
594 PrintTotalElapsedTime "TOT" "$TOT"
595 echo "" >&2