Debian Codegraph @

archive-pmc

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-pmc
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 # database-specific parameters
30
31 dbase="pmc"
32 recname="PMCInfo"
33 dotmaxIdx="50"
34 dotmaxInv="5"
35 fields="TITL ABST TEXT PAIR AUTH JOUR YEAR SIZE UID"
36
37 # control flags set by command-line arguments
38
39 useFtp=true
40 useHttps=false
41 noAspera=false
42 stem=false
43
44 info=false
45
46 clean=false
47 scrap=false
48 scrub=false
49 scour=false
50 erase=false
51 zap=false
52
53 datafiles=true
54 download=true
55 populate=true
56
57 justtest=false
58 justmiss=false
59
60 e2index=false
61 e2invert=false
62 e2collect=false
63 e2merge=false
64 e2post=false
65
66 while [ $# -gt 0 ]
67 do
68 case "$1" in
69 download | -download )
70 download=true
71 populate=false
72 shift
73 ;;
74 verify | -verify )
75 datafiles=false
76 download=false
77 populate=false
78 justtest=true
79 shift
80 ;;
81 missing | -missing )
82 datafiles=false
83 download=false
84 populate=false
85 justmiss=true
86 shift
87 ;;
88 daily | -daily )
89 e2index=true
90 e2invert=true
91 datafiles=true
92 shift
93 ;;
94 index | -index | reindex | -reindex )
95 e2index=true
96 e2invert=true
97 e2collect=true
98 e2merge=true
99 e2post=true
100 datafiles=true
101 shift
102 ;;
103 stem | -stem | stemmed | -stemmed )
104 stem=true
105 shift
106 ;;
107 clean | -clean | clear | -clear )
108 # delete Indices contents and Increment files
109 clean=true
110 shift
111 ;;
112 scrap | -scrap )
113 # only delete Postings directories
114 scrap=true
115 shift
116 ;;
117 scrub | -scrub )
118 clean=true
119 # and delete Postings directories
120 scrub=true
121 shift
122 ;;
123 scour | -scour )
124 clean=true
125 scrub=true
126 # and delete Data, Archive, and Sentinels directories
127 scour=true
128 shift
129 ;;
130 erase | -erase )
131 clean=true
132 scrub=true
133 scour=true
134 # and delete Extras directory contents
135 erase=true
136 shift
137 ;;
138 zap | -zap )
139 clean=true
140 scrub=true
141 scour=true
142 erase=true
143 # and delete Source records and all remaining directories
144 zap=true
145 shift
146 ;;
147 -info )
148 info=true
149 shift
150 ;;
151 -ftp )
152 useFtp=true
153 useHttps=false
154 noAspera=true
155 export EDIRECT_NO_ASPERA=true
156 shift
157 ;;
158 -http | -https )
159 useFtp=false
160 useHttps=true
161 shift
162 ;;
163 * )
164 break
165 ;;
166 esac
167 done
168
169 if [ "$clean" == true ] && [ "$e2index" == true ]
170 then
171 echo "ERROR: Cleaning and indexing must be done in separate commands, EXITING" >&2
172 exit 1
173 fi
174
175 if [ "$zap" = true ]
176 then
177 pm-clean -db "$dbase" -zap
178 exit 0
179 fi
180
181 if [ "$erase" = true ]
182 then
183 pm-clean -db "$dbase" -erase
184 exit 0
185 fi
186
187 if [ "$scour" = true ]
188 then
189 pm-clean -db "$dbase" -scour
190 exit 0
191 fi
192
193 if [ "$scrub" = true ]
194 then
195 pm-clean -db "$dbase" -scrub
196 exit 0
197 fi
198
199 if [ "$scrap" = true ]
200 then
201 pm-clean -db "$dbase" -scrap
202 exit 0
203 fi
204
205 if [ "$clean" = true ]
206 then
207 pm-clean -db "$dbase" -clean
208 exit 0
209 fi
210
211 if [ "$stem" = true ]
212 then
213 fields=$( echo "$fields STEM" )
214 fi
215
216 # get path to local folder
217
218 osname=$( uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/' )
219
220 GetLocalArchiveFolder() {
221
222 dbs="$1"
223 fld="$2"
224
225 # find selected local archive folder from environment variables or configuration file
226 target=$( rchive -local "$dbs" "$fld" )
227
228 if [ -z "$target" ] || [ "$target" = "" ]
229 then
230 echo "ERROR: Must supply path to local data by setting EDIRECT_LOCAL_ARCHIVE environment variable" >&2
231 exit 1
232 fi
233
234 if [ -n "$osname" ] && [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
235 then
236 target=$( cygpath -w "$target" )
237 fi
238
239 # remove trailing slash
240 target=${target%/}
241
242 echo "$target"
243 }
244
245 date
246 echo "" >&2
247
248 pm-setup -db "$dbase"
249
250 echo "Preparing Drives" >&2
251 pm-prepare -db "$dbase"
252 echo "" >&2
253
254 archiveBase=$( GetLocalArchiveFolder "$dbase" "Archive" )
255 dataBase=$( GetLocalArchiveFolder "$dbase" "Data" )
256 extrasBase=$( GetLocalArchiveFolder "$dbase" "Extras" )
257 indexBase=$( GetLocalArchiveFolder "$dbase" "Index" )
258 invertBase=$( GetLocalArchiveFolder "$dbase" "Invert" )
259 mergedBase=$( GetLocalArchiveFolder "$dbase" "Merged" )
260 postingsBase=$( GetLocalArchiveFolder "$dbase" "Postings" )
261 sourceBase=$( GetLocalArchiveFolder "$dbase" "Source" )
262
263 DAT=""
264 DWN=""
265 POP=""
266
267 IDX=""
268 INV=""
269 COL=""
270 MRG=""
271 PST=""
272
273 DoInfo() {
274
275 ret="0"
276
277 if [ -d "${sourceBase}" ]
278 then
279 cd "${sourceBase}"
280
281 current=$(
282 ls *.tar.gz 2>/dev/null |
283 grep baseline | cut -d '.' -f 4 | sort -n | tail -n 1
284 )
285
286 latest=$(
287 nquire -lst ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/oa_comm/xml |
288 grep baseline | grep ".tar.gz" |
289 cut -d '.' -f 4 | sort -n | tail -n 1
290 )
291
292 if [ -n "$current" ] && [ -n "$latest" ]
293 then
294 if [ "$current" != "$latest" ]
295 then
296 echo "ERROR: Need to update PMC release files from ${current} to ${latest} by first running archive-pmc -zap" >&2
297 else
298 echo "PMC files starting at ${current} are using current release" >&2
299 fi
300 elif [ -z "$current" ]
301 then
302 echo "Missing current files" >&2
303 elif [ -z "$latest" ]
304 then
305 echo "Missing latest files" >&2
306 fi
307 fi
308
309 echo "$ret"
310 }
311
312 if [ "$info" = true ]
313 then
314
315 ignore=$( DoInfo )
316
317 exit 0
318 fi
319
320 DoSerials() {
321
322 year=""
323
324 files=$(
325 # obtain names of base and update files for several years
326 nquire -bulk -get https://ftp.nlm.nih.gov projects/serfilelease |
327 sed -ne 's,.* href="\([^/"]*\)".*,\1,p' | grep -v marcxml
328 )
329
330 if [ -n "$files" ]
331 then
332 year=$(
333 # get latest year embedded in file names
334 echo "$files" | grep serfilebase | sort -Vr | head -n 1 |
335 sed -e 's/serfilebase.//' -e 's/.xml//'
336 )
337 if [ -n "$year" ]
338 then
339 # limit to serfilebase and serfile updates for current year
340 files=$( echo "$files" | grep "$year" )
341 fi
342 fi
343
344 basefile="serfilebase.${year}.xml"
345 updates=$( echo "$files" | grep -v serfilebase | sort -V )
346
347 if [ ! -f "serials.txt" ] && [ ! -s "$basefile" ]
348 then
349 echo "$basefile" >&2
350 nquire -bulk -get https://ftp.nlm.nih.gov projects/serfilelease "${basefile}" > $basefile
351 fi
352
353 if [ ! -f "serials.txt" ] && [ -s "$basefile" ]
354 then
355 echo "# ${basefile}" >> serials.txt
356 cat "$basefile" |
357 xtract -pattern NLMCatalogRecord -def "-" -element NlmUniqueID PublicationInfo/Country >> serials.txt
358 cat "$basefile" |
359 xtract -pattern DeleteCatalogRecord -block NlmUniqueID -element NlmUniqueID -lbl "-" -deq "\n" >> serials.txt
360 fi
361
362 if [ -f "serials.txt" ] && [ -n "$updates" ]
363 then
364 echo "$updates" |
365 while read serfile
366 do
367 if [ ! -s "$serfile" ]
368 then
369 echo "$serfile" >&2
370 nquire -bulk -get https://ftp.nlm.nih.gov projects/serfilelease "${serfile}" > $serfile
371 fi
372 if [ -s "$serfile" ]
373 then
374 if ! grep -Fq "$serfile" serials.txt
375 then
376 echo "# ${serfile}" >> serials.txt
377 cat "$serfile" |
378 xtract -pattern NLMCatalogRecord -def "-" -element NlmUniqueID PublicationInfo/Country >> serials.txt
379 cat "$serfile" |
380 xtract -pattern DeleteCatalogRecord -block NlmUniqueID -element NlmUniqueID -lbl "-" -deq "\n" >> serials.txt
381 fi
382 fi
383 done
384 fi
385 }
386
387 finish_jtas() {
388
389 tr -s ' ' |
390 sed -e 's/^ *//g' -e 's/ *$//g' |
391 sort-table -k 1,1f -k 3,3n -k 4,4nr -k 2,2f |
392 uniq -i |
393 awk -F '\t' '(NR == 1 || $1 != prev_key) { if (NR > 1) { print prev_line }; prev_key = $1; prev_line = $0 } END { print prev_line }' |
394 cut -f 1,2
395 }
396
397 multi_jtas() {
398
399 tr -s ' ' |
400 sed -e 's/^ *//g' -e 's/ *$//g' |
401 sort-table -k 1,1f -k 3,3n -k 4,4nr -k 2,2f |
402 uniq -i |
403 awk -F '\t' '(NR > 1 && $1 == prev_key && $4 == prev_flag) { print } (NR == 1 || $1 != prev_key) { print; prev_key = $1; prev_flag = $4 }' |
404 cut -f 1,2 | sort | uniq |
405 awk -F '\t' '{ if (NR == 1 || $1 != prev_key) { if (NR > 1) { print saved }; prev_key = $1; saved = $1 "\t" $2 } else { saved = saved " | " $2 } } END { print saved }'
406 }
407
408 JourCache() {
409
410 if [ "$useFtp" = true ]
411 then
412 nquire -ftp ftp.ncbi.nlm.nih.gov pubmed jourcache.xml
413 elif [ "$useHttps" = true ]
414 then
415 nquire -bulk -get https://ftp.ncbi.nlm.nih.gov pubmed jourcache.xml
416 fi
417 }
418
419 DoJournals() {
420
421 if [ ! -f "jourconv.xml" ]
422 then
423 if [ ! -f "jourcache.xml" ]
424 then
425 if [ -f "serials.txt" ]
426 then
427 JourCache |
428 grep -v DOCTYPE | grep -v ELEMENT | grep -v ATTLIST |
429 xtract -transfigure serials.txt \
430 -head "<JournalCache>" -tail "</JournalCache>" \
431 -pattern Journal -pkg Journal \
432 -block "Journal/*" -element "*" \
433 -block Journal -wrp Country -translate NlmUniqueID |
434 transmute -format > jourcache.xml
435 else
436 JourCache |
437 grep -v DOCTYPE | grep -v ELEMENT | grep -v ATTLIST |
438 transmute -format > jourcache.xml
439 fi
440 fi
441
442 if [ -f "jourcache.xml" ]
443 then
444 cat jourcache.xml |
445 xtract -set Set -pattern Journal \
446 -if Name -and MedAbbr \
447 -NAME Name -ABRV MedAbbr -ACTV ActivityFlag \
448 -group Name -pkg Rec \
449 -wrp Key -jour Name -wrp Abrv -jour "&ABRV" \
450 -wrp Indx -jour "&NAME" -wrp Name -element "&NAME" \
451 -wrp Type -lbl "1" -wrp Flag -element "&ACTV" \
452 -group MedAbbr -pkg Rec \
453 -wrp Key -jour MedAbbr -wrp Abrv -jour "&ABRV" \
454 -wrp Indx -jour "&NAME" -wrp Name -element "&NAME" \
455 -wrp Type -lbl "2" -wrp Flag -element "&ACTV" \
456 -group Alias \
457 -block Alias -pkg Rec \
458 -wrp Key -jour Alias -wrp Abrv -jour "&ABRV" \
459 -wrp Indx -jour "&NAME" -wrp Name -element "&NAME" \
460 -wrp Type -lbl "3" -wrp Flag -element "&ACTV" \
461 -group Journal -if "&ABRV" -equals "bioRxiv" \
462 -block Journal -pkg Rec \
463 -wrp Key -lbl "biorxiv.org" -wrp Abrv -jour "&ABRV" \
464 -wrp Indx -jour "&NAME" -wrp Name -element "&NAME" \
465 -wrp Type -lbl "3" -wrp Flag -element "&ACTV" \
466 -block Journal -pkg Rec \
467 -wrp Key -lbl "biorxivorg" -wrp Abrv -jour "&ABRV" \
468 -wrp Indx -jour "&NAME" -wrp Name -element "&NAME" \
469 -wrp Type -lbl "3" -wrp Flag -element "&ACTV" |
470 xtract -set Set -pattern Rec \
471 -group Rec \
472 -block Rec -pkg Rec \
473 -wrp Key -lower Key -wrp Abrv -element Abrv \
474 -wrp Indx -element Indx -wrp Name -element Name \
475 -wrp Type -element Type -wrp Flag -element Flag |
476 xtract -set Set -pattern Rec \
477 -group Rec \
478 -block Rec -pkg Rec \
479 -wrp Key -element Key -wrp Abrv -element Abrv \
480 -wrp Indx -element Indx -wrp Name -element Name \
481 -wrp Type -element Type -wrp Flag -element Flag \
482 -block Rec -if Key -starts-with "journal " -pkg Rec \
483 -wrp Key -pfx "<Key>the " -element Key -wrp Abrv -element Abrv \
484 -wrp Indx -element Indx -wrp Name -element Name \
485 -wrp Type -element Type -wrp Flag -element Flag \
486 -block Rec -if Key -starts-with "the journal " -pkg Rec \
487 -wrp Key -element "Key[5:]" -wrp Abrv -element Abrv \
488 -wrp Indx -element Indx -wrp Name -element Name \
489 -wrp Type -element Type -wrp Flag -element Flag |
490 transmute -format > jourconv.xml
491 fi
492 fi
493
494 if [ -f "jourconv.xml" ]
495 then
496 if [ ! -f "jourabrv.txt" ]
497 then
498 cat jourconv.xml | xtract -pattern Rec -element Key Abrv Type Flag | finish_jtas > jourabrv.txt
499 fi
500 if [ ! -f "jourindx.txt" ]
501 then
502 cat jourconv.xml | xtract -pattern Rec -element Key Indx Type Flag | finish_jtas > jourindx.txt
503 fi
504 if [ ! -f "journame.txt" ]
505 then
506 cat jourconv.xml | xtract -pattern Rec -element Key Name Type Flag | finish_jtas > journame.txt
507 fi
508 if [ ! -f "joursets.txt" ]
509 then
510 cat jourconv.xml | xtract -pattern Rec -element Key Name Type Flag | multi_jtas > joursets.txt
511 fi
512 if [ ! -f "jourmaps.xml" ] && [ -f "jourindx.txt" ]
513 then
514 cat jourindx.txt | tbl2xml -set JournalMaps -rec Journal Key Indx > jourmaps.xml
515 fi
516 fi
517 }
518
519 if [ "$datafiles" = true ]
520 then
521 seconds_start=$(date "+%s")
522
523 if [ -d "${extrasBase}" ]
524 then
525 cd "${extrasBase}"
526
527 echo "Downloading Serials" >&2
528 DoSerials
529
530 echo "Downloading Journals" >&2
531 DoJournals
532 fi
533
534 echo "Copying to Data Directory"
535 for fl in jourabrv.txt jourindx.txt journame.txt joursets.txt
536 do
537 if [ ! -f "${dataBase}/$fl" ] && [ -f "${extrasBase}/$fl" ]
538 then
539 cp "${extrasBase}/$fl" "${dataBase}/$fl"
540 fi
541 done
542
543 seconds_end=$(date "+%s")
544 seconds=$((seconds_end - seconds_start))
545 DAT=$seconds
546 echo "DAT $DAT seconds" >&2
547 echo "" >&2
548 sleep 1
549 fi
550
551 DownloadFTPorASP() {
552
553 fl="$1"
554 url="$2"
555
556 if [ "$noAspera" = true ]
557 then
558 echo "$fl" | nquire -dwn "${url}"
559 else
560 echo "$fl" | nquire -asp "${url}"
561 fi
562 }
563
564 DownloadOneByFTP() {
565
566 dir="$1"
567 fl="$2"
568
569 url="ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml"
570
571 DownloadFTPorASP "$fl" "${url}"
572
573 # delete if file is present but empty
574 if [ -f "$fl" ] && [ ! -s "$fl" ]
575 then
576 rm -f "$fl"
577 fi
578
579 # retry if no file
580 if [ ! -f "$fl" ]
581 then
582 sleep 10
583 echo "First Failed Download Retry" >&2
584 DownloadFTPorASP "$fl" "${url}"
585 fi
586
587 # retry again if still no file
588 if [ ! -f "$fl" ]
589 then
590 sleep 20
591 echo "Second Failed Download Retry" >&2
592 DownloadFTPorASP "$fl" "${url}"
593 fi
594
595 # retry once more if still no file, using -dwn instead of -asp
596 if [ ! -f "$fl" ]
597 then
598 sleep 30
599 echo "Third Failed Download Retry" >&2
600 echo "$fl" | nquire -dwn "${url}"
601 fi
602
603 # verify contents
604 if [ -s "$fl" ]
605 then
606 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
607 if [ -n "$errs" ]
608 then
609 # delete and retry one more time
610 rm -f "$fl"
611 sleep 10
612 echo "Invalid Contents Retry" >&2
613 DownloadFTPorASP "$fl" "${url}"
614 if [ -s "$fl" ]
615 then
616 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
617 if [ -n "$errs" ]
618 then
619 rm -f "$fl"
620 frst=$( echo "$errs" | head -n 1 )
621 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
622 fi
623 else
624 echo "Download Attempts Failed" >&2
625 fi
626 fi
627 else
628 rm -f "$fl"
629 echo "Download of '$fl' Failed" >&2
630 fi
631 }
632
633 DownloadOneByHTTPS() {
634
635 dir="$1"
636 fl="$2"
637
638 url="https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml"
639
640 nquire -bulk -get "${url}" "$fl" > "$fl"
641
642 # delete if file is present but empty
643 if [ -f "$fl" ] && [ ! -s "$fl" ]
644 then
645 rm -f "$fl"
646 fi
647
648 # retry if no file
649 if [ ! -f "$fl" ]
650 then
651 sleep 10
652 echo "First Failed Download Retry" >&2
653 nquire -bulk -get "${url}" "$fl" > "$fl"
654 fi
655
656 # retry again if still no file
657 if [ ! -f "$fl" ]
658 then
659 sleep 20
660 echo "Second Failed Download Retry" >&2
661 nquire -bulk -get "${url}" "$fl" > "$fl"
662 fi
663
664 # retry once more if still no file
665 if [ ! -f "$fl" ]
666 then
667 sleep 30
668 echo "Third Failed Download Retry" >&2
669 nquire -bulk -get "${url}" "$fl" > "$fl"
670 fi
671
672 # verify contents
673 if [ -s "$fl" ]
674 then
675 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
676 if [ -n "$errs" ]
677 then
678 # delete and retry one more time
679 rm -f "$fl"
680 sleep 10
681 echo "Invalid Contents Retry" >&2
682 nquire -bulk -get "${url}" "$fl" > "$fl"
683 if [ -s "$fl" ]
684 then
685 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
686 if [ -n "$errs" ]
687 then
688 rm -f "$fl"
689 frst=$( echo "$errs" | head -n 1 )
690 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
691 fi
692 fi
693 fi
694 else
695 rm -f "$fl"
696 echo "Download of '$fl' Failed" >&2
697 fi
698 }
699
700 DownloadSection() {
701
702 dir="$1"
703 flt="$2"
704
705 if [ "$useFtp" = true ]
706 then
707 nquire -lst "ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
708 grep ".tar.gz" | grep "$flt" |
709 skip-if-file-exists |
710 while read fl
711 do
712 sleep 1
713 echo "$fl" >&2
714 DownloadOneByFTP "$dir" "$fl"
715 done
716 elif [ "$useHttps" = true ]
717 then
718 nquire -get "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
719 xtract -pattern a -if a -starts-with "oa_" -and a -ends-with ".tar.gz" -and a -contains "$flt" -element a |
720 skip-if-file-exists |
721 while read fl
722 do
723 sleep 1
724 echo "$fl" >&2
725 DownloadOneByHTTPS "$dir" "$fl"
726 done
727 fi
728 }
729
730 if [ "$download" = true ]
731 then
732 seconds_start=$(date "+%s")
733 echo "Downloading New PMC Files" >&2
734
735 if [ -d "${sourceBase}" ]
736 then
737 cd "${sourceBase}"
738
739 for flt in baseline incr
740 do
741 for dir in oa_comm oa_noncomm oa_other
742 do
743 DownloadSection "$dir" "$flt"
744 if [ $? -ne 0 ]
745 then
746 DownloadSection "$dir" "$flt"
747 fi
748 done
749 done
750 fi
751
752 seconds_end=$(date "+%s")
753 seconds=$((seconds_end - seconds_start))
754 DWN=$seconds
755 echo "DWN $DWN seconds" >&2
756 echo "" >&2
757 sleep 1
758 fi
759
760 CheckSection() {
761
762 dir="$1"
763 flt="$2"
764
765 if [ "$useFtp" = true ]
766 then
767 nquire -lst "ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
768 grep ".tar.gz" | grep "$flt" |
769 skip-if-file-exists |
770 while read fl
771 do
772 echo "$fl" >&2
773 done
774 elif [ "$useHttps" = true ]
775 then
776 nquire -get "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
777 xtract -pattern a -if a -starts-with "oa_" -and a -ends-with ".tar.gz" -and a -contains "$flt" -element a |
778 skip-if-file-exists |
779 while read fl
780 do
781 echo "$fl" >&2
782 done
783 fi
784 }
785
786 if [ "$justmiss" = true ]
787 then
788 seconds_start=$(date "+%s")
789 echo "Looking for Missing PMC Files" >&2
790
791 if [ -d "${sourceBase}" ]
792 then
793 cd "${sourceBase}"
794
795 for flt in baseline incr
796 do
797 for dir in oa_comm oa_noncomm oa_other
798 do
799 CheckSection "$dir" "$flt"
800 done
801 done
802 fi
803
804 seconds_end=$(date "+%s")
805 seconds=$((seconds_end - seconds_start))
806 echo "$seconds seconds" >&2
807 echo "" >&2
808 exit 0
809 fi
810
811 if [ "$justtest" = true ]
812 then
813 seconds_start=$(date "+%s")
814 echo "Verifing PMC Archive" >&2
815
816 if [ -d "${sourceBase}" ]
817 then
818 cd "${sourceBase}"
819
820 for fl in *.tar.gz
821 do
822 printf "."
823 # verify contents
824 if [ -s "$fl" ]
825 then
826 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
827 if [ -n "$errs" ]
828 then
829 printf "\n"
830 echo "Invalid Contents '$fl'" >&2
831 fi
832 else
833 printf "\n"
834 echo "Empty file '$fl'" >&2
835 fi
836 done
837 printf "\n"
838 fi
839
840 seconds_end=$(date "+%s")
841 seconds=$((seconds_end - seconds_start))
842 echo "" >&2
843 echo "$seconds seconds" >&2
844 echo "" >&2
845 exit 0
846 fi
847
848 PMCStash() {
849
850 fl="$1"
851
852 base=${fl%.tar.gz}
853 echo "$base" >&2
854
855 tar -xOzf "$fl" --to-stdout |
856 pmc2info |
857 transmute -mixed -format |
858 rchive -gzip -db "$dbase" \
859 -archive "${archiveBase}" "${indexBase}" "${invertBase}" \
860 -index UID -pattern PMCInfo
861
862 touch "${archiveBase}/Sentinels/$base.snt"
863 }
864
865 if [ "$populate" = true ]
866 then
867 seconds_start=$(date "+%s")
868 echo "Populating PMC Archive" >&2
869
870 if [ -d "${sourceBase}" ]
871 then
872 cd "${sourceBase}"
873
874 for flt in baseline incr
875 do
876 for dir in oa_comm oa_noncomm oa_other
877 do
878 for fl in *.tar.gz
879 do
880 echo "$fl" | grep "$flt" | grep "$dir"
881 done |
882 while read fl
883 do
884 base=${fl%.tar.gz}
885 # skip if sentinel present or if file is present but empty
886 if [ ! -f "${archiveBase}/Sentinels/$base.snt" ] && [ -s "$fl" ]
887 then
888 PMCStash "$fl"
889 fi
890 done
891 done
892 done
893 fi
894
895 seconds_end=$(date "+%s")
896 seconds=$((seconds_end - seconds_start))
897 POP=$seconds
898 echo "POP $POP seconds" >&2
899 echo "" >&2
900 sleep 1
901 fi
902
903 # check archive, printing "OK" from "OK Adeyemo" if record is successfully retrieved
904 echo 4948736 |
905 xfetch -db pmc |
906 xtract -pattern AUTH -if LastName -equals Adeyemo \
907 -pfx "Archive is " -element Initials
908
909 echo "" >&2
910
911 # variable contains pmc-database-specific xtract indexing instructions
912 read -r -d '' idxtxt <<- EOS
913 xtract -set IdxDocumentSet -rec IdxDocument \
914 -pattern PMCInfo -UID PMCInfo/UID -LEN -len "*" \
915 -wrp IdxUid -element "&UID" -clr -rst -tab "" \
916 -group PMCInfo -pkg IdxSearchFields \
917 -block PMCInfo \
918 -wrp UID -pad "&UID" \
919 -wrp SIZE -inc "&LEN" \
920 -wrp YEAR -element YEAR \
921 -wrp JOUR -plain JOUR \
922 -wrp JOUR -plain SRC \
923 -subset AUTH \
924 -wrp AUTH -sep " " -author LastName,Initials \
925 -block PMCInfo \
926 -wrp TITL -indexer TITLE/TEXT \
927 -wrp ABST -indexer ABSTRACT/TEXT \
928 -wrp TEXT -indexer TEXT \
929 -wrp PAIR -pairx TITLE/TEXT
930 EOS
931
932 wait
933
934 if [ "$e2index" = true ]
935 then
936 seconds_start=$(date "+%s")
937 echo "Incremental Indexing" >&2
938
939 if [ "$stem" = true ]
940 then
941 idxtxt=$( echo "$idxtxt -block PMCInfo -wrp STEM -indexer TEXT" )
942 fi
943
944 temp=$(mktemp /tmp/INDEX_TEMP.XXXXXXXXX)
945 # generate file with xtract indexing arguments, split onto separate lines, skipping past xtract command itself
946 echo "${idxtxt}" | xargs -n1 echo | tail -n +2 > $temp
947 ( rchive -db "$dbase" -e2incIndex "${archiveBase}" "${indexBase}" -idxargs "$temp" -dotmax "$dotmaxIdx" -e2index )
948 rm "$temp"
949
950 seconds_end=$(date "+%s")
951 seconds=$((seconds_end - seconds_start))
952 IDX=$seconds
953 echo "IDX $IDX seconds" >&2
954 echo "" >&2
955 sleep 1
956 fi
957
958 wait
959
960 if [ "$e2invert" = true ]
961 then
962 seconds_start=$(date "+%s")
963 echo "Incremental Inversion" >&2
964
965 ( rchive -db "$dbase" -dotmax "$dotmaxInv" -e2incInvert "${indexBase}" "${invertBase}" )
966
967 if [ -d "${invertBase}" ]
968 then
969 cd "${invertBase}"
970
971 rm -f *.inv.gz
972 fi
973
974 seconds_end=$(date "+%s")
975 seconds=$((seconds_end - seconds_start))
976 INV=$seconds
977 echo "INV $INV seconds" >&2
978 echo "" >&2
979 sleep 1
980 fi
981
982 wait
983
984 if [ "$e2collect" = true ]
985 then
986 seconds_start=$(date "+%s")
987 echo "Collect Inverted Sets" >&2
988
989 if [ -d "${invertBase}" ]
990 then
991 cd "${invertBase}"
992
993 rm -f *.inv.gz
994
995 idx=0
996 for dir in "${invertBase}"/*
997 do
998 if [ -d "$dir" ]
999 then
1000 cd "$dir"
1001 printf "."
1002 ( rchive -gzip -join *.inv.gz > "${invertBase}/${dbase}$(printf %02d $idx).inv.gz" )
1003 idx=$(( idx + 1 ))
1004 wait
1005 fi
1006 done
1007 printf "\n"
1008 fi
1009
1010 seconds_end=$(date "+%s")
1011 seconds=$((seconds_end - seconds_start))
1012 COL=$seconds
1013 echo "COL $COL seconds" >&2
1014 echo "" >&2
1015 sleep 1
1016 fi
1017
1018 wait
1019
1020 if [ "$e2merge" = true ]
1021 then
1022 seconds_start=$(date "+%s")
1023 echo "Merging Inverted Indices" >&2
1024
1025 if [ -d "${invertBase}" ]
1026 then
1027 cd "${invertBase}"
1028
1029 ( rchive -gzip -db "$dbase" -merge "${mergedBase}" *.inv.gz )
1030 fi
1031
1032 seconds_end=$(date "+%s")
1033 seconds=$((seconds_end - seconds_start))
1034 MRG=$seconds
1035 echo "MRG $MRG seconds" >&2
1036 echo "" >&2
1037 sleep 1
1038 if [ ! -f "${mergedBase}/zz.mrg.gz" ]
1039 then
1040 echo "ERROR: Merge failed to complete - missing zz.mrg.gz file" >&2
1041 echo "" >&2
1042 echo "EXITING DUE TO BUILD FAILURE" >&2
1043 echo "" >&2
1044 # do not continue
1045 e2post=false
1046 fi
1047 fi
1048
1049 wait
1050
1051 if [ "$e2post" = true ]
1052 then
1053 seconds_start=$(date "+%s")
1054 echo "Producing Postings Files" >&2
1055
1056 if [ -d "${mergedBase}" ]
1057 then
1058 cd "${mergedBase}"
1059
1060 for fl in *.mrg.gz
1061 do
1062 echo "$fl"
1063 done |
1064 sort |
1065 xargs -n 100 echo |
1066 while read files
1067 do
1068 ( rchive -db "$dbase" -promote "${postingsBase}" "$fields" $files )
1069 done
1070 fi
1071
1072 seconds_end=$(date "+%s")
1073 seconds=$((seconds_end - seconds_start))
1074 PST=$seconds
1075 echo "PST $PST seconds" >&2
1076 echo "" >&2
1077 sleep 1
1078 fi
1079
1080 wait
1081
1082 # check postings
1083 okay=""
1084 if [ "$e2post" = true ]
1085 then
1086 okay=$( xsearch -db "$dbase" -query "occupational hazards among the abattoir workers [TITL] AND 2016 [YEAR]" |
1087 xfetch -db pmc |
1088 xtract -pattern AUTH -if LastName -equals Adeyemo -element Initials )
1089 if [ "$okay" = "OK" ]
1090 then
1091 echo "Archive and Index are $okay" >&2
1092 echo "" >&2
1093 fi
1094 fi
1095
1096 if [ "$e2post" = true ] && [ "$okay" = "OK" ] && [ -d "${mergedBase}" ]
1097 then
1098 target="${mergedBase}"
1099 find "$target" -name "*.mrg" -delete
1100 find "$target" -name "*.mrg.gz" -delete
1101
1102 if [ -d "${invertBase}" ]
1103 then
1104 cd "${invertBase}"
1105 rm -f *.inv.gz
1106 fi
1107 fi
1108
1109 cd
1110
1111 echo "ARCHIVE-PMC" >&2
1112
1113 echo "" >&2
1114
1115 PrintTime() {
1116
1117 if [ "$1" = true ]
1118 then
1119 echo "$2 $3 seconds" >&2
1120 fi
1121 }
1122
1123 PrintTime "$datafiles" "DAT" "$DAT"
1124 PrintTime "$download" "DWN" "$DWN"
1125 PrintTime "$populate" "POP" "$POP"
1126
1127 PrintTime "$e2index" "IDX" "$IDX"
1128 PrintTime "$e2invert" "INV" "$INV"
1129 PrintTime "$e2collect" "COL" "$COL"
1130 PrintTime "$e2merge" "MRG" "$MRG"
1131 PrintTime "$e2post" "PST" "$PST"
1132
1133 echo "" >&2
1134
1135 function PrintTotalElapsedTime {
1136 local L=$1
1137 local T=$2
1138 local D=$((T/60/60/24))
1139 local H=$((T/60/60%24))
1140 local M=$((T/60%60))
1141 local S=$((T%60))
1142 printf '%s %d second' "$L" $T 1>&2
1143 (( $T > 1 )) && printf 's' 1>&2
1144 if [ "$T" -gt 59 ]
1145 then
1146 printf ', or' 1>&2
1147 (( $D > 0 )) && printf ' %d day' $D 1>&2
1148 (( $D > 1 )) && printf 's' 1>&2
1149 (( $H > 0 )) && printf ' %d hour' $H 1>&2
1150 (( $H > 1 )) && printf 's' 1>&2
1151 (( $M > 0 )) && printf ' %d minute' $M 1>&2
1152 (( $M > 1 )) && printf 's' 1>&2
1153 (( $S > 0 )) && printf ' %d second' $S 1>&2
1154 (( $S > 1 )) && printf 's' 1>&2
1155 fi
1156 printf '\n' 1>&2
1157 }
1158
1159 total_end=$(date "+%s")
1160 total=$((total_end - total_start))
1161 TOT=$total
1162 PrintTotalElapsedTime "TOT" "$TOT"
1163 echo "" >&2