-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute.cpp
3174 lines (2956 loc) · 150 KB
/
compute.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <algorithm>
#include <vector>
#include <tuple>
#include <gmp.h>
#include <chrono>
#include <stdint.h>
#include <omp.h>
void poly_mult_update(mpz_t* p1, mpz_t* p2, uint32_t max_degree) {
// Computes p1 = p1 * p2
mpz_t* product = new mpz_t[1+max_degree];
mpz_t term;
mpz_init(term);
mpz_t tempProd;
mpz_init(tempProd);
for (size_t i = 0; i <= max_degree; i++) {
mpz_init(product[i]); // initialize product[i] to 0
for (size_t j = 0; j <= i; j++) {
mpz_set (tempProd, product[i]);
mpz_mul(term, p1[j], p2[i-j]); // multiply coefficients
mpz_add(product[i], tempProd, term); // add to product[i]
}
}
mpz_clear(term);
mpz_clear(tempProd);
for (size_t i = 0; i <= max_degree; i++) {
mpz_set (p1[i], product[i]);
mpz_clear(product[i]);
}
delete product;
}
void poly_mult(mpz_t* p1, mpz_t* p2, mpz_t* product, uint32_t max_degree) {
/*
Expects product to be additive identity
Computes product = p1 * p2
*/
mpz_t term;
mpz_init(term);
mpz_t tempProd;
mpz_init(tempProd);
for (size_t i = 0; i <= max_degree; i++) {
for (size_t j = 0; j <= i; j++) {
mpz_set (tempProd, product[i]);
mpz_mul(term, p1[j], p2[i-j]); // multiply coefficients
mpz_add(product[i], tempProd, term); // add to product[i]
}
}
mpz_clear(term);
mpz_clear(tempProd);
}
int main(int argc, char *argv[]){
int option;
int32_t precision = 128;
int32_t genomeSize = 1000;
int32_t readCountHap1 = -1;
int32_t readCountHap2 = -1;
int32_t hetLocus = 200;
int32_t threadCount = 32;
std::string readDistFileHap1;
std::string readDistFileHap2;
std::cout << "inputs: Genome Size, Read count, Het Locus, Read Distribution File, Precision" << std::endl;
while ((option = getopt(argc, argv, "g:R:r:h:D:d:p:t:")) != -1) {
switch (option){
case 'g':
genomeSize = atoi(optarg);
std::cout << "Genome size " << genomeSize << std::endl;
break;
case 'R':
readCountHap1 = atoi(optarg);
std::cout << "Read Count Haplotype 1 " << readCountHap1 << std::endl;
break;
case 'r':
readCountHap2 = atoi(optarg);
std::cout << "Read Count Haplotype 2 " << readCountHap2 << std::endl;
break;
case 'h':
hetLocus = atoi(optarg);
std::cout << "Heterozygous Locus " << hetLocus << std::endl;
break;
case 'D':
readDistFileHap1 = optarg;
std::cout << "Hap 1 Distribution in " << readDistFileHap1 << std::endl;
break;
case 'd':
readDistFileHap2 = optarg;
std::cout << "Hap 2 Distribution in " << readDistFileHap2 << std::endl;
break;
case 'p':
precision = atoi(optarg);
std::cout << "Precision " << precision << std::endl;
break;
case 't':
threadCount = atoi(optarg);
std::cout << "Number of threads " << threadCount << std::endl;
break;
default:
std::cout << "One of the inputs is invalid." << std::endl;
std::cerr << "Usage: " << argv[0] << "-g genomeSize -R readCountHap1 -r readCountHap2 -h heterozygousLocus -D readDistributionFileHap1 -d readDistributionFileHap2 -p precision -t numThreads" << std::endl;
return 1;
}
}
if (precision <= 0
|| genomeSize <= 0
|| readCountHap1 <= 0
|| readCountHap2 <= 0
|| hetLocus <= 0
|| hetLocus >= genomeSize
|| readDistFileHap1.empty()
|| readDistFileHap2.empty()
|| threadCount <= 0) {
std::cout << "One of the inputs is invalid." << std::endl;
std::cerr << "Error: Missing required options." << std::endl;
std::cerr << "Usage: " << argv[0] << "-g genomeSize -R readCountHap1 -r readCountHap2 -h heterozygousLocus -D readDistributionFileHap1 -d readDistributionFileHap2 -p precision -t threadCount" << std::endl;
return 1;
}
// Setting OpenMP parameters
omp_set_dynamic (0);
omp_set_num_threads (threadCount);
// Setting precision
mpf_set_default_prec(precision);
std::cout << "Set precision to " << precision << " bits" << std::endl;
/*
readDistFile contains lines of the form:
readLength readCount
It is assumed that each read length appears exactly once.
Read lengths appear in increasing order.
*/
typedef std::tuple<int32_t, int32_t> distElement;
std::vector<distElement> readLengthDistHap1;
std::vector<distElement> readLengthDistHap2;
std::ifstream readLengthFileHap1(readDistFileHap1);
if (!readLengthFileHap1) {
std::cout << "Error concerning read length distribution for Haplotype 1." << std::endl;
std::cerr << "Can not open read distribution file for Haplotype 1" << std::endl;
return 1;
}
std::ifstream readLengthFileHap2(readDistFileHap2);
if (!readLengthFileHap2) {
std::cout << "Error concerning read length distribution for Haplotype 2." << std::endl;
std::cerr << "Can not open read distribution file for Haplotype 2" << std::endl;
return 1;
}
int32_t tempReadLength = -1;
int32_t tempReadCount = -1;
int32_t maxReadLengthHap1 = -1;
int32_t maxReadLengthHap2 = -1;
std::string line;
while (std::getline(readLengthFileHap1, line)) {
std::istringstream iss(line);
if (iss >> tempReadLength >> tempReadCount){
if (tempReadLength <= 0) {
std::cout << "Read lengths can not be negative. Please vet the input file for haplotype 1." << std::endl;
std::cerr << "Invalid read length " << tempReadLength << std::endl;
return 1;
}
if (tempReadCount <= 0) {
std::cout << "Read counts can not be negative. Please vet the input file for haplotype 1." << std::endl;
std::cerr << "Invalid read count " << tempReadCount << std::endl;
return 1;
}
std::cout << tempReadLength << " " << tempReadCount << std::endl;
if (tempReadLength > maxReadLengthHap1)
maxReadLengthHap1 = tempReadLength;
std::cout << "Max Read Length Haplotype 1 " << maxReadLengthHap1 << std::endl;
readLengthDistHap1.emplace_back (tempReadLength, tempReadCount);
}
}
readLengthFileHap1.close();
while (std::getline(readLengthFileHap2, line)) {
std::istringstream iss(line);
if (iss >> tempReadLength >> tempReadCount){
if (tempReadLength <= 0) {
std::cout << "Read lengths can not be negative. Please vet the input file for haplotype 1." << std::endl;
std::cerr << "Invalid read length " << tempReadLength << std::endl;
return 1;
}
if (tempReadCount <= 0) {
std::cout << "Read counts can not be negative. Please vet the input file for haplotype 1." << std::endl;
std::cerr << "Invalid read count " << tempReadCount << std::endl;
return 1;
}
std::cout << tempReadLength << " " << tempReadCount << std::endl;
if (tempReadLength > maxReadLengthHap2)
maxReadLengthHap2 = tempReadLength;
std::cout << "Max Read Length Haplotype 2 " << maxReadLengthHap2 << std::endl;
readLengthDistHap2.emplace_back (tempReadLength, tempReadCount);
}
}
readLengthFileHap2.close();
int32_t readDistHap1[1+maxReadLengthHap1] = {};
for (const auto& tuple1 : readLengthDistHap1){
int32_t getReadLength = std::get<0>(tuple1);
int32_t getReadCount = std::get<1>(tuple1);
readDistHap1 [getReadLength] = getReadCount;
}
int32_t readDistHap2[1+maxReadLengthHap2] = {};
for (const auto& tuple2 : readLengthDistHap2) {
int32_t getReadLength = std::get<0>(tuple2);
int32_t getReadCount = std::get<1>(tuple2);
readDistHap2 [getReadLength] = getReadCount;
}
int32_t distinctReadLengthsHap1 = 0;
for (int32_t i = 0; i <= maxReadLengthHap1; i++) {
if (readDistHap1[i] > 0)
distinctReadLengthsHap1 += 1;
}
int32_t distinctReadLengthsHap2 = 0;
for (int32_t i = 0; i <= maxReadLengthHap2; i++) {
if (readDistHap2[i] > 0)
distinctReadLengthsHap2 += 1;
}
std::cout << "Distinct read lengths on haplotype 1 " << distinctReadLengthsHap1 << std::endl;
std::cout << "Distinct read lengths on haplotype 2 " << distinctReadLengthsHap2 << std::endl;
int32_t validReadLengthsHap1[distinctReadLengthsHap1] = {};
for (int32_t i = 0, j = 0; i <= maxReadLengthHap1; i++) {
if (readDistHap1[i] == 0)
continue;
validReadLengthsHap1[j] = i;
j += 1;
}
int32_t validReadLengthsHap2[distinctReadLengthsHap2] = {};
for (int32_t i = 0, j = 0; i <= maxReadLengthHap2; i++) {
if (readDistHap2[i] == 0)
continue;
validReadLengthsHap2[j] = i;
j += 1;
}
// DEBUG
std::cout << "Valid Read Lengths Hap 1";
for (int32_t i = 0; i < distinctReadLengthsHap1; i++)
std::cout << validReadLengthsHap1[i] << " ";
std::cout << std::endl;
std::cout << "Valid Read Lengths Hap 2";
for (int32_t i = 0; i < distinctReadLengthsHap2; i++)
std::cout << validReadLengthsHap2[i] << " ";
std::cout << std::endl;
// END DEBUG
/*
We compute total number of read sequencing outputs in the following manner:
On hap1 reads covering hetLocus have to have generating functions 1...
On hap2 reads covering hetLocus have to have generating functions 1...
*/
mpz_t cVar;
mpz_init (cVar);
// These permit 0 or more on hap1 covering hetLocus
mpz_t* genFuncHap1[distinctReadLengthsHap1][1+genomeSize];
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
genFuncHap1[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
genFuncHap1[i][j] = new mpz_t[size];
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_init (genFuncHap1[i][j][k]);
mpz_set_ui (genFuncHap1[i][j][k], 1);
}
}
}
// These permit 0 or more on hap2 covering hetLocus
mpz_t* genFuncHap2[distinctReadLengthsHap2][1+genomeSize];
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
genFuncHap2[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
genFuncHap2[i][j] = new mpz_t[size];
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_init (genFuncHap2[i][j][k]);
mpz_set_ui (genFuncHap2[i][j][k], 1);
}
}
}
std::cout << "Initialised generating functions" << std::endl;
/*
generatingFunctions contains all the polynomials for reads of length
validReadLengths[i] stopping at position j.
For a fixed i, all polynomials will be multiplied.
The result is stored in an array of polynomials "products" indexed by i.
*/
mpz_t* prodHap1[distinctReadLengthsHap1];
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
prodHap1[i] = new mpz_t[size];
// j goes from 0 upto n_i
for (int32_t j = 0; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_init (prodHap1[i][j]);
}
mpz_set_ui (prodHap1[i][0], 1);
}
mpz_t* prodHap2[distinctReadLengthsHap2];
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
prodHap2[i] = new mpz_t[size];
// j goes from 0 upto n_i
for (int32_t j = 0; j <= readDistHap2[validReadLengthsHap2[i]]; j++) {
mpz_init (prodHap2[i][j]);
}
mpz_set_ui (prodHap2[i][0], 1);
}
std::cout << "Initialised products" << std::endl;
/*
Multiplication occurs below
PARALLELIZED
*/
auto start = std::chrono::high_resolution_clock::now();
#pragma omp parallel for
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
poly_mult_update (
prodHap1[i],
genFuncHap1[i][j],
readDistHap1[validReadLengthsHap1[i]]
);
}
}
#pragma omp parallel for
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
poly_mult_update (
prodHap2[i],
genFuncHap2[i][j],
readDistHap2[validReadLengthsHap2[i]]
);
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds> (
end
- start
);
std::cout << "All products computed in " << duration.count() << " ms" << std::endl;
// Extract total number of permutations for a haplotype
mpz_t totalReadSeqOutputsHap1;
mpz_init (totalReadSeqOutputsHap1);
mpz_set_ui (totalReadSeqOutputsHap1, 1);
mpz_t totalReadSeqOutputsHap2;
mpz_init (totalReadSeqOutputsHap2);
mpz_set_ui (totalReadSeqOutputsHap2, 1);
mpz_set_ui (cVar, 0);
start = std::chrono::high_resolution_clock::now();
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
mpz_set (cVar, totalReadSeqOutputsHap1);
mpz_mul (
totalReadSeqOutputsHap1,
cVar,
prodHap1[i][readDistHap1[validReadLengthsHap1[i]]]
);
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
mpz_set (cVar, totalReadSeqOutputsHap2);
mpz_mul (
totalReadSeqOutputsHap2,
cVar,
prodHap2[i][readDistHap2[validReadLengthsHap2[i]]]
);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds> (
end
- start
);
std::cout << "Total count computed in " << duration.count() << " ms" << std::endl;
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 0; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_clear (prodHap1[i][j]);
}
delete prodHap1[i];
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 0; j <= readDistHap2[validReadLengthsHap2[i]]; j++) {
mpz_clear (prodHap2[i][j]);
}
delete prodHap2[i];
}
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_clear (genFuncHap1[i][j][k]);
}
delete genFuncHap1[i][j];
}
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_clear (genFuncHap2[i][j][k]);
}
delete genFuncHap2[i][j];
}
}
// These are for zero on hap1
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
genFuncHap1[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
genFuncHap1[i][j] = new mpz_t[size];
if ( (hetLocus <= j) && (j <= ((hetLocus + maxReadLengthHap1) - 1)) ) {
if (validReadLengthsHap1[i] >= ((j - hetLocus) + 1)) {
mpz_init (genFuncHap1[i][j][0]);
mpz_set_ui (genFuncHap1[i][j][0], 1);
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_init (genFuncHap1[i][j][k]);
mpz_set_ui (genFuncHap1[i][j][k], 0);
}
}
else{
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_init (genFuncHap1[i][j][k]);
mpz_set_ui (genFuncHap1[i][j][k], 1);
}
}
}
else {
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_init (genFuncHap1[i][j][k]);
mpz_set_ui (genFuncHap1[i][j][k], 1);
}
}
}
}
// These are for 0 on hap2
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
genFuncHap2[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
genFuncHap2[i][j] = new mpz_t[size];
if ( (hetLocus <= j) && (j <= ((hetLocus + maxReadLengthHap2) - 1)) ) {
if (validReadLengthsHap2[i] >= ((j - hetLocus) + 1)) {
mpz_init (genFuncHap2[i][j][0]);
mpz_set_ui (genFuncHap2[i][j][0], 1);
for (int32_t k = 1; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_init (genFuncHap2[i][j][k]);
mpz_set_ui (genFuncHap2[i][j][k], 0);
}
}
else {
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_init (genFuncHap2[i][j][k]);
mpz_set_ui (genFuncHap2[i][j][k], 1);
}
}
}
else {
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_init (genFuncHap2[i][j][k]);
mpz_set_ui (genFuncHap2[i][j][k], 1);
}
}
}
}
std::cout << "Initialised generating functions" << std::endl;
/*
generatingFunctions contains all the polynomials for reads of length
validReadLengths[i] stopping at position j.
For a fixed i, all polynomials will be multiplied.
The result is stored in an array of polynomials "products" indexed by i.
*/
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
prodHap1[i] = new mpz_t[size];
// j goes from 0 upto n_i
for (int32_t j = 0; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_init (prodHap1[i][j]);
}
mpz_set_ui (prodHap1[i][0], 1);
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
prodHap2[i] = new mpz_t[size];
// j goes from 0 upto n_i
for (int32_t j = 0; j <= readDistHap2[validReadLengthsHap2[i]]; j++) {
mpz_init (prodHap2[i][j]);
}
mpz_set_ui (prodHap2[i][0], 1);
}
std::cout << "Initialised products" << std::endl;
/*
Multiplication occurs below
PARALLELIZED
*/
start = std::chrono::high_resolution_clock::now();
#pragma omp parallel for
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
poly_mult_update (
prodHap1[i],
genFuncHap1[i][j],
readDistHap1[validReadLengthsHap1[i]]
);
}
}
#pragma omp parallel for
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
poly_mult_update (
prodHap2[i],
genFuncHap2[i][j],
readDistHap2[validReadLengthsHap2[i]]
);
}
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds> (
end
- start
);
std::cout << "All products computed in " << duration.count() << " ms" << std::endl;
// Extract total number of permutations for a haplotype
mpz_t totalReadSeqOutputsZeroHap1;
mpz_init (totalReadSeqOutputsZeroHap1);
mpz_set_ui (totalReadSeqOutputsZeroHap1, 1);
mpz_t totalReadSeqOutputsZeroHap2;
mpz_init (totalReadSeqOutputsZeroHap2);
mpz_set_ui (totalReadSeqOutputsZeroHap2, 1);
mpz_set_ui (cVar, 0);
start = std::chrono::high_resolution_clock::now();
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
mpz_set (cVar, totalReadSeqOutputsZeroHap1);
mpz_mul (
totalReadSeqOutputsZeroHap1,
cVar,
prodHap1[i][readDistHap1[validReadLengthsHap1[i]]]
);
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
mpz_set (cVar, totalReadSeqOutputsZeroHap2);
mpz_mul (
totalReadSeqOutputsZeroHap2,
cVar,
prodHap2[i][readDistHap2[validReadLengthsHap2[i]]]
);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds> (
end
- start
);
std::cout << "Total count computed in " << duration.count() << " ms" << std::endl;
// char strP[256];
// mp_exp_t expP;
// mpz_get_str(strP, &expP, 10, 0, totalReadSeqOutputs);
mpz_t T11;
mpz_init (T11);
mpz_t T10;
mpz_init (T10);
mpz_t T01;
mpz_init (T01);
mpz_t T00;
mpz_init (T00);
mpz_mul (T11, totalReadSeqOutputsHap1, totalReadSeqOutputsHap2);
mpz_mul (T10, totalReadSeqOutputsHap1, totalReadSeqOutputsZeroHap2);
mpz_mul (T01, totalReadSeqOutputsZeroHap1, totalReadSeqOutputsHap2);
mpz_mul (T00, totalReadSeqOutputsZeroHap1, totalReadSeqOutputsZeroHap2);
std::cout << "T11 " << T11 << std::endl;
std::cout << "T10 " << T10 << std::endl;
std::cout << "T01 " << T01 << std::endl;
std::cout << "T00 " << T00 << std::endl;
// Deleting genFuncHap1, genFuncHap2, prodHap1, and prodHap2
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 0; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_clear (prodHap1[i][j]);
}
delete prodHap1[i];
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 0; j <= readDistHap2[validReadLengthsHap2[i]]; j++) {
mpz_clear (prodHap2[i][j]);
}
delete prodHap2[i];
}
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_clear (genFuncHap1[i][j][k]);
}
delete genFuncHap1[i][j];
}
}
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_clear (genFuncHap2[i][j][k]);
}
delete genFuncHap2[i][j];
}
}
/*
Computing number of permutations for a fixed (x1, x2). Multiple steps here:
1. Count permutations on haplotype 1 where we only condition on classes 2
and 4 : N1
2. Count permutations on haplotype 1 where we condition on classes 2 and
4, and force class 1 to have no reads : N11
3. Count permutations on haplotype 1 where we condition on classes 2 and
4, and force class 3 to have no reads : N13
4. Count permutations on haplotype 1 where we condition on classes 2 and
4, and force classes 1 and 3 to have no reads : N113
5. Count permutations on haplotype 2 where we only condition on classes 2
and 4 : N2
6. Count permutations on haplotype 2 where we condition on classes 2 and
4, and force class 1 to have no reads : N21
7. Count permutations on haplotype 2 where we condition on classes 2 and
4, and force class 3 to have no reads : N23
8. Count permutations on haplotype 2 where we condition on classes 2 and
4, and force classes 1 and 3 to have no reads : N213
From this point, the code is parllelized again for loop x1.
Below is a list of variables defined inside the loop x1.
*/
mpz_t* aggregateCount = new mpz_t[threadCount];
for (int32_t i = 0; i < threadCount; i++) {
mpz_init (aggregateCount[i]);
}
mpz_t* aggregateError = new mpz_t[threadCount];
for (int32_t i = 0; i < threadCount; i++) {
mpz_init (aggregateError[i]);
}
/*
For Haplotype 1:
* x1 lies in [hetLocus + 1, (hetLocus + maxReadLengthHap1) - 1]
1. Class 1 Reads stop at x1 and cover hetLocus.
Thus, their lengths are at least (x1 - hetLocus) + 1.
Test condition: validReadLengths[i] >= (x1 - hetLocus) + 1.
At least one of these reads must exist.
2. Class 2 Reads stop in [hetLocus, x1 - 1].
Fixing the stop position as j, their lengths are at least (j - hetLocus) + 1.
Test condition: validReadLengths[i] >= (j - hetLocus) + 1.
3. Class 3 Reads stop in [hetLocus+1, x1 - 1] and also start in [hetLocus+1, x1 - 1].
Of these, the reads which start and stop in [hetLocus+1, x2] don't cause coverage gaps.
Reads which start and stop in [x2+1, x1-1] may cause coverage gaps, but doesn't fit our definition.
Reads which start in [hetLocus+1, x2] and stop in [x2+1, x1-1] cause assembly gaps.
At least one of the reads which cause assembly gaps must exist on either haplotype.
4. Class 4 Reads stop in [1, hetLocus - 1] or [x1+1, genomeSize].
They must start in either [1, hetLocus - 1] or [x2+1, genomeSize].
For Haplotype 2:
* x2 lies in [hetLocus, (hetLocus + maxReadLengthHap2) - 1].
* If the right limit is x1 - 1, nothing changes in the code.
* If the right limit is hetLocus + maxReadLengthHap2 - 1, then every occurrence of x1 - 1 must be replaced.
1. Class 1 Reads stop at x2 and cover hetLocus.
Thus, their lengths are at least (x2 - hetLocus) + 1
Test condition: validReadLengths[i] >= (x2 - hetLocus) + 1.
At least one of these reads must exist.
2. Class 2 Reads stop in [hetLocus, x2 - 1].
Fixing the stop position as j, their lengths are at least (j - hetLocus) + 1.
Test condition: validReadLengths[i] >= (j - hetLocus) + 1.
3. Class 3 Reads stop in [hetLocus+1, x1 - 1] and also start in [hetLocus+1, x1 - 1].
Of these, the reads which start and stop in [hetLocus+1, x2] don't cause coverage gaps.
Reads which start and stop in [x2+1, x1-1] cause coverage gaps but don't fit our definition.
Reads which start in [hetLocus+1, x2] and stop in [x2+1, x1-1] case assembly gaps.
At least one of the reads which cause assembly gaps must exist on either haplotype.
4. Class 4 Reads stop in [1, hetLocus - 1] or [x1+1, genomeSize].
They must start in either [1, hetLocus - 1] or [x2+1, genomeSize].
If x2 == x1:
Do nothing. Continue.
If x2 > x1:
Reverse the behaviours of haplotype 1 and haplotype 2.
Initialise polynomials for haplotype 1 with the conditions of haplotype 2 and vice versa.
*/
#pragma omp parallel for
for (int32_t x1 = hetLocus; x1 <= ((hetLocus + maxReadLengthHap1) - 1); x1++) {
// Define and initialise generatingFunctions
mpz_t* generatingFunctionsHap1[distinctReadLengthsHap1][1+genomeSize];
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
generatingFunctionsHap1[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
generatingFunctionsHap1[i][j] = new mpz_t[size];
for (int32_t k = 0; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_init (generatingFunctionsHap1[i][j][k]);
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
}
mpz_t* generatingFunctionsHap2[distinctReadLengthsHap2][1+genomeSize];
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
generatingFunctionsHap2[i][0] = NULL;
for (int32_t j = 1; j <= genomeSize; j++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
generatingFunctionsHap2[i][j] = new mpz_t[size];
for (int32_t k = 0; k <= readDistHap2[validReadLengthsHap2[i]]; k++) {
mpz_init (generatingFunctionsHap2[i][j][k]);
mpz_set_ui (generatingFunctionsHap2[i][j][k], 1);
}
}
}
// Define and initialise calcVar
mpz_t calcVar;
mpz_init (calcVar);
// Define and initialise products
mpz_t* productsHap1[distinctReadLengthsHap1];
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
int32_t size = 1 + readDistHap1[validReadLengthsHap1[i]];
productsHap1[i] = new mpz_t[size];
for (int32_t j = 0; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_init (productsHap1[i][j]);
}
mpz_set_ui (productsHap1[i][0], 1);
}
mpz_t* productsHap2[distinctReadLengthsHap2];
for (int32_t i = 0; i < distinctReadLengthsHap2; i++) {
int32_t size = 1 + readDistHap2[validReadLengthsHap2[i]];
productsHap2[i] = new mpz_t[size];
for (int32_t j = 0; j <= readDistHap2[validReadLengthsHap2[i]]; j++) {
mpz_init (productsHap2[i][j]);
}
mpz_set_ui (productsHap2[i][0], 1);
}
// Inner for loop x2
// int32_t limitHap2 = std::min((x1 - 1), ((hetLocus + maxReadLengthHap2) - 1));
for (int32_t x2 = hetLocus; x2 <= ((hetLocus + maxReadLengthHap2) - 1); x2++) {
if (x1 == x2){
continue;
}
mpz_t N1;
mpz_init (N1);
mpz_t N11;
mpz_init (N11);
mpz_t N13;
mpz_init (N13);
mpz_t N113;
mpz_init (N113);
mpz_t N2;
mpz_init (N2);
mpz_t N21;
mpz_init (N21);
mpz_t N23;
mpz_init (N23);
mpz_t N213;
mpz_init (N213);
if (x2 < x1) {
/*
Compute N1
j == x1 means the reads are in class 1, or class 3b which upon
deletion create a coverage gap.
(j >= hetLocus) && (j <= x2) accounts for some of the reads in
class 2, and some reads in class 3 which upon deletion do not
create a coverage gap.
(j >= x2 + 1) && (j <= (x1-1)) accounts for the remaining reads
in class 2, and those reads in class 3 which upon deletion
cause a coverage gap.
The rest are class 4.
*/
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
if (j == x1) {
// Group 1, 3b, 3c
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] >= ((x1 - hetLocus) + 1)) {
// Group 1 (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else if (((x1 - x2) < validReadLengthsHap1[i]) && (validReadLengthsHap1[i] < ((x1 - hetLocus) + 1))) {
// Group 3b (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
// Group 3c (valid) if 1 <= i <= (x1 - x2)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
}
else if (((x2 + 1) <= j) && (j <= (x1 - 1))) {
// Group 2, 3b, 3c
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] >= ((j - hetLocus) + 1)) {
// Group 2 (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else if (((j - x2) < validReadLengthsHap1[i]) && (validReadLengthsHap1[i] < ((j - hetLocus) + 1))) {
// Group 3b (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
// Group 3c (valid) if 1 <= i <= (j - x2)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
}
else if (((hetLocus + 1) < j) && (j <= x2)) {
// Group 2, 3a
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] >= ((j - hetLocus) + 1)) {
// Group 2 (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
// Group 3a (valid) if ((1 <= validReadLengthsHap1[i]) && (validReadLengthsHap1[i] < ((j - hetLocus) + 1)))
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
}
else if (j == hetLocus) {
// Group 2 (valid)
// Account for 1 <= i <= maxReadLengthHap1
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else if (j < hetLocus) {
// Group 4
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] <= ((genomeSize + j) - x2)) {
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 0);
}
}
}
else {
// Group 4 j > x1
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] <= (j - x2)) {
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 0);
}
}
}
}
}
// Product polynomials
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
mpz_set_ui (productsHap1[i][0], 1);
for (int32_t j = 1; j <= readDistHap1[validReadLengthsHap1[i]]; j++) {
mpz_set_ui (productsHap1[i][j], 0);
}
}
// Multiplication
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
poly_mult_update (
productsHap1[i],
generatingFunctionsHap1[i][j],
readDistHap1[validReadLengthsHap1[i]]
);
}
}
// Extract N1
mpz_set_ui (N1, 1);
mpz_set_ui (calcVar, 0);
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
mpz_set (calcVar, N1);
mpz_mul (
N1,
calcVar,
productsHap1[i][readDistHap1[validReadLengthsHap1[i]]]
);
}
/*
Compute N11
j == x1 means the reads are in class 1, or class 3 which upon
deletion create a coverage gap.
(j >= hetLocus) && (j <= x2) accounts for some of the reads in
class 2, and some reads in class 3 which upon deletion do not
create a coverage gap.
(j >= x2 + 1) && (j <= (x1-1)) accounts for the remaining reads
in class 2, and those reads in class 3 which upon deletion
cause a coverage gap.
The rest are class 4.
*/
for (int32_t i = 0; i < distinctReadLengthsHap1; i++) {
for (int32_t j = 1; j <= genomeSize; j++) {
if (j == x1) {
// Group 1, 3b, 3c
// Account for 1 <= i <= maxReadLengthHap1
if (validReadLengthsHap1[i] >= ((x1 - hetLocus) + 1)) {
// Group 1 (invalid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 0);
}
}
else if (((x1 - x2) < validReadLengthsHap1[i]) && (validReadLengthsHap1[i] < ((x1 - hetLocus) + 1))) {
// Group 3b (valid)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);
}
}
else {
// Group 3c (valid) if 1 <= i <= (x1 - x2)
for (int32_t k = 1; k <= readDistHap1[validReadLengthsHap1[i]]; k++) {
mpz_set_ui (generatingFunctionsHap1[i][j][k], 1);