Class: OLS::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/ols/graph.rb

Overview

Utility class for representing an ontology graph. You should NOT really interact with instances of this class directly, use the methods on the OLS module and the resulting OLS::Term objects.

Author:

Instance Method Summary collapse

Constructor Details

#initializeGraph

Creates a new OLS::Graph object



12
13
14
# File 'lib/ols/graph.rb', line 12

def initialize
  @graph = {}
end

Instance Method Details

#[](key) ⇒ Hash

Fetch the object/parents/children hash for a given term.

Parameters:

  • key (String)

    The ontology term id

Returns:

  • (Hash)

    The object/parents/children hash for the given term



53
54
55
# File 'lib/ols/graph.rb', line 53

def [](key)
  @graph[key]
end

#add_relationship(parent, child) ⇒ Object

Add an edge/relationship to the ontology graph

Parameters:

Raises:

  • (TypeError)

    Raised if parent or child are not an OLS::Term objects



81
82
83
84
85
86
87
88
89
90
# File 'lib/ols/graph.rb', line 81

def add_relationship(parent,child)
  raise TypeError, "You must pass an OLS::Term object" unless parent.is_a? OLS::Term
  raise TypeError, "You must pass an OLS::Term object" unless child.is_a? OLS::Term

  add_to_graph(parent) if self.find(parent.term_id).nil?
  add_to_graph(child) if self.find(child.term_id).nil?

  @graph[parent.term_id][:children].push(child.term_id) unless @graph[parent.term_id][:children].include?(child.term_id)
  @graph[child.term_id][:parents].push(parent.term_id) unless @graph[child.term_id][:parents].include?(parent.term_id)
end

#add_to_graph(term) ⇒ Object

Add an OLS::Term object into the graph

Parameters:

  • term (OLS::Term)

    The OLS::Term object to add

Raises:

  • (TypeError)

    Raised if term is not an OLS::Term object



69
70
71
72
73
74
# File 'lib/ols/graph.rb', line 69

def add_to_graph(term)
  raise TypeError, "You must pass an OLS::Term object" unless term.is_a? OLS::Term
  unless @graph.has_key?(term.term_id)
    @graph[term.term_id] = { :object => term, :parents => [], :children => [] }
  end
end

#find(key) ⇒ OLS::Term

Fetch the OLS::Term object for a node in the graph.

Parameters:

  • key (String)

    The ontology term id

Returns:

  • (OLS::Term)

    The OLS::Term object for this term id



61
62
63
# File 'lib/ols/graph.rb', line 61

def find(key)
  @graph[key][:object] if @graph.has_key? key
end

#initialize_copy(source) ⇒ Object

Object function used by .clone and .dup to create copies of OLS::Graph objects.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ols/graph.rb', line 17

def initialize_copy(source)
  super
  @graph = {}

  source.raw_graph.each do |term_id,term_details|
    old_term = term_details[:object]

    new_term = OLS::Term.new( old_term.term_id, old_term.term_name, self )
    [
      :@already_fetched_parents,
      :@already_fetched_children,
      :@already_fetched_metadata,
      :@definition,
      :@synonyms
    ].each do |instance_var|
      new_term.instance_variable_set(instance_var,old_term.instance_variable_get(instance_var))
    end

    @graph[term_id] = {
      :object => new_term,
      :parents => term_details[:parents].dup,
      :children => term_details[:children].dup
    }
  end
end

#raw_graphObject

Accessor for the internal graph hash TODO: OLS::Term monkeys around with this in a few places - write methods to handle the access needed so we don’t have to expose this



45
46
47
# File 'lib/ols/graph.rb', line 45

def raw_graph
  @graph
end