summaryrefslogtreecommitdiff
path: root/lib/lazar.rb
blob: 399f5c16582a13b28d28c0999d39f3f0f2cafc5d (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
=begin
* Name: lazar.rb
* Description: Lazar model representation
* Author: Andreas Maunz <andreas@maunz.de>, Christoph Helma
* Date: 10/2012
=end

module OpenTox

  module Model

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

      field :title, type: String
      field :description, type: String
      #field :parameters, type: Array, default: []
      field :creator, type: String, default: __FILE__
      # datasets
      field :training_dataset_id, type: BSON::ObjectId
      field :feature_dataset_id, type: BSON::ObjectId
      # algorithms
      #field :feature_generation, type: String
      #field :feature_calculation_algorithm, type: String
      field :prediction_algorithm, type: String
      field :similarity_algorithm, type: String
      # prediction features
      field :prediction_feature_id, type: BSON::ObjectId
      field :predicted_value_id, type: BSON::ObjectId
      field :predicted_variables, type: Array
      # parameters
      field :nr_hits, type: Boolean
      field :min_sim, type: Float
      #field :propositionalized, type:Boolean
      field :min_train_performance, type: Float

      attr_accessor :prediction_dataset
      attr_accessor :training_dataset
      attr_accessor :feature_dataset
      attr_accessor :query_fingerprint
      attr_accessor :neighbors

      # Check parameters for plausibility
      # Prepare lazar object (includes graph mining)
      # @param[Array] lazar parameters as strings
      # @param[Hash] REST parameters, as input by user
      def self.create training_dataset, feature_dataset, prediction_feature=nil, nr_hits=false, params={}
        
        lazar = OpenTox::Model::Lazar.new

        bad_request_error "No features found in feature dataset #{feature_dataset.id}." if feature_dataset.features.empty?
        lazar.feature_dataset_id = feature_dataset.id
        @training_dataset = training_dataset
        bad_request_error "Training dataset compounds do not match feature dataset compounds. Please ensure that they are in the same order." unless @training_dataset.compounds == feature_dataset.compounds
        lazar.training_dataset_id = @training_dataset.id

        if prediction_feature
          resource_not_found_error "No feature '#{params[:prediction_feature]}' in dataset '#{@training_dataset.id}'" unless @training_dataset.features.include?( params[:prediction_feature] )
        else # try to read prediction_feature from dataset
          resource_not_found_error "Please provide a prediction_feature parameter" unless @training_dataset.features.size == 1
          prediction_feature = @training_dataset.features.first
        end

        lazar.prediction_feature_id = prediction_feature.id
        lazar.title = prediction_feature.title 

        if params and params[:prediction_algorithm]
          bad_request_error "Unknown prediction_algorithm #{params[:prediction_algorithm]}" unless OpenTox::Algorithm::Neighbors.respond_to?(params[:prediction_algorithm])
          lazar.prediction_algorithm = params[:prediction_algorithm]
        end

        unless lazar.prediction_algorithm # set defaults
          # TODO consider params
          if prediction_feature.nominal
            lazar.prediction_algorithm = "OpenTox::Algorithm::Classification.weighted_majority_vote"
            lazar.similarity_algorithm = "OpenTox::Algorithm::Similarity.tanimoto"
            lazar.min_sim = 0.3 unless lazar.min_sim
          elsif prediction_feature.numeric
            lazar.prediction_algorithm = "OpenTox::Algorithm::Regression.local_svm_regression" 
            lazar.similarity_algorithm = "OpenTox::Algorithm::Similarity.cosine"
            # cosine similartiy is default
            lazar.min_sim = 0.7 unless lazar.min_sim
          end
        end
        #lazar.prediction_algorithm =~ /majority_vote/ ? lazar.propositionalized = false :  lazar.propositionalized = true

        lazar.min_sim = params[:min_sim].to_f if params[:min_sim] and params[:min_sim].numeric?
        # TODO: get info from training_dataset
        lazar.nr_hits =  nr_hits
        #lazar.feature_generation = feature_dataset.training_algorithm
        #lazar.parameters << {"title" => "feature_generation_uri", "paramValue" => params[:feature_generation_uri]}

        bad_request_error "Parameter min_train_performance is not numeric." if params[:min_train_performance] and !params[:min_train_performance].numeric?
        lazar.min_train_performance = params[:min_train_performance].to_f if params[:min_train_performance] and params[:min_train_performance].numeric?
        lazar.min_train_performance = 0.1 unless lazar.min_train_performance

        lazar.save
        lazar
      end

      def predict object

        # tailored for performance
        # all consistency checks should be done during model creation

        time = Time.now

        # prepare prediction dataset
        prediction_dataset = LazarPrediction.new
        prediction_feature = OpenTox::Feature.find prediction_feature_id
        prediction_dataset.title = "Lazar prediction for #{prediction_feature.title}",
        prediction_dataset.creator = __FILE__,

        confidence_feature = OpenTox::Feature.find_or_create_by({
          "title" => "Prediction confidence",
          "numeric" => true
        })

        prediction_dataset.features = [ confidence_feature, prediction_feature ]

        @training_dataset = OpenTox::Dataset.find(training_dataset_id)
        @feature_dataset = OpenTox::Dataset.find(feature_dataset_id)

        compounds = []
        case object.class.to_s
        when "OpenTox::Compound"
          compounds = [object] 
        when "Array"
          compounds = object
        when "OpenTox::Dataset"
          compounds = object.compounds
        else 
          bad_request_error "Please provide a OpenTox::Compound an Array of OpenTox::Compounds or an OpenTox::Dataset as parameter."
        end

        $logger.debug "Setup: #{Time.now-time}"
        time = Time.now

        @query_fingerprint = Algorithm.run(feature_dataset.feature_calculation_algorithm, compounds, @feature_dataset.features.collect{|f| f.name} )

        $logger.debug "Fingerprint calculation: #{Time.now-time}"
        time = Time.now

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

        compounds.each_with_index do |compound,c|

          $logger.debug "predict compound #{c+1}/#{compounds.size} #{compound.inchi}"

          database_activities = @training_dataset.values(compound,prediction_feature)
          if database_activities and !database_activities.empty?
            database_activities.each do |database_activity|
              $logger.debug "do not predict compound, it occurs in dataset with activity #{database_activity}"
              prediction_dataset.compound_ids << compound.id
              prediction_dataset[c,0] = database_activity
              prediction_dataset[c,1] = nil
            end
            next
          else
            t = Time.new

            if prediction_algorithm =~ /Regression/
              mtf = OpenTox::Algorithm::Transform::ModelTransformer.new(self)
              mtf.transform
              training_fingerprints = mtf.n_prop
              training_activities = mtf.activities
              p training_activities
              query_fingerprint = mtf.q_prop
              neighbors = [[nil,nil,nil,query_fingerprint]]
            else
              training_fingerprints = @feature_dataset.data_entries
              # TODO fix for multi feature datasets
              training_activities = @training_dataset.data_entries[i].first
              query_fingerprint = @query_fingerprint[c]
              neighbors = []
            end
            $logger.debug "Transform: #{Time.now-t}"
            t = Time.new
            

            # find neighbors
            training_fingerprints.each_with_index do |fingerprint, i|

              sim = Algorithm.run(similarity_algorithm,fingerprint, query_fingerprint)
              if sim > self.min_sim
                if prediction_algorithm =~ /Regression/
                  neighbors << [@feature_dataset.compounds[i],sim,training_activities[i], fingerprint]
                else
                  neighbors << [@feature_dataset.compounds[i],sim,training_activities[i]] 
                end
              end
            end

            if prediction_algorithm =~ /Regression/
              prediction = Algorithm.run(prediction_algorithm, neighbors, :min_train_performance => self.min_train_performance)
            else
              prediction = Algorithm.run(prediction_algorithm, neighbors)
            end

            $logger.debug "Prediction time: #{Time.now-time}"
            time = Time.now

            p prediction
            # AM: transform to original space (TODO)
            confidence_value = ((confidence_value+1.0)/2.0).abs if prediction.first and similarity_algorithm =~ /cosine/


            $logger.debug "predicted value: #{prediction[0]}, confidence: #{prediction[1]}"
          end
          prediction_dataset.compound_ids << compound
          prediction_dataset[c,0] = prediction[0]
          prediction_dataset[c,1] = prediction[1]

        end 
        prediction_dataset

      end
      
      def training_activities
        # TODO select predicted variable
            #@training_activities = @training_dataset.data_entries.collect{|entry|
              #act = entry[prediction_feature_pos] if entry
              #@prediction_feature.feature_type=="classification" ? @prediction_feature.value_map.invert[act] : act
            #}
        @training_dataset.data_entries.flatten
      end

    end

  end

end