39 inline void check_and_throw(
bool condition,
const std::string &error_msg) {
42 throw(std::runtime_error(error_msg));
48 inline uint8_t nuc_hash(
char nuc) {
59 inline char inv_nuc_hash(uint8_t hash) {
70 inline char complement(
char nuc) {
76 default :
return NULL_CHAR;
81 std::string translate(
const std::string &nuc_seq) {
82 return CodonTable::table().translate(nuc_seq);
86 prob_t loglikelihood(
const std::vector<prob_t> &vec, prob_t laplace = 1e-80) {
88 for (
size_t i = 0; i < vec.size(); ++i) {
91 if (vec[i] && !std::isnan(vec[i])) {
106 void prob_summary(
const std::vector<prob_t> &prob_vec, prob_t prev_ll = 0) {
107 size_t zeros = 0, negative = 0, bignums = 0, nans = 0;
109 for (
size_t i = 0; i < prob_vec.size(); ++i) {
110 zeros += prob_vec[i] ? 0 : 1;
111 negative += prob_vec[i] < 0 ? 1 : 0;
112 bignums += prob_vec[i] > 1 ? 1 : 0;
113 nans += std::isnan(prob_vec[i]) ? 1 : 0;
116 std::cout <<
"Loglikelihood:\t" << std::setprecision(9) << loglikelihood(prob_vec);
118 if (std::abs(loglikelihood(prob_vec) - prev_ll) < 1e-5) { std::cout <<
" (exact)"; }
119 else if (loglikelihood(prob_vec) > prev_ll) { std::cout <<
" (grows)"; }
120 else { std::cout <<
" (drops)"; }
122 std::cout << std::endl;
123 std::cout <<
"Error probabilities:\t" << (size_t) (zeros + negative + bignums + nans) << std::endl;
124 if (zeros) std::cout <<
" Zeros: \t" << (size_t) zeros << std::endl;
125 if (nans) std::cout <<
" NaNs: \t" << (size_t) nans << std::endl;
126 if (negative) std::cout <<
" Negatives: \t" << (size_t) negative << std::endl;
127 if (bignums) std::cout <<
" Bigger than one: \t" << (size_t) bignums << std::endl;
131 inline bool is_out_of_frame(
const std::string &sequence) {
132 return sequence.size() % 3 != 0;
136 inline bool has_end_codon(
const std::string &sequence) {
137 for (
size_t i = 0; i < sequence.size(); i += 3) {
138 if (sequence[i] ==
'T' 139 && ((sequence[i+1] ==
'A' && sequence[i+2] ==
'G')
140 || (sequence[i+1] ==
'A' && sequence[i+2] ==
'A')
141 || (sequence[i+1] ==
'G' && sequence[i+2] ==
'A')))
149 inline bool has_bad_aa_codons(
const sequence_t &sequence) {
150 for (
size_t i = 0; i < sequence.size(); ++i) {
151 if (sequence[i] ==
'~' || sequence[i] ==
'*') {
159 inline bool has_oof_aa_codon(
const sequence_t &sequence) {
160 for (
size_t i = 0; i < sequence.size(); ++i) {
161 if (sequence[i] ==
'~') {