-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sam/examples: Add async query example
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Queries a bgzipped SAM file with a given region. | ||
//! | ||
//! The input bgzipped SAM file must have an associated coordinate-sorted index (CSI) in the same | ||
//! directory. | ||
//! | ||
//! The result matches the output of `samtools view <src> <region>`. | ||
use std::{env, path::PathBuf}; | ||
|
||
use futures::TryStreamExt; | ||
use noodles_bgzf as bgzf; | ||
use noodles_csi as csi; | ||
use noodles_sam as sam; | ||
use tokio::{ | ||
fs::File, | ||
io::{self, AsyncWriteExt}, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut args = env::args().skip(1); | ||
|
||
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(bgzf::AsyncReader::new) | ||
.map(sam::r#async::io::Reader::new)?; | ||
|
||
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 writer = sam::r#async::io::Writer::new(io::stdout()); | ||
|
||
while let Some(record) = query.try_next().await? { | ||
writer.write_alignment_record(&header, &record).await?; | ||
} | ||
|
||
writer.get_mut().shutdown().await?; | ||
|
||
Ok(()) | ||
} |