-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving average and base-quality stubs
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::ops::Range; | ||
|
||
pub(crate) fn find_oscillating_quals(bqs: &[u8]) -> Range<usize> { | ||
return 0..0; | ||
} | ||
|
||
pub(crate) enum Tail { | ||
Left, | ||
Right, | ||
Both, | ||
} | ||
|
||
/// A simple moving average calculator. | ||
/// Only requires that T is convertable to f64. | ||
/// Uses space of window * size_of(T) bytes. | ||
struct MovingAverage<T> { | ||
window: usize, | ||
values: Vec<T>, | ||
sum: f64, | ||
idx: usize, | ||
} | ||
|
||
impl<T: Copy + Default + std::convert::Into<f64>> MovingAverage<T> { | ||
/// create a new moving average calculator with a window of `window` values. | ||
fn new(window: usize) -> Self { | ||
Self { window, values: vec![T::default(); window], sum: 0.0, idx: 0 } | ||
} | ||
|
||
/// push a new value into the moving average calculator and get the new mean. | ||
fn push(&mut self, value: T) -> f64 { | ||
let old_value = self.values[self.idx]; | ||
self.values[self.idx] = value; | ||
self.sum = self.sum + value.into() - old_value.into(); | ||
self.idx = (self.idx + 1) % self.window; | ||
self.mean() | ||
} | ||
|
||
/// get the current mean. | ||
#[inline] | ||
fn mean(&self) -> f64 { | ||
self.sum / (self.window as f64) | ||
} | ||
} | ||
|
||
pub(crate) fn find_low_quality_bases( | ||
bqs: &[u8], | ||
min_quality: u8, | ||
window: u8, | ||
tail: Tail, | ||
) -> Range<usize> { | ||
if matches!(tail, Tail::Left | Tail::Both) { | ||
let mut i = 0; | ||
while i < bqs.len() && bqs[i] < min_quality { | ||
i += 1; | ||
} | ||
return 0..i; | ||
} | ||
if matches!(tail, Tail::Right | Tail::Both) { | ||
let mut i = bqs.len() - 1; | ||
while i > 0 && bqs[i] < min_quality { | ||
i -= 1; | ||
} | ||
return i..bqs.len(); | ||
} | ||
0..0 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_find_oscillating_quals() { | ||
let bqs = b"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJ"; | ||
let range = find_oscillating_quals(bqs); | ||
assert_eq!(range, 0..0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod barcode_matching; | ||
pub mod base_quality; | ||
pub mod pair_overlap; | ||
pub mod samples; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// A simple moving average calculator. | ||
/// Only requires that T is convertable to f64. | ||
/// Uses space of window * size_of(T) bytes. | ||
struct MovingAverage<T> { | ||
window: usize, | ||
values: Vec<T>, | ||
sum: f64, | ||
idx: usize, | ||
} | ||
|
||
impl<T: Copy + Default + std::convert::Into<f64>> MovingAverage<T> { | ||
/// create a new moving average calculator with a window of `window` values. | ||
fn new(window: usize) -> Self { | ||
Self { window, values: vec![T::default(); window], sum: 0.0, idx: 0 } | ||
} | ||
|
||
/// push a new value into the moving average calculator and get the new mean. | ||
fn push(&mut self, value: T) -> f64 { | ||
let old_value = self.values[self.idx]; | ||
self.values[self.idx] = value; | ||
self.sum = self.sum + value.into() - old_value.into(); | ||
self.idx = (self.idx + 1) % self.window; | ||
self.mean() | ||
} | ||
|
||
/// get the current mean. | ||
#[inline] | ||
fn mean(&self) -> f64 { | ||
self.sum / (self.window as f64) | ||
} | ||
} | ||
|
||
// write some tests for the calculator | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_moving_average() {} | ||
} |