The Computer Language
Benchmarks Game

regex-redux Ruby #6 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org
#
# regex-dna program contributed by jose fco. gonzalez
# optimized & parallelized by Rick Branson
# optimized for ruby2 by Aaron Tavistock
# converted from regex-dna program

require 'fiber'

seq = $stdin.read.force_encoding("ASCII-8BIT")
origin_len = seq.size

seq.gsub!(/>.*\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'
]

results = matchers.map do |matcher|
  Fiber.new do
    count = seq.scan( Regexp.new(matcher) ).size
    Fiber.yield "#{matcher} #{count}"
  end.resume
end

replacements = {
  /tHa[Nt]/ => '<4>',
  /aND|caN|Ha[DS]|WaS/ => '<3>',
  /a[NSt]|BY/ => '<2>',
  /<[^>]*>/ => '|',
  /\|[^|][^|]*\|/ => '-'
}
seq.gsub!(Regexp.new(replacements.keys.join('|')), replacements)

puts "#{results.join("\n")}\n\n#{origin_len}\n#{clean_len}\n#{seq.size}"
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]


Mon, 20 Mar 2017 23:17:07 GMT

COMMAND LINE:
/usr/local/src/ruby/bin/ruby -W0 regexredux.yarv-6.yarv 0 < regexredux-input50000.txt

UNEXPECTED OUTPUT 

13c13
< 428941
---
> 273927

PROGRAM OUTPUT:
agggtaaa|tttaccct 3
[cgt]gggtaaa|tttaccc[acg] 12
a[act]ggtaaa|tttacc[agt]t 43
ag[act]gtaaa|tttac[agt]ct 27
agg[act]taaa|ttta[agt]cct 58
aggg[acg]aaa|ttt[cgt]ccct 16
agggt[cgt]aa|tt[acg]accct 15
agggta[cgt]a|t[acg]taccct 18
agggtaa[cgt]|[acg]ttaccct 20

508411
500000
428941