scdata 0.2.3

High-performance Rust library for constructing sparse single-cell UMI count data with deterministic MatrixMarket export
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
use std::collections::{HashMap, HashSet};

use crate::ambient_rna_detect::AmbientRnaDetect;
use crate::cell_data::GeneUmiHash;

use core::fmt;
use int_to_str::int_to_str::IntToStr;

/// Struct describing multimapping reads.
#[derive(Clone, Debug, Default)]
pub struct MultiMapper {
    pub features: HashSet<u64>,
    pub positions: Vec<usize>,
}

/// Cell-local feature store in a global feature space.
///
/// Logic:
/// - `seen` is the registry of already observed `(feature_id, umi)` pairs
/// - `total_reads` stores the accumulated per-feature value
/// - duplicate `(feature_id, umi)` pairs are rejected
#[derive(Clone, Debug, Default)]
pub struct CellData {
    pub name: u64,

    /// Registry of already seen feature_id + umi pairs.
    pub seen: HashSet<GeneUmiHash>,

    /// feature_id -> accumulated value / count
    pub total_reads: HashMap<u64, f32>,

    /// Multimapped sequences.
    pub multimapper: HashMap<u64, MultiMapper>,
}

impl fmt::Display for CellData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "CellData Summary")?;
        writeln!(f, "----------------")?;
        writeln!(f, "Cell Name (ID): {}", self.name)?;
        writeln!(f, "Total UMIs: {}", self.total_umis())?;
        writeln!(f, "Seen Feature/UMI Pairs: {}", self.seen.len())?;

        writeln!(f, "\nPer-Feature Totals:")?;
        for (feature_id, count) in &self.total_reads {
            writeln!(f, "  {} => {}", feature_id, count)?;
        }

        writeln!(f, "\nSeen Feature/UMI Registry:")?;
        for key in &self.seen {
            writeln!(f, "  {:?}", key)?;
        }

        writeln!(f, "\nMultimappers ({} entries):", self.multimapper.len())?;
        for (seq_id, mm) in &self.multimapper {
            writeln!(
                f,
                "  Seq {}: Features {:?}, Positions {:?}",
                seq_id, mm.features, mm.positions
            )?;
        }

        Ok(())
    }
}

impl CellData {
    /// Create an empty cell container.
    pub fn new(name: u64) -> Self {
        Self {
            name,
            ..Default::default()
        }
    }

    /// Split this cell into ambient and non-ambient fractions.
    ///
    /// The decision is made per seen feature/UMI observation.
    pub fn split_ambient(&self, ambient: &AmbientRnaDetect) -> (Self, Self) {
        let mut non_ambient = Self::new(self.name);
        let mut ambient_data = Self::new(self.name);

        for gh in &self.seen {
            if ambient.is_ambient(gh) {
                ambient_data.add(*gh);
            } else {
                non_ambient.add(*gh);
            }
        }

        (non_ambient, ambient_data)
    }

    /// Merge another cell into this one.
    ///
    /// Only previously unseen `(feature_id, umi)` pairs are inserted.
    pub fn merge(&mut self, other: &CellData) {
        for gh in &other.seen {
            if !self.seen.insert(*gh) {
                #[cfg(debug_assertions)]
                eprintln!(
                    "Warning: duplicate GeneUmiHash {:?} found during merge for cell {}",
                    gh, self.name
                );
                continue;
            }

            *self.total_reads.entry(gh.0).or_insert(0.0) += 1.0;
        }
    }

    /// Insert one unique feature/UMI observation.
    ///
    /// Returns `false` if this exact feature/UMI pair was already seen.
    pub fn add(&mut self, fh: GeneUmiHash) -> bool {
        if !self.seen.insert(fh) {
            #[cfg(debug_assertions)]
            eprintln!(
                "Warning: duplicate GeneUmiHash {:?} detected in cell {} — ignoring.",
                fh, self.name
            );
            return false;
        }

        *self.total_reads.entry(fh.0).or_insert(0.0) += 1.0;
        true
    }

    /// Insert one unique feature/UMI observation with a numeric value.
    ///
    /// Duplicate feature/UMI pairs are rejected.
    pub fn add_value(&mut self, fh: GeneUmiHash, value: f32) -> bool {
        if !self.seen.insert(fh) {
            #[cfg(debug_assertions)]
            eprintln!(
                "Warning: duplicate GeneUmiHash {:?} detected in cell {} — ignoring value insert.",
                fh, self.name
            );
            return false;
        }

        *self.total_reads.entry(fh.0).or_insert(0.0) += value;
        true
    }

    /// better API - Number of unique feature/UMI observations in this cell.
    pub fn total_umis(&self) -> usize {
        self.seen.len()
    }

    /// Accumulated value for one feature id.
    pub fn total_umis_4_gene_id(&self, feature_id: &u64) -> f32 {
        *self.total_reads.get(feature_id).unwrap_or(&0.0)
    }

    /// Mean value helper.
    ///
    /// In the current model this is just the total feature value divided
    /// by the number of unique UMIs seen for that feature.
    pub fn get_mean_for_gene(&self, feature_id: &u64) -> Option<f32> {
        let total = *self.total_reads.get(feature_id)?;
        let n = self.seen.iter().filter(|gh| gh.0 == *feature_id).count();

        if n > 0 { Some(total / n as f32) } else { None }
    }

    /// Dense row export helper.
    pub fn to_str_for_feature_ids(&self, feature_ids: &[u64], _length: usize) -> String {
        let mut data = Vec::<String>::with_capacity(feature_ids.len() + 4);

        data.push(IntToStr::u8_array_to_str(&self.name.to_le_bytes()).to_string());

        let mut total = 0.0f32;
        let mut max = 0.0f32;
        let mut max_id: Option<u64> = None;

        for id in feature_ids {
            let n = *self.total_reads.get(id).unwrap_or(&0.0);

            if n > max {
                max = n;
                max_id = Some(*id);
            }

            data.push(n.to_string());
            total += n;
        }

        let mut dist2max = f32::MAX;
        for id in feature_ids {
            let n = *self.total_reads.get(id).unwrap_or(&0.0);
            let diff = max - n;

            if diff > 0.0 && diff < dist2max {
                dist2max = diff;
            }
        }

        data.push(
            max_id
                .map(|id| id.to_string())
                .unwrap_or_else(|| "na".to_string()),
        );

        data.push(if total > 0.0 {
            (max / total).to_string()
        } else {
            "0".to_string()
        });

        data.push(total.to_string());

        data.push(if max > 0.0 && dist2max.is_finite() {
            (dist2max / max).to_string()
        } else {
            "0".to_string()
        });

        data.join("\t")
    }

    #[cfg(feature = "compare")]
    /// quick compare function
    pub fn compare(&self, other: &Self, label: &str, cell_id: u64) -> Result<(), String> {
        const EPS: f32 = 1e-6;

        if self.name != other.name {
            return Err(format!(
                "{label}: cell {cell_id} name differs: left={} right={}",
                self.name, other.name
            ));
        }

        if self.seen != other.seen {
            let only_left: Vec<_> = self
                .seen
                .difference(&other.seen)
                .take(10)
                .cloned()
                .collect();

            let only_right: Vec<_> = other
                .seen
                .difference(&self.seen)
                .take(10)
                .cloned()
                .collect();

            return Err(format!(
                "{label}: cell {cell_id} distinct molecule count differs: left={} right={}
  sample only-in-left (up to 10): {:?}
  sample only-in-right (up to 10): {:?}",
                self.seen.len(),
                other.seen.len(),
                only_left,
                only_right
            ));
        }

        if self.total_reads.len() != other.total_reads.len() {
            let only_left: Vec<_> = self
                .total_reads
                .keys()
                .filter(|k| !other.total_reads.contains_key(*k))
                .take(10)
                .copied()
                .collect();

            let only_right: Vec<_> = other
                .total_reads
                .keys()
                .filter(|k| !self.total_reads.contains_key(*k))
                .take(10)
                .copied()
                .collect();

            return Err(format!(
                "{label}: cell {cell_id} total_reads feature count differs: left={} right={}
  sample only-in-left: {:?}
  sample only-in-right: {:?}",
                self.total_reads.len(),
                other.total_reads.len(),
                only_left,
                only_right
            ));
        }
        for (feature_id, left_value) in &self.total_reads {
            let Some(right_value) = other.total_reads.get(feature_id) else {
                return Err(format!(
                    "{label}: cell {cell_id} feature {feature_id} exists on left but not right"
                ));
            };

            if (*left_value - *right_value).abs() > EPS {
                return Err(format!(
                    "{label}: cell {cell_id} feature {feature_id} differs: left={left_value} right={right_value}"
                ));
            }
        }

        for feature_id in other.total_reads.keys() {
            if !self.total_reads.contains_key(feature_id) {
                return Err(format!(
                    "{label}: cell {cell_id} feature {feature_id} exists on right but not left"
                ));
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_new_cell() {
        let cell = CellData::new(42);

        assert_eq!(cell.name, 42);
        assert_eq!(cell.total_umis(), 0);
        assert!(cell.seen.is_empty());
        assert!(cell.total_reads.is_empty());
    }

    #[test]
    fn test_add_unique_feature_umi() {
        let mut cell = CellData::new(1);

        let fh = GeneUmiHash(10, 123);
        let inserted = cell.add(fh);

        assert!(inserted);
        assert_eq!(cell.total_umis(), 1);
        assert!(cell.seen.contains(&fh));
        assert_eq!(cell.total_reads.get(&10), Some(&1.0));
    }

    #[test]
    fn test_add_duplicate_rejected() {
        let mut cell = CellData::new(1);

        let fh = GeneUmiHash(10, 123);

        assert!(cell.add(fh));
        assert!(!cell.add(fh));

        assert_eq!(cell.total_umis(), 1);
        assert_eq!(cell.total_reads.get(&10), Some(&1.0));
    }

    #[test]
    fn test_add_value_new_only() {
        let mut cell = CellData::new(1);

        let fh = GeneUmiHash(5, 111);

        assert!(cell.add_value(fh, 2.0));
        assert!(!cell.add_value(fh, 3.0));

        assert!(cell.seen.contains(&fh));
        assert_eq!(cell.total_reads.get(&5), Some(&2.0));
        assert_eq!(cell.total_umis(), 1);
    }

    #[test]
    fn test_total_umis() {
        let mut cell = CellData::new(1);

        cell.add(GeneUmiHash(1, 10));
        cell.add(GeneUmiHash(1, 11));
        cell.add(GeneUmiHash(2, 12));

        assert_eq!(cell.total_umis(), 3);
    }

    #[test]
    fn test_merge_cells() {
        let mut a = CellData::new(1);
        let mut b = CellData::new(1);

        let fh1 = GeneUmiHash(1, 10);
        let fh2 = GeneUmiHash(2, 20);

        a.add(fh1);
        b.add(fh2);

        a.merge(&b);

        assert_eq!(a.total_umis(), 2);
        assert!(a.seen.contains(&fh1));
        assert!(a.seen.contains(&fh2));
        assert_eq!(a.total_reads.get(&1), Some(&1.0));
        assert_eq!(a.total_reads.get(&2), Some(&1.0));
    }

    #[test]
    fn test_total_umis_for_feature() {
        let mut cell = CellData::new(1);

        cell.add(GeneUmiHash(5, 1));
        cell.add(GeneUmiHash(5, 2));
        cell.add(GeneUmiHash(3, 1));

        assert_eq!(cell.total_umis_4_gene_id(&5), 2.0);
        assert_eq!(cell.total_umis_4_gene_id(&3), 1.0);
        assert_eq!(cell.total_umis_4_gene_id(&9), 0.0);
    }

    #[test]
    fn test_mean_for_feature() {
        let mut cell = CellData::new(1);

        cell.add_value(GeneUmiHash(1, 1), 2.0);
        cell.add_value(GeneUmiHash(1, 2), 4.0);

        let mean = cell.get_mean_for_gene(&1).unwrap();

        assert!((mean - 3.0).abs() < 1e-6);
    }
}