/
br_pmfetch.rb
executable file
·422 lines (320 loc) · 9.87 KB
/
br_pmfetch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env ruby
#
# = pmfetch - PubMed client
#
# Copyright:: Copyright (C) 2004, 2005
# Toshiaki Katayama <k@bioruby.org>
# License:: The Ruby License
#
# $Id:$
#
require 'bio'
PROG_VER = "Powered by BioRuby #{Bio::BIORUBY_VERSION_ID}"
PROG_NAME = File.basename($0)
require 'getoptlong'
### formatting
class String
def fill(fill_column = 80, prefix = '', separater = ' ')
prefix = ' ' * prefix if prefix.is_a?(Integer)
maxlen = fill_column - prefix.length
raise "prefix is longer than fill_column" if maxlen <= 0
cursor = pos = 0
lines = []
while cursor < self.length
line = self[cursor, maxlen]
pos = line.rindex(separater)
pos = nil if line.length < maxlen
if pos
len = pos + separater.length
lines << self[cursor, len]
cursor += len
else
lines << self[cursor, maxlen]
cursor += maxlen
end
end
return lines.join("\n#{prefix}")
end
end
module Bio
class Reference
def report
if (num = @authors.size) > 10
authors = "#{@authors[0]} et al. (#{num} authors)"
elsif num > 4
sep = ',' * (num - 1)
authors = "#{@authors[0]}#{sep} #{@authors[-1]}"
else
authors = authors_join(' & ')
end
journal = "#{@journal} #{@year} #{@volume}(#{@issue}):#{@pages}"
indent = 8
prefix = ' ' * indent
[
"#{@pages[/\d+/]}".ljust(indent) + "#{@title}".fill(78, indent),
authors,
"#{journal} [PMID:#{@pubmed}]",
].join("\n#{prefix}")
end
end
end
class PMFetch
class Examples < StandardError; end
class Version < StandardError; end
class Usage < StandardError; end
### default options
def initialize
@format = 'rd'
@search_opts = {
'retmax' => 20,
}
@query = nil
@query_opts = []
@pmid_list_only = false
pmfetch
end
### main
def pmfetch
begin
set_options
parse_options
check_query
rescue PMFetch::Examples
puts examples
exit
rescue PMFetch::Version
puts version
exit
rescue PMFetch::Usage
puts usage
exit
rescue GetoptLong::MissingArgument, GetoptLong::InvalidOption
puts usage
exit
end
list = pm_esearch
if list.empty?
;
elsif @pmid_list_only
puts list
else
pm_efetch(list)
end
end
### help
def usage
%Q[
Usage: #{PROG_NAME} [options...] "query string"
or #{PROG_NAME} --query "query string" [other options...]
Options:
-q --query "genome AND virus" Query string for PubMed search
-t --title "mobile elements" Title of the article to search
-j --journal "genome res" Journal title to search
-v --volume # Journal volume to search
-i --issue # Journal issue to search
-p --page # First page number of the article to search
-a --author "Altschul SF" Author name to search
-m --mesh "SARS virus" MeSH term to search
-f --format bibtex Summary output format
--pmidlist Output only a list of PubMed IDs
-n --retmax # Number of articles to retrieve at the maximum
-N --retstart # Starting number of the articles to retrieve
-s --sort pub+date Sort method for the summary output
--reldate # Search articles published within recent # days
--mindate YYYY/MM/DD Search articles published after the date
--maxdate YYYY/MM/DD Search articles published before the date
--help Output this help, then exit
--examples Output examples, then exit
--version Output version number, then exit
Formats:
endnote, medline, bibitem, bibtex, report, rd,
nature, science, genome_res, genome_biol, nar, current, trends, cell
Sort:
author, journal, pub+date, page
See the following pages for the PubMed search options:
http://www.ncbi.nlm.nih.gov/entrez/query/static/help/pmhelp.html
http://www.ncbi.nlm.nih.gov/entrez/query/static/esearch_help.html
#{version}
]
end
def version
PROG_VER
end
def examples
DATA.read.gsub('PMFetch', PROG_NAME)
end
private
### options
def set_options
@parser = GetoptLong.new
@parser.set_options(
[ '--query', '-q', GetoptLong::REQUIRED_ARGUMENT ],
[ '--title', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--journal', '-j', GetoptLong::REQUIRED_ARGUMENT ],
[ '--volume', '-v', GetoptLong::REQUIRED_ARGUMENT ],
[ '--issue', '-i', GetoptLong::REQUIRED_ARGUMENT ],
[ '--page', '-p', GetoptLong::REQUIRED_ARGUMENT ],
[ '--author', '-a', GetoptLong::REQUIRED_ARGUMENT ],
[ '--mesh', '-m', GetoptLong::REQUIRED_ARGUMENT ],
[ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
[ '--pmidlist', GetoptLong::NO_ARGUMENT ],
[ '--retmax', '-n', GetoptLong::REQUIRED_ARGUMENT ],
[ '--retstart', '-N', GetoptLong::REQUIRED_ARGUMENT ],
[ '--sort', '-s', GetoptLong::REQUIRED_ARGUMENT ],
[ '--reldate', GetoptLong::REQUIRED_ARGUMENT ],
[ '--mindate', GetoptLong::REQUIRED_ARGUMENT ],
[ '--maxdate', GetoptLong::REQUIRED_ARGUMENT ],
[ '--examples', GetoptLong::NO_ARGUMENT ],
[ '--help', GetoptLong::NO_ARGUMENT ],
[ '--version', GetoptLong::NO_ARGUMENT ]
)
end
def parse_options
@parser.each_option do |optname, optarg|
case optname
when /--query/
@query = optarg
when /--title/
@query_opts << "#{optarg}[ti]"
when /--journal/
@query_opts << "#{optarg}[ta]"
when /--volume/
@query_opts << "#{optarg}[vi]"
when /--issue/
@query_opts << "#{optarg}[ip]"
when /--page/
@query_opts << "#{optarg}[pg]"
when /--author/
@query_opts << "#{optarg}[au]"
when /--mesh/
@query_opts << "#{optarg}[mh]"
when /--format/
@format = optarg
when /--pmidlist/
@pmid_list_only = true
when /--examples/
raise PMFetch::Examples
when /--help/
raise PMFetch::Usage
when /--version/
raise PMFetch::Version
when /--sort/
@sort = optarg
@search_opts["sort"] = @sort unless @sort == "page"
else
optname.delete!('-')
@search_opts[optname] = optarg
end
end
end
### check query
def check_query
p @query if $DEBUG
@query ||= ARGV.join(" ") unless ARGV.empty?
p @query if $DEBUG
@query_str = [ @query, @query_opts ].flatten.compact.join(" AND ")
p @query_str if $DEBUG
if @query_str.empty?
raise PMFetch::Usage
end
end
### search
def pm_esearch
return Bio::PubMed.esearch(@query_str, @search_opts)
end
def pm_efetch(list)
entries = Bio::PubMed.efetch(list)
if @format == 'medline'
medline_format(entries)
else
entries = parse_entries(entries)
if @sort == 'page'
entries = sort_entries(entries)
end
if @format == 'report'
report_format(entries)
else
other_format(entries)
end
end
end
### output
def medline_format(entries)
entries.each do |entry|
puts entry
puts '//'
end
end
def parse_entries(entries)
entries.map { |entry| Bio::MEDLINE.new(entry) }
end
def sort_entries(entries)
if RUBY_VERSION > "1.8.0"
entries.sort_by { |x|
[ x.journal, x.volume.to_i, x.issue.to_i, x.pages.to_i ]
}
else
entries.map { |x|
[ x.journal, x.volume.to_i, x.issue.to_i, x.pages.to_i, x ]
}.sort { |a, b|
a[0..3] <=> b[0..3]
}.map { |y|
y.pop
}
end
end
def report_format(entries)
entries.each do |entry|
puts entry.reference.report
puts
end
end
def other_format(entries)
entries.each do |entry|
puts entry.reference.format(@format)
puts
end
end
end
PMFetch.new
__END__
= Examples : PubMed search
These four lines will do the same job.
% PMFetch transcription factor
% PMFetch "transcription factor"
% PMFetch --query "transcription factor"
% PMFetch -q "transcription factor"
Retrieve max 100 artiecles (20 is a NCBI's default) at a time, use --retmax as
% PMFetch -q "transcription factor" --retmax 100
and, to retrieve next 100 articles, use --retstart as
% PMFetch -q "transcription factor" --retmax 100 --retstart 100
You can narrow the search target for an issue of the journal.
% PMFetch --journal development --volume 131 --issue 3 transcription factor
Short options are also available.
% PMFetch -j development -v 131 -i 3 transcription factor
Search articles indexed in PubMed within these 90 days.
% PMFetch -q "transcription factor" --reldate 90
Search articles indexed in PubMed during the period of 2001/04/01 to 2001/08/31
% PMFetch -q "transcription factor" --mindate 2001/04/01 --maxdate 2001/08/31
Output format can be changed by --format option.
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f report
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f rd
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f endnote
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f medline
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f bibitem
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f bibtex
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f nature
% PMFetch -q "transcription factor" -j development -v 131 -i 3 -f science
Generate title listings for the journal report meeting (don't forget
to inclease the number of --retmax for fetching all titles).
% PMFetch -f report -j development -v 131 -i 3 -n 100
Search by author name.
% PMFetch -a "Karlin S"
% PMFetch -a "Koonin EV"
Search by MeSH term.
% PMFetch -m "computational biology"
% PMFetch -m "SARS virus"
Search by PubMed ID (PMID).
% PMFetch 12345
Output PMID only.
% PMFetch --pmidlist tardigrada