use std::error::Error;
use std::process;
use clap::{Arg, Command};
use rustynetics::bigwig::BigWigFile;
use rustynetics::track_statistics::{bin_summary_statistics_from_string, BinSummaryStatistics};
fn query(
filename_in: &str,
chrom: &str,
from: usize,
to: usize,
bin_size: usize,
bin_overlap: usize,
summary: BinSummaryStatistics,
verbose: bool,
) -> Result<(), Box<dyn Error>> {
if verbose {
eprintln!("Opening bigWig file {}", filename_in);
}
let mut reader = BigWigFile::new_reader(filename_in).unwrap_or_else(|err| {
eprintln!("Error opening file: {}", err);
process::exit(1);
});
let result = reader.query_slice(chrom, from, to, summary, bin_size, bin_overlap, f64::NAN)?;
println!("{:?}", result.0);
Ok(())
}
fn main() {
let matches = Command::new("BigWig Query")
.version("1.0")
.about("Query BigWig files")
.arg(
Arg::new("input")
.help("The input BigWig file")
.required(true),
)
.arg(
Arg::new("chrom")
.help("The chromosome to query")
.required(true),
)
.arg(Arg::new("from").help("The start position").required(true))
.arg(Arg::new("to").help("The end position").required(true))
.arg(
Arg::new("binsize")
.help("The bin size for the query")
.required(true),
)
.arg(
Arg::new("bin-overlap")
.long("bin-overlap")
.default_value("0")
.help("Number of overlapping bins when computing the summary"),
)
.arg(
Arg::new("bin-summary")
.long("bin-summary")
.default_value("mean")
.help("Bin summary statistic [mean, max, min]"),
)
.arg(
Arg::new("verbose")
.short('v')
.long("verbose")
.action(clap::ArgAction::SetTrue)
.help("Be verbose"),
)
.get_matches();
let filename_in = matches.get_one::<String>("input").unwrap();
let chrom = matches.get_one::<String>("chrom").unwrap();
let from = matches
.get_one::<String>("from")
.unwrap()
.parse::<usize>()
.unwrap_or_else(|_| {
eprintln!("Invalid start position");
process::exit(1);
});
let to = matches
.get_one::<String>("to")
.unwrap()
.parse::<usize>()
.unwrap_or_else(|_| {
eprintln!("Invalid end position");
process::exit(1);
});
let bin_size = matches
.get_one::<String>("binsize")
.unwrap()
.parse::<usize>()
.unwrap_or_else(|_| {
eprintln!("Invalid bin size");
process::exit(1);
});
let bin_overlap = matches
.get_one::<String>("bin-overlap")
.unwrap()
.parse::<usize>()
.unwrap();
let summary_str = matches.get_one::<String>("bin-summary").unwrap();
let verbose = matches.get_flag("verbose");
let summary = bin_summary_statistics_from_string(summary_str).unwrap_or_else(|| {
eprintln!("Invalid bin summary statistic. Choose from: mean, max, min");
process::exit(1);
});
if let Err(e) = query(
filename_in,
chrom,
from,
to,
bin_size,
bin_overlap,
summary,
verbose,
) {
eprintln!("{}", e);
process::exit(1);
}
}