1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  def scan(pattern, max_mismatches = 0, max_insertions = 0, max_deletions = 0)
    @pattern = pattern + "[#{max_mismatches},#{max_insertions},#{max_deletions}]"
    matches  = []

    pattern_file = Filesys.tmpfile

    File.open(pattern_file, mode = 'w') do |ios|
      ios.puts @pattern
    end

    cmd = "scan_for_matches #{pattern_file}"

    Open3.popen3(cmd) do |stdin, stdout, stderr|
      #raise SeqError, "scan_for_matches failed: #{stderr.gets}" unless stderr.empty?

      stdin << ">dummy\n#{self.seq}\n"

      stdin.close

      while block = stdout.gets($/ + ">")
        if block =~ />dummy:\[(\d+),(\d+)\]\n(.+)/
          start  = $1.to_i
          stop   = $2.to_i
          match  = $3.delete(" ")
          length = stop - start

          if block_given?
            yield Match.new(start, length, match)
          else
            matches << Match.new(start, length, match)
          end
        end
      end
    end

    matches unless block_given?
  end