From 9ff3f4f82dedb5ccaa924fe311486bad3ed1248d Mon Sep 17 00:00:00 2001
From: martinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Date: Fri, 21 Jun 2013 09:09:29 +0000
Subject: [PATCH] fixed ambiguity in levenshtein.rb

git-svn-id: http://biopieces.googlecode.com/svn/trunk@2183 74ccb610-7750-0410-82ae-013aeee3265d
---
 code_ruby/lib/maasha/{ => seq}/levenshtein.rb | 31 ++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
 rename code_ruby/lib/maasha/{ => seq}/levenshtein.rb (69%)

diff --git a/code_ruby/lib/maasha/levenshtein.rb b/code_ruby/lib/maasha/seq/levenshtein.rb
similarity index 69%
rename from code_ruby/lib/maasha/levenshtein.rb
rename to code_ruby/lib/maasha/seq/levenshtein.rb
index bbc2c70..b9f8243 100644
--- a/code_ruby/lib/maasha/levenshtein.rb
+++ b/code_ruby/lib/maasha/seq/levenshtein.rb
@@ -45,6 +45,35 @@ class Levenshtein
   # >>>>>>>>>>>>>>> RubyInline C code <<<<<<<<<<<<<<<
 
   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
+      };
+    }
+
     builder.prefix %{
       unsigned int min(unsigned int a, unsigned int b, unsigned int c)
       {
@@ -87,7 +116,7 @@ class Levenshtein
        
           for (j = 0; j < t_len; j++)
           {
-            cost = (s[i] == t[j]) ? 0 : 1;
+            cost = (MATCH(s[i], t[j])) ? 0 : 1;
             v1[j + 1] = min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost);
           }
        
-- 
2.39.5