]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq/backtrack.rb
refactored backtrack methods to use options hash in method invocations
[biopieces.git] / code_ruby / lib / maasha / seq / backtrack.rb
index 8a2fb12632585208fa1af9eda27ce6df36243336..782252465d974c7491a2efa9a9185d9422a316e0 100644 (file)
@@ -44,21 +44,34 @@ module BackTrack
   MAX_DEL    = 5 # Maximum number of deletions allowed
 
   # ------------------------------------------------------------------------------
-  #   str.patmatch(pattern[, start|[start, stop][, max_mis[, max_ins[, max_del]]]])
+  #   str.patmatch(pattern[, options])
   #   -> Match
-  #   str.patmatch(pattern[, start|[start, stop][, max_mis[, max_ins[, max_del]]]]) { |match|
+  #   str.patmatch(pattern[, options]) { |match|
   #     block
   #   }
   #   -> Match
   #
+  #   options:
+  #     :start
+  #     :stop
+  #     :max_mismatches
+  #     :max_insertions
+  #     :max_deletions
+  #
   # ------------------------------------------------------------------------------
   # Method to iterate through a sequence from a given start position to the end of
   # the sequence or to a given stop position to locate a pattern allowing for a
   # maximum number of mismatches, insertions, and deletions. Insertions are
   # nucleotides found in the pattern but not in the sequence. Deletions are
   # nucleotides found in the sequence but not in the pattern.
-  def patmatch(pattern, start = 0, max_mismatches = 0, max_insertions = 0, max_deletions = 0)
-    self.patscan(pattern, start, max_mismatches, max_insertions, max_deletions) do |m|
+  def patmatch(pattern, options = {})
+    options[:start]          ||= 0
+    options[:stop]           ||= self.length - 1
+    options[:max_mismatches] ||= 0
+    options[:max_insertions] ||= 0
+    options[:max_deletions]  ||= 0
+
+    self.patscan(pattern, options) do |m|
       if block_given?
         yield m
       else
@@ -76,13 +89,20 @@ module BackTrack
   end
 
   # ------------------------------------------------------------------------------
-  #   str.patscan(pattern[, start|[start, stop][, max_mis[, max_ins[, max_del]]]])
+  #   str.patscan(pattern[, options])
   #   -> Array
-  #   str.patscan(pattern[, start|[start, stop][, max_mis[, max_ins[, max_del]]]]) { |match|
+  #   str.patscan(pattern[, options]) { |match|
   #     block
   #   }
   #   -> Match
   #
+  #   options:
+  #     :start
+  #     :stop
+  #     :max_mismatches
+  #     :max_insertions
+  #     :max_deletions
+  #
   # ------------------------------------------------------------------------------
   # Method to iterate through a sequence from a given start position to the end of
   # the sequence or to a given stop position to locate a pattern allowing for a
@@ -91,23 +111,30 @@ module BackTrack
   # nucleotides found in the sequence but not in the pattern. Matches found in
   # block context return the Match object. Otherwise matches are returned in an
   # Array of Match objects.
-  def patscan(pattern, start = 0, max_mismatches = 0, max_insertions = 0, max_deletions = 0)
-    if start.is_a? Array
-      start, stop = *start
-    else
-      stop = self.length - 1
-    end
+  def patscan(pattern, options = {})
+    options[:start]          ||= 0
+    options[:stop]           ||= self.length - 1
+    options[:max_mismatches] ||= 0
+    options[:max_insertions] ||= 0
+    options[:max_deletions]  ||= 0
 
     raise BackTrackError, "Bad pattern: #{pattern}"                                          unless pattern.downcase =~ OK_PATTERN
-    raise BackTrackError, "start: #{start} out of range (0 .. #{self.length - 1})"           unless (0 ... self.length).include? start
-    raise BackTrackError, "stop: #{stop} out of range (0 .. #{self.length - 1})"             unless (0 ... self.length).include? stop
-    raise BackTrackError, "max_mismatches: #{max_mismatches} out of range (0 .. #{MAX_MIS})" unless (0 .. MAX_MIS).include? max_mismatches
-    raise BackTrackError, "max_insertions: #{max_insertions} out of range (0 .. #{MAX_INS})" unless (0 .. MAX_INS).include? max_insertions
-    raise BackTrackError, "max_deletions: #{max_deletions} out of range (0 .. #{MAX_DEL})"   unless (0 .. MAX_DEL).include? max_deletions
+    raise BackTrackError, "start: #{options[:start]} out of range (0 .. #{self.length - 1})"           unless (0 ... self.length).include? options[:start]
+    raise BackTrackError, "stop: #{options[:stop]} out of range (0 .. #{self.length - 1})"             unless (0 ... self.length).include? options[:stop]
+    raise BackTrackError, "max_mismatches: #{options[:max_mismatches]} out of range (0 .. #{MAX_MIS})" unless (0 .. MAX_MIS).include? options[:max_mismatches]
+    raise BackTrackError, "max_insertions: #{options[:max_insertions]} out of range (0 .. #{MAX_INS})" unless (0 .. MAX_INS).include? options[:max_insertions]
+    raise BackTrackError, "max_deletions: #{options[:max_deletions]} out of range (0 .. #{MAX_DEL})"   unless (0 .. MAX_DEL).include? options[:max_deletions]
 
     matches = []
 
-    while result = scan_C(self.seq, pattern, start, stop, max_mismatches, max_insertions, max_deletions)
+    while result = scan_C(self.seq,
+                          pattern,
+                          options[:start],
+                          options[:stop],
+                          options[:max_mismatches],
+                          options[:max_insertions],
+                          options[:max_deletions]
+                         )
       match = Match.new(result.first, result.last, self.seq[result.first ... result.first + result.last])
 
       if block_given?
@@ -116,7 +143,7 @@ module BackTrack
         matches << match
       end
 
-      start = result.first + 1
+      options[:start] = result.first + 1
     end
 
     return matches.empty? ? nil : matches unless block_given?