biovault 0.1.61

A bioinformatics data vault CLI tool
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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

// Male-specific Y chromosome markers (confirmed in 100% of males, 0% of females)
const MALE_Y_MARKERS: &[&str] = &[
    "rs11575897",
    "rs2534636",
    "i3000043",
    "i3000045",
    "i4000162",
    "rs13303871",
    "rs35284970",
    "rs3895",
    "i4000120",
    "i4000121",
];

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenotypeMetadata {
    pub data_type: String,
    pub source: Option<String>,
    pub grch_version: Option<String>,
    pub row_count: Option<i64>,
    pub chromosome_count: Option<i64>,
    pub inferred_sex: Option<String>,
}

impl Default for GenotypeMetadata {
    fn default() -> Self {
        Self {
            data_type: "Unknown".to_string(),
            source: None,
            grch_version: None,
            row_count: None,
            chromosome_count: None,
            inferred_sex: None,
        }
    }
}

/// Detect if a file is a genotype file and extract metadata (fast version - header only)
pub fn detect_genotype_metadata(file_path: &str) -> Result<GenotypeMetadata> {
    let path = Path::new(file_path);

    if !path.exists() {
        return Ok(GenotypeMetadata::default());
    }

    let file = File::open(path)?;
    let reader = BufReader::new(file);
    let mut lines = reader.lines();

    // Read first 50 lines for header analysis
    let mut header_text = String::new();
    let mut data_lines = Vec::new();
    let mut line_count = 0;
    let max_header_lines = 50;

    for line_result in &mut lines {
        let line = line_result?;
        line_count += 1;

        // Collect lines starting with # or comment markers as header
        if line.starts_with('#') || line.starts_with("//") {
            header_text.push_str(&line);
            header_text.push('\n');
        } else if !line.trim().is_empty() {
            // Non-comment, non-empty line - potential data
            data_lines.push(line);

            // Collect up to 10 data lines for structure validation
            if data_lines.len() >= 10 {
                break;
            }
        }

        if line_count >= max_header_lines {
            break;
        }
    }

    // Check if this looks like a genotype file
    let is_genotype = validate_genotype_structure(&data_lines);

    if !is_genotype {
        return Ok(GenotypeMetadata::default());
    }

    // File is a genotype - extract metadata from headers
    let header_lower = header_text.to_lowercase();

    let source = detect_source(&header_lower);
    let grch_version = detect_grch_version(&header_lower);

    Ok(GenotypeMetadata {
        data_type: "Genotype".to_string(),
        source,
        grch_version,
        row_count: None,
        chromosome_count: None,
        inferred_sex: None,
    })
}

/// Analyze genotype file for row count, chromosome count, and inferred sex (slow - reads entire file)
pub fn analyze_genotype_file(file_path: &str) -> Result<GenotypeMetadata> {
    let path = Path::new(file_path);

    if !path.exists() {
        return Ok(GenotypeMetadata::default());
    }

    // First detect source from header (need this for sex inference)
    let metadata = detect_genotype_metadata(file_path)?;
    let source = metadata.source.clone();

    // Count total rows and chromosomes by reading the entire file
    let (row_count, chromosome_count, inferred_sex) =
        count_rows_and_chromosomes(file_path, source.as_deref())?;

    // Return combined metadata (detected + analyzed)
    Ok(GenotypeMetadata {
        data_type: metadata.data_type,
        source: metadata.source,
        grch_version: metadata.grch_version,
        row_count: Some(row_count as i64),
        chromosome_count: Some(chromosome_count as i64),
        inferred_sex: Some(inferred_sex),
    })
}

/// Count data rows and unique chromosomes in a genotype file
fn count_rows_and_chromosomes(
    file_path: &str,
    source: Option<&str>,
) -> Result<(usize, usize, String)> {
    let file = File::open(file_path)?;
    let reader = BufReader::new(file);

    let mut row_count = 0;
    let mut chromosomes = HashSet::new();
    let mut male_markers_called = 0;

    let is_23andme = source.map(|s| s.contains("23andMe")).unwrap_or(false);

    for line_result in reader.lines() {
        let line = line_result?;

        // Skip comments and empty lines
        if line.starts_with('#') || line.starts_with("//") || line.trim().is_empty() {
            continue;
        }

        // Count this as a data row
        row_count += 1;

        // Extract chromosome (column 2 in TSV format)
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() >= 4 {
            let rsid = parts[0].trim();
            let chr = parts[1].trim();
            let genotype = parts[3].trim();

            chromosomes.insert(chr.to_string());

            // For 23andMe files, check male-specific Y markers
            if is_23andme && (chr == "Y" || chr == "24") && MALE_Y_MARKERS.contains(&rsid) {
                // Check if genotype is called (not missing)
                if !genotype.is_empty() && genotype != "--" && genotype != "00" {
                    male_markers_called += 1;
                }
            }
        }
    }

    // Infer sex based on source
    let inferred_sex = if is_23andme {
        // 23andMe: Use male-specific Y marker logic (validated heuristics)
        if male_markers_called >= 5 {
            "Male".to_string()
        } else if chromosomes.contains("X") || chromosomes.contains("23") {
            "Female".to_string()
        } else {
            "Unknown".to_string()
        }
    } else {
        // Other sources: Conservative inference - we don't have validated heuristics yet
        let has_x = chromosomes.contains("X") || chromosomes.contains("23");
        let has_y = chromosomes.contains("Y") || chromosomes.contains("24");

        if has_x && has_y {
            // Both X and Y present - return Unknown since we don't have validated heuristics
            // for determining if Y chromosome data indicates male sex in non-23andMe files
            "Unknown".to_string()
        } else if has_x && !has_y {
            // Only X, no Y - likely female
            "Female".to_string()
        } else {
            "Unknown".to_string()
        }
    };

    Ok((row_count, chromosomes.len(), inferred_sex))
}

/// Validate that data lines match genotype file structure
fn validate_genotype_structure(data_lines: &[String]) -> bool {
    if data_lines.is_empty() {
        return false;
    }

    let mut valid_lines = 0;

    for line in data_lines {
        let parts: Vec<&str> = line.split('\t').collect();

        // Should have at least 4 columns (rsid, chromosome, position, genotype)
        // Additional columns (like Dynamic DNA's gs, baf, lrr) are allowed
        if parts.len() < 4 {
            continue;
        }

        // Check column 1: rsid (should start with 'rs' or 'i')
        let rsid = parts[0].trim();
        if !rsid.starts_with("rs") && !rsid.starts_with('i') {
            continue;
        }

        // Check column 2: chromosome (should be 1-22, X, Y, MT, or similar)
        let chr = parts[1].trim();
        if !is_valid_chromosome(chr) {
            continue;
        }

        // Check column 3: position (should be an integer)
        if parts[2].trim().parse::<u64>().is_err() {
            continue;
        }

        // Check column 4: genotype (should be valid genotype format)
        let genotype = parts[3].trim();
        if !is_valid_genotype(genotype) {
            continue;
        }

        valid_lines += 1;
    }

    // Consider it a genotype file if at least 70% of data lines are valid
    let threshold = (data_lines.len() * 7) / 10;
    valid_lines >= threshold
}

fn is_valid_chromosome(chr: &str) -> bool {
    // Check for numeric chromosomes 1-22
    if let Ok(num) = chr.parse::<u8>() {
        return (1..=22).contains(&num);
    }

    // Check for sex chromosomes and mitochondrial
    matches!(
        chr.to_uppercase().as_str(),
        "X" | "Y" | "MT" | "M" | "23" | "24"
    )
}

fn is_valid_genotype(genotype: &str) -> bool {
    // Valid genotypes include:
    // - Two-letter genotypes: AA, AG, TT, etc.
    // - Indels: D, I, DD, DI, II
    // - Missing: --, 00
    // - Single letters for haploid: A, T, G, C

    if genotype.is_empty() {
        return false;
    }

    let g = genotype.to_uppercase();

    // Missing data
    if g == "--" || g == "00" {
        return true;
    }

    // Check if all characters are valid nucleotides or indel markers
    for c in g.chars() {
        if !matches!(c, 'A' | 'T' | 'G' | 'C' | 'D' | 'I' | '-' | '0') {
            return false;
        }
    }

    // Valid length (1-2 characters)
    matches!(g.len(), 1 | 2)
}

/// Detect source from header text (case-insensitive)
fn detect_source(header_lower: &str) -> Option<String> {
    if header_lower.contains("23andme") {
        Some("23andMe".to_string())
    } else if header_lower.contains("ancestrydna") || header_lower.contains("ancestry dna") {
        Some("AncestryDNA".to_string())
    } else if header_lower.contains("genes for good") || header_lower.contains("genesforgood") {
        Some("Genes for Good".to_string())
    } else if header_lower.contains("dynamic dna")
        || header_lower.contains("ddna")
        || header_lower.contains("dynamicdnalabs")
    {
        Some("Dynamic DNA".to_string())
    } else {
        Some("Unknown".to_string())
    }
}

/// Detect GRCh version from header text (case-insensitive)
fn detect_grch_version(header_lower: &str) -> Option<String> {
    // Build 36 / GRCh36 / hg18
    if header_lower.contains("build 36")
        || header_lower.contains("grch36")
        || header_lower.contains("hg18")
    {
        return Some("36".to_string());
    }

    // Build 37 / GRCh37 / hg19
    if header_lower.contains("build 37")
        || header_lower.contains("grch37")
        || header_lower.contains("hg19")
    {
        return Some("37".to_string());
    }

    // Build 38 / GRCh38 / hg38
    if header_lower.contains("build 38")
        || header_lower.contains("grch38")
        || header_lower.contains("hg38")
    {
        return Some("38".to_string());
    }

    Some("Unknown".to_string())
}

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

    #[test]
    fn test_valid_chromosome() {
        assert!(is_valid_chromosome("1"));
        assert!(is_valid_chromosome("22"));
        assert!(is_valid_chromosome("X"));
        assert!(is_valid_chromosome("Y"));
        assert!(is_valid_chromosome("MT"));
        assert!(!is_valid_chromosome("0"));
        assert!(!is_valid_chromosome("23"));
        assert!(!is_valid_chromosome("ABC"));
    }

    #[test]
    fn test_valid_genotype() {
        assert!(is_valid_genotype("AA"));
        assert!(is_valid_genotype("AG"));
        assert!(is_valid_genotype("TT"));
        assert!(is_valid_genotype("--"));
        assert!(is_valid_genotype("00"));
        assert!(is_valid_genotype("D"));
        assert!(is_valid_genotype("I"));
        assert!(is_valid_genotype("DD"));
        assert!(is_valid_genotype("A"));
        assert!(!is_valid_genotype(""));
        assert!(!is_valid_genotype("AAA"));
        assert!(!is_valid_genotype("XY"));
    }

    #[test]
    fn test_detect_source() {
        assert_eq!(
            detect_source("this is from 23andme"),
            Some("23andMe".to_string())
        );
        assert_eq!(
            detect_source("ancestrydna test"),
            Some("AncestryDNA".to_string())
        );
        assert_eq!(
            detect_source("genes for good data"),
            Some("Genes for Good".to_string())
        );
        assert_eq!(
            detect_source("# this data file generated by dynamic dna (ddna) laboratories"),
            Some("Dynamic DNA".to_string())
        );
        assert_eq!(
            detect_source("data from ddna"),
            Some("Dynamic DNA".to_string())
        );
        assert_eq!(
            detect_source("https://dynamicdnalabs.com"),
            Some("Dynamic DNA".to_string())
        );
        assert_eq!(detect_source("unknown source"), Some("Unknown".to_string()));
    }

    #[test]
    fn test_detect_grch_version() {
        assert_eq!(detect_grch_version("build 36"), Some("36".to_string()));
        assert_eq!(detect_grch_version("grch37"), Some("37".to_string()));
        assert_eq!(detect_grch_version("hg38"), Some("38".to_string()));
        assert_eq!(detect_grch_version("build 38"), Some("38".to_string()));
        assert_eq!(detect_grch_version("grch38"), Some("38".to_string()));
        assert_eq!(
            detect_grch_version("chromosomal location realtive to build 38 of the human reference"),
            Some("38".to_string())
        );
        assert_eq!(
            detect_grch_version("no version info"),
            Some("Unknown".to_string())
        );
    }
}