# 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
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
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