-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphred.cpp
126 lines (115 loc) · 4.61 KB
/
phred.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <lior.galanti@nyu.edu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "phred.h"
void PhredScale::assemble_false_positive_probability() {
for(uint8_t q(1); q < 0x80; ++q) {
false_positive_probability[q] = pow(PHRED_PROBABILITY_BASE, q);
}
};
void PhredScale::assemble_true_positive_probability() {
for(uint8_t q(1); q < 0x80; ++q) {
true_positive_probability[q] = 1.0 - false_positive_probability[q];
}
};
void PhredScale::assemble_true_positive_quality() {
for(uint8_t q(1); q < 0x80; ++q) {
true_positive_quality[q] = -10.0 * log10(true_positive_probability[q]);
}
};
void PhredScale::assemble_substitution_lookup() {
for(uint8_t q(1); q < 0x80; ++q) {
for(uint8_t e(0); e < 0x10; ++e) {
for(uint8_t o(0); o < 0x10; ++o) {
uint8_t key(e<<0x4|o);
switch(key) {
case 0x11:
case 0x22:
case 0x44:
case 0x88:
substitution_lookup[q<<0x8|key] = true_positive_quality[q];
break;
case 0x12:
case 0x14:
case 0x18:
case 0x21:
case 0x24:
case 0x28:
case 0x41:
case 0x42:
case 0x48:
case 0x81:
case 0x82:
case 0x84:
substitution_lookup[q<<0x8|key] = q;
break;
default:
substitution_lookup[q<<0x8|key] = UNIFORM_BASE_QUALITY;
break;
}
}
}
}
};
PhredScale::PhredScale() {
assemble_false_positive_probability();
assemble_true_positive_probability();
assemble_true_positive_quality();
assemble_substitution_lookup();
};
ostream& operator<<(ostream& o, const PhredScale& scale) {
o << setprecision(DISPLAY_FLOAT_PRECISION);
if(false) {
o << "False Positive Probability" << endl;
for(uint8_t q(1); q < 0x80; ++q) {
o << to_string(uint32_t(q)) << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << scale.false_positive_probability[q] << endl;
}
o << endl;
}
if(false) {
o << "True Positive Probability" << endl;
for(uint8_t q(1); q < 0x80; ++q) {
o << to_string(uint32_t(q)) << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << scale.true_positive_probability[q] << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << scale.true_positive_quality[q] << endl;
}
o << endl;
}
if(true) {
o << "Substitution Probability By Expected and Observed" << endl;
o << "E" << '\t';
o << "O" << '\t';
o << "Q" << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << "Probability" << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << "Phred" << endl;
const double phred_probability_base(pow(10.0, -0.1));
for(uint8_t quality(1); quality < 0x80; ++quality) {
for(uint8_t expected(0); expected < 0x10; ++expected) {
for(uint8_t observed(0); observed < 0x10; ++observed) {
double score(scale.substitution_quality(expected, observed, quality));
double probability(pow(phred_probability_base, score));
o << BamToAmbiguousAscii[expected] << '\t';
o << BamToAmbiguousAscii[observed] << '\t';
o << to_string(quality) << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << probability << '\t';
o << setw(DISPLAY_FLOAT_PRECISION) << left << score << endl;
}
}
}
}
return o;
};