use std::cmp::Ordering;
use std::str::FromStr;
use crate::error::{Error, Result};
use crate::pileup::EntryKind;
const MAX_SUMMED_QUALITY: u16 = 93;
const NO_CALL: u8 = b'N';
const NO_CALL_QUALITY: u8 = 2;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AgreementStrategy {
#[default]
Consensus,
MaxQual,
PassThrough,
}
impl AgreementStrategy {
pub fn as_str(self) -> &'static str {
match self {
AgreementStrategy::Consensus => "consensus",
AgreementStrategy::MaxQual => "max_qual",
AgreementStrategy::PassThrough => "pass_through",
}
}
}
impl FromStr for AgreementStrategy {
type Err = Error;
fn from_str(name: &str) -> Result<Self> {
named(
[Self::Consensus, Self::MaxQual, Self::PassThrough],
Self::as_str,
"AgreementStrategy",
name,
)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DisagreementStrategy {
#[default]
Consensus,
MaskBoth,
MaskLowerQual,
}
impl DisagreementStrategy {
pub fn as_str(self) -> &'static str {
match self {
DisagreementStrategy::Consensus => "consensus",
DisagreementStrategy::MaskBoth => "mask_both",
DisagreementStrategy::MaskLowerQual => "mask_lower_qual",
}
}
}
impl FromStr for DisagreementStrategy {
type Err = Error;
fn from_str(name: &str) -> Result<Self> {
named(
[Self::Consensus, Self::MaskBoth, Self::MaskLowerQual],
Self::as_str,
"DisagreementStrategy",
name,
)
}
}
fn named<S: Copy>(
strategies: [S; 3],
as_str: fn(S) -> &'static str,
kind: &'static str,
name: &str,
) -> Result<S> {
strategies
.into_iter()
.find(|&strategy| as_str(strategy) == name)
.ok_or_else(|| Error::UnknownStrategy {
kind,
name: name.to_owned(),
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Observation {
pub kind: EntryKind,
pub base: Option<u8>,
pub quality: Option<u8>,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Vote {
held: EntryKind,
called: Option<(u8, u8)>,
deleted: Option<u8>,
}
impl Default for Vote {
fn default() -> Self {
Self {
held: EntryKind::Skip,
called: None,
deleted: None,
}
}
}
impl Vote {
pub(crate) fn add(
&mut self,
read: Observation,
agreement: AgreementStrategy,
disagreement: DisagreementStrategy,
min_base_quality: i64,
) {
if rank(read.kind) > rank(self.held) {
self.held = read.kind;
}
let Some(quality) = read
.quality
.filter(|&quality| i64::from(quality) >= min_base_quality)
else {
return;
};
match (read.kind, read.base) {
(EntryKind::Base, Some(base)) => {
self.called = Some(match self.called {
Some(template) => call(template, (base, quality), agreement, disagreement),
None => (base, quality),
});
}
(EntryKind::Deletion, _) => self.deleted = self.deleted.max(Some(quality)),
_ => {}
}
}
pub(crate) fn result(self) -> Observation {
match (self.called, self.deleted) {
(Some((base, quality)), _) => Observation {
kind: EntryKind::Base,
base: Some(base),
quality: Some(quality),
},
(None, Some(quality)) => Observation {
kind: EntryKind::Deletion,
base: None,
quality: Some(quality),
},
(None, None) => Observation {
kind: self.held,
base: None,
quality: None,
},
}
}
}
fn rank(kind: EntryKind) -> u8 {
match kind {
EntryKind::Base => 3,
EntryKind::Deletion => 2,
EntryKind::Skip => 1,
EntryKind::Insertion => 0,
}
}
fn call(
first: (u8, u8),
second: (u8, u8),
agreement: AgreementStrategy,
disagreement: DisagreementStrategy,
) -> (u8, u8) {
let ((base1, quality1), (base2, quality2)) = (first, second);
match (base1 == NO_CALL, base2 == NO_CALL) {
(true, true) => (NO_CALL, quality1.max(quality2)),
(true, false) => second,
(false, true) => first,
(false, false) if base1 == base2 => {
let quality = match agreement {
AgreementStrategy::Consensus => {
(u16::from(quality1) + u16::from(quality2)).min(MAX_SUMMED_QUALITY) as u8
}
AgreementStrategy::MaxQual | AgreementStrategy::PassThrough => {
quality1.max(quality2)
}
};
(base1, quality)
}
(false, false) => match (disagreement, quality1.cmp(&quality2)) {
(DisagreementStrategy::MaskBoth, _) | (_, Ordering::Equal) => {
(NO_CALL, NO_CALL_QUALITY)
}
(DisagreementStrategy::Consensus, Ordering::Greater) => {
(base1, (quality1 - quality2).max(NO_CALL_QUALITY))
}
(DisagreementStrategy::Consensus, Ordering::Less) => {
(base2, (quality2 - quality1).max(NO_CALL_QUALITY))
}
(DisagreementStrategy::MaskLowerQual, Ordering::Greater) => first,
(DisagreementStrategy::MaskLowerQual, Ordering::Less) => second,
},
}
}