Module: PubMed

Defined in:
lib/rbbt/sources/pubmed.rb

Overview

This module offers an interface with PubMed, to perform queries, and retrieve simple information from articles. It uses the caching services of Rbbt.

Defined Under Namespace

Classes: Article

Constant Summary collapse

@@pubmed_lag =
1

Class Method Summary collapse

Class Method Details

.get_article(pmids) ⇒ Object

Returns the Article object containing the information for the PubMed ID specified as an argument. If pmid is an array instead of a single identifier it returns an hash with the Article object for each id. It uses the Rbbt cache to save the articles xml.



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
# File 'lib/rbbt/sources/pubmed.rb', line 180

def self.get_article(pmid)

  if pmid.is_a? Array
    missing = []
    list = {}

    pmid.each{|p|
      filename = p.to_s + '.xml'
      if File.exists? FileCache.path(filename)
        list[p] = Article.new(Open.read(FileCache.path(filename)))
      else
        missing << p
      end
    }

    return list unless missing.any?

    articles = get_online(missing)

    articles.each{|p, xml|
      filename = p + '.xml'
      FileCache.add(filename,xml)
      list[p] =  Article.new(xml)
    }

    return list

  else
    filename = pmid.to_s + '.xml'

    if File.exists? FileCache.path(filename)
      return Article.new(Open.read(FileCache.path(filename)))
    else
      xml = get_online(pmid)
      FileCache.add(filename,xml)

      return Article.new(xml)
    end
  end
end

.query(query, retmax = nil) ⇒ Object

Performs the specified query and returns an array with the PubMed Ids returned. retmax can be used to limit the number of ids returned, if is not specified 30000 is used.



16
17
18
19
20
# File 'lib/rbbt/sources/pubmed.rb', line 16

def self.query(query, retmax=nil)
  retmax ||= 30000

  Open.read("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?retmax=#{retmax}&db=pubmed&term=#{query}",:quiet => true, :nocache => true).scan(/<Id>(\d+)<\/Id>/).flatten
end