summaryrefslogtreecommitdiff
path: root/fminer.rb
blob: 732132be841e11c0e8a813b45d1f0f19370e70d0 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
require 'rubygems'
require 'opentox-ruby'
require 'test/unit'
require 'validate-owl.rb'

class Float
  def round_to(x)
    (self * 10**x).round.to_f / 10**x
  end
end

class FminerTest < Test::Unit::TestCase

  def setup
    @dump_dir = FileUtils.mkdir_p File.join(File.dirname(__FILE__),"dump",File.basename(__FILE__,".rb"))
    FileUtils.mkdir_p File.join(File.dirname(__FILE__),"reference",File.basename(__FILE__,".rb"))
  end

  def cleanup # executed only when assertions succeed (teardown is called even when assertions fail)
    FileUtils.cp @dumpfile, @dumpfile.sub(/dump/,"reference")
    FileUtils.rm @dumpfile
    @dataset.delete(@@subjectid)
  end

  def dump
    @dataset = OpenTox::Dataset.find @dataset_uri, @@subjectid
    @dumpfile = File.join(@dump_dir,caller[0][/`.*'/][1..-2])+".yaml"
    File.open(@dumpfile,"w+"){|f| f.puts @dataset.to_yaml}
  end

  def test_bbrc
    feature = @@classification_training_dataset.features.keys.first
    @dataset_uri = OpenTox::Algorithm::Fminer::BBRC.new.run({:dataset_uri => @@classification_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid}).to_s
    dump 
    assert_equal 41, @dataset.features.size # 32 bit
    #assert_equal 52, @dataset.features.size
    # assert no hit counts present
    count=0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C10H13N3O2/c1-13(12-15)7-3-5-10(14)9-4-2-6-11-8-9/h2,4,6,8H,3,5,7H2,1H3').size > 0
        e.each { |p,h|
          if p.to_s.scan('bbrc/40').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/29').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/18').size>0
            count += 1 if h[0] == 1
          end
        }
      end
    }
    assert_equal 3, count

    # assert some values
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/31').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.97
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&a]-[#6&a]:[#6&a]:[#6&a]:[#6&a]-[#7&A]"
      end
    }
    cleanup
  end

  def test_regression_bbrc
    feature = File.join @@regression_training_dataset.uri,"feature/LC50_mmol" 
    @dataset_uri = OpenTox::Algorithm::Fminer::BBRC.new.run({:dataset_uri => @@regression_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid, :feature_type=>"paths"}).to_s
    dump
    assert_equal 131, @dataset.features.size # 32 bit
    #assert_equal 219, @dataset.features.size
    
    # assert no hit counts present
    count = 0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C5H10N2O2S/c1-4(10-3)7-9-5(8)6-2/h1-3H3,(H,6,8)').size > 0
        e.each { |p,h|
          if p.to_s.scan('bbrc/86').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/54').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/32').size>0
            count += 1 if h[0] == 1
          end
        }
      end
    }
    assert_equal 3, count

    # assert some values
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/188').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], "activating"
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 1.0
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&a]:[#6&a]:[#8&a]:[#6&a]:[#6&a]"
      end
    }

    cleanup
  end

  def test_last

    feature = @@classification_training_dataset.features.keys.first
    @dataset_uri = OpenTox::Algorithm::Fminer::LAST.new.run({:dataset_uri => @@classification_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid}).to_s
    dump
    assert_in_delta 21, @dataset.features.size, 2 # 32 bit

    # assert no hit counts present
    count = 0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C5H10N2O/c8-6-7-4-2-1-3-5-7/h1-5H2').size > 0
        e.each { |p,h|
          if p.to_s.scan('last/11').size>0
            count += 1 if h[0] == true
          end
          if p.to_s.scan('last/5').size>0
            count += 1 if h[0] == true
          end
          if p.to_s.scan('last/13').size>0
            count += 1 if h[0] == true
          end
        }
      end
    }
    #assert_equal 3, count

    # assert some values
    #@dataset.features.each { |c,e|
    #  if c.to_s.scan('feature/last/3').size > 0
    #    assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
    #    assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
    #    assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#8&A]=[#6&A]-[#6&A]-[#6&A]"
    #  end
    #}
    cleanup
  end


def test_regression_last
  feature = File.join @@regression_training_dataset.uri,"feature/LC50_mmol" 
  @dataset_uri = OpenTox::Algorithm::Fminer::LAST.new.run({:dataset_uri => @@regression_training_dataset.uri, :prediction_feature => feature, 
                                                          :min_frequency => 40,
                                                          :subjectid => @@subjectid}).to_s
  dump
  assert_in_delta 16, @dataset.features.size, 8

  
  # assert no hit counts present
  #count=0
  #@dataset.data_entries.each { |c,e|
  #  if c.to_s.scan('InChI=1S/C9H10O3/c1-2-12-9-5-7(6-10)3-4-8(9)11/h3-6,11H,2H2,1H3').size > 0
  #    e.each { |p,h|
  #      if p.to_s.scan('last/5').size>0
  #        count += 1 if h[0] == true
  #      end
  #      if p.to_s.scan('last/11').size>0
  #        count += 1 if h[0] == true
  #      end
  #      if p.to_s.scan('last/13').size>0
  #        count += 1 if h[0] == true
  #      end
  #    }
  #  end
  #}
  #assert_in_delta 3, count, 2

  # assert some values
  #@dataset.features.each { |c,e|
  #  if c.to_s.scan('feature/last/3').size > 0
  #    assert_equal e['http://www.opentox.org/api/1.1#effect'], "activating"
  #    assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 1.0
  #    assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#8&A]=[#6&A]-[#8&A]-[#6&A]"
  #  end
  #}
  cleanup
end



  def test_bbrc_rest_parameters_nr_hits
    feature = @@classification_training_dataset.features.keys.first
    @dataset_uri = OpenTox::RestClientWrapper.post(File.join(CONFIG[:services]["opentox-algorithm"],"fminer","bbrc"),{
      "dataset_uri" => @@classification_training_dataset.uri,
      "prediction_feature" => feature,
      "nr_hits" => true,
       :subjectid => @@subjectid })
    dump
    assert_equal 41, @dataset.features.size # 32 bit
    #assert_equal 52, @dataset.features.size

    # assert hit counts present
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C14H19N3S.ClH/c1-16(2)9-10-17(12-13-6-5-11-18-13)14-7-3-4-8-15-14;/h3-8,11H,9-10,12H2,1-2H3;1H').size > 0
        e.each { |p,h|
          if p.to_s.scan('bbrc/39').size>0
            assert_equal 6, h[0]
          end
          if p.to_s.scan('bbrc/18').size>0
            assert_equal 5, h[0]
          end
          if p.to_s.scan('bbrc/38').size>0
            assert_equal 14, h[0]
          end
        }
      end
    }

    # assert some values
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/31').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.97
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&a]-[#6&a]:[#6&a]:[#6&a]:[#6&a]-[#7&A]"
      end
    }

    cleanup
  end

  def test_bbrc_rest_parameters_bb_false
    feature = @@classification_training_dataset.features.keys.first
    @dataset_uri = OpenTox::RestClientWrapper.post(File.join(CONFIG[:services]["opentox-algorithm"],"fminer","bbrc"),{
      "dataset_uri" => @@classification_training_dataset.uri,
      "prediction_feature" => feature,
      "backbone" => false,
       :subjectid => @@subjectid })
    dump
    assert_equal 139, @dataset.features.size # 32 bit
    #assert_equal 52, @dataset.features.size

    # assert no hit counts present
    count=0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C10H13N3O2/c1-13(12-15)7-3-5-10(14)9-4-2-6-11-8-9/h2,4,6,8H,3,5,7H2,1H3').size > 0
        e.each { |p,h|
          if p.to_s.scan('bbrc/113').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/99').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/77').size>0
            count += 1 if h[0] == 1
          end
        }
      end
    }
    assert_equal 3, count


    # assert some values
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/31').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#8&A]-[#6&A]-[#7&A]-[#7&A]=[#8&A]"
      end
    }

    cleanup
  end

  def test_bbrc_multinomial
    feature = @@multinomial_training_dataset.features.keys.first
    @dataset_uri = OpenTox::Algorithm::Fminer::BBRC.new.run({:dataset_uri => @@multinomial_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid}).to_s
    dump 
    assert_equal 152, @dataset.features.size # 32 bit

    #assert no hit counts present
    count=0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C10H7NO2/c12-11(13)10-7-3-5-8-4-1-2-6-9(8)10/h1-7H').size > 0
        e.each { |p,h|
          if p.to_s.scan('bbrc/37').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/38').size>0
            count += 1 if h[0] == 1
          end
          if p.to_s.scan('bbrc/39').size>0
            count += 1 if h[0] == 1
          end
        }
      end
    }
    assert_equal 3, count

    # assert some values
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/0').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 1.00
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&A]-[#6&A](=[#6&A])(-[#6&A])"
      end
    }
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/92').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 1
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#7&A]-[#6&a](:[#6&a]:[#6&a]:[#6&a])(:[#6&a]:[#6&a]-[#16&A])"
      end
    }
    @dataset.features.each { |c,e|
      if c.to_s.scan('feature/bbrc/42').size > 0
        assert_equal e['http://www.opentox.org/api/1.1#effect'], 3
        assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
        assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&a]:[#6&a]:[#6&a]:[#6&a]:[#6&a]:[#7&a]:[#6&a]"
      end
    }
    cleanup
  end

  def test_last_multinomial
    feature = @@multinomial_training_dataset.features.keys.first
    @dataset_uri = OpenTox::Algorithm::Fminer::LAST.new.run({:dataset_uri => @@multinomial_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid}).to_s
    dump 
    assert_in_delta 138, @dataset.features.size, 2 # 32 bit

    #assert no hit counts present
    count=0
    @dataset.data_entries.each { |c,e|
      if c.to_s.scan('InChI=1S/C7H6N2O4/c8-6-3-4(9(12)13)1-2-5(6)7(10)11/h1-3H,8H2,(H,10,11)').size > 0
        e.each { |p,h|
          if p.to_s.scan('last/127').size>0
            count += 1 if h[0] == true
          end
          if p.to_s.scan('last/54').size>0
            count += 1 if h[0] == true
          end
          if p.to_s.scan('last/120').size>0
            count += 1 if h[0] == true
          end
        }
      end
    }
    #assert_equal 3, count

    # assert some values
    #@dataset.features.each { |c,e|
    #  if c.to_s.scan('feature/last/54').size > 0
    #    assert_equal e['http://www.opentox.org/api/1.1#effect'], 1
    #    assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
    #    assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#7&A;$([#7&A](=[#8&A])=[#8&A]),$([#7&A](-[#6&a])=[#8&A])](~*)=[#8&A]"
    #  end
    #}
    #@dataset.features.each { |c,e|
    #  if c.to_s.scan('feature/last/48').size > 0
    #    assert_equal e['http://www.opentox.org/api/1.1#effect'], 2
    #    assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.99
    #    assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#6&A]=[#6&A](-[#6&A])-[#6&A]"
    #  end
    #}
    #@dataset.features.each { |c,e|
    #  if c.to_s.scan('feature/bbrc/76').size > 0
    #    assert_equal e['http://www.opentox.org/api/1.1#effect'], "2"
    #    assert_equal e['http://www.opentox.org/api/1.1#pValue'].to_f.round_to(2), 0.0
    #    assert_equal e['http://www.opentox.org/api/1.1#smarts'], "[#7&A]-[#6&a]:[#6&a]:[#6&a](-[#16&A;$([#16&A](-,=[#8&A])-[#6&a]),$([#16&A](=[#8&A])-[#6&a])](~*)):[#6&a]:[#6&a;$([#6&a](:[#6&a])(:[#6&a]):[#6&a]),$([#6&a](:[#6&a])(:[#6&a]):[#6&a])](~*)(~*):[#6&a]:[#6&a]"
    #  end
    #}
    cleanup
  end
  
  def test_match
    feature = @@classification_training_dataset.features.keys.first
    feature_dataset_uri = OpenTox::Algorithm::Fminer::BBRC.new.run({
      :dataset_uri => @@classification_training_dataset.uri, :prediction_feature => feature, :subjectid => @@subjectid}).to_s
    feature_dataset = OpenTox::Dataset.find(feature_dataset_uri,@@subjectid)
    tmp_resources = [ feature_dataset_uri ]
    [true,false].each do |hits|
      matched_dataset_uri = OpenTox::RestClientWrapper.post(File.join(CONFIG[:services]["opentox-algorithm"],"fminer","bbrc","match"),
        {:feature_dataset_uri => feature_dataset_uri, :dataset_uri => @@multinomial_training_dataset.uri, 
         :nr_hits => hits, :subjectid => @@subjectid}).to_s
      tmp_resources << matched_dataset_uri
      matched_dataset = OpenTox::Dataset.find(matched_dataset_uri,@@subjectid)
      # matched dataset should have same features as feature dataset
      assert_equal feature_dataset.features.keys.sort,matched_dataset.features.keys.sort
      # matched datset should have same compounds as input dataset for matching
      assert_equal matched_dataset.compounds.sort,@@multinomial_training_dataset.compounds.sort
      matched_dataset.compounds.each do |c|
        matched_dataset.features.keys.each do |f|
          if matched_dataset.data_entries[c] and matched_dataset.data_entries[c][f]
            v = matched_dataset.data_entries[c][f]
            if hits
              assert_equal v.size,1
              assert v[0].is_a?(Integer)
              assert v[0]>0
            else
              assert_equal v,[1]
            end 
          end
        end
      end
    end
    tmp_resources.each{|uri| OpenTox::RestClientWrapper.delete(uri,{:subjectid=>@@subjectid})}
  end

end