Skip to content

Instantly share code, notes, and snippets.

@Dr-Emann
Forked from samuell/gc_count.rs
Last active August 29, 2015 14:04
Show Gist options
  • Save Dr-Emann/2e54278663f5d137a518 to your computer and use it in GitHub Desktop.
Save Dr-Emann/2e54278663f5d137a518 to your computer and use it in GitHub Desktop.
//based off https://gist.github.com/samuell/5555803
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::env;
fn main() {
let mut file = BufReader::new(File::open("Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa"));
let content = gc_content(&mut file);
println!("{:.2}", content * 100.0);
}
fn gc_content<R: BufRead>(reader: &mut R) -> f32 {
let mut gc_count = 0u32;
let mut at_count = 0u32;
let mut line = Vec::with_capacity(1000);
loop {
reader.read_until(b'\n', &mut line).unwrap();
if line.len() == 0 { break; }
if line[0] == b'>' { line.clear(); continue; }
for &byte in &line {
match byte {
b'G' | b'C' => gc_count += 1,
b'A' | b'T' => at_count += 1,
_ => {},
}
}
line.clear();
}
gc_count as f32 / (gc_count as f32 + at_count as f32)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment