Skip to main content

blast_example/
blast_parse.rs

1use anyhow::Result;
2use clap::Parser;
3use microBioRust::blast::*;
4use tokio::io::AsyncWriteExt;
5
6#[derive(Parser, Debug)]
7#[command(
8    name = "blast-parsers",
9    author,
10    version,
11    about = "async microBioRust BLAST parsers: for outfmt6 (single line tabular) and outfmt5 (xml)"
12)]
13struct Cli {
14    ///Use .gz for gzip-compressed files.
15    #[arg(short, long, default_value = "-")]
16    input: String,
17    /// Format: '6' (tabular) or '5' (xml). If omitted we try to infer by file suffix only
18    #[arg(short, long)]
19    format: Option<String>,
20    /// Output newline-delimited JSON (one JSON object per record/iteration)
21    #[arg(long)]
22    json: bool,
23}
24
25#[tokio::main]
26async fn main() -> Result<()> {
27    let args = Cli::parse();
28    let fmt = infer_format(&args.input, &args.format);
29    let reader_box = open_async_reader(&args.input).await?;
30    if fmt == "6" {
31        stream_outfmt6_to_json(reader_box).await?;
32    } else {
33        // Build AsyncBlastXmlIter from reader_box
34        let iter_reader = reader_box;
35        let mut iter = AsyncBlastXmlIter::from_reader(iter_reader);
36        while let Some(res) = iter.next_iteration().await {
37            match res {
38                Ok(iter_rec) => {
39                    if args.json {
40                        let mut buf = Vec::new();
41                        serde_json::to_writer(&mut buf, &iter_rec)?;
42                        buf.push(b'\n');
43                        tokio::io::stdout().write_all(&buf).await?;
44                    } else {
45                        println!(
46                            "query {:?} hits {}",
47                            iter_rec.query_def,
48                            iter_rec.hits.len()
49                        );
50                    }
51                }
52                Err(e) => eprintln!("xml parse error: {}", e),
53            }
54        }
55    }
56
57    Ok(())
58}