diff --git a/src/cli.rs b/src/cli.rs index 8c83631..ab88da2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, /// Input annotations file (GFF3). #[arg(short = 'a', long)] diff --git a/src/commands/quantify.rs b/src/commands/quantify.rs index 107b2c2..e319e3c 100644 --- a/src/commands/quantify.rs +++ b/src/commands/quantify.rs @@ -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}; @@ -20,7 +25,7 @@ pub fn quantify( filter: Filter, strand_specification_option: StrandSpecificationOption, worker_count: NonZeroUsize, - results_dst: R, + dst: Option, ) -> anyhow::Result<()> where P: AsRef, @@ -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 = 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"); diff --git a/src/main.rs b/src/main.rs index 58be868..4701a76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()?, @@ -44,7 +42,7 @@ fn quantify(options: cli::Quantify) -> anyhow::Result<()> { filter, strand_specification_option, threads, - results_dst, + options.output, ) } diff --git a/tests/quantify_single_end_forward_test.rs b/tests/quantify_single_end_forward_test.rs index 01f8686..a2587a5 100644 --- a/tests/quantify_single_end_forward_test.rs +++ b/tests/quantify_single_end_forward_test.rs @@ -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)?;