]> git.donarmstrong.com Git - biopieces.git/commitdiff
reverted patternmatcher to original
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 30 Mar 2011 11:15:10 +0000 (11:15 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 30 Mar 2011 11:15:10 +0000 (11:15 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1316 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/patternmatcher.rb

index 17c9f4d26cf1a20d4b541f35cd2a5494d5b60f8e..b4d92b840c1e769867e2ac43cb2b8b5082f20eec 100644 (file)
@@ -61,6 +61,7 @@ module PatternMatcher
   # located a Match object will be returned. If all paths are exhausted and
   # no match is located the position is incremented. If no match is located
   # whatsoever, then nil is returned.
+  # TODO: converging paths should be skipped for speed-up.
   def match(pattern, pos = 0, max_mismatches = 0, max_insertions = 0, max_deletions = 0)
     @pattern        = pattern
     @max_mismatches = max_mismatches
@@ -75,33 +76,20 @@ module PatternMatcher
         new_paths = []
 
         paths.each do |path|
-          new_paths << path
+          next if path.exhausted?(@seq, @pattern)
+          return path.to_match if match_found?(path)
 
-          (path.insertions ... @max_insertions).each do |i|
-            new_path = path.dup
-            new_path.insertions    += i + 1
-            new_path.pattern_index += i + 1
-            new_paths << new_path
+          if path.match?(@seq, @pattern)
+            new_paths << path.match
+          elsif path.mismatches < max_mismatches
+            new_paths << path.mismatch
           end
 
-          (path.deletions ... @max_deletions).each do |i|
-            new_path = path.dup
-            new_path.length    += i + 1
-            new_path.deletions += i + 1
-            new_path.seq_index += i + 1
-            new_paths << new_path
-          end
+          new_paths << path.insertion if path.insertions < max_insertions
+          new_paths << path.deletion  if path.deletions  < max_deletions
         end
 
-        paths = []
-
-        new_paths.each do |p|
-          next if p.exhausted?(@seq, @pattern)
-          next if p.mismatches > @max_mismatches
-          return p.to_match if match_found?(p)
-          p.extend(@seq, @pattern)
-          paths << p
-        end
+        paths = new_paths
       end
 
       pos += 1
@@ -184,17 +172,41 @@ module PatternMatcher
       Match.new(@pos, @matches, @mismatches, @insertions, @deletions, @length)
     end
 
-    # Method that extends a path with either a match or a mismatch.
-    def extend(seq, pattern)
-      if self.match?(seq, pattern)
-        self.matches       += 1
-      else
-        self.mismatches    += 1
-      end
+    # Method that returns a new Match object for a matching path
+    def match
+      path_match                = self.dup
+      path_match.length        += 1
+      path_match.matches       += 1
+      path_match.seq_index     += 1
+      path_match.pattern_index += 1
+      path_match
+    end
+
+    # Method that returns a new Match object for a matching path
+    def mismatch
+      path_mismatch                = self.dup
+      path_mismatch.length        += 1
+      path_mismatch.mismatches    += 1
+      path_mismatch.seq_index     += 1
+      path_mismatch.pattern_index += 1
+      path_mismatch
+    end
+
+    # Method that returns a new Match object for a insertion path
+    def insertion
+      path_insertion                = self.dup
+      path_insertion.insertions    += 1
+      path_insertion.pattern_index += 1
+      path_insertion
+    end
 
-      self.length        += 1
-      self.seq_index     += 1
-      self.pattern_index += 1
+    # Method that returns a new Match object for a deletion path
+    def deletion
+      path_deletion            = self.dup
+      path_deletion.length    += 1
+      path_deletion.deletions += 1
+      path_deletion.seq_index += 1
+      path_deletion
     end
   end