Skip to content

Commit

Permalink
filter_read also takes Option<&Alignment>
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed May 24, 2023
1 parent d593b4f commit 8f8e4bc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/par_granges_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use perbase_lib::{
position::pileup_position::PileupPosition,
read_filter::ReadFilter,
};
use rust_htslib::bam::{self, record::Record, Read};
use rust_htslib::bam::{self, pileup::Alignment, record::Record, Read};
use std::path::PathBuf;

// To use ParGranges you will need to implement a [`RegionProcessor`](par_granges::RegionProcessor),
Expand All @@ -32,7 +32,7 @@ struct BasicReadFilter {
impl ReadFilter for BasicReadFilter {
// Filter reads based SAM flags and mapping quality, true means pass
#[inline]
fn filter_read(&self, read: &Record) -> bool {
fn filter_read(&self, read: &Record, _alignment: Option<&Alignment>) -> bool {
let flags = read.flags();
(!flags) & &self.include_flags == 0
&& flags & &self.exclude_flags == 0
Expand Down
4 changes: 2 additions & 2 deletions src/commands/only_depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<F: ReadFilter> OnlyDepthProcessor<F> {
for record in reader
.rc_records()
.map(|r| r.expect("Read record"))
.filter(|read| self.read_filter.filter_read(&read))
.filter(|read| self.read_filter.filter_read(&read, None))
.flat_map(|record| IterAlignedBlocks::new(record, self.mate_fix))
{
let rec_start = u32::try_from(record.0).expect("check overflow");
Expand Down Expand Up @@ -396,7 +396,7 @@ impl<F: ReadFilter> OnlyDepthProcessor<F> {
for record in reader
.rc_records()
.map(|r| r.expect("Read record"))
.filter(|read| self.read_filter.filter_read(&read))
.filter(|read| self.read_filter.filter_read(&read, None))
{
let rec_start = u32::try_from(record.reference_start()).expect("check overflow");
let rec_stop = u32::try_from(record.reference_end()).expect("check overflow");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! position::pileup_position::PileupPosition,
//! read_filter::ReadFilter,
//! };
//! use rust_htslib::bam::{self, record::Record, Read};
//! use rust_htslib::bam::{self, record::Record, Read, pileup::Alignment};
//! use std::path::PathBuf;
//!
//! // To use ParGranges you will need to implement a [`RegionProcessor`](par_granges::RegionProcessor),
Expand All @@ -44,7 +44,7 @@
//! impl ReadFilter for BasicReadFilter {
//! // Filter reads based SAM flags and mapping quality, true means pass
//! #[inline]
//! fn filter_read(&self, read: &Record) -> bool {
//! fn filter_read(&self, read: &Record, _: Option<&Alignment>) -> bool {
//! let flags = read.flags();
//! (!flags) & &self.include_flags == 0
//! && flags & &self.exclude_flags == 0
Expand Down
8 changes: 5 additions & 3 deletions src/lib/position/pileup_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::position::Position;
use crate::read_filter::ReadFilter;
use itertools::Itertools;
use proptest::strategy::W;
use rust_htslib::bam::{
self,
pileup::{Alignment, Pileup},
Expand Down Expand Up @@ -72,7 +73,7 @@ impl PileupPosition {
read_filter: &F,
base_filter: Option<u8>,
) {
if !read_filter.filter_read(&record) {
if !read_filter.filter_read(&record, Some(alignment)) {
self.depth -= 1;
self.fail += 1;
return;
Expand Down Expand Up @@ -201,9 +202,10 @@ impl PileupPosition {
Ordering::Less => Ordering::Less,
Ordering::Equal => {
// Check if a is first in pair
if a.1.flags() & 64 == 0 && read_filter.filter_read(&a.1) {
if a.1.flags() & 64 == 0 && read_filter.filter_read(&a.1, Some(&a.0)) {
Ordering::Greater
} else if b.1.flags() & 64 == 0 && read_filter.filter_read(&b.1) {
} else if b.1.flags() & 64 == 0 && read_filter.filter_read(&b.1, Some(&b.0))
{
Ordering::Less
} else {
// Default to `a` in the event that there is no first in pair for some reason
Expand Down
5 changes: 3 additions & 2 deletions src/lib/read_filter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! A trait and default implementation of a read filter.
use rust_htslib::bam::pileup::Alignment;
use rust_htslib::bam::record::Record;

/// Anything that implements ReadFilter can apply a filter set to read.
pub trait ReadFilter {
/// filters a read, true is pass, false if fail
fn filter_read(&self, read: &Record) -> bool;
fn filter_read(&self, read: &Record, alignment: Option<&Alignment>) -> bool;
}

/// A straightforward read filter.
Expand All @@ -28,7 +29,7 @@ impl DefaultReadFilter {
impl ReadFilter for DefaultReadFilter {
/// Filter reads based SAM flags and mapping quality
#[inline(always)]
fn filter_read(&self, read: &Record) -> bool {
fn filter_read(&self, read: &Record, _alignment: Option<&Alignment>) -> bool {
let flags = read.flags();
(!flags) & &self.include_flags == 0
&& flags & &self.exclude_flags == 0
Expand Down

0 comments on commit 8f8e4bc

Please sign in to comment.