mirror of
https://github.com/pcvolkmer/fastq-tools.git
synced 2025-09-13 05:02:53 +00:00
feat: add optional input file argument
This commit is contained in:
18
README.md
18
README.md
@@ -15,9 +15,10 @@ Commands:
|
||||
help Print this message or the help of the given subcommand(s)
|
||||
|
||||
Options:
|
||||
-d, --decompress decompress input as gzip compressed data
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
-i, --input <INPUT_FILE> Input file (optional)
|
||||
-d, --decompress Decompress input as gzip compressed data
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
```
|
||||
|
||||
### Info
|
||||
@@ -33,6 +34,11 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option:
|
||||
```shell
|
||||
cat file_fastq.gz | fastq-tools --decompress info
|
||||
```
|
||||
Using optional input file argument:
|
||||
|
||||
```shell
|
||||
fastq-tools --decompress --input file_fastq.gz info
|
||||
```
|
||||
|
||||
This will result in output like
|
||||
|
||||
@@ -52,4 +58,10 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option:
|
||||
|
||||
```shell
|
||||
cat file_fastq.gz | fastq-tools -d scramble | gzip > scrambled_fastq.gz
|
||||
```
|
||||
|
||||
Using optional input file argument:
|
||||
|
||||
```shell
|
||||
fastq-tools -d -i file_fastq.gz scramble | gzip > scrambled_fastq.gz
|
||||
```
|
@@ -1,4 +1,5 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
@@ -6,10 +7,12 @@ use clap::{Parser, Subcommand};
|
||||
pub struct Args {
|
||||
#[command(subcommand)]
|
||||
pub(crate) command: Command,
|
||||
#[arg(short = 'i', long = "input", help = "Input file (optional)")]
|
||||
pub(crate) input_file: Option<PathBuf>,
|
||||
#[arg(
|
||||
short = 'd',
|
||||
long = "decompress",
|
||||
help = "decompress input as gzip compressed data"
|
||||
help = "Decompress input as gzip compressed data"
|
||||
)]
|
||||
pub(crate) decompress: bool,
|
||||
}
|
||||
|
29
src/main.rs
29
src/main.rs
@@ -8,6 +8,7 @@ use console::Style;
|
||||
use flate2::read::GzDecoder;
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
fn scramble_sequence(value: &str, seed: u32) -> String {
|
||||
@@ -55,25 +56,43 @@ fn scramble_sequence(value: &str, seed: u32) -> String {
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
let stdin = std::io::stdin();
|
||||
let input: Box<dyn BufRead> = match args.input_file {
|
||||
Some(input_file) => {
|
||||
let file = match File::open(input_file) {
|
||||
Ok(file) => file,
|
||||
_ => {
|
||||
println!(
|
||||
"{}\n",
|
||||
Style::new()
|
||||
.bold()
|
||||
.red()
|
||||
.apply_to("🔥 Cannot open input file")
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
Box::new(BufReader::new(file))
|
||||
}
|
||||
_ => Box::new(BufReader::new(std::io::stdin())),
|
||||
};
|
||||
|
||||
match &args.command {
|
||||
Command::Info => {
|
||||
if args.decompress {
|
||||
let gz_decoder = GzDecoder::new(stdin);
|
||||
let gz_decoder = GzDecoder::new(input);
|
||||
let buf_reader = BufReader::new(gz_decoder);
|
||||
info(buf_reader)
|
||||
} else {
|
||||
info(BufReader::new(stdin))
|
||||
info(BufReader::new(input))
|
||||
}
|
||||
}
|
||||
Command::Scramble => {
|
||||
if args.decompress {
|
||||
let gz_decoder = GzDecoder::new(stdin);
|
||||
let gz_decoder = GzDecoder::new(input);
|
||||
let buf_reader = BufReader::new(gz_decoder);
|
||||
scramble(buf_reader)
|
||||
} else {
|
||||
scramble(BufReader::new(stdin))
|
||||
scramble(BufReader::new(input))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user