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
//! Lazily-evaluated BCF record and fields.

mod convert;
mod filters;
mod genotypes;
mod info;
pub(crate) mod value;

pub(crate) use self::value::Value;
pub use self::{filters::Filters, genotypes::Genotypes, info::Info};

use std::io;

use noodles_vcf as vcf;

/// A chromosome ID.
pub type ChromosomeId = usize;

/// A BCF record.
#[derive(Clone, Debug, PartialEq)]
pub struct Record {
    pub(crate) chrom: ChromosomeId,
    pub(crate) pos: vcf::record::Position,
    pub(crate) rlen: usize,
    pub(crate) qual: Option<vcf::record::QualityScore>,
    pub(crate) id: vcf::record::Ids,
    pub(crate) r#ref: vcf::record::ReferenceBases,
    pub(crate) alt: vcf::record::AlternateBases,
    pub(crate) filter: Filters,
    pub(crate) info: Info,
    pub(crate) genotypes: Genotypes,
}

impl Record {
    /// Returns the chromosome ID of the record.
    ///
    /// The chromosome ID represents an index in the contig string map, which associates an ID (by
    /// position) with a contig record in the VCF header (by name). That is, to get the associated
    /// contig record in the VCF header, the contig string map must first be queried by position to
    /// find the chromosome name, and then the contigs in the VCF header can be queried by name.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert_eq!(record.chromosome_id(), 0);
    /// ```
    pub fn chromosome_id(&self) -> ChromosomeId {
        self.chrom
    }

    /// Returns the start position of this record.
    ///
    /// Despite the BCF format using 0-based positions, this normalizes the value as a 1-based
    /// position.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert_eq!(usize::from(record.position()), 1);
    /// ```
    pub fn position(&self) -> vcf::record::Position {
        self.pos
    }

    pub(crate) fn rlen(&self) -> usize {
        self.rlen
    }

    /// Returns the end position of this record.
    ///
    /// This value is 1-based.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert_eq!(record.end().map(usize::from)?, 1);
    /// # Ok::<(), io::Error>(())
    /// ```
    pub fn end(&self) -> io::Result<vcf::record::Position> {
        use vcf::record::Position;

        let start = usize::from(self.position());
        let len = self.rlen();
        let end = start + len - 1;

        Ok(Position::from(end))
    }

    /// Returns the quality score.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert!(record.quality_score().is_none());
    /// ```
    pub fn quality_score(&self) -> Option<vcf::record::QualityScore> {
        self.qual
    }

    /// Returns the IDs.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert!(record.ids().is_empty());
    /// ```
    pub fn ids(&self) -> &vcf::record::Ids {
        &self.id
    }

    pub(crate) fn reference_bases(&self) -> &vcf::record::ReferenceBases {
        &self.r#ref
    }

    pub(crate) fn alternate_bases(&self) -> &vcf::record::AlternateBases {
        &self.alt
    }

    /// Returns the filters.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert!(record.filters().is_empty());
    /// ```
    pub fn filters(&self) -> &Filters {
        &self.filter
    }

    /// Returns the info.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert!(record.info().is_empty());
    /// ```
    pub fn info(&self) -> &Info {
        &self.info
    }

    /// Returns the genotypes.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf as bcf;
    /// let record = bcf::lazy::Record::default();
    /// assert!(record.genotypes().is_empty());
    /// ```
    pub fn genotypes(&self) -> &Genotypes {
        &self.genotypes
    }
}

impl Default for Record {
    fn default() -> Self {
        use vcf::record::reference_bases::Base;

        Self {
            chrom: 0,
            pos: vcf::record::Position::from(1),
            rlen: 1,
            qual: None,
            id: vcf::record::Ids::default(),
            r#ref: vcf::record::ReferenceBases::try_from(vec![Base::A]).unwrap(),
            alt: vcf::record::AlternateBases::default(),
            filter: Filters::default(),
            info: Info::default(),
            genotypes: Genotypes::default(),
        }
    }
}