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
//! Genomic interval annotation
//!
//! This module provides infrastructure for annotating genomic intervals
//! with contextual information from reference databases (genes, exons, etc.).
//!
//! # Design Philosophy
//!
//! - **Lazy by default**: Annotations are computed on-demand during iteration
//! - **Format-agnostic**: Works with any format that converts to GenomicInterval
//! - **Composable**: Multiple annotation sources can be chained
//! - **Fast queries**: O(log n + k) overlap queries using interval trees
//!
//! # Examples
//!
//! ```no_run
//! use genomicframe_core::interval::annotation::AnnotationIndex;
//! use genomicframe_core::formats::bed::BedReader;
//!
//! // Build annotation index from BED file
//! let exons = AnnotationIndex::from_bed("exons.bed", |record| {
//!     record.name.clone().unwrap_or_else(|| "unknown".to_string())
//! })?;
//!
//! // Query for overlapping annotations
//! let interval = GenomicInterval::new("chr1", 1000, 2000);
//! let genes = exons.query(&interval);
//! # use genomicframe_core::interval::GenomicInterval;
//! # Ok::<(), genomicframe_core::error::Error>(())
//! ```

use crate::core::GenomicRecordIterator;
use crate::error::Result;
use crate::formats::bed::{BedReader, BedRecord};
use crate::interval::GenomicInterval;
use std::collections::HashMap;
use std::path::Path;

/// Annotation metadata attached to an interval
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Annotation {
    /// The genomic interval
    pub interval: GenomicInterval,
    /// Free-form annotation data (gene name, feature type, etc.)
    pub data: String,
}

/// Interval tree node for O(log n + k) overlap queries
#[derive(Debug, Clone)]
pub struct IntervalTreeNode {
    center: u64,
    left_sorted: Vec<Annotation>,  // Sorted by start position
    right_sorted: Vec<Annotation>, // Sorted by end position
    left: Option<Box<IntervalTreeNode>>,
    right: Option<Box<IntervalTreeNode>>,
}

/// A standalone interval tree for efficient overlap queries
///
/// This provides direct access to the tree structure for advanced use cases.
#[derive(Debug, Clone)]
pub struct IntervalTree {
    root: Option<IntervalTreeNode>,
    count: usize,
}

impl IntervalTree {
    /// Create an empty interval tree
    pub fn new() -> Self {
        Self {
            root: None,
            count: 0,
        }
    }

    /// Build interval tree from a collection of annotations
    pub fn from_annotations(annotations: Vec<Annotation>) -> Self {
        let count = annotations.len();
        let root = IntervalTreeNode::from_annotations(annotations);
        Self { root, count }
    }

    /// Query for overlapping annotations
    pub fn query<'a>(&'a self, interval: &GenomicInterval) -> Vec<&'a str> {
        let mut results = Vec::new();
        if let Some(ref root) = self.root {
            root.query(interval, &mut results);
        }
        results
    }

    /// Query for overlapping annotations (full structs)
    pub fn query_annotations<'a>(&'a self, interval: &GenomicInterval) -> Vec<&'a Annotation> {
        let mut results = Vec::new();
        if let Some(ref root) = self.root {
            root.query_annotations(interval, &mut results);
        }
        results
    }

    /// Count of annotations in the tree
    pub fn len(&self) -> usize {
        self.count
    }

    /// Check if tree is empty
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }
}

impl Default for IntervalTree {
    fn default() -> Self {
        Self::new()
    }
}

impl IntervalTreeNode {
    /// Build interval tree from a list of annotations
    fn from_annotations(annotations: Vec<Annotation>) -> Option<Self> {
        if annotations.is_empty() {
            return None;
        }

        // Base case: for small sets, stop recursing and store directly
        // This prevents stack overflow and improves performance for leaf nodes
        // With a threshold of 64, max tree depth is ~log2(N/64), so for 32K items
        // that's ~9 levels, well within stack limits
        if annotations.len() <= 64 {
            let mut left_sorted = annotations.clone();
            let mut right_sorted = annotations;
            left_sorted.sort_by_key(|a| a.interval.start);
            right_sorted.sort_by_key(|a| a.interval.end);

            let center = left_sorted[left_sorted.len() / 2].interval.start;

            return Some(IntervalTreeNode {
                center,
                left_sorted,
                right_sorted,
                left: None,
                right: None,
            });
        }

        // Find center point (median of all start/end coordinates)
        let mut coords: Vec<u64> = annotations
            .iter()
            .flat_map(|a| vec![a.interval.start, a.interval.end])
            .collect();
        coords.sort_unstable();
        let center = coords[coords.len() / 2];

        // Partition intervals
        let mut left_intervals = Vec::new();
        let mut right_intervals = Vec::new();
        let mut center_intervals = Vec::new();

        for ann in annotations {
            if ann.interval.end <= center {
                left_intervals.push(ann);
            } else if ann.interval.start > center {
                right_intervals.push(ann);
            } else {
                // Overlaps center
                center_intervals.push(ann);
            }
        }

        // Sort center intervals for efficient stabbing queries
        let mut left_sorted = center_intervals.clone();
        let mut right_sorted = center_intervals;

        left_sorted.sort_by_key(|a| a.interval.start);
        right_sorted.sort_by_key(|a| a.interval.end);

        Some(IntervalTreeNode {
            center,
            left_sorted,
            right_sorted,
            left: IntervalTreeNode::from_annotations(left_intervals).map(Box::new),
            right: IntervalTreeNode::from_annotations(right_intervals).map(Box::new),
        })
    }

    /// Query for overlapping intervals
    fn query<'a>(&'a self, interval: &GenomicInterval, results: &mut Vec<&'a str>) {
        // Check intervals that span the center
        if interval.end <= self.center {
            // Query left of center
            for ann in &self.left_sorted {
                if ann.interval.start >= interval.end {
                    break; // No more overlaps possible
                }
                if ann.interval.overlaps(interval) {
                    results.push(&ann.data);
                }
            }
        } else if interval.start > self.center {
            // Query right of center
            for ann in self.right_sorted.iter().rev() {
                if ann.interval.end <= interval.start {
                    break; // No more overlaps possible
                }
                if ann.interval.overlaps(interval) {
                    results.push(&ann.data);
                }
            }
        } else {
            // Query spans center - check all center intervals
            for ann in &self.left_sorted {
                if ann.interval.overlaps(interval) {
                    results.push(&ann.data);
                }
            }
        }

        // Recurse into subtrees
        if interval.start < self.center {
            if let Some(ref left) = self.left {
                left.query(interval, results);
            }
        }
        if interval.end > self.center {
            if let Some(ref right) = self.right {
                right.query(interval, results);
            }
        }
    }

    /// Query for overlapping annotations (full structs)
    fn query_annotations<'a>(
        &'a self,
        interval: &GenomicInterval,
        results: &mut Vec<&'a Annotation>,
    ) {
        // Check intervals that span the center
        if interval.end <= self.center {
            for ann in &self.left_sorted {
                if ann.interval.start >= interval.end {
                    break;
                }
                if ann.interval.overlaps(interval) {
                    results.push(ann);
                }
            }
        } else if interval.start > self.center {
            for ann in self.right_sorted.iter().rev() {
                if ann.interval.end <= interval.start {
                    break;
                }
                if ann.interval.overlaps(interval) {
                    results.push(ann);
                }
            }
        } else {
            for ann in &self.left_sorted {
                if ann.interval.overlaps(interval) {
                    results.push(ann);
                }
            }
        }

        if interval.start < self.center {
            if let Some(ref left) = self.left {
                left.query_annotations(interval, results);
            }
        }
        if interval.end > self.center {
            if let Some(ref right) = self.right {
                right.query_annotations(interval, results);
            }
        }
    }
}

/// Fast interval index using interval trees for O(log n + k) overlap queries
///
/// This implementation uses a centered interval tree per chromosome,
/// providing logarithmic query time instead of linear scanning.
#[derive(Debug, Clone)]
pub struct AnnotationIndex {
    /// Interval trees grouped by chromosome
    trees: HashMap<String, IntervalTreeNode>,
    /// Total annotation count (cached for efficiency)
    count: usize,
}

impl AnnotationIndex {
    /// Create an empty annotation index
    pub fn new() -> Self {
        Self {
            trees: HashMap::new(),
            count: 0,
        }
    }

    /// Build interval trees from collected annotations
    ///
    /// This should be called after all insertions are complete.
    /// For efficiency, batch all insertions then call this once.
    pub fn build_trees(&mut self, annotations: HashMap<String, Vec<Annotation>>) {
        self.count = annotations.values().map(|v| v.len()).sum();

        for (chrom, ann_list) in annotations {
            if let Some(tree) = IntervalTreeNode::from_annotations(ann_list) {
                self.trees.insert(chrom, tree);
            }
        }
    }

    /// Build annotation index from BED file
    ///
    /// The `extractor` function converts each BED record into annotation data.
    /// Typically this extracts the "name" field, but can be customized.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use genomicframe_core::interval::annotation::AnnotationIndex;
    ///
    /// // Extract gene names from BED name field
    /// let index = AnnotationIndex::from_bed("genes.bed", |record| {
    ///     record.name.clone().unwrap_or_default()
    /// })?;
    ///
    /// // Custom extraction: combine name and score
    /// let index = AnnotationIndex::from_bed("features.bed", |record| {
    ///     format!("{}:{}",
    ///         record.name.as_deref().unwrap_or("unknown"),
    ///         record.score.unwrap_or(0))
    /// })?;
    /// # Ok::<(), genomicframe_core::error::Error>(())
    /// ```
    pub fn from_bed<P, F>(path: P, extractor: F) -> Result<Self>
    where
        P: AsRef<Path>,
        F: Fn(&BedRecord) -> String,
    {
        let mut reader = BedReader::from_path(path)?;
        let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        while let Some(record) = reader.next_record()? {
            let interval: GenomicInterval = (&record).into();
            let data = extractor(&record);

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

        let mut index = Self::new();
        index.build_trees(annotations);
        Ok(index)
    }

    /// Build annotation index from an iterator of BED records
    pub fn from_records<'a, I, F>(records: I, extractor: F) -> Self
    where
        I: IntoIterator<Item = &'a BedRecord>,
        F: Fn(&BedRecord) -> String,
    {
        let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        for record in records {
            let interval: GenomicInterval = record.into();
            let data = extractor(record);

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

        let mut index = Self::new();
        index.build_trees(annotations);
        index
    }

    /// Build annotation index from a reader (possibly filtered)
    pub fn from_reader<I, F>(mut reader: I, extractor: F) -> Result<Self>
    where
        I: crate::core::GenomicRecordIterator<Record = BedRecord>,
        F: Fn(&BedRecord) -> String,
    {
        let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        while let Some(record) = reader.next_record()? {
            let interval: GenomicInterval = (&record).into();
            let data = extractor(&record);

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

        let mut index = Self::new();
        index.build_trees(annotations);
        Ok(index)
    }

    /// Query for all annotations overlapping the given interval
    ///
    /// Returns references to annotation data strings.
    ///
    /// # Performance
    /// O(log n + k) where n = number of annotations on the chromosome,
    /// k = number of overlapping annotations
    pub fn query(&self, interval: &GenomicInterval) -> Vec<&str> {
        let mut results = Vec::new();

        // Try exact chrom match first
        if let Some(tree) = self.trees.get(&interval.chrom) {
            tree.query(interval, &mut results);
            return results;
        }

        // Fallback: try alternate chrom name (add/remove leading "chr")
        let alt_chrom = if interval.chrom.starts_with("chr") {
            interval.chrom.trim_start_matches("chr").to_string()
        } else {
            format!("chr{}", interval.chrom)
        };

        if let Some(tree) = self.trees.get(&alt_chrom) {
            // Create a normalized query interval for overlap checking
            let normalized_query = GenomicInterval {
                chrom: alt_chrom,
                start: interval.start,
                end: interval.end,
            };

            tree.query(&normalized_query, &mut results);
        }

        results
    }

    /// Query for all annotations overlapping the given interval
    ///
    /// Returns full Annotation structs (includes intervals).
    pub fn query_annotations(&self, interval: &GenomicInterval) -> Vec<&Annotation> {
        let mut results = Vec::new();

        // Try exact chrom match first
        if let Some(tree) = self.trees.get(&interval.chrom) {
            tree.query_annotations(interval, &mut results);
            return results;
        }

        // Fallback: try alternate chrom name
        let alt_chrom = if interval.chrom.starts_with("chr") {
            interval.chrom.trim_start_matches("chr").to_string()
        } else {
            format!("chr{}", interval.chrom)
        };

        if let Some(tree) = self.trees.get(&alt_chrom) {
            let normalized_query = GenomicInterval {
                chrom: alt_chrom,
                start: interval.start,
                end: interval.end,
            };

            tree.query_annotations(&normalized_query, &mut results);
        }

        results
    }

    /// Count total annotations in the index
    pub fn len(&self) -> usize {
        self.count
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Get chromosomes present in the index
    pub fn chromosomes(&self) -> Vec<&str> {
        self.trees.keys().map(|s| s.as_str()).collect()
    }
}

impl Default for AnnotationIndex {
    fn default() -> Self {
        Self::new()
    }
}

/// Multi-index query result for efficient batch queries
#[derive(Debug, Clone)]
pub struct MultiQueryResult<'a> {
    pub genes: Vec<&'a str>,
    pub exons: Vec<&'a str>,
}

/// Query multiple annotation indexes simultaneously for better cache locality
///
/// This is more efficient than calling query() on each index separately
/// when you need results from multiple sources.
pub fn multi_query<'a>(
    interval: &GenomicInterval,
    genes: &'a AnnotationIndex,
    exons: &'a AnnotationIndex,
) -> MultiQueryResult<'a> {
    // Query both indexes - compiler can potentially optimize this
    let gene_results = genes.query(interval);
    let exon_results = exons.query(interval);

    MultiQueryResult {
        genes: gene_results,
        exons: exon_results,
    }
}

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

    #[test]
    fn test_annotation_index() {
        let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        annotations
            .entry("chr1".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr1", 100, 200),
                data: "GeneA".to_string(),
            });

        annotations
            .entry("chr1".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr1", 150, 250),
                data: "GeneB".to_string(),
            });

        annotations
            .entry("chr2".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr2", 100, 200),
                data: "GeneC".to_string(),
            });

        let mut index = AnnotationIndex::new();
        index.build_trees(annotations);

        // Query overlapping interval
        let query = GenomicInterval::new("chr1", 175, 225);
        let results = index.query(&query);

        assert_eq!(results.len(), 2);
        assert!(results.contains(&"GeneA"));
        assert!(results.contains(&"GeneB"));

        // Query non-overlapping interval
        let query = GenomicInterval::new("chr1", 300, 400);
        let results = index.query(&query);
        assert_eq!(results.len(), 0);

        // Query different chromosome
        let query = GenomicInterval::new("chr2", 150, 250);
        let results = index.query(&query);
        assert_eq!(results.len(), 1);
        assert!(results.contains(&"GeneC"));
    }

    #[test]
    fn test_annotation_index_stats() {
        let mut annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        annotations
            .entry("chr1".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr1", 100, 200),
                data: "GeneA".to_string(),
            });

        annotations
            .entry("chr2".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr2", 100, 200),
                data: "GeneB".to_string(),
            });

        let mut index = AnnotationIndex::new();
        index.build_trees(annotations);

        assert!(!index.is_empty());
        assert_eq!(index.len(), 2);

        let chroms = index.chromosomes();
        assert_eq!(chroms.len(), 2);
        assert!(chroms.contains(&"chr1"));
        assert!(chroms.contains(&"chr2"));
    }

    #[test]
    fn test_multi_query() {
        let mut gene_annotations: HashMap<String, Vec<Annotation>> = HashMap::new();
        let mut exon_annotations: HashMap<String, Vec<Annotation>> = HashMap::new();

        gene_annotations
            .entry("chr1".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr1", 100, 500),
                data: "GeneA".to_string(),
            });

        exon_annotations
            .entry("chr1".to_string())
            .or_insert_with(Vec::new)
            .push(Annotation {
                interval: GenomicInterval::new("chr1", 150, 200),
                data: "ExonA1".to_string(),
            });

        let mut genes = AnnotationIndex::new();
        genes.build_trees(gene_annotations);

        let mut exons = AnnotationIndex::new();
        exons.build_trees(exon_annotations);

        // Query both simultaneously
        let query = GenomicInterval::new("chr1", 175, 225);
        let results = multi_query(&query, &genes, &exons);

        assert_eq!(results.genes.len(), 1);
        assert_eq!(results.exons.len(), 1);
        assert_eq!(results.genes[0], "GeneA");
        assert_eq!(results.exons[0], "ExonA1");
    }
}