2012-07-06 17:15:52 -05:00
|
|
|
// xfail-pretty
|
|
|
|
|
|
|
|
// multi tasking k-nucleotide
|
|
|
|
|
|
|
|
use std;
|
|
|
|
import std::map;
|
|
|
|
import std::map::hashmap;
|
|
|
|
import std::sort;
|
|
|
|
|
2012-07-10 13:58:43 -05:00
|
|
|
import pipes::{stream, port, chan};
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// given a map, print a sorted version of it
|
2012-07-14 00:57:48 -05:00
|
|
|
fn sort_and_fmt(mm: hashmap<~[u8], uint>, total: uint) -> ~str {
|
2012-07-06 17:15:52 -05:00
|
|
|
fn pct(xx: uint, yy: uint) -> float {
|
2012-08-01 19:30:05 -05:00
|
|
|
return (xx as float) * 100f / (yy as float);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
pure fn le_by_val<TT: copy, UU: copy>(kv0: &(TT,UU),
|
|
|
|
kv1: &(TT,UU)) -> bool {
|
|
|
|
let (_, v0) = *kv0;
|
|
|
|
let (_, v1) = *kv1;
|
2012-08-01 19:30:05 -05:00
|
|
|
return v0 >= v1;
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
pure fn le_by_key<TT: copy, 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;
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort by key, then by value
|
|
|
|
fn sortKV<TT: copy, UU: copy>(orig: ~[(TT,UU)]) -> ~[(TT,UU)] {
|
2012-08-01 19:30:05 -05:00
|
|
|
return sort::merge_sort(le_by_val, sort::merge_sort(le_by_key, orig));
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut pairs = ~[];
|
|
|
|
|
|
|
|
// map -> [(k,%)]
|
|
|
|
mm.each(fn&(key: ~[u8], val: uint) -> bool {
|
|
|
|
vec::push(pairs, (key, pct(val, total)));
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2012-07-06 17:15:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let pairs_sorted = sortKV(pairs);
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut buffer = ~"";
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
pairs_sorted.each(fn&(kv: (~[u8], float)) -> bool unsafe {
|
|
|
|
let (k,v) = kv;
|
2012-08-22 19:24:52 -05:00
|
|
|
buffer += (fmt!("%s %0.3f\n", str::to_upper(str::unsafe::from_bytes(k)), v));
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2012-07-06 17:15:52 -05:00
|
|
|
});
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return buffer;
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a map, search for the frequency of a pattern
|
2012-07-14 00:57:48 -05:00
|
|
|
fn find(mm: hashmap<~[u8], uint>, key: ~str) -> uint {
|
2012-08-23 17:44:57 -05:00
|
|
|
match mm.find(str::to_bytes(str::to_lower(key))) {
|
2012-08-03 21:59:04 -05:00
|
|
|
option::none => { return 0u; }
|
|
|
|
option::some(num) => { return num; }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a map, increment the counter for a key
|
|
|
|
fn update_freq(mm: hashmap<~[u8], uint>, key: &[u8]) {
|
|
|
|
let key = vec::slice(key, 0, key.len());
|
2012-08-06 14:34:08 -05:00
|
|
|
match mm.find(key) {
|
2012-08-03 21:59:04 -05:00
|
|
|
option::none => { mm.insert(key, 1u ); }
|
|
|
|
option::some(val) => { mm.insert(key, 1u + val); }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
2012-07-26 16:40:00 -05:00
|
|
|
fn windows_with_carry(bb: &[u8], nn: uint,
|
2012-07-06 17:15:52 -05:00
|
|
|
it: fn(window: &[u8])) -> ~[u8] {
|
|
|
|
let mut ii = 0u;
|
|
|
|
|
|
|
|
let len = vec::len(bb);
|
|
|
|
while ii < len - (nn - 1u) {
|
2012-07-26 15:43:27 -05:00
|
|
|
it(vec::view(bb, ii, ii+nn));
|
2012-07-06 17:15:52 -05:00
|
|
|
ii += 1u;
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return vec::slice(bb, len - (nn - 1u), len);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2012-07-10 13:58:43 -05:00
|
|
|
fn make_sequence_processor(sz: uint, from_parent: pipes::port<~[u8]>,
|
2012-07-14 00:57:48 -05:00
|
|
|
to_parent: pipes::chan<~str>) {
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
let freqs: hashmap<~[u8], uint> = map::bytes_hash();
|
|
|
|
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| {
|
|
|
|
update_freq(freqs, window);
|
|
|
|
total += 1u;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let buffer = match sz {
|
2012-08-03 21:59:04 -05:00
|
|
|
1u => { sort_and_fmt(freqs, total) }
|
|
|
|
2u => { sort_and_fmt(freqs, total) }
|
2012-08-22 19:24:52 -05:00
|
|
|
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
|
|
|
_ => { ~"" }
|
2012-07-06 17:15:52 -05:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
//comm::send(to_parent, fmt!("yay{%u}", sz));
|
2012-07-06 17:15:52 -05:00
|
|
|
to_parent.send(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a FASTA file on stdin, process sequence THREE
|
2012-07-14 00:57:48 -05:00
|
|
|
fn main(args: ~[~str]) {
|
|
|
|
let rdr = if os::getenv(~"RUST_BENCH").is_some() {
|
2012-07-06 17:15:52 -05:00
|
|
|
// 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::connect(
|
2012-08-22 19:24:52 -05:00
|
|
|
env!("CFG_SRC_DIR"),
|
2012-07-14 00:57:48 -05:00
|
|
|
~"src/test/bench/shootout-k-nucleotide.data"
|
2012-07-06 17:15:52 -05:00
|
|
|
);
|
|
|
|
result::get(io::file_reader(path))
|
|
|
|
} else {
|
|
|
|
io::stdin()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// initialize each sequence sorter
|
|
|
|
let sizes = ~[1u,2u,3u,4u,6u,12u,18u];
|
|
|
|
let streams = vec::map(sizes, |_sz| some(stream()));
|
|
|
|
let streams = vec::to_mut(streams);
|
|
|
|
let mut from_child = ~[];
|
|
|
|
let to_child = vec::mapi(sizes, |ii, sz| {
|
|
|
|
let mut stream = none;
|
|
|
|
stream <-> streams[ii];
|
|
|
|
let (to_parent_, from_child_) = option::unwrap(stream);
|
|
|
|
|
|
|
|
vec::push(from_child, from_child_);
|
|
|
|
|
2012-07-10 13:58:43 -05:00
|
|
|
let (to_child, from_parent) = pipes::stream();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
do task::spawn_with(from_parent) |from_parent| {
|
|
|
|
make_sequence_processor(sz, from_parent, to_parent_);
|
|
|
|
};
|
|
|
|
|
|
|
|
to_child
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// latch stores true after we've started
|
|
|
|
// reading the sequence of interest
|
|
|
|
let mut proc_mode = false;
|
|
|
|
|
|
|
|
while !rdr.eof() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let line: ~str = rdr.read_line();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2012-07-09 16:37:48 -05:00
|
|
|
if str::len(line) == 0u { again; }
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match (line[0], proc_mode) {
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// 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-03 21:59:04 -05:00
|
|
|
option::some(_) => { proc_mode = true; }
|
|
|
|
option::none => { }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// break our processing
|
2012-08-03 21:59:04 -05:00
|
|
|
('>' as u8, true) => { break; }
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// process the sequence for k-mers
|
2012-08-03 21:59:04 -05:00
|
|
|
(_, true) => {
|
2012-08-23 17:44:57 -05:00
|
|
|
let line_bytes = str::to_bytes(line);
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
for sizes.eachi |ii, _sz| {
|
|
|
|
let mut lb = line_bytes;
|
|
|
|
to_child[ii].send(lb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whatever
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { }
|
2012-07-06 17:15:52 -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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|