opentox-ruby-api-wrapper 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/model.rb CHANGED
@@ -1,10 +1,93 @@
1
1
  module OpenTox
2
2
  module Model
3
-
4
- class Lazar
5
3
 
6
- attr_accessor :dependent_variable, :activity_dataset_uri, :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features, :algorithm
7
-
4
+ class Generic
5
+
6
+ MODEL_ATTRIBS = [:uri, :title, :creator, :date, :format, :predictedVariables, :independentVariables, :dependentVariables, :trainingDataset, :algorithm]
7
+ MODEL_ATTRIBS.each{ |a| attr_accessor(a) }
8
+
9
+ def self.find(uri)
10
+ owl = OpenTox::Owl.from_uri(uri, "Model")
11
+ return self.new(owl)
12
+ end
13
+
14
+ def self.to_rdf(model)
15
+ owl = OpenTox::Owl.create 'Model', model.uri
16
+ (MODEL_ATTRIBS - [:uri]).each do |a|
17
+ owl.set(a.to_s,model.send(a.to_s))
18
+ end
19
+ owl.rdf
20
+ end
21
+
22
+ protected
23
+ def initialize(owl)
24
+ [:date, :creator, :title, :format, :algorithm, :dependentVariables,
25
+ :independentVariables, :predictedVariables, :trainingDataset].each do |a|
26
+ self.send("#{a.to_s}=".to_sym, owl.get(a.to_s))
27
+ end
28
+ @uri = owl.uri
29
+ if ENV['RACK_ENV'] =~ /test|debug/
30
+ begin
31
+ raise "uri invalid" unless Utils.is_uri?(@uri)
32
+ raise "no algorithm" unless @algorithm and @algorithm.size>0
33
+ raise "no dependent variables" unless @dependentVariables and @dependentVariables.size>0
34
+ raise "no indenpendent variables" unless @independentVariables
35
+ raise "no predicted variables" unless @predictedVariables and @predictedVariables.size>0
36
+ rescue => ex
37
+ RestClientWrapper.raise_uri_error "invalid model: '"+ex.message+"'\n"+self.to_yaml+"\n",@uri.to_s
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ class PredictionModel < Generic
44
+
45
+ def self.build( algorithm_uri, algorithm_params )
46
+
47
+ if algorithm_uri =~ /ambit2/
48
+ LOGGER.warn "Ambit hack, replacing 'prediction_feature' with 'target'"
49
+ algorithm_params[:target] = algorithm_params[:prediction_feature]
50
+ algorithm_params.delete(:prediction_feature)
51
+ end
52
+
53
+ LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s
54
+ uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s
55
+ LOGGER.debug "Build model done: "+uri.to_s
56
+ RestClientWrapper.raise_uri_error("Invalid build model result: '"+uri.to_s+"'", algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri)
57
+ return PredictionModel.find(uri)
58
+ end
59
+
60
+ def predict_dataset( dataset_uri )
61
+
62
+ LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s
63
+ uri = RestClientWrapper.post(@uri, {:dataset_uri=>dataset_uri})
64
+ RestClientWrapper.raise_uri_error("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri)
65
+ uri
66
+ end
67
+
68
+ def classification?
69
+ #HACK replace with request to ontology server
70
+ if @title =~ /lazar classification/
71
+ return true
72
+ elsif @uri =~/ntua/ and @title =~ /mlr/
73
+ return false
74
+ elsif @uri =~/tu-muenchen/ and @title =~ /regression|M5P|GaussP/
75
+ return false
76
+ elsif @uri =~/ambit2/ and @title =~ /pKa/
77
+ return false
78
+ elsif @uri =~/majority/
79
+ return (@uri =~ /class/) != nil
80
+ else
81
+ raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'"
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ class Lazar < Generic
88
+
89
+ attr_accessor :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features
90
+
8
91
  def initialize
9
92
  @source = "http://github.com/helma/opentox-model"
10
93
  @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
@@ -22,107 +105,9 @@ module OpenTox
22
105
  resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
23
106
  end
24
107
 
25
-
26
108
  def self.find_all
27
- RestClient.get(@@config[:services]["opentox-model"]).chomp.split("\n")
28
- end
29
- =begin
30
- include Owl
31
-
32
- # Create a new prediction model from a dataset
33
- def initialize
34
- super
35
- self.source = "http://github.com/helma/opentox-model"
36
- self.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
37
- self.independentVariables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") # TODO read this from dataset
38
- end
39
-
40
- def self.from_yaml(yaml)
41
- yaml = YAML.load yaml
42
- lazar = Lazar.new
43
- lazar.title = "lazar model for #{yaml[:endpoint]}"
44
- lazar.parameters = {
45
- "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{yaml[:activity_dataset]}" },
46
- "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{yaml[:endpoint]}" },
47
- "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=#{File.join(@@config[:services]["opentox-algorithm"],"fminer")}"} #TODO write to yaml
48
- }
49
- lazar.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
50
- lazar.trainingDataset = yaml[:activity_dataset]
51
- lazar.dependentVariables = yaml[:endpoint]
52
- lazar.predictedVariables = yaml[:endpoint] + "_lazar_prediction"
53
- lazar
54
- end
55
-
56
- def self.find(uri)
57
- yaml = RestClient.get(uri, :accept => "application/x-yaml")
58
- OpenTox::Model::Lazar.from_yaml(yaml)
59
- end
60
-
61
- # Predict a compound
62
- def predict(compound)
63
- # nicht absichern??
64
- resource = RestClient::Resource.new(@uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
65
- resource.post(:compound_uri => compound.uri)
66
- end
67
-
68
- def self.base_uri
69
- File.join @@config[:services]["opentox-model"],'lazar'
70
- end
71
-
72
- def self.create(data)
73
- resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
74
- resource.post(data, :content_type => "application/x-yaml").chomp.to_s
75
- end
76
-
77
- def delete
78
- resource = RestClient::Resource.new(self.uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
79
- resource.delete
80
- #RestClient.delete @uri if @uri
81
- #RestClient.delete model.task_uri if model.task_uri
82
- end
83
-
84
- # def self.create(task)
85
- # @uri = RestClient.post(@@config[:services]["opentox-model"], :task_uri => task.uri)
86
- # end
87
-
88
- # def yaml=(data)
89
- # RestClient.put(@@uri, data, :content_type => "application/x-yaml").to_s
90
- # end
91
-
92
- def endpoint
93
- YAML.load(RestClient.get(uri))[:endpoint]
94
- end
95
-
96
- def algorithm=(algorithm)
97
- me = @model.subject(RDF['type'],OT[self.owl_class])
98
- @model.add me, OT['algorithm'], Redland::Uri.new(algorithm) # untyped individual comes from this line, why??
99
- @model.add Redland::Uri.new(algorithm), RDF['type'], OT['Algorithm']
100
- end
101
-
102
- def trainingDataset=(trainingDataset)
103
- me = @model.subject(RDF['type'],OT[self.owl_class])
104
- @model.add me, OT['trainingDataset'], Redland::Uri.new(trainingDataset) # untyped individual comes from this line, why??
105
- @model.add Redland::Uri.new(trainingDataset), RDF['type'], OT['Dataset']
106
- end
107
-
108
- def dependentVariables=(dependentVariables)
109
- me = @model.subject(RDF['type'],OT[self.owl_class])
110
- @model.add me, OT['dependentVariables'], Redland::Uri.new(dependentVariables) # untyped individual comes from this line, why??
111
- @model.add Redland::Uri.new(dependentVariables), RDF['type'], OT['Feature']
112
- end
113
-
114
- def independentVariables=(independentVariables)
115
- me = @model.subject(RDF['type'],OT[self.owl_class])
116
- @model.add me, OT['independentVariables'], Redland::Uri.new(independentVariables) # untyped individual comes from this line, why??
117
- @model.add Redland::Uri.new(independentVariables), RDF['type'], OT['Feature']
118
- end
119
-
120
- def predictedVariables=(predictedVariables)
121
- me = @model.subject(RDF['type'],OT[self.owl_class])
122
- @model.add me, OT['predictedVariables'], Redland::Uri.new(predictedVariables) # untyped individual comes from this line, why??
123
- @model.add Redland::Uri.new(predictedVariables), RDF['type'], OT['Feature']
109
+ RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n")
124
110
  end
125
- =end
126
111
  end
127
112
  end
128
113
  end
@@ -8,6 +8,6 @@ rescue LoadError
8
8
  puts "Please install Openbabel with 'rake openbabel:install' in the compound component"
9
9
  end
10
10
 
11
- ['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization'].each do |lib|
11
+ ['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization','features', 'rest_client_wrapper'].each do |lib|
12
12
  require lib
13
13
  end