extern crate getopts;
extern crate fasten;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use fasten::fasten_base_options;
use fasten::fasten_base_options_matches;
fn main(){
let mut opts = fasten_base_options();
opts.optopt("","id","Progress identifier. Default: unnamed","STRING");
opts.optopt("","update-every","Update progress every n reads.","INT");
opts.optflag("p","print","Print the reads back to stdout");
let matches = fasten_base_options_matches("Prints a progress meter for number of fastq entries", opts);
let print_reads:bool = matches.opt_present("print");
let progress_id:String = {
if matches.opt_present("id") {
matches.opt_str("id")
.expect("ERROR parsing --id")
} else {
String::from("unnamed")
}
};
let my_file = File::open("/dev/stdin").expect("Could not open file");
let my_buffer=BufReader::new(my_file);
let lines_per_read :usize = 4;
let update_every :usize = {
if matches.opt_present("update-every") {
let tmp :usize =
matches.opt_str("update-every")
.expect("ERROR parsing update-every")
.parse()
.expect("ERROR parsing update-every as INT");
tmp * lines_per_read
} else {
100
}
};
let mut line_counter = 0;
eprint!("\r{} progress: {}", progress_id, line_counter/lines_per_read);
for res in my_buffer.lines() {
let line=res.expect("ERROR: did not get a line");
if print_reads {
println!("{}", line);
}
line_counter += 1;
match line_counter % update_every {
0=>{
eprint!("\r{} progress: {}", progress_id, line_counter/lines_per_read);
}
_=>{
}
}
}
eprint!("\n");
let msg = format!("{}: Finished progress on {} reads", progress_id, line_counter);
fasten::logmsg(&msg);
}