pub const NULL_BACKGROUND: f64 = 0.001;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Params {
pub p: f64,
pub c: f64,
pub pi_ancient: f64,
pub pi_contamination: f64,
}
impl Default for Params {
fn default() -> Self {
Params {
p: 0.3,
c: 0.01,
pi_ancient: 0.001,
pi_contamination: 0.001,
}
}
}
pub fn error_rate(q: u8) -> f64 {
10f64.powf(-f64::from(q) / 10.0) / 3.0
}
pub fn damage(z: u32, p: f64, c: f64) -> f64 {
(1.0 - p).powf(f64::from(z) - 1.0) * p + c
}
fn p_match(d: f64, pi: f64, e: f64) -> f64 {
(1.0 - pi) * (1.0 - e) * (1.0 - d) + (1.0 - pi) * e * d + pi * e * (1.0 - d)
}
fn p_match_two_ends(a: f64, b: f64, pi: f64, e: f64) -> f64 {
(1.0 - pi) * (1.0 - e) * (1.0 - a) * (1.0 - b)
+ (1.0 - pi) * e * a * (1.0 - b)
+ (1.0 - pi) * e * b * (1.0 - a)
+ pi * e * (1.0 - a) * (1.0 - b)
}
fn log_ratio(is_match: bool, p_ancient: f64, p_null: f64) -> f64 {
if is_match {
(p_ancient / p_null).ln()
} else {
((1.0 - p_ancient) / (1.0 - p_null)).ln()
}
}
pub fn site_term_ds(is_match: bool, z: u32, q: u8, params: &Params) -> f64 {
let table = ((1..=DsTable::MAX_Z).contains(&z) && q <= DsTable::MAX_Q)
.then(|| DsTable::for_params(params))
.flatten();
match table {
Some(table) => table.get(is_match, z, q),
None => site_term_ds_direct(is_match, z, q, params),
}
}
struct DsTable {
params: Params,
terms: Vec<f64>,
}
impl DsTable {
const MAX_Z: u32 = 256;
const MAX_Q: u8 = 93;
const N_Q: usize = Self::MAX_Q as usize + 1;
fn new(params: Params) -> Self {
let mut terms = Vec::with_capacity(2 * Self::MAX_Z as usize * Self::N_Q);
for is_match in [false, true] {
for z in 1..=Self::MAX_Z {
for q in 0..=Self::MAX_Q {
terms.push(site_term_ds_direct(is_match, z, q, ¶ms));
}
}
}
DsTable { params, terms }
}
fn for_params(params: &Params) -> Option<&'static DsTable> {
static TABLE: std::sync::OnceLock<DsTable> = std::sync::OnceLock::new();
let table = TABLE.get_or_init(|| DsTable::new(*params));
(table.params == *params).then_some(table)
}
fn get(&self, is_match: bool, z: u32, q: u8) -> f64 {
let row = usize::from(is_match) * Self::MAX_Z as usize + (z as usize - 1);
self.terms[row * Self::N_Q + usize::from(q)]
}
}
fn site_term_ds_direct(is_match: bool, z: u32, q: u8, params: &Params) -> f64 {
let e = error_rate(q);
log_ratio(
is_match,
p_match(damage(z, params.p, params.c), params.pi_ancient, e),
p_match(NULL_BACKGROUND, params.pi_contamination, e),
)
}
pub fn site_term_ss(is_match: bool, z: u32, y: u32, q: u8, params: &Params) -> f64 {
site_term_ds(is_match, z, q, params) + site_term_s1(is_match, z, y, q, params)
}
pub fn site_term_s1(is_match: bool, z: u32, y: u32, q: u8, params: &Params) -> f64 {
let in_range = |d: u32| (1..=S1Table::MAX_Z).contains(&d);
if in_range(z)
&& in_range(y)
&& q <= S1Table::MAX_Q
&& let Some(t) = S1Table::for_params(params)
{
let (a, b, e) = (
t.damage[z as usize - 1],
t.damage[y as usize - 1],
t.error[usize::from(q)],
);
return log_ratio(
is_match,
p_match_two_ends(a, b, params.pi_ancient, e),
t.null[usize::from(q)],
);
}
site_term_s1_direct(is_match, z, y, q, params)
}
struct S1Table {
params: Params,
damage: Vec<f64>,
error: Vec<f64>,
null: Vec<f64>,
}
impl S1Table {
const MAX_Z: u32 = DsTable::MAX_Z;
const MAX_Q: u8 = DsTable::MAX_Q;
fn new(params: Params) -> Self {
let error: Vec<f64> = (0..=Self::MAX_Q).map(error_rate).collect();
S1Table {
params,
damage: (1..=Self::MAX_Z)
.map(|z| damage(z, params.p, params.c))
.collect(),
null: error
.iter()
.map(|&e| {
p_match_two_ends(NULL_BACKGROUND, NULL_BACKGROUND, params.pi_contamination, e)
})
.collect(),
error,
}
}
fn for_params(params: &Params) -> Option<&'static S1Table> {
static TABLE: std::sync::OnceLock<S1Table> = std::sync::OnceLock::new();
let table = TABLE.get_or_init(|| S1Table::new(*params));
(table.params == *params).then_some(table)
}
}
fn site_term_s1_direct(is_match: bool, z: u32, y: u32, q: u8, params: &Params) -> f64 {
let e = error_rate(q);
log_ratio(
is_match,
p_match_two_ends(
damage(z, params.p, params.c),
damage(y, params.p, params.c),
params.pi_ancient,
e,
),
p_match_two_ends(NULL_BACKGROUND, NULL_BACKGROUND, params.pi_contamination, e),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-12
}
#[test]
fn error_rate_is_a_third_of_phred_probability() {
assert!(close(error_rate(0), 1.0 / 3.0));
assert!(close(error_rate(10), 0.1 / 3.0));
assert!(close(error_rate(40), 1e-4 / 3.0));
}
#[test]
fn damage_follows_paper_eq3_with_defaults() {
assert!(close(damage(1, 0.3, 0.01), 0.31));
assert!(close(damage(2, 0.3, 0.01), 0.22));
assert!(close(damage(3, 0.3, 0.01), 0.157));
}
#[test]
fn ds_terms_match_pmdtools_at_q40() {
let p = Params::default();
assert!(close(site_term_ds(false, 1, 40, &p), 5.029717285061266));
assert!(close(site_term_ds(true, 1, 40, &p), -0.37004823819308086));
assert!(close(site_term_ds(false, 30, 40, &p), 1.6917038398804485));
}
#[test]
fn ss_terms_match_pmdtools_at_q40() {
let p = Params::default();
assert!(close(site_term_ss(false, 1, 30, 40, &p), 9.681909363978177));
assert!(close(site_term_ss(false, 30, 1, 40, &p), 6.343895918797358));
assert!(close(
site_term_ss(true, 1, 30, 40, &p),
-0.7491557655766737
));
}
#[test]
fn ds_table_is_bit_identical_to_direct_computation() {
let other = Params {
p: 0.5,
c: 0.02,
pi_ancient: 0.002,
pi_contamination: 0.003,
};
for params in [Params::default(), other] {
let table = DsTable::new(params);
for is_match in [false, true] {
for z in 1..=DsTable::MAX_Z {
for q in 0..=DsTable::MAX_Q {
assert_eq!(
table.get(is_match, z, q).to_bits(),
site_term_ds_direct(is_match, z, q, ¶ms).to_bits()
);
}
}
}
}
let p = Params::default();
assert_eq!(
site_term_ds(false, 1000, 40, &p).to_bits(),
site_term_ds_direct(false, 1000, 40, &p).to_bits()
);
assert_eq!(
site_term_ds(true, 5, 255, &other).to_bits(),
site_term_ds_direct(true, 5, 255, &other).to_bits()
);
}
#[test]
fn s1_table_is_bit_identical_to_direct_computation() {
let other = Params {
p: 0.5,
c: 0.02,
pi_ancient: 0.002,
pi_contamination: 0.003,
};
let distances = [1, 2, 3, 7, 30, 100, 255, 256];
for params in [Params::default(), other] {
let table = S1Table::new(params);
for is_match in [false, true] {
for z in 1..=S1Table::MAX_Z {
for y in distances {
for q in 0..=S1Table::MAX_Q {
let (a, b, e) = (
table.damage[z as usize - 1],
table.damage[y as usize - 1],
table.error[usize::from(q)],
);
let cached = log_ratio(
is_match,
p_match_two_ends(a, b, params.pi_ancient, e),
table.null[usize::from(q)],
);
assert_eq!(
cached.to_bits(),
site_term_s1_direct(is_match, z, y, q, ¶ms).to_bits()
);
}
}
}
}
}
let p = Params::default();
assert_eq!(
site_term_s1(false, 300, 5, 40, &p).to_bits(),
site_term_s1_direct(false, 300, 5, 40, &p).to_bits()
);
assert_eq!(
site_term_s1(true, 5, 6, 200, &other).to_bits(),
site_term_s1_direct(true, 5, 6, 200, &other).to_bits()
);
}
#[test]
fn s1_term_counts_each_site_once() {
let p = Params::default();
assert!(close(site_term_s1(false, 1, 30, 40, &p), 4.65219207891691));
assert!(close(site_term_s1(false, 30, 1, 40, &p), 4.65219207891691));
assert!(close(
site_term_s1(true, 1, 30, 40, &p),
-0.37910752738359293
));
assert!(close(
site_term_ss(false, 1, 30, 40, &p),
site_term_ds(false, 1, 40, &p) + site_term_s1(false, 1, 30, 40, &p)
));
}
}