convert_genome 0.3.2

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
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::Parser;
use tracing_subscriber::{EnvFilter, fmt};
use url::Url;

use crate::{
    ConversionConfig, ConversionSummary, OutputFormat, convert_dtc_file,
    input::InputFormat,
    remote::{self, RemoteResource},
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, clap::ValueEnum)]
pub enum Sex {
    Male,
    Female,
    Unknown,
}

#[derive(Debug, Parser)]
#[command(author, version, about = "Convert DTC genotype text files to VCF or BCF", long_about = None)]
struct Cli {
    /// Input DTC genotype file (23andMe, LivingDNA, etc.)
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Input file format (auto-detected if not specified)
    #[arg(long, value_enum, default_value_t = InputFormat::Auto)]
    input_format: InputFormat,

    /// Reference genome FASTA (GRCh38). If omitted, a known reference is downloaded as needed.
    #[arg(long, value_name = "REFERENCE")]
    reference: Option<PathBuf>,

    /// Output VCF or BCF path (mutually exclusive with --output-dir)
    #[arg(value_name = "OUTPUT", conflicts_with = "output_dir")]
    output: Option<PathBuf>,

    /// Output directory (always produces genotypes.vcf; also panel.vcf when --panel is set)
    #[arg(long, value_name = "DIR", conflicts_with = "output")]
    output_dir: Option<PathBuf>,

    /// Output file format
    #[arg(long, value_enum, default_value_t = OutputFormat::Vcf)]
    format: OutputFormat,

    /// Optional explicit FASTA index (.fai) path
    #[arg(long, value_name = "FAI")]
    reference_fai: Option<PathBuf>,

    /// Reference panel VCF/BCF for allele harmonization (Beagle compatibility)
    #[arg(long, value_name = "FILE")]
    panel: Option<PathBuf>,

    /// Sample identifier to embed in the VCF header
    #[arg(long, value_name = "SAMPLE")]
    sample: Option<String>,

    /// Target build for the output (embedded in metadata)
    #[arg(long = "output-build", default_value = "GRCh38")]
    assembly: String,

    /// Caller-asserted input build (e.g. GRCh37, GRCh38). When set, skip
    /// position-based build detection (`check_build`) and treat the input
    /// as already in this build. Saves ~13 minutes per invocation for
    /// callers that already know the input build (e.g. fixed-build
    /// production pipelines).
    #[arg(long, value_name = "BUILD")]
    input_build: Option<String>,

    /// When set, omit reference-only sites from the output
    #[arg(long)]
    variants_only: bool,

    /// Standardize/normalize the input file without format conversion.
    /// Performs: chromosome naming normalization, allele polarization against
    /// reference, sex chromosome ploidy enforcement, and sorting.
    #[arg(long)]
    standardize: bool,

    /// Logging verbosity (e.g. error, warn, info, debug)
    #[arg(long, default_value = "info")]
    log_level: String,

    /// Sex of the sample (auto-detected if not specified)
    #[arg(long, value_enum)]
    sex: Option<Sex>,

    /// Clinical-safety floor: fail (nonzero exit) if fewer than this many
    /// variant records are emitted. Guards against empty / malformed /
    /// unparseable inputs silently producing a near-empty result that flows
    /// into downstream scoring. Lower it only if a small panel is expected.
    #[arg(long, value_name = "N", default_value_t = crate::conversion::DEFAULT_MIN_EMITTED_VARIANTS)]
    min_emitted_variants: usize,

    /// Clinical-safety floor: minimum auto-detected build confidence (0..=1)
    /// required to proceed. Below this the input matches neither GRCh37 nor
    /// GRCh38 decisively and the run fails rather than silently assuming a
    /// build. Bypass detection entirely with --input-build.
    #[arg(long, value_name = "C", default_value_t = crate::conversion::DEFAULT_MIN_BUILD_CONFIDENCE)]
    min_build_confidence: f64,

    /// Clinical-safety ceiling: fail if more than this fraction (0..=1) of
    /// input lines cannot be parsed. Guards against the wrong file format
    /// (e.g. binary data or a non-genome CSV) being read as a sparse genome.
    #[arg(long, value_name = "R", default_value_t = crate::conversion::DEFAULT_MAX_PARSE_ERROR_RATIO)]
    max_parse_error_ratio: f64,
}

pub fn run() -> Result<()> {
    let cli = Cli::parse();
    init_logging(&cli.log_level)?;

    // Validate output arguments
    let output = match (&cli.output, &cli.output_dir) {
        (Some(output), None) => output.clone(),
        (None, Some(dir)) => {
            // Directory mode - create output directory and use genotypes.vcf as output
            std::fs::create_dir_all(dir)
                .with_context(|| format!("failed to create output directory: {}", dir.display()))?;
            dir.join("genotypes.vcf")
        }
        (None, None) => {
            anyhow::bail!("Either --output or --output-dir is required");
        }
        _ => unreachable!(), // conflicts_with handles other cases
    };

    let sample_id = cli
        .sample
        .clone()
        .or_else(|| derive_sample_name(&cli.input))
        .unwrap_or_else(|| String::from("sample"));

    let mut resources = ResourceManager::new();
    let input_origin = cli.input.to_string_lossy().to_string();
    let reference_origin = cli
        .reference
        .as_ref()
        .map(|path| path.to_string_lossy().to_string());
    let reference_fai_origin = cli
        .reference_fai
        .as_ref()
        .map(|path| path.to_string_lossy().to_string());

    let resolved_input = resources.resolve(&cli.input)?;
    if cli.reference.is_none() && cli.reference_fai.is_some() {
        anyhow::bail!("--reference-fai requires --reference");
    }

    let resolved_reference = match &cli.reference {
        Some(path) => Some(resources.resolve(path)?),
        None => None,
    };
    let resolved_reference_fai = match &cli.reference_fai {
        Some(path) => Some(resources.resolve(path)?),
        None => None,
    };

    // Resolve panel if provided
    let resolved_panel = match &cli.panel {
        Some(path) => Some(resources.resolve(path)?),
        None => None,
    };

    let input_format = if matches!(cli.input_format, InputFormat::Auto) {
        InputFormat::detect(&resolved_input)
    } else {
        cli.input_format
    };

    let config = ConversionConfig {
        input: resolved_input,
        input_format,
        input_origin,
        reference_fasta: resolved_reference,
        reference_origin,
        reference_fai: resolved_reference_fai,
        reference_fai_origin,
        output: output.clone(),
        output_dir: cli.output_dir.clone(),
        output_format: cli.format,
        sample_id,
        assembly: cli.assembly.clone(),
        include_reference_sites: !cli.variants_only,
        sex: cli.sex,
        par_boundaries: crate::reference::ParBoundaries::new(&cli.assembly),
        standardize: cli.standardize,
        panel: resolved_panel,
        input_build: cli.input_build.clone(),
        min_emitted_variants: cli.min_emitted_variants,
        min_build_confidence: cli.min_build_confidence,
        max_parse_error_ratio: cli.max_parse_error_ratio,
    };

    let summary = convert_dtc_file(config)?;
    print_summary(&summary);

    Ok(())
}

fn init_logging(level: &str) -> Result<()> {
    let filter = EnvFilter::try_new(level).unwrap_or_else(|_| EnvFilter::new("info"));
    fmt()
        .with_env_filter(filter)
        .with_target(false)
        .try_init()
        .ok();
    Ok(())
}

fn derive_sample_name(path: &Path) -> Option<String> {
    if let Some(raw) = path.to_str()
        && raw.contains("://")
        && let Ok(url) = Url::parse(raw)
    {
        return url
            .path_segments()
            .and_then(|mut segments| segments.next_back())
            .filter(|segment| !segment.is_empty())
            .map(|segment| segment.replace('.', "_"));
    }

    path.file_stem()
        .map(|s| s.to_string_lossy().replace('.', "_"))
        .filter(|s| !s.is_empty())
}

struct ResourceManager {
    remotes: Vec<RemoteResource>,
}

impl ResourceManager {
    fn new() -> Self {
        Self {
            remotes: Vec::new(),
        }
    }

    fn resolve<P>(&mut self, path: P) -> Result<PathBuf>
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();
        let raw = path.to_string_lossy();
        if let Some(url) = parse_url(&raw) {
            let resource = remote::fetch_remote_resource(&url)
                .with_context(|| format!("failed to fetch {url}"))?;
            let local_path = resource.local_path().to_path_buf();
            self.remotes.push(resource);
            Ok(local_path)
        } else {
            Ok(path.to_path_buf())
        }
    }
}

fn parse_url(raw: &str) -> Option<Url> {
    if raw.contains("://") {
        Url::parse(raw).ok()
    } else {
        None
    }
}

fn print_summary(summary: &ConversionSummary) {
    println!(
        "Processed {total} records; emitted {emitted} ({variants} variants, {references} reference).",
        total = summary.total_records,
        emitted = summary.emitted_records,
        variants = summary.variant_records,
        references = summary.reference_records,
    );

    if summary.skipped_reference_sites > 0 {
        println!(
            "Skipped {skipped} reference-only sites due to --variants-only.",
            skipped = summary.skipped_reference_sites
        );
    }

    if summary.missing_genotype_records > 0 {
        println!(
            "Encountered {count} sites with missing genotypes.",
            count = summary.missing_genotype_records
        );
    }

    if summary.symbolic_allele_records > 0 {
        println!(
            "Skipped {count} indel-like genotypes (represented as symbolic alleles).",
            count = summary.symbolic_allele_records
        );
    }

    if summary.unknown_chromosomes > 0
        || summary.reference_failures > 0
        || summary.invalid_genotypes > 0
    {
        println!(
            "Warnings: {chrom} unknown chromosomes, {ref_err} reference lookup failures, {invalid} invalid genotypes.",
            chrom = summary.unknown_chromosomes,
            ref_err = summary.reference_failures,
            invalid = summary.invalid_genotypes
        );
    }

    if summary.parse_errors > 0 {
        println!(
            "Ignored {count} malformed input lines.",
            count = summary.parse_errors
        );
    }
}

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

    #[test]
    fn parses_output_without_reference_positional() {
        let cli = Cli::parse_from(["convert_genome", "input.txt", "output.vcf"]);
        assert_eq!(cli.input, PathBuf::from("input.txt"));
        assert_eq!(cli.reference, None);
        assert_eq!(cli.output, Some(PathBuf::from("output.vcf")));
    }

    #[test]
    fn parses_explicit_reference_flag() {
        let cli = Cli::parse_from([
            "convert_genome",
            "input.txt",
            "--reference",
            "ref.fa",
            "output.vcf",
        ]);
        assert_eq!(cli.reference, Some(PathBuf::from("ref.fa")));
        assert_eq!(cli.output, Some(PathBuf::from("output.vcf")));
    }

    #[test]
    fn parses_output_dir_without_reference() {
        let cli = Cli::parse_from([
            "convert_genome",
            "--output-dir",
            "out",
            "input.vcf.gz",
            "--panel",
            "panel.bcf",
            "--standardize",
            "--output-build",
            "GRCh38",
        ]);

        assert_eq!(cli.input, PathBuf::from("input.vcf.gz"));
        assert_eq!(cli.reference, None);
        assert_eq!(cli.output_dir, Some(PathBuf::from("out")));
        assert_eq!(cli.panel, Some(PathBuf::from("panel.bcf")));
        assert!(cli.standardize);
        assert_eq!(cli.assembly, "GRCh38");
    }

    #[test]
    fn input_build_is_optional_and_defaults_to_none() {
        let cli = Cli::parse_from(["convert_genome", "input.vcf.gz", "output.vcf"]);
        assert_eq!(cli.input_build, None);
    }

    #[test]
    fn parses_input_build_flag() {
        let cli = Cli::parse_from([
            "convert_genome",
            "input.vcf.gz",
            "output.vcf",
            "--input-build",
            "GRCh38",
        ]);
        assert_eq!(cli.input_build.as_deref(), Some("GRCh38"));
    }

    #[test]
    fn parses_output_build_flag() {
        let cli = Cli::parse_from([
            "convert_genome",
            "input.vcf.gz",
            "output.vcf",
            "--output-build",
            "GRCh37",
        ]);
        assert_eq!(cli.assembly, "GRCh37");
    }

    #[test]
    fn assembly_flag_is_rejected() {
        // Clean break: --assembly is no longer accepted as of 0.2.0.
        // Callers must use --output-build instead.
        let result = Cli::try_parse_from([
            "convert_genome",
            "input.vcf.gz",
            "output.vcf",
            "--assembly",
            "GRCh37",
        ]);
        assert!(result.is_err(), "--assembly should no longer be accepted");
    }
}