Module: OLS

Defined in:
lib/ols.rb,
lib/ols/term.rb,
lib/ols/cache.rb,
lib/ols/graph.rb,
lib/ols/version.rb

Overview

Simple wrapper for interacting with the OLS (Ontology Lookup Service - www.ebi.ac.uk/ontology-lookup/) database (created and managed by the EBI).

Author:

Defined Under Namespace

Classes: Cache, Graph, Term, TermNotFoundError

Constant Summary collapse

VERSION =

OLS::VERSION - The OLS gem version

"0.3.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log=(value) ⇒ Object (writeonly)

Set whether to log HTTP requests - pass in true or false



103
104
105
# File 'lib/ols.rb', line 103

def log=(value)
  @log = value
end

.log_levelObject

Return the log level. Defaults to :warn



122
123
124
# File 'lib/ols.rb', line 122

def log_level
  @log_level ||= :warn
end

.loggerObject

Returns the logger. Defaults to an instance of Logger writing to STDOUT



114
115
116
# File 'lib/ols.rb', line 114

def logger
  @logger ||= ::Logger.new STDOUT
end

.proxyObject

Returns a HTTP proxy url. Will read the http_proxy environment variable if present



130
131
132
# File 'lib/ols.rb', line 130

def proxy
  @proxy ||= ( ENV['http_proxy'] || ENV['HTTP_PROXY'] )
end

Class Method Details

.add_ontology_to_cache(ontology) ⇒ Object Also known as: refresh_ontology_in_cache

Add an ontology to the cache.

Parameters:

  • ontology (String)

    The ontology to add

Raises:

  • (ArgumentError)

    Raised if the ontology is not part of OLS

Since:

  • 0.2.0



169
170
171
# File 'lib/ols.rb', line 169

def add_ontology_to_cache(ontology)
  @cache.add_ontology_to_cache(ontology)
end

.cached_ontologiesArray

Returns a list of the cached ontologies.

Returns:

  • (Array)

    A list of the cached ontologies

Since:

  • 0.2.0



160
161
162
# File 'lib/ols.rb', line 160

def cached_ontologies
  @cache.cached_ontologies
end

.clientObject

Returns the raw (Savon) SOAP client for the OLS webservice

Returns:

  • (Object)

    The raw (Savon) SOAP client for the OLS webservice



17
18
19
20
# File 'lib/ols.rb', line 17

def client
  @client = setup_soap_client if @client.nil?
  @client
end

.find_by_id(term_id) ⇒ OLS::Term

Fetch an ontology term (OLS::Term) by its id

Parameters:

  • term_id (String/Symbol)

    An ontology id to look for - i.e. ‘GO:0023034’

Returns:

  • (OLS::Term)

    An OLS::Term object for the requested ontology id

Raises:

  • OLS::TermNotFoundError Raised if the requested ontology id cannot be found



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ols.rb', line 88

def find_by_id(term_id)
  term = nil

  term = @cache.find_by_id(term_id) if using_cache?

  if term.nil?
    term_name = request(:get_term_by_id) { soap.body = { :termId => term_id } }
    raise TermNotFoundError if term_name.eql?(term_id)
    term = OLS::Term.new(term_id,term_name)
  end

  term
end

.log?Boolean

Returns whether to log HTTP/SOAP requests. Defaults to false

Returns:

  • (Boolean)


106
107
108
# File 'lib/ols.rb', line 106

def log?
  @log ? true : false
end

.ontologiesHash

Fetch a hash of all ontologies in the OLS service, keyed by their short-name

Returns:

  • (Hash)

    names of ontologies: short-name (keys), full-names (values)



48
49
50
51
52
53
54
55
56
57
# File 'lib/ols.rb', line 48

def ontologies
  @ontologies ||= {}
  if @ontologies.empty?
    response = request :get_ontology_names
    response[:item].each do |ont|
      @ontologies[ont[:key]] = ont[:value]
    end
  end
  @ontologies
end

.remove_ontology_from_cache(ontology) ⇒ Object

Remove an ontology from the cache.

Parameters:

  • ontology (String)

    The ontology to remove

Raises:

  • (ArgumentError)

    Raised if the ontology is not part of OLS

Since:

  • 0.2.0



180
181
182
# File 'lib/ols.rb', line 180

def remove_ontology_from_cache(ontology)
  @cache.remove_ontology_from_cache(ontology)
end

.request(method, &block) ⇒ Hash

Generic request method to allow simple access to all the OLS webservice end-points (provided by Savon)

Parameters:

  • method (String/Symbol)

    The SOAP method to call

  • &block (Block)

    An optional code-block to pass to the SOAP call

Returns:

  • (Hash)

    The OLS method call return



28
29
30
31
32
33
34
35
36
# File 'lib/ols.rb', line 28

def request(method,&block)
  response = nil
  if block
    response = self.client.request(method,&block)
  else
    response = self.client.request method
  end
  response.body[:"#{method}_response"][:"#{method}_return"]
end

.root_terms(ontology) ⇒ Array

Fetch all the root terms for a given ontology

Parameters:

  • ontology (String/Symbol)

    The short-name of the ontology

Returns:

  • (Array)

    An array of OLS::Term objects for all root terms the requested ontology



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ols.rb', line 63

def root_terms(ontology)
  root_terms = []
  root_terms = @cache.root_terms(ontology) if using_cache?

  if root_terms.empty?
    response = request(:get_root_terms) { soap.body = { :ontologyName => ontology } }

    if response[:item].is_a? Array
      response[:item].each do |term|
        root_terms.push( OLS::Term.new(term[:key],term[:value]) )
      end
    else
      term = response[:item]
      root_terms.push( OLS::Term.new(term[:key],term[:value]) )
    end
  end

  root_terms
end

.setup_cache(options = {}) ⇒ Object

Configure the OLS gem to use a local cache. Useful if you have some serious ontology activity going on, or you want to insulate yourself from server outages and the like.

NOTE: We only support a file-based (on-disk) cache at the moment. By default it will look in/use the current working directory, or you can pass a configuration hash as follows:

OLS.setup_cache({ :directory => '/path/to/cache_directory' })

Support for other cache backends will come in future builds.

Since:

  • 0.2.0



152
153
154
# File 'lib/ols.rb', line 152

def setup_cache(options={})
  @cache = OLS::Cache.new(options)
end

.using_cache?Boolean

Are we using a local cache? Defaults to false.

Returns:

  • (Boolean)

See Also:

  • #setup_cache

Since:

  • 0.2.0



137
138
139
# File 'lib/ols.rb', line 137

def using_cache?
  @cache ? true : false
end

.versionString

Fetch the version string for the current build of OLS

Returns:

  • (String)

    Then version string for the current OLS build



41
42
43
# File 'lib/ols.rb', line 41

def version
  request :get_version
end