summaryrefslogtreecommitdiff
path: root/lib/lazar.rb
blob: d6a6f4703250d0bd6c8449f53b637b09ecdd2b39 (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
=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: "model"

      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: Symbol
      field :similarity_algorithm, type: Symbol
      # prediction features
      field :prediction_feature_id, type: BSON::ObjectId
      field :predicted_value_id, type: BSON::ObjectId
      field :predicted_variables, type: Array
      # parameters
      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, 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
        #@training_dataset = OpenTox::Dataset.find(feature_dataset.parameters.select{|p| p["title"] == "dataset_id"}.first["paramValue"])
        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
          lazar.prediction_algorithm = :weighted_majority_vote if prediction_feature.nominal
          lazar.prediction_algorithm = :local_svm_regression if prediction_feature.numeric
        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?
        lazar.nr_hits =  params[:nr_hits] if params[:nr_hits]
        lazar.feature_generation = feature_dataset.creator
        #lazar.parameters << {"title" => "feature_generation_uri", "paramValue" => params[:feature_generation_uri]}
        # TODO insert algorithm into feature dataset
        # TODO store algorithms in mongodb?
        if lazar.feature_generation =~ /fminer|bbrc|last/
          if (lazar[:nr_hits] == "true")
            lazar.feature_calculation_algorithm = "smarts_count"
          else
            lazar.feature_calculation_algorithm = "smarts_match"
          end
          lazar.similarity_algorithm = "tanimoto"
          lazar.min_sim = 0.3 unless lazar.min_sim
        elsif lazar.feature_generation =~/descriptor/ or lazar.feature_generation.nil?
          # cosine similartiy is default (e.g. used when no fetature_generation_uri is given and a feature_dataset_uri is provided instead)
          lazar.similarity_algorithm = "cosine"
          lazar.min_sim = 0.7 unless lazar.min_sim
        else
          bad_request_error "unkown feature generation method #{lazar.feature_generation}"
        end

        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 params

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

        time = Time.now

        # prepare prediction dataset
        prediction_dataset = OpenTox::Dataset.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 = []
        if params[:compound]
          compounds = [ params[:compound]] 
        elsif params[:compounds]
          compounds = params[:compounds]
        elsif params[:dataset]
          compounds = params[:dataset].compounds
        else 
          bad_request_error "Please provide one of the parameters: :compound, :compounds, :dataset"
        end

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

        @query_fingerprint = OpenTox::Algorithm::Descriptor.send( feature_calculation_algorithm, compounds, @feature_dataset.features.collect{|f| f["title"]} )

        $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, database_activity, nil]
            end
            next
          else

            # TODO reintroduce for regression
            #mtf = OpenTox::Algorithm::Transform::ModelTransformer.new(self)
            #mtf.transform
            #

            # find neighbors
            neighbors = []
            @feature_dataset.data_entries.each_with_index do |fingerprint, i|

              sim = OpenTox::Algorithm::Similarity.send(similarity_algorithm,fingerprint, @query_fingerprint[c])
              # TODO fix for multi feature datasets
              neighbors << [@feature_dataset.compounds[i],@training_dataset.data_entries[i].first,sim] if sim > self.min_sim
            end

            prediction = OpenTox::Algorithm::Classification.send(prediction_algorithm, neighbors)

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

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


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

        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