diff options
author | dv <dv@dv.de> | 2011-07-19 17:55:13 +0200 |
---|---|---|
committer | dv <dv@dv.de> | 2011-07-19 17:55:13 +0200 |
commit | a688cb99ace5cbfd8802951f57c46f1eb1926a0b (patch) | |
tree | 4766386bcaa8dfe53103a5c8accd573b45fbb661 | |
parent | 3bb4f9e651d959ec53d8a84cdd0f0e52e4eade9d (diff) |
added p_sum_support
-rw-r--r-- | lib/algorithm.rb | 59 | ||||
-rw-r--r-- | lib/compound.rb | 4 | ||||
-rw-r--r-- | lib/model.rb | 10 |
3 files changed, 53 insertions, 20 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 53e58eb..3170efb 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -153,20 +153,41 @@ module OpenTox # @param [Array] features_a Features of first compound # @param [Array] features_b Features of second compound # @param [optional, Hash] weights Weights for all features - # @param [optional, Hash] params Keys: `fingerprints:, compound:, nr_hits:` are required - + # @param [optional, Hash] params Keys: `:training_compound, :compound, :fingerprints, :nr_hits, :compound_features_hits` are required # @return [Float] (Weighted) tanimoto similarity def self.tanimoto(features_a,features_b,weights=nil,params=nil) common_features = features_a & features_b all_features = (features_a + features_b).uniq - common_p_sum = 0.0 + #LOGGER.debug "dv --------------- common: #{common_features}, all: #{all_features}" if common_features.size > 0 if weights - LOGGER.debug "dv --------------- common_features: #{common_features}, params_hits: #{params[:compound_hits]}" - common_features.each{|f| common_p_sum += Algorithm.gauss(weights[f]*Algorithm.support(f,params))} - all_p_sum = 0.0 - LOGGER.debug "dv --------------- all_features: #{all_features}" - all_features.each{|f| all_p_sum += Algorithm.gauss(weights[f]*Algorithm.support(f,params))} + if params[:nr_hits] == "true" + params[:weights] = weights + params[:mode] = "min" + params[:features] = common_features + common_p_sum = Algorithm.p_sum_support(params) + params[:mode] = "max" + params[:features] = all_features + all_p_sum = Algorithm.p_sum_support(params) + #common_p_sum = 0.0 + #common_features.each{|f| + # compound_hits = params[:compound_features_hits][f] + # neighbor_hits = Algorithm.support(f,params) + # common_p = weights[f] * [compound_hits, neighbor_hits].min + # common_p_sum += Algorithm.gauss(common_p) + #} +# all_p_sum = 0.0 + #all_features.each{|f| + # compound_hits = params[:compound_features_hits][f] + # neighbor_hits = Algorithm.support(f,params) + # all_p = weights[f] * [compound_hits, neighbor_hits].max + # all_p_sum += Algorithm.gauss(all_p) + #} + else + common_features.each{|f| common_p_sum += Algorithm.gauss(weights[f])}#*Algorithm.support(f,params))} + all_p_sum = 0.0 + all_features.each{|f| all_p_sum += Algorithm.gauss(weights[f])}#*Algorithm.support(f,params))} + end common_p_sum/all_p_sum else common_features.to_f/all_features @@ -824,12 +845,26 @@ module OpenTox # @param [Hash] params Keys: `fingerprints:, compound:, nr_hits:` are required # return [Numeric] Support value def self.support(feature,params) - LOGGER.debug "dv ------------- feature: #{feature}" - LOGGER.debug "dv ------------- compound #{params[:compound]}" - LOGGER.debug "dv ------------- feature value #{params[:fingerprints][params[:compound]][feature]}" - params[:fingerprints][params[:compound]][feature] + params[:fingerprints][params[:training_compound]][feature] end + # Returns Support value of an fingerprint + # @param [Hash] params Keys: `:weights, :fingerprints, :features, :compound, :nr_hits:, :mode` are required + # return [Numeric] Support value + def self.p_sum_support(params) + p_sum = 0.0 + params[:features].each{|f| + #LOGGER.debug "compound_features_hits: #{params[:compound_features_hits][f]}" + compound_hits = params[:compound_features_hits][f] + #LOGGER.debug "compound_hits: #{compound_hits}" + neighbor_hits = Algorithm.support(f,params) + #LOGGER.debug "neighbor_hits: #{neighbor_hits}" + p_sum += eval "(Algorithm.gauss(params[:weights][f]) * ([compound_hits, neighbor_hits].compact.#{params[:mode]}))" + #LOGGER.debug "p_sum: #{p_sum}" + } + p_sum + end + end end diff --git a/lib/compound.rb b/lib/compound.rb index 3ec321a..616db2c 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -178,9 +178,9 @@ module OpenTox obconversion.read_string(obmol,@inchi) smarts_pattern = OpenBabel::OBSmartsPattern.new smarts_hits = {} - LOGGER.debug "dv ----------- obmol #{Compound.new(@inchi).to_smiles}" + #LOGGER.debug "dv ----------- obmol #{Compound.new(@inchi).to_smiles}" smarts_array.collect do |smarts| - LOGGER.debug "dv ----------- all smarts #{smarts}" + #LOGGER.debug "dv ----------- all smarts #{smarts}" smarts_pattern.init(smarts) if smarts_pattern.match(obmol) hits = smarts_pattern.get_map_list diff --git a/lib/model.rb b/lib/model.rb index 4aefea3..f9f2685 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -310,14 +310,12 @@ module OpenTox # Adds a neighbor to @neighbors if it passes the similarity threshold. def add_neighbor(training_features, training_compound) - compound_match_hits = {} + compound_features_hits = {} if @nr_hits == "true" - compound_match_hits = @compound.match_hits(@compound_features) #OpenTox::Compound.new(training_compound).match_hits(@compound_features) - LOGGER.debug "dv ------------ training_compound: #{training_compound}" - LOGGER.debug "dv ------------ training_features: #{training_features}" - LOGGER.debug "dv ------------ compound_features: #{@compound_features}" + compound_features_hits = @compound.match_hits(@compound_features) #OpenTox::Compound.new(training_compound).match_hits(@compound_features) + LOGGER.debug "dv ------------ compound_features_hits: #{@compound_features_hits}" end - sim = eval("#{@similarity_algorithm}(training_features, @compound_features, @p_values, ( { :compound => training_compound, :fingerprints => @fingerprints, :nr_hits => @nr_hits, :compound_hits => compound_match_hits } ) )") + sim = eval("#{@similarity_algorithm}(training_features, @compound_features, @p_values, ( { :training_compound => training_compound, :compound => @compound.uri, :fingerprints => @fingerprints, :nr_hits => @nr_hits, :compound_features_hits => compound_features_hits } ) )") if sim > @min_sim @activities[training_compound].each do |act| @neighbors << { |