use crate::dim::{Frame, ReferenceId};
use super::codon::GENES;
pub const FRAME: &str = "K03455";
pub const BASES: u64 = 9719;
pub const POL: u64 = 2085;
pub const LAST: u64 = {
let mut last = 0;
let mut index = 0;
while index < GENES.len() {
if GENES[index].end as u64 > last {
last = GENES[index].end as u64;
}
index += 1;
}
last
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Mismatch {
TooShort {
bases: u64,
},
Undeclared {
bases: Option<u64>,
},
Sliced {
frame: Frame,
},
Elsewhere {
frame: Frame,
},
Several {
contigs: usize,
},
}
#[must_use]
pub fn expected() -> String {
format!("{FRAME} from base 1 ({BASES} bases)")
}
impl Mismatch {
#[must_use]
pub fn detail(&self) -> String {
match self {
Self::TooShort { bases } => format!(
"The reference is {bases} bases long and the gene windows reach base {LAST}, \
so they cannot all land on the genes they name; protease is read from \
wherever base {} of this file happens to be.",
first_of("PR"),
),
Self::Undeclared { bases: Some(bases) } => format!(
"The reference is {bases} bases where whole-genome {FRAME} is {BASES}, so \
this engine cannot tell from the file whether its gene windows land on the \
genes they name."
),
Self::Undeclared { bases: None } => format!(
"The reference does not record a length, so this engine cannot tell whether \
it is numbered along {FRAME} from base 1."
),
Self::Sliced { frame } => format!(
"Base 1 of this reference is base {} of {FRAME}, so every position in it is \
{} lower than the position this engine means by the same number.",
frame.first(),
frame.offset(),
),
Self::Elsewhere { frame } => format!(
"This reference is numbered along {}, and these gene windows are positions \
in {FRAME}.",
frame.describe(),
),
Self::Several { contigs } => format!(
"The reference holds {contigs} sequences, and a resistance reference is one \
viral genome numbered along {FRAME}."
),
}
}
#[must_use]
pub fn conclusion(&self) -> String {
match self {
Self::TooShort { .. } | Self::Sliced { .. } => format!(
"This engine reads whole-genome {FRAME} and does not shift its windows. If \
this is a pol-relative reference — {FRAME} {POL}..5096 is the usual slice — \
pad it back into the frame with `\"N\" * {} + pol` and declare \
`frame=(\"{FRAME}\", 1)`.",
POL - 1,
),
Self::Undeclared { .. } => format!(
"Which frame a reference is numbered along cannot be read from a FASTA. \
Declare it where you load the reference: `frame=(\"{FRAME}\", 1)` for a \
whole genome, or the base it starts at for a slice."
),
Self::Elsewhere { .. } => format!(
"Convert the reference to {FRAME} coordinates, or declare the frame it is \
really numbered along."
),
Self::Several { .. } => {
"Pass the single genome the calls were made against.".to_owned()
}
}
}
#[must_use]
pub fn describe(&self) -> String {
format!("{} {}", self.detail(), self.conclusion())
}
}
#[must_use]
pub fn mismatch(reference: &ReferenceId) -> Option<Mismatch> {
if let Some(frame) = reference.frame() {
if !is_k03455(frame.name()) {
return Some(Mismatch::Elsewhere {
frame: frame.clone(),
});
}
return (!frame.is_whole()).then(|| Mismatch::Sliced {
frame: frame.clone(),
});
}
match reference.contigs() {
[only] => match only.length {
Some(BASES) => None,
Some(bases) if bases < LAST => Some(Mismatch::TooShort { bases }),
bases => Some(Mismatch::Undeclared { bases }),
},
contigs => Some(Mismatch::Several {
contigs: contigs.len(),
}),
}
}
fn is_k03455(name: &str) -> bool {
let bare = name.split('.').next().unwrap_or(name).trim();
bare.eq_ignore_ascii_case(FRAME) || bare.eq_ignore_ascii_case("HXB2")
}
fn first_of(name: &str) -> u32 {
let mut index = 0;
while index < GENES.len() {
if GENES[index].name.as_bytes() == name.as_bytes() {
return GENES[index].start;
}
index += 1;
}
0
}