Skip to content

Instantly share code, notes, and snippets.

@Dr-Emann
Forked from samuell/gc_count.rs
Last active Aug 29, 2015
Embed
What would you like to do?
//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