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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! ## K-mer processing, searching, and counting.
//!
//! This module provides structs and methods for handling common k-mer
//! operations efficiently using integer encodings. A k-mer is a short
//! subsequence of nucleotides, which are represented in *Zoe* by [`Kmer`].
//!
//! *Zoe* has two structs to store k-mers:
//!
//! - A [`KmerSet`], which is a [`HashSet`] for k-mers
//! - A [`KmerCounter`] to store k-mers and their counts (using a [`HashMap`])
//!
//! These structs can be populated from individual k-mers, sequences, or k-mers
//! with up to `N` mismatches. They can be then be queried, iterated over, or
//! used to search within a sequence. See [`KmerSet`] and [`KmerCounter`] for
//! more details.
//!
//! ## Generic Parameters
//!
//! Many structs related to k-mers, including [`KmerSet`] and [`KmerCounter`],
//! are generic over a maximum possible k-mer length `MAX_LEN`. This is used to
//! determine (at compile-time) the appropriate integer type to store the
//! encoded k-mer. The actual k-mer length can be set to a different value at
//! runtime as long as it is less than `MAX_LEN`. For guidance on picking the
//! appropriate `MAX_LEN`, see [`SupportedKmerLen`].
//!
//! [`KmerSet`] and [`KmerCounter`] are also generic over the type of
//! [`KmerEncoder`]. *Zoe* currently provides two encoders:
//!
//! - [`ThreeBitKmerEncoder`], which uses three bits to store each base. It
//! allows for `A`, `C`, `G`, `T`, and `N` to all be represented. It does not
//! preserve case or the distinction between `T` and `U`. `N` is used as a
//! catch-all for bases that are not `ACGTUNacgtun`.
//! - [`TwoBitKmerEncoder`], which uses two bits to store each base. It allows
//! for `A`, `C`, `G`, and `T` to be represented. It is important to use this
//! only on sanitized data, since anything outside of `ACGTacgt` is
//! interpretted as `A`. Currently, this does not support generating variants.
//!
//! For convenience, the type aliases [`ThreeBitKmerSet`],
//! [`ThreeBitKmerCounter`], [`TwoBitKmerSet`], and [`TwoBitKmerCounter`] are
//! provided.
//!
//! <div class="warning important">
//!
//! **Important**
//!
//! When performing more specialized k-mer operations, you may need to directly
//! encode and decode k-mers, rather than relying on the methods in [`KmerSet`]
//! or [`KmerCounter`]. It is important to use the same [`KmerEncoder`] to both
//! encode and decode the k-mers.
//!
//! </div>
//!
//! ## Examples
//!
//! Count the 3-mers in a sequence:
//! ```
//! # use zoe::{kmer::encoders::three_bit::ThreeBitKmerCounter, prelude::*};
//! let sequence = b"GGCCACCAAGGCCA";
//! let mut kmer_counter = ThreeBitKmerCounter::<3>::new(3).unwrap();
//! kmer_counter.tally_from_sequence(sequence);
//! for (kmer, count) in kmer_counter {
//! println!("{kmer}\t{count}");
//! }
//! ```
//!
//! Search for the 17-mers of a primer within a sequence, with up to one
//! mismatch:
//! ```
//! # use zoe::{kmer::encoders::three_bit::ThreeBitKmerSet, prelude::*};
//! let primer = b"TGATAGTTTTAGAGTTAGGTAG";
//! let sequence = b"TGCCCGTAACGTACAGTTTTACAGTTAGGTACCC";
//! let mut kmer_set = ThreeBitKmerSet::<17>::new(17).unwrap();
//! kmer_set.insert_from_sequence_with_variants::<1>(primer);
//! let kmer_pos = kmer_set.find_in_seq(sequence);
//! assert_eq!(kmer_pos, Some(14..31));
//! ```
//!
//! This can be equivalent performed using:
//! ```
//! # use zoe::{kmer::{encoders::three_bit::ThreeBitKmerSet, FindKmers}, prelude::*};
//! let primer: Nucleotides = b"TGATAGTTTTAGAGTTAGGTAG".into();
//! let sequence: Nucleotides = b"TGCCCGTAACGTACAGTTTTACAGTTAGGTACCC".into();
//! let mut kmer_set = ThreeBitKmerSet::<17>::new(17).unwrap();
//! kmer_set.insert_from_sequence_with_variants::<1>(primer);
//! let kmer_pos = sequence.find_kmers(&kmer_set);
//! assert_eq!(kmer_pos, Some(14..31));
//! ```
//!
//! [`HashSet`]: std::collections::HashSet
//! [`HashMap`]: std::collections::HashMap
//! [`Display`]: std::fmt::Display
//! [`Kmer`]: Kmer
//! [`KmerEncoder`]: KmerEncoder
//! [`KmerSet`]: KmerSet
//! [`KmerCounter`]: KmerCounter
//! [`ThreeBitKmerEncoder`]: encoders::three_bit::ThreeBitKmerEncoder
//! [`ThreeBitKmerSet`]: encoders::three_bit::ThreeBitKmerSet
//! [`ThreeBitKmerCounter`]: encoders::three_bit::ThreeBitKmerCounter
//! [`TwoBitKmerEncoder`]: encoders::two_bit::TwoBitKmerEncoder
//! [`TwoBitKmerSet`]: encoders::two_bit::TwoBitKmerSet
//! [`TwoBitKmerCounter`]: encoders::two_bit::TwoBitKmerCounter
//! [`SupportedKmerLen`]: SupportedKmerLen
//! [`insert_from_sequence`]: KmerSet::insert_from_sequence
//! [`find_in_seq`]: FindKmersInSeq::find_in_seq
//! [`find_in_seq_rev`]: FindKmersInSeq::find_in_seq_rev
//! [`FindKmers`]: FindKmers
//! [`find_kmers`]: FindKmers::find_kmers
//! [`find_kmers_rev`]: FindKmers::find_kmers_rev
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use KmerEncoder;
use crate::;
/// A k-mer, stored as ASCII bytes in an array of size `MAX_LEN`.
///
/// Since k-mers are short sequences, *Zoe* stores them on the stack using an
/// array, rather than allocating a vector. This struct stores the array as well
/// as the length of the k-mer.