Class: OLS::OntologyTerm
- Inherits:
-
Tree::TreeNode
- Object
- Tree::TreeNode
- OLS::OntologyTerm
- 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
-
#all_child_names ⇒ Array
Returns a flat array containing all the possible child term names for this given ontology term.
-
#all_child_terms ⇒ Array
Returns a flat array containing all the possible child terms for this given ontology term.
-
#already_fetched_children ⇒ Object
Returns the value of attribute already_fetched_children.
-
#already_fetched_parents ⇒ Object
Returns the value of attribute already_fetched_parents.
-
#leaf_node ⇒ Object
writeonly
Sets the attribute leaf_node.
-
#root_term ⇒ Object
writeonly
Sets the attribute root_term.
Class Method Summary collapse
-
.json_create(json_hash) ⇒ OntologyTerm
Class level function to build an OntologyTerm object from a serialized JSON hash.
Instance Method Summary collapse
-
#build_tree ⇒ Object
Function to force the OntologyTerm object to flesh out it’s structure from the OLS database.
-
#child_tree ⇒ OntologyTerm
Returns the children of this term as a tree.
-
#children ⇒ Array
Returns an array of the direct children (OntologyTerm objects) of this term.
-
#detached_copy ⇒ OntologyTerm
Returns a copy of the receiver node, with its parent and children links removed.
-
#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.
-
#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.
-
#initialize(name, content = nil) ⇒ OntologyTerm
constructor
A new instance of OntologyTerm.
-
#is_leaf? ⇒ Boolean
Returns
true
if the receiver node is a ‘leaf’ - i.e., one without any children. -
#is_root? ⇒ Boolean
Returns
true
if the receiver is a root node. -
#merge(tree) ⇒ OntologyTerm
Function that merges one OntologyTerm tree into another.
-
#parent=(parent) ⇒ OntologyTerm
Method to set the parent node for the receiver node.
-
#parentage ⇒ Array
Returns an array of parent OntologyTerm objects.
-
#term ⇒ String
Override to ensure compatibility with Tree::TreeNode.
-
#term_name ⇒ String
Override to ensure compatibility with Tree::TreeNode.
-
#to_json(*a) ⇒ Object
Creates a JSON representation of this node including all it’s children.
-
#to_s ⇒ String
Returns string representation of the receiver node.
Constructor Details
#initialize(name, content = nil) ⇒ OntologyTerm
Returns a new instance of OntologyTerm.
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_names ⇒ Array
Returns a flat array containing all the possible child term names 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_terms ⇒ Array
Returns a flat array containing all the possible child terms 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_children ⇒ Object
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_parents ⇒ Object
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
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
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
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_tree ⇒ Object
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_tree ⇒ OntologyTerm
Returns 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 |
#children ⇒ Array
Returns 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_copy ⇒ OntologyTerm
Returns a copy of the receiver node, with its parent and children links removed. The original node remains attached to its tree.
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.
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.
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.
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.
225 226 227 |
# File 'lib/ols.rb', line 225 def parent=(parent) # :nodoc: @parent = parent end |
#parentage ⇒ Array
Returns an array of parent OntologyTerm objects.
120 121 122 123 |
# File 'lib/ols.rb', line 120 def parentage get_parents super end |
#term ⇒ String
Override to ensure compatibility with Tree::TreeNode.
78 79 80 |
# File 'lib/ols.rb', line 78 def term self.name end |
#term_name ⇒ String
Override to ensure compatibility with Tree::TreeNode.
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.
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_s ⇒ String
Returns string representation of the receiver node. This method is primarily meant for debugging purposes.
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 |