Skip to content

Commit a322ea6

Browse files
committed
cli: Change quantify output option to be optional
If not set, output is written to stdout.
1 parent 8de8be8 commit a322ea6

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/cli.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ pub struct Quantify {
8080
#[arg(long, value_parser = parse_mapping_quality, default_value = "10")]
8181
pub min_mapping_quality: MappingQuality,
8282

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

8789
/// Input annotations file (GFF3).
8890
#[arg(short = 'a', long)]

src/commands/quantify.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{fs::File, io::BufWriter, num::NonZeroUsize, path::Path};
1+
use std::{
2+
fs::File,
3+
io::{self, BufWriter, Write},
4+
num::NonZeroUsize,
5+
path::Path,
6+
};
27

38
use anyhow::Context as AnyhowContext;
49
use noodles::{bam, bgzf};
@@ -20,7 +25,7 @@ pub fn quantify<P, Q, R>(
2025
filter: Filter,
2126
strand_specification_option: StrandSpecificationOption,
2227
worker_count: NonZeroUsize,
23-
results_dst: R,
28+
dst: Option<R>,
2429
) -> anyhow::Result<()>
2530
where
2631
P: AsRef<Path>,
@@ -97,9 +102,15 @@ where
97102
)?,
98103
};
99104

100-
let writer = File::create(results_dst.as_ref())
101-
.map(BufWriter::new)
102-
.with_context(|| format!("Could not open {}", results_dst.as_ref().display()))?;
105+
let writer: Box<dyn Write> = if let Some(dst) = dst {
106+
File::create(dst.as_ref())
107+
.map(BufWriter::new)
108+
.map(Box::new)
109+
.with_context(|| format!("Could not open {}", dst.as_ref().display()))?
110+
} else {
111+
let stdout = io::stdout().lock();
112+
Box::new(BufWriter::new(stdout))
113+
};
103114

104115
info!("writing counts");
105116

src/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ fn quantify(options: cli::Quantify) -> anyhow::Result<()> {
1818
let bam_src = options.src;
1919
let annotations_src = options.annotations;
2020

21-
let results_dst = options.output;
22-
2321
let threads = match options.threads {
2422
Some(n) => n,
2523
None => thread::available_parallelism()?,
@@ -44,7 +42,7 @@ fn quantify(options: cli::Quantify) -> anyhow::Result<()> {
4442
filter,
4543
strand_specification_option,
4644
threads,
47-
results_dst,
45+
options.output,
4846
)
4947
}
5048

tests/quantify_single_end_forward_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn test_quantify_with_single_end_forward_sample() -> anyhow::Result<()> {
2424
filter,
2525
strand_specification_option,
2626
worker_count,
27-
&results_dst,
27+
Some(&results_dst),
2828
)?;
2929

3030
let actual = fs::read_to_string(results_dst)?;

0 commit comments

Comments
 (0)