rust/src/test/bench/shootout-k-nucleotide-pipes.rs

225 lines
6.4 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-pretty (extra blank line is inserted in vec::mapi call)
// multi tasking k-nucleotide
extern mod std;
2012-09-05 14:32:05 -05:00
use std::sort;
use core::hashmap::HashMap;
use core::io::ReaderUtil;
use core::comm::{stream, Port, Chan};
use core::cmp::Ord;
// given a map, print a sorted version of it
fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
fn pct(xx: uint, yy: uint) -> float {
2012-08-01 19:30:05 -05:00
return (xx as float) * 100f / (yy as float);
}
fn le_by_val<TT:Copy,UU:Copy + Ord>(kv0: &(TT,UU),
kv1: &(TT,UU)) -> bool {
let (_, v0) = *kv0;
let (_, v1) = *kv1;
2012-08-01 19:30:05 -05:00
return v0 >= v1;
}
fn le_by_key<TT:Copy + Ord,UU:Copy>(kv0: &(TT,UU),
kv1: &(TT,UU)) -> bool {
let (k0, _) = *kv0;
let (k1, _) = *kv1;
2012-08-01 19:30:05 -05:00
return k0 <= k1;
}
// sort by key, then by value
fn sortKV<TT:Copy + Ord,UU:Copy + Ord>(orig: ~[(TT,UU)]) -> ~[(TT,UU)] {
return sort::merge_sort(sort::merge_sort(orig, le_by_key), le_by_val);
}
let mut pairs = ~[];
// map -> [(k,%)]
for mm.each |&key, &val| {
pairs.push((key, pct(val, total)));
2013-02-02 13:47:41 -06:00
}
let pairs_sorted = sortKV(pairs);
let mut buffer = ~"";
for pairs_sorted.each |kv| {
2012-12-14 17:57:59 -06:00
let (k,v) = copy *kv;
unsafe {
let b = str::raw::from_bytes(k);
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
buffer += (fmt!("%s %0.3f\n", b.to_ascii().to_upper().to_str_ascii(), v));
}
}
2012-08-01 19:30:05 -05:00
return buffer;
}
// given a map, search for the frequency of a pattern
fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint {
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
match mm.find(&str::to_bytes(key.to_ascii().to_lower().to_str_ascii())) {
2012-08-20 14:23:37 -05:00
option::None => { return 0u; }
2013-03-23 20:22:00 -05:00
option::Some(&num) => { return num; }
}
}
// given a map, increment the counter for a key
fn update_freq(mm: &mut HashMap<~[u8], uint>, key: &[u8]) {
let key = vec::slice(key, 0, key.len()).to_vec();
2013-03-23 20:22:00 -05:00
let newval = match mm.pop(&key) {
Some(v) => v + 1,
None => 1
};
mm.insert(key, newval);
}
// given a ~[u8], for each window call a function
// i.e., for "hello" and windows of size four,
// run it("hell") and it("ello"), then return "llo"
fn windows_with_carry(bb: &[u8], nn: uint,
it: &fn(window: &[u8])) -> ~[u8] {
let mut ii = 0u;
let len = vec::len(bb);
while ii < len - (nn - 1u) {
it(vec::slice(bb, ii, ii+nn));
ii += 1u;
}
return vec::slice(bb, len - (nn - 1u), len).to_vec();
}
fn make_sequence_processor(sz: uint,
2013-04-26 16:04:39 -05:00
from_parent: &comm::Port<~[u8]>,
to_parent: &comm::Chan<~str>) {
let mut freqs: HashMap<~[u8], uint> = HashMap::new();
let mut carry: ~[u8] = ~[];
let mut total: uint = 0u;
let mut line: ~[u8];
loop {
line = from_parent.recv();
if line == ~[] { break; }
carry = windows_with_carry(carry + line, sz, |window| {
2013-03-23 20:22:00 -05:00
update_freq(&mut freqs, window);
total += 1u;
});
}
let buffer = match sz {
2013-03-23 20:22:00 -05:00
1u => { sort_and_fmt(&freqs, total) }
2u => { sort_and_fmt(&freqs, total) }
3u => { fmt!("%u\t%s", find(&freqs, ~"GGT"), ~"GGT") }
4u => { fmt!("%u\t%s", find(&freqs, ~"GGTA"), ~"GGTA") }
6u => { fmt!("%u\t%s", find(&freqs, ~"GGTATT"), ~"GGTATT") }
12u => { fmt!("%u\t%s", find(&freqs, ~"GGTATTTTAATT"), ~"GGTATTTTAATT") }
18u => { fmt!("%u\t%s", find(&freqs, ~"GGTATTTTAATTTATAGT"), ~"GGTATTTTAATTTATAGT") }
2012-08-03 21:59:04 -05:00
_ => { ~"" }
};
2013-02-15 04:44:18 -06:00
to_parent.send(buffer);
}
// given a FASTA file on stdin, process sequence THREE
fn main() {
let args = os::args();
2013-04-26 16:04:39 -05:00
let rdr = if os::getenv(~"RUST_BENCH").is_some() {
// FIXME: Using this compile-time env variable is a crummy way to
// get to this massive data set, but include_bin! chokes on it (#2598)
let path = Path(env!("CFG_SRC_DIR"))
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
2012-09-25 18:23:04 -05:00
result::get(&io::file_reader(&path))
} else {
io::stdin()
};
// initialize each sequence sorter
2012-09-19 00:44:34 -05:00
let sizes = ~[1,2,3,4,6,12,18];
2012-08-20 14:23:37 -05:00
let streams = vec::map(sizes, |_sz| Some(stream()));
2013-02-15 04:44:18 -06:00
let mut streams = streams;
let mut from_child = ~[];
let to_child = vec::mapi(sizes, |ii, sz| {
let sz = *sz;
2012-08-20 14:23:37 -05:00
let mut stream = None;
stream <-> streams[ii];
let (from_child_, to_parent_) = stream.unwrap();
2013-02-15 04:44:18 -06:00
from_child.push(from_child_);
2013-02-02 05:10:12 -06:00
let (from_parent, to_child) = comm::stream();
2013-02-15 04:44:18 -06:00
do task::spawn_with(from_parent) |from_parent| {
2013-04-26 16:04:39 -05:00
make_sequence_processor(sz, &from_parent, &to_parent_);
};
2013-02-15 04:44:18 -06:00
to_child
});
// latch stores true after we've started
// reading the sequence of interest
let mut proc_mode = false;
while !rdr.eof() {
let line: ~str = rdr.read_line();
if str::len(line) == 0u { loop; }
2012-08-06 14:34:08 -05:00
match (line[0], proc_mode) {
// start processing if this is the one
2012-08-03 21:59:04 -05:00
('>' as u8, false) => {
2012-08-06 14:34:08 -05:00
match str::find_str_from(line, ~"THREE", 1u) {
2012-08-20 14:23:37 -05:00
option::Some(_) => { proc_mode = true; }
option::None => { }
}
}
// break our processing
2012-08-03 21:59:04 -05:00
('>' as u8, true) => { break; }
// process the sequence for k-mers
2012-08-03 21:59:04 -05:00
(_, true) => {
let line_bytes = str::to_bytes(line);
for sizes.eachi |ii, _sz| {
2012-12-14 17:57:59 -06:00
let mut lb = copy line_bytes;
to_child[ii].send(lb);
}
}
// whatever
2012-08-03 21:59:04 -05:00
_ => { }
}
}
// finish...
for sizes.eachi |ii, _sz| {
to_child[ii].send(~[]);
}
// now fetch and print result messages
for sizes.eachi |ii, _sz| {
io::println(from_child[ii].recv());
}
}