Skip to content

Data API

Data conversion utilities for SLAF format.

SLAFConverter

slaf.data.converter.SLAFConverter

Convert single-cell data formats to SLAF format with optimized storage.

SLAFConverter provides efficient conversion from various single-cell data formats (primarily AnnData/h5ad) to the SLAF format. It optimizes storage by using integer keys, COO-style expression tables, and efficient metadata handling. Chunked conversion is now the default for optimal memory efficiency.

Key Features
  • AnnData/h5ad file conversion
  • Integer key optimization for memory efficiency
  • COO-style sparse matrix storage
  • Automatic metadata type inference
  • Lance format for high-performance storage
  • Chunked processing by default for memory efficiency

Examples:

>>> # Basic conversion from h5ad file (chunked is now the default)
>>> converter = SLAFConverter()
>>> converter.convert("data.h5ad", "output.slaf")
Converting data.h5ad to SLAF format...
Optimizations: int_keys=True
Loaded: 1000 cells × 20000 genes
Conversion complete! Saved to output.slaf
>>> # Conversion with custom optimization settings
>>> converter = SLAFConverter(use_integer_keys=False)
>>> converter.convert("data.h5ad", "output_string_keys.slaf")
Converting data.h5ad to SLAF format...
Optimizations: int_keys=False
Loaded: 1000 cells × 20000 genes
Conversion complete! Saved to output_string_keys.slaf
>>> # Convert existing AnnData object (chunked is now the default)
>>> import scanpy as sc
>>> adata = sc.read_h5ad("data.h5ad")
>>> converter = SLAFConverter()
>>> converter.convert_anndata(adata, "output_from_object.slaf")
Converting AnnData object to SLAF format...
Optimizations: int_keys=True
Loaded: 1000 cells × 20000 genes
Conversion complete! Saved to output_from_object.slaf
Source code in slaf/data/converter.py
  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
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
class SLAFConverter:
    """
    Convert single-cell data formats to SLAF format with optimized storage.

    SLAFConverter provides efficient conversion from various single-cell data formats
    (primarily AnnData/h5ad) to the SLAF format. It optimizes storage by using
    integer keys, COO-style expression tables, and efficient metadata handling.
    Chunked conversion is now the default for optimal memory efficiency.

    Key Features:
        - AnnData/h5ad file conversion
        - Integer key optimization for memory efficiency
        - COO-style sparse matrix storage
        - Automatic metadata type inference
        - Lance format for high-performance storage
        - Chunked processing by default for memory efficiency

    Examples:
        >>> # Basic conversion from h5ad file (chunked is now the default)
        >>> converter = SLAFConverter()
        >>> converter.convert("data.h5ad", "output.slaf")
        Converting data.h5ad to SLAF format...
        Optimizations: int_keys=True
        Loaded: 1000 cells × 20000 genes
        Conversion complete! Saved to output.slaf

        >>> # Conversion with custom optimization settings
        >>> converter = SLAFConverter(use_integer_keys=False)
        >>> converter.convert("data.h5ad", "output_string_keys.slaf")
        Converting data.h5ad to SLAF format...
        Optimizations: int_keys=False
        Loaded: 1000 cells × 20000 genes
        Conversion complete! Saved to output_string_keys.slaf

        >>> # Convert existing AnnData object (chunked is now the default)
        >>> import scanpy as sc
        >>> adata = sc.read_h5ad("data.h5ad")
        >>> converter = SLAFConverter()
        >>> converter.convert_anndata(adata, "output_from_object.slaf")
        Converting AnnData object to SLAF format...
        Optimizations: int_keys=True
        Loaded: 1000 cells × 20000 genes
        Conversion complete! Saved to output_from_object.slaf
    """

    def __init__(
        self,
        use_integer_keys: bool = True,
        chunked: bool = True,  # Changed from False to True - make chunked the default
        chunk_size: int = 5000,  # Smaller chunks to avoid memory alignment issues in Lance v2.1
        sort_metadata: bool = False,
        create_indices: bool = False,  # Disable indices by default for small datasets
        optimize_storage: bool = True,  # Only store integer IDs in expression table
        use_optimized_dtypes: bool = True,  # Use uint16/uint32 for better compression
        enable_v2_manifest: bool = True,  # Enable v2 manifest paths for better performance
        compact_after_write: bool = False,  # Compact dataset after writing for optimal storage (disabled by default to avoid manifest corruption)
        tiledb_collection_name: str = "RNA",  # Collection name for TileDB format
        enable_checkpointing: bool = True,  # Enable checkpointing for long-running conversions
    ):
        """
        Initialize converter with optimization options.

        Args:
            use_integer_keys: Use integer keys instead of strings in sparse data.
                             This saves significant memory and improves query performance.
                             Set to False only if you need to preserve original string IDs.
            chunked: Use chunked processing for memory efficiency (default: True).
                    Chunked processing is now the default for optimal memory efficiency.
                    Set to False only for small datasets or debugging purposes.
            chunk_size: Size of each chunk when chunked=True (default: 5000).
            create_indices: Whether to create indices for query performance.
                          Default: False for small datasets to reduce storage overhead.
                          Set to True for large datasets where query performance is important.
            optimize_storage: Only store integer IDs in expression table to reduce storage size.
                           String IDs are available in metadata tables for mapping.
            use_optimized_dtypes: Use optimized data types (uint16/uint32) for better compression.
                                This can significantly reduce storage size for large datasets.
            enable_v2_manifest: Enable v2 manifest paths for better query performance.
                              This is recommended for large datasets.
            compact_after_write: Compact the dataset after writing to optimize storage.
                               This creates a new version but significantly reduces file size.
            tiledb_collection_name: Name of the measurement collection for TileDB format.
                                  Default: "RNA". Only used when converting from TileDB format.
            enable_checkpointing: Enable checkpointing for long-running conversions.
                                This allows resuming from the last completed chunk if the
                                conversion is interrupted. Default: True.

        Examples:
            >>> # Default optimization (recommended)
            >>> converter = SLAFConverter()
            >>> print(f"Using chunked processing: {converter.chunked}")
            Using chunked processing: True

            >>> # Non-chunked processing for small datasets
            >>> converter = SLAFConverter(chunked=False)
            >>> print(f"Using chunked processing: {converter.chunked}")
            Using chunked processing: False

            >>> # Custom chunk size for large datasets
            >>> converter = SLAFConverter(chunk_size=10000)
            >>> print(f"Chunk size: {converter.chunk_size}")
            Chunk size: 5000
        """
        self.use_integer_keys = use_integer_keys
        self.chunked = chunked
        self.chunk_size = chunk_size
        self.sort_metadata = sort_metadata
        self.create_indices = create_indices
        self.optimize_storage = optimize_storage
        self.use_optimized_dtypes = use_optimized_dtypes
        self.enable_v2_manifest = enable_v2_manifest
        self.compact_after_write = compact_after_write
        self.tiledb_collection_name = tiledb_collection_name
        self.enable_checkpointing = enable_checkpointing

    def _is_cloud_path(self, path: str) -> bool:
        """Check if path is a cloud storage path."""
        return path.startswith(("s3://", "gs://", "azure://", "r2://"))

    def _path_exists(self, path: str) -> bool:
        """Check if path exists, works with both local and cloud paths."""
        if self._is_cloud_path(path):
            if not SMART_OPEN_AVAILABLE:
                logger.warning(
                    "smart-open not available, cannot check cloud path existence"
                )
                return False
            try:
                # For Lance datasets, check if the directory exists by trying to list contents
                # Lance datasets are directories, not files
                if path.endswith(".lance"):
                    # Try to access the Lance dataset directory
                    import lance

                    lance.dataset(path)  # This will fail if the dataset doesn't exist
                    return True
                else:
                    # For regular files, try to access the file directly
                    with smart_open(path, "r") as f:
                        f.read(1)  # Try to read 1 byte
                    return True
            except Exception:
                return False
        else:
            return os.path.exists(path)

    def _ensure_directory_exists(self, path: str):
        """Ensure directory exists, works with both local and cloud paths."""
        if not self._is_cloud_path(path):
            os.makedirs(path, exist_ok=True)
        # For cloud paths, directories are created implicitly

    def _open_file(self, path: str, mode: str = "r"):
        """Open file with cloud storage compatibility."""
        if self._is_cloud_path(path):
            if not SMART_OPEN_AVAILABLE:
                raise ImportError("smart-open required for cloud storage operations")
            return smart_open(path, mode)
        else:
            return open(path, mode)

    def _save_checkpoint(self, output_path: str, checkpoint_data: dict):
        """Save checkpoint data to config.json"""
        if not self.enable_checkpointing:
            return

        config_path = f"{output_path}/config.json"

        # Load existing config if it exists, otherwise create minimal config
        if self._path_exists(config_path):
            with self._open_file(config_path) as f:
                config = json.load(f)
        else:
            # Create minimal config for checkpointing
            config = {
                "format_version": "0.3",
                "array_shape": [0, 0],  # Will be updated when conversion completes
                "n_cells": 0,
                "n_genes": 0,
                "tables": {
                    "expression": "expression.lance",
                    "cells": "cells.lance",
                    "genes": "genes.lance",
                },
                "optimizations": {
                    "use_integer_keys": self.use_integer_keys,
                    "optimize_storage": self.optimize_storage,
                },
                "metadata": {
                    "conversion_in_progress": True,
                    "sparsity": 0.0,
                    "density": 0.0,
                    "expression_count": 0,
                },
            }

        # Update checkpoint data
        config["checkpoint"] = checkpoint_data

        # Save updated config
        with self._open_file(config_path, "w") as f:
            json.dump(config, f, indent=2)

        logger.info(f"Checkpoint saved: {checkpoint_data}")

    def _load_checkpoint(self, output_path: str) -> dict | None:
        """Load checkpoint data from config.json"""
        if not self.enable_checkpointing:
            return None

        config_path = f"{output_path}/config.json"

        if not self._path_exists(config_path):
            return None

        try:
            with self._open_file(config_path) as f:
                config = json.load(f)
                return config.get("checkpoint")
        except Exception as e:
            logger.warning(f"Could not load checkpoint: {e}")
            return None

    def _load_checkpoint_smart(self, output_path: str) -> dict | None:
        """Load checkpoint with fragment-based resume logic to avoid duplication"""
        checkpoint = self._load_checkpoint(output_path)
        if not checkpoint:
            return None

        # Get actual progress from Lance dataset using fragment count
        expression_path = f"{output_path}/expression.lance"
        logger.info(f"🔍 Checking fragment-based resume for: {expression_path}")

        # First, check if the path exists
        path_exists = self._path_exists(expression_path)
        logger.info(f"🔍 Path exists check result: {path_exists}")

        if path_exists:
            logger.info("✅ Expression dataset exists, loading...")
            try:
                expression_dataset = lance.dataset(expression_path)
                fragment_count = len(expression_dataset.get_fragments())
                logger.info(f"🔍 Found {fragment_count} fragments in dataset")
                logger.info(
                    f"🔍 Original checkpoint: last_completed_chunk={checkpoint.get('last_completed_chunk')}"
                )

                # Calculate exact resume chunk using fragment count
                last_checkpoint_boundary = checkpoint.get("last_completed_chunk", 0)
                checkpoint_interval = (
                    10  # Should match the interval used in checkpointing
                )
                chunks_since_checkpoint = fragment_count % checkpoint_interval

                # Resume from the next chunk after what was actually written
                # The fragment count represents the actual progress, so we don't need total_chunks
                # Just ensure we don't exceed the total chunks for the current file
                actual_resume_chunk = (
                    last_checkpoint_boundary + chunks_since_checkpoint + 1
                )

                # Update checkpoint with actual resume position
                checkpoint["last_completed_chunk"] = actual_resume_chunk - 1
                checkpoint["fragment_count"] = fragment_count
                checkpoint["chunks_since_checkpoint"] = chunks_since_checkpoint

                logger.info(
                    f"Fragment-based resume: {fragment_count} fragments written, "
                    f"chunks since checkpoint: {chunks_since_checkpoint}, "
                    f"resuming from chunk {actual_resume_chunk}"
                )
                logger.info(
                    f"🔧 Updated checkpoint: last_completed_chunk={checkpoint['last_completed_chunk']}"
                )

            except Exception as e:
                logger.warning(f"Could not determine fragment count: {e}")
                # Fall back to regular checkpoint logic
                pass

        return checkpoint

    def _clear_checkpoint(self, output_path: str):
        """Clear checkpoint data from config.json"""
        if not self.enable_checkpointing:
            return

        config_path = f"{output_path}/config.json"

        if not self._path_exists(config_path):
            return

        try:
            with self._open_file(config_path) as f:
                config = json.load(f)

            # Remove checkpoint data
            if "checkpoint" in config:
                del config["checkpoint"]

            # Save updated config
            with self._open_file(config_path, "w") as f:
                json.dump(config, f, indent=2)

            logger.info("Checkpoint cleared - conversion completed successfully")
        except Exception as e:
            logger.warning(f"Could not clear checkpoint: {e}")

    def convert(
        self,
        input_path: str | list[str],
        output_path: str,
        input_format: str = "auto",
        skip_validation: bool = False,
    ):
        """
        Convert single-cell data to SLAF format with optimized storage.

        SLAFConverter provides efficient conversion from various single-cell data formats
        to the SLAF format. It optimizes storage by using integer keys, COO-style
        expression tables, and efficient metadata handling.

        Supported Input Formats:
            - **h5ad**: AnnData files (.h5ad) - the standard single-cell format
            - **10x MTX**: 10x Genomics MTX directories containing matrix.mtx,
              barcodes.tsv, and genes.tsv files
            - **10x H5**: 10x Genomics H5 files (.h5) - Cell Ranger output format
            - **tiledb**: TileDB SOMA format (.tiledb) - high-performance single-cell format

        The converter automatically detects the input format based on file extension
        and directory structure. For optimal performance, you can also specify the
        format explicitly.

        Args:
            input_path: Path to the input file or directory to convert.
                       - For h5ad: path to .h5ad file
                       - For MTX: path to directory containing matrix.mtx, barcodes.tsv, genes.tsv
                       - For H5: path to .h5 file
            output_path: Path where the SLAF dataset will be saved.
                        Should be a directory path, not a file path.
            input_format: Format of input data. Options:
                         - "auto" (default): Auto-detect format
                         - "h5ad": AnnData format
                         - "10x_mtx": 10x MTX directory format
                         - "10x_h5": 10x H5 file format
                         - "tiledb": TileDB SOMA format

        Raises:
            FileNotFoundError: If the input file doesn't exist.
            ValueError: If the input file is corrupted, invalid, or format cannot be detected.
            RuntimeError: If the conversion process fails.

        Examples:
            >>> # Auto-detect format (recommended)
            >>> converter = SLAFConverter()
            >>> converter.convert("data.h5ad", "output.slaf")
            Converting data.h5ad to SLAF format...
            Optimizations: int_keys=True
            Loaded: 1000 cells × 20000 genes
            Conversion complete! Saved to output.slaf

            >>> # Convert 10x MTX directory
            >>> converter.convert("filtered_feature_bc_matrix/", "output.slaf")
            Converting 10x MTX directory filtered_feature_bc_matrix/ to SLAF format...
            Loaded: 2700 cells × 32738 genes
            Conversion complete! Saved to output.slaf

            >>> # Convert 10x H5 file
            >>> converter.convert("data.h5", "output.slaf")
            Converting 10x H5 file data.h5 to SLAF format...
            Loaded: 2700 cells × 32738 genes
            Conversion complete! Saved to output.slaf

            >>> # Convert TileDB SOMA file
            >>> converter.convert("data.tiledb", "output.slaf")
            Converting TileDB SOMA file data.tiledb to SLAF format...
            Loaded: 50000 cells × 20000 genes
            Conversion complete! Saved to output.slaf

            >>> # Explicit format specification
            >>> converter.convert("data.h5", "output.slaf", input_format="10x_h5")
            Converting 10x H5 file data.h5 to SLAF format...
            Loaded: 2700 cells × 32738 genes
            Conversion complete! Saved to output.slaf

            >>> # Convert with chunked processing for large datasets
            >>> converter = SLAFConverter(chunked=True, chunk_size=5000)
            >>> converter.convert("large_data.h5ad", "output.slaf")
            Converting large_data.h5ad to SLAF format...
            Optimizations: int_keys=True, chunked=True
            Processing in chunks of 5000 cells...
            Conversion complete! Saved to output.slaf

            >>> # Error handling for unsupported format
            >>> try:
            ...     converter.convert("unknown_file.txt", "output.slaf")
            ... except ValueError as e:
            ...     print(f"Error: {e}")
            Error: Cannot detect format for: unknown_file.txt
        """
        # Handle both single paths and lists of files
        if isinstance(input_path, list):
            # Direct list of files - use multi-file conversion
            input_files = input_path
            # Detect format from first file
            if input_format == "auto":
                from .utils import detect_format

                input_format = detect_format(input_files[0])
        else:
            # Single path - discover files
            input_files, detected_format = discover_input_files(input_path)
            # Use detected format if auto, otherwise use specified format
            if input_format == "auto":
                input_format = detected_format

        # Validate multi-file compatibility if multiple files (unless skipped)
        if not skip_validation:
            validate_input_files(input_files, input_format)

        # Handle multiple files vs single file
        if len(input_files) > 1:
            self._convert_multiple_files(input_files, output_path, input_format)
        else:
            # Single file - use existing logic
            single_file = input_files[0]
            if input_format == "h5ad":
                if not SCANPY_AVAILABLE:
                    raise ImportError(
                        "Scanpy is required for h5ad conversion. "
                        "Install with: pip install scanpy"
                    )
                self._convert_h5ad(single_file, output_path)
            elif input_format == "10x_mtx":
                self._convert_10x_mtx(single_file, output_path)
            elif input_format == "10x_h5":
                self._convert_10x_h5(single_file, output_path)
            elif input_format == "tiledb":
                self._convert_tiledb(single_file, output_path)
            else:
                raise ValueError(f"Unsupported format: {input_format}")

    def convert_anndata(self, adata, output_path: str):
        """Convert AnnData object to SLAF format with COO-style expression table"""
        if self.chunked:
            raise ValueError(
                "convert_anndata() not supported in chunked mode. "
                "Use convert() with file path instead."
            )

        logger.info("Converting AnnData object to SLAF format...")
        logger.info(f"Optimizations: int_keys={self.use_integer_keys}")
        logger.info(f"Loaded: {adata.n_obs} cells × {adata.n_vars} genes")

        # Validate optimized data types
        if not self._validate_optimized_dtypes_anndata(adata):
            self.use_optimized_dtypes = False

        # Convert the AnnData object
        self._convert_anndata(adata, output_path)

    def _convert_h5ad(self, h5ad_path: str, output_path: str):
        """Convert h5ad file to SLAF format (existing logic)"""
        logger.info(f"Converting {h5ad_path} to SLAF format...")
        logger.info(
            f"Optimizations: int_keys={self.use_integer_keys}, chunked={self.chunked}, sort_metadata={self.sort_metadata}"
        )

        if self.chunked:
            self._convert_chunked(h5ad_path, output_path)
        else:
            # Load h5ad using scanpy backed mode, then convert to in-memory
            logger.info("Loading h5ad file in backed mode...")
            adata_backed = sc.read_h5ad(h5ad_path, backed="r")
            logger.info(
                f"Loaded: {adata_backed.n_obs} cells × {adata_backed.n_vars} genes"
            )

            # Convert backed data to in-memory AnnData to avoid CSRDataset issues
            logger.info("Converting backed data to in-memory format...")
            adata = sc.AnnData(
                X=adata_backed.X[:],  # Load the full matrix into memory
                obs=adata_backed.obs.copy(),
                var=adata_backed.var.copy(),
                uns=adata_backed.uns.copy() if hasattr(adata_backed, "uns") else {},
            )

            # Close the backed file
            adata_backed.file.close()

            logger.info("Successfully converted to in-memory AnnData")

            # Convert the loaded AnnData object
            self._convert_anndata(adata, output_path)

    def _convert_10x_mtx(self, mtx_dir: str, output_path: str):
        """Convert 10x MTX directory to SLAF format"""
        logger.info(f"Converting 10x MTX directory {mtx_dir} to SLAF format...")

        if self.chunked:
            # Use native chunked reader for 10x MTX
            logger.info("Using native chunked reader for 10x MTX...")
            self._convert_chunked(mtx_dir, output_path)
        else:
            # Use scanpy to read MTX files
            try:
                adata = sc.read_10x_mtx(mtx_dir)
            except Exception as e:
                logger.error(f"Error reading 10x MTX files: {e}")
                logger.error(
                    "Please ensure the directory contains matrix.mtx and either genes.tsv or features.tsv files"
                )
                raise ValueError(
                    f"Failed to read 10x MTX format from {mtx_dir}: {e}"
                ) from e

            logger.info(f"Loaded: {adata.n_obs} cells × {adata.n_vars} genes")

            # Convert using existing AnnData conversion logic
            self._convert_anndata(adata, output_path)

    def _convert_10x_h5(self, h5_path: str, output_path: str):
        """Convert 10x H5 file to SLAF format"""
        logger.info(f"Converting 10x H5 file {h5_path} to SLAF format...")

        if self.chunked:
            # Use native chunked reader for 10x H5
            logger.info("Using native chunked reader for 10x H5...")
            self._convert_chunked(h5_path, output_path)
        else:
            # Try to read as 10x H5 first, fall back to regular h5ad
            try:
                adata_backed = sc.read_10x_h5(h5_path, genome="X")
                logger.info("Successfully read as 10x H5 format")
            except Exception:
                # Fall back to reading as regular h5ad
                logger.info("Reading as regular h5ad file...")
                adata_backed = sc.read_h5ad(h5_path, backed="r")

            logger.info(
                f"Loaded: {adata_backed.n_obs} cells × {adata_backed.n_vars} genes"
            )

            # Convert backed data to in-memory AnnData to avoid CSRDataset issues
            logger.info("Converting backed data to in-memory format...")
            adata = sc.AnnData(
                X=adata_backed.X[:],  # Load the full matrix into memory
                obs=adata_backed.obs.copy(),
                var=adata_backed.var.copy(),
                uns=adata_backed.uns.copy() if hasattr(adata_backed, "uns") else {},
            )

            # Close the backed file if it exists
            if hasattr(adata_backed, "file") and adata_backed.file is not None:
                adata_backed.file.close()

            logger.info("Successfully converted to in-memory AnnData")

            # Convert using existing AnnData conversion logic
            self._convert_anndata(adata, output_path)

    def _convert_tiledb(self, tiledb_path: str, output_path: str):
        """Convert TileDB SOMA file to SLAF format"""
        logger.info(f"Converting TileDB SOMA file {tiledb_path} to SLAF format...")

        if self.chunked:
            # Use native chunked reader for TileDB
            logger.info("Using native chunked reader for TileDB...")
            self._convert_chunked(tiledb_path, output_path)
        else:
            # For non-chunked mode, we need to load the entire dataset
            # This is not recommended for large datasets
            logger.warning("Non-chunked mode not recommended for TileDB format")
            raise NotImplementedError(
                "Non-chunked conversion not supported for TileDB format. "
                "Use chunked=True (default) for TileDB conversion."
            )

    def _convert_multiple_files(
        self, input_files: list[str], output_path: str, input_format: str
    ):
        """
        Convert multiple files to a single SLAF dataset with auto-incrementing IDs and checkpointing.

        This method processes files sequentially and adds fragments to the same
        SLAF dataset, avoiding the overhead of creating separate SLAF files.

        Args:
            input_files: List of input file paths
            output_path: Output SLAF directory path
            input_format: Format of input files
        """
        logger.info(
            f"Converting {len(input_files)} {input_format} files to SLAF format..."
        )

        # Check for existing checkpoint with smart resume logic
        checkpoint = self._load_checkpoint_smart(output_path)
        if checkpoint:
            logger.info(f"Found checkpoint: {checkpoint}")
            if checkpoint.get("status") == "completed":
                logger.info(
                    "Multi-file conversion already completed according to checkpoint"
                )
                return
            elif checkpoint.get("status") == "in_progress":
                logger.info("Resuming multi-file conversion from checkpoint...")
            else:
                logger.info("Starting fresh multi-file conversion...")

        # Create output directory (only for local paths)
        self._ensure_directory_exists(output_path)

        # Check layer consistency across all files
        layer_names = self._check_layer_consistency(input_files, input_format)
        if layer_names is None:
            logger.warning(
                "Layers are inconsistent across files. Skipping layers.lance creation."
            )
            layer_names = []

        # Track source file information
        source_file_info = []
        global_cell_offset = 0
        total_genes = 0
        total_cells = 0

        # Determine starting file and chunk from checkpoint
        start_file_idx = 0
        start_chunk_idx = 0
        if checkpoint and checkpoint.get("status") == "in_progress":
            # Handle both file-level and chunk-level checkpoints
            last_completed_chunk = checkpoint.get("last_completed_chunk", -1)
            if last_completed_chunk == -1:
                # File-level checkpoint: file was fully completed, start next file
                start_file_idx = checkpoint.get("last_completed_file", -1) + 1
                start_chunk_idx = 0
            else:
                # Chunk-level checkpoint: file is partially completed, resume same file
                start_file_idx = checkpoint.get("last_completed_file", -1)
                start_chunk_idx = last_completed_chunk + 1
            global_cell_offset = checkpoint.get("global_cell_offset", 0)

            # Enhanced logging for resume tracking
            logger.info("=" * 60)
            logger.info("🔄 CHECKPOINT RESUME DETECTED")
            logger.info("=" * 60)
            logger.info(
                f"📁 Resuming from FILE {start_file_idx + 1} of {len(input_files)}"
            )
            logger.info(f"📊 Resuming from CHUNK {start_chunk_idx} within that file")
            logger.info(
                f"📈 Last completed file: {checkpoint.get('last_completed_file', -1) + 1}"
            )
            logger.info(
                f"📈 Last completed chunk: {checkpoint.get('last_completed_chunk', -1)}"
            )
            logger.info(f"🔢 Global cell offset: {global_cell_offset:,}")
            if "fragment_count" in checkpoint:
                logger.info(
                    f"🧩 Fragment count: {checkpoint.get('fragment_count', 0):,}"
                )
            if "chunks_since_checkpoint" in checkpoint:
                logger.info(
                    f"⏭️  Chunks since checkpoint: {checkpoint.get('chunks_since_checkpoint', 0)}"
                )
            logger.info("=" * 60)

        # Process each file and add fragments to the same SLAF dataset
        for i, file_path in enumerate(input_files):
            # Skip files if resuming from checkpoint
            if i < start_file_idx:
                logger.info(
                    f"⏭️  Skipping file {i + 1}/{len(input_files)}: {os.path.basename(file_path)} (already processed)"
                )
                continue

            # Enhanced file processing logging
            if i == start_file_idx:
                logger.info("=" * 60)
                logger.info(
                    f"🔄 RESUMING FILE {i + 1}/{len(input_files)}: {os.path.basename(file_path)}"
                )
                logger.info(
                    f"📊 Starting from chunk {start_chunk_idx} within this file"
                )
                logger.info("=" * 60)
            else:
                logger.info(
                    f"📁 Processing file {i + 1}/{len(input_files)}: {os.path.basename(file_path)}"
                )

            try:
                # Use chunked reader to process file directly
                with create_chunked_reader(
                    file_path,
                    chunk_size=self.chunk_size,
                    collection_name=self.tiledb_collection_name,
                ) as reader:
                    logger.info(
                        f"Loaded: {reader.n_obs:,} cells × {reader.n_vars:,} genes"
                    )

                    # Get metadata from reader
                    obs_df = reader.get_obs_metadata()
                    var_df = reader.get_var_metadata()

                    # Ensure cell_id and gene_id columns exist
                    if "cell_id" not in obs_df.columns:
                        obs_df["cell_id"] = reader.obs_names
                    if "gene_id" not in var_df.columns:
                        var_df["gene_id"] = reader.var_names

                    # Add integer IDs with global offset
                    obs_df["cell_integer_id"] = range(
                        global_cell_offset, global_cell_offset + len(obs_df)
                    )
                    var_df["gene_integer_id"] = range(len(var_df))

                    # Add source file information to cell metadata
                    source_file = os.path.basename(file_path)
                    obs_df["source_file"] = source_file

                    # Precompute cell start indices
                    obs_df["cell_start_index"] = self._compute_cell_start_indices(
                        reader, obs_df
                    )

                    # Convert metadata to Lance tables
                    cell_metadata_table = self._create_metadata_table(
                        obs_df, "cell_id", integer_mapping=None
                    )
                    gene_metadata_table = self._create_metadata_table(
                        var_df, "gene_id", integer_mapping=None
                    )

                    # Process expression data in chunks and write directly
                    cells_path = f"{output_path}/cells.lance"
                    genes_path = f"{output_path}/genes.lance"

                    # Write metadata tables (overwrite for first file, append for subsequent)
                    # Skip metadata writing for files that were already completed
                    # Only write metadata for files that haven't been started yet, or for the current file if starting from beginning
                    should_write_metadata = (
                        i > start_file_idx  # Future files - always write metadata
                        or (
                            i == start_file_idx and start_chunk_idx == 0
                        )  # Current file only if starting from beginning
                    )

                    if i == 0:
                        # First file - create new datasets
                        lance.write_dataset(
                            cell_metadata_table,
                            cells_path,
                            mode="overwrite",
                            enable_v2_manifest_paths=self.enable_v2_manifest,
                            data_storage_version="2.1",
                        )
                        lance.write_dataset(
                            gene_metadata_table,
                            genes_path,
                            mode="overwrite",
                            enable_v2_manifest_paths=self.enable_v2_manifest,
                            data_storage_version="2.1",
                        )
                        total_genes = len(var_df)
                    elif should_write_metadata:
                        # Subsequent files - append to existing datasets (only if not resuming from middle)
                        lance.write_dataset(
                            cell_metadata_table,
                            cells_path,
                            mode="append",
                            enable_v2_manifest_paths=self.enable_v2_manifest,
                            data_storage_version="2.1",
                        )

                    # Process expression data in chunks with checkpointing support
                    # If we're resuming from this file, use the checkpoint chunk index
                    # Otherwise start from chunk 0
                    chunk_start_idx = start_chunk_idx if i == start_file_idx else 0
                    self._process_file_chunks_with_checkpoint(
                        reader, output_path, i, chunk_start_idx, global_cell_offset
                    )

                    # Process layers if they exist and are consistent
                    if layer_names:
                        logger.info(
                            f"Processing {len(layer_names)} consistent layers for file {i + 1}/{len(input_files)}..."
                        )
                        # Determine value type from layer data (assume float32 for layers)
                        # Update reader's value_type to match (needed for _read_chunk_layers_wide)
                        original_value_type = reader.value_type
                        reader.value_type = "float32"
                        try:
                            # For multi-file, only create empty table on first file (i == 0)
                            # For subsequent files, skip empty table creation and append directly
                            layers_path = f"{output_path}/layers.lance"
                            is_first_file = i == 0 and not self._path_exists(
                                layers_path
                            )
                            self._process_expression_with_checkpoint(
                                reader,
                                output_path,
                                value_type="float32",
                                checkpoint=None,  # Layers don't use checkpointing
                                layers=True,
                                table_name="layers",
                                cell_offset=global_cell_offset,
                                skip_empty_table=not is_first_file,  # Skip if not first file
                            )
                        finally:
                            # Restore original value_type
                            reader.value_type = original_value_type

                    # Track source file information
                    source_file_info.append(
                        {
                            "file_path": file_path,
                            "file_name": source_file,
                            "n_cells": len(obs_df),
                            "cell_offset": global_cell_offset,
                        }
                    )

                    # Update global cell offset
                    global_cell_offset += len(obs_df)
                    total_cells += len(obs_df)

                    # Save checkpoint after each file (chunk-level checkpointing is handled in _process_file_chunks_with_checkpoint)
                    if self.enable_checkpointing:
                        checkpoint_data = {
                            "status": "in_progress",
                            "last_completed_file": i,
                            "last_completed_chunk": -1,  # -1 indicates file is fully completed
                            "global_cell_offset": global_cell_offset,
                            "total_files": len(input_files),
                            "timestamp": pd.Timestamp.now().isoformat(),
                        }
                        self._save_checkpoint(output_path, checkpoint_data)

            except Exception as e:
                logger.error(f"Failed to process file {file_path}: {e}")
                # Save checkpoint with error status
                if self.enable_checkpointing:
                    checkpoint_data = {
                        "status": "error",
                        "last_completed_file": i - 1,
                        "last_completed_chunk": -1,  # -1 indicates previous file was fully completed
                        "global_cell_offset": global_cell_offset,
                        "error": str(e),
                        "timestamp": pd.Timestamp.now().isoformat(),
                    }
                    self._save_checkpoint(output_path, checkpoint_data)
                # Continue with other files
                continue

        if not source_file_info:
            raise RuntimeError("No files were successfully processed")

        # Create indices if enabled
        if self.create_indices:
            self._create_indices(output_path)

        # Compact dataset if enabled
        if self.compact_after_write:
            self._compact_dataset(output_path)

        # Save config with multi-file information
        self._save_multi_file_config(
            output_path,
            source_file_info,
            None,  # We don't need to pass tables since they're already written
            None,
            None,
            layer_names=layer_names,  # Pass layer names if consistent
        )

        # Clear checkpoint after successful completion
        self._clear_checkpoint(output_path)

        logger.info(
            f"Multi-file conversion complete! Processed {len(source_file_info)} files"
        )
        logger.info(f"Total cells: {total_cells}, Total genes: {total_genes}")

    def append(
        self, input_path: str, existing_slaf_path: str, input_format: str = "auto"
    ):
        """
        Append new data to an existing SLAF dataset.

        This method adds new data to an existing SLAF dataset by:
        1. Validating compatibility with existing dataset
        2. Reading existing dataset metadata
        3. Appending new data with auto-incrementing IDs
        4. Updating configuration with new source file info

        Args:
            input_path: Path to new input file or directory
            existing_slaf_path: Path to existing SLAF dataset
            input_format: Format of input data (auto-detected if not specified)
        """
        logger.info(
            f"Appending data from {input_path} to existing SLAF dataset {existing_slaf_path}"
        )

        # Check if existing SLAF dataset exists
        if not self._path_exists(existing_slaf_path):
            raise FileNotFoundError(
                f"Existing SLAF dataset not found: {existing_slaf_path}"
            )

        # Discover input files
        input_files, detected_format = discover_input_files(input_path)
        if input_format == "auto":
            input_format = detected_format

        # Validate compatibility with existing dataset
        self._validate_append_compatibility(
            input_files, input_format, existing_slaf_path
        )

        # Read existing dataset metadata
        existing_cells_dataset = lance.dataset(
            os.path.join(existing_slaf_path, "cells.lance")
        )
        existing_cells_table = existing_cells_dataset.to_table()
        current_cell_count = len(existing_cells_table)

        # Track source file information
        source_file_info = []
        global_cell_offset = current_cell_count
        total_new_cells = 0

        # Process each new file and append to existing dataset
        for i, file_path in enumerate(input_files):
            logger.info(
                f"Processing file {i + 1}/{len(input_files)}: {os.path.basename(file_path)}"
            )

            try:
                # Use chunked reader to process file directly
                with create_chunked_reader(
                    file_path,
                    chunk_size=self.chunk_size,
                    collection_name=self.tiledb_collection_name,
                ) as reader:
                    logger.info(
                        f"Loaded: {reader.n_obs:,} cells × {reader.n_vars:,} genes"
                    )

                    # Get metadata from reader
                    obs_df = reader.get_obs_metadata()
                    var_df = reader.get_var_metadata()

                    # Ensure cell_id and gene_id columns exist
                    if "cell_id" not in obs_df.columns:
                        obs_df["cell_id"] = reader.obs_names
                    if "gene_id" not in var_df.columns:
                        var_df["gene_id"] = reader.var_names

                    # Add integer IDs with global offset
                    obs_df["cell_integer_id"] = range(
                        global_cell_offset, global_cell_offset + len(obs_df)
                    )

                    # Add source file information to cell metadata
                    source_file = os.path.basename(file_path)
                    obs_df["source_file"] = source_file

                    # Check if existing dataset has source_file column
                    existing_cells_dataset = lance.dataset(
                        os.path.join(existing_slaf_path, "cells.lance")
                    )
                    existing_cells_table = existing_cells_dataset.to_table()
                    existing_columns = set(existing_cells_table.column_names)

                    # If existing dataset doesn't have source_file column, add it to existing data
                    if "source_file" not in existing_columns:
                        logger.info("Adding source_file column to existing dataset...")
                        # Read existing cells data
                        existing_cells_df = existing_cells_table.to_pandas()
                        existing_cells_df["source_file"] = (
                            "original_data"  # Default source for existing data
                        )

                        # Recreate the cells dataset with source_file column
                        updated_cells_table = pa.table(existing_cells_df)
                        lance.write_dataset(
                            updated_cells_table,
                            os.path.join(existing_slaf_path, "cells.lance"),
                            mode="overwrite",
                            enable_v2_manifest_paths=self.enable_v2_manifest,
                            data_storage_version="2.1",
                        )
                        logger.info("✓ Added source_file column to existing dataset")

                    # Precompute cell start indices
                    obs_df["cell_start_index"] = self._compute_cell_start_indices(
                        reader, obs_df
                    )

                    # Convert metadata to Lance tables
                    cell_metadata_table = self._create_metadata_table(
                        obs_df, "cell_id", integer_mapping=None
                    )

                    # Append to existing cells dataset
                    cells_path = os.path.join(existing_slaf_path, "cells.lance")
                    lance.write_dataset(
                        cell_metadata_table,
                        cells_path,
                        mode="append",
                        enable_v2_manifest_paths=self.enable_v2_manifest,
                        data_storage_version="2.1",
                    )

                    # Process expression data in chunks with checkpointing support
                    # Use the same checkpointing infrastructure as convert method
                    self._process_file_chunks_with_checkpoint(
                        reader, existing_slaf_path, i, 0, global_cell_offset
                    )

                    # Track source file information
                    source_file_info.append(
                        {
                            "file_path": file_path,
                            "file_name": source_file,
                            "n_cells": len(obs_df),
                            "cell_offset": global_cell_offset,
                        }
                    )

                    # Update global cell offset
                    global_cell_offset += len(obs_df)
                    total_new_cells += len(obs_df)

                    # Save checkpoint after each file (for append operations)
                    if self.enable_checkpointing:
                        checkpoint_data = {
                            "status": "in_progress",
                            "last_completed_file": i,
                            "last_completed_chunk": -1,  # -1 indicates file is fully completed
                            "global_cell_offset": global_cell_offset,
                            "operation": "append",  # Mark this as an append operation
                            "timestamp": pd.Timestamp.now().isoformat(),
                        }
                        self._save_checkpoint(existing_slaf_path, checkpoint_data)

            except Exception as e:
                logger.error(f"Failed to process file {file_path}: {e}")
                # Save checkpoint with error status
                if self.enable_checkpointing:
                    checkpoint_data = {
                        "status": "error",
                        "last_completed_file": i - 1,
                        "last_completed_chunk": -1,  # -1 indicates previous file was fully completed
                        "global_cell_offset": global_cell_offset,
                        "operation": "append",  # Mark this as an append operation
                        "error": str(e),
                        "timestamp": pd.Timestamp.now().isoformat(),
                    }
                    self._save_checkpoint(existing_slaf_path, checkpoint_data)
                # Continue with other files
                continue

        if not source_file_info:
            raise RuntimeError("No files were successfully processed")

        # Update configuration with new source file information
        self._update_config_with_append(
            existing_slaf_path, source_file_info, total_new_cells
        )

        # Clear checkpoint after successful completion
        self._clear_checkpoint(existing_slaf_path)

        logger.info(
            f"Append complete! Added {total_new_cells} cells from {len(source_file_info)} files"
        )
        logger.info(f"Total cells in dataset: {current_cell_count + total_new_cells}")

    def _validate_append_compatibility(
        self, input_files: list[str], input_format: str, existing_slaf_path: str
    ):
        """Validate that new files are compatible with existing SLAF dataset."""
        logger.info("Validating compatibility with existing SLAF dataset...")

        # Load existing dataset metadata
        existing_cells_dataset = lance.dataset(f"{existing_slaf_path}/cells.lance")
        existing_cells_table = existing_cells_dataset.to_table()
        existing_genes_dataset = lance.dataset(f"{existing_slaf_path}/genes.lance")
        existing_genes_table = existing_genes_dataset.to_table()

        # Get existing gene set and cell metadata schema
        existing_genes = set(existing_genes_table.column("gene_id").to_numpy())
        existing_cell_columns = set(existing_cells_table.column_names)

        # Validate new files against existing dataset
        for file_path in input_files:
            try:
                # Extract schema from new file
                genes, cells, value_type = self._extract_schema_info(
                    file_path, input_format
                )

                # Check gene compatibility
                if genes != existing_genes:
                    missing_genes = existing_genes - genes
                    extra_genes = genes - existing_genes
                    error_msg = f"File {os.path.basename(file_path)} is incompatible with existing dataset:"
                    if missing_genes:
                        error_msg += f"\n  Missing genes: {sorted(missing_genes)[:5]}{'...' if len(missing_genes) > 5 else ''}"
                    if extra_genes:
                        error_msg += f"\n  Extra genes: {sorted(extra_genes)[:5]}{'...' if len(extra_genes) > 5 else ''}"
                    raise ValueError(error_msg)

                # Check cell metadata schema compatibility
                # Exclude columns that are added during SLAF conversion
                slaF_added_columns = {
                    "cell_id",
                    "cell_integer_id",
                    "cell_start_index",
                    "source_file",
                }
                new_cell_columns = cells - slaF_added_columns
                existing_cell_columns_no_slaf = (
                    existing_cell_columns - slaF_added_columns
                )

                if new_cell_columns != existing_cell_columns_no_slaf:
                    missing_cols = existing_cell_columns_no_slaf - new_cell_columns
                    extra_cols = new_cell_columns - existing_cell_columns_no_slaf
                    error_msg = f"File {os.path.basename(file_path)} has incompatible cell metadata schema:"
                    if missing_cols:
                        error_msg += f"\n  Missing columns: {sorted(missing_cols)}"
                    if extra_cols:
                        error_msg += f"\n  Extra columns: {sorted(extra_cols)}"
                    raise ValueError(error_msg)

            except Exception as e:
                raise ValueError(
                    f"Validation failed for {os.path.basename(file_path)}: {e}"
                ) from e

        logger.info("✓ All files are compatible with existing dataset")

    def _load_existing_config(self, existing_slaf_path: str) -> dict:
        """Load existing SLAF configuration."""
        config_path = f"{existing_slaf_path}/config.json"
        if not self._path_exists(config_path):
            raise FileNotFoundError(f"Config file not found: {config_path}")

        with self._open_file(config_path) as f:
            return json.load(f)

    def _update_config_with_append(
        self, existing_slaf_path: str, source_file_info: list, total_new_cells: int
    ):
        """Update configuration with new source file information."""
        config_path = f"{existing_slaf_path}/config.json"
        config = self._load_existing_config(existing_slaf_path)

        # Update cell count
        config["n_cells"] += total_new_cells
        config["array_shape"][0] = config["n_cells"]

        # Update multi_file information
        if "multi_file" not in config:
            config["multi_file"] = {
                "source_files": [],
                "total_files": 0,
                "total_cells_from_files": 0,
            }

        # Add new source files
        config["multi_file"]["source_files"].extend(source_file_info)
        config["multi_file"]["total_files"] = len(config["multi_file"]["source_files"])
        config["multi_file"]["total_cells_from_files"] = sum(
            info["n_cells"] for info in config["multi_file"]["source_files"]
        )

        # Update created_at timestamp
        config["last_updated"] = pd.Timestamp.now().isoformat()

        # Save updated config
        with self._open_file(config_path, "w") as f:
            json.dump(config, f, indent=2)

        logger.info(
            f"Updated configuration with {len(source_file_info)} new source files"
        )

    def _extract_schema_info(
        self, file_path: str, format_type: str
    ) -> tuple[set[str], set[str], str]:
        """Extract schema information from a file for compatibility checking."""
        if format_type == "h5ad":
            return self._extract_h5ad_schema(file_path)
        elif format_type == "10x_mtx":
            return self._extract_10x_mtx_schema(file_path)
        elif format_type == "10x_h5":
            return self._extract_10x_h5_schema(file_path)
        elif format_type == "tiledb":
            return self._extract_tiledb_schema(file_path)
        else:
            raise ValueError(f"Unsupported format: {format_type}")

    def _extract_h5ad_schema(self, file_path: str) -> tuple[set[str], set[str], str]:
        """Extract schema information from h5ad file."""
        import scanpy as sc

        # Read in backed mode for efficiency
        adata = sc.read_h5ad(file_path, backed="r")

        # Get gene IDs
        gene_ids = set(adata.var_names)

        # Get cell metadata columns
        cell_columns = set(adata.obs.columns)

        # Determine value type from expression data
        if hasattr(adata.X, "data"):
            sample_data = adata.X.data[:1000]  # Sample first 1000 values
        else:
            # For backed mode, try to get a sample
            try:
                sample_data = (
                    adata.X[:100, :100].data
                    if hasattr(adata.X, "data")
                    else adata.X[:100, :100].toarray().flatten()
                )
            except Exception:
                sample_data = np.array([0])  # Fallback

        # Determine value type
        if np.issubdtype(sample_data.dtype, np.integer):
            value_type = "uint16"
        else:
            value_type = "float32"

        adata.file.close()
        return gene_ids, cell_columns, value_type

    def _extract_10x_mtx_schema(self, file_path: str) -> tuple[set[str], set[str], str]:
        """Extract schema information from 10x MTX directory."""
        import scanpy as sc

        # Read MTX files
        adata = sc.read_10x_mtx(file_path)

        # Get gene IDs
        gene_ids = set(adata.var_names)

        # Get cell metadata columns
        cell_columns = set(adata.obs.columns)

        # 10x MTX typically has integer counts
        value_type = "uint16"

        return gene_ids, cell_columns, value_type

    def _extract_10x_h5_schema(self, file_path: str) -> tuple[set[str], set[str], str]:
        """Extract schema information from 10x H5 file."""
        import scanpy as sc

        try:
            # Try to read as 10x H5 first
            adata = sc.read_10x_h5(file_path, genome="X")
        except Exception:
            # Fall back to regular h5ad
            adata = sc.read_h5ad(file_path, backed="r")

        # Get gene IDs
        gene_ids = set(adata.var_names)

        # Get cell metadata columns
        cell_columns = set(adata.obs.columns)

        # 10x H5 typically has integer counts
        value_type = "uint16"

        if hasattr(adata, "file"):
            adata.file.close()

        return gene_ids, cell_columns, value_type

    def _extract_tiledb_schema(self, file_path: str) -> tuple[set[str], set[str], str]:
        """Extract schema information from TileDB SOMA file."""
        import tiledbsoma as soma

        # Open TileDB SOMA experiment
        with soma.open(file_path) as exp:
            # Get measurement collection
            ms = exp.ms[self.tiledb_collection_name]

            # Get gene IDs from var
            var_df = ms.var.read().concat().to_pandas()
            gene_ids = set(var_df.index)

            # Get cell metadata columns from obs
            obs_df = ms.obs.read().concat().to_pandas()
            cell_columns = set(obs_df.columns)

            # TileDB SOMA typically has float32 values
            value_type = "float32"

            return gene_ids, cell_columns, value_type

    def _convert_anndata(self, adata, output_path: str):
        """Internal method to convert AnnData object to SLAF format"""
        # Create output directory (only for local paths)
        self._ensure_directory_exists(output_path)

        # Validate optimized data types and determine value type
        validation_result, value_type = self._validate_optimized_dtypes_anndata(adata)
        if not validation_result:
            self.use_optimized_dtypes = False

        # Create integer key mappings if needed
        cell_id_mapping = None
        gene_id_mapping = None

        if self.use_integer_keys:
            logger.info("Creating integer key mappings...")
            cell_id_mapping = self._create_id_mapping(adata.obs.index, "cell")
            gene_id_mapping = self._create_id_mapping(adata.var.index, "gene")

        # Convert expression data to COO format
        logger.info("Converting expression data to COO format...")
        expression_table = self._sparse_to_coo_table(
            sparse_matrix=adata.X,
            cell_ids=adata.obs.index,
            gene_ids=adata.var.index,
            value_type=value_type,
        )

        # Convert metadata
        logger.info("Converting metadata...")

        # Note: Sorting is disabled to maintain consistency between metadata and expression data ordering
        # TODO: Implement proper sorting that affects both metadata and expression data
        obs_df = adata.obs.copy()
        var_df = adata.var.copy()

        # Precompute cell start indices for fast cell-based queries
        logger.info("Precomputing cell start indices...")
        obs_df["cell_start_index"] = self._compute_cell_start_indices_anndata(
            adata, obs_df
        )

        cell_metadata_table = self._create_metadata_table(
            df=obs_df, entity_id_col="cell_id", integer_mapping=cell_id_mapping
        )
        gene_metadata_table = self._create_metadata_table(
            df=var_df, entity_id_col="gene_id", integer_mapping=gene_id_mapping
        )

        # Write all Lance tables
        logger.info("Writing Lance tables...")
        table_configs = [
            ("expression", expression_table),
            ("cells", cell_metadata_table),
            ("genes", gene_metadata_table),
        ]

        self._write_lance_tables(output_path, table_configs)

        # Convert layers if they exist
        layer_names = []
        if hasattr(adata, "layers") and adata.layers and len(adata.layers) > 0:
            logger.info(f"Converting {len(adata.layers)} layers...")
            layer_names = self._convert_layers(
                adata.layers,
                output_path,
                adata.obs.index,
                adata.var.index,
                value_type,
            )

        # Convert obsm if it exists
        obsm_keys = []
        if hasattr(adata, "obsm") and adata.obsm and len(adata.obsm) > 0:
            logger.info(f"Converting {len(adata.obsm)} obsm embeddings...")
            obsm_keys = self._convert_obsm(
                adata.obsm,
                output_path,
                adata.obs.index,
                cell_id_mapping,
            )

        # Convert varm if it exists
        varm_keys = []
        if hasattr(adata, "varm") and adata.varm and len(adata.varm) > 0:
            logger.info(f"Converting {len(adata.varm)} varm embeddings...")
            varm_keys = self._convert_varm(
                adata.varm,
                output_path,
                adata.var.index,
                gene_id_mapping,
            )

        # Convert uns if it exists
        if hasattr(adata, "uns") and adata.uns and len(adata.uns) > 0:
            logger.info("Converting uns metadata...")
            self._convert_uns(adata.uns, output_path)

        # Compact dataset for optimal storage (only if enabled)
        if self.compact_after_write:
            self._compact_dataset(output_path)

        # Save config (with layers, obsm, varm metadata if they were converted)
        self._save_config(
            output_path,
            adata.shape,
            layer_names=layer_names,
            obsm_keys=obsm_keys,
            varm_keys=varm_keys,
        )
        logger.info(f"Conversion complete! Saved to {output_path}")

    def _convert_chunked(self, h5ad_path: str, output_path: str):
        """Convert h5ad file using chunked processing with checkpointing support"""
        logger.info(f"Processing in chunks of {self.chunk_size} cells...")

        # Check for existing checkpoint with smart resume logic
        checkpoint = self._load_checkpoint_smart(output_path)
        if checkpoint:
            logger.info(f"Found checkpoint: {checkpoint}")
            if checkpoint.get("status") == "completed":
                logger.info("Conversion already completed according to checkpoint")
                return
            elif checkpoint.get("status") == "in_progress":
                logger.info("Resuming from checkpoint...")
                # Resume logic will be handled in _process_expression
            else:
                logger.info("Starting fresh conversion...")

        # First, create a temporary reader to determine the value type
        with create_chunked_reader(
            h5ad_path,
            chunk_size=self.chunk_size,
            collection_name=self.tiledb_collection_name,
        ) as temp_reader:
            # Validate optimized data types and determine value type
            validation_result, value_type = self._validate_optimized_dtypes(temp_reader)
            if not validation_result:
                self.use_optimized_dtypes = False

        # Now create the reader with the correct value type
        with create_chunked_reader(
            h5ad_path,
            chunk_size=self.chunk_size,
            value_type=value_type,
            collection_name=self.tiledb_collection_name,
        ) as reader:
            logger.info(f"Loaded: {reader.n_obs:,} cells × {reader.n_vars:,} genes")

            # Create output directory (only for local paths)
            self._ensure_directory_exists(output_path)

            # Write metadata tables efficiently (without loading everything into memory)
            # Only write metadata if not resuming from checkpoint
            if not checkpoint or checkpoint.get("status") != "in_progress":
                self._write_metadata_efficiently(reader, output_path)

            # Check if layers exist before processing
            layer_names = []
            if hasattr(reader, "adata") and reader.adata is not None:
                if (
                    hasattr(reader.adata, "layers")
                    and reader.adata.layers
                    and len(reader.adata.layers) > 0
                ):
                    layer_names = list(reader.adata.layers.keys())
                    logger.info(f"Detected {len(layer_names)} layers: {layer_names}")

            # Process expression data with checkpointing
            self._process_expression_with_checkpoint(
                reader,
                output_path,
                value_type,
                checkpoint,
                layers=False,
                table_name="expression",
            )

            # Convert layers if they exist (chunked processing)
            if layer_names:
                logger.info(f"Converting {len(layer_names)} layers in chunked mode...")
                # For layers, use float32 as default (layers typically contain float data)
                # Update reader's value_type to match (needed for _read_chunk_layers_wide)
                original_value_type = reader.value_type
                reader.value_type = "float32"
                try:
                    self._process_expression_with_checkpoint(
                        reader,
                        output_path,
                        value_type="float32",
                        checkpoint=None,  # Layers don't use checkpointing (processed after expression)
                        layers=True,
                        table_name="layers",
                    )
                finally:
                    # Restore original value_type
                    reader.value_type = original_value_type

            # Create indices (if enabled)
            if self.create_indices:
                self._create_indices(output_path)

            # Compact dataset for optimal storage (only if enabled)
            if self.compact_after_write:
                self._compact_dataset(output_path)

            # Save config and clear checkpoint (with layers metadata if layers were converted)
            self._save_config(
                output_path, (reader.n_obs, reader.n_vars), layer_names=layer_names
            )
            self._clear_checkpoint(output_path)
            logger.info(f"Conversion complete! Saved to {output_path}")

    def _write_metadata_efficiently(self, reader, output_path: str):
        """Write metadata tables efficiently while preserving all columns"""
        logger.info("Writing metadata tables...")

        # Get full metadata from reader (this loads all columns)
        obs_df = reader.get_obs_metadata()
        var_df = reader.get_var_metadata()

        # Ensure cell_id and gene_id columns exist with actual names
        if "cell_id" not in obs_df.columns:
            obs_df["cell_id"] = reader.obs_names
        if "gene_id" not in var_df.columns:
            var_df["gene_id"] = reader.var_names

        # Note: Sorting is disabled in chunked mode to maintain consistency
        # between metadata and expression data ordering
        # TODO: Implement proper sorting that affects both metadata and expression data

        # Add integer IDs if enabled
        if self.use_integer_keys:
            obs_df["cell_integer_id"] = range(len(obs_df))
            var_df["gene_integer_id"] = range(len(var_df))

        # Precompute cell start indices for fast cell-based queries
        logger.info("Precomputing cell start indices...")
        obs_df["cell_start_index"] = self._compute_cell_start_indices(reader, obs_df)

        # Convert to Lance tables
        cell_metadata_table = self._create_metadata_table(
            obs_df,
            "cell_id",
            integer_mapping=None,  # Already added above
        )
        gene_metadata_table = self._create_metadata_table(
            var_df,
            "gene_id",
            integer_mapping=None,  # Already added above
        )

        # Write metadata tables
        lance.write_dataset(
            cell_metadata_table,
            f"{output_path}/cells.lance",
            mode="overwrite",
            enable_v2_manifest_paths=self.enable_v2_manifest,
            data_storage_version="2.1",
        )
        lance.write_dataset(
            gene_metadata_table,
            f"{output_path}/genes.lance",
            mode="overwrite",
            enable_v2_manifest_paths=self.enable_v2_manifest,
            data_storage_version="2.1",
        )

        logger.info("Metadata tables written!")

    def _process_expression(self, reader, output_path: str, value_type="uint16"):
        """Process expression data in single-threaded mode with large chunks"""
        logger.info("Processing expression data in single-threaded mode...")

        # Calculate total chunks
        total_chunks = (reader.n_obs + self.chunk_size - 1) // self.chunk_size
        logger.info(
            f"Processing {total_chunks} chunks with chunk size {self.chunk_size:,}..."
        )

        # Memory monitoring
        process = None
        initial_memory = None
        try:
            import psutil

            process = psutil.Process()
            initial_memory = process.memory_info().rss / 1024 / 1024  # MB
            logger.info(f"Initial memory usage: {initial_memory:.1f} MB")
        except ImportError:
            logger.info("Install psutil for memory monitoring: pip install psutil")

        # Create Lance dataset with schema
        expression_path = f"{output_path}/expression.lance"
        schema = self._get_expression_schema(value_type)

        # Create empty dataset first
        logger.info("Creating initial Lance dataset...")
        schema = self._get_expression_schema(value_type)

        # Create empty table with correct schema based on settings
        if value_type == "uint16":
            value_pa_type = pa.uint16()
        elif value_type == "float32":
            value_pa_type = pa.float32()
        else:
            raise ValueError(f"Unsupported value type: {value_type}")

        if self.optimize_storage:
            if self.use_optimized_dtypes:
                empty_table = pa.table(
                    {
                        "cell_integer_id": pa.array([], type=pa.uint32()),
                        "gene_integer_id": pa.array([], type=pa.uint16()),
                        "value": pa.array([], type=value_pa_type),
                    }
                )
            else:
                empty_table = pa.table(
                    {
                        "cell_integer_id": pa.array([], type=pa.int32()),
                        "gene_integer_id": pa.array([], type=pa.int32()),
                        "value": pa.array([], type=value_pa_type),
                    }
                )
        else:
            if self.use_optimized_dtypes:
                empty_table = pa.table(
                    {
                        "cell_id": pa.array([], type=pa.string()),
                        "gene_id": pa.array([], type=pa.string()),
                        "cell_integer_id": pa.array([], type=pa.uint32()),
                        "gene_integer_id": pa.array([], type=pa.uint16()),
                        "value": pa.array([], type=value_pa_type),
                    }
                )
            else:
                empty_table = pa.table(
                    {
                        "cell_id": pa.array([], type=pa.string()),
                        "gene_id": pa.array([], type=pa.string()),
                        "cell_integer_id": pa.array([], type=pa.int32()),
                        "gene_integer_id": pa.array([], type=pa.int32()),
                        "value": pa.array([], type=value_pa_type),
                    }
                )

        # Create initial Lance dataset (using max_rows_per_file for large fragments)
        lance.write_dataset(
            empty_table,
            expression_path,
            mode="overwrite",
            schema=schema,
            max_rows_per_file=10000000,  # 10M rows per file to avoid memory issues
            enable_v2_manifest_paths=self.enable_v2_manifest,
            data_storage_version="2.1",
        )

        # Process chunks sequentially
        logger.info("Processing chunks sequentially...")
        import time

        from tqdm import tqdm

        processing_start_time = time.time()

        for _chunk_idx, (chunk_table, _obs_slice) in enumerate(
            tqdm(
                reader.iter_chunks(chunk_size=self.chunk_size),
                total=total_chunks,
                desc="Processing chunks",
                unit="chunk",
            )
        ):
            # Process chunk (data type conversion and string ID addition if needed)
            if not self.use_optimized_dtypes:
                # Convert from optimized dtypes to standard dtypes
                cell_integer_ids = (
                    chunk_table.column("cell_integer_id").to_numpy().astype(np.int32)
                )
                gene_integer_ids = (
                    chunk_table.column("gene_integer_id").to_numpy().astype(np.int32)
                )
                values = chunk_table.column("value").to_numpy().astype(np.float32)

                chunk_table = pa.table(
                    {
                        "cell_integer_id": pa.array(cell_integer_ids),
                        "gene_integer_id": pa.array(gene_integer_ids),
                        "value": pa.array(values),
                    }
                )

            if not self.optimize_storage:
                # Get cell and gene names
                cell_names = reader.obs_names
                gene_names = reader.var_names

                # Create string ID arrays
                cell_integer_ids = chunk_table.column("cell_integer_id").to_numpy()
                gene_integer_ids = chunk_table.column("gene_integer_id").to_numpy()

                cell_ids = cell_names[cell_integer_ids].astype(str)
                gene_ids = gene_names[gene_integer_ids].astype(str)

                # Create new table with string IDs
                chunk_table = pa.table(
                    {
                        "cell_id": pa.array(cell_ids),
                        "gene_id": pa.array(gene_ids),
                        "cell_integer_id": chunk_table.column("cell_integer_id"),
                        "gene_integer_id": chunk_table.column("gene_integer_id"),
                        "value": chunk_table.column("value"),
                    }
                )

            # Write chunk to Lance dataset (using max_rows_per_file for large fragments)
            lance.write_dataset(
                chunk_table,
                expression_path,
                mode="append",
                max_rows_per_file=10000000,  # 10M rows per file to avoid memory issues
                enable_v2_manifest_paths=self.enable_v2_manifest,
                data_storage_version="2.1",
            )

        # Final memory report
        if process is not None and initial_memory is not None:
            try:
                final_memory = process.memory_info().rss / 1024 / 1024  # MB
                memory_increase = final_memory - initial_memory
                logger.info(
                    f"Final memory usage: {final_memory:.1f} MB (change: {memory_increase:+.1f} MB)"
                )
            except Exception:
                pass

        # Calculate and log overall statistics
        total_processing_time = time.time() - processing_start_time

        logger.info(
            f"Expression data processing complete! "
            f"Processed {total_chunks} chunks in {total_processing_time:.1f}s "
            f"({total_processing_time / total_chunks:.2f}s per chunk average)"
        )

    def _process_expression_with_checkpoint(
        self,
        reader,
        output_path: str,
        value_type="uint16",
        checkpoint=None,
        layers: bool = False,
        table_name: str = "expression",
        cell_offset: int = 0,
        skip_empty_table: bool = False,
    ):
        """
        Process expression or layer data with checkpointing support.

        Args:
            reader: Chunked reader instance
            output_path: Output SLAF directory path
            value_type: Value type for data ("uint16" or "float32")
            checkpoint: Optional checkpoint data for resuming
            layers: If True, processes all layers in wide format. If False, processes expression (X).
            table_name: Name of the table to write to ("expression" or "layers")
            cell_offset: Global cell offset to add to cell_integer_id (for multi-file conversion)
            skip_empty_table: If True, skip creating empty table (for multi-file append mode)
        """
        if layers:
            logger.info("Processing layers data with checkpointing...")
        else:
            logger.info("Processing expression data with checkpointing...")

        # Calculate total chunks
        total_chunks = (reader.n_obs + self.chunk_size - 1) // self.chunk_size
        logger.info(
            f"Processing {total_chunks} chunks with chunk size {self.chunk_size:,}..."
        )

        # Determine starting chunk from checkpoint
        start_chunk = 0
        if checkpoint and checkpoint.get("status") == "in_progress":
            start_chunk = checkpoint.get("last_completed_chunk", 0) + 1
            logger.info(
                f"Resuming from chunk {start_chunk} (last completed: {checkpoint.get('last_completed_chunk', -1)})"
            )

        # Memory monitoring
        process = None
        initial_memory = None
        try:
            import psutil

            process = psutil.Process()
            initial_memory = process.memory_info().rss / 1024 / 1024  # MB
            logger.info(f"Initial memory usage: {initial_memory:.1f} MB")
        except ImportError:
            logger.info("Install psutil for memory monitoring: pip install psutil")

        # Create Lance dataset with schema
        expression_path = f"{output_path}/expression.lance"
        if layers:
            table_path = f"{output_path}/{table_name}.lance"
            # For layers, we need to build schema dynamically from available layers
            if hasattr(reader, "adata") and reader.adata is not None:
                if hasattr(reader.adata, "layers") and reader.adata.layers:
                    layer_names = list(reader.adata.layers.keys())
                    # Build schema for layers table (wide format)
                    value_pa_type = (
                        pa.uint16() if value_type == "uint16" else pa.float32()
                    )
                    schema_fields = [
                        pa.field("cell_integer_id", pa.uint32()),
                        pa.field("gene_integer_id", pa.uint16()),
                    ]
                    for layer_name in layer_names:
                        schema_fields.append(
                            pa.field(layer_name, value_pa_type, nullable=True)
                        )
                    schema = pa.schema(schema_fields)
                else:
                    # No layers, use expression schema
                    schema = self._get_expression_schema(value_type)
            else:
                schema = self._get_expression_schema(value_type)
        else:
            table_path = expression_path
            schema = self._get_expression_schema(value_type)

        # Create empty dataset first (only if not resuming and not skipping)
        if start_chunk == 0 and not skip_empty_table:
            if layers:
                logger.info("Creating initial layers Lance dataset...")
            else:
                logger.info("Creating initial Lance dataset...")
            # Schema already set above

            # Create empty table with correct schema based on settings
            if layers:
                # For layers, create empty table with all layer columns
                if hasattr(reader, "adata") and reader.adata is not None:
                    if hasattr(reader.adata, "layers") and reader.adata.layers:
                        layer_names = list(reader.adata.layers.keys())
                        empty_dict = {
                            "cell_integer_id": pa.array([], type=pa.uint32()),
                            "gene_integer_id": pa.array([], type=pa.uint16()),
                        }
                        value_pa_type = (
                            pa.uint16() if value_type == "uint16" else pa.float32()
                        )
                        for layer_name in layer_names:
                            empty_dict[layer_name] = pa.array([], type=value_pa_type)
                        empty_table = pa.table(empty_dict)
                    else:
                        # No layers, create minimal table
                        empty_table = pa.table(
                            {
                                "cell_integer_id": pa.array([], type=pa.uint32()),
                                "gene_integer_id": pa.array([], type=pa.uint16()),
                            }
                        )
                else:
                    # Can't determine layers, create minimal table
                    empty_table = pa.table(
                        {
                            "cell_integer_id": pa.array([], type=pa.uint32()),
                            "gene_integer_id": pa.array([], type=pa.uint16()),
                        }
                    )
            else:
                # Expression table
                if value_type == "uint16":
                    value_pa_type = pa.uint16()
                elif value_type == "float32":
                    value_pa_type = pa.float32()
                else:
                    raise ValueError(f"Unsupported value type: {value_type}")

                if self.optimize_storage:
                    if self.use_optimized_dtypes:
                        empty_table = pa.table(
                            {
                                "cell_integer_id": pa.array([], type=pa.uint32()),
                                "gene_integer_id": pa.array([], type=pa.uint16()),
                                "value": pa.array([], type=value_pa_type),
                            }
                        )
                    else:
                        empty_table = pa.table(
                            {
                                "cell_integer_id": pa.array([], type=pa.int32()),
                                "gene_integer_id": pa.array([], type=pa.int32()),
                                "value": pa.array([], type=value_pa_type),
                            }
                        )
                else:
                    if self.use_optimized_dtypes:
                        empty_table = pa.table(
                            {
                                "cell_id": pa.array([], type=pa.string()),
                                "gene_id": pa.array([], type=pa.string()),
                                "cell_integer_id": pa.array([], type=pa.uint32()),
                                "gene_integer_id": pa.array([], type=pa.uint16()),
                                "value": pa.array([], type=value_pa_type),
                            }
                        )
                    else:
                        empty_table = pa.table(
                            {
                                "cell_id": pa.array([], type=pa.string()),
                                "gene_id": pa.array([], type=pa.string()),
                                "cell_integer_id": pa.array([], type=pa.int32()),
                                "gene_integer_id": pa.array([], type=pa.int32()),
                                "value": pa.array([], type=value_pa_type),
                            }
                        )

            # Create initial Lance dataset (using max_rows_per_file for large fragments)
            lance.write_dataset(
                empty_table,
                table_path,
                mode="overwrite",
                schema=schema,
                max_rows_per_file=10000000,  # 10M rows per file to avoid memory issues
                enable_v2_manifest_paths=self.enable_v2_manifest,
                data_storage_version="2.1",
            )

        # Process chunks sequentially with checkpointing
        logger.info("Processing chunks sequentially with checkpointing...")
        import time

        from tqdm import tqdm

        processing_start_time = time.time()

        # Create iterator for chunks
        chunk_iterator = reader.iter_chunks(chunk_size=self.chunk_size)

        # Skip chunks if resuming
        for _ in range(start_chunk):
            try:
                next(chunk_iterator)
            except StopIteration:
                logger.warning(
                    f"Tried to skip {start_chunk} chunks but iterator ended early"
                )
                break

        # Process remaining chunks
        for chunk_idx in tqdm(
            range(start_chunk, total_chunks),
            desc="Processing chunks",
            unit="chunk",
            initial=start_chunk,
            total=total_chunks,
        ):
            try:
                # Get next chunk - use layers flag if provided
                if layers:
                    # For layers, we need to read the wide format chunk
                    # Get the slice from the iterator first
                    _, obs_slice = next(chunk_iterator)
                    # Read layers chunk in wide format with cell offset
                    chunk_table = reader._read_chunk_as_arrow(
                        obs_slice.start,
                        obs_slice.stop,
                        layers=True,
                        cell_offset=cell_offset,
                    )
                else:
                    chunk_table, _obs_slice = next(chunk_iterator)

                # Process chunk (data type conversion and string ID addition if needed)
                # For layers, skip dtype conversion as they're already in wide format
                if not layers and not self.use_optimized_dtypes:
                    # Convert from optimized dtypes to standard dtypes (expression only)
                    cell_integer_ids = (
                        chunk_table.column("cell_integer_id")
                        .to_numpy()
                        .astype(np.int32)
                    )
                    gene_integer_ids = (
                        chunk_table.column("gene_integer_id")
                        .to_numpy()
                        .astype(np.int32)
                    )
                    values = chunk_table.column("value").to_numpy().astype(np.float32)

                    chunk_table = pa.table(
                        {
                            "cell_integer_id": pa.array(cell_integer_ids),
                            "gene_integer_id": pa.array(gene_integer_ids),
                            "value": pa.array(values),
                        }
                    )

                # For layers, skip string ID addition as they use wide format
                if not layers and not self.optimize_storage:
                    # Get cell and gene names
                    cell_names = reader.obs_names
                    gene_names = reader.var_names

                    # Create string ID arrays
                    cell_integer_ids = chunk_table.column("cell_integer_id").to_numpy()
                    gene_integer_ids = chunk_table.column("gene_integer_id").to_numpy()

                    cell_ids = cell_names[cell_integer_ids].astype(str)
                    gene_ids = gene_names[gene_integer_ids].astype(str)

                    # Create new table with string IDs
                    chunk_table = pa.table(
                        {
                            "cell_id": pa.array(cell_ids),
                            "gene_id": pa.array(gene_ids),
                            "cell_integer_id": chunk_table.column("cell_integer_id"),
                            "gene_integer_id": chunk_table.column("gene_integer_id"),
                            "value": chunk_table.column("value"),
                        }
                    )

                # Save checkpoint BEFORE writing chunk to avoid duplication
                if self.enable_checkpointing:
                    should_save_checkpoint = (
                        chunk_idx % 10 == 0  # Every 10 chunks
                        or chunk_idx == total_chunks - 1  # Last chunk
                    )

                    if should_save_checkpoint:
                        checkpoint_data = {
                            "status": "in_progress",
                            "last_completed_chunk": chunk_idx,
                            "total_chunks": total_chunks,
                            "chunk_size": self.chunk_size,
                            "timestamp": pd.Timestamp.now().isoformat(),
                        }
                        self._save_checkpoint(output_path, checkpoint_data)

                # Write chunk to Lance dataset (using max_rows_per_file for large fragments)
                lance.write_dataset(
                    chunk_table,
                    table_path,
                    mode="append",
                    max_rows_per_file=10000000,  # 10M rows per file to avoid memory issues
                    enable_v2_manifest_paths=self.enable_v2_manifest,
                    data_storage_version="2.1",
                )

            except StopIteration:
                logger.warning(f"Chunk iterator ended at chunk {chunk_idx}")
                break
            except Exception as e:
                logger.error(f"Error processing chunk {chunk_idx}: {e}")
                # Save checkpoint with error status
                if self.enable_checkpointing:
                    checkpoint_data = {
                        "status": "error",
                        "last_completed_chunk": chunk_idx - 1,
                        "total_chunks": total_chunks,
                        "error": str(e),
                        "timestamp": pd.Timestamp.now().isoformat(),
                    }
                    self._save_checkpoint(output_path, checkpoint_data)
                raise

        # Final memory report
        if process is not None and initial_memory is not None:
            try:
                final_memory = process.memory_info().rss / 1024 / 1024  # MB
                memory_increase = final_memory - initial_memory
                logger.info(
                    f"Final memory usage: {final_memory:.1f} MB (change: {memory_increase:+.1f} MB)"
                )
            except Exception:
                pass

        # Calculate and log overall statistics
        total_processing_time = time.time() - processing_start_time

        logger.info(
            f"Expression data processing complete! "
            f"Processed {total_chunks} chunks in {total_processing_time:.1f}s "
            f"({total_processing_time / total_chunks:.2f}s per chunk average)"
        )

    def _process_file_chunks_with_checkpoint(
        self,
        reader,
        output_path: str,
        file_idx: int,
        start_chunk_idx: int,
        global_cell_offset: int,
    ):
        """Process chunks for a single file with checkpointing support"""
        expression_path = f"{output_path}/expression.lance"

        # Calculate total chunks for this file
        total_chunks = (reader.n_obs + self.chunk_size - 1) // self.chunk_size

        logger.info(
            f"📊 Processing file {file_idx + 1} chunks: {start_chunk_idx}/{total_chunks}"
        )
        if start_chunk_idx > 0:
            logger.info(
                f"🔄 Resuming from chunk {start_chunk_idx} (chunks 0-{start_chunk_idx - 1} already processed)"
            )

        # Create iterator for chunks
        chunk_iterator = reader.iter_chunks(chunk_size=self.chunk_size)

        # Skip chunks if resuming from checkpoint
        if start_chunk_idx > 0:
            logger.info(f"⏭️  Skipping {start_chunk_idx} already-processed chunks...")
            for _ in range(start_chunk_idx):
                try:
                    next(chunk_iterator)
                except StopIteration:
                    logger.warning(
                        f"Tried to skip {start_chunk_idx} chunks but iterator ended early"
                    )
                    break

        # Process remaining chunks
        for chunk_idx in range(start_chunk_idx, total_chunks):
            try:
                # Get next chunk
                chunk_table, _obs_slice = next(chunk_iterator)

                # Log progress every 10 chunks or on first/last chunk
                if (
                    chunk_idx % 10 == 0
                    or chunk_idx == start_chunk_idx
                    or chunk_idx == total_chunks - 1
                ):
                    logger.info(
                        f"📈 Processing chunk {chunk_idx + 1}/{total_chunks} (file {file_idx + 1})"
                    )

                # Adjust cell integer IDs with global offset
                cell_integer_ids = (
                    chunk_table.column("cell_integer_id").to_numpy()
                    + global_cell_offset
                )

                # Create adjusted chunk table
                if self.optimize_storage:
                    # Only store integer IDs for maximum storage efficiency
                    adjusted_chunk = pa.table(
                        {
                            "cell_integer_id": pa.array(cell_integer_ids),
                            "gene_integer_id": chunk_table.column("gene_integer_id"),
                            "value": chunk_table.column("value"),
                        }
                    )
                else:
                    # Store both string and integer IDs for compatibility
                    adjusted_chunk = pa.table(
                        {
                            "cell_id": chunk_table.column("cell_id"),
                            "gene_id": chunk_table.column("gene_id"),
                            "cell_integer_id": pa.array(cell_integer_ids),
                            "gene_integer_id": chunk_table.column("gene_integer_id"),
                            "value": chunk_table.column("value"),
                        }
                    )

                # Save checkpoint BEFORE writing chunk to avoid duplication
                # This ensures we know exactly what was written if a failure occurs
                if self.enable_checkpointing:
                    should_save_checkpoint = (
                        chunk_idx % 10 == 0  # Every 10 chunks
                        or chunk_idx == total_chunks - 1  # Last chunk of file
                    )

                    if should_save_checkpoint:
                        checkpoint_data = {
                            "status": "in_progress",
                            "last_completed_file": file_idx,  # Current file being processed
                            "last_completed_chunk": chunk_idx,  # Current chunk being processed
                            "global_cell_offset": global_cell_offset,
                            "timestamp": pd.Timestamp.now().isoformat(),
                        }
                        self._save_checkpoint(output_path, checkpoint_data)

                # Write chunk directly to expression dataset
                # Check if this is the first chunk of the first file in a new dataset
                is_first_chunk_of_new_dataset = file_idx == 0 and chunk_idx == 0

                # Check if the expression dataset already exists (for append operations)
                expression_dataset_exists = self._path_exists(expression_path)
                if is_first_chunk_of_new_dataset and not expression_dataset_exists:
                    # First chunk of first file in a new dataset - create new dataset
                    lance.write_dataset(
                        adjusted_chunk,
                        expression_path,
                        mode="overwrite",
                        max_rows_per_file=10000000,
                        enable_v2_manifest_paths=self.enable_v2_manifest,
                        data_storage_version="2.1",
                    )
                else:
                    # Append to existing dataset (either subsequent chunks or append operations)
                    lance.write_dataset(
                        adjusted_chunk,
                        expression_path,
                        mode="append",
                        max_rows_per_file=10000000,
                        enable_v2_manifest_paths=self.enable_v2_manifest,
                        data_storage_version="2.1",
                    )

            except StopIteration:
                logger.warning(f"Chunk iterator ended at chunk {chunk_idx}")
                break
            except Exception as e:
                logger.error(
                    f"Error processing chunk {chunk_idx} in file {file_idx + 1}: {e}"
                )
                # Save checkpoint with error status
                if self.enable_checkpointing:
                    checkpoint_data = {
                        "status": "error",
                        "last_completed_file": file_idx,
                        "last_completed_chunk": chunk_idx - 1,
                        "global_cell_offset": global_cell_offset,
                        "error": str(e),
                        "timestamp": pd.Timestamp.now().isoformat(),
                    }
                    self._save_checkpoint(output_path, checkpoint_data)
                raise

    def _validate_optimized_dtypes(self, reader):
        """Validate that data fits in optimized data types and determine appropriate value type"""
        if not self.use_optimized_dtypes:
            return True, "float32"

        logger.info("Validating data fits in optimized data types...")

        # Check if gene count fits in uint16
        if reader.n_vars > 65535:
            logger.info(f"Warning: {reader.n_vars:,} genes exceeds uint16 limit")
            logger.info("Falling back to standard data types")
            return False, "float32"

        # Check if cell count fits in uint32 (0-4,294,967,295)
        if reader.n_obs > 4294967295:
            logger.info(
                f"Warning: {reader.n_obs:,} cells exceeds uint32 limit (4,294,967,295)"
            )
            logger.info("Falling back to standard data types")
            return False, "float32"

        # Sample original data from the file to determine data type
        logger.info("Sampling original expression values to determine data type...")

        # For chunked readers, we need to check the original data type from the file
        # Handle TileDB readers specially
        if hasattr(reader, "_experiment") and reader._experiment is not None:
            # TileDB reader - check original data directly
            try:
                X = reader._experiment.ms[reader.collection_name].X["data"]
                # Sample some original data
                sample_size = min(10000, X.shape[0])
                sample_data = X.read((slice(0, sample_size),)).tables().concat()
                sample_values = sample_data.column("soma_data").to_numpy()

                # Check if original data is integer or float
                is_integer = np.issubdtype(sample_values.dtype, np.integer)
                max_value = np.max(sample_values)
                min_value = np.min(sample_values)

                if is_integer and max_value <= 65535 and min_value >= 0:
                    logger.info(
                        f"Original integer expression values fit in uint16 range: [{min_value}, {max_value}]"
                    )
                    logger.info("Using uint16 for integer count data")
                    return True, "uint16"
                elif not is_integer:
                    # Check if float data contains only integer values
                    rounded_data = np.round(sample_values)
                    is_integer_values = np.allclose(
                        sample_values, rounded_data, rtol=1e-10
                    )

                    if is_integer_values and max_value <= 65535 and min_value >= 0:
                        logger.info(
                            f"Float data contains only integer values: [{min_value}, {max_value}]"
                        )
                        logger.info("Converting to uint16 for count data")
                        return True, "uint16"
                    else:
                        logger.info(
                            f"Float data contains fractional values: [{min_value}, {max_value}]"
                        )
                        logger.info("Keeping as float32 for normalized/float data")
                        return False, "float32"
                else:
                    logger.info(
                        f"Warning: Original integer values range [{min_value}, {max_value}] exceeds uint16 range [0, 65535]"
                    )
                    logger.info("Falling back to float32")
                    return False, "float32"
            except Exception as e:
                logger.warning(f"Error checking TileDB original data: {e}")
                # Fall through to fallback logic
        elif hasattr(reader, "file") and reader.file is not None:
            # Check the original data type from the h5ad file
            X_group = reader.file["X"]
            if "data" in X_group:
                # Sample some original data
                data = X_group["data"]
                sample_size = min(10000, len(data))
                # Use sequential sampling instead of random to avoid indexing issues
                sample_data = data[:sample_size]

                # Check if original data is integer or float
                is_integer = np.issubdtype(sample_data.dtype, np.integer)
                max_value = np.max(sample_data)
                min_value = np.min(sample_data)

                if is_integer and max_value <= 65535 and min_value >= 0:
                    logger.info(
                        f"Original integer expression values fit in uint16 range: [{min_value}, {max_value}]"
                    )
                    logger.info("Using uint16 for integer count data")
                    return True, "uint16"
                elif not is_integer:
                    # Check if float data contains only integer values
                    # Round to nearest integer and check if it's the same
                    rounded_data = np.round(sample_data)
                    is_integer_values = np.allclose(
                        sample_data, rounded_data, rtol=1e-10
                    )

                    if is_integer_values and max_value <= 65535 and min_value >= 0:
                        logger.info(
                            f"Float data contains only integer values: [{min_value}, {max_value}]"
                        )
                        logger.info("Converting to uint16 for count data")
                        return True, "uint16"
                    else:
                        logger.info(
                            f"Float data contains fractional values: [{min_value}, {max_value}]"
                        )
                        logger.info("Keeping as float32 for normalized/float data")
                        return False, "float32"
                else:
                    logger.info(
                        f"Warning: Original integer values range [{min_value}, {max_value}] exceeds uint16 range [0, 65535]"
                    )
                    logger.info("Falling back to float32")
                    return False, "float32"
            else:
                # Fallback to checking processed data
                sample_size = min(100000, reader.n_obs)
                sample_chunks = list(reader.iter_chunks(chunk_size=sample_size))

                if sample_chunks:
                    sample_chunk = sample_chunks[0][0]

                    # Handle different data types from chunked reader
                    if hasattr(sample_chunk, "column"):  # PyArrow Table
                        # Get the value column from the PyArrow table
                        value_column = sample_chunk.column("value")
                        sample_data = value_column.to_numpy()
                    elif sparse.issparse(sample_chunk):
                        sample_data = sample_chunk.data
                    else:
                        sample_data = sample_chunk.flatten()

                    # Check if data is integer or float
                    is_integer = np.issubdtype(sample_data.dtype, np.integer)
                    max_value = np.max(sample_data)
                    min_value = np.min(sample_data)

                    if is_integer and max_value <= 65535 and min_value >= 0:
                        logger.info(
                            f"Integer expression values fit in uint16 range: [{min_value}, {max_value}]"
                        )
                        logger.info("Using uint16 for integer count data")
                        return True, "uint16"
                    elif not is_integer:
                        # Check if float data contains only integer values
                        # Round to nearest integer and check if it's the same
                        rounded_data = np.round(sample_data)
                        is_integer_values = np.allclose(
                            sample_data, rounded_data, rtol=1e-10
                        )

                        if is_integer_values and max_value <= 65535 and min_value >= 0:
                            logger.info(
                                f"Float data contains only integer values: [{min_value}, {max_value}]"
                            )
                            logger.info("Converting to uint16 for count data")
                            return True, "uint16"
                        else:
                            logger.info(
                                f"Float data contains fractional values: [{min_value}, {max_value}]"
                            )
                            logger.info("Keeping as float32 for normalized/float data")
                            return False, "float32"
                    else:
                        logger.info(
                            f"Warning: Integer values range [{min_value}, {max_value}] exceeds uint16 range [0, 65535]"
                        )
                        logger.info("Falling back to float32")
                        return False, "float32"

        logger.info("Data validation passed - using optimized data types")
        return True, "uint16"  # Default to uint16 for integer data

    def _validate_optimized_dtypes_anndata(self, adata):
        """Validate that AnnData object's expression data fits in optimized data types and determine appropriate value type"""
        if not self.use_optimized_dtypes:
            return True, "float32"

        logger.info(
            "Validating AnnData object's expression data fits in optimized data types..."
        )

        # Check if gene count fits in uint16 (0-65535)
        if adata.n_vars > 65535:
            logger.info(f"Warning: {adata.n_vars:,} genes exceeds uint16 limit")
            logger.info("Falling back to standard data types")
            return False, "float32"

        # Check if cell count fits in uint32 (0-4,294,967,295)
        if adata.n_obs > 4294967295:
            logger.warning(
                f"{adata.n_obs:,} cells exceeds uint32 limit (4,294,967,295)"
            )
            logger.info("Falling back to standard data types")
            return False, "float32"

        # Sample some values to check data type and range
        logger.info("Sampling expression values to determine data type...")
        # Handle backed mode where X might not have .data attribute
        if hasattr(adata.X, "data"):
            sample_data = adata.X.data[:100000]
        else:
            # For backed mode, convert to COO and get data
            if hasattr(adata.X, "tocoo"):
                coo = adata.X.tocoo()
            else:
                try:
                    coo = sparse.coo_matrix(adata.X)
                except ValueError:
                    # If dtype is not supported, try to convert to a supported type
                    logger.warning(
                        "adata.X has unsupported dtype, attempting conversion"
                    )
                    try:
                        coo = sparse.coo_matrix(adata.X.astype(np.float32))
                    except Exception:
                        logger.warning(
                            "Could not convert adata.X, using fallback method"
                        )
                        # For backed mode, we might need to load a small sample
                        sample_size = min(1000, adata.n_obs)
                        sample_adata = adata[:sample_size, :]
                        if sparse.issparse(sample_adata.X):
                            coo = sample_adata.X.tocoo()
                        else:
                            coo = sparse.coo_matrix(sample_adata.X)
            sample_data = coo.data[:100000]

        # Check if data is integer or float
        is_integer = np.issubdtype(sample_data.dtype, np.integer)
        max_value = np.max(sample_data)
        min_value = np.min(sample_data)

        if is_integer and max_value <= 65535 and min_value >= 0:
            logger.info(
                f"Integer expression values fit in uint16 range: [{min_value}, {max_value}]"
            )
            logger.info("Using uint16 for integer count data")
            return True, "uint16"
        elif not is_integer:
            logger.info(f"Float expression values detected: [{min_value}, {max_value}]")
            logger.info("Using float32 for normalized/float data")
            return True, "float32"
        else:
            logger.info(
                f"Warning: Integer values range [{min_value}, {max_value}] exceeds uint16 range"
            )
            logger.info("Falling back to float32")
            return False, "float32"

        logger.info(
            "AnnData object's expression data validation passed - using optimized data types"
        )
        return True, "uint16"  # Default to uint16 for integer data

    def _compact_dataset(self, output_path: str):
        """Compact the dataset to optimize storage after writing"""
        logger.info("Compacting dataset for optimal storage...")

        try:
            # Compact expression table
            expression_path = f"{output_path}/expression.lance"
            if self._path_exists(expression_path):
                logger.info("  Compacting expression table...")
                dataset = lance.dataset(expression_path)
                dataset.optimize.compact_files(
                    target_rows_per_fragment=1024 * 1024
                )  # 1M rows per fragment
                logger.info("  Expression table compacted!")
            else:
                logger.warning("  Expression table not found, skipping compaction")

            # Compact metadata tables
            for table_name in ["cells", "genes"]:
                table_path = f"{output_path}/{table_name}.lance"
                if self._path_exists(table_path):
                    logger.info(f"  Compacting {table_name} table...")
                    dataset = lance.dataset(table_path)
                    dataset.optimize.compact_files(
                        target_rows_per_fragment=100000
                    )  # 100K rows per fragment for metadata
                    logger.info(f"  {table_name} table compacted!")
                else:
                    logger.warning(
                        f"  {table_name} table not found, skipping compaction"
                    )

            logger.info("Dataset compaction complete!")
        except Exception as e:
            logger.error(f"Error during dataset compaction: {e}")
            logger.error(
                "Dataset may be in an inconsistent state. Consider recreating without compaction."
            )
            raise

    def _get_expression_schema(self, value_type="uint16"):
        """Get the schema for expression table"""
        # Determine the appropriate value type
        if value_type == "uint16":
            value_pa_type = pa.uint16()
        elif value_type == "float32":
            value_pa_type = pa.float32()
        else:
            raise ValueError(f"Unsupported value type: {value_type}")

        if self.optimize_storage:
            # Only store integer IDs for maximum storage efficiency
            if self.use_optimized_dtypes:
                # Use optimized data types for better compression
                return pa.schema(
                    [
                        ("cell_integer_id", pa.uint32()),
                        ("gene_integer_id", pa.uint16()),
                        ("value", value_pa_type),
                    ]
                )
            else:
                # Use standard data types
                return pa.schema(
                    [
                        ("cell_integer_id", pa.int32()),
                        ("gene_integer_id", pa.int32()),
                        ("value", value_pa_type),
                    ]
                )
        else:
            # Store both string and integer IDs for compatibility
            if self.use_optimized_dtypes:
                return pa.schema(
                    [
                        ("cell_id", pa.string()),
                        ("gene_id", pa.string()),
                        ("cell_integer_id", pa.uint32()),
                        ("gene_integer_id", pa.uint16()),
                        ("value", value_pa_type),
                    ]
                )
            else:
                return pa.schema(
                    [
                        ("cell_id", pa.string()),
                        ("gene_id", pa.string()),
                        ("cell_integer_id", pa.int32()),
                        ("gene_integer_id", pa.int32()),
                        ("value", value_pa_type),
                    ]
                )

    def _create_id_mapping(self, entity_ids, entity_type: str) -> list[dict[str, Any]]:
        """Create mapping from original entity IDs to integer indices"""
        # Direct assignment using pandas operations
        df = pd.DataFrame()
        df[f"{entity_type}_id"] = pd.Series(entity_ids).astype(str)
        df["integer_id"] = range(len(entity_ids))
        return df.to_dict(orient="records")

    def _sparse_to_coo_table(
        self,
        sparse_matrix,
        cell_ids,
        gene_ids,
        value_type="uint16",
    ):
        """Convert scipy sparse matrix to COO format PyArrow table with integer IDs"""
        # Handle backed mode where tocoo() might not be available
        if hasattr(sparse_matrix, "tocoo"):
            coo_matrix = sparse_matrix.tocoo()
        else:
            # For backed mode, convert to COO using scipy
            try:
                coo_matrix = sparse.coo_matrix(sparse_matrix)
            except (ValueError, AttributeError):
                # If dtype is not supported or object doesn't have expected methods
                logger.warning(
                    "sparse_matrix has unsupported dtype or missing methods, using fallback"
                )
                # For CSRDataset objects, we need to handle them differently
                # Let's try to get the data in a way that works for backed mode
                try:
                    # Try to get the data directly from the backed object
                    if hasattr(sparse_matrix, "data"):
                        data = sparse_matrix.data
                        if hasattr(sparse_matrix, "indices") and hasattr(
                            sparse_matrix, "indptr"
                        ):
                            # It's a CSR matrix
                            indices = sparse_matrix.indices
                            indptr = sparse_matrix.indptr
                            # Convert to COO
                            row_indices = []
                            col_indices = []
                            for i in range(len(indptr) - 1):
                                start = indptr[i]
                                end = indptr[i + 1]
                                row_indices.extend([i] * (end - start))
                                col_indices.extend(indices[start:end])
                            coo_matrix = sparse.coo_matrix(
                                (data, (row_indices, col_indices)),
                                shape=sparse_matrix.shape,
                            )
                        else:
                            # Fallback: try to convert to numpy array first
                            try:
                                dense_array = np.array(sparse_matrix)
                                coo_matrix = sparse.coo_matrix(dense_array)
                            except Exception:
                                # Last resort: create empty COO matrix
                                logger.warning(
                                    "Could not convert sparse_matrix, creating empty COO matrix"
                                )
                                coo_matrix = sparse.coo_matrix(sparse_matrix.shape)
                    else:
                        # Try to convert to numpy array first
                        try:
                            dense_array = np.array(sparse_matrix)
                            coo_matrix = sparse.coo_matrix(dense_array)
                        except Exception:
                            # Last resort: create empty COO matrix
                            logger.warning(
                                "Could not convert sparse_matrix, creating empty COO matrix"
                            )
                            coo_matrix = sparse.coo_matrix(sparse_matrix.shape)
                except Exception as e:
                    logger.warning(f"All conversion methods failed: {e}")
                    # Last resort: create empty COO matrix
                    coo_matrix = sparse.coo_matrix(sparse_matrix.shape)

        logger.info(f"Processing {len(coo_matrix.data):,} non-zero elements...")

        # Create integer ID arrays for efficient range queries
        if self.use_optimized_dtypes:
            cell_integer_id_array = coo_matrix.row.astype(np.uint32)
            gene_integer_id_array = coo_matrix.col.astype(np.uint16)
            # Convert values based on the determined type
            if value_type == "uint16":
                value_array = coo_matrix.data.astype(np.uint16)
                value_dtype = np.uint16
                value_pa_type = pa.uint16()
            elif value_type == "float32":
                value_array = coo_matrix.data.astype(np.float32)
                value_dtype = np.float32
                value_pa_type = pa.float32()
            else:
                raise ValueError(f"Unsupported value type: {value_type}")
        else:
            cell_integer_id_array = coo_matrix.row.astype(np.int32)
            gene_integer_id_array = coo_matrix.col.astype(np.int32)
            # Expression values - use the determined type
            if value_type == "uint16":
                value_array = coo_matrix.data.astype(np.uint16)
                value_dtype = np.uint16
                value_pa_type = pa.uint16()
            elif value_type == "float32":
                value_array = coo_matrix.data.astype(np.float32)
                value_dtype = np.float32
                value_pa_type = pa.float32()
            else:
                raise ValueError(f"Unsupported value type: {value_type}")

        # Create string ID arrays
        cell_id_array = np.array(cell_ids)[coo_matrix.row].astype(str)
        gene_id_array = np.array(gene_ids)[coo_matrix.col].astype(str)

        # Check for nulls in string arrays
        if bool(np.any(pd.isnull(cell_id_array))) or bool(
            np.any(pd.isnull(gene_id_array))
        ):
            raise ValueError("Null values found in cell_id or gene_id arrays!")

        # Create table based on storage optimization
        if self.optimize_storage:
            # Only store integer IDs for maximum storage efficiency
            if self.use_optimized_dtypes:
                table = pa.table(
                    {
                        "cell_integer_id": pa.array(
                            cell_integer_id_array, type=pa.uint32()
                        ),
                        "gene_integer_id": pa.array(
                            gene_integer_id_array, type=pa.uint16()
                        ),
                        "value": pa.array(
                            value_array.astype(value_dtype), type=value_pa_type
                        ),
                    }
                )
            else:
                table = pa.table(
                    {
                        "cell_integer_id": pa.array(
                            cell_integer_id_array, type=pa.int32()
                        ),
                        "gene_integer_id": pa.array(
                            gene_integer_id_array, type=pa.int32()
                        ),
                        "value": pa.array(
                            value_array.astype(value_dtype), type=value_pa_type
                        ),
                    }
                )
        else:
            # Store both string and integer IDs for compatibility
            if self.use_optimized_dtypes:
                table = pa.table(
                    {
                        "cell_id": pa.array(cell_id_array, type=pa.string()),
                        "gene_id": pa.array(gene_id_array, type=pa.string()),
                        "cell_integer_id": pa.array(
                            cell_integer_id_array, type=pa.uint32()
                        ),
                        "gene_integer_id": pa.array(
                            gene_integer_id_array, type=pa.uint16()
                        ),
                        "value": pa.array(
                            value_array.astype(value_dtype), type=value_pa_type
                        ),
                    }
                )
            else:
                table = pa.table(
                    {
                        "cell_id": pa.array(cell_id_array, type=pa.string()),
                        "gene_id": pa.array(gene_id_array, type=pa.string()),
                        "cell_integer_id": pa.array(
                            cell_integer_id_array, type=pa.int32()
                        ),
                        "gene_integer_id": pa.array(
                            gene_integer_id_array, type=pa.int32()
                        ),
                        "value": pa.array(
                            value_array.astype(value_dtype), type=value_pa_type
                        ),
                    }
                )

        # Validate schema
        if self.optimize_storage:
            if self.use_optimized_dtypes:
                expected_types = {
                    "cell_integer_id": pa.uint32(),
                    "gene_integer_id": pa.uint16(),
                    "value": value_pa_type,
                }
            else:
                expected_types = {
                    "cell_integer_id": pa.int32(),
                    "gene_integer_id": pa.int32(),
                    "value": value_pa_type,
                }
        else:
            if self.use_optimized_dtypes:
                expected_types = {
                    "cell_id": pa.string(),
                    "gene_id": pa.string(),
                    "cell_integer_id": pa.uint32(),
                    "gene_integer_id": pa.uint16(),
                    "value": value_pa_type,
                }
            else:
                expected_types = {
                    "cell_id": pa.string(),
                    "gene_id": pa.string(),
                    "cell_integer_id": pa.int32(),
                    "gene_integer_id": pa.int32(),
                    "value": value_pa_type,
                }

        # Validate schema
        for col, expected_type in expected_types.items():
            assert table.schema.field(col).type == expected_type, (
                f"{col} is not {expected_type} type!"
            )
            assert table.column(col).null_count == 0, f"Nulls found in {col} column!"

        return table

    def _convert_layers(
        self,
        layers: dict[str, Any],
        output_path: str,
        cell_ids: Any,
        gene_ids: Any,
        value_type: str = "uint16",
    ) -> list[str]:
        """
        Convert AnnData layers to layers.lance table in wide format.

        Args:
            layers: Dictionary of layer names to sparse matrices
            output_path: Output SLAF directory path
            cell_ids: Cell IDs (index or array)
            gene_ids: Gene IDs (index or array)
            value_type: Value type for layer data ("uint16" or "float32")

        Returns:
            List of layer names that were successfully converted
        """
        logger.info("Converting layers to wide format...")

        # Get base structure from expression table (all cell-gene pairs)
        expression_path = f"{output_path}/expression.lance"
        expression_dataset = lance.dataset(expression_path)

        # Read all (cell_integer_id, gene_integer_id) pairs from expression
        expression_df = (
            pl.scan_pyarrow_dataset(expression_dataset)
            .select(["cell_integer_id", "gene_integer_id"])
            .unique()
            .collect()
        )

        # Start with base table
        layers_table = expression_df

        layer_names = []
        for layer_name, layer_matrix in layers.items():
            logger.info(f"Converting layer '{layer_name}'...")

            # Validate layer shape matches expression
            if layer_matrix.shape != (len(cell_ids), len(gene_ids)):
                logger.warning(
                    f"Layer '{layer_name}' shape {layer_matrix.shape} doesn't match "
                    f"expression shape ({len(cell_ids)}, {len(gene_ids)}). Skipping."
                )
                continue

            # Convert to COO format using existing method
            layer_coo_table = self._sparse_to_coo_table(
                sparse_matrix=layer_matrix,
                cell_ids=cell_ids,
                gene_ids=gene_ids,
                value_type=value_type,
            )

            # Convert to Polars DataFrame
            layer_df_raw = pl.from_arrow(layer_coo_table)
            assert isinstance(layer_df_raw, pl.DataFrame), (
                "Expected DataFrame from Arrow table"
            )
            layer_df = layer_df_raw.select(
                ["cell_integer_id", "gene_integer_id", "value"]
            )

            # Rename value column to layer name for wide format
            layer_df = layer_df.rename({"value": layer_name})

            # Left join to add layer column (nullable for sparse data)
            layers_table = layers_table.join(
                layer_df,
                on=["cell_integer_id", "gene_integer_id"],
                how="left",
            )

            layer_names.append(layer_name)

        if not layer_names:
            logger.warning("No valid layers to convert")
            return []

        # Convert back to PyArrow for Lance
        combined_layers = layers_table.to_arrow()

        # Write to layers.lance
        layers_path = f"{output_path}/layers.lance"
        logger.info(f"Writing layers table to {layers_path}...")

        # Use same settings as expression table
        lance.write_dataset(
            combined_layers,
            layers_path,
            mode="overwrite",
            max_rows_per_file=10000000,  # Same as expression table
            enable_v2_manifest_paths=self.enable_v2_manifest,
            data_storage_version="2.1",
        )

        logger.info(f"Successfully converted {len(layer_names)} layers: {layer_names}")
        return layer_names

    def _convert_obsm(
        self,
        obsm: dict[str, np.ndarray],
        output_path: str,
        cell_ids: Any,
        cell_id_mapping: list[dict[str, Any]] | None = None,
    ) -> list[str]:
        """
        Convert AnnData obsm to FixedSizeListArray columns in cells.lance.

        Args:
            obsm: Dictionary of obsm key names to numpy arrays (shape: n_obs × n_dims)
            output_path: Output SLAF directory path
            cell_ids: Cell IDs (index or array)
            cell_id_mapping: Optional integer ID mapping for cells

        Returns:
            List of obsm keys that were successfully converted
        """
        import lance
        import numpy as np
        import pyarrow as pa

        logger.info("Converting obsm embeddings to FixedSizeListArray columns...")

        # Load existing cells.lance table
        cells_path = f"{output_path}/cells.lance"
        cells_dataset = lance.dataset(cells_path)

        obsm_keys = []
        obsm_dimensions = {}

        for key, embedding in obsm.items():
            logger.info(f"Converting obsm '{key}'...")

            # Validate embedding shape
            if embedding.shape[0] != len(cell_ids):
                logger.warning(
                    f"obsm '{key}' shape {embedding.shape[0]} doesn't match "
                    f"n_obs {len(cell_ids)}. Skipping."
                )
                continue

            # Convert to numpy array
            embedding = np.asarray(embedding)
            n_dims = embedding.shape[1] if len(embedding.shape) > 1 else 1

            # Get cell integer IDs in order
            if cell_id_mapping and self.use_integer_keys:
                # Create mapping dict for fast lookup
                # cell_id_mapping has keys: "cell_id" and "integer_id"
                id_map = {
                    item["cell_id"]: item["integer_id"] for item in cell_id_mapping
                }
                integer_ids = np.array(
                    [id_map.get(str(cid), i) for i, cid in enumerate(cell_ids)],
                    dtype=np.uint32,
                )
            else:
                integer_ids = np.arange(len(cell_ids), dtype=np.uint32)

            # Create FixedSizeListArray from embedding
            value_dtype = pa.float32() if embedding.dtype.kind == "f" else pa.float32()
            flat_values = pa.array(embedding.flatten(), type=value_dtype)
            vector_array = pa.FixedSizeListArray.from_arrays(flat_values, n_dims)

            # Create table with integer IDs and vector column
            column_table = pa.table(
                {
                    "cell_integer_id": pa.array(integer_ids, type=pa.uint32()),
                    key: vector_array,
                }
            )

            # Add column to cells.lance using add_columns with UDF
            # Create factory function to avoid loop variable binding issues
            def create_obsm_udf(obsm_key, obsm_column_table):
                """Factory function to create UDF with captured variables"""

                def add_obsm_column_udf(batch):
                    """UDF to add obsm column by joining batch with embedding data"""
                    import polars as pl

                    batch_df = pl.from_arrow(batch)
                    column_df = pl.from_arrow(obsm_column_table)

                    # Join to get the embedding column
                    result_df = batch_df.join(
                        column_df, on=["cell_integer_id"], how="left"
                    )
                    embedding_chunked = (
                        result_df.select([obsm_key]).to_arrow().column(0)
                    )
                    embedding_array = embedding_chunked.combine_chunks()

                    return pa.RecordBatch.from_arrays(
                        [embedding_array], names=[obsm_key]
                    )

                return add_obsm_column_udf

            cells_dataset.add_columns(create_obsm_udf(key, column_table))

            # Reload dataset to ensure consistency
            cells_dataset = lance.dataset(cells_path)

            obsm_keys.append(key)
            obsm_dimensions[key] = n_dims

        if not obsm_keys:
            logger.warning("No valid obsm embeddings to convert")
            return []

        logger.info(
            f"Successfully converted {len(obsm_keys)} obsm embeddings: {obsm_keys}"
        )
        return obsm_keys

    def _convert_varm(
        self,
        varm: dict[str, np.ndarray],
        output_path: str,
        gene_ids: Any,
        gene_id_mapping: list[dict[str, Any]] | None = None,
    ) -> list[str]:
        """
        Convert AnnData varm to FixedSizeListArray columns in genes.lance.

        Args:
            varm: Dictionary of varm key names to numpy arrays (shape: n_vars × n_dims)
            output_path: Output SLAF directory path
            gene_ids: Gene IDs (index or array)
            gene_id_mapping: Optional integer ID mapping for genes

        Returns:
            List of varm keys that were successfully converted
        """
        import lance
        import numpy as np
        import pyarrow as pa

        logger.info("Converting varm embeddings to FixedSizeListArray columns...")

        # Load existing genes.lance table
        genes_path = f"{output_path}/genes.lance"
        genes_dataset = lance.dataset(genes_path)

        varm_keys = []
        varm_dimensions = {}

        for key, embedding in varm.items():
            logger.info(f"Converting varm '{key}'...")

            # Validate embedding shape
            if embedding.shape[0] != len(gene_ids):
                logger.warning(
                    f"varm '{key}' shape {embedding.shape[0]} doesn't match "
                    f"n_vars {len(gene_ids)}. Skipping."
                )
                continue

            # Convert to numpy array
            embedding = np.asarray(embedding)
            n_dims = embedding.shape[1] if len(embedding.shape) > 1 else 1

            # Get gene integer IDs in order
            if gene_id_mapping and self.use_integer_keys:
                # Create mapping dict for fast lookup
                # gene_id_mapping has keys: "gene_id" and "integer_id"
                id_map = {
                    item["gene_id"]: item["integer_id"] for item in gene_id_mapping
                }
                integer_ids = np.array(
                    [id_map.get(str(gid), i) for i, gid in enumerate(gene_ids)],
                    dtype=np.uint16,
                )
            else:
                integer_ids = np.arange(len(gene_ids), dtype=np.uint16)

            # Create FixedSizeListArray from embedding
            value_dtype = pa.float32() if embedding.dtype.kind == "f" else pa.float32()
            flat_values = pa.array(embedding.flatten(), type=value_dtype)
            vector_array = pa.FixedSizeListArray.from_arrays(flat_values, n_dims)

            # Create table with integer IDs and vector column
            column_table = pa.table(
                {
                    "gene_integer_id": pa.array(integer_ids, type=pa.uint16()),
                    key: vector_array,
                }
            )

            # Add column to genes.lance using add_columns with UDF
            # Create factory function to avoid loop variable binding issues
            def create_varm_udf(varm_key, varm_column_table):
                """Factory function to create UDF with captured variables"""

                def add_varm_column_udf(batch):
                    """UDF to add varm column by joining batch with embedding data"""
                    import polars as pl

                    batch_df = pl.from_arrow(batch)
                    column_df = pl.from_arrow(varm_column_table)

                    # Join to get the embedding column
                    result_df = batch_df.join(
                        column_df, on=["gene_integer_id"], how="left"
                    )
                    embedding_chunked = (
                        result_df.select([varm_key]).to_arrow().column(0)
                    )
                    embedding_array = embedding_chunked.combine_chunks()

                    return pa.RecordBatch.from_arrays(
                        [embedding_array], names=[varm_key]
                    )

                return add_varm_column_udf

            genes_dataset.add_columns(create_varm_udf(key, column_table))

            # Reload dataset to ensure consistency
            genes_dataset = lance.dataset(genes_path)

            varm_keys.append(key)
            varm_dimensions[key] = n_dims

        if not varm_keys:
            logger.warning("No valid varm embeddings to convert")
            return []

        logger.info(
            f"Successfully converted {len(varm_keys)} varm embeddings: {varm_keys}"
        )
        return varm_keys

    def _convert_uns(self, uns: dict, output_path: str):
        """
        Convert AnnData uns to uns.json file.

        Args:
            uns: Dictionary of unstructured metadata
            output_path: Output SLAF directory path
        """
        import json

        import numpy as np
        import pandas as pd

        logger.info("Converting uns metadata to uns.json...")

        # Serialize uns to JSON-compatible format
        def json_serialize(value):
            """Convert value to JSON-serializable format"""
            if isinstance(value, np.ndarray):
                return value.tolist()
            elif isinstance(value, np.integer | np.floating):
                return value.item()
            elif isinstance(value, pd.Series):
                return value.tolist()
            elif isinstance(value, dict):
                return {k: json_serialize(v) for k, v in value.items()}
            elif isinstance(value, list):
                return [json_serialize(item) for item in value]
            return value

        uns_serialized = {k: json_serialize(v) for k, v in uns.items()}

        # Write to uns.json
        uns_path = f"{output_path}/uns.json"
        with self._open_file(uns_path, "w") as f:
            json.dump(uns_serialized, f, indent=2)

        logger.info(
            f"Successfully converted uns metadata with {len(uns_serialized)} keys"
        )

    def _check_layer_consistency(
        self, input_files: list[str], input_format: str
    ) -> list[str] | None:
        """
        Check if layers are consistent across all input files.

        Args:
            input_files: List of input file paths
            input_format: Format of input files

        Returns:
            List of layer names if consistent, None if inconsistent
        """
        if input_format not in ["h5ad", "10x_h5"]:
            # Only h5ad and 10x_h5 formats support layers
            return None

        layer_sets = []
        for file_path in input_files:
            try:
                # Use chunked reader to check layers
                with create_chunked_reader(
                    file_path,
                    chunk_size=self.chunk_size,
                    collection_name=self.tiledb_collection_name,
                ) as reader:
                    if hasattr(reader, "adata") and reader.adata is not None:
                        if hasattr(reader.adata, "layers") and reader.adata.layers:
                            layer_names = sorted(reader.adata.layers.keys())
                            layer_sets.append(set(layer_names))
                        else:
                            # No layers in this file
                            layer_sets.append(set())
                    else:
                        # Can't check layers for this file type
                        return None
            except Exception as e:
                logger.warning(
                    f"Could not check layers in {file_path}: {e}. Skipping layer consistency check."
                )
                return None

        if not layer_sets:
            # No layers in any file
            return []

        # Check if all files have the same layers
        first_set = layer_sets[0]
        if all(layer_set == first_set for layer_set in layer_sets):
            # All files have the same layers
            return sorted(first_set)
        else:
            # Layers are inconsistent
            logger.warning(
                f"Layer inconsistency detected across files. "
                f"File 1 has layers: {sorted(layer_sets[0])}, "
                f"but other files have different layers."
            )
            return None

    def _create_metadata_table(
        self,
        df: pd.DataFrame,
        entity_id_col: str,
        integer_mapping: list[dict[str, Any]] | None = None,
    ) -> pa.Table:
        result_df = df.copy()
        # Assign entity ID column using index directly to avoid misalignment
        result_df[entity_id_col] = df.index.astype(str)
        if integer_mapping and self.use_integer_keys:
            integer_id_col = f"{entity_id_col.replace('_id', '')}_integer_id"
            result_df[integer_id_col] = range(len(df))
        result_df = result_df.where(pd.notnull(result_df), None)
        # Convert all categorical/object columns to string for Arrow compatibility
        for col in result_df.columns:
            if (
                isinstance(result_df[col].dtype, pd.CategoricalDtype)
                or result_df[col].dtype == object
            ):
                result_df[col] = result_df[col].astype(str)
        # Ensure all ID columns are string and non-null
        result_df[entity_id_col] = result_df[entity_id_col].astype(str)
        if bool(result_df[entity_id_col].isnull().any()):
            raise ValueError(f"Null values found in {entity_id_col} column!")

        # Reset index to avoid __index_level_0__ column in Arrow table
        result_df = result_df.reset_index(drop=True)

        # Sanitize column names: replace '.' with '_' for Lance compatibility
        # Lance doesn't allow '.' in field names
        column_mapping = {
            col: col.replace(".", "_") for col in result_df.columns if "." in col
        }
        if column_mapping:
            result_df = result_df.rename(columns=column_mapping)
            logger.debug(f"Sanitized column names: {column_mapping}")

        table = pa.table(result_df)

        return table

    def _write_lance_tables(
        self, output_path: str, table_configs: list[tuple[str, pa.Table]]
    ):
        """Write multiple Lance tables with consistent naming"""
        for table_name, table in table_configs:
            table_path = f"{output_path}/{table_name}.lance"

            # Write table with basic settings (using max_rows_per_file for large fragments)
            if table_name == "expression":
                lance.write_dataset(
                    table,
                    table_path,
                    max_rows_per_file=10000000,  # 10M rows per file to avoid memory issues
                    enable_v2_manifest_paths=self.enable_v2_manifest,
                    data_storage_version="2.1",
                )
            else:
                lance.write_dataset(
                    table,
                    table_path,
                    enable_v2_manifest_paths=self.enable_v2_manifest,
                    data_storage_version="2.1",
                )

        # Create indices after all tables are written (if enabled)
        if self.create_indices:
            self._create_indices(output_path)

    def _create_indices(self, output_path: str):
        """Create optimal indices for SLAF tables with column existence checks"""
        logger.info("Creating indices for optimal query performance...")

        # Define desired indices for each table
        # For small datasets, create fewer indices to reduce overhead
        table_indices = {
            "cells": [
                "cell_id",
                "cell_integer_id",
                # Only create metadata indices for larger datasets
            ],
            "genes": ["gene_id", "gene_integer_id"],
            "expression": [
                "cell_integer_id",
                "gene_integer_id",
            ],  # Only integer indices for efficiency
        }

        # Create indices for each table
        for table_name, desired_columns in table_indices.items():
            table_path = f"{output_path}/{table_name}.lance"
            if self._path_exists(table_path):
                dataset = lance.dataset(table_path)
                schema = dataset.schema

                for column in desired_columns:
                    if column in schema.names:
                        logger.info(f"  Creating index on {table_name}.{column}")
                        dataset.create_scalar_index(column, "BTREE")

        logger.info("Index creation complete!")

    def _compute_cell_start_indices(self, reader, obs_df: pd.DataFrame) -> list[int]:
        """Compute cell start indices during metadata creation"""
        # Check for existing n_genes or gene_count column
        if "n_genes" in obs_df.columns:
            logger.info("Using existing n_genes column for cell start indices")
            gene_counts = obs_df["n_genes"].to_numpy()
        elif "gene_count" in obs_df.columns:
            logger.info("Using existing gene_count column for cell start indices")
            gene_counts = obs_df["gene_count"].to_numpy()
        elif "n_genes_by_counts" in obs_df.columns:
            logger.info(
                "Using existing n_genes_by_counts column for cell start indices"
            )
            gene_counts = obs_df["n_genes_by_counts"].to_numpy()
        else:
            # Calculate from expression data
            logger.info("Calculating gene counts from expression data...")

            # Collect all chunk counts
            all_chunk_gene_counts = []
            for chunk_table, _obs_slice in reader.iter_chunks(
                chunk_size=self.chunk_size
            ):
                # Count genes per cell in this chunk using Polars groupby
                chunk_df = pl.from_arrow(chunk_table)
                assert isinstance(chunk_df, pl.DataFrame)
                if len(chunk_df) > 0:
                    chunk_gene_counts = chunk_df.group_by("cell_integer_id").agg(
                        pl.len().alias("count")
                    )
                    all_chunk_gene_counts.append(chunk_gene_counts)

            # Concatenate and aggregate all chunk counts
            if all_chunk_gene_counts:
                combined_gene_counts = pl.concat(all_chunk_gene_counts)
                final_gene_counts = combined_gene_counts.group_by(
                    "cell_integer_id"
                ).agg(pl.sum("count").alias("count"))

                # Create a complete gene counts array for all cells
                # Initialize with zeros for all cells
                gene_counts = np.zeros(len(obs_df), dtype=np.int64)

                # Fill in the counts for cells that have expression data
                cell_ids = final_gene_counts["cell_integer_id"].to_numpy()
                counts = final_gene_counts["count"].to_numpy()
                gene_counts[cell_ids] = counts

                logger.info(f"Gene counts: {gene_counts}")
            else:
                gene_counts = np.zeros(len(obs_df), dtype=np.int64)

        # Compute cumulative sum with first value as 0
        return np.insert(np.cumsum(gene_counts)[:-1], 0, 0).tolist()

    def _compute_cell_start_indices_anndata(
        self, adata, obs_df: pd.DataFrame
    ) -> list[int]:
        """Compute cell start indices for AnnData object"""
        # Check for existing n_genes or gene_count column
        if "n_genes" in obs_df.columns:
            logger.info("Using existing n_genes column for cell start indices")
            gene_counts = obs_df["n_genes"].to_numpy()
        elif "gene_count" in obs_df.columns:
            logger.info("Using existing gene_count column for cell start indices")
            gene_counts = obs_df["gene_count"].to_numpy()
        elif "n_genes_by_counts" in obs_df.columns:
            logger.info(
                "Using existing n_genes_by_counts column for cell start indices"
            )
            gene_counts = obs_df["n_genes_by_counts"].to_numpy()
        else:
            # Calculate from expression data
            logger.info("Calculating gene counts from expression data...")
            # Convert sparse matrix to COO to count genes per cell
            if sparse.issparse(adata.X):
                if hasattr(adata.X, "tocoo"):
                    coo = adata.X.tocoo()
                else:
                    # Handle backed mode where tocoo() might not be available
                    try:
                        coo = sparse.coo_matrix(adata.X)
                    except ValueError:
                        # If dtype is not supported, try to convert to a supported type
                        logger.warning(
                            "adata.X has unsupported dtype, attempting conversion"
                        )
                        # Try to convert to float32 first
                        try:
                            coo = sparse.coo_matrix(adata.X.astype(np.float32))
                        except Exception:
                            # If that fails, try to get the data in a different way
                            logger.warning(
                                "Could not convert adata.X, using fallback method"
                            )
                            # For backed mode, we might need to load a small sample
                            sample_size = min(1000, adata.n_obs)
                            sample_adata = adata[:sample_size, :]
                            if sparse.issparse(sample_adata.X):
                                coo = sample_adata.X.tocoo()
                            else:
                                coo = sparse.coo_matrix(sample_adata.X)
            else:
                try:
                    coo = sparse.coo_matrix(adata.X)
                except ValueError:
                    # If dtype is not supported, try to convert to a supported type
                    logger.warning(
                        "adata.X has unsupported dtype, attempting conversion"
                    )
                    try:
                        coo = sparse.coo_matrix(adata.X.astype(np.float32))
                    except Exception:
                        logger.warning(
                            "Could not convert adata.X, using fallback method"
                        )
                        # For backed mode, we might need to load a small sample
                        sample_size = min(1000, adata.n_obs)
                        sample_adata = adata[:sample_size, :]
                        if sparse.issparse(sample_adata.X):
                            coo = sample_adata.X.tocoo()
                        else:
                            coo = sparse.coo_matrix(sample_adata.X)

            # Count genes per cell using numpy bincount
            n_cells = adata.n_obs
            gene_counts = np.bincount(coo.row, minlength=n_cells)

        # Compute cumulative sum with first value as 0
        return np.insert(np.cumsum(gene_counts)[:-1], 0, 0).tolist()

    def _compute_expression_statistics(
        self, expression_dataset
    ) -> tuple[dict[str, float], int]:
        """Compute basic statistics from expression dataset using SQL"""
        # Use Polars to compute statistics directly from Lance dataset

        logger.info(
            "Computing expression statistics using fragment-by-fragment processing..."
        )

        # Initialize running statistics
        running_stats = {
            "min_value": float("inf"),
            "max_value": float("-inf"),
            "sum_value": 0.0,
            "sum_squared": 0.0,
            "count": 0,
        }

        # Process each fragment individually to avoid memory issues
        fragments = expression_dataset.get_fragments()
        total_fragments = len(fragments)

        logger.info(
            f"Processing {total_fragments} fragments for statistics computation..."
        )

        from tqdm import tqdm

        for i, fragment in enumerate(tqdm(fragments, desc="Computing statistics")):
            try:
                # Create Polars LazyFrame from this fragment
                ldf = pl.scan_pyarrow_dataset(fragment)

                # Compute fragment-level statistics
                fragment_stats = ldf.select(
                    [
                        pl.col("value").min().alias("min_value"),
                        pl.col("value").max().alias("max_value"),
                        pl.col("value").sum().alias("sum_value"),
                        (pl.col("value") ** 2).sum().alias("sum_squared"),
                        pl.col("value").count().alias("count"),
                    ]
                ).collect()

                # Extract values from the result
                row = fragment_stats.row(0)
                frag_min, frag_max, frag_sum, frag_sum_squared, frag_count = row

                # Update running statistics
                running_stats["min_value"] = min(running_stats["min_value"], frag_min)
                running_stats["max_value"] = max(running_stats["max_value"], frag_max)
                running_stats["sum_value"] += frag_sum
                running_stats["sum_squared"] += frag_sum_squared
                running_stats["count"] += frag_count

            except Exception as e:
                logger.warning(f"Error processing fragment {i}: {e}")
                logger.warning("Continuing with remaining fragments...")
                continue

        # Compute final statistics
        if running_stats["count"] == 0:
            logger.warning("No valid data found for statistics computation")
            return {
                "min_value": 0.0,
                "max_value": 0.0,
                "mean_value": 0.0,
                "std_value": 0.0,
            }, 0

        # Calculate mean
        mean_value = running_stats["sum_value"] / running_stats["count"]

        # Calculate standard deviation using the formula: sqrt((sum(x²) - n*mean²) / (n-1))
        variance = (
            running_stats["sum_squared"] - running_stats["count"] * mean_value**2
        ) / (running_stats["count"] - 1)
        std_value = variance**0.5 if variance > 0 else 0.0

        stats = {
            "min_value": float(running_stats["min_value"]),
            "max_value": float(running_stats["max_value"]),
            "mean_value": float(mean_value),
            "std_value": float(std_value),
        }

        logger.info(
            f"Statistics computed: min={stats['min_value']:.2f}, max={stats['max_value']:.2f}, mean={stats['mean_value']:.2f}, std={stats['std_value']:.2f}"
        )

        return stats, int(running_stats["count"])

    def _save_config(
        self,
        output_path: str,
        shape: tuple,
        layer_names: list[str] | None = None,
        obsm_keys: list[str] | None = None,
        varm_keys: list[str] | None = None,
    ):
        """Save SLAF configuration with computed metadata"""
        n_cells = int(shape[0])
        n_genes = int(shape[1])

        # Compute additional metadata for faster info() method
        logger.info("Computing dataset statistics...")

        # Reference Lance dataset
        expression = lance.dataset(f"{output_path}/expression.lance")

        # Compute basic statistics and count from expression data
        expression_stats, expression_count = self._compute_expression_statistics(
            expression
        )

        total_possible_elements = n_cells * n_genes
        sparsity = 1 - (expression_count / total_possible_elements)

        # Load existing config to preserve checkpoint data
        config_path = f"{output_path}/config.json"
        existing_config = {}
        if self._path_exists(config_path):
            try:
                with self._open_file(config_path) as f:
                    existing_config = json.load(f)
            except Exception:
                pass  # If we can't load existing config, start fresh

        # Build tables dict
        tables = {
            "expression": "expression.lance",
            "cells": "cells.lance",
            "genes": "genes.lance",
        }

        # Add layers table if layers were converted
        if layer_names and len(layer_names) > 0:
            tables["layers"] = "layers.lance"

        config = {
            "format_version": "0.4",  # Bumped to 0.4 for layers support
            "array_shape": [n_cells, n_genes],
            "n_cells": n_cells,
            "n_genes": n_genes,
            "tables": tables,
            "optimizations": {
                "use_integer_keys": self.use_integer_keys,
                "optimize_storage": self.optimize_storage,
            },
            "metadata": {
                "expression_count": int(expression_count),
                "sparsity": float(sparsity),
                "density": float(1 - sparsity),
                "total_possible_elements": int(total_possible_elements),
                "expression_stats": expression_stats,
            },
            "created_at": pd.Timestamp.now().isoformat(),
        }

        # Add layers metadata if layers were converted
        if layer_names and len(layer_names) > 0:
            # All converted layers are immutable by default
            config["layers"] = {
                "available": layer_names,
                "immutable": layer_names,  # Converted layers cannot be deleted
                "mutable": [],  # No mutable layers yet
            }

        # Add obsm metadata if obsm was converted
        if obsm_keys and len(obsm_keys) > 0:
            # Get dimensions from cells.lance schema
            cells_dataset = lance.dataset(f"{output_path}/cells.lance")
            obsm_dimensions = {}
            schema = cells_dataset.schema
            for field in schema:
                if field.name in obsm_keys and isinstance(
                    field.type, pa.FixedSizeListType
                ):
                    obsm_dimensions[field.name] = field.type.list_size

            # All converted obsm keys are immutable by default
            config["obsm"] = {
                "available": obsm_keys,
                "immutable": obsm_keys,  # Converted obsm cannot be deleted
                "mutable": [],  # No mutable obsm yet
                "dimensions": obsm_dimensions,
            }

        # Add varm metadata if varm was converted
        if varm_keys and len(varm_keys) > 0:
            # Get dimensions from genes.lance schema
            genes_dataset = lance.dataset(f"{output_path}/genes.lance")
            varm_dimensions = {}
            schema = genes_dataset.schema
            for field in schema:
                if field.name in varm_keys and isinstance(
                    field.type, pa.FixedSizeListType
                ):
                    varm_dimensions[field.name] = field.type.list_size

            # All converted varm keys are immutable by default
            config["varm"] = {
                "available": varm_keys,
                "immutable": varm_keys,  # Converted varm cannot be deleted
                "mutable": [],  # No mutable varm yet
                "dimensions": varm_dimensions,
            }

        # Preserve checkpoint data if it exists
        if "checkpoint" in existing_config:
            config["checkpoint"] = existing_config["checkpoint"]

        config_path = f"{output_path}/config.json"
        with self._open_file(config_path, "w") as f:
            json.dump(config, f, indent=2)

    def _save_multi_file_config(
        self,
        output_path: str,
        source_file_info: list,
        combined_expression: pa.Table | None = None,
        combined_cells: pa.Table | None = None,
        combined_genes: pa.Table | None = None,
        layer_names: list[str] | None = None,
    ):
        """Save SLAF configuration for multi-file conversion with source file tracking"""

        # If tables are not provided, load them from the Lance datasets
        if combined_cells is None or combined_genes is None:
            cells_dataset = lance.dataset(f"{output_path}/cells.lance")
            genes_dataset = lance.dataset(f"{output_path}/genes.lance")
            n_cells = len(cells_dataset.to_table())
            n_genes = len(genes_dataset.to_table())
        else:
            n_cells = len(combined_cells) if combined_cells is not None else 0
            n_genes = len(combined_genes) if combined_genes is not None else 0

        # Compute additional metadata for faster info() method
        logger.info("Computing multi-file dataset statistics...")

        # Reference Lance dataset
        expression = lance.dataset(f"{output_path}/expression.lance")

        # Compute basic statistics and count from expression data
        expression_stats, expression_count = self._compute_expression_statistics(
            expression
        )

        total_possible_elements = n_cells * n_genes
        sparsity = (
            1 - (expression_count / total_possible_elements)
            if total_possible_elements > 0
            else 1.0
        )

        # Calculate total cells and genes from source files
        total_cells_from_files = sum(info["n_cells"] for info in source_file_info)

        config = {
            "format_version": "0.4",  # Bumped to 0.4 for layers support
            "array_shape": [n_cells, n_genes],
            "n_cells": n_cells,
            "n_genes": n_genes,
            "tables": {
                "expression": "expression.lance",
                "cells": "cells.lance",
                "genes": "genes.lance",
            },
            "optimizations": {
                "use_integer_keys": self.use_integer_keys,
                "optimize_storage": self.optimize_storage,
            },
            "metadata": {
                "expression_count": int(expression_count),
                "sparsity": float(sparsity),
                "density": float(1 - sparsity),
                "total_possible_elements": int(total_possible_elements),
                "expression_stats": expression_stats,
            },
            "multi_file": {
                "source_files": source_file_info,
                "total_files": len(source_file_info),
                "total_cells_from_files": total_cells_from_files,
            },
            "created_at": pd.Timestamp.now().isoformat(),
        }

        # Add layers metadata if layers were converted
        if layer_names:
            config["tables"]["layers"] = "layers.lance"
            config["layers"] = {
                "available": layer_names,
                "immutable": layer_names,  # All converted layers are immutable
                "mutable": [],
            }

        config_path = f"{output_path}/config.json"
        with self._open_file(config_path, "w") as f:
            json.dump(config, f, indent=2)

Functions

__init__(use_integer_keys: bool = True, chunked: bool = True, chunk_size: int = 5000, sort_metadata: bool = False, create_indices: bool = False, optimize_storage: bool = True, use_optimized_dtypes: bool = True, enable_v2_manifest: bool = True, compact_after_write: bool = False, tiledb_collection_name: str = 'RNA', enable_checkpointing: bool = True)

Initialize converter with optimization options.

Parameters:

Name Type Description Default
use_integer_keys bool

Use integer keys instead of strings in sparse data. This saves significant memory and improves query performance. Set to False only if you need to preserve original string IDs.

True
chunked bool

Use chunked processing for memory efficiency (default: True). Chunked processing is now the default for optimal memory efficiency. Set to False only for small datasets or debugging purposes.

True
chunk_size int

Size of each chunk when chunked=True (default: 5000).

5000
create_indices bool

Whether to create indices for query performance. Default: False for small datasets to reduce storage overhead. Set to True for large datasets where query performance is important.

False
optimize_storage bool

Only store integer IDs in expression table to reduce storage size. String IDs are available in metadata tables for mapping.

True
use_optimized_dtypes bool

Use optimized data types (uint16/uint32) for better compression. This can significantly reduce storage size for large datasets.

True
enable_v2_manifest bool

Enable v2 manifest paths for better query performance. This is recommended for large datasets.

True
compact_after_write bool

Compact the dataset after writing to optimize storage. This creates a new version but significantly reduces file size.

False
tiledb_collection_name str

Name of the measurement collection for TileDB format. Default: "RNA". Only used when converting from TileDB format.

'RNA'
enable_checkpointing bool

Enable checkpointing for long-running conversions. This allows resuming from the last completed chunk if the conversion is interrupted. Default: True.

True

Examples:

>>> # Default optimization (recommended)
>>> converter = SLAFConverter()
>>> print(f"Using chunked processing: {converter.chunked}")
Using chunked processing: True
>>> # Non-chunked processing for small datasets
>>> converter = SLAFConverter(chunked=False)
>>> print(f"Using chunked processing: {converter.chunked}")
Using chunked processing: False
>>> # Custom chunk size for large datasets
>>> converter = SLAFConverter(chunk_size=10000)
>>> print(f"Chunk size: {converter.chunk_size}")
Chunk size: 5000
Source code in slaf/data/converter.py
def __init__(
    self,
    use_integer_keys: bool = True,
    chunked: bool = True,  # Changed from False to True - make chunked the default
    chunk_size: int = 5000,  # Smaller chunks to avoid memory alignment issues in Lance v2.1
    sort_metadata: bool = False,
    create_indices: bool = False,  # Disable indices by default for small datasets
    optimize_storage: bool = True,  # Only store integer IDs in expression table
    use_optimized_dtypes: bool = True,  # Use uint16/uint32 for better compression
    enable_v2_manifest: bool = True,  # Enable v2 manifest paths for better performance
    compact_after_write: bool = False,  # Compact dataset after writing for optimal storage (disabled by default to avoid manifest corruption)
    tiledb_collection_name: str = "RNA",  # Collection name for TileDB format
    enable_checkpointing: bool = True,  # Enable checkpointing for long-running conversions
):
    """
    Initialize converter with optimization options.

    Args:
        use_integer_keys: Use integer keys instead of strings in sparse data.
                         This saves significant memory and improves query performance.
                         Set to False only if you need to preserve original string IDs.
        chunked: Use chunked processing for memory efficiency (default: True).
                Chunked processing is now the default for optimal memory efficiency.
                Set to False only for small datasets or debugging purposes.
        chunk_size: Size of each chunk when chunked=True (default: 5000).
        create_indices: Whether to create indices for query performance.
                      Default: False for small datasets to reduce storage overhead.
                      Set to True for large datasets where query performance is important.
        optimize_storage: Only store integer IDs in expression table to reduce storage size.
                       String IDs are available in metadata tables for mapping.
        use_optimized_dtypes: Use optimized data types (uint16/uint32) for better compression.
                            This can significantly reduce storage size for large datasets.
        enable_v2_manifest: Enable v2 manifest paths for better query performance.
                          This is recommended for large datasets.
        compact_after_write: Compact the dataset after writing to optimize storage.
                           This creates a new version but significantly reduces file size.
        tiledb_collection_name: Name of the measurement collection for TileDB format.
                              Default: "RNA". Only used when converting from TileDB format.
        enable_checkpointing: Enable checkpointing for long-running conversions.
                            This allows resuming from the last completed chunk if the
                            conversion is interrupted. Default: True.

    Examples:
        >>> # Default optimization (recommended)
        >>> converter = SLAFConverter()
        >>> print(f"Using chunked processing: {converter.chunked}")
        Using chunked processing: True

        >>> # Non-chunked processing for small datasets
        >>> converter = SLAFConverter(chunked=False)
        >>> print(f"Using chunked processing: {converter.chunked}")
        Using chunked processing: False

        >>> # Custom chunk size for large datasets
        >>> converter = SLAFConverter(chunk_size=10000)
        >>> print(f"Chunk size: {converter.chunk_size}")
        Chunk size: 5000
    """
    self.use_integer_keys = use_integer_keys
    self.chunked = chunked
    self.chunk_size = chunk_size
    self.sort_metadata = sort_metadata
    self.create_indices = create_indices
    self.optimize_storage = optimize_storage
    self.use_optimized_dtypes = use_optimized_dtypes
    self.enable_v2_manifest = enable_v2_manifest
    self.compact_after_write = compact_after_write
    self.tiledb_collection_name = tiledb_collection_name
    self.enable_checkpointing = enable_checkpointing

convert(input_path: str | list[str], output_path: str, input_format: str = 'auto', skip_validation: bool = False)

Convert single-cell data to SLAF format with optimized storage.

SLAFConverter provides efficient conversion from various single-cell data formats to the SLAF format. It optimizes storage by using integer keys, COO-style expression tables, and efficient metadata handling.

Supported Input Formats
  • h5ad: AnnData files (.h5ad) - the standard single-cell format
  • 10x MTX: 10x Genomics MTX directories containing matrix.mtx, barcodes.tsv, and genes.tsv files
  • 10x H5: 10x Genomics H5 files (.h5) - Cell Ranger output format
  • tiledb: TileDB SOMA format (.tiledb) - high-performance single-cell format

The converter automatically detects the input format based on file extension and directory structure. For optimal performance, you can also specify the format explicitly.

Parameters:

Name Type Description Default
input_path str | list[str]

Path to the input file or directory to convert. - For h5ad: path to .h5ad file - For MTX: path to directory containing matrix.mtx, barcodes.tsv, genes.tsv - For H5: path to .h5 file

required
output_path str

Path where the SLAF dataset will be saved. Should be a directory path, not a file path.

required
input_format str

Format of input data. Options: - "auto" (default): Auto-detect format - "h5ad": AnnData format - "10x_mtx": 10x MTX directory format - "10x_h5": 10x H5 file format - "tiledb": TileDB SOMA format

'auto'

Raises:

Type Description
FileNotFoundError

If the input file doesn't exist.

ValueError

If the input file is corrupted, invalid, or format cannot be detected.

RuntimeError

If the conversion process fails.

Examples:

>>> # Auto-detect format (recommended)
>>> converter = SLAFConverter()
>>> converter.convert("data.h5ad", "output.slaf")
Converting data.h5ad to SLAF format...
Optimizations: int_keys=True
Loaded: 1000 cells × 20000 genes
Conversion complete! Saved to output.slaf
>>> # Convert 10x MTX directory
>>> converter.convert("filtered_feature_bc_matrix/", "output.slaf")
Converting 10x MTX directory filtered_feature_bc_matrix/ to SLAF format...
Loaded: 2700 cells × 32738 genes
Conversion complete! Saved to output.slaf
>>> # Convert 10x H5 file
>>> converter.convert("data.h5", "output.slaf")
Converting 10x H5 file data.h5 to SLAF format...
Loaded: 2700 cells × 32738 genes
Conversion complete! Saved to output.slaf
>>> # Convert TileDB SOMA file
>>> converter.convert("data.tiledb", "output.slaf")
Converting TileDB SOMA file data.tiledb to SLAF format...
Loaded: 50000 cells × 20000 genes
Conversion complete! Saved to output.slaf
>>> # Explicit format specification
>>> converter.convert("data.h5", "output.slaf", input_format="10x_h5")
Converting 10x H5 file data.h5 to SLAF format...
Loaded: 2700 cells × 32738 genes
Conversion complete! Saved to output.slaf
>>> # Convert with chunked processing for large datasets
>>> converter = SLAFConverter(chunked=True, chunk_size=5000)
>>> converter.convert("large_data.h5ad", "output.slaf")
Converting large_data.h5ad to SLAF format...
Optimizations: int_keys=True, chunked=True
Processing in chunks of 5000 cells...
Conversion complete! Saved to output.slaf
>>> # Error handling for unsupported format
>>> try:
...     converter.convert("unknown_file.txt", "output.slaf")
... except ValueError as e:
...     print(f"Error: {e}")
Error: Cannot detect format for: unknown_file.txt
Source code in slaf/data/converter.py
def convert(
    self,
    input_path: str | list[str],
    output_path: str,
    input_format: str = "auto",
    skip_validation: bool = False,
):
    """
    Convert single-cell data to SLAF format with optimized storage.

    SLAFConverter provides efficient conversion from various single-cell data formats
    to the SLAF format. It optimizes storage by using integer keys, COO-style
    expression tables, and efficient metadata handling.

    Supported Input Formats:
        - **h5ad**: AnnData files (.h5ad) - the standard single-cell format
        - **10x MTX**: 10x Genomics MTX directories containing matrix.mtx,
          barcodes.tsv, and genes.tsv files
        - **10x H5**: 10x Genomics H5 files (.h5) - Cell Ranger output format
        - **tiledb**: TileDB SOMA format (.tiledb) - high-performance single-cell format

    The converter automatically detects the input format based on file extension
    and directory structure. For optimal performance, you can also specify the
    format explicitly.

    Args:
        input_path: Path to the input file or directory to convert.
                   - For h5ad: path to .h5ad file
                   - For MTX: path to directory containing matrix.mtx, barcodes.tsv, genes.tsv
                   - For H5: path to .h5 file
        output_path: Path where the SLAF dataset will be saved.
                    Should be a directory path, not a file path.
        input_format: Format of input data. Options:
                     - "auto" (default): Auto-detect format
                     - "h5ad": AnnData format
                     - "10x_mtx": 10x MTX directory format
                     - "10x_h5": 10x H5 file format
                     - "tiledb": TileDB SOMA format

    Raises:
        FileNotFoundError: If the input file doesn't exist.
        ValueError: If the input file is corrupted, invalid, or format cannot be detected.
        RuntimeError: If the conversion process fails.

    Examples:
        >>> # Auto-detect format (recommended)
        >>> converter = SLAFConverter()
        >>> converter.convert("data.h5ad", "output.slaf")
        Converting data.h5ad to SLAF format...
        Optimizations: int_keys=True
        Loaded: 1000 cells × 20000 genes
        Conversion complete! Saved to output.slaf

        >>> # Convert 10x MTX directory
        >>> converter.convert("filtered_feature_bc_matrix/", "output.slaf")
        Converting 10x MTX directory filtered_feature_bc_matrix/ to SLAF format...
        Loaded: 2700 cells × 32738 genes
        Conversion complete! Saved to output.slaf

        >>> # Convert 10x H5 file
        >>> converter.convert("data.h5", "output.slaf")
        Converting 10x H5 file data.h5 to SLAF format...
        Loaded: 2700 cells × 32738 genes
        Conversion complete! Saved to output.slaf

        >>> # Convert TileDB SOMA file
        >>> converter.convert("data.tiledb", "output.slaf")
        Converting TileDB SOMA file data.tiledb to SLAF format...
        Loaded: 50000 cells × 20000 genes
        Conversion complete! Saved to output.slaf

        >>> # Explicit format specification
        >>> converter.convert("data.h5", "output.slaf", input_format="10x_h5")
        Converting 10x H5 file data.h5 to SLAF format...
        Loaded: 2700 cells × 32738 genes
        Conversion complete! Saved to output.slaf

        >>> # Convert with chunked processing for large datasets
        >>> converter = SLAFConverter(chunked=True, chunk_size=5000)
        >>> converter.convert("large_data.h5ad", "output.slaf")
        Converting large_data.h5ad to SLAF format...
        Optimizations: int_keys=True, chunked=True
        Processing in chunks of 5000 cells...
        Conversion complete! Saved to output.slaf

        >>> # Error handling for unsupported format
        >>> try:
        ...     converter.convert("unknown_file.txt", "output.slaf")
        ... except ValueError as e:
        ...     print(f"Error: {e}")
        Error: Cannot detect format for: unknown_file.txt
    """
    # Handle both single paths and lists of files
    if isinstance(input_path, list):
        # Direct list of files - use multi-file conversion
        input_files = input_path
        # Detect format from first file
        if input_format == "auto":
            from .utils import detect_format

            input_format = detect_format(input_files[0])
    else:
        # Single path - discover files
        input_files, detected_format = discover_input_files(input_path)
        # Use detected format if auto, otherwise use specified format
        if input_format == "auto":
            input_format = detected_format

    # Validate multi-file compatibility if multiple files (unless skipped)
    if not skip_validation:
        validate_input_files(input_files, input_format)

    # Handle multiple files vs single file
    if len(input_files) > 1:
        self._convert_multiple_files(input_files, output_path, input_format)
    else:
        # Single file - use existing logic
        single_file = input_files[0]
        if input_format == "h5ad":
            if not SCANPY_AVAILABLE:
                raise ImportError(
                    "Scanpy is required for h5ad conversion. "
                    "Install with: pip install scanpy"
                )
            self._convert_h5ad(single_file, output_path)
        elif input_format == "10x_mtx":
            self._convert_10x_mtx(single_file, output_path)
        elif input_format == "10x_h5":
            self._convert_10x_h5(single_file, output_path)
        elif input_format == "tiledb":
            self._convert_tiledb(single_file, output_path)
        else:
            raise ValueError(f"Unsupported format: {input_format}")

convert_anndata(adata, output_path: str)

Convert AnnData object to SLAF format with COO-style expression table

Source code in slaf/data/converter.py
def convert_anndata(self, adata, output_path: str):
    """Convert AnnData object to SLAF format with COO-style expression table"""
    if self.chunked:
        raise ValueError(
            "convert_anndata() not supported in chunked mode. "
            "Use convert() with file path instead."
        )

    logger.info("Converting AnnData object to SLAF format...")
    logger.info(f"Optimizations: int_keys={self.use_integer_keys}")
    logger.info(f"Loaded: {adata.n_obs} cells × {adata.n_vars} genes")

    # Validate optimized data types
    if not self._validate_optimized_dtypes_anndata(adata):
        self.use_optimized_dtypes = False

    # Convert the AnnData object
    self._convert_anndata(adata, output_path)

append(input_path: str, existing_slaf_path: str, input_format: str = 'auto')

Append new data to an existing SLAF dataset.

This method adds new data to an existing SLAF dataset by: 1. Validating compatibility with existing dataset 2. Reading existing dataset metadata 3. Appending new data with auto-incrementing IDs 4. Updating configuration with new source file info

Parameters:

Name Type Description Default
input_path str

Path to new input file or directory

required
existing_slaf_path str

Path to existing SLAF dataset

required
input_format str

Format of input data (auto-detected if not specified)

'auto'
Source code in slaf/data/converter.py
def append(
    self, input_path: str, existing_slaf_path: str, input_format: str = "auto"
):
    """
    Append new data to an existing SLAF dataset.

    This method adds new data to an existing SLAF dataset by:
    1. Validating compatibility with existing dataset
    2. Reading existing dataset metadata
    3. Appending new data with auto-incrementing IDs
    4. Updating configuration with new source file info

    Args:
        input_path: Path to new input file or directory
        existing_slaf_path: Path to existing SLAF dataset
        input_format: Format of input data (auto-detected if not specified)
    """
    logger.info(
        f"Appending data from {input_path} to existing SLAF dataset {existing_slaf_path}"
    )

    # Check if existing SLAF dataset exists
    if not self._path_exists(existing_slaf_path):
        raise FileNotFoundError(
            f"Existing SLAF dataset not found: {existing_slaf_path}"
        )

    # Discover input files
    input_files, detected_format = discover_input_files(input_path)
    if input_format == "auto":
        input_format = detected_format

    # Validate compatibility with existing dataset
    self._validate_append_compatibility(
        input_files, input_format, existing_slaf_path
    )

    # Read existing dataset metadata
    existing_cells_dataset = lance.dataset(
        os.path.join(existing_slaf_path, "cells.lance")
    )
    existing_cells_table = existing_cells_dataset.to_table()
    current_cell_count = len(existing_cells_table)

    # Track source file information
    source_file_info = []
    global_cell_offset = current_cell_count
    total_new_cells = 0

    # Process each new file and append to existing dataset
    for i, file_path in enumerate(input_files):
        logger.info(
            f"Processing file {i + 1}/{len(input_files)}: {os.path.basename(file_path)}"
        )

        try:
            # Use chunked reader to process file directly
            with create_chunked_reader(
                file_path,
                chunk_size=self.chunk_size,
                collection_name=self.tiledb_collection_name,
            ) as reader:
                logger.info(
                    f"Loaded: {reader.n_obs:,} cells × {reader.n_vars:,} genes"
                )

                # Get metadata from reader
                obs_df = reader.get_obs_metadata()
                var_df = reader.get_var_metadata()

                # Ensure cell_id and gene_id columns exist
                if "cell_id" not in obs_df.columns:
                    obs_df["cell_id"] = reader.obs_names
                if "gene_id" not in var_df.columns:
                    var_df["gene_id"] = reader.var_names

                # Add integer IDs with global offset
                obs_df["cell_integer_id"] = range(
                    global_cell_offset, global_cell_offset + len(obs_df)
                )

                # Add source file information to cell metadata
                source_file = os.path.basename(file_path)
                obs_df["source_file"] = source_file

                # Check if existing dataset has source_file column
                existing_cells_dataset = lance.dataset(
                    os.path.join(existing_slaf_path, "cells.lance")
                )
                existing_cells_table = existing_cells_dataset.to_table()
                existing_columns = set(existing_cells_table.column_names)

                # If existing dataset doesn't have source_file column, add it to existing data
                if "source_file" not in existing_columns:
                    logger.info("Adding source_file column to existing dataset...")
                    # Read existing cells data
                    existing_cells_df = existing_cells_table.to_pandas()
                    existing_cells_df["source_file"] = (
                        "original_data"  # Default source for existing data
                    )

                    # Recreate the cells dataset with source_file column
                    updated_cells_table = pa.table(existing_cells_df)
                    lance.write_dataset(
                        updated_cells_table,
                        os.path.join(existing_slaf_path, "cells.lance"),
                        mode="overwrite",
                        enable_v2_manifest_paths=self.enable_v2_manifest,
                        data_storage_version="2.1",
                    )
                    logger.info("✓ Added source_file column to existing dataset")

                # Precompute cell start indices
                obs_df["cell_start_index"] = self._compute_cell_start_indices(
                    reader, obs_df
                )

                # Convert metadata to Lance tables
                cell_metadata_table = self._create_metadata_table(
                    obs_df, "cell_id", integer_mapping=None
                )

                # Append to existing cells dataset
                cells_path = os.path.join(existing_slaf_path, "cells.lance")
                lance.write_dataset(
                    cell_metadata_table,
                    cells_path,
                    mode="append",
                    enable_v2_manifest_paths=self.enable_v2_manifest,
                    data_storage_version="2.1",
                )

                # Process expression data in chunks with checkpointing support
                # Use the same checkpointing infrastructure as convert method
                self._process_file_chunks_with_checkpoint(
                    reader, existing_slaf_path, i, 0, global_cell_offset
                )

                # Track source file information
                source_file_info.append(
                    {
                        "file_path": file_path,
                        "file_name": source_file,
                        "n_cells": len(obs_df),
                        "cell_offset": global_cell_offset,
                    }
                )

                # Update global cell offset
                global_cell_offset += len(obs_df)
                total_new_cells += len(obs_df)

                # Save checkpoint after each file (for append operations)
                if self.enable_checkpointing:
                    checkpoint_data = {
                        "status": "in_progress",
                        "last_completed_file": i,
                        "last_completed_chunk": -1,  # -1 indicates file is fully completed
                        "global_cell_offset": global_cell_offset,
                        "operation": "append",  # Mark this as an append operation
                        "timestamp": pd.Timestamp.now().isoformat(),
                    }
                    self._save_checkpoint(existing_slaf_path, checkpoint_data)

        except Exception as e:
            logger.error(f"Failed to process file {file_path}: {e}")
            # Save checkpoint with error status
            if self.enable_checkpointing:
                checkpoint_data = {
                    "status": "error",
                    "last_completed_file": i - 1,
                    "last_completed_chunk": -1,  # -1 indicates previous file was fully completed
                    "global_cell_offset": global_cell_offset,
                    "operation": "append",  # Mark this as an append operation
                    "error": str(e),
                    "timestamp": pd.Timestamp.now().isoformat(),
                }
                self._save_checkpoint(existing_slaf_path, checkpoint_data)
            # Continue with other files
            continue

    if not source_file_info:
        raise RuntimeError("No files were successfully processed")

    # Update configuration with new source file information
    self._update_config_with_append(
        existing_slaf_path, source_file_info, total_new_cells
    )

    # Clear checkpoint after successful completion
    self._clear_checkpoint(existing_slaf_path)

    logger.info(
        f"Append complete! Added {total_new_cells} cells from {len(source_file_info)} files"
    )
    logger.info(f"Total cells in dataset: {current_cell_count + total_new_cells}")