-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputeMatrices.h
525 lines (467 loc) · 22.2 KB
/
ComputeMatrices.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*******************************************************************************
*
* ComputeMatrices.h
*
* DESCRIPTION: Implementation of the algorithms for the problems:
* 0-zeros: trimZeroOne (line 43)
* z-zeros: trimZeroOneZerosAllowed (line 117)
* p-percent: trimZeroOnePercentZerosAllowed (line 269)
* m-mean: trimIntegerMean (line 402)
*
* RUNTIMES: If the input has r reads of length l:
* 0-zeros: worst-case: O( r * l ) expected: O( r * l )
* z-zeros: worst-case: O( r * l ) expected: O( r * l )
* p-percent: worst-case: O( r * l^2 ) expected: O( r * l )
* m-mean: worst-case: O( r * l^2 ) expected: O( r * l )
*
* AUTHORS: Ivo Hedtke (ivo.hedtke@uni-osnabrueck.de)
* Matthias Mueller-Hannemann (muellerh@informatik.uni-halle.de)
*
* CREATED: 13 Dec 2013
*
* LAST CHANGE: 25 Jul 2014
*
* Version 1.1: (25 Jul 2014) Speed-Up for p-percent and m-mean: If we have a
* nice instance, the runtime is O( r * l )
*
*/
#include <fstream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
namespace ComputeMatrices {
// 0-zeros
// =======
// readFASTQ => each time skip 3 lines
// (thresholdGoodValues >= 0) => lines of the grid are not '0' and '1' so use a threshold:
// (quality < threshold)? '0' : '1'
vector<vector<int> > trimZeroOne(
const string& inputfile,
const int& numberOfSequences,
const int& lengthOfSequence,
const int& thresholdGoodValues,
const int& shiftToConvertChars)
{
int thresholdPlusShift = thresholdGoodValues + shiftToConvertChars;
// alloc and init c and cT
// cT = counter of triangles
// c(i,j)=m means, there are m lines where a 1-block starts at i & ends at j
vector<vector<int> > c (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<vector<int> > cT (lengthOfSequence, vector<int>(lengthOfSequence,0));
// read the file row by row
string zeile;
// open file
ifstream in(inputfile, ios::in);
bool stillInOneBlock = false;
int startOfOneBlock;
for (int z = 0; z < numberOfSequences; z++) {
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
getline(in,zeile);
startOfOneBlock = 0;
zeile.append("!"); // dummy 0 at the end
// ASSERT: ASCII("!")=33 and this is the smallest possible char in a quality score string
// we need a dummy "bad" quality score at the end for our algorithm
for (int i = 0; i <= lengthOfSequence; i++) {
if (zeile[i] < thresholdPlusShift) {
if (stillInOneBlock) {
stillInOneBlock = false;
cT[startOfOneBlock][i-1]++;
}
} else {
if (!stillInOneBlock) {
stillInOneBlock = true;
startOfOneBlock = i;
}
}
}
}
// compute c from cT
vector<int> columnSumAbove (lengthOfSequence,0);
// first fill the last column of c
c[0][lengthOfSequence-1] = cT[0][lengthOfSequence-1];
for (int i=1; i < lengthOfSequence; i++){
c[i][lengthOfSequence-1] = cT[i][lengthOfSequence-1] + c[i-1][lengthOfSequence-1];
}
// next fill the first row of c
for (int j=lengthOfSequence-2; j>= 0; j--){
c[0][j] = cT[0][j] + c[0][j+1];
columnSumAbove[j] = cT[0][j];
}
// now fill the rest
for (int i=1; i < lengthOfSequence; i++){
for (int j=lengthOfSequence-2; j>= i; j--){
c[i][j] = cT[i][j] + c[i][j+1] + columnSumAbove[j];
columnSumAbove[j] += cT[i][j];
}
}
return c;
}
////////////////////////////////////////////////////////////////////////////////
// z-zeros
vector<vector<int> > trimZeroOneZerosAllowed(
const string& inputfile,
const int& numberOfSequences,
const int& lengthOfSequence,
const int& numberOfAllowedZerosPerSequence,
const int& thresholdGoodValues,
const int& shiftToConvertChars)
{
int thresholdPlusShift = thresholdGoodValues + shiftToConvertChars;
// alloc and init c and cC
// cC = counter of columns
// c(i,j)=m means, there are m lines where a block of "only ones and at most
// k zeros" starts at i and ends at j
vector<vector<int> > c (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<vector<int> > cC (lengthOfSequence, vector<int>(lengthOfSequence,0));
// read the file row by row
string zeile;
// store the positions of the left ends of each 1-block
vector<int> leftOne(lengthOfSequence,0);
// store the positions of the right ends of each 1-block
vector<int> rightOne(lengthOfSequence,0);
// store the positions of all zeros in the current column
vector<int> positionsOfZeros(lengthOfSequence,0);
// if some zeros are allowed, there is a leftmost and a rightmost zero in
// each identified block that consists of "only ones and at most k zeros"
int leftBorderZero, rightBorderZero;
// each block of such a type has a left and a right border
int leftBorderOneBlock, rightBorderOneBlock;
// open file
ifstream in(inputfile, ios::in);
bool stillInOneBlock;
int startOfOneBlock;
int numberOfZerosInCurrentRow;
// loop over all lines of the file
for (int z = 0; z < numberOfSequences; z++) {
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
getline(in,zeile);
startOfOneBlock = 0;
numberOfZerosInCurrentRow = 0;
stillInOneBlock = (zeile[0]>=thresholdPlusShift);
for (int i = 0; i < lengthOfSequence; i++) {
// initialize leftOne and rightOne
leftOne[i] = 0;
rightOne[i] = 0;
// store positions of Ones
if (zeile[i]<thresholdPlusShift) {
positionsOfZeros[numberOfZerosInCurrentRow]=i;
numberOfZerosInCurrentRow++;
}
// fill leftOne
if (zeile[i]<thresholdPlusShift) {
if (stillInOneBlock) {
stillInOneBlock = false;
}
} else {
if (!stillInOneBlock) {
stillInOneBlock = true;
startOfOneBlock = i;
}
leftOne[i]=startOfOneBlock;
}
}
// fill rightOne
stillInOneBlock = (zeile[lengthOfSequence-1]>=thresholdPlusShift);
if (stillInOneBlock) {
startOfOneBlock = lengthOfSequence-1;
}
for (int i = lengthOfSequence-1; i >=0; i--) {
if (zeile[i]<thresholdPlusShift) {
if (stillInOneBlock) {
stillInOneBlock = false;
}
} else {
if (!stillInOneBlock) {
stillInOneBlock = true;
startOfOneBlock = i;
}
rightOne[i]=startOfOneBlock;
}
}
if (numberOfZerosInCurrentRow <= numberOfAllowedZerosPerSequence) {
for (int j=0; j < lengthOfSequence; j++)
cC[0][j]++;
} else {
int previousBlock = -1;
for (int i = 0; i <= numberOfZerosInCurrentRow-numberOfAllowedZerosPerSequence; i++) {
leftBorderZero = positionsOfZeros[i];
rightBorderZero = positionsOfZeros[i+numberOfAllowedZerosPerSequence-1];
// leftBorderOneBlock is either
// 1) = 0, if leftBorderZero == 0
// 2) = leftBorderZero, if leftBorderZero-1 is *not* part of a
// 1-block in zeile
// 3) = leftOne[leftBorderZero-1], if leftBorderZero-1 is part
// of a 1-block in zeile
if ( leftBorderZero == 0 ) {
leftBorderOneBlock = 0;
} else { // leftBorderZero > 0
if (zeile[leftBorderZero-1]>=thresholdPlusShift) { // 1-block left of 0
leftBorderOneBlock = leftOne[leftBorderZero-1];
} else { // no 1-block
leftBorderOneBlock = leftBorderZero;
}
}
// same for rightBorderOneBlock
if (rightBorderZero == lengthOfSequence-1) {
rightBorderOneBlock = lengthOfSequence-1;
} else {
if (zeile[rightBorderZero+1]>=thresholdPlusShift) { // 1-block right of 0
rightBorderOneBlock = rightOne[rightBorderZero+1];
} else { // no 1-block
rightBorderOneBlock = rightBorderZero;
}
}
// add to cC
for (int j= previousBlock+1; j <=rightBorderOneBlock; j++) {
cC[leftBorderOneBlock][j]++;
}
previousBlock = rightBorderOneBlock;
}
}
}
// compute c from cC
// first fill the first row of c
for (int j=0; j< lengthOfSequence; j++){
c[0][j] = cC[0][j];
}
// now fill the rest
for (int i=1; i < lengthOfSequence; i++){
for (int j=i; j<lengthOfSequence; j++){
c[i][j] = cC[i][j] + c[i-1][j];
}
}
return c;
}
////////////////////////////////////////////////////////////////////////////////
//p-percent
vector<vector<int> > trimZeroOnePercentZerosAllowed(const string& inputfile,
const int& numberOfSequences,
const int& lengthOfSequence,
const double& percentOfAllowedZerosPerSequence,
const int& thresholdGoodValues,
const int& shiftToConvertChars)
{
int thresholdPlusShift = thresholdGoodValues + shiftToConvertChars;
// c(i,j)=m means, there are m lines where a block of "only ones and at most
// p percent zeros" starts at i and ends at j
vector<vector<int> > c (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<vector<int>> cT (lengthOfSequence, vector<int>(lengthOfSequence,0));
// read the file row by row
string zeile;
// pre compute allowed zeros per width for given percent
vector<int> preCompAllowedZeros (lengthOfSequence+1);
for (int i = 0; i <= lengthOfSequence; i++) {
preCompAllowedZeros[i] = (int) (percentOfAllowedZerosPerSequence * i);
}
// open file
ifstream in(inputfile, ios::in);
for (int z = 0; z < numberOfSequences; z++) {
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
getline(in,zeile);
// pre processing to access the #zeros in O(1)
// #zeros in g[L..R] equals partialSums[R+1] - partialSums[L]
vector<int> partialSums(lengthOfSequence+1, 0);
partialSums[0] = 0;
for (int i = 0; i < lengthOfSequence; i++) {
partialSums[i+1] = (zeile[i] < thresholdPlusShift) + partialSums[i];
}
// find block with values >= thresholdPlusShift), because all
// subblocks fulfill the p-percent condition
vector<pair<int,int>> oneBlocks;
int startOfOneBlock = 0;
bool stillInOneBlock = false;
zeile.append("!"); // dummy 0 at the end
// ASSERT: ASCII("!")=33 and this is the smallest possible char in a quality score string
// we need a dummy "bad" quality score at the end for our algorithm
for (int i = 0; i <= lengthOfSequence; i++) {
if (zeile[i] < thresholdPlusShift) {
if (stillInOneBlock) {
stillInOneBlock = false;
oneBlocks.push_back( make_pair(startOfOneBlock,i-1) );
cT[startOfOneBlock][i-1]++;
}
} else {
if (!stillInOneBlock) {
stillInOneBlock = true;
startOfOneBlock = i;
}
}
}
// remember that we added "!" to zeile, but we will not read it later,
// so there is no need to delete it
// compute c(l,r) for all (l,r) not in the triangles of oneBlocks
// HORIZONTAL
int startrow = 0;
for (auto p: oneBlocks) {
for (int row = startrow; row < p.first; row++) {
for (int col = row+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) <= preCompAllowedZeros[col+1-row]) {
c[row][col]++;
}
}
}
startrow = p.second + 1;
// VERTICAL: everything right of the triangle induced by p
for (int row = p.first; row <= p.second; row++) {
for (int col = p.second+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) <= preCompAllowedZeros[col+1-row]) {
c[row][col]++;
}
}
}
}
// everything after last triangle of 1s
for (int row = startrow; row < lengthOfSequence; row++) {
for (int col = row+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) <= preCompAllowedZeros[col+1-row]) {
c[row][col]++;
}
}
}
}
// compute c_aux from cT like in 0-zeros:
vector<vector<int>> c_aux (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<int> columnSumAbove (lengthOfSequence,0);
// first fill the last column of c_aux
c_aux[0][lengthOfSequence-1] = cT[0][lengthOfSequence-1];
for (int i=1; i < lengthOfSequence; i++){
c_aux[i][lengthOfSequence-1] = cT[i][lengthOfSequence-1] + c_aux[i-1][lengthOfSequence-1];
}
// next fill the first row of c_aux
for (int j=lengthOfSequence-2; j>= 0; j--){
c_aux[0][j] = cT[0][j] + c_aux[0][j+1];
columnSumAbove[j] = cT[0][j];
}
// now fill the rest
for (int i=1; i < lengthOfSequence; i++){
for (int j=lengthOfSequence-2; j>= i; j--){
c_aux[i][j] = cT[i][j] + c_aux[i][j+1] + columnSumAbove[j];
columnSumAbove[j] += cT[i][j];
}
}
// add c and c_aux
for (int i = 0; i < lengthOfSequence; i++) {
for (int j = i; j < lengthOfSequence; j++) {
c[i][j] += c_aux[i][j];
}
}
return c;
}
////////////////////////////////////////////////////////////////////////////////
// m-mean
vector<vector<int>> trimIntegerMean(const string& inputfile,
const int& numberOfSequences,
const int& lengthOfSequence,
const double& givenMean,
const int& shiftToConvertChars)
{
// c(i,j)=x means, there are x lines where a block of starting at index i
// and ending at index j with mean value at least "givenMean"
vector<vector<int>> c (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<vector<int>> cT (lengthOfSequence, vector<int>(lengthOfSequence,0));
double shiftedMean = shiftToConvertChars + givenMean;
// read the file row by row
string zeile;
// open file
ifstream in(inputfile, ios::in);
for (int z = 0; z < numberOfSequences; z++) {
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
in.ignore(numeric_limits<streamsize>::max(), '\n'); // skip 3 lines
getline(in,zeile);
// pre processing to access the mean in O(1)
vector<int> partialSums(lengthOfSequence+1, 0);
partialSums[0] = 0;
for (int i = 0; i < lengthOfSequence; i++) {
partialSums[i+1] = (zeile[i] - shiftedMean) + partialSums[i];
}
// find block with values >= mean, because all subblocks fulfill the
// m-mean condition
vector<pair<int,int>> oneBlocks;
int startOfOneBlock = 0;
bool stillInOneBlock = false;
zeile.append("!"); // dummy 0 at the end
// ASSERT: ASCII("!")=33 and this is the smallest possible char in a quality score string
// we need a dummy "bad" quality score at the end for our algorithm
for (int i = 0; i <= lengthOfSequence; i++) {
if (zeile[i] < shiftedMean) {
if (stillInOneBlock) {
stillInOneBlock = false;
oneBlocks.push_back( make_pair(startOfOneBlock,i-1) );
cT[startOfOneBlock][i-1]++;
}
} else {
if (!stillInOneBlock) {
stillInOneBlock = true;
startOfOneBlock = i;
}
}
}
// remember that we added "!" to zeile, but we will not read it later,
// so there is no need to delete it
// compute c(l,r) for all (l,r) not in the triangles of oneBlocks
// HORIZONTAL
int startrow = 0;
for (auto p: oneBlocks) {
for (int row = startrow; row < p.first; row++) {
for (int col = row+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) >= 0) {
c[row][col]++;
}
}
}
startrow = p.second + 1;
// VERTICAL: everything right of the triangle induced by p
for (int row = p.first; row <= p.second; row++) {
for (int col = p.second+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) >= 0) {
c[row][col]++;
}
}
}
}
// everything after last triangle of 1s
for (int row = startrow; row < lengthOfSequence; row++) {
for (int col = row+1; col < lengthOfSequence; col++) {
if ( (partialSums[col+1] - partialSums[row]) >= 0) {
c[row][col]++;
}
}
}
}
// compute c_aux from cT like in 0-zeros:
vector<vector<int>> c_aux (lengthOfSequence, vector<int>(lengthOfSequence,0));
vector<int> columnSumAbove (lengthOfSequence,0);
// first fill the last column of c_aux
c_aux[0][lengthOfSequence-1] = cT[0][lengthOfSequence-1];
for (int i=1; i < lengthOfSequence; i++){
c_aux[i][lengthOfSequence-1] = cT[i][lengthOfSequence-1] + c_aux[i-1][lengthOfSequence-1];
}
// next fill the first row of c_aux
for (int j=lengthOfSequence-2; j>= 0; j--){
c_aux[0][j] = cT[0][j] + c_aux[0][j+1];
columnSumAbove[j] = cT[0][j];
}
// now fill the rest
for (int i=1; i < lengthOfSequence; i++){
for (int j=lengthOfSequence-2; j>= i; j--){
c_aux[i][j] = cT[i][j] + c_aux[i][j+1] + columnSumAbove[j];
columnSumAbove[j] += cT[i][j];
}
}
// add c and c_aux
for (int i = 0; i < lengthOfSequence; i++) {
for (int j = i; j < lengthOfSequence; j++) {
c[i][j] += c_aux[i][j];
}
}
return c;
}
}