pub mod cell_pair_units;
pub mod coarse_group_ward_tree;
pub mod cross_batch_matched_fold;
pub(crate) mod local_swap_refinement;
pub(crate) mod pseudobulk_sums;
pub mod sample_units;
pub(crate) mod top_down_fold;
pub use cell_pair_units::{
build_cell_pair_units, collapse_cell_pairs_and_estimate_state_aware_batch_fold,
split_cell_reads_over_pair_units_without_double_counting, CellPairBatchFold, CellPairUnits,
PairNodeOfEndpointNodes,
};
pub use coarse_group_ward_tree::{CoarseGroupWardTree, EXACT_WARD_MAX_GROUPS};
pub use cross_batch_matched_fold::{MatchingCoordinates, StateAwareBatchFold};
pub use sample_units::{
estimate_state_aware_batch_fold_over_samples, SampleBatchFold, UnseenPlacement,
};
pub const DEFAULT_FOLD_KNN: usize = 10;
pub const DEFAULT_FOLD_PSEUDO_COUNT: f32 = 10.0;
pub(crate) const COORD_DIM: usize = 30;
pub(crate) const PROFILE_DIM: usize = 20;
#[derive(Clone, Debug)]
pub struct StateAwareBatchFoldParams {
pub knn: usize,
pub pseudo_count: f32,
pub min_pseudobulks: usize,
pub span_labels: Option<Vec<usize>>,
pub min_spans: Option<usize>,
pub block_size: Option<usize>,
}
impl Default for StateAwareBatchFoldParams {
fn default() -> Self {
Self {
knn: DEFAULT_FOLD_KNN,
pseudo_count: DEFAULT_FOLD_PSEUDO_COUNT,
min_pseudobulks: DEFAULT_FOLD_KNN,
span_labels: None,
min_spans: None,
block_size: None,
}
}
}
impl StateAwareBatchFoldParams {
pub(crate) fn span_labels_of_samples(
&self,
batch: &[usize],
n_batches: usize,
) -> anyhow::Result<(Vec<usize>, usize)> {
match &self.span_labels {
Some(l) => {
anyhow::ensure!(l.len() == batch.len(), "span labels: one per column");
Ok((l.clone(), l.iter().max().map_or(0, |&m| m + 1)))
}
None => Ok((batch.to_vec(), n_batches)),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct CollapsePseudobulks<'a> {
pub finest: &'a [usize],
pub coarsest: &'a [usize],
}
pub(crate) struct BatchSpanningRule {
pub(crate) n_labels: usize,
pub(crate) min_spans: usize,
pub(crate) min_pseudobulks: usize,
}
impl BatchSpanningRule {
pub(crate) fn new(params: &StateAwareBatchFoldParams, n_labels: usize) -> Self {
let default_min = if params.span_labels.is_some() {
2
} else {
n_labels.div_ceil(2)
};
Self {
n_labels,
min_spans: params.min_spans.unwrap_or(default_min).max(2),
min_pseudobulks: params.min_pseudobulks,
}
}
pub(crate) fn spans(&self, count: &[usize]) -> bool {
count.iter().filter(|&&c| c >= self.min_pseudobulks).count() >= self.min_spans
}
pub(crate) fn count(&self, members: impl IntoIterator<Item = (usize, usize)>) -> Vec<usize> {
let seen: rustc_hash::FxHashSet<(usize, usize)> = members.into_iter().collect();
let mut count = vec![0usize; self.n_labels];
for (_, l) in seen {
count[l] += 1;
}
count
}
}
pub(crate) fn compact_present<K: Copy + Eq + std::hash::Hash>(
labels: &[Option<K>],
) -> (Vec<Option<usize>>, Vec<K>) {
let present: Vec<K> = labels.iter().flatten().copied().collect();
let (dense, n) = crate::alg::dc_poisson::compact_labels(&present);
let mut distinct = Vec::with_capacity(n);
let mut it = dense.into_iter();
let out = labels
.iter()
.map(|l| {
l.map(|k| {
let d = it.next().expect("one dense id per present label");
if d == distinct.len() {
distinct.push(k);
}
d
})
})
.collect();
(out, distinct)
}
#[cfg(test)]
mod tests;