compact_genome/implementation/
array_kmer.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! A simple representation of a k-mer as an array.

use crate::implementation::vec_sequence::SliceSubGenome;
use crate::interface::alphabet::Alphabet;
use crate::interface::k_mer::{Kmer, OwnedKmer};
use crate::interface::sequence::{GenomeSequence, GenomeSequenceMut, OwnedGenomeSequence};
use ref_cast::RefCast;
use std::ops::{Index, IndexMut, Range};
use traitsequence::interface::{OwnedSequence, Sequence, SequenceMut};

/// A k-mer stored as array of plain characters.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct ArrayKmer<const K: usize, AlphabetType: Alphabet> {
    array: [AlphabetType::CharacterType; K],
}

impl<const K: usize, AlphabetType: Alphabet> Kmer<K, AlphabetType, SliceSubGenome<AlphabetType>>
    for SliceSubGenome<AlphabetType>
{
}

impl<const K: usize, AlphabetType: Alphabet>
    OwnedKmer<K, AlphabetType, SliceSubGenome<AlphabetType>> for ArrayKmer<K, AlphabetType>
{
    fn successor(&self, successor: <AlphabetType as Alphabet>::CharacterType) -> Self {
        let mut array = self.array.clone();

        array.rotate_left(1);
        array[array.len() - 1] = successor;

        Self { array }
    }
}

impl<const K: usize, AlphabetType: Alphabet>
    GenomeSequence<AlphabetType, SliceSubGenome<AlphabetType>> for ArrayKmer<K, AlphabetType>
{
    fn as_genome_subsequence(&self) -> &SliceSubGenome<AlphabetType> {
        SliceSubGenome::ref_cast(&self.array[..])
    }
}

impl<const K: usize, AlphabetType: Alphabet>
    Sequence<AlphabetType::CharacterType, SliceSubGenome<AlphabetType>>
    for ArrayKmer<K, AlphabetType>
{
    type Iterator<'a> = std::slice::Iter<'a, AlphabetType::CharacterType> where Self: 'a, AlphabetType::CharacterType: 'a;

    fn iter(&self) -> Self::Iterator<'_> {
        self.array.iter()
    }

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

impl<const K: usize, AlphabetType: Alphabet>
    OwnedGenomeSequence<AlphabetType, SliceSubGenome<AlphabetType>> for ArrayKmer<K, AlphabetType>
{
}

impl<const K: usize, AlphabetType: Alphabet>
    OwnedSequence<AlphabetType::CharacterType, SliceSubGenome<AlphabetType>>
    for ArrayKmer<K, AlphabetType>
{
}

impl<const K: usize, AlphabetType: Alphabet>
    GenomeSequenceMut<AlphabetType, SliceSubGenome<AlphabetType>> for ArrayKmer<K, AlphabetType>
{
    fn as_genome_subsequence_mut(&mut self) -> &mut SliceSubGenome<AlphabetType> {
        SliceSubGenome::ref_cast_mut(&mut self.array[..])
    }
}

impl<const K: usize, AlphabetType: Alphabet>
    SequenceMut<AlphabetType::CharacterType, SliceSubGenome<AlphabetType>>
    for ArrayKmer<K, AlphabetType>
{
    type IteratorMut<'a> = std::slice::IterMut<'a, AlphabetType::CharacterType>  where Self: 'a, AlphabetType::CharacterType: 'a;

    fn iter_mut(&mut self) -> Self::IteratorMut<'_> {
        self.array.iter_mut()
    }
}

impl<const K: usize, AlphabetType: Alphabet> FromIterator<AlphabetType::CharacterType>
    for ArrayKmer<K, AlphabetType>
{
    fn from_iter<T: IntoIterator<Item = AlphabetType::CharacterType>>(iter: T) -> Self {
        Self {
            array: iter
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .unwrap_or_else(|error: Vec<_>| {
                    panic!("iterator is not of length k = {K}, but {}", error.len())
                }),
        }
    }
}

impl<const K: usize, AlphabetType: Alphabet> Index<Range<usize>> for ArrayKmer<K, AlphabetType> {
    type Output = SliceSubGenome<AlphabetType>;

    fn index(&self, index: Range<usize>) -> &Self::Output {
        self.as_genome_subsequence().index(index)
    }
}

impl<const K: usize, AlphabetType: Alphabet> Index<usize> for ArrayKmer<K, AlphabetType> {
    type Output = AlphabetType::CharacterType;

    fn index(&self, index: usize) -> &Self::Output {
        self.as_genome_subsequence().index(index)
    }
}

impl<const K: usize, AlphabetType: Alphabet> IndexMut<Range<usize>> for ArrayKmer<K, AlphabetType> {
    fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
        self.as_genome_subsequence_mut().index_mut(index)
    }
}

impl<const K: usize, AlphabetType: Alphabet> IndexMut<usize> for ArrayKmer<K, AlphabetType> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.as_genome_subsequence_mut().index_mut(index)
    }
}

#[cfg(test)]
mod tests {
    use traitsequence::interface::Sequence;

    use crate::{
        implementation::alphabets::dna_alphabet::{DnaAlphabet, DnaCharacter},
        interface::{k_mer::OwnedKmer, sequence::OwnedGenomeSequence},
    };

    use super::ArrayKmer;

    #[test]
    fn successor() {
        let kmer = ArrayKmer::<4, DnaAlphabet>::from_slice_u8(b"ACGT").unwrap();
        let successor_a = kmer.successor(b'A'.try_into().unwrap());
        let successor_c = kmer.successor(b'C'.try_into().unwrap());
        let successor_g = kmer.successor(b'G'.try_into().unwrap());
        let successor_t = kmer.successor(b'T'.try_into().unwrap());

        assert_eq!(
            kmer.iter().cloned().collect::<Vec<_>>(),
            vec![
                DnaCharacter::try_from(b'A').unwrap(),
                DnaCharacter::try_from(b'C').unwrap(),
                DnaCharacter::try_from(b'G').unwrap(),
                DnaCharacter::try_from(b'T').unwrap()
            ],
        );

        assert_eq!(
            successor_a.iter().cloned().collect::<Vec<_>>(),
            vec![
                DnaCharacter::try_from(b'C').unwrap(),
                DnaCharacter::try_from(b'G').unwrap(),
                DnaCharacter::try_from(b'T').unwrap(),
                DnaCharacter::try_from(b'A').unwrap()
            ],
        );

        assert_eq!(
            successor_c.iter().cloned().collect::<Vec<_>>(),
            vec![
                DnaCharacter::try_from(b'C').unwrap(),
                DnaCharacter::try_from(b'G').unwrap(),
                DnaCharacter::try_from(b'T').unwrap(),
                DnaCharacter::try_from(b'C').unwrap()
            ],
        );

        assert_eq!(
            successor_g.iter().cloned().collect::<Vec<_>>(),
            vec![
                DnaCharacter::try_from(b'C').unwrap(),
                DnaCharacter::try_from(b'G').unwrap(),
                DnaCharacter::try_from(b'T').unwrap(),
                DnaCharacter::try_from(b'G').unwrap()
            ],
        );

        assert_eq!(
            successor_t.iter().cloned().collect::<Vec<_>>(),
            vec![
                DnaCharacter::try_from(b'C').unwrap(),
                DnaCharacter::try_from(b'G').unwrap(),
                DnaCharacter::try_from(b'T').unwrap(),
                DnaCharacter::try_from(b'T').unwrap()
            ],
        );
    }
}