Skip to content

Commit

Permalink
util/examples: Add async variant view example
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jul 23, 2024
1 parent e0e51b7 commit 782d892
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion noodles-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ futures = { workspace = true, optional = true, features = ["std"] }
tokio = { workspace = true, optional = true, features = ["fs", "io-util"] }

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, features = ["io-std", "macros", "rt-multi-thread"] }

[package.metadata.docs.rs]
all-features = true
Expand Down Expand Up @@ -83,3 +83,7 @@ required-features = ["variant"]
[[example]]
name = "util_variant_view"
required-features = ["variant"]

[[example]]
name = "util_variant_view_async"
required-features = ["async", "variant"]
40 changes: 40 additions & 0 deletions noodles-util/examples/util_variant_view_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Prints a variant file in the VCF format.
//!
//! The result matches the output of `bcftools view <src>`.
use std::env;

use futures::TryStreamExt;
use noodles_util::variant;
use noodles_vcf as vcf;
use tokio::io::{self, AsyncWriteExt};

#[tokio::main]
async fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");

let builder = variant::r#async::io::reader::Builder::default();

let mut reader = if src == "-" {
builder.build_from_reader(io::stdin()).await?
} else {
builder.build_from_path(src).await?
};

let header = reader.read_header().await?;

let mut writer = vcf::r#async::io::Writer::new(io::stdout());
writer.write_header(&header).await?;

let mut records = reader.records();

while let Some(record) = records.try_next().await? {
writer
.write_variant_record(&header, record.as_ref())
.await?;
}

writer.get_mut().shutdown().await?;

Ok(())
}

0 comments on commit 782d892

Please sign in to comment.