extern crate fasten;
extern crate getopts;
extern crate rand;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use rand::prelude::*;
use fasten::fasten_base_options;
use fasten::fasten_base_options_matches;
fn main(){
let mut opts = fasten_base_options();
opts.optopt("f","frequency","Frequency of sequences to print, 0 to 1. Default: 1","FLOAT");
let matches = fasten_base_options_matches("Downsample your reads", opts);
let frequency :f32 = {
if matches.opt_present("frequency") {
matches.opt_str("frequency")
.expect("ERROR parsing frequency parameter")
.parse()
.expect("ERROR parsing frequency as a float")
} else {
1.0
}
};
let lines_per_read={
if matches.opt_present("paired-end") {
8
}else{
4
}
};
let my_file = File::open("/dev/stdin").expect("Could not open file");
let my_buffer=BufReader::new(my_file);
let mut line_counter =0;
let mut entry = String::new();
let mut randoms :Vec<f32> = vec![];
for line in my_buffer.lines() {
let line=line.expect("ERROR: did not get a line");
line_counter+=1;
entry.push_str(&line);
entry.push_str("\n");
if line_counter % lines_per_read == 0 {
if randoms.len() < 1 {
randoms = rand_32();
}
let r:f32 = randoms.pop().unwrap();
if r < frequency {
print!("{}",entry);
}
entry = String::new();
}
}
}
fn rand_32() -> Vec<f32> {
let mut rng = rand::thread_rng();
let floats :Vec<f32> = (0..10000)
.map(|_| rng.gen::<f32>())
.collect();
return floats;
}