Skip to content

Commit 1604a56

Browse files
committed
cli/commands/quantify: Filter low mapping quality records
1 parent 7826a93 commit 1604a56

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

atlas-cli/src/cli/quantify.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::PathBuf;
22

33
use clap::Parser;
4+
use noodles::sam::alignment::record::MappingQuality;
45

56
#[derive(Parser)]
67
pub struct Args {
@@ -16,6 +17,16 @@ pub struct Args {
1617
#[arg(long)]
1718
pub annotations: PathBuf,
1819

20+
/// Record mapping quality threshold.
21+
#[arg(long, value_parser = parse_mapping_quality, default_value = "10")]
22+
pub min_mapping_quality: MappingQuality,
23+
1924
/// Source input (BAM).
2025
pub src: PathBuf,
2126
}
27+
28+
fn parse_mapping_quality(s: &str) -> Result<MappingQuality, &'static str> {
29+
s.parse::<u8>()
30+
.map_err(|_| "invalid input")
31+
.and_then(|n| MappingQuality::new(n).ok_or("missing mapping quality"))
32+
}

atlas-cli/src/commands/quantify.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ pub fn quantify(args: quantify::Args) -> Result<(), QuantifyError> {
7373
"detected library layout"
7474
);
7575

76-
let filter = Filter;
76+
let min_mapping_quality = args.min_mapping_quality;
77+
let filter = Filter::new(min_mapping_quality);
7778

7879
let mut reader = bam::io::reader::Builder.build_from_path(src)?;
7980
reader.read_header()?;
+16-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
use std::io;
22

3-
use noodles::bam;
3+
use noodles::{bam, sam::alignment::record::MappingQuality};
44

55
use super::count::Event;
66

7-
pub(super) struct Filter;
7+
pub(super) struct Filter {
8+
min_mapping_quality: MappingQuality,
9+
}
810

911
impl Filter {
12+
pub(super) fn new(min_mapping_quality: MappingQuality) -> Self {
13+
Self {
14+
min_mapping_quality,
15+
}
16+
}
17+
1018
pub(super) fn filter(&self, record: &bam::Record) -> io::Result<Option<Event<'_>>> {
1119
let flags = record.flags();
1220

1321
if flags.is_unmapped() {
1422
return Ok(Some(Event::Unmapped));
1523
}
1624

25+
if let Some(mapping_quality) = record.mapping_quality() {
26+
if mapping_quality < self.min_mapping_quality {
27+
return Ok(Some(Event::LowQuality));
28+
}
29+
}
30+
1731
Ok(None)
1832
}
1933
}

0 commit comments

Comments
 (0)