Class: NCBO::ResourceIndex

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

Defined Under Namespace

Classes: Annotation, Annotations, 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.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_annotations(element, concepts, resource, options = {}) ⇒ Object



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

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

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



58
59
60
# File 'lib/ncbo_resource_index.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.rb', line 73

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

.ontologies(options) ⇒ Object



200
201
202
# File 'lib/ncbo_resource_index.rb', line 200

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


178
179
180
# File 'lib/ncbo_resource_index.rb', line 178

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

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



146
147
148
# File 'lib/ncbo_resource_index.rb', line 146

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

.resources(options) ⇒ Object



215
216
217
# File 'lib/ncbo_resource_index.rb', line 215

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

.resources_hash(options) ⇒ Object



230
231
232
# File 'lib/ncbo_resource_index.rb', line 230

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

Instance Method Details

#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
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ncbo_resource_index.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
  
  "http://rest.bioontology.org/resource_index/details/false/virtual/concept/1032/resource/AE/0/9999?conceptid=Melanoma&elementid=E-GEOD-18509"
  
  concept_annotations = []
  concepts.each do |concept|
    split_concept = concept.split("/")
    ontology_id = split_concept[0]
    concept_id = split_concept[1]
    virtual = @options[:isVirtualOntologyId] ? "/virtual" : ""
    puts ["#{@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("")
    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.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.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



204
205
206
207
208
209
210
211
212
213
# File 'lib/ncbo_resource_index.rb', line 204

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



242
243
244
# File 'lib/ncbo_resource_index.rb', line 242

def options
  @options
end


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ncbo_resource_index.rb', line 182

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)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ncbo_resource_index.rb', line 150

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
  
  puts ["#{@options[:resource_index_location]}",
                     "elements-ranked-by-concepts/#{@options[:resourceids].join(",")}",
                     "?offset=#{@options[:offset]}",
                     "&limit=#{@options[:limit]}",
                     "&conceptids=#{@options[:conceptids].join(",")}",
                     "&ontologiesToKeepInResult=#{@options[:ontologiesToKeepInResult]}",
                     "&isVirtualOntologyId=#{@options[:isVirtualOntologyId]}",
                     "&apikey=#{@options[:apikey]}"].join("")
  result_xml = open(["#{@options[:resource_index_location]}",
                     "elements-ranked-by-concepts/#{@options[:resourceids].join(",")}",
                     "?offset=#{@options[:offset]}",
                     "&limit=#{@options[:limit]}",
                     "&conceptids=#{@options[:conceptids].join(",")}",
                     "&ontologiesToKeepInResult=#{@options[:ontologiesToKeepInResult]}",
                     "&isVirtualOntologyId=#{@options[:isVirtualOntologyId]}",
                     "&apikey=#{@options[:apikey]}"].join("")).read
  Parser::ResourceIndex.parse_ranked_element_results(result_xml)
end

#resources(options = {}) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/ncbo_resource_index.rb', line 219

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



234
235
236
237
238
239
240
# File 'lib/ncbo_resource_index.rb', line 234

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