Debian Codegraph @

archive-nihocc

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-nihocc
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 hasgo=$( command -v go )
30 if [ ! -x "$hasgo" ]
31 then
32 echo "ERROR: The Go (golang) compiler must be installed locally in order to process the data, EXITING" >&2
33 exit 1
34 fi
35
36 # database-specific parameters
37
38 dbase="pubmed"
39 fields="CITED CITES"
40
41 # control flags set by command-line arguments
42
43 useFtp=true
44 useHttps=false
45
46 scratch=false
47
48 download=true
49
50 e2index=false
51 e2invert=false
52 e2merge=false
53 e2post=false
54
55 while [ $# -gt 0 ]
56 do
57 case "$1" in
58 daily | -daily )
59 e2index=true
60 e2invert=true
61 shift
62 ;;
63 index | -index | reindex | -reindex )
64 e2index=true
65 e2invert=true
66 e2merge=true
67 e2post=true
68 shift
69 ;;
70 clean | -clean | clear | -clear | scrub | -scrub | scour | -scour | scratch | -scratch | erase | -erase )
71 # delete Scratch directories
72 scratch=true
73 shift
74 ;;
75 -ftp )
76 useFtp=true
77 useHttps=false
78 export EDIRECT_NO_ASPERA=true
79 shift
80 ;;
81 -http | -https )
82 useFtp=false
83 useHttps=true
84 shift
85 ;;
86 * )
87 break
88 ;;
89 esac
90 done
91
92 if [ "$scratch" == true ] && [ "$e2index" == true ]
93 then
94 echo "ERROR: Cleaning and indexing must be done in separate commands, EXITING" >&2
95 exit 1
96 fi
97
98 if [ "$scratch" = true ]
99 then
100 pm-clean -db "$dbase" -fields "$fields" -scratch
101 exit 0
102 fi
103
104 # get path to local folder
105
106 osname=$( uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/' )
107
108 GetLocalArchiveFolder() {
109
110 dbs="$1"
111 fld="$2"
112
113 # find selected local archive folder from environment variables or configuration file
114 target=$( rchive -local "$dbs" "$fld" )
115
116 if [ -z "$target" ] || [ "$target" = "" ]
117 then
118 echo "ERROR: Must supply path to local data by setting EDIRECT_LOCAL_ARCHIVE environment variable" >&2
119 exit 1
120 fi
121
122 if [ -n "$osname" ] && [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
123 then
124 target=$( cygpath -w "$target" )
125 fi
126
127 # remove trailing slash
128 target=${target%/}
129
130 echo "$target"
131 }
132
133 date
134 echo "" >&2
135
136 pm-setup -db "$dbase"
137
138 echo "Preparing Drives" >&2
139 pm-prepare -db "$dbase"
140 echo "" >&2
141
142 archiveBase=$( GetLocalArchiveFolder "$dbase" "Archive" )
143 dataBase=$( GetLocalArchiveFolder "$dbase" "Data" )
144 extrasBase=$( GetLocalArchiveFolder "$dbase" "Extras" )
145 indexBase=$( GetLocalArchiveFolder "$dbase" "Index" )
146 invertBase=$( GetLocalArchiveFolder "$dbase" "Invert" )
147 mergedBase=$( GetLocalArchiveFolder "$dbase" "Merged" )
148 postingsBase=$( GetLocalArchiveFolder "$dbase" "Postings" )
149 scratchBase=$( GetLocalArchiveFolder "$dbase" "Scratch" )
150 currentBase=$( GetLocalArchiveFolder "$dbase" "Current" )
151 indexedBase=$( GetLocalArchiveFolder "$dbase" "Indexed" )
152 invertedBase=$( GetLocalArchiveFolder "$dbase" "Inverted" )
153
154 DWN=""
155
156 IDX=""
157 INV=""
158 MRG=""
159 PST=""
160
161 DoNIHOCC() {
162
163 base_url=""
164 new_file_date=""
165
166 latest_occ=$(
167 nquire -bulk -get https://api.figshare.com/v2/collections/4586573/articles |
168 xtract -pattern anon -sort-rev published_date |
169 xtract -pattern anon -position first -element "*"
170 )
171 if [ -n "$latest_occ" ]
172 then
173 base_url=$( echo "$latest_occ" | xtract -pattern anon -element url )
174 new_file_date=$( echo "$latest_occ" | xtract -pattern anon -element published_date | cut -c 1-10 )
175 fi
176
177 if [ -f "open_citation_collection.zip" ] && [ -n "$new_file_date" ]
178 then
179 curr_file_date=$( date -r open_citation_collection.zip "+%Y-%m-%d" )
180 if [ -n "$curr_file_date" ]
181 then
182 if [[ "$new_file_date" > "$curr_file_date" ]]
183 then
184 echo "Removing old $curr_file_date download of open_citation_collection.zip" >&2
185 rm -f "open_citation_collection.zip"
186 else
187 echo "Current public $new_file_date version of open_citation_collection.zip is not later than existing $curr_file_date download" >&2
188 fi
189 fi
190 fi
191
192 if [ ! -f "open_citation_collection.zip" ] && [ -n "$base_url" ]
193 then
194 download_url=$(
195 nquire -get "$base_url" |
196 xtract -pattern opt -group files \
197 -if name -equals open_citation_collection.zip \
198 -element download_url
199 )
200 if [ -n "$download_url" ]
201 then
202 orig_name=$( echo "${download_url}" | tr '/' '\n' | tail -n 1 )
203 echo "Downloading new $new_file_date version of open_citation_collection.zip will likely take at least two to three hours" >&2
204 nquire -get "$download_url" > open_citation_collection.zip
205 if [ -f "open_citation_collection.zip" ]
206 then
207 echo "Downloading open_citation_collection.zip is complete" >&2
208 else
209 echo "Downloading of open_citation_collection.zip failed" >&2
210 fi
211 fi
212 fi
213 }
214
215 if [ "$download" = true ]
216 then
217 seconds_start=$(date "+%s")
218 echo "Downloading NIH Open Citation Collection" >&2
219
220 if [ -d "${extrasBase}" ]
221 then
222 cd "${extrasBase}"
223
224 DoNIHOCC
225
226 sleep 1
227 if [ ! -f "open_citation_collection.zip" ]
228 then
229 echo "ERROR: Unable to download open_citation_collection.zip file to Extras directory" >&2
230 echo "" >&2
231 echo "EXITING DUE TO MISSING NCBI OCC DATA FILE" >&2
232 exit 1
233 fi
234 fi
235
236 seconds_end=$(date "+%s")
237 seconds=$((seconds_end - seconds_start))
238 DWN=$seconds
239 echo "DWN $DWN seconds" >&2
240 echo "" >&2
241 sleep 1
242 fi
243
244 if [ -d "${scratchBase}" ]
245 then
246 echo "Removing Previous Indices" >&2
247
248 target="${currentBase}"
249 find "$target" -name "*.xml" -delete
250 find "$target" -name "*.xml.gz" -delete
251
252 target="${indexedBase}"
253 find "$target" -name "*.e2x" -delete
254 find "$target" -name "*.e2x.gz" -delete
255
256 target="${invertedBase}"
257 find "$target" -name "*.inv" -delete
258 find "$target" -name "*.inv.gz" -delete
259 fi
260
261 if [ -d "${mergedBase}" ]
262 then
263 target="${mergedBase}"
264 find "$target" -name "*.mrg" -delete
265 find "$target" -name "*.mrg.gz" -delete
266 fi
267
268 if [ "$e2index" = true ]
269 then
270 seconds_start=$(date "+%s")
271 echo "Indexing NIH Open Citation Collection" >&2
272
273 cd "${indexedBase}"
274
275 target="${indexedBase}"
276 # obtain maximum live PMID value by Entrez query
277 sleep 2
278 max_pmid=$(
279 ecollect -db pubmed -subset "all [SB] NOT pubmed books [SB]" -retmax 1000 |
280 xtract -pattern eSearchResult -max Id
281 )
282 if [ -z "$max_pmid" ]
283 then
284 sleep 10
285 max_pmid=$(
286 ecollect -db pubmed -subset "all [SB] NOT pubmed books [SB]" -retmax 1000 |
287 xtract -pattern eSearchResult -max Id
288 )
289 fi
290
291 if [ -z "$max_pmid" ]
292 then
293 max_pmid="0"
294 fi
295
296 unzip -cq "${extrasBase}/open_citation_collection.zip" |
297 go run "$pth/extern/prep-nihocc.go" "$max_pmid" |
298 go run "$pth/extern/prep-finish.go" 20000000 "$target" "nihocc"
299
300 seconds_end=$(date "+%s")
301 seconds=$((seconds_end - seconds_start))
302 IDX=$seconds
303 echo "IDX $IDX seconds" >&2
304 echo "" >&2
305 sleep 1
306 fi
307
308 if [ "$e2invert" = true ]
309 then
310 seconds_start=$(date "+%s")
311 echo "Inverting NIH Open Citation Collection" >&2
312
313 if [ -d "${indexedBase}" ]
314 then
315 cd "${indexedBase}"
316
317 target="${invertedBase}"
318 for fl in *.e2x.gz
319 do
320 base=${fl%.e2x.gz}
321 echo "$base.inv"
322 gunzip -c "$fl" |
323 rchive -e2invert |
324 gzip -1 > "$target/$base.inv.gz"
325 sleep 1
326 done
327 fi
328
329 seconds_end=$(date "+%s")
330 seconds=$((seconds_end - seconds_start))
331 INV=$seconds
332 echo "INV $INV seconds" >&2
333 echo "" >&2
334 sleep 1
335 fi
336
337 if [ "$e2merge" = true ]
338 then
339 seconds_start=$(date "+%s")
340 echo "Merging Inverted Indices" >&2
341
342 if [ -d "${invertedBase}" ]
343 then
344 cd "${invertedBase}"
345
346 target="${mergedBase}"
347 osname=`uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/'`
348 if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
349 then
350 target=`cygpath -w "$target"`
351 fi
352 target=${target%/}
353 rchive -gzip -mergelink "$target" *.inv.gz
354 fi
355
356 seconds_end=$(date "+%s")
357 seconds=$((seconds_end - seconds_start))
358 MRG=$seconds
359 echo "MRG $MRG seconds" >&2
360 echo "" >&2
361 sleep 1
362 fi
363
364 if [ "$e2post" = true ]
365 then
366 seconds_start=$(date "+%s")
367 echo "Producing Postings Files" >&2
368
369 if [ -d "${mergedBase}" ]
370 then
371 cd "${mergedBase}"
372
373 target="${postingsBase}"
374 osname=`uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/'`
375 if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
376 then
377 target=`cygpath -w "$target"`
378 fi
379 target=${target%/}
380 for fl in *.mrg.gz
381 do
382 echo "$fl"
383 done |
384 sort |
385 xargs -n 100 echo |
386 while read files
387 do
388 rchive -db pubmed -promotelink "$target" "$fields" $files
389 done
390 fi
391
392 seconds_end=$(date "+%s")
393 seconds=$((seconds_end - seconds_start))
394 PST=$seconds
395 echo "PST $PST seconds" >&2
396 echo "" >&2
397 sleep 1
398 fi
399
400 wait
401
402 # check postings
403 okay=""
404 if [ "$e2post" = true ]
405 then
406 okay=$( echo 2539356 | xlink -db pubmed -target CITED | grep -w 1324175 )
407 if [ -n "$okay" ]
408 then
409 echo "Archive and Index are OK" >&2
410 echo "" >&2
411 fi
412 fi
413
414 if [ "$e2post" = true ] && [ -n "$okay" ] && [ -d "${mergedBase}" ]
415 then
416 target="${mergedBase}"
417 find "$target" -name "*.mrg" -delete
418 find "$target" -name "*.mrg.gz" -delete
419 fi
420
421 cd
422
423 echo "ARCHIVE-NIHOCC" >&2
424
425 echo "" >&2
426
427 PrintTime() {
428
429 if [ "$1" = true ]
430 then
431 echo "$2 $3 seconds" >&2
432 fi
433 }
434
435 PrintTime "$download" "DWN" "$DWN"
436
437 PrintTime "$e2index" "IDX" "$IDX"
438 PrintTime "$e2invert" "INV" "$INV"
439 PrintTime "$e2merge" "MRG" "$MRG"
440 PrintTime "$e2post" "PST" "$PST"
441
442 echo "" >&2
443
444 function PrintTotalElapsedTime {
445 local L=$1
446 local T=$2
447 local D=$((T/60/60/24))
448 local H=$((T/60/60%24))
449 local M=$((T/60%60))
450 local S=$((T%60))
451 printf '%s %d second' "$L" $T 1>&2
452 (( $T > 1 )) && printf 's' 1>&2
453 if [ "$T" -gt 59 ]
454 then
455 printf ', or' 1>&2
456 (( $D > 0 )) && printf ' %d day' $D 1>&2
457 (( $D > 1 )) && printf 's' 1>&2
458 (( $H > 0 )) && printf ' %d hour' $H 1>&2
459 (( $H > 1 )) && printf 's' 1>&2
460 (( $M > 0 )) && printf ' %d minute' $M 1>&2
461 (( $M > 1 )) && printf 's' 1>&2
462 (( $S > 0 )) && printf ' %d second' $S 1>&2
463 (( $S > 1 )) && printf 's' 1>&2
464 fi
465 printf '\n' 1>&2
466 }
467
468 total_end=$(date "+%s")
469 total=$((total_end - total_start))
470 TOT=$total
471 PrintTotalElapsedTime "TOT" "$TOT"
472 echo "" >&2