Debian Codegraph @

archive-nmcds

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-nmcds
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="nmcds"
32 recname="NMSummary"
33 dotmaxIdx="500"
34 dotmaxInv="10"
35 fields="ACCN ACVR OFST ORGN TXID CHR MAP GENE GUID PROD LOC GCODE PACC PAVR PLEN CLEN MLEN NIVL UID"
36
37 # control flags set by command-line arguments
38
39 useFtp=true
40 useHttps=false
41 noAspera=false
42
43 clean=false
44 scrub=false
45 scour=false
46 erase=false
47 zap=false
48
49 download=true
50 generate=true
51 populate=true
52
53 e2index=false
54 e2invert=false
55 e2merge=false
56 e2post=false
57
58 while [ $# -gt 0 ]
59 do
60 case "$1" in
61 index | -index | reindex | -reindex )
62 e2index=true
63 e2invert=true
64 e2merge=true
65 e2post=true
66 shift
67 ;;
68 clean | -clean | clear | -clear )
69 # delete Indices contents and Increment files
70 clean=true
71 shift
72 ;;
73 scrub | -scrub )
74 clean=true
75 # and delete Postings directories
76 scrub=true
77 shift
78 ;;
79 scour | -scour )
80 clean=true
81 scrub=true
82 # and delete Data, Archive, and Sentinels directories
83 scour=true
84 shift
85 ;;
86 erase | -erase )
87 clean=true
88 scrub=true
89 scour=true
90 # and delete Extras directory contents
91 erase=true
92 shift
93 ;;
94 zap | -zap )
95 clean=true
96 scrub=true
97 scour=true
98 erase=true
99 # and delete Source records and all remaining directories
100 zap=true
101 shift
102 ;;
103 -ftp )
104 useFtp=true
105 useHttps=false
106 noAspera=true
107 export EDIRECT_NO_ASPERA=true
108 shift
109 ;;
110 -http | -https )
111 useFtp=false
112 useHttps=true
113 shift
114 ;;
115 * )
116 break
117 ;;
118 esac
119 done
120
121 if [ "$clean" == true ] && [ "$e2index" == true ]
122 then
123 echo "ERROR: Cleaning and indexing must be done in separate commands, EXITING" >&2
124 exit 1
125 fi
126
127 if [ "$zap" = true ]
128 then
129 pm-clean -db "$dbase" -zap
130 exit 0
131 fi
132
133 if [ "$erase" = true ]
134 then
135 pm-clean -db "$dbase" -erase
136 exit 0
137 fi
138
139 if [ "$scour" = true ]
140 then
141 pm-clean -db "$dbase" -scour
142 exit 0
143 fi
144
145 if [ "$scrub" = true ]
146 then
147 pm-clean -db "$dbase" -scrub
148 exit 0
149 fi
150
151 if [ "$clean" = true ]
152 then
153 pm-clean -db "$dbase" -clean
154 exit 0
155 fi
156
157 # get path to local folder
158
159 osname=$( uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/' )
160
161 GetLocalArchiveFolder() {
162
163 dbs="$1"
164 fld="$2"
165
166 # find selected local archive folder from environment variables or configuration file
167 target=$( rchive -local "$dbs" "$fld" )
168
169 if [ -z "$target" ] || [ "$target" = "" ]
170 then
171 echo "ERROR: Must supply path to local data by setting EDIRECT_LOCAL_ARCHIVE environment variable" >&2
172 exit 1
173 fi
174
175 if [ -n "$osname" ] && [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
176 then
177 target=$( cygpath -w "$target" )
178 fi
179
180 # remove trailing slash
181 target=${target%/}
182
183 echo "$target"
184 }
185
186 date
187 echo "" >&2
188
189 pm-setup -db "$dbase"
190
191 echo "Preparing Drives" >&2
192 pm-prepare -db "$dbase"
193 echo "" >&2
194
195 archiveBase=$( GetLocalArchiveFolder "$dbase" "Archive" )
196 dataBase=$( GetLocalArchiveFolder "$dbase" "Data" )
197 extrasBase=$( GetLocalArchiveFolder "$dbase" "Extras" )
198 indexBase=$( GetLocalArchiveFolder "$dbase" "Index" )
199 invertBase=$( GetLocalArchiveFolder "$dbase" "Invert" )
200 mergedBase=$( GetLocalArchiveFolder "$dbase" "Merged" )
201 postingsBase=$( GetLocalArchiveFolder "$dbase" "Postings" )
202 sourceBase=$( GetLocalArchiveFolder "$dbase" "Source" )
203
204 DWN=""
205 POP=""
206
207 IDX=""
208 INV=""
209 MRG=""
210 PST=""
211
212 if [ "$download" = true ]
213 then
214 seconds_start=$(date "+%s")
215 echo "Downloading RefSeq mRNA Files" >&2
216
217 if [ -d "${sourceBase}" ]
218 then
219 cd "${sourceBase}"
220
221 species="H_sapiens"
222
223 nquire -lst ftp.ncbi.nlm.nih.gov refseq "$species" mRNA_Prot |
224 grep rna.gbff.gz | sort -V |
225 skip-if-file-exists |
226 while read fl
227 do
228 echo "$fl" >&2
229 if [ "$noAspera" = true ]
230 then
231 echo "$fl" | nquire -dwn ftp.ncbi.nlm.nih.gov refseq "$species" mRNA_Prot
232 else
233 echo "$fl" | nquire -asp ftp.ncbi.nlm.nih.gov refseq "$species" mRNA_Prot
234 fi
235 done
236 fi
237
238 seconds_end=$(date "+%s")
239 seconds=$((seconds_end - seconds_start))
240 DWN=$seconds
241 echo "DWN $DWN seconds" >&2
242 echo "" >&2
243 sleep 1
244 fi
245
246 GBFtoNMSummary() {
247
248 label="$1"
249
250 ls "${label}"*.rna.gbff.gz | sort -V |
251 while read fl
252 do
253 echo "$fl" >&2
254 gunzip -c "$fl" |
255 gbf2xml |
256 xtract -rec Rec -pattern INSDSeq \
257 -ACCN INSDSeq_accession-version -LCUS INSDSeq_locus -SEQ INSDSeq_sequence \
258 -division INSDSeq -if "&ACCN" -starts-with "NM_" \
259 -group INSDFeature -if INSDFeature_key -equals source \
260 -branch INSDFeature -pkg SRC \
261 -block INSDQualifier -if INSDQualifier_name -equals organism -wrp Organism -element INSDQualifier_value \
262 -block INSDQualifier -if INSDQualifier_name -equals db_xref -and INSDQualifier_value -starts-with "taxon:" \
263 -wrp TaxonID -element "INSDQualifier_value[taxon:|]" \
264 -block INSDQualifier -if INSDQualifier_name -equals chromosome -wrp Chromosome -element INSDQualifier_value \
265 -block INSDQualifier -if INSDQualifier_name -equals map -wrp Map -element INSDQualifier_value \
266 -group INSDFeature -if INSDFeature_key -equals CDS \
267 -wrp Accession -first "&ACCN,&LCUS" -wrp Offset -min INSDInterval_from -wrp NumIvals -num INSDInterval \
268 -branch INSDFeature -pkg TMP \
269 -block INSDQualifier -if INSDQualifier_name -equals gene -wrp Gene -element INSDQualifier_value \
270 -block INSDQualifier -if INSDQualifier_name -equals db_xref -and INSDQualifier_value -starts-with "GeneID:" \
271 -wrp GeneID -element "INSDQualifier_value[GeneID:|]" \
272 -block INSDQualifier -if INSDQualifier_name -equals product -wrp Product -element INSDQualifier_value \
273 -block INSDFeature_intervals -pkg Location \
274 -subset INSDInterval -FR INSDInterval_from -TO INSDInterval_to \
275 -pfx "" -tab ".." -element "&FR" -pfx "" -tab "," -element "&TO" \
276 -subset INSDFeature_intervals -deq "\t" \
277 -block INSDQualifier -if INSDQualifier_name -equals ribosomal_slippage -wrp Exception -element INSDQualifier_name \
278 -block INSDQualifier -if INSDQualifier_name -equals transl_table -wrp GeneticCode -element INSDQualifier_value \
279 -block INSDFeature -unless INSDQualifier_name -equals transl_table -wrp GeneticCode -lbl "1" \
280 -block INSDQualifier -if INSDQualifier_name -equals protein_id -wrp ProtID -element INSDQualifier_value \
281 -block INSDQualifier -if INSDQualifier_name -equals translation -wrp Protein -element INSDQualifier_value \
282 -block INSDFeature_intervals -pkg Coding \
283 -subset INSDInterval -FR INSDInterval_from -TO INSDInterval_to -clr -nucleic "&SEQ[&FR:&TO]" \
284 -block INSDFeature -wrp Message -upper "&SEQ" |
285 xtract -rec NMSummary -pattern Rec \
286 -block Accession -element "*" -block Offset -wrp Offset -dec Offset \
287 -block "SRC/*" -element "*" -block "TMP/*" -element "*" \
288 -block TMP/Protein -wrp ProtLength -len Protein -block TMP/Coding -wrp CDSLength -len Coding \
289 -block TMP/Message -wrp MRnaLength -len Message -block NumIvals -wrp NumIntervals -element NumIvals
290 done |
291 transmute -format > "${extrasBase}/${label}_cds.xml"
292 }
293
294 if [ "$generate" = true ]
295 then
296 seconds_start=$(date "+%s")
297 echo "Clearing RefSeq mRNA Archive and Indices" >&2
298
299 if [ -d "${extrasBase}" ]
300 then
301 rm -f "${extrasBase}/human_cds.xml"
302 rm -f "${extrasBase}/nm_cds.txt"
303 rm -f "${extrasBase}/accessions.txt"
304 fi
305
306 if [ -d "${indexBase}" ]
307 then
308 target="${indexBase}"
309 find "$target" -name "*.e2x" -delete
310 find "$target" -name "*.e2x.gz" -delete
311 cd "${indexBase}"
312 rm -rf * &
313 wait
314 fi
315
316 if [ -d "${invertBase}" ]
317 then
318 target="${invertBase}"
319 find "$target" -name "*.inv" -delete
320 find "$target" -name "*.inv.gz" -delete
321 fi
322
323 if [ -d "${mergedBase}" ]
324 then
325 target="${mergedBase}"
326 find "$target" -name "*.mrg" -delete
327 find "$target" -name "*.mrg.gz" -delete
328 fi
329
330 sleep 1
331
332 echo "" >&2
333 echo "Creating RefSeq mRNA Records" >&2
334
335 if [ -d "${sourceBase}" ]
336 then
337 cd "${sourceBase}"
338
339 GBFtoNMSummary "human"
340 fi
341
342 echo "Creating Master Accession List" >&2
343 if [ -d "${extrasBase}" ]
344 then
345 cd "${extrasBase}"
346
347 # use line number for UID
348 for fl in *_cds.xml
349 do
350 echo "$fl" >&2
351 cat "$fl" |
352 xtract -pattern NMSummary -element Accession
353 done |
354 sort -V | uniq -i | print-columns '$1, NR' > accessions.txt
355 fi
356
357 echo "Creating CDS Offset Table" >&2
358 if [ -d "${extrasBase}" ]
359 then
360 cd "${extrasBase}"
361
362 for fl in *_cds.xml
363 do
364 echo "$fl" >&2
365 cat "$fl" |
366 xtract -pattern NMSummary -def "-" -element Accession Offset Coding
367 done |
368 sort -V | uniq -i > nm_cds.txt
369 fi
370
371 seconds_end=$(date "+%s")
372 seconds=$((seconds_end - seconds_start))
373 GEN=$seconds
374 echo "GEN $GEN seconds" >&2
375 echo "" >&2
376 sleep 1
377 fi
378
379 if [ "$populate" = true ]
380 then
381 seconds_start=$(date "+%s")
382 echo "Populating RefSeq mRNA Archive" >&2
383
384 if [ -d "${extrasBase}" ]
385 then
386 cd "${extrasBase}"
387
388 cat *_cds.xml |
389 transmute -format |
390 rchive -gzip -db "$dbase" -transform "${extrasBase}/accessions.txt" \
391 -archive "${archiveBase}" "${indexBase}" "${invertBase}" \
392 -index Accession -pattern NMSummary
393 fi
394
395 seconds_end=$(date "+%s")
396 seconds=$((seconds_end - seconds_start))
397 POP=$seconds
398 echo "POP $POP seconds" >&2
399 echo "" >&2
400 sleep 1
401 fi
402
403 # variable contains taxonomy-database-specific xtract indexing instructions
404 read -r -d '' idxtxt <<- EOS
405 xtract -set IdxDocumentSet -rec IdxDocument \
406 -pattern NMSummary -UID -translate NMSummary/Accession \
407 -wrp IdxUid -element "&UID" -clr -rst -tab "" \
408 -group NMSummary -pkg IdxSearchFields \
409 -block NMSummary \
410 -wrp UID -pad "&UID" \
411 -wrp ACCN -element "Accession[|.]" \
412 -wrp ACVR -accession Accession \
413 -wrp OFST -element Offset \
414 -wrp ORGN -element Organism \
415 -wrp TXID -element TaxonID \
416 -wrp CHR -element Chromosome \
417 -wrp MAP -element Map \
418 -wrp GENE -element Gene \
419 -wrp GUID -element GeneID \
420 -wrp PROD -element Product \
421 -wrp LOC -element Location \
422 -wrp GCODE -element GeneticCode \
423 -wrp PACC -element "ProtID[|.]" \
424 -wrp PAVR -accession ProtID \
425 -wrp PLEN -element ProtLength \
426 -wrp CLEN -element CDSLength \
427 -wrp MLEN -element MRnaLength \
428 -wrp NIVL -element NumIntervals
429 EOS
430
431 if [ "$e2index" = true ]
432 then
433 seconds_start=$(date "+%s")
434 echo "Incremental Indexing" >&2
435
436 temp=$(mktemp /tmp/INDEX_TEMP.XXXXXXXXX)
437 # generate file with xtract indexing arguments, split onto separate lines, skipping past xtract command itself
438 echo "${idxtxt}" | xargs -n1 echo | tail -n +2 > $temp
439 rchive -db "$dbase" -transform "${extrasBase}/accessions.txt" \
440 -e2incIndex "${archiveBase}" "${indexBase}" -idxargs "$temp" -dotmax "$dotmaxIdx" -e2index
441 rm "$temp"
442
443 seconds_end=$(date "+%s")
444 seconds=$((seconds_end - seconds_start))
445 IDX=$seconds
446 echo "IDX $IDX seconds" >&2
447 echo "" >&2
448 sleep 1
449 fi
450
451 if [ "$e2invert" = true ]
452 then
453 seconds_start=$(date "+%s")
454 echo "Incremental Inversion" >&2
455
456 rchive -db "$dbase" -dotmax "$dotmaxInv" -e2incInvert "${indexBase}" "${invertBase}"
457
458 seconds_end=$(date "+%s")
459 seconds=$((seconds_end - seconds_start))
460 INV=$seconds
461 echo "INV $INV seconds" >&2
462 echo "" >&2
463 sleep 1
464 fi
465
466 if [ "$e2merge" = true ]
467 then
468 seconds_start=$(date "+%s")
469 echo "Merging Inverted Indices" >&2
470
471 if [ -d "${invertBase}" ]
472 then
473 cd "${invertBase}"
474
475 rchive -gzip -db "$dbase" -merge "${mergedBase}" *.inv.gz
476 fi
477
478 seconds_end=$(date "+%s")
479 seconds=$((seconds_end - seconds_start))
480 MRG=$seconds
481 echo "MRG $MRG seconds" >&2
482 echo "" >&2
483 sleep 1
484 if [ ! -f "${mergedBase}/zz.mrg.gz" ]
485 then
486 echo "ERROR: Merge failed to complete - missing zz.mrg.gz file" >&2
487 echo "" >&2
488 echo "EXITING DUE TO BUILD FAILURE" >&2
489 echo "" >&2
490 # do not continue
491 e2post=false
492 fi
493 fi
494
495 if [ "$e2post" = true ]
496 then
497 seconds_start=$(date "+%s")
498 echo "Producing Postings Files" >&2
499
500 if [ -d "${mergedBase}" ]
501 then
502 cd "${mergedBase}"
503
504 for fl in *.mrg.gz
505 do
506 echo "$fl"
507 done |
508 sort |
509 xargs -n 100 echo |
510 while read files
511 do
512 rchive -db "$dbase" -promote "${postingsBase}" "$fields" $files
513 done
514 fi
515
516 seconds_end=$(date "+%s")
517 seconds=$((seconds_end - seconds_start))
518 PST=$seconds
519 echo "PST $PST seconds" >&2
520 echo "" >&2
521 sleep 1
522 fi
523
524 wait
525
526 if [ "$e2post" = true ] && [ -d "${mergedBase}" ]
527 then
528 target="${mergedBase}"
529 find "$target" -name "*.mrg" -delete
530 find "$target" -name "*.mrg.gz" -delete
531 fi
532
533 cd
534
535 echo "ARCHIVE-NMCDS" >&2
536
537 echo "" >&2
538
539 PrintTime() {
540
541 if [ "$1" = true ]
542 then
543 echo "$2 $3 seconds" >&2
544 fi
545 }
546
547 PrintTime "$download" "DWN" "$DWN"
548 PrintTime "$generate" "GEN" "$GEN"
549 PrintTime "$populate" "POP" "$POP"
550
551 PrintTime "$e2index" "IDX" "$IDX"
552 PrintTime "$e2invert" "INV" "$INV"
553 PrintTime "$e2merge" "MRG" "$MRG"
554 PrintTime "$e2post" "PST" "$PST"
555
556 echo "" >&2
557
558 function PrintTotalElapsedTime {
559 local L=$1
560 local T=$2
561 local D=$((T/60/60/24))
562 local H=$((T/60/60%24))
563 local M=$((T/60%60))
564 local S=$((T%60))
565 printf '%s %d seconds' "$L" $T 1>&2
566 if [ "$T" -gt 59 ]
567 then
568 printf ', or' 1>&2
569 (( $D > 0 )) && printf ' %d days' $D 1>&2
570 (( $H > 0 )) && printf ' %d hours' $H 1>&2
571 (( $M > 0 )) && printf ' %d minutes' $M 1>&2
572 (( $S > 0 )) && printf ' %d seconds' $S 1>&2
573 fi
574 printf '\n' 1>&2
575 }
576
577 total_end=$(date "+%s")
578 total=$((total_end - total_start))
579 TOT=$total
580 PrintTotalElapsedTime "TOT" "$TOT"
581 echo "" >&2