Debian Codegraph @

cit2pmid

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 # sample query:
7
8 # cit2pmid -local \
9 # -title "nucleotide sequences required for tn3 transposition immunity" \
10 # -author "Kans JA" -author "Casadaban MJ" \
11 # -journal "J Bacteriol" -year 1989 -volume 171 -issue 4 -page 1904-14
12
13 pth=$( dirname "$0" )
14
15 case "$pth" in
16 /* )
17 ;; # already absolute
18 * )
19 pth=$(cd "$pth" && pwd)
20 ;;
21 esac
22
23 case ":$PATH:" in
24 *:"$pth":* )
25 ;;
26 * )
27 PATH="$PATH:$pth"
28 export PATH
29 ;;
30 esac
31
32 # handle common flags - dot command is equivalent of "source"
33
34 if [ ! -f "$pth"/xcommon.sh ]
35 then
36 echo "ERROR: Unable to find '$pth/xcommon.sh' file" >&2
37 exit 1
38 fi
39
40 . "$pth"/xcommon.sh
41
42 # initialize specific flags
43
44 debug=false
45 strict=false
46 mode="remote"
47
48 # read mode flags
49
50 while [ $# -gt 0 ]
51 do
52 case "$1" in
53 -debug )
54 debug=true
55 shift
56 ;;
57 -strict )
58 strict=true
59 shift
60 ;;
61 -remote )
62 # use nquire -citmatch remote service
63 mode="remote"
64 shift
65 ;;
66 -eutils )
67 # use EDirect call to esearch
68 mode="eutils"
69 shift
70 ;;
71 -local )
72 # use transmute -r2p for relaxed search with local archive
73 mode="local"
74 shift
75 ;;
76 -exact )
77 # use xsearch for strict matching with local archive
78 mode="exact"
79 shift
80 ;;
81 -verify )
82 # first calls -local, then calls -remote to confirm candidates
83 mode="verify"
84 shift
85 ;;
86 * )
87 # break without shift to get data arguments
88 break
89 ;;
90 esac
91 done
92
93 if [ "$mode" = "local" ] || [ "$mode" = "exact" ]
94 then
95 FindPostingsFolder
96 fi
97
98 # initialize citation fields
99
100 titl=""
101 faut=""
102 laut=""
103 jour=""
104 year=""
105 volu=""
106 issu=""
107 page=""
108
109 # -asn reads CPub ASN.1
110 if [ $# -eq 2 ] && [ "$1" = "-asn" ]
111 then
112 asn="$2"
113 shift
114 shift
115 if [ "$asn" = "-" ]
116 then
117 # read from stdin
118 asn=$( cat )
119 fi
120 vals=$(
121 echo "$asn" |
122 xtract -rec Rec -pattern article -pkg CIT \
123 -group article/title -wrp "Title" -element name \
124 -group article/authors/names \
125 -block name/name -position first \
126 -wrp "Faut" -sep " " -auth "last,initials" \
127 -block name/name -position last \
128 -wrp "Laut" -sep " " -auth "last,initials" \
129 -group article/from/journal \
130 -block title/name -wrp "Journal" -element name \
131 -block imp/date -wrp "Year" -year "std/*" \
132 -block imp/volume -wrp "Volume" -element volume \
133 -block imp/issue -wrp "Issue" -element issue \
134 -block imp/pages -wrp "Pages" -page pages |
135 transmute -format
136 )
137
138 ParseMessage "$vals" CIT \
139 titl Title faut Faut laut Laut jour Journal \
140 year Year volu Volume issu Issue page Pages
141 fi
142
143 # -cit reads CITATION XML
144 if [ $# -eq 2 ] && [ "$1" = "-cit" ]
145 then
146 cit="$2"
147 shift
148 shift
149 if [ "$cit" = "-" ]
150 then
151 # read from stdin
152 cit=$( cat )
153 fi
154 vals=$(
155 echo "$cit" |
156 xtract -rec CIT -pattern CITATION \
157 -wrp "Title" -element TITL \
158 -wrp "First" -element FAUT \
159 -wrp "Last" -element LAUT \
160 -wrp "Journal" -element JOUR \
161 -wrp "Year" -element YEAR \
162 -wrp "Volume" -element VOL \
163 -wrp "Issue" -element ISS \
164 -wrp "Pages" -page PAGE |
165 transmute -format
166 )
167
168 ParseMessage "$vals" CIT \
169 titl Title faut First laut Last jour Journal \
170 year Year volu Volume issu Issue page Pages
171 fi
172
173 # read individual field and value arguments
174
175 isAtStart=yes
176 while [ $# -gt 0 ]
177 do
178 typ="$1"
179
180 case "$typ:$isAtStart" in
181 -* )
182 ;;
183 *:yes )
184 echo "ERROR: Missing type for $typ" >&2
185 exit 1
186 ;;
187 * )
188 echo "ERROR: Missing type for $typ - please remember to quote multi-word values" >&2
189 exit 1
190 ;;
191 esac
192 isAtStart=no
193
194 shift
195 if [ $# -lt 1 ]
196 then
197 echo "ERROR: Missing ${typ} argument" >&2
198 exit 1
199 fi
200 # trim flanking whitespace, compress internal runs of whitespace
201 tag=$( echo "$1" | awk '{$1=$1;print}' )
202 shift
203
204 case "${typ}" in
205 -title | -TITL )
206 # incomplete title may need other fields to disambiguate
207 titl="${tag}"
208 ;;
209 -author )
210 # can add first and last authors in separate arguments
211 auth="${tag}"
212 if [ -z "$faut" ]
213 then
214 faut="${auth}"
215 else
216 laut="${auth}"
217 fi
218 ;;
219 -FAUT )
220 faut="${tag}"
221 ;;
222 -LAUT )
223 laut="${tag}"
224 ;;
225 -journal | -JOUR )
226 jour="${tag}"
227 ;;
228 -year | -YEAR | -PDAT )
229 year="${tag}"
230 ;;
231 -volume | -VOL )
232 volu="${tag}"
233 ;;
234 -issue | -ISS )
235 issu="${tag}"
236 ;;
237 -page | -pages | -PAGE )
238 # only keep first page
239 page=$( echo "${tag}" | sed -e 's/-.*//' )
240 ;;
241 * )
242 echo "ERROR: Unrecognized command '${typ}'" >&2
243 exit 1
244 ;;
245 esac
246 done
247
248 # clean up parentheses
249 titl=$( echo "$titl" | tr -d '(' | tr -d ')' | tr -d '.' )
250 jour=$( echo "$jour" | tr -d '(' | tr -d ')' | tr -d '.' )
251
252 # echo "titl $titl"
253 # echo "faut $faut"
254 # echo "laut $laut"
255 # echo "jour $jour"
256 # echo "year $year"
257 # echo "volu $volu"
258 # echo "issu $issu"
259 # echo "page $page"
260
261 # initialize query variable
262
263 query=""
264
265 CleanAuthor() {
266
267 athr=$( echo "$1" | tr 'A-Z' 'a-z' )
268 lins=$( echo "$athr" | wc -w )
269 if [ $lins -gt 2 ]
270 then
271 fst=$( echo "$athr" | cut -d ' ' -f 1 )
272 scd=$( echo "$athr" | cut -d ' ' -f 2 )
273 thd=$( echo "$athr" | cut -d ' ' -f 3 )
274 if [ "$thd" = "jr" ] || [ "$thd" = "sr" ]
275 then
276 athr=$( echo "${fst} ${scd}" )
277 fi
278 fi
279 lins=$( echo "$athr" | wc -w )
280 if [ $lins -eq 2 ]
281 then
282 fst=$( echo "$athr" | cut -d ' ' -f 1 )
283 scd=$( echo "$athr" | cut -d ' ' -f 2 )
284 scd=${scd:0:1}
285 athr=$( echo "${fst} ${scd}" )
286 fi
287 echo "$athr"
288 }
289
290 if [ "$mode" = "eutils" ]
291 then
292 titl=$( echo "${titl}" | sed -e 's/[^A-Za-z0-9]/ /g' | tr -s ' ' )
293 faut=$( CleanAuthor "$faut" )
294 laut=$( CleanAuthor "$laut" )
295 fi
296
297 # query construction function
298
299 AppendOneArg() {
300
301 qry="$1"
302 arg="$2"
303 fld="$3"
304
305 sep=""
306 spc=""
307 if [ -n "$qry" ]
308 then
309 sep=" AND "
310 spc=" "
311 fi
312
313 if [ -n "$arg" ]
314 then
315 case "$mode" in
316 remote )
317 qry=$( echo "${qry}${spc}${arg}" )
318 ;;
319 eutils )
320 qry=$( echo "${qry}${sep}${arg} [${fld}]" )
321 ;;
322 local )
323 qry=$( echo "${qry}<${fld}>${arg}</${fld}>" )
324 ;;
325 exact )
326 qry=$( echo "${qry}${sep}${arg} [${fld}]" )
327 ;;
328 verify )
329 qry=$( echo "${qry} -${fld} \"${arg}\"" )
330 ;;
331 esac
332 fi
333
334 echo "$qry"
335 }
336
337 # citation fields appended in fixed order after title and authors
338
339 if [ "$mode" = "local" ]
340 then
341 query="<CITATION>"
342 fi
343
344 # add in order of GenBank format REFERENCE for citmatch expectation
345
346 if [ "$mode" != "eutils" ]
347 then
348 query=$( AppendOneArg "$query" "${faut}" "FAUT" )
349 query=$( AppendOneArg "$query" "${laut}" "LAUT" )
350 else
351 query=$( echo "${query}(${faut} [FAUT] OR ${laut} [LAUT])" )
352 fi
353 if [ "$mode" != "eutils" ]
354 then
355 query=$( AppendOneArg "$query" "${titl}" "TITL" )
356 fi
357 query=$( AppendOneArg "$query" "${jour}" "JOUR" )
358 query=$( AppendOneArg "$query" "${volu}" "VOL" )
359 query=$( AppendOneArg "$query" "${issu}" "ISS" )
360 query=$( AppendOneArg "$query" "${page}" "PAGE" )
361 if [ "$mode" = "eutils" ]
362 then
363 query=$( AppendOneArg "$query" "${year}" "PDAT" )
364 else
365 query=$( AppendOneArg "$query" "${year}" "YEAR" )
366 fi
367
368 if [ "$mode" = "local" ]
369 then
370 query=$( echo "$query</CITATION>" )
371 fi
372
373 # execute search, print matched PMIDs
374
375 result=""
376
377 case "$mode" in
378 remote )
379 if [ "$debug" = true ]
380 then
381 echo ""
382 echo "nquire -citmatch \"${query}\""
383 echo ""
384 fi
385 result=$(
386 nquire -citmatch "${query}" |
387 xtract -pattern opt -sep "\n" -element uids/pubmed |
388 head -n 20
389 )
390 ;;
391 eutils )
392 if [ "$debug" = true ]
393 then
394 echo ""
395 echo "QURY ${query}"
396 echo "TITL ${titl}"
397 echo ""
398 fi
399 result=$( esearch -db pubmed -query "${query}" -title "${titl}" < /dev/null |
400 efetch -format uid )
401 ;;
402 local )
403 if [ "$debug" = true ]
404 then
405 echo "${query}"
406 if [ "$strict" = true ]
407 then
408 result=$( echo "${query}" | ref2pmid -options strict debug )
409 else
410 result=$( echo "${query}" | ref2pmid -options debug )
411 fi
412 elif [ "$strict" = true ]
413 then
414 result=$( echo "${query}" | ref2pmid -options strict |
415 xtract -mixed -pattern CITATION -sep "\n" -element PMID )
416 else
417 result=$( echo "${query}" | ref2pmid |
418 xtract -mixed -pattern CITATION -sep "\n" -element PMID )
419 fi
420 ;;
421 exact )
422 if [ "$debug" = true ]
423 then
424 echo "${query}"
425 fi
426 result=$( xsearch -query "${query}" | head -n 20 )
427 ;;
428 verify )
429 if [ "$debug" = true ]
430 then
431 echo "${query}"
432 fi
433 # first query against local archive with strict matching
434 first=$( cit2pmid -local -strict \
435 "-FAUT" "${faut}" "-LAUT" "${laut}" \
436 "-TITL" "${titl}" \
437 "-JOUR" "${jour}" "-VOL" "${volu}" \
438 "-ISS" "${issu}" "-PAGE" "${page}" \
439 "-YEAR" "${year}" )
440 if [ -n "$first" ]
441 then
442 # then confirm positive candidates with call to citmatch
443 second=$( cit2pmid -remote \
444 "-FAUT" "${faut}" "-LAUT" "${laut}" \
445 "-TITL" "${titl}" \
446 "-JOUR" "${jour}" "-VOL" "${volu}" \
447 "-ISS" "${issu}" "-PAGE" "${page}" \
448 "-YEAR" "${year}" )
449 if [ -n "$second" ] && [ "$first" = "$second" ]
450 then
451 result=$( echo "$second" )
452 fi
453 fi
454 ;;
455 esac
456
457 # only print results if 5 or fewer candidate matches
458
459 num=$( echo "$result" | wc -l )
460 if [ $num -lt 6 ]
461 then
462 echo "$result"
463 fi