summaryrefslogtreecommitdiff
path: root/lib/model.rb
blob: a272580d60c04050aaf8ed9b6d1bf68088483304 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
module OpenTox

  module Model

    class Lazar 

      include OpenTox
      include Mongoid::Document
      include Mongoid::Timestamps
      store_in collection: "models"

      field :name, type: String
      field :creator, type: String, default: __FILE__
      field :training_dataset_id, type: BSON::ObjectId
      field :prediction_feature_id, type: BSON::ObjectId
      field :algorithms, type: Hash
      field :relevant_features, type: Hash
      
      def self.create prediction_feature:nil, training_dataset:nil, algorithms:{}
        bad_request_error "Please provide a prediction_feature and/or a training_dataset." unless prediction_feature or training_dataset
        prediction_feature = training_dataset.features.first unless prediction_feature
        # TODO: prediction_feature without training_dataset: use all available data
        # explicit prediction algorithm
        if algorithms[:prediction] and algorithms[:prediction][:method]
          case algorithms[:prediction][:method]
          when /Classifiction/
            model = LazarClassification.new
          when /Regression/
            model = LazarRegression.new 
          end

        # guess model type
        elsif prediction_feature.numeric? 
          model = LazarRegression.new 
        else
          model = LazarClassification.new
        end

        # set defaults
        substance_classes = training_dataset.substances.collect{|s| s.class.to_s}.uniq
        bad_request_error "Cannot create models for mixed substance classes '#{substance_classes.join ', '}'." unless substance_classes.size == 1

        if substance_classes.first == "OpenTox::Compound"

          model.algorithms = {
            :descriptors => {
              :method => "fingerprint",
              :type => 'MP2D',
            },
            :similarity => {
              :method => "Algorithm::Similarity.tanimoto",
              :min => 0.1
            },
            :feature_selection => nil
          }

          if model.class == LazarClassification
            model.algorithms[:prediction] = {
                :method => "Algorithm::Classification.weighted_majority_vote",
            }
          elsif model.class == LazarRegression
            model.algorithms[:prediction] = {
              :method => "Algorithm::Regression.caret",
              :parameters => "pls",
            }
          end

        elsif substance_classes.first == "OpenTox::Nanoparticle"
          model.algorithms = {
            :descriptors => {
              :method => "properties",
              #:types => ["P-CHEM","Proteomics"],
              :types => ["P-CHEM"],
            },
            :similarity => {
              :method => "Algorithm::Similarity.weighted_cosine",
              :min => 0.5
            },
            :prediction => {
              :method => "Algorithm::Regression.caret",
              :parameters => "rf",
            },
            :feature_selection => {
              :method => "Algorithm::FeatureSelection.correlation_filter",
            },
          }
        else
          bad_request_error "Cannot create models for #{substance_classes.first}."
        end
        
        # overwrite defaults with explicit parameters
        algorithms.each do |type,parameters|
          if parameters and parameters.is_a? Hash
            parameters.each do |p,v|
              model.algorithms[type] ||= {}
              model.algorithms[type][p] = v
            end
          else
            model.algorithms[type] = parameters
          end
        end

        model.prediction_feature_id = prediction_feature.id
        model.training_dataset_id = training_dataset.id
        model.name = "#{training_dataset.name} #{prediction_feature.name}" 

        if model.algorithms[:feature_selection] and model.algorithms[:feature_selection][:method]
          model.relevant_features = Algorithm.run model.algorithms[:feature_selection][:method], dataset: training_dataset, prediction_feature: prediction_feature, types: model.algorithms[:descriptors][:types]
        end
        model.save
        model
      end

      def predict_substance substance
        neighbors = substance.neighbors dataset_id: training_dataset_id, prediction_feature_id: prediction_feature_id, descriptors: algorithms[:descriptors], similarity: algorithms[:similarity], relevant_features: relevant_features
        measurements = nil
        prediction = {}
        # handle query substance
        if neighbors.collect{|n| n["_id"]}.include? substance.id

          query = neighbors.select{|n| n["_id"] == substance.id}.first
          measurements = training_dataset.values(query["_id"],prediction_feature_id)
          prediction[:measurements] = measurements
          prediction[:warning] = "#{measurements.size} substances have been removed from neighbors, because they are identical with the query substance."
          neighbors.delete_if{|n| n["_id"] == substance.id} # remove query substance for an unbiased prediction (also useful for loo validation)
        end
        if neighbors.empty?
          prediction.merge!({:value => nil,:probabilities => nil,:warning => "Could not find similar substances with experimental data in the training dataset.",:neighbors => []})
        elsif neighbors.size == 1
          value = nil
          m = neighbors.first["measurements"]
          if m.size == 1 # single measurement
            value = m.first
          else # multiple measurement
            if m.collect{|t| t.numeric?}.uniq == [true] # numeric
              value = m.median
            elsif m.uniq.size == 1 # single value
              value = m.first
            else # contradictory results
              # TODO add majority vote??
            end
          end
          prediction.merge!({:value => value, :probabilities => nil, :warning => "Only one similar compound in the training set. Predicting median of its experimental values.", :neighbors => neighbors}) if value
        else
          # call prediction algorithm
          case algorithms[:descriptors][:method]
          when "fingerprint"
            descriptors = substance.fingerprints[algorithms[:descriptors][:type]]
          when "properties"
            descriptors = substance.properties
          else
            bad_request_error "Descriptor method '#{algorithms[:descriptors][:method]}' not available."
          end
          params = algorithms[:prediction].merge({:descriptors => descriptors, :neighbors => neighbors})
          params.delete :method
          result = Algorithm.run algorithms[:prediction][:method], params
          prediction.merge! result
          prediction[:neighbors] = neighbors
          prediction[:neighbors] ||= []
        end
        prediction
      end

      def predict object

        training_dataset = Dataset.find training_dataset_id

        # parse data
        substances = []
        if object.is_a? Substance
          substances = [object] 
        elsif object.is_a? Array
          substances = object
        elsif object.is_a? Dataset
          substances = object.substances
        else 
          bad_request_error "Please provide a OpenTox::Compound an Array of OpenTox::Substances or an OpenTox::Dataset as parameter."
        end

        # make predictions
        predictions = {}
        substances.each do |c|
          predictions[c.id.to_s] = predict_substance c
          predictions[c.id.to_s][:prediction_feature_id] = prediction_feature_id 
        end

        # serialize result
        if object.is_a? Substance
          prediction = predictions[substances.first.id.to_s]
          prediction[:neighbors].sort!{|a,b| b[1] <=> a[1]} # sort according to similarity
          return prediction
        elsif object.is_a? Array
          return predictions
        elsif object.is_a? Dataset
          # prepare prediction dataset
          measurement_feature = Feature.find prediction_feature_id

          prediction_feature = NumericFeature.find_or_create_by( "name" => measurement_feature.name + " (Prediction)" )
          prediction_dataset = LazarPrediction.create(
            :name => "Lazar prediction for #{prediction_feature.name}",
            :creator =>  __FILE__,
            :prediction_feature_id => prediction_feature.id,
            :predictions => predictions
          )
          return prediction_dataset
        end

      end

      def training_dataset
        Dataset.find(training_dataset_id)
      end

      def prediction_feature
        Feature.find(prediction_feature_id)
      end

    end

    class LazarClassification < Lazar

    end

    class LazarRegression < Lazar

    end

    class Prediction

      include OpenTox
      include Mongoid::Document
      include Mongoid::Timestamps

      field :endpoint, type: String
      field :species, type: String
      field :source, type: String
      field :unit, type: String
      field :model_id, type: BSON::ObjectId
      field :repeated_crossvalidation_id, type: BSON::ObjectId
      field :leave_one_out_validation_id, type: BSON::ObjectId

      def predict object
        Lazar.find(model_id).predict object
      end

      def training_dataset
        model.training_dataset
      end

      def model
        Lazar.find model_id
      end

      def repeated_crossvalidation
        Validation::RepeatedCrossValidation.find repeated_crossvalidation_id
      end

      def crossvalidations
        repeated_crossvalidation.crossvalidations
      end

      def leave_one_out_validation
        Validation::LeaveOneOut.find leave_one_out_validation_id
      end

      def regression?
        model.is_a? LazarRegression
      end

      def classification?
        model.is_a? LazarClassification
      end

      def self.from_csv_file file
        metadata_file = file.sub(/csv$/,"json")
        bad_request_error "No metadata file #{metadata_file}" unless File.exist? metadata_file
        prediction_model = self.new JSON.parse(File.read(metadata_file))
        training_dataset = Dataset.from_csv_file file
        prediction_feature = training_dataset.features.first
        model = nil
        if prediction_feature.nominal?
          model = LazarClassification.create prediction_feature, training_dataset
        elsif prediction_feature.numeric?
          model = LazarRegression.create prediction_feature, training_dataset
        end
        prediction_model[:model_id] = model.id
        prediction_model[:prediction_feature_id] = prediction_feature.id
        prediction_model[:repeated_crossvalidation_id] = Validation::RepeatedCrossValidation.create(model).id
        #prediction_model[:leave_one_out_validation_id] = Validation::LeaveOneOut.create(model).id
        prediction_model.save
        prediction_model
      end

    end

    class NanoPrediction < Prediction

      def self.from_json_dump dir, category
        Import::Enanomapper.import dir

        prediction_model = self.new(
          :endpoint => "log2(Net cell association)",
          :source => "https://data.enanomapper.net/",
          :species => "A549 human lung epithelial carcinoma cells",
          :unit => "log2(ug/Mg)"
        )
        params = {
          :feature_selection_algorithm => :correlation_filter,
          :feature_selection_algorithm_parameters => {:category => category},
          :neighbor_algorithm => "physchem_neighbors",
          :neighbor_algorithm_parameters => {:min_sim => 0.5},
          :prediction_algorithm => "OpenTox::Algorithm::Regression.physchem_regression",
          :prediction_algorithm_parameters => {:method => 'rf'}, # random forests
          } 
        training_dataset = Dataset.find_or_create_by(:name => "Protein Corona Fingerprinting Predicts the Cellular Interaction of Gold and Silver Nanoparticles")
        prediction_feature = Feature.find_or_create_by(name: "log2(Net cell association)", category: "TOX")
        #prediction_feature = Feature.find("579621b84de73e267b414e55")
        prediction_model[:prediction_feature_id] = prediction_feature.id
        model = Model::LazarRegression.create(prediction_feature, training_dataset, params)
        prediction_model[:model_id] = model.id
        repeated_cv = Validation::RepeatedCrossValidation.create model
        prediction_model[:repeated_crossvalidation_id] = Validation::RepeatedCrossValidation.create(model).id
        #prediction_model[:leave_one_out_validation_id] = Validation::LeaveOneOut.create(model).id
        prediction_model.save
        prediction_model
      end

    end

  end

end