Skip to content

Commit

Permalink
back to working example
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Feb 3, 2025
1 parent 4d29473 commit d10add8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
22 changes: 14 additions & 8 deletions examples/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ struct Args {
fn main() -> io::Result<()> {
let args = Args::parse();

let a = bedder::sniff::HtsFile::new(&args.a, "r").expect("Failed to open file");

let ai = a.into();
let a = BufReader::new(fs::File::open(&args.a)?);
let ai = bedder::sniff::open(a, &args.a)?;

//let ai = sniff::open_file(&args.a)?;
let bis = args
.b
.iter()
.map(|b| {
bedder::sniff::HtsFile::new(b, "r")
.expect("Failed to open file")
.into()
let f = BufReader::new(fs::File::open(b)?);
let b = bedder::sniff::open(f, b)?;
Ok(b.into_positioned_iterator())
})
.collect::<Vec<Box<dyn bedder::position::PositionedIterator>>>();
.collect::<io::Result<Vec<Box<dyn bedder::position::PositionedIterator>>>>()?;

// bedder always requires a hashmap that indicates the chromosome order

Expand All @@ -94,6 +93,11 @@ fn main() -> io::Result<()> {
}
};

let ai = match ai {
bedder::sniff::BedderReader::BedderBed(rdr) => rdr,
_ => unimplemented!(),
};

// we can have any number of b (other_iterators).
let it = IntersectionIterator::new(ai, bis, &h)?;

Expand Down Expand Up @@ -148,7 +152,9 @@ fn main() -> io::Result<()> {
}
//eprintln!("report: {:?}", report);
//eprintln!("args: {:?}", &args);
let columns = ["chrom", "start", "stop", "a_id", "b_id", "a_count", "b_count"];
let columns = [
"chrom", "start", "stop", "a_id", "b_id", "a_count", "b_count",
];
//let c = ColumnReporter::new();
let v = vec![];

Expand Down
40 changes: 38 additions & 2 deletions src/sniff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::bedder_bed::BedderBed;
use crate::position::PositionedIterator;
use flate2::bufread::GzDecoder;
use std::io;
use std::io::Read;
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;

#[derive(Debug)]
pub enum FileType {
Expand All @@ -17,7 +20,40 @@ pub enum Compression {
None,
}

pub enum BedderReader<R>
where
R: io::BufRead + io::Seek + 'static,
{
BedderBed(BedderBed<'static, R>),
}

impl<R> BedderReader<R>
where
R: io::BufRead + io::Seek + 'static,
{
pub fn new<P: AsRef<Path>>(reader: R, path: P) -> io::Result<Self> {
open(reader, path)
}

pub fn into_positioned_iterator(self) -> Box<dyn PositionedIterator> {
match self {
BedderReader::BedderBed(rdr) => Box::new(rdr),
}
}
}
// TODO: https://github.com/quinlan-lab/bedder-rs/blob/ffddd2b3a2075594a5375fb81b8672f4f5039acf/src/sniff.rs
pub fn open<P: AsRef<Path>, R: io::BufRead + io::Seek + 'static>(
mut reader: R,
p: P,
) -> io::Result<BedderReader<R>> {
let (ft, c) =
sniff(&mut reader).map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
let rdr = match ft {
FileType::Bed => BedderReader::BedderBed(BedderBed::new(reader, Some(p))),
_ => unimplemented!(),
};
Ok(rdr)
}

pub fn sniff<R: io::BufRead>(
rdr: &mut R,
Expand Down

0 comments on commit d10add8

Please sign in to comment.