diff --git a/noodles-sam/examples/sam_query_async.rs b/noodles-sam/examples/sam_query_async.rs index 57f608c0f..6310e341f 100644 --- a/noodles-sam/examples/sam_query_async.rs +++ b/noodles-sam/examples/sam_query_async.rs @@ -5,9 +5,9 @@ //! //! 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_bgzf as bgzf; use noodles_csi as csi; use noodles_sam as sam; @@ -16,6 +16,8 @@ use tokio::{ io::{self, AsyncWriteExt}, }; +const UNMAPPED: &str = "*"; + #[tokio::main] async fn main() -> Result<(), Box> { let mut args = env::args().skip(1); @@ -31,12 +33,18 @@ async fn main() -> Result<(), Box> { let header = reader.read_header().await?; let index = csi::r#async::read(src.with_extension("sam.csi")).await?; - let region = raw_region.parse()?; - 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?; }