Skip to content

Commit

Permalink
sam/examples/query_async: Handle querying unmapped region
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Sep 19, 2024
1 parent 32dd90a commit ec17185
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions noodles-sam/examples/sam_query_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//!
//! The result matches the output of `samtools view <src> <region>`.

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;
Expand All @@ -16,6 +16,8 @@ use tokio::{
io::{self, AsyncWriteExt},
};

const UNMAPPED: &str = "*";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args().skip(1);
Expand All @@ -31,12 +33,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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, &region)?;

let mut records: Pin<Box<dyn Stream<Item = io::Result<sam::Record>>>> =
if raw_region == UNMAPPED {
reader.query_unmapped(&index).await.map(Box::pin)?
} else {
let region = raw_region.parse()?;
reader.query(&header, &index, &region).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?;
}

Expand Down

0 comments on commit ec17185

Please sign in to comment.