-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalc_phred_average.cpp
58 lines (46 loc) · 1.56 KB
/
calc_phred_average.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "calc_phred_average.hpp"
#include <cmath>
#include <cstdlib> // for abs
#include <string>
#include <utility>
std::pair<uint32_t, uint32_t>
calc_phred_average(const std::string qual)
{
double phred_sum = 0.0;
double first_avg = 0.0;
double second_avg = 0.0;
size_t qual_size = qual.size();
for (size_t i = 0; i < qual_size; ++i) {
// Convert ASCII character to Phred score
int phred_score = (int)(qual.at(i) - 33);
// Delog the Phred score: 10^(-Q/10)
double delog_phred = pow(10.0, -phred_score / 10.0);
phred_sum += delog_phred;
// Store sum for the first half
if (i == qual_size / 2 - 1) {
first_avg = phred_sum;
}
}
// Compute the second half average
second_avg = phred_sum - first_avg;
// Calculate averages by dividing by half of the length
second_avg = second_avg / (qual_size * 0.5);
first_avg = first_avg / (qual_size * 0.5);
// Return pair: total average Phred score and absolute difference between
// first and second halves
return std::make_pair((uint32_t)(-10 * log10(phred_sum / qual_size)),
(uint32_t)abs((int32_t)(-10 * log10(first_avg)) -
(int32_t)(-10 * log10(second_avg))));
}
double
sum_phred(const std::string& qual)
{
double phred_sum = 0;
for (const auto& phred : qual) {
int phred_score = (int)(phred - 33);
// Delog the Phred score: 10^(-Q/10)
double delog_phred = pow(10.0, -phred_score / 10.0);
phred_sum += delog_phred;
}
return phred_sum;
}