2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// 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.
|
|
|
|
|
2014-02-07 13:08:32 -06:00
|
|
|
// ignore-android: FIXME(#10393)
|
2014-05-10 19:39:08 -05:00
|
|
|
// ignore-pretty very bad with line comments
|
2013-11-09 21:02:23 -06:00
|
|
|
|
2012-07-06 17:15:52 -05:00
|
|
|
// multi tasking k-nucleotide
|
|
|
|
|
2015-01-07 20:53:58 -06:00
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2014-12-25 18:17:30 -06:00
|
|
|
use std::ascii::{AsciiExt, OwnedAsciiExt};
|
2015-01-03 21:42:21 -06:00
|
|
|
use std::cmp::Ordering::{self, Less, Greater, Equal};
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::collections::HashMap;
|
2014-01-31 14:35:36 -06:00
|
|
|
use std::mem::replace;
|
2014-11-09 23:26:10 -06:00
|
|
|
use std::num::Float;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::option;
|
|
|
|
use std::os;
|
2015-01-02 01:53:35 -06:00
|
|
|
use std::sync::mpsc::{channel, Sender, Receiver};
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::thread::Thread;
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-12-19 21:42:00 -06:00
|
|
|
fn f64_cmp(x: f64, y: f64) -> Ordering {
|
|
|
|
// arbitrarily decide that NaNs are larger than everything.
|
|
|
|
if y.is_nan() {
|
|
|
|
Less
|
|
|
|
} else if x.is_nan() {
|
|
|
|
Greater
|
|
|
|
} else if x < y {
|
|
|
|
Less
|
|
|
|
} else if x == y {
|
|
|
|
Equal
|
|
|
|
} else {
|
|
|
|
Greater
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-06 17:15:52 -05:00
|
|
|
// given a map, print a sorted version of it
|
2014-05-22 18:57:53 -05:00
|
|
|
fn sort_and_fmt(mm: &HashMap<Vec<u8> , uint>, total: uint) -> String {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// sort by key, then by value
|
2014-03-05 16:02:44 -06:00
|
|
|
fn sortKV(mut orig: Vec<(Vec<u8> ,f64)> ) -> Vec<(Vec<u8> ,f64)> {
|
2013-12-19 21:42:00 -06:00
|
|
|
orig.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b));
|
|
|
|
orig.sort_by(|&(_, a), &(_, b)| f64_cmp(b, a));
|
|
|
|
orig
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut pairs = Vec::new();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut buffer = String::new();
|
2013-11-28 06:52:11 -06:00
|
|
|
for &(ref k, v) in pairs_sorted.iter() {
|
2015-01-07 20:53:58 -06:00
|
|
|
buffer.push_str(format!("{:?} {:0.3}\n",
|
2014-12-25 18:17:30 -06:00
|
|
|
k.to_ascii_uppercase(),
|
|
|
|
v).as_slice());
|
2012-09-19 18:55:01 -05:00
|
|
|
}
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2014-05-12 19:56:43 -05:00
|
|
|
return buffer
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a map, search for the frequency of a pattern
|
2014-05-22 18:57:53 -05:00
|
|
|
fn find(mm: &HashMap<Vec<u8> , uint>, key: String) -> uint {
|
2014-12-25 18:17:30 -06:00
|
|
|
let key = key.into_ascii_lowercase();
|
2014-11-12 17:51:51 -06:00
|
|
|
match mm.get(key.as_bytes()) {
|
2014-11-28 10:57:41 -06:00
|
|
|
option::Option::None => { return 0u; }
|
|
|
|
option::Option::Some(&num) => { return num; }
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a map, increment the counter for a key
|
2014-03-05 16:02:44 -06:00
|
|
|
fn update_freq(mm: &mut HashMap<Vec<u8> , uint>, key: &[u8]) {
|
2014-10-15 01:05:01 -05:00
|
|
|
let key = key.to_vec();
|
2015-01-02 01:53:35 -06:00
|
|
|
let newval = match mm.remove(&key) {
|
2013-03-23 20:22:00 -05:00
|
|
|
Some(v) => v + 1,
|
|
|
|
None => 1
|
|
|
|
};
|
|
|
|
mm.insert(key, newval);
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 17:28:08 -06:00
|
|
|
// given a Vec<u8>, for each window call a function
|
2012-07-06 17:15:52 -05:00
|
|
|
// i.e., for "hello" and windows of size four,
|
|
|
|
// run it("hell") and it("ello"), then return "llo"
|
2015-01-05 07:23:55 -06:00
|
|
|
fn windows_with_carry<F>(bb: &[u8], nn: uint, mut it: F) -> Vec<u8> where
|
|
|
|
F: FnMut(&[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) {
|
2015-01-19 10:07:13 -06:00
|
|
|
it(&bb[ii..ii+nn]);
|
2012-07-06 17:15:52 -05:00
|
|
|
ii += 1u;
|
|
|
|
}
|
|
|
|
|
2015-01-19 10:07:13 -06:00
|
|
|
return bb[len - (nn - 1u)..len].to_vec();
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:19:58 -05:00
|
|
|
fn make_sequence_processor(sz: uint,
|
2014-03-05 16:02:44 -06:00
|
|
|
from_parent: &Receiver<Vec<u8>>,
|
2014-05-22 18:57:53 -05:00
|
|
|
to_parent: &Sender<String>) {
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut freqs: HashMap<Vec<u8>, uint> = HashMap::new();
|
|
|
|
let mut carry = Vec::new();
|
2012-07-06 17:15:52 -05:00
|
|
|
let mut total: uint = 0u;
|
|
|
|
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut line: Vec<u8>;
|
2012-07-06 17:15:52 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
|
2015-01-02 11:24:56 -06:00
|
|
|
line = from_parent.recv().unwrap();
|
2014-10-15 01:05:01 -05:00
|
|
|
if line == Vec::new() { break; }
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2014-10-15 01:05:01 -05:00
|
|
|
carry.push_all(line.as_slice());
|
|
|
|
carry = windows_with_carry(carry.as_slice(), sz, |window| {
|
|
|
|
update_freq(&mut freqs, window);
|
|
|
|
total += 1u;
|
|
|
|
});
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
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) }
|
2014-05-27 22:44:58 -05:00
|
|
|
3u => { format!("{}\t{}", find(&freqs, "GGT".to_string()), "GGT") }
|
|
|
|
4u => { format!("{}\t{}", find(&freqs, "GGTA".to_string()), "GGTA") }
|
|
|
|
6u => { format!("{}\t{}", find(&freqs, "GGTATT".to_string()), "GGTATT") }
|
|
|
|
12u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATT".to_string()), "GGTATTTTAATT") }
|
|
|
|
18u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATTTATAGT".to_string()),
|
2014-04-15 20:17:48 -05:00
|
|
|
"GGTATTTTAATTTATAGT") }
|
2014-05-25 05:17:19 -05:00
|
|
|
_ => { "".to_string() }
|
2012-07-06 17:15:52 -05:00
|
|
|
};
|
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
to_parent.send(buffer).unwrap();
|
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() {
|
2014-01-15 15:25:09 -06:00
|
|
|
use std::io::{stdio, MemReader, BufferedReader};
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-10-06 18:08:56 -05:00
|
|
|
let rdr = if os::getenv("RUST_BENCH").is_some() {
|
2014-12-21 15:57:09 -06:00
|
|
|
let foo = include_bytes!("shootout-k-nucleotide.data");
|
2014-10-15 01:05:01 -05:00
|
|
|
box MemReader::new(foo.to_vec()) as Box<Reader>
|
2013-10-06 18:08:56 -05:00
|
|
|
} else {
|
2014-05-05 20:56:44 -05:00
|
|
|
box stdio::stdin() as Box<Reader>
|
2013-10-06 18:08:56 -05:00
|
|
|
};
|
|
|
|
let mut rdr = BufferedReader::new(rdr);
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2013-06-29 00:05:50 -05:00
|
|
|
// initialize each sequence sorter
|
2014-03-05 16:02:44 -06:00
|
|
|
let sizes = vec!(1u,2,3,4,6,12,18);
|
2015-01-02 01:53:35 -06:00
|
|
|
let mut streams = range(0, sizes.len()).map(|_| {
|
|
|
|
Some(channel::<String>())
|
|
|
|
}).collect::<Vec<_>>();
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut from_child = Vec::new();
|
2014-09-14 22:27:36 -05:00
|
|
|
let to_child = sizes.iter().zip(streams.iter_mut()).map(|(sz, stream_ref)| {
|
2012-09-21 20:43:30 -05:00
|
|
|
let sz = *sz;
|
2014-01-31 14:35:36 -06:00
|
|
|
let stream = replace(stream_ref, None);
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_parent_, from_child_) = 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
|
|
|
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_child, from_parent) = channel();
|
2012-07-06 17:15:52 -05:00
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
Thread::spawn(move|| {
|
2013-04-26 16:04:39 -05:00
|
|
|
make_sequence_processor(sz, &from_parent, &to_parent_);
|
2015-01-05 23:59:45 -06:00
|
|
|
});
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
to_child
|
2014-03-05 16:02:44 -06:00
|
|
|
}).collect::<Vec<Sender<Vec<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;
|
|
|
|
|
2013-12-08 01:12:07 -06:00
|
|
|
for line in rdr.lines() {
|
2014-07-16 15:37:28 -05:00
|
|
|
let line = line.unwrap().as_slice().trim().to_string();
|
2013-10-17 23:08:48 -05:00
|
|
|
|
|
|
|
if line.len() == 0u { continue; }
|
|
|
|
|
2014-06-19 20:22:33 -05:00
|
|
|
match (line.as_bytes()[0] as char, proc_mode) {
|
2013-10-17 23:08:48 -05:00
|
|
|
|
|
|
|
// start processing if this is the one
|
|
|
|
('>', false) => {
|
2014-05-20 01:19:56 -05:00
|
|
|
match line.as_slice().slice_from(1).find_str("THREE") {
|
2015-01-07 20:53:58 -06:00
|
|
|
Some(_) => { proc_mode = true; }
|
|
|
|
None => { }
|
2013-10-17 23:08:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// break our processing
|
|
|
|
('>', true) => { break; }
|
|
|
|
|
|
|
|
// process the sequence for k-mers
|
|
|
|
(_, true) => {
|
|
|
|
let line_bytes = line.as_bytes();
|
|
|
|
|
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2014-10-15 01:05:01 -05:00
|
|
|
let lb = line_bytes.to_vec();
|
2015-01-02 01:53:35 -06:00
|
|
|
to_child[ii].send(lb).unwrap();
|
2013-10-17 23:08:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whatever
|
|
|
|
_ => { }
|
|
|
|
}
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// finish...
|
2013-10-17 23:08:48 -05:00
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2015-01-02 01:53:35 -06:00
|
|
|
to_child[ii].send(Vec::new()).unwrap();
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// now fetch and print result messages
|
2013-10-17 23:08:48 -05:00
|
|
|
for (ii, _sz) in sizes.iter().enumerate() {
|
2015-01-07 20:53:58 -06:00
|
|
|
println!("{:?}", from_child[ii].recv().unwrap());
|
2012-07-06 17:15:52 -05:00
|
|
|
}
|
|
|
|
}
|