genomicframe-core 0.2.0

High-performance genomics I/O and interoperability layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Shared statistics utilities for genomic data
//!
//! This module provides composable, streaming-friendly statistics
//! that can be computed over large genomic datasets with minimal memory usage.

use crate::parallel::Mergeable;
use std::collections::HashMap;

/// Running statistics accumulator (Welford's online algorithm)
///
/// Computes mean, variance, and standard deviation in a single pass
/// with numerically stable updates. Memory: O(1)
#[derive(Debug, Clone, Default)]
pub struct RunningStats {
    count: usize,
    mean: f64,
    m2: f64, // Sum of squared differences from mean
    min: Option<f64>,
    max: Option<f64>,
}

impl RunningStats {
    /// Create a new running statistics accumulator
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a single value to the accumulator
    pub fn push(&mut self, value: f64) {
        self.count += 1;
        let delta = value - self.mean;
        self.mean += delta / self.count as f64;
        let delta2 = value - self.mean;
        self.m2 += delta * delta2;

        self.min = Some(self.min.map_or(value, |m| m.min(value)));
        self.max = Some(self.max.map_or(value, |m| m.max(value)));
    }

    /// Get the count of values
    pub fn count(&self) -> usize {
        self.count
    }

    /// Get the mean value
    pub fn mean(&self) -> Option<f64> {
        if self.count > 0 {
            Some(self.mean)
        } else {
            None
        }
    }

    /// Get the sample variance
    pub fn variance(&self) -> Option<f64> {
        if self.count > 1 {
            Some(self.m2 / (self.count - 1) as f64)
        } else {
            None
        }
    }

    /// Get the sample standard deviation
    pub fn std_dev(&self) -> Option<f64> {
        self.variance().map(|v| v.sqrt())
    }

    /// Get the minimum value
    pub fn min(&self) -> Option<f64> {
        self.min
    }

    /// Get the maximum value
    pub fn max(&self) -> Option<f64> {
        self.max
    }
}

/// Implementation of Mergeable for parallel statistics computation
///
/// Uses Chan's parallel variance algorithm to correctly merge
/// running statistics from multiple threads.
///
/// Reference: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
impl Mergeable for RunningStats {
    fn merge(&mut self, other: Self) {
        if other.count == 0 {
            return; // Nothing to merge
        }
        if self.count == 0 {
            *self = other; // Replace with other
            return;
        }

        let total_count = self.count + other.count;
        let delta = other.mean - self.mean;

        // Merge min/max
        self.min = match (self.min, other.min) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (Some(a), None) => Some(a),
            (None, Some(b)) => Some(b),
            (None, None) => None,
        };

        self.max = match (self.max, other.max) {
            (Some(a), Some(b)) => Some(a.max(b)),
            (Some(a), None) => Some(a),
            (None, Some(b)) => Some(b),
            (None, None) => None,
        };

        // Merge mean and m2 using Chan's algorithm
        self.m2 += other.m2 + delta * delta * (self.count * other.count) as f64 / total_count as f64;
        self.mean = (self.mean * self.count as f64 + other.mean * other.count as f64)
            / total_count as f64;
        self.count = total_count;
    }
}

/// Accumulator for categorical data (counts by category)
///
/// Memory: O(k) where k is the number of unique categories
#[derive(Debug, Clone, Default)]
pub struct CategoryCounter<T: std::hash::Hash + Eq> {
    counts: HashMap<T, usize>,
    total: usize,
}

impl<T: std::hash::Hash + Eq> CategoryCounter<T> {
    /// Create a new category counter
    pub fn new() -> Self {
        Self {
            counts: HashMap::new(),
            total: 0,
        }
    }

    /// Increment count for a category
    pub fn increment(&mut self, category: T) {
        *self.counts.entry(category).or_insert(0) += 1;
        self.total += 1;
    }

    /// Increment count for a category by a specific amount
    pub fn increment_by(&mut self, category: T, amount: usize) {
        *self.counts.entry(category).or_insert(0) += amount;
        self.total += amount;
    }

    /// Get count for a specific category
    pub fn get(&self, category: &T) -> usize {
        self.counts.get(category).copied().unwrap_or(0)
    }

    /// Get total count across all categories
    pub fn total(&self) -> usize {
        self.total
    }

    /// Get the number of unique categories
    pub fn num_categories(&self) -> usize {
        self.counts.len()
    }

    /// Get frequency (proportion) for a category
    pub fn frequency(&self, category: &T) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            self.get(category) as f64 / self.total as f64
        }
    }

    /// Get all categories and their counts
    pub fn categories(&self) -> &HashMap<T, usize> {
        &self.counts
    }

    /// Iterate over (category, count) pairs
    pub fn iter(&self) -> impl Iterator<Item = (&T, &usize)> {
        self.counts.iter()
    }
}

/// Specialized implementation for String to allow efficient &str increments
impl CategoryCounter<String> {
    /// Optimized increment for &str - only allocates when category is new
    ///
    /// This is much more efficient than increment(category.to_string()) because
    /// it only allocates a String when inserting a new category, not on every call.
    ///
    /// For existing categories, this does a HashMap lookup with &str (no allocation),
    /// and only allocates a new String when the category is seen for the first time.
    pub fn increment_str(&mut self, category: &str) {
        // First try to increment existing entry (no allocation)
        if let Some(count) = self.counts.get_mut(category) {
            *count += 1;
        } else {
            // Only allocate String for new categories
            self.counts.insert(category.to_string(), 1);
        }
        self.total += 1;
    }
}

/// Implementation of Mergeable for parallel statistics computation
///
/// Merges category counts from multiple threads by summing counts
/// for each category.
impl<T: std::hash::Hash + Eq + Send> Mergeable for CategoryCounter<T> {
    fn merge(&mut self, other: Self) {
        for (category, count) in other.counts {
            self.increment_by(category, count);
        }
    }
}

/// Percentile calculator using reservoir sampling for memory efficiency
///
/// Stores up to `capacity` samples, then switches to reservoir sampling
/// for approximate percentile calculation.
#[derive(Debug, Clone)]
pub struct PercentileEstimator {
    samples: Vec<f64>,
    capacity: usize,
    total_seen: usize,
}

impl PercentileEstimator {
    /// Create a new percentile estimator with given capacity
    ///
    /// For exact percentiles, set capacity >= expected number of values.
    /// For approximate percentiles on large datasets, use smaller capacity (e.g., 10,000).
    pub fn new(capacity: usize) -> Self {
        Self {
            samples: Vec::with_capacity(capacity),
            capacity,
            total_seen: 0,
        }
    }

    /// Add a value to the estimator
    pub fn push(&mut self, value: f64) {
        self.total_seen += 1;

        if self.samples.len() < self.capacity {
            // Still filling buffer
            self.samples.push(value);
        } else {
            // Reservoir sampling: random replacement
            use std::collections::hash_map::RandomState;
            use std::hash::{BuildHasher, Hash, Hasher};

            let mut hasher = RandomState::new().build_hasher();
            self.total_seen.hash(&mut hasher);
            let random_index = (hasher.finish() as usize) % self.total_seen;

            if random_index < self.capacity {
                self.samples[random_index] = value;
            }
        }
    }

    /// Calculate percentile (0.0 to 1.0)
    ///
    /// Returns None if no samples have been added.
    pub fn percentile(&mut self, p: f64) -> Option<f64> {
        if self.samples.is_empty() {
            return None;
        }

        // Sort samples (required for percentile calculation)
        self.samples.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let index = (p * (self.samples.len() - 1) as f64) as usize;
        Some(self.samples[index])
    }

    /// Get the median (50th percentile)
    pub fn median(&mut self) -> Option<f64> {
        self.percentile(0.5)
    }

    /// Get the total number of values seen (not just stored)
    pub fn total_seen(&self) -> usize {
        self.total_seen
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_running_stats() {
        let mut stats = RunningStats::new();

        stats.push(10.0);
        stats.push(20.0);
        stats.push(30.0);

        assert_eq!(stats.count(), 3);
        assert_eq!(stats.mean(), Some(20.0));
        assert_eq!(stats.min(), Some(10.0));
        assert_eq!(stats.max(), Some(30.0));

        // Variance should be 100.0
        let var = stats.variance().unwrap();
        assert!((var - 100.0).abs() < 1e-10);

        // Std dev should be 10.0
        let std = stats.std_dev().unwrap();
        assert!((std - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_category_counter() {
        let mut counter = CategoryCounter::new();

        counter.increment("A");
        counter.increment("B");
        counter.increment("A");
        counter.increment("C");

        assert_eq!(counter.total(), 4);
        assert_eq!(counter.num_categories(), 3);
        assert_eq!(counter.get(&"A"), 2);
        assert_eq!(counter.get(&"B"), 1);
        assert_eq!(counter.frequency(&"A"), 0.5);
    }

    #[test]
    fn test_percentile_estimator() {
        let mut estimator = PercentileEstimator::new(100);

        for i in 1..=100 {
            estimator.push(i as f64);
        }

        assert_eq!(estimator.total_seen(), 100);

        let median = estimator.median().unwrap();
        assert!((median - 50.5).abs() < 1.0); // Approximate

        let p95 = estimator.percentile(0.95).unwrap();
        assert!(p95 > 90.0 && p95 <= 100.0);
    }

    #[test]
    fn test_running_stats_merge() {
        // Create two stats objects
        let mut stats1 = RunningStats::new();
        stats1.push(10.0);
        stats1.push(20.0);
        stats1.push(30.0);

        let mut stats2 = RunningStats::new();
        stats2.push(40.0);
        stats2.push(50.0);

        // Merge stats2 into stats1
        stats1.merge(stats2);

        // Should have combined statistics
        assert_eq!(stats1.count(), 5);
        assert_eq!(stats1.mean(), Some(30.0)); // (10+20+30+40+50)/5 = 30
        assert_eq!(stats1.min(), Some(10.0));
        assert_eq!(stats1.max(), Some(50.0));

        // Variance of [10, 20, 30, 40, 50] should be 250
        let var = stats1.variance().unwrap();
        assert!((var - 250.0).abs() < 1e-10);
    }

    #[test]
    fn test_running_stats_merge_empty() {
        let mut stats1 = RunningStats::new();
        stats1.push(10.0);

        let stats2 = RunningStats::new(); // Empty

        stats1.merge(stats2);
        assert_eq!(stats1.count(), 1);
        assert_eq!(stats1.mean(), Some(10.0));
    }

    #[test]
    fn test_category_counter_merge() {
        let mut counter1 = CategoryCounter::new();
        counter1.increment("A");
        counter1.increment("B");
        counter1.increment("A");

        let mut counter2 = CategoryCounter::new();
        counter2.increment("B");
        counter2.increment("C");
        counter2.increment("C");

        counter1.merge(counter2);

        assert_eq!(counter1.total(), 6);
        assert_eq!(counter1.get(&"A"), 2);
        assert_eq!(counter1.get(&"B"), 2);
        assert_eq!(counter1.get(&"C"), 2);
        assert_eq!(counter1.num_categories(), 3);
    }

    #[test]
    fn test_mergeable_merge_all() {
        let mut stats1 = RunningStats::new();
        stats1.push(10.0);

        let mut stats2 = RunningStats::new();
        stats2.push(20.0);

        let mut stats3 = RunningStats::new();
        stats3.push(30.0);

        let merged = RunningStats::merge_all(vec![stats1, stats2, stats3]).unwrap();

        assert_eq!(merged.count(), 3);
        assert_eq!(merged.mean(), Some(20.0));
    }
}