From 48c0b099df6dc27838beeafbc131df578408fd4f Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Mon, 16 Sep 2024 13:06:55 -0500 Subject: [PATCH] bam/examples/query_async: Handle querying unmapped region --- noodles-bam/examples/bam_query_async.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/noodles-bam/examples/bam_query_async.rs b/noodles-bam/examples/bam_query_async.rs index fcb1f7fee..dd6718386 100644 --- a/noodles-bam/examples/bam_query_async.rs +++ b/noodles-bam/examples/bam_query_async.rs @@ -4,29 +4,38 @@ //! //! The result matches the output of `samtools view `. -use std::{env, path::PathBuf}; +use std::{env, path::PathBuf, pin::Pin}; -use futures::TryStreamExt; +use futures::{Stream, TryStreamExt}; use noodles_bam::{self as bam, bai}; use noodles_sam as sam; use tokio::{fs::File, io}; +const UNMAPPED: &str = "*"; + #[tokio::main] async fn main() -> Result<(), Box> { - let mut args = env::args(); + let mut args = env::args().skip(1); - let src = args.nth(1).map(PathBuf::from).expect("missing src"); - let region = args.next().expect("missing region").parse()?; + let src = args.next().map(PathBuf::from).expect("missing src"); + let raw_region = args.next().expect("missing region"); let mut reader = File::open(&src).await.map(bam::r#async::io::Reader::new)?; - let header = reader.read_header().await?; + let index = bai::r#async::read(src.with_extension("bam.bai")).await?; - let mut query = reader.query(&header, &index, ®ion)?; + + let mut records: Pin>>> = + if raw_region == UNMAPPED { + reader.query_unmapped(&index).await.map(Box::pin)? + } else { + let region = raw_region.parse()?; + reader.query(&header, &index, ®ion).map(Box::pin)? + }; let mut writer = sam::r#async::io::Writer::new(io::stdout()); - while let Some(record) = query.try_next().await? { + while let Some(record) = records.try_next().await? { writer.write_alignment_record(&header, &record).await?; }