diff --git a/noodles-bam/examples/bam_query.rs b/noodles-bam/examples/bam_query.rs index a652d2877..e26c5a445 100644 --- a/noodles-bam/examples/bam_query.rs +++ b/noodles-bam/examples/bam_query.rs @@ -7,24 +7,31 @@ use std::{env, io, path::PathBuf}; use noodles_bam as bam; -use noodles_sam as sam; +use noodles_sam::{self as sam, alignment::Record}; use sam::AlignmentWriter; +const UNMAPPED: &str = "*"; + fn main() -> Result<(), Box> { let mut args = env::args(); let src = args.nth(1).map(PathBuf::from).expect("missing src"); - let region = args.next().expect("missing region").parse()?; + let raw_region = args.next().expect("missing region"); let mut reader = bam::indexed_reader::Builder::default().build_from_path(src)?; let header = reader.read_header()?; - let query = reader.query(&header, ®ion)?; + let records: Box>> = if raw_region == UNMAPPED { + reader.query_unmapped(&header).map(Box::new)? + } else { + let region = raw_region.parse()?; + reader.query(&header, ®ion).map(Box::new)? + }; let stdout = io::stdout().lock(); let mut writer = sam::Writer::new(stdout); - for result in query { + for result in records { let record = result?; writer.write_alignment_record(&header, &record)?; }