-
Notifications
You must be signed in to change notification settings - Fork 4
/
fastq.cpp
327 lines (246 loc) · 8.02 KB
/
fastq.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "fastq.h"
#include <math.h>
fastq::fastq(string inPath){
file = inPath;
return;
}
void fastq::load2show(options* opts){
string line;
uint relPos = 0;
uint i = 0;
int number = 0;
string name = "";
string dnaseq = "";
string qualseq = "";
// clear what we have
content.clear();
// open file
gzFile infile = gzopen(opts->input, "rb");
//ifstream infile;
//infile.open(opts->input);
if(opts->lastInPad == 0){
return;
}
// jump to start of what we want to load
gzseek(infile, index[opts->firstInPad].tellg, SEEK_SET);
i = opts->firstInPad;
while( i < opts->lastInPad and getline(infile, line) ){
// each sequence of fastq
// fills at least two lines, name and spacer
// then the chars are counted and divisted by cols
if(relPos == 0){
// this is the name, line
// make new entry into content
name = line.erase(0, 1);;
number = i;
}else if(relPos == 1){
// this should be DNA
dnaseq = line;
}else if(relPos == 3){
// this should quality data
qualseq = line;
// now create the fastq seq and put it in the right place
fastqSeq b;
b.name = name;
b.number = number;
b.inpad = true;
setDNAline(b, dnaseq);
addQualityData(b,qualseq, opts);
// now save it
content.push_back(b);
}
relPos++;
if( relPos == 4 ){
// reset the relative Position counter
i++;
relPos = 0;
}
}
}
void fastq::showthese(options* opts, int dir, WINDOW* Wtext){
//dir 1 means down, 0 means up
// sets the in pad variable to indictae which entries to show
// also determines the offset the editor must have to make scrolling look
// nice
// first iterate through and make a vector of lines/per entry
// remember also which sequence was the last the user had in the pad
// this is directionally dependent, so either the first row of the last
// pad or the last row.
vector<int> linevec; // hold the number of lines, each entry has
int lines;
int firstSeq = opts->firstInPad;
int lastSeq = opts->lastInPad;
int i = 0;
// build a vector holding line counts
for(auto& it: index) {
indexStruc& ind = it;
lines = 1 + ceil((float)ind.lengthName/(float)opts->textcols) +
ceil((float)ind.lengthSeq/(float)opts->textcols) ;
linevec.push_back(lines);
}
// ok, indexing is done, now lets find out what to show
int linesBelowAndUp = round(opts->avaiLines/2);
if(linesBelowAndUp < opts->textrows){
// this fixes a bug, whne the window is larger then half
// the buffer which then allows escaping of the window
linesBelowAndUp = opts->textrows + 1;
}
// scroll N lines up in both cases
int j = 0;
if(dir == 1){
i = lastSeq;
}else{
i = firstSeq;
}
int delta = (int)opts->offset - (int) (opts->avaiLines - opts->textrows );
int oldoff = opts->offset;
while(j < linesBelowAndUp && i > 0){
j = j + linevec[i];
i--;
}
// set offset to match the new pad value
opts->offset = j;
if(dir == 1){
opts->offset -= opts->textrows - delta;
}else{
opts->offset += oldoff;
}
if(opts->offset < 0) opts->offset = 0;
// now we move down find the last entry in the pad
if(i < 0){ i = 0;}
opts->firstInPad = i;
j = 0;
while(j < (int)opts->avaiLines && i < (int)index.size()){
j = j + linevec[i];
i++;
opts->lastInPad = i;
}
opts->linesTohave = j;
// now we know what to show, we can actually go ahead and load them.
load2show(opts);
}
int fastq::getline(gzFile& infile, string& line){
line = "";
int a;
char aa;
while(true){
a = gzgetc(infile);
if(a == -1){
return(0);
}
aa = (char)a;
if(aa == '\n' or aa == '\r'){
break;
}
line.push_back(aa);
}
return 1;
}
void fastq::buildIndex(options* opts){
//dir 1 down 0 up
string line;
uint bfull = 0;
uint i = index.size();
uint relPos = 0;
// check if this is the first time we do this
uint firstIndex = false;
if(index.size() == 0){
firstIndex = true;
}
gzFile infile = gzopen(opts->input, "rb");
if(infile != NULL ){
if( firstIndex == false ){
// jump down
// last index pos:
gzseek(infile, opts->IndexTellg, SEEK_SET);
}
uint number = 0;
double ctellg = 0;
uint lengthName = 0;
uint lengthSeq = 0;
while( getline(infile, line) ){
if(firstIndex and bfull >= 2*opts->avaiLines ){
// break if we only want to index a first quick glance of the file
// for fast response times
break;
}
// each sequence of fastq
// fills at least two lines, name and spacer
// then the chars are counted and divisted by cols
if(relPos == 0){
// this is the name, line
// make new entry into content
lengthName = line.erase(0, 1).size();
number = i;
ctellg = gztell(infile); // remember the position of the name start
ctellg = ctellg - line.size() - 2; // tahts why we go back the name line length
i++;
}else if(relPos == 1){
// this should be DNA
lengthSeq = line.size();
}else if(relPos == 3){
// now create the fastq seq and put it in the right place
indexStruc ind;
ind.number = number;
ind.lengthSeq = lengthSeq;
ind.lengthName = lengthName;
ind.tellg = ctellg;
ind.inpad = false;
// update the bfull variable
bfull = bfull + 2 + ceil((float)lengthSeq/(float)opts->textcols) ;
// name rows of DNA space
index.push_back(ind);
}
relPos++;
if( relPos == 4 ){
// reset the relative Position counter
relPos = 0;
}
// save seek pointer for later readings
opts->IndexTellg = gztell(infile);
}
}
// close up
gzclose(infile);
return;
}
void fastq::setDNAline(fastqSeq& b, string& sDNA){
b.dna.append(sDNA);
}
void fastq::addQualityData(fastqSeq& b, string& qual, options* opts){
b.dna.addQuality(qual);
//estimate quality range
int q;
for(auto it = qual.begin(); it != qual.end(); ++it) {
q = static_cast<int>(*it);
if(q < minQal){
minQal = q;
}
if(q > maxQal){
maxQal = q;
}
}
// set possible quality
possibleQual.clear();
for(auto& qv : opts->qm) {
if(minQal >= qv.second.first && maxQal <= qv.second.second){
possibleQual.push_back(qv.first);
}
}
// in case there is no kown mode
// the integers still code for quality
// so make a new unknown coding
if(possibleQual.size() == 0){
opts->qm["unknown"] = std::make_pair(minQal, maxQal);
possibleQual.push_back("unknown");
}
}
qualitymap buildQualityMap(){
qualitymap map;
// Name //min max
map["Sanger"] = std::make_pair(33, 73);
map["Solexa"] = std::make_pair(59, 104);
map["Illumina 1.3+"] = std::make_pair(64, 104);
map["Illumina 1.8+"] = std::make_pair(33, 74);
return map;
}