cyanea-omics 0.1.0

Omics data structures for the Cyanea bioinformatics ecosystem
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
//! AnnData-like container for single-cell omics data.
//!
//! Provides an in-memory representation inspired by the Python AnnData format,
//! the standard data structure in the scverse ecosystem (scanpy, scvi-tools).
//!
//! # Structure
//!
//! - `X` — primary data matrix (cells × genes), dense or sparse
//! - `obs` — per-cell metadata (string key-value pairs)
//! - `var` — per-gene metadata
//! - `obsm` / `varm` — multi-dimensional annotations (e.g. PCA, UMAP embeddings)
//! - `layers` — alternative data matrices (e.g. raw counts, normalized)
//!
//! # Example
//!
//! ```
//! use cyanea_omics::single_cell::{AnnData, MatrixData};
//!
//! let x = MatrixData::Dense(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
//! let adata = AnnData::new(
//!     x,
//!     vec!["cell_1".into(), "cell_2".into()],
//!     vec!["gene_a".into(), "gene_b".into()],
//! ).unwrap();
//! assert_eq!(adata.n_obs(), 2);
//! assert_eq!(adata.n_vars(), 2);
//! ```

use std::collections::HashMap;

use cyanea_core::{CyaneaError, Result, Summarizable};

use crate::sparse::SparseMatrix;

/// The primary data matrix, either dense or sparse.
#[derive(Debug, Clone)]
pub enum MatrixData {
    /// Dense row-major matrix (n_obs × n_vars).
    Dense(Vec<Vec<f64>>),
    /// Sparse COO matrix.
    Sparse(SparseMatrix),
}

impl MatrixData {
    /// (n_obs, n_vars).
    pub fn shape(&self) -> (usize, usize) {
        match self {
            MatrixData::Dense(rows) => {
                let n_obs = rows.len();
                let n_vars = rows.first().map_or(0, |r| r.len());
                (n_obs, n_vars)
            }
            MatrixData::Sparse(s) => s.shape(),
        }
    }

    /// Get a value at (obs_idx, var_idx).
    pub fn get(&self, obs: usize, var: usize) -> f64 {
        match self {
            MatrixData::Dense(rows) => {
                rows.get(obs).and_then(|r| r.get(var)).copied().unwrap_or(0.0)
            }
            MatrixData::Sparse(s) => s.get(obs, var),
        }
    }

    /// Set a value at (obs_idx, var_idx).
    ///
    /// For dense matrices, sets directly. For sparse matrices, inserts a triplet
    /// (does not deduplicate — the last-inserted value wins on `get()`).
    pub fn set(&mut self, obs: usize, var: usize, val: f64) {
        match self {
            MatrixData::Dense(rows) => {
                if let Some(row) = rows.get_mut(obs) {
                    if let Some(cell) = row.get_mut(var) {
                        *cell = val;
                    }
                }
            }
            MatrixData::Sparse(s) => {
                let _ = s.insert(obs, var, val);
            }
        }
    }

    /// Sum of values in each column.
    pub fn column_sums(&self) -> Vec<f64> {
        match self {
            MatrixData::Dense(rows) => {
                let n_vars = rows.first().map_or(0, |r| r.len());
                let mut sums = vec![0.0; n_vars];
                for row in rows {
                    for (j, &v) in row.iter().enumerate() {
                        sums[j] += v;
                    }
                }
                sums
            }
            MatrixData::Sparse(s) => s.column_sums(),
        }
    }

    /// Mean value of each column.
    pub fn column_means(&self) -> Vec<f64> {
        match self {
            MatrixData::Dense(rows) => {
                let n_obs = rows.len();
                if n_obs == 0 {
                    return vec![];
                }
                let sums = self.column_sums();
                let n = n_obs as f64;
                sums.into_iter().map(|s| s / n).collect()
            }
            MatrixData::Sparse(s) => s.column_means(),
        }
    }

    /// Sum of values in each row.
    pub fn row_sums(&self) -> Vec<f64> {
        match self {
            MatrixData::Dense(rows) => rows.iter().map(|r| r.iter().sum()).collect(),
            MatrixData::Sparse(s) => s.row_sums(),
        }
    }

    /// Flatten to a row-major `Vec<f64>` for interop with cyanea-stats/ml.
    pub fn to_flat_row_major(&self) -> Vec<f64> {
        let (n_obs, n_vars) = self.shape();
        match self {
            MatrixData::Dense(rows) => {
                let mut flat = Vec::with_capacity(n_obs * n_vars);
                for row in rows {
                    flat.extend_from_slice(row);
                }
                flat
            }
            MatrixData::Sparse(s) => {
                let mut flat = vec![0.0; n_obs * n_vars];
                for (r, c, v) in s.iter() {
                    flat[r * n_vars + c] = v;
                }
                flat
            }
        }
    }
}

/// A metadata column with typed data.
///
/// Supports string, numeric, and categorical columns as found in `.h5ad` files.
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnData {
    /// Free-text string values.
    Strings(Vec<String>),
    /// Numeric (f64) values.
    Numeric(Vec<f64>),
    /// Categorical data stored as integer codes indexing into a category list.
    Categorical {
        codes: Vec<i32>,
        categories: Vec<String>,
    },
}

impl ColumnData {
    /// Number of elements in this column.
    pub fn len(&self) -> usize {
        match self {
            ColumnData::Strings(v) => v.len(),
            ColumnData::Numeric(v) => v.len(),
            ColumnData::Categorical { codes, .. } => codes.len(),
        }
    }

    /// Whether the column is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Try to get as string slice. Returns `None` if not `Strings` variant.
    pub fn as_strings(&self) -> Option<&Vec<String>> {
        match self {
            ColumnData::Strings(v) => Some(v),
            _ => None,
        }
    }

    /// Try to get as numeric slice. Returns `None` if not `Numeric` variant.
    pub fn as_numeric(&self) -> Option<&Vec<f64>> {
        match self {
            ColumnData::Numeric(v) => Some(v),
            _ => None,
        }
    }

    /// Subset to the given indices.
    fn subset(&self, indices: &[usize]) -> Self {
        match self {
            ColumnData::Strings(v) => {
                ColumnData::Strings(indices.iter().map(|&i| v[i].clone()).collect())
            }
            ColumnData::Numeric(v) => {
                ColumnData::Numeric(indices.iter().map(|&i| v[i]).collect())
            }
            ColumnData::Categorical { codes, categories } => ColumnData::Categorical {
                codes: indices.iter().map(|&i| codes[i]).collect(),
                categories: categories.clone(),
            },
        }
    }
}

/// Per-cell or per-gene quality control metrics.
#[derive(Debug, Clone)]
pub struct QcMetrics {
    /// Total counts per observation.
    pub total_counts: Vec<f64>,
    /// Number of non-zero features per observation.
    pub n_features: Vec<usize>,
}

/// AnnData-like container for single-cell data.
#[derive(Debug, Clone)]
pub struct AnnData {
    /// Primary data matrix (n_obs × n_vars).
    x: MatrixData,
    /// Observation (cell) names.
    obs_names: Vec<String>,
    /// Variable (gene) names.
    var_names: Vec<String>,
    /// Per-cell metadata.
    obs: HashMap<String, ColumnData>,
    /// Per-gene metadata.
    var: HashMap<String, ColumnData>,
    /// Multi-dimensional observation annotations (e.g. PCA embeddings).
    obsm: HashMap<String, Vec<Vec<f64>>>,
    /// Multi-dimensional variable annotations.
    varm: HashMap<String, Vec<Vec<f64>>>,
    /// Alternative data layers (same shape as X).
    layers: HashMap<String, MatrixData>,
    /// Pairwise observation annotations (e.g. kNN graphs, distance matrices).
    obsp: HashMap<String, SparseMatrix>,
    /// Unstructured metadata (free-form key-value pairs).
    uns: HashMap<String, String>,
}

impl AnnData {
    /// Create a new AnnData container.
    ///
    /// # Errors
    ///
    /// Returns an error if the matrix dimensions don't match the name vectors.
    pub fn new(
        x: MatrixData,
        obs_names: Vec<String>,
        var_names: Vec<String>,
    ) -> Result<Self> {
        let (n_obs, n_vars) = x.shape();
        if obs_names.len() != n_obs {
            return Err(CyaneaError::InvalidInput(format!(
                "obs_names length ({}) does not match n_obs ({})",
                obs_names.len(),
                n_obs
            )));
        }
        if var_names.len() != n_vars {
            return Err(CyaneaError::InvalidInput(format!(
                "var_names length ({}) does not match n_vars ({})",
                var_names.len(),
                n_vars
            )));
        }

        Ok(Self {
            x,
            obs_names,
            var_names,
            obs: HashMap::new(),
            var: HashMap::new(),
            obsm: HashMap::new(),
            varm: HashMap::new(),
            layers: HashMap::new(),
            obsp: HashMap::new(),
            uns: HashMap::new(),
        })
    }

    /// Number of observations (cells).
    pub fn n_obs(&self) -> usize {
        self.obs_names.len()
    }

    /// Number of variables (genes).
    pub fn n_vars(&self) -> usize {
        self.var_names.len()
    }

    /// Shape of the primary data matrix.
    pub fn shape(&self) -> (usize, usize) {
        self.x.shape()
    }

    /// Access the primary data matrix.
    pub fn x(&self) -> &MatrixData {
        &self.x
    }

    /// Observation names.
    pub fn obs_names(&self) -> &[String] {
        &self.obs_names
    }

    /// Variable names.
    pub fn var_names(&self) -> &[String] {
        &self.var_names
    }

    /// Add a per-cell string metadata column.
    pub fn add_obs(&mut self, key: &str, values: Vec<String>) -> Result<()> {
        self.add_obs_column(key, ColumnData::Strings(values))
    }

    /// Add a per-cell numeric metadata column.
    pub fn add_obs_numeric(&mut self, key: &str, values: Vec<f64>) -> Result<()> {
        self.add_obs_column(key, ColumnData::Numeric(values))
    }

    /// Add a per-cell metadata column of any type.
    pub fn add_obs_column(&mut self, key: &str, data: ColumnData) -> Result<()> {
        if data.len() != self.n_obs() {
            return Err(CyaneaError::InvalidInput(format!(
                "obs '{}' length ({}) does not match n_obs ({})",
                key,
                data.len(),
                self.n_obs()
            )));
        }
        self.obs.insert(key.to_string(), data);
        Ok(())
    }

    /// Get per-cell metadata column as typed data.
    pub fn get_obs(&self, key: &str) -> Option<&ColumnData> {
        self.obs.get(key)
    }

    /// Get per-cell metadata column as strings (convenience for backward compat).
    pub fn get_obs_strings(&self, key: &str) -> Option<&Vec<String>> {
        self.obs.get(key).and_then(|c| c.as_strings())
    }

    /// All observation metadata columns.
    pub fn obs_columns(&self) -> &HashMap<String, ColumnData> {
        &self.obs
    }

    /// Add a per-gene string metadata column.
    pub fn add_var(&mut self, key: &str, values: Vec<String>) -> Result<()> {
        self.add_var_column(key, ColumnData::Strings(values))
    }

    /// Add a per-gene numeric metadata column.
    pub fn add_var_numeric(&mut self, key: &str, values: Vec<f64>) -> Result<()> {
        self.add_var_column(key, ColumnData::Numeric(values))
    }

    /// Add a per-gene metadata column of any type.
    pub fn add_var_column(&mut self, key: &str, data: ColumnData) -> Result<()> {
        if data.len() != self.n_vars() {
            return Err(CyaneaError::InvalidInput(format!(
                "var '{}' length ({}) does not match n_vars ({})",
                key,
                data.len(),
                self.n_vars()
            )));
        }
        self.var.insert(key.to_string(), data);
        Ok(())
    }

    /// Get per-gene metadata column as typed data.
    pub fn get_var(&self, key: &str) -> Option<&ColumnData> {
        self.var.get(key)
    }

    /// Get per-gene metadata column as strings (convenience for backward compat).
    pub fn get_var_strings(&self, key: &str) -> Option<&Vec<String>> {
        self.var.get(key).and_then(|c| c.as_strings())
    }

    /// All variable metadata columns.
    pub fn var_columns(&self) -> &HashMap<String, ColumnData> {
        &self.var
    }

    /// Add a multi-dimensional observation annotation (e.g. PCA embedding).
    pub fn add_obsm(&mut self, key: &str, data: Vec<Vec<f64>>) -> Result<()> {
        if data.len() != self.n_obs() {
            return Err(CyaneaError::InvalidInput(format!(
                "obsm '{}' length ({}) does not match n_obs ({})",
                key,
                data.len(),
                self.n_obs()
            )));
        }
        self.obsm.insert(key.to_string(), data);
        Ok(())
    }

    /// Get a multi-dimensional observation annotation.
    pub fn get_obsm(&self, key: &str) -> Option<&Vec<Vec<f64>>> {
        self.obsm.get(key)
    }

    /// Add a multi-dimensional variable annotation.
    pub fn add_varm(&mut self, key: &str, data: Vec<Vec<f64>>) -> Result<()> {
        if data.len() != self.n_vars() {
            return Err(CyaneaError::InvalidInput(format!(
                "varm '{}' length ({}) does not match n_vars ({})",
                key,
                data.len(),
                self.n_vars()
            )));
        }
        self.varm.insert(key.to_string(), data);
        Ok(())
    }

    /// Get a multi-dimensional variable annotation.
    pub fn get_varm(&self, key: &str) -> Option<&Vec<Vec<f64>>> {
        self.varm.get(key)
    }

    /// Add an alternative data layer.
    pub fn add_layer(&mut self, key: &str, layer: MatrixData) -> Result<()> {
        let (n_obs, n_vars) = layer.shape();
        if n_obs != self.n_obs() || n_vars != self.n_vars() {
            return Err(CyaneaError::InvalidInput(format!(
                "layer '{}' shape ({}, {}) does not match ({}, {})",
                key,
                n_obs,
                n_vars,
                self.n_obs(),
                self.n_vars()
            )));
        }
        self.layers.insert(key.to_string(), layer);
        Ok(())
    }

    /// Get an alternative data layer.
    pub fn get_layer(&self, key: &str) -> Option<&MatrixData> {
        self.layers.get(key)
    }

    /// All observation multi-dimensional annotations.
    pub fn obsm_keys(&self) -> &HashMap<String, Vec<Vec<f64>>> {
        &self.obsm
    }

    /// All variable multi-dimensional annotations.
    pub fn varm_keys(&self) -> &HashMap<String, Vec<Vec<f64>>> {
        &self.varm
    }

    /// All alternative data layers.
    pub fn layers_keys(&self) -> &HashMap<String, MatrixData> {
        &self.layers
    }

    /// Mutable access to the primary data matrix.
    pub fn x_mut(&mut self) -> &mut MatrixData {
        &mut self.x
    }

    /// Replace the primary data matrix. The new matrix must have the same shape.
    pub fn set_x(&mut self, new_x: MatrixData) -> Result<()> {
        let (n_obs, n_vars) = new_x.shape();
        if n_obs != self.n_obs() || n_vars != self.n_vars() {
            return Err(CyaneaError::InvalidInput(format!(
                "new X shape ({}, {}) does not match ({}, {})",
                n_obs, n_vars, self.n_obs(), self.n_vars()
            )));
        }
        self.x = new_x;
        Ok(())
    }

    /// Subset to the given variable (gene) indices.
    pub fn subset_vars(&self, indices: &[usize]) -> Result<AnnData> {
        for &i in indices {
            if i >= self.n_vars() {
                return Err(CyaneaError::InvalidInput(format!(
                    "var index {} out of bounds (n_vars={})",
                    i, self.n_vars()
                )));
            }
        }

        let x = subset_matrix_cols(&self.x, indices, self.n_obs());
        let var_names: Vec<String> = indices.iter().map(|&i| self.var_names[i].clone()).collect();

        let mut adata = AnnData::new(x, self.obs_names.clone(), var_names)?;

        // Copy obs metadata
        adata.obs = self.obs.clone();
        // Subset var metadata
        for (key, col) in &self.var {
            adata.var.insert(key.clone(), col.subset(indices));
        }
        // Copy obsm/varm
        adata.obsm = self.obsm.clone();
        // Subset layers
        for (key, layer) in &self.layers {
            let sub = subset_matrix_cols(layer, indices, self.n_obs());
            adata.layers.insert(key.clone(), sub);
        }
        // Copy obsp and uns
        adata.obsp = self.obsp.clone();
        adata.uns = self.uns.clone();

        Ok(adata)
    }

    /// Add a pairwise observation annotation (e.g. kNN graph).
    ///
    /// The matrix must be n_obs × n_obs.
    pub fn add_obsp(&mut self, key: &str, matrix: SparseMatrix) -> Result<()> {
        let (r, c) = matrix.shape();
        if r != self.n_obs() || c != self.n_obs() {
            return Err(CyaneaError::InvalidInput(format!(
                "obsp '{}' shape ({}, {}) does not match n_obs ({})",
                key, r, c, self.n_obs()
            )));
        }
        self.obsp.insert(key.to_string(), matrix);
        Ok(())
    }

    /// Get a pairwise observation annotation.
    pub fn get_obsp(&self, key: &str) -> Option<&SparseMatrix> {
        self.obsp.get(key)
    }

    /// Add unstructured metadata.
    pub fn add_uns(&mut self, key: &str, value: String) {
        self.uns.insert(key.to_string(), value);
    }

    /// Get unstructured metadata.
    pub fn get_uns(&self, key: &str) -> Option<&str> {
        self.uns.get(key).map(|s| s.as_str())
    }

    /// All pairwise observation annotation keys.
    pub fn obsp_keys(&self) -> &HashMap<String, SparseMatrix> {
        &self.obsp
    }

    /// All unstructured metadata.
    pub fn uns_keys(&self) -> &HashMap<String, String> {
        &self.uns
    }

    /// Mutable access to a layer.
    pub fn get_layer_mut(&mut self, key: &str) -> Option<&mut MatrixData> {
        self.layers.get_mut(key)
    }

    /// Subset to the given observation indices.
    pub fn subset_obs(&self, indices: &[usize]) -> Result<AnnData> {
        for &i in indices {
            if i >= self.n_obs() {
                return Err(CyaneaError::InvalidInput(format!(
                    "obs index {} out of bounds (n_obs={})",
                    i,
                    self.n_obs()
                )));
            }
        }

        let x = subset_matrix_rows(&self.x, indices, self.n_vars());
        let obs_names: Vec<String> = indices.iter().map(|&i| self.obs_names[i].clone()).collect();

        let mut adata = AnnData::new(x, obs_names, self.var_names.clone())?;

        // Subset obs metadata
        for (key, col) in &self.obs {
            adata.obs.insert(key.clone(), col.subset(indices));
        }
        // Copy var metadata
        adata.var = self.var.clone();

        // Subset obsm
        for (key, data) in &self.obsm {
            let sub: Vec<Vec<f64>> = indices.iter().map(|&i| data[i].clone()).collect();
            adata.obsm.insert(key.clone(), sub);
        }
        adata.varm = self.varm.clone();
        adata.uns = self.uns.clone();

        Ok(adata)
    }

    /// Compute basic QC metrics.
    pub fn qc_metrics(&self) -> QcMetrics {
        let n = self.n_obs();
        let p = self.n_vars();
        let mut total_counts = vec![0.0; n];
        let mut n_features = vec![0usize; n];

        for i in 0..n {
            for j in 0..p {
                let v = self.x.get(i, j);
                total_counts[i] += v;
                if v > 0.0 {
                    n_features[i] += 1;
                }
            }
        }

        QcMetrics {
            total_counts,
            n_features,
        }
    }
}

fn subset_matrix_cols(x: &MatrixData, col_indices: &[usize], n_obs: usize) -> MatrixData {
    match x {
        MatrixData::Dense(rows) => {
            let sub: Vec<Vec<f64>> = rows
                .iter()
                .map(|row| col_indices.iter().map(|&j| row[j]).collect())
                .collect();
            MatrixData::Dense(sub)
        }
        MatrixData::Sparse(s) => {
            let n_new_cols = col_indices.len();
            let mut new_s = SparseMatrix::new(n_obs, n_new_cols);
            // Build reverse map: old_col -> new_col
            let mut col_map = HashMap::new();
            for (new_j, &old_j) in col_indices.iter().enumerate() {
                col_map.insert(old_j, new_j);
            }
            for (r, c, v) in s.iter() {
                if let Some(&new_c) = col_map.get(&c) {
                    let _ = new_s.insert(r, new_c, v);
                }
            }
            MatrixData::Sparse(new_s)
        }
    }
}

fn subset_matrix_rows(x: &MatrixData, indices: &[usize], n_vars: usize) -> MatrixData {
    match x {
        MatrixData::Dense(rows) => {
            let sub: Vec<Vec<f64>> = indices.iter().map(|&i| rows[i].clone()).collect();
            MatrixData::Dense(sub)
        }
        MatrixData::Sparse(s) => {
            let n_new = indices.len();
            let mut new_s = SparseMatrix::new(n_new, n_vars);
            for (new_row, &old_row) in indices.iter().enumerate() {
                for j in 0..n_vars {
                    let v = s.get(old_row, j);
                    if v != 0.0 {
                        let _ = new_s.insert(new_row, j, v);
                    }
                }
            }
            MatrixData::Sparse(new_s)
        }
    }
}

impl Summarizable for AnnData {
    fn summary(&self) -> String {
        format!(
            "AnnData: {} obs \u{00d7} {} vars, {} layers, {} obsm, {} varm, {} obsp, {} uns",
            self.n_obs(),
            self.n_vars(),
            self.layers.len(),
            self.obsm.len(),
            self.varm.len(),
            self.obsp.len(),
            self.uns.len(),
        )
    }
}

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

    fn sample_adata() -> AnnData {
        let x = MatrixData::Dense(vec![
            vec![1.0, 2.0, 0.0],
            vec![3.0, 0.0, 4.0],
            vec![0.0, 5.0, 6.0],
        ]);
        AnnData::new(
            x,
            vec!["cell_1".into(), "cell_2".into(), "cell_3".into()],
            vec!["gene_a".into(), "gene_b".into(), "gene_c".into()],
        )
        .unwrap()
    }

    #[test]
    fn basic_construction() {
        let adata = sample_adata();
        assert_eq!(adata.n_obs(), 3);
        assert_eq!(adata.n_vars(), 3);
        assert_eq!(adata.shape(), (3, 3));
    }

    #[test]
    fn dimension_mismatch_error() {
        let x = MatrixData::Dense(vec![vec![1.0, 2.0]]);
        let result = AnnData::new(
            x,
            vec!["cell_1".into(), "cell_2".into()], // 2 names, 1 row
            vec!["gene_a".into(), "gene_b".into()],
        );
        assert!(result.is_err());
    }

    #[test]
    fn obs_metadata() {
        let mut adata = sample_adata();
        adata
            .add_obs(
                "cell_type",
                vec!["T-cell".into(), "B-cell".into(), "NK".into()],
            )
            .unwrap();
        let ct = adata.get_obs_strings("cell_type").unwrap();
        assert_eq!(ct[0], "T-cell");
        assert!(adata.get_obs("missing").is_none());
    }

    #[test]
    fn obs_metadata_length_mismatch() {
        let mut adata = sample_adata();
        let result = adata.add_obs("bad", vec!["a".into()]);
        assert!(result.is_err());
    }

    #[test]
    fn var_metadata() {
        let mut adata = sample_adata();
        adata
            .add_var(
                "gene_type",
                vec!["coding".into(), "coding".into(), "lncRNA".into()],
            )
            .unwrap();
        let gt = adata.get_var_strings("gene_type").unwrap();
        assert_eq!(gt[2], "lncRNA");
    }

    #[test]
    fn obsm_embedding() {
        let mut adata = sample_adata();
        let pca = vec![vec![0.1, 0.2], vec![0.3, 0.4], vec![0.5, 0.6]];
        adata.add_obsm("X_pca", pca).unwrap();
        let emb = adata.get_obsm("X_pca").unwrap();
        assert_eq!(emb.len(), 3);
        assert_eq!(emb[0], vec![0.1, 0.2]);
    }

    #[test]
    fn layers() {
        let mut adata = sample_adata();
        let raw = MatrixData::Dense(vec![
            vec![10.0, 20.0, 0.0],
            vec![30.0, 0.0, 40.0],
            vec![0.0, 50.0, 60.0],
        ]);
        adata.add_layer("raw_counts", raw).unwrap();
        let layer = adata.get_layer("raw_counts").unwrap();
        assert_eq!(layer.get(0, 0), 10.0);
    }

    #[test]
    fn layer_shape_mismatch() {
        let mut adata = sample_adata();
        let bad = MatrixData::Dense(vec![vec![1.0]]);
        assert!(adata.add_layer("bad", bad).is_err());
    }

    #[test]
    fn subset_obs() {
        let mut adata = sample_adata();
        adata
            .add_obs(
                "label",
                vec!["a".into(), "b".into(), "c".into()],
            )
            .unwrap();
        let sub = adata.subset_obs(&[0, 2]).unwrap();
        assert_eq!(sub.n_obs(), 2);
        assert_eq!(sub.n_vars(), 3);
        assert_eq!(sub.obs_names(), &["cell_1", "cell_3"]);
        let labels = sub.get_obs_strings("label").unwrap();
        assert_eq!(labels, &["a", "c"]);
    }

    #[test]
    fn qc_metrics() {
        let adata = sample_adata();
        let qc = adata.qc_metrics();
        assert_eq!(qc.total_counts, vec![3.0, 7.0, 11.0]);
        assert_eq!(qc.n_features, vec![2, 2, 2]);
    }

    #[test]
    fn sparse_x() {
        let s = SparseMatrix::from_triplets(
            vec![0, 1],
            vec![0, 1],
            vec![5.0, 10.0],
            2,
            2,
        )
        .unwrap();
        let x = MatrixData::Sparse(s);
        let adata = AnnData::new(
            x,
            vec!["c1".into(), "c2".into()],
            vec!["g1".into(), "g2".into()],
        )
        .unwrap();
        assert_eq!(adata.x().get(0, 0), 5.0);
        assert_eq!(adata.x().get(0, 1), 0.0);
    }

    #[test]
    fn summary_format() {
        let adata = sample_adata();
        let s = adata.summary();
        assert!(s.contains("3 obs"));
        assert!(s.contains("3 vars"));
        assert!(s.contains("0 obsp"));
        assert!(s.contains("0 uns"));
    }

    #[test]
    fn matrix_data_set_dense() {
        let mut x = MatrixData::Dense(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
        x.set(0, 1, 99.0);
        assert_eq!(x.get(0, 1), 99.0);
        assert_eq!(x.get(0, 0), 1.0);
    }

    #[test]
    fn matrix_data_set_sparse() {
        let s = SparseMatrix::new(2, 2);
        let mut x = MatrixData::Sparse(s);
        x.set(0, 1, 5.0);
        assert_eq!(x.get(0, 1), 5.0);
    }

    #[test]
    fn matrix_data_column_sums_dense() {
        let x = MatrixData::Dense(vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
        ]);
        assert_eq!(x.column_sums(), vec![5.0, 7.0, 9.0]);
    }

    #[test]
    fn matrix_data_column_means_dense() {
        let x = MatrixData::Dense(vec![
            vec![2.0, 4.0],
            vec![6.0, 8.0],
        ]);
        let means = x.column_means();
        assert!((means[0] - 4.0).abs() < 1e-10);
        assert!((means[1] - 6.0).abs() < 1e-10);
    }

    #[test]
    fn matrix_data_row_sums_dense() {
        let x = MatrixData::Dense(vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
        ]);
        assert_eq!(x.row_sums(), vec![6.0, 15.0]);
    }

    #[test]
    fn matrix_data_to_flat_row_major_dense() {
        let x = MatrixData::Dense(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
        assert_eq!(x.to_flat_row_major(), vec![1.0, 2.0, 3.0, 4.0]);
    }

    #[test]
    fn matrix_data_to_flat_row_major_sparse() {
        let s = SparseMatrix::from_triplets(
            vec![0, 1],
            vec![1, 0],
            vec![2.0, 3.0],
            2,
            2,
        )
        .unwrap();
        let x = MatrixData::Sparse(s);
        assert_eq!(x.to_flat_row_major(), vec![0.0, 2.0, 3.0, 0.0]);
    }

    #[test]
    fn x_mut_modify() {
        let mut adata = sample_adata();
        adata.x_mut().set(0, 0, 42.0);
        assert_eq!(adata.x().get(0, 0), 42.0);
    }

    #[test]
    fn set_x_valid() {
        let mut adata = sample_adata();
        let new_x = MatrixData::Dense(vec![
            vec![10.0, 20.0, 30.0],
            vec![40.0, 50.0, 60.0],
            vec![70.0, 80.0, 90.0],
        ]);
        adata.set_x(new_x).unwrap();
        assert_eq!(adata.x().get(0, 0), 10.0);
    }

    #[test]
    fn set_x_shape_mismatch() {
        let mut adata = sample_adata();
        let bad = MatrixData::Dense(vec![vec![1.0]]);
        assert!(adata.set_x(bad).is_err());
    }

    #[test]
    fn subset_vars_basic() {
        let mut adata = sample_adata();
        adata.add_var("type", vec!["a".into(), "b".into(), "c".into()]).unwrap();
        let sub = adata.subset_vars(&[0, 2]).unwrap();
        assert_eq!(sub.n_vars(), 2);
        assert_eq!(sub.n_obs(), 3);
        assert_eq!(sub.var_names(), &["gene_a", "gene_c"]);
        assert_eq!(sub.x().get(0, 0), 1.0);
        assert_eq!(sub.x().get(0, 1), 0.0); // was gene_c at col 2
        let types = sub.get_var_strings("type").unwrap();
        assert_eq!(types, &["a", "c"]);
    }

    #[test]
    fn subset_vars_out_of_bounds() {
        let adata = sample_adata();
        assert!(adata.subset_vars(&[0, 10]).is_err());
    }

    #[test]
    fn obsp_add_get() {
        let mut adata = sample_adata();
        let mut m = SparseMatrix::new(3, 3);
        m.insert(0, 1, 0.5).unwrap();
        m.insert(1, 2, 0.3).unwrap();
        adata.add_obsp("connectivities", m).unwrap();
        let conn = adata.get_obsp("connectivities").unwrap();
        assert_eq!(conn.get(0, 1), 0.5);
        assert!(adata.get_obsp("missing").is_none());
    }

    #[test]
    fn obsp_wrong_shape() {
        let mut adata = sample_adata();
        let m = SparseMatrix::new(2, 2); // 3x3 needed
        assert!(adata.add_obsp("bad", m).is_err());
    }

    #[test]
    fn uns_add_get() {
        let mut adata = sample_adata();
        adata.add_uns("method", "leiden".into());
        assert_eq!(adata.get_uns("method"), Some("leiden"));
        assert_eq!(adata.get_uns("missing"), None);
    }

    #[test]
    fn get_layer_mut() {
        let mut adata = sample_adata();
        let raw = MatrixData::Dense(vec![
            vec![10.0, 20.0, 0.0],
            vec![30.0, 0.0, 40.0],
            vec![0.0, 50.0, 60.0],
        ]);
        adata.add_layer("counts", raw).unwrap();
        let layer = adata.get_layer_mut("counts").unwrap();
        layer.set(0, 0, 99.0);
        assert_eq!(adata.get_layer("counts").unwrap().get(0, 0), 99.0);
    }
}