public snapshot · system/tools/nib/bench/xlang/ · system@main · commit a92f56ab098e · shared 2026-11-22T22:43:57Z· frozen: this page does not change when the repository does
all files
  1package main
  2
  3import (
  4	"bufio"
  5	"os"
  6)
  7
  8const alu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
  9
 10const (
 11	im = 139968
 12	ia = 3877
 13	ic = 29573
 14)
 15
 16var last uint32 = 42
 17
 18func random(max float64) float64 {
 19	last = (last*ia + ic) % im
 20	return max * float64(last) / im
 21}
 22
 23type entry struct {
 24	c byte
 25	p float64
 26}
 27
 28func cumulative(t []entry) []entry {
 29	out := make([]entry, len(t))
 30	p := 0.0
 31	for i, e := range t {
 32		p += e.p
 33		out[i] = entry{e.c, p}
 34	}
 35	return out
 36}
 37
 38func repeatFasta(w *bufio.Writer, header string, s string, n int) {
 39	w.WriteString(header)
 40	w.WriteByte('\n')
 41	pos := 0
 42	for n > 0 {
 43		line := n
 44		if line > 60 {
 45			line = 60
 46		}
 47		for i := 0; i < line; i++ {
 48			w.WriteByte(s[pos])
 49			pos++
 50			if pos == len(s) {
 51				pos = 0
 52			}
 53		}
 54		w.WriteByte('\n')
 55		n -= line
 56	}
 57}
 58
 59func randomFasta(w *bufio.Writer, header string, t []entry, n int) {
 60	w.WriteString(header)
 61	w.WriteByte('\n')
 62	cum := cumulative(t)
 63	buf := make([]byte, 0, 61)
 64	for n > 0 {
 65		line := n
 66		if line > 60 {
 67			line = 60
 68		}
 69		buf = buf[:0]
 70		for i := 0; i < line; i++ {
 71			r := random(1.0)
 72			c := cum[len(cum)-1].c
 73			for _, e := range cum {
 74				if e.p >= r {
 75					c = e.c
 76					break
 77				}
 78			}
 79			buf = append(buf, c)
 80		}
 81		buf = append(buf, '\n')
 82		w.Write(buf)
 83		n -= line
 84	}
 85}
 86
 87func main() {
 88	n := 2500000
 89	iub := []entry{
 90		{'a', 0.27}, {'c', 0.12}, {'g', 0.12}, {'t', 0.27},
 91		{'B', 0.02}, {'D', 0.02}, {'H', 0.02}, {'K', 0.02},
 92		{'M', 0.02}, {'N', 0.02}, {'R', 0.02}, {'S', 0.02},
 93		{'V', 0.02}, {'W', 0.02}, {'Y', 0.02},
 94	}
 95	homo := []entry{
 96		{'a', 0.3029549426680},
 97		{'c', 0.1979883004921},
 98		{'g', 0.1975473066391},
 99		{'t', 0.3015094502008},
100	}
101	w := bufio.NewWriter(os.Stdout)
102	defer w.Flush()
103	repeatFasta(w, ">ONE Homo sapiens alu", alu, n*2)
104	randomFasta(w, ">TWO IUB ambiguity codes", iub, n*3)
105	randomFasta(w, ">THREE Homo sapiens frequency", homo, n*5)
106}