]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-k-nucleotide-pipes.rs
auto merge of #15530 : adrientetar/rust/proper-fonts, r=alexcrichton
[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 extern crate collections;
17
18 use std::collections::HashMap;
19 use std::mem::replace;
20 use std::option;
21 use std::os;
22 use std::string::String;
23
24 fn f64_cmp(x: f64, y: f64) -> Ordering {
25     // arbitrarily decide that NaNs are larger than everything.
26     if y.is_nan() {
27         Less
28     } else if x.is_nan() {
29         Greater
30     } else if x < y {
31         Less
32     } else if x == y {
33         Equal
34     } else {
35         Greater
36     }
37 }
38
39 // given a map, print a sorted version of it
40 fn sort_and_fmt(mm: &HashMap<Vec<u8> , uint>, total: uint) -> String {
41    fn pct(xx: uint, yy: uint) -> f64 {
42       return (xx as f64) * 100.0 / (yy as f64);
43    }
44
45    // sort by key, then by value
46    fn sortKV(mut orig: Vec<(Vec<u8> ,f64)> ) -> Vec<(Vec<u8> ,f64)> {
47         orig.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b));
48         orig.sort_by(|&(_, a), &(_, b)| f64_cmp(b, a));
49         orig
50    }
51
52    let mut pairs = Vec::new();
53
54    // map -> [(k,%)]
55    for (key, &val) in mm.iter() {
56       pairs.push(((*key).clone(), pct(val, total)));
57    }
58
59    let pairs_sorted = sortKV(pairs);
60
61    let mut buffer = String::new();
62    for &(ref k, v) in pairs_sorted.iter() {
63        buffer.push_str(format!("{} {:0.3f}\n",
64                                k.as_slice()
65                                .to_ascii()
66                                .to_upper()
67                                .into_string(), v).as_slice());
68    }
69
70    return buffer
71 }
72
73 // given a map, search for the frequency of a pattern
74 fn find(mm: &HashMap<Vec<u8> , uint>, key: String) -> uint {
75    let key = key.to_owned().into_ascii().as_slice().to_lower().into_string();
76    match mm.find_equiv(&key.as_bytes()) {
77       option::None      => { return 0u; }
78       option::Some(&num) => { return num; }
79    }
80 }
81
82 // given a map, increment the counter for a key
83 fn update_freq(mm: &mut HashMap<Vec<u8> , uint>, key: &[u8]) {
84     let key = Vec::from_slice(key);
85     let newval = match mm.pop(&key) {
86         Some(v) => v + 1,
87         None => 1
88     };
89     mm.insert(key, newval);
90 }
91
92 // given a Vec<u8>, for each window call a function
93 // i.e., for "hello" and windows of size four,
94 // run it("hell") and it("ello"), then return "llo"
95 fn windows_with_carry(bb: &[u8], nn: uint, it: |window: &[u8]|) -> Vec<u8> {
96    let mut ii = 0u;
97
98    let len = bb.len();
99    while ii < len - (nn - 1u) {
100       it(bb.slice(ii, ii+nn));
101       ii += 1u;
102    }
103
104    return Vec::from_slice(bb.slice(len - (nn - 1u), len));
105 }
106
107 fn make_sequence_processor(sz: uint,
108                            from_parent: &Receiver<Vec<u8>>,
109                            to_parent: &Sender<String>) {
110    let mut freqs: HashMap<Vec<u8>, uint> = HashMap::new();
111    let mut carry = Vec::new();
112    let mut total: uint = 0u;
113
114    let mut line: Vec<u8>;
115
116    loop {
117
118       line = from_parent.recv();
119       if line == Vec::new() { break; }
120
121        carry = windows_with_carry(carry.append(line.as_slice()).as_slice(),
122                                   sz,
123                                   |window| {
124          update_freq(&mut freqs, window);
125          total += 1u;
126       });
127    }
128
129    let buffer = match sz {
130        1u => { sort_and_fmt(&freqs, total) }
131        2u => { sort_and_fmt(&freqs, total) }
132        3u => { format!("{}\t{}", find(&freqs, "GGT".to_string()), "GGT") }
133        4u => { format!("{}\t{}", find(&freqs, "GGTA".to_string()), "GGTA") }
134        6u => { format!("{}\t{}", find(&freqs, "GGTATT".to_string()), "GGTATT") }
135       12u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATT".to_string()), "GGTATTTTAATT") }
136       18u => { format!("{}\t{}", find(&freqs, "GGTATTTTAATTTATAGT".to_string()),
137                        "GGTATTTTAATTTATAGT") }
138         _ => { "".to_string() }
139    };
140
141     to_parent.send(buffer);
142 }
143
144 // given a FASTA file on stdin, process sequence THREE
145 fn main() {
146     use std::io::{stdio, MemReader, BufferedReader};
147
148     let rdr = if os::getenv("RUST_BENCH").is_some() {
149         let foo = include_bin!("shootout-k-nucleotide.data");
150         box MemReader::new(Vec::from_slice(foo)) as Box<Reader>
151     } else {
152         box stdio::stdin() as Box<Reader>
153     };
154     let mut rdr = BufferedReader::new(rdr);
155
156     // initialize each sequence sorter
157     let sizes = vec!(1u,2,3,4,6,12,18);
158     let mut streams = Vec::from_fn(sizes.len(), |_| Some(channel::<String>()));
159     let mut from_child = Vec::new();
160     let to_child  = sizes.iter().zip(streams.mut_iter()).map(|(sz, stream_ref)| {
161         let sz = *sz;
162         let stream = replace(stream_ref, None);
163         let (to_parent_, from_child_) = stream.unwrap();
164
165         from_child.push(from_child_);
166
167         let (to_child, from_parent) = channel();
168
169         spawn(proc() {
170             make_sequence_processor(sz, &from_parent, &to_parent_);
171         });
172
173         to_child
174     }).collect::<Vec<Sender<Vec<u8> >> >();
175
176
177    // latch stores true after we've started
178    // reading the sequence of interest
179    let mut proc_mode = false;
180
181    for line in rdr.lines() {
182        let line = line.unwrap().as_slice().trim().to_owned();
183
184        if line.len() == 0u { continue; }
185
186        match (line.as_bytes()[0] as char, proc_mode) {
187
188            // start processing if this is the one
189            ('>', false) => {
190                match line.as_slice().slice_from(1).find_str("THREE") {
191                    option::Some(_) => { proc_mode = true; }
192                    option::None    => { }
193                }
194            }
195
196            // break our processing
197            ('>', true) => { break; }
198
199            // process the sequence for k-mers
200            (_, true) => {
201                let line_bytes = line.as_bytes();
202
203                for (ii, _sz) in sizes.iter().enumerate() {
204                    let lb = Vec::from_slice(line_bytes);
205                    to_child.get(ii).send(lb);
206                }
207            }
208
209            // whatever
210            _ => { }
211        }
212    }
213
214    // finish...
215    for (ii, _sz) in sizes.iter().enumerate() {
216        to_child.get(ii).send(Vec::new());
217    }
218
219    // now fetch and print result messages
220    for (ii, _sz) in sizes.iter().enumerate() {
221        println!("{}", from_child.get(ii).recv());
222    }
223 }