ols 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -1
- data/Gemfile +4 -0
- data/History.txt +6 -0
- data/README.rdoc +48 -57
- data/lib/ols.rb +115 -387
- data/lib/ols/graph.rb +91 -0
- data/lib/ols/term.rb +647 -0
- data/lib/ols/version.rb +4 -1
- data/ols.gemspec +7 -12
- data/script/console +9 -0
- data/test/test_helper.rb +24 -9
- data/test/test_ols.rb +44 -112
- data/test/test_ols_graph.rb +36 -0
- data/test/test_ols_term.rb +484 -0
- metadata +100 -105
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 0.1.0 2011-11-04
|
2
|
+
|
3
|
+
* Total rewrite (as a result please note API changes)...
|
4
|
+
* Switch to using the OLS SOAP service instead of local MySQL instance
|
5
|
+
* Remove the use of RubyTree as ontologies are Directed Acyclic Graphs (DAGs), not trees
|
6
|
+
|
1
7
|
=== 0.0.1 2011-04-15
|
2
8
|
|
3
9
|
* Initial release:
|
data/README.rdoc
CHANGED
@@ -2,19 +2,14 @@
|
|
2
2
|
|
3
3
|
http://github.com/dazoakley/ols
|
4
4
|
|
5
|
-
OLS provides a simple interface to the EBI's Ontology Lookup Service (http://www.ebi.ac.uk/ontology-lookup/).
|
6
|
-
It provides an easy lookup of ontology terms and automagically builds up ontology
|
7
|
-
(http://rubytree.rubyforge.org/) as a base library.
|
8
|
-
|
9
|
-
<b>PLEASE NOTE</b>: The current version of this gem requires a local install of the OLS database running on MySQL.
|
10
|
-
Please see http://www.ebi.ac.uk/ontology-lookup/databaseExport.do I will update the code in the future
|
11
|
-
to run off the soap service.
|
5
|
+
OLS provides a simple interface to the EBI's Ontology Lookup Service (http://www.ebi.ac.uk/ontology-lookup/).
|
6
|
+
It provides an easy lookup of ontology terms and automagically builds up ontology graphs.
|
12
7
|
|
13
8
|
== Install
|
14
9
|
|
15
10
|
gem install ols
|
16
11
|
|
17
|
-
== Usage
|
12
|
+
== Basic Usage
|
18
13
|
|
19
14
|
Include the module in your code:
|
20
15
|
|
@@ -23,67 +18,63 @@ Include the module in your code:
|
|
23
18
|
|
24
19
|
Then, to lookup an ontology term:
|
25
20
|
|
26
|
-
ont = OLS
|
21
|
+
ont = OLS.find_by_id('EMAP:3018')
|
27
22
|
|
28
23
|
This will create a simple tree object for the EMAP term EMAP:3018
|
29
24
|
|
30
|
-
ont.
|
31
|
-
ont.term # => "EMAP:3018"
|
32
|
-
|
33
|
-
ont.content # => "TS18,nose"
|
25
|
+
ont.term_id # => "EMAP:3018"
|
34
26
|
ont.term_name # => "TS18,nose"
|
35
27
|
|
36
28
|
Find out about your ontology term
|
37
|
-
|
29
|
+
|
38
30
|
ont.is_root? # => false
|
39
31
|
ont.is_leaf? # => false
|
40
|
-
|
41
|
-
<b>NOTE</b>: OntologyTerm's are lazily loaded, so upon initial build the OntologyTerm is just a single node in a
|
42
|
-
tree - no parents, no children. See here (note the "Total Nodes Loaded" count):
|
43
|
-
|
44
|
-
ont.to_s # => "Term: EMAP:3018 Term Name: TS18,nose Root Term?: false Leaf Node?: false Total Nodes Loaded: 1"
|
45
|
-
|
46
|
-
The tree for the term is built up (parents) and down (children) as it is requested:
|
47
|
-
|
48
|
-
ont.parentage # => Array of all parent terms (these will be loaded into the tree now)
|
49
|
-
ont.children # => Array of all direct child terms (these will also be loaded into the tree now)
|
50
|
-
ont.to_s # => "Term: EMAP:3018 Term Name: TS18,nose Root Term?: false Leaf Node?: false Total Nodes Loaded: 4"
|
51
|
-
|
52
|
-
Alternatively, if you want to force load the tree:
|
53
|
-
|
54
|
-
ont.buld_tree # => Will load both parents and children into the tree
|
55
|
-
|
56
|
-
Find out more about your tree:
|
57
|
-
|
58
|
-
ont.root # => Gives you the root node - in this case: "EMAP:0"
|
59
|
-
ont.all_child_terms # => An array of all child ontology terms
|
60
|
-
ont.all_child_name # => An array of all child ontology term names
|
61
|
-
|
62
|
-
For more information on general usage, take a look in the test suite. Also, OLS::OntologyTerm is a child object of
|
63
|
-
Tree::TreeNode (from the rubytree gem - http://rubytree.rubyforge.org/) and as such inherits all of its methods.
|
32
|
+
ont.to_s # => "EMAP:3018 - TS18,nose"
|
64
33
|
|
65
|
-
|
34
|
+
The graph for the term is built up (parents) and down (children) as it is requested:
|
66
35
|
|
67
|
-
|
36
|
+
ont.parents # => Array of all parent terms objects
|
37
|
+
ont.children # => Array of all direct child term objects
|
68
38
|
|
69
|
-
|
70
|
-
:database => 'ols',
|
71
|
-
:host => '127.0.0.1',
|
72
|
-
:port => 3306,
|
73
|
-
:user => 'ols',
|
74
|
-
:password => 'ols'
|
75
|
-
}
|
39
|
+
Alternatively, if you want to force load the graph (around this term):
|
76
40
|
|
77
|
-
|
78
|
-
|
41
|
+
ont.focus_graph! # => Will load all parents and children into the graph
|
42
|
+
ont.size # => 19
|
79
43
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
44
|
+
Find out more about your graph:
|
45
|
+
|
46
|
+
ont.root # => Gives you the root node - in this case: "EMAP:0"
|
47
|
+
ont.all_parent_ids # => An array of all parent ontology terms
|
48
|
+
ont.all_parent_names # => An array of all parent ontology term names
|
49
|
+
ont.all_child_ids # => An array of all child ontology terms
|
50
|
+
ont.all_child_names # => An array of all child ontology term names
|
51
|
+
|
52
|
+
Visualise your graph (useful for exploring):
|
53
|
+
|
54
|
+
ont.root.print_graph
|
55
|
+
|
56
|
+
# Gives the following:
|
57
|
+
#
|
58
|
+
# * EMAP:0
|
59
|
+
# |---+ EMAP:2636
|
60
|
+
# |---+ EMAP:2822
|
61
|
+
# |---+ EMAP:2987
|
62
|
+
# |---+ EMAP:3018
|
63
|
+
# |---+ EMAP:3022
|
64
|
+
# |---+ EMAP:3023
|
65
|
+
# |---+ EMAP:3024
|
66
|
+
# |---> EMAP:3025
|
67
|
+
# |---> EMAP:3026
|
68
|
+
# |---+ EMAP:3027
|
69
|
+
# |---> EMAP:3029
|
70
|
+
# |---> EMAP:3028
|
71
|
+
# |---+ EMAP:3030
|
72
|
+
# |---> EMAP:3031
|
73
|
+
# |---> EMAP:3032
|
74
|
+
# |---> EMAP:3019
|
75
|
+
# |---+ EMAP:3020
|
76
|
+
# |---> EMAP:3021
|
77
|
+
#
|
87
78
|
|
88
79
|
== Meta
|
89
80
|
|
@@ -110,7 +101,7 @@ included in all copies or substantial portions of the Software.
|
|
110
101
|
|
111
102
|
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
112
103
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
113
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
104
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
114
105
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
115
106
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
116
107
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
data/lib/ols.rb
CHANGED
@@ -1,422 +1,150 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
|
2
|
-
require '
|
3
|
-
require 'tree'
|
4
|
-
require 'sequel'
|
5
|
-
require 'mysql2'
|
6
|
-
require 'json'
|
3
|
+
require 'savon'
|
7
4
|
|
8
5
|
# Simple wrapper for interacting with the OLS (Ontology Lookup Service - http://www.ebi.ac.uk/ontology-lookup/)
|
9
|
-
# database (created and managed by the EBI).
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# @author Darren Oakley
|
6
|
+
# database (created and managed by the EBI).
|
7
|
+
#
|
8
|
+
# @author Darren Oakley (https://github.com/dazoakley)
|
13
9
|
module OLS
|
14
|
-
|
15
|
-
class << self
|
16
|
-
attr_accessor :db_connection_details
|
17
|
-
|
18
|
-
def ols_db
|
19
|
-
@ols_db = connect_to_db() if @ols_db.nil?
|
20
|
-
@ols_db
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def get_db_connection_details
|
26
|
-
defaults = {
|
27
|
-
:database => 'ols',
|
28
|
-
:host => '127.0.0.1',
|
29
|
-
:port => 3306,
|
30
|
-
:user => 'ols',
|
31
|
-
:password => 'ols'
|
32
|
-
}
|
33
|
-
|
34
|
-
if !OLS.db_connection_details.nil? and OLS.db_connection_details.is_a? Hash
|
35
|
-
defaults.merge!(OLS.db_connection_details)
|
36
|
-
end
|
37
|
-
|
38
|
-
defaults
|
39
|
-
end
|
40
|
-
|
41
|
-
def connect_to_db
|
42
|
-
params = { :adapter => 'mysql2', :encoding => 'utf8' }.merge(get_db_connection_details)
|
43
|
-
Sequel.connect(params)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
10
|
# Error class for when we can't find a given ontology term.
|
48
|
-
class
|
49
|
-
|
50
|
-
# Class representing an ontology term as part of a tree structure. Uses the Tree::TreeNode (rubytree)
|
51
|
-
# gem as a base class.
|
52
|
-
class OntologyTerm < Tree::TreeNode
|
53
|
-
attr_accessor :already_fetched_parents, :already_fetched_children
|
54
|
-
|
55
|
-
attr_accessor :root_term, :leaf_node
|
56
|
-
attr_writer :all_child_terms, :all_child_names
|
57
|
-
protected :root_term, :leaf_node
|
58
|
-
|
59
|
-
# @param [String] name The ontology term (id) i.e. GO:00032
|
60
|
-
# @param [String] content The ontology term name/description - optional this will be looked up in the OLS database
|
61
|
-
def initialize( name, content=nil )
|
62
|
-
super
|
63
|
-
|
64
|
-
@already_fetched_parents = false
|
65
|
-
@already_fetched_children = false
|
66
|
-
@root_term = false
|
67
|
-
@leaf_node = false
|
68
|
-
|
69
|
-
@all_child_terms = nil
|
70
|
-
@all_child_names = nil
|
71
|
-
|
72
|
-
get_term_details if @content.nil? or @content.empty?
|
73
|
-
end
|
74
|
-
|
75
|
-
# Override to ensure compatibility with Tree::TreeNode.
|
76
|
-
#
|
77
|
-
# @return [String] The ontology term (id) i.e. GO00032
|
78
|
-
def term
|
79
|
-
self.name
|
80
|
-
end
|
11
|
+
class TermNotFoundError < StandardError; end
|
81
12
|
|
82
|
-
|
83
|
-
#
|
84
|
-
# @return [String] The ontology term name/description
|
85
|
-
def term_name
|
86
|
-
self.content
|
87
|
-
end
|
88
|
-
|
89
|
-
# Returns +true+ if the receiver is a root node. Note that
|
90
|
-
# orphaned children will also be reported as root nodes.
|
91
|
-
#
|
92
|
-
# @return [Boolean] +true+ if this is a root node.
|
93
|
-
def is_root?
|
94
|
-
@root_term
|
95
|
-
end
|
96
|
-
|
97
|
-
# Returns +true+ if the receiver node is a 'leaf' - i.e., one without
|
98
|
-
# any children.
|
99
|
-
#
|
100
|
-
# @return [Boolean] +true+ if this is a leaf node.
|
101
|
-
def is_leaf?
|
102
|
-
@leaf_node
|
103
|
-
end
|
104
|
-
|
105
|
-
# Returns string representation of the receiver node.
|
106
|
-
# This method is primarily meant for debugging purposes.
|
107
|
-
#
|
108
|
-
# @return [String] A string representation of the node.
|
109
|
-
def to_s
|
110
|
-
"Term: #{@name}" +
|
111
|
-
" Term Name: " + (@content || "<Empty>") +
|
112
|
-
" Root Term?: #{is_root?}" +
|
113
|
-
" Leaf Node?: #{is_leaf?} " +
|
114
|
-
" Total Nodes Loaded: #{size()}"
|
115
|
-
end
|
116
|
-
|
117
|
-
# Returns an array of parent OntologyTerm objects.
|
118
|
-
#
|
119
|
-
# @return [Array] An array of parent OntologyTerm objects
|
120
|
-
def parentage
|
121
|
-
get_parents
|
122
|
-
super
|
123
|
-
end
|
124
|
-
|
125
|
-
# Returns the children of this term as a tree. Will include the current term
|
126
|
-
# as the 'root' of the tree.
|
13
|
+
class << self
|
14
|
+
# Returns the raw (Savon) SOAP client for the OLS webservice
|
127
15
|
#
|
128
|
-
# @return [
|
129
|
-
def
|
130
|
-
|
131
|
-
|
132
|
-
child_tree.remove_from_parent!
|
133
|
-
child_tree
|
16
|
+
# @return [Object] The raw (Savon) SOAP client for the OLS webservice
|
17
|
+
def client
|
18
|
+
@client = setup_soap_client if @client.nil?
|
19
|
+
@client
|
134
20
|
end
|
135
21
|
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
|
140
|
-
|
141
|
-
|
22
|
+
# Generic request method to allow simple access to all the
|
23
|
+
# OLS webservice end-points (provided by Savon)
|
24
|
+
#
|
25
|
+
# @param [String/Symbol] method The SOAP method to call
|
26
|
+
# @param [Block] &block An optional code-block to pass to the SOAP call
|
27
|
+
# @return [Hash] The OLS method call return
|
28
|
+
def request(method,&block)
|
29
|
+
response = nil
|
30
|
+
if block
|
31
|
+
response = self.client.request(method,&block)
|
32
|
+
else
|
33
|
+
response = self.client.request method
|
34
|
+
end
|
35
|
+
response.body[:"#{method}_response"][:"#{method}_return"]
|
142
36
|
end
|
143
37
|
|
144
|
-
#
|
145
|
-
# for this given ontology term.
|
146
|
-
#
|
147
|
-
# @return [Array] An array of all possible child terms (Strings) for this given ontology term
|
148
|
-
def all_child_terms
|
149
|
-
get_all_child_lists
|
150
|
-
return @all_child_terms
|
151
|
-
end
|
152
|
-
|
153
|
-
# Returns a flat array containing all the possible child term
|
154
|
-
# names for this given ontology term.
|
38
|
+
# Fetch the version string for the current build of OLS
|
155
39
|
#
|
156
|
-
# @return [
|
157
|
-
def
|
158
|
-
|
159
|
-
return @all_child_names
|
160
|
-
end
|
161
|
-
|
162
|
-
# Function to force the OntologyTerm object to flesh out it's structure
|
163
|
-
# from the OLS database. By default OntologyTerm objects are lazy and will
|
164
|
-
# only retieve child data one level below themselves, so this is used to
|
165
|
-
# recursivley flesh out a full tree.
|
166
|
-
def build_tree
|
167
|
-
get_parents
|
168
|
-
get_children( self, true )
|
40
|
+
# @return [String] Then version string for the current OLS build
|
41
|
+
def version
|
42
|
+
request :get_version
|
169
43
|
end
|
170
|
-
|
171
|
-
# Creates a JSON representation of this node including all it's children. This requires the JSON gem to be
|
172
|
-
# available, or else the operation fails with a warning message.
|
173
|
-
#
|
174
|
-
# @return The JSON representation of this subtree.
|
175
|
-
#
|
176
|
-
# @see {OntologyTerm#json_create}
|
177
|
-
def to_json(*a)
|
178
|
-
json_hash = {
|
179
|
-
"name" => name,
|
180
|
-
"content" => content,
|
181
|
-
"root_term" => @root_term,
|
182
|
-
"leaf_node" => @leaf_node,
|
183
|
-
"all_child_terms" => @all_child_terms,
|
184
|
-
"all_child_names" => @all_child_names,
|
185
|
-
JSON.create_id => self.class.name
|
186
|
-
}
|
187
44
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
# @example
|
198
|
-
# emap = JSON.parse( File.read("emap.json"), :max_nesting => false )
|
199
|
-
#
|
200
|
-
# @param [Hash] json_hash The parsed JSON hash to de-serialize
|
201
|
-
# @return [OntologyTerm] The de-serialized object
|
202
|
-
def self.json_create(json_hash)
|
203
|
-
node = new(json_hash["name"], json_hash["content"])
|
204
|
-
node.already_fetched_children = true if json_hash["children"]
|
205
|
-
|
206
|
-
node.root_term = true if json_hash["root_term"]
|
207
|
-
node.leaf_node = true if json_hash["leaf_node"]
|
208
|
-
node.all_child_terms = json_hash["all_child_terms"]
|
209
|
-
node.all_child_names = json_hash["all_child_names"]
|
210
|
-
|
211
|
-
json_hash["children"].each do |child|
|
212
|
-
child.already_fetched_parents = true
|
213
|
-
child.already_fetched_children = true if child.has_children?
|
214
|
-
node << child
|
215
|
-
end if json_hash["children"]
|
216
|
-
|
217
|
-
return node
|
218
|
-
end
|
219
|
-
|
220
|
-
# Method to set the parent node for the receiver node.
|
221
|
-
# This method should *NOT* be invoked by client code.
|
222
|
-
#
|
223
|
-
# @param [OntologyTerm] parent The parent node.
|
224
|
-
# @return [OntologyTerm] The parent node.
|
225
|
-
def parent=(parent) # :nodoc:
|
226
|
-
@parent = parent
|
227
|
-
end
|
228
|
-
|
229
|
-
# Recursive function to query the OLS database and collect all of
|
230
|
-
# the parent objects and insert them into @parents in the correct
|
231
|
-
# order.
|
232
|
-
def get_parents( node=self )
|
233
|
-
unless @already_fetched_parents
|
234
|
-
sql = <<-SQL
|
235
|
-
select
|
236
|
-
subject_term.identifier as child_identifier,
|
237
|
-
subject_term.term_name as child_term,
|
238
|
-
predicate_term.term_name as relation,
|
239
|
-
object_term.identifier as parent_identifier,
|
240
|
-
object_term.term_name as parent_term,
|
241
|
-
object_term.is_root_term as parent_is_root
|
242
|
-
from
|
243
|
-
term_relationship tr
|
244
|
-
join term as subject_term on tr.subject_term_pk = subject_term.term_pk
|
245
|
-
join term as predicate_term on tr.predicate_term_pk = predicate_term.term_pk
|
246
|
-
join term as object_term on tr.object_term_pk = object_term.term_pk
|
247
|
-
where
|
248
|
-
predicate_term.term_name in ('part_of','is_a','develops_from')
|
249
|
-
and subject_term.identifier = ?
|
250
|
-
SQL
|
251
|
-
|
252
|
-
OLS.ols_db[ sql, node.term ].each do |row|
|
253
|
-
parent = OntologyTerm.new( row[:parent_identifier], row[:parent_term] )
|
254
|
-
parent.root_term = true if row[:parent_is_root].to_i == 1
|
255
|
-
parent << node
|
256
|
-
get_parents( parent )
|
45
|
+
# Fetch a hash of all ontologies in the OLS service, keyed by their short-name
|
46
|
+
#
|
47
|
+
# @return [Hash] names of ontologies: short-name (keys), full-names (values)
|
48
|
+
def ontologies
|
49
|
+
@ontologies ||= {}
|
50
|
+
if @ontologies.empty?
|
51
|
+
response = request :get_ontology_names
|
52
|
+
response[:item].each do |ont|
|
53
|
+
@ontologies[ont[:key]] = ont[:value]
|
257
54
|
end
|
258
|
-
|
259
|
-
@already_fetched_parents = true
|
260
55
|
end
|
56
|
+
@ontologies
|
261
57
|
end
|
262
|
-
|
263
|
-
#
|
264
|
-
#
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
object_term.term_name as parent_term
|
275
|
-
from
|
276
|
-
term_relationship tr
|
277
|
-
join term as subject_term on tr.subject_term_pk = subject_term.term_pk
|
278
|
-
join term as predicate_term on tr.predicate_term_pk = predicate_term.term_pk
|
279
|
-
join term as object_term on tr.object_term_pk = object_term.term_pk
|
280
|
-
where
|
281
|
-
predicate_term.term_name in ('part_of','is_a','develops_from')
|
282
|
-
and object_term.identifier = ?
|
283
|
-
SQL
|
284
|
-
|
285
|
-
OLS.ols_db[sql,node.term].each do |row|
|
286
|
-
child = OntologyTerm.new( row[:child_identifier], row[:child_term] )
|
287
|
-
child.leaf_node = true if row[:child_is_leaf].to_i == 1
|
288
|
-
child.get_children( child, true ) if recursively and !child.is_leaf?
|
289
|
-
node << child
|
58
|
+
|
59
|
+
# Fetch all the root terms for a given ontology
|
60
|
+
#
|
61
|
+
# @param [String/Symbol] ontology The short-name of the ontology
|
62
|
+
# @return [Array] An array of OLS::Term objects for all root terms the requested ontology
|
63
|
+
def root_terms(ontology)
|
64
|
+
root_terms = []
|
65
|
+
response = request(:get_root_terms) { soap.body = { :ontologyName => ontology } }
|
66
|
+
|
67
|
+
if response[:item].is_a? Array
|
68
|
+
response[:item].each do |term|
|
69
|
+
root_terms.push( OLS::Term.new(term[:key],term[:value]) )
|
290
70
|
end
|
291
|
-
|
292
|
-
|
71
|
+
else
|
72
|
+
term = response[:item]
|
73
|
+
root_terms.push( OLS::Term.new(term[:key],term[:value]) )
|
293
74
|
end
|
75
|
+
|
76
|
+
root_terms
|
294
77
|
end
|
295
|
-
|
296
|
-
#
|
297
|
-
# The original node remains attached to its tree.
|
78
|
+
|
79
|
+
# Fetch an ontology term (OLS::Term) by its id
|
298
80
|
#
|
299
|
-
# @
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
81
|
+
# @param [String/Symbol] term_id An ontology id to look for - i.e. 'GO:0023034'
|
82
|
+
# @return [OLS::Term] An OLS::Term object for the requested ontology id
|
83
|
+
# @raise OLS::TermNotFoundError Raised if the requested ontology id cannot be found
|
84
|
+
def find_by_id(term_id)
|
85
|
+
term_name = request(:get_term_by_id) { soap.body = { :termId => term_id } }
|
86
|
+
raise TermNotFoundError if term_name.eql?(term_id)
|
87
|
+
OLS::Term.new(term_id,term_name)
|
305
88
|
end
|
306
|
-
|
307
|
-
#
|
89
|
+
|
90
|
+
# Set whether to log HTTP requests - pass in +true+ or +false+
|
91
|
+
attr_writer :log
|
92
|
+
|
93
|
+
# Returns whether to log HTTP/SOAP requests. Defaults to +false+
|
308
94
|
#
|
309
|
-
# @
|
310
|
-
|
311
|
-
|
312
|
-
unless tree.is_a?(OLS::OntologyTerm)
|
313
|
-
raise TypeError, "You can only merge in another OntologyTerm tree!"
|
314
|
-
end
|
315
|
-
|
316
|
-
unless self.root.name == tree.root.name
|
317
|
-
raise ArgumentError, "Unable to merge trees as they do not share the same root!"
|
318
|
-
end
|
319
|
-
|
320
|
-
new_tree = merge_subtrees( self.root, tree.root )
|
95
|
+
# @return [Boolean] To log or not to log, that is the question...
|
96
|
+
def log?
|
97
|
+
@log ? true : false
|
321
98
|
end
|
322
|
-
|
323
|
-
private
|
324
|
-
|
325
|
-
# Utility function to recursivley merge two subtrees
|
326
|
-
#
|
327
|
-
# @param [OntologyTerm] tree1 The target tree to merge into
|
328
|
-
# @param [OntologyTerm] tree2 The donor tree (that will be merged into target)
|
329
|
-
def merge_subtrees( tree1, tree2 )
|
330
|
-
names1 = tree1.has_children? ? tree1.children.map { |child| child.name } : []
|
331
|
-
names2 = tree2.has_children? ? tree2.children.map { |child| child.name } : []
|
332
99
|
|
333
|
-
|
334
|
-
|
335
|
-
tree1 << tree2[name].detached_subtree_copy
|
336
|
-
end
|
100
|
+
# Set the logger to use
|
101
|
+
attr_writer :logger
|
337
102
|
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
end
|
103
|
+
# Returns the logger. Defaults to an instance of +Logger+ writing to STDOUT
|
104
|
+
def logger
|
105
|
+
@logger ||= ::Logger.new STDOUT
|
106
|
+
end
|
343
107
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
# Helper function to query the OLS database and grab the full
|
348
|
-
# details of the ontology term.
|
349
|
-
def get_term_details
|
350
|
-
# This query ensures we look at the most recent fully loaded ontologies
|
351
|
-
sql = <<-SQL
|
352
|
-
select term.*
|
353
|
-
from term
|
354
|
-
join ontology on ontology.ontology_id = term.ontology_id
|
355
|
-
where term.identifier = ?
|
356
|
-
order by ontology.fully_loaded desc, ontology.load_date asc
|
357
|
-
SQL
|
108
|
+
# Set the log level
|
109
|
+
attr_writer :log_level
|
358
110
|
|
359
|
-
|
111
|
+
# Return the log level. Defaults to :warn
|
112
|
+
def log_level
|
113
|
+
@log_level ||= :warn
|
114
|
+
end
|
360
115
|
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
@root_term = true if subject[:is_root_term].to_i == 1
|
369
|
-
@leaf_node = true if subject[:is_leaf].to_i == 1
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
# Helper function to try to find an ontology term via a synonym.
|
374
|
-
def get_term_from_synonym
|
375
|
-
sql = <<-SQL
|
376
|
-
select term.*
|
377
|
-
from term
|
378
|
-
join ontology on ontology.ontology_id = term.ontology_id
|
379
|
-
join term_synonym on term.term_pk = term_synonym.term_pk
|
380
|
-
where term_synonym.synonym_value = ?
|
381
|
-
order by ontology.fully_loaded desc, ontology.load_date asc
|
382
|
-
SQL
|
383
|
-
|
384
|
-
term_set = OLS.ols_db[ sql, @name ].all()
|
385
|
-
|
386
|
-
if term_set.size == 0
|
387
|
-
raise OLS::OntologyTermNotFoundError, "Unable to find the term '#{@name}' in the OLS database."
|
388
|
-
end
|
389
|
-
|
390
|
-
subject = term_set.first
|
391
|
-
@name = subject[:identifier]
|
392
|
-
@content = subject[:term_name]
|
393
|
-
@term_pk = subject[:term_pk]
|
394
|
-
@ontology_id = subject[:ontology_id]
|
395
|
-
@root_term = true if subject[:is_root_term].to_i == 1
|
396
|
-
@leaf_node = true if subject[:is_leaf].to_i == 1
|
397
|
-
end
|
398
|
-
|
399
|
-
# Helper function to produce the flat lists of all the child
|
400
|
-
# terms and names.
|
401
|
-
def get_all_child_lists
|
402
|
-
get_children
|
116
|
+
# Set a HTTP proxy to use
|
117
|
+
attr_writer :proxy
|
118
|
+
|
119
|
+
# Returns a HTTP proxy url. Will read the +http_proxy+ environment variable if present
|
120
|
+
def proxy
|
121
|
+
@proxy ||= ( ENV['http_proxy'] || ENV['HTTP_PROXY'] )
|
122
|
+
end
|
403
123
|
|
404
|
-
|
405
|
-
@all_child_terms = []
|
406
|
-
@all_child_names = []
|
124
|
+
private
|
407
125
|
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
126
|
+
# Helper function to initialize the (Savon) SOAP client
|
127
|
+
def setup_soap_client
|
128
|
+
Savon.configure do |config|
|
129
|
+
config.log = false unless OLS.log?
|
130
|
+
config.log_level = OLS.log_level
|
131
|
+
config.logger = OLS.logger
|
132
|
+
end
|
414
133
|
|
415
|
-
|
416
|
-
|
417
|
-
|
134
|
+
HTTPI.log = false unless OLS.log?
|
135
|
+
HTTPI.log_level = OLS.log_level
|
136
|
+
HTTPI.logger = OLS.logger
|
137
|
+
|
138
|
+
Savon::Client.new do |wsdl, http|
|
139
|
+
wsdl.document = "http://www.ebi.ac.uk/ontology-lookup/OntologyQuery.wsdl"
|
140
|
+
http.proxy = OLS.proxy unless OLS.proxy.nil?
|
418
141
|
end
|
419
|
-
|
142
|
+
end
|
420
143
|
end
|
421
|
-
|
422
144
|
end
|
145
|
+
|
146
|
+
directory = File.expand_path(File.dirname(__FILE__))
|
147
|
+
|
148
|
+
require File.join(directory, 'ols', 'version')
|
149
|
+
require File.join(directory, 'ols', 'graph')
|
150
|
+
require File.join(directory, 'ols', 'term')
|