Class: Bio::NCBI::REST

Inherits:
Object show all
Defined in:
lib/bio/io/ncbirest.rb

Direct Known Subclasses

PubMed

Constant Summary collapse

NCBI_INTERVAL =

Make no more than one request every 3 seconds.

3
@@last_access =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.efetch(*args) ⇒ Object



148
149
150
# File 'lib/bio/io/ncbirest.rb', line 148

def self.efetch(*args)
  self.new.efetch(*args)
end

.esearch(*args) ⇒ Object



144
145
146
# File 'lib/bio/io/ncbirest.rb', line 144

def self.esearch(*args)
  self.new.esearch(*args)
end

Instance Method Details

#efetch(ids, hash = {}) ⇒ Object

Retrieve a database entry by given ID and using E-Utils (efetch) and returns an array of entry string. Multiple IDs can be supplied.

For information on the possible arguments, see


Arguments:

  • ids: list of NCBI entry IDs (required)

  • hash: hash of E-Utils option => “nuccore”, “rettype” => “gb”

    • db: “nuccore”, “nucleotide”, “protein”, “pubmed”, …

    • retmode: “text”, “xml”, “html”, …

    • rettype: “gb”, “medline”, “count”,…

    • retmax: integer (default 100)

    • retstart: integer

    • field

    • reldate

    • mindate

    • maxdate

    • datetype

Returns

Array of entry String



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bio/io/ncbirest.rb', line 122

def efetch(ids, hash = {})
  return nil if ids.to_s.empty?
  ids = ids.join(",") if ids === Array

  serv = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
  opts = {
    "tool"     => "bioruby",
    "retmode"  => "text",
    "id"       => ids,
  }
  opts.update(hash)

  ncbi_access_wait

  response, = Bio::Command.post_form(serv, opts)
  result = response.body
  if opts["retmode"] == "text"
    result = result.split(/\n\n+/)
  end
  return result
end

#esearch(str, hash = {}) ⇒ Object

Search the NCBI database by given keywords using E-Utils and returns an array of entry IDs.

For information on the possible arguments, see


Arguments:

  • str: query string (required)

  • hash: hash of E-Utils option => “nuccore”, “rettype” => “gb”

    • db: “nuccore”, “nucleotide”, “protein”, “pubmed”, …

    • retmode: “text”, “xml”, “html”, …

    • rettype: “gb”, “medline”, “count”, …

    • retmax: integer (default 100)

    • retstart: integer

    • field

    • reldate

    • mindate

    • maxdate

    • datetype

Returns

array of entry IDs or a number of results



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bio/io/ncbirest.rb', line 77

def esearch(str, hash = {})
  return nil if str.empty?

  serv = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
  opts = {
    "retmax" => 100,
    "tool"   => "bioruby",
    "term"   => str
  }
  opts.update(hash)

  ncbi_access_wait

  response, = Bio::Command.post_form(serv, opts)
  result = response.body
  if opts['rettype'] == 'count'
    result = result.scan(/<Count>(.*?)<\/Count>/m).flatten.first.to_i
  else
    result = result.scan(/<Id>(.*?)<\/Id>/m).flatten
  end
  return result
end