From 9e99495ecbff147218023c136bade9e56a502fed Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 13 Oct 2016 14:39:04 +0200 Subject: descriptor tests fixed --- lib/compound.rb | 14 ++++++-------- lib/model.rb | 4 ++-- lib/nanoparticle.rb | 2 -- test/compound.rb | 4 ++-- test/descriptor.rb | 47 +++++++++++++++++++++-------------------------- test/model.rb | 2 +- test/regression.rb | 2 +- 7 files changed, 33 insertions(+), 42 deletions(-) diff --git a/lib/compound.rb b/lib/compound.rb index 72882d0..b47364c 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -75,11 +75,7 @@ module OpenTox fingerprints[type] end - def calculated_properties types=["PhysChem::OPENBABEL"] - descriptors = [] - types.each do |t| - descriptors += PhysChem.descriptors OpenTox.const_get(t) - end + def calculate_properties descriptors=PhysChem::OPENBABEL # TODO: speedup java descriptors calculated_ids = properties.keys # BSON::ObjectId instances are not allowed as keys in a BSON document. @@ -98,7 +94,8 @@ module OpenTox end end save - properties.select{|id,v| descriptors.collect{|d| d.id.to_s}.include? id} + descriptors.collect{|d| properties[d.id.to_s]} + #properties.select{|id,v| descriptors.collect{|d| d.id.to_s}.include? id} end def smarts_match smarts, count=false @@ -303,8 +300,9 @@ module OpenTox # Calculate molecular weight of Compound with OB and store it in object # @return [Float] molecular weight def molecular_weight - mw_feature = PhysChem.find_or_create_by(:name => "Openbabel.MW") - calculated_properties[mw_feature.id.to_s] + mw_feature = PhysChem.find_or_create_by(:name => "Openbabel.MW").id.to_s + calculate_properties unless properties[mw_feature] + properties[mw_feature] end private diff --git a/lib/model.rb b/lib/model.rb index 859df8b..7029c31 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -131,7 +131,7 @@ module OpenTox end else # parse independent_variables - if (model.algorithms[:descriptors] & ["PhysChem::OPENBABEL","PhysChem::CDK","PhysChem::JOELIB"]).empty? + if (model.algorithms[:descriptors] & [PhysChem::OPENBABEL,PhysChem::CDK,PhysChem::JOELIB]).empty? properties = model.substances.collect { |s| s.properties } all_property_ids = properties.collect{|p| p.keys}.flatten.uniq model.descriptor_ids = all_property_ids.select{|id| model.algorithms[:descriptors].include? Feature.find(id).category } @@ -139,7 +139,7 @@ module OpenTox # calculate physchem properties else - properties = model.substances.collect { |s| s.calculated_properties(model.algorithms[:descriptors]) } + properties = model.substances.collect { |s| s.calculate_properties(model.algorithms[:descriptors]) } model.descriptor_ids = properties.collect{|p| p.keys}.flatten.uniq model.independent_variables = model.descriptor_ids.collect{|i| properties.collect{|p| p[i]}} end diff --git a/lib/nanoparticle.rb b/lib/nanoparticle.rb index f74f263..23e155c 100644 --- a/lib/nanoparticle.rb +++ b/lib/nanoparticle.rb @@ -6,8 +6,6 @@ module OpenTox field :core, type: Hash, default: {} field :coating, type: Array, default: [] - attr_accessor :scaled_values - def add_feature feature, value, dataset unless feature.name == "ATOMIC COMPOSITION" or feature.name == "FUNCTIONAL GROUP" # redundand case feature.category diff --git a/test/compound.rb b/test/compound.rb index d1cfb3d..19f51fd 100644 --- a/test/compound.rb +++ b/test/compound.rb @@ -124,7 +124,7 @@ print c.sdf def test_physchem c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C" - assert_equal PhysChem::OPENBABEL.size, c.calculated_properties.size - assert_equal PhysChem::OPENBABEL.size, c.calculated_properties(["PhysChem::OPENBABEL"]).size + assert_equal PhysChem::OPENBABEL.size, c.properties.size + assert_equal PhysChem::OPENBABEL.size, c.properties([PhysChem::OPENBABEL]).size end end diff --git a/test/descriptor.rb b/test/descriptor.rb index 2a5be60..911f5c3 100644 --- a/test/descriptor.rb +++ b/test/descriptor.rb @@ -27,57 +27,52 @@ class DescriptorTest < MiniTest::Test def test_compound_openbabel_single c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N" PhysChem.openbabel_descriptors # required for descriptor initialisation, TODO: move into libs - PhysChem.find_or_create_by(:name => "Openbabel.logP") - result = c.calculated_physchem [PhysChem.find_or_create_by(:name => "Openbabel.logP")] - assert_equal 1.12518, result.first.last.round(5) + 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 PhysChem.cdk_descriptors # required for descriptor initialisation, TODO: move into libs c = OpenTox::Compound.from_smiles "c1ccccc1" - result = c.calculated_physchem [PhysChem.find_or_create_by(:name => "Cdk.AtomCount.nAtom")] - assert_equal 12, result.first.last + 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" - result = c.calculated_physchem [PhysChem.find_or_create_by(:name => "Cdk.AtomCount.nAtom")] - assert_equal 17, result.first.last + 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.calculated_physchem physchem_features - assert_equal [1, 0, 0, 1, 0, 2, 1, 1, 0], result.values + result = c.calculate_properties physchem_features + assert_equal [1, 0, 0, 1, 0, 2, 1, 1, 0], result end def test_compound_joelib_single PhysChem.joelib_descriptors # required for descriptor initialisation, TODO: move into libs c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C#N" - result = c.calculated_physchem [PhysChem.find_or_create_by(:name => "Joelib.LogP")] - assert_equal 2.65908, result.first.last + 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" - result = c.calculated_physchem PhysChem.descriptors amr = PhysChem.find_or_create_by(:name => "Cdk.ALOGP.AMR", :library => "Cdk") sbonds = PhysChem.find_by(:name => "Openbabel.sbonds") - assert_equal 30.8723, result[amr.id.to_s] - assert_equal 5, result[sbonds.id.to_s] + 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.calculated_physchem [ "Openbabel.logP", "Cdk.AtomCount.nAtom", "Joelib.LogP" ].collect{|d| PhysChem.find_or_create_by(:name => d)} + 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 - result.each do |fid,v| - feature = Feature.find(fid) - case feature.name - when "Openbabel.logP" - assert_equal 1.12518, v.round(5) - when "Cdk.AtomCount.nAtom" - assert_equal 17.0, v.round(5) - when "Joelib.LogP" - assert_equal 2.65908, v.round(5) - end - end + 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 diff --git a/test/model.rb b/test/model.rb index 017ce10..322ad90 100644 --- a/test/model.rb +++ b/test/model.rb @@ -49,7 +49,7 @@ class ModelTest < MiniTest::Test def test_physchem_regression algorithms = { - :descriptors => ["PhysChem::OPENBABEL"], + :descriptors => [PhysChem::OPENBABEL], :similarity => { :method => "Algorithm::Similarity.cosine", } diff --git a/test/regression.rb b/test/regression.rb index b1051f1..cdbac4b 100644 --- a/test/regression.rb +++ b/test/regression.rb @@ -44,7 +44,7 @@ class LazarRegressionTest < MiniTest::Test def test_local_physchem_regression training_dataset = Dataset.from_csv_file "#{DATA_DIR}/EPAFHM.medi_log10.csv" algorithms = { - :descriptors => ["PhysChem::OPENBABEL"], + :descriptors => [PhysChem::OPENBABEL], :similarity => { :method => "Algorithm::Similarity.weighted_cosine", :min => 0.5 -- cgit v1.2.3