rvdna 0.3.0

rvDNA — AI-native genomic analysis. 20-SNP biomarker risk scoring, streaming anomaly detection, 64-dim profile vectors, 23andMe genotyping, CYP2D6/CYP2C19 pharmacogenomics, variant calling, protein prediction, and HNSW vector search in pure 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
# Bounded Context Map - Genomic Analysis Platform

## Context Map Overview

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                         GENOMIC ANALYSIS PLATFORM                            │
└─────────────────────────────────────────────────────────────────────────────┘

    ┌──────────────────┐
    │   Pipeline       │ ◄───────── Orchestration Layer
    │   Context        │
    └────────┬─────────┘
             │ ACL (maps domain events to pipeline commands)
    ┌────────┴─────────────────────────────────────────────┐
    │                                                       │
    ▼                                                       ▼
┌─────────────────┐                               ┌─────────────────┐
│   Sequence      │ Customer-Supplier             │   Alignment     │
│   Context       ├──────────────────────────────►│   Context       │
│                 │ (provides k-mer indices)      │                 │
└────────┬────────┘                               └────────┬────────┘
         │                                                 │
         │ Shared Kernel (GenomicPosition, QualityScore)  │
         │                                                 │
         ▼                                                 ▼
┌─────────────────┐                               ┌─────────────────┐
│   Variant       │                               │   Protein       │
│   Context       │◄──────────────────────────────┤   Context       │
│                 │  Partner (variant→structure)  │                 │
└────────┬────────┘                               └─────────────────┘
         │ ACL (translates variants to epigenetic events)
┌─────────────────┐
│  Epigenomic     │
│  Context        │
└────────┬────────┘
         │ Customer-Supplier (epigenetic→drug response)
┌─────────────────┐
│ Pharmacogenomic │
│ Context         │
└─────────────────┘

Legend:
  Customer-Supplier: →  (upstream provides services to downstream)
  Shared Kernel:     ├─┤ (shared domain model)
  Partner:           ◄─► (mutual dependency)
  ACL:               [A] (anti-corruption layer)
```

## 1. Sequence Context

**Module**: `kmer.rs`

**Responsibility**: K-mer indexing, sequence sketching, and similarity search

**Core Aggregates**:
- `KmerIndex` - Root aggregate managing k-mer → position mappings
- `MinHashSketch` - Aggregate for approximate sequence similarity

**Key Types**:
```rust
pub struct KmerEncoder {
    k: usize,
    alphabet_size: usize,
}

pub struct KmerIndex {
    k: usize,
    index: HashMap<u64, Vec<usize>>, // k-mer hash → positions
}

pub struct MinHashSketch {
    k: usize,
    num_hashes: usize,
    signatures: Vec<u64>,
}
```

**Published Events**:
- `SequenceIndexed { sequence_id: String, kmer_count: usize }`
- `SimilarSequenceFound { query_id: String, match_id: String, similarity: f64 }`

**Domain Language**:
- K-mer: substring of length k
- Minimizer: canonical k-mer representation
- Sketch: compressed sequence signature
- Jaccard similarity: set overlap metric

**Invariants**:
- K-mer length must be 3 ≤ k ≤ 32
- MinHash signature size must be ≥ 1
- All k-mers normalized to canonical form (min(kmer, reverse_complement))

## 2. Alignment Context

**Module**: `alignment.rs`

**Responsibility**: Sequence alignment using attention mechanisms and motif detection

**Core Aggregates**:
- `AttentionAligner` - Root aggregate for pairwise sequence alignment
- `MotifScanner` - Aggregate for regulatory motif discovery

**Key Types**:
```rust
pub struct AttentionAligner {
    attention_service: Arc<AttentionService>,
    gap_penalty: f64,
    match_bonus: f64,
}

pub struct MotifScanner {
    attention_service: Arc<AttentionService>,
    min_score: f64,
    known_motifs: Vec<MotifPattern>,
}

pub struct AlignmentResult {
    pub score: f64,
    pub aligned_query: String,
    pub aligned_target: String,
    pub attention_weights: Vec<Vec<f64>>,
}
```

**Published Events**:
- `AlignmentCompleted { query_id: String, target_id: String, score: f64 }`
- `MotifDetected { sequence_id: String, motif: String, position: usize, score: f64 }`

**Domain Language**:
- Alignment: optimal mapping between two sequences
- Gap penalty: cost of insertions/deletions
- Attention weight: learned similarity between positions
- Motif: conserved sequence pattern (e.g., TATA box)
- PWM (Position Weight Matrix): motif scoring matrix

**Invariants**:
- Gap penalty must be negative
- Match bonus must be positive
- Motif minimum score 0.0 ≤ score ≤ 1.0
- Alignment score monotonically decreases with gaps

**Relationship with Sequence Context**:
- **Type**: Customer-Supplier
- **Direction**: Sequence → Alignment
- **Integration**: Alignment consumes k-mer indices for fast seed-and-extend
- **Translation**: None (direct dependency)

## 3. Variant Context

**Module**: `variant.rs`

**Responsibility**: Variant calling, genotyping, and population genetics

**Core Aggregates**:
- `VariantDatabase` - Root aggregate managing variant collection
- `VariantCaller` - Service aggregate for variant detection

**Key Types**:
```rust
pub struct VariantCaller {
    min_quality: f64,
    min_depth: usize,
    gnn_service: Arc<GnnService>,
}

pub struct Variant {
    pub position: GenomicPosition,
    pub reference: String,
    pub alternate: String,
    pub quality: f64,
    pub genotype: Genotype,
    pub depth: usize,
    pub allele_frequency: Option<f64>,
}

pub struct VariantDatabase {
    variants: HashMap<GenomicPosition, Variant>,
    graph_index: Option<GraphIndex>, // GNN-based variant relationships
}

pub enum Genotype {
    Homozygous(Allele),
    Heterozygous(Allele, Allele),
}
```

**Published Events**:
- `VariantCalled { position: GenomicPosition, variant: Variant }`
- `GenotypeUpdated { sample_id: String, position: GenomicPosition, genotype: Genotype }`
- `PopulationFrequencyCalculated { variant_id: String, frequency: f64 }`

**Domain Language**:
- SNP (Single Nucleotide Polymorphism): single base change
- Indel: insertion or deletion
- Genotype: allele combination (0/0, 0/1, 1/1)
- Allele frequency: population prevalence
- Quality score: confidence in variant call (Phred scale)
- Coverage depth: number of reads supporting variant

**Invariants**:
- Quality score ≥ 0 (Phred scale)
- Coverage depth ≥ 1
- Allele frequency 0.0 ≤ AF ≤ 1.0
- Reference and alternate alleles must differ
- Genotype alleles must match available alleles

**Relationship with Alignment Context**:
- **Type**: Customer-Supplier
- **Direction**: Alignment → Variant
- **Integration**: Variant caller uses alignment results to identify mismatches
- **Translation**: Alignment gaps → insertion/deletion variants

**Shared Kernel with Sequence Context**:
- `GenomicPosition { chromosome: String, position: usize }`
- `QualityScore(f64)` (Phred-scaled)
- `Nucleotide` enum (A, C, G, T)

## 4. Protein Context

**Module**: `protein.rs`

**Responsibility**: Protein structure prediction and contact map generation

**Core Aggregates**:
- `ProteinGraph` - Root aggregate representing protein as graph
- `ContactPredictor` - Service aggregate for 3D contact prediction

**Key Types**:
```rust
pub struct ProteinGraph {
    pub sequence: String, // amino acid sequence
    pub nodes: Vec<AminoAcid>,
    pub edges: Vec<(usize, usize, ContactType)>,
}

pub struct ContactPredictor {
    gnn_service: Arc<GnnService>,
    attention_service: Arc<AttentionService>,
    distance_threshold: f64, // Ångströms
}

pub struct ContactPrediction {
    pub residue_i: usize,
    pub residue_j: usize,
    pub probability: f64,
    pub distance: Option<f64>,
}

pub enum ContactType {
    Backbone,
    SideChain,
    HydrogenBond,
    DisulfideBridge,
}
```

**Published Events**:
- `ProteinTranslated { gene_id: String, protein_sequence: String }`
- `StructurePredicted { protein_id: String, contact_count: usize }`
- `FoldingPathwayComputed { protein_id: String, energy: f64 }`

**Domain Language**:
- Amino acid: protein building block (20 standard types)
- Residue: amino acid position in sequence
- Contact: spatial proximity between residues (<8Å)
- Secondary structure: local folding patterns (helix, sheet, loop)
- Tertiary structure: 3D protein fold
- Contact map: matrix of residue-residue distances

**Invariants**:
- Sequence length ≥ 1
- Contact probability 0.0 ≤ p ≤ 1.0
- Distance threshold > 0.0 (typically 8.0Å)
- Contact pairs must be |i - j| ≥ 4 (exclude local contacts)

**Relationship with Variant Context**:
- **Type**: Partner (bidirectional)
- **Direction**: Variant ↔ Protein
- **Integration**:
  - Variant → Protein: coding variants cause amino acid changes
  - Protein → Variant: structural changes inform variant pathogenicity
- **Translation**:
  - Variant ACL translates nucleotide changes to codon changes
  - Protein ACL maps structure disruption to clinical significance

## 5. Epigenomic Context

**Module**: `epigenomics.rs`

**Responsibility**: DNA methylation analysis and epigenetic age prediction

**Core Aggregates**:
- `EpigeneticIndex` - Root aggregate managing methylation sites
- `HorvathClock` - Service aggregate for epigenetic age calculation

**Key Types**:
```rust
pub struct MethylationProfile {
    pub cpg_sites: HashMap<GenomicPosition, f64>, // position → beta value
    pub total_sites: usize,
    pub mean_methylation: f64,
}

pub struct HorvathClock {
    pub coefficients: HashMap<String, f64>, // CpG site → weight
    pub intercept: f64,
}

pub struct CpGSite {
    pub position: GenomicPosition,
    pub beta_value: f64, // 0.0 (unmethylated) to 1.0 (methylated)
    pub coverage: usize,
}

pub struct EpigeneticAge {
    pub chronological_age: Option<f64>,
    pub predicted_age: f64,
    pub acceleration: f64, // predicted - chronological
}
```

**Published Events**:
- `MethylationProfileGenerated { sample_id: String, site_count: usize }`
- `EpigeneticAgeCalculated { sample_id: String, age: f64, acceleration: f64 }`
- `DifferentialMethylationDetected { region: GenomicRegion, delta_beta: f64 }`

**Domain Language**:
- CpG site: cytosine-guanine dinucleotide (methylation target)
- Beta value: methylation level (0 = unmethylated, 1 = fully methylated)
- Epigenetic clock: age predictor based on methylation
- Age acceleration: difference between epigenetic and chronological age
- DMR (Differentially Methylated Region): region with changed methylation

**Invariants**:
- Beta value 0.0 ≤ β ≤ 1.0
- Coverage ≥ 1
- Horvath coefficients sum to meaningful scale
- Age ≥ 0.0

**Relationship with Variant Context**:
- **Type**: Anti-Corruption Layer
- **Direction**: Variant → Epigenomic
- **Integration**: Variants in regulatory regions affect methylation patterns
- **Translation**:
  - ACL translates genetic variants to epigenetic effects
  - Maps SNPs → methylation quantitative trait loci (mQTL)
  - Prevents variant domain concepts from leaking into epigenetic model

## 6. Pharmacogenomic Context

**Module**: `pharma.rs`

**Responsibility**: Pharmacogenetic analysis and drug-gene interaction prediction

**Core Aggregates**:
- `DrugInteractionGraph` - Root aggregate representing drug-gene network
- `StarAlleleCaller` - Service aggregate for haplotype phasing

**Key Types**:
```rust
pub struct StarAlleleCaller {
    gene_definitions: HashMap<String, GeneDefinition>,
    min_coverage: usize,
}

pub struct StarAllele {
    pub gene: String,
    pub allele: String, // e.g., "*1", "*2", "*17"
    pub variants: Vec<Variant>,
    pub function: AlleleFunction,
}

pub enum AlleleFunction {
    Normal,
    Increased,
    Decreased,
    NoFunction,
}

pub struct DrugInteractionGraph {
    pub nodes: Vec<DrugGeneNode>,
    pub edges: Vec<(usize, usize, InteractionType)>,
}

pub struct DrugResponse {
    pub drug: String,
    pub diplotype: Diplotype,
    pub phenotype: MetabolizerPhenotype,
    pub recommendation: ClinicalRecommendation,
}

pub enum MetabolizerPhenotype {
    UltraRapid,
    Rapid,
    Normal,
    Intermediate,
    Poor,
}
```

**Published Events**:
- `StarAlleleIdentified { gene: String, allele: String, diplotype: String }`
- `DrugResponsePredicted { drug: String, phenotype: MetabolizerPhenotype }`
- `InteractionDetected { drug1: String, drug2: String, severity: Severity }`

**Domain Language**:
- Star allele: named haplotype variant (e.g., CYP2D6*4)
- Diplotype: pair of haplotypes (e.g., *1/*4)
- Metabolizer phenotype: drug metabolism rate
- Pharmacogene: gene affecting drug response
- Drug-gene interaction: how genetics modulates drug efficacy/toxicity

**Invariants**:
- Diplotype must have exactly 2 alleles
- Phenotype derivable from diplotype
- Coverage ≥ minimum threshold for calling
- All star allele variants must exist in variant database

**Relationship with Epigenomic Context**:
- **Type**: Customer-Supplier
- **Direction**: Epigenomic → Pharmacogenomic
- **Integration**: Methylation affects drug metabolism gene expression
- **Translation**: Methylation beta values → gene expression levels → phenotype

## 7. Pipeline Context

**Module**: `pipeline.rs`

**Responsibility**: Orchestration of multi-stage genomic analysis workflow

**Core Aggregates**:
- `GenomicPipeline` - Root aggregate orchestrating all contexts

**Key Types**:
```rust
pub struct GenomicPipeline {
    pub kmer_encoder: KmerEncoder,
    pub aligner: AttentionAligner,
    pub variant_caller: VariantCaller,
    pub protein_predictor: ContactPredictor,
    pub methylation_analyzer: MethylationAnalyzer,
    pub pharma_analyzer: StarAlleleCaller,
}

pub struct PipelineConfig {
    pub k: usize,
    pub min_variant_quality: f64,
    pub min_coverage: usize,
    pub enable_protein_prediction: bool,
    pub enable_epigenetic_analysis: bool,
    pub enable_pharmacogenomics: bool,
}

pub struct AnalysisResult {
    pub sequence_stats: SequenceStats,
    pub variants: Vec<Variant>,
    pub protein_structures: Vec<ProteinGraph>,
    pub methylation_profile: Option<MethylationProfile>,
    pub drug_responses: Vec<DrugResponse>,
}
```

**Published Events**:
- `PipelineStarted { sample_id: String, stages: Vec<String> }`
- `StageCompleted { stage: String, duration_ms: u64 }`
- `PipelineCompleted { sample_id: String, total_duration_ms: u64 }`
- `PipelineFailed { stage: String, error: String }`

**Domain Language**:
- Pipeline: directed acyclic graph of analysis stages
- Stage: atomic analysis unit (alignment, variant calling, etc.)
- Workflow: ordered execution of stages
- Checkpoint: saved intermediate state
- Provenance: lineage tracking of analysis steps

**Invariants**:
- All enabled stages must execute in dependency order
- Failed stage halts downstream execution
- All results traceable to input data and parameters

**Anti-Corruption Layers**:

The Pipeline Context uses ACLs to prevent downstream contexts from depending on upstream implementation details:

1. **Sequence ACL**: Translates k-mer indices to alignment seeds
2. **Alignment ACL**: Converts alignment gaps to variant candidates
3. **Variant ACL**: Maps variants to protein mutations
4. **Protein ACL**: Translates structure to functional predictions
5. **Epigenetic ACL**: Converts methylation to gene expression estimates
6. **Pharmacogenomic ACL**: Maps genotypes to clinical recommendations

## Context Relationship Matrix

| From ↓ / To → | Sequence | Alignment | Variant | Protein | Epigenomic | Pharma | Pipeline |
|---------------|----------|-----------|---------|---------|------------|--------|----------|
| Sequence      | -        | C-S       | SK      | SK      | -          | -      | ACL      |
| Alignment     | -        | -         | C-S     | -       | -          | -      | ACL      |
| Variant       | -        | -         | -       | Partner | ACL        | -      | ACL      |
| Protein       | -        | -         | Partner | -       | -          | -      | ACL      |
| Epigenomic    | -        | -         | -       | -       | -          | C-S    | ACL      |
| Pharma        | -        | -         | -       | -       | -          | -      | ACL      |
| Pipeline      | C-S      | C-S       | C-S     | C-S     | C-S        | C-S    | -        |

**Legend**:
- C-S: Customer-Supplier
- SK: Shared Kernel
- Partner: Partnership
- ACL: Anti-Corruption Layer

## Integration Patterns

### 1. Event-Driven Integration

Contexts communicate via domain events to maintain loose coupling:

```rust
// Example: Variant Context publishes event
pub enum DomainEvent {
    VariantCalled(VariantCalledEvent),
    ProteinStructurePredicted(ProteinPredictedEvent),
    // ...
}

// Pipeline Context subscribes and translates
impl EventHandler for GenomicPipeline {
    fn handle(&mut self, event: DomainEvent) {
        match event {
            DomainEvent::VariantCalled(e) => {
                if e.variant.is_coding() {
                    self.trigger_protein_analysis(e.variant);
                }
            }
            // ...
        }
    }
}
```

### 2. Shared Kernel Components

Core domain types shared across contexts:

```rust
// In types.rs (core domain)
pub struct GenomicPosition {
    pub chromosome: String,
    pub position: usize,
}

pub struct QualityScore(pub f64); // Phred-scaled

pub enum Nucleotide { A, C, G, T }

pub struct GenomicRegion {
    pub chromosome: String,
    pub start: usize,
    pub end: usize,
}
```

### 3. Anti-Corruption Layer Example

```rust
// Variant → Protein ACL
pub struct VariantToProteinTranslator {
    codon_table: CodonTable,
}

impl VariantToProteinTranslator {
    pub fn translate_variant(&self, variant: &Variant) -> Option<ProteinMutation> {
        // Prevents protein context from depending on variant implementation
        let codon_change = self.map_to_codon(variant)?;
        let aa_change = self.codon_table.translate(codon_change)?;

        Some(ProteinMutation {
            position: variant.position.position / 3,
            reference_aa: aa_change.reference,
            alternate_aa: aa_change.alternate,
        })
    }
}
```

## Bounded Context Responsibilities Summary

1. **Sequence Context**: K-mer indexing and sequence similarity (foundation)
2. **Alignment Context**: Pairwise alignment and motif discovery
3. **Variant Context**: Variant calling and population genetics
4. **Protein Context**: Structure prediction and functional analysis
5. **Epigenomic Context**: Methylation profiling and age prediction
6. **Pharmacogenomic Context**: Drug-gene interactions and clinical recommendations
7. **Pipeline Context**: Workflow orchestration and result aggregation

Each context maintains its own ubiquitous language, domain model, and business rules while integrating through well-defined relationships.