From 639cbb47df2733ee062b65422f7ec18533ba8939 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Tue, 4 Dec 2012 12:51:49 +0000 Subject: [PATCH] added patmatch method to backtrack.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@2027 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/seq/backtrack.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/code_ruby/lib/maasha/seq/backtrack.rb b/code_ruby/lib/maasha/seq/backtrack.rb index 532bf64..948d68b 100644 --- a/code_ruby/lib/maasha/seq/backtrack.rb +++ b/code_ruby/lib/maasha/seq/backtrack.rb @@ -40,6 +40,24 @@ module BackTrack MAX_INS = 5 # Maximum number of insertions allowed MAX_DEL = 5 # Maximum number of deletions allowed + def patmatch(pattern, offset = 0, max_mismatches = 0, max_insertions = 0, max_deletions = 0) + self.patscan(pattern, offset, max_mismatches, max_insertions, max_deletions) do |m| + if block_given? + yield m + else + return m + end + + break + end + + if block_given? + yield nil + else + return nil + end + end + # ------------------------------------------------------------------------------ # str.scan(pattern[, max_mismatches[, max_insertions[, max_deletions]]]) # -> Array @@ -65,12 +83,12 @@ module BackTrack matches = [] if block_given? - while result = track_C(self.seq, self.length, pattern, offset, max_mismatches, max_insertions, max_deletions) + while result = scan_C(self.seq, self.length, pattern, offset, max_mismatches, max_insertions, max_deletions) yield Match.new(result.first, result.last, self.seq[result.first ... result.first + result.last]) offset = result.first + 1 end else - while result = track_C(self.seq, self.length, pattern, offset, max_mismatches, max_insertions, max_deletions) + while result = scan_C(self.seq, self.length, pattern, offset, max_mismatches, max_insertions, max_deletions) matches << Match.new(result.first, result.last, self.seq[result.first ... result.first + result.last]) offset = result.first + 1 end @@ -113,7 +131,8 @@ module BackTrack # 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 - # reporting the match endpoints. + # reporting the match endpoints. State is used to avoid ins followed by del and visa versa which + # are nonsense. builder.prefix %{ unsigned int backtrack( char *ss, // Sequence start @@ -145,7 +164,7 @@ module BackTrack # Find pattern (p) in a sequence (s) starting at pos, with at most mis mismatches, ins # insertions and del deletions. builder.c %{ - VALUE track_C( + VALUE scan_C( VALUE _s, // Sequence VALUE _len, // Sequence length VALUE _p, // Pattern -- 2.39.5