forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasta.fs
100 lines (86 loc) · 2.52 KB
/
fasta.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/// The Computer Language Benchmarks Game
/// http://shootout.alioth.debian.org/
///
/// Contributed by Valentin Kraevskiy
module Fasta
let im = 139968
let ia = 3877
let ic = 29573
let mutable seed = 42
let inline random max =
seed <- (seed * ia + ic) % im
max * float seed / float im
let alu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"B
let makeCumulative =
List.fold (fun (cp, res) (c, p) -> cp + p, (c, cp + p) :: res) (0.0, [])
>> snd
>> List.toArray
let homoSapiens =
makeCumulative [ 'a'B, 0.3029549426680
'c'B, 0.1979883004921
'g'B, 0.1975473066391
't'B, 0.3015094502008 ]
let iub =
makeCumulative [ 'a'B, 0.27
'c'B, 0.12
'g'B, 0.12
't'B, 0.27
'B'B, 0.02
'D'B, 0.02
'H'B, 0.02
'K'B, 0.02
'M'B, 0.02
'N'B, 0.02
'R'B, 0.02
'S'B, 0.02
'V'B, 0.02
'W'B, 0.02
'Y'B, 0.02 ]
let inline selectRandom (f : _ []) =
let r = random 1.0
let rec find =
function
| 0 -> fst f.[0]
| n when r < snd f.[n] -> fst f.[n]
| n -> find (n - 1)
find <| f.Length - 1
let width = 60
let stream = System.Console.OpenStandardOutput()
let buffer = Array.create 1024 0uy
let mutable index = 0
let inline flush() =
stream.Write(buffer, 0, index)
index <- 0
let inline write b =
buffer.[index] <- b
index <- index + 1
if index = buffer.Length then flush()
let randomFasta desc table n =
Array.iter write desc
for i in 1..n do
write <| selectRandom table
if i % width = 0 then write '\n'B
if n % width <> 0 then write '\n'B
let repeatFasta desc (table : byte []) n =
Array.iter write desc
for i in 1..n do
write <| table.[(i - 1) % table.Length]
if i % width = 0 then write '\n'B
if n % width <> 0 then write '\n'B
[<EntryPoint>]
let main args =
let n =
try
int args.[0]
with _ -> 1000
repeatFasta ">ONE Homo sapiens alu\n"B alu (2 * n)
randomFasta ">TWO IUB ambiguity codes\n"B iub (3 * n)
randomFasta ">THREE Homo sapiens frequency\n"B homoSapiens (5 * n)
flush()
0