forked from BenLangmead/bowtie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qual.h
201 lines (187 loc) · 5.22 KB
/
qual.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef QUAL_H_
#define QUAL_H_
#include <stdexcept>
extern unsigned char qualRounds[];
extern unsigned char solToPhred[];
/// Translate a Phred-encoded ASCII character into a Phred quality
static inline uint8_t phredCharToPhredQual(char c) {
return ((uint8_t)c >= 33 ? ((uint8_t)c - 33) : 0);
}
/**
* Convert a Solexa-scaled quality value into a Phred-scale quality
* value.
*
* p = probability that base is miscalled
* Qphred = -10 * log10 (p)
* Qsolexa = -10 * log10 (p / (1 - p))
* See: http://en.wikipedia.org/wiki/FASTQ_format
*
*/
static inline uint8_t solexaToPhred(int sol) {
assert_lt(sol, 256);
if(sol < -10) return 0;
return solToPhred[sol+10];
}
class SimplePhredPenalty {
public:
static uint8_t mmPenalty (uint8_t qual) {
return qual;
}
static uint8_t delPenalty(uint8_t qual) {
return qual;
}
static uint8_t insPenalty(uint8_t qual_left, uint8_t qual_right) {
return std::max(qual_left, qual_right);
}
};
class MaqPhredPenalty {
public:
static uint8_t mmPenalty (uint8_t qual) {
return qualRounds[qual];
}
static uint8_t delPenalty(uint8_t qual) {
return qualRounds[qual];
}
static uint8_t insPenalty(uint8_t qual_left, uint8_t qual_right) {
return qualRounds[std::max(qual_left, qual_right)];
}
};
static inline uint8_t mmPenalty(bool maq, uint8_t qual) {
if(maq) {
return MaqPhredPenalty::mmPenalty(qual);
} else {
return SimplePhredPenalty::mmPenalty(qual);
}
}
static inline uint8_t delPenalty(bool maq, uint8_t qual) {
if(maq) {
return MaqPhredPenalty::delPenalty(qual);
} else {
return SimplePhredPenalty::delPenalty(qual);
}
}
static inline uint8_t insPenalty(bool maq, uint8_t qual_left, uint8_t qual_right) {
if(maq) {
return MaqPhredPenalty::insPenalty(qual_left, qual_right);
} else {
return SimplePhredPenalty::insPenalty(qual_left, qual_right);
}
}
/**
* Take an ASCII-encoded quality value and convert it to a Phred33
* ASCII char.
*/
inline static char charToPhred33(char c, bool solQuals, bool phred64Quals) {
if(c == ' ') {
cerr << "Saw a space but expected an ASCII-encoded quality value." << endl
<< "Are quality values formatted as integers? If so, try --integer-quals." << endl;
throw 1;
}
if (solQuals) {
// Convert solexa-scaled chars to phred
// http://maq.sourceforge.net/fastq.shtml
char cc = solexaToPhred((int)c - 64) + 33;
if (cc < 33) {
cerr << "Saw ASCII character "
<< ((int)c)
<< " but expected 64-based Solexa qual (converts to " << (int)cc << ")." << endl
<< "Try not specifying --solexa-quals." << endl;
throw 1;
}
c = cc;
}
else if(phred64Quals) {
if (c < 64) {
cerr << "Saw ASCII character "
<< ((int)c)
<< " but expected 64-based Phred qual." << endl
<< "Try not specifying --solexa1.3-quals/--phred64-quals." << endl;
throw 1;
}
// Convert to 33-based phred
c -= (64-33);
}
else {
// Keep the phred quality
if (c < 33) {
cerr << "Saw ASCII character "
<< ((int)c)
<< " but expected 33-based Phred qual." << endl;
throw 1;
}
}
return c;
}
/**
* Take an integer quality value and convert it to a Phred33 ASCII
* char.
*/
inline static char intToPhred33(int iQ, bool solQuals) {
int pQ;
if (solQuals) {
// Convert from solexa quality to phred
// quality and translate to ASCII
// http://maq.sourceforge.net/qual.shtml
pQ = solexaToPhred((int)iQ) + 33;
} else {
// Keep the phred quality and translate
// to ASCII
pQ = (iQ <= 93 ? iQ : 93) + 33;
}
if (pQ < 33) {
cerr << "Saw negative Phred quality " << ((int)pQ-33) << "." << endl;
throw 1;
}
assert_geq(pQ, 0);
return (int)pQ;
}
/**
* Fill the q[] array with the penalties that are determined by
* subtracting the quality values of the alternate basecalls from
* the quality of the primary basecall.
*/
inline static uint8_t penaltiesAt(size_t off, uint8_t *q,
int alts,
const String<char>& qual,
const String<Dna5>* altQry,
const String<char>* altQual)
{
uint8_t primQ = qual[off]; // qual of primary call
uint8_t bestPenalty = primQ - 33;
q[0] = q[1] = q[2] = q[3] = bestPenalty;
for(int i = 0; i < alts; i++) {
uint8_t altQ = altQual[i][off]; // qual of alt call
if(altQ == 33) break; // no alt call
assert_leq(altQ, primQ);
if(primQ - altQ < bestPenalty) {
bestPenalty = primQ - altQ;
}
// Get the base
int altC = (int)altQry[i][off];
assert_lt(altC, 4);
q[altC] = primQ - altQ;
}
return bestPenalty;
}
/**
* Fill the q[] array with the penalties that are determined by
* subtracting the quality values of the alternate basecalls from
* the quality of the primary basecall.
*/
inline static uint8_t loPenaltyAt(size_t off, int alts,
const String<char>& qual,
const String<char>* altQual)
{
uint8_t primQ = qual[off]; // qual of primary call
uint8_t bestPenalty = primQ - 33;
for(int i = 0; i < alts; i++) {
uint8_t altQ = altQual[i][off]; // qual of alt call
if(altQ == 33) break; // no more alt calls at this position
assert_leq(altQ, primQ);
if(primQ - altQ < bestPenalty) {
bestPenalty = primQ - altQ;
}
}
return bestPenalty;
}
#endif /*QUAL_H_*/