Class: OLS::OntologyTerm

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

Overview

Class representing an ontology term as part of a tree structure. Uses the Tree::TreeNode (rubytree) gem as a base class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content = nil) ⇒ OntologyTerm

Returns a new instance of OntologyTerm.

Parameters:

  • name (String)

    The ontology term (id) i.e. GO:00032

  • content (String) (defaults to: nil)

    The ontology term name/description - optional this will be looked up in the OLS database



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ols.rb', line 61

def initialize( name, content=nil )
  super
  
  @already_fetched_parents  = false
  @already_fetched_children = false
  @root_term                = false
  @leaf_node                = false
  
  @all_child_terms          = nil
  @all_child_names          = nil
  
  get_term_details if @content.nil? or @content.empty?
end

Instance Attribute Details

#all_child_namesArray

Returns a flat array containing all the possible child term names for this given ontology term.

Returns:

  • (Array)

    A flat array containing all the possible child term names (Strings) for this given ontology term



157
158
159
160
# File 'lib/ols.rb', line 157

def all_child_names
  get_all_child_lists
  return @all_child_names
end

#all_child_termsArray

Returns a flat array containing all the possible child terms for this given ontology term.

Returns:

  • (Array)

    An array of all possible child terms (Strings) for this given ontology term



148
149
150
151
# File 'lib/ols.rb', line 148

def all_child_terms
  get_all_child_lists
  return @all_child_terms
end

#already_fetched_childrenObject

Returns the value of attribute already_fetched_children.



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

def already_fetched_children
  @already_fetched_children
end

#already_fetched_parentsObject

Returns the value of attribute already_fetched_parents.



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

def already_fetched_parents
  @already_fetched_parents
end

#leaf_node=(value) ⇒ Object

Sets the attribute leaf_node

Parameters:

  • value

    the value to set the attribute leaf_node to.



55
56
57
# File 'lib/ols.rb', line 55

def leaf_node=(value)
  @leaf_node = value
end

#root_term=(value) ⇒ Object

Sets the attribute root_term

Parameters:

  • value

    the value to set the attribute root_term to.



55
56
57
# File 'lib/ols.rb', line 55

def root_term=(value)
  @root_term = value
end

Class Method Details

.json_create(json_hash) ⇒ OntologyTerm

Class level function to build an OntologyTerm object from a serialized JSON hash

Examples:

emap = JSON.parse( File.read("emap.json"), :max_nesting => false )

Parameters:

  • json_hash (Hash)

    The parsed JSON hash to de-serialize

Returns:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ols.rb', line 202

def self.json_create(json_hash)
  node = new(json_hash["name"], json_hash["content"])
  node.already_fetched_children = true if json_hash["children"]
  
  node.root_term       = true if json_hash["root_term"]
  node.leaf_node       = true if json_hash["leaf_node"]
  node.all_child_terms = json_hash["all_child_terms"]
  node.all_child_names = json_hash["all_child_names"]
  
  json_hash["children"].each do |child|
    child.already_fetched_parents  = true
    child.already_fetched_children = true if child.has_children?
    node << child
  end if json_hash["children"]
  
  return node
end

Instance Method Details

#build_treeObject

Function to force the OntologyTerm object to flesh out it’s structure from the OLS database. By default OntologyTerm objects are lazy and will only retieve child data one level below themselves, so this is used to recursivley flesh out a full tree.



166
167
168
169
# File 'lib/ols.rb', line 166

def build_tree
  get_parents
  get_children( self, true )
end

#child_treeOntologyTerm

Returns the children of this term as a tree. Will include the current term as the ‘root’ of the tree.

Returns:

  • (OntologyTerm)

    The children of this term as a tree. Will include the current term as the ‘root’ of the tree.



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

def child_tree
  build_tree
  child_tree = self.clone
  child_tree.remove_from_parent!
  child_tree
end

#childrenArray

Returns an array of the direct children (OntologyTerm objects) of this term.

Returns:

  • (Array)

    An array of the direct children (OntologyTerm objects) of this term.



139
140
141
142
# File 'lib/ols.rb', line 139

def children
  get_children
  super
end

#detached_copyOntologyTerm

Returns a copy of the receiver node, with its parent and children links removed. The original node remains attached to its tree.

Returns:



300
301
302
303
304
305
# File 'lib/ols.rb', line 300

def detached_copy
  copy = OLS::OntologyTerm.new(@name, @content ? @content.clone : nil)
  copy.root_term = @root_term
  copy.leaf_node = @leaf_node
  return copy
end

#get_children(node = self, recursively = false) ⇒ Object

Recursive function to query the OLS database and collect all of the child objects and build up a tree of OntologyTerm’s.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/ols.rb', line 265

def get_children( node=self, recursively=false )
  unless @already_fetched_children or node.has_children?
    sql = <<-SQL
      select
        subject_term.identifier  as child_identifier,
        subject_term.term_name   as child_term,
        subject_term.is_leaf     as child_is_leaf,
        predicate_term.term_name as relation,
        object_term.identifier   as parent_identifier,
        object_term.term_name    as parent_term
      from
        term_relationship tr
        join term as subject_term   on tr.subject_term_pk   = subject_term.term_pk
        join term as predicate_term on tr.predicate_term_pk = predicate_term.term_pk
        join term as object_term    on tr.object_term_pk    = object_term.term_pk
      where
            predicate_term.term_name in ('part_of','is_a','develops_from')
        and object_term.identifier = ?
    SQL
    
    OLS.ols_db[sql,node.term].each do |row|
      child = OntologyTerm.new( row[:child_identifier], row[:child_term] )
      child.leaf_node = true if row[:child_is_leaf].to_i == 1
      child.get_children( child, true ) if recursively and !child.is_leaf?
      node << child
    end
    
    @already_fetched_children = true
  end
end

#get_parents(node = self) ⇒ Object

Recursive function to query the OLS database and collect all of the parent objects and insert them into @parents in the correct order.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ols.rb', line 232

def get_parents( node=self )
  unless @already_fetched_parents
    sql = <<-SQL
      select
        subject_term.identifier  as child_identifier,
        subject_term.term_name   as child_term,
        predicate_term.term_name as relation,
        object_term.identifier   as parent_identifier,
        object_term.term_name    as parent_term,
        object_term.is_root_term as parent_is_root
      from
        term_relationship tr
        join term as subject_term 	on tr.subject_term_pk   = subject_term.term_pk
        join term as predicate_term on tr.predicate_term_pk = predicate_term.term_pk
        join term as object_term    on tr.object_term_pk    = object_term.term_pk
      where
            predicate_term.term_name in ('part_of','is_a','develops_from')
        and subject_term.identifier = ?
    SQL
    
    OLS.ols_db[ sql, node.term ].each do |row|
      parent           = OntologyTerm.new( row[:parent_identifier], row[:parent_term] )
      parent.root_term = true if row[:parent_is_root].to_i == 1
      parent << node
      get_parents( parent )
    end
    
    @already_fetched_parents = true
  end
end

#is_leaf?Boolean

Returns true if the receiver node is a ‘leaf’ - i.e., one without any children.

Returns:

  • (Boolean)

    true if this is a leaf node.



101
102
103
# File 'lib/ols.rb', line 101

def is_leaf?
  @leaf_node
end

#is_root?Boolean

Returns true if the receiver is a root node. Note that orphaned children will also be reported as root nodes.

Returns:

  • (Boolean)

    true if this is a root node.



93
94
95
# File 'lib/ols.rb', line 93

def is_root?
  @root_term
end

#merge(tree) ⇒ OntologyTerm

Function that merges one OntologyTerm tree into another.

Parameters:

  • tree (OntologyTerm)

    The tree that is to be merged into self

Returns:



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/ols.rb', line 311

def merge( tree )
  unless tree.is_a?(OLS::OntologyTerm)
    raise TypeError, "You can only merge in another OntologyTerm tree!"
  end
  
  unless self.root.name == tree.root.name
    raise ArgumentError, "Unable to merge trees as they do not share the same root!"
  end
  
  new_tree = merge_subtrees( self.root, tree.root )
end

#parent=(parent) ⇒ OntologyTerm

Method to set the parent node for the receiver node. This method should NOT be invoked by client code.

Parameters:

Returns:



225
226
227
# File 'lib/ols.rb', line 225

def parent=(parent)         # :nodoc:
  @parent = parent
end

#parentageArray

Returns an array of parent OntologyTerm objects.

Returns:

  • (Array)

    An array of parent OntologyTerm objects



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

def parentage
  get_parents
  super
end

#termString

Override to ensure compatibility with Tree::TreeNode.

Returns:

  • (String)

    The ontology term (id) i.e. GO00032



78
79
80
# File 'lib/ols.rb', line 78

def term
  self.name
end

#term_nameString

Override to ensure compatibility with Tree::TreeNode.

Returns:

  • (String)

    The ontology term name/description



85
86
87
# File 'lib/ols.rb', line 85

def term_name
  self.content
end

#to_json(*a) ⇒ Object

Creates a JSON representation of this node including all it’s children. This requires the JSON gem to be available, or else the operation fails with a warning message.

Returns:

  • The JSON representation of this subtree.

See Also:

  • OLS::OntologyTerm.{OntologyTerm{OntologyTerm#json_create}


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ols.rb', line 177

def to_json(*a)
  json_hash = {
    "name"            => name,
    "content"         => content,
    "root_term"       => @root_term,
    "leaf_node"       => @leaf_node,
    "all_child_terms" => @all_child_terms,
    "all_child_names" => @all_child_names,
    JSON.create_id    => self.class.name
  }

  if has_children?
    json_hash["children"] = children
  end

  return JSON.generate( json_hash, :max_nesting => false )
end

#to_sString

Returns string representation of the receiver node. This method is primarily meant for debugging purposes.

Returns:

  • (String)

    A string representation of the node.



109
110
111
112
113
114
115
# File 'lib/ols.rb', line 109

def to_s
  "Term: #{@name}" +
    " Term Name: " + (@content || "<Empty>") +
    " Root Term?: #{is_root?}" +
    " Leaf Node?: #{is_leaf?} " +
    " Total Nodes Loaded: #{size()}"
end