-
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 #72 from OndrejSladky/refactor-ac
Refactoring AC code to a separate directory to simplify code structure.
- Loading branch information
Showing
19 changed files
with
144 additions
and
148 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,44 @@ | ||
#pragma once | ||
#include <string> | ||
#include <vector> | ||
|
||
struct KMer { | ||
std::string value; | ||
size_t length() const{ return value.size(); } | ||
}; | ||
|
||
/// Return the complementary nucleotide for the given one. | ||
char ComplementaryNucleotide(const char nucleotide) { | ||
if (nucleotide == 'A') return 'T'; | ||
else if (nucleotide == 'T') return 'A'; | ||
else if (nucleotide == 'G') return 'C'; | ||
else if (nucleotide == 'C') return 'G'; | ||
throw std::invalid_argument("cannot find complementary nucleotide for letter " + std::string(1, nucleotide)); | ||
} | ||
|
||
/// Compute the reverse complement of the given k-mer. | ||
KMer ReverseComplement(const KMer &kMer) { | ||
KMer ans; | ||
ans.value.reserve(kMer.length()); | ||
for (int i = (int)kMer.length() - 1; i >= 0; --i) { | ||
ans.value += ComplementaryNucleotide(kMer.value[i]); | ||
} | ||
return ans; | ||
} | ||
|
||
/// Convert the given basic nucleotide to int so it can be used for indexing in AC. | ||
/// If non-existing nucleotide is given, return -1. | ||
int NucleotideToInt (char c) { | ||
switch (c) { | ||
case 'A': return 0; | ||
case 'C': return 1; | ||
case 'G': return 2; | ||
case 'T': return 3; | ||
case 'a': return 0; | ||
case 'c': return 1; | ||
case 'g': return 2; | ||
case 't': return 3; | ||
default: return -1; | ||
} | ||
} | ||
|
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,79 @@ | ||
#pragma once | ||
|
||
// Implicitly initialize kseq. | ||
#include "../parser.h" | ||
|
||
#include "kmers_ac.h" | ||
|
||
/// Record of one fasta sequence. | ||
struct FastaRecord { | ||
// The name of the record. Starts with '>'. | ||
std::string name; | ||
// The genetic sequence. | ||
std::string sequence; | ||
}; | ||
|
||
/// Read fasta file with given path. | ||
std::vector<FastaRecord> ReadFasta(std::string &path) { | ||
gzFile fp; | ||
kseq_t *seq; | ||
std::vector<FastaRecord> records; | ||
|
||
fp = gzopen(path.c_str(), "r"); | ||
if (fp == nullptr) { | ||
throw std::invalid_argument("couldn't open file " + path); | ||
} | ||
seq = kseq_init(fp); | ||
while (kseq_read(seq) >= 0) { | ||
records.push_back(FastaRecord{ | ||
seq->name.s, | ||
seq->seq.s, | ||
}); | ||
} | ||
kseq_destroy(seq); | ||
gzclose(fp); | ||
return records; | ||
} | ||
|
||
/// Create a list of unique k-mers in no particular order. | ||
/// This runs in O(k*data.size) expected time. | ||
void AddKMersFromSequence(std::unordered_set<std::string> &kMers, std::string &data, int k) { | ||
// Convert the sequence to uppercase letters. | ||
std::transform(data.begin(), data.end(), data.begin(), toupper); | ||
size_t possibleKMerEnd = k; | ||
for (size_t i = 1; i <= data.size(); ++i) { | ||
if (data[i-1] != 'A' && data[i-1] != 'C' && data[i-1] != 'G' && data[i-1] != 'T') { | ||
// Skip this and the next k-1 k-mers. | ||
possibleKMerEnd = i + k; | ||
} | ||
if (i >= possibleKMerEnd) { | ||
kMers.insert(data.substr(i - k, k)); | ||
} | ||
} | ||
} | ||
|
||
/// Return only the subset of k-mers where no two k-mers are complements of one another. | ||
/// The k-mers are chosen with no particular property. | ||
std::unordered_set<std::string> FilterKMersWithComplement(std::unordered_set<std::string> &kMers) { | ||
std::unordered_set<std::string> ret; | ||
for (auto &&x : kMers) { | ||
auto kMer = KMer{x}; | ||
if (ret.count(ReverseComplement(kMer).value) == 0) ret.insert(x); | ||
} | ||
return ret; | ||
} | ||
|
||
/// Create a list of unique k-mers in no particular order. | ||
/// This runs in O(k*data.size) expected time. | ||
std::vector<KMer> ConstructKMers(std::vector<FastaRecord> &data, int k, bool complements) { | ||
std::unordered_set<std::string> uniqueKMers; | ||
for (auto &&record : data) { | ||
AddKMersFromSequence(uniqueKMers, record.sequence, k); | ||
} | ||
if (complements) uniqueKMers = FilterKMersWithComplement(uniqueKMers); | ||
std::vector<KMer> result; | ||
for (auto &kMer : uniqueKMers) { | ||
result.push_back(KMer{kMer}); | ||
} | ||
return result; | ||
} |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#pragma once | ||
#include "../src/ac_automaton.h" | ||
#include "../src/ac/ac_automaton.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#pragma once | ||
#include "../src/global_ac.h" | ||
#include "../src/ac/global_ac.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
#include "../src/kmers.h" | ||
#include "../src/ac/kmers_ac.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
|
Oops, something went wrong.