Skip to content

Instantly share code, notes, and snippets.

@cls cls/count-dna.cpp
Last active Mar 9, 2018

Embed
What would you like to do?
Efficient counting of DNA bases and codons
#include <array>
enum Base { A, C, T, G };
constexpr Base base(char c)
{
return static_cast<Base>((c >> 1) & 0b11);
}
static_assert(base('A') == Base::A);
static_assert(base('C') == Base::C);
static_assert(base('T') == Base::T);
static_assert(base('G') == Base::G);
void count_dna_bases(const std::string &str, std::array<int, 4> &counts)
{
for (char c : str) {
++counts[base(c)];
}
}
constexpr size_t codon(Base x, Base y, Base z)
{
return (x << 4) | (y << 2) | (z << 0);
}
void count_dna_codons(const std::string &str, std::array<int, 64> &counts)
{
for (size_t i = 2; i < str.size(); ++i) {
++counts[codon(base(str[i-2]), base(str[i-1]), base(str[i-0]))];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.