The Computer Language
Benchmarks Game

regex-dna Ruby #8 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org
#
# contributed by jose fco. gonzalez
# optimized & parallelized by Rick Branson
# optimized & parallelized by Aaron Tavistock

def count_pattern_matches(seq, matchers)
  threads = []
  results = {}
  matchers.each do |matcher|
    threads << Thread.new do
      read, write = IO.pipe
      Process.fork do
        read.close
        count = 0
        seq.scan( Regexp.new(matcher) ) { count += 1 }
        write.print(count)
      end
      Process.wait
      write.close
      results[matcher] = read.read.to_i
    end
  end
  threads.each { |t| t.join }
  results
end

seq = STDIN.read
origin_len = seq.size

seq.gsub!(/>[^\n]+\n|\n/,'')
clean_len = seq.size

matchers = [
  'agggtaaa|tttaccct',
  '[cgt]gggtaaa|tttaccc[acg]',
  'a[act]ggtaaa|tttacc[agt]t',
  'ag[act]gtaaa|tttac[agt]ct',
  'agg[act]taaa|ttta[agt]cct',
  'aggg[acg]aaa|ttt[cgt]ccct',
  'agggt[cgt]aa|tt[acg]accct',
  'agggta[cgt]a|t[acg]taccct',
  'agggtaa[cgt]|[acg]ttaccct'
]

match_counts = count_pattern_matches(seq, matchers)

replacements = {
  'B' => '(c|g|t)',
  'D' => '(a|g|t)',
  'H' => '(a|c|t)',
  'K' => '(g|t)',
  'M' => '(a|c)',
  'N' => '(a|c|g|t)',
  'R' => '(a|g)',
  'S' => '(c|t)',
  'V' => '(a|c|g)',
  'W' => '(a|t)',
  'Y' => '(c|t)'
}

seq.gsub!(/[BDHKMNRSVWY]/, replacements)

matchers.each do |matcher|
  print "#{matcher} #{match_counts[matcher]}\n"
end
print "\n#{origin_len}\n#{clean_len}\n#{seq.size}\n"
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
ruby 2.3.0p0 (2015-12-25 revision 53290) [i686-linux]


Sat, 26 Dec 2015 06:14:09 GMT

COMMAND LINE:
/usr/local/src/ruby/bin/ruby -W0 regexdna.yarv-8.yarv 0 < regexdna-input5000000.txt

PROGRAM OUTPUT:
agggtaaa|tttaccct 356
[cgt]gggtaaa|tttaccc[acg] 1250
a[act]ggtaaa|tttacc[agt]t 4252
ag[act]gtaaa|tttac[agt]ct 2894
agg[act]taaa|ttta[agt]cct 5435
aggg[acg]aaa|ttt[cgt]ccct 1537
agggt[cgt]aa|tt[acg]accct 1431
agggta[cgt]a|t[acg]taccct 1608
agggtaa[cgt]|[acg]ttaccct 2178

50833411
50000000
66800214