summaryrefslogtreecommitdiff
path: root/webapp/lazar.rb
blob: 5e1c4df8303eaedea285f2ad3c82cf47f11b6356 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# lazar.rb
# Lazar service
# Author: Andreas Maunz


$lazar_params = [ 
  "training_dataset_uri", 
  "prediction_feature_uri", 
  "feature_dataset_uri",
  "feature_generation_uri", 
  "feature_calculation_algorithm", 
  "similarity_algorithm",
  "min_sim", 
  "prediction_algorithm", 
  "propositionalized", 
  "pc_type", 
  "lib", 
  "min_train_performance" 
]
$lazar_min_train_performance_default = 0.1


module OpenTox
  class Application < Service

    
    # Get representation of lazar algorithm
    # @return [String] Representation
    get '/lazar/?' do
      algorithm = OpenTox::Algorithm.new(url_for('/lazar',:full))
      algorithm.metadata = {
        DC.title => 'lazar',
        DC.creator => 'helma@in-silico.ch, andreas@maunz.de',
        RDF.Type => [OT.Algorithm]
      }
      algorithm.parameters = [
        { DC.description => "Dataset URI", OT.paramScope => "mandatory", DC.title => "dataset_uri" },
        { DC.description => "Feature URI for dependent variable", OT.paramScope => "optional", DC.title => "prediction_feature" },
        { DC.description => "Feature generation service URI", OT.paramScope => "optional", DC.title => "feature_generation_uri" },
        { DC.description => "Feature dataset URI", OT.paramScope => "optional", DC.title => "feature_dataset_uri" },
        { DC.description => "Further parameters for the feature generation service", OT.paramScope => "optional" }
      ]
      format_output(algorithm)
    end


    # Create a lazar prediction model
    # @param [String] dataset_uri Training dataset URI
    # @param [optional,String] prediction_feature URI of the feature to be predicted
    # @param [optional,String] feature_generation_uri URI of the feature generation algorithm 
    # @param [optional,String] - further parameters for the feature generation service 
    # @return [text/uri-list] Task URI 
    post '/lazar/?' do 
      params[:subjectid] = @subjectid
      resource_not_found_error "No dataset_uri parameter." unless params[:dataset_uri]
      task = OpenTox::Task.create(
                                  $task[:uri],
                                  @subjectid,
                                  { RDF::DC.description => "Create lazar model",
                                    RDF::DC.creator => url_for('/lazar',:full)
                                  }
                                ) do |task|
        begin 
          lazar = OpenTox::Model.new(nil, @subjectid)
          lazar.parameters = lazar.check_params($lazar_params, params)
          lazar.metadata = { 
            DC.title => "lazar model", 
            OT.dependentVariables => lazar.find_parameter_value("prediction_feature_uri"),
            OT.trainingDataset => lazar.find_parameter_value("training_dataset_uri"),
            OT.featureDataset => lazar.find_parameter_value("feature_dataset_uri"),
            RDF.type => ( OpenTox::Feature.find(lazar.find_parameter_value("prediction_feature_uri")).feature_type == "classification" ? 
              [OT.Model, OTA.ClassificationLazySingleTarget] :
              [OT.Model, OTA.RegressionLazySingleTarget] 
            )
          }
          # task.progress 10
          lazar.put @subjectid
          lazar.uri
        rescue => e
          $logger.debug "#{e.class}: #{e.message}"
          $logger.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
        end
      end
      response['Content-Type'] = 'text/uri-list'
      service_unavailable_error "Service unavailable" if task.cancelled?
      halt 202,task.uri.to_s+"\n"
    end


    # Make a lazar prediction -- not to be called directly
    # @param [String] compound_uri URI of compound to be predicted
    # @param [String] training_dataset_uri URI of training dataset
    # @param [String] prediction_feature_uri URI of prediction feature
    # @param [String] feature_dataset_uri URI of feature dataset
    # @param [String] feature_calculation_algorithm Name of feature calculation algorithm
    # @param [String] min_sim Numeric value for minimum similarity
    # @param [String] prediction_algorithm Name of prediction algorithm
    # @param [String] propositionalized Whether propositionalization should be used 
    # @param [optional,String] pc_type Physico-chemical descriptor type
    # @param [optional,String] pc_lib Physico-chemical descriptor library
    # @param [optional,String] Further parameters for the feature generation service 
    # @return [text/uri-list] Task URI 
    post '/lazar/predict/?' do 

      params[:subjectid] = @subjectid
      if ( (params[:compound_uri] and params[:dataset_uri]) or 
           (!params[:compound_uri] and !params[:dataset_uri])
         )
        bad_request_error "Submit either compound uri or dataset uri"
      end

      task = OpenTox::Task.create(
        $task[:uri],
        @subjectid,
        { 
          RDF::DC.description => "Create lazar model",
          RDF::DC.creator => url_for('/lazar/predict',:full)
        }
      ) do |task|

        begin 

          if params[:dataset_uri]
            compounds = OpenTox::Dataset.find(params[:dataset_uri]).compounds
          else
            compounds = [ OpenTox::Compound.new(params[:compound_uri]) ]
          end

          compounds.each { |query_compound|
            params[:compound_uri] = query_compound.uri # AM: store compound in params hash
            unless @prediction_dataset # AM: only once for dataset predictions
              @prediction_dataset = OpenTox::Dataset.new(nil, @subjectid) 

              @model_params_hash = $lazar_params.inject({}){ |h,p|
                h[p] = params[p].to_s unless params[p].nil?
                h
              }
              @model = OpenTox::Model.new(@model_params_hash)

              @prediction_dataset.metadata = {
                DC.title => "Lazar prediction",
                DC.creator => @uri.to_s,
                OT.hasSource => @uri.to_s,
                OT.dependentVariables => @model_params_hash["prediction_feature_uri"],
                OT.predictedVariables => @model_params_hash["prediction_feature_uri"]
              }

              $logger.debug "Loading t dataset"
              @training_dataset = OpenTox::Dataset.find(params[:training_dataset_uri], @subjectid)
              @prediction_feature = OpenTox::Feature.find(params[:prediction_feature_uri],@subjectid)
              @confidence_feature = OpenTox::Feature.find_by_title("confidence", {RDF.type => [RDF::OT.NumericFeature]})
              @similarity_feature = OpenTox::Feature.find_by_title("similarity", {RDF.type => [RDF::OT.NumericFeature]})
              @prediction_dataset.features = [ @prediction_feature, @confidence_feature, @similarity_feature ]
            end
            
            database_activity = @training_dataset.database_activity(params)
            if database_activity

              prediction_value = database_activity.to_f
              confidence_value = 1.0

            else
              @model = OpenTox::Model.new(@model_params_hash)

              unless @feature_dataset
                $logger.debug "Loading f dataset"
                @feature_dataset = OpenTox::Dataset.find(params[:feature_dataset_uri], @subjectid) # This takes time
              end

              case @feature_dataset.find_parameter_value("nr_hits")
                when "true" then @model.feature_calculation_algorithm = "match_hits"
                when "false" then @model.feature_calculation_algorithm = "match"
              end
              pc_type = @feature_dataset.find_parameter_value("pc_type")
              @model.pc_type = pc_type unless pc_type.nil?
              lib = @feature_dataset.find_parameter_value("lib")
              @model.lib = lib unless lib.nil?

              # AM: transform to cosine space
              @model.min_sim = (@model.min_sim.to_f*2.0-1.0).to_s if @model.similarity_algorithm =~ /cosine/

              if @feature_dataset.features.size > 0
                compound_params = { 
                  :compound => query_compound, 
                  :feature_dataset => @feature_dataset,
                  :pc_type => @model.pc_type,
                  :lib => @model.lib
                }
                # use send, not eval, for calling the method (good backtrace)
                $logger.debug "Calculating q fps"
                compound_fingerprints = OpenTox::Algorithm::FeatureValues.send( @model.feature_calculation_algorithm, compound_params, @subjectid )
              else
                bad_request_error "No features found"
              end

              @model.add_data(@training_dataset, @feature_dataset, @prediction_feature, compound_fingerprints, @subjectid)
              mtf = OpenTox::Algorithm::Transform::ModelTransformer.new(@model)
              mtf.transform
              $logger.debug "Predicting q"
              prediction = OpenTox::Algorithm::Neighbors.send(@model.prediction_algorithm,  { :props => mtf.props,
                                                            :acts => mtf.acts,
                                                            :sims => mtf.sims,
                                                            :value_map => @training_dataset.value_map(@prediction_feature),
                                                            :min_train_performance => @model.min_train_performance
                                                          } )
              prediction_value = prediction[:prediction].to_f
              confidence_value = prediction[:confidence].to_f

              # AM: transform to original space
              confidence_value = ((confidence_value+1.0)/2.0).abs if @model.similarity_algorithm =~ /cosine/
              prediction_value = @training_dataset.value_map(@prediction_feature)[prediction[:prediction].to_i] if @prediction_feature.feature_type == "classification"

              $logger.debug "Prediction: '#{prediction_value}'"
              $logger.debug "Confidence: '#{confidence_value}'"
            end

            @prediction_dataset << [ 
              query_compound, 
              prediction_value, 
              confidence_value, 
              nil
            ]
            @model.neighbors.each { |neighbor|
              @prediction_dataset << [ 
                OpenTox::Compound.new(neighbor[:compound]), 
                @training_dataset.value_map(@prediction_feature)[neighbor[:activity]], 
                nil, 
                neighbor[:similarity] 
              ]
            }

          }

         @prediction_dataset.parameters = $lazar_params.collect { |p|
           {DC.title => p, OT.paramValue => @model.instance_variable_get("@#{p}")} unless  @model.instance_variable_get("@#{p}").nil?
         }

          @prediction_dataset.put
          $logger.debug @prediction_dataset.uri
          @prediction_dataset.uri

        rescue => e
          $logger.debug "#{e.class}: #{e.message}"
          $logger.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
        end

      end
      response['Content-Type'] = 'text/uri-list'
      service_unavailable_error "Service unavailable" if task.cancelled?
      halt 202,task.uri.to_s+"\n"
    end


  end
end