]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-k-nucleotide-pipes.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / test / bench / shootout-k-nucleotide-pipes.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-android: FIXME(#10393)
12 // ignore-pretty very bad with line comments
13
14 // multi tasking k-nucleotide
15
16 #![feature(slicing_syntax)]
17
18 use std::ascii::{AsciiExt, OwnedAsciiExt};
19 use std::cmp::Ordering::{self, Less, Greater, Equal};
20 use std::collections::HashMap;
21 use std::mem::replace;
22 use std::num::Float;
23 use std::option;
24 use std::os;
25 use std::sync::mpsc::{channel, Sender, Receiver};
26 use std::thread::Thread;
27
28 fn f64_cmp(x: f64, y: f64) -> Ordering {
29     // arbitrarily decide that NaNs are larger than everything.
30     if y.is_nan() {
31         Less
32     } else if x.is_nan() {
33         Greater
34     } else if x < y {
35         Less
36     } else if x == y {
37         Equal
38     } else {
39         Greater
40     }
41 }
42
43 // given a map, print a sorted version of it
44 fn sort_and_fmt(mm: &HashMap<Vec<u8> , uint>, total: uint) -> String {
45    fn pct(xx: uint, yy: uint) -> f64 {
46       return (xx as f64) * 100.0 / (yy as f64);
47    }
48
49    // sort by key, then by value
50    fn sortKV(mut orig: Vec<(Vec<u8> ,f64)> ) -> Vec<(Vec<u8> ,f64)> {
51         orig.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b));
52         orig.sort_by(|&(_, a), &(_, b)| f64_cmp(b, a));
53         orig
54    }
55
56    let mut pairs = Vec::new();
57
58    // map -> [(k,%)]
59    for (key, &val) in mm.iter() {
60       pairs.push(((*key).clone(), pct(val, total)));
61    }
62
63    let pairs_sorted = sortKV(pairs);
64
65    let mut buffer = String::new();
66    for &(ref k, v) in pairs_sorted.iter() {
67        buffer.push_str(format!("{} {:0.3}\n",
68                                k.to_ascii_uppercase(),
69                                v).as_slice());
70    }
71
72    return buffer
73 }
74
75 // given a map, search for the frequency of a pattern
76 fn find(mm: &HashMap<Vec<u8> , uint>, key: String) -> uint {
77    let key = key.into_ascii_lowercase();
78    match mm.get(key.as_bytes()) {
79       option::Option::None      => { return 0u; }
80       option::Option::Some(&num) => { return num; }
81    }
82 }
83
84 // given a map, increment the counter for a key
85 fn update_freq(mm: &mut HashMap<Vec<u8> , uint>, key: &[u8]) {
86     let key = key.to_vec();
87     let newval = match mm.remove(&key) {
88         Some(v) => v + 1,
89         None => 1
90     };
91     mm.insert(key, newval);
92 }
93
94 // given a Vec<u8>, for each window call a function
95 // i.e., for "hello" and windows of size four,
96 // run it("hell") and it("ello"), then return "llo"
97 fn windows_with_carry(bb: &[u8], nn: uint, it: |window: &[u8]|) -> Vec<u8> {
98    let mut ii = 0u;
99
100    let len = bb.len();
101    while ii < len - (nn - 1u) {
102       it(bb[ii..ii+nn]);
103       ii += 1u;
104    }
105
106    return bb[len - (nn - 1u)..len].to_vec();
107 }
108
109 fn make_sequence_processor(sz: uint,
110                            from_parent: &Receiver<Vec<u8>>,
111                            to_parent: &Sender<String>) {
112    let mut freqs: HashMap<Vec<u8>, uint> = HashMap::new();
113    let mut carry = Vec::new();
114    let mut total: uint = 0u;
115
116    let mut line: Vec<u8>;
117
118    loop {
119
120        line = from_parent.recv().unwrap();
121        if line == Vec::new() { break; }
122
123        carry.push_all(line.as_slice());
124        carry = windows_with_carry(carry.as_slice(), sz, |window| {
125            update_freq(&mut freqs, window);
126            total += 1u;
127        });
128    }
129
130    let buffer = match sz {
131        1u => { sort_and_fmt(&freqs, total) }
132        2u => { sort_and_fmt(&freqs, total) }
133        3u => { format!("{}\t{}", find(&freqs, "GGT".to_string()), "GGT") }
134        4u => { format!("{}\t{}", find(&freqs, "GGTA".to_string()), "GGTA") }
135        6u => { format!("{}\t{}", find(&freqs, "GGTATT".to_string()), "GGTATT") }
136       12u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATT".to_string()), "GGTATTTTAATT") }
137       18u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATTTATAGT".to_string()),
138                        "GGTATTTTAATTTATAGT") }
139         _ => { "".to_string() }
140    };
141
142     to_parent.send(buffer).unwrap();
143 }
144
145 // given a FASTA file on stdin, process sequence THREE
146 fn main() {
147     use std::io::{stdio, MemReader, BufferedReader};
148
149     let rdr = if os::getenv("RUST_BENCH").is_some() {
150         let foo = include_bytes!("shootout-k-nucleotide.data");
151         box MemReader::new(foo.to_vec()) as Box<Reader>
152     } else {
153         box stdio::stdin() as Box<Reader>
154     };
155     let mut rdr = BufferedReader::new(rdr);
156
157     // initialize each sequence sorter
158     let sizes = vec!(1u,2,3,4,6,12,18);
159     let mut streams = range(0, sizes.len()).map(|_| {
160         Some(channel::<String>())
161     }).collect::<Vec<_>>();
162     let mut from_child = Vec::new();
163     let to_child  = sizes.iter().zip(streams.iter_mut()).map(|(sz, stream_ref)| {
164         let sz = *sz;
165         let stream = replace(stream_ref, None);
166         let (to_parent_, from_child_) = stream.unwrap();
167
168         from_child.push(from_child_);
169
170         let (to_child, from_parent) = channel();
171
172         Thread::spawn(move|| {
173             make_sequence_processor(sz, &from_parent, &to_parent_);
174         }).detach();
175
176         to_child
177     }).collect::<Vec<Sender<Vec<u8> >> >();
178
179
180    // latch stores true after we've started
181    // reading the sequence of interest
182    let mut proc_mode = false;
183
184    for line in rdr.lines() {
185        let line = line.unwrap().as_slice().trim().to_string();
186
187        if line.len() == 0u { continue; }
188
189        match (line.as_bytes()[0] as char, proc_mode) {
190
191            // start processing if this is the one
192            ('>', false) => {
193                match line.as_slice().slice_from(1).find_str("THREE") {
194                    option::Option::Some(_) => { proc_mode = true; }
195                    option::Option::None    => { }
196                }
197            }
198
199            // break our processing
200            ('>', true) => { break; }
201
202            // process the sequence for k-mers
203            (_, true) => {
204                let line_bytes = line.as_bytes();
205
206                for (ii, _sz) in sizes.iter().enumerate() {
207                    let lb = line_bytes.to_vec();
208                    to_child[ii].send(lb).unwrap();
209                }
210            }
211
212            // whatever
213            _ => { }
214        }
215    }
216
217    // finish...
218    for (ii, _sz) in sizes.iter().enumerate() {
219        to_child[ii].send(Vec::new()).unwrap();
220    }
221
222    // now fetch and print result messages
223    for (ii, _sz) in sizes.iter().enumerate() {
224        println!("{}", from_child[ii].recv().unwrap());
225    }
226 }