]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-reverse-complement.rs
auto merge of #14186 : omasanori/rust/suppress-warnings, r=alexcrichton
[rust.git] / src / test / bench / shootout-reverse-complement.rs
1 // Copyright 2013-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-pretty very bad with line comments
12 // ignore-android doesn't terminate?
13
14 use std::iter::range_step;
15 use std::io::{stdin, stdout, File};
16
17 static LINE_LEN: uint = 60;
18
19 fn make_complements() -> [u8, ..256] {
20     let transforms = [
21         ('A', 'T'), ('C', 'G'), ('G', 'C'), ('T', 'A'),
22         ('U', 'A'), ('M', 'K'), ('R', 'Y'), ('W', 'W'),
23         ('S', 'S'), ('Y', 'R'), ('K', 'M'), ('V', 'B'),
24         ('H', 'D'), ('D', 'H'), ('B', 'V'), ('N', 'N'),
25         ('\n', '\n')];
26     let mut complements: [u8, ..256] = [0, ..256];
27     for (i, c) in complements.mut_iter().enumerate() {
28         *c = i as u8;
29     }
30     let lower = 'A' as u8 - 'a' as u8;
31     for &(from, to) in transforms.iter() {
32         complements[from as uint] = to as u8;
33         complements[(from as u8 - lower) as uint] = to as u8;
34     }
35     complements
36 }
37
38 fn main() {
39     let complements = make_complements();
40     let data = if std::os::getenv("RUST_BENCH").is_some() {
41         File::open(&Path::new("shootout-k-nucleotide.data")).read_to_end()
42     } else {
43         stdin().read_to_end()
44     };
45     let mut data = data.unwrap();
46
47     for seq in data.as_mut_slice().mut_split(|c| *c == '>' as u8) {
48         // skip header and last \n
49         let begin = match seq.iter().position(|c| *c == '\n' as u8) {
50             None => continue,
51             Some(c) => c
52         };
53         let len = seq.len();
54         let seq = seq.mut_slice(begin + 1, len - 1);
55
56         // arrange line breaks
57         let len = seq.len();
58         let off = LINE_LEN - len % (LINE_LEN + 1);
59         for i in range_step(LINE_LEN, len, LINE_LEN + 1) {
60             for j in std::iter::count(i, -1).take(off) {
61                 seq[j] = seq[j - 1];
62             }
63             seq[i - off] = '\n' as u8;
64         }
65
66         // reverse complement, as
67         //    seq.reverse(); for c in seq.mut_iter() {*c = complements[*c]}
68         // but faster:
69         let mut it = seq.mut_iter();
70         loop {
71             match (it.next(), it.next_back()) {
72                 (Some(front), Some(back)) => {
73                     let tmp = complements[*front as uint];
74                     *front = complements[*back as uint];
75                     *back = tmp;
76                 }
77                 (Some(last), None) => *last = complements[*last as uint], // last element
78                 _ => break // vector exhausted.
79             }
80         }
81     }
82
83     stdout().write(data.as_slice()).unwrap();
84 }