summaryrefslogtreecommitdiff
path: root/lib/model.rb
blob: 1f5f27d91e954692dc40b8c9b1d2dd9f515ee9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module OpenTox
	module Model

		class Generic

			attr_accessor :uri, :title, :source, :identifier, :predicted_variables, :independent_variables, :dependent_variables, :activity_dataset_uri, :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features, :algorithm

			def self.find(uri)
				owl = OpenTox::Owl.from_uri(uri)
        return self.new(owl)
      end
      
      protected
      def initialize(owl)
        @title = owl.title
				@source = owl.source
				@identifier = owl.identifier.sub(/^\[/,'').sub(/\]$/,'')
				@uri = owl.uri.to_s #@identifier
				@algorithm = owl.algorithm
				@dependent_variables = owl.dependentVariables
				@independent_variables = owl.independentVariables
				@predicted_variables = owl.predictedVariables
        
        raise "invalid model:\n"+self.to_yaml+"\n" unless Utils.is_uri?(@uri) && @dependent_variables.to_s.size>0 &&  
          @independent_variables.to_s.size>0 && @predicted_variables.to_s.size>0 if ENV['RACK_ENV'] =~ /test|debug/
			end
	 end
  
  
   class PredictionModel < Generic
     
     def self.build( algorithm_uri, algorithm_params )
        
       LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s
       uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s
       
       if uri.to_s =~ /ambit.*task|tu-muenchen.*task/
         uri = PredictionModel.redirect_task(uri)
       elsif Utils.task_uri?(uri)
        uri = OpenTox::Task.find(uri).wait_for_resource.to_s
       end
       raise "invalid build model result: "+uri.to_s unless uri =~ /model/
       return PredictionModel.find(uri)
     end
     
     def self.redirect_task( uri )
       raise "no redirect task uri: "+uri.to_s unless uri.to_s =~ /ambit.*task|tu-muenchen.*task/
       
       while (uri.to_s =~ /ambit.*task|tu-muenchen.*task/) 
         #HACK handle redirect
         LOGGER.debug "REDIRECT TASK: "+uri.to_s
         redirect = ""
         while (redirect.size == 0)
           IO.popen("bin/redirect.sh "+uri.to_s) do |f| 
             while line = f.gets
               redirect += line.chomp
             end
           end
           sleep 0.3
         end
         uri = redirect
         LOGGER.debug "REDIRECT TO: "+uri.to_s
       end
       return uri
     end
    
     def predict_dataset( dataset_uri )

       LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s
       
       #HACK using curl
       uri = ""
       IO.popen("curl -X POST -d dataset_uri='"+dataset_uri+"' "+@uri.to_s+" 2> /dev/null") do |f| 
         while line = f.gets
           uri += line
         end
       end

       if uri.to_s =~ /ambit.*task|tu-muenchen.*task/
         uri = PredictionModel.redirect_task(uri)
         raise "invalid redirect result: " unless uri =~ /ambit.*dataset/
         return uri
       else
         uri = OpenTox::Task.find(uri).wait_for_resource.to_s if Utils.task_uri?(uri)
         return uri if Utils.dataset_uri?(uri)
         raise "not sure about prediction result: "+uri.to_s
       end
     end
    
     def classification?
       #HACK replace with request to ontology server
       if @title =~ /lazar classification/
         return true
       elsif @uri =~/ntua/ and @title =~ /mlr/
         return false
       elsif @uri =~/tu-muenchen/ and @title =~ /regression/
         return false
       else
         raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'"
       end
     end
   end
  
   
		class Lazar < Generic

			def initialize
				@source = "http://github.com/helma/opentox-model"
				@algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
				#@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative")
				@features = []
				@effects = {}
				@activities = {}
				@p_values = {}
				@fingerprints = {}
			end

			def save
				@features.uniq!
			  resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
			  resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
			end

			def self.find_all
				RestClient.get(@@config[:services]["opentox-model"]).chomp.split("\n")
			end
=begin
			
			# Predict a compound
			def predict(compound)
				# nicht absichern??
				resource = RestClient::Resource.new(@uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
				resource.post(:compound_uri => compound.uri)
			end

			def self.base_uri
				File.join @@config[:services]["opentox-model"],'lazar'
			end

			def self.create(data)
			  resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
			  resource.post(data, :content_type => "application/x-yaml").chomp.to_s
			end

			def delete
			  resource = RestClient::Resource.new(self.uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
				resource.delete
				#RestClient.delete @uri if @uri
				#RestClient.delete model.task_uri if model.task_uri
			end

#			def self.create(task)
#				@uri = RestClient.post(@@config[:services]["opentox-model"], :task_uri => task.uri)
#			end

#			def yaml=(data)
#				RestClient.put(@@uri, data, :content_type => "application/x-yaml").to_s
#			end

			def endpoint
				YAML.load(RestClient.get(uri))[:endpoint]
			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 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 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