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

class DescriptorTest < MiniTest::Test

  def test_list
    # check available descriptors
    assert_equal 15,PhysChem.openbabel_descriptors.size,"incorrect number of Openbabel descriptors"
    assert_equal 45,PhysChem.joelib_descriptors.size,"incorrect number of Joelib descriptors"
    assert_equal 286,PhysChem.cdk_descriptors.size,"incorrect number of Cdk descriptors"
    assert_equal 346,PhysChem.descriptors.size,"incorrect number of physchem descriptors"
  end

  def test_smarts
    c = OpenTox::Compound.from_smiles "N=C=C1CCC(=F=FO)C1"
    File.open("/tmp/tmp.png","w+"){|f| f.puts c.png}
    assert_match /^PNG/,`file -b /tmp/tmp.png`
    File.delete "/tmp/tmp.png"
    s = Smarts.find_or_create_by(:smarts => "F=F")
    result = c.smarts_match [s]
    assert_equal [1], result
    smarts = ["CC", "C", "C=C", "CO", "F=F", "C1CCCC1", "NN"].collect{|s| Smarts.find_or_create_by(:smarts => s)}
    result = c.smarts_match smarts
    assert_equal [1, 1, 1, 0, 1, 1, 0], result
    smarts_count = [10, 6, 2, 0, 2, 10, 0]
    result = c.smarts_match smarts, true
    assert_equal smarts_count, result
  end

  def test_compound_openbabel_single
    c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N"
    feature = PhysChem.find_or_create_by(:name => "Openbabel.logP")
    result = c.calculate_properties([feature])
    assert_equal 1.12518, result.first.round(5)
    assert_equal 1.12518, c.properties[feature.id.to_s].round(5)
  end

  def test_compound_cdk_single
    c = OpenTox::Compound.from_smiles "c1ccccc1"
    feature = PhysChem.find_or_create_by(:name => "Cdk.AtomCount.nAtom")
    result = c.calculate_properties([feature])
    assert_equal 12, result.first
    c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N"
    feature = PhysChem.find_or_create_by(:name => "Cdk.AtomCount.nAtom")
    result = c.calculate_properties([feature])
    assert_equal 17, result.first
    c_types = {"Cdk.CarbonTypes.C1SP1"=>1, "Cdk.CarbonTypes.C2SP1"=>0, "Cdk.CarbonTypes.C1SP2"=>0, "Cdk.CarbonTypes.C2SP2"=>1, "Cdk.CarbonTypes.C3SP2"=>0, "Cdk.CarbonTypes.C1SP3"=>2, "Cdk.CarbonTypes.C2SP3"=>1, "Cdk.CarbonTypes.C3SP3"=>1, "Cdk.CarbonTypes.C4SP3"=>0}
    physchem_features = c_types.collect{|t,nr| PhysChem.find_or_create_by(:name => t)}
    result = c.calculate_properties physchem_features
    assert_equal [1, 0, 0, 1, 0, 2, 1, 1, 0], result
  end

  def test_compound_joelib_single
    c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N"
    result = c.calculate_properties [PhysChem.find_or_create_by(:name => "Joelib.LogP")]
    assert_equal 2.65908, result.first
  end

  def test_compound_all
    c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N"
    amr = PhysChem.find_or_create_by(:name => "Cdk.ALOGP.AMR", :library => "Cdk")
    sbonds = PhysChem.find_by(:name => "Openbabel.sbonds")
    result = c.calculate_properties([amr,sbonds])
    assert_equal 30.8723, result[0]
    assert_equal 5, result[1]
  end

  def test_compound_descriptor_parameters
    PhysChem.descriptors
    c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N"
    result = c.calculate_properties [ "Openbabel.logP", "Cdk.AtomCount.nAtom", "Joelib.LogP" ].collect{|d| PhysChem.find_or_create_by(:name => d)}
    assert_equal 3, result.size
    assert_equal 1.12518, result[0].round(5)
    assert_equal 17.0, result[1].round(5)
    assert_equal 2.65908, result[2].round(5)
  end

end