Debian Codegraph @

xfilter

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: xfilter
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 db_argument_used=false
70 minimal_arguments=false
71
72 while [ $# -gt 0 ]
73 do
74 case "$1" in
75 -version )
76 version=$( einfo -version )
77 echo "$version"
78 exit 0
79 ;;
80 -h | -help | --help | help )
81 version=$( einfo -version )
82 echo "xfilter $version"
83 echo ""
84 echo "USAGE: xfilter"
85 echo " -query"
86 echo " query arguments"
87 echo ""
88 echo "EXAMPLES"
89 echo ""
90 echo " xfilter -query \"2020:2025 [YEAR]\""
91 echo ""
92 echo " xsearch -db pubmed -query \"Hoffmann PC [AUTH]\" |"
93 echo " elink -related |"
94 echo " xfilter -query \"Dopamine [MESH] AND Deficiency [SUBH]\" |"
95 echo " efetch -format apa"
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 db_argument_used=true
130 ;;
131 -id )
132 CheckForArgumentValue "$tag" "$rem"
133 shift
134 ids="$1"
135 shift
136 while [ $# -gt 0 ]
137 do
138 case "$1" in
139 -* )
140 break
141 ;;
142 * )
143 # concatenate run of UIDs with commas
144 ids="$ids,$1"
145 shift
146 ;;
147 esac
148 done
149 ;;
150 -input )
151 CheckForArgumentValue "$tag" "$rem"
152 shift
153 input="$1"
154 shift
155 ;;
156 * )
157 break
158 ;;
159 esac
160 done
161
162 # check for ENTREZ_DIRECT message or piped UIDs unless database and UIDs provided in command line
163
164 if [ -z "$dbase" ]
165 then
166 ParseStdin
167 elif [ -z "$ids" ] && [ -z "$input" ]
168 then
169 ParseStdin
170 fi
171
172 # set pubmed as default database for the time being
173 if [ -z "$dbase" ]
174 then
175 dbase="pubmed"
176 fi
177
178 # eventually will require explicit database in argument or piped message
179 if [ -z "$dbase" ]
180 then
181 echo "Must supply database in -db argument"
182 exit 1
183 fi
184
185 # normalize date arguments
186
187 FixDateConstraints
188
189 # get path to local postings folder
190
191 FindPostingsFolder
192
193 if [ $# -lt 1 ]
194 then
195 minimal_arguments=true
196 if [ "$db_argument_used" = false ]
197 then
198 echo "ERROR: Insufficient arguments given to xfilter" >&2
199 exit 1
200 fi
201 fi
202
203 # call rchive -query
204
205 uids=""
206 flt=""
207 num="0"
208
209 val="$1"
210 shift
211 case "$val" in
212 -query )
213 case "$*" in
214 "AND "* | "OR "* | "NOT "* )
215 uids=$( GetUIDs | word-at-a-time | rchive -db "$dbase" -query "[PIPE] $*" )
216 ;;
217 "[PIPE] "* )
218 uids=$( GetUIDs | word-at-a-time | rchive -db "$dbase" -query "$*" )
219 ;;
220 *)
221 uids=$( GetUIDs | word-at-a-time | rchive -db "$dbase" -query "[PIPE] AND $*" )
222 ;;
223 esac
224 ;;
225 -* )
226 exec >&2
227 echo "ERROR: Unrecognized option $val" >&2
228 exit 1
229 ;;
230 * )
231 if [ "$db_argument_used" = true ] && [ "$minimal_arguments" = true ]
232 then
233 uids=$( GetUIDs )
234 else
235 exec >&2
236 echo "ERROR: Unrecognized argument $val" >&2
237 exit 1
238 fi
239 ;;
240 esac
241
242 if [ -n "$uids" ]
243 then
244 if [ "$raw" = true ]
245 then
246 echo "$uids"
247 else
248 flt=$( echo "$uids" | sed -e 's/^/ <Id>/' -e 's/$/<\/Id>/' )
249 num=$( echo "$uids" | wc -l | tr -d ' ' )
250 echo "<ENTREZ_DIRECT>"
251 if [ -n "$dbase" ]
252 then
253 echo " <Db>${dbase}</Db>"
254 fi
255 if [ -n "$num" ]
256 then
257 echo " <Count>${num}</Count>"
258 fi
259 if [ -n "$flt" ]
260 then
261 echo "$flt"
262 fi
263 echo " <Source>Local</Source>"
264 echo "</ENTREZ_DIRECT>"
265 fi
266 fi
267
268 exit 0