|
97 | 97 | #![allow(unused_imports)] |
98 | 98 | use anyhow::{Context, Result}; |
99 | 99 | use async_compression::tokio::bufread::GzipDecoder as AsyncGzDecoder; |
100 | | -use clap::Parser; |
101 | 100 | use quick_xml::events::Event; |
102 | 101 | use quick_xml::reader::Reader; |
103 | 102 | use quick_xml::escape::unescape; |
@@ -173,11 +172,11 @@ pub struct Statistics { |
173 | 172 | // first macro: For Option fields (Strings, Option<u32>, etc.) |
174 | 173 | macro_rules! read_parse_opt { |
175 | 174 | ($self:expr, $tag:expr, $parent_opt:expr, $field:ident, $ok_ty:ty) => {{ |
176 | | - // 1. Read the content first. This borrows 'self', but the borrow ends |
177 | | - // as soon as this statement finishes. |
| 175 | + //read content first. This borrows 'self', but the borrow ends |
| 176 | + //as soon as this statement finishes. |
178 | 177 | let res = $self.read_tag_content($tag).await; |
179 | 178 |
|
180 | | - // 2. Now 'self' is free again. We can handle the result. |
| 179 | + //now 'self' is free again and we can handle the result. |
181 | 180 | match res { |
182 | 181 | Ok(text) => { |
183 | 182 | // 3. Re-borrow only the specific field we want to update. |
@@ -566,58 +565,29 @@ where |
566 | 565 | } |
567 | 566 |
|
568 | 567 |
|
| 568 | +//here we use input in a format such as this in order to capture the required format of XML (5) or Tabular (6) and true or false for Json output |
| 569 | +//see examples for further detail |
569 | 570 |
|
570 | | -#[derive(Parser, Debug)] |
571 | | -#[command(name = "blast-parsers", author, version, about = "async microBioRust BLAST parsers: for outfmt6 (single line tabular) and outfmt5 (xml)")] |
572 | | -struct Cli { |
573 | | - ///Use .gz for gzip-compressed files. |
574 | | - #[arg(short, long, default_value = "-")] |
575 | | - input: String, |
576 | | - /// Format: '6' (tabular) or '5' (xml). If omitted we try to infer by file suffix only |
577 | | - #[arg(short, long)] |
578 | | - format: Option<String>, |
579 | | - /// Output newline-delimited JSON (one JSON object per record/iteration) |
580 | | - #[arg(long)] |
581 | | - json: bool, |
582 | | -} |
| 571 | +//#[derive(Parser, Debug)] |
| 572 | +//#[command(name = "blast-parsers", author, version, about = "async microBioRust BLAST parsers: for outfmt6 (single line tabular) and outfmt5 (xml)")] |
| 573 | +//struct Cli { |
| 574 | +// ///Use .gz for gzip-compressed files. |
| 575 | +// #[arg(short, long, default_value = "-")] |
| 576 | +// input: String, |
| 577 | +// /// Format: '6' (tabular) or '5' (xml). If omitted we try to infer by file suffix only |
| 578 | +// #[arg(short, long)] |
| 579 | +// format: Option<String>, |
| 580 | +// /// Output newline-delimited JSON (one JSON object per record/iteration) |
| 581 | +// #[arg(long)] |
| 582 | +// json: bool, |
| 583 | +//} |
583 | 584 |
|
584 | | -fn infer_format(path: &str, explicit: &Option<String>) -> String { |
| 585 | +pub fn infer_format(path: &str, explicit: &Option<String>) -> String { |
585 | 586 | if let Some(f) = explicit { return f.clone(); } |
586 | 587 | if path.ends_with(".xml") || path.ends_with(".xml.gz") { "5".to_string() } |
587 | 588 | else { "6".to_string() } |
588 | 589 | } |
589 | 590 |
|
590 | | -#[tokio::main] |
591 | | -async fn main() -> Result<()> { |
592 | | - let args = Cli::parse(); |
593 | | - let fmt = infer_format(&args.input, &args.format); |
594 | | - let reader_box = open_async_reader(&args.input).await?; |
595 | | - if fmt == "6" { |
596 | | - stream_outfmt6_to_json(reader_box).await?; |
597 | | - } else { |
598 | | - // Build AsyncBlastXmlIter from reader_box |
599 | | - let iter_reader = reader_box; |
600 | | - let mut iter = AsyncBlastXmlIter::from_reader(iter_reader); |
601 | | - while let Some(res) = iter.next_iteration().await { |
602 | | - match res { |
603 | | - Ok(iter_rec) => { |
604 | | - if args.json { |
605 | | - let mut buf = Vec::new(); |
606 | | - serde_json::to_writer(&mut buf, &iter_rec)?; |
607 | | - buf.push(b'\n'); |
608 | | - tokio::io::stdout().write_all(&buf).await?; |
609 | | - } else { |
610 | | - println!("query {:?} hits {}", iter_rec.query_def, iter_rec.hits.len()); |
611 | | - } |
612 | | - } |
613 | | - Err(e) => eprintln!("xml parse error: {}", e), |
614 | | - } |
615 | | - } |
616 | | - } |
617 | | - |
618 | | - Ok(()) |
619 | | -} |
620 | | - |
621 | 591 | // Unit tests (async if relevant) |
622 | 592 | #[cfg(test)] |
623 | 593 | mod tests { |
|
0 commit comments