testStorage.cpp 3.25 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
2
3
4
5
#include <core/fasta.h>
#include <core/kmerstore.h>
#include "tests.h"
 
template<template <class> class T>
Mikaël Salson's avatar
Mikaël Salson committed
6
void testKmerStoreWithKmerSimple(int k, bool revcomp, int test_id ) {
Mikaël Salson's avatar
Mikaël Salson committed
7
  T<Kmer> *index = createIndex<T<Kmer> >(k, revcomp);
8
9

  TAP_TEST(k == index->getK(), TEST_KMERSTORE_GET_K, "");
Mikaël Salson's avatar
Mikaël Salson committed
10
11
12
13
  
  for (int i = 0; i < nb_seq-2; i++) {
    string tmp = seq[2*i].substr(0, k);
    if (revcomp) {
Mikaël Salson's avatar
Mikaël Salson committed
14
      string rc = ::revcomp(tmp);
Mikaël Salson's avatar
Mikaël Salson committed
15
      TAP_TEST((*index)[tmp].count == (seq[2*i].length()-k+1)*2, test_id, "");
Mikaël Salson's avatar
Mikaël Salson committed
16
      TAP_TEST((*index)[tmp].count == (*index)[rc].count, test_id, "K-mer and its revcomp should have the same count");
Mikaël Salson's avatar
Mikaël Salson committed
17
18
19
20
21
22
23
    } else {
      TAP_TEST((*index)[tmp].count == (seq[2*i].length()-k+1), test_id, "");
    }
  }
  delete index;
}

Mikaël Salson's avatar
Mikaël Salson committed
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
template<template <class> class T>
void testKmerStoreWithKmer(int k, int test_id) {
  T<Kmer> *index = new T<Kmer>(k, true);
  T<Kmer> *index2 = new T<Kmer>(k, true);
  Fasta reads("../../data/representative_revcomp.fa");

  index->insert(reads);
  list<Sequence> readCollection = reads.getAll();

  for (list<Sequence>::iterator it = readCollection.begin(); it != readCollection.end(); it++) {
    it->sequence = revcomp(it->sequence);
  }

  index2->insert(reads);

  // Traverse read collection
  for (int i = 0; i < reads.size(); i++) {
    vector<Kmer> counts = index->getResults(reads.sequence(i));
    vector<Kmer> counts2 = index2->getResults(reads.sequence(i));

    TAP_TEST(counts.size() == counts2.size(), test_id, "The list of kmer counts should have the same size in both cases");
    for (size_t j = 0; j < counts.size(); j++) {
      TAP_TEST(counts[j].count == counts2[j].count, test_id, "The count of kmers should be the same when reading in opposite ways" );
    }
  }
49
50
51
52

  // Test with no result
  vector<Kmer> counts = index->getResults("ATTA");
  TAP_TEST(counts.size() == 0, test_id, "");
Mikaël Salson's avatar
Mikaël Salson committed
53
54
55
56
57
  
  delete index;
  delete index2;
}

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
template<template <class> class KmerStore>
void testKmerStoreSeed() {
  KmerStore<Kmer> *index = new KmerStore<Kmer>(8, true);

  TAP_TEST(index->getK() == 8, TEST_KMERSTORE_GET_K, "");
  TAP_TEST(index->getS() == 8, TEST_KMERSTORE_GET_S, "");
  TAP_TEST(index->getSeed() == "########", TEST_KMERSTORE_GET_SEED, "");
  delete index;

  string seed = "#####-#####";
  index = new KmerStore<Kmer>(seed, true);
  TAP_TEST(index->getK() == 10, TEST_KMERSTORE_GET_K, "");
  TAP_TEST(index->getS() == 11, TEST_KMERSTORE_GET_S, "");
  TAP_TEST(index->getSeed() == seed, TEST_KMERSTORE_GET_SEED, "");

  delete index;

  seed = "##-##-##-##";
  index = new KmerStore<Kmer>(seed, true);
  TAP_TEST(index->getK() == 8, TEST_KMERSTORE_GET_K, "");
  TAP_TEST(index->getS() == 11, TEST_KMERSTORE_GET_S, "");
  TAP_TEST(index->getSeed() == seed, TEST_KMERSTORE_GET_SEED, "");

  delete index;
}

Mikaël Salson's avatar
Mikaël Salson committed
84
void testStorage() {
Mikaël Salson's avatar
Mikaël Salson committed
85
86
87
88
89
  testKmerStoreWithKmerSimple<ArrayKmerStore>(5, false, TEST_ARRAY_KMERSTORE);
  testKmerStoreWithKmerSimple<ArrayKmerStore>(5, true, TEST_ARRAY_KMERSTORE_RC);

  testKmerStoreWithKmerSimple<MapKmerStore>(5, false, TEST_MAP_KMERSTORE);
  testKmerStoreWithKmerSimple<MapKmerStore>(5, true, TEST_MAP_KMERSTORE_RC);
Mikaël Salson's avatar
Mikaël Salson committed
90

Mikaël Salson's avatar
Mikaël Salson committed
91
92
  testKmerStoreWithKmer<ArrayKmerStore>(10, TEST_ARRAY_KMERSTORE_RC);
  testKmerStoreWithKmer<MapKmerStore>(14, TEST_MAP_KMERSTORE_RC);
93
94
95

  testKmerStoreSeed<ArrayKmerStore>();
  testKmerStoreSeed<MapKmerStore>();
Mikaël Salson's avatar
Mikaël Salson committed
96
}