The Computer Language
Benchmarks Game

k-nucleotide F# Mono LLVM program

source code

(* The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Jimmy Tang
 *)
open System
open System.IO
open System.Collections.Generic

// make our hashtable using System.Collections.Generic.Dictionary
let maketable (dna:string) (length:int) =
   let d = new Dictionary<_,_>()
   for start in 0..(dna.Length - length) do
      let substr = dna.Substring(start, length)
      let x = ref (ref 0)
      if d.TryGetValue(substr, x) then x.Value := ! !x + 1
      else d.[substr] <- ref 1
   d

// frequency for all substrings of a given length
let frequencies (dna:string) (length:int) = [
   let d = maketable dna length
   let total = d.Values |> Seq.map (!) |> Seq.sum
   yield! [ for pair in d ->
             pair.Key.ToUpper(), (float(pair.Value.Value) * 100.0 /float(total))]
         |> List.sortBy (snd >> (~-))
         |> List.map (fun (s,c) -> sprintf "%s %.3f" s c)
   yield ""
]

// frequency of occurrence for a particular substring
let countSubstring dna (substring:string) = [
   let d = maketable dna (substring.Length)
   yield (sprintf "%d\t%s"
      (if d.ContainsKey(substring) then !d.[substring] else 0)
      (substring.ToUpper()))
]

let input = Console.In
let dna = seq { while true do yield input.ReadLine() }
        |> Seq.takeWhile (fun x -> x <> null)
        |> Seq.skipWhile (fun x -> not(x.StartsWith(">THREE")))
        |> Seq.skip 1
        |> String.concat ""

[for len in [1;2] -> async { return frequencies dna len }] @
[for str in ["ggt";"ggta";"ggtatt";"ggtattttaatt";"ggtattttaatttatagt"]
 -> async { return countSubstring dna str }]
|> List.rev
|> Async.Parallel
|> Async.RunSynchronously
|> Array.rev
|> Seq.concat
|> Seq.iter (printfn "%s")
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
F# Compiler for F# 4.0 (Open Source Edition)
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Sat, 07 May 2016 18:55:27 GMT

MAKE:
mv knucleotide.fsharp knucleotide.fs
/usr/local/bin/fsharpc --target:exe --platform:x86 -O  -o knucleotide.fsharp_run.exe knucleotide.fs
F# Compiler for F# 4.0 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License
rm knucleotide.fs
4.55s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen knucleotide.fsharp_run.exe 0 < knucleotide-input250000.txt

PROGRAM FAILED 


PROGRAM OUTPUT:

Stacktrace:


Native stacktrace:


Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object