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.
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.
11 // ignore-pretty very bad with line comments
12 // ignore-android doesn't terminate?
14 use std::iter::range_step;
15 use std::io::{stdin, stdout, File};
17 static LINE_LEN: uint = 60;
19 fn make_complements() -> [u8, ..256] {
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'),
26 let mut complements: [u8, ..256] = [0, ..256];
27 for (i, c) in complements.mut_iter().enumerate() {
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;
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()
45 let mut data = data.unwrap();
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) {
54 let seq = seq.mut_slice(begin + 1, len - 1);
56 // arrange line breaks
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) {
63 seq[i - off] = '\n' as u8;
66 // reverse complement, as
67 // seq.reverse(); for c in seq.mut_iter() {*c = complements[*c]}
69 let mut it = seq.mut_iter();
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];
77 (Some(last), None) => *last = complements[*last as uint], // last element
78 _ => break // vector exhausted.
83 stdout().write(data.as_slice()).unwrap();