diff options
author | Christoph Helma <helma@in-silico.de> | 2009-12-20 19:16:25 +0100 |
---|---|---|
committer | Christoph Helma <helma@in-silico.de> | 2009-12-20 19:16:25 +0100 |
commit | 89bc8a67e63cf8e752f3b89aa52f7db87ce7ea7f (patch) | |
tree | 7bcb8dbabc70d5d026c7811fae700a803653cdc3 /lib/model.rb | |
parent | fbee9fbf8ce286a1264ef4ce8f3dfb77d048d067 (diff) |
model adapted to OWL-DL, dataset predictions added
Diffstat (limited to 'lib/model.rb')
-rw-r--r-- | lib/model.rb | 147 |
1 files changed, 99 insertions, 48 deletions
diff --git a/lib/model.rb b/lib/model.rb index 50d6bea..2f4525a 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -1,16 +1,18 @@ module OpenTox module Model + class Lazar include Owl + + attr_accessor :dataset, :predictions # Create a new prediction model from a dataset - def initialize - super - end - - def read_yaml(id,yaml) - @lazar = YAML.load yaml - self.identifier = File.join(@@config[:services]["opentox-model"],'lazar',id) + def initialize(yaml) + super() + id = File.basename(yaml,'.yaml') + # TODO Untyped Individual: http://localhost:4003/lazar/{id} ???? + @lazar = YAML.load_file yaml + self.uri = File.join(@@config[:services]["opentox-model"],'lazar',id) self.title = "lazar model for #{@lazar[:endpoint]}" self.source = "http://github.com/helma/opentox-model" self.parameters = { @@ -18,19 +20,24 @@ module OpenTox "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{@lazar[:endpoint]}" }, "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=" } #TODO write to yaml } - self.algorithm = File.join(@@config[:services]["opentox-model"],"lazar") + self.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") self.trainingDataset = @lazar[:activity_dataset] self.dependentVariables = @lazar[:endpoint] - self.predictedVariables = @lazar[:endpoint] + " lazar prediction" + self.independentVariables = "http://localhost:4002/fminer#BBRC_representative" # TODO read this from dataset + self.predictedVariables = @lazar[:endpoint] #+ " lazar prediction" + @dataset = OpenTox::Dataset.new + @predictions = {} end def self.find(uri) +=begin begin YAML.load(RestClient.get uri) Lazar.new uri rescue halt 404, "Model #{uri} not found." end +=end end def self.find_all @@ -42,6 +49,65 @@ module OpenTox RestClient.post(@uri, :compound_uri => compound.uri) end + def database_activity?(compound_uri) + # find database activities + db_activities = @lazar[:activities][compound_uri] + if db_activities + c = @dataset.find_or_create_compound(compound_uri) + f = @dataset.find_or_create_feature(@lazar[:endpoint]) + v = db_activities.join(',') + @dataset.add c,f,v + @predictions[compound_uri] = { @lazar[:endpoint] => {:measured_activities => db_activities}} + true + else + false + end + end + + def classify(compound_uri) + + compound = OpenTox::Compound.new(:uri => compound_uri) + compound_matches = compound.match @lazar[:features] + + conf = 0.0 + neighbors = [] + classification = nil + + @lazar[:fingerprints].each do |uri,matches| + + sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,@lazar[:p_values]) + if sim > 0.3 + neighbors << uri + @lazar[:activities][uri].each do |act| + case act.to_s + when 'true' + conf += OpenTox::Utils.gauss(sim) + when 'false' + conf -= OpenTox::Utils.gauss(sim) + end + end + end + end + + conf = conf/neighbors.size + if conf > 0.0 + classification = true + elsif conf < 0.0 + classification = false + end + + compound = @dataset.find_or_create_compound(compound_uri) + feature = @dataset.find_or_create_feature(@lazar[:endpoint]) + tuple = @dataset.create_tuple(feature,{ 'lazar#classification' => classification, 'lazar#confidence' => conf}) + @dataset.add_tuple compound,tuple + @predictions[compound_uri] = { @lazar[:endpoint] => { :lazar_prediction => { + :classification => classification, + :confidence => conf, + :neighbors => neighbors, + :features => compound_matches + } } } + end + def self.base_uri @@config[:services]["opentox-model"] end @@ -54,50 +120,35 @@ module OpenTox YAML.load(RestClient.get uri)[:endpoint] end - end - end - - -=begin - module Model - - class LazarClassification < OpenTox - - - end - - end - - module Prediction - - module Classification - - class Lazar < OpenTox - - def initialize(params) - super(params[:uri]) - end - - def classification - YAML.load(RestClient.get(@uri))[:classification] - end - - def confidence - YAML.load(RestClient.get(@uri))[:confidence] - end + def algorithm=(algorithm) + me = @model.subject(RDF['type'],OT[self.owl_class]) + @model.add me, OT['algorithm'], Redland::Uri.new(algorithm) # untyped individual comes from this line, why?? + @model.add Redland::Uri.new(algorithm), RDF['type'], OT['Algorithm'] + end - def neighbors - RestClient.get @uri + '/neighbors' - end + def trainingDataset=(trainingDataset) + me = @model.subject(RDF['type'],OT[self.owl_class]) + @model.add me, OT['trainingDataset'], Redland::Uri.new(trainingDataset) # untyped individual comes from this line, why?? + @model.add Redland::Uri.new(trainingDataset), RDF['type'], OT['Dataset'] + end - def features - RestClient.get @uri + '/features' - end + def dependentVariables=(dependentVariables) + me = @model.subject(RDF['type'],OT[self.owl_class]) + @model.add me, OT['dependentVariables'], Redland::Uri.new(dependentVariables) # untyped individual comes from this line, why?? + @model.add Redland::Uri.new(dependentVariables), RDF['type'], OT['Feature'] + end + def independentVariables=(independentVariables) + me = @model.subject(RDF['type'],OT[self.owl_class]) + @model.add me, OT['independentVariables'], Redland::Uri.new(independentVariables) # untyped individual comes from this line, why?? + @model.add Redland::Uri.new(independentVariables), RDF['type'], OT['Feature'] end + def predictedVariables=(predictedVariables) + me = @model.subject(RDF['type'],OT[self.owl_class]) + @model.add me, OT['predictedVariables'], Redland::Uri.new(predictedVariables) # untyped individual comes from this line, why?? + @model.add Redland::Uri.new(predictedVariables), RDF['type'], OT['Feature'] + end end - end -=end end |