2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
|
2010-09-15 20:22:10 -05:00
|
|
|
/* -*- mode: rust; indent-tabs-mode: nil -*-
|
|
|
|
* Implementation of 'fasta' benchmark from
|
|
|
|
* Computer Language Benchmarks Game
|
|
|
|
* http://shootout.alioth.debian.org/
|
|
|
|
*/
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-09-05 14:32:05 -05:00
|
|
|
use io::WriterUtil;
|
2010-09-15 20:22:10 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn LINE_LENGTH() -> uint { return 60u; }
|
2010-09-15 20:22:10 -05:00
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
type myrandom = @{mut last: u32};
|
2012-01-13 04:48:55 -06:00
|
|
|
|
|
|
|
fn myrandom_next(r: myrandom, mx: u32) -> u32 {
|
|
|
|
r.last = (r.last * 3877u32 + 29573u32) % 139968u32;
|
|
|
|
mx * r.last / 139968u32
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type aminoacids = {ch: char, prob: u32};
|
2010-09-15 20:22:10 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn make_cumulative(aa: ~[aminoacids]) -> ~[aminoacids] {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut cp: u32 = 0u32;
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut ans: ~[aminoacids] = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for aa.each |a| { cp += a.prob; ans += ~[{ch: a.ch, prob: cp}]; }
|
2012-08-01 19:30:05 -05:00
|
|
|
return ans;
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn select_random(r: u32, genelist: ~[aminoacids]) -> char {
|
2012-08-01 19:30:05 -05:00
|
|
|
if r < genelist[0].prob { return genelist[0].ch; }
|
2012-06-29 18:26:56 -05:00
|
|
|
fn bisect(v: ~[aminoacids], lo: uint, hi: uint, target: u32) -> char {
|
2011-07-27 07:19:39 -05:00
|
|
|
if hi > lo + 1u {
|
|
|
|
let mid: uint = lo + (hi - lo) / 2u;
|
2011-08-19 17:16:48 -05:00
|
|
|
if target < v[mid].prob {
|
2012-08-01 19:30:05 -05:00
|
|
|
return bisect(v, lo, mid, target);
|
|
|
|
} else { return bisect(v, mid, hi, target); }
|
|
|
|
} else { return v[hi].ch; }
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
2012-12-06 17:56:52 -06:00
|
|
|
return bisect(copy genelist, 0, vec::len::<aminoacids>(genelist) - 1, r);
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn make_random_fasta(wr: io::Writer, id: ~str, desc: ~str, genelist: ~[aminoacids], n: int) {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_line(~">" + id + ~" " + desc);
|
2012-08-27 16:22:25 -05:00
|
|
|
let rng = @{mut last: rand::Rng().next()};
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut op: ~str = ~"";
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, n as uint) |_i| {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut op, select_random(myrandom_next(rng, 100u32),
|
|
|
|
genelist));
|
2012-02-23 03:44:04 -06:00
|
|
|
if str::len(op) >= LINE_LENGTH() {
|
2012-06-09 03:08:26 -05:00
|
|
|
wr.write_line(op);
|
2012-07-14 00:57:48 -05:00
|
|
|
op = ~"";
|
2011-12-22 16:42:52 -06:00
|
|
|
}
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
2012-06-09 03:08:26 -05:00
|
|
|
if str::len(op) > 0u { wr.write_line(op); }
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn make_repeat_fasta(wr: io::Writer, id: ~str, desc: ~str, s: ~str, n: int) unsafe {
|
2012-07-14 00:57:48 -05:00
|
|
|
wr.write_line(~">" + id + ~" " + desc);
|
|
|
|
let mut op: ~str = ~"";
|
2012-02-23 03:44:04 -06:00
|
|
|
let sl: uint = str::len(s);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, n as uint) |i| {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::raw::push_byte(&mut op, s[i % sl]);
|
2012-02-23 03:44:04 -06:00
|
|
|
if str::len(op) >= LINE_LENGTH() {
|
2012-06-09 03:08:26 -05:00
|
|
|
wr.write_line(op);
|
2012-07-14 00:57:48 -05:00
|
|
|
op = ~"";
|
2011-12-22 16:42:52 -06:00
|
|
|
}
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
2012-06-09 03:08:26 -05:00
|
|
|
if str::len(op) > 0u { wr.write_line(op); }
|
2010-09-15 20:22:10 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn acid(ch: char, prob: u32) -> aminoacids { return {ch: ch, prob: prob}; }
|
2011-07-26 07:49:40 -05:00
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
2012-06-09 03:08:26 -05:00
|
|
|
// alioth tests k-nucleotide with this data at 25,000,000
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"5000000"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"1000"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else {
|
|
|
|
args
|
|
|
|
};
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let writer = if os::getenv(~"RUST_BENCH").is_some() {
|
2012-09-25 18:23:04 -05:00
|
|
|
result::get(&io::file_writer(&Path("./shootout-fasta.data"),
|
2012-08-24 17:28:43 -05:00
|
|
|
~[io::Truncate, io::Create]))
|
2012-06-09 03:08:26 -05:00
|
|
|
} else {
|
|
|
|
io::stdout()
|
|
|
|
};
|
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
let n = int::from_str(args[1]).get();
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let iub: ~[aminoacids] =
|
|
|
|
make_cumulative(~[acid('a', 27u32), acid('c', 12u32), acid('g', 12u32),
|
2011-08-19 17:16:48 -05:00
|
|
|
acid('t', 27u32), acid('B', 2u32), acid('D', 2u32),
|
|
|
|
acid('H', 2u32), acid('K', 2u32), acid('M', 2u32),
|
|
|
|
acid('N', 2u32), acid('R', 2u32), acid('S', 2u32),
|
2012-06-29 18:26:56 -05:00
|
|
|
acid('V', 2u32), acid('W', 2u32), acid('Y', 2u32)]);
|
|
|
|
let homosapiens: ~[aminoacids] =
|
|
|
|
make_cumulative(~[acid('a', 30u32), acid('c', 20u32), acid('g', 20u32),
|
|
|
|
acid('t', 30u32)]);
|
2012-07-14 00:57:48 -05:00
|
|
|
let alu: ~str =
|
|
|
|
~"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
|
|
|
|
~"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
|
|
|
|
~"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
|
|
|
|
~"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
|
|
|
|
~"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
|
|
|
|
~"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
|
|
|
|
~"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
|
|
|
|
make_repeat_fasta(writer, ~"ONE", ~"Homo sapiens alu", alu, n * 2);
|
|
|
|
make_random_fasta(writer, ~"TWO", ~"IUB ambiguity codes", iub, n * 3);
|
|
|
|
make_random_fasta(writer, ~"THREE",
|
|
|
|
~"Homo sapiens frequency", homosapiens, n * 5);
|
2011-08-15 23:54:52 -05:00
|
|
|
}
|