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
//! Parser implementation for matrices in JASPAR (2016) format.
//!
//! The [JASPAR database](https://jaspar.elixir.no/docs/) stores manually
//! curated DNA-binding sites as count matrices.
//!
//! The JASPAR files contains a FASTA-like header line for each record,
//! followed by one line per symbol storing tab-separated counts at each
//! position. The 2016 version introduces bracketed matrix columns for
//! each symbol, allowing for non-standard alphabets to be used:
//! ```text
//! >MA0001.3	AGL3
//! A  [     0      0     82     40     56     35     65     25     64      0 ]
//! C  [    92     79      1      4      0      0      1      4      0      0 ]
//! G  [     0      0      2      3      1      0      4      3     28     92 ]
//! T  [     3     16     10     48     38     60     25     63      3      3 ]
//! ```

use std::io::BufRead;

use lightmotif::abc::Alphabet;
use lightmotif::pwm::CountMatrix;

use crate::error::Error;

mod parse;

// ---

#[derive(Debug, Clone)]
pub struct Record<A: Alphabet> {
    id: String,
    description: Option<String>,
    matrix: CountMatrix<A>,
}

impl<A: Alphabet> Record<A> {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    pub fn matrix(&self) -> &CountMatrix<A> {
        &self.matrix
    }
}

impl<A: Alphabet> AsRef<CountMatrix<A>> for Record<A> {
    fn as_ref(&self) -> &CountMatrix<A> {
        &self.matrix
    }
}

// ---

pub struct Reader<B: BufRead, A: Alphabet> {
    buffer: Vec<u8>,
    bufread: B,
    start: usize,
    _alphabet: std::marker::PhantomData<A>,
}

impl<B: BufRead, A: Alphabet> Reader<B, A> {
    pub fn new(mut reader: B) -> Self {
        let mut buffer = Vec::new();
        let start = reader.read_until(b'>', &mut buffer).unwrap_or(1) - 1;

        Self {
            bufread: reader,
            buffer,
            start,
            _alphabet: std::marker::PhantomData,
        }
    }
}

impl<B: BufRead, A: Alphabet> Iterator for Reader<B, A> {
    type Item = Result<Record<A>, Error>;
    fn next(&mut self) -> Option<Self::Item> {
        match self.bufread.read_until(b'>', &mut self.buffer) {
            Ok(n) => {
                let bytes = if n == 0 {
                    &self.buffer[self.start..]
                } else {
                    &self.buffer[self.start..=self.start + n]
                };
                let text = match std::str::from_utf8(bytes) {
                    Ok(text) => text,
                    Err(_) => {
                        return Some(Err(Error::Io(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            "decoding error",
                        ))));
                    }
                };
                if n == 0 && text.trim().is_empty() {
                    return None;
                }
                let (rest, record) = match self::parse::record::<A>(text) {
                    Err(e) => return Some(Err(Error::from(e))),
                    Ok((rest, record)) => (rest, record),
                };
                self.start += n + 1 - rest.len();
                if self.start > self.buffer.capacity() / 2 {
                    let n = self.buffer.len();
                    self.buffer.copy_within(self.start.., 0);
                    self.buffer.truncate(n - self.start);
                    self.start = 0;
                }
                Some(Ok(record))
            }
            Err(e) => Some(Err(Error::from(e))),
        }
    }
}

#[cfg(test)]
mod test {

    use lightmotif::Dna;

    #[test]
    fn test_single() {
        let text = concat!(
            ">MA0001.1 RUNX1\n",
            "A [10 12  4  1  2  2  0  0  0  8 13 ]\n",
            "C [ 2  2  7  1  0  8  0  0  1  2  2 ]\n",
            "G [ 3  1  1  0 23  0 26 26  0  0  4 ]\n",
            "T [11 11 14 24  1 16  0  0 25 16  7 ]\n",
        );
        let mut reader = super::Reader::<_, Dna>::new(std::io::Cursor::new(text));
        let record = reader.next().unwrap().unwrap();
        assert_eq!(&record.id, "MA0001.1");
        assert!(reader.next().is_none());
    }

    #[test]
    fn test_multi() {
        let text = concat!(
            ">MA0001.1 RUNX1\n",
            "A [10 12  4  1  2  2  0  0  0  8 13 ]\n",
            "C [ 2  2  7  1  0  8  0  0  1  2  2 ]\n",
            "G [ 3  1  1  0 23  0 26 26  0  0  4 ]\n",
            "T [11 11 14 24  1 16  0  0 25 16  7 ]\n",
            ">MA0002.1 RUNX1\n",
            "A [10 12  4  1  2  2  0  0  0  8 13 ]\n",
            "C [ 2  2  7  1  0  8  0  0  1  2  2 ]\n",
            "G [ 3  1  1  0 23  0 26 26  0  0  4 ]\n",
            "T [11 11 14 24  1 16  0  0 25 16  7 ]\n",
        );
        let mut reader = super::Reader::<_, Dna>::new(std::io::Cursor::new(text));
        let record = reader.next().unwrap().unwrap();
        assert_eq!(&record.id, "MA0001.1");
        let record = reader.next().unwrap().unwrap();
        assert_eq!(&record.id, "MA0002.1");
        assert!(reader.next().is_none());
    }
}