|
| 1 | +use std::{fs::File, io::BufWriter}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use binseq::{BinseqWriterBuilder, write::Format}; |
| 5 | +use bitnuc::BitSize; |
| 6 | +use clap::Parser; |
| 7 | + |
| 8 | +type BoxedWriter = Box<dyn std::io::Write + Send>; |
| 9 | + |
| 10 | +#[derive(Parser)] |
| 11 | +struct Args { |
| 12 | + /// Input FASTX to encode into BINSEQ format |
| 13 | + #[clap(required = true)] |
| 14 | + input: String, |
| 15 | + |
| 16 | + /// Input FASTX to encode into BINSEQ format (R2) |
| 17 | + #[clap(required = false)] |
| 18 | + input2: Option<String>, |
| 19 | + |
| 20 | + /// Output file path for BINSEQ format |
| 21 | + #[clap(short = 'o', long)] |
| 22 | + output: Option<String>, |
| 23 | + |
| 24 | + /// Default prefix for writing BINSEQ: `<prefix>.<ext>` |
| 25 | + #[clap(short = 'p', long, default_value = "output")] |
| 26 | + prefix: String, |
| 27 | + |
| 28 | + /// Format of the output BINSEQ file |
| 29 | + /// |
| 30 | + /// [bq: bq|BQ|b, vbq: vbq|VBQ|v, cbq: cbq|CBQ|c] |
| 31 | + #[clap(short = 'f', long)] |
| 32 | + format: Option<Format>, |
| 33 | + |
| 34 | + /// Exclude quality information in BINSEQ output |
| 35 | + /// |
| 36 | + /// (bq ignores quality always) |
| 37 | + #[clap(short = 'Q', long)] |
| 38 | + exclude_quality: bool, |
| 39 | + |
| 40 | + /// Exclude sequence headers in BINSEQ output |
| 41 | + /// |
| 42 | + /// (bq ignores headers always) |
| 43 | + #[clap(short = 'H', long)] |
| 44 | + exclude_headers: bool, |
| 45 | + |
| 46 | + /// Compression level for BINSEQ output (0: auto) |
| 47 | + #[clap(long, default_value_t = 0)] |
| 48 | + compression_level: i32, |
| 49 | + |
| 50 | + /// Default BITSIZE for BINSEQ output (2: 2bit, 4: 4bit) |
| 51 | + #[clap(long, default_value_t = 2)] |
| 52 | + bitsize: u8, |
| 53 | + |
| 54 | + /// Default BLOCKSIZE in KB for BINSEQ output (vbq,cbq) |
| 55 | + #[clap(long, default_value_t = 128)] |
| 56 | + blocksize: usize, |
| 57 | + |
| 58 | + /// Number of threads to use for parallel processing, 0: all available |
| 59 | + #[clap(short = 'T', long, default_value = "0")] |
| 60 | + threads: usize, |
| 61 | +} |
| 62 | +impl Args { |
| 63 | + /// Determines the output format based on the file extension or the provided format |
| 64 | + fn format(&self) -> Format { |
| 65 | + if let Some(format) = self.format { |
| 66 | + format |
| 67 | + } else { |
| 68 | + if let Some(output) = &self.output { |
| 69 | + match output.split(".").last() { |
| 70 | + Some("bq") => Format::Bq, |
| 71 | + Some("vbq") => Format::Vbq, |
| 72 | + Some("cbq") => Format::Cbq, |
| 73 | + _ => Format::default(), |
| 74 | + } |
| 75 | + } else { |
| 76 | + Format::default() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + fn bitsize(&self) -> BitSize { |
| 81 | + match self.bitsize { |
| 82 | + 4 => BitSize::Four, |
| 83 | + _ => BitSize::Two, |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Creates an output file handle |
| 88 | + fn ohandle(&self) -> Result<BoxedWriter> { |
| 89 | + let path = if let Some(output) = &self.output { |
| 90 | + output.to_string() |
| 91 | + } else { |
| 92 | + format!("{}{}", &self.prefix, self.format().extension()) |
| 93 | + }; |
| 94 | + let ofile = File::create(path).map(BufWriter::new)?; |
| 95 | + Ok(Box::new(ofile)) |
| 96 | + } |
| 97 | + |
| 98 | + fn is_paired(&self) -> bool { |
| 99 | + self.input2.is_some() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn main() -> Result<()> { |
| 104 | + let args = Args::parse(); |
| 105 | + let handle = args.ohandle()?; |
| 106 | + let builder = BinseqWriterBuilder::new(args.format()) |
| 107 | + .bitsize(args.bitsize()) |
| 108 | + .block_size(args.blocksize * 1024) |
| 109 | + .headers(!args.exclude_headers) |
| 110 | + .quality(!args.exclude_quality) |
| 111 | + .compression_level(args.compression_level) |
| 112 | + .encode_fastx(handle); |
| 113 | + if args.is_paired() { |
| 114 | + builder.input_paired(&args.input, args.input2.as_ref().unwrap()) |
| 115 | + } else { |
| 116 | + builder.input(&args.input) |
| 117 | + } |
| 118 | + .threads(args.threads) |
| 119 | + .run()?; |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
0 commit comments