genomicframe-core 0.2.0

High-performance genomics I/O and interoperability layer
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
//! Execution engine that converts logical plans into filtered readers
//!
//! This module executes optimized LogicalPlans by creating readers and applying
//! compiled filters. It handles the translation from lazy plans to eager execution.

use crate::core::GenomicRecordIterator;
use crate::error::{Error, Result};
use crate::expression::{Expr, ExprToFilter};
use crate::filters::RecordFilter;
use crate::plan::{LogicalPlan, PlanNode};
use crate::schema::FileFormat;
use std::path::PathBuf;

// Import format-specific types
use crate::formats::bam::{BamReader, BamRecord};
use crate::formats::bed::{BedReader, BedRecord};
use crate::formats::fastq::{FastqReader, FastqRecord};
use crate::formats::vcf::{VcfReader, VcfRecord};

// ============================================================================
// Execution Result Types
// ============================================================================

/// Result of executing a plan - holds the reader and metadata
pub enum ExecutionResult {
    Vcf(VcfExecution),
    Bam(BamExecution),
    Bed(BedExecution),
    Fastq(FastqExecution),
}
// TODO: Ergonomics! Only allow the user to call if the ExecutionResult is a BED result
impl ExecutionResult {
    /// Build an annotation index from BED execution results
    ///
    /// This provides an ergonomic way to go directly from a filtered query
    /// to an annotation index without manual iteration.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use genomicframe_core::plan::LogicalPlan;
    /// use genomicframe_core::execution::execute;
    /// use genomicframe_core::expression::col;
    /// use genomicframe_core::schema::{lit, FileFormat};
    ///
    /// // Query: Large genes only
    /// let plan = LogicalPlan::scan("genes.bed", FileFormat::Bed)
    ///     .filter(col("length").gte(lit(1000)));
    ///
    /// // Execute and build annotation index in one go!
    /// let genes = execute(plan)?
    ///     .to_annotation_index(|record| {
    ///         record.name.clone().unwrap_or_else(|| "unknown".to_string())
    ///     })?;
    /// # Ok::<(), genomicframe_core::error::Error>(())
    /// ```
    pub fn to_annotation_index<F>(
        self,
        extractor: F,
    ) -> Result<crate::interval::annotation::AnnotationIndex>
    where
        F: Fn(&BedRecord) -> String,
    {
        use crate::interval::annotation::{Annotation, AnnotationIndex};
        use crate::interval::GenomicInterval;
        use std::collections::HashMap;

        match self {
            ExecutionResult::Bed(mut bed_exec) => {
                // Collect all annotations grouped by chromosome
                let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

                while let Some(result) = bed_exec.next() {
                    let record = result?;
                    let interval: GenomicInterval = (&record).into();
                    let data = extractor(&record);

                    annotations
                        .entry(interval.chrom.clone())
                        .or_insert_with(Vec::new)
                        .push(Annotation { interval, data });
                }

                // Build interval trees efficiently from collected annotations
                let mut index = AnnotationIndex::new();
                index.build_trees(annotations);

                Ok(index)
            }
            _ => Err(Error::invalid_input(
                "to_annotation_index() only works with BED execution results",
            )),
        }
    }

    /// Annotate VCF variants with gene/exon information
    ///
    /// This provides an ergonomic way to annotate variants from VCF files
    /// with gene and exon annotations, returning structured statistics.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use genomicframe_core::plan::LogicalPlan;
    /// use genomicframe_core::execution::execute;
    /// use genomicframe_core::expression::{col, lit};
    /// use genomicframe_core::schema::FileFormat;
    ///
    /// // Load annotation indexes (genes and exons)
    /// let genes = /* ... load genes ... */;
    /// let exons = /* ... load exons ... */;
    ///
    /// // Query and annotate in one go!
    /// let plan = LogicalPlan::scan("variants.vcf", FileFormat::Vcf)
    ///     .filter(col("qual").gte(lit(30.0)));
    ///
    /// let result = execute(plan)?
    ///     .annotate_with_genes(&genes, &exons)?;
    ///
    /// println!("Genic variants: {}", result.genic_variants);
    /// println!("Exonic variants: {}", result.exonic_variants);
    /// # use genomicframe_core::interval::annotation::AnnotationIndex;
    /// # let genes = AnnotationIndex::new();
    /// # let exons = AnnotationIndex::new();
    /// # Ok::<(), genomicframe_core::error::Error>(())
    /// ```
    pub fn annotate_with_genes(
        self,
        genes: &crate::interval::annotation::AnnotationIndex,
        exons: &crate::interval::annotation::AnnotationIndex,
    ) -> Result<AnnotationResult> {
        use crate::interval::GenomicInterval;

        match self {
            ExecutionResult::Vcf(mut vcf_exec) => {
                let mut result = AnnotationResult::new();

                while let Some(record_result) = vcf_exec.next() {
                    let record = record_result?;
                    result.total_variants += 1;

                    // Classify variant type
                    if record.is_snp() {
                        result.snp_count += 1;
                    } else if record.is_indel() {
                        result.indel_count += 1;
                    }

                    // Convert to genomic interval
                    let interval: GenomicInterval = (&record).into();

                    // Query annotations using interval trees (O(log n + k))
                    // AnnotationIndex.query() automatically handles chromosome normalization (chr22 ↔ 22)
                    let overlapping_genes = genes.query(&interval);
                    let overlapping_exons = exons.query(&interval);

                    if !overlapping_genes.is_empty() {
                        result.genic_variants += 1;

                        if !overlapping_exons.is_empty() {
                            result.exonic_variants += 1;

                            // Collect examples (up to 5)
                            if result.example_exonic.len() < 5 {
                                result.example_exonic.push(AnnotatedVariant {
                                    chrom: record.chrom.clone(),
                                    pos: record.pos,
                                    reference: record.reference.clone(),
                                    alt: record.alt.clone(),
                                    qual: record.qual,
                                    genes: overlapping_genes
                                        .iter()
                                        .map(|s| s.to_string())
                                        .collect(),
                                    exons: overlapping_exons
                                        .iter()
                                        .map(|s| s.to_string())
                                        .collect(),
                                });
                            }
                        }
                    } else {
                        result.intergenic_variants += 1;
                    }
                }

                Ok(result)
            }
            ExecutionResult::Bam(mut bam_exec) => {
                let mut result = AnnotationResult::new();

                while let Some(record_result) = bam_exec.next() {
                    let record = record_result?;
                    result.total_variants += 1; // For BAM, this counts total reads

                    // Skip unmapped reads
                    if record.is_unmapped() {
                        continue;
                    }

                    // Convert to genomic interval
                    let interval: GenomicInterval = (&record).into();

                    // Query annotations using interval trees (O(log n + k))
                    let overlapping_genes = genes.query(&interval);
                    let overlapping_exons = exons.query(&interval);

                    if !overlapping_genes.is_empty() {
                        result.genic_variants += 1; // For BAM, this counts genic reads

                        if !overlapping_exons.is_empty() {
                            result.exonic_variants += 1; // For BAM, this counts exonic reads
                        }
                    } else {
                        result.intergenic_variants += 1;
                    }
                }

                Ok(result)
            }
            _ => Err(Error::invalid_input(
                "annotate_with_genes() only works with VCF and BAM execution results",
            )),
        }
    }

    /// Count total records in the execution result
    ///
    /// This consumes the iterator and returns the total count.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use genomicframe_core::plan::LogicalPlan;
    /// use genomicframe_core::execution::execute;
    /// use genomicframe_core::schema::FileFormat;
    ///
    /// let plan = LogicalPlan::scan("variants.vcf", FileFormat::Vcf);
    /// let count = execute(plan)?.count()?;
    /// println!("Total variants: {}", count);
    /// # Ok::<(), genomicframe_core::error::Error>(())
    /// ```
    pub fn count(mut self) -> Result<usize> {
        let mut total = 0;

        match &mut self {
            ExecutionResult::Vcf(vcf_exec) => {
                while let Some(_) = vcf_exec.next() {
                    total += 1;
                }
            }
            ExecutionResult::Bam(bam_exec) => {
                while let Some(_) = bam_exec.next() {
                    total += 1;
                }
            }
            ExecutionResult::Bed(bed_exec) => {
                while let Some(_) = bed_exec.next() {
                    total += 1;
                }
            }
            ExecutionResult::Fastq(fastq_exec) => {
                while let Some(_) = fastq_exec.next() {
                    total += 1;
                }
            }
        }

        Ok(total)
    }

    /// Peek at the first N records
    ///
    /// Returns a string representation of the first N records for quick inspection.
    /// This is useful for exploratory analysis and debugging.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use genomicframe_core::plan::LogicalPlan;
    /// use genomicframe_core::execution::execute;
    /// use genomicframe_core::expression::col;
    /// use genomicframe_core::schema::FileFormat;
    ///
    /// let plan = LogicalPlan::scan("variants.vcf", FileFormat::Vcf);
    /// let preview = execute(plan)?.head(5)?;
    /// println!("{}", preview);
    /// # Ok::<(), genomicframe_core::error::Error>(())
    /// ```
    pub fn head(mut self, n: usize) -> Result<String> {
        use std::fmt::Write;
        let mut output = String::new();

        match &mut self {
            ExecutionResult::Vcf(vcf_exec) => {
                writeln!(&mut output, "First {} VCF records:", n).unwrap();
                writeln!(&mut output, "{:-<80}", "").unwrap();
                for (i, record_result) in vcf_exec.take(n).enumerate() {
                    let record = record_result?;
                    writeln!(
                        &mut output,
                        "{}. {}:{} {}>{} QUAL={:.1}",
                        i + 1,
                        record.chrom,
                        record.pos,
                        record.reference,
                        record.alt.join(","),
                        record.qual.unwrap_or(0.0)
                    )
                    .unwrap();
                }
            }
            ExecutionResult::Bam(bam_exec) => {
                writeln!(&mut output, "First {} BAM records:", n).unwrap();
                writeln!(&mut output, "{:-<120}", "").unwrap();
                writeln!(
                    &mut output,
                    "{:<5} | {:<8} | {:<10} | {:<12} | {:<8} | {:<10} | {:<20}",
                    "Index", "MAPQ", "Chrom/Rname", "Position", "Length", "Flags", "Read Name"
                )
                .unwrap();
                writeln!(&mut output, "{:-<120}", "").unwrap();

                let mut count = 0;
                for (i, record_result) in bam_exec.take(n).enumerate() {
                    let record = record_result?;
                    let length = record.seq_string().len();
                    let flags = format!("0x{:04x}", record.flag);
                    writeln!(
                        &mut output,
                        "{:<5} | {:<8} | {:<10} | {:<12} | {:<8} | {:<10} | {:<20}",
                        i + 1,
                        record.mapq,
                        record.rname,
                        format!("{}-{}", record.pos, record.pos + length as i32),
                        length,
                        flags,
                        &record.qname[..record.qname.len().min(20)]
                    )
                    .unwrap();
                    count += 1;
                }

                if count == 0 {
                    writeln!(&mut output, "No records passed filters").unwrap();
                }

                // Show scan statistics
                let scanned = bam_exec.scanned_count();
                let skipped = bam_exec.skipped_errors();
                if scanned > 0 || skipped > 0 {
                    writeln!(&mut output, "\nScan statistics:").unwrap();
                    writeln!(&mut output, "  Records scanned: {}", scanned).unwrap();
                    writeln!(&mut output, "  Records returned: {}", count).unwrap();
                    if skipped > 0 {
                        writeln!(&mut output, "  Parse errors skipped: {}", skipped).unwrap();
                    }
                }
            }
            ExecutionResult::Bed(bed_exec) => {
                writeln!(&mut output, "First {} BED records:", n).unwrap();
                writeln!(&mut output, "{:-<80}", "").unwrap();
                for (i, record_result) in bed_exec.take(n).enumerate() {
                    let record = record_result?;
                    writeln!(
                        &mut output,
                        "{}. {}:{}-{} {}",
                        i + 1,
                        record.chrom,
                        record.start,
                        record.end,
                        record.name.as_deref().unwrap_or("-")
                    )
                    .unwrap();
                }
            }
            ExecutionResult::Fastq(fastq_exec) => {
                writeln!(&mut output, "First {} FASTQ records:", n).unwrap();
                writeln!(&mut output, "{:-<80}", "").unwrap();
                for (i, record_result) in fastq_exec.take(n).enumerate() {
                    let record = record_result?;
                    writeln!(
                        &mut output,
                        "{}. {} length={} mean_qual={:.1}",
                        i + 1,
                        record.id,
                        record.sequence.len(),
                        record.mean_quality()
                    )
                    .unwrap();
                }
            }
        }

        Ok(output)
    }
}

// ============================================================================
// Annotation Result Types
// ============================================================================

/// Result of variant annotation with genes/exons
#[derive(Debug, Clone)]
pub struct AnnotationResult {
    pub total_variants: usize,
    pub snp_count: usize,
    pub indel_count: usize,
    pub genic_variants: usize,
    pub exonic_variants: usize,
    pub intergenic_variants: usize,
    pub example_exonic: Vec<AnnotatedVariant>,
}

impl AnnotationResult {
    fn new() -> Self {
        Self {
            total_variants: 0,
            snp_count: 0,
            indel_count: 0,
            genic_variants: 0,
            exonic_variants: 0,
            intergenic_variants: 0,
            example_exonic: Vec::new(),
        }
    }

    /// Print a summary of the annotation results
    pub fn print_summary(&self) {
        // Generic title (works for both VCF variants and BAM reads)
        let item_type = if self.snp_count > 0 || self.indel_count > 0 {
            "VCF Annotation Results"
        } else {
            "BAM Annotation Results"
        };

        println!("  📈 {}:", item_type);

        let count_label = if self.snp_count > 0 || self.indel_count > 0 {
            "Total variants"
        } else {
            "Total reads"
        };

        println!("     {}:     {:>12}", count_label, self.total_variants);

        // Only show SNP/Indel stats for VCF
        if self.snp_count > 0 || self.indel_count > 0 {
            println!(
                "     SNPs:               {:>12} ({:>5.1}%)",
                self.snp_count,
                (self.snp_count as f64 / self.total_variants.max(1) as f64) * 100.0
            );
            println!(
                "     Indels:             {:>12} ({:>5.1}%)",
                self.indel_count,
                (self.indel_count as f64 / self.total_variants.max(1) as f64) * 100.0
            );
        }

        println!("\n  📊 Annotation Categories:");
        println!(
            "     Genic:              {:>12} ({:>5.1}%)",
            self.genic_variants,
            (self.genic_variants as f64 / self.total_variants.max(1) as f64) * 100.0
        );
        println!(
            "     Exonic:             {:>12} ({:>5.1}%)",
            self.exonic_variants,
            (self.exonic_variants as f64 / self.total_variants.max(1) as f64) * 100.0
        );
        println!(
            "     Intergenic:         {:>12} ({:>5.1}%)",
            self.intergenic_variants,
            (self.intergenic_variants as f64 / self.total_variants.max(1) as f64) * 100.0
        );

        // Show example exonic variants
        if !self.example_exonic.is_empty() {
            println!("\n  📋 Example High-Quality Exonic Variants:");
            for (i, variant) in self.example_exonic.iter().enumerate() {
                println!(
                    "     {}. {}:{} {}>{} QUAL={:.1}",
                    i + 1,
                    variant.chrom,
                    variant.pos,
                    variant.reference,
                    variant.alt.join(","),
                    variant.qual.unwrap_or(0.0)
                );
                println!("        Genes: {}", variant.genes.join(", "));
                println!("        Exons: {} exon(s)", variant.exons.len());
            }
        }

        println!("\n  ✅ Functional Impact:");
        let coding_percent =
            (self.exonic_variants as f64 / self.total_variants.max(1) as f64) * 100.0;
        if coding_percent > 10.0 {
            println!("     HIGH coding variant rate ({:.1}%)", coding_percent);
            println!("     Likely exome/targeted sequencing data");
        } else if coding_percent > 1.0 {
            println!("     TYPICAL coding variant rate ({:.1}%)", coding_percent);
            println!("     Standard whole-genome sequencing");
        } else {
            println!("     LOW coding variant rate ({:.1}%)", coding_percent);
            println!("     Most variants in non-coding regions");
        }
    }
}

/// An annotated variant with gene/exon information
#[derive(Debug, Clone)]
pub struct AnnotatedVariant {
    pub chrom: String,
    pub pos: u64,
    pub reference: String,
    pub alt: Vec<String>,
    pub qual: Option<f64>,
    pub genes: Vec<String>,
    pub exons: Vec<String>,
}

/// VCF execution result
pub struct VcfExecution {
    reader: VcfReader,
    filter: Option<Box<dyn RecordFilter<VcfRecord>>>,
    limit: Option<usize>,
    count: usize,
}

/// BAM execution result
pub struct BamExecution {
    reader: BamReader<std::fs::File>,
    filter: Option<Box<dyn RecordFilter<BamRecord>>>,
    limit: Option<usize>,
    count: usize,
    skipped_errors: usize,
}

impl BamExecution {
    /// Get the number of records that failed to parse and were skipped
    pub fn skipped_errors(&self) -> usize {
        self.skipped_errors
    }

    /// Get the number of records scanned so far
    pub fn scanned_count(&self) -> usize {
        self.count
    }
}

/// BED execution result
pub struct BedExecution {
    reader: BedReader,
    filter: Option<Box<dyn RecordFilter<BedRecord>>>,
    limit: Option<usize>,
    count: usize,
}

/// FASTQ execution result
pub struct FastqExecution {
    reader: FastqReader,
    filter: Option<Box<dyn RecordFilter<FastqRecord>>>,
    limit: Option<usize>,
    count: usize,
}

// ============================================================================
// Iterator implementations for execution results
// ============================================================================

impl Iterator for VcfExecution {
    type Item = Result<VcfRecord>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            // Check limit BEFORE reading next record (scan limit, not result limit)
            if let Some(limit) = self.limit {
                if self.count >= limit {
                    return None;
                }
            }

            // Get next record
            match self.reader.next_record() {
                Ok(Some(record)) => {
                    self.count += 1; // Count ALL records scanned

                    // Apply filter if present
                    if let Some(ref filter) = self.filter {
                        if !filter.test(&record) {
                            continue; // Skip filtered records
                        }
                    }
                    return Some(Ok(record));
                }
                Ok(None) => return None,
                Err(e) => return Some(Err(e)),
            }
        }
    }
}

impl Iterator for BamExecution {
    type Item = Result<BamRecord>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            // Check limit BEFORE reading next record (scan limit, not result limit)
            if let Some(limit) = self.limit {
                if self.count >= limit {
                    return None;
                }
            }

            match self.reader.next_record() {
                Ok(Some(record)) => {
                    self.count += 1; // Count ALL records scanned

                    if let Some(ref filter) = self.filter {
                        if !filter.test(&record) {
                            continue;
                        }
                    }
                    return Some(Ok(record));
                }
                Ok(None) => return None,
                Err(e) => {
                    // Skip records that fail to parse (e.g., invalid CIGAR, corrupt data)
                    // This matches the behavior of filtered readers that skip bad records
                    self.skipped_errors += 1;
                    self.count += 1; // Still count as scanned

                    // Only print warning for first few errors to avoid spam
                    if self.skipped_errors <= 3 {
                        eprintln!("Warning: Skipping BAM record that failed to parse: {}", e);
                    } else if self.skipped_errors == 4 {
                        eprintln!("Warning: Additional parse errors will be counted silently...");
                    }

                    continue; // Try next record
                }
            }
        }
    }
}

impl Iterator for BedExecution {
    type Item = Result<BedRecord>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(limit) = self.limit {
                if self.count >= limit {
                    return None;
                }
            }

            match self.reader.next_record() {
                Ok(Some(record)) => {
                    if let Some(ref filter) = self.filter {
                        if !filter.test(&record) {
                            continue;
                        }
                    }
                    self.count += 1;
                    return Some(Ok(record));
                }
                Ok(None) => return None,
                Err(e) => return Some(Err(e)),
            }
        }
    }
}

impl Iterator for FastqExecution {
    type Item = Result<FastqRecord>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(limit) = self.limit {
                if self.count >= limit {
                    return None;
                }
            }

            match self.reader.next_record() {
                Ok(Some(record)) => {
                    if let Some(ref filter) = self.filter {
                        if !filter.test(&record) {
                            continue;
                        }
                    }
                    self.count += 1;
                    return Some(Ok(record));
                }
                Ok(None) => return None,
                Err(e) => return Some(Err(e)),
            }
        }
    }
}

// ============================================================================
// Main Executor
// ============================================================================

/// Executes a logical plan and returns an iterator over results
pub fn execute(plan: LogicalPlan) -> Result<ExecutionResult> {
    // Optimize the plan first
    let optimized = plan.optimize();

    // Extract execution parameters
    let format = optimized
        .format()
        .ok_or_else(|| Error::invalid_input("Plan has no data source"))?;
    let path = extract_scan_path(&optimized.root)?;
    let filter_expr = extract_filter(&optimized.root);
    let limit = extract_limit(&optimized.root);

    // Execute based on format
    match format {
        FileFormat::Vcf => {
            let reader = VcfReader::from_path(&path)?;
            let filter = if let Some(expr) = filter_expr {
                Some(expr.compile()?)
            } else {
                None
            };
            Ok(ExecutionResult::Vcf(VcfExecution {
                reader,
                filter,
                limit,
                count: 0,
            }))
        }
        FileFormat::Bam => {
            let mut reader = BamReader::from_path(&path)?;
            reader.read_header()?; // CRITICAL: Must read header before reading records
            let filter = if let Some(expr) = filter_expr {
                Some(expr.compile()?)
            } else {
                None
            };
            Ok(ExecutionResult::Bam(BamExecution {
                reader,
                filter,
                limit,
                count: 0,
                skipped_errors: 0,
            }))
        }
        FileFormat::Bed => {
            let reader = BedReader::from_path(&path)?;
            let filter = if let Some(expr) = filter_expr {
                Some(expr.compile()?)
            } else {
                None
            };
            Ok(ExecutionResult::Bed(BedExecution {
                reader,
                filter,
                limit,
                count: 0,
            }))
        }
        FileFormat::Fastq => {
            let reader = FastqReader::from_path(&path)?;
            let filter = if let Some(expr) = filter_expr {
                Some(expr.compile()?)
            } else {
                None
            };
            Ok(ExecutionResult::Fastq(FastqExecution {
                reader,
                filter,
                limit,
                count: 0,
            }))
        }
        _ => Err(Error::invalid_input(format!(
            "Unsupported format: {:?}",
            format
        ))),
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

fn extract_scan_path(node: &PlanNode) -> Result<PathBuf> {
    match node {
        PlanNode::Scan { path, .. } => Ok(path.clone()),
        PlanNode::Filter { input, .. } => extract_scan_path(input),
        PlanNode::Limit { input, .. } => extract_scan_path(input),
        PlanNode::MaxScan { input, .. } => extract_scan_path(input),
        _ => Err(Error::invalid_input("No Scan node found in plan")),
    }
}

fn extract_filter(node: &PlanNode) -> Option<Expr> {
    match node {
        PlanNode::Filter { input, predicate } => {
            // Combine with inner filters
            if let Some(inner_filter) = extract_filter(input) {
                Some(predicate.clone().and(inner_filter))
            } else {
                Some(predicate.clone())
            }
        }
        PlanNode::Limit { input, .. } => extract_filter(input),
        PlanNode::MaxScan { input, .. } => extract_filter(input),
        PlanNode::Scan { .. } => None,
        _ => None,
    }
}

fn extract_limit(node: &PlanNode) -> Option<usize> {
    match node {
        PlanNode::Limit { count, .. } => Some(*count),
        PlanNode::MaxScan { count, .. } => Some(*count),
        PlanNode::Filter { input, .. } => extract_limit(input),
        _ => None,
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::expression::{col, lit};

    #[test]
    fn test_execute_vcf_no_filter() {
        let plan = LogicalPlan::scan("examples/vcf/test_data.vcf", FileFormat::Vcf);

        let result = execute(plan);
        assert!(result.is_ok());

        if let Ok(ExecutionResult::Vcf(mut exec)) = result {
            let mut count = 0;
            for record in exec.by_ref() {
                assert!(record.is_ok());
                count += 1;
            }
            assert!(count > 0);
        } else {
            panic!("Expected VCF execution result");
        }
    }

    #[test]
    fn test_execute_vcf_with_filter() {
        let plan = LogicalPlan::scan("examples/vcf/test_data.vcf", FileFormat::Vcf)
            .filter(col("qual").gt(lit(30.0)));

        let result = execute(plan);
        assert!(result.is_ok());

        if let Ok(ExecutionResult::Vcf(mut exec)) = result {
            let mut count = 0;
            for record in exec.by_ref() {
                if let Ok(rec) = record {
                    if let Some(qual) = rec.qual {
                        assert!(qual > 30.0);
                    }
                    count += 1;
                }
            }
            assert!(count > 0);
        }
    }

    #[test]
    fn test_execute_vcf_with_limit() {
        let plan = LogicalPlan::scan("examples/vcf/test_data.vcf", FileFormat::Vcf).limit(5);

        let result = execute(plan);
        assert!(result.is_ok());

        if let Ok(ExecutionResult::Vcf(exec)) = result {
            let count = exec.count();
            assert_eq!(count, 5);
        }
    }

    #[test]
    fn test_execute_vcf_filter_and_limit() {
        let plan = LogicalPlan::scan("examples/vcf/test_data.vcf", FileFormat::Vcf)
            .filter(col("qual").gt(lit(20.0)))
            .limit(10);

        let result = execute(plan);
        assert!(result.is_ok());

        if let Ok(ExecutionResult::Vcf(exec)) = result {
            let records: Vec<_> = exec.collect();
            assert!(records.len() <= 10);
            for record in records {
                if let Ok(rec) = record {
                    if let Some(qual) = rec.qual {
                        assert!(qual > 20.0);
                    }
                }
            }
        }
    }
}