hookkaido 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43fa5c7c35cc9356aa25ab096a92b9ac009ab261619ea5362296685b10aeca79
4
+ data.tar.gz: cfd220e1c1cad4596ac1af84b1b2e96ee710a6d9adb401ce9a28a7c44ccba8ad
5
+ SHA512:
6
+ metadata.gz: 1065670322b34a7f0bc5dae369016d75b1c885933435ebf7ff577a33462d5305f028823423a524dbdca565d8948c4cc8f7b6e11f40564be1e2776d127471d3c8
7
+ data.tar.gz: fff89ee6e896e1e1c8d60bd90e061ec08bfee0e89938ee664f34760f9e87a5e017a2ba151176854346de830a2ebbbb5bdce6073dccfae8f75224b8980b991618
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025 Species File Group
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # Hookkaido
2
+
3
+ Hookkaido is a Ruby wrapper on the [OLS](https://www.ebi.ac.uk/ols4/) API. Code follows the spirit/approach of the Gem [serrano](https://github.com/sckott/serrano), and indeed much of the wrapping utility is copied 1:1 from that repo, thanks [@sckott](https://github.com/sckott).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hookkaido'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hookkaido
20
+
21
+ ## Quick start
22
+
23
+ Search for ontology terms from the (default) Uberon ontology matching 'femur':
24
+ ```ruby
25
+ bin/console
26
+ h = Hookkaido.search('femur')
27
+ h.keys
28
+ => [:results, :page, :pagesize, :total]
29
+
30
+ h[:total]
31
+ => 32
32
+ h[:results].first
33
+ => {iri: "http://purl.obolibrary.org/obo/UBERON_0015052", label: "femur endochondral element", ontology_prefix: "uberon", description: "A femur bone or its cartilage or pre-cartilage precursor."}
34
+ ```
35
+
36
+ Uberon aggregates results from many ontologies; you're likely to get more targeted results by specifying an ontology more specific to your use case, such as HAO (Hymenopteran Anatomy Ontology) for example:
37
+ ```ruby
38
+ h = Hookkaido.search('femur', ontologies: 'hao')
39
+ h[:total]
40
+ => 10
41
+ h[:results].map{ |r| [r[:label], r[:description], r[:iri]] }
42
+ =>
43
+ [["mesofemur", "The femur that is located on the mid leg.", "http://purl.obolibrary.org/obo/HAO_0001131"],
44
+ ["metafemur", "The femur that is located on the hind leg.", "http://purl.obolibrary.org/obo/HAO_0001140"],
45
+ ["profemur", "The femur that is located on the fore leg.", "http://purl.obolibrary.org/obo/HAO_0001124"],
46
+ ["femur", "The leg segment that is distal to the trochanter and proximal to the tibia.", "http://purl.obolibrary.org/obo/HAO_0000327"],
47
+ ["trochantellus", "The area that is located proximally on the femur and is delimited by a groove.", "http://purl.obolibrary.org/obo/HAO_0001033"],
48
+ ["protrochantero-profemoral muscle",
49
+ "The intrinsic leg muscle that arises anteriorly from the wall of the protrochanter and inserts on the proximal profemoral apodeme.",
50
+ "http://purl.obolibrary.org/obo/HAO_0001238"],
51
+ ["mesotrochantero-mesofemoral muscle",
52
+ "The intrinsic leg muscle that arises anteriorly from the wall of the mesotrochanter and inserts on the proximal mesofemoral apodeme.",
53
+ "http://purl.obolibrary.org/obo/HAO_0001261"],
54
+ ["metatrochantero-metafemoral muscle",
55
+ "The intrinsic leg muscle that arises anteriorly from the wall of the metatrochanter and inserts on the proximal metafemoral apodeme.",
56
+ "http://purl.obolibrary.org/obo/HAO_0001280"],
57
+ ["tibial fossa of the femur", "The fossa that is located distally on the femur and accommodates the femoral condyle of the tibia.", "http://purl.obolibrary.org/obo/HAO_0001207"],
58
+ ["sturdy spines of the hind femur", "The spine that is located on the ventral face of the metafemur.", "http://purl.obolibrary.org/obo/HAO_0002491"]]
59
+ ```
60
+
61
+ Search more than one specific ontology - *at most 3 may be provided*, if none are provided then the default Uberon is searched.
62
+ ```ruby
63
+ Hookkaido.search('femur', ontologies: ['hao', 'lepao'])
64
+ ```
65
+ Available ontology identifiers can be found at https://www.ebi.ac.uk/ols4/ontologies, or you can return the list (with much less data per ontology) using Hookkaido:
66
+ ```ruby
67
+ a = Hookkaido.ontologies
68
+
69
+ a.first
70
+ =>
71
+ {oid: "ado",
72
+ title: "Alzheimer's Disease Ontology (ADO)",
73
+ description:
74
+ "Alzheimer's Disease Ontology is a knowledge-based ontology that encompasses varieties of concepts related to Alzheimer'S Disease, foundamentally structured by upper level Basic Formal Ontology(BFO). This Ontology is enriched by the interrelational entities that demonstrate the nextwork of the understanding on Alzheimer's disease and can be readily applied for text mining."}
75
+ ```
76
+
77
+ ### Pagination
78
+
79
+ For pagination in `search`, use the `page` and `per` parameters:
80
+ ```ruby
81
+ h = Hookkaido.search('head', per: 50, page: 1)
82
+
83
+ [h[:page], h[:per], h[:total]]
84
+ => [1, 50, 143]
85
+ ```
86
+
87
+ The `ontologies` endpoint isn't paged - at time of writing it will return ~285 ontologies.
88
+
89
+ ---
90
+
91
+ ## Development
92
+
93
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
94
+
95
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, update the `CHANGELOG.md`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
96
+
97
+ ## Contributing
98
+
99
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SpeciesFileGroup/hookkaido. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/SpeciesFileGroup/hookkaido/blob/main/CODE_OF_CONDUCT.md).
100
+
101
+ ## License
102
+
103
+ The gem is available as open source under the terms of the [MIT license](https://github.com/SpeciesFileGroup/hookkaido/blob/main/LICENSE.txt). You can learn more about the MIT license on [Wikipedia](https://en.wikipedia.org/wiki/MIT_License) and compare it with other open source licenses at the [Open Source Initiative](https://opensource.org/license/mit/).
104
+
105
+ ## Code of Conduct
106
+
107
+ Everyone interacting in the Hookkaido project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/SpeciesFileGroup/hookkaido/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ module Hookkaido
3
+ class Error < StandardError; end
4
+ class BadRequest < Error; end
5
+ class NotFound < Error; end
6
+ class InternalServerError < Error; end
7
+ class BadGateway < Error; end
8
+ class ServiceUnavailable < Error; end
9
+ class GatewayTimeout < Error; end
10
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require 'faraday'
3
+ require 'multi_json'
4
+ module Faraday
5
+ module TwOntologyErrors
6
+ class Middleware < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env).on_complete do |response|
9
+ case response[:status].to_i
10
+ when 400 then raise Hookkaido::BadRequest, compose(response)
11
+ when 404 then raise Hookkaido::NotFound, compose(response)
12
+ when 500 then raise Hookkaido::InternalServerError, compose(response)
13
+ when 502 then raise Hookkaido::BadGateway, compose(response)
14
+ when 503 then raise Hookkaido::ServiceUnavailable, compose(response)
15
+ when 504 then raise Hookkaido::GatewayTimeout, compose(response)
16
+ end
17
+ end
18
+ end
19
+ private
20
+ def compose(response)
21
+ body = begin
22
+ MultiJson.load(response[:body]) if response[:body].to_s.strip.start_with?('{', '[')
23
+ rescue MultiJson::ParseError
24
+ nil
25
+ end
26
+ "#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status], body].compact.join(' ')}"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ module Configuration
3
+ def configuration
4
+ yield self
5
+ end
6
+ def define_setting(name, default = nil)
7
+ class_variable_set("@@#{name}", default)
8
+ define_class_method "#{name}=" do |value|
9
+ class_variable_set("@@#{name}", value)
10
+ end
11
+ define_class_method name do
12
+ class_variable_get("@@#{name}")
13
+ end
14
+ end
15
+ private
16
+ def define_class_method(name, &block)
17
+ (class << self; self; end).instance_eval { define_method name, &block }
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'faraday'
3
+ require 'faraday/follow_redirects'
4
+ require 'multi_json'
5
+ require_relative 'utils'
6
+
7
+ module Hookkaido
8
+ class Request
9
+ def initialize(url:, verbose: false, headers: {}, timeout: 10)
10
+ @url = url
11
+ @verbose = verbose
12
+ @headers = headers
13
+ @timeout = timeout
14
+ end
15
+
16
+ def perform(endpoint, params: {}, method: :get)
17
+ Faraday::Utils.default_space_encoding = '+'
18
+ conn = Faraday.new(url: @url) do |f|
19
+ f.request :url_encoded
20
+ f.response :follow_redirects
21
+ f.response :logger if @verbose
22
+ f.options.timeout = @timeout
23
+ f.use Faraday::TwOntologyErrors::Middleware
24
+ f.adapter Faraday.default_adapter
25
+ end
26
+
27
+ conn.headers['Accept'] = 'application/json,*/*'
28
+ conn.headers[:user_agent] = make_user_agent
29
+ conn.headers['X-USER-AGENT'] = make_user_agent
30
+ @headers.each { |k, v| conn.headers[k] = v }
31
+ res = case method
32
+ when :get then conn.get(endpoint, params)
33
+ when :post then conn.post(endpoint, params)
34
+ else raise ArgumentError, "Unsupported method: #{method}"
35
+ end
36
+
37
+ MultiJson.load(res.body)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+ module Hookkaido
3
+ module Sources
4
+ module OLS
5
+ BASE = 'https://www.ebi.ac.uk/ols4/api'
6
+
7
+ # Fetch OLS ontology IDs (lowercased)
8
+ def self.ontologies(verbose:, timeout:)
9
+ json = Hookkaido::Request
10
+ .new(url: BASE, verbose: verbose, timeout: timeout)
11
+ .perform('ontologies', params: { size: 500 }, method: :get)
12
+
13
+ arr = []
14
+ if json.is_a?(Hash)
15
+ emb = json['_embedded'] && json['_embedded']['ontologies']
16
+ if emb.is_a?(Array)
17
+ emb.each do |o|
18
+ oid = (o['config'] && o['config']['id'])&.to_s&.downcase || ''
19
+ title = (o['config'] && o['config']['title']) || ''
20
+ description = (o['config'] && o['config']['description']) || ''
21
+ arr << { oid:, title:, description: }
22
+ end
23
+ end
24
+ elsif json.is_a?(Array)
25
+ json.each do |o|
26
+ oid = (o['config'] && o['config']['id'])&.to_s&.downcase || ''
27
+ title = (o['config'] && o['config']['title']) || ''
28
+ description = (o['config'] && o['config']['description']) || ''
29
+ arr << { oid:, title:, description: }
30
+ end
31
+ end
32
+
33
+ arr.uniq
34
+ end
35
+
36
+ # Single-call search across up to 3 ontologies; returns results + combined total
37
+ def self.search(term, ontologies:, rows:, start:, verbose:, timeout:)
38
+ onts = Array(ontologies).map { |x| x.to_s.downcase }.uniq
39
+ params = {
40
+ q: term,
41
+ type: 'class',
42
+ ontology: onts.join(','), # multi-ontology in one call
43
+ queryFields: 'label,synonym',
44
+ fieldList: 'iri,label,description,ontology_prefix',
45
+ rows: rows,
46
+ start: start
47
+ }
48
+
49
+ json = Hookkaido::Request
50
+ .new(url: BASE, verbose: verbose, timeout: timeout)
51
+ .perform('search', params: params, method: :get)
52
+
53
+ resp = json && json['response']
54
+ docs = (resp && resp['docs']) || []
55
+ total = (resp && resp['numFound']) || 0
56
+
57
+ results = docs.map { |doc| normalize_doc(doc) }.compact
58
+ { results: results, total: total.to_i }
59
+ end
60
+
61
+ def self.normalize_doc(doc)
62
+ iri = doc['iri']
63
+ return nil unless iri
64
+
65
+ label = doc['label']
66
+ label = label.first if label.is_a?(Array)
67
+
68
+ description = doc['description']
69
+ description = description.first if description.is_a?(Array)
70
+
71
+ {
72
+ iri: iri,
73
+ label:,
74
+ ontology_prefix: doc['ontology_prefix'],
75
+ description:
76
+ }
77
+ end
78
+ private_class_method :normalize_doc
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ def make_user_agent
3
+ requa = "Faraday/v" + Faraday::VERSION
4
+ habua = "Hookkaido/v" + Hookkaido::VERSION
5
+ ua = "#{requa} #{habua}"
6
+ ua += " (mailto:%s)" % Hookkaido.mailto if Hookkaido.mailto
7
+ ua
8
+ end
9
+
10
+ class Hash
11
+ def tosymbols
12
+ map { |(k, v)| [k.to_sym, v] }.to_h
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Hookkaido
3
+ VERSION = "0.1.0"
4
+ end
data/lib/hookkaido.rb ADDED
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ require 'erb'
3
+ require_relative 'hookkaido/error'
4
+ require_relative 'hookkaido/version'
5
+ require_relative 'hookkaido/request'
6
+ require 'hookkaido/helpers/configuration'
7
+ require_relative 'hookkaido/sources/ols'
8
+
9
+ module Hookkaido
10
+ extend Configuration
11
+
12
+ define_setting :timeout, (ENV['TW_ONTOLOGY_TIMEOUT'] || 10).to_i
13
+ define_setting :mailto, ENV['HOOKKAIDO_API_EMAIL']
14
+
15
+ # List available ontology IDs (lowercased)
16
+ def self.ontologies(verbose: false)
17
+ Sources::OLS.ontologies(verbose: verbose, timeout: timeout)
18
+ end
19
+
20
+ # Search OLS for terms in specific ontologies matching a user-provided term.
21
+ # @!param term [String] search string that returned ontology terms should
22
+ # match in some way
23
+ # @!param ontologies
24
+ # Return: { results:, page:, per:, total: }
25
+ def self.search(term, ontologies: [], per: 25, page: 1, verbose: false)
26
+ raise ArgumentError, 'term cannot be blank' if term.to_s.strip.empty?
27
+
28
+ requested = Array(ontologies).flat_map { |t| t.is_a?(String) && t.include?(',') ? t.split(',') : [t] }
29
+ requested = requested.map { |t| t.to_s.strip.downcase }.reject(&:empty?)
30
+ requested = ['uberon'] if requested.empty?
31
+ requested = requested.uniq.first(3)
32
+ # Uberon is a union of many other ontologies, so can return a lot of results
33
+ # - put other results first.
34
+ requested.push('uberon') if requested.delete('uberon')
35
+
36
+ # Validate against OLS catalog
37
+ #available = ontologies(verbose: verbose)
38
+ #targets = (requested & available)
39
+ #targets = ['uberon'] if targets.empty?
40
+ targets = requested
41
+
42
+ per_page = per.to_i
43
+ start = [(page.to_i - 1), 0].max * per_page
44
+
45
+ payload = Sources::OLS.search(
46
+ term,
47
+ ontologies: targets,
48
+ rows: per_page,
49
+ start: start,
50
+ verbose: verbose,
51
+ timeout: timeout
52
+ )
53
+
54
+ results = payload[:results] || []
55
+ total = payload[:total].to_i
56
+
57
+ # De-duplicate by IRI
58
+ uniq = {}
59
+ results.each { |r| uniq[r[:iri]] ||= r if r[:iri] }
60
+
61
+ {
62
+ results: uniq.values,
63
+ page: page.to_i,
64
+ per: per_page,
65
+ total: total
66
+ }
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hookkaido
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Klein
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: faraday-follow_redirects
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0.1'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.4'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0.1'
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '0.4'
46
+ - !ruby/object:Gem::Dependency
47
+ name: multi_json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.15'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.15'
60
+ description: Look up ontology terms across selected OLS ontologies.
61
+ email:
62
+ - trklein@illinois.edu
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - LICENSE.txt
68
+ - README.md
69
+ - lib/hookkaido.rb
70
+ - lib/hookkaido/error.rb
71
+ - lib/hookkaido/faraday.rb
72
+ - lib/hookkaido/helpers/configuration.rb
73
+ - lib/hookkaido/request.rb
74
+ - lib/hookkaido/sources/ols.rb
75
+ - lib/hookkaido/utils.rb
76
+ - lib/hookkaido/version.rb
77
+ homepage: https://github.com/SpeciesFileGroup/hookkaido
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ generated_by: ChatGPT (OpenAI) assistance
82
+ homepage_uri: https://github.com/SpeciesFileGroup/hookkaido
83
+ source_code_uri: https://github.com/SpeciesFileGroup/hookkaido
84
+ rubygems_mfa_required: 'false'
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.5.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.6.9
100
+ specification_version: 4
101
+ summary: OLS4-backed ontology search
102
+ test_files: []