-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite of shootout-reverse-complement.rs
This version is inspired by the best version in C by Mr Ledrug, but without the parallelisation.
- Loading branch information
Showing
1 changed file
with
70 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,79 @@ | ||
// xfail-pretty | ||
// xfail-test | ||
|
||
use std::cast::transmute; | ||
use std::libc::{STDOUT_FILENO, c_int, fdopen, fgets, fopen, fputc, fwrite}; | ||
use std::libc::{size_t}; | ||
use std::ptr::null; | ||
|
||
static LINE_LEN: u32 = 80; | ||
|
||
static COMPLEMENTS: [u8, ..256] = [ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
|
||
0, | ||
'T' as u8, | ||
'V' as u8, | ||
'G' as u8, | ||
'H' as u8, | ||
0, | ||
0, | ||
'C' as u8, | ||
'D' as u8, | ||
0, | ||
0, | ||
'M' as u8, | ||
0, | ||
'K' as u8, | ||
'N' as u8, | ||
0, | ||
0, | ||
0, | ||
'Y' as u8, | ||
'S' as u8, | ||
'A' as u8, | ||
'A' as u8, | ||
'B' as u8, | ||
'W' as u8, | ||
0, | ||
'R' as u8, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
|
||
0, | ||
'T' as u8, | ||
'V' as u8, | ||
'G' as u8, | ||
'H' as u8, | ||
0, | ||
0, | ||
'C' as u8, | ||
'D' as u8, | ||
0, | ||
0, | ||
'M' as u8, | ||
0, | ||
'K' as u8, | ||
'N' as u8, | ||
0, | ||
0, | ||
0, | ||
'Y' as u8, | ||
'S' as u8, | ||
'A' as u8, | ||
'A' as u8, | ||
'B' as u8, | ||
'W' as u8, | ||
0, | ||
'R' as u8, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
|
||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
]; | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// 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. | ||
|
||
use std::iter::range_step; | ||
use std::io::{stdin, stdout, File}; | ||
|
||
static LINE_LEN: uint = 60; | ||
|
||
fn make_complements() -> [u8, ..256] { | ||
let transforms = [ | ||
('A', 'T'), ('C', 'G'), ('G', 'C'), ('T', 'A'), | ||
('U', 'A'), ('M', 'K'), ('R', 'Y'), ('W', 'W'), | ||
('S', 'S'), ('Y', 'R'), ('K', 'M'), ('V', 'B'), | ||
('H', 'D'), ('D', 'H'), ('B', 'V'), ('N', 'N'), | ||
('\n', '\n')]; | ||
let mut complements: [u8, ..256] = [0, ..256]; | ||
for (i, c) in complements.mut_iter().enumerate() { | ||
*c = i as u8; | ||
} | ||
let lower = 'A' as u8 - 'a' as u8; | ||
for &(from, to) in transforms.iter() { | ||
complements[from as u8] = to as u8; | ||
complements[from as u8 - lower] = to as u8; | ||
} | ||
complements | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let mode = "r"; | ||
//let stdin = fdopen(STDIN_FILENO as c_int, transmute(&mode[0])); | ||
let path = "reversecomplement-input.txt"; | ||
let stdin = fopen(transmute(&path[0]), transmute(&mode[0])); | ||
let mode = "w"; | ||
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0])); | ||
|
||
let mut out: ~[u8] = ~[]; | ||
out.reserve(12777888); | ||
let mut pos = 0; | ||
|
||
loop { | ||
let needed = pos + (LINE_LEN as uint) + 1; | ||
if out.capacity() < needed { | ||
out.reserve_at_least(needed); | ||
} | ||
|
||
let mut ptr = out.unsafe_mut_ref(pos); | ||
if fgets(transmute(ptr), LINE_LEN as c_int, stdin) == null() { | ||
break; | ||
} | ||
|
||
// Don't change lines that begin with '>' or ';'. | ||
let first = *ptr; | ||
if first == ('>' as u8) { | ||
while *ptr != 0 { | ||
ptr = ptr.offset(1); | ||
} | ||
*ptr = '\n' as u8; | ||
|
||
pos = (ptr as uint) - (out.unsafe_ref(0) as uint); | ||
fwrite(transmute(out.unsafe_ref(0)), | ||
1, | ||
pos as size_t, | ||
stdout); | ||
|
||
pos = 0; | ||
continue; | ||
let complements = make_complements(); | ||
let mut data = if std::os::getenv("RUST_BENCH").is_some() { | ||
File::open(&Path::init("shootout-k-nucleotide.data")).read_to_end() | ||
} else { | ||
stdin().read_to_end() | ||
}; | ||
|
||
for seq in data.mut_split(|c| *c == '>' as u8) { | ||
// skip header and last \n | ||
let begin = match seq.iter().position(|c| *c == '\n' as u8) { | ||
None => continue, | ||
Some(c) => c | ||
}; | ||
let len = seq.len(); | ||
let seq = seq.mut_slice(begin + 1, len - 1); | ||
|
||
// arrange line breaks | ||
let len = seq.len(); | ||
let off = LINE_LEN - len % (LINE_LEN + 1); | ||
for i in range_step(LINE_LEN, len, LINE_LEN + 1) { | ||
for j in std::iter::count(i, -1).take(off) { | ||
seq[j] = seq[j - 1]; | ||
} | ||
seq[i - off] = '\n' as u8; | ||
} | ||
|
||
// Complement other lines. | ||
loop { | ||
let ch = *ptr; | ||
if ch == 0 { | ||
break; | ||
// reverse complement, as | ||
// seq.reverse(); for c in seq.mut_iter() {*c = complements[*c]} | ||
// but faster: | ||
let mut it = seq.mut_iter(); | ||
loop { | ||
match (it.next(), it.next_back()) { | ||
(Some(front), Some(back)) => { | ||
let tmp = complements[*front]; | ||
*front = complements[*back]; | ||
*back = tmp; | ||
} | ||
*ptr = COMPLEMENTS.unsafe_get(ch as uint); | ||
ptr = ptr.offset(1); | ||
_ => break // vector exhausted. | ||
} | ||
*ptr = '\n' as u8; | ||
|
||
pos = (ptr as uint) - (out.unsafe_ref(0) as uint); | ||
} | ||
|
||
fwrite(transmute(out.unsafe_ref(0)), 1, pos as size_t, stdout); | ||
} | ||
|
||
stdout().write(data); | ||
} |
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from alexcrichton
at TeXitoi@e76e83c
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging TeXitoi/rust/shootout-reverse-complement-resurected = e76e83c into auto
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TeXitoi/rust/shootout-reverse-complement-resurected = e76e83c merged ok, testing candidate = d18d1147
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some tests failed:
failure: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/2805
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/2801
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-c/builds/646
exception: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-t/builds/646
exception: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/1911
exception: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/1910
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/2817
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/1909
exception: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/1910
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/2819
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/1909
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/1909
exception: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android/builds/1985
exception: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/2806
exception: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/1910
exception: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/1912
exception: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/2581
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from alexcrichton
at TeXitoi@e76e83c
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging TeXitoi/rust/shootout-reverse-complement-resurected = e76e83c into auto
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TeXitoi/rust/shootout-reverse-complement-resurected = e76e83c merged ok, testing candidate = bd5305f
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/2806
success: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-c/builds/651
success: http://buildbot.rust-lang.org/builders/auto-mac-32-nopt-t/builds/651
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/2810
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/1916
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/1915
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/2822
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/1914
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/1915
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/2824
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/1914
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/1914
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android/builds/1990
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/2811
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/1915
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/1917
success: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/2586
e76e83c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = bd5305f