Skip to content

Commit

Permalink
cli: Change quantify output option to be optional
Browse files Browse the repository at this point in the history
If not set, output is written to stdout.
  • Loading branch information
zaeleus committed Feb 15, 2025
1 parent 8de8be8 commit a322ea6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ pub struct Quantify {
#[arg(long, value_parser = parse_mapping_quality, default_value = "10")]
pub min_mapping_quality: MappingQuality,

/// Output destination for feature counts.
/// Output destination.
///
/// If not set, output is written to stdout.
#[arg(short = 'o', long)]
pub output: PathBuf,
pub output: Option<PathBuf>,

/// Input annotations file (GFF3).
#[arg(short = 'a', long)]
Expand Down
21 changes: 16 additions & 5 deletions src/commands/quantify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{fs::File, io::BufWriter, num::NonZeroUsize, path::Path};
use std::{
fs::File,
io::{self, BufWriter, Write},
num::NonZeroUsize,
path::Path,
};

use anyhow::Context as AnyhowContext;
use noodles::{bam, bgzf};
Expand All @@ -20,7 +25,7 @@ pub fn quantify<P, Q, R>(
filter: Filter,
strand_specification_option: StrandSpecificationOption,
worker_count: NonZeroUsize,
results_dst: R,
dst: Option<R>,
) -> anyhow::Result<()>
where
P: AsRef<Path>,
Expand Down Expand Up @@ -97,9 +102,15 @@ where
)?,
};

let writer = File::create(results_dst.as_ref())
.map(BufWriter::new)
.with_context(|| format!("Could not open {}", results_dst.as_ref().display()))?;
let writer: Box<dyn Write> = if let Some(dst) = dst {
File::create(dst.as_ref())
.map(BufWriter::new)
.map(Box::new)
.with_context(|| format!("Could not open {}", dst.as_ref().display()))?
} else {
let stdout = io::stdout().lock();
Box::new(BufWriter::new(stdout))
};

info!("writing counts");

Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ fn quantify(options: cli::Quantify) -> anyhow::Result<()> {
let bam_src = options.src;
let annotations_src = options.annotations;

let results_dst = options.output;

let threads = match options.threads {
Some(n) => n,
None => thread::available_parallelism()?,
Expand All @@ -44,7 +42,7 @@ fn quantify(options: cli::Quantify) -> anyhow::Result<()> {
filter,
strand_specification_option,
threads,
results_dst,
options.output,
)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/quantify_single_end_forward_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_quantify_with_single_end_forward_sample() -> anyhow::Result<()> {
filter,
strand_specification_option,
worker_count,
&results_dst,
Some(&results_dst),
)?;

let actual = fs::read_to_string(results_dst)?;
Expand Down

0 comments on commit a322ea6

Please sign in to comment.