-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewpack.C
More file actions
262 lines (245 loc) · 6.3 KB
/
Copy pathnewpack.C
File metadata and controls
262 lines (245 loc) · 6.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// newpack.C
// New packing functions
// Xu Junjie, Kevin
// University of Oregon
// Feb 2014
#include <bitset>
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include "newpack.h"
namespace pip {
namespace pack {
Pack::Pack(const string& sequence, const string& quality)
{
/*qualityFormat_ = find_quality_format(quality);
sequenceLength_ = sequence.length();
pack(sequence,quality);*/
Pack(sequence,quality,1);
}
Pack::Pack(const string& sequence, const string& quality, int)
{
qualityFormat_ = find_quality_format(quality);
sequenceLength_ = sequence.length();
losslessPack(sequence,quality);
}
Pack::~Pack() {}
unsigned int Pack::sequenceLength() const
{
return sequenceLength_;
}
QualityFormat Pack::qualityFormat() const
{
return qualityFormat_;
}
const unsigned char* Pack::rawData() const
{
return v.data();
}
void Pack::pack(const string& sequence, const string& quality)
{
if (sequenceLength_ != quality.length()) {
// error
return;
}
v.reserve(sequenceLength_);
unsigned char c = 0; // temporary output character
for (unsigned int i = 0; i<sequenceLength_; ++i) {
// 0b00000110 = 6 bitmask, we want the 2 bits 3 from the right
c |= (sequence[i] & 6) << 5; // pack sequence character into the leftmost 2 bits
switch (qualityFormat_) {
case Illumina18:
c |= quality[i]-Illu18QFLower;
break;
default:
break;
}
v.push_back(c);
c = 0;
}
}
void Pack::losslessPack(const string& sequence, const string& quality)
{
if (sequenceLength_ != quality.length()) {
// error
return;
}
v.reserve(sequenceLength_);
unsigned char c = 0; // temporary output character
for (unsigned int i = 0; i<sequenceLength_; ++i) {
// give different bases different int ranges
// 'A': 0-49, 'C': 50-99, 'T': 100-149, 'G': 150-199, 'N': 200+
switch (sequence[i]) {
case 'C':
c = C;
break;
case 'T':
c = T;
break;
case 'G':
c = G;
break;
case 'N':
c = N;
}
switch (qualityFormat_) {
case Illumina18:
c += quality[i]-Illu18QFLower;
break;
default:
break;
}
v.push_back(c);
c = 0;
}
}
void losslessUnpack(const Pack& pack, string& outSequence, string& outQuality)
{
losslessUnpack(
pack.rawData(),
pack.sequenceLength(),
pack.qualityFormat(),
outSequence,
outQuality);
}
void losslessUnpack(const unsigned char* data, int sequenceLength, QualityFormat qualityFormat, string &outSequence, string &outQuality)
{
outSequence.reserve(sequenceLength);
outQuality.reserve(sequenceLength);
for (auto i = 0; i < sequenceLength; ++i) {
auto c = data[i];
// Rebuilds nucleotides based on integer range
// 'A': 0-49, 'C': 50-99, 'T': 100-149, 'G': 150-199, 'N': 200+
if (c<C) {
outSequence += "A";
}
else if (c<T) {
outSequence += "C";
c -= C;
}
else if (c<G) {
outSequence += "T";
c -= T;
}
else if (c<N) {
outSequence += "G";
c -= G;
}
else {
outSequence += "N";
c -= N;
}
switch (qualityFormat) {
case Illumina18:
outQuality += (c+Illu18QFLower);
break;
default: break;
}
}
}
void unpack(const Pack& pack, string& outSequence, string& outQuality)
{
unpack(
pack.rawData(),
pack.sequenceLength(),
pack.qualityFormat(),
outSequence,
outQuality);
}
void unpack(const unsigned char* data, int sequenceLength, QualityFormat qualityFormat, string &outSequence, string &outQuality)
{
outSequence.reserve(sequenceLength);
outQuality.reserve(sequenceLength);
for (auto i = 0; i < sequenceLength; ++i) {
auto c = data[i];
switch (c >> 6) {
case bitA:
outSequence += "A";
break;
case bitC:
outSequence += "C";
break;
case bitT:
outSequence += "T";
break;
case bitG:
outSequence += "G";
}
// 63 = 0b00111111 bitmask, we take out the first 2 bits and leave the rest
c &= 63;
switch (qualityFormat) {
case Illumina18:
outQuality += (c+Illu18QFLower);
break;
default: break;
}
}
}
QualityFormat find_quality_format(const string& quality)
{
char lowerBoundary = 'E'; // start in the "middle" of the quality
char upperBoundary = 'E'; // ranges
for (auto& character : quality) {
// character is out of valid range, exit
if (character<ValidQF::LowerLimit || character>ValidQF::UpperLimit) {
return Unrecognized;
}
else if (character<lowerBoundary) {
lowerBoundary = character; // lower the boundary
}
else if (character>upperBoundary) {
upperBoundary = character; // raise the boundary
}
}
// bitfield to determine quality format
// start out with all formats possible
std::bitset<5> format("11111");
// Check all lower boundaries
format.set(Illumina15,!(lowerBoundary<'B')); // Illumina 1.5+ impossible
format.set(Illumina13,!(lowerBoundary<'@')); // Illumina 1.3+ impossible
format.set(Solexa,!(lowerBoundary<';')); // Solexa impossible
format.set(Illumina18,!(lowerBoundary<'#')); // Illumina 1.8+ impossible
// Check all upper boundaries
format.set(Sanger,!(upperBoundary>'I')); // Sanger impossible
format.set(Illumina18,!(upperBoundary>'J')); // Illumina 1.8+ impossible
// Determine what's left that is possible
if (format[Illumina18]) return Illumina18;
if (format[Sanger]) return Sanger;
if (format[Illumina15]) return Illumina15;
if (format[Illumina13]) return Illumina13;
if (format[Solexa]) return Solexa;
return Unrecognized;
}
} /* pack */
}
using namespace pip::pack;
ostream& operator<<(ostream& os, const Pack& obj)
{
switch(obj.qualityFormat()) {
case Illumina18:
os << "Illumina 1.8+ Phred+33";
break;
case Sanger:
os << "Sanger Phred+33";
break;
case Illumina15:
os << "Illumina 1.5+ Phred+64";
break;
case Illumina13:
os << "Illumina 1.3+ Phred+64";
break;
case Solexa:
os << "Solexa Solexa+64";
break;
default:
os << "Unrecognized";
}
os << " Sequence length: " << obj.sequenceLength();
os << "\nRaw: " << obj.rawData() << "\n";
return os;
}