Commit b73346fd authored by Mikaël Salson's avatar Mikaël Salson

Tools: Function for converting a nucleotide to an integer.

Use bitwise operations which avoids branching with if/switch.
Tests added
parent 42402816
......@@ -119,6 +119,18 @@ string complement(const string &dna) {
return comp;
}
int nuc_to_int(char nuc) {
// A : 1000 0001
// C : 1000 0011
// G : 1000 0111
// T : 1001 1000
// pos : 3210
// Bit de poids fort : b_3 ou b_2
// Bit de poids faible : xor entre b_3, b_2 et b_1
return (((nuc & 4) >> 1) | ((nuc & 8) >> 2)) // poids fort
| (((nuc & 8) >> 3) ^ ((nuc & 4) >> 2) ^ ((nuc & 2) >> 1));
}
Sequence create_sequence(string label_full, string label, string sequence, string quality) {
Sequence seq;
seq.label_full = label_full;
......
......@@ -58,6 +58,11 @@ char complement_nucleotide(char nuc);
*/
string complement(const string &dna);
/**
* @pre nuc est un nucléotide en majuscule (A, C, G ou T)
* @return le code entier du nucléotide (respectivement 0, 1, 2 ou 3)
*/
int nuc_to_int(char nuc);
string extract_from_label(string str, int field, string separator);
......
......@@ -38,8 +38,16 @@ void testCreateSequence() {
TAP_TEST(seq2.quality == "", TEST_CREATE_SEQUENCE_QUALITY, "");
}
void testNucToInt() {
TAP_TEST(nuc_to_int('A') == 0, TEST_NUC_TO_INT, "");
TAP_TEST(nuc_to_int('C') == 1, TEST_NUC_TO_INT, "");
TAP_TEST(nuc_to_int('G') == 2, TEST_NUC_TO_INT, "");
TAP_TEST(nuc_to_int('T') == 3, TEST_NUC_TO_INT, "");
}
void testTools() {
testFasta1();
testRevcomp();
testCreateSequence();
testNucToInt();
}
......@@ -12,6 +12,7 @@ enum {
TEST_CREATE_SEQUENCE_LABEL,
TEST_CREATE_SEQUENCE_SEQUENCE,
TEST_CREATE_SEQUENCE_QUALITY,
TEST_NUC_TO_INT,
/* Storage tests */
TEST_ARRAY_KMERSTORE,
......@@ -97,6 +98,7 @@ inline void declare_tests() {
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_LABEL, "create_sequence: label field");
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_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