summaryrefslogtreecommitdiff
path: root/lazar.rb
blob: 59ec9cacc812c6a80df9cba7b32cb5955edace28 (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
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 LazarTest < Test::Unit::TestCase

  def setup
    @predictions = []
    @models = []
  end

  def teardown
    @predictions.each {|p| p.delete(@@subjectid)}
    @models.each {|m| m.delete(@@subjectid)}
  end

=begin
=end
  def test_create_regression_model
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => @@regression_training_dataset.uri, :subjectid => @@subjectid}).to_s
    puts model_uri
    validate_owl model_uri,@@subjectid
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    @models << lazar
    assert_equal 185, lazar.features.size
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid).to_s
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    @predictions << prediction
    assert_equal prediction.value(compound).round_to(3),0.2849.round_to(3)
    assert_equal prediction.confidence(compound).round_to(3), 0.3223.round_to(3)
    #assert_equal prediction.value(compound).round_to(4), 0.2847.round_to(4)
    #assert_equal prediction.confidence(compound).round_to(4), 0.3223.round_to(4)
    assert_equal prediction.neighbors(compound).size, 73
  end

  def test_classification_model

    # create model
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => @@classification_training_dataset.uri, :subjectid => @@subjectid}).to_s
    validate_owl model_uri,@@subjectid
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    @models << lazar
    assert_equal lazar.features.size, 52

    # single prediction
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    @predictions << prediction
    #puts prediction_uri
    assert_equal prediction.value(compound), false
    assert_equal prediction.confidence(compound).round_to(4), 0.3067.round_to(4)
    assert_equal prediction.neighbors(compound).size, 14

    # dataset activity
    compound = OpenTox::Compound.from_smiles("CNN")
    prediction_uri  = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    prediction = OpenTox::LazarPrediction.find prediction_uri, @@subjectid
    @predictions << prediction
    assert !prediction.measured_activities(compound).empty?
    assert_equal prediction.measured_activities(compound).first, true
    assert prediction.value(compound).nil?

    # dataset prediction
    test_dataset = OpenTox::Dataset.create_from_csv_file("data/multicolumn.csv", @@subjectid)
    prediction = OpenTox::LazarPrediction.find lazar.run(:dataset_uri => test_dataset.uri, :subjectid => @@subjectid), @@subjectid
    @predictions << prediction
    assert_equal prediction.compounds.size, 4
    compound = OpenTox::Compound.from_smiles "CC(=Nc1ccc2c(c1)Cc1ccccc21)O"
    assert_equal prediction.value(compound), nil
    assert_equal prediction.measured_activities(compound).first, true
  end

  def test_classification_svm_model

    # create model
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => @@classification_training_dataset.uri, :subjectid => @@subjectid, :prediction_algorithm => "local_svm_classification"}).to_s
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    @models << lazar
    assert_equal lazar.features.size, 52

    # single prediction
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    @predictions << prediction
    assert_equal prediction.value(compound), false
    assert_equal prediction.confidence(compound).round_to(4), 0.4131.round_to(4)
    assert_equal prediction.neighbors(compound).size, 14

    # dataset prediction
    test_dataset = OpenTox::Dataset.create_from_csv_file("data/multicolumn.csv", @@subjectid)
    prediction = OpenTox::LazarPrediction.find lazar.run(:dataset_uri => test_dataset.uri, :subjectid => @@subjectid), @@subjectid
    @predictions << prediction
    assert_equal prediction.compounds.size, 4
    compound = OpenTox::Compound.from_smiles "CC(=Nc1ccc2c(c1)Cc1ccccc21)O"
    assert_equal prediction.value(compound), nil
    assert_equal prediction.measured_activities(compound).first, true

  end

=begin
  def test_ambit_classification_model

    # create model
    dataset_uri = "http://apps.ideaconsult.net:8080/ambit2/dataset/9"
    feature_uri ="http://apps.ideaconsult.net:8080/ambit2/feature/21573"
    #model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => dataset_uri, :prediction_feature => feature_uri}).to_s
    #lazar = OpenTox::Model::Lazar.find model_uri
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => dataset_uri, :prediction_feature => feature_uri, :subjectid => @@subjectid}).to_s
    validate_owl model_uri,@@subjectid
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    assert_equal lazar.features.size, 6609
    #puts "Model: #{lazar.uri}"
    #puts lazar.features.size

    # single prediction
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    #prediction_uri = lazar.run(:compound_uri => compound.uri)
    #prediction = OpenTox::LazarPrediction.find(prediction_uri)
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    #puts "Prediction: #{prediction.uri}"
    #puts prediction.value(compound)
    assert_equal prediction.value(compound), true
    #puts @prediction.confidence(compound).round_to(4)
    #assert_equal @prediction.confidence(compound).round_to(4), 0.3005.round_to(4)
    #assert_equal @prediction.neighbors(compound).size, 15
    #@prediction.delete(@@subjectid)

    # dataset activity
    #compound = OpenTox::Compound.from_smiles("CNN")
    #prediction_uri  = @lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    #@prediction = OpenTox::LazarPrediction.find prediction_uri, @@subjectid
    #assert !@prediction.measured_activities(compound).empty?
    #assert_equal @prediction.measured_activities(compound).first, true
    #assert @prediction.value(compound).nil?
    #@prediction.delete(@@subjectid)

    # dataset prediction
    #@lazar.delete(@@subjectid)
  end
=end

end