sshash-lib 0.5.0

Sparse and Skew Hashing of k-mers - Core library
Documentation
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Streaming query for efficient k-mer lookups
//!
//! This module implements streaming queries, which optimize lookup performance
//! when querying consecutive k-mers (sliding window over a sequence).
//!
//! Key optimizations:
//! - Incremental k-mer updates (drop first base, add last base)
//! - Reuse minimizer state across adjacent k-mers
//! - Extend within the same string when possible (avoiding MPHF lookups)
//! - Skip searches when minimizer unchanged and previous lookup failed

use crate::kmer::{Kmer, KmerBits};
use crate::minimizer::{MinimizerInfo, MinimizerIterator};
use crate::encoding::encode_base;

/// Result of a k-mer lookup
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LookupResult {
    /// Absolute k-mer ID (global across all strings)
    pub kmer_id: u64,
    /// Relative k-mer ID within the string (0 <= kmer_id_in_string < string_size)
    pub kmer_id_in_string: u64,
    /// Bit offset into the string data
    pub kmer_offset: u64,
    /// Orientation: +1 for forward, -1 for reverse complement
    pub kmer_orientation: i8,
    
    /// String ID containing this k-mer
    pub string_id: u64,
    /// Start position of the string (in bases)
    pub string_begin: u64,
    /// End position of the string (in bases)
    pub string_end: u64,
    
    /// Whether the minimizer was found in the index
    pub minimizer_found: bool,
}

impl LookupResult {
    /// Create a new lookup result indicating "not found"
    pub fn not_found() -> Self {
        Self {
            kmer_id: u64::MAX,
            kmer_id_in_string: u64::MAX,
            kmer_offset: u64::MAX,
            kmer_orientation: 1, // Forward by default
            string_id: u64::MAX,
            string_begin: u64::MAX,
            string_end: u64::MAX,
            minimizer_found: true,
        }
    }

    /// Check if this result represents a found k-mer
    #[inline]
    pub fn is_found(&self) -> bool {
        self.kmer_id != u64::MAX
    }

    /// Get the string length
    #[inline]
    pub fn string_length(&self) -> u64 {
        if self.is_found() {
            self.string_end - self.string_begin
        } else {
            0
        }
    }
}

impl Default for LookupResult {
    fn default() -> Self {
        Self::not_found()
    }
}

/// Streaming query engine for efficient consecutive k-mer lookups
///
/// This struct maintains state across multiple lookups to optimize
/// queries for sliding windows over sequences.
///
/// # Example
/// ```no_run
/// use sshash_lib::streaming_query::StreamingQuery;
/// // Assuming we have a dictionary...
/// // let mut query = StreamingQuery::new(&dict, true); // canonical=true
/// // 
/// // Process consecutive k-mers efficiently
/// // let result1 = query.lookup("ACGTACGTACGTACGTACGTACGTACGTACG");
/// // let result2 = query.lookup("CGTACGTACGTACGTACGTACGTACGTACGT"); // Sliding by 1
/// ```
pub struct StreamingQuery<const K: usize>
where
    Kmer<K>: KmerBits,
{
    k: usize,
    _m: usize, // Will be used in full Dictionary lookup
    _canonical: bool, // Will be used in full Dictionary lookup
    
    // K-mer state
    start: bool,
    kmer: Option<Kmer<K>>,
    kmer_rc: Option<Kmer<K>>,
    
    // Minimizer state
    minimizer_it: MinimizerIterator,
    minimizer_it_rc: MinimizerIterator,
    curr_mini_info: MinimizerInfo,
    prev_mini_info: MinimizerInfo,
    curr_mini_info_rc: MinimizerInfo,
    prev_mini_info_rc: MinimizerInfo,
    
    // String extension state
    remaining_string_bases: u64,
    
    // Result state
    result: LookupResult,
    
    // Performance counters
    num_searches: u64,
    num_extensions: u64,
    num_invalid: u64,
    num_negative: u64,
}

impl<const K: usize> StreamingQuery<K>
where
    Kmer<K>: KmerBits,
{
    /// Create a new streaming query engine
    ///
    /// # Arguments
    /// * `k` - K-mer size
    /// * `m` - Minimizer size
    /// * `canonical` - Whether to use canonical k-mers (min of forward/RC)
    pub fn new(k: usize, m: usize, canonical: bool) -> Self {
        assert_eq!(k, K, "k parameter must match const generic K");
        
        let dummy_mini = MinimizerInfo::new(u64::MAX, 0, 0);
        
        Self {
            k,
            _m: m,
            _canonical: canonical,
            start: true,
            kmer: None,
            kmer_rc: None,
            minimizer_it: MinimizerIterator::with_seed(k, m, 1),
            minimizer_it_rc: MinimizerIterator::with_seed(k, m, 1),
            curr_mini_info: dummy_mini,
            prev_mini_info: dummy_mini,
            curr_mini_info_rc: dummy_mini,
            prev_mini_info_rc: dummy_mini,
            remaining_string_bases: 0,
            result: LookupResult::not_found(),
            num_searches: 0,
            num_extensions: 0,
            num_invalid: 0,
            num_negative: 0,
        }
    }

    /// Reset the query state (call this when starting a new sequence)
    pub fn reset(&mut self) {
        self.start = true;
        self.remaining_string_bases = 0;
        self.result = LookupResult::not_found();
        self.minimizer_it.set_position(0);
        self.minimizer_it_rc.set_position(0);
    }

    /// Perform a streaming lookup for a k-mer
    ///
    /// This is the main entry point for queries. For optimal performance,
    /// call this with consecutive k-mers (sliding by 1 base at a time).
    ///
    /// # Arguments
    /// * `kmer_str` - DNA string of length K
    ///
    /// # Returns
    /// A LookupResult indicating whether the k-mer was found and its location
    pub fn lookup(&mut self, kmer_bytes: &[u8]) -> LookupResult {
        // MVP version without Dictionary integration (always seeds)
        self.lookup_internal(kmer_bytes, None)
    }

    /// Perform a streaming lookup with dictionary integration.
    ///
    /// Accepts a `&Dictionary` at call time rather than storing a reference,
    /// so callers can manage the dictionary's lifetime independently (e.g. via `Arc`).
    pub fn lookup_with_dict(&mut self, kmer_bytes: &[u8], dict: &crate::dictionary::Dictionary) -> LookupResult {
        self.lookup_internal(kmer_bytes, Some(dict))
    }

    fn lookup_internal(&mut self, kmer_bytes: &[u8], dict_opt: Option<&crate::dictionary::Dictionary>) -> LookupResult {
        // 1. Validation
        let is_valid = if self.start {
            self.is_valid_kmer_bytes(kmer_bytes)
        } else {
            self.is_valid_base(kmer_bytes[self.k - 1])
        };

        if !is_valid {
            self.num_invalid += 1;
            self.reset();
            return self.result.clone();
        }

        // 2. Compute k-mer and reverse complement, update minimizers
        if self.start {
            // First k-mer: parse from scratch using fast byte encoding
            let km = Kmer::<K>::from_ascii_unchecked(kmer_bytes);
            self.kmer = Some(km);
            let rc = km.reverse_complement();
            self.kmer_rc = Some(rc);

            self.curr_mini_info = self.minimizer_it.next(km);
            self.curr_mini_info_rc = self.minimizer_it_rc.next(rc);
        } else {
            // Update incrementally: drop first base, add new last base
            if let Some(mut km) = self.kmer {
                // Drop first base (shift left)
                for i in 0..(self.k - 1) {
                    let base = km.get_base(i + 1);
                    km.set_base(i, base);
                }

                // Add new last base
                let new_base = kmer_bytes[self.k - 1];
                if let Ok(encoded) = encode_base(new_base) {
                    km.set_base(self.k - 1, encoded);

                    self.kmer = Some(km);

                    // Update RC: pad (shift right), set first base to complement
                    if let Some(mut km_rc) = self.kmer_rc {
                        for i in (1..self.k).rev() {
                            let base = km_rc.get_base(i - 1);
                            km_rc.set_base(i, base);
                        }
                        
                        // Complement of new base at position 0
                        let complement = crate::encoding::complement_base(encoded);
                        km_rc.set_base(0, complement);

                        self.kmer_rc = Some(km_rc);

                        self.curr_mini_info = self.minimizer_it.next(km);
                        self.curr_mini_info_rc = self.minimizer_it_rc.next(km_rc);
                    }
                }
            }
        }

        // 3. Compute result (either extend or search)
        if self.remaining_string_bases == 0 {
            self.seed(dict_opt);
        } else {
            // Try to extend within current string
            if let Some(dict) = dict_opt {
                self.try_extend(dict);
            } else {
                // No dictionary, can't extend
                self.seed(dict_opt);
            }
        }

        // 4. Update state
        self.prev_mini_info = self.curr_mini_info;
        self.prev_mini_info_rc = self.curr_mini_info_rc;
        self.start = false;

        self.result.clone()
    }

    /// Validate a full k-mer byte slice
    fn is_valid_kmer_bytes(&self, bytes: &[u8]) -> bool {
        if bytes.len() != self.k {
            return false;
        }
        for &b in bytes {
            if !matches!(b, b'A' | b'C' | b'G' | b'T' | b'a' | b'c' | b'g' | b't') {
                return false;
            }
        }
        true
    }

    /// Validate a single base
    fn is_valid_base(&self, b: u8) -> bool {
        matches!(b, b'A' | b'C' | b'G' | b'T' | b'a' | b'c' | b'g' | b't')
    }

    /// Perform a full search (seed) for the current k-mer
    ///
    /// This is called when we can't extend within the current string.
    fn seed(&mut self, dict_opt: Option<&crate::dictionary::Dictionary>) {
        self.remaining_string_bases = 0;

        // Optimization: if minimizer unchanged and previous was not found, skip
        if !self.start
            && self.curr_mini_info.value == self.prev_mini_info.value
            && self.curr_mini_info_rc.value == self.prev_mini_info_rc.value
            && !self.result.minimizer_found
        {
            assert_eq!(self.result.kmer_id, u64::MAX);
            self.num_negative += 1;
            return;
        }

        if let (Some(dict), Some(kmer)) = (dict_opt, self.kmer) {
            if self._canonical {
                // Canonical mode: matching C++ lookup_canonical logic in seed.
                //
                // Use freshly extracted minimizer info for the lookup because
                // the streaming MinimizerIterator's pos_in_kmer can be wrong
                // for RC k-mers (it doesn't account for the reverse sliding
                // direction, matching C++'s reverse template parameter).
                // The streaming values are still correct for the negative
                // optimization check above (which only uses .value).
                let kmer_rc = kmer.reverse_complement();
                let mini_fwd = dict.extract_minimizer::<K>(&kmer);
                let mini_rc = dict.extract_minimizer::<K>(&kmer_rc);

                if mini_fwd.value < mini_rc.value {
                    self.result = dict.lookup_canonical_streaming::<K>(&kmer, &kmer_rc, mini_fwd);
                } else if mini_rc.value < mini_fwd.value {
                    self.result = dict.lookup_canonical_streaming::<K>(&kmer, &kmer_rc, mini_rc);
                } else {
                    self.result = dict.lookup_canonical_streaming::<K>(&kmer, &kmer_rc, mini_fwd);
                    if self.result.kmer_id == u64::MAX {
                        self.result = dict.lookup_canonical_streaming::<K>(&kmer, &kmer_rc, mini_rc);
                    }
                }
            } else {
                // Regular mode: try forward, then RC with backward orientation.
                // Also use fresh minimizer extraction for correct pos_in_kmer.
                let mini_fwd = dict.extract_minimizer::<K>(&kmer);
                self.result = dict.lookup_regular_streaming::<K>(&kmer, mini_fwd);
                let minimizer_found = self.result.minimizer_found;
                if self.result.kmer_id == u64::MAX {
                    assert_eq!(self.result.kmer_orientation, 1); // forward
                    let kmer_rc = kmer.reverse_complement();
                    let mini_rc = dict.extract_minimizer::<K>(&kmer_rc);
                    self.result = dict.lookup_regular_streaming::<K>(&kmer_rc, mini_rc);
                    self.result.kmer_orientation = -1; // backward
                    let minimizer_rc_found = self.result.minimizer_found;
                    self.result.minimizer_found = minimizer_rc_found || minimizer_found;
                }
            }

            if self.result.kmer_id == u64::MAX {
                self.num_negative += 1;
                return;
            }

            assert!(self.result.minimizer_found);
            self.num_searches += 1;

            // Calculate remaining bases for extension, matching C++ exactly:
            //   forward:  (string_end - string_begin - k) - kmer_id_in_string
            //   backward: kmer_id_in_string
            let string_size = self.result.string_end - self.result.string_begin;
            if self.result.kmer_orientation > 0 {
                self.remaining_string_bases =
                    (string_size - self.k as u64) - self.result.kmer_id_in_string;
            } else {
                self.remaining_string_bases = self.result.kmer_id_in_string;
            }
        } else {
            // No dictionary available
            self.result = LookupResult::not_found();
            self.num_negative += 1;
        }
    }
    
    /// Try to extend within the current string
    ///
    /// Matches C++ streaming_query extension logic:
    /// - Read the expected next k-mer from the string data
    /// - If it matches the current k-mer (or its RC), update result fields
    fn try_extend(&mut self, dict: &crate::dictionary::Dictionary) {
        if let (Some(kmer), Some(kmer_rc)) = (self.kmer, self.kmer_rc) {
            // Compute the absolute position of the expected next k-mer
            // C++: kmer_offset = 2 * (kmer_id + string_id * (k-1))
            // The absolute base position in the concatenated strings
            let abs_pos = self.result.kmer_id_in_string as usize
                + self.result.string_begin as usize;

            let next_abs_pos = if self.result.kmer_orientation > 0 {
                abs_pos + 1
            } else {
                abs_pos.wrapping_sub(1)
            };

            // Read expected k-mer from string data at the next position
            let expected_kmer: Kmer<K> = dict.spss().decode_kmer_at(next_abs_pos);

            if expected_kmer.bits() == kmer.bits()
                || expected_kmer.bits() == kmer_rc.bits()
            {
                // Successfully extended!
                self.num_extensions += 1;
                let delta = self.result.kmer_orientation as i64;
                self.result.kmer_id = (self.result.kmer_id as i64 + delta) as u64;
                self.result.kmer_id_in_string =
                    (self.result.kmer_id_in_string as i64 + delta) as u64;
                self.result.kmer_offset =
                    (self.result.kmer_offset as i64 + delta) as u64;
                self.remaining_string_bases -= 1;
                return;
            }
        }
        
        // Extension failed, do a full search
        self.seed(Some(dict));
    }

    /// Get the number of full searches performed
    pub fn num_searches(&self) -> u64 {
        self.num_searches
    }

    /// Get the number of extensions (no search needed)
    pub fn num_extensions(&self) -> u64 {
        self.num_extensions
    }

    /// Get the number of positive lookups (found)
    pub fn num_positive_lookups(&self) -> u64 {
        self.num_searches + self.num_extensions
    }

    /// Get the number of negative lookups (not found)
    pub fn num_negative_lookups(&self) -> u64 {
        self.num_negative
    }

    /// Get the number of invalid lookups (malformed input)
    pub fn num_invalid_lookups(&self) -> u64 {
        self.num_invalid
    }
}

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

    #[test]
    fn test_lookup_result_creation() {
        let result = LookupResult::not_found();
        assert!(!result.is_found());
        assert_eq!(result.kmer_id, u64::MAX);
    }

    #[test]
    fn test_lookup_result_string_length() {
        let mut result = LookupResult::not_found();
        result.string_begin = 100;
        result.string_end = 200;
        result.kmer_id = 42; // Mark as found
        
        assert_eq!(result.string_length(), 100);
    }

    #[test]
    fn test_streaming_query_creation() {
        let query: StreamingQuery<31> = StreamingQuery::new(31, 13, true);
        assert_eq!(query.k, 31);
        assert_eq!(query._m, 13);
        assert!(query._canonical);
        assert_eq!(query.num_searches(), 0);
    }

    #[test]
    fn test_streaming_query_reset() {
        let mut query: StreamingQuery<31> = StreamingQuery::new(31, 13, false);
        query.num_searches = 10;
        query.num_extensions = 5;
        
        query.reset();
        
        assert!(query.start);
        assert_eq!(query.remaining_string_bases, 0);
    }

    #[test]
    fn test_streaming_query_validation() {
        let query: StreamingQuery<31> = StreamingQuery::new(31, 13, true);
        
        assert!(query.is_valid_kmer_bytes(b"ACGTACGTACGTACGTACGTACGTACGTACG")); // 31 bases
        assert!(!query.is_valid_kmer_bytes(b"ACGT")); // Too short
        assert!(!query.is_valid_kmer_bytes(b"ACGTACGTACGTACGTACGTACGTACGTACGN")); // Invalid base
        
        assert!(query.is_valid_base(b'A'));
        assert!(query.is_valid_base(b'a'));
        assert!(!query.is_valid_base(b'N'));
    }

    #[test]
    fn test_streaming_query_lookup_invalid() {
        let mut query: StreamingQuery<15> = StreamingQuery::new(15, 7, true);
        
        // Invalid: too short
        let result = query.lookup(b"ACGT");
        assert!(!result.is_found());
        assert_eq!(query.num_invalid_lookups(), 1);

        // Invalid: has 'N'
        query.reset();
        let result = query.lookup(b"ACGTACGTACGTACN");
        assert!(!result.is_found());
        assert_eq!(query.num_invalid_lookups(), 2);
    }

    #[test]
    fn test_streaming_query_incremental_update() {
        let mut query: StreamingQuery<9> = StreamingQuery::new(9, 5, false);

        // First lookup
        let _result1 = query.lookup(b"ACGTACGTA");
        assert!(!query.start); // No longer in start state

        // Second lookup (sliding by 1)
        let _result2 = query.lookup(b"CGTACGTAC");
        
        // Even though lookups fail (no dictionary), state should update
        assert!(!query.start);
    }
}

/// Streaming query engine integrated with Dictionary
///
/// This provides the full streaming query functionality by connecting
/// to a Dictionary instance for actual k-mer lookups.
pub struct StreamingQueryEngine<'a, const K: usize>
where
    Kmer<K>: KmerBits,
{
    dict: &'a crate::dictionary::Dictionary,
    query: StreamingQuery<K>,
}

impl<'a, const K: usize> StreamingQueryEngine<'a, K>
where
    Kmer<K>: KmerBits,
{
    /// Create a new streaming query engine for a dictionary
    pub fn new(dict: &'a crate::dictionary::Dictionary) -> Self {
        let canonical = dict.canonical();
        Self {
            dict,
            query: StreamingQuery::new(dict.k(), dict.m(), canonical),
        }
    }
    
    /// Reset the query state
    pub fn reset(&mut self) {
        self.query.reset();
    }
    
    /// Perform a streaming lookup
    pub fn lookup(&mut self, kmer_bytes: &[u8]) -> LookupResult {
        // Perform streaming lookup with dictionary integration
        self.query.lookup_with_dict(kmer_bytes, self.dict)
    }
    
    /// Get the number of full searches performed
    pub fn num_searches(&self) -> u64 {
        self.query.num_searches()
    }
    
    /// Get the number of extensions (no search needed)
    pub fn num_extensions(&self) -> u64 {
        self.query.num_extensions()
    }
    
    /// Get statistics
    pub fn stats(&self) -> StreamingQueryStats {
        StreamingQueryStats {
            num_searches: self.query.num_searches(),
            num_extensions: self.query.num_extensions(),
            num_invalid: self.query.num_invalid_lookups(),
            num_negative: self.query.num_negative_lookups(),
        }
    }
}

/// Statistics from streaming queries
#[derive(Debug, Clone)]
pub struct StreamingQueryStats {
    /// Number of full MPHF lookups performed
    pub num_searches: u64,
    /// Number of k-mers resolved by extending from a previous result
    pub num_extensions: u64,
    /// Number of lookups that failed validation (hash collision)
    pub num_invalid: u64,
    /// Number of k-mers not found in the dictionary
    pub num_negative: u64,
}