Debian Codegraph @

xlink

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: xlink
28 #
29 # Author: Jonathan Kans, Aaron Ucko
30 #
31 # Version Creation Date: 01/11/2025
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"/xcommon.sh ]
57 then
58 echo "ERROR: Unable to find '$pth/xcommon.sh' file" >&2
59 exit 1
60 fi
61
62 . "$pth"/xcommon.sh
63
64 # initialize specific flags
65
66 dbase=""
67 debug=false
68 raw=false
69 target=""
70
71 while [ $# -gt 0 ]
72 do
73 case "$1" in
74 -version )
75 version=$( einfo -version )
76 echo "$version"
77 exit 0
78 ;;
79 -h | -help | --help | help )
80 version=$( einfo -version )
81 echo "xlink $version"
82 echo ""
83 echo "USAGE: xlink"
84 echo " -target"
85 echo " link argument"
86 echo ""
87 echo "EXAMPLES"
88 echo ""
89 echo " xsearch -db pubmed -query \"Havran W* [AUTH]\" |"
90 echo " xlink -target CITED |"
91 echo " xfilter -query \"2020:2025 [YEAR]\" |"
92 echo " xfetch |"
93 echo " xtract -pattern PubmedArticle -histogram Journal/ISOAbbreviation |"
94 echo " sort-table -nr |"
95 echo " just-top-hits 10"
96 echo ""
97 exit 0
98 ;;
99 * )
100 break
101 ;;
102 esac
103 done
104
105 while [ $# -gt 0 ]
106 do
107 tag="$1"
108 rem="$#"
109 case "$tag" in
110 -raw )
111 raw=true
112 shift
113 ;;
114 -debug )
115 debug=true
116 shift
117 ;;
118 -path | -master )
119 # ignore for backward compatibility, but now requiring environment variable for path
120 # target=$2
121 shift
122 shift
123 ;;
124 -db )
125 CheckForArgumentValue "$tag" "$rem"
126 shift
127 dbase="$1"
128 shift
129 ;;
130 -id )
131 CheckForArgumentValue "$tag" "$rem"
132 shift
133 ids="$1"
134 shift
135 while [ $# -gt 0 ]
136 do
137 case "$1" in
138 -* )
139 break
140 ;;
141 * )
142 # concatenate run of UIDs with commas
143 ids="$ids,$1"
144 shift
145 ;;
146 esac
147 done
148 ;;
149 -input )
150 CheckForArgumentValue "$tag" "$rem"
151 shift
152 input="$1"
153 shift
154 ;;
155 * )
156 break
157 ;;
158 esac
159 done
160
161 # check for ENTREZ_DIRECT message or piped UIDs unless database and UIDs provided in command line
162
163 if [ -z "$dbase" ]
164 then
165 ParseStdin
166 elif [ -z "$ids" ] && [ -z "$input" ]
167 then
168 ParseStdin
169 fi
170
171 # set pubmed as default database for the time being
172 if [ -z "$dbase" ]
173 then
174 dbase="pubmed"
175 fi
176
177 # eventually will require explicit database in argument or piped message
178 if [ -z "$dbase" ]
179 then
180 echo "Must supply database in -db argument"
181 exit 1
182 fi
183
184 # normalize date arguments
185
186 FixDateConstraints
187
188 # get path to local postings folder
189
190 FindPostingsFolder
191
192 if [ $# -lt 2 ]
193 then
194 echo "ERROR: Insufficient arguments given to xlink" >&2
195 exit 1
196 fi
197
198 val="$1"
199 shift
200 case "$val" in
201 -target )
202 target="$1"
203 shift
204 ;;
205 -* )
206 exec >&2
207 echo "ERROR: Unrecognized option $val" >&2
208 exit 1
209 ;;
210 * )
211 exec >&2
212 echo "ERROR: Unrecognized argument $val" >&2
213 exit 1
214 ;;
215 esac
216
217 # get database-specific parameters from xlink.ini configuration file
218
219 dest="$dbase"
220
221 if [ ! -f "$pth"/xlink.ini ]
222 then
223 echo "ERROR: Unable to find '$pth/xlink.ini' file" >&2
224 exit 1
225 fi
226
227 mssg=$(
228 cat "${pth}/xlink.ini" |
229 ini2xml |
230 xtract -rec Rec -pattern "ConfigFile/*" -select "$dbase" |
231 tr '\n' ' '
232 )
233 if [ -n "$mssg" ]
234 then
235 ParseConfig "$mssg" Rec dest "$target"
236 fi
237
238 # call rchive -link
239
240 if [ "$raw" = true ]
241 then
242 GetUIDs |
243 word-at-a-time |
244 rchive -db "$dbase" -link "$target"
245 else
246 flt=""
247 num="0"
248 uids=$( GetUIDs | word-at-a-time | rchive -db "$dbase" -link "$target" )
249 if [ -n "$uids" ]
250 then
251 flt=$( echo "$uids" | sed -e 's/^/ <Id>/' -e 's/$/<\/Id>/' )
252 num=$( echo "$uids" | wc -l | tr -d ' ' )
253 echo "<ENTREZ_DIRECT>"
254 if [ -n "$dest" ]
255 then
256 echo " <Db>${dest}</Db>"
257 fi
258 if [ -n "$num" ]
259 then
260 echo " <Count>${num}</Count>"
261 fi
262 if [ -n "$flt" ]
263 then
264 echo "$flt"
265 fi
266 echo " <Source>Local</Source>"
267 echo "</ENTREZ_DIRECT>"
268 fi
269 fi
270
271 exit 0