-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from OndrejSladky/optimize-masks
KmerCamel can optimize masks.
- Loading branch information
Showing
9 changed files
with
216 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
#include "parser.h" | ||
#include "khash_utils.h" | ||
#include "kmers.h" | ||
|
||
/// For the given masked superstring output the same superstring with mask with minimal/maximal number of ones. | ||
void OptimizeOnes(std::ifstream &in, std::ostream &of, kh_S64_t *kMers, int k, bool complements, bool minimize) { | ||
char c; | ||
int beforeKMerEnd = k; | ||
kmer_t currentKMer = 0; | ||
kmer_t mask = (((kmer_t) 1) << (2 * k) ) - 1; | ||
bool readingHeader = false; | ||
while (in >> std::noskipws >> c) { | ||
// Start of a header. | ||
if (c == '>') { | ||
readingHeader = true; | ||
currentKMer &= mask >> 1; | ||
for (char ch : NumberToKMer(currentKMer, k - beforeKMerEnd)) of << UpperToLower(ch); | ||
currentKMer = 0; | ||
beforeKMerEnd = k; | ||
} | ||
// Reprint the header. | ||
if (readingHeader) of << c; | ||
if (c == '\n') readingHeader = false; | ||
if (readingHeader) continue; | ||
auto data = NucleotideToInt(c); | ||
// Disregard white space. | ||
if (c == '\n' || c == '\r' || c == ' ') continue; | ||
// Break at an unknown character. | ||
if (data == -1) { | ||
currentKMer &= mask >> 1; | ||
for (char ch : NumberToKMer(currentKMer, k - beforeKMerEnd)) of << UpperToLower(ch); | ||
currentKMer = 0; | ||
beforeKMerEnd = k; | ||
continue; | ||
} | ||
currentKMer <<= 2; | ||
currentKMer &= mask; | ||
currentKMer |= data; | ||
--beforeKMerEnd; | ||
kmer_t reverseComplement = ReverseComplement(currentKMer, k); | ||
if (beforeKMerEnd == 0) { | ||
++beforeKMerEnd; | ||
char to_print = NucleotideAtIndex(currentKMer, k, 0); | ||
auto key = kh_get_S64(kMers, currentKMer); | ||
if (key != kh_end(kMers)) { | ||
if (minimize) { | ||
kh_del_S64(kMers, key); | ||
} | ||
} else if (complements && (key = kh_get_S64(kMers, reverseComplement)) != kh_end(kMers)) { | ||
if (minimize) { | ||
kh_del_S64(kMers, key); | ||
} | ||
} else { | ||
to_print = UpperToLower(to_print); | ||
} | ||
of << to_print; | ||
} | ||
} | ||
currentKMer &= mask >> 1; | ||
for (char ch : NumberToKMer(currentKMer, k - beforeKMerEnd)) of << UpperToLower(ch); | ||
of << "\n"; | ||
} | ||
|
||
int Optimize(std::string &algorithm, std::string path, std::ostream &of, int k, bool complements) { | ||
kh_S64_t *kMers = kh_init_S64(); | ||
ReadKMers(kMers, path, k, complements, true); | ||
|
||
std::ifstream in(path); | ||
if (in.is_open()) { | ||
if (algorithm == "ones") { | ||
OptimizeOnes(in, of, kMers, k, complements, false); | ||
} else if (algorithm == "zeros") { | ||
OptimizeOnes(in, of, kMers, k, complements, true); | ||
} else { | ||
if (algorithm == "runs") { | ||
std::cerr | ||
<< "Minimization of the number of runs of ones is not supported. Please use the Python script from supplement." | ||
<< std::endl; | ||
} else { | ||
std::cerr << "Algorithm '" + algorithm + "' not recognized." << std::endl; | ||
} | ||
in.close(); | ||
return 1; | ||
} | ||
in.close(); | ||
} else { | ||
throw std::invalid_argument("couldn't open file " + path); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma once | ||
#include "../src/masks.h" | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
#include <string> | ||
#include <filesystem> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace { | ||
|
||
// Retrieving current path on Windows does not work as on linux | ||
// therefore the following unittest is linux-specific. | ||
#ifdef __unix__ | ||
|
||
TEST(Masks, OptimizeOnes) { | ||
std::string path = std::filesystem::current_path(); | ||
path += "/tests/testdata/masktest.fa"; | ||
|
||
struct TestCase { | ||
std::vector<kmer_t> kMers; | ||
int k; | ||
bool complements; | ||
bool minimize; | ||
std::string wantResult; | ||
}; | ||
std::vector<TestCase> tests = { | ||
{ | ||
{KMerToNumber({"ACG"}), KMerToNumber({"CGT"}), KMerToNumber({"TAA"})}, | ||
3, false, true, | ||
"> superstring\nACgTaacgt\n" | ||
}, | ||
{ | ||
{KMerToNumber({"ACG"}), KMerToNumber({"CGT"}), KMerToNumber({"TAA"})}, | ||
3, false, false, | ||
"> superstring\nACgTaACgt\n" | ||
}, | ||
{ | ||
{KMerToNumber({"CGT"}), KMerToNumber({"TAA"})}, | ||
3, true, true, | ||
"> superstring\nAcgTaacgt\n" | ||
}, | ||
{ | ||
{KMerToNumber({"CGT"}), KMerToNumber({"TAA"})}, | ||
3, true, false, | ||
"> superstring\nACgTaACgt\n" | ||
}, | ||
}; | ||
|
||
for (auto &t : tests) { | ||
std::stringstream of; | ||
std::ifstream in(path); | ||
if (!in.is_open()) { | ||
throw std::invalid_argument("couldn't open file " + path); | ||
} | ||
auto kMersDict = kh_init_S64(); | ||
int ret; | ||
for (auto &kMer : t.kMers) kh_put_S64(kMersDict, kMer, &ret); | ||
|
||
OptimizeOnes(in, of, kMersDict, t.k, t.complements, t.minimize); | ||
in.close(); | ||
|
||
EXPECT_EQ(t.wantResult, of.str()); | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
> superstring | ||
ACGTAACGT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ AC | |
>2 | ||
CGTANATGC | ||
>3 with description | ||
ACC | ||
AcC | ||
CGT | ||
TTA | ||
ACG | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters