Debian Codegraph @

archive-pids

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-pids
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="PMCID"
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 IDX=""
155 INV=""
156 MRG=""
157 PST=""
158
159 if [ -d "${scratchBase}" ]
160 then
161 echo "Removing Previous Indices" >&2
162
163 target="${currentBase}"
164 find "$target" -name "*.xml" -delete
165 find "$target" -name "*.xml.gz" -delete
166
167 target="${indexedBase}"
168 find "$target" -name "*.e2x" -delete
169 find "$target" -name "*.e2x.gz" -delete
170
171 target="${invertedBase}"
172 find "$target" -name "*.inv" -delete
173 find "$target" -name "*.inv.gz" -delete
174 fi
175
176 if [ -d "${mergedBase}" ]
177 then
178 target="${mergedBase}"
179 find "$target" -name "*.mrg" -delete
180 find "$target" -name "*.mrg.gz" -delete
181 fi
182
183 if [ "$e2index" = true ]
184 then
185 seconds_start=$(date "+%s")
186 echo "Indexing PMID-PMCID" >&2
187
188 cd "${indexedBase}"
189
190 target="${indexedBase}"
191
192 for dir in "/Volumes/working/pubmed/Invert"/*
193 do
194 if [ -d "$dir" ]
195 then
196 gunzip -c "$dir"/*.inv.gz |
197 xtract -pattern InvDocument -KEY InvKey \
198 -block InvIDs/PMCID \
199 -tab "," -element PMCID "&KEY" -deq "\n"
200 fi
201 done |
202 go run "$pth/extern/prep-pmcid.go" |
203 go run "$pth/extern/prep-finish.go" 500000 "$target" "pmcid"
204
205 seconds_end=$(date "+%s")
206 seconds=$((seconds_end - seconds_start))
207 IDX=$seconds
208 echo "IDX $IDX seconds" >&2
209 echo "" >&2
210 sleep 1
211 fi
212
213 if [ "$e2invert" = true ]
214 then
215 seconds_start=$(date "+%s")
216 echo "Inverting PMID-PMCID" >&2
217
218 if [ -d "${indexedBase}" ]
219 then
220 cd "${indexedBase}"
221
222 target="${invertedBase}"
223 for fl in *.e2x.gz
224 do
225 base=${fl%.e2x.gz}
226 echo "$base.inv"
227 gunzip -c "$fl" |
228 rchive -e2invert |
229 gzip -1 > "$target/$base.inv.gz"
230 sleep 1
231 done
232 fi
233
234 seconds_end=$(date "+%s")
235 seconds=$((seconds_end - seconds_start))
236 INV=$seconds
237 echo "INV $INV seconds" >&2
238 echo "" >&2
239 sleep 1
240 fi
241
242 if [ "$e2merge" = true ]
243 then
244 seconds_start=$(date "+%s")
245 echo "Merging Inverted Indices" >&2
246
247 if [ -d "${invertedBase}" ]
248 then
249 cd "${invertedBase}"
250
251 target="${mergedBase}"
252 osname=`uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/'`
253 if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
254 then
255 target=`cygpath -w "$target"`
256 fi
257 target=${target%/}
258 rchive -gzip -mergelink "$target" *.inv.gz
259 fi
260
261 seconds_end=$(date "+%s")
262 seconds=$((seconds_end - seconds_start))
263 MRG=$seconds
264 echo "MRG $MRG seconds" >&2
265 echo "" >&2
266 sleep 1
267 fi
268
269 if [ "$e2post" = true ]
270 then
271 seconds_start=$(date "+%s")
272 echo "Producing Postings Files" >&2
273
274 if [ -d "${mergedBase}" ]
275 then
276 cd "${mergedBase}"
277
278 target="${postingsBase}"
279 osname=`uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/'`
280 if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
281 then
282 target=`cygpath -w "$target"`
283 fi
284 target=${target%/}
285 for fl in *.mrg.gz
286 do
287 echo "$fl"
288 done |
289 sort |
290 xargs -n 100 echo |
291 while read files
292 do
293 rchive -db pubmed -promotelink "$target" "$fields" $files
294 done
295 fi
296
297 seconds_end=$(date "+%s")
298 seconds=$((seconds_end - seconds_start))
299 PST=$seconds
300 echo "PST $PST seconds" >&2
301 echo "" >&2
302 sleep 1
303 fi
304
305 wait
306
307 # check postings
308 okay=""
309 if [ "$e2post" = true ]
310 then
311 okay=$( echo 2539356 | xlink -db pubmed -target PMCID | grep -w 209839 )
312 if [ -n "$okay" ]
313 then
314 echo "Archive and Index are OK" >&2
315 echo "" >&2
316 fi
317 fi
318
319 if [ "$e2post" = true ] && [ -n "$okay" ] && [ -d "${mergedBase}" ]
320 then
321 target="${mergedBase}"
322 find "$target" -name "*.mrg" -delete
323 find "$target" -name "*.mrg.gz" -delete
324 fi
325
326 cd
327
328 echo "ARCHIVE-PIDS " >&2
329
330 echo "" >&2
331
332 PrintTime() {
333
334 if [ "$1" = true ]
335 then
336 echo "$2 $3 seconds" >&2
337 fi
338 }
339
340 PrintTime "$download" "DWN" "$DWN"
341
342 PrintTime "$e2index" "IDX" "$IDX"
343 PrintTime "$e2invert" "INV" "$INV"
344 PrintTime "$e2merge" "MRG" "$MRG"
345 PrintTime "$e2post" "PST" "$PST"
346
347 echo "" >&2
348
349 function PrintTotalElapsedTime {
350 local L=$1
351 local T=$2
352 local D=$((T/60/60/24))
353 local H=$((T/60/60%24))
354 local M=$((T/60%60))
355 local S=$((T%60))
356 printf '%s %d second' "$L" $T 1>&2
357 (( $T > 1 )) && printf 's' 1>&2
358 if [ "$T" -gt 59 ]
359 then
360 printf ', or' 1>&2
361 (( $D > 0 )) && printf ' %d day' $D 1>&2
362 (( $D > 1 )) && printf 's' 1>&2
363 (( $H > 0 )) && printf ' %d hour' $H 1>&2
364 (( $H > 1 )) && printf 's' 1>&2
365 (( $M > 0 )) && printf ' %d minute' $M 1>&2
366 (( $M > 1 )) && printf 's' 1>&2
367 (( $S > 0 )) && printf ' %d second' $S 1>&2
368 (( $S > 1 )) && printf 's' 1>&2
369 fi
370 printf '\n' 1>&2
371 }
372
373 total_end=$(date "+%s")
374 total=$((total_end - total_start))
375 TOT=$total
376 PrintTotalElapsedTime "TOT" "$TOT"
377 echo "" >&2