noodles_vcf/record/samples/
sample.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
use std::{io, iter};

use super::{series::value::parse_value, Keys};
use crate::{variant::record::samples::series::Value, Header};

/// A VCF record samples sample.
#[derive(Debug, Eq, PartialEq)]
pub struct Sample<'s> {
    src: &'s str,
    keys: Keys<'s>,
}

impl<'s> Sample<'s> {
    pub(super) fn new(src: &'s str, keys: Keys<'s>) -> Self {
        Self { src, keys }
    }

    /// Returns the value at the given index.
    pub fn get_index<'h: 's>(
        &self,
        header: &'h Header,
        i: usize,
    ) -> Option<Option<io::Result<Value<'s>>>> {
        self.values(header).nth(i)
    }

    /// Returns an iterator over values.
    pub fn values<'h: 's>(
        &self,
        header: &'h Header,
    ) -> impl Iterator<Item = Option<io::Result<Value<'s>>>> + '_ {
        self.iter(header)
            .map(|result| result.map(|(_, value)| value).transpose())
    }

    /// Returns an iterator over fields.
    pub fn iter<'h: 's>(
        &self,
        header: &'h Header,
    ) -> Box<dyn Iterator<Item = io::Result<(&str, Option<Value<'s>>)>> + '_> {
        const DELIMITER: char = ':';

        if self.as_ref().is_empty() {
            Box::new(iter::empty())
        } else {
            Box::new(
                self.keys
                    .iter()
                    .zip(self.src.split(DELIMITER))
                    .map(|(key, s)| parse_value(s, header, key).map(|value| (key, value))),
            )
        }
    }
}

impl<'a> AsRef<str> for Sample<'a> {
    fn as_ref(&self) -> &str {
        self.src
    }
}

impl<'r> crate::variant::record::samples::Sample for Sample<'r> {
    fn get<'a, 'h: 'a>(
        &'a self,
        header: &'h Header,
        key: &str,
    ) -> Option<io::Result<Option<Value<'a>>>> {
        for result in self.iter(header) {
            match result {
                Ok((k, v)) => {
                    if k == key {
                        return Some(Ok(v));
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }

        None
    }

    fn get_index<'a, 'h: 'a>(
        &'a self,
        header: &'h Header,
        i: usize,
    ) -> Option<io::Result<Option<Value<'a>>>> {
        self.iter(header)
            .nth(i)
            .map(|result| result.map(|(_, value)| value))
    }

    fn iter<'a, 'h: 'a>(
        &'a self,
        header: &'h Header,
    ) -> Box<dyn Iterator<Item = io::Result<(&str, Option<Value<'a>>)>> + 'a> {
        Box::new(self.iter(header))
    }
}

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

    #[test]
    fn test_values() {
        let header = Header::default();

        let keys = Keys::new("GT:GQ");
        let sample = Sample::new("0|0:.", keys);
        let mut iter = sample.values(&header);

        assert!(matches!(iter.next(), Some(Some(Ok(Value::Genotype(_))))));
        assert!(matches!(iter.next(), Some(None)));
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_iter() {
        let header = Header::default();

        let keys = Keys::new("GT:GQ");
        let sample = Sample::new("0|0:.", keys);
        let mut iter = sample.iter(&header);

        assert!(matches!(
            iter.next(),
            Some(Ok(("GT", Some(Value::Genotype(_)))))
        ));

        assert!(matches!(iter.next(), Some(Ok(("GQ", None)))));

        assert!(iter.next().is_none());
    }
}