Commit 6870d879 authored by Mikaël Salson's avatar Mikaël Salson

Tools: dna_to_int

Was done in IKMerStore but we should provide it as a tool. Now IKmerStore uses the function dna_to_int.
Tests added.
parent 8dd80181
......@@ -340,11 +340,7 @@ ArrayKmerStore<T>::~ArrayKmerStore(){
**/
template <class T>
int ArrayKmerStore<T>::index(const seqtype& word) const{
int index_word = 0;
for(int i = 0 ; i < this->k ; i++){
index_word = (index_word << 2) | nuc_to_int(word[i]);
}
return index_word;
return dna_to_int(word, this->k);
}
template <class T>
......
......@@ -131,6 +131,14 @@ int nuc_to_int(char nuc) {
| (((nuc & 8) >> 3) ^ ((nuc & 4) >> 2) ^ ((nuc & 2) >> 1));
}
int dna_to_int(const string &word, int size) {
int index_word = 0;
for(int i = 0 ; i < size ; i++){
index_word = (index_word << 2) | nuc_to_int(word[i]);
}
return index_word;
}
Sequence create_sequence(string label_full, string label, string sequence, string quality) {
Sequence seq;
seq.label_full = label_full;
......
......@@ -64,6 +64,11 @@ string complement(const string &dna);
*/
int nuc_to_int(char nuc);
/**
* Convert size nucleotides from a DNA string to an integer.
*/
int dna_to_int(const string &, int size);
string extract_from_label(string str, int field, string separator);
/**
......
......@@ -45,9 +45,17 @@ void testNucToInt() {
TAP_TEST(nuc_to_int('T') == 3, TEST_NUC_TO_INT, "");
}
void testDNAToInt() {
TAP_TEST(dna_to_int("A", 1) == 0, TEST_DNA_TO_INT, "");
TAP_TEST(dna_to_int("AAAAAAA", 7) == 0, TEST_DNA_TO_INT, "");
TAP_TEST(dna_to_int("ATTAGGA", 7) == 3880, TEST_DNA_TO_INT, "");
TAP_TEST(dna_to_int("TTTT", 4) == 255, TEST_DNA_TO_INT, "");
}
void testTools() {
testFasta1();
testRevcomp();
testCreateSequence();
testNucToInt();
testDNAToInt();
}
......@@ -13,6 +13,7 @@ enum {
TEST_CREATE_SEQUENCE_SEQUENCE,
TEST_CREATE_SEQUENCE_QUALITY,
TEST_NUC_TO_INT,
TEST_DNA_TO_INT,
/* Storage tests */
TEST_ARRAY_KMERSTORE,
......@@ -99,6 +100,7 @@ inline void declare_tests() {
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_SEQUENCE, "create_sequence: sequence field");
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_QUALITY, "create_sequence: quality field");
RECORD_TAP_TEST(TEST_NUC_TO_INT, "nuc_to_int()");
RECORD_TAP_TEST(TEST_DNA_TO_INT, "dna_to_int()");
RECORD_TAP_TEST(TEST_ARRAY_KMERSTORE, "Testing ArrayKmerStore");
RECORD_TAP_TEST(TEST_MAP_KMERSTORE, "Testing MapKmerStore");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment