diff options
author | Christoph Helma <helma@in-silico.de> | 2009-11-27 18:15:00 +0100 |
---|---|---|
committer | Christoph Helma <helma@in-silico.de> | 2009-11-27 18:15:00 +0100 |
commit | f8d34b1a3c8a87b0a22282d653e4626661766927 (patch) | |
tree | 45385d1a2889d79aca1b9deab6e788ee8a3e6e80 /lazar.rb | |
parent | b99ea3317ac409fa29f8ec7429d7a2138358585f (diff) |
Prediction calculation fixed
Diffstat (limited to 'lazar.rb')
-rw-r--r-- | lazar.rb | 151 |
1 files changed, 52 insertions, 99 deletions
@@ -1,11 +1,3 @@ -require 'redland' -require 'rdf/redland' -require 'rdf/redland/util' - -@@storage = Redland::MemoryStore.new -@@parser = Redland::Parser.new -@@serializer = Redland::Serializer.new - get '/?' do # get index of models Dir["models/*"].collect{|model| url_for("/", :full) + File.basename(model,".yaml")}.sort.join("\n") end @@ -31,81 +23,34 @@ delete '/:id/?' do end end -post '/lazar/?' do # create model - halt 404, "Dataset #{params[:activity_dataset_uri]} not found" unless OpenTox::Dataset.find(params[:activity_dataset_uri]) - halt 404, "Dataset #{params[:feature_dataset_uri]} not found" unless OpenTox::Dataset.find(params[:feature_dataset_uri]) - activities = Redland::Model.new @storage - features = Redland::Model.new @storage - training_activities = OpenTox::Dataset.find params[:activity_dataset_uri] - training_features = OpenTox::Dataset.find params[:feature_dataset_uri] - @@parser.parse_string_into_model(activities,training_activities,'/') - @@parser.parse_string_into_model(features,training_features,'/') - feature = Redland::Node.new(Redland::Uri.new(File.join(@@config[:services]["opentox-algorithm"],'fminer'))) - p_value = Redland::Node.new(Redland::Uri.new(File.join(@@config[:services]["opentox-algorithm"],'fminer/p_value'))) - effect = Redland::Node.new(Redland::Uri.new(File.join(@@config[:services]["opentox-algorithm"],'fminer/effect'))) - - smarts = [] - p_vals = {} - effects = {} - fingerprints = {} - features.triples do |s,p,o| - s = s.uri.to_s.sub(/^\//,'') - case p - when feature - fingerprints[s] = [] unless fingerprints[s] - fingerprints[s] << o.uri.to_s.sub(/^\//,'') - when p_value - sma = s.to_s - smarts << sma - p_vals[sma] = o.to_s.to_f - when effect - sma = s.to_s - effects[sma] = o.to_s - end - end +post '/?' do # create model - activity_uris = [] - act = {} - activities.triples do |s,p,o| - activity_uris << p.uri.to_s - s = s.uri.to_s - case o.to_s - when "true" - act[s] = true - when "false" - act[s] = false + case request.content_type + when /application\/x-yaml|text\/yaml/ + input = request.env["rack.input"].read + id = Dir["models/*"].collect{|model| File.basename(model,".yaml").to_i}.sort.last + if id.nil? + id = 1 + else + id += 1 end - end - - activity_uris.uniq! - if activity_uris.size != 1 - halt 400 - "Dataset #{params[:activity_dataset_uri]} has not exactly one feature." - end - - id = Dir["models/*"].collect{|models| File.basename(models,".yaml").to_i}.sort.last - if id.nil? - id = 1 + File.open(File.join("models",id.to_s + ".yaml"),"w+") { |f| f.write input } + url_for("/#{id}", :full) else - id += 1 + halt 400, "MIME type \"#{request.content_type}\" not supported." end - File.open(File.join("models",id.to_s + ".yaml"),"w") do |f| - f.write({ - :endpoint => activity_uris[0], - :features => smarts, - :p_values => p_vals, - :effects => effects, - :fingerprints => fingerprints, - :activities => act - }.to_yaml) - end url_for("/#{id}", :full) end # PREDICTIONS post '/:id/?' do # create prediction + + storage = Redland::MemoryStore.new + parser = Redland::Parser.new + serializer = Redland::Serializer.new + path = File.join("models",params[:id] + ".yaml") if !File.exists? path status 404 @@ -118,48 +63,56 @@ post '/:id/?' do # create prediction # find database activities if data[:activities][compound.uri] - output = Redland::Model.new @storage + output = Redland::Model.new storage output.add Redland::Uri.new(compound.uri), Redland::Uri.new(data[:endpoint]), Redland::Literal.new(data[:activities][compound.uri].to_s) - halt 200, @@serializer.model_to_string(Redland::Uri.new(url_for("/",:full)), output) - end + response = serializer.model_to_string(Redland::Uri.new(url_for("/",:full)), output) + else + compound_matches = compound.match data[:features] - compound_matches = compound.match data[:features] + conf = 0.0 + neighbors = [] + classification = nil - conf = 0.0 - neighbors = [] - classification = nil + data[:fingerprints].each do |uri,matches| - data[:fingerprints].each do |uri,matches| - sim = weighted_tanimoto(compound_matches,matches,data[:p_values]) - if sim > 0.3 + sim = weighted_tanimoto(compound_matches,matches,data[:p_values]) + if sim > 0.3 - neighbors << uri - case data[:activities][uri].to_s - when 'true' - conf += OpenTox::Utils.gauss(sim) - when 'false' - conf -= OpenTox::Utils.gauss(sim) + neighbors << uri + case data[:activities][uri].to_s + when 'true' + puts "t: #{sim}" + conf += OpenTox::Utils.gauss(sim) + when 'false' + conf -= OpenTox::Utils.gauss(sim) + end end end + conf = conf/neighbors.size if conf > 0.0 classification = true elsif conf < 0.0 classification = false end - end - - output = Redland::Model.new @storage - output.add Redland::Uri.new(compound.uri), Redland::Uri.new(url_for("/#{params[:id]}/classification",:full)), classification.to_s - output.add Redland::Uri.new(compound.uri), Redland::Uri.new(url_for("/#{params[:id]}/confidence",:full)), conf.to_s - @@serializer.model_to_string(Redland::Uri.new(url_for("/",:full)), output) + output = Redland::Model.new storage + output.add Redland::Uri.new(compound.uri), Redland::Uri.new(url_for("/#{params[:id]}/classification",:full)), classification.to_s + output.add Redland::Uri.new(compound.uri), Redland::Uri.new(url_for("/#{params[:id]}/confidence",:full)), conf.to_s + neighbors.each do |neighbor| + output.add Redland::Uri.new(compound.uri), Redland::Uri.new(url_for("/#{params[:id]}/neighbor",:full)), Redland::Uri.new(neighbor) + end + response =serializer.model_to_string(Redland::Uri.new(url_for("/",:full)), output) + end -# { :classification => classification, -# :confidence => conf, -# :neighbors => neighbors, -# :features => compound_matches -# }.to_yaml + m = { :classification => classification, + :confidence => conf, + :neighbors => neighbors, + :features => compound_matches + } + puts m.to_yaml + + response end |