noodles_vcf/io/reader/
record_buf.rs

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
//! VCF record reader.

mod alternate_bases;
mod filters;
mod ids;
mod info;
mod position;
mod quality_score;
mod reference_bases;
mod reference_sequence_name;
mod samples;
pub(crate) mod value;

use std::{error, fmt};

use self::{
    alternate_bases::parse_alternate_bases, filters::parse_filters, ids::parse_ids,
    info::parse_info, position::parse_position, quality_score::parse_quality_score,
    reference_bases::parse_reference_bases, reference_sequence_name::parse_reference_sequence_name,
    samples::parse_samples,
};
use crate::{variant::RecordBuf, Header};

const MISSING: &str = ".";

/// An error when a raw VCF record fails to parse.
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The position is invalid.
    InvalidPosition(position::ParseError),
    /// The IDs are invalid.
    InvalidIds(ids::ParseError),
    /// The reference bases are invalid.
    InvalidReferenceBases(reference_bases::ParseError),
    /// The alternate bases are invalid.
    InvalidAlternateBases(alternate_bases::ParseError),
    /// The quality score is invalid.
    InvalidQualityScore(quality_score::ParseError),
    /// The filters are invalid.
    InvalidFilters(filters::ParseError),
    /// The info is invalid.
    InvalidInfo(info::ParseError),
    /// The samples are invalid.
    InvalidSamples(samples::ParseError),
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::InvalidPosition(e) => Some(e),
            Self::InvalidIds(e) => Some(e),
            Self::InvalidReferenceBases(e) => Some(e),
            Self::InvalidAlternateBases(e) => Some(e),
            Self::InvalidQualityScore(e) => Some(e),
            Self::InvalidFilters(e) => Some(e),
            Self::InvalidInfo(e) => Some(e),
            Self::InvalidSamples(e) => Some(e),
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidPosition(_) => write!(f, "invalid position"),
            Self::InvalidIds(_) => write!(f, "invalid IDs"),
            Self::InvalidReferenceBases(_) => write!(f, "invalid reference bases"),
            Self::InvalidAlternateBases(_) => write!(f, "invalid alternate bases"),
            Self::InvalidQualityScore(_) => write!(f, "invalid quality score"),
            Self::InvalidFilters(_) => write!(f, "invalid filters"),
            Self::InvalidInfo(_) => write!(f, "invalid info"),
            Self::InvalidSamples(_) => write!(f, "invalid samples"),
        }
    }
}

pub(crate) fn parse_record_buf(
    mut s: &str,
    header: &Header,
    record: &mut RecordBuf,
) -> Result<(), ParseError> {
    let field = next_field(&mut s);
    parse_reference_sequence_name(field, record.reference_sequence_name_mut());

    let field = next_field(&mut s);
    *record.variant_start_mut() = parse_position(field).map_err(ParseError::InvalidPosition)?;

    record.ids_mut().as_mut().clear();
    let field = next_field(&mut s);
    if field != MISSING {
        parse_ids(field, record.ids_mut()).map_err(ParseError::InvalidIds)?;
    }

    let field = next_field(&mut s);
    parse_reference_bases(field, record.reference_bases_mut())
        .map_err(ParseError::InvalidReferenceBases)?;

    record.alternate_bases_mut().as_mut().clear();
    let field = next_field(&mut s);
    if field != MISSING {
        parse_alternate_bases(field, record.alternate_bases_mut())
            .map_err(ParseError::InvalidAlternateBases)?;
    }

    let field = next_field(&mut s);
    *record.quality_score_mut() = match field {
        MISSING => None,
        _ => parse_quality_score(field)
            .map(Some)
            .map_err(ParseError::InvalidQualityScore)?,
    };

    let field = next_field(&mut s);
    match field {
        MISSING => record.filters_mut().as_mut().clear(),
        _ => parse_filters(field, record.filters_mut()).map_err(ParseError::InvalidFilters)?,
    }

    record.info_mut().clear();
    let field = next_field(&mut s);
    if field != MISSING {
        parse_info(header, field, record.info_mut()).map_err(ParseError::InvalidInfo)?;
    }

    parse_samples(header, s, record.samples_mut()).map_err(ParseError::InvalidSamples)?;

    Ok(())
}

fn next_field<'a>(s: &mut &'a str) -> &'a str {
    const DELIMITER: char = '\t';

    let (field, rest) = s
        .split_once(DELIMITER)
        .unwrap_or_else(|| s.split_at(s.len()));

    *s = rest;

    field
}