Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: implement Instances goo model #129

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
41 changes: 22 additions & 19 deletions lib/ontologies_linked_data/config/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

module LinkedData
extend self

attr_reader :settings

@settings = OpenStruct.new
@settings_run = false

DEFAULT_PREFIX = 'http://data.bioontology.org/'.freeze

def config(&block)
return if @settings_run

@settings_run = true

overide_connect_goo = false
Expand Down Expand Up @@ -40,6 +44,7 @@ def config(&block)
@settings.ui_host ||= 'bioportal.bioontology.org'
@settings.replace_url_prefix ||= false
@settings.id_url_prefix ||= 'http://data.bioontology.org/'

@settings.queries_debug ||= false
@settings.enable_monitoring ||= false
@settings.cube_host ||= 'localhost'
Expand Down Expand Up @@ -92,11 +97,9 @@ def config(&block)
unless @settings.redis_host.nil?
puts "Error: 'redis_host' is not a valid conf parameter."
puts ' Redis databases were split into multiple hosts (09/22/13).'
raise Exception, 'redis_host is not a valid conf parameter.'
end

# Check to make sure url prefix has trailing slash
@settings.rest_url_prefix = @settings.rest_url_prefix + '/' unless @settings.rest_url_prefix[-1].eql?('/')
@settings.rest_url_prefix = "#{@settings.rest_url_prefix}/" unless @settings.rest_url_prefix[-1].eql?('/')

puts "(LD) >> Using rdf store #{@settings.goo_host}:#{@settings.goo_port}"
puts "(LD) >> Using term search server at #{@settings.search_server_url}"
Expand Down Expand Up @@ -134,15 +137,14 @@ def connect_goo
port: @settings.goo_redis_port)

if @settings.enable_monitoring
puts "(LD) >> Enable SPARQL monitoring with cube #{@settings.cube_host}:"+
"#{@settings.cube_port}"
puts "(LD) >> Enable SPARQL monitoring with cube #{@settings.cube_host}:#{@settings.cube_port}"
conf.enable_cube do |opts|
opts[:host] = @settings.cube_host
opts[:port] = @settings.cube_port
end
end
end
rescue Exception => e
rescue StandardError => e
abort("EXITING: Cannot connect to triplestore and/or search server:\n #{e}\n#{e.backtrace.join("\n")}")
end
end
Expand All @@ -152,26 +154,27 @@ def connect_goo
# We do this at initial runtime because goo needs namespaces for its DSL
def goo_namespaces
Goo.configure do |conf|
conf.add_namespace(:omv, RDF::Vocabulary.new("http://omv.ontoware.org/2005/05/ontology#"))
conf.add_namespace(:skos, RDF::Vocabulary.new("http://www.w3.org/2004/02/skos/core#"))
conf.add_namespace(:owl, RDF::Vocabulary.new("http://www.w3.org/2002/07/owl#"))
conf.add_namespace(:rdfs, RDF::Vocabulary.new("http://www.w3.org/2000/01/rdf-schema#"))
conf.add_namespace(:omv, RDF::Vocabulary.new('http://omv.ontoware.org/2005/05/ontology#'))
conf.add_namespace(:skos, RDF::Vocabulary.new('http://www.w3.org/2004/02/skos/core#'))
conf.add_namespace(:owl, RDF::Vocabulary.new('http://www.w3.org/2002/07/owl#'))
conf.add_namespace(:rdf, RDF::Vocabulary.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#'))
conf.add_namespace(:rdfs, RDF::Vocabulary.new('http://www.w3.org/2000/01/rdf-schema#'))
conf.add_namespace(:metadata,
RDF::Vocabulary.new("http://data.bioontology.org/metadata/"),
RDF::Vocabulary.new('http://data.bioontology.org/metadata/'),
default = true)
conf.add_namespace(:metadata_def,
RDF::Vocabulary.new("http://data.bioontology.org/metadata/def/"))
conf.add_namespace(:dc, RDF::Vocabulary.new("http://purl.org/dc/elements/1.1/"))
conf.add_namespace(:xsd, RDF::Vocabulary.new("http://www.w3.org/2001/XMLSchema#"))
RDF::Vocabulary.new('http://data.bioontology.org/metadata/def/'))
conf.add_namespace(:dc, RDF::Vocabulary.new('http://purl.org/dc/elements/1.1/'))
conf.add_namespace(:xsd, RDF::Vocabulary.new('http://www.w3.org/2001/XMLSchema#'))
conf.add_namespace(:oboinowl_gen,
RDF::Vocabulary.new("http://www.geneontology.org/formats/oboInOwl#"))
conf.add_namespace(:obo_purl, RDF::Vocabulary.new("http://purl.obolibrary.org/obo/"))
RDF::Vocabulary.new('http://www.geneontology.org/formats/oboInOwl#'))
conf.add_namespace(:obo_purl, RDF::Vocabulary.new('http://purl.obolibrary.org/obo/'))
conf.add_namespace(:umls,
RDF::Vocabulary.new("http://bioportal.bioontology.org/ontologies/umls/"))
conf.id_prefix = "http://data.bioontology.org/"
RDF::Vocabulary.new('http://bioportal.bioontology.org/ontologies/umls/'))
conf.id_prefix = DEFAULT_PREFIX
conf.pluralize_models(true)
end
end
self.goo_namespaces
goo_namespaces

end
181 changes: 47 additions & 134 deletions lib/ontologies_linked_data/models/instance.rb
Original file line number Diff line number Diff line change
@@ -1,156 +1,69 @@

module LinkedData
module Models
class Instance
include LinkedData::Hypermedia::Resource
include LinkedData::HTTPCache::CacheableResource
class Instance < LinkedData::Models::Base

attr_reader :id, :properties
attr_accessor :label
serialize_default :id, :label, :properties
model :named_individual, name_with: :id, collection: :submission,
namespace: :owl, schemaless: :true , rdf_type: lambda { |*x| RDF::OWL[:NamedIndividual]}

# HTTP cache settings.
cache_timeout 14400
attribute :label, namespace: :rdfs, enforce: [:list]
attribute :prefLabel, namespace: :skos, enforce: [:existence], alias: true

def initialize(id,label,properties)
@id = id
if label.nil?
sep = "/"
if not id.to_s["#"].nil?
sep = "#"
end
label = id.to_s.split(sep).last
end
@label = label
@properties = properties
end
attribute :types, namespace: :rdf, enforce: [:list], property: :type
attribute :submission, collection: lambda { |s| s.resource_id }, namespace: :metadata

def add_property_value(p,o)
ps = p.to_s
if not @properties.include?(ps)
@properties[ps] = []
end
@properties[ps] << o
end
serialize_never :submission, :id
serialize_methods :properties

cache_timeout 14400

def self.type_uri
LinkedData.settings.id_url_prefix+"metadata/Instance"
def properties
self.unmapped
end

end
end

module InstanceLoader
def self.count_instances_by_class(submission_id,class_id)
query = <<-eos
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT (count(DISTINCT ?s) as ?c) WHERE
{
GRAPH <#{submission_id.to_s}> {
?s a owl:NamedIndividual .
?s a <#{class_id.to_s}> .
}
}
eos
epr = Goo.sparql_query_client(:main)
graphs = [submission_id]
resultset = epr.query(query, graphs: graphs)
resultset.each do |r|
return r[:c].object
end
return 0
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
s = LinkedData::Models::OntologySubmission.find(submission_id).first
instances_by_class_where_query(s, class_id: class_id).count
end

def self.get_instances_by_class(submission_id,class_id)
query = <<-eos
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?s ?label WHERE
{
GRAPH <#{submission_id.to_s}> {
?s a owl:NamedIndividual .
?s a <#{class_id.to_s}> .
}
}
eos
epr = Goo.sparql_query_client(:main)
graphs = [submission_id]
resultset = epr.query(query, graphs: graphs)
instances = []
resultset.each do |r|
inst = LinkedData::Models::Instance.new(r[:s],nil,{})
instances << inst
end

if instances.empty?
return []
end

include_instance_properties(submission_id,instances)
return instances
def self.get_instances_by_class(submission_id, class_id, page_no: nil, size: nil)
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
s = LinkedData::Models::OntologySubmission.find(submission_id).first

inst = instances_by_class_where_query(s, class_id: class_id, page_no: page_no, size: size).all

# TODO test if "include=all" parameter is passed in the request
# For getting all the properties # For getting all the properties
load_unmapped s,inst unless inst.nil? || inst.empty?
inst
end

def self.get_instances_by_ontology(submission_id,page_no,size)
query = <<-eos
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?s ?label WHERE
{
GRAPH <#{submission_id.to_s}> {
?s a owl:NamedIndividual .
}
}
eos
epr = Goo.sparql_query_client(:main)
graphs = [submission_id]
resultset = epr.query(query, graphs: graphs)

total_size = resultset.size
range_start = (page_no - 1) * size
range_end = (page_no * size) - 1
resultset = resultset[range_start..range_end]

instances = []
resultset.each do |r|
inst = LinkedData::Models::Instance.new(r[:s],r[:label],{})
instances << inst
end unless resultset.nil?

if instances.size > 0
include_instance_properties(submission_id,instances)
end

page = Goo::Base::Page.new(page_no,size,total_size,instances)
return page
def self.get_instances_by_ontology(submission_id, page_no: nil, size: nil)
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
s = LinkedData::Models::OntologySubmission.find(submission_id).first
inst = s.nil? ? [] : instances_by_class_where_query(s, page_no: page_no, size: size).all

## TODO test if "include=all" parameter is passed in the request
load_unmapped s, inst unless inst.nil? || inst.empty? # For getting all the properties
inst
end

def self.include_instance_properties(submission_id,instances)
index = Hash.new
instances.each do |inst|
index[inst.id.to_s] = inst
end
uris = index.keys.map { |x| x.to_s }
uri_filter = uris.map { |x| "?s = <#{x}>"}.join(" || ")

query = <<-eos
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?s ?p ?o WHERE
{
GRAPH <#{submission_id.to_s}> {
?s ?p ?o .
}
FILTER( #{uri_filter} )
}
eos
epr = Goo.sparql_query_client(:main)
graphs = [submission_id]
resultset = epr.query(query, graphs: graphs)
resultset.each do |sol|
s = sol[:s]
p = sol[:p]
o = sol[:o]
if not p.to_s["label"].nil?
index[s.to_s].label = o.to_s
else
index[s.to_s].add_property_value(p,o)
end
end
def self.instances_by_class_where_query(submission, class_id: nil, page_no: nil, size: nil)
where_condition = class_id.nil? ? nil : {types: RDF::URI.new(class_id.to_s)}
query = LinkedData::Models::Instance.where(where_condition).in(submission).include(:types, :label, :prefLabel)
query.page(page_no, size) unless page_no.nil?
query
end

def self.load_unmapped(submission, models)
LinkedData::Models::Instance.where.in(submission).models(models).include(:unmapped).all
end


end
end
Loading