The Computer Language
Benchmarks Game

regex-redux OCaml #4 program

source code

(*
 * The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * regex-dna program contributed by Paolo Ribeca, August 2011
 *
 * The regexp machinery comes from a previous OCaml version by
 *  Christophe TROESTLER and Mauricio Fernandez
 *
 * converted from regex-dna program
 *)

let workers = 16

let variants, variants_to_string =
  let variants = [|
    "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"
  |]
  (* Remove the "\\" which is mandatory in OCaml regex *)
  and re_bs = Str.regexp_string "\\" in
  Array.map Str.regexp_case_fold variants,
  Array.map (Str.global_replace re_bs "") variants

let iupacs =
  Array.map (fun (re, s) -> Str.regexp re, s) [|
    "tHa[Nt]", "<4>";
    "aND\\|caN\\|Ha[DS]\\|WaS", "<3>";
    "a[NSt]\\|BY", "<2>";
    "<[^>]*>", "\\|";
    "\\|[^|][^|]*\\|", "-"
  |]

(* Read all of a redirected FASTA format file from stdin *)
let file_data, file_length =
  let b = Buffer.create 0xffffff and s = String.create 0xfff and r = ref 1 in
  while !r > 0 do
    r := input stdin s 0 0xfff; Buffer.add_substring b s 0 !r
  done;
  Buffer.contents b, Buffer.length b

(* Remove FASTA sequence descriptions and all linefeed characters *)
let dna = Str.global_replace (Str.regexp "^>.*$\\|\n") "" file_data
let code_length = String.length dna

(* Count matches of [re] *)
let count re s =
  let i = ref 0 and n = ref 0 in
  try
    while true do
      i := 1 + Str.search_forward re s !i;
      incr n
    done;
    assert false
  with Not_found -> !n

let () =
  let chunk_size = code_length / workers
  and rem = code_length mod workers in
  assert (chunk_size >= 7);
  let w = Array.make workers stdin
  and red_workers = workers - 1 in
  for i = 0 to red_workers do
    let delta = if i > 0 then 7 else 0 in
    let lo = i * chunk_size + min i rem - delta in
    let len = chunk_size + (if i < rem then 1 else 0) + delta
    and input, output = Unix.pipe () in
    match Unix.fork () with
    | 0 ->
      Unix.close input;
      let chunk = String.sub dna lo len
      and output = Unix.out_channel_of_descr output in
      (* First all the regexps... *)
      Array.iter
        (fun re -> output_binary_int output (count re chunk))
        variants;
      (* ...and then all the IUPAC replacements *)
      if i > 0 then
        for j = 0 to 6 do
          chunk.[j] <- ' '
        done;
      let b = ref chunk in
      Array.iter
        (fun (re, s) -> b := Str.global_replace re s !b)
        iupacs;
      output_binary_int output (String.length !b - delta);
      exit 0
    | _ ->
      Unix.close output;
      w.(i) <- Unix.in_channel_of_descr input
  done;
  let counts_variants = Array.init (Array.length variants) (fun _ -> ref 0)
  and count_iupac = ref 0 in
  Array.iter
    (fun input ->
      Array.iter
        (fun count -> count := !count + input_binary_int input)
        counts_variants;
      count_iupac := !count_iupac + input_binary_int input)
    w;
  Array.iteri
    (fun i re -> Printf.printf "%s %i\n" re !(counts_variants.(i)))
    variants_to_string;
  Printf.printf "\n%i\n%i\n%i\n%!" file_length code_length !count_iupac

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
The OCaml native-code compiler, version 4.06.0


Sun, 05 Nov 2017 22:27:15 GMT

MAKE:
mv regexredux.ocaml-4.ocaml regexredux.ocaml-4.ml
/opt/src/ocaml-4.06.0/bin/ocamlopt -noassert -unsafe -fPIC -nodynlink -inline 100 -O3 unix.cmxa str.cmxa regexredux.ocaml-4.ml -o regexredux.ocaml-4.ocaml_run
File "regexredux.ocaml-4.ml", line 43, characters 41-54:
Warning 3: deprecated: String.create
Use Bytes.create instead.
File "regexredux.ocaml-4.ml", line 45, characters 55-56:
Error: This expression has type bytes but an expression was expected of type
         string
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:417: recipe for target 'regexredux.ocaml-4.ocaml_run' failed
make: [regexredux.ocaml-4.ocaml_run] Error 2 (ignored)
rm regexredux.ocaml-4.ml

0.03s to complete and log all make actions

COMMAND LINE:
./regexredux.ocaml-4.ocaml_run 0 < regexredux-input50000.txt

MAKE ERROR