Skip to content

Instantly share code, notes, and snippets.

@PoisonAlien
Last active June 11, 2019 13:57
Show Gist options
  • Save PoisonAlien/036fbb189ed5507dce4fb3231e18bc74 to your computer and use it in GitHub Desktop.
Save PoisonAlien/036fbb189ed5507dce4fb3231e18bc74 to your computer and use it in GitHub Desktop.
Parse output from bam-readcount
use std::io::{BufRead, BufReader};
use std::fs::File;
use std::env;
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2{
println!("Usage: parse_rc <foo.rc>");
process::exit(0);
}
let filename = args[1].clone();
let file = File::open(filename).expect("Unable to open the file");
let reader = BufReader::new(file);
println!("Ref_Allele\tA_count\tC_count\tG_count\tT_count\tt_vaf");
for line in reader.lines(){
let line = line.unwrap();
parse_line(line);
}
}
fn get_max(v: Vec<u16>) -> u16 {
let mut max_val = v[0];
for val in v{
if val > max_val{
max_val = val;
}
}
max_val
}
fn get_sum(v: &Vec<u16>) -> u16{
let mut sum = 0;
for val in v{
sum = sum + val;
}
sum
}
fn get_vaf(v: Vec<u16>, refbase: &str) -> f32 {
//v.sort();
let depth = get_sum(&v);
//println!("{}", depth);
let mut vaf = 0.0;
if depth == 0{
return vaf;
}
if refbase == "A"{
vaf = v[0] as f32 / depth as f32;
}else if refbase == "C"{
//println!("{}", v[1] as f32 / depth as f32);
vaf = v[1] as f32 / depth as f32;
}else if refbase == "G"{
vaf = v[2] as f32 / depth as f32;
}else if refbase == "T"{
vaf = v[3] as f32 / depth as f32;
}
return vaf
}
fn parse_line(l: String){
let line_spl: Vec<&str> = l.split('\t').collect();
let ref_allele: String = line_spl[2].parse().unwrap();
let depth: u16 = line_spl[3].parse().unwrap();;
let a_depth: Vec<&str> = line_spl[5].split(':').collect();
let a_depth: u16 = a_depth[1].parse().unwrap();
let c_depth: Vec<&str> = line_spl[6].split(':').collect();
let c_depth: u16 = c_depth[1].parse().unwrap();
let g_depth: Vec<&str> = line_spl[7].split(':').collect();
let g_depth: u16 = g_depth[1].parse().unwrap();
let t_depth: Vec<&str> = line_spl[8].split(':').collect();
let t_depth: u16 = t_depth[1].parse().unwrap();
let vec_depth: Vec<u16> = vec![a_depth, c_depth, g_depth, t_depth];
//let max_val = get_max(vec_depth);
let vaf = get_vaf(vec_depth, &ref_allele);
println!("{}\t{}\t{}\t{}\t{}\t{}", &ref_allele, &a_depth, &c_depth, &g_depth, &t_depth, 1.00-vaf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment