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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#![allow(clippy::unreadable_literal)]

pub mod errors;

use std::collections::{HashMap, VecDeque};
use std::iter::Peekable;
use std::str;

use bbhash::MPHF;
use failure::{Error, SyncFailure};
use lazy_static::lazy_static;
use nthash::{ntf64, NtHashForwardIterator};

use crate::errors::UKHSError;

lazy_static! {
    static ref UKHS_HASHES: HashMap<(usize, usize), &'static str> = {
        let mut map = HashMap::new();
        map.insert((7, 20), include_str!("../data/res_7_20_4_0.txt"));
        map.insert((9, 20), include_str!("../data/res_9_20_4_0.txt"));
        map.insert((9, 30), include_str!("../data/res_9_30_4_0.txt"));
        // TODO: add others
        map
    };
}

pub struct UKHS {
    k: usize,
    w: usize,
    mphf: MPHF,
    revmap: Vec<u64>,
    kmers: Vec<String>,
    kmers_hashes: Vec<u64>,
}

impl<'a> UKHS {
    pub fn new(k: usize, w: usize) -> Result<UKHS, Error> {
        if k > w {
            return Err(UKHSError::KSizeOutOfWRange { ksize: k, wsize: w }.into());
        }

        let w_round = (w / 10) * 10;
        let entries = UKHS_HASHES[&(k, w_round)];
        let mut kmers: Vec<String> = entries
            .split('\n')
            .filter_map(|s| {
                if s.len() == k {
                    Some(String::from(s))
                } else {
                    None
                }
            })
            .collect();

        // TODO: is the order relevant for interoperability?
        // for now this is necessary to make binary_search work
        kmers.sort_unstable();

        let kmers_hashes: Vec<u64> = kmers.iter().map(|h| ntf64(h.as_bytes(), 0, k)).collect();

        let mphf = MPHF::new(kmers_hashes.clone(), 1, 1.0); // TODO: any way to avoid this clone?
        let mut revmap = vec![0; kmers_hashes.len()];
        for hash in &kmers_hashes {
            revmap[mphf.lookup(*hash).unwrap() as usize] = *hash;
        }

        Ok(UKHS {
            k,
            w,
            mphf,
            revmap,
            kmers,
            kmers_hashes,
        })
    }

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

    pub fn k(&self) -> usize {
        self.k
    }

    pub fn w(&self) -> usize {
        self.w
    }

    pub fn query_bucket(&self, hash: u64) -> Option<usize> {
        if let Some(pos) = self.mphf.lookup(hash) {
            if self.revmap[pos as usize] == hash {
                return Some(pos as usize);
            }
        }
        None
    }

    /// Creates a new UKHSIterator with internal state properly initialized.
    pub fn iter_sequence(&'a self, seq: &'a [u8]) -> UKHSIterator<'a> {
        let mut max_idx = seq.len() - self.k + 1;

        if self.k > seq.len() || self.w > seq.len() {
            // In these cases the sequence is too small for having any k-mers or
            // w-mers, so the iterator will return None right away.
            max_idx = 0;
        }

        UKHSIterator {
            seq,
            ukhs: self,
            current_k_idx: 0,
            current_w_idx: 0,
            max_idx,
        }
    }

    /// Creates a new UKHSHashIterator with internal state properly initialized.
    pub fn hash_iter_sequence(&'a self, seq: &'a [u8]) -> Result<UKHSHashIterator<'a>, Error> {
        if self.k > seq.len() {
            return Err(UKHSError::KSizeOutOfRange {
                ksize: self.k,
                sequence: String::from_utf8(seq.to_vec()).unwrap(),
            }
            .into());
        }

        if self.w > seq.len() {
            return Err(UKHSError::WSizeOutOfRange {
                wsize: self.w,
                sequence: String::from_utf8(seq.to_vec()).unwrap(),
            }
            .into());
        }

        let mut nthash_k_iter = NtHashForwardIterator::new(seq, self.k)
            .map_err(SyncFailure::new)?
            .peekable();
        let mut nthash_w_iter = NtHashForwardIterator::new(seq, self.w)
            .map_err(SyncFailure::new)?
            .peekable();

        let current_w_hash = nthash_w_iter.next().unwrap();
        let mut current_unikmers = VecDeque::with_capacity(self.k);

        for i in 0..=self.w - self.k {
            let k_hash = nthash_k_iter.next().unwrap();

            if self.contains(k_hash) {
                current_unikmers.push_back((i, k_hash));
            }
        }

        let current_w_idx = 0;
        let current_k_idx = 0;
        let max_k_pos = seq.len() - self.k + 1;

        Ok(UKHSHashIterator {
            nthash_k_iter,
            nthash_w_iter,
            ukhs: self,
            current_w_hash,
            current_w_idx,
            current_k_idx,
            max_k_pos,
            current_unikmers,
        })
    }

    pub fn contains(&self, hash: u64) -> bool {
        if let Some(pos) = self.mphf.lookup(hash) {
            if self.revmap[pos as usize] == hash {
                return true;
            }
        }
        false
    }

    pub fn contains_kmer(&self, kmer: &str) -> bool {
        self.kmers.binary_search(&kmer.into()).is_ok()
    }

    pub fn kmer_for_ukhs_hash(&self, hash: u64) -> Option<String> {
        if let Some(pos) = self.kmers_hashes.iter().position(|&x| x == hash) {
            Some(self.kmers[pos].clone())
        } else {
            None
        }
    }
}

/// An iterator for finding universal hitting k-mers in a sequence.
///
/// ```
///     # use failure::Error;
///     use ukhs::UKHS;
///
///     # fn main() -> Result<(), Error> {
///     let seq = b"ACACCGTAGCCTCCAGATGC";
///     let ukhs = UKHS::new(7, 20)?;
///
///     let it = ukhs.iter_sequence(seq);
///     let ukhs: Vec<(String, String)> = it.collect();
///     assert_eq!(ukhs,
///                [
///                    ("ACACCGTAGCCTCCAGATGC".into(), "ACACCGT".into()),
///                    ("ACACCGTAGCCTCCAGATGC".into(), "CCGTAGC".into()),
///                    ("ACACCGTAGCCTCCAGATGC".into(), "AGCCTCC".into()),
///                    ("ACACCGTAGCCTCCAGATGC".into(), "GCCTCCA".into())
///                ]);
///     # Ok(())
///     # }
/// ```
pub struct UKHSIterator<'a> {
    seq: &'a [u8],
    ukhs: &'a UKHS,
    current_k_idx: usize,
    current_w_idx: usize,
    max_idx: usize,
}

impl<'a> Iterator for UKHSIterator<'a> {
    type Item = (String, String);

    fn next(&mut self) -> Option<Self::Item> {
        while self.current_k_idx != self.max_idx {
            let last_k_pos = self.current_w_idx + self.ukhs.w() - self.ukhs.k() + 1;

            if self.current_k_idx == last_k_pos {
                self.current_w_idx += 1;
                self.current_k_idx = self.current_w_idx;
            }

            let current_kmer =
                str::from_utf8(&self.seq[self.current_k_idx..self.current_k_idx + self.ukhs.k])
                    .unwrap();

            self.current_k_idx += 1;

            if self.ukhs.contains_kmer(current_kmer) {
                let wmer_start = self.current_w_idx;
                let current_wmer =
                    str::from_utf8(&self.seq[wmer_start..wmer_start + self.ukhs.w]).unwrap();

                return Some((current_wmer.into(), current_kmer.into()));
            };
        }
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.max_idx, Some(self.max_idx))
    }
}

impl<'a> ExactSizeIterator for UKHSIterator<'a> {}

/// An iterator for finding universal hitting k-mers in a sequence.
/// It uses ntHash for efficient k-mer hashing.
///
/// ```
///     # use failure::Error;
///     use ukhs::UKHS;
///
///     # fn main() -> Result<(), Error> {
///     let seq = b"ACACCGTAGCCTCCAGATGC";
///     let ukhs = UKHS::new(7, 20)?;
///
///     let it = ukhs.hash_iter_sequence(seq)?;
///     let matches: Vec<(u64, u64)> = it.collect();
///     assert_eq!(matches,
///                [
///                    (0x37137c91412bb512, 0xfbd9591aa929c685),
///                    (0x37137c91412bb512, 0x9cd9a1bcb779d6ad),
///                    (0x37137c91412bb512, 0x46fa47d28c0ffba5),
///                    (0x37137c91412bb512, 0xf482addc6edbc920)
///                ]);
///     # Ok(())
///     # }
/// ```
///
/// If you're only interested in the UKHS hashes (and not which w-mer it comes
/// from) you can also collect only the UKHS hashes:
///
/// ```
///     # use failure::Error;
///     use ukhs::UKHS;
///
///     # fn main() -> Result<(), Error> {
///     let ukhs = UKHS::new(7, 20)?;
///     let seq = b"ACACCGTAGCCTCCAGATGC";
///     let it = ukhs.hash_iter_sequence(seq)?;
///     let hashes: Vec<u64> = it.map(|(_, hash)| hash).collect();
///     assert_eq!(hashes, [0xfbd9591aa929c685, 0x9cd9a1bcb779d6ad, 0x46fa47d28c0ffba5, 0xf482addc6edbc920]);
///     # Ok(())
///     # }
/// ```
pub struct UKHSHashIterator<'a> {
    ukhs: &'a UKHS,
    nthash_k_iter: Peekable<NtHashForwardIterator<'a>>,
    nthash_w_iter: Peekable<NtHashForwardIterator<'a>>,
    current_w_hash: u64,
    current_w_idx: usize,
    current_k_idx: usize,
    max_k_pos: usize,
    current_unikmers: VecDeque<(usize, u64)>,
}

impl<'a> Iterator for UKHSHashIterator<'a> {
    type Item = (u64, u64);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            // We're past the last possible k-mer; stop.
            if self.current_k_idx >= self.max_k_pos {
                return None;
            }

            let last_k_pos = self.current_w_idx + self.ukhs.w() - self.ukhs.k() + 1;

            // First state: draining queue
            if self.current_k_idx < last_k_pos {
                self.current_k_idx += 1;

                if let Some((pos, k_hash)) = self
                    .current_unikmers
                    .iter()
                    .find(|(p, _)| *p >= self.current_k_idx - 1)
                {
                    self.current_k_idx = *pos + 1;
                    return Some((self.current_w_hash, *k_hash));
                } else {
                    // In this case we went through all the current_unikmers;
                    // set current_k_idx to last_k_pos
                    self.current_k_idx = last_k_pos;
                }
            } else {
                // Second state:
                // - if front of current_unikmers < current_idx, pop it

                self.current_w_idx += 1;

                if let Some((pos, _)) = self.current_unikmers.front() {
                    if *pos < self.current_w_idx {
                        self.current_unikmers.pop_front();
                    }
                }

                // - push_back next k-mer (if it is inside next w-mer)
                let new_k_hash = self.nthash_k_iter.next().unwrap();

                if self.ukhs.contains(new_k_hash) {
                    self.current_unikmers.push_back((last_k_pos, new_k_hash));
                }

                // - advance to next w-mer
                self.current_w_hash = self.nthash_w_iter.next().unwrap();

                self.current_k_idx = self.current_w_idx;
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // TODO: max_idx is the is the maximum,
        // but very unlikely (every k-mer would have to be a UKHS k-mer.
        self.nthash_k_iter.size_hint()
    }
}

impl<'a> ExactSizeIterator for UKHSHashIterator<'a> {}

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

    use std::collections::HashSet;
    use std::iter::FromIterator;

    #[test]
    fn basic_check() {
        let seq = b"ACACCGTAGCCTCCAGATGC";
        let w = 20;
        let k = 7;
        let ukhs = UKHS::new(k, w).unwrap();

        let it = ukhs.iter_sequence(seq);
        let mut unikmers: Vec<String> = it.map(|(_, x)| x).collect();
        unikmers.sort_unstable();

        assert_eq!(unikmers, ["ACACCGT", "AGCCTCC", "CCGTAGC", "GCCTCCA"]);

        let it = ukhs.hash_iter_sequence(seq).unwrap();
        let ukhs_hash: Vec<(u64, u64)> = it.collect();
        assert!(ukhs_hash.len() >= seq.len() - w + 1);

        let mut ukhs_unhash_set: HashSet<String> = ukhs_hash
            .iter()
            .map(|(_, hash)| ukhs.kmer_for_ukhs_hash(*hash).unwrap())
            .collect();
        let mut ukhs_unhash = Vec::<String>::from_iter(ukhs_unhash_set.into_iter());
        ukhs_unhash.sort_unstable();

        assert_eq!(unikmers, ukhs_unhash);
        assert_eq!(
            ukhs_hash,
            [
                (0x37137c91412bb512, 0xfbd9591aa929c685),
                (0x37137c91412bb512, 0x9cd9a1bcb779d6ad),
                (0x37137c91412bb512, 0x46fa47d28c0ffba5),
                (0x37137c91412bb512, 0xf482addc6edbc920)
            ]
        );
    }

    #[test]
    fn longer_check() {
        let seq = b"ACACCGTAGCCTCCAGATGCGTAG";
        /*
                 ACACCGTAGCCTCCAGATGCGTAG
                 *  *   **       *

        w-mer 0: ACACCGTAGCCTCCAGATGC
                 *  *   **
        w-mer 1: CACCGTAGCCTCCAGATGCG
                   *   **
        w-mer 2: ACCGTAGCCTCCAGATGCGT
                  *   **
        w-mer 3: CCGTAGCCTCCAGATGCGTA
                 *   **       *
        w-mer 4: CGTAGCCTCCAGATGCGTAG
                    **       *
        */
        let k = 7;
        let w = 20;
        let ukhs = UKHS::new(k, w).unwrap();

        let it = ukhs.iter_sequence(seq);
        let mut unikmers: Vec<String> = it.map(|(_, x)| x).collect();

        assert_eq!(
            unikmers,
            [
                "ACACCGT", "CCGTAGC", "AGCCTCC", "GCCTCCA", // w-mer 0
                "CCGTAGC", "AGCCTCC", "GCCTCCA", // w-mer 1
                "CCGTAGC", "AGCCTCC", "GCCTCCA", // w-mer 2
                "CCGTAGC", "AGCCTCC", "GCCTCCA", "ATGCGTA", // w-mer 3
                "AGCCTCC", "GCCTCCA", "ATGCGTA" // w-mer 4
            ]
        );

        let it = ukhs.hash_iter_sequence(seq).unwrap();
        let ukhs_hash: Vec<(u64, u64)> = it.collect();

        assert!(
            ukhs_hash.len() >= seq.len() - w + 1,
            "iter len {}, should be at least {}",
            ukhs_hash.len(),
            seq.len() - w + 1
        );

        let mut ukhs_unhash: Vec<String> = ukhs_hash
            .iter()
            .map(|(_, hash)| ukhs.kmer_for_ukhs_hash(*hash).unwrap())
            .collect();

        assert_eq!(unikmers, ukhs_unhash);

        assert_eq!(
            ukhs_hash,
            [
                // w-mer 0
                // (ACACCGTAGCCTCCAGATGC, ACACCGT)
                (0x37137c91412bb512, 0xfbd9591aa929c685),
                // (ACACCGTAGCCTCCAGATGC, CCGTAGC)
                (0x37137c91412bb512, 0x9cd9a1bcb779d6ad),
                // (ACACCGTAGCCTCCAGATGC, AGCCTCC)
                (0x37137c91412bb512, 0x46fa47d28c0ffba5),
                // (ACACCGTAGCCTCCAGATGC, GCCTCCA)
                (0x37137c91412bb512, 0xf482addc6edbc920),
                // w-mer 1
                // (CACCGTAGCCTCCAGATGCG, CCGTAGC)
                (0xf52d9b92474381bf, 0x9cd9a1bcb779d6ad),
                // (CACCGTAGCCTCCAGATGCG, AGCCTCC)
                (0xf52d9b92474381bf, 0x46fa47d28c0ffba5),
                // (CACCGTAGCCTCCAGATGCG, GCCTCCA)
                (0xf52d9b92474381bf, 0xf482addc6edbc920),
                // w-mer 2
                // (ACCGTAGCCTCCAGATGCGT, CCGTAGC)
                (0xdb5854d371a65e15, 0x9cd9a1bcb779d6ad),
                // (ACCGTAGCCTCCAGATGCGT, AGCCTCC)
                (0xdb5854d371a65e15, 0x46fa47d28c0ffba5),
                // (ACCGTAGCCTCCAGATGCGT, GCCTCCA)
                (0xdb5854d371a65e15, 0xf482addc6edbc920),
                // w-mer 3
                // (CCGTAGCCTCCAGATGCGTA, CCGTAGC)
                (0x31020e7531c970e0, 0x9cd9a1bcb779d6ad),
                // (CCGTAGCCTCCAGATGCGTA, AGCCTCC)
                (0x31020e7531c970e0, 0x46fa47d28c0ffba5),
                // (CCGTAGCCTCCAGATGCGTA, GCCTCCA)
                (0x31020e7531c970e0, 0xf482addc6edbc920),
                // (CCGTAGCCTCCAGATGCGTA, ATGCGTA)
                (0x31020e7531c970e0, 0x6903a07436e4ffa1),
                // w-mer 4
                // (CCGTAGCCTCCAGATGCGTA, CCGTAGC)
                // (CGTAGCCTCCAGATGCGTAG, AGCCTCC)
                (0x5a6008385506dbd8, 0x46fa47d28c0ffba5),
                // (CGTAGCCTCCAGATGCGTAG, GCCTCCA)
                (0x5a6008385506dbd8, 0xf482addc6edbc920),
                // (CGTAGCCTCCAGATGCGTAG, TGCGTAG)
                (0x5a6008385506dbd8, 0x6903a07436e4ffa1)
            ]
        );
    }
}