-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.rs
48 lines (41 loc) · 1.25 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#![feature(core_intrinsics)]
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::intrinsics::unlikely;
const VALUE: [u64; 256] = {
let mut array = [0u64; 256];
array[b'A' as usize] = 1;
array[b'T' as usize] = 1;
array[b'G' as usize] = 1 << 32;
array[b'C' as usize] = 1 << 32;
array
};
const CHUNKSIZE: usize = 16;
fn main() {
let filename = "chry_multiplied.fa";
let file = File::open(filename).unwrap();
let mut reader = BufReader::new(file);
let mut totals = 0u64;
let mut line = Vec::with_capacity(100);
loop {
if unlikely(reader.read_until(b'\n', &mut line).expect("error reading") == 0) {
break;
}
if unlikely(line[0] == b'>') {
line.clear();
continue;
}
let mut chunker = line.chunks_exact(CHUNKSIZE);
while let Some(chars) = chunker.next() {
totals += chars.iter().map(|b| VALUE[*b as usize]).sum::<u64>();
}
for c in chunker.remainder() {
totals += VALUE[*c as usize];
}
line.clear();
}
let at = totals & 0xFFFFFFFF;
let gc = totals >> 32;
let gc_ratio: f64 = gc as f64 / (gc as f64 + at as f64);
println!("GC ratio (gc/(gc+at)): {}", gc_ratio);
}