forked from aquaskyline/MICA-aligner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicMappingQuality.c
507 lines (437 loc) · 18.5 KB
/
MicMappingQuality.c
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
/*
*
*
* MicMappingQuality.c
* Soap3(gpu)
*
* Copyright (C) 2011, HKU
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "MicMappingQuality.h"
__attribute__((target(mic)))
static void MicCalculateSraResult(
MICSRAArguments * sraArgs,
CPTSRAModel * cpPModels, CPTSRAModel * cpNModels,
int * numBestOutput, int * num2ndBestOutput) {
int NO_ENTRY = 999;
int size = *sraArgs->occCount;
int numBest = 0;
int bestMismatch = NO_ENTRY;
int numSecondBest = 0;
int secondBestMismatch = NO_ENTRY;
int i;
for (i = 0; i < size; i++) {
int currentMismatch = sraArgs->metaBlock[i].numOfErr;
if (currentMismatch < bestMismatch) {
// New best is found, push the ranking down
// Original best becomes the second best
secondBestMismatch = bestMismatch;
numSecondBest = numBest;
// New best
numBest = 1;
bestMismatch = currentMismatch;
} else if (currentMismatch == bestMismatch) {
numBest++;
} else if (currentMismatch < secondBestMismatch) {
numSecondBest = 1;
secondBestMismatch = currentMismatch;
} else if (currentMismatch == secondBestMismatch) {
numSecondBest++;
}
}
// No second best is found, we need to perform oneMoreMismatch
if (secondBestMismatch == NO_ENTRY) {
MICProcessReadDoubleStrand(sraArgs, cpPModels,
cpNModels);
numSecondBest = *sraArgs->occCount - size;
if (numSecondBest < 0) {
numSecondBest = 0;
}
// TODO: Handle the case where oneMoreMismatch may give too many results
}
(* numBestOutput) = numBest;
(* num2ndBestOutput) = numSecondBest;
}
__attribute__((target(mic)))
static void MicCalculatePeOptimal(
MICPEArguments * peArgs,
DPScores * dpScores,
int * optimalScoreOutput,
int * numOptimalOutput,
int * subOptimalScoreOutput,
int * numSubOptimalOutput) {
int NO_ENTRY = -99999;
int size = *peArgs->occCount;
int numBest = 0;
int bestScore = NO_ENTRY;
int secondBestScore = NO_ENTRY;
int readMateTotalLegnth = peArgs->readArgs->seedLength +
peArgs->mateArgs->seedLength;
int i;
for (i = 0; i < size; i += 2) {
int currentError = peArgs->outputMeta[i].numOfErr +
peArgs->outputMeta[i + 1].numOfErr;
int currentScore = readMateTotalLegnth -
currentError * (dpScores->dpMismatch - dpScores->dpMatch);
if (currentScore > bestScore) {
// New best is found, push the ranking down
// Original best becomes the second best
secondBestScore = bestScore;
// New best
numBest = 1;
bestScore = currentScore;
} else if (currentScore == bestScore) {
numBest++;
} else if (currentScore > secondBestScore) {
secondBestScore = currentScore;
}
}
(* numOptimalOutput) = numBest;
(* optimalScoreOutput) = bestScore;
(* numSubOptimalOutput) = size / 2 - numBest;
(* subOptimalScoreOutput) = secondBestScore;
}
// A more generic function for getting the num of best
// and second best alignment from DP, which can be used by
// Default DP as well as Deep DP
__attribute__((target(mic)))
static void _MicCalculateDpResult(
MICDPOccurrence * dpOccurrences, uint32_t count,
int * numBestOutput, int * num2ndBestOutput,
uint32_t start, uint32_t step) {
int NO_ENTRY = -99999;
int numBest = 0;
int bestScore = NO_ENTRY;
int i;
for (i = start; i < count; i += step) {
int currentScore = dpOccurrences[i].score;
if (currentScore > bestScore) {
// New best
numBest = 1;
bestScore = currentScore;
} else if (currentScore == bestScore) {
numBest++;
}
}
(* numBestOutput) = numBest;
(* num2ndBestOutput) = count - numBest;
}
// This function gives number of best alignment and
// number of second best alignment from DP results,
// without considering the mixing of read and mate DP results
__attribute__((target(mic)))
static void MicCalculateDpResult(
MICDPOccurrence * dpOccurrences, uint32_t count,
int * numBestOutput, int * num2ndBestOutput) {
_MicCalculateDpResult(dpOccurrences, count,
numBestOutput, num2ndBestOutput, 0, 1);
}
__attribute__((target(mic)))
static void MicCalculateDeepDpReadResult(
MICDPOccurrence * dpOccurrences, uint32_t count,
int * numBestOutput, int * num2ndBestOutput) {
_MicCalculateDpResult(dpOccurrences, count,
numBestOutput, num2ndBestOutput, 0 /* start */, 2 /* step */);
}
__attribute__((target(mic)))
static void MicCalculateDeepDpMateResult(
MICDPOccurrence * dpOccurrences, uint32_t count,
int * numBestOutput, int * num2ndBestOutput) {
_MicCalculateDpResult(dpOccurrences, count,
numBestOutput, num2ndBestOutput, 1 /* start */, 2 /* step */);
}
// Jeanno: Currently the result given by (New) Default DP are stored
// in MICPEArguments (Sra Side result) and in MICDPOccurrence array
// (DP side result). Looking forward to redesigning it in the future.
__attribute__((target(mic)))
static void MicCalculateDpOptimal(
MICPEArguments * peArgs,
MICSRAArguments * sraArgs, // The base side sraArg
MICDPOccurrence * dpOccurrences,
DPScores * dpScores,
int * optimalScoreOutput,
int * numOptimalOutput,
int * subOptimalScoreOutput,
int * numSubOptimalOutput) {
int NO_ENTRY = -99999;
int count = *peArgs->occCount;
int numBest = 0;
int bestScore = NO_ENTRY;
int secondBestScore = NO_ENTRY;
int i;
for (i = 0; i < count; i++) {
int currentError = peArgs->outputMeta[i].numOfErr;
int currentScore = sraArgs->seedLength +
currentError * (dpScores->dpMismatch - dpScores->dpMatch) +
dpOccurrences[i].score;
if (currentScore > bestScore) {
// New best is found, push the ranking down
// Original best becomes the second best
secondBestScore = bestScore;
// New best
numBest = 1;
bestScore = currentScore;
} else if (currentScore == bestScore) {
numBest++;
} else if (currentScore > secondBestScore) {
secondBestScore = currentScore;
}
}
(* numOptimalOutput) = numBest;
(* optimalScoreOutput) = bestScore;
(* numSubOptimalOutput) = count - numBest;
(* subOptimalScoreOutput) = secondBestScore;
}
__attribute__((target(mic)))
static void MicCalculateDeepDpOptimal(
MICDPOccurrence * dpOccurrences,
uint32_t count,
int * optimalScoreOutput,
int * numOptimalOutput,
int * subOptimalScoreOutput,
int * numSubOptimalOutput) {
int NO_ENTRY = -99999;
int numBest = 0;
int bestScore = NO_ENTRY;
int secondBestScore = NO_ENTRY;
int i;
for (i = 0; i < count; i += 2) {
int currentScore = dpOccurrences[i].score + dpOccurrences[i + 1].score;
if (currentScore > bestScore) {
// New best is found, push the ranking down
// Original best becomes the second best
secondBestScore = bestScore;
// New best
numBest = 1;
bestScore = currentScore;
} else if (currentScore == bestScore) {
numBest++;
} else if (currentScore > secondBestScore) {
secondBestScore = currentScore;
}
}
(* numOptimalOutput) = numBest;
(* optimalScoreOutput) = bestScore;
(* numSubOptimalOutput) = count - numBest;
(* subOptimalScoreOutput) = secondBestScore;
}
// This is used to get optimal scores after performing mix base DP
__attribute__((target(mic)))
static void MicCalculateMixDpOptimal(
MICPEArguments * peArgs,
MICDPOccurrence * dpOccurrences,
DPScores * dpScores,
int * optimalScoreOutput,
int * numOptimalOutput,
int * subOptimalScoreOutput,
int * numSubOptimalOutput) {
int size = *peArgs->occCount;
// Find the starting point of Mate base
int mateStart;
for (mateStart = 0; mateStart < size; mateStart++) {
MICDPOccurrence * dpOcc = dpOccurrences + mateStart;
if (dpOccurrences->ambPosition == 0 &&
dpOccurrences->matchLen == 0) {
break;
}
}
if (mateStart == size) {
// In this case readBase result doesn't exist
// We can then treat it as single-based mate based DP result
MicCalculateDpOptimal(peArgs, peArgs->mateArgs, dpOccurrences, dpScores,
optimalScoreOutput, numOptimalOutput,
subOptimalScoreOutput, numSubOptimalOutput);
return;
}
mateStart++;
// dpOccurrences[mateStart] should be at the start of mate base results here
// Prepare peArgs for read and mate separately
// to mimic the argument for one side base DP optimal calculation
MICPEArguments readBasePeArgs = *peArgs;
uint16_t readBaseOcc = mateStart - 1;
readBasePeArgs.occCount = &readBaseOcc;
MICPEArguments mateBasePeArgs = *peArgs;
mateBasePeArgs.output += mateStart;
mateBasePeArgs.outputMeta += mateStart;
uint16_t mateBaseOcc = *peArgs->occCount - readBaseOcc;
mateBasePeArgs.occCount = &mateBaseOcc;
// Perform a read base Dp Optimal calculation
int readBasePeOptimalScore, readBasePeNumOptimal,
readBasePeSubOptScore, readBasePeNumSubOpt;
MicCalculateDpOptimal(&readBasePeArgs, readBasePeArgs.readArgs,
dpOccurrences, dpScores,
&readBasePeOptimalScore, &readBasePeNumOptimal,
&readBasePeSubOptScore, &readBasePeNumSubOpt);
// Perform a mate base Dp Optimal calculation
int mateBasePeOptimalScore, mateBasePeNumOptimal,
mateBasePeSubOptScore, mateBasePeNumSubOpt;
MicCalculateDpOptimal(&mateBasePeArgs, mateBasePeArgs.mateArgs,
dpOccurrences, dpScores,
&mateBasePeOptimalScore, &mateBasePeNumOptimal,
&mateBasePeSubOptScore, &mateBasePeNumSubOpt);
// Calculate the final result
// Combining the separated result from readBase and mateBase DP
// into the final opt and subopt result
(* optimalScoreOutput) = readBasePeOptimalScore;
(* numOptimalOutput) = readBasePeNumOptimal;
(* subOptimalScoreOutput) = readBasePeSubOptScore;
(* numSubOptimalOutput) = readBasePeNumSubOpt;
// Merge mateBaseOptimal result into final output
if (mateBasePeOptimalScore > readBasePeOptimalScore) {
(* subOptimalScoreOutput) = readBasePeOptimalScore;
(* numSubOptimalOutput) = readBasePeNumOptimal;
(* optimalScoreOutput) = mateBasePeOptimalScore;
(* numOptimalOutput) = mateBasePeNumOptimal;
} else {
if (mateBasePeOptimalScore == readBasePeOptimalScore) {
(* numOptimalOutput) += mateBasePeNumOptimal;
} else {
if (mateBasePeOptimalScore > readBasePeSubOptScore) {
(* subOptimalScoreOutput) = mateBasePeOptimalScore;
(* numSubOptimalOutput) = mateBasePeNumOptimal;
} else if (mateBasePeOptimalScore == readBasePeSubOptScore) {
(* numSubOptimalOutput) += mateBasePeNumOptimal;
}
}
}
// Merge mateBaseSubOptimal result into final output
// Since mateBaseSubOpt must not be opt in the final result,
// we can safely skip comparing it to the current opt score
if (mateBasePeSubOptScore > (* subOptimalScoreOutput)) {
(* subOptimalScoreOutput) = mateBasePeSubOptScore;
(* numSubOptimalOutput) = mateBasePeNumSubOpt;
} else if (mateBasePeSubOptScore == (* subOptimalScoreOutput)) {
(* numSubOptimalOutput) += mateBasePeNumSubOpt;
}
}
// g_log_n: this should be initiate by bwase_initialize
// the size of the array is 256
__attribute__((target(mic)))
PEMappingQuality MicCalculatePEMappingQuality(
MICSRAArguments * readSraArgs,
MICSRAArguments * mateSraArgs,
MICPEArguments * peArgs,
MICDPOccurrence * dpOccurrences,
CPTSRAModel * cpPModels,
CPTSRAModel * cpNModels,
int * g_log_n) {
DPScores * dpScores = peArgs->dpScores;
PEMappingQuality ret;
ret.readQuality = 0;
ret.mateQuality = 0;
if (*peArgs->outputStatus == MIC_PE_OUTPUT_STATUS_PAIR) {
// Find readNumBest & readNum2ndBest
int readNumBest, readNum2ndBest;
MicCalculateSraResult(readSraArgs, cpPModels, cpNModels,
&readNumBest, &readNum2ndBest);
// Find mateNumBest & mateNum2ndBest
int mateNumBest, mateNum2ndBest;
MicCalculateSraResult(mateSraArgs, cpPModels, cpNModels,
&mateNumBest, &mateNum2ndBest);
// Find peOptimalScore & peNumOptimal
// Find peSubOptScore & peNumSubOpt
int peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt;
MicCalculatePeOptimal(peArgs, dpScores, &peOptimalScore, &peNumOptimal,
&peSubOptScore, &peNumSubOpt);
// Plug the parameter needed for the scores
bwaLikePairQualScore(readNumBest, readNum2ndBest, mateNumBest, mateNum2ndBest,
g_log_n, peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt,
readSraArgs->seedLength, mateSraArgs->seedLength,
&ret.readQuality, &ret.mateQuality);
} else if (*peArgs->outputStatus == MIC_PE_OUTPUT_STATUS_DP_BASE_READ) {
// Read alignment is from SRA
// Mate alignment is from DP
// Find readNumBest & readNum2ndBest
int readNumBest, readNum2ndBest;
MicCalculateSraResult(readSraArgs, cpPModels, cpNModels,
&readNumBest, &readNum2ndBest);
// Find mateNumBest & mateNum2ndBest
int mateNumBest, mateNum2ndBest;
MicCalculateDpResult(dpOccurrences, *peArgs->occCount,
&mateNumBest, &mateNum2ndBest);
// Find peOptimalScore & peNumOptimal
// Find peSubOptScore & peNumSubOpt
int peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt;
MicCalculateDpOptimal(peArgs, readSraArgs, dpOccurrences, dpScores,
&peOptimalScore, &peNumOptimal,
&peSubOptScore, &peNumSubOpt);
// Plug the parameter needed for the scores
bwaLikePairQualScore(readNumBest, readNum2ndBest, mateNumBest, mateNum2ndBest,
g_log_n, peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt,
readSraArgs->seedLength, mateSraArgs->seedLength,
&ret.readQuality, &ret.mateQuality);
} else if (*peArgs->outputStatus == MIC_PE_OUTPUT_STATUS_DP_BASE_MATE) {
// Read alignment is from DP
// Mate alignment is from SRA
// Find readNumBest & readNum2ndBest
int readNumBest, readNum2ndBest;
MicCalculateDpResult(dpOccurrences, *peArgs->occCount,
&readNumBest, &readNum2ndBest);
// Find mateNumBest & mateNum2ndBest
int mateNumBest, mateNum2ndBest;
MicCalculateSraResult(mateSraArgs, cpPModels, cpNModels,
&mateNumBest, &mateNum2ndBest);
// Find peOptimalScore & peNumOptimal
// Find peSubOptScore & peNumSubOpt
int peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt;
MicCalculateDpOptimal(peArgs, mateSraArgs, dpOccurrences, dpScores,
&peOptimalScore, &peNumOptimal,
&peSubOptScore, &peNumSubOpt);
// Plug the parameter needed for the scores
bwaLikePairQualScore(readNumBest, readNum2ndBest, mateNumBest, mateNum2ndBest,
g_log_n, peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt,
readSraArgs->seedLength, mateSraArgs->seedLength,
&ret.readQuality, &ret.mateQuality);
} else if (*peArgs->outputStatus == MIC_PE_OUTPUT_STATUS_DP_MIX_BASE) {
// Find readNumBest & readNum2ndBest
int readNumBest, readNum2ndBest;
MicCalculateSraResult(readSraArgs, cpPModels, cpNModels,
&readNumBest, &readNum2ndBest);
// Find mateNumBest & mateNum2ndBest
int mateNumBest, mateNum2ndBest;
MicCalculateSraResult(mateSraArgs, cpPModels, cpNModels,
&mateNumBest, &mateNum2ndBest);
// Find peOptimalScore & peNumOptimal
// Find peSubOptScore & peNumSubOpt
int peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt;
MicCalculateMixDpOptimal(peArgs, dpOccurrences, dpScores,
&peOptimalScore, &peNumOptimal,
&peSubOptScore, &peNumSubOpt);
} else if (*peArgs->outputStatus == MIC_PE_OUTPUT_STATUS_DP_SEED) {
// // Case of Deep DP
// // Find readNumBest & readNum2ndBest
int readNumBest, readNum2ndBest;
MicCalculateDeepDpReadResult(dpOccurrences, *peArgs->output,
&readNumBest, &readNum2ndBest);
// Find mateNumBest & mateNum2ndBest
int mateNumBest, mateNum2ndBest;
MicCalculateDeepDpMateResult(dpOccurrences, *peArgs->output,
&mateNumBest, &mateNum2ndBest);
// Find peOptimalScore & peNumOptimal
// Find peSubOptScore & peNumSubOpt
int peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt;
MicCalculateDeepDpOptimal(dpOccurrences, *peArgs->occCount,
&peOptimalScore, &peNumOptimal,
&peSubOptScore, &peNumSubOpt);
// Plug the parameter needed for the scores
bwaLikePairQualScore(readNumBest, readNum2ndBest, mateNumBest, mateNum2ndBest,
g_log_n, peOptimalScore, peNumOptimal, peSubOptScore, peNumSubOpt,
readSraArgs->seedLength, mateSraArgs->seedLength,
&ret.readQuality, &ret.mateQuality);
}
return ret;
}