use rayon::prelude::*;
use crate::error::{Result, arg_err};
use crate::pvalue::with_threads;
use crate::stats::{p_adjust_bh, pbinom_upper};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PeaksMode {
Central,
Local,
}
impl PeaksMode {
pub fn as_str(&self) -> &'static str {
match self {
PeaksMode::Central => "central",
PeaksMode::Local => "local",
}
}
}
impl std::str::FromStr for PeaksMode {
type Err = crate::error::Error;
fn from_str(s: &str) -> Result<PeaksMode> {
match s {
"central" => Ok(PeaksMode::Central),
"local" => Ok(PeaksMode::Local),
_ => arg_err(format!("unknown mode '{s}'; expected 'central' or 'local'")),
}
}
}
impl std::fmt::Display for PeaksMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PeaksOptions {
pub mode: PeaksMode,
pub min_window: usize,
pub max_window: Option<usize>,
pub window_step: usize,
pub position_step: usize,
pub nthreads: usize,
}
impl Default for PeaksOptions {
fn default() -> Self {
PeaksOptions {
mode: PeaksMode::Central,
min_window: 10,
max_window: None,
window_step: 10,
position_step: 5,
nthreads: 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PeakHit {
pub motif: usize,
pub sequence: usize,
pub centre: f64,
pub score: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MotifPeak {
pub motif: usize,
pub nhits: usize,
pub best_window: Option<usize>,
pub best_centre: Option<f64>,
pub hits_in: usize,
pub hits_out: usize,
pub expected_in: Option<f64>,
pub enrichment: Option<f64>,
pub log2_enrichment: Option<f64>,
pub pvalue: f64,
pub qvalue: f64,
pub centres: Vec<i64>,
}
fn test_motif(
motif: usize,
centres: &[f64],
len: usize,
widths: &[usize],
opts: &PeaksOptions,
) -> MotifPeak {
let n = centres.len();
let lenf = len as f64;
let mid = lenf / 2.0 - 1.0;
let (mut best_p, mut best_w, mut best_c, mut best_k) = (1.0, None, None, 0);
let mut n_tests = 0usize;
let mut sorted = centres.to_vec();
sorted.sort_by(f64::total_cmp);
let count = |c: f64, half: f64| {
sorted.partition_point(|&x| x <= c + half) - sorted.partition_point(|&x| x < c - half)
};
for &w in widths {
let half = w as f64 / 2.0;
let p = w as f64 / lenf;
let cs: Vec<f64> = match opts.mode {
PeaksMode::Central => vec![mid],
PeaksMode::Local => {
let (lo, hi) = (half.ceil() as i64, (lenf - half).floor() as i64);
if hi < lo {
continue;
}
(lo..=hi)
.step_by(opts.position_step)
.map(|c| c as f64 - 1.0)
.collect()
}
};
for c in cs {
let k = count(c, half);
let pv = pbinom_upper(k as u64, n as u64, p);
n_tests += 1;
if pv < best_p {
(best_p, best_w, best_c, best_k) = (pv, Some(w), Some(c), k);
}
}
}
let pvalue = (best_p * n_tests.max(1) as f64).min(1.0);
let expected_in = best_w.map(|w| n as f64 * w as f64 / lenf);
let enrichment = expected_in.filter(|&e| e > 0.0).map(|e| best_k as f64 / e);
MotifPeak {
motif,
nhits: n,
best_window: best_w,
best_centre: best_c,
hits_in: best_k,
hits_out: n - best_k,
expected_in,
enrichment,
log2_enrichment: enrichment.filter(|&e| e > 0.0).map(f64::log2),
pvalue,
qvalue: 0.0,
centres: centres
.iter()
.map(|c| (c + 1.0).round_ties_even() as i64 - 1)
.collect(),
}
}
pub fn motif_peaks(
hits: &[PeakHit],
seq_length: usize,
opts: &PeaksOptions,
) -> Result<Vec<MotifPeak>> {
if seq_length < 2 {
return arg_err("the sequence length must be at least 2");
}
if opts.min_window < 1 || opts.window_step < 1 || opts.position_step < 1 {
return arg_err("min_window, window_step and position_step must be at least 1");
}
let max_window = opts.max_window.unwrap_or(seq_length / 2).min(seq_length);
if max_window < opts.min_window {
return arg_err(format!(
"the widest window ({max_window}) is narrower than min_window ({})",
opts.min_window
));
}
let widths: Vec<usize> = (opts.min_window..=max_window)
.step_by(opts.window_step)
.collect();
let mut best: Vec<(usize, usize, usize)> = Vec::new(); let mut index: std::collections::HashMap<(usize, usize), usize> = Default::default();
for (i, h) in hits.iter().enumerate() {
match index.get(&(h.motif, h.sequence)) {
Some(&b) => {
if h.score > hits[best[b].2].score {
best[b].2 = i;
}
}
None => {
index.insert((h.motif, h.sequence), best.len());
best.push((h.motif, h.sequence, i));
}
}
}
let mut kept: Vec<usize> = best.iter().map(|b| b.2).collect();
kept.sort_unstable_by_key(|&i| (hits[i].motif, i));
let mut groups: Vec<(usize, Vec<f64>)> = Vec::new();
for &i in &kept {
match groups.last_mut() {
Some((m, c)) if *m == hits[i].motif => c.push(hits[i].centre),
_ => groups.push((hits[i].motif, vec![hits[i].centre])),
}
}
let mut out: Vec<MotifPeak> = with_threads(opts.nthreads, || {
groups
.par_iter()
.map(|(m, c)| test_motif(*m, c, seq_length, &widths, opts))
.collect()
})?;
let q = p_adjust_bh(&out.iter().map(|p| p.pvalue).collect::<Vec<_>>());
for (p, q) in out.iter_mut().zip(q) {
p.qvalue = q;
}
Ok(out)
}