|
| 1 | +#lang racket/base |
| 2 | + |
| 3 | +;;; The Computer Language Benchmarks Game |
| 4 | +;;; https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ |
| 5 | + |
| 6 | +;;; Derived from C version by Joern Inge Vestgaarden |
| 7 | +;;; and Jorge Peixoto de Morais Neto |
| 8 | +;;; Contributed by Sam Tobin-Hochstadt |
| 9 | + |
| 10 | +(require racket/cmdline racket/require (for-syntax racket/base) (only-in racket/flonum for/flvector) |
| 11 | + (filtered-in (λ (name) (regexp-replace #rx"unsafe-" name "")) |
| 12 | + racket/unsafe/ops)) |
| 13 | + |
| 14 | +(define +alu+ |
| 15 | + (bytes-append #"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" |
| 16 | + #"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" |
| 17 | + #"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" |
| 18 | + #"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" |
| 19 | + #"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" |
| 20 | + #"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" |
| 21 | + #"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA")) |
| 22 | + |
| 23 | +(define (build-table t) |
| 24 | + (cons (apply bytes (map (compose char->integer car) t)) |
| 25 | + (for/flvector ([i t]) (cdr i)))) |
| 26 | + |
| 27 | +(define IUB |
| 28 | + (build-table |
| 29 | + '([#\a . 0.27] [#\c . 0.12] [#\g . 0.12] [#\t . 0.27] [#\B . 0.02] |
| 30 | + [#\D . 0.02] [#\H . 0.02] [#\K . 0.02] [#\M . 0.02] [#\N . 0.02] |
| 31 | + [#\R . 0.02] [#\S . 0.02] [#\V . 0.02] [#\W . 0.02] [#\Y . 0.02]))) |
| 32 | + |
| 33 | +(define HOMOSAPIEN |
| 34 | + (build-table '([#\a . 0.3029549426680] [#\c . 0.1979883004921] |
| 35 | + [#\g . 0.1975473066391] [#\t . 0.3015094502008]))) |
| 36 | + |
| 37 | +;; ------------- |
| 38 | + |
| 39 | +(define line-length 60) |
| 40 | + |
| 41 | +(define IA 3877) |
| 42 | +(define IC 29573) |
| 43 | +(define IM 139968) |
| 44 | + |
| 45 | +;; ------------------------------- |
| 46 | + |
| 47 | +(define LAST 42) |
| 48 | + |
| 49 | +;; ------------------------------- |
| 50 | + |
| 51 | +(define (make-cumulative-table frequency-table) |
| 52 | + (define bs (car frequency-table)) |
| 53 | + (define ps (cdr frequency-table)) |
| 54 | + (define len (bytes-length bs)) |
| 55 | + (let loop ([i 0] [cum 0.0]) |
| 56 | + (when (fx< i len) |
| 57 | + (define this (flvector-ref ps i)) |
| 58 | + (define new (fl+ this cum)) |
| 59 | + (flvector-set! ps i new) |
| 60 | + (loop (fx+ 1 i) new)))) |
| 61 | + |
| 62 | +;; ------------- |
| 63 | + |
| 64 | +(define (random-next max) |
| 65 | + (set! LAST (fxmodulo (fx+ IC (fx* LAST IA)) IM)) |
| 66 | + (fl/ (fl* max (fx->fl LAST)) (fx->fl IM))) |
| 67 | + |
| 68 | +;; ------------- |
| 69 | + |
| 70 | +(define (repeat-fasta s count) |
| 71 | + (define out (current-output-port)) |
| 72 | + (define len (bytes-length s)) |
| 73 | + (define s2 (make-bytes (fx+ len line-length))) |
| 74 | + (bytes-copy! s2 0 s 0 len) |
| 75 | + (bytes-copy! s2 len s 0 line-length) |
| 76 | + (let loop ([count count] [pos 0]) |
| 77 | + (define line (fxmin line-length count)) |
| 78 | + (write-bytes s2 out pos (fx+ pos line)) |
| 79 | + (newline out) |
| 80 | + (define count* (fx- count line)) |
| 81 | + (when (fx> count* 0) |
| 82 | + (define pos* (fx+ pos line)) |
| 83 | + (loop count* (if (fx>= pos* len) (fx- pos* len) pos*))))) |
| 84 | + |
| 85 | + |
| 86 | +;; ------------- |
| 87 | + |
| 88 | +(define-syntax-rule (random-fasta genelist cnt) |
| 89 | + (let () |
| 90 | + (define out (current-output-port)) |
| 91 | + (define ps (cdr genelist)) |
| 92 | + (define cs (car genelist)) |
| 93 | + (let loop ([count cnt]) |
| 94 | + (define line (fxmin line-length count)) |
| 95 | + (define buf (make-bytes (fx+ 1 line-length))) |
| 96 | + (let inner ([pos 0]) |
| 97 | + (define r (random-next 1.0)) |
| 98 | + (define i (let wh ([i 0]) (if (fl< (flvector-ref ps i) r) (wh (fx+ i 1)) i))) |
| 99 | + (bytes-set! buf pos (bytes-ref cs i)) |
| 100 | + (define pos+ (fx+ pos 1)) |
| 101 | + (when (fx< pos+ line) |
| 102 | + (inner pos+))) |
| 103 | + (bytes-set! buf line (char->integer #\newline)) |
| 104 | + (write-bytes buf out 0 (fx+ line 1)) |
| 105 | + (define count- (fx- count line)) |
| 106 | + (when (fx> count- 0) |
| 107 | + (loop count-))))) |
| 108 | + |
| 109 | +;; ------------------------------- |
| 110 | + |
| 111 | +(define n (command-line #:args (n) (string->number n))) |
| 112 | + |
| 113 | +(make-cumulative-table IUB) |
| 114 | +(make-cumulative-table HOMOSAPIEN) |
| 115 | + |
| 116 | +(display ">ONE Homo sapiens alu\n") |
| 117 | +(repeat-fasta +alu+ (* n 2)) |
| 118 | +(display ">TWO IUB ambiguity codes\n") |
| 119 | +(random-fasta IUB (* n 3)) |
| 120 | +(display ">THREE Homo sapiens frequency\n") |
| 121 | +(random-fasta HOMOSAPIEN (* n 5)) |
0 commit comments