2012-12-10 19:32:48 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-06-29 00:05:50 -05:00
|
|
|
// xfail-pretty the `let to_child` line gets an extra newline
|
2012-07-06 17:15:52 -05:00
|
|
|
// multi tasking k-nucleotide
|
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
extern mod extra;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
use extra::sort;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::cmp::Ord;
|
|
|
|
use std::comm::{stream, Port, Chan};
|
|
|
|
use std::comm;
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::hashmap::HashMap;
|
|
|
|
use std::io::ReaderUtil;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::io;
|
|
|
|
use std::option;
|
|
|
|
use std::os;
|
|
|
|
use std::str;
|
|
|
|
use std::task;
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::util;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::vec;
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// given a map, print a sorted version of it
|
2013-04-03 08:28:36 -05:00
|
|
|
fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
|
2013-09-26 01:26:09 -05:00
|
|
|
fn pct(xx: uint, yy: uint) -> f64 {
|
|
|
|
return (xx as f64) * 100.0 / (yy as f64);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:43:25 -05:00
|
|
|
fn le_by_val<TT:Clone,
|
|
|
|
UU:Clone + Ord>(
|
2013-07-02 14:47:32 -05:00
|
|
|
kv0: &(TT,UU),
|
|
|
|
kv1: &(TT,UU))
|
|
|
|
-> bool {
|
|
|
|
let (_, v0) = (*kv0).clone();
|
|
|
|
let (_, v1) = (*kv1).clone();
|
2012-08-01 19:30:05 -05:00
|
|
|
return v0 >= v1;
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:43:25 -05:00
|
|
|
fn le_by_key<TT:Clone + Ord,
|
|
|
|
UU:Clone>(
|
2013-07-02 14:47:32 -05:00
|
|
|
kv0: &(TT,UU),
|
|
|
|
kv1: &(TT,UU))
|
|
|
|
-> bool {
|
|
|
|
let (k0, _) = (*kv0).clone();
|
|
|
|
let (k1, _) = (*kv1).clone();
|
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
|
2013-07-10 16:43:25 -05:00
|
|
|
fn sortKV<TT:Clone + Ord, UU:Clone + Ord>(orig: ~[(TT,UU)]) -> ~[(TT,UU)] {
|
2012-09-27 20:44:31 -05:00
|
|
|
return sort::merge_sort(sort::merge_sort(orig, le_by_key), le_by_val);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut pairs = ~[];
|
|
|
|
|
|
|
|
// map -> [(k,%)]
|
2013-08-03 11:45:23 -05:00
|
|
|
for (key, &val) in mm.iter() {
|
2013-07-17 01:49:42 -05:00
|
|
|
pairs.push(((*key).clone(), pct(val, total)));
|
2013-02-02 13:47:41 -06:00
|
|
|
}
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
let pairs_sorted = sortKV(pairs);
|
2012-09-19 18:55:01 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut buffer = ~"";
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for kv in pairs_sorted.iter() {
|
2013-07-02 14:47:32 -05:00
|
|
|
let (k,v) = (*kv).clone();
|
2012-09-19 18:55:01 -05:00
|
|
|
unsafe {
|
2013-09-05 07:17:24 -05:00
|
|
|
let b = str::raw::from_utf8(k);
|
2013-04-23 11:20:55 -05:00
|
|
|
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
|
2013-08-07 21:21:36 -05:00
|
|
|
// to_ascii_move and to_str_move to not do a unnecessary copy.
|
2013-09-30 01:13:47 -05:00
|
|
|
buffer.push_str(format!("{} {:0.3f}\n",
|
|
|
|
b.to_ascii().to_upper().to_str_ascii(), v));
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
|
|
|
}
|
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
|
2013-04-03 08:28:36 -05:00
|
|
|
fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint {
|
2013-04-23 11:20:55 -05:00
|
|
|
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
|
2013-08-07 21:21:36 -05:00
|
|
|
// to_ascii_move and to_str_move to not do a unnecessary copy.
|
2013-06-10 22:10:37 -05:00
|
|
|
let key = key.to_ascii().to_lower().to_str_ascii();
|
|
|
|
match mm.find_equiv(&key.as_bytes()) {
|
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; }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a map, increment the counter for a key
|
2013-04-03 08:28:36 -05:00
|
|
|
fn update_freq(mm: &mut HashMap<~[u8], uint>, key: &[u8]) {
|
2013-06-27 04:48:50 -05:00
|
|
|
let key = key.to_owned();
|
2013-03-23 20:22:00 -05:00
|
|
|
let newval = match mm.pop(&key) {
|
|
|
|
Some(v) => v + 1,
|
|
|
|
None => 1
|
|
|
|
};
|
|
|
|
mm.insert(key, newval);
|
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,
|
2013-03-07 16:38:38 -06:00
|
|
|
it: &fn(window: &[u8])) -> ~[u8] {
|
2012-07-06 17:15:52 -05:00
|
|
|
let mut ii = 0u;
|
|
|
|
|
2013-05-14 04:52:12 -05:00
|
|
|
let len = bb.len();
|
2012-07-06 17:15:52 -05:00
|
|
|
while ii < len - (nn - 1u) {
|
2013-06-27 04:48:50 -05:00
|
|
|
it(bb.slice(ii, ii+nn));
|
2012-07-06 17:15:52 -05:00
|
|
|
ii += 1u;
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:48:50 -05:00
|
|
|
return bb.slice(len - (nn - 1u), len).to_owned();
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:19:58 -05:00
|
|
|
fn make_sequence_processor(sz: uint,
|
2013-07-17 14:31:20 -05:00
|
|
|
from_parent: &Port<~[u8]>,
|
|
|
|
to_parent: &Chan<~str>) {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut freqs: HashMap<~[u8], uint> = HashMap::new();
|
2012-07-06 17:15:52 -05:00
|
|
|
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);
|
2012-07-06 17:15:52 -05:00
|
|
|
total += 1u;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-01 01:13:36 -06:00
|
|
|
let buffer = match sz {
|
2013-03-23 20:22:00 -05:00
|
|
|
1u => { sort_and_fmt(&freqs, total) }
|
|
|
|
2u => { sort_and_fmt(&freqs, total) }
|
2013-09-30 01:13:47 -05:00
|
|
|
3u => { format!("{}\t{}", find(&freqs, ~"GGT"), "GGT") }
|
|
|
|
4u => { format!("{}\t{}", find(&freqs, ~"GGTA"), "GGTA") }
|
|
|
|
6u => { format!("{}\t{}", find(&freqs, ~"GGTATT"), "GGTATT") }
|
|
|
|
12u => { format!("{}\t{}", find(&freqs, ~"GGTATTTTAATT"), "GGTATTTTAATT") }
|
|
|
|
18u => { format!("{}\t{}", find(&freqs, ~"GGTATTTTAATTTATAGT"), "GGTATTTTAATTTATAGT") }
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { ~"" }
|
2012-07-06 17:15:52 -05:00
|
|
|
};
|
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
to_parent.send(buffer);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a FASTA file on stdin, process sequence THREE
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2013-07-17 14:31:20 -05:00
|
|
|
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
|
2012-11-07 15:41:02 -06:00
|
|
|
// get to this massive data set, but include_bin! chokes on it (#2598)
|
2012-08-24 17:28:43 -05:00
|
|
|
let path = Path(env!("CFG_SRC_DIR"))
|
|
|
|
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
|
2013-07-26 20:36:51 -05:00
|
|
|
io::file_reader(&path).unwrap()
|
2012-07-06 17:15:52 -05:00
|
|
|
} else {
|
|
|
|
io::stdin()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-29 00:05:50 -05:00
|
|
|
// initialize each sequence sorter
|
|
|
|
let sizes = ~[1u,2,3,4,6,12,18];
|
|
|
|
let mut streams = vec::from_fn(sizes.len(), |_| Some(stream::<~str>()));
|
2012-07-06 17:15:52 -05:00
|
|
|
let mut from_child = ~[];
|
2013-08-09 22:09:47 -05:00
|
|
|
let to_child = do sizes.iter().zip(streams.mut_iter()).map |(sz, stream_ref)| {
|
2012-09-21 20:43:30 -05:00
|
|
|
let sz = *sz;
|
2013-06-29 00:05:50 -05:00
|
|
|
let stream = util::replace(stream_ref, None);
|
2013-03-16 14:49:12 -05:00
|
|
|
let (from_child_, to_parent_) = stream.unwrap();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
from_child.push(from_child_);
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
let (from_parent, to_child) = comm::stream();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
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_);
|
2012-07-06 17:15:52 -05:00
|
|
|
};
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
to_child
|
2013-06-29 00:05:50 -05:00
|
|
|
}.collect::<~[Chan<~[u8]>]>();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
|
|
|
|
2012-07-06 17:15:52 -05:00
|
|
|
// 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
|
|
|
|
2013-10-01 16:31:03 -05:00
|
|
|
if line.len() == 0u { continue; }
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-05-13 13:26:15 -05:00
|
|
|
match (line[0] as char, proc_mode) {
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// start processing if this is the one
|
2013-05-13 13:26:15 -05:00
|
|
|
('>', false) => {
|
2013-07-17 14:31:20 -05:00
|
|
|
match line.slice_from(1).find_str("THREE") {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(_) => { proc_mode = true; }
|
|
|
|
option::None => { }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// break our processing
|
2013-05-13 13:26:15 -05:00
|
|
|
('>', 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) => {
|
2013-06-10 22:10:37 -05:00
|
|
|
let line_bytes = line.as_bytes();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2013-07-17 14:31:20 -05:00
|
|
|
let lb = line_bytes.to_owned();
|
2012-07-06 17:15:52 -05:00
|
|
|
to_child[ii].send(lb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whatever
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish...
|
2013-08-03 11:45:23 -05:00
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2012-07-06 17:15:52 -05:00
|
|
|
to_child[ii].send(~[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now fetch and print result messages
|
2013-08-03 11:45:23 -05:00
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2012-07-06 17:15:52 -05:00
|
|
|
io::println(from_child[ii].recv());
|
|
|
|
}
|
|
|
|
}
|