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
//! Schema system for genomic data formats
//!
//! This module provides metadata about the structure of different genomic formats,
//! enabling introspection, validation, and type-safe query building.

use std::collections::HashMap;

// ============================================================================
// Core Schema Types
// ============================================================================

/// Schema for a genomic data format
///
/// Describes the columns/fields available in a format and their types.
#[derive(Debug, Clone)]
pub struct GenomicSchema {
    /// The file format this schema describes
    pub format: FileFormat,

    /// Columns in this format
    pub columns: Vec<ColumnDef>,

    /// Quick lookup by column name
    column_map: HashMap<String, usize>,
}

/// Definition of a single column/field
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDef {
    /// Column name (e.g., "qual", "chrom", "pos")
    pub name: String,

    /// Data type of this column
    pub dtype: DataType,

    /// Optional genomic-specific type information
    pub genomic_type: Option<GenomicType>,

    /// Whether this column is nullable
    pub nullable: bool,

    /// Human-readable description
    pub description: String,
}

/// Supported file formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FileFormat {
    Vcf,
    Bam,
    Sam,
    Bed,
    Fastq,
    Fasta,
    Gff,
}

/// Generic data types
#[derive(Debug, Clone, PartialEq)]
pub enum DataType {
    /// Boolean (true/false)
    Boolean,

    /// 32-bit integer
    Int32,

    /// 64-bit integer
    Int64,

    /// 32-bit float
    Float32,

    /// 64-bit float
    Float64,

    /// UTF-8 string
    String,

    /// List of values
    List(Box<DataType>),

    /// Struct with named fields
    Struct(Vec<ColumnDef>),
}

/// Genomic-specific type information
///
/// Provides semantic meaning to columns for genomic operations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenomicType {
    /// Chromosome/contig name
    Chromosome,

    /// Genomic position (1-based)
    Position,

    /// Quality score (Phred-scaled)
    Quality,

    /// Reference allele
    ReferenceAllele,

    /// Alternate allele(s)
    AlternateAllele,

    /// Filter status (PASS, FAIL, etc.)
    Filter,

    /// Strand (+/-)
    Strand,

    /// Mapping quality (BAM)
    MappingQuality,

    /// CIGAR string (BAM)
    Cigar,

    /// DNA sequence
    Sequence,

    /// Base quality scores
    BaseQuality,
}

// ============================================================================
// Schema Implementation
// ============================================================================

impl GenomicSchema {
    /// Create a new schema
    pub fn new(format: FileFormat, columns: Vec<ColumnDef>) -> Self {
        let mut column_map = HashMap::new();
        for (idx, col) in columns.iter().enumerate() {
            column_map.insert(col.name.clone(), idx);
        }

        Self {
            format,
            columns,
            column_map,
        }
    }

    /// Get column by name
    pub fn column(&self, name: &str) -> Option<&ColumnDef> {
        self.column_map.get(name).map(|&idx| &self.columns[idx])
    }

    /// Check if column exists
    pub fn has_column(&self, name: &str) -> bool {
        self.column_map.contains_key(name)
    }

    /// Get all column names
    pub fn column_names(&self) -> Vec<&str> {
        self.columns.iter().map(|c| c.name.as_str()).collect()
    }
}

// ============================================================================
// Format-Specific Schemas
// ============================================================================

impl GenomicSchema {
    /// VCF format schema
    pub fn vcf() -> Self {
        Self::new(
            FileFormat::Vcf,
            vec![
                ColumnDef {
                    name: "chrom".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Chromosome),
                    nullable: false,
                    description: "Chromosome name".to_string(),
                },
                ColumnDef {
                    name: "pos".to_string(),
                    dtype: DataType::Int64,
                    genomic_type: Some(GenomicType::Position),
                    nullable: false,
                    description: "1-based position".to_string(),
                },
                ColumnDef {
                    name: "id".to_string(),
                    dtype: DataType::String,
                    genomic_type: None,
                    nullable: true,
                    description: "Variant ID".to_string(),
                },
                ColumnDef {
                    name: "ref".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::ReferenceAllele),
                    nullable: false,
                    description: "Reference allele".to_string(),
                },
                ColumnDef {
                    name: "alt".to_string(),
                    dtype: DataType::List(Box::new(DataType::String)),
                    genomic_type: Some(GenomicType::AlternateAllele),
                    nullable: false,
                    description: "Alternate allele(s)".to_string(),
                },
                ColumnDef {
                    name: "qual".to_string(),
                    dtype: DataType::Float64,
                    genomic_type: Some(GenomicType::Quality),
                    nullable: true,
                    description: "Quality score (Phred-scaled)".to_string(),
                },
                ColumnDef {
                    name: "filter".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Filter),
                    nullable: false,
                    description: "Filter status (PASS, FAIL, etc.)".to_string(),
                },
            ],
        )
    }

    /// BAM format schema
    pub fn bam() -> Self {
        Self::new(
            FileFormat::Bam,
            vec![
                ColumnDef {
                    name: "qname".to_string(),
                    dtype: DataType::String,
                    genomic_type: None,
                    nullable: false,
                    description: "Query name".to_string(),
                },
                ColumnDef {
                    name: "flag".to_string(),
                    dtype: DataType::Int32,
                    genomic_type: None,
                    nullable: false,
                    description: "Bitwise flags".to_string(),
                },
                ColumnDef {
                    name: "rname".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Chromosome),
                    nullable: true,
                    description: "Reference sequence name".to_string(),
                },
                ColumnDef {
                    name: "pos".to_string(),
                    dtype: DataType::Int64,
                    genomic_type: Some(GenomicType::Position),
                    nullable: false,
                    description: "1-based leftmost position".to_string(),
                },
                ColumnDef {
                    name: "mapq".to_string(),
                    dtype: DataType::Int32,
                    genomic_type: Some(GenomicType::MappingQuality),
                    nullable: false,
                    description: "Mapping quality".to_string(),
                },
                ColumnDef {
                    name: "cigar".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Cigar),
                    nullable: true,
                    description: "CIGAR string".to_string(),
                },
                ColumnDef {
                    name: "seq".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Sequence),
                    nullable: false,
                    description: "Read sequence".to_string(),
                },
                ColumnDef {
                    name: "qual".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::BaseQuality),
                    nullable: true,
                    description: "Base quality scores".to_string(),
                },
            ],
        )
    }

    /// BED format schema
    pub fn bed() -> Self {
        Self::new(
            FileFormat::Bed,
            vec![
                ColumnDef {
                    name: "chrom".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Chromosome),
                    nullable: false,
                    description: "Chromosome name".to_string(),
                },
                ColumnDef {
                    name: "start".to_string(),
                    dtype: DataType::Int64,
                    genomic_type: Some(GenomicType::Position),
                    nullable: false,
                    description: "0-based start position".to_string(),
                },
                ColumnDef {
                    name: "end".to_string(),
                    dtype: DataType::Int64,
                    genomic_type: Some(GenomicType::Position),
                    nullable: false,
                    description: "End position (exclusive)".to_string(),
                },
                ColumnDef {
                    name: "name".to_string(),
                    dtype: DataType::String,
                    genomic_type: None,
                    nullable: true,
                    description: "Feature name".to_string(),
                },
                ColumnDef {
                    name: "score".to_string(),
                    dtype: DataType::Float64,
                    genomic_type: None,
                    nullable: true,
                    description: "Score (0-1000)".to_string(),
                },
                ColumnDef {
                    name: "strand".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Strand),
                    nullable: true,
                    description: "Strand (+/-)".to_string(),
                },
            ],
        )
    }

    /// FASTQ format schema
    pub fn fastq() -> Self {
        Self::new(
            FileFormat::Fastq,
            vec![
                ColumnDef {
                    name: "id".to_string(),
                    dtype: DataType::String,
                    genomic_type: None,
                    nullable: false,
                    description: "Read identifier".to_string(),
                },
                ColumnDef {
                    name: "sequence".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::Sequence),
                    nullable: false,
                    description: "Read sequence".to_string(),
                },
                ColumnDef {
                    name: "quality".to_string(),
                    dtype: DataType::String,
                    genomic_type: Some(GenomicType::BaseQuality),
                    nullable: false,
                    description: "Base quality scores (Phred+33)".to_string(),
                },
            ],
        )
    }
}

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

impl FileFormat {
    /// Get the default schema for this format
    pub fn schema(&self) -> GenomicSchema {
        match self {
            FileFormat::Vcf => GenomicSchema::vcf(),
            FileFormat::Bam | FileFormat::Sam => GenomicSchema::bam(),
            FileFormat::Bed => GenomicSchema::bed(),
            FileFormat::Fastq => GenomicSchema::fastq(),
            FileFormat::Fasta => {
                // Minimal FASTA schema
                GenomicSchema::new(
                    FileFormat::Fasta,
                    vec![
                        ColumnDef {
                            name: "id".to_string(),
                            dtype: DataType::String,
                            genomic_type: None,
                            nullable: false,
                            description: "Sequence identifier".to_string(),
                        },
                        ColumnDef {
                            name: "sequence".to_string(),
                            dtype: DataType::String,
                            genomic_type: Some(GenomicType::Sequence),
                            nullable: false,
                            description: "Sequence".to_string(),
                        },
                    ],
                )
            }
            FileFormat::Gff => {
                // GFF schema
                GenomicSchema::new(
                    FileFormat::Gff,
                    vec![
                        ColumnDef {
                            name: "seqid".to_string(),
                            dtype: DataType::String,
                            genomic_type: Some(GenomicType::Chromosome),
                            nullable: false,
                            description: "Sequence ID".to_string(),
                        },
                        ColumnDef {
                            name: "source".to_string(),
                            dtype: DataType::String,
                            genomic_type: None,
                            nullable: true,
                            description: "Source".to_string(),
                        },
                        ColumnDef {
                            name: "type".to_string(),
                            dtype: DataType::String,
                            genomic_type: None,
                            nullable: false,
                            description: "Feature type".to_string(),
                        },
                        ColumnDef {
                            name: "start".to_string(),
                            dtype: DataType::Int64,
                            genomic_type: Some(GenomicType::Position),
                            nullable: false,
                            description: "1-based start position".to_string(),
                        },
                        ColumnDef {
                            name: "end".to_string(),
                            dtype: DataType::Int64,
                            genomic_type: Some(GenomicType::Position),
                            nullable: false,
                            description: "End position (inclusive)".to_string(),
                        },
                        ColumnDef {
                            name: "score".to_string(),
                            dtype: DataType::Float64,
                            genomic_type: None,
                            nullable: true,
                            description: "Score".to_string(),
                        },
                        ColumnDef {
                            name: "strand".to_string(),
                            dtype: DataType::String,
                            genomic_type: Some(GenomicType::Strand),
                            nullable: true,
                            description: "Strand (+/-/.)".to_string(),
                        },
                    ],
                )
            }
        }
    }
}

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

    #[test]
    fn test_vcf_schema() {
        let schema = GenomicSchema::vcf();
        assert_eq!(schema.format, FileFormat::Vcf);
        assert!(schema.has_column("chrom"));
        assert!(schema.has_column("qual"));
        assert!(!schema.has_column("invalid"));

        let qual_col = schema.column("qual").unwrap();
        assert_eq!(qual_col.dtype, DataType::Float64);
        assert_eq!(qual_col.genomic_type, Some(GenomicType::Quality));
    }

    #[test]
    fn test_bam_schema() {
        let schema = GenomicSchema::bam();
        assert_eq!(schema.format, FileFormat::Bam);
        assert!(schema.has_column("mapq"));

        let mapq_col = schema.column("mapq").unwrap();
        assert_eq!(mapq_col.genomic_type, Some(GenomicType::MappingQuality));
    }

    #[test]
    fn test_bed_schema() {
        let schema = GenomicSchema::bed();
        assert_eq!(schema.column_names().len(), 6);
        assert!(schema.has_column("chrom"));
        assert!(schema.has_column("start"));
        assert!(schema.has_column("end"));
    }

    #[test]
    fn test_fastq_schema() {
        let schema = GenomicSchema::fastq();
        assert_eq!(schema.columns.len(), 3);

        let seq_col = schema.column("sequence").unwrap();
        assert_eq!(seq_col.genomic_type, Some(GenomicType::Sequence));
    }

    #[test]
    fn test_format_schema_method() {
        let vcf_schema = FileFormat::Vcf.schema();
        assert_eq!(vcf_schema.format, FileFormat::Vcf);

        let bam_schema = FileFormat::Bam.schema();
        assert_eq!(bam_schema.format, FileFormat::Bam);
    }
}