noodles_bcf/record/
samples.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
142
143
144
145
146
147
148
149
150
151
152
153
154
//! BCF record samples.

mod sample;
pub mod series;

use std::{io, iter};

use noodles_vcf as vcf;

use self::series::read_series;
pub use self::{sample::Sample, series::Series};

/// BCF record genotypes.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Samples<'r> {
    src: &'r [u8],
    sample_count: usize,
    format_count: usize,
}

impl<'r> Samples<'r> {
    pub(super) fn new(src: &'r [u8], sample_count: usize, format_count: usize) -> Self {
        Self {
            src,
            sample_count,
            format_count,
        }
    }

    /// Returns the number of fields per sample.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bcf::record::Samples;
    /// let samples = Samples::default();
    /// assert_eq!(samples.format_count(), 0);
    /// ```
    pub fn format_count(&self) -> usize {
        self.format_count
    }

    /// Returns the sample with the given sample name.
    pub fn get(&'r self, header: &vcf::Header, sample_name: &str) -> Option<Sample<'r>> {
        header
            .sample_names()
            .get_index_of(sample_name)
            .and_then(|i| self.get_index(i))
    }

    /// Returns a sample at the given index.
    pub fn get_index(&'r self, i: usize) -> Option<Sample<'r>> {
        if i < self.sample_count {
            Some(Sample::new(self, i))
        } else {
            None
        }
    }

    /// Returns the series with the given column name.
    pub fn select<'h: 'r>(
        &'r self,
        header: &'h vcf::Header,
        column_name: &str,
    ) -> Option<io::Result<Series<'r>>> {
        for result in self.series() {
            let series = match result {
                Ok(s) => s,
                Err(e) => return Some(Err(e)),
            };

            match series.name(header) {
                Ok(name) if name == column_name => return Some(Ok(series)),
                Ok(_) => {}
                Err(e) => return Some(Err(e)),
            }
        }

        None
    }

    /// Returns an iterator over series.
    pub fn series(&'r self) -> impl Iterator<Item = io::Result<Series<'r>>> + 'r {
        let mut src = self.src;

        iter::from_fn(move || {
            if src.is_empty() {
                None
            } else {
                Some(read_series(&mut src, self.sample_count))
            }
        })
    }

    /// Returns an iterator over samples.
    pub fn iter(&self) -> impl Iterator<Item = Sample<'_>> {
        (0..self.sample_count).map(|i| Sample::new(self, i))
    }
}

impl<'r> AsRef<[u8]> for Samples<'r> {
    fn as_ref(&self) -> &[u8] {
        self.src
    }
}

impl<'r> vcf::variant::record::Samples for Samples<'r> {
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize {
        self.sample_count
    }

    fn column_names<'a, 'h: 'a>(
        &'a self,
        header: &'h vcf::Header,
    ) -> Box<dyn Iterator<Item = io::Result<&'a str>> + 'a> {
        Box::new(
            self.series()
                .map(|result| result.and_then(|series| series.name(header))),
        )
    }

    fn select<'a, 'h: 'a>(
        &'a self,
        header: &'h vcf::Header,
        column_name: &str,
    ) -> Option<io::Result<Box<dyn vcf::variant::record::samples::Series + 'a>>> {
        self.select(header, column_name).map(|result| {
            result.map(|series| Box::new(series) as Box<dyn vcf::variant::record::samples::Series>)
        })
    }

    fn series(
        &self,
    ) -> Box<
        dyn Iterator<Item = io::Result<Box<dyn vcf::variant::record::samples::Series + '_>>> + '_,
    > {
        Box::new(self.series().map(|result| {
            result.map(|series| Box::new(series) as Box<dyn vcf::variant::record::samples::Series>)
        }))
    }

    fn iter(
        &self,
    ) -> Box<dyn Iterator<Item = Box<dyn vcf::variant::record::samples::Sample + '_>> + '_> {
        Box::new(
            self.iter()
                .map(|sample| Box::new(sample) as Box<dyn vcf::variant::record::samples::Sample>),
        )
    }
}