k-nucleotide F# .NET program
source code
(* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* 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:
64-bit Ubuntu quad core
.NET SDK 9.0.100
Host Version: 9.0.0
Commit: 9d5a6a9aa4
<OutputType>Exe
<TargetFramework>net9.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false
Fri, 15 Nov 2024 02:01:14 GMT
MAKE:
cp knucleotide.fsharpcore Program.fs
cp Include/fsharpcore/program.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/opt/src/dotnet-sdk-9.0.100/dotnet build -c Release --use-current-runtime
Determining projects to restore...
Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/program.fsproj (in 759 ms).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/Program.fs(43,40): warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/program.fsproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/Program.fs(45,12): warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have equivalent nullability. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/program.fsproj]
program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/bin/Release/net9.0/linux-x64/program.dll
Build succeeded.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/Program.fs(43,40): warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/program.fsproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/Program.fs(45,12): warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have equivalent nullability. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/program.fsproj]
2 Warning(s)
0 Error(s)
Time Elapsed 00:00:10.21
12.32s to complete and log all make actions
COMMAND LINE:
./bin/Release/net9.0/linux-x64/program 0 < knucleotide-input25000000.txt
PROGRAM OUTPUT:
A 30.295
T 30.151
C 19.800
G 19.754
AA 9.177
TA 9.132
AT 9.131
TT 9.091
CA 6.002
AC 6.001
AG 5.987
GA 5.984
CT 5.971
TC 5.971
GT 5.957
TG 5.956
CC 3.917
GC 3.911
CG 3.909
GG 3.902
1471758 GGT
446535 GGTA
47336 GGTATT
893 GGTATTTTAATT
893 GGTATTTTAATTTATAGT