kmerstore.h 17.4 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1 2 3 4 5 6 7 8
#ifndef KMERSTORE_H
#define KMERSTORE_H

#include <map>
#include <string>
#include <list>
#include <stdexcept>
#include <stdint.h>
9
#include <math.h>
10
#include "bioreader.hpp"
Mikaël Salson's avatar
Mikaël Salson committed
11 12 13 14
#include "tools.h"

using namespace std;

15 16 17
typedef
enum { KMER_INDEX, AC_AUTOMATON } IndexTypes;

18
#define MAX_PRECOMPUTED_PROBA 500 /* Precompute 500 probabilities for each index load */
Mikaël Salson's avatar
Mikaël Salson committed
19 20 21 22 23
class Kmer {
public:
  unsigned int count;

  Kmer();
24 25 26 27

  /**
   * This constructor is used via a IKmerStore<Kmer> index (hence the argument list)       
   */
28
  Kmer(const string &label, int strand=1, size_t length=0);
29

Mikaël Salson's avatar
Mikaël Salson committed
30
  Kmer &operator+=(const Kmer &);
31

32 33 34 35 36
  /**
   * @return the length of the kmer (only there for compatibility with KmerAffect)
   */
  size_t getLength() const;

37 38 39 40
  /**
   * When indexing revcomp, should the value be the same or not?
   * In other words, does the information stored by the class is strand-dependent?
   */
Mikaël Salson's avatar
Mikaël Salson committed
41
  static bool hasRevcompSymetry();
42 43 44 45

  /**
   * @return true if the element is the same as when initialised with default constructor.
   */
46
  bool isNull() const;
Cyprien Borée's avatar
Cyprien Borée committed
47 48
	
	string getLabel() const;
49 50 51 52 53 54

  /**
   * @return true iff the kmer is unknown (which doesn't make sense here but
   * it only there for a reason of compatibility with KmerAffect)
   */
  bool isUnknown() const;
Mikaël Salson's avatar
Mikaël Salson committed
55 56
} ;
ostream &operator<<(ostream &os, const Kmer &kmer);
57 58 59 60 61 62 63
bool operator==(const Kmer &k1, const Kmer &k2);
bool operator<(const Kmer &k1, const Kmer &k2);
bool operator>(const Kmer &k1, const Kmer &k2);
bool operator<=(const Kmer &k1, const Kmer &k2);
bool operator>=(const Kmer &k1, const Kmer &k2);
bool operator!=(const Kmer &k1, const Kmer &k2);

Mikaël Salson's avatar
Mikaël Salson committed
64 65 66 67 68 69 70 71 72 73 74


/* K-mer indexing */

template <class T> class IKmerStore
{
protected:
  bool revcomp_indexed;
  int k; // weight of the seed
  int s; // span of the seed (s >= k)
  string seed ;
75
  size_t nb_kmers_inserted;
76
  size_t max_size_indexing;
77
  bool finished_building;
78 79
  map<float, vector<double> > precomputed_proba_with_system,
    precomputed_proba_without_system;
Mikaël Salson's avatar
Mikaël Salson committed
80 81 82 83 84

public:

  virtual ~IKmerStore();

85 86
  static int last_id;
  int id; // id of this index
87
  int refs; // number of germlines using this index
88
  bool multiple_in_one;
89

90
  list< pair <T, BioReader> > labels;
91

92 93
  IKmerStore();

Mikaël Salson's avatar
Mikaël Salson committed
94 95 96
  /**
   * @param input: A single FASTA file
   * @param label: label that must be associated to the given files
97
   * @param seed: the seed to use for indexing. By default it will be the seed of the index
98
   * @post All the sequences in the FASTA files have been indexed, and the label is stored in the list of labels
Mikaël Salson's avatar
Mikaël Salson committed
99
   */
100
  void insert(BioReader& input, const string& label="", int keep_only = 0, string seed = "");
Mikaël Salson's avatar
Mikaël Salson committed
101 102 103 104

  /**
   * @param input: A list of FASTA files
   * @param label: label that must be associated to the given files
105
   * @param seed: the seed to use for indexing. By default it will be the seed of the index
106
   * @post All the sequences in the FASTA files have been indexed, and the label is stored in the list of labels
Mikaël Salson's avatar
Mikaël Salson committed
107
   */
108
  void insert(list<BioReader>& input, const string& label="", int keep_only = 0, string seed = "");
Mikaël Salson's avatar
Mikaël Salson committed
109 110 111 112
  
  /**
   * @param input: A sequence to be cut in k-mers
   * @param label: label that must be associated to the given files
113 114 115 116
   * @param keep_only: if > 0 will keep at most the last keep_only nucleotides
   *                   of the sequence. if < 0 will keep at most the first
   *                   keep_only nucleotides of the sequence. if == 0,
   *                   will keep all the sequence.
117
   * @param seed: the seed to use for indexing. By default it will be the seed of the index. Also note that when the index ! hasDifferentKmerTypes(), the seed of the index will always be used (and therefore the parameter will be ignored)
Mikaël Salson's avatar
Mikaël Salson committed
118 119
   * @post All the k-mers in the sequence have been indexed.
   */
120 121 122 123 124 125 126 127 128 129
  virtual void insert(const seqtype &sequence,
                      const string &label,
                      bool ignore_extended_nucleotides=true, int keep_only = 0,
                      string seed="");

  /**
   * Perform extra steps to finish the building of the index. After using
   * that function, we are not supposed to insert any other sequence.
   * No query function should be called before calling that function.
   */
130
  virtual void finish_building();
Mikaël Salson's avatar
Mikaël Salson committed
131 132 133 134 135

  /**
   * @param word: a k-mer
   * @return the value for the kmer as stored in the structure (don't do revcomp)
   */ 
136
  virtual T& get(seqtype &word) = 0;
Mikaël Salson's avatar
Mikaël Salson committed
137

138
  /**
139 140 141 142
   * @return the percentage of kmers that are set in the index.
   *         When ! hasDifferentKmerTypes(), the index load is always the same
   *         (apart for the unknown kmer).
   *         When kmer.isUnknown(), we return the load of all th other kmers.
143
   */
144
  virtual float getIndexLoad(T kmer) const;
145

146 147 148 149 150 151
  /**
   * @return the given integer (size of a read),
   *         but limit this size to max_size_indexing if it was defined
   */
  int atMostMaxSizeIndexing(int n) const;

152 153 154
  /**
   * @return probability that the number of kmers is 'at_least' or more in a sequence of length 'length'
   */
155
  double getProbabilityAtLeastOrAbove(T kmer, int at_least, int length);
156

Mikaël Salson's avatar
Mikaël Salson committed
157 158 159 160 161 162 163 164 165 166
  /**
   * @return the value of k
   */
  int getK() const;

  /**
   * @return the value of s
   */
  int getS() const;

167 168 169 170 171
  /**
   * @return the seed used
   */
  string getSeed() const;

172 173 174 175
  /**
   * @param kmer: a kmer
   * @return one label associated with the kmer
   */
176
  BioReader getLabel(T kmer) const;
177

178 179 180 181 182
  /**
   * @return whether the index differentiate kmer types
   */
  virtual bool hasDifferentKmerTypes() const;

Mikaël Salson's avatar
Mikaël Salson committed
183 184 185 186
  /**
   * @param seq: a sequence
   * @param no_revcomp: force not to revcomp the sequence, even if
   *                    the index was built with revcomp.
187 188
   * @return a vector of length seq.length() - d, where d is the span
   *         of the seed (minus one) or is 0 when it doesn't apply.
189
   * containing for each position the corresponding value in the index.
Mikaël Salson's avatar
Mikaël Salson committed
190
   */
191
  virtual vector<T> getResults(const seqtype &seq, bool no_revcomp=false, string seed="") = 0;
Mikaël Salson's avatar
Mikaël Salson committed
192 193 194 195 196 197 198 199 200 201 202 203

  /**
   * @return true iff the revcomp is indexed
   */
  bool isRevcomp() const;

  /**
   * @param string: a k-mer
   * @return the value in the index associated to the given k-mer.
   *         isRevcomp() ==> the revcomp of the k-mer may be considered
   * @pre word.length() == this->k
   */
204
  virtual T& operator[](seqtype& word) = 0;
205 206 207 208

 protected:

  void precompute_proba(float index_load);
Mikaël Salson's avatar
Mikaël Salson committed
209 210
};

211 212 213
template<class T>
IKmerStore<T>::IKmerStore() {
  id = ++last_id;
Mikaël Salson's avatar
Mikaël Salson committed
214
  refs = 0;
215
  finished_building = false;
216
  multiple_in_one = false;
217 218
}

219
template<class T> int IKmerStore<T>::last_id = 0;
Mikaël Salson's avatar
Mikaël Salson committed
220 221 222 223 224

template <class T> 
class MapKmerStore : public IKmerStore<T>
{
public:	
225
  map<seqtype, T> store;
Mikaël Salson's avatar
Mikaël Salson committed
226 227 228 229 230 231 232

  /**
   * @param bool: if true, will also index revcomp
   * @param seed: the seed
   */
  MapKmerStore(string seed, bool=false);
  MapKmerStore(int k, bool=false);
233

234
  vector<T> getResults(const seqtype &seq, bool no_revcomp=false, string seed="");
235 236
  T& get(seqtype &word);
  T& operator[](seqtype & word);
237 238 239

 private:
  void init();
Mikaël Salson's avatar
Mikaël Salson committed
240 241 242 243 244 245 246
};

template <class T> 
class ArrayKmerStore : public IKmerStore<T>
{
  T* store;
	
247
  int index(const seqtype& word) const;
248 249 250 251 252 253 254 255

  /**
   * Allocates memory for store
   * @throws bad_alloc: when there is not enough memory or when the
   *                    the total number of kmers cannot be stored in an 
   *                    unsigned int
   */
  void init();
Mikaël Salson's avatar
Mikaël Salson committed
256 257 258 259 260 261 262 263
public:
  /**
   * @param bool: if true, will also index revcomp
   * @param seed: the seed
   */
  ArrayKmerStore(string seed, bool=false);
  ArrayKmerStore(int k, bool=false);
  ~ArrayKmerStore();
264

265
  vector<T> getResults(const seqtype &seq, bool no_revcomp=false, const string seed="");
266 267
  T& get(seqtype &word);
  T& operator[](seqtype & word);
268
  T& operator[](int word);
Mikaël Salson's avatar
Mikaël Salson committed
269 270 271 272 273 274 275 276 277
};


// IKmerStore

template<class T>
IKmerStore<T>::~IKmerStore(){}

template<class T> 
278
void IKmerStore<T>::insert(list<BioReader>& input,
279
                           const string &label,
280 281
                           int keep_only,
                           string seed){
282
  for(list<BioReader>::iterator it = input.begin() ; it != input.end() ; it++){
283
    insert(*it, label, keep_only, seed);
Mikaël Salson's avatar
Mikaël Salson committed
284 285 286 287
  }
}

template<class T> 
288
void IKmerStore<T>::insert(BioReader& input,
289
                           const string &label,
290 291
                           int keep_only,
                           string seed){
Mikaël Salson's avatar
Mikaël Salson committed
292
  for (int r = 0; r < input.size(); r++) {
293
    insert(input.sequence(r), label, true, keep_only, seed);
Mikaël Salson's avatar
Mikaël Salson committed
294
  }
295

296
  labels.push_back(make_pair(T(label, 1, seed.size()), input)) ;
297 298

  if (revcomp_indexed  && ! T::hasRevcompSymetry()) {
299
    labels.push_back(make_pair(T(label, -1, seed.size()), input)) ;
300
  }
Mikaël Salson's avatar
Mikaël Salson committed
301 302 303
}

template<class T> 
304
void IKmerStore<T>::insert(const seqtype &sequence,
305
                           const string &label,
306
                           bool ignore_extended_nucleotides,
307 308
                           int keep_only,
                           string seed){
309 310 311 312 313 314 315
  size_t start_indexing = 0;
  size_t end_indexing = sequence.length();
  if (keep_only > 0 && sequence.length() > (size_t)keep_only) {
    start_indexing = sequence.length() - keep_only;
  } else if (keep_only < 0 && sequence.length() > (size_t) -keep_only) {
    end_indexing = -keep_only;
  }
316

317
  if (seed.empty() || ! this->hasDifferentKmerTypes())
318 319 320
    seed = this->seed;

  size_t seed_span = seed.length();
321 322 323 324 325
  size_t size_indexing = end_indexing - start_indexing;
  if (size_indexing > max_size_indexing) {
    max_size_indexing = size_indexing;
  }

326 327
  for(size_t i = start_indexing ; i + seed_span < end_indexing + 1 ; i++) {
    seqtype substr = sequence.substr(i, seed_span);
328
    seqtype kmer = spaced(substr, seed);
329

330
    if (ignore_extended_nucleotides && has_extended_nucleotides(kmer))
331 332
      continue;

Mikaël Salson's avatar
Mikaël Salson committed
333 334
    int strand = 1;
    if (revcomp_indexed && T::hasRevcompSymetry()) {
335
      seqtype rc_kmer = revcomp(kmer);
Mikaël Salson's avatar
Mikaël Salson committed
336 337 338 339 340
      if (rc_kmer.compare(kmer) < 0) {
        strand = -1;
        kmer = rc_kmer;
      }
    }
341 342 343 344
    T &this_kmer = this->get(kmer);
    if (this_kmer.isNull()) {
      nb_kmers_inserted++;
    }
345
    this_kmer += T(label, strand, seed.size());
Mikaël Salson's avatar
Mikaël Salson committed
346
    if (revcomp_indexed && ! T::hasRevcompSymetry()) {
347
      seqtype rc_kmer = spaced(revcomp(substr), seed);
348 349 350
      T &this_rc_kmer = this->get(rc_kmer);
      if (this_rc_kmer.isNull())
        nb_kmers_inserted++;
351
      this_rc_kmer += T(label, -1, seed.size());
Mikaël Salson's avatar
Mikaël Salson committed
352 353 354
    }
  }
}
355

356
template<class T>
357 358 359
void IKmerStore<T>::finish_building() {
  finished_building = true;
}
360

361 362
template<class T>
float IKmerStore<T>::getIndexLoad(const T kmer) const {
363
  float index_load = nb_kmers_inserted / pow(4.0, k);
364
  return (kmer.isUnknown()) ? 1 - index_load : index_load;
365
}
Mikaël Salson's avatar
Mikaël Salson committed
366

367 368 369 370 371 372 373
template<class T>
int IKmerStore<T>::atMostMaxSizeIndexing(int n) const {
  if (!max_size_indexing || n < max_size_indexing)
    return n ;

  return max_size_indexing ;
}
374 375

template<class T>
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
void IKmerStore<T>::precompute_proba(float index_load) {
  precomputed_proba_with_system[index_load] = vector<double>(MAX_PRECOMPUTED_PROBA);
  precomputed_proba_without_system[index_load] = vector<double>(MAX_PRECOMPUTED_PROBA);

  vector<double> &pproba_with = precomputed_proba_with_system[index_load];
  vector<double> &pproba_without = precomputed_proba_without_system[index_load];

  pproba_with[0] = 1;
  pproba_without[0] = 1;
  for (int i = 1; i < MAX_PRECOMPUTED_PROBA; i++) {
    pproba_with[i] = pproba_with[i - 1] * index_load;
    pproba_without[i] = pproba_without[i - 1] * (1 - index_load);
  }
}

template<class T>
double IKmerStore<T>::getProbabilityAtLeastOrAbove(const T kmer, int at_least, int length)  {
393

394 395
  if (at_least == 0) return 1.0; // even if 'length' is very small
  
396 397
  // n: number of kmers in the sequence
  int n = length - getS() + 1;
398
  float index_load = getIndexLoad(kmer) ;
399 400 401
  if (! precomputed_proba_without_system.count(index_load)) {
    precompute_proba(index_load);
  }
402 403

  double proba = 0;
404 405 406 407 408 409 410 411 412 413 414

  double probability_not_having_system;
  double probability_having_system;
  if (precomputed_proba_with_system.at(index_load).size() > (size_t)at_least)
    probability_having_system = precomputed_proba_with_system.at(index_load)[at_least];
  else
    probability_having_system = pow(index_load, at_least);
  if (precomputed_proba_without_system.at(index_load).size() > (size_t)n - at_least)
    probability_not_having_system = precomputed_proba_without_system.at(index_load)[n-at_least];
  else
    probability_not_having_system = pow(1 - index_load, n - at_least);
415 416 417 418 419 420
  for (int i=at_least; i<=n; i++) {
    proba += nChoosek(n, i) * probability_having_system * probability_not_having_system;
    probability_having_system *= index_load;
    probability_not_having_system /= (1 - index_load);
  }

421 422 423 424
#ifdef DEBUG_KMS_EVALUE
  cerr << "e-value:\tindex_load=" << index_load << ",\tat_least=" << at_least << ",\tlength=" << length <<",\tp-value=" << proba << endl;
#endif

425 426 427 428 429
  return proba;
}



Mikaël Salson's avatar
Mikaël Salson committed
430 431 432 433 434 435 436 437 438 439
template<class T>
int IKmerStore<T>::getK() const {
  return k;
}

template<class T>
int IKmerStore<T>::getS() const {
  return s;
}

440 441 442 443 444
template<class T>
string IKmerStore<T>::getSeed() const {
  return seed;
}

445
template<class T>
446 447
BioReader IKmerStore<T>::getLabel(T kmer) const {
  for (typename list< pair<T, BioReader> >::const_iterator it = labels.begin(); it != labels.end(); ++it)
448 449
    if (it->first == kmer)
      return it->second ;
450 451 452 453 454 455
  // Nothing interesting found
  // Try by ignoring length if the index is not able to deal with different lengths
  if (! hasDifferentKmerTypes() && kmer.getLength() != (unsigned char)~0) {
    kmer.setLength(~0);
    return getLabel(kmer);
  }
456
  return BIOREADER_AMBIGUOUS ;
457
}
458

459 460 461 462 463
template<class T>
bool IKmerStore<T>::hasDifferentKmerTypes() const {
  return false;
}

464
// .getResults()
Mikaël Salson's avatar
Mikaël Salson committed
465
template<class T>
466
vector<T> MapKmerStore<T>::getResults(const seqtype &seq, bool no_revcomp, string seed) {
467

Mikaël Salson's avatar
Mikaël Salson committed
468
  string local_seed = seed;
469
  if (! seed.size())
Mikaël Salson's avatar
Mikaël Salson committed
470 471
    local_seed = IKmerStore<T>::seed;
  int s = local_seed.size();
472

Mikaël Salson's avatar
Mikaël Salson committed
473 474 475 476 477
  if ((int)seq.length() < s - 1) {
    return vector<T>(0);
  }
  vector<T> result(seq.length() - s + 1);
  for (size_t i=0; i + s < seq.length() + 1; i++) {
Mikaël Salson's avatar
Mikaël Salson committed
478
    seqtype kmer = spaced(seq.substr(i, s), local_seed);
479
    //    seqtype kmer = seq.substr(i, s);
Mikaël Salson's avatar
Mikaël Salson committed
480
    // cout << kmer << endl << kmer0 << endl << endl ;
481
    if (IKmerStore<T>::revcomp_indexed && no_revcomp) {
Mikaël Salson's avatar
Mikaël Salson committed
482 483 484 485 486 487 488 489
      result[i] = get(kmer);
    } else {
      result[i] = (*this)[kmer];
    }
  }
  return result;
}

490
template<class T>
491
vector<T> ArrayKmerStore<T>::getResults(const seqtype &seq, bool no_revcomp, string seed) {
Mikaël Salson's avatar
Mikaël Salson committed
492 493

  string local_seed = seed;
494
  if (! seed.size())
Mikaël Salson's avatar
Mikaël Salson committed
495 496
    local_seed = IKmerStore<T>::seed;
  int s = local_seed.size();
497 498 499 500 501 502 503 504 505 506 507 508

  int N = (int)seq.length();

  if (N < s - 1) {
    return vector<T>(0);
  }
  vector<T> result(N - s + 1);

  /* Read once the sequence, convert it to int* */
  int* intseq = new int[N];
  for (int i=0; i<N; i++)
    {
509
      intseq[i] = nuc_to_int(seq[i]);
510 511 512 513
    }

  /* Compute results */
  for (size_t i=0; (int) i+s < N+1; i++) {
Mikaël Salson's avatar
Mikaël Salson committed
514
    int kmer = spaced_int(intseq + i, local_seed);
515 516 517 518
    if (IKmerStore<T>::revcomp_indexed && no_revcomp) {
      result[i] = store[kmer]; // getfromint(kmer); // store[kmer];
      // cout << i << "/" << N << "  " << kmer << result[i] << endl ;
    } else {
519
      result[i] = (*this)[kmer]; // Deals with revcomp
520 521 522 523 524 525 526
    }
  }

  delete[] intseq ;
  return result;
}

Mikaël Salson's avatar
Mikaël Salson committed
527 528 529 530 531 532 533 534 535

template<class T>
bool IKmerStore<T>::isRevcomp() const {
  return revcomp_indexed;
}

// MapKmerStore

template <class T>
536
MapKmerStore<T>::MapKmerStore(string seed, bool revcomp):IKmerStore<T>(){
Mikaël Salson's avatar
Mikaël Salson committed
537 538 539 540 541
  this->seed = seed;   
  int k = seed_weight(seed);
  this->k = k;
  this->s = seed.size();
  this->revcomp_indexed = revcomp;
542
  init();
Mikaël Salson's avatar
Mikaël Salson committed
543 544 545
}

template <class T>
546
MapKmerStore<T>::MapKmerStore(int k, bool revcomp):IKmerStore<T>(){
Mikaël Salson's avatar
Mikaël Salson committed
547 548 549 550
  this->seed = seed_contiguous(k);
  this->k = k;
  this->s = k;
  this->revcomp_indexed = revcomp;
551 552 553 554 555 556
  init();
}

template <class T>
void MapKmerStore<T>::init() {
  this->nb_kmers_inserted = 0;
557
  this->max_size_indexing = 0;
Mikaël Salson's avatar
Mikaël Salson committed
558 559 560
}

template <class T> 
561
T& MapKmerStore<T>::get(seqtype& word){
Mikaël Salson's avatar
Mikaël Salson committed
562 563 564 565
  return store[word];
}

template <class T> 
566
T& MapKmerStore<T>::operator[](seqtype& word){
Mikaël Salson's avatar
Mikaël Salson committed
567
  if (this->isRevcomp() && T::hasRevcompSymetry()) {
568
    seqtype rc_kmer = revcomp(word);
Mikaël Salson's avatar
Mikaël Salson committed
569 570 571 572 573 574 575 576 577
    if (rc_kmer.compare(word) < 0)
      word = rc_kmer;
  }
  return store[word];
}

// ArrayKmerStore

template <class T> 
578
ArrayKmerStore<T>::ArrayKmerStore(int k, bool revcomp):IKmerStore<T>() {
Mikaël Salson's avatar
Mikaël Salson committed
579 580 581 582
  this->seed = seed_contiguous(k);
  this->k = k;
  this->s = k;
  this->revcomp_indexed = revcomp;
583
  init();
Mikaël Salson's avatar
Mikaël Salson committed
584 585 586 587
}


template <class T> 
588
ArrayKmerStore<T>::ArrayKmerStore(string seed, bool revcomp):IKmerStore<T>(){
Mikaël Salson's avatar
Mikaël Salson committed
589 590 591 592 593
  this->seed = seed; 
  int k = seed_weight(seed);
  this->k = k;
  this->s = seed.size();
  this->revcomp_indexed = revcomp;
594 595 596 597 598
  init();
}

template <class T>
void ArrayKmerStore<T>::init() {
599
  this->nb_kmers_inserted = 0;
600
  this->max_size_indexing = 0;
601
  if ((size_t)(this->k << 1) >= sizeof(int) * 8)
Mikaël Salson's avatar
Mikaël Salson committed
602
    throw std::bad_alloc();
603
  store = new T[(unsigned int)1 << (this->k << 1)];
Mikaël Salson's avatar
Mikaël Salson committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617
  if (! store)
    throw std::bad_alloc();
}

template <class T> 
ArrayKmerStore<T>::~ArrayKmerStore(){
  delete [] store;
}

/**	Returns index of word in an array of size 4^k
	Considering B(A) = 0, B(C) = 1, B(T) = 2, B(G) = 3
	index_word = \sum_{i=0}^{k-1}B(word[i])*4^(k-i-1)
**/
template <class T> 
618
int ArrayKmerStore<T>::index(const seqtype& word) const{
Mikaël Salson's avatar
Mikaël Salson committed
619
  return dna_to_int(word, this->k);
Mikaël Salson's avatar
Mikaël Salson committed
620 621 622
}

template <class T> 
623
T& ArrayKmerStore<T>::get(seqtype& word){
Mikaël Salson's avatar
Mikaël Salson committed
624 625 626 627
  return store[index(word)];
}

template <class T> 
628
T& ArrayKmerStore<T>::operator[](seqtype& word){
629 630 631 632 633
  return (*this)[index(word)];
}

template <class T> 
T& ArrayKmerStore<T>::operator[](int word){
Mikaël Salson's avatar
Mikaël Salson committed
634
  if (this->isRevcomp() && T::hasRevcompSymetry()) {
635 636
    int rc_kmer = revcomp_int(word, IKmerStore<T>::k);
    if (rc_kmer < word)
Mikaël Salson's avatar
Mikaël Salson committed
637 638
      word = rc_kmer;
  }
639
  return store[word];
Mikaël Salson's avatar
Mikaël Salson committed
640 641 642
}

#endif