use std::collections::HashMap;
use std::io::BufRead;
use std::path::PathBuf;
pub type BedRegions = HashMap<String, Vec<(u32, u32)>>;
pub fn parse_bed(path: &PathBuf) -> Result<BedRegions, Box<dyn std::error::Error>> {
let mut regions: BedRegions = HashMap::new();
let file = std::fs::File::open(path)?;
let reader = std::io::BufReader::new(file);
for line in reader.lines() {
let line = line?;
if line.starts_with('#') || line.trim().is_empty() {
continue;
}
let fields: Vec<_> = line.split_whitespace().collect();
if fields.len() < 3 {
continue;
}
let chrom = fields[0].to_string();
let start: u32 = fields[1].parse()?;
let end: u32 = fields[2].parse()?;
if end <= start {
continue;
}
let start = start.saturating_add(1);
regions.entry(chrom).or_default().push((start, end));
}
Ok(regions)
}