use std::collections::BTreeMap;
use super::ruleset::{Acids, Drug, Ruleset};
#[derive(Clone, Debug, Default)]
pub struct Protein {
acids: BTreeMap<u32, Acids>,
}
impl Protein {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn saw(&mut self, position: u32, acids: Acids) {
if !acids.is_empty() {
self.acids.insert(position, acids);
}
}
#[must_use]
pub fn at(&self, position: u32) -> Option<Acids> {
self.acids.get(&position).copied()
}
#[must_use]
pub fn read(&self) -> usize {
self.acids.len()
}
}
#[derive(Clone, Debug, Default)]
pub struct Observed {
proteins: BTreeMap<String, Protein>,
}
impl Observed {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn with(&mut self, gene: &str, protein: Protein) {
self.proteins.insert(gene.to_owned(), protein);
}
#[must_use]
pub fn protein(&self, gene: &str) -> Option<&Protein> {
self.proteins.get(gene)
}
}
#[derive(Clone, Debug)]
pub struct Assessment {
pub drug: String,
pub name: String,
pub class: String,
pub gene: String,
pub score: Option<i32>,
pub level: Option<u8>,
pub interpretation: String,
pub mutations: Vec<(u32, Acids)>,
pub unread: Vec<u32>,
}
impl Assessment {
#[must_use]
pub fn is_complete(&self) -> bool {
self.unread.is_empty()
}
}
pub fn interpret(ruleset: &Ruleset, observed: &Observed) -> Result<Vec<Assessment>, String> {
ruleset
.drugs
.iter()
.map(|drug| assess(ruleset, drug, observed))
.collect()
}
fn assess(ruleset: &Ruleset, drug: &Drug, observed: &Observed) -> Result<Assessment, String> {
let Some(protein) = observed.protein(&drug.gene) else {
return Ok(Assessment {
drug: drug.abbreviation.clone(),
name: drug.name.clone(),
class: drug.class.clone(),
gene: drug.gene.clone(),
score: None,
level: None,
interpretation: "Not assessed".to_owned(),
mutations: Vec::new(),
unread: Vec::new(),
});
};
let mut total = 0;
let mut mutations: BTreeMap<u32, Acids> = BTreeMap::new();
let mut unread: Vec<u32> = Vec::new();
for group in &drug.groups {
let mut best: Option<(i32, Vec<(u32, Acids)>)> = None;
for item in group {
let mut seen = Vec::with_capacity(item.residues.len());
let mut refuted = false;
let mut missing = Vec::new();
for residue in &item.residues {
match protein.at(residue.position) {
Some(acids) if acids.meets(residue.acids) => {
seen.push((residue.position, acids));
}
Some(_) => refuted = true,
None => missing.push(residue.position),
}
}
if refuted {
continue;
}
if missing.is_empty() {
if best.as_ref().is_none_or(|(score, _)| item.score > *score) {
best = Some((item.score, seen));
}
} else {
for position in missing {
if !unread.contains(&position) {
unread.push(position);
}
}
}
}
if let Some((score, seen)) = best {
total += score;
for (position, acids) in seen {
mutations.insert(position, acids);
}
}
}
let level = ruleset.level(total)?;
unread.sort_unstable();
Ok(Assessment {
drug: drug.abbreviation.clone(),
name: drug.name.clone(),
class: drug.class.clone(),
gene: drug.gene.clone(),
score: Some(total),
level: Some(level.order),
interpretation: level.name.clone(),
mutations: mutations.into_iter().collect(),
unread,
})
}