summaryrefslogtreecommitdiff
path: root/test/data_entry.rb
blob: 7c9e49e7e183cb675dd3a389963c0ba590f9a0d4 (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
require_relative "setup.rb"

class DataEntryTest < MiniTest::Test

  def test_create

    # add features
    features = ["test1", "test2"].collect do |title|
      NumericBioAssay.find_or_create_by( :title => title, :numeric => true)
    end

    compounds = []
    input = [
      ["c1ccccc1NN",1,2],
      ["CC(C)N",4,5],
      ["C1C(C)CCCC1",6,7],
    ]
    input.each do |row|
      smi = row.shift
      compound = Compound.find_or_create_by(:smiles => smi)
      compounds << compound
      row.each_with_index do |value,i|
        DataEntry.find_or_create compound, features[i], value
      end
    end
    
    assert_equal 3, compounds.size
    assert_equal 2, features.size
    input.each_with_index do |row,i|
      row.each_with_index do |v,j|
        assert_equal DataEntry[compounds[i],features[j]], input[i][j]
      end
    end
  end

  def test_create_from_file
    d = OpenTox::Dataset.from_csv_file File.join(DATA_DIR,"EPAFHM.mini.csv")
    assert_equal OpenTox::Dataset, d.class
    refute_nil d.warnings
    assert_match /row 13/, d.warnings.join
    assert_match "EPAFHM.mini.csv",  d.source
    assert_equal 1, d.features.size
    feature = d.features.first
    assert_kind_of NumericBioAssay, feature
    assert_match "EPAFHM.mini.csv",  feature.source
    assert_equal 0.0113, DataEntry[d.compounds.first, feature]
    assert_equal 0.0113, d[d.compounds.first, feature]
    assert_equal 0.00323, DataEntry[d.compounds[5], feature]
    assert_equal 0.00323, d[d.compounds[5], feature]
  end

  def test_upload_kazius
    d = OpenTox::Dataset.from_csv_file File.join DATA_DIR, "kazius.csv"
    assert_empty d.warnings
    #  493 COC1=C(C=C(C(=C1)Cl)OC)Cl,1
    c = d.compounds[491]
    assert_equal c.smiles, "COc1cc(c(cc1Cl)OC)Cl"
    assert_equal DataEntry[c,d.features.first], 1
  end

  def test_upload_feature_dataset
    t = Time.now
    f = File.join DATA_DIR, "rat_feature_dataset.csv"
    d = OpenTox::Dataset.from_csv_file f
    assert_equal 458, d.features.size
    d.save
    p "Upload: #{Time.now-t}"
    d2 = OpenTox::Dataset.find d.id
    t = Time.now
    assert_equal d.features.size, d2.features.size
    csv = CSV.read f
    csv.shift # remove header
    assert_equal csv.size, d2.compounds.size
    assert_equal csv.first.size-1, d2.features.size
    d2.compounds.each_with_index do |compound,i|
      row = csv[i]
      row.shift # remove compound
      assert_equal row, d2.fingerprint(compound)
    end
    p "Dowload: #{Time.now-t}"
    d2.delete
    assert_raises Mongoid::Errors::DocumentNotFound do
      Dataset.find d.id
    end
  end


end