Debian Codegraph @

elink

raw
1 #!/bin/bash
2
3 # ===========================================================================
4 #
5 # PUBLIC DOMAIN NOTICE
6 # National Center for Biotechnology Information (NCBI)
7 #
8 # This software/database is a "United States Government Work" under the
9 # terms of the United States Copyright Act. It was written as part of
10 # the author's official duties as a United States Government employee and
11 # thus cannot be copyrighted. This software/database is freely available
12 # to the public for use. The National Library of Medicine and the U.S.
13 # Government do not place any restriction on its use or reproduction.
14 # We would, however, appreciate having the NCBI and the author cited in
15 # any work or product based on this material.
16 #
17 # Although all reasonable efforts have been taken to ensure the accuracy
18 # and reliability of the software and data, the NLM and the U.S.
19 # Government do not and cannot warrant the performance or results that
20 # may be obtained by using this software or data. The NLM and the U.S.
21 # Government disclaim all warranties, express or implied, including
22 # warranties of performance, merchantability or fitness for any particular
23 # purpose.
24 #
25 # ===========================================================================
26 #
27 # File Name: elink
28 #
29 # Author: Jonathan Kans, Aaron Ucko
30 #
31 # Version Creation Date: 06/03/2020
32 #
33 # ==========================================================================
34
35 pth=$( dirname "$0" )
36
37 case "$pth" in
38 /* )
39 ;; # already absolute
40 * )
41 pth=$(cd "$pth" && pwd)
42 ;;
43 esac
44
45 case ":$PATH:" in
46 *:"$pth":* )
47 ;;
48 * )
49 PATH="$PATH:$pth"
50 export PATH
51 ;;
52 esac
53
54 # handle common flags - dot command is equivalent of "source"
55
56 if [ ! -f "$pth"/ecommon.sh ]
57 then
58 echo "ERROR: Unable to find '$pth/ecommon.sh' file" >&2
59 exit 1
60 fi
61
62 . "$pth"/ecommon.sh
63
64 # initialize specific flags
65
66 internal=false
67
68 target=""
69
70 name=""
71 cmmd=""
72 mode=""
73 filter=""
74
75 idtype=""
76 related=false
77
78 cited=false
79 cites=false
80
81 chunk=100
82 default_chunk=true
83
84 elink_debug=false
85 if [ -n "${ELINK_DEBUG}" ] && [ "${ELINK_DEBUG}" = true ]
86 then
87 elink_debug=true
88 fi
89
90 # read command-line arguments
91
92 while [ $# -gt 0 ]
93 do
94 tag="$1"
95 rem="$#"
96 case "$tag" in
97 -internal )
98 internal=true
99 shift
100 ;;
101 -newmode | -oldmode )
102 shift
103 ;;
104 -db )
105 CheckForArgumentValue "$tag" "$rem"
106 shift
107 dbase="$1"
108 shift
109 ;;
110 -id )
111 CheckForArgumentValue "$tag" "$rem"
112 shift
113 ids="$1"
114 shift
115 while [ $# -gt 0 ]
116 do
117 case "$1" in
118 -* )
119 break
120 ;;
121 * )
122 # concatenate run of UIDs with commas
123 ids="$ids,$1"
124 shift
125 ;;
126 esac
127 done
128 ;;
129 -format )
130 shift
131 if [ $# -gt 0 ]
132 then
133 shift
134 if [ "$1" = "acc" ] || [ "$1" = "accn" ]
135 then
136 idtype=acc
137 fi
138 else
139 DisplayError "Missing -format argument"
140 exit 1
141 fi
142 ;;
143 -target )
144 shift
145 if [ $# -gt 0 ]
146 then
147 if [ -n "$target" ]
148 then
149 if [ "$target" = "$1" ]
150 then
151 DisplayWarning "Redundant -target '$1' argument"
152 else
153 DisplayError "Colliding -target '$target' and '$1' arguments"
154 exit 1
155 fi
156 fi
157 target="$1"
158 shift
159 else
160 DisplayError "Missing -target argument"
161 exit 1
162 fi
163 ;;
164 -name | -linkname )
165 CheckForArgumentValue "$tag" "$rem"
166 shift
167 name="$1"
168 shift
169 ;;
170 -cmd )
171 CheckForArgumentValue "$tag" "$rem"
172 shift
173 cmmd="$1"
174 shift
175 ;;
176 -mode )
177 CheckForArgumentValue "$tag" "$rem"
178 shift
179 mode="$1"
180 shift
181 ;;
182 -filter )
183 CheckForArgumentValue "$tag" "$rem"
184 shift
185 # set term for filtering after link -related (undocumented)
186 filter="$1"
187 shift
188 ;;
189 -related )
190 related=true
191 shift
192 ;;
193 -neighbor )
194 related=true
195 shift
196 ;;
197 -cited )
198 cited=true
199 shift
200 ;;
201 -cites )
202 cites=true
203 shift
204 ;;
205 -elink_debug | -elink-debug )
206 elink_debug=true
207 shift
208 ;;
209 -chunk )
210 CheckForArgumentValue "$tag" "$rem"
211 shift
212 # override default chunk value (undocumented)
213 chunk=$(( $1 ))
214 default_chunk=false
215 shift
216 ;;
217 -batch )
218 # accept -batch flag from old scripts - now standard behavior
219 shift
220 ;;
221 -h | -help | --help | help )
222 echo "elink $version"
223 echo ""
224 newVersion=$( NewerEntrezDirectVersion )
225 if [ -n "$newVersion" ]
226 then
227 DisplayNote "EDirect version ${newVersion} is now available"
228 echo "" >&2
229 cat "$pth/help/elink-help.txt"
230 echo ""
231 DisplayNote "EDirect version ${newVersion} is now available"
232 echo "" >&2
233 else
234 cat "$pth/help/elink-help.txt"
235 echo ""
236 fi
237 exit 0
238 ;;
239 -* )
240 ParseCommonArgs "$@"
241 if [ "$argsConsumed" -gt 0 ]
242 then
243 shift "$argsConsumed"
244 else
245 DisplayError "Unrecognized option $1"
246 exit 1
247 fi
248 ;;
249 * )
250 DisplayError "Unrecognized argument $1"
251 shift
252 ;;
253 esac
254 done
255
256 FinishSetup
257
258 # check for ENTREZ_DIRECT message or piped UIDs unless database and UIDs provided in command line
259
260 if [ -z "$db" ]
261 then
262 ParseStdin
263 elif [ -z "$ids" ] && [ -z "$input" ]
264 then
265 ParseStdin
266 fi
267
268 # additional argument reality checks
269
270 if [ -n "$db" ] && [ -n "$dbase" ] && [ "$db" != "$dbase" ]
271 then
272 DisplayError "Colliding -db '$db' and ENTREZ_DIRECT Db '$dbase' arguments"
273 exit 1
274 fi
275
276 if [ "$related" = true ]
277 then
278 if [ -n "$db" ] && [ -n "$target" ] && [ "$db" != "$target" ]
279 then
280 DisplayError "-related -db '$db' incompatible with -target '$target'"
281 exit 1
282 elif [ -n "$dbase" ] && [ -n "$target" ] && [ "$dbase" != "$target" ]
283 then
284 DisplayError "-related and ENTREZ_DIRECT Db '$dbase' incompatible with -target '$target'"
285 exit 1
286 fi
287 fi
288
289 if [ -z "$ids$rest$qury$input" ]
290 then
291 needHistory=true
292 fi
293
294 # take database from dbase value or -db argument
295
296 if [ -z "$dbase" ]
297 then
298 dbase="$db"
299 fi
300
301 if [ "$dbase" = "nucleotide" ]
302 then
303 dbase="nuccore"
304 fi
305
306 # check for missing required arguments
307
308 if [ -z "$dbase" ]
309 then
310 DisplayError "Missing -db argument"
311 exit 1
312 fi
313
314 # normalize to lower-case (e.g., SRA -> sra)
315
316 dbase=$( echo "$dbase" | tr '[:upper:]' '[:lower:]' )
317
318 # take optional days and datetype arguments from message
319
320 if [ -z "$reldate" ] && [ -n "$reldatex" ]
321 then
322 reldate="$reldatex"
323 fi
324 if [ -z "$mindate" ] && [ -n "$mindatex" ]
325 then
326 mindate="$mindatex"
327 fi
328 if [ -z "$maxdate" ] && [ -n "$maxdatex" ]
329 then
330 maxdate="$maxdatex"
331 fi
332 if [ -z "$datetype" ] && [ -n "$datetypex" ]
333 then
334 datetype="$datetypex"
335 fi
336
337 # normalize date arguments
338
339 FixDateConstraints
340
341 # convert spaces between UIDs to commas
342
343 ids=$( echo "$ids" | sed -e "s/ /,/g; s/,,*/,/g" )
344
345 # cmd aliases
346
347 case "$cmmd" in
348 history )
349 cmmd="neighbor_history"
350 ;;
351 neighbors )
352 # silently convert known typo in existing scripts
353 cmmd="neighbor"
354 ;;
355 entrez )
356 # alias for edirect
357 cmmd="edirect"
358 ;;
359 uids )
360 # alias for uid
361 cmmd="uid"
362 ;;
363 score )
364 cmmd="neighbor_score"
365 if [ -z "$target" ]
366 then
367 target="$dbase"
368 fi
369 ;;
370 llibs )
371 cmmd="llinkslib"
372 ;;
373 esac
374
375 # special cases for target, cmd, and linkname
376
377 case "$cmmd" in
378 acheck )
379 ;;
380 ncheck | lcheck | llinks | llinkslib | prlinks )
381 target=""
382 ;;
383 neighbor | neighbor_score | neighbor_history )
384 ;;
385 edirect | uid )
386 if [ -z "$target" ]
387 then
388 target="$dbase"
389 fi
390
391 if [ -z "$name" ]
392 then
393 # set default name
394 name="${dbase}_${target}"
395 fi
396 ;;
397 * )
398 if [ -n "$cmmd" ]
399 then
400 DisplayWarning "Unrecognized -cmd option $cmmd, ignoring for now"
401 cmmd=""
402 fi
403 if [ -z "$target" ] && [ "$related" = false ] && [ "$cited" = false ] && [ "$cites" = false ]
404 then
405 DisplayError "Must supply -target or -related on command line"
406 exit 1
407 fi
408 if [ -z "$target" ]
409 then
410 target="$dbase"
411 fi
412
413 if [ -z "$name" ]
414 then
415 # set default name
416 name="${dbase}_${target}"
417 # special case for pubmed_pmc - commented out now that the link has returned
418 # if [ $name = "pubmed_pmc" ]
419 # then
420 # name="pubmed_pmc_local"
421 # fi
422 fi
423 ;;
424 esac
425
426 if [ -z "$cmmd" ]
427 then
428 cmmd="neighbor_history"
429 fi
430
431 if [ "$dbase" = "nlmcatalog" ]
432 then
433 DisplayError "Entrez Direct does not support links for the nlmcatalog database"
434 exit 1
435 fi
436
437 # input reality checks
438
439 if [ "$needHistory" = true ]
440 then
441
442 if [ -t 0 ]
443 then
444 DisplayError "ENTREZ_DIRECT message not piped from stdin"
445 exit 1
446 fi
447 if [ -z "$web_env" ]
448 then
449 DisplayError "WebEnv value not found in elink input"
450 exit 1
451 fi
452 if [ -z "$qry_key" ]
453 then
454 DisplayError "QueryKey value not found in elink input"
455 exit 1
456 fi
457 if [ -z "$num" ] || [ "$num" -lt 1 ]
458 then
459 # print message with count of 0 if no results to process
460 WriteEDirect "$target" "$web_env" "$qry_key" "0" "$stp" "$err"
461 exit 0
462 fi
463 fi
464
465 if [ "$cited" = true ] || [ "$cites" = true ]
466 then
467 if [ "$dbase" != "pubmed" ]
468 then
469 DisplayError "-cited or -cites can only be used with -db pubmed"
470 exit 1
471 fi
472 fi
473
474 # lookup accessions in -id argument or piped from stdin
475
476 if [ "$elink_debug" = true ]
477 then
478 echo "LookupSpecialAccessions" >&2
479 fi
480
481 LookupSpecialAccessions
482
483 # -cited or -cites access the NIH Open Citation Collection dataset (see PMID 31600197)
484
485 LinkInIcite() {
486
487 iciteElement="$1"
488 GetUIDs |
489 join-into-groups-of "$chunk" |
490 while read uids
491 do
492 nquire -get https://icite.od.nih.gov/api/pubs -pmids "$uids" |
493 transmute -j2x |
494 xtract -pattern opt -sep "\n" -element "$iciteElement"
495 done |
496 accn-at-a-time |
497 sort -n | uniq
498 }
499
500 QueryIcite() {
501
502 cits=$( LinkInIcite "$1" )
503
504 if [ -n "$cits" ]
505 then
506 # post to history appears to be broken for large sets, instantiate in message instead
507 num=$( echo "$cits" | wc -l | tr -d ' ' )
508 echo "<ENTREZ_DIRECT>"
509 echo " <Db>${dbase}</Db>"
510 echo " <Count>${num}</Count>"
511 echo "$cits" |
512 accn-at-a-time |
513 while read uid
514 do
515 echo " <Id>${uid}</Id>"
516 done
517 echo "</ENTREZ_DIRECT>"
518 else
519 echo "<ENTREZ_DIRECT>"
520 echo " <Db>pubmed</Db>"
521 echo " <Count>0</Count>"
522 echo "</ENTREZ_DIRECT>"
523 fi
524 }
525
526 if [ "$cited" = true ]
527 then
528 # equivalent of -name pubmed_pubmed_citedin (for pubmed records also in pmc)
529 QueryIcite "cited_by"
530
531 exit 0
532 fi
533
534 if [ "$cites" = true ]
535 then
536 # equivalent of -name pubmed_pubmed_refs (for pubmed records also in pmc)
537 QueryIcite "references"
538
539 exit 0
540 fi
541
542 # helper function adds link-specific arguments (if set)
543
544 RunWithLinkArgs() {
545
546 if [ "$log" = true ]
547 then
548 printf "." >&2
549 fi
550
551 AddIfNotEmpty -dbfrom "$dbase" \
552 AddIfNotEmpty -db "$target" \
553 AddIfNotEmpty -cmd "$cmmd" \
554 AddIfNotEmpty -linkname "$name" \
555 AddIfNotEmpty -retmode "$mode" \
556 AddIfNotEmpty -idtype "$idtype" \
557 AddIfNotEmpty -term "$filter" \
558 RunWithCommonArgs "$@"
559 }
560
561 # explicitly-set non-history link requests generate XML results
562
563 if [ "$cmmd" != "neighbor_history" ] && [ "$cmmd" != "edirect" ] && [ "$cmmd" != "uid" ]
564 then
565
566 GetUIDs |
567 join-into-groups-of "$chunk" |
568 while read uids
569 do
570 uids=$( echo "$uids" | tr ',' ' ' )
571 set nquire -url "$base" elink.fcgi
572 # $uids is unquoted so the shell will perform word splitting on it
573 for uid in $uids
574 do
575 # individual -id arguments get a separate set of link results for each uid
576 set "$@" -id "$uid"
577 done
578 RunWithLinkArgs "$@" |
579 transmute -format indent -doctype ""
580 done
581
582 exit 0
583 fi
584
585 # -quick uses history for elink without trying to circumvent truncation limits
586
587 if [ "$quick" = true ] && [ "$needHistory" = true ]
588 then
589
590 err=""
591 cmmd="neighbor_history"
592 res=$( RunWithLinkArgs nquire -url "$base" elink.fcgi \
593 -WebEnv "$web_env" -query_key "$qry_key" )
594
595 if [ -n "$res" ]
596 then
597 dt=""
598 ParseMessage "$res" eLinkResult dt DbTo web_env WebEnv qry_key QueryKey
599
600 if [ -n "$err" ]
601 then
602 DisplayError "elink failed - $err"
603 exit 1
604 fi
605 if [ -z "$web_env" ]
606 then
607 echo "WebEnv value not found in elink output - WebEnv1 $wb"
608 exit 1
609 fi
610 if [ -n "$wb" ] && [ "$web_env" != "$wb" ]
611 then
612 echo "WebEnv mismatch in elink output - WebEnv1 $wb, WebEnv2 $web_env"
613 exit 1
614 fi
615 if [ -z "$qry_key" ]
616 then
617 lst=$( echo "$res" | tail -n 8 )
618 echo "QueryKey value not found in elink output - unable to retrieve count"
619 echo "$lst"
620 exit 1
621 fi
622
623 # need to call esearch to get count of results stored in history - this will also increment the QueryKey
624 num="0"
625 nbr=$( RunWithCommonArgs nquire -url "$base" esearch.fcgi \
626 -WebEnv "$web_env" -query_key "$qry_key" -retmax 0 -db "$target" )
627 if [ -n "$nbr" ]
628 then
629 num=$( echo "$nbr" |
630 sed -e 's|<TranslationStack>.*</TranslationStack>||' |
631 sed -e 's|<QueryTranslation>.*</QueryTranslation>||' |
632 xtract -pattern eSearchResult -element Count )
633 fi
634
635 WriteEDirect "$dt" "$web_env" "$qry_key" "$num" "$stp" "$err"
636 fi
637
638 exit 0
639 fi
640
641 # special case originally for PubMed with new SOLR server
642
643 LinkAllInChunks() {
644
645 # use neighbor command instead of history mechanism
646 cmmd="neighbor"
647
648 # default chunk of 100 avoids overflow of elink.fcgi server or backend database
649 GetUIDs |
650 join-into-groups-of "$chunk" |
651 while read uids
652 do
653 RunWithLinkArgs nquire -url "$base" elink.fcgi -id "$uids" "$@" |
654 xtract -pattern LinkSet -sep "\n" -element Link/Id
655 done |
656 sort -V | uniq -i | grep '.'
657 }
658
659 # query in small chunks for efficiency, return sorted and uniqued UID list
660
661 if [ "$cmmd" = "uid" ]
662 then
663
664 if [ "$log" = true ]
665 then
666 printf "ELink\n" >&2
667 fi
668 flt=""
669 raw=$( LinkAllInChunks )
670 if [ "$log" = true ]
671 then
672 printf "\n" >&2
673 fi
674
675 echo "$raw"
676
677 exit 0
678 fi
679
680 if [ "$target" = "pubmed" ] || [ "$cmmd" = "edirect" ]
681 then
682
683 if [ "$log" = true ]
684 then
685 printf "ELink\n" >&2
686 fi
687 flt=""
688 raw=$( LinkAllInChunks )
689 if [ "$log" = true ]
690 then
691 printf "\n" >&2
692 fi
693 if [ -n "$raw" ]
694 then
695 flt=$( echo "$raw" | sed -e 's/^/ <Id>/' -e 's/$/<\/Id>/' )
696 num=$( echo "$raw" | wc -l | tr -d ' ' )
697 else
698 # clear values if results are empty
699 flt=""
700 num="0"
701 fi
702
703 seconds_end=$(date "+%s")
704 seconds_elapsed=$((seconds_end - seconds_start))
705
706 # create -format xids output
707 echo "<ENTREZ_DIRECT>"
708 if [ -n "$target" ]
709 then
710 echo " <Db>${target}</Db>"
711 fi
712 if [ -n "$num" ]
713 then
714 echo " <Count>${num}</Count>"
715 fi
716 if [ -n "$stp" ]
717 then
718 # increment step value
719 stp=$(( stp + 1 ))
720 echo " <Step>${stp}</Step>"
721 fi
722 if [ -n "$flt" ]
723 then
724 echo "$flt"
725 fi
726 if [ "$quick" = true ] || [ "$quickx" = "Y" ]
727 then
728 echo " <Quick>Y</Quick>"
729 fi
730 if [ "$debug" = true ] || [ "$debugx" = "Y" ]
731 then
732 echo " <Debug>Y</Debug>"
733 fi
734 if [ "$log" = true ] || [ "$logx" = "Y" ]
735 then
736 echo " <Log>Y</Log>"
737 fi
738 if [ "$timer" = true ] && [ -n "$seconds_elapsed" ]
739 then
740 echo " <Elapsed>${seconds_elapsed}</Elapsed>"
741 fi
742 echo "</ENTREZ_DIRECT>"
743
744 exit 0
745 fi
746
747 # helper function adds web environment argument for history (if set)
748
749 RunWithLinkHistoryArgs() {
750
751 AddIfNotEmpty -WebEnv "$web_env" \
752 RunWithLinkArgs "$@"
753 }
754
755 # -cmd neighbor_history now passes UID list to external elink if input is a history reference
756
757 # esearch -db gene -query "Pancreatic Cancer Biomarker AND human [ORGN]" | tee /dev/tty |
758 # elink -target protein -log | tee /dev/tty |
759 # efetch -format docsum > refseq.txt
760
761 # internally converted to:
762
763 # esearch -db gene -query "Pancreatic Cancer Biomarker AND human [ORGN]" | tee /dev/tty |
764 # efetch -format uid | tee /dev/tty |
765 # elink -db gene -target protein -log | tee /dev/tty |
766 # efetch -format docsum > refseq.txt
767
768 MakeXIDMessage() {
769
770 echo "<ENTREZ_DIRECT>"
771 if [ -n "$dbase" ]
772 then
773 echo " <Db>${dbase}</Db>"
774 fi
775 # instantiate UIDs within ENTREZ_DIRECT message
776
777 res=$( GetUIDs )
778
779 nm=$( echo "$res" | wc -l | tr -d ' ' )
780 if [ -n "$nm" ]
781 then
782 echo " <Count>${nm}</Count>"
783 fi
784 if [ -n "$stp" ]
785 then
786 # increment step value
787 stp=$(( stp + 1 ))
788 echo " <Step>${stp}</Step>"
789 fi
790 echo "$res" |
791 while read uid
792 do
793 echo " <Id>${uid}</Id>"
794 done
795 if [ -n "$err" ]
796 then
797 echo " <Error>${err}</Error>"
798 fi
799
800 seconds_end=$(date "+%s")
801 seconds_elapsed=$((seconds_end - seconds_start))
802
803 if [ "$log" = true ] || [ "$logx" = "Y" ]
804 then
805 echo " <Log>Y</Log>"
806 fi
807
808 if [ "$timer" = true ] && [ -n "$seconds_elapsed" ]
809 then
810 echo " <Elapsed>${seconds_elapsed}</Elapsed>"
811 fi
812
813 echo "</ENTREZ_DIRECT>"
814 }
815
816 # workaround for history server limitations
817
818 if [ "$needHistory" = true ]
819 then
820
821 # instantiate UIDs into ENTREZ_DIRECT message
822 xids=$( MakeXIDMessage )
823
824 lg=""
825 ck=""
826 ed=""
827
828 if [ "$log" = true ]
829 then
830 lg="ok"
831 fi
832 if [ "$default_chunk" = false ]
833 then
834 ck="ok"
835 fi
836 if [ "$elink_debug" = true ]
837 then
838 ed="ok"
839 fi
840
841 if [ "$elink_debug" = true ]
842 then
843 echo "Launching External Elink" >&2
844 echo "Launching elink -db $dbase -target $target " ${ck:+"-chunk"} ${ck:+"${chunk}"} ${lg:+"-log"} ${ed:+"-elink_debug"} >&2
845 fi
846
847 # shell parameter expansion populates optional arguments sent to separate instance of elink,
848 # execution logic just below in LinkInGroups function
849 ( echo "$xids" | elink -db "$dbase" -target "$target" ${ck:+"-chunk"} ${ck:+"${chunk}"} ${lg:+"-log"} ${ed:+"-elink_debug"} )
850
851 if [ "$elink_debug" = true ]
852 then
853 echo "External Elink Launched" >&2
854 fi
855
856 exit 0
857 fi
858
859 # -cmd neighbor_history
860
861 # in new instance called from above, GetUIDs will obtain the uid list from instantiated message,
862 # the previous web_env is abandoned, and the first call to RunWithLinkHistoryArgs will assign a new
863 # web_env value for individual link subset results and the final merge
864
865 wb="$web_env"
866
867 LinkInGroups() {
868
869 if [ "$log" = true ]
870 then
871 printf "ELink\n" >&2
872 fi
873
874 if [ "$elink_debug" = true ]
875 then
876 echo "Enter LinkInGroups" >&2
877 fi
878
879 # calculate num if using raw uids or -id argument
880 if [ -z "$num" ] || [ "$num" = 0 ]
881 then
882 if [ -n "$rest" ]
883 then
884 num=$( echo "$rest" | wc -l | tr -d ' ' )
885 elif [ -n "$ids" ]
886 then
887 num=$( echo "$ids" | wc -l | tr -d ' ' )
888 fi
889 fi
890
891 if [ -n "$num" ] && [ "$num" -gt 100 ] && [ "$default_chunk" = true ]
892 then
893 # integer division of ( num + 97 ) / 98 allows up to 99 groups to be loaded into memory,
894 # fusion of result components with "(#1) OR (#2) ... OR (#99)" will create the 100th (maximum) entry
895 numer=$(( $num + 97 ))
896 denom=$(( 98 ))
897 chunk=$(( $numer / $denom ))
898 fi
899
900 loop_count=0
901
902 # limit to 99 history elements storing link results - any more will fail once combined into another entry,
903 # use awk instead of head -n 99 to avoid prematurely closed (broken) pipe error
904 GetUIDs |
905 join-into-groups-of "$chunk" |
906 awk 'NR < 100' |
907 while read uids
908 do
909 # limit to first 100 to avoid backend server error
910 firstuids=$( echo "$uids" | tr ',' '\n' | head -n 100 | join-into-groups-of 100 )
911 loop_count=$(( loop_count + 1 ))
912 err=""
913 res=$( RunWithLinkHistoryArgs nquire -url "$base" elink.fcgi -id "$firstuids" )
914
915 if [ -n "$res" ]
916 then
917 dt=""
918 ParseMessage "$res" eLinkResult dt DbTo web_env WebEnv qry_key QueryKey
919
920 if [ "$elink_debug" = true ]
921 then
922 echo "WebEnv $web_env, QueryKey $qry_key, Count $loop_count" >&2
923 if [ "$qry_key" == "" ]
924 then
925 echo "RES" >&2
926 echo "$res" | grep -v "<Id>" >&2
927 fi
928 fi
929
930 if [ -n "$err" ]
931 then
932 DisplayError "elink failed - $err"
933 exit 1
934 fi
935 if [ -z "$web_env" ]
936 then
937 echo "WebEnv value not found in elink output - WebEnv1 $wb"
938 exit 1
939 fi
940 if [ -n "$wb" ] && [ "$web_env" != "$wb" ]
941 then
942 echo "WebEnv mismatch in elink output - WebEnv1 $wb, WebEnv2 $web_env"
943 exit 1
944 fi
945
946 WriteEDirectStep "$dt" "$web_env" "$qry_key" "$err"
947 fi
948 done
949
950 if [ "$elink_debug" = true ]
951 then
952 echo "Leave LinkInGroups" >&2
953 fi
954
955 if [ "$log" = true ]
956 then
957 printf "\n" >&2
958 fi
959 }
960
961 lnks=$( LinkInGroups )
962
963 if [ -n "$lnks" ]
964 then
965
966 # extract first database and webenv values, and all key numbers
967 comps=$( echo "$lnks" | xtract -wrp Set,Rec -pattern ENTREZ_DIRECT \
968 -wrp Web -element WebEnv -wrp Key -element QueryKey )
969
970 wbnv=$( echo "$comps" | xtract -pattern Set -first Web )
971 qrry=$( echo "$comps" | xtract -pattern Set -block Rec -pfx "(#" -sfx ")" -tab " OR " -element Key )
972
973 if [ "$elink_debug" = true ]
974 then
975 echo "QRRY $qrry" >&2
976 fi
977
978 err=""
979 num=""
980 if [ -z "$qrry" ]
981 then
982 # no neighbors or links can be a normal response,
983 # e.g., elink -db gene -id 496376 -target medgen
984 WriteEDirect "$target" "$web_env" "$qry_key" "0" "$stp" "$err"
985 exit 0
986 fi
987
988 # send search command, e.g, "(#1) OR (#2)", along with database and web environment
989 srch=$( RunWithCommonArgs nquire -url "$base" esearch.fcgi -db "$target" \
990 -WebEnv "$wbnv" -term "$qrry" -retmax 0 -usehistory y )
991
992 if [ -n "$srch" ]
993 then
994 res=$( echo "$srch" | sed -e 's|<TranslationStack>.*</TranslationStack>||' )
995 ParseMessage "$res" eSearchResult web_env WebEnv qry_key QueryKey num Count
996
997 if [ "$elink_debug" = true ]
998 then
999 echo "SRCH $res" >&2
1000 fi
1001 fi
1002
1003 if [ -n "$num" ] && [ "$num" -lt 1 ]
1004 then
1005 tmp=$( GetUIDs )
1006 uids=$( echo "$tmp" | head -n "$chunk" | join-into-groups-of "$chunk" )
1007 res=$( RunWithCommonArgs nquire -url "$base" elink.fcgi \
1008 -dbfrom "$dbase" -id "$uids" -cmd "acheck" )
1009
1010 if [ -n "$res" ]
1011 then
1012 ParseMessage "$res" eLinkResult ignore DbFrom
1013
1014 if [ -z "$err" ]
1015 then
1016 tst=$( echo "$res" | xtract -pattern LinkInfo -if LinkName -equals "$name" -element LinkName )
1017 if [ -n "$tst" ]
1018 then
1019 DisplayError "UNEXPECTED EMPTY LINK RESULT FOR ${name}"
1020 fi
1021 else
1022 DisplayError "-cmd acheck TEST FAILED - ${err}"
1023 fi
1024 fi
1025 fi
1026
1027 WriteEDirect "$target" "$web_env" "$qry_key" "$num" "$stp" "$err"
1028
1029 exit 0
1030 fi
1031
1032 # warn on error
1033
1034 DisplayError "ELink failure"
1035 exit 1