From fcb0574da3d9e52ad68669b7c84e0a8e029ad240 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Tue, 11 Sep 2012 12:57:00 +0200 Subject: Updated match routines --- lib/compound.rb | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'lib/compound.rb') diff --git a/lib/compound.rb b/lib/compound.rb index 19d00fb..da16c85 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -187,28 +187,18 @@ module OpenTox end =end - # Match an array of smarts strings, returns array with matching smarts - # @example - # compound = Compound.from_name("Benzene") - # compound.match(['cc','cN']) # returns ['cc'] - # @param [Array] smarts_array Array with Smarts strings - # @return [Array] Array with matching Smarts strings - def match(smarts_array) - obconversion = OpenBabel::OBConversion.new - obmol = OpenBabel::OBMol.new - obconversion.set_in_format('inchi') - obconversion.read_string(obmol,inchi) - smarts_pattern = OpenBabel::OBSmartsPattern.new - smarts_array.collect { |smarts| - smarts_pattern.init(smarts) - smarts if smarts_pattern.match(obmol) - }.compact - end - # Match an array of smarts strings, returns hash with matching smarts as key and number of non-unique hits as value + + # Match an array of smarts strings, returns hash + # Keys: matching smarts, values: number of non-unique hits, or 1 # @param [Array] smarts_array Array with Smarts strings - # @return [Hash] Hash with matching smarts as key and number of non-unique hits as value - def match_hits(smarts_array) + # @param [Boolean] Whether non-unique hits or 1 should be produced + # @return [Array] Array with matching Smarts strings + # @example { + # compound = Compound.from_name("Benzene") + # compound.match(['cc','cN']) # returns { 'cc' => 12, 'cN' => 0 } + # } + def match_hits(smarts_array, use_hits=true) obconversion = OpenBabel::OBConversion.new obmol = OpenBabel::OBMol.new obconversion.set_in_format('inchi') @@ -218,12 +208,21 @@ module OpenTox smarts_array.collect do |smarts| smarts_pattern.init(smarts) if smarts_pattern.match(obmol) - hits = smarts_pattern.get_map_list - smarts_hits[smarts] = hits.to_a.size + if use_hits + hits = smarts_pattern.get_map_list + smarts_hits[smarts] = hits.to_a.size + else + smarts_hits[smarts] = 1 + end end end return smarts_hits end + # Provided for backward compatibility + def match(smarts_array) + match_hits(smarts_array,false) + end + end end -- cgit v1.2.3