bixverse-rs 0.5.3

A Rust library with various utilities for computational biology and single cell.
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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
//! Shared infrastructure for doublet detection methods.

use faer::{Mat, MatRef, concat};
use half::f16;
use indexmap::IndexSet;
use rand::{Rng, RngCore, SeedableRng, rngs::StdRng};
use rayon::prelude::*;
use rustc_hash::{FxHashMap, FxHashSet};
use std::time::Instant;

use crate::core::math::MAX_OVERSAMPLING_SINGLE_CELL;
use crate::core::math::pca_svd::*;
use crate::core::math::sparse::sparse_svd_lanczos;
use crate::prelude::*;

/// Genes read per batch while streaming gene chunks for doublet scoring.
const GENE_BATCH_SIZE: usize = 1000;
use crate::single_cell::sc_processing::hvg::*;
use crate::single_cell::sc_processing::pca::*;

////////////
// Consts //
////////////

/// Maximum number of doublets to use for Scrublet and DoubletDetection
pub const MAX_DOUBLET_K_NEIGHBOURS: usize = 250;

/// Minimum cell count below which fast_cluster falls back to standard Louvain.
/// Relevant for scdblfinder and doublet detection.
pub const FAST_CLUSTER_MIN_CELLS: usize = 10_000;

////////////
// Params //
////////////

/// Common HVG parameter bundle
pub struct HvgOpts {
    /// Minimum percentile for the gene variance
    pub min_gene_var_pctl: f32,
    /// HVG method to use
    pub method: String,
    /// Loess span parameter
    pub loess_span: f32,
    /// Optional clipping parameter
    pub clip_max: Option<f32>,
    /// Number of bins
    pub n_bins: usize,
    /// Binning strategy, one of `"equal_width"` or `"equal_freq"`
    pub binning_strategy: String,
}

/// Common PCA parameter bundle
pub struct PcaOpts {
    /// Shall the data be log transformed
    pub log_transform: bool,
    /// Shall the data be mean centred
    pub mean_center: bool,
    /// Shall the data have scaled variance
    pub normalise_variance: bool,
    /// Number of PCs to use
    pub no_pcs: usize,
    /// Shall randomised SVD be used
    pub random_svd: bool,
}

//////////////////////
// HVG + lib sizes  //
//////////////////////

/// Select highly variable genes
///
/// Identical logic used by Scrublet, Boost and ScDblFinder. Returns gene
/// indices sorted in ascending order.
///
/// ### Params
///
/// * `reader` - Reader over the gene-based count store.
/// * `cells_to_keep` - Indices of the cells to include in this analysis.
/// * `opts` - Hvg parameters
/// * `streaming` - Shall the data be streamed (reduces memory pressure)
/// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for detailed
///   verbosity.
///
/// ### Returns
///
/// Returns the indices of the HVG
pub fn select_hvg<S: SingleCellReading>(
    reader: &S,
    cells_to_keep: &[usize],
    opts: &HvgOpts,
    streaming: bool,
    verbose: usize,
) -> Result<Vec<usize>, BixverseErrors> {
    let hvg_type = parse_hvg_method(&opts.method).unwrap_or_default();

    let sort_key: Vec<f64> = match hvg_type {
        HvgMethod::Vst => {
            let res = if streaming {
                get_hvg_vst_streaming(
                    reader,
                    cells_to_keep,
                    opts.loess_span,
                    opts.clip_max,
                    verbose,
                )
            } else {
                get_hvg_vst(
                    reader,
                    cells_to_keep,
                    opts.loess_span,
                    opts.clip_max,
                    verbose,
                )
            }?;
            res.var_std
        }
        HvgMethod::Dispersion => {
            let res = if streaming {
                get_hvg_dispersion_streaming(
                    reader,
                    cells_to_keep,
                    &opts.binning_strategy,
                    opts.n_bins,
                    verbose,
                )
            } else {
                get_hvg_dispersion(
                    reader,
                    cells_to_keep,
                    &opts.binning_strategy,
                    opts.n_bins,
                    verbose,
                )
            }?;
            res.dispersion
        }
        HvgMethod::MeanVarBin => {
            let res = if streaming {
                get_hvg_mvb_streaming(
                    reader,
                    cells_to_keep,
                    &opts.binning_strategy,
                    opts.n_bins,
                    verbose,
                )
            } else {
                get_hvg_mvb(
                    reader,
                    cells_to_keep,
                    &opts.binning_strategy,
                    opts.n_bins,
                    verbose,
                )
            }?;
            res.dispersion_scaled
        }
    };

    let n_genes = sort_key.len() as f32;
    let n_to_take = (n_genes * (1.0 - opts.min_gene_var_pctl)).ceil() as usize;
    let mut indices: Vec<(usize, f64)> = sort_key.into_iter().enumerate().collect();
    indices.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
    indices.truncate(n_to_take);
    let mut result: Vec<usize> = indices.into_iter().map(|(i, _)| i).collect();
    result.sort_unstable();

    Ok(result)
}

/// Compute per-cell library sizes restricted to HVG genes
///
/// ### Params
///
/// * `reader` - Reader over the gene-based count store.
/// * `cells_to_keep` - Indices of the cells to include in this analysis.
/// * `hvg_genes` - Indices of the highly variable genes
///
/// ### Returns
///
/// Library sizes per cell given only the HVG genes
pub fn compute_hvg_library_sizes<S: SingleCellReading>(
    reader: &S,
    cells_to_keep: &[usize],
    hvg_genes: &[usize],
) -> Result<Vec<usize>, BixverseErrors> {
    let hvg_set: FxHashSet<usize> = hvg_genes.iter().copied().collect();

    cells_to_keep
        .par_iter()
        .map(|&cell_idx| -> Result<usize, BixverseErrors> {
            let chunk = reader.read_cell(cell_idx)?;
            Ok(chunk
                .indices
                .iter()
                .enumerate()
                .filter(|&(_, &gene_idx)| hvg_set.contains(&(gene_idx as usize)))
                .map(|(i, _)| chunk.data_raw.get(i) as usize)
                .sum())
        })
        .collect()
}

/// Compute target library size (mean of HVG library sizes if not provided)
///
/// ### Params
///
/// * `explicit` -
/// * `hvg_library_sizes` -
///
/// ### Returns
///
///
pub fn resolve_target_size(explicit: Option<f32>, hvg_library_sizes: &[usize]) -> f32 {
    explicit.unwrap_or_else(|| {
        let sum = hvg_library_sizes.iter().sum::<usize>() as f32;
        sum / hvg_library_sizes.len() as f32
    })
}

/////////
// PCA //
/////////

/// Type alias for Scrublet PCA results
///
/// ### Fields
///
/// * `0` - PCA scores
/// * `1` - PCA loadings
/// * `2` - Gene means
/// * `3` - Gene standard deviations
pub type DoubletPcaRes = (Mat<f32>, Mat<f32>, Vec<f32>, Vec<f32>);

/// Scale `Vec<CsrCellChunk>` using pre-calculated gene means and stds
///
/// This ensures simulated doublets are scaled using the SAME statistics
/// as the observed cells (critical for proper PCA projection)
///
/// The background value a gene takes where a cell stores nothing is a column
/// constant, so the columns are filled first, in parallel and contiguously
/// (`Mat` is column-major), and only then are the stored entries scattered on
/// top. The scatter strides across columns and stays sequential, but it touches
/// `nnz` elements rather than `n_cells * n_genes`.
///
/// Prefer [`project_cell_chunks_with_stats`] when the result is only going to
/// be multiplied by the PCA loadings, which avoids this matrix entirely.
///
/// ### Params
///
/// * `chunks` - Vector of cell chunks (simulated doublets)
/// * `gene_means` - Mean for each gene (from observed data)
/// * `gene_stds` - Std dev for each gene (from observed data)
/// * `mean_center` - Centre the data around the mean
/// * `normalise_variance` - Normalise the variance
/// * `n_genes` - Total number of genes
///
/// ### Returns
///
/// Dense matrix (cells x genes) with z-scored values
pub fn scale_cell_chunks_with_stats(
    chunks: &[CsrCellChunk],
    gene_means: &[f32],
    gene_stds: &[f32],
    mean_center: bool,
    normalise_variance: bool,
    n_genes: usize,
) -> Mat<f32> {
    let n_cells = chunks.len();
    let mut result = Mat::<f32>::zeros(n_cells, n_genes);

    if mean_center {
        result
            .par_col_iter_mut()
            .enumerate()
            .for_each(|(gene, mut col)| {
                let background = if normalise_variance {
                    -gene_means[gene] / gene_stds[gene]
                } else {
                    -gene_means[gene]
                };
                col.fill(background);
            });
    }

    for (cell_idx, chunk) in chunks.iter().enumerate() {
        for (i, &gene) in chunk.indices.iter().enumerate() {
            let gene = gene as usize;
            let mut value = chunk.data_norm[i].to_f32();
            if mean_center {
                value -= gene_means[gene];
            }
            if normalise_variance {
                value /= gene_stds[gene];
            }
            *result.get_mut(cell_idx, gene) = value;
        }
    }

    result
}

/// Project cell chunks straight onto PCA loadings, skipping the dense matrix.
///
/// [`scale_cell_chunks_with_stats`] followed by a GEMM against the loadings
/// materialises an `n_cells x n_genes` block only to reduce it away. Since the
/// scaling is affine, the projection splits into a sparse part and a constant:
///
/// ```text
/// ((X - mu) / sd) V  =  (X / sd) V  -  1 ((mu / sd)^T V)
/// ```
///
/// The right-hand term is one row of length `n_pcs`, computed once. The left is
/// an axpy of the matching loadings row per stored entry, so the cost is
/// `nnz * n_pcs` instead of `n_cells * n_genes * n_pcs`, with no dense
/// intermediate. The loadings are transposed into row-major order up front so
/// each axpy runs over a contiguous slice.
///
/// The result is mathematically the same as the two-step path but not
/// bit-identical, since the sums accumulate in a different order.
///
/// ### Params
///
/// * `chunks` - Vector of cell chunks (simulated doublets)
/// * `gene_means` - Mean for each gene (from observed data)
/// * `gene_stds` - Std dev for each gene (from observed data)
/// * `loadings` - PCA loadings, shape (genes, PCs). Its row count defines the
///   gene axis and must match `gene_means` and `gene_stds`.
/// * `mean_center` - Centre the data around the mean
/// * `normalise_variance` - Normalise the variance
///
/// ### Returns
///
/// Dense matrix (cells x PCs) of projected scores.
pub fn project_cell_chunks_with_stats(
    chunks: &[CsrCellChunk],
    gene_means: &[f32],
    gene_stds: &[f32],
    loadings: MatRef<'_, f32>,
    mean_center: bool,
    normalise_variance: bool,
) -> Mat<f32> {
    let n_cells = chunks.len();
    let n_genes = loadings.nrows();
    let n_pcs = loadings.ncols();

    // the old scale-then-GEMM path took `n_genes` explicitly and faer checked
    // `A.ncols() == B.nrows()`, so a mismatched statistics vector died loudly.
    // Deriving the axis from the loadings removed that check, and a `gene_means`
    // longer than `n_genes` would otherwise truncate the background term
    // silently and return a wrong projection. Assert rather than return
    // `Result`, since `reproject_doublets` is `pub` and infallible.
    assert_eq!(
        gene_means.len(),
        n_genes,
        "gene_means must match the gene axis of the loadings"
    );
    assert_eq!(
        gene_stds.len(),
        n_genes,
        "gene_stds must match the gene axis of the loadings"
    );

    // `par_chunks_mut(0)` panics; the old GEMM handled a zero-column loadings
    // and returned an `n_cells x 0` matrix, so keep that
    if n_pcs == 0 {
        return Mat::<f32>::zeros(n_cells, 0);
    }

    // row-major copy so the per-entry axpy walks a contiguous slice
    let mut loadings_rm = vec![0.0_f32; n_genes * n_pcs];
    loadings_rm
        .par_chunks_mut(n_pcs)
        .enumerate()
        .for_each(|(gene, row)| {
            for (pc, slot) in row.iter_mut().enumerate() {
                *slot = loadings[(gene, pc)];
            }
        });

    // constant contribution of the implicit -mean/sd background, negated so
    // each cell can start from it directly
    let mut background = vec![0.0_f32; n_pcs];
    if mean_center {
        for gene in 0..n_genes {
            let mut centre = gene_means[gene];
            if normalise_variance {
                centre /= gene_stds[gene];
            }
            if centre == 0.0 {
                continue;
            }
            f32::bxv_axpy_simd(
                &mut background,
                -centre,
                &loadings_rm[gene * n_pcs..(gene + 1) * n_pcs],
            );
        }
    }

    let mut scores = vec![0.0_f32; n_cells * n_pcs];
    scores
        .par_chunks_mut(n_pcs)
        .zip(chunks.par_iter())
        .for_each(|(row, chunk)| {
            row.copy_from_slice(&background);

            for (i, &gene) in chunk.indices.iter().enumerate() {
                let gene = gene as usize;
                let mut value = chunk.data_norm[i].to_f32();
                if normalise_variance {
                    value /= gene_stds[gene];
                }
                if value == 0.0 {
                    continue;
                }
                f32::bxv_axpy_simd(row, value, &loadings_rm[gene * n_pcs..(gene + 1) * n_pcs]);
            }
        });

    Mat::from_fn(n_cells, n_pcs, |cell, pc| scores[cell * n_pcs + pc])
}

/// Calculate PCA for doublet detection methods using sparse SVD
///
/// Computes PCA on observed cells without densifying the full matrix. Gene
/// chunks are re-normalised using HVG-specific library sizes (matching the
/// Scrublet normalisation scheme), assembled into a CSC sparse matrix, and
/// decomposed via sparse randomised SVD or Lanczos. This avoids holding
/// an `n_cells x n_genes` dense matrix in memory, which is the primary
/// memory bottleneck in both Scrublet and Boost.
///
/// The returned gene means and standard deviations are computed from the
/// re-normalised sparse data and must be used to scale simulated doublets
/// before projecting them into the same PC space (via
/// `scale_cell_chunks_with_stats`).
///
/// ### Params
///
/// * `reader` - Reader over the gene-based count store.
/// * `cell_indices` - Slice of cell indices to include in the analysis.
/// * `gene_indices` - Slice of gene indices (HVGs) to use.
/// * `hvg_library_sizes` - Per-cell library sizes computed over HVG genes
///   only. Must be in the same order as `cell_indices`.
/// * `target_size` - Normalisation target size (e.g. mean HVG library size).
/// * `log_transform` - Whether to apply `ln(1 + x)` after size-factor
///   normalisation.
/// * `mean_center` - Whether to implicitly centre columns during SVD.
/// * `normalise_variance` - Whether to implicitly scale columns to unit
///   variance during SVD.
/// * `no_pcs` - Number of principal components to compute.
/// * `random_svd` - If true, use randomised sparse SVD; otherwise use
///   Lanczos-based sparse SVD.
/// * `seed` - Seed for reproducibility (randomised SVD and Lanczos init).
/// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for detailed
///   verbosity.
///
/// ### Returns
///
/// A `ScrubletPcaRes` tuple of `(scores, loadings, gene_means, gene_stds)`
/// where scores is `n_cells x no_pcs`, loadings is `n_genes x no_pcs`,
/// and means/stds are per-gene vectors for downstream doublet projection.
#[allow(clippy::too_many_arguments)]
pub fn pca_observed<S: SingleCellReading>(
    reader: &S,
    cell_indices: &[usize],
    gene_indices: &[usize],
    hvg_library_sizes: &[usize],
    target_size: f32,
    log_transform: bool,
    mean_center: bool,
    normalise_variance: bool,
    no_pcs: usize,
    random_svd: bool,
    seed: usize,
    verbose: usize,
) -> Result<DoubletPcaRes, BixverseErrors> {
    let verbosity = parse_verbosity_level(verbose);

    let start_total = Instant::now();
    let cell_set: IndexSet<u32> = cell_indices.iter().map(|&x| x as u32).collect();
    let n_cells = cell_indices.len();

    let start_reading = Instant::now();
    let mut gene_chunks: Vec<CscGeneChunk> =
        reader.read_gene_parallel_filtered(gene_indices, &cell_set)?;
    if verbosity.normal_verbosity() {
        println!("Loaded in data: {:.2?}", start_reading.elapsed());
    }

    let start_prep = Instant::now();

    gene_chunks.par_iter_mut().for_each(|chunk| {
        for (i, &pos) in chunk.indices.iter().enumerate() {
            let raw_count = chunk.data_raw.get(i) as f32;
            let lib_size = hvg_library_sizes[pos as usize] as f32;
            let val = if log_transform {
                ((raw_count / lib_size) * target_size).ln_1p()
            } else {
                (raw_count / lib_size) * target_size
            };
            chunk.data_norm[i] = F16::from(half::f16::from_f32(val.clamp(-65504.0, 65504.0)));
        }
    });

    let csc = from_gene_chunks::<f32>(gene_chunks, &DataLayerReturn::Norm, n_cells)?;

    let col_means: Vec<f64> = sparse_csc_column_means(&csc, true, None)?;
    let col_stds: Vec<f64> = sparse_csc_column_stds(&csc, &col_means, true, None)?;

    let means_for_svd = if mean_center {
        Some(&col_means[..])
    } else {
        None
    };
    let stds_for_svd = if normalise_variance {
        Some(&col_stds[..])
    } else {
        None
    };

    if verbosity.normal_verbosity() {
        println!("Finished data preparation: {:.2?}", start_prep.elapsed());
    }

    let start_svd = Instant::now();

    let (scores, loadings) = if random_svd {
        let svd_res = randomised_sparse_svd::<f32, f64>(
            csc,
            no_pcs,
            seed as u64,
            true,
            Some(MAX_OVERSAMPLING_SINGLE_CELL),
            None,
            means_for_svd,
            stds_for_svd,
            None,
        )?;
        let scores_f64 = compute_pc_scores(&svd_res);
        let scores = Mat::<f32>::from_fn(n_cells, no_pcs, |i, j| scores_f64[(i, j)] as f32);
        let loadings = Mat::<f32>::from_fn(gene_indices.len(), no_pcs, |i, j| {
            svd_res.v()[(i, j)] as f32
        });
        (scores, loadings)
    } else {
        let svd_res = sparse_svd_lanczos::<f32, f32, f64>(
            &csc,
            no_pcs,
            seed as u64,
            true,
            means_for_svd,
            stds_for_svd,
            None,
        )?;
        let scores_f64 = compute_pc_scores(&svd_res);
        let scores = Mat::<f32>::from_fn(scores_f64.nrows(), scores_f64.ncols(), |i, j| {
            scores_f64[(i, j)] as f32
        });
        let loadings = Mat::<f32>::from_fn(svd_res.v().nrows(), svd_res.v().ncols(), |i, j| {
            svd_res.v()[(i, j)] as f32
        });
        (scores, loadings)
    };

    if verbosity.normal_verbosity() {
        println!("Finished PCA calculations: {:.2?}", start_svd.elapsed());
        println!(
            "Total run time PCA detection: {:.2?}",
            start_total.elapsed()
        );
    }

    // Cast means/stds back to f32 for the return type
    let col_means_f32: Vec<f32> = col_means.iter().map(|&x| x as f32).collect();
    let col_stds_f32: Vec<f32> = col_stds.iter().map(|&x| x as f32).collect();

    Ok((scores, loadings, col_means_f32, col_stds_f32))
}

/// Run PCA on observed cells, project simulated doublets, return combined
///
/// The observed cells are decomposed via sparse SVD (no densification).
/// Simulated doublet chunks are projected into the same PC space using the
/// observed statistics, via [`project_cell_chunks_with_stats`], so neither side
/// materialises a dense matrix. Returns the vertically concatenated scores:
/// observed on top, simulated on bottom.
///
/// ### Params
///
/// * `reader` - Reader over the gene-based count store.
/// * `cells_to_keep` - Indices of the cells to keep
/// * `hvg_genes` - Indices of the HVG genes
/// * `hvg_library_sizes` - Library sizes adjusted for only including HVG
/// * `target_size` - The target size to normalise against
/// * `sim_chunks` - Slice of simulated chunks
/// * `opts` - The [`PcaOpts`] for this run
/// * `seed` - Random seed
/// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for detailed
///   verbosity.
///
/// ### Returns
///
/// A tuple of `(combined_scores, DoubletPcaRes)`
#[allow(clippy::too_many_arguments)]
pub fn pca_and_project<S: SingleCellReading>(
    reader: &S,
    cells_to_keep: &[usize],
    hvg_genes: &[usize],
    hvg_library_sizes: &[usize],
    target_size: f32,
    sim_chunks: &[CsrCellChunk],
    opts: &PcaOpts,
    seed: usize,
    verbose: usize,
) -> Result<(Mat<f32>, DoubletPcaRes), BixverseErrors> {
    let pca_res = pca_observed(
        reader,
        cells_to_keep,
        hvg_genes,
        hvg_library_sizes,
        target_size,
        opts.log_transform,
        opts.mean_center,
        opts.normalise_variance,
        opts.no_pcs,
        opts.random_svd,
        seed,
        verbose,
    )?;

    let sim_pca = project_cell_chunks_with_stats(
        sim_chunks,
        &pca_res.2,
        &pca_res.3,
        pca_res.1.as_ref(),
        opts.mean_center,
        opts.normalise_variance,
    );

    let combined = concat![[&pca_res.0], [sim_pca]];

    Ok((combined, pca_res))
}

/// Reproject new doublets based on existing PCA results
///
/// ### Params
///
/// * `sim_chunks` - Slice of simulated chunks
/// * `pca_res` - The DoubletPcaRes for reprojecting the data
/// * `opts` - The [`PcaOpts`] for this run
///
/// ### Returns
///
/// Combined scores
pub fn reproject_doublets(
    sim_chunks: &[CsrCellChunk],
    pca_res: &DoubletPcaRes,
    opts: &PcaOpts,
) -> Mat<f32> {
    let sim_pca = project_cell_chunks_with_stats(
        sim_chunks,
        &pca_res.2,
        &pca_res.3,
        pca_res.1.as_ref(),
        opts.mean_center,
        opts.normalise_variance,
    );

    concat![[&pca_res.0], [sim_pca]]
}

/////////
// kNN //
/////////

/// Dispatch to the correct kNN implementation
///
/// Pure routing function. The `k` value should already be adjusted by the
/// caller (e.g. scaled for combined obs+sim size).
///
/// ### Params
///
/// * `embd` - PCA embedding matrix of n_cells x features
/// * `k` - Number of neighbours to use
/// * `knn_params` - Parameters for the various kNN approximate nearest
///   neighbour searches
/// * `seed` - Seed for reproducibility
/// * `verbose` - If `0` -> silent or `1` for normal verbosity, `2` for detailed
///   verbosity.
///
/// ### Returns
///
/// The kNN indices for the cells
pub fn dispatch_knn(
    embd: MatRef<f32>,
    k: usize,
    knn_params: &KnnParams,
    seed: usize,
    verbose: bool,
) -> Result<Vec<Vec<usize>>, BixverseErrors> {
    let method = parse_knn_method(&knn_params.knn_method).unwrap_or_default();
    warn_unsupported_extract(knn_params.extract_knn, method);

    match method {
        KnnSearch::Hnsw => generate_knn_hnsw(
            embd,
            &knn_params.ann_dist,
            k,
            knn_params.m,
            knn_params.ef_construction,
            knn_params.ef_search,
            seed,
            false,
            verbose,
        ),
        KnnSearch::Annoy => generate_knn_annoy(
            embd,
            &knn_params.ann_dist,
            k,
            knn_params.n_tree,
            knn_params.search_budget,
            seed,
            false,
            verbose,
        ),
        KnnSearch::NNDescent => generate_knn_nndescent(
            embd,
            &knn_params.ann_dist,
            k,
            knn_params.diversify_prob,
            knn_params.ef_budget,
            knn_params.delta,
            knn_params.extract_knn,
            seed,
            false,
            verbose,
        ),
        KnnSearch::Exhaustive => generate_knn_exhaustive(embd, &knn_params.ann_dist, k, verbose),
        KnnSearch::Ivf => generate_knn_ivf(
            embd,
            &knn_params.ann_dist,
            k,
            knn_params.n_list,
            knn_params.n_list,
            seed,
            false,
            verbose,
        ),
        KnnSearch::KmKnn => generate_knn_kmknn(
            embd,
            &knn_params.ann_dist,
            k,
            knn_params.n_list,
            seed,
            verbose,
        ),
    }
}

/// Compute adjusted k for combined observed + simulated kNN.
///
/// Scales k by `(1 + n_sim / n_obs)` to maintain effective neighbourhood
/// density when the graph contains both real and synthetic cells. If
/// `base_k` is 0, defaults to `round(0.5 * sqrt(n_obs))`.
///
/// ### Params
///
/// * `base_k` - Requested number of neighbours. If 0, auto-selected from
///   `n_obs`.
/// * `n_obs` - Number of observed cells.
/// * `n_sim` - Number of simulated doublets.
///
/// ### Returns
///
/// The adjusted k value to use for kNN construction on the combined
/// (observed + simulated) embedding. This is clamped to `MAX_DOUBLET_K_NEIGHBOURS`.
pub fn adjusted_k(base_k: usize, n_obs: usize, n_sim: usize) -> usize {
    let k = if base_k == 0 {
        ((n_obs as f32).sqrt() * 0.5).round() as usize
    } else {
        base_k
    };
    let r = n_sim as f32 / n_obs as f32;
    let adj_k = (k as f32 * (1.0 + r)).round() as usize;

    adj_k.min(MAX_DOUBLET_K_NEIGHBOURS)
}

////////////////////////
// Doublet simulation //
////////////////////////

/// Create simulated doublet chunks from explicit cell pairs.
///
/// Core simulation shared across all doublet detection methods. Each pair
/// of cell positions is read from disk, their HVG counts are summed,
/// normalised to `target_size` using the combined HVG library size, and
/// gene indices are remapped to contiguous HVG positions. Pair *selection*
/// strategy differs across methods (random for Scrublet/Boost,
/// cluster-aware for ScDblFinder) but chunk creation is identical.
///
/// ### Params
///
/// * `pairs` - Slice of `(pos_a, pos_b)` tuples indexing into
///   `cells_to_keep`.
/// * `cells_to_keep` - Original cell indices for disk retrieval.
/// * `hvg_library_sizes` - Per-cell library sizes over HVG genes, parallel
///   to `cells_to_keep`.
/// * `hvg_genes` - Sorted gene indices of the selected HVGs.
/// * `reader` - Reader over the cell-based count store.
/// * `target_size` - Normalisation target library size.
/// * `log_transform` - Whether to apply `ln(1 + x)` after normalisation.
///
/// ### Returns
///
/// Vector of `CsrCellChunk`, one per pair, with gene indices remapped to
/// contiguous HVG positions (0-based).
pub fn simulate_from_pairs<S: SingleCellReading>(
    pairs: &[(usize, usize)],
    cells_to_keep: &[usize],
    hvg_library_sizes: &[usize],
    hvg_genes: &[usize],
    reader: &S,
    target_size: f32,
    log_transform: bool,
) -> Result<Vec<CsrCellChunk>, BixverseErrors> {
    let hvg_set: FxHashSet<usize> = hvg_genes.iter().copied().collect();
    let gene_to_hvg_idx: FxHashMap<usize, u32> = hvg_genes
        .iter()
        .enumerate()
        .map(|(hvg_idx, &orig_idx)| (orig_idx, hvg_idx as u32))
        .collect();

    pairs
        .par_iter()
        .enumerate()
        .map(|(doublet_idx, &(pos_i, pos_j))| {
            let cell1 = reader.read_cell(cells_to_keep[pos_i])?;
            let cell2 = reader.read_cell(cells_to_keep[pos_j])?;

            let hvg_combined_lib_size = hvg_library_sizes[pos_i] + hvg_library_sizes[pos_j];

            let mut doublet = CsrCellChunk::add_cells_scrublet(
                &cell1,
                &cell2,
                &hvg_set,
                hvg_combined_lib_size,
                target_size,
                log_transform,
                doublet_idx,
            );

            for idx in doublet.indices.iter_mut() {
                *idx = gene_to_hvg_idx[&(*idx as usize)];
            }

            Ok(doublet)
        })
        .collect()
}

/////////////////////
// Pair generation //
/////////////////////

/// Generate random cell pairs with replacement.
///
/// Each pair is drawn independently and uniformly from `[0, n_cells)`.
/// A cell can be paired with itself or appear in multiple pairs.
///
/// ### Params
///
/// * `n_cells` - Number of cells to sample from.
/// * `n_pairs` - Number of pairs to generate.
/// * `seed` - Seed for reproducibility.
///
/// ### Returns
///
/// Vector of `(pos_a, pos_b)` tuples, each indexing into the cell
/// population.
pub fn random_pairs(n_cells: usize, n_pairs: usize, seed: usize) -> Vec<(usize, usize)> {
    use rand::prelude::*;
    let mut rng = StdRng::seed_from_u64(seed as u64);
    (0..n_pairs)
        .map(|_| {
            let i = rng.random_range(0..n_cells);
            let j = rng.random_range(0..n_cells);
            (i, j)
        })
        .collect()
}

/////////////////////////
// Threshold detection //
/////////////////////////

/// Find the doublet score threshold using Otsu's method.
///
/// Maximises between-class variance of the observed score distribution to
/// find the optimal binary split. Robust to both bimodal and skewed
/// distributions.
///
/// ### Params
///
/// * `scores_obs` - Doublet scores for observed cells.
/// * `n_bins` - Number of histogram bins (50-100 works well).
///
/// ### Returns
///
/// The threshold score. Cells above this value are called doublets.
pub fn find_threshold_otsu(scores_obs: &[f32], n_bins: usize) -> f32 {
    let (max_score, min_score) = array_max_min(scores_obs);

    if (max_score - min_score).abs() < 1e-6 {
        return (min_score + max_score) / 2.0;
    }

    let bin_width = (max_score - min_score) / n_bins as f32;
    let mut hist = vec![0usize; n_bins];

    for &score in scores_obs {
        let bin = ((score - min_score) / bin_width).floor() as usize;
        hist[bin.min(n_bins - 1)] += 1;
    }

    let total = scores_obs.len() as f32;
    let prob: Vec<f32> = hist.iter().map(|&c| c as f32 / total).collect();

    let mut w0 = 0.0f32;
    let mut sum0 = 0.0f32;
    let total_mean: f32 = prob.iter().enumerate().map(|(i, &p)| i as f32 * p).sum();

    let mut best_variance = 0.0f32;
    let mut best_bin = 0usize;

    for i in 0..n_bins {
        w0 += prob[i];
        if w0 < 1e-10 {
            continue;
        }

        let w1 = 1.0 - w0;
        if w1 < 1e-10 {
            break;
        }

        sum0 += i as f32 * prob[i];

        let mu0 = sum0 / w0;
        let mu1 = (total_mean - sum0) / w1;
        let between_var = w0 * w1 * (mu0 - mu1).powi(2);

        if between_var > best_variance {
            best_variance = between_var;
            best_bin = i;
        }
    }

    min_score + (best_bin as f32 + 0.5) * bin_width
}

/////////////////
// scDblFinder //
/////////////////

////////////////////
// Gene selection //
////////////////////

/// Simple overall-mean ranking path.
///
/// ### Params
///
/// * `gene_chunks` - Slice of the gene chunks for which to get the top N
///   expressed genes.
/// * `n_cells` - Number of total cells
/// * `n_top` - Total number of top genes to return
///
/// ### Returns
///
/// The indices of the genes to take forward
fn select_by_overall_mean(
    gene_chunks: &[CscGeneChunk],
    n_cells: usize,
    n_top: usize,
) -> Vec<usize> {
    let nf = n_cells as f64;
    let means: Vec<f64> = gene_chunks
        .par_iter()
        .map(|chunk| {
            let sum: f64 = (0..chunk.indices.len())
                .map(|i| chunk.data_raw.get(i) as f64)
                .sum();
            sum / nf
        })
        .collect();
    top_n_indices(&means, n_top)
}

/// Per-cluster round-robin selection matching R's selFeatures.
///
/// ### Params
///
/// * `gene_chunks` - Slice of the gene chunks for which to get the top N
///   expressed genes.
/// * `cluster_labels` - Cluster lables <- this will be used to identify the
///   top highly expressed genes across the clusters.
/// * `n_top` - Total number of top genes to return
///
/// ### Returns
///
/// The indices of the genes to take forward
fn select_by_cluster_roundrobin(
    gene_chunks: &[CscGeneChunk],
    cluster_labels: &[usize],
    n_top: usize,
) -> Vec<usize> {
    // build a dense cluster id mapping (original label -> 0..K index)
    let unique_clusters: Vec<usize> = {
        let mut s: FxHashSet<usize> = FxHashSet::default();
        for &c in cluster_labels {
            s.insert(c);
        }
        let mut v: Vec<usize> = s.into_iter().collect();
        v.sort_unstable();
        v
    };
    let n_clusters = unique_clusters.len();

    // degenerate case: one cluster is equivalent to no clusters
    if n_clusters <= 1 {
        return select_by_overall_mean(gene_chunks, cluster_labels.len(), n_top);
    }

    let cluster_idx: FxHashMap<usize, usize> = unique_clusters
        .iter()
        .enumerate()
        .map(|(i, &c)| (c, i))
        .collect();

    // compute per-gene per-cluster sums of raw counts. after
    // filter_selected_cells, chunk.indices[i] is a position
    // into cells_to_keep (== cluster_labels).
    let cluster_sums: Vec<Vec<f64>> = gene_chunks
        .par_iter()
        .map(|chunk| {
            let mut sums = vec![0.0f64; n_clusters];
            for i in 0..chunk.indices.len() {
                let pos = chunk.indices[i] as usize;
                let c_idx = cluster_idx[&cluster_labels[pos]];
                sums[c_idx] += chunk.data_raw.get(i) as f64;
            }
            sums
        })
        .collect();

    let n_genes = gene_chunks.len();

    // for each cluster, rank genes by that cluster's sum descending,
    // take the top n_top indices. This gives us n_clusters lists.
    let per_cluster_rankings: Vec<Vec<usize>> = (0..n_clusters)
        .into_par_iter()
        .map(|c_idx| {
            let col: Vec<f64> = (0..n_genes).map(|g| cluster_sums[g][c_idx]).collect();
            top_n_indices_ordered(&col, n_top)
        })
        .collect();

    // round-robin flatten + dedupe + truncate to n_top
    let mut result = roundrobin_select(&per_cluster_rankings, n_top);

    // Fallback: if clusters have heavy rank overlap and we run out of
    // unique candidates, fill from overall sum ranking. R would return
    // NAs here; this is more robust.
    if result.len() < n_top {
        let totals: Vec<f64> = cluster_sums.iter().map(|sums| sums.iter().sum()).collect();
        let fallback = top_n_indices_ordered(&totals, gene_chunks.len());
        let mut seen: FxHashSet<usize> = result.iter().copied().collect();
        for g in fallback {
            if seen.insert(g) {
                result.push(g);
                if result.len() >= n_top {
                    break;
                }
            }
        }
    }

    result.sort_unstable();
    result
}

/// Per-cluster round-robin selection matching R's selFeatures.
///
/// A streaming version of the HVG.
///
/// ### Params
///
/// * `gene_chunks` - Slice of the gene chunks for which to get the top N
///   expressed genes.
/// * `cluster_labels` - Cluster lables <- this will be used to identify the
///   top highly expressed genes across the clusters.
/// * `n_top` - Total number of top genes to return
///
/// ### Returns
///
/// The indices of the genes to take forward
pub fn select_top_genes_streaming<S: SingleCellReading>(
    reader: &S,
    cells_to_keep: &[usize],
    clusters: Option<&[usize]>,
    n_top: usize,
    verbose: bool,
) -> Result<Vec<usize>, BixverseErrors> {
    let n_total_genes = reader.get_header().total_genes;

    if n_top >= n_total_genes {
        return Ok((0..n_total_genes).collect());
    }
    if cells_to_keep.is_empty() {
        return Ok((0..n_top).collect());
    }

    let cell_set: IndexSet<u32> = cells_to_keep.iter().map(|&x| x as u32).collect();

    // resolve cluster setup. Single cluster -> overall-mean path.
    let cluster_setup: Option<(&[usize], FxHashMap<usize, usize>, usize)> = match clusters {
        Some(labels) => {
            assert_eq!(
                labels.len(),
                cells_to_keep.len(),
                "cluster labels must be parallel to cells_to_keep",
            );
            let mut s: FxHashSet<usize> = FxHashSet::default();
            for &c in labels {
                s.insert(c);
            }
            if s.len() <= 1 {
                None
            } else {
                let mut v: Vec<usize> = s.into_iter().collect();
                v.sort_unstable();
                let n_clusters = v.len();
                let map: FxHashMap<usize, usize> =
                    v.iter().enumerate().map(|(i, &c)| (c, i)).collect();
                Some((labels, map, n_clusters))
            }
        }
        None => None,
    };

    let num_batches = n_total_genes.div_ceil(GENE_BATCH_SIZE);
    let n_cells = cells_to_keep.len();
    let start_total = Instant::now();

    let mut means: Vec<f64> = Vec::new();
    let mut cluster_sums: Vec<Vec<f64>> = Vec::new();
    if cluster_setup.is_some() {
        cluster_sums.reserve(n_total_genes);
    } else {
        means.reserve(n_total_genes);
    }

    for batch_idx in 0..num_batches {
        let start = batch_idx * GENE_BATCH_SIZE;
        let end = ((batch_idx + 1) * GENE_BATCH_SIZE).min(n_total_genes);
        let gene_indices: Vec<usize> = (start..end).collect();
        let mut gene_chunks = reader.read_gene_parallel(&gene_indices)?;
        gene_chunks.par_iter_mut().for_each(|chunk| {
            chunk.filter_selected_cells(&cell_set);
        });

        if let Some((labels, cluster_idx, n_clusters)) = &cluster_setup {
            let batch_sums: Vec<Vec<f64>> = gene_chunks
                .par_iter()
                .map(|chunk| {
                    let mut sums = vec![0.0f64; *n_clusters];
                    for i in 0..chunk.indices.len() {
                        let pos = chunk.indices[i] as usize;
                        let c_idx = cluster_idx[&labels[pos]];
                        sums[c_idx] += chunk.data_raw.get(i) as f64;
                    }
                    sums
                })
                .collect();
            cluster_sums.extend(batch_sums);
        } else {
            let nf = n_cells as f64;
            let batch_means: Vec<f64> = gene_chunks
                .par_iter()
                .map(|chunk| {
                    let sum: f64 = (0..chunk.indices.len())
                        .map(|i| chunk.data_raw.get(i) as f64)
                        .sum();
                    sum / nf
                })
                .collect();
            means.extend(batch_means);
        }

        if verbose {
            report_decile_progress(end, start, n_total_genes, "genes", start_total.elapsed());
        }

        drop(gene_chunks);
    }

    match cluster_setup {
        None => Ok(top_n_indices(&means, n_top)),
        Some((_, _, n_clusters)) => {
            let n_genes = cluster_sums.len();
            let per_cluster_rankings: Vec<Vec<usize>> = (0..n_clusters)
                .into_par_iter()
                .map(|c_idx| {
                    let col: Vec<f64> = (0..n_genes).map(|g| cluster_sums[g][c_idx]).collect();
                    top_n_indices_ordered(&col, n_top)
                })
                .collect();

            let mut result = roundrobin_select(&per_cluster_rankings, n_top);

            if result.len() < n_top {
                let totals: Vec<f64> = cluster_sums.iter().map(|sums| sums.iter().sum()).collect();
                let fallback = top_n_indices_ordered(&totals, n_genes);
                let mut seen: FxHashSet<usize> = result.iter().copied().collect();
                for g in fallback {
                    if seen.insert(g) {
                        result.push(g);
                        if result.len() >= n_top {
                            break;
                        }
                    }
                }
            }

            result.sort_unstable();
            Ok(result)
        }
    }
}

/// Round-robin across per-cluster rankings, collecting unique gene indices
/// until `n_top` are found.
///
/// Iterates rank 0 across all clusters, then rank 1, etc., matching R's
/// `as.numeric(t(apply(..., 2, ...)))` column-major flattening after transpose.
///
/// ### Params
///
/// * `per_cluster_rankings` - Rank on a per cluster basis
/// * `n_top` - Number of genes to return
///
/// ### Returns
///
fn roundrobin_select(per_cluster_rankings: &[Vec<usize>], n_top: usize) -> Vec<usize> {
    let n_clusters = per_cluster_rankings.len();
    let max_rank = per_cluster_rankings
        .iter()
        .map(|v| v.len())
        .max()
        .unwrap_or(0);

    let mut seen: FxHashSet<usize> = FxHashSet::default();
    let mut result: Vec<usize> = Vec::with_capacity(n_top);

    for rank in 0..max_rank {
        for c_idx in 0..n_clusters {
            if rank < per_cluster_rankings[c_idx].len() {
                let g = per_cluster_rankings[c_idx][rank];
                if seen.insert(g) {
                    result.push(g);
                    if result.len() >= n_top {
                        return result;
                    }
                }
            }
        }
    }
    result
}

/// Return the `n_top` indices of the largest values, sorted ascending by index.
///
/// ### Params
///
/// * `values` - The values for which to return the top N indices
/// * `n_top` - Number of top genes to return
///
/// ### Returns
///
/// The indices of the selected genes.
fn top_n_indices(values: &[f64], n_top: usize) -> Vec<usize> {
    let mut result = top_n_indices_ordered(values, n_top);
    result.sort_unstable();
    result
}

/// Like `top_n_indices` but returns them in ranking order
///
/// Ranked descending by value, not sorted by index. Needed for the round-robin
/// step since rank position matters.
///
/// ### Params
///
/// * `values` - The values for which to return the top N indices
/// * `n_top` - Number of top genes to return
///
/// ### Returns
///
/// The indices of the selected genes.
fn top_n_indices_ordered(values: &[f64], n_top: usize) -> Vec<usize> {
    let n = values.len();
    let k = n_top.min(n);
    let mut indexed: Vec<(usize, f64)> = values.iter().copied().enumerate().collect();
    indexed.sort_unstable_by(|a, b| {
        b.1.partial_cmp(&a.1)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.0.cmp(&b.0))
    });
    indexed.into_iter().take(k).map(|(i, _)| i).collect()
}

/////////////////////////////
// Main selection function //
/////////////////////////////

/// Select the top `n_top` genes by mean raw expression, optionally
/// with per-cluster round-robin selection.
///
/// Matches R scDblFinder's `selFeatures()` with `propMarkers=0`. Operates on
/// RAW(!) counts -> library size implicitly drives selection, which is
/// intentional for doublet detection.
///
/// ### Details
///
/// #### With clusters
///
/// For each cluster, ranks genes by that cluster's summed raw counts
/// (descending) and takes the top `n_top`. Then round-robin through ranks,
/// gene at rank 1 from each cluster, then rank 2, etc., deduplicating and
/// stopping at `n_top` unique genes. This ensures each cluster contributes
/// representative features even if its cell count is small.
///
/// #### Without clusters
///
/// Simple top-N by overall mean raw expression.
///
/// ### Params
///
/// * `reader` - Reader over the gene-based count store.
/// * `cells_to_keep` - Cell indices to include.
/// * `clusters` - Optional cluster labels, parallel to `cells_to_keep`. If
///   `None`, falls back to overall mean ranking.
/// * `n_top` - Number of genes to select.
///
/// ### Returns
///
/// Sorted ascending vector of gene indices.
pub fn select_top_genes<S: SingleCellReading>(
    reader: &S,
    cells_to_keep: &[usize],
    clusters: Option<&[usize]>,
    n_top: usize,
) -> Result<Vec<usize>, BixverseErrors> {
    let n_total_genes = reader.get_header().total_genes;

    if n_top >= n_total_genes {
        return Ok((0..n_total_genes).collect());
    }
    if cells_to_keep.is_empty() {
        return Ok((0..n_top).collect());
    }

    let cell_set: IndexSet<u32> = cells_to_keep.iter().map(|&x| x as u32).collect();
    let all_indices: Vec<usize> = (0..n_total_genes).collect();
    let mut gene_chunks: Vec<CscGeneChunk> = reader.read_gene_parallel(&all_indices)?;

    gene_chunks.par_iter_mut().for_each(|chunk| {
        chunk.filter_selected_cells(&cell_set);
    });

    match clusters {
        None => Ok(select_by_overall_mean(
            &gene_chunks,
            cells_to_keep.len(),
            n_top,
        )),
        Some(labels) => {
            assert_eq!(
                labels.len(),
                cells_to_keep.len(),
                "cluster labels must be parallel to cells_to_keep",
            );
            Ok(select_by_cluster_roundrobin(&gene_chunks, labels, n_top))
        }
    }
}

////////////////////////
// Doublet generation //
////////////////////////

/// Parameters controlling scDblFinder-style doublet simulation noise.
#[derive(Clone, Debug)]
pub struct ScDblSimParams {
    /// Fraction of doublets whose counts are Poisson-resampled.
    ///
    /// Each non-zero count `c` is replaced by `Poisson(c)`. This adds
    /// realistic sampling noise matching the stochastic capture process
    /// in droplet-based scRNA-seq. Default: 0.25.
    pub resamp_frac: f32,

    /// Fraction of doublets whose total counts are halved before any
    /// resampling.
    ///
    /// Real doublets share the reagent budget within a single droplet,
    /// so their effective library size is typically less than the sum
    /// of two singlets. Default: 0.25.
    pub half_size_frac: f32,

    /// Fraction of doublets with cluster-based size adjustment.
    ///
    /// The contribution of each parent cell is weighted by the average
    /// of (a) the actual library size ratio and (b) the ratio of
    /// cluster median library sizes. This prevents systematic bias
    /// when combining cells from clusters with very different depths.
    /// The total library size is preserved after reweighting.
    /// Default: 0.25.
    pub adjust_size_frac: f32,
}

impl Default for ScDblSimParams {
    fn default() -> Self {
        Self {
            resamp_frac: 0.25,
            half_size_frac: 0.25,
            adjust_size_frac: 0.25,
        }
    }
}

/// Pre-rolled per-doublet treatment flags.
///
/// Generated deterministically from the master seed before parallel
/// execution, so results are reproducible regardless of thread
/// scheduling.
#[derive(Clone, Debug)]
pub struct DoubletTreatment {
    /// Shall the size be adjusted
    adjust_size: bool,
    /// Shall the size be halved
    half_size: bool,
    /// Shall the counts be resampled via Poisson sampling
    resamp: bool,
    /// Per-doublet seed for Poisson resampling (ensures reproducibility
    /// under parallel execution).
    rng_seed: u64,
}

/////////////
// Helpers //
/////////////

/// Sample from Poisson(lambda) using the inverse transform method.
///
/// O(lambda) expected time. For lambda > 500, uses the normal
/// approximation with continuity correction to avoid excessive
/// iteration. Both regimes are adequate for scRNA-seq count data.
///
/// ### Params
///
/// * `rng` - Random number generator
/// * `lambda` - Lambda value
///
/// ### Returns
///
/// The poisson value for that lambda
fn poisson_sample(rng: &mut impl Rng, lambda: f64) -> u32 {
    if lambda <= 0.0 {
        return 0;
    }
    if lambda > 500.0 {
        // Box-Muller for normal variate
        let u1: f64 = rng.random::<f64>().max(1e-300);
        let u2: f64 = rng.random::<f64>();
        let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
        let val = lambda + lambda.sqrt() * z;
        return val.round().max(0.0) as u32;
    }
    let l = (-lambda).exp();
    let mut k = 0u32;
    let mut p = 1.0f64;
    loop {
        p *= rng.random::<f64>();
        if p < l {
            return k;
        }
        k += 1;
    }
}

/// Compute median library size per cluster.
///
/// Returns a map from cluster label to the median selected gene library size of
/// cells in that cluster. Used for size-adjusted doublet generation.
///
/// ### Params
///
/// * `cluster_labels` - The labels for the clusters
/// * `selected_gene_library_sizes` - Library sizes of the selected genes
///
/// ### Returns
///
/// HashMap with clusters and their median library sizes
fn compute_cluster_median_lib_sizes(
    cluster_labels: &[usize],
    selected_genes_library_sizes: &[usize],
) -> FxHashMap<usize, f64> {
    let mut per_cluster: FxHashMap<usize, Vec<f64>> = FxHashMap::default();
    for (i, &cl) in cluster_labels.iter().enumerate() {
        per_cluster
            .entry(cl)
            .or_default()
            .push(selected_genes_library_sizes[i] as f64);
    }
    per_cluster
        .into_iter()
        .map(|(cl, mut sizes)| {
            sizes.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
            let med = if sizes.len() % 2 == 0 {
                (sizes[sizes.len() / 2 - 1] + sizes[sizes.len() / 2]) / 2.0
            } else {
                sizes[sizes.len() / 2]
            };
            (cl, med.max(1.0))
        })
        .collect()
}

////////////////
// Core logic //
////////////////

/// Generate a single doublet's raw counts from two dense parent count vectors.
///
/// This is the testable core of the simulation pipeline. It operates on dense
/// gene-count vectors (length = number of selected genes) and applies the full
/// R-matching noise model from scDblFinder.
///
/// ### Params
///
/// * `parent_a` - Dense selected genes count vector for parent cell A.
/// * `parent_b` - Dense selected genes count vector for parent cell B.
/// * `lib_a` - Selected genes library size of parent A.
/// * `lib_b` - Selected genes library size of parent B.
/// * `cluster_a` - Cluster label of parent A.
/// * `cluster_b` - Cluster label of parent B.
/// * `cluster_medians` - Median selected gene library size per cluster.
/// * `treatment` - Which noise treatments to apply.
///
/// ### Returns
///
/// `(counts, effective_library_size)` where `counts` is a dense
/// vector of noise-injected raw counts.
#[allow(clippy::too_many_arguments)]
pub fn generate_single_doublet(
    parent_a: &[u32],
    parent_b: &[u32],
    lib_a: usize,
    lib_b: usize,
    cluster_a: usize,
    cluster_b: usize,
    cluster_medians: &FxHashMap<usize, f64>,
    treatment: &DoubletTreatment,
) -> (Vec<u32>, usize) {
    let n_genes = parent_a.len();
    debug_assert_eq!(parent_b.len(), n_genes);

    let combined_lib = (lib_a + lib_b) as f64;
    let mut counts_f64 = vec![0.0f64; n_genes];

    if treatment.adjust_size {
        // Cluster-based size adjustment (R's adjustSize logic)
        //
        // factor = average of:
        //   (1) actual library size ratio: lib_a / (lib_a + lib_b)
        //   (2) cluster median ratio: med_a / (med_a + med_b)
        // Clamped to [0.2, 0.8].
        //
        // doublet = parent_a * factor + parent_b * (1 - factor)
        // Then rescaled so total counts = lib_a + lib_b.
        let actual_ratio = lib_a as f64 / combined_lib;
        let med_a = cluster_medians.get(&cluster_a).copied().unwrap_or(1.0);
        let med_b = cluster_medians.get(&cluster_b).copied().unwrap_or(1.0);
        let cluster_ratio = med_a / (med_a + med_b);
        let factor = ((actual_ratio + cluster_ratio) / 2.0).clamp(0.2, 0.8);

        for g in 0..n_genes {
            counts_f64[g] = parent_a[g] as f64 * factor + parent_b[g] as f64 * (1.0 - factor);
        }

        // Rescale to preserve combined library size
        let raw_sum: f64 = counts_f64.iter().sum();
        if raw_sum > 0.0 {
            let scale = combined_lib / raw_sum;
            for v in counts_f64.iter_mut() {
                *v *= scale;
            }
        }
    } else {
        // Simple sum (no adjustment)
        for g in 0..n_genes {
            counts_f64[g] = parent_a[g] as f64 + parent_b[g] as f64;
        }
    }

    // Library size halving
    if treatment.half_size {
        for v in counts_f64.iter_mut() {
            *v *= 0.5;
        }
    }

    // Poisson resampling or rounding
    let mut rng = StdRng::seed_from_u64(treatment.rng_seed);
    let counts: Vec<u32> = if treatment.resamp {
        counts_f64
            .iter()
            .map(|&v| poisson_sample(&mut rng, v))
            .collect()
    } else {
        counts_f64
            .iter()
            .map(|&v| v.round().max(0.0) as u32)
            .collect()
    };

    let effective_lib: usize = counts.iter().map(|&c| c as usize).sum();
    (counts, effective_lib)
}

impl CsrCellChunk {
    /// Construct a chunk from pre-computed doublet simulation data.
    ///
    /// Unlike `from_data`, this does not recompute normalisation! The caller
    /// has already applied the correct normalisation (with noise-injected raw
    /// counts and the effective library size).
    ///
    /// ### Params
    ///
    /// * `indices` - Selected genes-remapped gene positions (sparse, sorted).
    /// * `raw_counts` - Raw counts at each position (post-noise).
    /// * `norm_values` - Pre-computed normalised values (same length
    ///   as `indices`).
    /// * `effective_lib_size` - Sum of raw counts after all noise
    ///   treatments.
    /// * `doublet_index` - Index of this doublet in the simulation batch (used
    ///   as `original_index`).
    ///
    /// ### Returns
    ///
    /// A `CsrCellChunk` ready for downstream PCA and feature
    /// engineering.
    pub fn from_doublet_simulation(
        indices: Vec<u32>,
        raw_counts: Vec<u32>,
        norm_values: Vec<f32>,
        effective_lib_size: usize,
        doublet_index: usize,
    ) -> Self {
        debug_assert_eq!(indices.len(), raw_counts.len());
        debug_assert_eq!(indices.len(), norm_values.len());

        let data_raw = RawCounts::from_u32_auto(&raw_counts);
        let data_norm: Vec<F16> = norm_values
            .iter()
            .map(|&v| F16::from(f16::from_f32(v)))
            .collect();

        Self {
            data_raw,
            data_norm,
            library_size: effective_lib_size,
            indices,
            original_index: doublet_index,
            to_keep: true,
        }
    }
}

/// Simulate doublets with scDblFinder-style noise injection.
///
/// Replaces `simulate_from_pairs` in the scDblFinder pipeline. Each
/// pair of observed cells is read from disk, their HVG counts are
/// combined (with optional cluster-based size adjustment), then
/// optionally halved and/or Poisson-resampled. The result is stored
/// as sparse chunks with both raw and normalised values, ready for
/// consumption by `pca_combined` and `build_feature_matrix`.
///
/// ### Params
///
/// * `pairs` - `(pos_a, pos_b)` tuples indexing into `cells_to_keep`.
/// * `cells_to_keep` - Original cell indices for disk retrieval.
/// * `hvg_library_sizes` - Per-cell HVG library sizes, parallel to
///   `cells_to_keep`.
/// * `cluster_labels` - Per-cell cluster labels, parallel to
///   `cells_to_keep`.
/// * `hvg_genes` - Sorted gene indices of selected HVGs.
/// * `reader` - Reader over the cell-based count store.
/// * `target_size` - Normalisation target library size.
/// * `log_transform` - Whether to apply `ln(1 + x)` after
///   normalisation.
/// * `params` - Noise injection parameters.
/// * `seed` - Master seed for reproducibility.
///
/// ### Returns
///
/// `(chunks, library_sizes)` where `chunks` contains sparse
/// representations with noise-injected raw counts and normalised
/// values, and `library_sizes` holds the post-noise effective HVG
/// library size for each doublet.
#[allow(clippy::too_many_arguments)]
pub fn simulate_doublets_scdbl<S: SingleCellReading>(
    pairs: &[(usize, usize)],
    cells_to_keep: &[usize],
    selected_genes_library_sizes: &[usize],
    cluster_labels: &[usize],
    selected_genes: &[usize],
    reader: &S,
    target_size: f32,
    log_transform: bool,
    params: &ScDblSimParams,
    seed: usize,
) -> Result<(Vec<CsrCellChunk>, Vec<usize>), BixverseErrors> {
    let n_sim = pairs.len();
    let n_hvg = selected_genes.len();
    let gene_to_hvg_idx: FxHashMap<usize, usize> = selected_genes
        .iter()
        .enumerate()
        .map(|(pos, &orig)| (orig, pos))
        .collect();

    let cluster_medians =
        compute_cluster_median_lib_sizes(cluster_labels, selected_genes_library_sizes);

    // Pre-roll treatments deterministically
    let mut master_rng = StdRng::seed_from_u64(seed as u64 + 0xDEAD);
    let treatments: Vec<DoubletTreatment> = (0..n_sim)
        .map(|_| DoubletTreatment {
            adjust_size: master_rng.random::<f32>() < params.adjust_size_frac,
            half_size: master_rng.random::<f32>() < params.half_size_frac,
            resamp: master_rng.random::<f32>() < params.resamp_frac,
            rng_seed: master_rng.next_u64(),
        })
        .collect();

    let results: Vec<(CsrCellChunk, usize)> = (0..n_sim)
        .into_par_iter()
        .map(|di| -> Result<(CsrCellChunk, usize), BixverseErrors> {
            let (pos_a, pos_b) = pairs[di];
            let cell_a = reader.read_cell(cells_to_keep[pos_a])?;
            let cell_b = reader.read_cell(cells_to_keep[pos_b])?;
            let mut dense_a = vec![0u32; n_hvg];
            let mut dense_b = vec![0u32; n_hvg];
            for (i, &gene_idx) in cell_a.indices.iter().enumerate() {
                let gi = gene_idx as usize;
                if let Some(&hvg_pos) = gene_to_hvg_idx.get(&gi) {
                    dense_a[hvg_pos] = cell_a.data_raw.get(i);
                }
            }
            for (i, &gene_idx) in cell_b.indices.iter().enumerate() {
                let gi = gene_idx as usize;
                if let Some(&hvg_pos) = gene_to_hvg_idx.get(&gi) {
                    dense_b[hvg_pos] = cell_b.data_raw.get(i);
                }
            }
            let (counts, effective_lib) = generate_single_doublet(
                &dense_a,
                &dense_b,
                selected_genes_library_sizes[pos_a],
                selected_genes_library_sizes[pos_b],
                cluster_labels[pos_a],
                cluster_labels[pos_b],
                &cluster_medians,
                &treatments[di],
            );
            let lib_f32 = (effective_lib as f32).max(1.0);
            let mut sp_indices: Vec<u32> = Vec::new();
            let mut sp_raw: Vec<u32> = Vec::new();
            let mut sp_norm: Vec<f32> = Vec::new();
            for (g, &c) in counts.iter().enumerate() {
                if c > 0 {
                    sp_indices.push(g as u32);
                    sp_raw.push(c);
                    let normed = if log_transform {
                        ((c as f32 / lib_f32) * target_size).ln_1p()
                    } else {
                        (c as f32 / lib_f32) * target_size
                    };
                    sp_norm.push(normed);
                }
            }
            let chunk = CsrCellChunk::from_doublet_simulation(
                sp_indices,
                sp_raw,
                sp_norm,
                effective_lib,
                di,
            );
            Ok((chunk, effective_lib))
        })
        .collect::<Result<Vec<_>, _>>()?;

    let mut chunks = Vec::with_capacity(n_sim);
    let mut lib_sizes = Vec::with_capacity(n_sim);
    for (chunk, lib) in results {
        chunks.push(chunk);
        lib_sizes.push(lib);
    }

    Ok((chunks, lib_sizes))
}

/// Parameters for the unrecognisable-origin filter.
#[derive(Clone, Debug)]
pub struct UnrecognisableFilterParams {
    /// Minimum sims per origin to evaluate. R default: 5.
    pub min_size: usize,
    /// Minimum separation between sim median and the worst-case parent/global
    /// median. R default: 0.1.
    pub min_med_diff: f32,
}

impl Default for UnrecognisableFilterParams {
    fn default() -> Self {
        Self {
            min_size: 5,
            min_med_diff: 0.1,
        }
    }
}

/// Canonicalise a parent cluster pair. Returns `None` for homotypic
/// pairs (which R skips via its `grepl("+", ...)` filter).
#[inline]
pub fn canonical_pair(a: usize, b: usize) -> Option<(usize, usize)> {
    if a == b {
        None
    } else {
        Some((a.min(b), a.max(b)))
    }
}

/// Linear-interpolation quantile matching R's `type = 7` default.
///
/// Input must be sorted ascending.
fn quantile_sorted(sorted: &[f32], p: f32) -> f32 {
    if sorted.is_empty() {
        return f32::NAN;
    }
    if sorted.len() == 1 {
        return sorted[0];
    }
    let idx = p.clamp(0.0, 1.0) * (sorted.len() - 1) as f32;
    let lo = idx.floor() as usize;
    let hi = (lo + 1).min(sorted.len() - 1);
    let frac = idx - lo as f32;
    sorted[lo] * (1.0 - frac) + sorted[hi] * frac
}

/// Identify canonical origins whose simulated doublets are indistinguishable
/// from their parent clusters' real cells.
///
/// Flags an origin `(A, B)` if EITHER:
///   (a) sim 10th percentile < max(parent A 90th, parent B 90th)
///   (b) sim median - max(global median, parent A median,
///       parent B median) < min_med_diff
pub fn identify_unrecognisable_origins(
    obs_scores: &[f32],
    obs_cluster_labels: &[usize],
    sim_scores: &[f32],
    sim_parent_clusters: &[(usize, usize)],
    params: &UnrecognisableFilterParams,
) -> FxHashSet<(usize, usize)> {
    assert_eq!(obs_scores.len(), obs_cluster_labels.len());
    assert_eq!(sim_scores.len(), sim_parent_clusters.len());

    // Global median of observed scores
    let global_median = {
        let mut sorted: Vec<f32> = obs_scores.to_vec();
        sorted.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        quantile_sorted(&sorted, 0.5)
    };

    // Per-cluster 50th and 90th percentiles of observed scores
    let mut per_cluster: FxHashMap<usize, Vec<f32>> = FxHashMap::default();
    for (i, &cl) in obs_cluster_labels.iter().enumerate() {
        per_cluster.entry(cl).or_default().push(obs_scores[i]);
    }
    let mut cluster_pcts: FxHashMap<usize, (f32, f32)> = FxHashMap::default();
    for (cl, mut scores) in per_cluster {
        scores.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        cluster_pcts.insert(
            cl,
            (quantile_sorted(&scores, 0.5), quantile_sorted(&scores, 0.9)),
        );
    }

    // Group sim scores by canonical heterotypic origin
    let mut sims_by_origin: FxHashMap<(usize, usize), Vec<f32>> = FxHashMap::default();
    for (si, &(ca, cb)) in sim_parent_clusters.iter().enumerate() {
        if let Some(canon) = canonical_pair(ca, cb) {
            sims_by_origin
                .entry(canon)
                .or_default()
                .push(sim_scores[si]);
        }
    }

    // Evaluate predicate per origin
    let mut flagged: FxHashSet<(usize, usize)> = FxHashSet::default();
    for (origin, mut scores) in sims_by_origin {
        if scores.len() < params.min_size {
            continue;
        }
        scores.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        let sim_p10 = quantile_sorted(&scores, 0.1);
        let sim_p50 = quantile_sorted(&scores, 0.5);

        let fallback = (f32::NEG_INFINITY, f32::NEG_INFINITY);
        let (p50_a, p90_a) = cluster_pcts.get(&origin.0).copied().unwrap_or(fallback);
        let (p50_b, p90_b) = cluster_pcts.get(&origin.1).copied().unwrap_or(fallback);

        let cond_a = sim_p10 < p90_a.max(p90_b);
        let max_reference = global_median.max(p50_a).max(p50_b);
        let cond_b = (sim_p50 - max_reference) < params.min_med_diff;

        if cond_a || cond_b {
            flagged.insert(origin);
        }
    }

    flagged
}

/// Build a per-sim exclusion mask from a set of flagged canonical
/// origins. Homotypic sims are always false (not a valid origin).
pub fn mark_sims_from_flagged_origins(
    sim_parent_clusters: &[(usize, usize)],
    flagged: &FxHashSet<(usize, usize)>,
) -> Vec<bool> {
    sim_parent_clusters
        .iter()
        .map(|&(ca, cb)| {
            canonical_pair(ca, cb)
                .map(|c| flagged.contains(&c))
                .unwrap_or(false)
        })
        .collect()
}

///////////
// Tests //
///////////

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

    /////////////////////
    // Doublet scaling //
    /////////////////////

    /// Build a small set of cell chunks over `n_genes` genes.
    fn scaling_chunks(n_genes: usize) -> Vec<CsrCellChunk> {
        let rows: Vec<Vec<u32>> = vec![
            vec![5, 0, 3, 0, 11, 0, 2, 0],
            vec![0, 7, 0, 1, 0, 4, 0, 9],
            vec![1, 1, 0, 0, 6, 0, 0, 2],
        ];

        rows.iter()
            .enumerate()
            .map(|(cell, row)| {
                let idx: Vec<u32> = (0..n_genes as u32)
                    .filter(|&g| row[g as usize] > 0)
                    .collect();
                let data: Vec<u32> = idx.iter().map(|&g| row[g as usize]).collect();
                CsrCellChunk::from_data(&data, &idx, cell, 1e4, true)
            })
            .collect()
    }

    /// Loadings with no structure worth reading into, just distinct values.
    fn scaling_loadings(n_genes: usize, n_pcs: usize) -> Mat<f32> {
        Mat::from_fn(n_genes, n_pcs, |g, k| {
            ((g * 7 + k * 13) % 11) as f32 * 0.1 - 0.5
        })
    }

    /// The fused projection has to reproduce scale-then-GEMM in every flag
    /// combination, since that is the path it replaced.
    #[test]
    fn test_projection_matches_scale_then_gemm() {
        let n_genes = 8;
        let n_pcs = 4;
        let chunks = scaling_chunks(n_genes);
        let loadings = scaling_loadings(n_genes, n_pcs);

        let gene_means: Vec<f32> = (0..n_genes).map(|g| 0.05 * g as f32 + 0.2).collect();
        let gene_stds: Vec<f32> = (0..n_genes).map(|g| 0.5 + 0.1 * g as f32).collect();

        for &mean_center in &[false, true] {
            for &normalise_variance in &[false, true] {
                let scaled = scale_cell_chunks_with_stats(
                    &chunks,
                    &gene_means,
                    &gene_stds,
                    mean_center,
                    normalise_variance,
                    n_genes,
                );
                let want = &scaled * &loadings;

                let got = project_cell_chunks_with_stats(
                    &chunks,
                    &gene_means,
                    &gene_stds,
                    loadings.as_ref(),
                    mean_center,
                    normalise_variance,
                );

                assert_eq!(got.nrows(), want.nrows());
                assert_eq!(got.ncols(), want.ncols());
                for cell in 0..want.nrows() {
                    for pc in 0..want.ncols() {
                        assert_relative_eq!(
                            got[(cell, pc)],
                            want[(cell, pc)],
                            epsilon = 1e-4,
                            max_relative = 1e-4
                        );
                    }
                }
            }
        }
    }

    /// A zero-column loadings used to flow through the GEMM and yield an
    /// `n_cells x 0` matrix; the fused path must not panic in `par_chunks_mut`.
    #[test]
    fn test_projection_handles_zero_pcs() {
        let n_genes = 8;
        let chunks = scaling_chunks(n_genes);
        let gene_means = vec![0.5_f32; n_genes];
        let gene_stds = vec![1.5_f32; n_genes];
        let loadings = Mat::<f32>::zeros(n_genes, 0);

        let got = project_cell_chunks_with_stats(
            &chunks,
            &gene_means,
            &gene_stds,
            loadings.as_ref(),
            true,
            true,
        );

        assert_eq!(got.nrows(), chunks.len());
        assert_eq!(got.ncols(), 0);
    }

    /// The explicit `n_genes` parameter used to make faer check this at the
    /// GEMM. Deriving the axis from the loadings put the check here instead, and
    /// it has to survive release builds.
    #[test]
    #[should_panic(expected = "gene_means must match the gene axis")]
    fn test_projection_rejects_mismatched_gene_stats() {
        let n_genes = 8;
        let chunks = scaling_chunks(n_genes);
        let gene_means = vec![0.5_f32; n_genes + 1];
        let gene_stds = vec![1.5_f32; n_genes];
        let loadings = scaling_loadings(n_genes, 3);

        let _ = project_cell_chunks_with_stats(
            &chunks,
            &gene_means,
            &gene_stds,
            loadings.as_ref(),
            true,
            true,
        );
    }

    /// Unstored genes take the background value, stored ones the scaled value.
    #[test]
    fn test_scaling_fills_the_background_for_absent_genes() {
        let n_genes = 8;
        let chunks = scaling_chunks(n_genes);
        let gene_means: Vec<f32> = (0..n_genes).map(|g| 0.05 * g as f32 + 0.2).collect();
        let gene_stds: Vec<f32> = (0..n_genes).map(|g| 0.5 + 0.1 * g as f32).collect();

        let scaled =
            scale_cell_chunks_with_stats(&chunks, &gene_means, &gene_stds, true, true, n_genes);

        for (cell, chunk) in chunks.iter().enumerate() {
            let stored: FxHashSet<u32> = chunk.indices.iter().copied().collect();
            for gene in 0..n_genes {
                if stored.contains(&(gene as u32)) {
                    continue;
                }
                assert_relative_eq!(
                    scaled[(cell, gene)],
                    -gene_means[gene] / gene_stds[gene],
                    epsilon = 1e-6
                );
            }
        }
    }

    /// Without mean centring the background is zero, so the matrix stays as
    /// sparse in content as the chunks are.
    #[test]
    fn test_scaling_without_centring_leaves_zeros() {
        let n_genes = 8;
        let chunks = scaling_chunks(n_genes);
        let gene_means = vec![1.0_f32; n_genes];
        let gene_stds = vec![2.0_f32; n_genes];

        let scaled =
            scale_cell_chunks_with_stats(&chunks, &gene_means, &gene_stds, false, true, n_genes);

        for (cell, chunk) in chunks.iter().enumerate() {
            let stored: FxHashSet<u32> = chunk.indices.iter().copied().collect();
            for gene in 0..n_genes {
                if !stored.contains(&(gene as u32)) {
                    assert_eq!(scaled[(cell, gene)], 0.0);
                }
            }
        }
    }

    ////////////////////
    // Feat selection //
    ////////////////////

    /// Indices come back in descending value order, not index order.
    #[test]
    fn test_top_n_ordered_preserves_rank_order() {
        let values = vec![1.0, 5.0, 3.0, 2.0, 4.0];
        assert_eq!(top_n_indices_ordered(&values, 3), vec![1, 4, 2]);
    }

    /// The unordered variant picks the same set but hands it back index-sorted.
    #[test]
    fn test_top_n_indices_sorts_by_index() {
        let values = vec![1.0, 5.0, 3.0, 2.0, 4.0];
        assert_eq!(top_n_indices(&values, 3), vec![1, 2, 4]);
    }

    /// Ties resolve to the lower index, so selection is deterministic.
    #[test]
    fn test_top_n_ordered_ties_prefer_lower_index() {
        let values = vec![5.0, 5.0, 5.0, 1.0];
        assert_eq!(top_n_indices_ordered(&values, 2), vec![0, 1]);
    }

    /// Selection sweeps rank by rank across clusters, not cluster by cluster.
    #[test]
    fn test_roundrobin_basic() {
        // 3 clusters, distinct top picks
        let rankings = vec![vec![10, 20, 30], vec![11, 21, 31], vec![12, 22, 32]];
        let result = roundrobin_select(&rankings, 5);
        assert_eq!(result, vec![10, 11, 12, 20, 21]);
    }

    /// A gene already picked by an earlier cluster is not picked twice.
    #[test]
    fn test_roundrobin_dedupe() {
        // Overlapping rankings
        let rankings = vec![
            vec![1, 2, 3, 4],
            vec![1, 3, 5, 7], // 1 and 3 duplicate
            vec![2, 5, 8, 9], // 2 and 5 duplicate
        ];
        let result = roundrobin_select(&rankings, 5);
        assert_eq!(result, vec![1, 2, 3, 5, 8]);
    }

    /// The sweep stops at `n_top` mid-round rather than finishing the round.
    #[test]
    fn test_roundrobin_stops_at_n_top() {
        let rankings = vec![vec![1, 2, 3], vec![4, 5, 6]];
        let result = roundrobin_select(&rankings, 3);
        assert_eq!(result, vec![1, 4, 2]);
        assert_eq!(result.len(), 3);
    }

    /// Clusters that run out of ranks are skipped, not treated as the end of the sweep.
    #[test]
    fn test_roundrobin_small_per_cluster_lists() {
        // Some clusters have fewer ranks than others
        let rankings = vec![vec![1, 2], vec![3], vec![4, 5, 6]];
        let result = roundrobin_select(&rankings, 10);
        assert_eq!(result, vec![1, 3, 4, 2, 5, 6]);
    }

    /// Too few unique genes returns a short list rather than padding or looping.
    #[test]
    fn test_roundrobin_insufficient_unique() {
        // All clusters have the same top picks; result is smaller than n_top
        let rankings = vec![vec![1, 2, 3], vec![1, 2, 3], vec![1, 2, 3]];
        let result = roundrobin_select(&rankings, 10);
        assert_eq!(result, vec![1, 2, 3]); // only 3 unique available
    }

    /// No clusters is an empty result, not a panic on the first round.
    #[test]
    fn test_roundrobin_empty() {
        let rankings: Vec<Vec<usize>> = vec![];
        let result = roundrobin_select(&rankings, 5);
        assert_eq!(result, Vec::<usize>::new());
    }

    /////////////////
    // Doublet gen //
    /////////////////

    /// Verify Poisson sampler produces correct mean and variance
    /// for a range of lambda values.
    #[test]
    fn test_poisson_sampler_statistics() {
        let mut rng = StdRng::seed_from_u64(42);
        for &lambda in &[0.5, 2.0, 10.0, 50.0, 200.0, 1000.0] {
            // 5000 rather than 50000, which was six seconds of the crate's test
            // time. The tolerances below are dominated by their relative terms,
            // 5% on the mean and 10% on the variance, and the sampling error at
            // this count is `sqrt(2/n) = 2%` on the variance. So the bounds are
            // unchanged in practice and a broken sampler still cannot hide.
            let n = 5_000;
            let samples: Vec<u32> = (0..n).map(|_| poisson_sample(&mut rng, lambda)).collect();
            let mean = samples.iter().map(|&s| s as f64).sum::<f64>() / n as f64;
            let var = samples
                .iter()
                .map(|&s| {
                    let d = s as f64 - mean;
                    d * d
                })
                .sum::<f64>()
                / n as f64;

            // Poisson: E[X] = Var[X] = lambda
            let tol = 3.0 * (lambda / n as f64).sqrt(); // ~3 SE
            assert!(
                (mean - lambda).abs() < lambda * 0.05 + tol,
                "lambda={}: mean={:.2}, expected={:.2}",
                lambda,
                mean,
                lambda
            );
            assert!(
                (var - lambda).abs() < lambda * 0.1 + tol * lambda,
                "lambda={}: var={:.2}, expected={:.2}",
                lambda,
                var,
                lambda
            );
        }
    }

    /// Zero and negative rates return zero rather than looping or drawing.
    #[test]
    fn test_poisson_sampler_zero() {
        let mut rng = StdRng::seed_from_u64(99);
        for _ in 0..100 {
            assert_eq!(poisson_sample(&mut rng, 0.0), 0);
            assert_eq!(poisson_sample(&mut rng, -1.0), 0);
        }
    }

    /// Medians are per cluster, including even-sized and single-cell clusters.
    #[test]
    fn test_cluster_median_lib_sizes() {
        let clusters = vec![0, 0, 0, 1, 1, 2];
        let lib_sizes = vec![100, 200, 300, 50, 150, 500];
        let medians = compute_cluster_median_lib_sizes(&clusters, &lib_sizes);

        assert!((medians[&0] - 200.0).abs() < 1e-6); // median of [100, 200, 300]
        assert!((medians[&1] - 100.0).abs() < 1e-6); // median of [50, 150]
        assert!((medians[&2] - 500.0).abs() < 1e-6); // single value
    }

    /// With all treatments disabled, output should be the exact sum
    /// of parent counts.
    #[test]
    fn test_no_treatment_is_simple_sum() {
        let parent_a = vec![0, 5, 10, 0, 3];
        let parent_b = vec![2, 0, 7, 1, 0];
        let cluster_medians = FxHashMap::default();
        let treatment = DoubletTreatment {
            adjust_size: false,
            half_size: false,
            resamp: false,
            rng_seed: 0,
        };

        let (counts, lib) = generate_single_doublet(
            &parent_a,
            &parent_b,
            18,
            10,
            0,
            1,
            &cluster_medians,
            &treatment,
        );

        assert_eq!(counts, vec![2, 5, 17, 1, 3]);
        assert_eq!(lib, 28);
    }

    /// Library size halving should produce ~0.5x the sum.
    #[test]
    fn test_half_size_halves_library() {
        let parent_a = vec![10, 20, 30, 40];
        let parent_b = vec![10, 20, 30, 40];
        let cluster_medians = FxHashMap::default();

        // Without halving
        let no_half = DoubletTreatment {
            adjust_size: false,
            half_size: false,
            resamp: false,
            rng_seed: 0,
        };
        let (_, lib_full) = generate_single_doublet(
            &parent_a,
            &parent_b,
            100,
            100,
            0,
            0,
            &cluster_medians,
            &no_half,
        );

        // With halving
        let with_half = DoubletTreatment {
            adjust_size: false,
            half_size: true,
            resamp: false,
            rng_seed: 0,
        };
        let (_, lib_half) = generate_single_doublet(
            &parent_a,
            &parent_b,
            100,
            100,
            0,
            0,
            &cluster_medians,
            &with_half,
        );

        assert_eq!(lib_full, 200);
        assert_eq!(lib_half, 100); // 200 / 2 = 100
    }

    /// Poisson resampling should add noise: repeated doublets from the same
    /// parents should NOT be identical.
    #[test]
    fn test_resamp_adds_noise() {
        let parent_a = vec![10, 20, 30, 40, 50];
        let parent_b = vec![5, 15, 25, 35, 45];
        let cluster_medians = FxHashMap::default();

        let mut all_equal = true;
        let mut first: Option<Vec<u32>> = None;

        for seed in 0..20u64 {
            let treatment = DoubletTreatment {
                adjust_size: false,
                half_size: false,
                resamp: true,
                rng_seed: seed * 7919 + 42,
            };
            let (counts, _) = generate_single_doublet(
                &parent_a,
                &parent_b,
                135,
                125,
                0,
                0,
                &cluster_medians,
                &treatment,
            );
            if let Some(ref f) = first {
                if &counts != f {
                    all_equal = false;
                    break;
                }
            } else {
                first = Some(counts);
            }
        }

        assert!(
            !all_equal,
            "Poisson resampling should produce different counts across seeds"
        );
    }

    /// Poisson resampling should preserve mean counts (law of large
    /// numbers).
    #[test]
    fn test_resamp_preserves_mean() {
        let parent_a = vec![0, 10, 50, 100];
        let parent_b = vec![5, 10, 50, 100];
        let expected_sum = [5, 20, 100, 200];
        let cluster_medians = FxHashMap::default();
        let n_reps = 5000;

        let mut accum = [0.0f64; 4];
        for rep in 0..n_reps {
            let treatment = DoubletTreatment {
                adjust_size: false,
                half_size: false,
                resamp: true,
                rng_seed: rep as u64 * 31 + 7,
            };
            let (counts, _) = generate_single_doublet(
                &parent_a,
                &parent_b,
                160,
                165,
                0,
                0,
                &cluster_medians,
                &treatment,
            );
            for (i, &c) in counts.iter().enumerate() {
                accum[i] += c as f64;
            }
        }

        for i in 0..4 {
            let mean = accum[i] / n_reps as f64;
            let expected = expected_sum[i] as f64;
            let tol = 3.0 * (expected / n_reps as f64).sqrt().max(0.5);
            assert!(
                (mean - expected).abs() < expected * 0.05 + tol,
                "gene {}: mean={:.2}, expected={:.2}",
                i,
                mean,
                expected
            );
        }
    }

    /// Size adjustment should preserve total library size while
    /// changing per-gene contributions.
    #[test]
    fn test_adjust_size_preserves_total_library() {
        let parent_a = vec![10, 20, 30, 40];
        let parent_b = vec![40, 30, 20, 10];
        let lib_a = 100;
        let lib_b = 100;

        let mut cluster_medians = FxHashMap::default();
        // Cluster 0 has 2x the median lib size of cluster 1
        cluster_medians.insert(0, 200.0);
        cluster_medians.insert(1, 100.0);

        let treatment = DoubletTreatment {
            adjust_size: true,
            half_size: false,
            resamp: false,
            rng_seed: 0,
        };

        let (counts, lib) = generate_single_doublet(
            &parent_a,
            &parent_b,
            lib_a,
            lib_b,
            0,
            1,
            &cluster_medians,
            &treatment,
        );

        // Total should be preserved (lib_a + lib_b = 200)
        assert_eq!(lib, lib_a + lib_b);

        // With asymmetric cluster medians, gene contributions should
        // differ from the simple sum
        let simple_sum: Vec<u32> = parent_a
            .iter()
            .zip(&parent_b)
            .map(|(&a, &b)| a + b)
            .collect();
        assert_ne!(
            counts, simple_sum,
            "adjusted counts should differ from simple sum"
        );
    }

    /// Size adjustment mixing factor should be clamped to [0.2, 0.8].
    #[test]
    fn test_adjust_size_clamping() {
        // Extreme library size ratio, but enough counts in parent_a
        // that clamping to 0.2 produces visible contribution.
        let parent_a = vec![100, 0, 0, 0];
        let parent_b = vec![0, 0, 0, 10000];
        let mut cluster_medians = FxHashMap::default();
        cluster_medians.insert(0, 1.0);
        cluster_medians.insert(1, 10000.0);

        let treatment = DoubletTreatment {
            adjust_size: true,
            half_size: false,
            resamp: false,
            rng_seed: 0,
        };

        let (counts, lib) = generate_single_doublet(
            &parent_a,
            &parent_b,
            100,
            10000,
            0,
            1,
            &cluster_medians,
            &treatment,
        );

        // Total library size preserved
        assert_eq!(lib, 10100);

        // Without clamping, factor ≈ 0.005 and gene 0 would get ~5 counts.
        // With clamping to 0.2, gene 0 gets: 100 * 0.2 = 20, rescaled by
        // 10100 / (20 + 8000) ≈ 1.26, so ~25 counts.
        assert!(
            counts[0] > 10,
            "clamping should give parent_a at least 20% weight; gene 0 got {}",
            counts[0]
        );

        // Gene 3 should get the bulk
        assert!(
            counts[3] > counts[0],
            "gene 3 ({}) should still dominate gene 0 ({})",
            counts[3],
            counts[0]
        );
    }

    /// Over many doublets with noise, library size distribution
    /// should NOT be a single spike at exactly 2x median.
    #[test]
    fn test_library_size_distribution_has_variance() {
        let parent = vec![10, 20, 30, 40, 50]; // lib = 150
        let cluster_medians = FxHashMap::default();
        let params = ScDblSimParams::default();
        let n = 1000;

        let mut master_rng = StdRng::seed_from_u64(123);
        let treatments: Vec<DoubletTreatment> = (0..n)
            .map(|_| DoubletTreatment {
                adjust_size: master_rng.random::<f32>() < params.adjust_size_frac,
                half_size: master_rng.random::<f32>() < params.half_size_frac,
                resamp: master_rng.random::<f32>() < params.resamp_frac,
                rng_seed: master_rng.next_u64(),
            })
            .collect();

        let lib_sizes: Vec<usize> = treatments
            .iter()
            .map(|t| {
                let (_, lib) =
                    generate_single_doublet(&parent, &parent, 150, 150, 0, 0, &cluster_medians, t);
                lib
            })
            .collect();

        let mean = lib_sizes.iter().sum::<usize>() as f64 / n as f64;
        let var = lib_sizes
            .iter()
            .map(|&l| {
                let d = l as f64 - mean;
                d * d
            })
            .sum::<f64>()
            / n as f64;

        // Without noise, all lib sizes would be exactly 300.
        // With noise (halving + resampling), we expect substantial
        // variance.
        let n_at_300 = lib_sizes.iter().filter(|&&l| l == 300).count();
        assert!(
            n_at_300 < n * 9 / 10,
            "expected fewer than 90% at exactly 2x; got {}/{} at 300",
            n_at_300,
            n
        );
        assert!(
            var > 100.0,
            "expected substantial variance in lib sizes; got var={:.1}",
            var
        );
    }

    /// The same seed gives the same doublet, so runs are reproducible.
    #[test]
    fn test_deterministic_with_same_seed() {
        let parent_a = vec![5, 10, 15, 20];
        let parent_b = vec![8, 12, 3, 25];
        let cluster_medians = FxHashMap::default();

        let treatment = DoubletTreatment {
            adjust_size: false,
            half_size: true,
            resamp: true,
            rng_seed: 42,
        };

        let (c1, l1) = generate_single_doublet(
            &parent_a,
            &parent_b,
            50,
            48,
            0,
            1,
            &cluster_medians,
            &treatment,
        );
        let (c2, l2) = generate_single_doublet(
            &parent_a,
            &parent_b,
            50,
            48,
            0,
            1,
            &cluster_medians,
            &treatment,
        );

        assert_eq!(c1, c2);
        assert_eq!(l1, l2);
    }

    /////////////////////////////
    // Unrecognisable doublets //
    /////////////////////////////

    /// Quantiles at the ends and the middle of an odd-length sorted slice.
    #[test]
    fn test_quantile_basic() {
        let s = vec![0.1, 0.2, 0.3, 0.4, 0.5];
        assert!((quantile_sorted(&s, 0.0) - 0.1).abs() < 1e-6);
        assert!((quantile_sorted(&s, 0.5) - 0.3).abs() < 1e-6);
        assert!((quantile_sorted(&s, 1.0) - 0.5).abs() < 1e-6);
    }

    /// Quantiles between two samples interpolate linearly rather than rounding to one.
    #[test]
    fn test_quantile_interpolation() {
        let s = vec![0.0, 10.0];
        assert!((quantile_sorted(&s, 0.5) - 5.0).abs() < 1e-6);
        assert!((quantile_sorted(&s, 0.1) - 1.0).abs() < 1e-6);
    }

    /// Empty input gives NaN and a single value gives itself, rather than indexing out of range.
    #[test]
    fn test_quantile_empty_and_single() {
        assert!(quantile_sorted(&[], 0.5).is_nan());
        assert_eq!(quantile_sorted(&[3.15], 0.5), 3.15);
    }

    /// Parent pairs are sorted and homotypic ones dropped, so origins key consistently.
    #[test]
    fn test_canonical_pair_ordering() {
        assert_eq!(canonical_pair(2, 5), Some((2, 5)));
        assert_eq!(canonical_pair(5, 2), Some((2, 5)));
        assert_eq!(canonical_pair(3, 3), None);
    }

    /// An origin below `min_size` is left alone, since its quantiles mean nothing.
    #[test]
    fn test_too_few_sims_not_flagged() {
        // Only 4 sims for origin (0,1) -- below min_size=5
        let obs = vec![0.1; 10];
        let clust: Vec<usize> = (0..10).map(|i| i % 2).collect();
        let sim = vec![0.1, 0.15, 0.2, 0.25];
        let sp = vec![(0, 1); 4];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.is_empty());
    }

    /// Simulated doublets that sit well above the real cells stay usable.
    #[test]
    fn test_clear_separation_not_flagged() {
        // Obs at 0.1, sim at 0.9+
        let obs = vec![0.1; 20];
        let clust: Vec<usize> = (0..20).map(|i| i % 2).collect();
        let sim = vec![0.9, 0.92, 0.95, 0.97, 0.99, 0.88, 0.91, 0.94];
        let sp = vec![(0, 1); 8];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.is_empty());
    }

    /// One noisy parent cluster is enough to flag the origin; the check is over both.
    #[test]
    fn test_cond_a_triggers_via_any_parent() {
        // Cluster 0 has noisy real cells (90th pct = 0.96)
        // Cluster 1 is clean
        // Sim (0,1) 10th pct ~0.57 < 0.96 -> cond_a triggers via cluster 0
        let obs = vec![
            0.6, 0.7, 0.8, 0.9, 1.0, // cluster 0 high
            0.1, 0.1, 0.1, 0.1, 0.1, // cluster 1 low
        ];
        let clust = vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
        let sim = vec![0.5, 0.6, 0.65, 0.7, 0.75, 0.6, 0.65, 0.7];
        let sp = vec![(0, 1); 8];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.contains(&(0, 1)));
    }

    /// The median-proximity condition flags on its own, with the tail condition quiet.
    #[test]
    fn test_cond_b_triggers_on_median_proximity() {
        // Designed so cond_a does NOT trigger (sim 10th > max parent 90th)
        // but sim median is within 0.1 of worst parent median.
        // Cluster 0: all 0.1 -> p50=0.1, p90=0.1
        // Cluster 1: all 0.15 -> p50=0.15, p90=0.15
        // Sim 10th ~ 0.207 > 0.15 (cond_a false)
        // Sim 50th ~ 0.235, max ref = 0.15, diff 0.085 < 0.1 (cond_b true)
        let obs = vec![0.1, 0.1, 0.1, 0.1, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15];
        let clust = vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
        let sim = vec![0.20, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27];
        let sp = vec![(0, 1); 8];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.contains(&(0, 1)));
    }

    /// Reversed parent pairs pool into one origin instead of two half-sized ones.
    #[test]
    fn test_canonical_combines_reversed_pairs() {
        // Alternating (0,1) and (1,0) should combine into one origin
        // of 8 sims, triggering cond_b.
        let obs = vec![0.1, 0.1, 0.1, 0.1, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15];
        let clust = vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
        let sim = vec![0.20, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27];
        let sp = vec![
            (0, 1),
            (1, 0),
            (0, 1),
            (1, 0),
            (0, 1),
            (1, 0),
            (0, 1),
            (1, 0),
        ];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.contains(&(0, 1)));
    }

    /// Homotypic origins never reach the flagging logic.
    #[test]
    fn test_homotypic_ignored() {
        // All sims have homotypic origin (0,0) -- skipped entirely
        let obs = vec![0.1; 10];
        let clust = vec![0; 10];
        let sim = vec![0.1; 8];
        let sp = vec![(0, 0); 8];
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.is_empty());
    }

    /// Origins are judged one at a time, so a bad one does not take a good one with it.
    #[test]
    fn test_mixed_flagged_and_kept() {
        // (0,1) clearly separated (kept)
        // (2,3) overlaps cluster 2's high-scoring cells (flagged)
        let obs = vec![
            0.1, 0.1, 0.1, 0.1, 0.1, // cluster 0
            0.1, 0.1, 0.1, 0.1, 0.1, // cluster 1
            0.6, 0.7, 0.8, 0.9, 1.0, // cluster 2 (high)
            0.1, 0.1, 0.1, 0.1, 0.1, // cluster 3
        ];
        let clust = vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3];
        let sim = vec![
            0.90, 0.92, 0.95, 0.97, 0.99, 0.91, 0.93, 0.96, 0.50, 0.60, 0.65, 0.70, 0.75, 0.60,
            0.65, 0.70,
        ];
        let sp: Vec<(usize, usize)> = std::iter::repeat_n((0, 1), 8)
            .chain(std::iter::repeat_n((2, 3), 8))
            .collect();
        let flagged = identify_unrecognisable_origins(&obs, &clust, &sim, &sp, &Default::default());
        assert!(flagged.contains(&(2, 3)));
        assert!(!flagged.contains(&(0, 1)));
    }

    /// The mask is built from canonical pairs, so (1,0) is marked by a flagged (0,1).
    #[test]
    fn test_mark_sims_from_flagged_origins_canonical() {
        // Flagged set contains (0,1); sims with either (0,1) or (1,0)
        // should both be marked. (2,3) is not flagged. Homotypic (5,5) false.
        let sp = vec![(0, 1), (2, 3), (1, 0), (3, 2), (5, 5)];
        let mut flagged = FxHashSet::default();
        flagged.insert((0, 1));
        let mask = mark_sims_from_flagged_origins(&sp, &flagged);
        assert_eq!(mask, vec![true, false, true, false, false]);
    }

    /// Empty inputs return an empty flag set rather than panicking on a quantile.
    #[test]
    fn test_empty_inputs() {
        let flagged = identify_unrecognisable_origins(&[], &[], &[], &[], &Default::default());
        assert!(flagged.is_empty());
    }
}