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
  1use std::io::{BufWriter, Write};
  2
  3const ALU: &[u8] = b"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
  4
  5const IM: u32 = 139968;
  6const IA: u32 = 3877;
  7const IC: u32 = 29573;
  8
  9struct Rng {
 10    last: u32,
 11}
 12
 13impl Rng {
 14    fn next(&mut self, max: f64) -> f64 {
 15        self.last = (self.last * IA + IC) % IM;
 16        max * self.last as f64 / IM as f64
 17    }
 18}
 19
 20fn make_cumulative(t: &[(u8, f64)]) -> Vec<(u8, f64)> {
 21    let mut p = 0.0;
 22    t.iter()
 23        .map(|&(c, q)| {
 24            p += q;
 25            (c, p)
 26        })
 27        .collect()
 28}
 29
 30fn repeat_fasta<W: Write>(out: &mut W, header: &str, s: &[u8], n: usize) {
 31    writeln!(out, "{}", header).unwrap();
 32    let mut pos = 0;
 33    let mut left = n;
 34    while left > 0 {
 35        let line = left.min(60);
 36        for _ in 0..line {
 37            out.write_all(&[s[pos]]).unwrap();
 38            pos += 1;
 39            if pos == s.len() {
 40                pos = 0;
 41            }
 42        }
 43        out.write_all(b"\n").unwrap();
 44        left -= line;
 45    }
 46}
 47
 48fn random_fasta<W: Write>(out: &mut W, header: &str, t: &[(u8, f64)], n: usize, rng: &mut Rng) {
 49    writeln!(out, "{}", header).unwrap();
 50    let cum = make_cumulative(t);
 51    let mut left = n;
 52    let mut buf = Vec::with_capacity(61);
 53    while left > 0 {
 54        let line = left.min(60);
 55        buf.clear();
 56        for _ in 0..line {
 57            let r = rng.next(1.0);
 58            let mut c = cum[cum.len() - 1].0;
 59            for &(ch, p) in &cum {
 60                if p >= r {
 61                    c = ch;
 62                    break;
 63                }
 64            }
 65            buf.push(c);
 66        }
 67        buf.push(b'\n');
 68        out.write_all(&buf).unwrap();
 69        left -= line;
 70    }
 71}
 72
 73fn main() {
 74    let n = 2_500_000usize;
 75    let iub: [(u8, f64); 15] = [
 76        (b'a', 0.27),
 77        (b'c', 0.12),
 78        (b'g', 0.12),
 79        (b't', 0.27),
 80        (b'B', 0.02),
 81        (b'D', 0.02),
 82        (b'H', 0.02),
 83        (b'K', 0.02),
 84        (b'M', 0.02),
 85        (b'N', 0.02),
 86        (b'R', 0.02),
 87        (b'S', 0.02),
 88        (b'V', 0.02),
 89        (b'W', 0.02),
 90        (b'Y', 0.02),
 91    ];
 92    let homo: [(u8, f64); 4] = [
 93        (b'a', 0.3029549426680),
 94        (b'c', 0.1979883004921),
 95        (b'g', 0.1975473066391),
 96        (b't', 0.3015094502008),
 97    ];
 98    let stdout = std::io::stdout();
 99    let mut out = BufWriter::new(stdout.lock());
100    let mut rng = Rng { last: 42 };
101    repeat_fasta(&mut out, ">ONE Homo sapiens alu", ALU, n * 2);
102    random_fasta(&mut out, ">TWO IUB ambiguity codes", &iub, n * 3, &mut rng);
103    random_fasta(
104        &mut out,
105        ">THREE Homo sapiens frequency",
106        &homo,
107        n * 5,
108        &mut rng,
109    );
110    out.flush().unwrap();
111}