Debian Codegraph @

xsearch

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: xsearch
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
70 while [ $# -gt 0 ]
71 do
72 case "$1" in
73 -version )
74 version=$( einfo -version )
75 echo "$version"
76 exit 0
77 ;;
78 -h | -help | --help | help )
79 version=$( einfo -version )
80 echo "xsearch $version"
81 echo ""
82 echo "USAGE: xsearch"
83 echo " -query | -match | -exact | -title | -words | -pairs"
84 echo " query arguments"
85 echo ""
86 echo "EXAMPLES"
87 echo ""
88 echo " xsearch -query \"(literacy AND numeracy) NOT (adolescent OR child)\""
89 echo ""
90 echo " xsearch -query \"selective serotonin reuptake inhibit*\""
91 echo ""
92 echo " xsearch -query \"vitamin c + + common cold\""
93 echo ""
94 echo " xsearch -query \"vitamin c ~ ~ common cold\""
95 echo ""
96 echo " xsearch -query \"C14.907.617.812* [TREE] AND 2015:2018 [YEAR]\""
97 echo ""
98 echo " xsearch -title \"Genetic Control of Biochemical Reactions in Neurospora.\""
99 echo ""
100 echo " xsearch -match \"nucleotide sequences required for tn3 transposition immunity [PAIR]\" |"
101 echo " just-top-hits 1 | cut -f 2 |"
102 echo " efetch -db pubmed -format abstract"
103 echo ""
104 exit 0
105 ;;
106 * )
107 break
108 ;;
109 esac
110 done
111
112 while [ $# -gt 0 ]
113 do
114 tag="$1"
115 rem="$#"
116 case "$tag" in
117 -raw )
118 raw=true
119 shift
120 ;;
121 -debug )
122 debug=true
123 shift
124 ;;
125 -path | -master )
126 # ignore for backward compatibility, but now requiring environment variable for path
127 # target=$2
128 shift
129 shift
130 ;;
131 -db )
132 CheckForArgumentValue "$tag" "$rem"
133 shift
134 dbase="$1"
135 shift
136 ;;
137 * )
138 break
139 ;;
140 esac
141 done
142
143 # set pubmed as default database for the time being
144 if [ -z "$dbase" ]
145 then
146 dbase="pubmed"
147 fi
148
149 # eventually will require explicit database in argument or piped message
150 if [ -z "$dbase" ]
151 then
152 echo "Must supply database in -db argument"
153 exit 1
154 fi
155
156 # get path to local postings folder
157
158 FindPostingsFolder
159
160 val="-query"
161 if [ $# -gt 0 ]
162 then
163 val="$1"
164 shift
165 fi
166
167 group_phrases() {
168 uniq |
169 paste -sd "," - |
170 sed -e 's/^+//g' -e 's/+$//g' -e 's/,+,/+/g' -e 's/^,//g' -e 's/,$//g' -e 's/+/ /g'
171 }
172
173 word_pairs() {
174 while read first rest
175 do
176 if [ -z "$rest" ]
177 then
178 echo "$first"
179 continue
180 fi
181 prev=$first
182 for curr in $rest
183 do
184 echo "$prev $curr"
185 prev="$curr"
186 done
187 done
188 }
189
190 # call rchive -query or -match functions
191
192 case "$val" in
193 -query | -phrase | -search )
194 if [ "$raw" = true ]
195 then
196 rchive -db "$dbase" -query "$*"
197 else
198 flt=""
199 num="0"
200 uids=$( rchive -db "$dbase" -query "$*" )
201 if [ -n "$uids" ]
202 then
203 flt=$( echo "$uids" | sed -e 's/^/ <Id>/' -e 's/$/<\/Id>/' )
204 num=$( echo "$uids" | wc -l | tr -d ' ' )
205 echo "<ENTREZ_DIRECT>"
206 if [ -n "$dbase" ]
207 then
208 echo " <Db>${dbase}</Db>"
209 fi
210 if [ -n "$num" ]
211 then
212 echo " <Count>${num}</Count>"
213 fi
214 if [ -n "$flt" ]
215 then
216 echo "$flt"
217 fi
218 echo " <Source>Local</Source>"
219 echo "</ENTREZ_DIRECT>"
220 fi
221 fi
222 ;;
223 -match | -partial )
224 rchive -db "$dbase" -match "$*"
225 ;;
226 -exact )
227 rchive -db "$dbase" -exact "$*"
228 ;;
229 -title )
230 rchive -db "$dbase" -title "$*"
231 ;;
232 -words )
233 echo "$*" |
234 word-at-a-time |
235 filter-stop-words |
236 while read txt
237 do
238 rchive -db "$dbase" -title "$txt"
239 done |
240 sort-uniq-count-rank -n
241 ;;
242 -pairs )
243 echo "$*" |
244 word-at-a-time |
245 filter-stop-words -plus |
246 group_phrases |
247 fmt -w 1 |
248 tr ',' ' ' |
249 word_pairs |
250 while read txt
251 do
252 rchive -db "$dbase" -title "$txt"
253 done |
254 sort-uniq-count-rank -n
255 ;;
256 -mock )
257 rchive -db "$dbase" -mock "$*"
258 ;;
259 -mocks )
260 rchive -db "$dbase" -mocks "$*"
261 ;;
262 -mockt )
263 rchive -db "$dbase" -mockt "$*"
264 ;;
265 -mockx )
266 rchive -db "$dbase" -mockx "$*"
267 ;;
268 -mockp )
269 echo "$*" |
270 word-at-a-time |
271 filter-stop-words -plus |
272 group_phrases |
273 fmt -w 1 |
274 tr ',' ' ' |
275 word_pairs
276 ;;
277 -* )
278 exec >&2
279 echo "ERROR: Unrecognized option $val" >&2
280 exit 1
281 ;;
282 * )
283 exec >&2
284 echo "ERROR: Unrecognized argument $val" >&2
285 exit 1
286 ;;
287 esac
288
289 exit 0