File: Kmer.hpp

package info (click to toggle)
rapmap 0.12.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: cpp: 38,057; ansic: 2,754; sh: 209; python: 82; makefile: 15
file content (579 lines) | stat: -rw-r--r-- 18,973 bytes parent folder | download | duplicates (5)
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
#ifndef __COMBINELIB_KMERS_HPP__
#define __COMBINELIB_KMERS_HPP__

#include <cassert>
#include <climits>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <type_traits>

namespace combinelib {
namespace kmers {

#ifndef __DEFINE_LIKELY_MACRO__
#define __DEFINE_LIKELY_MACRO__
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect((x), 1)
#define UNLIKELY(x) __builtin_expect((x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
#endif

/**
 *
 * The following lookup tables and reverse complement code is taken from
 *Jellyfish
 * https://github.com/gmarcais/Jellyfish/blob/master/include/jellyfish/mer_dna.hpp
 *
 **/

#define R -1
#define I -2
#define O -3
#define A 0
#define C 1
#define G 2
#define T 3
static constexpr int codes[256] = {
    O, O, O, O, O, O, O, O, O, O, I, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, R, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, A, R, C, R, O, O, G,
    R, O, O, R, O, R, R, O, O, O, R, R, T, O, R, R, R, R, O, O, O, O, O, O,
    O, A, R, C, R, O, O, G, R, O, O, R, O, R, R, O, O, O, R, R, T, O, R, R,
    R, R, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O};

static constexpr char complements[256] = {
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'T', 'N', 'G', 'N', 'N', 'N', 'C', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'A', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'T', 'N', 'G', 'N', 'N', 'N', 'C', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'A', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
    'N'};

#undef R
#undef I
#undef O
#undef A
#undef C
#undef G
#undef T

// Checkered mask. cmask<uint16_t, 1> is every other bit on
// (0x55). cmask<uint16_t,2> is two bits one, two bits off (0x33). Etc.
template <typename U, int len, int l = sizeof(U) * 8 / (2 * len)> struct cmask {
  static const U v =
      (cmask<U, len, l - 1>::v << (2 * len)) | ((static_cast<U>(1) << len) - 1);
};
template <typename U, int len> struct cmask<U, len, 0> {
  static const U v = 0;
};

// Fast reverse complement of one word through bit tweedling.
static inline uint64_t word_reverse_complement(uint64_t w, uint16_t k_) {
  typedef uint64_t U;
  w = ((w >> 2) & cmask<U, 2>::v) | ((w & cmask<U, 2>::v) << 2);
  w = ((w >> 4) & cmask<U, 4>::v) | ((w & cmask<U, 4>::v) << 4);
  w = ((w >> 8) & cmask<U, 8>::v) | ((w & cmask<U, 8>::v) << 8);
  w = ((w >> 16) & cmask<U, 16>::v) | ((w & cmask<U, 16>::v) << 16);
  w = (w >> 32) | (w << 32);
  return ((static_cast<U>(-1)) - w) >> (2 * (32 - k_));
}
static constexpr char revCodes[4] = {'A', 'C', 'G', 'T'};
/**
 * The above from Jellyfish (mer_dna.hpp)
 */

static constexpr int8_t rc_table[128] = {
    78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, // 15
    78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, // 31
    78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, // 787
    78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, // 63
    78, 84, 78, 71, 78, 78, 78, 67, 78, 78, 78, 78, 78, 78, 78, 78, // 79
    78, 78, 78, 78, 65, 65, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, // 95
    78, 84, 78, 71, 78, 78, 78, 67, 78, 78, 78, 78, 78, 78, 78, 78, // 101
    78, 78, 78, 78, 65, 65, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78  // 127
};

/**
 * Since we define these implementations at file scope in a header, we mark them
 *constant to
 * avoid duplicate symbol errors due to external linkage.
 **/

static decltype(codes[0]) codeForChar(char c) {
  return codes[static_cast<uint8_t>(c)];
}
static char charForCode(int i) { return revCodes[i]; }
static decltype(complements[0]) complement(char c) {
  return complements[static_cast<uint8_t>(c)];
}
static int complement(int i) { return 0x3 - i; }
static bool isValidNuc(int i) { return i >= 0; }
static bool isValidNuc(char c) { return isValidNuc(codeForChar(c)); }
static bool notValidNuc(int i) { return !isValidNuc(i); }
static bool notValidNuc(char c) { return !isValidNuc(c); }

// from :
// https://stackoverflow.com/questions/1392059/algorithm-to-generate-bit-mask
template <typename R> static constexpr R bitmask(unsigned int const onecount) {
  return (onecount == 0)
             ? 0
             : (static_cast<R>(-(onecount != 0)) &
                (static_cast<R>(-1) >> ((sizeof(R) * CHAR_BIT) - onecount)));
}

// table that contains bit patterns to mask out the top bits of a word.
// The table is such that maskTable[k] will mask out the top (64 - 2*k) bits of
// the word.
static const constexpr uint64_t maskTable[] = {
    bitmask<uint64_t>(0),  bitmask<uint64_t>(2),  bitmask<uint64_t>(4),
    bitmask<uint64_t>(6),  bitmask<uint64_t>(8),  bitmask<uint64_t>(10),
    bitmask<uint64_t>(12), bitmask<uint64_t>(14), bitmask<uint64_t>(16),
    bitmask<uint64_t>(18), bitmask<uint64_t>(20), bitmask<uint64_t>(22),
    bitmask<uint64_t>(24), bitmask<uint64_t>(26), bitmask<uint64_t>(28),
    bitmask<uint64_t>(30), bitmask<uint64_t>(32), bitmask<uint64_t>(34),
    bitmask<uint64_t>(36), bitmask<uint64_t>(38), bitmask<uint64_t>(40),
    bitmask<uint64_t>(42), bitmask<uint64_t>(44), bitmask<uint64_t>(46),
    bitmask<uint64_t>(48), bitmask<uint64_t>(50), bitmask<uint64_t>(52),
    bitmask<uint64_t>(54), bitmask<uint64_t>(56), bitmask<uint64_t>(58),
    bitmask<uint64_t>(60), bitmask<uint64_t>(62)};

constexpr const uint64_t nucleotidesPerByte = 4;

// from :
// https://stackoverflow.com/questions/31952237/looking-for-a-constexpr-ceil-function
constexpr uint64_t ceil(double num) {
  return (static_cast<double>(static_cast<uint64_t>(num)) == num)
             ? static_cast<uint64_t>(num)
             : static_cast<uint64_t>(num) + ((num > 0) ? 1 : 0);
}

constexpr uint64_t numWordsRequired(uint64_t K) {
  return ceil(K / (1.0 * nucleotidesPerByte * (sizeof(uint64_t))));
}

/**
 * @returns the binary encoding for character c
 **/
static int64_t doEncodeBinary(char c) { return codes[static_cast<uint8_t>(c)]; }

/**
 * @returns true of the character `c` was a valid nucleotide and false
 *otherwise. The corresponding
 * code for this character is placed in the parameter `code`.
 **/
static bool encodeBinary(char c, int64_t& code) {
  code = codes[static_cast<uint8_t>(c)];
  return code >= 0;
}

static char decodeBinary(uint64_t n) { return revCodes[n]; }

/**
 * Convert an ascii character to the corresponding 2-bit encoding
 *
 * Following the encoding suggested [here](https://www.biostars.org/p/113640/),
 *originally
 * suggested by G. Rizk:
 * A : 0
 * C : 1
 * G : 3
 * T : 2
 * N : 4
 *
 * This function will work with both lower and upper case nucleotides.
 * @ASSUMPTION : c is in {A,C,G,T,N,a,c,g,t,n}
 **/
static uint64_t charToBitsGATB(char c) {
  // Convert to uppercase
  // https://stackoverflow.com/questions/10688831/fastest-way-to-capitalize-words
  return static_cast<uint64_t>((((c & ~0x20) >> 1) & 0x03) + ((c & 0x08) >> 3));
}

// Adapted from
// https://github.com/mengyao/Complete-Striped-Smith-Waterman-Library/blob/8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2/src/main.c#L36
static void reverseComplement(const std::string& seq, std::string& readWork) {
  readWork.resize(seq.length(), 'A');
  int32_t end = seq.length() - 1, start = 0;
  while (LIKELY(start < end)) {
    readWork[start] = (char)rc_table[(int8_t)seq[end]];
    readWork[end] = (char)rc_table[(int8_t)seq[start]];
    ++start;
    --end;
  }
  // If odd # of bases, we still have to complement the middle
  if (start == end) {
    readWork[start] = (char)rc_table[(int8_t)seq[start]];
  }
}

static std::string reverseComplement(const std::string& seq) {
  std::string work;
  reverseComplement(seq, work);
  return work;
}

static std::string stringRevComp(const std::string& seq) {
  return reverseComplement(seq);
}

/**
* From https://www.biostars.org/p/113640/.  This only works for a given word
*right now;
* will determine how to best generalize later.
**/
static uint64_t word_reverse_complement_gatb(uint64_t x, size_t k_) {
  uint64_t res = x;
  res = ((res >> 2 & 0x3333333333333333) | (res & 0x3333333333333333) << 2);
  res = ((res >> 4 & 0x0F0F0F0F0F0F0F0F) | (res & 0x0F0F0F0F0F0F0F0F) << 4);
  res = ((res >> 8 & 0x00FF00FF00FF00FF) | (res & 0x00FF00FF00FF00FF) << 8);
  res = ((res >> 16 & 0x0000FFFF0000FFFF) | (res & 0x0000FFFF0000FFFF) << 16);
  res = ((res >> 32 & 0x00000000FFFFFFFF) | (res & 0x00000000FFFFFFFF) << 32);
  res = res ^ 0xAAAAAAAAAAAAAAAA;
  return (res >> (2 * (32 - k_)));
}

// Some template magic to detect if a template type has a ``length()'' function.
template <typename...> using combinelib_void_t = void;

template <typename, typename = void>
struct has_length : public std::false_type {};

template <typename T>
struct has_length<T, combinelib_void_t<decltype(T().length())>>
    : public std::true_type {};

/**
 * The first template parameter, K, is the maximum length (in nucleotides)
 * of the k-mer that can be represented with this class.
 *
 * The second template parameter, CID, is a class-type specific tag that
 * will allow all instances of this particular class to share a value of
 * their k.  This idea is used in Jellyfish, which inspired the use here.
 **/
template <uint64_t K, uint64_t CID = 0> class Kmer {
  static_assert(
      K <= 32,
      "Currently, the Kmer class can only represent k-mers of size <= 32");

public:
  using base_type = uint64_t;

  explicit Kmer() {}

  template <
      typename ViewT,
      typename = typename std::enable_if<has_length<ViewT>::value, void>::type>
  Kmer(ViewT& v) {
    fromChars(v);
  }


  // NOTE: the template below should take care of this, but doesn't on gcc 4.8.2
  // try and figure this out.
  Kmer(const char* iter) {
    fromCharsIter_(iter);
  }

  // NOTE: the template below should take care of this, but doesn't on gcc 4.8.2
  // try and figure this out.
  Kmer(std::string::iterator iter) {
    fromCharsIter_(iter);
  }


  template <
      typename IterT,
      typename = typename std::enable_if<!has_length<IterT>::value, void>::type>
  Kmer(IterT v) {
    fromCharsIter_(v);
  }

  Kmer(const Kmer& other) = default;
  Kmer(Kmer&& other) = default;
  Kmer(Kmer& other) = default;
  Kmer& operator=(Kmer& other) = default;
  Kmer& operator=(Kmer&& other) = default;

  // NOTE: the template below should take care of this, but doesn't on gcc 4.8.2
  // try and figure this out.
  Kmer& operator=(const char* iter) {
    fromCharsIter_(iter);
    return *this;
  }

  template <
      typename IterT,
       typename = typename std::enable_if<!has_length<IterT>::value, void>::type>
  Kmer& operator=(IterT iter) {
    fromCharsIter_(iter);
    return *this;
  }

  template <
      typename ViewT,
      typename = typename std::enable_if<has_length<ViewT>::value, void>::type>
  Kmer& operator=(ViewT& v) {
    fromChars(v);
    return *this;
  }

  // NOTE: the template below should take care of this, but doesn't on gcc 4.8.2
  // try and figure this out.
  bool fromChars(const char* iter) {
    return fromCharsIter_(iter);
  }
  // NOTE: the template below should take care of this, but doesn't on gcc 4.8.2
  // try and figure this out.
  bool fromChars(std::string::iterator iter) {
    return fromCharsIter_(iter);
  }

  /**
   * Populate this kmer by consuming characters pointed to by iter.
   *
   * @ASSUMPTIONS:
   *  There are at least k_ characters to consume or
   *  (2) we will encounter a non-nucleotide (i.e. \0) character
   **/
  template <
      typename IterT,
      typename = typename std::enable_if<!has_length<IterT>::value, void>::type>
  bool fromChars(IterT iter) {
    return fromCharsIter_(iter);
  }

  /**
   *  This is the same as the above function, but it will be called if the
   *argument type
   *  `ViewT` has a "length()" member.  In that case, the function will
   *additionally check
   *  that the length of `v` is >= k_.
   **/
  template <
      typename ViewT,
      typename = typename std::enable_if<has_length<ViewT>::value, void>::type>
  bool fromCharsSafe(ViewT& v) {
    return (v.length() >= k_) ? fromChars(v.begin()) : false;
  }

  /**
   *  This is a convenience function taht lets us call fromChars on a string, or
   *string_vew (or similar object);
   **/
  template <
      typename ViewT,
      typename = typename std::enable_if<has_length<ViewT>::value, void>::type>
  bool fromChars(ViewT& v) {
    return fromChars(v.begin());
  }
  bool fromChars(Kmer& k) {
      data_[0] = k.data_[0];
      return true;
  }
  /**
   * Append the character `c` to the end of the k-mer
   **/
  uint64_t append(char c) {
    auto r = (data_[0] >> (2 * k_ - 2)) & 0x03;
    data_[0] = maskTable[k_] & ((data_[0] << 2) | doEncodeBinary(c));
    return r;
  }

  /**
   * Prepend the character `c` to the beginning of the k-mer
   **/
  uint64_t prepend(char c) {
    auto r = (data_[0] & 0x03);
    data_[0] = (data_[0] >> 2) | (doEncodeBinary(c) << (2 * k_ - 2));
    return r;
  }

   /**
   * Append the character `c` to the end of the k-mer
   **/
  uint64_t append(int i) {
    auto r = (data_[0] >> (2 * k_ - 2)) & 0x03;
    data_[0] = maskTable[k_] & ((data_[0] << 2) | static_cast<base_type>(i));
    return r;
  }

  /**
   * Prepend the character `c` to the beginning of the k-mer
   **/
  uint64_t prepend(int i) {
    auto r = (data_[0] & 0x03);
    data_[0] = (data_[0] >> 2) | (static_cast<base_type>(i) << (2 * k_ - 2));
    return r;
  }

  /**
   * @returns a `uint64_t` that represents the encoded `idx`-th word of this
   *k-mer
   **/
  uint64_t word(uint32_t idx) const { return data_[idx]; }

  /**
   * @returns a reference to the `uint64_t` that represents the encoded `idx`-th
   *word of this k-mer
   **/
  uint64_t& word__(uint32_t idx) { return data_[idx]; }

  const base_type* data() const { return &data_[0]; }

  /**
   *  @returns the number of bytes required by this k-mer
   **/
  uint64_t sizeInBytes() const { return sizeof(data_); }

  /**
   *  @returns the number of words required by this k-mer
   **/
  uint64_t sizeInWords() const { return sizeof(data_) / sizeof(base_type); }

  /**
   *  @returns the number of words required by this k-mer
   **/
  uint64_t nb_words() const { return sizeInWords(); }

  /**
   * Set the dynamic length of this k-mer class to be kIn nucleotides.
   * @returns the value of k for this class prior to this update.
   **/
  static uint16_t k(uint16_t kIn) {
    assert(kIn < K);
    std::swap(k_, kIn);
    return kIn;
  }

  /**
   * @returns the value of k used for this k-mer class
   **/
  static uint16_t k() { return k_; }

  std::string toStr() const {
    std::string s(k_, 'X');
    auto& d = data_[0];
    int32_t offset = (2 * k_) - 2;
    for (int32_t idx = 0; offset >= 0; offset -= 2, ++idx) {
      s[idx] = decodeBinary((d >> offset & 0x03));
    }
    return s;
  }

  bool isHomoPolymer() const {
    auto nuc = data_[0] & 0x3;
    return (data_[0] == (maskTable[k_] & ((data_[0] << 2) | nuc)));
  }
  bool is_homopolymer() const { return isHomoPolymer(); }

  void rc() { data_[0] = word_reverse_complement(data_[0], k_); }

  Kmer<K, CID> getRC() const {
    Kmer<K, CID> nk;
    nk.data_[0] = word_reverse_complement(data_[0], k_);
    return nk;
  }

  void canonicalize() {
    auto wrc = word_reverse_complement(data_[0], k_);
    data_[0] = (wrc < data_[0]) ? wrc : data_[0];
  }

  Kmer<K, CID> getCanonical() {
    Kmer<K, CID> rck = getRC();
    return (rck < *this) ? rck : *this;
  }

  template <uint64_t KP, uint64_t CIDP>
  friend std::ostream& operator<<(std::ostream& os, const Kmer<KP, CIDP>& k);

  template <uint64_t KP, uint64_t CIDP>
  friend bool operator==(const Kmer<KP, CIDP>& lhs, const Kmer<KP, CIDP>& rhs);

  template <uint64_t KP, uint64_t CIDP>
  friend bool operator!=(const Kmer<KP, CIDP>& lhs, const Kmer<KP, CIDP>& rhs);

  template <uint64_t KP, uint64_t CIDP>
  friend bool operator<(const Kmer<KP, CIDP>& lhs, const Kmer<KP, CIDP>& rhs);

  template <uint64_t KP, uint64_t CIDP>
  friend bool operator>(const Kmer<KP, CIDP>& lhs, const Kmer<KP, CIDP>& rhs);

private:
  template <typename IterT>
  bool fromCharsIter_(IterT iter) {
    data_[0] = 0;
    auto toConsume = 1; // numWordsRequired(k_);
    int64_t code{0};
    bool success = true;
    int32_t remK = static_cast<int32_t>(k_);
    for (int32_t w = 0; w < toConsume; ++w) {
      int32_t shift = std::min((2 * remK) - 2, 62);
      auto& currWord = data_[w];
      for (; remK > 0 and shift >= 0; ++iter, --remK, shift -= 2) {
        // success &= encodeBinary(*iter, code);
        if (!encodeBinary(*iter, code))
          return false;
        currWord |= (code << shift);
      }
    }
    return success;
  }

  base_type data_[numWordsRequired(K)] = {};
  static uint16_t k_;
};

template <uint64_t K, uint64_t CID> uint16_t Kmer<K, CID>::k_ = 0;

template <uint64_t K, uint64_t CID>
std::ostream& operator<<(std::ostream& os, const Kmer<K, CID>& k) {
  os << k.toStr();
  return os;
}

template <uint64_t K, uint64_t CID>
bool operator==(const Kmer<K, CID>& lhs, const Kmer<K, CID>& rhs) {
  return lhs.data_[0] == rhs.data_[0];
}

template <uint64_t K, uint64_t CID>
bool operator!=(const Kmer<K, CID>& lhs, const Kmer<K, CID>& rhs) {
  return !(lhs == rhs);
}

template <uint64_t K, uint64_t CID>
bool operator<(const Kmer<K, CID>& lhs, const Kmer<K, CID>& rhs) {
  return (lhs.data_[0] < rhs.data_[0]);
}

template <uint64_t K, uint64_t CID>
bool operator>(const Kmer<K, CID>& lhs, const Kmer<K, CID>& rhs) {
  return (lhs.data_[0] > rhs.data_[0]);
}

} // namespace kmers
} // namespace combinelib

#endif // __COMBINELIB_KMERS_HPP__