Class: NCBO::ResourceIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ncbo_resource_index_client.rb,
lib/ncbo_resource_index_client/data.rb

Defined Under Namespace

Classes: Annotation, Annotations, Element, RankedElements

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ResourceIndex

Returns a new instance of ResourceIndex.

Raises:

  • (ArgumentError)


13
14
15
16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ncbo_resource_index_client.rb', line 13

def initialize(args = {})
  @options = {}
  
  # Shared with Annotator
  @options[:resource_index_location] = "http://rest.bioontology.org/resource_index/"
  @options[:filterNumber] = true
  @options[:isStopWordsCaseSensitive] = false
  @options[:isVirtualOntologyId] = true
  @options[:levelMax] = 0
  @options[:longestOnly] = false
  @options[:ontologiesToExpand] = []
  @options[:ontologiesToKeepInResult] = []
  @options[:mappingTypes] = []
  @options[:minTermSize] = 3
  @options[:scored] = true
  @options[:semanticTypes] = []
  @options[:stopWords] = []
  @options[:wholeWordOnly] = true
  @options[:withDefaultStopWords] = true
  @options[:withSynonyms] = true
  
  # RI-specific
  @options[:conceptids] = []
  @options[:mode] = :union
  @options[:elementid] = []
  @options[:resourceids] = []
  @options[:elementDetails] = false
  @options[:withContext] = true
  @options[:offset] = 0
  @options[:limit] = 10
  @options[:format] = :xml
  @options[:counts] = false
  @options[:request_timeout] = 300
  
  @options.merge!(args)
  
  @ontologies = nil
  @options[:resourceids] ||= []
  
  # Check to make sure mappingTypes are capitalized
  fix_params

  raise ArgumentError, ":apikey is required, you can obtain one at http://bioportal.bioontology.org/accounts/new" if @options[:apikey].nil?
end

Class Method Details

.element(element_id, resource_id, options = {}) ⇒ Object



134
135
136
# File 'lib/ncbo_resource_index_client.rb', line 134

def self.element(element_id, resource_id, options = {})
  new(options).element(element_id, resource_id)
end

.element_annotations(element, concepts, resource, options = {}) ⇒ Object



87
88
89
# File 'lib/ncbo_resource_index_client.rb', line 87

def self.element_annotations(element, concepts, resource, options = {})
  new(options).element_annotations(element, concepts, resource)
end

.find_by_concept(concepts, options = {}) ⇒ Object



58
59
60
# File 'lib/ncbo_resource_index_client.rb', line 58

def self.find_by_concept(concepts, options = {})
  new(options).find_by_concept(concepts)
end

.find_by_element(element, resource, options = {}) ⇒ Object



73
74
75
# File 'lib/ncbo_resource_index_client.rb', line 73

def self.find_by_element(element, resource, options = {})
  new(options).find_by_element(element, resource)
end

.ontologies(options = {}) ⇒ Object



203
204
205
# File 'lib/ncbo_resource_index_client.rb', line 203

def self.ontologies(options = {})
  new(options).ontologies
end


181
182
183
# File 'lib/ncbo_resource_index_client.rb', line 181

def self.popular_concepts(resources = nil, options = {})
  new(options).popular_concepts(resources)
end

.ranked_elements(concepts, options = {}) ⇒ Object



155
156
157
# File 'lib/ncbo_resource_index_client.rb', line 155

def self.ranked_elements(concepts, options = {})
  new(options).ranked_elements(concepts)
end

.resources(options) ⇒ Object



218
219
220
# File 'lib/ncbo_resource_index_client.rb', line 218

def self.resources(options)
  new(options).resources
end

.resources_hash(options) ⇒ Object



233
234
235
# File 'lib/ncbo_resource_index_client.rb', line 233

def self.resources_hash(options)
  new(options).resources_hash
end

Instance Method Details

#element(element_id, resource_id) ⇒ Object

Raises:

  • (ArugmentError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ncbo_resource_index_client.rb', line 138

def element(element_id, resource_id)
  @options.merge!(options) unless options.empty?
  
  raise ArugmentError, "element_id must be provided" if element_id.nil?
  raise ArugmentError, "resource_id must be provided" if resource_id.nil?

  result_url = [
    "#{@options[:resource_index_location]}",
    "elementDetails/#{resource_id}",
    "?elementid=#{element_id}",
    "&apikey=#{@options[:apikey]}"
  ].join("")
  
  result_xml = open(result_url).read
  Parser::ResourceIndex.parse_element_results(result_xml)
end

#element_annotations(element, concepts, resource) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ncbo_resource_index_client.rb', line 91

def element_annotations(element, concepts, resource)
  @options[:conceptids] = concepts unless concepts.nil? || concepts.empty?
  raise ArgumentError, ":conceptids must be included" if @options[:conceptids].nil? || @options[:conceptids].empty?
  raise ArgumentError, ":resourceids must be an array" unless @options[:resourceids].kind_of? Array
  resource = resource.upcase
  
  concept_annotations = []
  concepts.each do |concept|
    split_concept = concept.split("/")
    ontology_id = split_concept[0]
    concept_id = split_concept[1]
    virtual = @options[:isVirtualOntologyId] ? "/virtual" : ""
    result_xml = open(["#{@options[:resource_index_location]}",
                       "details/#{@options[:elementDetails]}",
                       virtual,
                       "/concept/#{ontology_id}",
                       "/resource/#{resource}",
                       "/#{@options[:offset]}",
                       "/#{@options[:limit]}",
                       "?conceptid=#{CGI.escape(concept_id)}",
                       "&elementid=#{CGI.escape(element)}",
                       "&apikey=#{@options[:apikey]}"].join("")).read

    annotations = Parser::ResourceIndex.parse_element_annotations(result_xml)
    concept_annotations << annotations
  end

  if concept_annotations.length > 1
    # Merge the two result sets
    primary_annotations = Annotations.new
    primary_annotations.annotations = []
    primary_annotations.resource = resource
    concept_annotations.each do |result|
      primary_annotations.annotations.concat result.annotations
    end
  elsif concept_annotations.length == 1
    primary_annotations = concept_annotations[0]
  else
    primary_annotations = nil
  end
  primary_annotations
end

#find_by_concept(concepts = [], options = {}) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
# File 'lib/ncbo_resource_index_client.rb', line 62

def find_by_concept(concepts = [], options = {})
  @options[:conceptids] = concepts unless concepts.nil? || concepts.empty?
  @options.merge!(options) unless options.empty?
  fix_params
  
  raise ArgumentError, ":conceptids must be included" if @options[:conceptids].nil? || @options[:conceptids].empty?
  
  result_xml = resource_index_post
  Parser::ResourceIndex.parse_results(result_xml)
end

#find_by_element(element, resource, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
# File 'lib/ncbo_resource_index_client.rb', line 77

def find_by_element(element, resource, options = {})
  @options[:elementid] = element unless element.nil? || element.empty?
  @options[:resourceids] = [resource] unless resource.nil? || resource.empty?
  @options.merge!(options) unless options.empty?
  fix_params
  raise ArgumentError, ":elementid must be included" if @options[:elementid].nil? || @options[:elementid].empty?
  raise ArgumentError, ":resourceids must be included" if @options[:resourceids].nil? || @options[:resourceids].empty?
  Parser::ResourceIndex.parse_results(resource_index_post)
end

#ontologies(options = {}) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/ncbo_resource_index_client.rb', line 207

def ontologies(options = {})
  @options.merge!(options) unless options.empty?

  if @ontologies.nil?
    ontologies_xml = open("#{@options[:resource_index_location]}ontologies?apikey=#{@options[:apikey]}").read
    @ontologies = Parser::ResourceIndex.parse_included_ontologies(ontologies_xml)
  else
    @ontologies
  end
end

#optionsObject



245
246
247
# File 'lib/ncbo_resource_index_client.rb', line 245

def options
  @options
end


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ncbo_resource_index_client.rb', line 185

def popular_concepts(resources = nil, options = {})
  @options[:resourceids] = resources
  @options[:resourceids] = [resources] unless resources.nil? || resources.empty? || resources.kind_of?(Array)
  @options.merge!(options) unless options.empty?
  fix_params
  
  if @options[:resourceids].nil? || @options[:resourceids].empty?
    @options[:resourceids] = self.resources.collect {|resource| resource[:resourceId]}
  end
  
  popular_concepts = {}
  @options[:resourceids].each do |resource|
    popular_concepts_xml = open("#{@options[:resource_index_location]}most-used-concepts/#{resource}?apikey=#{@options[:apikey]}&offset=#{@options[:offset]}&limit=#{@options[:limit]}").read
    popular_concepts[resource] = Parser::ResourceIndex.parse_popular_concepts(popular_concepts_xml)
  end
  popular_concepts
end

#ranked_elements(concepts = [], options = {}) ⇒ Object

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ncbo_resource_index_client.rb', line 159

def ranked_elements(concepts = [], options = {})
  @options[:conceptids] = concepts unless concepts.nil? || concepts.empty?
  @options[:resourceids] ||= []
  @options.merge!(options) unless options.empty?
  fix_params
  
  raise ArgumentError, ":conceptids must be included" if @options[:conceptids].nil? || @options[:conceptids].empty?
  raise ArgumentError, ":resourceids must be an array" unless @options[:resourceids].kind_of? Array
  
  result_url = ["#{@options[:resource_index_location]}",
                     "elements-ranked-by-concepts/#{@options[:resourceids].join(",")}",
                     "?offset=#{@options[:offset]}",
                     "&limit=#{@options[:limit]}",
                     "&conceptids=#{@options[:conceptids].join(",")}",
                     "&ontologiesToKeepInResult=#{@options[:ontologiesToKeepInResult].join(",")}",
                     "&isVirtualOntologyId=#{@options[:isVirtualOntologyId]}",
                     "&apikey=#{@options[:apikey]}",
                     "&mode=#{@options[:mode]}"].join("")
  result_xml = open(result_url).read
  Parser::ResourceIndex.parse_ranked_element_results(result_xml)
end

#resources(options = {}) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'lib/ncbo_resource_index_client.rb', line 222

def resources(options = {})
  @options.merge!(options) unless options.empty?

  if @resources.nil?
    resources_xml = open("#{@options[:resource_index_location]}resources?apikey=#{@options[:apikey]}").read
    @resources = Parser::ResourceIndex.parse_resources(resources_xml)
  else
    @resources
  end
end

#resources_hash(options = {}) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/ncbo_resource_index_client.rb', line 237

def resources_hash(options = {})
  @options.merge!(options) unless options.empty?
  resources = resources()
  resources_hash = {}
  resources.each {|res| resources_hash[res[:resourceId].downcase.to_sym] = res}
  resources_hash
end