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

Tools: revcomp_int performs revcomp on an integer.

Revcomp directly on an integer.
Tests added
parent 6870d879
......@@ -207,6 +207,17 @@ string revcomp(const string &dna, bool do_revcomp) {
return rcomp;
}
int revcomp_int(int word, int size) {
int revcomp = 0;
while (size) {
revcomp <<= 2;
revcomp |= (word & 3) ^ 3;
word >>= 2;
size--;
}
return revcomp;
}
string reverse(const string &text) {
return string(text.rbegin(), text.rend());
}
......@@ -84,6 +84,12 @@ int remove_trailing_whitespaces(string &str);
*/
string revcomp(const string &dna, bool do_revcomp = true);
/**
* @return the int value corresponding to the revcomp of the DNA sequence
* represented by word, whose length (in number of nucleotides) is size.
*/
int revcomp_int(int word, int size);
/**
* @return the reverse of text (ie. text read from right to left)
*/
......
......@@ -52,10 +52,48 @@ void testDNAToInt() {
TAP_TEST(dna_to_int("TTTT", 4) == 255, TEST_DNA_TO_INT, "");
}
void testRevcompInt() {
TAP_TEST(revcomp_int(dna_to_int("AA", 2), 2) == dna_to_int("TT", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("AC", 2), 2) == dna_to_int("GT", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("AG", 2), 2) == dna_to_int("CT", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("AT", 2), 2) == dna_to_int("AT", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("CA", 2), 2) == dna_to_int("TG", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("CC", 2), 2) == dna_to_int("GG", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("CG", 2), 2) == dna_to_int("CG", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("CT", 2), 2) == dna_to_int("AG", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("GA", 2), 2) == dna_to_int("TC", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("GC", 2), 2) == dna_to_int("GC", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("GT", 2), 2) == dna_to_int("AC", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("TA", 2), 2) == dna_to_int("TA", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("TC", 2), 2) == dna_to_int("GA", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("TG", 2), 2) == dna_to_int("CA", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("TT", 2), 2) == dna_to_int("AA", 2),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("AAAAAAA", 7), 7) == dna_to_int("TTTTTTT", 7),
TEST_REVCOMP_INT, "");
TAP_TEST(revcomp_int(dna_to_int("ATTAGGA", 7), 7) == dna_to_int("TCCTAAT", 7),
TEST_REVCOMP_INT, "revcomp: " << revcomp_int(dna_to_int("ATTAGGA", 7), 7) <<", dna_to_int: " << dna_to_int("TCCTAAT", 7));
}
void testTools() {
testFasta1();
testRevcomp();
testCreateSequence();
testNucToInt();
testDNAToInt();
testRevcompInt();
}
......@@ -14,6 +14,7 @@ enum {
TEST_CREATE_SEQUENCE_QUALITY,
TEST_NUC_TO_INT,
TEST_DNA_TO_INT,
TEST_REVCOMP_INT,
/* Storage tests */
TEST_ARRAY_KMERSTORE,
......@@ -101,6 +102,7 @@ inline void declare_tests() {
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_REVCOMP_INT, "revcomp_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