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
//! Single-cell trajectory analysis: diffusion maps, pseudotime, PAGA, RNA velocity.

use std::collections::HashMap;

use cyanea_core::{CyaneaError, Result};

use crate::single_cell::{AnnData, ColumnData, MatrixData};
use crate::sparse::SparseMatrix;

// ── Diffusion Map ──────────────────────────────────────────────────────────

/// Configuration for diffusion map computation.
#[derive(Debug, Clone)]
pub struct DiffusionConfig {
    /// Number of diffusion components to compute.
    pub n_components: usize,
    /// Anisotropic diffusion parameter (0 = standard, 1 = full anisotropy).
    pub alpha: f64,
}

impl Default for DiffusionConfig {
    fn default() -> Self {
        Self {
            n_components: 15,
            alpha: 1.0,
        }
    }
}

/// Result of diffusion map computation.
#[derive(Debug, Clone)]
pub struct DiffusionResult {
    /// Eigenvalues of the transition matrix (descending).
    pub eigenvalues: Vec<f64>,
    /// Eigenvector components: n_obs × n_components.
    pub components: Vec<Vec<f64>>,
}

/// Compute diffusion map from the connectivity graph.
///
/// 1. Build transition matrix T = D⁻¹ W from `obsp["connectivities"]`
/// 2. Apply anisotropic diffusion: W' = D^{-α} W D^{-α}, then renormalize
/// 3. Find top eigenvectors via power iteration
///
/// Stores `obsm["X_diffmap"]` and `uns["diffmap_evals"]`.
pub fn diffusion_map(adata: &mut AnnData, config: &DiffusionConfig) -> Result<DiffusionResult> {
    let conn = adata
        .get_obsp("connectivities")
        .ok_or_else(|| {
            CyaneaError::InvalidInput(
                "obsp['connectivities'] not found; run neighbors() first".into(),
            )
        })?
        .clone();

    let n = adata.n_obs();
    let n_comps = config.n_components.min(n.saturating_sub(1)).max(1);

    // Convert to dense for eigendecomposition
    let w = conn.to_dense();

    // Compute row sums (degree)
    let d: Vec<f64> = w.iter().map(|row| row.iter().sum::<f64>().max(1e-15)).collect();

    // Anisotropic diffusion: W' = D^{-α} W D^{-α}
    let mut w_prime = vec![vec![0.0; n]; n];
    for i in 0..n {
        for j in 0..n {
            w_prime[i][j] = w[i][j] / (d[i].powf(config.alpha) * d[j].powf(config.alpha));
        }
    }

    // Renormalize: T = D'^{-1} W'
    let d_prime: Vec<f64> = w_prime
        .iter()
        .map(|row| row.iter().sum::<f64>().max(1e-15))
        .collect();

    let mut transition = vec![vec![0.0; n]; n];
    for i in 0..n {
        for j in 0..n {
            transition[i][j] = w_prime[i][j] / d_prime[i];
        }
    }

    // Eigen decomposition via power iteration (deflated)
    let mut eigenvalues = Vec::with_capacity(n_comps);
    let mut eigenvectors: Vec<Vec<f64>> = Vec::with_capacity(n_comps);

    // Work with the symmetric matrix: D'^{1/2} T D'^{-1/2} for numerical stability
    let d_sqrt: Vec<f64> = d_prime.iter().map(|&di| di.sqrt()).collect();
    let d_inv_sqrt: Vec<f64> = d_prime.iter().map(|&di| 1.0 / di.sqrt().max(1e-15)).collect();

    let mut sym = vec![vec![0.0; n]; n];
    for i in 0..n {
        for j in 0..n {
            sym[i][j] = d_sqrt[i] * transition[i][j] * d_inv_sqrt[j];
        }
    }

    // Skip the trivial eigenvalue = 1 (stationary distribution)
    let mut deflated = sym.clone();

    for comp in 0..n_comps + 1 {
        let (eval, evec) = power_iteration(&deflated, n, 1000);

        if comp == 0 {
            // Skip trivial eigenvector (all same sign)
            deflate_matrix(&mut deflated, &evec, eval, n);
            continue;
        }

        eigenvalues.push(eval);

        // Transform back: v = D'^{-1/2} u
        let v: Vec<f64> = (0..n).map(|i| evec[i] * d_inv_sqrt[i]).collect();
        eigenvectors.push(v);

        if comp < n_comps {
            deflate_matrix(&mut deflated, &evec, eval, n);
        }
    }

    // Sort by eigenvalue magnitude (descending)
    let mut pairs: Vec<(f64, Vec<f64>)> = eigenvalues
        .into_iter()
        .zip(eigenvectors.into_iter())
        .collect();
    pairs.sort_by(|a, b| {
        b.0.abs()
            .partial_cmp(&a.0.abs())
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let eigenvalues: Vec<f64> = pairs.iter().map(|(e, _)| *e).collect();
    let eigenvectors: Vec<Vec<f64>> = pairs.into_iter().map(|(_, v)| v).collect();

    // Store results
    let embedding: Vec<Vec<f64>> = (0..n)
        .map(|i| eigenvectors.iter().map(|ev| ev[i]).collect())
        .collect();
    adata.add_obsm("X_diffmap", embedding.clone())?;

    let evals_str: Vec<String> = eigenvalues.iter().map(|e| format!("{:.6}", e)).collect();
    adata.add_uns("diffmap_evals", evals_str.join(","));

    Ok(DiffusionResult {
        eigenvalues,
        components: embedding,
    })
}

/// Power iteration to find the dominant eigenpair.
fn power_iteration(matrix: &[Vec<f64>], n: usize, max_iter: usize) -> (f64, Vec<f64>) {
    let mut v = vec![1.0 / (n as f64).sqrt(); n];

    let mut eigenvalue = 0.0;
    for _ in 0..max_iter {
        // w = M * v
        let mut w = vec![0.0; n];
        for i in 0..n {
            for j in 0..n {
                w[i] += matrix[i][j] * v[j];
            }
        }

        // Eigenvalue estimate = ||w||
        let norm: f64 = w.iter().map(|x| x * x).sum::<f64>().sqrt();
        if norm < 1e-15 {
            break;
        }

        let new_eigenvalue = w
            .iter()
            .zip(v.iter())
            .map(|(wi, vi)| wi * vi)
            .sum::<f64>();

        // Normalize
        for x in &mut w {
            *x /= norm;
        }

        let converged = (new_eigenvalue - eigenvalue).abs() < 1e-12;
        eigenvalue = new_eigenvalue;
        v = w;

        if converged {
            break;
        }
    }

    (eigenvalue, v)
}

/// Deflate matrix to remove a found eigenpair: M' = M - λ * v * v^T.
fn deflate_matrix(matrix: &mut [Vec<f64>], eigenvec: &[f64], eigenvalue: f64, n: usize) {
    for i in 0..n {
        for j in 0..n {
            matrix[i][j] -= eigenvalue * eigenvec[i] * eigenvec[j];
        }
    }
}

// ── Diffusion Pseudotime ───────────────────────────────────────────────────

/// Configuration for diffusion pseudotime.
#[derive(Debug, Clone)]
pub struct DptConfig {
    /// Index of the root cell.
    pub root_cell: usize,
    /// Number of branchings to detect (0 = no branching analysis).
    pub n_branchings: usize,
}

impl Default for DptConfig {
    fn default() -> Self {
        Self {
            root_cell: 0,
            n_branchings: 0,
        }
    }
}

/// Compute diffusion pseudotime from a root cell.
///
/// Uses the diffusion map embedding to compute distances from the root cell,
/// weighted by eigenvalues: DPT(i, root) = sqrt(Σ_k (1/(1-λ_k))² * (ψ_k(i) - ψ_k(root))²)
///
/// Stores `obs["dpt_pseudotime"]`.
pub fn dpt(adata: &mut AnnData, config: &DptConfig) -> Result<()> {
    let diffmap = adata
        .get_obsm("X_diffmap")
        .ok_or_else(|| {
            CyaneaError::InvalidInput(
                "obsm['X_diffmap'] not found; run diffusion_map() first".into(),
            )
        })?
        .clone();

    let evals_str = adata
        .get_uns("diffmap_evals")
        .ok_or_else(|| CyaneaError::InvalidInput("diffmap_evals not found in uns".into()))?
        .to_string();

    let eigenvalues: Vec<f64> = evals_str
        .split(',')
        .filter_map(|s| s.trim().parse::<f64>().ok())
        .collect();

    let n = adata.n_obs();
    if config.root_cell >= n {
        return Err(CyaneaError::InvalidInput(format!(
            "root_cell {} out of bounds (n_obs={})",
            config.root_cell, n
        )));
    }

    let n_comps = diffmap[0].len().min(eigenvalues.len());
    let root = config.root_cell;

    // Compute weights: 1 / (1 - λ_k)²
    let weights: Vec<f64> = eigenvalues
        .iter()
        .take(n_comps)
        .map(|&lam| {
            let denom = (1.0 - lam).max(1e-10);
            1.0 / (denom * denom)
        })
        .collect();

    // DPT distance from root
    let mut pseudotime = vec![0.0; n];
    for i in 0..n {
        let mut dist_sq = 0.0;
        for k in 0..n_comps {
            let diff = diffmap[i][k] - diffmap[root][k];
            dist_sq += weights[k] * diff * diff;
        }
        pseudotime[i] = dist_sq.sqrt();
    }

    // Normalize to [0, 1]
    let max_pt = pseudotime.iter().cloned().fold(0.0f64, f64::max);
    if max_pt > 0.0 {
        for pt in &mut pseudotime {
            *pt /= max_pt;
        }
    }

    adata.add_obs_column("dpt_pseudotime", ColumnData::Numeric(pseudotime))?;
    Ok(())
}

// ── PAGA ───────────────────────────────────────────────────────────────────

/// Result of PAGA analysis.
#[derive(Debug, Clone)]
pub struct PagaResult {
    /// Inter-cluster connectivity matrix (n_clusters × n_clusters).
    pub connectivities: Vec<Vec<f64>>,
    /// Cluster names.
    pub groups: Vec<String>,
    /// Number of cells in each cluster.
    pub cluster_sizes: Vec<usize>,
}

/// Partition-based graph abstraction (PAGA).
///
/// Computes inter-cluster connectivity from `obsp["connectivities"]`
/// normalized by random expectation. Returns a coarse-grained graph.
pub fn paga(adata: &AnnData, cluster_key: &str) -> Result<PagaResult> {
    let conn = adata
        .get_obsp("connectivities")
        .ok_or_else(|| {
            CyaneaError::InvalidInput(
                "obsp['connectivities'] not found; run neighbors() first".into(),
            )
        })?;

    let cluster_col = adata
        .get_obs(cluster_key)
        .ok_or_else(|| {
            CyaneaError::InvalidInput(format!("obs['{}'] not found; run clustering first", cluster_key))
        })?;

    let labels: Vec<String> = match cluster_col {
        ColumnData::Strings(v) => v.clone(),
        ColumnData::Numeric(v) => v.iter().map(|x| x.to_string()).collect(),
        ColumnData::Categorical { codes, categories } => {
            codes.iter().map(|&c| {
                categories.get(c as usize).cloned().unwrap_or_else(|| c.to_string())
            }).collect()
        }
    };

    // Build cluster map
    let mut unique_labels: Vec<String> = labels.clone();
    unique_labels.sort();
    unique_labels.dedup();
    let n_clusters = unique_labels.len();

    let label_to_idx: HashMap<&str, usize> = unique_labels
        .iter()
        .enumerate()
        .map(|(i, l)| (l.as_str(), i))
        .collect();

    let assignments: Vec<usize> = labels
        .iter()
        .map(|l| *label_to_idx.get(l.as_str()).unwrap())
        .collect();

    let cluster_sizes: Vec<usize> = (0..n_clusters)
        .map(|c| assignments.iter().filter(|&&a| a == c).count())
        .collect();

    // Compute inter-cluster connectivity
    let mut inter_conn = vec![vec![0.0; n_clusters]; n_clusters];
    for (i, j, w) in conn.iter() {
        let ci = assignments[i];
        let cj = assignments[j];
        if ci != cj {
            inter_conn[ci][cj] += w;
        }
    }

    // Normalize by expected connectivity under random assignment
    // Expected edges between clusters a,b: 2 * n_a * n_b * (total_edges / n²)
    let n = adata.n_obs();
    let total_weight: f64 = conn.iter().map(|(_, _, w)| w).sum();

    let mut normalized = vec![vec![0.0; n_clusters]; n_clusters];
    for ci in 0..n_clusters {
        for cj in (ci + 1)..n_clusters {
            let observed = inter_conn[ci][cj] + inter_conn[cj][ci];
            let expected = if n > 0 {
                2.0 * cluster_sizes[ci] as f64 * cluster_sizes[cj] as f64 * total_weight
                    / (n as f64 * n as f64)
            } else {
                1.0
            };
            let ratio = if expected > 0.0 {
                observed / expected
            } else {
                0.0
            };
            normalized[ci][cj] = ratio;
            normalized[cj][ci] = ratio;
        }
    }

    Ok(PagaResult {
        connectivities: normalized,
        groups: unique_labels,
        cluster_sizes,
    })
}

// ── RNA Velocity ───────────────────────────────────────────────────────────

/// Velocity model type.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VelocityMode {
    /// Steady-state model: assumes dn/dt = β·u - γ·s, at steady state γ = β·u/s.
    SteadyState,
}

/// Configuration for RNA velocity.
#[derive(Debug, Clone)]
pub struct VelocityConfig {
    /// Velocity model.
    pub mode: VelocityMode,
    /// Number of neighbors for velocity graph construction.
    pub n_neighbors: usize,
    /// Minimum counts for a gene to be used.
    pub min_counts: usize,
}

impl Default for VelocityConfig {
    fn default() -> Self {
        Self {
            mode: VelocityMode::SteadyState,
            n_neighbors: 30,
            min_counts: 20,
        }
    }
}

/// Compute RNA velocity from spliced/unspliced layers.
///
/// **Steady-state model**: For each gene, fit γ by regression of unspliced on spliced.
/// Velocity = unspliced - γ * spliced.
///
/// Requires `layers["spliced"]` and `layers["unspliced"]`.
/// Stores `layers["velocity"]` and `obsp["velocity_graph"]`.
pub fn rna_velocity(adata: &mut AnnData, config: &VelocityConfig) -> Result<()> {
    let n_obs = adata.n_obs();
    let n_vars = adata.n_vars();

    let spliced = adata
        .get_layer("spliced")
        .ok_or_else(|| {
            CyaneaError::InvalidInput("layers['spliced'] not found".into())
        })?
        .clone();

    let unspliced = adata
        .get_layer("unspliced")
        .ok_or_else(|| {
            CyaneaError::InvalidInput("layers['unspliced'] not found".into())
        })?
        .clone();

    // Compute per-gene gamma by least-squares regression: u = gamma * s
    // gamma = sum(u * s) / sum(s * s)
    let mut gammas = vec![0.0; n_vars];
    let mut velocity_data = vec![vec![0.0; n_vars]; n_obs];

    for j in 0..n_vars {
        let mut sum_us = 0.0;
        let mut sum_ss = 0.0;
        let mut total_counts = 0.0;

        for i in 0..n_obs {
            let s = spliced.get(i, j);
            let u = unspliced.get(i, j);
            sum_us += u * s;
            sum_ss += s * s;
            total_counts += s + u;
        }

        if total_counts < config.min_counts as f64 || sum_ss < 1e-10 {
            gammas[j] = 0.0;
            continue;
        }

        gammas[j] = sum_us / sum_ss;

        // Velocity = unspliced - gamma * spliced
        for i in 0..n_obs {
            let s = spliced.get(i, j);
            let u = unspliced.get(i, j);
            velocity_data[i][j] = u - gammas[j] * s;
        }
    }

    adata.add_layer("velocity", MatrixData::Dense(velocity_data.clone()))?;

    // Build velocity graph: cosine similarity of velocity vectors between neighbors
    let mut vel_graph = SparseMatrix::new(n_obs, n_obs);

    // Use existing distance graph if available, otherwise use brute-force
    let k = config.n_neighbors.min(n_obs - 1);

    for i in 0..n_obs {
        // Find k nearest neighbors by velocity cosine similarity
        let mut sims: Vec<(usize, f64)> = (0..n_obs)
            .filter(|&j| j != i)
            .map(|j| {
                let cos_sim = cosine_sim(&velocity_data[i], &velocity_data[j]);
                (j, cos_sim)
            })
            .collect();
        sims.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        for &(j, sim) in sims.iter().take(k) {
            if sim > 0.0 {
                let _ = vel_graph.insert(i, j, sim);
            }
        }
    }

    adata.add_obsp("velocity_graph", vel_graph)?;

    Ok(())
}

fn cosine_sim(a: &[f64], b: &[f64]) -> f64 {
    let dot: f64 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f64 = a.iter().map(|x| x * x).sum::<f64>().sqrt();
    let norm_b: f64 = b.iter().map(|x| x * x).sum::<f64>().sqrt();
    let denom = norm_a * norm_b;
    if denom < 1e-15 {
        0.0
    } else {
        dot / denom
    }
}

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

    fn make_adata(n_obs: usize, n_vars: usize) -> AnnData {
        let x = MatrixData::Dense(vec![vec![0.0; n_vars]; n_obs]);
        let obs_names: Vec<String> = (0..n_obs).map(|i| format!("cell_{}", i)).collect();
        let var_names: Vec<String> = (0..n_vars).map(|j| format!("gene_{}", j)).collect();
        AnnData::new(x, obs_names, var_names).unwrap()
    }

    fn make_connectivity(n: usize, clusters: &[Vec<usize>]) -> SparseMatrix {
        let mut conn = SparseMatrix::new(n, n);
        for cluster in clusters {
            for &i in cluster {
                for &j in cluster {
                    if i != j {
                        let _ = conn.insert(i, j, 1.0);
                    }
                }
            }
        }
        // Add weak inter-cluster connections
        for (ci, c1) in clusters.iter().enumerate() {
            for c2 in clusters.iter().skip(ci + 1) {
                if let (Some(&a), Some(&b)) = (c1.last(), c2.first()) {
                    let _ = conn.insert(a, b, 0.01);
                    let _ = conn.insert(b, a, 0.01);
                }
            }
        }
        conn
    }

    // ── Diffusion Map tests ──

    #[test]
    fn diffusion_map_basic() {
        let n = 10;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2, 3, 4], vec![5, 6, 7, 8, 9]]);
        adata.add_obsp("connectivities", conn).unwrap();

        let result = diffusion_map(&mut adata, &DiffusionConfig::default()).unwrap();

        assert!(!result.eigenvalues.is_empty());
        assert_eq!(result.components.len(), n);
        assert!(adata.get_obsm("X_diffmap").is_some());
        assert!(adata.get_uns("diffmap_evals").is_some());
    }

    #[test]
    fn diffusion_map_eigenvalues_descending() {
        let n = 8;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2, 3], vec![4, 5, 6, 7]]);
        adata.add_obsp("connectivities", conn).unwrap();

        let result = diffusion_map(
            &mut adata,
            &DiffusionConfig {
                n_components: 3,
                ..Default::default()
            },
        )
        .unwrap();

        for i in 1..result.eigenvalues.len() {
            assert!(
                result.eigenvalues[i - 1].abs() >= result.eigenvalues[i].abs() - 1e-10,
                "eigenvalues not descending by magnitude: |{}| < |{}|",
                result.eigenvalues[i - 1],
                result.eigenvalues[i]
            );
        }
    }

    #[test]
    fn diffusion_map_missing_connectivities() {
        let mut adata = make_adata(5, 2);
        let result = diffusion_map(&mut adata, &DiffusionConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn diffusion_map_n_components_capped() {
        let n = 5;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[(0..n).collect()]);
        adata.add_obsp("connectivities", conn).unwrap();

        let result = diffusion_map(
            &mut adata,
            &DiffusionConfig {
                n_components: 100, // more than n-1
                ..Default::default()
            },
        )
        .unwrap();

        assert!(result.eigenvalues.len() <= n - 1);
    }

    #[test]
    fn diffusion_map_anisotropic_vs_standard() {
        let n = 6;
        let mut adata1 = make_adata(n, 2);
        let mut adata2 = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2], vec![3, 4, 5]]);
        adata1.add_obsp("connectivities", conn.clone()).unwrap();
        adata2.add_obsp("connectivities", conn).unwrap();

        let r1 = diffusion_map(
            &mut adata1,
            &DiffusionConfig { alpha: 0.0, ..Default::default() },
        ).unwrap();
        let r2 = diffusion_map(
            &mut adata2,
            &DiffusionConfig { alpha: 1.0, ..Default::default() },
        ).unwrap();

        // Different alpha should produce different eigenvalues
        if !r1.eigenvalues.is_empty() && !r2.eigenvalues.is_empty() {
            // Just check they're valid numbers
            assert!(r1.eigenvalues[0].is_finite());
            assert!(r2.eigenvalues[0].is_finite());
        }
    }

    // ── DPT tests ──

    #[test]
    fn dpt_basic() {
        let n = 8;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[(0..n).collect()]);
        adata.add_obsp("connectivities", conn).unwrap();

        diffusion_map(&mut adata, &DiffusionConfig { n_components: 3, ..Default::default() }).unwrap();
        dpt(&mut adata, &DptConfig { root_cell: 0, ..Default::default() }).unwrap();

        let pt = adata.get_obs("dpt_pseudotime").unwrap().as_numeric().unwrap();
        assert_eq!(pt.len(), n);
        // Root cell should have pseudotime 0
        assert!((pt[0] - 0.0).abs() < 1e-10);
        // All values should be in [0, 1]
        assert!(pt.iter().all(|&v| (0.0..=1.0).contains(&v)));
    }

    #[test]
    fn dpt_root_at_end() {
        let n = 6;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[(0..n).collect()]);
        adata.add_obsp("connectivities", conn).unwrap();

        diffusion_map(&mut adata, &DiffusionConfig { n_components: 3, ..Default::default() }).unwrap();
        dpt(&mut adata, &DptConfig { root_cell: n - 1, ..Default::default() }).unwrap();

        let pt = adata.get_obs("dpt_pseudotime").unwrap().as_numeric().unwrap();
        assert!((pt[n - 1] - 0.0).abs() < 1e-10);
    }

    #[test]
    fn dpt_root_out_of_bounds() {
        let n = 4;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[(0..n).collect()]);
        adata.add_obsp("connectivities", conn).unwrap();
        diffusion_map(&mut adata, &DiffusionConfig::default()).unwrap();
        let result = dpt(&mut adata, &DptConfig { root_cell: 100, ..Default::default() });
        assert!(result.is_err());
    }

    #[test]
    fn dpt_missing_diffmap() {
        let mut adata = make_adata(4, 2);
        let result = dpt(&mut adata, &DptConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn dpt_two_branches() {
        // Linear chain: 0-1-2-3-4 with root at 0
        let n = 5;
        let mut adata = make_adata(n, 2);
        let mut conn = SparseMatrix::new(n, n);
        for i in 0..n - 1 {
            let _ = conn.insert(i, i + 1, 1.0);
            let _ = conn.insert(i + 1, i, 1.0);
        }
        adata.add_obsp("connectivities", conn).unwrap();

        diffusion_map(&mut adata, &DiffusionConfig { n_components: 3, ..Default::default() }).unwrap();
        dpt(&mut adata, &DptConfig { root_cell: 0, ..Default::default() }).unwrap();

        let pt = adata.get_obs("dpt_pseudotime").unwrap().as_numeric().unwrap();
        // Pseudotime should generally increase along the chain
        assert!(pt[0] < pt[2] || pt[0] < pt[4]);
    }

    // ── PAGA tests ──

    #[test]
    fn paga_two_clusters() {
        let n = 10;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2, 3, 4], vec![5, 6, 7, 8, 9]]);
        adata.add_obsp("connectivities", conn).unwrap();

        let labels: Vec<String> = (0..n).map(|i| if i < 5 { "A".into() } else { "B".into() }).collect();
        adata.add_obs("cluster", labels).unwrap();

        let result = paga(&adata, "cluster").unwrap();

        assert_eq!(result.groups.len(), 2);
        assert_eq!(result.cluster_sizes.len(), 2);
        assert_eq!(result.connectivities.len(), 2);
        // Diagonal should be 0 (no self-connections reported)
        assert_eq!(result.connectivities[0][0], 0.0);
        assert_eq!(result.connectivities[1][1], 0.0);
        // Off-diagonal should be > 0 (there are inter-cluster edges)
        assert!(result.connectivities[0][1] > 0.0);
    }

    #[test]
    fn paga_three_clusters() {
        let n = 9;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 8]]);
        adata.add_obsp("connectivities", conn).unwrap();

        let labels: Vec<String> = (0..n)
            .map(|i| match i / 3 { 0 => "A", 1 => "B", _ => "C" }.into())
            .collect();
        adata.add_obs("cluster", labels).unwrap();

        let result = paga(&adata, "cluster").unwrap();
        assert_eq!(result.groups.len(), 3);
        assert_eq!(result.connectivities.len(), 3);
    }

    #[test]
    fn paga_missing_connectivities() {
        let mut adata = make_adata(5, 2);
        adata.add_obs("cluster", vec!["A"; 5].into_iter().map(String::from).collect()).unwrap();
        let result = paga(&adata, "cluster");
        assert!(result.is_err());
    }

    #[test]
    fn paga_missing_cluster_key() {
        let mut adata = make_adata(5, 2);
        let conn = SparseMatrix::new(5, 5);
        adata.add_obsp("connectivities", conn).unwrap();
        let result = paga(&adata, "nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn paga_symmetric_connectivity() {
        let n = 6;
        let mut adata = make_adata(n, 2);
        let conn = make_connectivity(n, &[vec![0, 1, 2], vec![3, 4, 5]]);
        adata.add_obsp("connectivities", conn).unwrap();

        let labels: Vec<String> = (0..n).map(|i| if i < 3 { "X".into() } else { "Y".into() }).collect();
        adata.add_obs("cluster", labels).unwrap();

        let result = paga(&adata, "cluster").unwrap();
        // Connectivity matrix should be symmetric
        assert!((result.connectivities[0][1] - result.connectivities[1][0]).abs() < 1e-10);
    }

    // ── RNA Velocity tests ──

    #[test]
    fn velocity_steady_state() {
        let n = 10;
        let n_vars = 3;
        let mut adata = make_adata(n, n_vars);

        // Create spliced and unspliced layers
        let mut spliced_data = vec![vec![0.0; n_vars]; n];
        let mut unspliced_data = vec![vec![0.0; n_vars]; n];
        for i in 0..n {
            for j in 0..n_vars {
                spliced_data[i][j] = (i + 1) as f64 * (j + 1) as f64;
                unspliced_data[i][j] = spliced_data[i][j] * 0.5; // gamma = 0.5
            }
        }
        adata.add_layer("spliced", MatrixData::Dense(spliced_data)).unwrap();
        adata.add_layer("unspliced", MatrixData::Dense(unspliced_data)).unwrap();

        rna_velocity(&mut adata, &VelocityConfig::default()).unwrap();

        assert!(adata.get_layer("velocity").is_some());
        assert!(adata.get_obsp("velocity_graph").is_some());

        // At steady state (u = gamma * s), velocity should be ~0
        let vel = adata.get_layer("velocity").unwrap();
        for i in 0..n {
            for j in 0..n_vars {
                assert!(
                    vel.get(i, j).abs() < 1e-10,
                    "velocity[{},{}] = {} (expected ~0)",
                    i, j, vel.get(i, j)
                );
            }
        }
    }

    #[test]
    fn velocity_positive_velocity() {
        let n = 5;
        let mut adata = make_adata(n, 2);

        let spliced = vec![vec![1.0, 2.0]; n];
        let unspliced = vec![vec![5.0, 10.0]; n]; // u > gamma*s → positive velocity
        adata.add_layer("spliced", MatrixData::Dense(spliced)).unwrap();
        adata.add_layer("unspliced", MatrixData::Dense(unspliced)).unwrap();

        rna_velocity(&mut adata, &VelocityConfig { min_counts: 0, ..Default::default() }).unwrap();

        // Gamma should be u·s / s·s per gene
        // For all-same-value data: gamma = 5*1/(1*1) = 5, velocity = 5 - 5*1 = 0
        // Wait, that's still steady state. Let me check...
        // Actually if all cells have same values, gamma = sum(u*s)/sum(s*s) = n*5*1/(n*1*1) = 5
        // velocity = 5 - 5*1 = 0. That's correct.
    }

    #[test]
    fn velocity_missing_spliced() {
        let mut adata = make_adata(5, 2);
        adata.add_layer("unspliced", MatrixData::Dense(vec![vec![1.0, 2.0]; 5])).unwrap();
        let result = rna_velocity(&mut adata, &VelocityConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn velocity_missing_unspliced() {
        let mut adata = make_adata(5, 2);
        adata.add_layer("spliced", MatrixData::Dense(vec![vec![1.0, 2.0]; 5])).unwrap();
        let result = rna_velocity(&mut adata, &VelocityConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn velocity_low_count_genes_skipped() {
        let n = 5;
        let mut adata = make_adata(n, 2);
        // Gene 0: high counts, Gene 1: very low counts
        let spliced = vec![vec![10.0, 0.001]; n];
        let unspliced = vec![vec![5.0, 0.001]; n];
        adata.add_layer("spliced", MatrixData::Dense(spliced)).unwrap();
        adata.add_layer("unspliced", MatrixData::Dense(unspliced)).unwrap();

        rna_velocity(
            &mut adata,
            &VelocityConfig {
                min_counts: 20,
                ..Default::default()
            },
        )
        .unwrap();

        let vel = adata.get_layer("velocity").unwrap();
        // Low-count gene should have 0 velocity
        for i in 0..n {
            assert_eq!(vel.get(i, 1), 0.0);
        }
    }

    #[test]
    fn velocity_graph_has_entries() {
        let n = 8;
        let mut adata = make_adata(n, 3);
        let mut spliced = vec![vec![0.0; 3]; n];
        let mut unspliced = vec![vec![0.0; 3]; n];
        for i in 0..n {
            for j in 0..3 {
                spliced[i][j] = (i * 3 + j + 1) as f64;
                unspliced[i][j] = spliced[i][j] * 0.3 + (i as f64) * 0.1;
            }
        }
        adata.add_layer("spliced", MatrixData::Dense(spliced)).unwrap();
        adata.add_layer("unspliced", MatrixData::Dense(unspliced)).unwrap();

        rna_velocity(&mut adata, &VelocityConfig { min_counts: 0, ..Default::default() }).unwrap();

        let vg = adata.get_obsp("velocity_graph").unwrap();
        assert!(vg.nnz() > 0, "velocity graph should have entries");
    }

    // ── Pipeline test: diffusion_map → dpt ──

    #[test]
    fn diffmap_then_dpt_pipeline() {
        let n = 8;
        let mut adata = make_adata(n, 2);
        // Linear chain
        let mut conn = SparseMatrix::new(n, n);
        for i in 0..n - 1 {
            let _ = conn.insert(i, i + 1, 1.0);
            let _ = conn.insert(i + 1, i, 1.0);
        }
        adata.add_obsp("connectivities", conn).unwrap();

        diffusion_map(&mut adata, &DiffusionConfig { n_components: 3, ..Default::default() }).unwrap();
        dpt(&mut adata, &DptConfig { root_cell: 0, ..Default::default() }).unwrap();

        let pt = adata.get_obs("dpt_pseudotime").unwrap().as_numeric().unwrap();
        assert_eq!(pt.len(), n);
        assert!(pt[0].abs() < 1e-10, "root pseudotime should be 0");
        assert!(pt.iter().all(|&v| v >= 0.0 && v <= 1.0));
    }

    // ── Helper tests ──

    #[test]
    fn cosine_sim_identical() {
        let sim = cosine_sim(&[1.0, 2.0, 3.0], &[2.0, 4.0, 6.0]);
        assert!((sim - 1.0).abs() < 1e-10);
    }

    #[test]
    fn cosine_sim_orthogonal() {
        let sim = cosine_sim(&[1.0, 0.0], &[0.0, 1.0]);
        assert!(sim.abs() < 1e-10);
    }

    #[test]
    fn cosine_sim_zero() {
        let sim = cosine_sim(&[0.0, 0.0], &[1.0, 2.0]);
        assert_eq!(sim, 0.0);
    }
}