|
| 1 | +-- The Computer Language Benchmarks Game |
| 2 | +-- http://benchmarksgame.alioth.debian.org/ |
| 3 | +-- contributed by Mike Pall |
| 4 | + |
| 5 | +-- Compability with Lua 5.3 |
| 6 | +if not loadstring then |
| 7 | + loadstring = load |
| 8 | +end |
| 9 | +if not unpack then |
| 10 | + unpack = table.unpack |
| 11 | +end |
| 12 | + |
| 13 | +local Last = 42 |
| 14 | +local function random(max) |
| 15 | + local y = (Last * 3877 + 29573) % 139968 |
| 16 | + Last = y |
| 17 | + return (max * y) / 139968 |
| 18 | +end |
| 19 | + |
| 20 | +local function make_repeat_fasta(id, desc, s, n) |
| 21 | + local write, sub = io.write, string.sub |
| 22 | + write(">", id, " ", desc, "\n") |
| 23 | + local p, sn, s2 = 1, #s, s..s |
| 24 | + for i=60,n,60 do |
| 25 | + write(sub(s2, p, p + 59), "\n") |
| 26 | + p = p + 60; if p > sn then p = p - sn end |
| 27 | + end |
| 28 | + local tail = n % 60 |
| 29 | + if tail > 0 then write(sub(s2, p, p + tail-1), "\n") end |
| 30 | +end |
| 31 | + |
| 32 | +local function make_random_fasta(id, desc, bs, n) |
| 33 | + io.write(">", id, " ", desc, "\n") |
| 34 | + loadstring([=[ |
| 35 | + local write, char, unpack, n, random = io.write, string.char, unpack, ... |
| 36 | + local buf, p = {}, 1 |
| 37 | + for i=60,n,60 do |
| 38 | + for j=p,p+59 do ]=]..bs..[=[ end |
| 39 | + buf[p+60] = 10; p = p + 61 |
| 40 | + if p >= 2048 then write(char(unpack(buf, 1, p-1))); p = 1 end |
| 41 | + end |
| 42 | + local tail = n % 60 |
| 43 | + if tail > 0 then |
| 44 | + for j=p,p+tail-1 do ]=]..bs..[=[ end |
| 45 | + p = p + tail; buf[p] = 10; p = p + 1 |
| 46 | + end |
| 47 | + write(char(unpack(buf, 1, p-1))) |
| 48 | + ]=], desc)(n, random) |
| 49 | +end |
| 50 | + |
| 51 | +local function bisect(c, p, lo, hi) |
| 52 | + local n = hi - lo |
| 53 | + if n == 0 then return "buf[j] = "..c[hi].."\n" end |
| 54 | + local mid = math.floor(n / 2) |
| 55 | + return "if r < "..p[lo+mid].." then\n"..bisect(c, p, lo, lo+mid).. |
| 56 | + "else\n"..bisect(c, p, lo+mid+1, hi).."end\n" |
| 57 | +end |
| 58 | + |
| 59 | +local function make_bisect(tab) |
| 60 | + local c, p, sum = {}, {}, 0 |
| 61 | + for i,row in ipairs(tab) do |
| 62 | + c[i] = string.byte(row[1]) |
| 63 | + sum = sum + row[2] |
| 64 | + p[i] = sum |
| 65 | + end |
| 66 | + return "local r = random(1)\n"..bisect(c, p, 1, #tab) |
| 67 | +end |
| 68 | + |
| 69 | +local alu = |
| 70 | + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG".. |
| 71 | + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA".. |
| 72 | + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT".. |
| 73 | + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA".. |
| 74 | + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG".. |
| 75 | + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC".. |
| 76 | + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA" |
| 77 | + |
| 78 | +local iub = make_bisect{ |
| 79 | + { "a", 0.27 }, |
| 80 | + { "c", 0.12 }, |
| 81 | + { "g", 0.12 }, |
| 82 | + { "t", 0.27 }, |
| 83 | + { "B", 0.02 }, |
| 84 | + { "D", 0.02 }, |
| 85 | + { "H", 0.02 }, |
| 86 | + { "K", 0.02 }, |
| 87 | + { "M", 0.02 }, |
| 88 | + { "N", 0.02 }, |
| 89 | + { "R", 0.02 }, |
| 90 | + { "S", 0.02 }, |
| 91 | + { "V", 0.02 }, |
| 92 | + { "W", 0.02 }, |
| 93 | + { "Y", 0.02 }, |
| 94 | +} |
| 95 | + |
| 96 | +local homosapiens = make_bisect{ |
| 97 | + { "a", 0.3029549426680 }, |
| 98 | + { "c", 0.1979883004921 }, |
| 99 | + { "g", 0.1975473066391 }, |
| 100 | + { "t", 0.3015094502008 }, |
| 101 | +} |
| 102 | + |
| 103 | +local N = tonumber(arg and arg[1]) or 1000 |
| 104 | +make_repeat_fasta('ONE', 'Homo sapiens alu', alu, N*2) |
| 105 | +make_random_fasta('TWO', 'IUB ambiguity codes', iub, N*3) |
| 106 | +make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, N*5) |
0 commit comments