-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMIC-PEAlgnmt.c
458 lines (379 loc) · 14.2 KB
/
MIC-PEAlgnmt.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
//
// MIC-PEAlgnmt.c
//
// SOAP2 / 2BWT
//
// Copyright (C) 2013, 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 "MIC-PEAlgnmt.h"
__attribute__((target(mic)))
void MICPEArgumentsConfig(
MICPEArguments * peArguments,
MICSRAArguments * readArgs,
MICSRAArguments * mateArgs,
unsigned int outputVacancy,
unsigned int metaVacancy,
unsigned int * output,
MICSRAOccMetadata * outputMeta,
uint16_t * occCount,
uint8_t * outputStatus) {
peArguments->readArgs = readArgs;
peArguments->mateArgs = mateArgs;
peArguments->output = output;
peArguments->outputMeta = outputMeta;
peArguments->outputVacancy = outputVacancy;
peArguments->metaVacancy = metaVacancy;
peArguments->outputStatus = outputStatus;
peArguments->occCount = occCount;
peArguments->mergeEnable = 0;
}
__attribute__((target(mic)))
static void MICQuickSort(unsigned int * nums, MICSRAOccMetadata * metadata, uint16_t size) {
if (size <= 1) return;
unsigned int pivot = nums[size/2];
unsigned int temp;
MICSRAOccMetadata tempMeta;
int left = 0;
int right = size - 1;
while (left <= right) {
while (nums[left] < pivot) {
left++;
}
while (nums[right] > pivot) {
right--;
}
if (left <= right) {
// Swap the elements
temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
tempMeta = metadata[left];
metadata[left] = metadata[right];
metadata[right] = tempMeta;
left++;
right--;
}
}
MICQuickSort(nums, metadata, right + 1);
MICQuickSort(nums + left, metadata + left, size - left);
}
// Input parameter pos, should be sorted before passing into this routine
__attribute__((target(mic)))
static uint16_t mergePositions(unsigned int * pos, MICSRAOccMetadata * metadata, uint16_t size) {
if (size == 0) {
return 0;
}
int i;
uint16_t newSize = 1;
for (i = 1; i < size; i++) {
if (pos[i] - pos[newSize - 1] < MIC_PAIRING_MERGE_LIMIT) {
pos[newSize] = pos[i];
metadata[newSize] = metadata[i];
newSize++;
}
}
return newSize;
}
__attribute__((target(mic)))
static void copySingleEndResults(MICPEArguments * peArgs, MICSRAArguments * sraArgs) {
if (*sraArgs->occCount > peArgs->outputVacancy ||
*sraArgs->occCount > peArgs->metaVacancy) {
(*peArgs->outputStatus) = MIC_PE_OUTPUT_STATUS_UNHANDLE;
return;
}
(*peArgs->occCount) = *sraArgs->occCount;
// Limit the output
if (*peArgs->occCount > peArgs->outputLimit &&
peArgs->outputLimit > -1) {
*peArgs->occCount = peArgs->outputLimit;
}
unsigned int i;
for (i=0; i<(*peArgs->occCount); i++) {
peArgs->output[i] = sraArgs->outputBlock[i];
peArgs->outputMeta[i] = sraArgs->metaBlock[i];
}
}
__attribute__((target(mic)))
static void pairOneStrand(unsigned int * readOcc, unsigned int * mateOcc,
MICSRAOccMetadata * readMeta, MICSRAOccMetadata * mateMeta,
uint32_t readOccSize, uint32_t mateOccSize,
MICPEArguments * peArguments, char readStrand) {
// Perform merging
unsigned int ub = peArguments->uBound;
unsigned int lb = peArguments->lBound;
unsigned int outputIdx = *peArguments->occCount;
uint16_t leftIdx = 0;
uint16_t rightStart = 0;
uint16_t rightEnd = 0;
unsigned int vacancy = peArguments->outputVacancy;
char shouldEnd = 0;
uint32_t leftOccSize;
unsigned int * leftOcc;
MICSRAOccMetadata * leftMeta;
uint32_t rightOccSize;
unsigned int * rightOcc;
MICSRAOccMetadata * rightMeta;
uint16_t rightLength;
// Correctly assign left and right legs
if (readStrand == 0) {
// case of read should be leftLeg
leftOccSize = readOccSize;
leftOcc = readOcc;
leftMeta = readMeta;
rightOccSize = mateOccSize;
rightOcc = mateOcc;
rightMeta = mateMeta;
rightLength = peArguments->mateArgs->seedLength;
} else {
// case of read should be rightLeg
leftOccSize = mateOccSize;
leftOcc = mateOcc;
leftMeta = mateMeta;
rightOccSize = readOccSize;
rightOcc = readOcc;
rightMeta = readMeta;
rightLength = peArguments->readArgs->seedLength;
}
// The range is [rightStart, rightEnd)
// rightEnd is not included.
// Pair each leftOcc with rightOcc
// Strands are checked
for (leftIdx=0; leftIdx<leftOccSize; leftIdx++) {
// This check aims at ignoring any duplicated occurrences on the list
if (leftIdx > 0 && leftOcc[leftIdx] == leftOcc[leftIdx-1]) {
continue;
}
if (leftMeta[leftIdx].strand != 0) {
continue;
}
// Move rightStart into correct index
while (rightStart < rightOccSize &&
(leftOcc[leftIdx] + lb > rightOcc[rightStart] + rightLength ||
rightMeta[rightStart].strand != 1)) {
rightStart++;
}
if (rightStart >= rightOccSize) {
break;
}
// Move rightEnd into correct index
rightEnd = rightStart;
while (rightEnd < rightOccSize &&
(leftOcc[leftIdx] + ub >= rightOcc[rightEnd] + rightLength ||
rightMeta[rightStart].strand != 1)) {
rightEnd++;
}
//assert(rightEnd <= rightOccSize);
// Write to output
int i;
for (i=rightStart; i<rightEnd &&
(*peArguments->outputStatus) != MIC_PE_OUTPUT_STATUS_UNHANDLE; i++) {
if (rightMeta[i].strand != 1) {
continue;
}
if (vacancy - outputIdx >= 2) {
// Put result into output
// Check output limit per PE alignment
if (outputIdx >= MIC_PE_MAX_RESULT) {
shouldEnd = 1;
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_CLOSED;
break;
} else if (outputIdx >= peArguments->outputLimit &&
peArguments->outputLimit > -1) {
shouldEnd = 1;
break;
}
//printf("Pair: %d, %d; Gap: %d\n", leftOcc[leftIdx], rightOcc[i], rightOcc[i] - leftOcc[leftIdx]);
// Handle output order
if (readStrand == 0) {
peArguments->outputMeta[outputIdx] = leftMeta[leftIdx];
peArguments->output[outputIdx++] = leftOcc[leftIdx];
}
peArguments->outputMeta[outputIdx] = rightMeta[i];
peArguments->output[outputIdx++] = rightOcc[i];
if (readStrand == 1) {
peArguments->outputMeta[outputIdx] = leftMeta[leftIdx];
peArguments->output[outputIdx++] = leftOcc[leftIdx];
}
} else {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_UNHANDLE;
shouldEnd = 1;
break;
}
}
if (shouldEnd) {
break;
}
}
if ((*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_OPEN) {
(*peArguments->occCount) = outputIdx;
} else {
(*peArguments->occCount) = 0;
}
}
__attribute__((target(mic)))
static void outputLeastMismatchResult(MICPEArguments * peArgs, MICSRAArguments * sraArgs) {
uint8_t leastError = 100;
uint16_t selectedOccIdx = 0;
uint16_t selectedMetaIdx = 0;
uint16_t occIdx = 0;
uint16_t metaIdx = 0;
for (metaIdx=0; metaIdx<(*sraArgs->metaCount); metaIdx++) {
uint8_t currentErrorNum = sraArgs->metaBlock[metaIdx].numOfErr;
if (currentErrorNum < leastError) {
selectedOccIdx = occIdx;
selectedMetaIdx = metaIdx;
leastError = currentErrorNum;
}
occIdx++;
}
// Now selectedOccIdx and selectedMetaIdx should have the least mismatches.
peArgs->output[(*peArgs->occCount)] = sraArgs->outputBlock[selectedOccIdx];
peArgs->outputMeta[(*peArgs->occCount)] = sraArgs->metaBlock[selectedMetaIdx];
(*peArgs->occCount)++;
}
__attribute__((target(mic)))
static void pairBadPair(MICPEArguments * peArgs) {
outputLeastMismatchResult(peArgs, peArgs->readArgs);
outputLeastMismatchResult(peArgs, peArgs->mateArgs);
(*peArgs->outputStatus) = MIC_PE_OUTPUT_STATUS_BAD_PAIR;
}
/**
* Returns number of output occurrences
**/
__attribute__((target(mic)))
void MICPEMappingInitialise(MICPEArguments * peArguments) {
(*peArguments->occCount) = 0;
}
/**
* Returns number of output occurrences
**/
__attribute__((target(mic)))
void MICPEMappingOccurrences(MICPEArguments * peArguments) {
MICSRAArguments * readArgs = peArguments->readArgs;
MICSRAArguments * mateArgs = peArguments->mateArgs;
if ((*readArgs->outputStatus) == MIC_OUTPUT_STATUS_SKIPPED ||
(*mateArgs->outputStatus) == MIC_OUTPUT_STATUS_SKIPPED) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_SKIPPED;
return;
}
if ((*readArgs->outputStatus) == MIC_OUTPUT_STATUS_UNHANDLE ||
(*mateArgs->outputStatus) == MIC_OUTPUT_STATUS_UNHANDLE ||
(*readArgs->outputStatus) == MIC_OUTPUT_STATUS_CLOSE ||
(*mateArgs->outputStatus) == MIC_OUTPUT_STATUS_CLOSE) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_CLOSED;
return;
}
if ((*readArgs->occCount == 0) || (*mateArgs->occCount == 0)) {
return;
}
if (peArguments->outputVacancy < 2 || peArguments->metaVacancy < 2) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_UNHANDLE;
return;
}
// Finished handling error cases
// Remaining possible cases of status
// PAIR, BAD_PAIR, UNHANDLE, CLOSED
// Expand the read and mate metaBlock into the length of occCount
uint32_t readOccSize = *readArgs->occCount;
uint32_t mateOccSize = *mateArgs->occCount;
MICSRAOccMetadata * readMeta = readArgs->metaBlock;
MICSRAOccMetadata * mateMeta = mateArgs->metaBlock;
// Copy out read and mate occ
unsigned int * readOcc = readArgs->outputBlock;
unsigned int * mateOcc = mateArgs->outputBlock;
// Enhanced quicksort to move extended metaBlock
// Sort Read Occ
MICQuickSort(readOcc, readMeta, readOccSize);
// Sort Mate Occ
MICQuickSort(mateOcc, mateMeta, mateOccSize);
if (peArguments->mergeEnable) {
readOccSize = mergePositions(readOcc, readMeta, readOccSize);
mateOccSize = mergePositions(mateOcc, mateMeta, mateOccSize);
}
pairOneStrand(readOcc, mateOcc, readMeta, mateMeta,
readOccSize, mateOccSize, peArguments, 0);
if ((*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_OPEN ||
(*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_PAIR) {
pairOneStrand(readOcc, mateOcc, readMeta, mateMeta,
readOccSize, mateOccSize, peArguments, 1);
}
if ((*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_OPEN &&
(*peArguments->occCount) > 0) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_PAIR;
}
}
/**
* Returns number of output occurrences
**/
__attribute__((target(mic)))
void MICPEMappingComplete(MICPEArguments * peArguments) {
MICSRAArguments * readArgs = peArguments->readArgs;
MICSRAArguments * mateArgs = peArguments->mateArgs;
if ((*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_SKIPPED ||
(*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_CLOSED ||
(*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_UNHANDLE) {
return;
}
if ((*readArgs->occCount == 0) && (*mateArgs->occCount == 0)) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_BOTH_NO_ALIGNMENT;
return;
}
if (*readArgs->occCount == 0) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_READ_NO_ALIGNMENT;
copySingleEndResults(peArguments, mateArgs);
return;
}
if (*mateArgs->occCount == 0) {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_MATE_NO_ALIGNMENT;
copySingleEndResults(peArguments, readArgs);
return;
}
if ((*peArguments->outputStatus) == MIC_PE_OUTPUT_STATUS_OPEN) {
if ((*peArguments->occCount) == 0) {
// Proceed to BAD_PAIR
pairBadPair(peArguments);
} else {
(*peArguments->outputStatus) = MIC_PE_OUTPUT_STATUS_PAIR;
}
}
}
__attribute__((target(mic)))
void MICPEArgumentsSetBounds(MICPEArguments * peArguments,
unsigned int lowerBound,
unsigned int upperBound) {
peArguments->lBound = lowerBound;
peArguments->uBound = upperBound;
}
// Setter function for DP related parameters
__attribute__((target(mic)))
void MICPEArgumentsSetDPScores(MICPEArguments * peArguments,
DPScores * dpScores) {
peArguments->dpScores = dpScores;
}
// Set a limit to output, occurrences after the limit will be dropped
__attribute__((target(mic)))
void MICPEArgumentsSetMaxOutput(MICPEArguments * peArguments,
int outputLimit) {
peArguments->outputLimit = outputLimit;
}
// Set a limit to output, occurrences after the limit will be dropped
__attribute__((target(mic)))
void MICPEArgumentsSetMergeEnable(MICPEArguments * peArguments,
uint8_t mergeEnable) {
peArguments->mergeEnable = mergeEnable;
}