46 lines
839 B
C++
46 lines
839 B
C++
#include "nucleotide_count.h"
|
|
|
|
#include <stdexcept>
|
|
#include <unordered_set>
|
|
|
|
|
|
namespace dna {
|
|
|
|
using namespace std;
|
|
|
|
|
|
static const unordered_set<char> valid_nucleotides = {'A', 'T', 'C', 'G'};
|
|
|
|
static inline bool is_valid_nucleotide(char c)
|
|
{
|
|
return valid_nucleotides.find(c) != valid_nucleotides.cend();
|
|
}
|
|
|
|
counter::counter(const string &str)
|
|
{
|
|
for (auto c : valid_nucleotides)
|
|
nucleotide_counts_[c] = 0;
|
|
|
|
for (auto c : str) {
|
|
|
|
if (!is_valid_nucleotide(c))
|
|
throw invalid_argument("invalid sequence");
|
|
|
|
++nucleotide_counts_[c];
|
|
}
|
|
}
|
|
|
|
NucleotideCounts counter::nucleotide_counts() const
|
|
{
|
|
return nucleotide_counts_;
|
|
}
|
|
|
|
int counter::count(char c) const
|
|
{
|
|
if (!is_valid_nucleotide(c))
|
|
throw invalid_argument("invalid nucleotide");
|
|
|
|
return nucleotide_counts_.at(c);
|
|
}
|
|
|
|
}
|