Module: Hookkaido::Sources::OLS

Defined in:
lib/hookkaido/sources/ols.rb

Constant Summary collapse

BASE =
'https://www.ebi.ac.uk/ols4/api'

Class Method Summary collapse

Class Method Details

.ontologies(verbose:, timeout:) ⇒ Object

Fetch OLS ontology IDs (lowercased)



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
# File 'lib/hookkaido/sources/ols.rb', line 8

def self.ontologies(verbose:, timeout:)
  json = Hookkaido::Request
    .new(url: BASE, verbose: verbose, timeout: timeout)
    .perform('ontologies', params: { size: 500 }, method: :get)

  arr = []
  if json.is_a?(Hash)
    emb = json['_embedded'] && json['_embedded']['ontologies']
    if emb.is_a?(Array)
      emb.each do |o|
        oid = (o['config'] && o['config']['id'])&.to_s&.downcase || ''
        title = (o['config'] && o['config']['title']) || ''
        description = (o['config'] && o['config']['description']) || ''
        arr << { oid:, title:, description: }
      end
    end
  elsif json.is_a?(Array)
    json.each do |o|
      oid = (o['config'] && o['config']['id'])&.to_s&.downcase || ''
      title = (o['config'] && o['config']['title']) || ''
      description = (o['config'] && o['config']['description']) || ''
      arr << { oid:, title:, description: }
    end
  end

  arr.uniq
end

.search(term, ontologies:, rows:, start:, verbose:, timeout:) ⇒ Object

Single-call search across up to 3 ontologies; returns results + combined total



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hookkaido/sources/ols.rb', line 37

def self.search(term, ontologies:, rows:, start:, verbose:, timeout:)
  onts = Array(ontologies).map { |x| x.to_s.downcase }.uniq
  params = {
    q: term,
    type: 'class',
    ontology: onts.join(','), # multi-ontology in one call
    queryFields: 'label,synonym',
    fieldList: 'iri,label,description,ontology_prefix',
    rows: rows,
    start: start
  }

  json = Hookkaido::Request
           .new(url: BASE, verbose: verbose, timeout: timeout)
           .perform('search', params: params, method: :get)

  resp  = json && json['response']
  docs  = (resp && resp['docs']) || []
  total = (resp && resp['numFound']) || 0

  results = docs.map { |doc| normalize_doc(doc) }.compact
  { results: results, total: total.to_i }
end