rustkmer 0.5.2

High-performance k-mer counting tool in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
# Python API Examples

This page provides comprehensive examples of using the RustKmer Python API for various bioinformatics applications. The examples range from basic usage to advanced workflows, demonstrating real-world use cases and best practices.

## Table of Contents

- [Basic Usage Examples]#basic-usage-examples
- [Database Operations]#database-operations
- [Query and Analysis Examples]#query-and-analysis-examples
- [Performance Optimization]#performance-optimization
- [Integration with Bioinformatics Tools]#integration-with-bioinformatics-tools
- [Machine Learning Applications]#machine-learning-applications
- [Visualization and Reporting]#visualization-and-reporting

---

## Basic Usage Examples

### Getting Started with RustKmer

```python
#!/usr/bin/env python3
"""
Basic RustKmer Python API usage example.
"""

from pyrustkmer import Database, KmerCounter, PyFuzzyQuery
from pathlib import Path

def basic_example():
    """Demonstrate basic RustKmer functionality."""

    # Example 1: Query an existing database
    print("=== Example 1: Query Database ===")
    db = PyDatabase("example.rkdb", LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)

    # Query a single k-mer
    kmer = "ATCGATCGATCGATCGATCGATCGATCGATCGATCG"
    result = db.query_exact(kmer)

    print(f"Query: {kmer}")
    print(f"Found: {result.found}")
    print(f"Count: {result.count}")
    print(f"Canonical: {result.canonical}")

    # Example 2: Get database statistics
    print("\n=== Example 2: Database Statistics ===")
    stats = db.get_stats()
    print(f"K-mer size: {stats.kmer_size}")
    print(f"Unique k-mers: {stats.unique_kmers:,}")
    print(f"Total counts: {stats.total_counts:,}")

    # Example 3: Context manager usage (recommended)
    print("\n=== Example 3: Context Manager ===")
    db = PyDatabase("example.rkdb", LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
        result = db.query_exact("GCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAG")
        print(f"Query result: {result.count}")
        # Database automatically closed

    print("Basic example completed!")

if __name__ == "__main__":
    basic_example()
```

### Creating and Querying Databases

```python
#!/usr/bin/env python3
"""
Create k-mer databases and perform queries.
"""

from pyrustkmer import KmerCounter, Database, PyFuzzyQuery
import tempfile
import os

def create_and_query_database():
    """Create a database from sequences and query it."""

    # Create sample FASTA file
    sequences = [
        "ATCGATCGATCGATCGATCGATCGATCGATCGATCG",
        "GCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAG",
        "ATCGATCGATCGATCGATCGATCGATCGATCGATCG"  # Duplicate
    ]

    fasta_content = ""
    for i, seq in enumerate(sequences):
        fasta_content += f">seq_{i+1}\n{seq}\n"

    # Write to temporary file
    with tempfile.NamedTemporaryFile(mode='w', suffix='.fasta', delete=False) as f:
        f.write(fasta_content)
        fasta_file = f.name

    try:
        print("=== Creating k-mer database ===")

        # Count k-mers
        counter = PyCounter(31, canonical=True)
        counter.add_from_fasta(fasta_file)

        # Get statistics
        total_kmers = counter.get_stats().total_kmers)
        unique_kmers = counter.get_unique_count()

        print(f"Total k-mers: {total_kmers:,}")
        print(f"Unique k-mers: {unique_kmers:,}")

        # Save database
        db_file = "example_database.rkdb"
        counter.save_database(db_file)
        print(f"Database saved to: {db_file}")

        # Query the database
        print("\n=== Querying database ===")
        db = PyDatabase(db_file, LoadMode.Preload)
            # Query exact match
            result1 = db.query_exact("ATCGATCGATCGATCGATCGATCGATCGATCGATCG")
            print(f"Exact query: {result1.count} occurrences")

            # Query non-existent k-mer
            result2 = db.query_exact("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
            print(f"Non-existent query: {result2.found}")

        print("Database creation and query completed!")

    finally:
        # Clean up temporary file
        os.unlink(fasta_file)

if __name__ == "__main__":
    create_and_query_database()
```

---

## Database Operations

### Database Merging and Comparison

```python
#!/usr/bin/env python3
"""
Merge multiple databases and compare their contents.
"""

from pyrustkmer import Database, KmerCounter, PyFuzzyQuery
import pandas as pd
from pathlib import Path
import tempfile

def merge_and_compare_databases():
    """Demonstrate database merging and comparison."""

    # Create sample databases
    print("=== Creating sample databases ===")

    # Database 1: AT-rich sequences
    seq1 = ["ATATATATATATATATATATATATATATATATAT", "ATCGATCGATCGATCGATCGATCGATCGATCGATCG"]

    # Database 2: GC-rich sequences
    seq2 = ["GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC", "GCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAG"]

    # Create temporary FASTA files
    temp_files = []
    sequences = [seq1, seq2]
    db_files = []

    for i, seqs in enumerate(sequences):
        with tempfile.NamedTemporaryFile(mode='w', suffix=f'_sample{i+1}.fasta', delete=False) as f:
            fasta_content = ""
            for j, seq in enumerate(seqs):
                fasta_content += f">sample{i+1}_seq{j+1}\n{seq}\n"
            f.write(fasta_content)
            temp_files.append(f.name)

        # Count k-mers and create database
        counter = PyCounter(21, canonical=True)
        counter.add_from_fasta(f.name)

        db_file = f"sample{i+1}_k21.rkdb"
        counter.save_database(db_file)
        db_files.append(db_file)

        print(f"Created {db_file} with {counter.get_unique_count():,} unique k-mers")

    try:
        # Compare databases
        print("\n=== Comparing databases ===")

        # Get common k-mers
        common_kmers = set()
        all_kmers = set()

        for i, db_file in enumerate(db_files):
            db_kmers = set()
            db = PyDatabase(db_file, LoadMode.Preload)
                for result in db.dump(limit=1000):  # Limit for demo
                    db_kmers.add(result.canonical)

            if i == 0:
                common_kmers = db_kmers
            else:
                common_kmers &= db_kmers
            all_kmers |= db_kmers

        print(f"Total unique k-mers across all databases: {len(all_kmers):,}")
        print(f"Common k-mers in all databases: {len(common_kmers):,}")

        # Create presence matrix
        print("\n=== Creating presence matrix ===")
        presence_data = []

        for i, db_file in enumerate(db_files):
            sample_name = f"sample_{i+1}"
            sample_data = {'Sample': sample_name}

            # Query a subset of common k-mers
            query_kmers = list(common_kmers)[:20]  # First 20 for demo

            db = PyDatabase(db_file, LoadMode.Preload)
                for kmer in query_kmers:
                    result = db.query_exact(kmer)
                    sample_data[kmer] = 1 if result.found else 0

            presence_data.append(sample_data)

        df = pd.DataFrame(presence_data)
        print("Presence matrix (first 10 k-mers):")
        print(df.iloc[:, :11])  # Sample + first 10 k-mers

        # Calculate similarity
        print("\n=== Database similarity ===")
        presence_matrix = df.iloc[:, 1:].values  # Exclude Sample column

        # Simple Jaccard similarity
        from sklearn.metrics import jaccard_score
        import numpy as np

        n_samples = len(db_files)
        similarity_matrix = np.zeros((n_samples, n_samples))

        for i in range(n_samples):
            for j in range(i, n_samples):
                if i == j:
                    similarity_matrix[i, j] = 1.0
                else:
                    intersection = np.sum(np.logical_and(presence_matrix[i], presence_matrix[j]))
                    union = np.sum(np.logical_or(presence_matrix[i], presence_matrix[j]))
                    similarity = intersection / union if union > 0 else 0
                    similarity_matrix[i, j] = similarity_matrix[j, i] = similarity

        print("Sample similarity matrix:")
        for i in range(n_samples):
            row = " ".join(f"{similarity_matrix[i, j]:.2f}" for j in range(n_samples))
            print(f"Sample {i+1}: {row}")

    finally:
        # Clean up
        for f in temp_files + db_files:
            if Path(f).exists():
                Path(f).unlink()

if __name__ == "__main__":
    merge_and_compare_databases()
```

### Database Backup and Migration

```python
#!/usr/bin/env python3
"""
Database backup, migration, and validation utilities.
"""

from pyrustkmer import Database, PyFuzzyQuery
import shutil
import hashlib
import json
from pathlib import Path

def backup_database(db_path, backup_dir):
    """Create a backup of a database with validation."""

    db_path = Path(db_path)
    backup_dir = Path(backup_dir)
    backup_dir.mkdir(parents=True, exist_ok=True)

    # Generate backup filename with timestamp
    import datetime
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    backup_file = backup_dir / f"{db_path.stem}_{timestamp}.rkdb"

    print(f"Backing up {db_path} to {backup_file}")

    # Copy database file
    shutil.copy2(db_path, backup_file)

    # Validate both databases
    print("Validating original database...")
    original_valid = validate_database(db_path)

    print("Validating backup database...")
    backup_valid = validate_database(backup_file)

    # Calculate checksums
    original_checksum = calculate_file_checksum(db_path)
    backup_checksum = calculate_file_checksum(backup_file)

    # Create backup metadata
    metadata = {
        'original_file': str(db_path),
        'backup_file': str(backup_file),
        'timestamp': timestamp,
        'original_checksum': original_checksum,
        'backup_checksum': backup_checksum,
        'original_valid': original_valid,
        'backup_valid': backup_valid,
        'file_size': backup_file.stat().st_size
    }

    metadata_file = backup_dir / f"{db_path.stem}_{timestamp}_metadata.json"
    with open(metadata_file, 'w') as f:
        json.dump(metadata, f, indent=2)

    print(f"Backup completed successfully!")
    print(f"Original checksum: {original_checksum}")
    print(f"Backup checksum: {backup_checksum}")
    print(f"Metadata saved to: {metadata_file}")

    return backup_file, metadata_file

def validate_database(db_path):
    """Validate database integrity and functionality."""

    try:
        db = PyDatabase(db_path, LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
            # Test basic functionality
            stats = db.get_stats()

            # Test query with a simple k-mer
            test_kmer = "A" * stats.kmer_size if stats.kmer_size else "ATCGATCGATCGATCGATCGATCGATCGATCGATCG"
            result = db.query_exact(test_kmer)

            # Test dump functionality
            dump_results = list(db.dump(limit=10))

        return True

    except Exception as e:
        print(f"Database validation failed: {e}")
        return False

def calculate_file_checksum(file_path):
    """Calculate SHA-256 checksum of a file."""

    sha256_hash = hashlib.sha256()
    with open(file_path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            sha256_hash.update(chunk)
    return sha256_hash.hexdigest()

def migrate_database_format(old_db_path, new_db_path):
    """Migrate database to new format (conceptual example)."""

    print(f"Migrating database from {old_db_path} to {new_db_path}")

    old_db = PyDatabase(old_db_path, LoadMode.Preload)
        stats = old_db.get_stats()
        print(f"Original database: {stats.unique_kmers:,} unique k-mers")

        # Create new database (in practice, this would use RustKmer's migration tools)
        # For this example, we'll just copy and validate
        shutil.copy2(old_db_path, new_db_path)

        # Validate new database
        new_db = PyDatabase(new_db_path, LoadMode.Preload)
            new_stats = new_db.get_stats()
            print(f"Migrated database: {new_stats.unique_kmers:,} unique k-mers")

            if stats.unique_kmers == new_stats.unique_kmers:
                print("Migration successful: k-mer counts match")
            else:
                print("Warning: k-mer counts differ between databases")

    return new_db_path

# Example usage
if __name__ == "__main__":
    # Example backup
    backup_file, metadata_file = backup_database(
        "example.rkdb",
        "database_backups"
    )

    # Example migration
    migrate_database_format(
        "example.rkdb",
        "example_migrated.rkdb"
    )
```

---

## Query and Analysis Examples

### Fuzzy Query Analysis

```python
#!/usr/bin/env python3
"""
Advanced fuzzy query analysis and interpretation.
"""

from pyrustkmer import Database, PyFuzzyQuery
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from collections import defaultdict
import numpy as np

def analyze_fuzzy_queries():
    """Comprehensive fuzzy query analysis."""

    db = PyDatabase("example.rkdb", LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)

    # Test k-mers with different characteristics
    test_kmers = [
        "ATCGATCGATCGATCGATCGATCGATCGATCGATCG",  # Balanced
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",  # Low complexity
        "GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC",  # High GC
        "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"   # Homopolymer
    ]

    print("=== Fuzzy Query Analysis ===")

    # Analyze each k-mer with different mutation tolerances
    all_results = []

    for kmer in test_kmers:
        print(f"\nAnalyzing: {kmer[:20]}...")

        for mutations in range(4):  # 0, 1, 2, 3 mutations
            result = fuzzy.query_fuzzy(kmer, mutations=mutations)

            analysis = {
                'kmer': kmer,
                'mutations_allowed': mutations,
                'total_matches': result.total_matches,
                'exact_matches': result.exact_matches,
                'fuzzy_matches': result.fuzzy_matches,
                'unique_variants': len(result.get_fuzzy_matches()),
                'max_count': max([m.count for m in result.matches]) if result.matches else 0,
                'mean_count': np.mean([m.count for m in result.matches]) if result.matches else 0
            }

            all_results.append(analysis)

            print(f"  Mutations {mutations}: {result.total_matches} matches "
                  f"({result.exact_matches} exact, {result.fuzzy_matches} fuzzy)")

    # Create analysis DataFrame
    df = pd.DataFrame(all_results)

    print("\n=== Summary Statistics ===")
    print(df.groupby('mutations_allowed').agg({
        'total_matches': ['mean', 'std'],
        'unique_variants': ['mean', 'std'],
        'fuzzy_matches': ['mean', 'std']
    }).round(2))

    # Analyze mutation patterns
    print("\n=== Mutation Pattern Analysis ===")

    for kmer in test_kmers[:2]:  # Analyze first 2 k-mers in detail
        result = fuzzy.query_fuzzy(kmer, mutations=2)

        # Count mutations by position
        position_mutations = defaultdict(int)
        total_variants = 0

        for match in result.get_fuzzy_matches():
            total_variants += match.count
            for mutation in match.mutations:
                if '>' in mutation:
                    position = int(mutation.split('>')[0][1:]) - 1  # 0-based position
                    position_mutations[position] += match.count

        if position_mutations:
            print(f"\nK-mer: {kmer[:20]}...")
            print("Mutation distribution by position:")
            for pos in sorted(position_mutations.keys()):
                frequency = position_mutations[pos] / total_variants
                print(f"  Position {pos+1}: {frequency:.2%}")

    # Create visualization
    create_fuzzy_query_visualization(df)

    return df

def create_fuzzy_query_visualization(df):
    """Create visualizations for fuzzy query results."""

    fig, axes = plt.subplots(2, 2, figsize=(15, 12))
    fig.suptitle('Fuzzy Query Analysis Results', fontsize=16)

    # Plot 1: Total matches vs mutations allowed
    ax1 = axes[0, 0]
    for kmer in df['kmer'].unique():
        kmer_data = df[df['kmer'] == kmer]
        ax1.plot(kmer_data['mutations_allowed'], kmer_data['total_matches'],
                marker='o', label=kmer[:15] + "...", linewidth=2, markersize=6)

    ax1.set_xlabel('Mutations Allowed')
    ax1.set_ylabel('Total Matches')
    ax1.set_title('Total Matches vs Mutation Tolerance')
    ax1.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    ax1.grid(True, alpha=0.3)

    # Plot 2: Exact vs fuzzy matches
    ax2 = axes[0, 1]
    mutations_groups = df.groupby('mutations_allowed')

    exact_matches = mutations_groups['exact_matches'].mean()
    fuzzy_matches = mutations_groups['fuzzy_matches'].mean()

    x = np.arange(len(exact_matches.index))
    width = 0.35

    ax2.bar(x - width/2, exact_matches.values, width, label='Exact Matches', alpha=0.7)
    ax2.bar(x + width/2, fuzzy_matches.values, width, label='Fuzzy Matches', alpha=0.7)

    ax2.set_xlabel('Mutations Allowed')
    ax2.set_ylabel('Average Matches')
    ax2.set_title('Exact vs Fuzzy Matches')
    ax2.set_xticks(x)
    ax2.set_xticklabels(exact_matches.index)
    ax2.legend()
    ax2.grid(True, alpha=0.3)

    # Plot 3: Unique variants
    ax3 = axes[1, 0]
    for kmer in df['kmer'].unique():
        kmer_data = df[df['kmer'] == kmer]
        ax3.plot(kmer_data['mutations_allowed'], kmer_data['unique_variants'],
                marker='s', label=kmer[:15] + "...", linewidth=2, markersize=6)

    ax3.set_xlabel('Mutations Allowed')
    ax3.set_ylabel('Unique Variants')
    ax3.set_title('Unique Variants vs Mutation Tolerance')
    ax3.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    ax3.grid(True, alpha=0.3)

    # Plot 4: Match abundance distribution
    ax4 = axes[1, 1]

    # Collect all match counts for visualization
    all_counts = []
    for mutations in df['mutations_allowed'].unique():
        counts = df[df['mutations_allowed'] == mutations]['max_count']
        all_counts.extend([(count, mutations) for count in counts])

    if all_counts:
        counts_df = pd.DataFrame(all_counts, columns=['count', 'mutations'])

        # Create boxplot
        sns.boxplot(data=counts_df, x='mutations', y='count', ax=ax4)
        ax4.set_xlabel('Mutations Allowed')
        ax4.set_ylabel('Max Match Count')
        ax4.set_title('Match Abundance Distribution')
        ax4.set_yscale('log')

    plt.tight_layout()
    plt.show()

def find_optimal_mutation_tolerance(db, test_kmers, max_mutations=4):
    """Find optimal mutation tolerance for different use cases."""

    print("=== Finding Optimal Mutation Tolerance ===")

    results = []

    for kmer in test_kmers:
        kmer_results = []

        for mutations in range(max_mutations + 1):
            result = fuzzy.query_fuzzy(kmer, mutations=mutations)

            # Calculate metrics
            if result.total_matches > 0:
                diversity = len(result.matches) / result.total_matches
                abundance = np.mean([m.count for m in result.matches])
            else:
                diversity = 0
                abundance = 0

            kmer_results.append({
                'mutations': mutations,
                'total_matches': result.total_matches,
                'diversity': diversity,
                'abundance': abundance,
                'efficiency': result.total_matches / (4 ** mutations) if mutations > 0 else result.total_matches
            })

        # Find optimal based on different criteria
        results.append({
            'kmer': kmer,
            'by_total_matches': max(kmer_results, key=lambda x: x['total_matches'])['mutations'],
            'by_diversity': max(kmer_results, key=lambda x: x['diversity'])['mutations'],
            'by_efficiency': max(kmer_results, key=lambda x: x['efficiency'])['mutations']
        })

    # Print recommendations
    for result in results:
        print(f"\nK-mer: {result['kmer'][:20]}...")
        print(f"  Optimal for total matches: {result['by_total_matches']} mutations")
        print(f"  Optimal for diversity: {result['by_diversity']} mutations")
        print(f"  Optimal for efficiency: {result['by_efficiency']} mutations")

    return results

if __name__ == "__main__":
    # Run analysis
    results_df = analyze_fuzzy_queries()

    # Find optimal mutation tolerances
    test_kmers = [
        "ATCGATCGATCGATCGATCGATCGATCGATCGATCG",
        "GCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAG"
    ]

    db = PyDatabase("example.rkdb", LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
    optimal_results = find_optimal_mutation_tolerance(db, test_kmers)
```

### Batch Query Processing

```python
#!/usr/bin/env python3
"""
Efficient batch query processing with progress tracking and error handling.
"""

from pyrustkmer import Database, PyFuzzyQuery
from concurrent.futures import ThreadPoolExecutor, as_completed
import pandas as pd
import time
import logging
from typing import List, Dict, Tuple

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class BatchQueryProcessor:
    """Efficient batch query processor with progress tracking."""

    def __init__(self, db_path: str, max_workers: int = 4):
        self.db_path = db_path
        self.max_workers = max_workers
        self.results = []

    def process_batch(self, queries: List[str], mutations: int = 1) -> Dict:
        """Process a batch of queries with progress tracking."""

        logger.info(f"Processing {len(queries)} queries with {mutations} mutations allowed")
        start_time = time.time()

        # Process queries in parallel
        db = PyDatabase(self.db_path, LoadMode.Preload)
            with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
                # Submit all queries
                future_to_query = {
                    executor.submit(self._single_query, db, query, mutations): query
                    for query in queries
                }

                # Collect results with progress tracking
                completed = 0
                for future in as_completed(future_to_query):
                    query = future_to_query[future]
                    completed += 1

                    try:
                        result = future.result()
                        self.results.append(result)

                        if completed % 100 == 0 or completed == len(queries):
                            progress = completed / len(queries) * 100
                            logger.info(f"Progress: {progress:.1f}% ({completed}/{len(queries)})")

                    except Exception as e:
                        logger.error(f"Query failed for {query[:20]}...: {e}")
                        self.results.append({
                            'query': query,
                            'error': str(e),
                            'success': False
                        })

        processing_time = time.time() - start_time
        successful = sum(1 for r in self.results if r.get('success', True))

        logger.info(f"Batch processing completed in {processing_time:.2f} seconds")
        logger.info(f"Successful queries: {successful}/{len(queries)} ({successful/len(queries):.1%})")

        return {
            'total_queries': len(queries),
            'successful': successful,
            'failed': len(queries) - successful,
            'processing_time': processing_time,
            'queries_per_second': len(queries) / processing_time,
            'results': self.results
        }

    def _single_query(self, db: Database, query: str, mutations: int) -> Dict:
        """Process a single fuzzy query."""

        try:
            result = fuzzy.query_fuzzy(query, mutations=mutations)

            return {
                'query': query,
                'success': True,
                'total_matches': result.total_matches,
                'exact_matches': result.exact_matches,
                'fuzzy_matches': result.fuzzy_matches,
                'top_match': result.get_top_matches(1)[0].kmer if result.matches else None,
                'top_match_count': result.get_top_matches(1)[0].count if result.matches else 0
            }

        except Exception as e:
            return {
                'query': query,
                'success': False,
                'error': str(e)
            }

    def results_to_dataframe(self) -> pd.DataFrame:
        """Convert results to pandas DataFrame for analysis."""

        successful_results = [r for r in self.results if r.get('success', True)]

        if not successful_results:
            return pd.DataFrame()

        return pd.DataFrame(successful_results)

    def generate_summary_report(self) -> Dict:
        """Generate comprehensive summary of batch processing results."""

        df = self.results_to_dataframe()

        if df.empty:
            return {'error': 'No successful results to analyze'}

        summary = {
            'total_queries': len(self.results),
            'successful_queries': len(df),
            'success_rate': len(df) / len(self.results),

            # Match statistics
            'mean_total_matches': df['total_matches'].mean(),
            'median_total_matches': df['total_matches'].median(),
            'max_total_matches': df['total_matches'].max(),

            # Exact vs fuzzy matches
            'total_exact_matches': df['exact_matches'].sum(),
            'total_fuzzy_matches': df['fuzzy_matches'].sum(),
            'queries_with_exact_matches': (df['exact_matches'] > 0).sum(),
            'queries_with_fuzzy_matches': (df['fuzzy_matches'] > 0).sum(),

            # Top match statistics
            'mean_top_match_count': df['top_match_count'].mean(),
            'median_top_match_count': df['top_match_count'].median()
        }

        return summary

def generate_test_queries(num_queries: int = 1000, kmer_length: int = 31) -> List[str]:
    """Generate test k-mers for batch processing."""

    import random

    bases = ['A', 'T', 'C', 'G']
    queries = []

    # Generate random k-mers
    for _ in range(num_queries):
        kmer = ''.join(random.choices(bases, k=kmer_length))
        queries.append(kmer)

    # Add some known patterns
    patterns = [
        "A" * kmer_length,
        "T" * kmer_length,
        "C" * kmer_length,
        "G" * kmer_length,
        "ATCG" * (kmer_length // 4),
        "GCTA" * (kmer_length // 4)
    ]

    queries.extend(patterns)

    return queries

def analyze_batch_results(batch_results: Dict, output_file: str = None):
    """Analyze batch processing results and generate visualizations."""

    if 'results' not in batch_results:
        print("No results to analyze")
        return

    # Create DataFrame
    df = pd.DataFrame([r for r in batch_results['results'] if r.get('success', True)])

    if df.empty:
        print("No successful results to analyze")
        return

    print("=== Batch Query Analysis ===")
    print(f"Total queries: {batch_results['total_queries']}")
    print(f"Successful: {batch_results['successful']}")
    print(f"Failed: {batch_results['failed']}")
    print(f"Processing time: {batch_results['processing_time']:.2f} seconds")
    print(f"Queries/second: {batch_results['queries_per_second']:.1f}")

    print("\n=== Match Statistics ===")
    print(f"Mean total matches: {df['total_matches'].mean():.2f}")
    print(f"Median total matches: {df['total_matches'].median():.2f}")
    print(f"Max total matches: {df['total_matches'].max()}")
    print(f"Queries with exact matches: {(df['exact_matches'] > 0).sum()}")
    print(f"Queries with fuzzy matches: {(df['fuzzy_matches'] > 0).sum()}")

    # Create visualizations
    try:
        import matplotlib.pyplot as plt
        import seaborn as sns

        fig, axes = plt.subplots(2, 2, figsize=(15, 12))
        fig.suptitle('Batch Query Processing Results', fontsize=16)

        # Total matches distribution
        axes[0, 0].hist(df['total_matches'], bins=50, alpha=0.7, edgecolor='black')
        axes[0, 0].set_xlabel('Total Matches')
        axes[0, 0].set_ylabel('Frequency')
        axes[0, 0].set_title('Total Matches Distribution')
        axes[0, 0].set_yscale('log')

        # Exact vs fuzzy matches
        exact_mask = df['exact_matches'] > 0
        fuzzy_mask = df['fuzzy_matches'] > 0

        labels = ['Only Exact', 'Only Fuzzy', 'Both', 'Neither']
        sizes = [
            (exact_mask & ~fuzzy_mask).sum(),
            (~exact_mask & fuzzy_mask).sum(),
            (exact_mask & fuzzy_mask).sum(),
            (~exact_mask & ~fuzzy_mask).sum()
        ]

        axes[0, 1].pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
        axes[0, 1].set_title('Match Type Distribution')

        # Top match counts
        axes[1, 0].hist(df['top_match_count'], bins=50, alpha=0.7, edgecolor='black')
        axes[1, 0].set_xlabel('Top Match Count')
        axes[1, 0].set_ylabel('Frequency')
        axes[1, 0].set_title('Top Match Count Distribution')
        axes[1, 0].set_yscale('log')

        # Performance over time (simulated)
        axes[1, 1].plot(range(len(df)), df['total_matches'], alpha=0.6)
        axes[1, 1].set_xlabel('Query Index')
        axes[1, 1].set_ylabel('Total Matches')
        axes[1, 1].set_title('Matches per Query')

        plt.tight_layout()

        if output_file:
            plt.savefig(output_file, dpi=300, bbox_inches='tight')
            print(f"\nVisualization saved to: {output_file}")
        else:
            plt.show()

    except ImportError:
        print("\nMatplotlib not available for visualization")

    # Save detailed results
    if output_file:
        detailed_file = output_file.replace('.png', '_details.csv')
        df.to_csv(detailed_file, index=False)
        print(f"Detailed results saved to: {detailed_file}")

# Example usage
if __name__ == "__main__":
    # Generate test queries
    queries = generate_test_queries(num_queries=500, kmer_length=31)

    # Create batch processor
    processor = BatchQueryProcessor("example.rkdb", max_workers=8)

    # Process batch
    batch_results = processor.process_batch(queries, mutations=2)

    # Analyze results
    analyze_batch_results(batch_results, "batch_query_analysis.png")
```

---

## Performance Optimization

### Memory-Efficient Database Processing

```python
#!/usr/bin/env python3
"""
Memory-efficient techniques for processing large databases.
"""

from pyrustkmer import Database, PyFuzzyQuery
import psutil
import gc
from pathlib import Path

class MemoryEfficientProcessor:
    """Process databases with memory monitoring and optimization."""

    def __init__(self, memory_limit_gb: float = 4.0):
        self.memory_limit_gb = memory_limit_gb
        self.memory_check_interval = 10000

    def get_memory_usage(self) -> float:
        """Get current memory usage in GB."""
        return psutil.Process().memory_info().rss / (1024**3)

    def process_large_database_safely(self, db_path: str,
                                     process_function=None,
                                     max_variants: int = None):
        """Process large database with memory safety checks."""

        print(f"Processing large database: {db_path}")
        print(f"Memory limit: {self.memory_limit_gb} GB")

        initial_memory = self.get_memory_usage()
        print(f"Initial memory usage: {initial_memory:.2f} GB")

        processed_items = 0
        results = []

        try:
            db = PyDatabase(db_path, LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
                if max_variants:
                    # Use limited dump
                    iterator = db.dump(limit=max_variants)
                else:
                    iterator = db.dump(canonical_only=True)

                for result in iterator:
                    processed_items += 1

                    # Process result if function provided
                    if process_function:
                        result_data = process_function(result)
                        if result_data:
                            results.append(result_data)

                    # Periodic memory checks
                    if processed_items % self.memory_check_interval == 0:
                        current_memory = self.get_memory_usage()
                        memory_increase = current_memory - initial_memory

                        print(f"Processed {processed_items:,} items, "
                              f"Memory: {current_memory:.2f} GB (+{memory_increase:.2f} GB)")

                        # Safety check
                        if current_memory > self.memory_limit_gb:
                            print(f"WARNING: Memory limit exceeded ({current_memory:.2f} GB)")
                            print("Triggering garbage collection...")
                            gc.collect()

                            # Check again after garbage collection
                            current_memory = self.get_memory_usage()
                            if current_memory > self.memory_limit_gb:
                                print("CRITICAL: Memory still exceeds limit, stopping processing")
                                break

                        # Periodic garbage collection
                        elif processed_items % (self.memory_check_interval * 5) == 0:
                            gc.collect()

            final_memory = self.get_memory_usage()
            print(f"\nProcessing completed!")
            print(f"Total items processed: {processed_items:,}")
            print(f"Final memory usage: {final_memory:.2f} GB")
            print(f"Memory increase: {final_memory - initial_memory:.2f} GB")

            return results

        except Exception as e:
            print(f"Error during processing: {e}")
            raise

    def stream_database_to_file(self, db_path: str, output_file: str,
                               max_items: int = None,
                               format_type: str = 'tsv'):
        """Stream database contents to file with minimal memory usage."""

        print(f"Streaming database to {output_file}")

        processed_items = 0

        db = PyDatabase(db_path, LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
            with open(output_file, 'w') as out_file:

                # Write header based on format
                if format_type == 'tsv':
                    out_file.write("kmer\tcount\tcanonical\n")
                elif format_type == 'csv':
                    out_file.write("kmer,count,canonical\n")

                # Stream results
                iterator = db.dump(limit=max_items)

                for result in iterator:
                    processed_items += 1

                    if format_type == 'tsv':
                        out_file.write(f"{result.kmer}\t{result.count}\t{result.canonical}\n")
                    elif format_type == 'csv':
                        out_file.write(f"{result.kmer},{result.count},{result.canonical}\n")

                    # Progress update
                    if processed_items % 100000 == 0:
                        memory_gb = self.get_memory_usage()
                        print(f"Processed {processed_items:,} items, Memory: {memory_gb:.2f} GB")

        print(f"Streaming completed: {processed_items:,} items written to {output_file}")
        return processed_items

def memory_optimized_analysis():
    """Example of memory-optimized database analysis."""

    # Create processor with 2GB memory limit
    processor = MemoryEfficientProcessor(memory_limit_gb=2.0)

    # Example processing function
    def analyze_abundant_kmers(result, min_count=100):
        """Process result to find abundant k-mers."""
        if result.count >= min_count:
            return {
                'kmer': result.kmer,
                'count': result.count,
                'canonical': result.canonical
            }
        return None

    # Process database safely
    try:
        abundant_kmers = processor.process_large_database_safely(
            "large_genome.rkdb",
            process_function=analyze_abundant_kmers,
            max_variants=1000000  # Limit to 1M items
        )

        print(f"\nFound {len(abundant_kmers)} abundant k-mers")

        # Sort by count and show top 10
        abundant_kmers.sort(key=lambda x: x['count'], reverse=True)
        for i, kmer in enumerate(abundant_kmers[:10], 1):
            print(f"{i:2d}. {kmer['kmer']}: {kmer['count']:,}")

    except Exception as e:
        print(f"Analysis failed: {e}")

    # Example streaming to file
    try:
        items_streamed = processor.stream_database_to_file(
            "large_genome.rkdb",
            "database_contents.tsv",
            max_items=500000,
            format_type='tsv'
        )
        print(f"Streamed {items_streamed:,} items to file")

    except Exception as e:
        print(f"Streaming failed: {e}")

# Chunked processing for very large databases
def process_in_chunks(db_path: str, chunk_size: int = 100000,
                      max_total_items: int = None):
    """Process database in fixed-size chunks."""

    print(f"Processing database in chunks of {chunk_size:,} items")

    chunk_results = []
    total_processed = 0

    db = PyDatabase(db_path, LoadMode.Preload)
fuzzy = PyFuzzyQuery(db)
        chunk_data = []

        for result in db.dump(canonical_only=True):
            chunk_data.append({
                'kmer': result.kmer,
                'count': result.count
            })

            if len(chunk_data) >= chunk_size:
                # Process chunk
                chunk_result = process_chunk(chunk_data, len(chunk_results) + 1)
                chunk_results.append(chunk_result)

                total_processed += len(chunk_data)
                print(f"Processed chunk {len(chunk_results)}: {len(chunk_data):,} items "
                      f"(Total: {total_processed:,})")

                # Clear chunk data
                chunk_data.clear()
                gc.collect()

                # Check total limit
                if max_total_items and total_processed >= max_total_items:
                    break

        # Process remaining items
        if chunk_data:
            chunk_result = process_chunk(chunk_data, len(chunk_results) + 1)
            chunk_results.append(chunk_result)
            total_processed += len(chunk_data)
            print(f"Processed final chunk: {len(chunk_data):,} items")

    print(f"Chunked processing completed: {total_processed:,} total items in {len(chunk_results)} chunks")
    return chunk_results

def process_chunk(chunk_data: list, chunk_number: int):
    """Process a single chunk of data."""

    # Example processing: find top 10 most abundant in chunk
    sorted_chunk = sorted(chunk_data, key=lambda x: x['count'], reverse=True)
    top_10 = sorted_chunk[:10]

    return {
        'chunk_number': chunk_number,
        'items_processed': len(chunk_data),
        'top_abundant': top_10,
        'mean_count': sum(item['count'] for item in chunk_data) / len(chunk_data),
        'max_count': max(item['count'] for item in chunk_data)
    }

# Example usage
if __name__ == "__main__":
    memory_optimized_analysis()

    # Example chunked processing
    # chunk_results = process_in_chunks("very_large_genome.rkdb", chunk_size=50000)
```

---

## Integration with Bioinformatics Tools

### BioPython Integration

```python
#!/usr/bin/env python3
"""
Integration with BioPython for comprehensive sequence analysis.
"""

from pyrustkmer import Database, KmerCounter, PyFuzzyQuery
from Bio import SeqIO, Seq, SeqRecord
from Bio.SeqUtils import gc_fraction
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
from collections import defaultdict
import numpy as np

class KmerBioAnalyzer:
    """Integrate RustKmer with BioPython for sequence analysis."""

    def __init__(self, db_path: str = None):
        self.db_path = db_path
        self.db = None

    def load_database(self, db_path: str):
        """Load k-mer database."""
        self.db_path = db_path
        self.db = PyDatabase(db_path)
fuzzy = PyFuzzyQuery(db)
        print(f"Loaded database: {db_path}")

    def analyze_fasta_file(self, fasta_file: str, k: int = 31,
                           create_database: bool = False):
        """Analyze FASTA file and optionally create database."""

        print(f"Analyzing FASTA file: {fasta_file}")

        # Parse sequences
        sequences = []
        total_length = 0

        for record in SeqIO.parse(fasta_file, "fasta"):
            sequences.append(record)
            total_length += len(record.seq)

        print(f"Found {len(sequences)} sequences, total length: {total_length:,} bp")

        # Extract basic statistics
        gc_contents = [gc_fraction(seq.seq) for seq in sequences]
        lengths = [len(seq.seq) for seq in sequences]

        print(f"GC content: {np.mean(gc_contents):.2%} ± {np.std(gc_contents):.2%}")
        print(f"Sequence lengths: {np.mean(lengths):.1f} ± {np.std(lengths):.1f}")

        # Create k-mer database if requested
        if create_database:
            print("Creating k-mer database...")

            db_file = fasta_file.replace('.fasta', '.fasta.rkdb').replace('.fa', '.fa.rkdb')
            counter = PyCounter(k, canonical=True)
            counter.add_from_fasta(fasta_file)
            counter.save_database(db_file)

            stats = counter.get_stats().total_kmers), counter.get_unique_count()
            print(f"Database created: {db_file}")
            print(f"Total k-mers: {stats[0]:,}, Unique: {stats[1]:,}")

            return {
                'sequences': sequences,
                'database_file': db_file,
                'total_kmers': stats[0],
                'unique_kmers': stats[1]
            }

        return {'sequences': sequences}

    def query_sequence_kmers(self, sequence: Seq.Seq, k: int = 31,
                           mutations: int = 0):
        """Query all k-mers from a sequence."""

        if not self.db:
            raise ValueError("No database loaded")

        seq_str = str(sequence).upper()
        kmer_results = []

        # Extract k-mers
        for i in range(len(seq_str) - k + 1):
            kmer = seq_str[i:i+k]

            # Skip k-mers with ambiguous bases
            if 'N' in kmer:
                continue

            if mutations == 0:
                # Exact query
                result = self.db.query_exact(kmer)
                kmer_results.append({
                    'position': i,
                    'kmer': kmer,
                    'count': result.count,
                    'found': result.found,
                    'canonical': result.canonical
                })
            else:
                # Fuzzy query
                result = self.fuzzy.query_fuzzy(kmer, mutations=mutations)
                kmer_results.append({
                    'position': i,
                    'kmer': kmer,
                    'total_matches': result.total_matches,
                    'exact_matches': result.exact_matches,
                    'fuzzy_matches': result.fuzzy_matches
                })

        return kmer_results

    def find_kmer_density_regions(self, sequence: Seq.Seq, k: int = 31,
                                  window_size: int = 1000, step: int = 100):
        """Find regions with high/low k-mer density."""

        if not self.db:
            raise ValueError("No database loaded")

        seq_str = str(sequence).upper()
        density_data = []

        for start in range(0, len(seq_str) - k + 1, step):
            end = min(start + window_size, len(seq_str))
            window_seq = seq_str[start:end]

            # Count k-mers in window
            kmer_count = 0
            found_kmers = 0

            for i in range(len(window_seq) - k + 1):
                kmer = window_seq[i:i+k]

                if 'N' in kmer:
                    continue

                kmer_count += 1
                result = self.db.query_exact(kmer)

                if result.found:
                    found_kmers += 1

            if kmer_count > 0:
                density = found_kmers / kmer_count
            else:
                density = 0

            density_data.append({
                'start': start,
                'end': end,
                'window_size': window_size,
                'kmer_count': kmer_count,
                'found_kmers': found_kmers,
                'density': density
            })

        return density_data

    def compare_sequence_databases(self, sequences: list, labels: list = None):
        """Compare k-mer content across multiple sequences."""

        if not self.db:
            raise ValueError("No database loaded")

        if labels is None:
            labels = [f"Seq_{i+1}" for i in range(len(sequences))]

        comparison_data = []

        for i, (sequence, label) in enumerate(zip(sequences, labels)):
            print(f"Analyzing {label}...")

            # Query all k-mers in sequence
            kmer_results = self.query_sequence_kmers(sequence)

            # Calculate statistics
            total_kmers = len(kmer_results)
            found_kmers = sum(1 for r in kmer_results if r.get('found', False))
            total_counts = sum(r.get('count', 0) for r in kmer_results)

            comparison_data.append({
                'label': label,
                'total_kmers': total_kmers,
                'found_kmers': found_kmers,
                'not_found_kmers': total_kmers - found_kmers,
                'coverage': found_kmers / total_kmers if total_kmers > 0 else 0,
                'total_counts': total_counts,
                'mean_count': total_counts / found_kmers if found_kmers > 0 else 0
            })

        df = pd.DataFrame(comparison_data)
        return df

    def create_kmer_abundance_profile(self, k: int = 31, top_n: int = 1000):
        """Create k-mer abundance profile from database."""

        if not self.db:
            raise ValueError("No database loaded")

        print("Creating k-mer abundance profile...")

        # Get top k-mers from database
        top_kmers = []

        for result in self.db.dump(limit=top_n, canonical_only=True):
            top_kmers.append({
                'kmer': result.kmer,
                'count': result.count,
                'gc_content': (result.kmer.count('G') + result.kmer.count('C')) / len(result.kmer),
                'log_count': np.log10(result.count + 1)
            })

        df = pd.DataFrame(top_kmers)

        print(f"Profile created with {len(df)} k-mers")
        print(f"Count range: {df['count'].min():,} - {df['count'].max():,}")
        print(f"Mean GC content: {df['gc_content'].mean():.2%}")

        return df

    def visualize_kmer_profiles(self, abundance_df, sequence_dfs: list = None):
        """Create visualizations of k-mer profiles."""

        fig, axes = plt.subplots(2, 2, figsize=(15, 12))
        fig.suptitle('K-mer Analysis Visualization', fontsize=16)

        # Plot 1: K-mer abundance distribution
        axes[0, 0].hist(abundance_df['count'], bins=50, alpha=0.7, edgecolor='black')
        axes[0, 0].set_xlabel('K-mer Count')
        axes[0, 0].set_ylabel('Frequency')
        axes[0, 0].set_title('K-mer Abundance Distribution')
        axes[0, 0].set_yscale('log')
        axes[0, 0].grid(True, alpha=0.3)

        # Plot 2: GC content distribution
        axes[0, 1].hist(abundance_df['gc_content'], bins=20, alpha=0.7, edgecolor='black')
        axes[0, 1].set_xlabel('GC Content')
        axes[0, 1].set_ylabel('Frequency')
        axes[0, 1].set_title('K-mer GC Content Distribution')
        axes[0, 1].grid(True, alpha=0.3)

        # Plot 3: Count vs GC content scatter
        axes[1, 0].scatter(abundance_df['gc_content'], abundance_df['count'],
                        alpha=0.6, s=10)
        axes[1, 0].set_xlabel('GC Content')
        axes[1, 0].set_ylabel('Count')
        axes[1, 0].set_title('Count vs GC Content')
        axes[1, 0].set_xscale('linear')
        axes[1, 0].set_yscale('log')
        axes[1, 0].grid(True, alpha=0.3)

        # Plot 4: Sequence comparison (if provided)
        if sequence_dfs:
            combined_df = pd.concat(sequence_dfs, ignore_index=True)

            x_pos = np.arange(len(combined_df))
            width = 0.35

            axes[1, 1].bar(x_pos - width/2, combined_df['coverage'],
                           width, label='Coverage', alpha=0.7)
            axes[1, 1].bar(x_pos + width/2, combined_df['mean_count'],
                           width, label='Mean Count', alpha=0.7)

            axes[1, 1].set_xlabel('Sequence')
            axes[1, 1].set_ylabel('Value')
            axes[1, 1].set_title('Sequence Comparison')
            axes[1, 1].set_xticks(x_pos)
            axes[1, 1].set_xticklabels(combined_df['label'], rotation=45)
            axes[1, 1].legend()
            axes[1, 1].grid(True, alpha=0.3)
        else:
            axes[1, 1].text(0.5, 0.5, 'No sequence data provided',
                            transform=axes[1, 1].transAxes,
                            ha='center', va='center', fontsize=12)

        plt.tight_layout()
        plt.show()

def advanced_biological_analysis():
    """Advanced biological analysis example."""

    # Initialize analyzer
    analyzer = KmerBioAnalyzer()

    # Example 1: Analyze existing database with sequence files
    print("=== Advanced Biological Analysis ===")

    # Load existing database
    analyzer.load_database("genome.rkdb")

    # Load sequences from FASTA
    sequences = list(SeqIO.parse("genome_sequences.fasta", "fasta"))[:5]  # First 5 sequences

    # Compare sequences
    comparison_df = analyzer.compare_sequence_databases(sequences)
    print("\nSequence Comparison:")
    print(comparison_df[['label', 'coverage', 'mean_count']].round(3))

    # Create k-mer abundance profile
    abundance_df = analyzer.create_kmer_abundance_profile(k=31, top_n=500)

    # Analyze specific sequence regions
    if sequences:
        test_seq = sequences[0]
        print(f"\nAnalyzing {test_seq.id}...")

        # Find high-density regions
        density_data = analyzer.find_kmer_density_regions(test_seq.seq)

        # Find top regions
        sorted_density = sorted(density_data, key=lambda x: x['density'], reverse=True)
        print("Top 5 high-density regions:")
        for region in sorted_density[:5]:
            print(f"  {region['start']}-{region['end']}: density={region['density']:.3f}")

    # Create visualizations
    analyzer.visualize_kmer_profiles(abundance_df, [comparison_df])

def sequence_feature_extraction():
    """Extract features from sequences using k-mer analysis."""

    analyzer = KmerBioAnalyzer()
    analyzer.load_database("genome.rkdb")

    # Load sequences
    sequences = list(SeqIO.parse("genome_sequences.fasta", "fasta"))

    features = []

    for seq_record in sequences:
        sequence = seq_record.seq
        seq_id = seq_record.id

        # Extract various features
        seq_length = len(sequence)
        gc_content = gc_fraction(sequence)

        # K-mer-based features
        kmer_results = analyzer.query_sequence_kmers(sequence, k=31)

        total_kmers = len(kmer_results)
        found_kmers = sum(1 for r in kmer_results if r['found'])
        total_counts = sum(r['count'] for r in kmer_results)

        # Calculate additional metrics
        coverage = found_kmers / total_kmers if total_kmers > 0 else 0
        avg_count = total_counts / found_kmers if found_kmers > 0 else 0

        # Find longest stretch of found k-mers
        max_consecutive = 0
        current_consecutive = 0

        for result in kmer_results:
            if result['found']:
                current_consecutive += 1
                max_consecutive = max(max_consecutive, current_consecutive)
            else:
                current_consecutive = 0

        features.append({
            'sequence_id': seq_id,
            'length': seq_length,
            'gc_content': gc_content,
            'total_kmers': total_kmers,
            'found_kmers': found_kmers,
            'coverage': coverage,
            'avg_count': avg_count,
            'max_consecutive': max_consecutive,
            'total_counts': total_counts
        })

    df = pd.DataFrame(features)

    print("=== Sequence Feature Extraction ===")
    print(f"Processed {len(df)} sequences")
    print("\nFeature Summary:")
    print(df[['length', 'gc_content', 'coverage', 'avg_count']].describe())

    return df

# Example usage
if __name__ == "__main__":
    # Run advanced analysis
    advanced_biological_analysis()

    # Extract features
    feature_df = sequence_feature_extraction()
```

---

This examples reference provides comprehensive, real-world examples of using the RustKmer Python API for various bioinformatics applications. Each example includes detailed explanations, error handling, and best practices for production use.