diff options
Diffstat (limited to 'lib/model.rb')
-rw-r--r-- | lib/model.rb | 108 |
1 files changed, 17 insertions, 91 deletions
diff --git a/lib/model.rb b/lib/model.rb index cd35f09..24f6e52 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -4,40 +4,28 @@ module OpenTox class Lazar include Owl - attr_accessor :dataset, :predictions - # Create a new prediction model from a dataset - 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]}" + def initialize + super self.source = "http://github.com/helma/opentox-model" - self.parameters = { - "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{@lazar[:activity_dataset]}" }, - "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-algorithm"],"lazar") - self.trainingDataset = @lazar[:activity_dataset] - self.dependentVariables = @lazar[:endpoint] - self.independentVariables = "http://localhost:4002/fminer#BBRC_representative" # TODO read this from dataset - self.predictedVariables = @lazar[:endpoint] #+ " lazar prediction" - @dataset = OpenTox::Dataset.new - @predictions = {} + self.independentVariables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") # TODO read this from dataset end - def self.find(uri) -=begin - begin - YAML.load(RestClient.get uri) - Lazar.new uri - rescue - halt 404, "Model #{uri} not found." - end -=end + def self.from_yaml(yaml) + yaml = YAML.load yaml + lazar = Lazar.new + lazar.title = "lazar model for #{yaml[:endpoint]}" + lazar.parameters = { + "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{yaml[:activity_dataset]}" }, + "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{yaml[:endpoint]}" }, + "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=#{File.join(@@config[:services]["opentox-algorithm"],"fminer")}"} #TODO write to yaml + } + lazar.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") + lazar.trainingDataset = yaml[:activity_dataset] + lazar.dependentVariables = yaml[:endpoint] + lazar.predictedVariables = yaml[:endpoint] #+ " lazar prediction" + lazar end def self.find_all @@ -49,68 +37,6 @@ 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]) - - if (classification != nil) - 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 - end - def self.base_uri @@config[:services]["opentox-model"] end |