]> git.donarmstrong.com Git - biopieces.git/commitdiff
DRY refactorying of backtrack code
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 7 Oct 2013 15:05:20 +0000 (15:05 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 7 Oct 2013 15:05:20 +0000 (15:05 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2227 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/seq/backtrack.rb

index ead956e894661be6f3bb31e08ccbfe9ec45626f0..8a2fb12632585208fa1af9eda27ce6df36243336 100644 (file)
@@ -23,6 +23,7 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 require 'inline' # RubyInline
+require 'maasha/seq/ambiguity'
 
 # Error class for all exceptions to do with BackTrack.
 class BackTrackError < StandardError; end
@@ -35,6 +36,8 @@ class BackTrackError < StandardError; end
 # Algorithm based on code kindly provided by j_random_hacker @ Stackoverflow:
 # http://stackoverflow.com/questions/7557017/approximate-string-matching-using-backtracking/
 module BackTrack
+  extend Ambiguity
+
   OK_PATTERN = Regexp.new('^[bflsycwphqrimtnkvadegu]+$')
   MAX_MIS    = 5 # Maximum number of mismatches allowed
   MAX_INS    = 5 # Maximum number of insertions allowed
@@ -122,34 +125,7 @@ module BackTrack
   private
 
   inline do |builder|
-    # Macro for matching nucleotides including ambiguity codes.
-    builder.prefix %{
-      #define MATCH(A,B) ((bitmap[A] & bitmap[B]) != 0)
-    }
-
-    # Bitmap for matching nucleotides including ambiguity codes.
-    # For each value bits are set from the left: bit pos 1 for A,
-    # bit pos 2 for T, bit pos 3 for C, and bit pos 4 for G.
-    builder.prefix %{
-      char bitmap[256] = {
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 1,14, 4,11, 0, 0, 8, 7, 0, 0,10, 0, 5,15, 0,
-          0, 0, 9,12, 2, 2,13, 3, 0, 6, 0, 0, 0, 0, 0, 0,
-          0, 1,14, 4,11, 0, 0, 8, 7, 0, 0,10, 0, 5,15, 0,
-          0, 0, 9,12, 2, 2,13, 3, 0, 6, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-      };
-    }
+    add_ambiguity_macro(builder)
 
     # Backtrack algorithm for matching a pattern (p) starting in a sequence (s) allowing for mis
     # mismatches, ins insertions and del deletions. ss is the start of the sequence, used only for