Class: OpenTox::Model::Lazar

Inherits:
Object
  • Object
show all
Includes:
Algorithm, OpenTox::Model
Defined in:
lib/model.rb

Overview

Lazy Structure Activity Relationship class

Instance Attribute Summary collapse

Attributes included from OpenTox

#metadata, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Algorithm

gauss, median, #run, #to_rdfxml

Methods included from OpenTox

#add_metadata, #load_metadata, login, text_to_html, #to_rdfxml

Methods included from OpenTox::Model

#run

Constructor Details

#initialize(uri = nil) ⇒ Lazar



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/model.rb', line 75

def initialize(uri=nil)

  if uri
    super uri
  else
    super CONFIG[:services]["opentox-model"]
  end
  
  [OT.algorithm] = File.join(CONFIG[:services]["opentox-algorithm"],"lazar")

  @features = []
  @effects = {}
  @activities = {}
  @p_values = {}
  @fingerprints = {}

  @feature_calculation_algorithm = "Substructure.match"
  @similarity_algorithm = "Similarity.tanimoto"
  @prediction_algorithm = "Neighbors.weighted_majority_vote"

  @min_sim = 0.3

end

Instance Attribute Details

#activitiesObject

Returns the value of attribute activities.



73
74
75
# File 'lib/model.rb', line 73

def activities
  @activities
end

#compoundObject

Returns the value of attribute compound.



73
74
75
# File 'lib/model.rb', line 73

def compound
  @compound
end

#effectsObject

Returns the value of attribute effects.



73
74
75
# File 'lib/model.rb', line 73

def effects
  @effects
end

#feature_calculation_algorithmObject

Returns the value of attribute feature_calculation_algorithm.



73
74
75
# File 'lib/model.rb', line 73

def feature_calculation_algorithm
  @feature_calculation_algorithm
end

#featuresObject

Returns the value of attribute features.



73
74
75
# File 'lib/model.rb', line 73

def features
  @features
end

#fingerprintsObject

Returns the value of attribute fingerprints.



73
74
75
# File 'lib/model.rb', line 73

def fingerprints
  @fingerprints
end

#min_simObject

Returns the value of attribute min_sim.



73
74
75
# File 'lib/model.rb', line 73

def min_sim
  @min_sim
end

#p_valuesObject

Returns the value of attribute p_values.



73
74
75
# File 'lib/model.rb', line 73

def p_values
  @p_values
end

#prediction_algorithmObject

Returns the value of attribute prediction_algorithm.



73
74
75
# File 'lib/model.rb', line 73

def prediction_algorithm
  @prediction_algorithm
end

#prediction_datasetObject

Returns the value of attribute prediction_dataset.



73
74
75
# File 'lib/model.rb', line 73

def prediction_dataset
  @prediction_dataset
end

#similarity_algorithmObject

Returns the value of attribute similarity_algorithm.



73
74
75
# File 'lib/model.rb', line 73

def similarity_algorithm
  @similarity_algorithm
end

#subjectidObject

Returns the value of attribute subjectid.



73
74
75
# File 'lib/model.rb', line 73

def subjectid
  @subjectid
end

Class Method Details

.all(subjectid = nil) ⇒ Array

Get URIs of all lazar models



101
102
103
# File 'lib/model.rb', line 101

def self.all(subjectid=nil)
  RestClientWrapper.get(CONFIG[:services]["opentox-model"], :subjectid => subjectid).to_s.split("\n")
end

.create(params) ⇒ OpenTox::Model::Lazar

Create a new lazar model



115
116
117
118
119
# File 'lib/model.rb', line 115

def self.create(params)
  lazar_algorithm = OpenTox::Algorithm::Generic.new File.join( CONFIG[:services]["opentox-algorithm"],"lazar")
  model_uri = lazar_algorithm.run(params)
  OpenTox::Model::Lazar.find(model_uri, params[:subjectid])
end

.find(uri, subjectid = nil) ⇒ OpenTox::Model::Lazar

Find a lazar model



108
109
110
# File 'lib/model.rb', line 108

def self.find(uri, subjectid=nil)
  YAML.load RestClientWrapper.get(uri,{:accept => 'application/x-yaml', :subjectid => subjectid})
end

Instance Method Details

#database_activity(subjectid) ⇒ Boolean

Find database activities and store them in @prediction_dataset



295
296
297
298
299
300
301
302
303
304
# File 'lib/model.rb', line 295

def database_activity(subjectid)
  if @activities[@compound.uri]
    @activities[@compound.uri].each { |act| @prediction_dataset.add @compound.uri, [OT.dependentVariables], act }
    @prediction_dataset.(OT.hasSource => [OT.trainingDataset])
    @prediction_dataset.save(subjectid)
    true
  else
    false
  end
end

#delete(subjectid) ⇒ Object

Delete model at model service



312
313
314
# File 'lib/model.rb', line 312

def delete(subjectid)
  RestClientWrapper.delete(@uri, :subjectid => subjectid) unless @uri == CONFIG[:services]["opentox-model"]
end

#neighborsObject

Find neighbors and store them as object variable



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/model.rb', line 272

def neighbors

  @compound_features = eval("#{@feature_calculation_algorithm}(@compound,@features)") if @feature_calculation_algorithm

  @neighbors = []
  @fingerprints.each do |training_compound,training_features|
    sim = eval("#{@similarity_algorithm}(@compound_features,training_features,@p_values)")
    if sim > @min_sim
      @activities[training_compound].each do |act|
        @neighbors << {
          :compound => training_compound,
          :similarity => sim,
          :features => training_features,
          :activity => act
        }
      end
    end
  end

end

#parameter(param) ⇒ String

Get a parameter value



124
125
126
# File 'lib/model.rb', line 124

def parameter(param)
  [OT.parameters].collect{|p| p[OT.paramValue] if p[DC.title] == param}.compact.first
end

#predict(compound_uri, verbose = false, subjectid = nil) ⇒ OpenTox::Dataset

Predict a compound



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
# File 'lib/model.rb', line 161

def predict(compound_uri,verbose=false,subjectid=nil)

  @compound = Compound.new compound_uri
  features = {}

  unless @prediction_dataset
    #@prediction_dataset = cached_prediction
    #return @prediction_dataset if cached_prediction
    @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid)
    @prediction_dataset.( {
      OT.hasSource => @uri,
      DC.creator => @uri,
      # TODO: fix dependentVariable
      DC.title => URI.decode(File.basename( [OT.dependentVariables] )),
      OT.parameters => [{DC.title => "compound_uri", OT.paramValue => compound_uri}]
    } )
  end

  return @prediction_dataset if database_activity(subjectid)

  neighbors
  prediction = eval("#{@prediction_algorithm}(@neighbors,{:similarity_algorithm => @similarity_algorithm, :p_values => @p_values})")

  prediction_feature_uri = File.join( @prediction_dataset.uri, "feature", "prediction", File.basename([OT.dependentVariables]),@prediction_dataset.compounds.size.to_s)
  # TODO: fix dependentVariable
  @prediction_dataset.[OT.dependentVariables] = prediction_feature_uri

  if @neighbors.size == 0
    @prediction_dataset.add_feature(prediction_feature_uri, {
      OT.isA => OT.MeasuredFeature,
      OT.hasSource => @uri,
      DC.creator => @uri,
      DC.title => URI.decode(File.basename( [OT.dependentVariables] )),
      OT.error => "No similar compounds in training dataset.",
      OT.parameters => [{DC.title => "compound_uri", OT.paramValue => compound_uri}]
    })
    @prediction_dataset.add @compound.uri, prediction_feature_uri, prediction[:prediction]

  else
    @prediction_dataset.add_feature(prediction_feature_uri, {
      OT.isA => OT.ModelPrediction,
      OT.hasSource => @uri,
      DC.creator => @uri,
      DC.title => URI.decode(File.basename( [OT.dependentVariables] )),
      OT.prediction => prediction[:prediction],
      OT.confidence => prediction[:confidence],
      OT.parameters => [{DC.title => "compound_uri", OT.paramValue => compound_uri}]
    })
    @prediction_dataset.add @compound.uri, prediction_feature_uri, prediction[:prediction]

    if verbose
      if @feature_calculation_algorithm == "Substructure.match"
        f = 0
        @compound_features.each do |feature|
          feature_uri = File.join( @prediction_dataset.uri, "feature", "descriptor", f.to_s)
          features[feature] = feature_uri
          @prediction_dataset.add_feature(feature_uri, {
            OT.isA => OT.Substructure,
            OT.smarts => feature,
            OT.pValue => @p_values[feature],
            OT.effect => @effects[feature]
          })
          @prediction_dataset.add @compound.uri, feature_uri, true
          f+=1
        end
      else
        @compound_features.each do |feature|
          features[feature] = feature
          @prediction_dataset.add @compound.uri, feature, true
        end
      end
      n = 0
      @neighbors.each do |neighbor|
        neighbor_uri = File.join( @prediction_dataset.uri, "feature", "neighbor", n.to_s )
        @prediction_dataset.add_feature(neighbor_uri, {
          OT.compound => neighbor[:compound],
          OT.similarity => neighbor[:similarity],
          OT.measuredActivity => neighbor[:activity],
          OT.isA => OT.Neighbor
        })
        @prediction_dataset.add @compound.uri, neighbor_uri, true
        f = 0 unless f
        neighbor[:features].each do |feature|
          if @feature_calculation_algorithm == "Substructure.match"
            feature_uri = File.join( @prediction_dataset.uri, "feature", "descriptor", f.to_s) unless feature_uri = features[feature]
          else
            feature_uri = feature
          end
          @prediction_dataset.add neighbor[:compound], feature_uri, true
          unless features.has_key? feature
            features[feature] = feature_uri
            @prediction_dataset.add_feature(feature_uri, {
              OT.isA => OT.Substructure,
              OT.smarts => feature,
              OT.pValue => @p_values[feature],
              OT.effect => @effects[feature]
            })
            f+=1
          end
        end
        n+=1
      end
      # what happens with dataset predictions?
    end
  end

  @prediction_dataset.save(subjectid)
  @prediction_dataset
end

#predict_dataset(dataset_uri, subjectid = nil, waiting_task = nil) ⇒ OpenTox::Dataset

Predict a dataset



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/model.rb', line 133

def predict_dataset(dataset_uri, subjectid=nil, waiting_task=nil)
  @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid)
  @prediction_dataset.({
    OT.hasSource => @uri,
    DC.creator => @uri,
    DC.title => URI.decode(File.basename( [OT.dependentVariables] )),
    OT.parameters => [{DC.title => "dataset_uri", OT.paramValue => dataset_uri}]
  })
  d = Dataset.new(dataset_uri,subjectid)
  d.load_compounds(subjectid)
  count = 0
  d.compounds.each do |compound_uri|
    begin
      predict(compound_uri,false,subjectid)
      count += 1
      waiting_task.progress( count/d.compounds.size.to_f*100.0 ) if waiting_task
    rescue => ex
      LOGGER.warn "prediction for compound "+compound_uri.to_s+" failed: "+ex.message
    end
  end
  @prediction_dataset.save(subjectid)
  @prediction_dataset
end

#save(subjectid) ⇒ Object

Save model at model service



307
308
309
# File 'lib/model.rb', line 307

def save(subjectid)
  self.uri = RestClientWrapper.post(@uri,self.to_yaml,{:content_type =>  "application/x-yaml", :subjectid => subjectid})
end