1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
use super::Base;
/// A 2D histogram of reference-read base substitutions.
///
/// Each bin is the frequency of a substitution from the reference base to a read base. A same base
/// substitution is not a substitution; thus, the main diagonal will always be 0s.
///
/// ```text
/// read base
/// A C G T N
/// +---+---+---+---+---+
/// A | 0 | | | | |
/// +---+---+---+---+---+
/// C | | 0 | | | |
/// +---+---+---+---+---+
/// reference base G | | | 0 | | |
/// +---+---+---+---+---+
/// T | | | | 0 | |
/// +---+---+---+---+---+
/// N | | | | | 0 |
/// +---+---+---+---+---+
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Histogram {
bins: [[u64; 5]; 5],
}
impl Histogram {
pub fn new(bins: [[u64; 5]; 5]) -> Self {
Self { bins }
}
pub fn get_bins(&self, reference_base: Base) -> &[u64] {
let i = reference_base as usize;
&self.bins[i]
}
pub fn get(&self, reference_base: Base, read_base: Base) -> u64 {
let i = reference_base as usize;
let j = read_base as usize;
self.bins[i][j]
}
pub fn hit(&mut self, reference_base: Base, read_base: Base) {
let i = reference_base as usize;
let j = read_base as usize;
self.bins[i][j] += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_bins() {
let mut histogram = Histogram::default();
histogram.hit(Base::A, Base::C);
assert_eq!(histogram.get_bins(Base::A), [0, 1, 0, 0, 0]);
}
#[test]
fn test_get() {
let mut histogram = Histogram::default();
assert_eq!(histogram.get(Base::A, Base::C), 0);
histogram.hit(Base::A, Base::C);
assert_eq!(histogram.get(Base::A, Base::C), 1);
}
#[test]
fn test_hit() {
let mut histogram = Histogram::default();
histogram.hit(Base::A, Base::C);
histogram.hit(Base::G, Base::T);
histogram.hit(Base::T, Base::A);
histogram.hit(Base::C, Base::G);
histogram.hit(Base::C, Base::G);
assert_eq!(
histogram.bins,
[
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
]
);
}
}