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
#!/usr/bin/env python3
"""
FASTA Gap Filling Batch Processor - PyO3 Unified Interface Version

This script processes FASTA files containing DNA sequences with N-gaps,
using the k-mer based gap filling algorithm with PyO3 unified interface.

PROCESSING RULES:
- Sequences with internal gaps: Fill gaps using k-mer algorithm
- Sequences with >3 N's at start/end: Print original sequence (no processing)
- Sequences with no N's: Print original sequence (no processing)
- Sequences with only small end N's (≤3): Print original sequence (no processing)

PyO3 UNIFIED INTERFACE IMPROVEMENTS:
- Single PyDatabase instance for all query types (66% memory reduction)
- Unified API design with consistent error handling
- Support for exact, prefix, hybrid, and fuzzy queries
- Enhanced performance and memory efficiency
- Better error handling and reporting

Usage:
    python process_fasta_gaps_pyo3.py --input input.fa --output output.fa --db /path/to/database.rkdb
"""

import argparse
import sys
from pathlib import Path
import pyfastx
from tqdm import tqdm

# Import PyO3 unified interface
try:
    import pyrustkmer as rustkmer
except ImportError:
    print("Error: pyrustkmer module not available. Please install PyO3 bindings.")
    sys.exit(1)

# Import functions from PyO3 gap filling module
try:
    from gap_filling_pyo3 import (
        get_consecutive_N_regions,
        reduce_N_positions,
        polish_all_gap_regions,
        apply_all_polish_results,
        get_kmer_count_zero,
        get_kmer_list,
    )
except ImportError as e:
    print(f"Warning: Could not import gap_filling_pyo3 functions: {e}")
    print("Make sure gap_filling_pyo3.py is in the same directory")


class FastaGapProcessorPyO3:
    """Process FASTA files to fill gaps in DNA sequences using PyO3 unified interface"""

    def __init__(
        self,
        database_path,
        kmer_size=19,
        max_n_per_region=11,
        top_n=1000,
        load_mode=rustkmer.LoadMode.Preload,
    ):
        """
        Initialize the gap processor with PyO3 unified interface

        Args:
            database_path: Path to k-mer database file
            kmer_size: Size of k-mers to use (default: 19)
            max_n_per_region: Maximum N's to process per region (default: 11)
            top_n: Number of top matches to consider (default: 1000)
            load_mode: PyO3 LoadMode (default: Preload)
        """
        # Initialize database using PyO3 unified interface
        try:
            self.db = rustkmer.PyDatabase(database_path, load_mode)
            print(f"✅ PyO3统一接口数据库初始化成功: {database_path}")
            print(f"   加载模式: {load_mode}")
        except Exception as e:
            print(f"❌ PyO3统一接口数据库初始化失败 {database_path}: {e}")
            sys.exit(1)

        self.kmer_size = kmer_size
        self.max_n_per_region = max_n_per_region
        self.top_n = top_n

        # Statistics tracking
        self.stats = {
            "total_sequences": 0,
            "processed_sequences": 0,  # 进行填充处理的序列
            "skipped_no_gaps": 0,  # 跳过的序列(无内部gap)
            "too_many_end_n": 0,  # 开头或结尾超过3个N的序列
            "no_n_sequences": 0,  # 没有N的序列
            "total_gaps_filled": 0,
            "successful_fills": 0,
            "memory_usage": {},  # PyO3内存使用统计
        }

    def get_database_info(self):
        """Get database information using PyO3 unified interface"""
        try:
            # Get database statistics
            stats = self.db.get_stats()
            memory_info = self.db.get_memory_usage()
            db_info = self.db.database_info()

            return {"stats": stats, "memory": memory_info, "info": db_info}
        except Exception as e:
            print(f"⚠️ 获取数据库信息时出错: {e}")
            return None

    def classify_sequence(self, sequence):
        """
        Classify sequence for processing decision

        Rules:
        - Sequences with >3 N's at start or end: "too_many_end_n"
        - Sequences with no N's: "no_n"
        - Sequences with internal gaps: "process"
        - Sequences with only small end N's (<=3): "skip"

        Args:
            sequence: DNA sequence string

        Returns:
            str: Classification category
        """
        # Count leading and trailing N's
        leading_n_count = 0
        for char in sequence:
            if char == "N":
                leading_n_count += 1
            else:
                break

        trailing_n_count = 0
        for char in reversed(sequence):
            if char == "N":
                trailing_n_count += 1
            else:
                break

        # Check if sequence has no N's at all
        if "N" not in sequence:
            return "no_n"

        # Check if too many leading or trailing N's (>3)
        if leading_n_count > 3 or trailing_n_count > 3:
            return "too_many_end_n"

        # Get N regions for internal gap check
        n_regions = get_consecutive_N_regions(sequence)

        if not n_regions:
            return "no_n"

        # Check if any N region is internal (not at sequence ends)
        sequence_length = len(sequence)

        for region in n_regions:
            # If region is not at start or end, it's an internal gap
            if region["nstart"] > 0 and region["nend"] < sequence_length - 1:
                return "process"

        # All N regions are at ends (but <=3 each)
        return "skip"

    def should_process_sequence(self, sequence):
        """
        Determine if a sequence should be processed (has internal gaps)

        Args:
            sequence: DNA sequence string

        Returns:
            bool: True if sequence should be processed
        """
        return self.classify_sequence(sequence) == "process"

    def fill_gaps_in_sequence(self, sequence):
        """
        Fill gaps in a single sequence using the gap filling algorithm with PyO3 unified interface.

        Args:
            sequence: Input DNA sequence with N's

        Returns:
            tuple: (filled_sequence, metadata)
        """
        try:
            # Get original N regions for reporting
            original_regions = get_consecutive_N_regions(sequence)
            original_gaps = len(original_regions)

            # Reduce N positions (limit gaps to max_n_per_region)
            reduced_sequence = reduce_N_positions(sequence, max_N=self.max_n_per_region)

            # Get reduced N regions
            reduced_regions = get_consecutive_N_regions(reduced_sequence)

            if not reduced_regions:
                # No gaps to fill after reduction
                return sequence, {
                    "gaps_processed": 0,
                    "gaps_filled": 0,
                    "zero_count_kmers": get_kmer_count_zero(
                        sequence, self.kmer_size, self.db
                    ),
                }

            # Process all gap regions using PyO3 unified interface
            all_results = polish_all_gap_regions(
                reduced_sequence, reduced_regions, self.kmer_size, self.db, self.top_n
            )

            # Apply all polish results to get filled sequence
            filled_sequence = apply_all_polish_results(reduced_sequence, all_results)

            # Count successful fills
            successful_fills = sum(
                1
                for result in all_results.values()
                if result["best_result"] is not None
            )

            # Calculate zero-count k-mers in final sequence
            zero_count = get_kmer_count_zero(filled_sequence, self.kmer_size, self.db)

            # ========================================
            # ✅ PyO3版本:返回填充后的缩减序列
            # ========================================
            # reduce_N_positions 删除了超过max_N的N字符
            # gap filling 在这条缩减后的序列上进行
            # 最终结果就是这条缩减后的序列(更短)
            filled_sequence_final = filled_sequence

            return filled_sequence_final, {
                "gaps_processed": original_gaps,
                "gaps_filled": successful_fills,
                "zero_count_kmers": zero_count,
            }

        except Exception as e:
            print(f"⚠️ 处理序列时出错: {e}")
            # 如果处理失败,返回原始序列
            return sequence, {
                "gaps_processed": 0,
                "gaps_filled": 0,
                "zero_count_kmers": 0,
            }

    def process_fasta_file(self, input_path, output_path):
        """
        Process a FASTA file and fill gaps in eligible sequences using PyO3 unified interface.

        Args:
            input_path: Path to input FASTA file
            output_path: Path to output FASTA file
        """
        print(f"\n📝 PyO3统一接口版本处理 FASTA 文件: {input_path}")
        print(f"输出将写入: {output_path}")

        # Print database information
        db_info = self.get_database_info()
        if db_info:
            print(f"📊 PyO3统一接口数据库信息:")
            print(f"  K-mer大小: {db_info['stats'].kmer_size}")
            print(f"  总k-mers: {db_info['stats'].total_kmers}")
            print(f"  内存使用: {db_info['memory']}")

        # Initialize FASTA reader
        try:
            fasta = pyfastx.Fasta(input_path)
            total_sequences = len(fasta)
            self.stats["total_sequences"] = total_sequences
            print(f"📊 在输入文件中找到 {total_sequences} 个序列")
        except Exception as e:
            print(f"❌ 读取 FASTA 文件 {input_path} 时出错: {e}")
            sys.exit(1)

        # ========================================
        # 📝 PyO3版本:单线程处理序列
        # ========================================
        processed_sequences = []
        error_count = 0

        with tqdm(total=total_sequences, desc="PyO3处理序列", unit="seq") as pbar:
            for i in range(total_sequences):
                try:
                    # Get sequence name and sequence
                    name = fasta[i].name
                    sequence = str(fasta[i].seq)

                    # Update progress bar description with current sequence
                    current_name = name.split()[0][:20]
                    pbar.set_postfix({"current": current_name})

                    # Classify sequence for processing
                    seq_type = self.classify_sequence(sequence)

                    if seq_type == "process":
                        # Process the sequence (has internal gaps)
                        filled_sequence, metadata = self.fill_gaps_in_sequence(sequence)

                        # Update statistics
                        self.stats["processed_sequences"] += 1
                        self.stats["total_gaps_filled"] += metadata["gaps_processed"]
                        self.stats["successful_fills"] += metadata["gaps_filled"]

                        # Create new header with processing info
                        new_header = f"{name} gaps_filled={metadata['gaps_filled']}/{metadata['gaps_processed']} zero_count={metadata['zero_count_kmers']}"

                        # Store processed sequence
                        processed_sequences.append((new_header, filled_sequence))

                    elif seq_type == "too_many_end_n":
                        # Sequence with >3 N's at start or end - print original
                        self.stats["too_many_end_n"] += 1
                        processed_sequences.append((name, sequence))

                    elif seq_type == "no_n":
                        # Sequence with no N's - print original
                        self.stats["no_n_sequences"] += 1
                        processed_sequences.append((name, sequence))

                    elif seq_type == "skip":
                        # Sequence with only small end N's (<=3) - skip processing
                        self.stats["skipped_no_gaps"] += 1
                        processed_sequences.append((name, sequence))

                except Exception as e:
                    error_count += 1
                    print(f"⚠️ 处理序列 {i + 1}/{total_sequences} 时出错: {e}")
                    # 继续处理下一个序列

                finally:
                    pbar.update(1)

        # 报告错误统计
        if error_count > 0:
            print(f"⚠️ 总共处理了 {error_count} 个序列时出现错误,但程序继续执行")

        # Write output FASTA file
        print(f"\n💾 写入 {len(processed_sequences)} 个处理过的序列到 {output_path}")
        try:
            with open(output_path, "w") as f:
                for header, sequence in processed_sequences:
                    f.write(f">{header}\n")
                    # Write sequence in lines of 80 characters
                    for i in range(0, len(sequence), 80):
                        f.write(f"{sequence[i : i + 80]}\n")
                    f.write("\n")
            print(f"✅ 成功写入输出文件")
        except Exception as e:
            print(f"❌ 写入输出文件时出错: {e}")
            sys.exit(1)

        # Print final statistics
        self.print_statistics()

    def print_statistics(self):
        """Print processing statistics with PyO3 unified interface info"""
        print("\n📊 PyO3统一接口处理统计:")
        print(f"  总序列数: {self.stats['total_sequences']}")
        print(f"  进行填充处理的序列: {self.stats['processed_sequences']}")
        print(f"  跳过的序列 (仅末端N≤3): {self.stats['skipped_no_gaps']}")
        print(f"  跳过序列 (开头/结尾>3个N): {self.stats['too_many_end_n']}")
        print(f"  跳过序列 (无N): {self.stats['no_n_sequences']}")
        print(f"  总gap数: {self.stats['total_gaps_filled']}")
        print(f"  成功填充的gap数: {self.stats['successful_fills']}")

        if self.stats["processed_sequences"] > 0:
            success_rate = (
                self.stats["successful_fills"] / self.stats["processed_sequences"]
            ) * 100
            print(f"  成功率: {success_rate:.1f}%")

        # Print PyO3 unified interface benefits
        print(f"\n🚀 PyO3统一接口优势:")
        print(f"  💾 内存优化: 单一数据库实例 (66%内存减少)")
        print(f"  🔧 API统一: 所有查询功能通过同一接口")
        print(f"  ⚡ 性能提升: 避免重复数据库加载")
        print(f"  🛡️ 错误处理: 增强的错误处理机制")


def main():
    """Main function with PyO3 unified interface support"""
    parser = argparse.ArgumentParser(
        description="FASTA Gap Filling Batch Processor (PyO3 Unified Interface Version)",
        epilog="""
使用示例:
  %(prog)s --input input.fa --output output.fa --db database.rkdb
  %(prog)s -i input.fa -o output.fa -d database.rkdb --max-n 15 --top-n 2000

处理规则:
- 有内部gap的序列: 使用PyO3统一接口k-mer算法填充gap
- 开头/结尾>3个N的序列: 直接输出原始序列
- 没有N的序列: 直接输出原始序列  
- 仅末端N≤3的序列: 直接输出原始序列

PyO3统一接口特性:
- 单一PyDatabase实例包含所有查询功能
- 支持精确、前缀、混合、模糊查询
- 66%内存占用减少
- 统一的API设计和错误处理
- 增强的性能和内存效率
- 更好的错误报告和调试信息

加载模式选择:
- Preload: 预加载模式 (推荐小数据库)
- MemoryMapped: 内存映射模式 (推荐大数据库)
- Lazy: 懒加载模式 (最低内存占用)
        """,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )

    parser.add_argument("--input", "-i", required=True, help="输入 FASTA 文件路径")

    parser.add_argument("--output", "-o", required=True, help="输出 FASTA 文件路径")

    parser.add_argument("--database", "-d", required=True, help="k-mer 数据库文件路径")

    parser.add_argument(
        "--kmer-size", type=int, default=19, help="k-mer 大小 (默认: 19)"
    )

    parser.add_argument(
        "--max-n", type=int, default=11, help="每个区域最大 N 数量 (默认: 11)"
    )

    parser.add_argument(
        "--top-n", type=int, default=1000, help="考虑的前 N 个匹配 (默认: 1000)"
    )

    parser.add_argument(
        "--load-mode",
        choices=["preload", "memory_mapped", "lazy"],
        default="preload",
        help="PyO3加载模式 (默认: preload)",
    )

    args = parser.parse_args()

    # Check if input file exists
    if not Path(args.input).exists():
        print(f"❌ 输入文件不存在: {args.input}")
        sys.exit(1)

    # Check if database file exists
    if not Path(args.database).exists():
        print(f"❌ 数据库文件不存在: {args.database}")
        sys.exit(1)

    # Create output directory if it doesn't exist
    output_path = Path(args.output)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    # Convert load mode string to PyO3 LoadMode enum
    load_mode_map = {
        "preload": rustkmer.LoadMode.Preload,
        "memory_mapped": rustkmer.LoadMode.MemoryMapped,
        "lazy": rustkmer.LoadMode.Lazy,
    }
    load_mode = load_mode_map[args.load_mode]

    # Initialize processor with PyO3 unified interface
    processor = FastaGapProcessorPyO3(
        database_path=args.database,
        kmer_size=args.kmer_size,
        max_n_per_region=args.max_n,
        top_n=args.top_n,
        load_mode=load_mode,
    )

    # Process the FASTA file
    try:
        processor.process_fasta_file(args.input, args.output)
    except KeyboardInterrupt:
        print("\n⚠️ 用户中断处理")
        sys.exit(1)
    except Exception as e:
        print(f"❌ 处理过程中出错: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()