convert_genome 0.1.10

Convert DTC, VCF, or BCF genome files to VCF, BCF, or PLINK 1.9
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
use noodles::vcf::variant::record_buf::samples::sample::Value;
use noodles::vcf::variant::record_buf::{AlternateBases, RecordBuf, Samples, samples::Keys};
use std::io;
use tracing;

use crate::conversion::{ConversionConfig, Ploidy, determine_ploidy, format_genotype};
use crate::dtc::{self, Allele as DtcAllele, Record as DtcRecord};
use crate::external_sort::{
    DtcExternalSorter, DtcOrder, RecordExternalSorter, RecordOrder, SortFormat, SortedRecordIter,
};
use crate::reference::ReferenceGenome;
use clap::ValueEnum;

/// Get optional max records limit from environment variable.
/// Set `CONVERT_GENOME_MAX_RECORDS=N` to limit buffered records (for OOM protection).
pub fn get_max_records_limit() -> Option<usize> {
    std::env::var("CONVERT_GENOME_MAX_RECORDS")
        .ok()
        .and_then(|s| s.parse().ok())
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, ValueEnum)]
pub enum InputFormat {
    /// Direct-to-consumer text output (23andMe, etc.)
    Dtc,
    /// Variant Call Format
    Vcf,
    /// Binary Call Format
    Bcf,
    /// Detect format automatically
    Auto,
}

impl InputFormat {
    pub fn detect(path: &std::path::Path) -> Self {
        use std::io::BufRead;

        // Use smart reader to peel off compression layers (GZIP, ZIP, etc.)
        if let Ok(mut reader) = crate::smart_reader::open_input(path) {
            // Peek at the beginning of the stream
            if let Ok(buf) = reader.fill_buf() {
                if buf.len() >= 3 && &buf[..3] == b"BCF" {
                    return Self::Bcf;
                }
                // VCF files typically start with '#' (0x23) for header
                if !buf.is_empty() && buf[0] == b'#' {
                    // It could be a comment in DTC, but usually VCF starts with ##fileformat=VCF
                    // We only confidently return VCF if we see the strict header.
                    if buf.starts_with(b"##fileformat=VCF") {
                        return Self::Vcf;
                    }
                    // If it starts with '#' but not the strict header, it might be DTC with comments.
                    // We fall through to extension check or DTC default.
                }
            }
        }

        // Fallback: Check file extension if content detection failed or ambiguous
        // (Though smart_reader failure usually means file error)
        if let Some(filename) = path.file_name().map(|n| n.to_string_lossy().to_lowercase()) {
            if filename.ends_with(".vcf.gz") || filename.ends_with(".vcf") {
                return Self::Vcf;
            } else if filename.ends_with(".bcf") || filename.ends_with(".bcf.gz") {
                return Self::Bcf;
            }
        }

        Self::Dtc // Default fallback for text formats
    }
}

/// Trait for a source of genomic variants.
/// Yields `RecordBuf` (generic VCF record wrapper).
pub trait VariantSource {
    fn next_variant(
        &mut self,
        summary: &mut crate::ConversionSummary,
    ) -> Option<io::Result<RecordBuf>>;
}

impl<T: VariantSource + ?Sized> VariantSource for Box<T> {
    fn next_variant(
        &mut self,
        summary: &mut crate::ConversionSummary,
    ) -> Option<io::Result<RecordBuf>> {
        (**self).next_variant(summary)
    }
}

/// Adapter for reading DTC (23andMe) files.
/// Buffers and sorts raw records to ensure cache locality and minimize memory usage.
pub struct DtcSource {
    records: crate::external_sort::DtcSortedIter,
    reference: Option<ReferenceGenome>,
    config: ConversionConfig,
    header_keys: Keys,
    initial_parse_errors: usize,
    initial_stats_synced: bool,
    inferred_strand: crate::source_ref::InferredStrand,
}

impl DtcSource {
    pub fn new<R: std::io::BufRead>(
        reader: dtc::Reader<R>,
        reference: Option<ReferenceGenome>,
        config: ConversionConfig,
        inferred_strand: Option<crate::source_ref::InferredStrand>,
    ) -> io::Result<Self> {
        let keys: Keys = vec![String::from("GT")].into_iter().collect();

        // Get optional limit from environment
        let max_records = get_max_records_limit();

        let order = if let Some(ref r) = reference {
            DtcOrder::from_reference(r)
        } else {
            DtcOrder::Natural
        };
        let mut sorter = DtcExternalSorter::new(order);

        // Read records (with optional limit for OOM protection)
        let mut parse_errors = 0;
        let mut records_read = 0;

        for res in reader {
            if let Some(limit) = max_records
                && records_read >= limit
            {
                tracing::info!(limit, "reached max_records limit, stopping read");
                break;
            }
            match res {
                Ok(rec) => {
                    sorter.push(rec)?;
                    records_read += 1;
                }
                Err(e) => {
                    parse_errors += 1;
                    tracing::warn!("failed to read input line: {}", e);
                }
            }
        }

        let records = sorter.finish()?;

        Ok(Self {
            records,
            reference,
            config,
            header_keys: keys,
            initial_parse_errors: parse_errors,
            initial_stats_synced: false,
            inferred_strand: inferred_strand.unwrap_or(crate::source_ref::InferredStrand::Forward),
        })
    }

    fn convert_record(
        &self,
        record: DtcRecord,
        summary: &mut crate::ConversionSummary,
    ) -> io::Result<Option<RecordBuf>> {
        // Validation and normalization logic moved from conversion.rs

        let canonical_name = if let Some(ref r) = self.reference {
            if let Some(name) = r.resolve_contig_name(&record.chromosome) {
                name.to_string()
            } else {
                // Skipping unknown chromosome
                summary.unknown_chromosomes += 1;
                return Ok(None);
            }
        } else {
            // No reference: pass through chromosome name (normalized slightly)
            record.chromosome.clone()
        };

        // Validate position
        let position = record.position;

        // Use reference.base instead of get_context
        let reference_base = if let Some(ref r) = self.reference {
            match r.base(&canonical_name, position) {
                Ok(base) => base,
                Err(e) => {
                    summary.reference_failures += 1;
                    tracing::warn!(
                        "reference lookup failed for {}:{}: {}",
                        canonical_name,
                        position,
                        e
                    );
                    return Ok(None);
                }
            }
        } else {
            // No reference: use placeholder 'N'
            'N'
        };

        // Parse alleles
        let mut allele_states = match record.parse_alleles() {
            Ok(states) => states,
            Err(e) => {
                summary.invalid_genotypes += 1;
                tracing::warn!("invalid alleles for {}:{}: {}", canonical_name, position, e);
                return Ok(None);
            }
        };

        // Apply Strand Lock
        if self.inferred_strand == crate::source_ref::InferredStrand::Reverse {
            for allele in &mut allele_states {
                if let DtcAllele::Base(s) = allele {
                    *s = reverse_complement(s);
                }
            }
        }

        // Biological Ploidy
        let ploidy = determine_ploidy(
            &canonical_name,
            position,
            self.config.sex.unwrap_or(crate::cli::Sex::Unknown),
            self.config.par_boundaries.as_ref(),
        );

        // Normalize
        let normalized_alleles = match ploidy {
            Ploidy::Zero => {
                summary.missing_genotype_records += 1;
                return Ok(None);
            }
            Ploidy::Haploid => {
                let unique: Vec<_> = allele_states
                    .iter()
                    .filter(|&a| *a != DtcAllele::Missing)
                    .cloned()
                    .collect();

                let mut uniq_vals = unique.clone();
                uniq_vals.dedup();

                if uniq_vals.is_empty() {
                    vec![DtcAllele::Missing]
                } else if uniq_vals.len() == 1 {
                    vec![uniq_vals[0].clone()]
                } else {
                    vec![DtcAllele::Missing]
                }
            }
            Ploidy::Diploid => allele_states,
        };

        // Build Alt Bases
        let mut alt_bases = Vec::new();
        for allele in normalized_alleles.iter() {
            match allele {
                DtcAllele::Base(base) => {
                    let b = base.to_uppercase();
                    let r = reference_base.to_string().to_uppercase();
                    if b != r && !alt_bases.contains(&b) {
                        alt_bases.push(b);
                    }
                }
                DtcAllele::Deletion => {
                    let del = String::from("DEL");
                    if !alt_bases.contains(&del) {
                        alt_bases.push(del);
                    }
                }
                DtcAllele::Insertion => {
                    let ins = String::from("INS");
                    if !alt_bases.contains(&ins) {
                        alt_bases.push(ins);
                    }
                }
                DtcAllele::Missing => {}
            }
        }

        if alt_bases.is_empty() && !self.config.include_reference_sites {
            summary.skipped_reference_sites += 1;
            return Ok(None);
        }

        // Formatted Alts (add < > if needed)
        let formatted_alts: Vec<String> = alt_bases
            .iter()
            .map(|a| {
                if a == "DEL" || a == "INS" {
                    format!("<{}>", a)
                } else {
                    a.clone()
                }
            })
            .collect();

        let alternate_bases = AlternateBases::from(formatted_alts);

        // Genotype string
        let genotype_string = format_genotype(&normalized_alleles, reference_base, &alt_bases)
            .map_err(|e| {
                // Do not increment summary here if we return Err, process_records will count it as parse_error?
                // But wait, user noted double counting.
                // If we return Err, process_records increments parse_errors.
                // If we want to skip it as invalid_genotype, we should return Ok(None) and increment here.
                summary.invalid_genotypes += 1;
                io::Error::new(io::ErrorKind::InvalidData, e.to_string())
            });

        let genotype_string = match genotype_string {
            Ok(s) => s,
            Err(e) => {
                tracing::warn!("genotype formatting failed: {}", e);
                return Ok(None); // Skip, don't return Err to avoid double count
            }
        };

        // Build RecordBuf with IDs
        let ids = record.id.as_ref().map(|id| {
            [id.clone()]
                .into_iter()
                .collect::<noodles::vcf::variant::record_buf::Ids>()
        });

        let samples = Samples::new(
            self.header_keys.clone(),
            vec![vec![Some(Value::String(genotype_string))]],
        );

        // Build info section
        let mut info_map: noodles::vcf::variant::record_buf::Info = Default::default();

        let has_del = alt_bases.iter().any(|b| b == "DEL");
        let has_ins = alt_bases.iter().any(|b| b == "INS");

        if has_del || has_ins {
            // Mark as imprecise structural variant
            info_map.insert(
                String::from("IMPRECISE"),
                Some(noodles::vcf::variant::record_buf::info::field::Value::Flag),
            );
            // Set structural variant type
            let svtype = if has_del && has_ins {
                "COMPLEX"
            } else if has_del {
                "DEL"
            } else {
                "INS"
            };
            info_map.insert(
                String::from("SVTYPE"),
                Some(noodles::vcf::variant::record_buf::info::field::Value::String(svtype.into())),
            );
        }

        Ok(Some(
            RecordBuf::builder()
                .set_reference_sequence_name(canonical_name)
                .set_variant_start(noodles::core::Position::new(position as usize).ok_or(
                    io::Error::new(io::ErrorKind::InvalidData, "invalid position"),
                )?)
                .set_ids(ids.unwrap_or_default()) // Use unwrap_or_default if ids can be None
                .set_reference_bases(reference_base.to_string())
                .set_alternate_bases(alternate_bases)
                .set_info(info_map)
                .set_samples(samples)
                .build(),
        ))
    }
}

impl VariantSource for DtcSource {
    fn next_variant(
        &mut self,
        summary: &mut crate::ConversionSummary,
    ) -> Option<io::Result<RecordBuf>> {
        // Sync initial stats once
        if !self.initial_stats_synced {
            summary.parse_errors += self.initial_parse_errors;
            // Note: total_records should typically track *valid* or *attemped* records.
            // If we want total to include malformed lines, add them here:
            // summary.total_records += self.initial_parse_errors as u64;
            // The previous behavior was to count lines read.
            self.initial_stats_synced = true;
        }

        // We iterate over the pre-sorted raw records
        while let Some(record) = self.records.next() {
            summary.total_records += 1;
            match self.convert_record(record, summary) {
                Ok(Some(buf)) => return Some(Ok(buf)),
                Ok(None) => continue, // Filtered
                Err(e) => return Some(Err(e)),
            }
        }
        None
    }
}

// ============================================================================
// VCF Source Adapter
// ============================================================================

use noodles::vcf;
use std::io::BufRead;

/// Adapter for reading VCF files.
pub struct VcfSource {
    records: SortedRecordIter,
    initial_parse_errors: usize,
    initial_stats_synced: bool,
}

impl VcfSource {
    /// Create a new VcfSource using natural chromosome ordering (no reference required)
    pub fn new_without_reference<R: BufRead>(mut reader: vcf::io::Reader<R>) -> io::Result<Self> {
        let header = reader.read_header()?;
        let max_records = get_max_records_limit();
        let mut parse_errors = 0;
        let mut records_read = 0;
        let mut sorter =
            RecordExternalSorter::new(header.clone(), SortFormat::Vcf, RecordOrder::Natural);

        for result in reader.record_bufs(&header) {
            if let Some(limit) = max_records
                && records_read >= limit
            {
                tracing::info!(limit, "reached max_records limit, stopping read");
                break;
            }
            match result {
                Ok(record) => {
                    sorter.push(record)?;
                    records_read += 1;
                }
                Err(e) => {
                    parse_errors += 1;
                    tracing::warn!("failed to read VCF record: {}", e);
                }
            }
        }

        let records = sorter.finish()?;

        Ok(Self {
            records,
            initial_parse_errors: parse_errors,
            initial_stats_synced: false,
        })
    }

    pub fn new<R: BufRead>(
        mut reader: vcf::io::Reader<R>,
        reference: &ReferenceGenome,
    ) -> io::Result<Self> {
        let header = reader.read_header()?;
        let max_records = get_max_records_limit();
        let mut parse_errors = 0;
        let mut records_read = 0;
        let order = RecordOrder::from_reference(reference);
        let mut sorter = RecordExternalSorter::new(header.clone(), SortFormat::Vcf, order);

        for result in reader.record_bufs(&header) {
            if let Some(limit) = max_records
                && records_read >= limit
            {
                tracing::info!(limit, "reached max_records limit, stopping read");
                break;
            }
            match result {
                Ok(record) => {
                    sorter.push(record)?;
                    records_read += 1;
                }
                Err(e) => {
                    parse_errors += 1;
                    tracing::warn!("failed to read VCF record: {}", e);
                }
            }
        }

        let records = sorter.finish()?;

        Ok(Self {
            records,
            initial_parse_errors: parse_errors,
            initial_stats_synced: false,
        })
    }
}

impl VariantSource for VcfSource {
    fn next_variant(
        &mut self,
        summary: &mut crate::ConversionSummary,
    ) -> Option<io::Result<RecordBuf>> {
        if !self.initial_stats_synced {
            summary.parse_errors += self.initial_parse_errors;
            self.initial_stats_synced = true;
        }

        self.records.next().map(|record| {
            summary.total_records += 1;
            record
        })
    }
}

// ============================================================================
// BCF Source Adapter
// ============================================================================

use noodles::bcf;

/// Adapter for reading BCF files.
pub struct BcfSource {
    records: SortedRecordIter,
    initial_parse_errors: usize,
    initial_stats_synced: bool,
}

impl BcfSource {
    /// Create a new BcfSource using natural chromosome ordering (no reference required)
    pub fn new_without_reference<R: std::io::Read>(
        mut reader: bcf::io::Reader<R>,
    ) -> io::Result<Self> {
        let header = reader.read_header()?;
        let max_records = get_max_records_limit();
        let mut parse_errors = 0;
        let mut records_read = 0;
        let mut sorter =
            RecordExternalSorter::new(header.clone(), SortFormat::Bcf, RecordOrder::Natural);

        for result in reader.record_bufs(&header) {
            if let Some(limit) = max_records
                && records_read >= limit
            {
                tracing::info!(limit, "reached max_records limit, stopping read");
                break;
            }
            match result {
                Ok(record) => {
                    sorter.push(record)?;
                    records_read += 1;
                }
                Err(e) => {
                    parse_errors += 1;
                    tracing::warn!("failed to read BCF record: {}", e);
                }
            }
        }

        let records = sorter.finish()?;

        Ok(Self {
            records,
            initial_parse_errors: parse_errors,
            initial_stats_synced: false,
        })
    }

    pub fn new<R: std::io::Read>(
        mut reader: bcf::io::Reader<R>,
        reference: &ReferenceGenome,
    ) -> io::Result<Self> {
        let header = reader.read_header()?;
        let max_records = get_max_records_limit();
        let mut parse_errors = 0;
        let mut records_read = 0;
        let order = RecordOrder::from_reference(reference);
        let mut sorter = RecordExternalSorter::new(header.clone(), SortFormat::Bcf, order);

        for result in reader.record_bufs(&header) {
            if let Some(limit) = max_records
                && records_read >= limit
            {
                tracing::info!(limit, "reached max_records limit, stopping read");
                break;
            }
            match result {
                Ok(record) => {
                    sorter.push(record)?;
                    records_read += 1;
                }
                Err(e) => {
                    parse_errors += 1;
                    tracing::warn!("failed to read BCF record: {}", e);
                }
            }
        }

        let records = sorter.finish()?;

        Ok(Self {
            records,
            initial_parse_errors: parse_errors,
            initial_stats_synced: false,
        })
    }
}

impl VariantSource for BcfSource {
    fn next_variant(
        &mut self,
        summary: &mut crate::ConversionSummary,
    ) -> Option<io::Result<RecordBuf>> {
        if !self.initial_stats_synced {
            summary.parse_errors += self.initial_parse_errors;
            self.initial_stats_synced = true;
        }

        self.records.next().map(|record| {
            summary.total_records += 1;
            record
        })
    }
}

// ============================================================================
// Natural Chromosome Ordering (no reference required)
// ============================================================================

/// Returns a sort key for chromosome names using natural ordering.
/// This allows sorting without a reference genome.
///
/// Order: 1-22, X, Y, MT/M, then other contigs alphabetically
pub fn natural_contig_order(name: &str) -> (u32, String) {
    let normalized = name
        .trim_start_matches("chr")
        .trim_start_matches("Chr")
        .trim_start_matches("CHR");

    // Try to parse as a number (autosomes 1-22)
    if let Ok(num) = normalized.parse::<u32>() {
        return (num, String::new());
    }

    // Handle sex chromosomes and mitochondrial
    match normalized.to_uppercase().as_str() {
        "X" => (23, String::new()),
        "Y" => (24, String::new()),
        "M" | "MT" => (25, String::new()),
        other => (1000, other.to_string()), // Other contigs sorted alphabetically after standard ones
    }
}

fn reverse_complement(s: &str) -> String {
    s.chars()
        .rev()
        .map(|c| match c {
            'A' => 'T',
            'T' => 'A',
            'C' => 'G',
            'G' => 'C',
            'a' => 't',
            't' => 'a',
            'c' => 'g',
            'g' => 'c',
            'N' => 'N',
            'n' => 'n',
            _ => c,
        })
        .collect()
}