From ca2e240e03a4e458f83281c28b529e44af81d132 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Sat, 19 Dec 2009 13:28:55 +0100 Subject: OWL-DL for fminer and datasets fixed --- lib/dataset.rb | 167 ++++++++++++++++++++++++--------------------------------- 1 file changed, 71 insertions(+), 96 deletions(-) (limited to 'lib/dataset.rb') diff --git a/lib/dataset.rb b/lib/dataset.rb index b96f3e9..225b900 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -7,17 +7,57 @@ module OpenTox super end - def add(compound_uri,feature_uri,value) - c = self.find_or_create_compound compound_uri - f = self.find_or_create_feature feature_uri - v = self.find_or_create_value value - self.add_data_entry(c,f,v) + # create/add to entry from uris or Redland::Resources + # TODO add tuple + def add(compound,feature,value) + compound = self.find_or_create_compound compound unless compound.class == Redland::Resource + feature = self.find_or_create_feature feature unless feature.class == Redland::Resource + data_entry = @model.subject OT['compound'], compound + if data_entry.nil? + data_entry = @model.create_resource + @model.add data_entry, RDF['type'], OT["DataEntry"] + @model.add data_entry, OT['compound'], compound + end + values = @model.create_resource + @model.add data_entry, OT['values'], values + @model.add values, RDF['type'], OT['FeatureValue'] + @model.add values, OT['feature'], feature + @model.add values, OT['value'], value.to_s + end + + def add_tuple(compound,tuple) + compound = self.find_or_create_compound compound unless compound.class == Redland::Resource + data_entry = @model.subject OT['compound'], compound + if data_entry.nil? + data_entry = @model.create_resource + @model.add data_entry, RDF['type'], OT["DataEntry"] + @model.add data_entry, OT['compound'], compound + end + @model.add data_entry, OT['values'], tuple + end + + def create_tuple(feature,t) + feature = self.find_or_create_feature feature unless feature.class == Redland::Resource + tuple = @model.create_resource + @model.add tuple, RDF['type'], OT["Tuple"] + @model.add tuple, OT['feature'], feature + t.each do |name,value| + f = self.find_or_create_feature name unless name.class == Redland::Resource + complex_value = @model.create_resource + feature = self.find_or_create_feature(name) + @model.add tuple, OT['complexValue'], complex_value + @model.add complex_value, RDF['type'], OT["FeatureValue"] + @model.add complex_value, OT['feature'], f + @model.add complex_value, OT['value'], value.to_s + end + tuple end # find or create a new compound and return the resource def find_or_create_compound(uri) compound = @model.subject(DC["identifier"], uri) if compound.nil? + #puts uri compound = @model.create_resource @model.add compound, RDF['type'], OT["Compound"] @model.add compound, DC["identifier"], uri @@ -32,13 +72,14 @@ module OpenTox feature = @model.create_resource @model.add feature, RDF['type'], OT["Feature"] @model.add feature, DC["identifier"], uri - @model.add feature, DC["title"], File.basename(uri) + @model.add feature, DC["title"], File.basename(uri).split(/#/)[1] @model.add feature, DC['source'], uri end feature end # find or create a new value and return the resource +=begin def find_or_create_value(v) value = @model.subject OT["value"], v.to_s if value.nil? @@ -48,47 +89,9 @@ module OpenTox end value end +=end - def tuple?(t) - statements = [] - has_tuple = true - t.each do |name,v| - feature = self.find_or_create_feature(:name => name) - value = self.find_or_create_value(v) - tuple = @model.subject(feature,value) - has_tuple = false if tuple.nil? - statements << [tuple,feature,value] - end - tuples_found = statements.collect{|s| s[0]}.uniq - has_tuple = false unless tuples_found.size == 1 - has_tuple - end - - def create_tuple(t) - tuple = @model.create_resource - @model.add tuple, RDF['type'], OT["Tuple"] - t.each do |name,value| - feature = self.find_or_create_feature(:name => name) - value = self.find_or_create_value(value) - pair = @model.create_resource - @model.add pair, RDF['type'], OT['FeatureValue'] - @model.add tuple, OT['complexValue'], pair - @model.add pair, OT['feature'], feature - @model.add pair, OT['value'], value #FIX - #@model.add tuple, OT['feature'], feature - #@model.add tuple, OT['values'], value - end - tuple - end - - def find_or_create_tuple(t) - if self.tuple?(t) - t - else - self.create_tuple(t) - end - end - +=begin def add_data_entry(compound,feature,value) data_entry = @model.create_resource @model.add data_entry, RDF['type'], OT["DataEntry"] @@ -96,6 +99,7 @@ module OpenTox @model.add data_entry, OT['feature'], feature @model.add data_entry, OT['values'], value end +=end def self.create(data, content_type = 'application/rdf+xml') uri = RestClient.post @@config[:services]["opentox-dataset"], data, :content_type => content_type @@ -254,57 +258,28 @@ module OpenTox end end - - -# def tanimoto(dataset) -# RestClient.get(File.join(@uri,'tanimoto',dataset.path)) -# end -# -# def weighted_tanimoto(dataset) -# RestClient.get(File.join(@uri,'weighted_tanimoto',dataset.path)) -# end =begin - def data_entries - data = {} - @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node| - compound = @model.object(compound_node, DC["identifier"]).to_s#.sub(/^\[(.*)\]$/,'\1') - #compound = OpenTox::Compound.new(:inchi => compound).smiles - data[compound] = [] unless data[compound] - #puts compound - @model.subjects(OT['compound'], compound_node).each do |data_entry| - feature_node = @model.object(data_entry, OT['feature']) - feature = @model.object(feature_node, DC["identifier"]).to_s - values_node = @model.object(data_entry, OT['values']) - type = @model.object(values_node,RDF['type']).to_s - case type - when /FeatureValue/ - @model.find(values_node, OT['value'], nil) do |s,p,value| - case value.to_s - when "true" - data[compound] << {feature => true} - when "false" - data[compound] << {feature => false} - else - data[compound] << {feature => value.to_s} - end - end - when /Tuple/ # this is really slow - t = {} - @model.find(values_node, OT['tuple'], nil) do |s,p,tuple| - @model.find(tuple, OT['feature'], nil) do |s,p,feature| - @name = @model.object(feature,DC['title']).to_s - end - @model.find(tuple, OT['value'], nil) do |s,p,value| - v = @model.object(value,OT['value']).to_s - t[@name] = v - #print @name + ": " - #puts v - end - end - data[compound] << t - end - end + def tuple?(t) + statements = [] + has_tuple = true + t.each do |name,v| + feature = self.find_or_create_feature(:name => name) + value = self.find_or_create_value(v) + tuple = @model.subject(feature,value) + has_tuple = false if tuple.nil? + statements << [tuple,feature,value] + end + tuples_found = statements.collect{|s| s[0]}.uniq + has_tuple = false unless tuples_found.size == 1 + has_tuple + end + + def find_or_create_tuple(t) + if self.tuple?(t) + t + else + self.create_tuple(t) end - data end =end + -- cgit v1.2.3