Module: BioPortal::RestAPI

Included in:
BioportalConcept
Defined in:
lib/bioportal.rb

Instance Method Summary collapse

Instance Method Details

#fetch_json(link) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/bioportal.rb', line 154

def fetch_json link
  @@bioportal_json ||={}
  json = @@bioportal_json[link]
  if json.nil?
    json = JSON.parse(open(link).read)
    @@bioportal_json[link]=json
  end
  json
end

#get_concept(ontology_acronym, class_id, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bioportal.rb', line 103

def get_concept ontology_acronym,class_id,options={}
  url = "/ontologies/#{ontology_acronym}/classes/#{URI.escape(class_id,":/")}?"
  options.keys.each{|key| url += "#{key.to_s}=#{URI.encode(options[key].to_s)}&"}
  url=bioportal_base_rest_url+url
  json = fetch_json(url)
  concept={}
  concept[:label]=json["prefLabel"]
  concept[:synonyms]=json["synonym"]
  concept[:definitions]=json["definition"]
  concept
end

#populate_ontology_details(search_result, options) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/bioportal.rb', line 144

def populate_ontology_details search_result, options
  apikey = options[:apikey]
  link = search_result[:ontology_link]
  link = link+"?apikey=#{apikey}"
  json = fetch_json(link)

  search_result[:ontology_name]=json["name"]
  search_result[:ontology_acronym]=json["acronym"]
end

#search(query, options = {}) ⇒ Object



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
# File 'lib/bioportal.rb', line 116

def search query,options={}
  options[:pagesize] ||= 10
  options[:page] ||= 1
  
  search_url="/search?q=%QUERY%&"
  options.keys.each {|key| search_url+="#{key.to_s}=#{URI.encode(options[key].to_s)}&"}
  search_url=search_url[0..-2] #chop of trailing &
  
  search_url=search_url.gsub("%QUERY%",URI.encode(query))
  full_search_path=bioportal_base_rest_url+search_url

  json = fetch_json(full_search_path)

  results = json["collection"].collect do |item|
    res = {}
    res[:ontology_link]=item["links"]["ontology"]
    res[:class_link]=item["links"]["self"]
    res[:preferred_label]=item["prefLabel"]
    res[:synonyms]=item["synonym"] || []
    res[:class_id]=item["@id"]
    populate_ontology_details res,options
    res
  end
  pages = json["pageCount"]

  return results.uniq,pages.to_i
end