-
Notifications
You must be signed in to change notification settings - Fork 8
/
PEAlgnmt.cpp
1296 lines (1119 loc) · 43.9 KB
/
PEAlgnmt.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* PEAlgnmt.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 "PEAlgnmt.h"
// --------------------------------------------------
// PE_DEBUG #1
// Uncomment the below to turn on logging message for
// PE matching of two occurrences.
//#define PE_DEBUG_PRINT_MATCHING_PROGRESS
// --------------------------------------------------
// --------------------------------------------------
// PE_DEBUG #2
// Uncomment the below to turn on logging message for
// PE Radix Sort of two occ lists.
//#define PE_DEBUG_PRINT_RADIX_PROGRESS
// --------------------------------------------------
// --------------------------------------------------
// PE_DEBUG #3
// Uncomment the below to turn on visual logging for
// PE matching of two occurrences.
//#define PE_DEBUG_PRINT_VISUAL_MATCHING
// --------------------------------------------------
void PERadixSort ( SRAOccurrence * list, unsigned int count,
SRAOccurrence * buffer1, SRAOccurrence * buffer2,
SRAOccurrence * sortedList );
void PEReportPairResult ( PEOutput * peOutput, SRAOccurrence * occ_1, SRAOccurrence * occ_2, unsigned int insertion );
inline int PEIsPairEndMatch ( PEInput * peInput, SRAOccurrence * occ_1, SRAOccurrence * occ_2, unsigned int * insertion );
inline int PEIsPairOutOfRange ( PEInput * peInput, SRAOccurrence * occ_1, SRAOccurrence * occ_2 );
void PEPairListInitialise ( PEPairList * pePairList );
void PEOutputInitialise ( PEOutput * peOutput );
PEPairList * PEPairListConstruct ();
void PEPairListFree ( PEPairList * pePairList );
/////////////////////////////////////////////////////
/*
Function SRAEnrichSARanges
*/
/////////////////////////////////////////////////////
/*
unsigned int SRAEnrichSARanges(BWT * bwt, HSP * hsp,
unsigned int saIndexLeft, unsigned int saIndexRight,
char * strand, int * mismatchCount) {
}
*/
/////////////////////////////////////////////////////
/*
Function PERetrievePositionFromSAIndex
This function is a place-holder for possibly extension of
PE to use High-Occ data-structure. However unlikely.
*/
/////////////////////////////////////////////////////
inline unsigned int PERetrievePositionFromSAIndex ( BWT * bwt, unsigned int saIndex )
{
unsigned int ambPosition = ( *bwt->_bwtSaValue ) ( bwt, saIndex );
return ambPosition;
}
/////////////////////////////////////////////////////
/*
Function PERetrieveChromoPositioning
*/
/////////////////////////////////////////////////////
void PERetrieveChromoPositioning ( BWT * bwt,
HSP * hsp,
unsigned int ambPosition,
int * sequenceId, unsigned int * offset )
{
unsigned int * ambiguityMap = hsp->ambiguityMap;
Translate * translate = hsp->translate;
unsigned int approxIndex = ambPosition >> GRID_SAMPLING_FACTOR_2_POWER;
unsigned int approxValue = ambiguityMap[approxIndex];
while ( translate[approxValue].startPos > ambPosition )
{
approxValue--;
}
ambPosition -= translate[approxValue].correction;
( *sequenceId ) = translate[approxValue].chrID;
( *offset ) = ambPosition;
}
/////////////////////////////////////////////////////
/*
Function PERadixSort
*/
/////////////////////////////////////////////////////
void PERadixSort ( SRAOccurrence * list, unsigned int count,
SRAOccurrence * buffer1, SRAOccurrence * buffer2,
SRAOccurrence * sortedList )
{
//How many bits we sort each pass
#define PE_RADIX_BIT_PER_PASS 4
//How many pass in total we run the sorting
#define PE_RADIX_PASS 8
//The multiplying result of the two parameters above should be
//greater than the bit-length of the maximum index on the ref.seq.
//e.g. 4 and 8 such that the sorting will cover 32-bit
// which can represent all index in the ref. seq.
unsigned int i, j;
unsigned int base;
int idxIndexSize = ( 1 << PE_RADIX_BIT_PER_PASS );
int radixMask = idxIndexSize - 1;
unsigned int idxRadix[idxIndexSize + 1];
SRAOccurrence * swap;
SRAOccurrence * activeBuffer = buffer1;
SRAOccurrence * oldBuffer = list;
#ifdef PE_DEBUG_PRINT_RADIX_PROGRESS
printf ( " idxIndexSize = %d\n", idxIndexSize );
printf ( " radixMask = %d\n", radixMask );
printf ( " PE_RADIX_BIT_PER_PASS = %d\n", PE_RADIX_BIT_PER_PASS );
printf ( " PE_RADIX_PASS = %d\n", PE_RADIX_PASS );
#endif
for ( base = 0; base < PE_RADIX_PASS; base++ )
{
//Initialise idxRadix
for ( i = 0; i <= idxIndexSize; i++ )
{
idxRadix[i] = 0;
}
//Count oldBuffer into activeBuffer
for ( i = 0; i < count; i++ )
{
unsigned int key = oldBuffer[i].ambPosition;
key = key >> base * PE_RADIX_BIT_PER_PASS;
key &= radixMask;
idxRadix[key + 1]++;
}
//Post-process idxRadix
for ( i = 1; i <= idxIndexSize; i++ )
{
idxRadix[i] += idxRadix[i - 1];
}
//Move oldBuffer into activeBuffer with ordering
for ( i = 0; i < count; i++ )
{
unsigned int key = oldBuffer[i].ambPosition;
key = key >> base * PE_RADIX_BIT_PER_PASS;
key &= radixMask;
activeBuffer[idxRadix[key]].ambPosition = oldBuffer[i].ambPosition;
activeBuffer[idxRadix[key]].mismatchCount = oldBuffer[i].mismatchCount;
activeBuffer[idxRadix[key]].strand = oldBuffer[i].strand;
idxRadix[key]++;
}
//Swap double buffer
swap = activeBuffer;
if ( base == 0 )
{
activeBuffer = buffer2;
}
else
{
activeBuffer = oldBuffer;
}
oldBuffer = swap;
}
//Count oldBuffer into sortedlist
for ( i = 0; i < count; i++ )
{
sortedList[i].ambPosition = oldBuffer[i].ambPosition;
sortedList[i].mismatchCount = oldBuffer[i].mismatchCount;
sortedList[i].strand = oldBuffer[i].strand;
}
}
inline void PEMappingCore ( PEInput * peInput, PEOutput * peOutput,
SRAOccurrence * occList_1, unsigned int occCount_1,
SRAOccurrence * occList_2, unsigned int occCount_2 )
{
// ATTENTION : BRUTE FORCE ENGINE
//---------------------------------------------------------
/*unsigned int occIndex_First = 0;
unsigned int occIndex_Last = 0;
for (occIndex_1=0;occIndex_1<occCount_1;occIndex_1++) {
int flag = 0;
for (occIndex_2=occIndex_First;occIndex_2<occCount_2;occIndex_2++) {
if (PEIsPairEndMatch(peInput, &(occList_1[occIndex_1]), &(occList_2[occIndex_2]))) {
if (flag==0) {
occIndex_First = occIndex_2;
flag=1;
}
//Output answer
PEReportPairResult(peOutput,&(occList_1[occIndex_1]),&(occList_2[occIndex_2]));
occIndex_Last = occIndex_2;
}
if (PEIsPairOutOfRange(peInput, &(occList_1[occIndex_1]), &(occList_2[occIndex_2]))) {
break;
}
}
}*/
// ATTENTION : BRUTE FORCE ENGINE - MARK 2
//---------------------------------------------------------
unsigned long i;
unsigned long occIndex_1 = 0;
unsigned long occIndex_2 = 0;
int OutputType = peInput->OutputType;
int strandLeftLeg = peInput->strandLeftLeg;
int strandRightLeg = peInput->strandRightLeg;
unsigned int insertion;
while ( occIndex_1 < occCount_1 && occIndex_2 < occCount_2 )
{
if ( occList_1[occIndex_1].ambPosition <= occList_2[occIndex_2].ambPosition )
{
if ( occList_1[occIndex_1].strand == strandLeftLeg )
{
for ( i = occIndex_2; i < occCount_2; i++ )
{
if ( PEIsPairEndMatch ( peInput, & ( occList_1[occIndex_1] ), & ( occList_2[i] ), &insertion ) )
{
PEReportPairResult ( peOutput, & ( occList_1[occIndex_1] ), & ( occList_2[i] ), insertion );
if ( OutputType == PE_REPORT_ONE ) { break; }
}
if ( PEIsPairOutOfRange ( peInput, & ( occList_1[occIndex_1] ), & ( occList_2[i] ) ) )
{
break;
}
}
}
occIndex_1++;
}
else
{
#ifndef BGS_FWD_FOR_FIRST_REV_FOR_SECOND
if ( occList_2[occIndex_2].strand == strandLeftLeg )
{
for ( i = occIndex_1; i < occCount_1; i++ )
{
if ( PEIsPairEndMatch ( peInput, & ( occList_2[occIndex_2] ), & ( occList_1[i] ), &insertion ) )
{
PEReportPairResult ( peOutput, & ( occList_1[i] ), & ( occList_2[occIndex_2] ), insertion );
if ( OutputType == PE_REPORT_ONE ) { break; }
}
if ( PEIsPairOutOfRange ( peInput, & ( occList_2[occIndex_2] ), & ( occList_1[i] ) ) )
{
break;
}
}
}
#endif
occIndex_2++;
}
}
//---------------------------------------------------------
}
/////////////////////////////////////////////////////
/*
Function PEValidateAndPreparePEInput
*/
/////////////////////////////////////////////////////
inline int PEValidateAndPreparePEInput ( PEInput * peInput )
{
int isValid = 1;
int isWarning = 0;
if ( peInput->bwt == NULL || peInput->hsp == NULL )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "[PEValidateAndPreparePEInput] BWT/HSP index missing.\n" );
#endif
isValid = 0;
}
if ( peInput->OutputType != PE_REPORT_ALL &&
peInput->OutputType != PE_REPORT_ONE )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "[PEValidateAndPreparePEInput] Defaulting Output Type.\n" );
#endif
peInput->OutputType = PE_REPORT_ALL;
isWarning = 1;
}
if ( peInput->strandLeftLeg == PE_NON_INPUT_VALUE ||
peInput->strandRightLeg == PE_NON_INPUT_VALUE )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "[PEValidateAndPreparePEInput] Defaulting strandLeftLeg / strandRightLeg.\n" );
#endif
peInput->strandLeftLeg = QUERY_POS_STRAND;
peInput->strandRightLeg = QUERY_NEG_STRAND;
isWarning = 1;
}
if ( peInput->patternLength == PE_NON_INPUT_VALUE )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "[PEValidateAndPreparePEInput] patternLength missing.\n" );
#endif
isValid = 0;
}
if ( ( peInput->insertSizeMean == PE_NON_INPUT_VALUE ||
peInput->insertSizeStdDev == PE_NON_INPUT_VALUE ) &&
( peInput->insertLbound == PE_NON_INPUT_VALUE ||
peInput->insertUbound == PE_NON_INPUT_VALUE ) )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "[PEValidateAndPreparePEInput] insertion size parameter missing.\n" );
#endif
isValid = 0;
}
if ( peInput->insertLbound == PE_NON_INPUT_VALUE ||
peInput->insertUbound == PE_NON_INPUT_VALUE )
{
peInput->insertLbound = peInput->insertSizeMean - 3 * peInput->insertSizeStdDev;
peInput->insertUbound = peInput->insertSizeMean + 3 * peInput->insertSizeStdDev;
}
return isValid;
}
/////////////////////////////////////////////////////
/*
Function PEMapping
*/
/////////////////////////////////////////////////////
void PEMapping ( PEInput * peInput, PEOutput * peOutput,
PESRAAlignmentResult * resultListA, unsigned int resultCountA,
PESRAAlignmentResult * resultListB, unsigned int resultCountB )
{
//2BWT Index parameter
BWT * bwt = peInput->bwt;
HSP * hsp = peInput->hsp;
//Declare variables
unsigned int i, j;
int k;
unsigned int allocMemory = 0;
//These sort buffer will be used as double buffer for the sorting
SRAOccurrence * sortBuffer_1;
SRAOccurrence * sortBuffer_2;
SRAOccurrence * occList_1;
SRAOccurrence * occList_2;
unsigned int occIndex_1, occIndex_2;
//Initialise the peOutput
PEOutputInitialise ( peOutput );
//enrich the peInput
// [m - 3 * sd, m + 3 * sd], and the
if ( !PEValidateAndPreparePEInput ( peInput ) )
{
peOutput->flag = PE_ALIGNMENT_INPUT_ERROR;
return;
}
//More information about the occurrence list
unsigned int occCount_1 = 0, occCount_2 = 0;
unsigned int occMaxCount = 0;
for ( i = 0; i < resultCountA; i++ )
{
occCount_1 += resultListA[i].saIndexRight - resultListA[i].saIndexLeft + 1;
}
for ( i = 0; i < resultCountB; i++ )
{
occCount_2 += resultListB[i].saIndexRight - resultListB[i].saIndexLeft + 1;
}
occMaxCount = occCount_1;
if ( occMaxCount < occCount_2 )
{
occMaxCount = occCount_2;
}
//Allocate enough memory for all occurrences
//We need at most (4 x max(occCount_1,occCount_2) x 10 Bytes (SRAOccurrence) in total for PE mapping.
//Assuming we have at least 256-Megabyte left in main memory
#define PE_ALLOCATED_BYTE 268435456
allocMemory = 4 * occMaxCount * sizeof ( SRAOccurrence );
if ( allocMemory > PE_ALLOCATED_BYTE )
{
fprintf ( stderr, "Insufficient memory to proceed. %u required exceeded %u.\n", allocMemory, PE_ALLOCATED_BYTE );
exit ( 1 );
}
sortBuffer_1 = ( SRAOccurrence * ) malloc ( occMaxCount * sizeof ( SRAOccurrence ) ) ;
sortBuffer_2 = ( SRAOccurrence * ) malloc ( occMaxCount * sizeof ( SRAOccurrence ) ) ;
occList_1 = ( SRAOccurrence * ) malloc ( occCount_1 * sizeof ( SRAOccurrence ) ) ;
occList_2 = ( SRAOccurrence * ) malloc ( occCount_2 * sizeof ( SRAOccurrence ) ) ;
//Retrieve the occurrences
occIndex_1 = 0;
for ( i = 0; i < resultCountA; i++ )
{
for ( j = resultListA[i].saIndexLeft; j <= resultListA[i].saIndexRight; j++ )
{
occList_1[occIndex_1].ambPosition = PERetrievePositionFromSAIndex ( bwt, j );
occList_1[occIndex_1].strand = resultListA[i].strand;
occList_1[occIndex_1].mismatchCount = resultListA[i].mismatchCount;
occIndex_1++;
}
}
occIndex_2 = 0;
for ( i = 0; i < resultCountB; i++ )
{
for ( j = resultListB[i].saIndexLeft; j <= resultListB[i].saIndexRight; j++ )
{
occList_2[occIndex_2].ambPosition = PERetrievePositionFromSAIndex ( bwt, j );
occList_2[occIndex_2].strand = resultListB[i].strand;
occList_2[occIndex_2].mismatchCount = resultListB[i].mismatchCount;
occIndex_2++;
}
}
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "%u(%u) positions are retrieved from %u SA ranges.\n", occIndex_1, occCount_1, resultCountA );
printf ( "%u(%u) positions are retrieved from %u SA ranges.\n", occIndex_2, occCount_2, resultCountB );
#endif
//Radix sort the lists
PERadixSort ( occList_1, occCount_1, sortBuffer_1, sortBuffer_2, occList_1 );
PERadixSort ( occList_2, occCount_2, sortBuffer_1, sortBuffer_2, occList_2 );
//Calling the core
PEMappingCore ( peInput, peOutput, occList_1, occCount_1, occList_2, occCount_2 );
peOutput->flag = PE_ALIGNMENT_COMPLETED;
free ( sortBuffer_1 );
free ( sortBuffer_2 );
free ( occList_1 );
free ( occList_2 );
}
/////////////////////////////////////////////////////
/*
Function PEMappingOccurrences
*/
/////////////////////////////////////////////////////
void PEMappingOccurrences ( PEInput * peInput, PEOutput * peOutput,
SRAOccurrence * occList_1, unsigned int occCount_1,
SRAOccurrence * occList_2, unsigned int occCount_2 )
{
//2BWT Index parameter
BWT * bwt = peInput->bwt;
HSP * hsp = peInput->hsp;
//Declare variables
unsigned int i, j;
int k;
unsigned int allocMemory = 0;
//These sort buffer will be used as double buffer for the sorting
SRAOccurrence * sortBuffer_1;
SRAOccurrence * sortBuffer_2;
unsigned int occIndex_1, occIndex_2;
//Initialise the peOutput
PEOutputInitialise ( peOutput );
//enrich the peInput
// [m - 3 * sd, m + 3 * sd], and the
if ( !PEValidateAndPreparePEInput ( peInput ) )
{
peOutput->flag = PE_ALIGNMENT_INPUT_ERROR;
return;
}
//More information about the occurrence list
unsigned int occMaxCount = 0;
occMaxCount = occCount_1;
if ( occMaxCount < occCount_2 )
{
occMaxCount = occCount_2;
}
//Allocate enough memory for all occurrences
//We need at most (4 x max(occCount_1,occCount_2) x 10 Bytes (SRAOccurrence) in total for PE mapping.
//Assuming we have at least 256-Megabyte left in main memory
#define PE_ALLOCATED_BYTE 268435456
allocMemory = 4 * occMaxCount * sizeof ( SRAOccurrence );
if ( allocMemory > PE_ALLOCATED_BYTE )
{
fprintf ( stderr, "Insufficient memory to proceed. %u required exceeded %u.\n", allocMemory, PE_ALLOCATED_BYTE );
exit ( 1 );
}
sortBuffer_1 = ( SRAOccurrence * ) malloc ( occMaxCount * sizeof ( SRAOccurrence ) ) ;
sortBuffer_2 = ( SRAOccurrence * ) malloc ( occMaxCount * sizeof ( SRAOccurrence ) ) ;
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "%u positions in list 1.\n", occCount_1 );
printf ( "%u positions in list 2.\n", occCount_2 );
#endif
//Radix sort the lists
if ( occCount_1 > 1 )
{ PERadixSort ( occList_1, occCount_1, sortBuffer_1, sortBuffer_2, occList_1 ); }
if ( occCount_2 > 1 )
{ PERadixSort ( occList_2, occCount_2, sortBuffer_1, sortBuffer_2, occList_2 ); }
//Calling the core
PEMappingCore ( peInput, peOutput, occList_1, occCount_1, occList_2, occCount_2 );
peOutput->flag = PE_ALIGNMENT_COMPLETED;
free ( sortBuffer_1 );
free ( sortBuffer_2 );
}
/////////////////////////////////////////////////////
/*
Function PEIsPairEndMatch
This function takes in 2 occurrence and determind
if the two occurrences form a pair end mapping.
Assume occ_1 <= occ_2.
Input:
peInput - The input parameter for PE
occ_1 - The occurrence on left leg
occ_2 - The occurrence on right leg
Output:
1 if the two occurrences form a pair end mapping
0 otherwise
*/
/////////////////////////////////////////////////////
inline int PEIsPairEndMatch ( PEInput * peInput, SRAOccurrence * occ_1, SRAOccurrence * occ_2, unsigned int * insertion )
{
//For a valid paired-end reads,
// let the mean and the standard deviation of the insert size be "m" and "sd",
// the distance between the leftmost of the left-read alignment and the rightmost
// of the right-read alignment should fall between [m - 3 * sd, m + 3 * sd], and the
// strand of the left-read alignment is NOT the same as the strand of the right-read alignment.
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( "PEIsPairEndMatch is invoked.\n" );
printf ( " pos = %u \t strand = %d \n", occ_1->ambPosition, occ_1->strand );
printf ( " pos = %u \t strand = %d \n", occ_2->ambPosition, occ_2->strand );
#endif
int strandLeftLeg = peInput->strandLeftLeg;
int strandRightLeg = peInput->strandRightLeg;
int strandCheck = ( occ_1->strand == strandLeftLeg &&
occ_2->strand == strandRightLeg );
//positions are left aligned, therefore it requires correction
unsigned int ambPos_2 = occ_2->ambPosition + peInput->patternLength - 1;
//occ_2->ambPosition += peInput->patternLength - 1;
unsigned int gap = ambPos_2 - occ_1->ambPosition + 1;
( *insertion ) = gap;
#ifdef PE_DEBUG_PRINT_VISUAL_MATCHING
char DEBUG_sorted[2];
DEBUG_sorted[0] = 'X';
DEBUG_sorted[1] = ' ';
printf ( " %10llu <----- %10llu -----> %10llu [%d:%d] %c\n", occ_1->ambPosition, gap, ambPos_2, peInput->insertLbound, peInput->insertUbound, DEBUG_sorted[ ( occ_1->ambPosition <= ambPos_2 ) & 1] );
#endif
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( " lbound = %u \t gap = %u \t ubound = %u\n", peInput->insertLbound, gap, peInput->insertUbound );
#endif
return ( ( peInput->insertLbound <= gap ) && ( gap <= peInput->insertUbound ) && strandCheck );
}
inline int PEIsPairOutOfRange ( PEInput * peInput, SRAOccurrence * occ_1, SRAOccurrence * occ_2 )
{
if ( occ_1->strand == occ_2->strand ) { return 0; }
unsigned int occ_1_pos = occ_1->ambPosition + peInput->insertUbound;
unsigned int occ_2_pos = occ_2->ambPosition + peInput->patternLength - 1;
return occ_1_pos < occ_2_pos;
}
void PEReportPairResult ( PEOutput * peOutput, SRAOccurrence * occ_1, SRAOccurrence * occ_2, unsigned int insertion )
{
#ifdef PE_DEBUG_PRINT_MATCHING_PROGRESS
printf ( " Pair-end alignment is found.\n" );
#endif
PEPairList * pePairList = peOutput->tail;
if ( pePairList->pairsCount == PE_MAX_BUCKET_SIZE )
{
if ( pePairList->next == NULL )
{
pePairList->next = PEPairListConstruct ();
}
pePairList = pePairList->next;
peOutput->tail = pePairList;
}
PEPairs * pePair = & ( pePairList->pairs[pePairList->pairsCount] );
pePair->algnmt_1 = occ_1->ambPosition;
pePair->strand_1 = occ_1->strand;
pePair->mismatch_1 = occ_1->mismatchCount;
pePair->algnmt_2 = occ_2->ambPosition;
pePair->strand_2 = occ_2->strand;
pePair->mismatch_2 = occ_2->mismatchCount;
pePair->insertion = insertion;
pePair->totalMismatchCount = occ_1->mismatchCount + occ_2->mismatchCount;
pePairList->pairsCount++;
}
/*
unsigned int PEMappingToDisk(PEInput * peInput, PEPairList * pePairList) {
}
*/
/////////////////////////////////////////////////////
// Constructor and Destructor
/////////////////////////////////////////////////////
PEInput * PEInputConstruct ( BWT * bwt, HSP * hsp )
{
PEInput * peInput = ( PEInput * ) malloc ( sizeof ( PEInput ) );
peInput->bwt = bwt;
peInput->hsp = hsp;
peInput->patternLength = PE_NON_INPUT_VALUE;
peInput->strandLeftLeg = PE_NON_INPUT_VALUE;
peInput->strandRightLeg = PE_NON_INPUT_VALUE;
peInput->insertSizeMean = PE_NON_INPUT_VALUE;
peInput->insertSizeStdDev = PE_NON_INPUT_VALUE;
peInput->insertLbound = PE_NON_INPUT_VALUE;
peInput->insertUbound = PE_NON_INPUT_VALUE;
peInput->OutputType = PE_REPORT_ALL;
return peInput;
}
void PEInputFree ( PEInput * peInput )
{
free ( peInput );
}
void PEPairListInitialise ( PEPairList * pePairList )
{
if ( pePairList == NULL ) { return; }
PEPairListInitialise ( pePairList->next );
pePairList->pairsCount = 0;
}
void PEOutputInitialise ( PEOutput * peOutput )
{
PEPairListInitialise ( peOutput->root );
peOutput->tail = peOutput->root;
peOutput->flag = PE_ALIGNMENT_INITIALISED;
}
PEPairList * PEPairListConstruct ()
{
PEPairList * pePairList = ( PEPairList * ) malloc ( sizeof ( PEPairList ) );
pePairList->pairsCount = 0;
pePairList->next = NULL;
return pePairList;
}
PEOutput * PEOutputConstruct ()
{
PEOutput * peOutput = ( PEOutput * ) malloc ( sizeof ( PEOutput ) );
peOutput->root = PEPairListConstruct ();
peOutput->tail = peOutput->root;
return peOutput;
}
void PEPairListFree ( PEPairList * pePairList )
{
if ( pePairList == NULL ) { return; }
PEPairListFree ( pePairList->next );
free ( pePairList );
}
void PEOutputFree ( PEOutput * peOutput )
{
PEPairListFree ( peOutput->root );
free ( peOutput );
}
/////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////
void PEPrintOccurrenceList ( const char * listName, SRAOccurrence * list, unsigned int length )
{
unsigned int i;
printf ( "List of occurrences %s =\n", listName );
for ( i = 0; i < length; i++ )
{
printf ( "%u ", list[i].ambPosition );
if ( ( i + 1 ) % 8 == 0 ) {printf ( "\n" );}
}
printf ( "\n" );
}
void PEPrintPEPairList ( PEPairList * pairList, int maxOutput )
{
int i;
int j = 0;
char strand[3];
strand[1] = '+';
strand[2] = '-';
while ( pairList != NULL && pairList->pairsCount > 0 )
{
for ( i = 0; i < pairList->pairsCount; i++ )
{
PEPairs * pePair = & ( pairList->pairs[i] );
printf ( "algnmt_1 = %u(%c) algnmt_2 = %u(%c)\n",
pePair->algnmt_1, strand[pePair->strand_1],
pePair->algnmt_2, strand[pePair->strand_2] );
j++;
if ( j >= maxOutput && maxOutput >= 0 ) { return; }
}
pairList = pairList->next;
}
}
void PEPrintPEOutput ( PEOutput * peOutput )
{
PEPrintPEPairList ( peOutput->root, -1 );
}
void PEPrintPEOutputWithLimit ( PEOutput * peOutput, int maxOutput )
{
PEPrintPEPairList ( peOutput->root, maxOutput );
}
unsigned int PECountPEPairList ( PEPairList * pairList )
{
unsigned int count = 0;
while ( pairList != NULL && pairList->pairsCount > 0 )
{
count += pairList->pairsCount;
pairList = pairList->next;
}
return count;
}
unsigned int PECountPEOutput ( PEOutput * peOutput )
{
return PECountPEPairList ( peOutput->root );
}
unsigned int PEStatsPEPairList ( PEPairList * pairList, PEPairs ** optimal, PEPairs ** suboptimal, unsigned int * mismatchStats )
{
unsigned int count = 0;
unsigned int i;
PEPairs * iOptimal = NULL;
PEPairs * iSubOptimal = NULL;
uint8_t iOptimal_MismatchCount = 255;
uint8_t iOptimal_MismatchDiff = 255;
uint8_t iSubOptimal_MismatchCount = 255;
uint8_t iSubOptimal_MismatchDiff = 255;
while ( pairList != NULL && pairList->pairsCount > 0 )
{
count += pairList->pairsCount;
for ( i = 0; i < pairList->pairsCount; i++ )
{
PEPairs * pePair = & ( pairList->pairs[i] );
int numMismatchOnPE = pePair->totalMismatchCount;
mismatchStats[numMismatchOnPE]++;
int diffMismatch = pePair->mismatch_1 - pePair->mismatch_2;
if ( pePair->mismatch_2 > pePair->mismatch_1 )
{
diffMismatch = -diffMismatch;
}
if ( numMismatchOnPE < iOptimal_MismatchCount )
{
iSubOptimal_MismatchCount = iOptimal_MismatchCount;
iSubOptimal_MismatchDiff = iOptimal_MismatchDiff;
iSubOptimal = iOptimal;
iOptimal = pePair;
iOptimal_MismatchCount = numMismatchOnPE;
iOptimal_MismatchDiff = diffMismatch;
}
else if ( numMismatchOnPE == iOptimal_MismatchCount &&
diffMismatch < iOptimal_MismatchDiff )
{
iOptimal = pePair;
iOptimal_MismatchCount = numMismatchOnPE;
iOptimal_MismatchDiff = diffMismatch;
}
}
pairList = pairList->next;
}
( *optimal ) = iOptimal;
( *suboptimal ) = iSubOptimal;
return count;
}
unsigned int PEStatsPEOutput ( PEOutput * peOutput, PEPairs ** optimal, PEPairs ** suboptimal, unsigned int * mismatchStats )
{
return PEStatsPEPairList ( peOutput->root, optimal, suboptimal, mismatchStats );
}
// Construct the structure ReadInputForDP for each CPU thread
ReadInputForDP * constructReadInputForDP ( int cpuNum )
{
ReadInputForDP * readInput;
readInput = ( ReadInputForDP * ) malloc ( sizeof ( ReadInputForDP ) );
readInput->readNum = 0;
unsigned int initial_size_sa_list = INITIAL_SIZE_SA_LIST_FOR_DP / cpuNum;
unsigned int initial_size_occ_list = INITIAL_SIZE_OCC_LIST_FOR_DP / cpuNum;
readInput->sa_list = ( PESRAAlignmentResult * ) malloc ( initial_size_sa_list * sizeof ( PESRAAlignmentResult ) );
readInput->occ_list = ( SRAOccurrence * ) malloc ( initial_size_occ_list * sizeof ( SRAOccurrence ) );
readInput->saRangeTotalNum = 0;
readInput->occTotalNum = 0;
readInput->saListSize = initial_size_sa_list;
readInput->occListSize = initial_size_occ_list;
readInput->lastReadID = 0xFFFFFFFF;
return readInput;
}
// Reset the structure ReadInputForDP
void resetReadInputForDP ( ReadInputForDP * readInput )
{
readInput->readNum = 0;
readInput->saRangeTotalNum = 0;
readInput->occTotalNum = 0;
}
// Release the memory for the structure ReadInputForDP
void freeReadInputForDP ( ReadInputForDP * readInput )
{
free ( readInput->sa_list );
free ( readInput->occ_list );
free ( readInput );
}
// To add the alignment results of a read to ReadInputForDP
void addToReadInputForDP ( ReadInputForDP * readInput, unsigned int readid, PESRAAlignmentResult * saList, unsigned int saNum,
SRAOccurrence * occList, unsigned int occNum )
{
if ( saNum > 0 )
{
if ( readInput->saRangeTotalNum + saNum > readInput->saListSize )
{
// enlarge the arrays "sa_list" by at least double
unsigned int new_size = readInput->saListSize * 2;
while ( readInput->saRangeTotalNum + saNum > new_size )
{
new_size *= 2;
}
PESRAAlignmentResult * new_sa_list = ( PESRAAlignmentResult * ) malloc ( sizeof ( PESRAAlignmentResult ) * new_size );
memcpy ( new_sa_list, readInput->sa_list, sizeof ( PESRAAlignmentResult ) *readInput->saRangeTotalNum );
free ( readInput->sa_list );
readInput->sa_list = new_sa_list;
readInput->saListSize = new_size;
}
// PESRAAlignmentResult* saListPtr = readInput->sa_list+readInput->saRangeTotalNum;
// memcpy(saListPtr,saList,sizeof(PESRAAlignmentResult)*saNum);
for ( unsigned int i = 0; i < saNum; i++ )
{
readInput->sa_list[i + readInput->saRangeTotalNum] = saList[i];
readInput->sa_list[i + readInput->saRangeTotalNum].readID = readid;
}
readInput->saRangeTotalNum += saNum;
}
if ( occNum > 0 )
{
if ( readInput->occTotalNum + occNum > readInput->occListSize )
{
// enlarge the arrays "occ_list" by at least double
unsigned int new_size = readInput->occListSize * 2;
while ( readInput->occTotalNum + occNum > new_size )
{
new_size *= 2;
}
SRAOccurrence * new_occ_list = ( SRAOccurrence * ) malloc ( sizeof ( SRAOccurrence ) * new_size );
memcpy ( new_occ_list, readInput->occ_list, sizeof ( SRAOccurrence ) *readInput->occTotalNum );
free ( readInput->occ_list );
readInput->occ_list = new_occ_list;
readInput->occListSize = new_size;
}
// SRAOccurrence* occListPtr = readInput->occ_list+readInput->occTotalNum;
// memcpy(occListPtr,occList,sizeof(SRAOccurrence)*occNum);
for ( unsigned int i = 0; i < occNum; i++ )
{
readInput->occ_list[i + readInput->occTotalNum] = occList[i];
readInput->occ_list[i + readInput->occTotalNum].readID = readid;
}
readInput->occTotalNum += occNum;
}
// if (saNum > 0 || occNum > 0)
unsigned int evenid = readid / 2 * 2;
if ( evenid != readInput->lastReadID )
{
readInput->readNum++;
readInput->lastReadID = evenid;
}
}
DynamicUint8Array * DynamicUint8ArrayConstruct ()
{
DynamicUint8Array * newuint8Array = ( DynamicUint8Array * ) malloc ( sizeof ( DynamicUint8Array ) );
newuint8Array->charStr = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * INITIAL_SIZE_CHAR_ARRAY );
newuint8Array->charStr[0] = '\0';
newuint8Array->size = INITIAL_SIZE_CHAR_ARRAY;
newuint8Array->length = 0;
return newuint8Array;
}
void DynamicUint8ArrayFree ( DynamicUint8Array * uint8Array )
{
free ( uint8Array->charStr );
free ( uint8Array );
}
void DynamicUint8ArrayReset ( DynamicUint8Array * uint8Array )
{
uint8Array->length = 0;
uint8Array->charStr[0] = '\0';
}
void appendStringToUint8Array ( DynamicUint8Array * uint8Array, char * charString, int len )
{
while ( uint8Array->length + len >= uint8Array->size )
{
// double the size
uint8_t * newArray = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * uint8Array->size * 2 );
memcpy ( newArray, uint8Array->charStr, uint8Array->length );
free ( uint8Array->charStr );
uint8Array->charStr = newArray;
uint8Array->size = uint8Array->size * 2;
}
memcpy ( uint8Array->charStr + uint8Array->length, charString, len );
uint8Array->length += len;
uint8Array->charStr[uint8Array->length] = '\0';
}
// ================================================
// To hold a set of single-end alignment results
// ================================================
AllHits * constructAllHits ()
{
AllHits * newAllHits = ( AllHits * ) malloc ( sizeof ( AllHits ) );
newAllHits->hitArray = ( Algnmt * ) malloc ( sizeof ( Algnmt ) * SIZE_1_M );
newAllHits->hitNum = 0;
newAllHits->hitArrayAvailSize = SIZE_1_M;
newAllHits->readPtrArray = ( ReadPtr * ) malloc ( sizeof ( ReadPtr ) * SIZE_1_M );
newAllHits->readNum = 0;