oba-client 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/History.md +3 -0
  2. data/README.md +4 -1
  3. data/lib/oba-client.rb +53 -40
  4. metadata +3 -3
data/History.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 2.0.2 / 2010-07-08
2
+ Parse version and nbAnnotation for ontologies.
3
+
1
4
  ## 2.0.1 / 2010-07-08
2
5
 
3
6
  Rubyforge doesn't allow "_", so we have "oba-client", not "oba_client".
data/README.md CHANGED
@@ -67,7 +67,10 @@ See [the Annotator documentation](http://www.bioontology.org/wiki/index.php/Anno
67
67
  :ontologies => [{
68
68
  :localOntologyId => 40404,
69
69
  :name => "Ontology Name",
70
- :virtualOntologyId => 1042
70
+ :virtualOntologyId => 1042,
71
+ :version => 1.1,
72
+ :nbAnnotation => 40
73
+
71
74
  }, "more ontologies..."]
72
75
 
73
76
  client2.execute("another text string, maybe longer this time.")
data/lib/oba-client.rb CHANGED
@@ -4,18 +4,26 @@ require "cgi"
4
4
  require "net/http"
5
5
  require "uri"
6
6
 
7
+ ##
8
+ # A class for interacting with the Open Biomedical Annotator. There are two
9
+ # things we do: get text, and parse it. We can do both independently or
10
+ # serially.
7
11
  class OBAClient
8
- VERSION = "2.0.1"
12
+ VERSION = "2.0.2"
9
13
 
14
+ ##
10
15
  # A high HTTP read timeout, as the service sometimes takes awhile to respond.
11
16
  DEFAULT_TIMEOUT = 30
12
17
 
18
+ ##
13
19
  # The endpoint URI for the production version of the Annotator service.
14
20
  DEFAULT_URI = "http://rest.bioontology.org/obs/annotator"
15
21
 
22
+ ##
16
23
  # The header for every request. There's no need to specify this per-instance.
17
24
  HEADER = {"Content-Type" => "application/x-www-form-urlencoded"}
18
25
 
26
+ ##
19
27
  # Parameters the annotator accepts. Any one not in this list (excluding
20
28
  # textToAnnotate) is not valid.
21
29
  ANNOTATOR_PARAMETERS = [
@@ -38,10 +46,7 @@ class OBAClient
38
46
  :withSynonyms,
39
47
  ]
40
48
 
41
- STATISTICS_BEANS_XPATH = "/success/data/annotatorResultBean/statistics/statisticsBean"
42
- ANNOTATION_BEANS_XPATH = "/success/data/annotatorResultBean/annotations/annotationBean"
43
- ONTOLOGY_BEANS_XPATH = "/success/data/annotatorResultBean/ontologies/ontologyUsedBean"
44
-
49
+ ##
45
50
  # Instantiate the class with a set of reused options. Options used by the
46
51
  # method are:
47
52
  #
@@ -59,7 +64,7 @@ class OBAClient
59
64
  if ontologies = options.delete(:ontologies)
60
65
  [:ontologiesToExpand, :ontologiesToKeepInResult].each do |k|
61
66
  if options.include?(k)
62
- puts "WARNING: specified both :ontologies and #{k}, ignoring given value of #{k}."
67
+ puts "WARNING: specified both :ontologies and #{k}, ignoring #{k}."
63
68
  end
64
69
  options[k] = ontologies
65
70
  end
@@ -78,10 +83,11 @@ class OBAClient
78
83
  end
79
84
 
80
85
  if !@options.include?(:email)
81
- puts "TIP: as a courtesy, consider including your email in the request."
86
+ puts "TIP: as a courtesy, consider including your email in the request (:email => 'a@b.com')"
82
87
  end
83
88
  end
84
89
 
90
+ ##
85
91
  # Perform the annotation.
86
92
  # @param [String] text The text to annotate.
87
93
  # @return [Hash<Symbol, Array>, String, nil] A Hash representing the parsed
@@ -105,21 +111,14 @@ class OBAClient
105
111
  end
106
112
  end
107
113
 
108
- # Convert a string true/false or 1/0 value to boolean.
109
- # @param [String] value The value to convert.
110
- # @return [true, false]
111
- def self.to_b(value)
112
- case value
113
- when "0" then false
114
- when "1" then true
115
- when "false" then false
116
- when "true" then true
117
- end
118
- end
114
+
115
+ STATISTICS_BEANS_XPATH = "/success/data/annotatorResultBean/statistics/statisticsBean"
116
+ ANNOTATION_BEANS_XPATH = "/success/data/annotatorResultBean/annotations/annotationBean"
117
+ ONTOLOGY_BEANS_XPATH = "/success/data/annotatorResultBean/ontologies/ontologyUsedBean"
119
118
 
119
+ ##
120
120
  # Attributes for mapping concepts (annotation concepts add one additional
121
- # attribute.
122
- # @see ANNOTATION_CONCEPT_ATTRIBUTES
121
+ # attribute. See also {ANNOTATION_CONCEPT_ATTRIBUTES}.
123
122
  CONCEPT_ATTRIBUTES = {
124
123
  :id => lambda {|c| c.xpath("id").text.to_i},
125
124
  :localConceptId => lambda {|c| c.xpath("localConceptId").text},
@@ -145,31 +144,33 @@ class OBAClient
145
144
  end
146
145
  }
147
146
 
148
- # Annotation concepts have the same attributes as mapping concepts, plus one.
149
- ANNOTATION_CONCEPT_ATTRIBUTES = CONCEPT_ATTRIBUTES.merge(
150
- :mappingType => lambda {|c| c.xpath("mappingType").text}
151
- )
152
-
153
- # Toplevel attributes for annotation contexts.
154
- ANNOTATION_CONTEXT_ATTRIBUTES = {
155
- :score => lambda {|c| c.xpath("score").text.to_i},
156
- :concept => lambda {|c| parse_concept(c.xpath("concept").first)},
157
- :context => lambda {|c| parse_context(c.xpath("context").first)}
158
- }
159
147
 
160
- # Toplevel attributes for all other contexts.
148
+ ##
149
+ # Toplevel attributes for mapping and mgrep contexts (both will add
150
+ # additional attributes).
161
151
  CONTEXT_ATTRIBUTES = {
162
152
  :contextName => lambda {|c| c.xpath("contextName").text},
163
153
  :isDirect => lambda {|c| to_b(c.xpath("isDirect").text)},
164
154
  :from => lambda {|c| c.xpath("from").text.to_i},
165
155
  :to => lambda {|c| c.xpath("to").text.to_i},
166
156
  }
157
+
158
+ ##
159
+ # Toplevel attributes for annotation contexts.
160
+ ANNOTATION_CONTEXT_ATTRIBUTES = {
161
+ :score => lambda {|c| c.xpath("score").text.to_i},
162
+ :concept => lambda {|c| parse_concept(c.xpath("concept").first)},
163
+ :context => lambda {|c| parse_context(c.xpath("context").first)}
164
+ }
167
165
 
166
+ ##
168
167
  # Toplevel attributes for mapping contexts.
169
168
  MAPPED_CONTEXT_ATTRIBUTES = CONTEXT_ATTRIBUTES.merge(
169
+ :mappingType => lambda {|c| c.xpath("mappingType").text},
170
170
  :mappedConcept => lambda {|c| parse_concept(c.xpath("mappedConcept").first)}
171
171
  )
172
172
 
173
+ ##
173
174
  # Toplevel attributes for mgrep contexts.
174
175
  MGREP_CONTEXT_ATTRIBUTES = CONTEXT_ATTRIBUTES.merge(
175
176
  :name => lambda {|c| c.xpath("term/name").text},
@@ -178,11 +179,6 @@ class OBAClient
178
179
  :dictionaryId => lambda {|c| c.xpath("term/dictionaryId").text}
179
180
  )
180
181
 
181
- CONCEPT_TYPES = {
182
- "concept" => ANNOTATION_CONCEPT_ATTRIBUTES,
183
- "mappedConcept" => CONCEPT_ATTRIBUTES
184
- }
185
-
186
182
  CONTEXT_CLASSES = {
187
183
  "annotationContextBean" => ANNOTATION_CONTEXT_ATTRIBUTES,
188
184
  "mgrepContextBean" => MGREP_CONTEXT_ATTRIBUTES,
@@ -195,7 +191,8 @@ class OBAClient
195
191
  # @return Hash<Symbol, Object> The parsed context.
196
192
  def self.parse_context(context)
197
193
  # Annotations (annotationBeans) do not have a class, so we'll refer to them
198
- # as annotationContextBeans
194
+ # as annotationContextBeans. context_class will be one of the types in
195
+ # {CONTEXT_CLASSES}.
199
196
  context_class = if context.attribute("class").nil?
200
197
  "annotationContextBean"
201
198
  else
@@ -213,7 +210,7 @@ class OBAClient
213
210
  # @param [Nokogiri::XML::Node] concept The root node of the concept.
214
211
  # @return [Hash<Symbol, Object>] The parsed concept.
215
212
  def self.parse_concept(concept)
216
- Hash[CONCEPT_TYPES[concept.name].map do |k, v|
213
+ Hash[CONCEPT_ATTRIBUTES.map do |k, v|
217
214
  [k, v.call(concept)]
218
215
  end]
219
216
  end
@@ -243,7 +240,9 @@ class OBAClient
243
240
  {
244
241
  :localOntologyId => ontology.xpath("localOntologyId").text.to_i,
245
242
  :virtualOntologyId => ontology.xpath("virtualOntologyId").text.to_i,
246
- :name => ontology.xpath("name").text
243
+ :name => ontology.xpath("name").text,
244
+ :version => ontology.xpath("version").text.to_f,
245
+ :nbAnnotation => ontology.xpath("nbAnnotation").text.to_i
247
246
  }
248
247
  end
249
248
 
@@ -253,4 +252,18 @@ class OBAClient
253
252
  :ontologies => ontologies
254
253
  }
255
254
  end
255
+
256
+ ##
257
+ # A little helper: convert a string true/false or 1/0 value to boolean.
258
+ # AFAIK, there's no better way to do this.
259
+ # @param [String] value The value to convert.
260
+ # @return [true, false]
261
+ def self.to_b(value)
262
+ case value
263
+ when "0" then false
264
+ when "1" then true
265
+ when "false" then false
266
+ when "true" then true
267
+ end
268
+ end
256
269
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oba-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 1
10
- version: 2.0.1
9
+ - 2
10
+ version: 2.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rob Tirrell