-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostcal.cpp
1238 lines (1088 loc) · 41.7 KB
/
postcal.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 <vector>
#include <unordered_map>
#include <algorithm>
#include <set>
#include <iostream>
#include <armadillo>
#include <iomanip>
#include <vector>
#include <math.h>
#include "util.h"
#include "postcal.h"
#include <sys/mman.h>
#include <omp.h>
#include <ctime>
#include <chrono>
using namespace arma;
double PostCal::log_prior(vector<int> configure, int numCausal, int **causal_bool_per_study) {
if (num_of_studies > 2) {
cout << "This prior does not work for more than 2 studies yet\n";
exit(1);
}
double p_of_c = 0;
if ( sharing_param != 0 ) {
for ( int i = 0; i < numCausal; i++ ) {
if ( causal_bool_per_study[0][i] == causal_bool_per_study[1][i] ) {
if ( causal_bool_per_study[0][i] == 1 ) {
p_of_c += log(sharing_param);
//printf("shared causal\n");
} else {
cout << "This case in prior should not happen\n"; //this is the all zeros case
exit(1);
}
} else {
p_of_c += log((1-sharing_param)*0.5); //0.5 adjustment for 2 studies bc not shared causal has two possibilities
//printf("not shared causal\n");
}
}
}
//printf("partial prior is %f\n", p_of_c);
int num_1_in_either = 0;
for ( int i = 0; i < numCausal; i++ ) {
if (causal_bool_per_study[0][i] == 1 or causal_bool_per_study[1][i] == 1 ) {
p_of_c += log(gamma);
num_1_in_either += 1;
//printf("causal snp\n");
}
}
p_of_c += ( unionSnpCount - num_1_in_either ) * log(1 - gamma);
//printf("num not causal snp = %d\n", unionSnpCount - num_1_in_either);
//printf("prior is %f\n", p_of_c);
//printf("num of 1 in either is %d\n", num_1_in_either);
return p_of_c;
}
// calibrate for sample size imbalance
mat PostCal::construct_diagC(vector<int> configure, int numCausal, int **causal_idx_per_study, int **causal_bool_per_study) {
mat Identity_M = mat(num_of_studies, num_of_studies, fill::eye);
mat Matrix_of_sigmaG = mat(num_of_studies, num_of_studies);
int min_size = * std::min_element(sample_sizes.begin(), sample_sizes.end());
/*for (int i = 0; i < num_of_studies; i ++) {
for (int j = 0; j < num_of_studies; j ++) {
if (i == j) // diagonal: scaled variance
Matrix_of_sigmaG(i, j) = s_squared * (double(sample_sizes[i]) / min_size);
else // off-diagonal: covariance
Matrix_of_sigmaG(i, j) = s_squared * sqrt(long(sample_sizes[i]) * long(sample_sizes[j])) / min_size;
}
} */
//mat temp1 = t_squared * Identity_M + Matrix_of_sigmaG;
//mat temp1 = Matrix_of_sigmaG;
//mat temp2 = mat(snpCount, snpCount, fill::zeros);
//for(int i = 0; i < snpCount; i++) {
// if (configure[i] == 1)
// temp2(i, i) = 1;
//}
vec diagC_main_diag(totalSnpCount, fill::zeros);
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
if ( causal_bool_per_study[i][j] == 1 ) {
int offset_studyi = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+i, 0);
int loc_in_studyi = causal_idx_per_study[i][j];
diagC_main_diag[offset_studyi + loc_in_studyi] = s_squared * (double(sample_sizes[i]) / min_size) + t_squared;
}
}
}
mat diagC = diagmat(diagC_main_diag);
//printf("diagonal diagC\n");
//diagC.print(std::cout);
//mat diagC = kron(temp1, temp2);
//adjust off diagonals to have sigma^2 when commonly causal in two studies
for ( int s1 = 0; s1 < num_of_studies; s1++ ) {
for ( int s2 = 0; s2 < num_of_studies; s2++ ) {
for ( int j = 0; j < numCausal; j++ ) {
if ( s1 == s2 ) { continue; } //already took care of diagonals
if (causal_bool_per_study[s1][j] == causal_bool_per_study[s2][j] && causal_bool_per_study[s1][j] == 1) { //causal in both
int study1_offset = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+s1, 0);
int study2_offset = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+s2, 0);
int loc_in_study1 = causal_idx_per_study[s1][j];
int loc_in_study2 = causal_idx_per_study[s2][j];
int s1_idx = study1_offset + loc_in_study1;
int s2_idx = study2_offset + loc_in_study2;
diagC(s1_idx, s2_idx) = s_squared * sqrt(long(sample_sizes[s1]) * long(sample_sizes[s2])) / min_size;
diagC(s2_idx, s1_idx) = s_squared * sqrt(long(sample_sizes[s1]) * long(sample_sizes[s2])) / min_size;
}
}
}
}
//printf("diagC again\n");
//diagC.print(std::cout);
return diagC;
}
double PostCal::likelihood(vector<int> configure, vector<double> * stat, double sigma_g_squared, mat sigmaC) {
//int causalCount = 0;
int totalCausalCount = 0;
double matDet = 0;
double res = 0;
for(int i = 0; i < totalSnpCount; i++) {
totalCausalCount += configure[i];
}
if (totalCausalCount == 0) {
cout << "This should have been taken care of in total likelihood function\n";
exit(1);
}
/*if(totalCausalCount == 0){ //TODO how to handle this case for multiple causal vectors
mat tmpResultMatrixNM = statMatrixtTran * invSigmaMatrix;
mat tmpResultMatrixNN = tmpResultMatrixNM * statMatrix;
res = tmpResultMatrixNN(0,0);
matDet = sigmaDet;
return (-res/2-sqrt(abs(matDet)));
}*/
//mat sigmaC = construct_diagC(configure);
int index_C = 0;
mat sigmaMatrixTran = sigmaMatrix.t();
// U is kn by mn matrix of columns corresponding to causal SNP in sigmacC
// In unequal sample size studies, U is adjusted for the sample sizes
//mat U(causalCount * num_of_studies, snpCount * num_of_studies, fill::zeros);
mat U(totalCausalCount, totalSnpCount, fill::zeros);
for (int i = 0; i < totalSnpCount; i++) {
if (configure[i] == 1) {
for (int j = 0; j < totalSnpCount; j++) {
U(index_C, j) = sigmaC(i, j);
}
index_C ++;
}
}
index_C = 0;
// V is mn by kn matrix of rows corresponding to causal SNP in sigma
// In unequal sample size studies, V does not change
//mat V(causalCount * num_of_studies, snpCount * num_of_studies, fill::zeros);
mat V(totalCausalCount, totalSnpCount, fill::zeros);
for (int i = 0; i < totalSnpCount; i++) {
if (configure[i] == 1) {
for (int j = 0; j < totalSnpCount; j++) {
V(index_C, j) = sigmaMatrixTran(i, j);
}
index_C ++;
}
}
V = V.t();
// UV = SigmaC * Sigma (kn by kn)
mat UV(totalCausalCount, totalCausalCount, fill::zeros);
UV = U * V;
//mat I_AA = mat(snpCount, snpCount, fill::eye); //can ignore, does not get used here
mat tmp_CC = mat(totalCausalCount, totalCausalCount, fill::eye) + UV;
matDet = det(tmp_CC) * sigmaDet;
mat temp1 = invSigmaMatrix * V;
mat temp2 = mat(totalSnpCount, totalCausalCount, fill::zeros);
#pragma omp critical
temp2 = temp1 * pinv(tmp_CC);
mat tmp_AA = invSigmaMatrix - temp2 * U ;
mat tmpResultMatrix1N = statMatrixtTran * tmp_AA;
mat tmpResultMatrix11 = tmpResultMatrix1N * statMatrix;
res = tmpResultMatrix11(0,0);
if(matDet==0) {
cout << "Error the matrix is singular and we fail to fix it. (reg lkl)" << endl;
exit(0);
}
/*
We compute the log of -res/2-log(det) to see if it is too big or not.
In the case it is too big we just make it a MAX value.
*/
double tmplogDet = log(sqrt(abs(matDet)));
double tmpFinalRes = -res/2 - tmplogDet;
return tmpFinalRes;
}
//here we still use Woodbury matrix, here sigma_matrix is B, and S is updated already
double PostCal::lowrank_likelihood(vector<int> configure, vector<double> * stat, double sigma_g_squared, mat sigmaC) {
//int causalCount = 0;
int totalCausalCount = 0;
double matDet = 0;
double res = 0;
for(int i = 0; i < totalSnpCount; i++) {
totalCausalCount += configure[i];
}
if (totalCausalCount == 0) {
cout << "This should have been taken care of in total likelihood function\n";
exit(1);
}
/*if(totalCausalCount == 0){ //TODO ok for now, but need to think
mat tmpResultMatrixNN = statMatrixtTran * statMatrix;
res = tmpResultMatrixNN(0,0);
matDet = 1;
return (-res/2-sqrt(abs(matDet)));
}*/
//mat sigmaC = construct_diagC(configure);
int index_C = 0;
mat sigmaMatrixTran = sigmaMatrix.t();
// In unequal sample size studies, U is adjusted for the sample sizes
// here we make U = B * sigmaC, this is still kn by mn
mat U = mat(totalCausalCount, totalSnpCount, fill::zeros);
mat small_sigma(totalSnpCount, totalCausalCount, fill::zeros);
mat small_sigmaC(totalCausalCount, totalCausalCount, fill::zeros);
for (int i = 0; i < totalSnpCount; i++) {
if (configure[i] == 1) {
for (int j = 0; j < totalSnpCount; j++) {
small_sigma(j, index_C) = sigmaMatrix(j, i);
}
small_sigmaC(index_C, index_C) = sigmaC(i, i);
index_C++;
}
}
U = small_sigma * small_sigmaC;
U = U.t();
index_C = 0;
// here V is B_trans, this is mn by kn
mat V = mat(totalCausalCount, totalSnpCount, fill::zeros);
for (int i = 0; i < totalSnpCount; i++) {
if (configure[i] == 1) {
for (int j = 0; j < totalSnpCount; j++) {
V(index_C, j) = sigmaMatrixTran(i, j);
}
index_C ++;
}
}
V = V.t();
// UV = B * SigmaC * Btrans (kn by kn)
mat UV(totalCausalCount, totalCausalCount, fill::zeros);
UV = U * V;
mat* I_AA = new mat(totalSnpCount, totalSnpCount, fill::eye);
mat tmp_CC = mat(totalCausalCount, totalCausalCount, fill::eye) + UV;
matDet = det(tmp_CC);
mat temp2 = mat(totalSnpCount, totalCausalCount, fill::zeros);
#pragma omp critical
temp2 = V * pinv(tmp_CC);
mat tmp_AA = *I_AA - temp2 * U ;
mat tmpResultMatrix1N = statMatrixtTran * tmp_AA;
mat tmpResultMatrix11 = tmpResultMatrix1N * statMatrix;
res = tmpResultMatrix11(0,0);
delete(I_AA);
if(matDet==0) {
cout << "Error the matrix is singular and we fail to fix it (low rank lkl)." << endl;
exit(0);
}
/*
We compute the log of -res/2-log(det) to see if it is too big or not.
In the case it is too big we just make it a MAX value.
*/
double tmplogDet = log(sqrt(abs(matDet)));
double tmpFinalRes = -res/2 - tmplogDet;
return tmpFinalRes;
}
int PostCal::nextBinary(vector<int>& data, int size) {
int i = 0;
int total_one = 0;
int index = size-1;
int one_countinus_in_end = 0;
while(index >= 0 && data[index] == 1) {
index = index - 1;
one_countinus_in_end = one_countinus_in_end + 1;
}
if(index >= 0) {
while(index >= 0 && data[index] == 0) {
index = index - 1;
}
}
if(index == -1) {
while(i < one_countinus_in_end+1 && i < size) {
data[i] = 1;
i=i+1;
}
i = 0;
while(i < size-one_countinus_in_end-1) {
data[i+one_countinus_in_end+1] = 0;
i=i+1;
}
}
else if(one_countinus_in_end == 0) {
data[index] = 0;
data[index+1] = 1;
}
else {
data[index] = 0;
while(i < one_countinus_in_end + 1) {
data[i+index+1] = 1;
if(i+index+1 >= size)
printf("ERROR3 %d\n", i+index+1);
i=i+1;
}
i = 0;
while(i < size - index - one_countinus_in_end - 2) {
data[i+index+one_countinus_in_end+2] = 0;
if(i+index+one_countinus_in_end+2 >= size) {
printf("ERROR4 %d\n", i+index+one_countinus_in_end+2);
}
i=i+1;
}
}
i = 0;
total_one = 0;
for(i = 0; i < size; i++)
if(data[i] == 1)
total_one = total_one + 1;
return(total_one);
}
vector<int> PostCal::findConfig(int iter) {
int numCausal = 0;
int temp = iter;
int sum = 0;
int unionSnpCount = all_snp_pos.size();
vector<int> config(unionSnpCount, 0);
int comb = nCr(unionSnpCount,numCausal);
while(temp > comb) {
temp = temp - comb;
numCausal++;
sum = sum + comb;
comb = nCr(unionSnpCount,numCausal);
}
int times = iter - sum; //this is the number of times we use find_next_binary
for(int i = 0; i < numCausal; i++){
config[i] = 1;
}
for(int i = 0; i < times; i++){
temp = nextBinary(config, unionSnpCount);
}
// printf("num causal in this config is %d\n", temp);
return config;
}
vector<int> PostCal::constructConfig(vector<int> input_causal_locs) {
vector<int> config(totalSnpCount, 0);
for ( int i = 0; i < input_causal_locs.size(); i++ ) {
if ( input_causal_locs[i] >= 0 ) {
config[input_causal_locs[i]] = 1;
}
}
return config;
}
double PostCal::computeTotalLikelihoodGivenConfigs(vector<double>* stat, double sigma_g_squared) {
printf("Input configs given\n");
double sumLikelihood = 0;
long int total_iteration = 0 ;
int mycount = 0;
printf("num total configs = %d\n", mycount);
int unionSnpCount = all_snp_pos.size();
cout << "Max Causal = " << maxCausalSNP << endl;
cout << "Union Snp Count = " << unionSnpCount << endl;
//clock_t start = clock();
vector<int> configure;
int chunksize;
if(total_iteration < 1000){
if ( total_iteration < 10 ) {
chunksize = total_iteration;
} else {
chunksize = total_iteration/10;
}
}
else{
chunksize = total_iteration/1000;
}
int curr_iter = 0;
char *configs = NULL;
size_t size_configs_file = 0;
int status = safe_mmap_read_only(configsFile, &configs, &size_configs_file);
if ( status < 0 ) {
printf("mmap did not succeed\n");
exit(1);
}
if ((num_configs * num_groups * sizeof(int16_t)) != size_configs_file) {
printf("config file is not the expected size\n");
exit(1);
}
//TODO fix this pragma
//#pragma omp parallel for schedule(static,chunksize) private(configure)
#pragma omp parallel for
for(long int cidx = 0; cidx < num_configs; cidx++) {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int16_t * input_causal_locs = (int16_t *) (configs + (cidx * num_groups * sizeof(int16_t))); //offset for (cidx)th config
vector<int> config(totalSnpCount, 0);
int numCausal = 0;
for ( int i = 0; i < num_groups; i++ ) {
if ( input_causal_locs[i] >= 0 ) {
config[input_causal_locs[i]] = 1;
numCausal += 1;
}
}
//printVec(config);
if ( numCausal == 0 ) { //if no causal, just update sum likelihood, nothing else should change
vector<int> tempConfigure(totalSnpCount, 0);
double tmp_likelihood = 0;
if(haslowrank==true){
mycount += 1;
mat tmpResultMatrixNN = statMatrixtTran * statMatrix;
double res = tmpResultMatrixNN(0,0);
double matDet = 1;
double lrl = (-res/2-sqrt(abs(matDet))); //lrl = low rank likelihood
tmp_likelihood = lrl + unionSnpCount * log(1-gamma);
}
else{
mycount += 1;
mat tmpResultMatrixNM = statMatrixtTran * invSigmaMatrix;
mat tmpResultMatrixNN = tmpResultMatrixNM * statMatrix;
double res = tmpResultMatrixNN(0,0);
double matDet = sigmaDet;
double ll = (-res/2-sqrt(abs(matDet)));
tmp_likelihood = ll + unionSnpCount * log(1-gamma);
}
for ( int ss = 0; ss < num_of_studies; ss++ ) {
noCausal[ss] = addlogSpace(noCausal[ss], tmp_likelihood);
}
#pragma omp critical
sumLikelihood = addlogSpace(sumLikelihood, tmp_likelihood);
continue;
}
vector<int> causal_locs;
//printf("causal locs are: ");
int curr_study = 0;
int curr_cum_num_snps = num_snps_all[curr_study];
for ( int i = 0; i < num_groups; i++ ) {
int causal_snp_global_idx = input_causal_locs[i];
if ( causal_snp_global_idx >= 0 ) {
//find which study this is associated with and find the snp's global position i.e. union pos
while (causal_snp_global_idx >= curr_cum_num_snps) {
curr_study += 1;
curr_cum_num_snps += num_snps_all[curr_study];
}
int union_pos = idx_to_union_pos_map[curr_study][causal_snp_global_idx - (curr_cum_num_snps - num_snps_all[curr_study])];
causal_locs.push_back(union_pos);
}
}
//at this point, causal locs could have duplicates and is not sorted
std::sort( causal_locs.begin(), causal_locs.end() );
causal_locs.erase( unique( causal_locs.begin(), causal_locs.end() ), causal_locs.end() );
//reset num causal to causal_locs.size(). num causal is now the unique number of causal snps in this config
numCausal = (int)causal_locs.size();
//printf("\n");
int** causal_bool_per_study_for_config = new int*[num_of_studies]; //this implicitly mallocs so it must be freed
int** causal_idx_per_study = new int*[num_of_studies]; //this implicitly mallocs so it must be freed
for ( int i = 0; i < num_of_studies; i++ ) {
causal_bool_per_study_for_config[i] = new int[numCausal];
causal_idx_per_study[i] = new int[numCausal];
}
for ( int i = 0; i < num_of_studies; i++ ) {
memset(causal_bool_per_study_for_config[i], 0, numCausal * sizeof(int));
memset(causal_idx_per_study[i], 0, numCausal * sizeof(int));
}
/*printf("Start causal bool per study for config\n");
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
printf("%d ", causal_bool_per_study_for_config[i][j]);
}
printf("\n");
}
printf("End causal bool per study for config\n");
printf("Start causal idx per study for config\n");
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
printf("%d ", causal_idx_per_study[i][j]);
}
printf("\n");
}
printf("End idx per study for config\n");
*/
int aux_idx = 0;
while ( aux_idx < num_groups) { //find first not -1 entry
if ( input_causal_locs[aux_idx] < 0 ) {
aux_idx += 1;
} else {
break;
}
}
curr_study = 0;
curr_cum_num_snps = 0;
for ( int i = 0; i < num_of_studies; i++ ) {
curr_cum_num_snps += num_snps_all[i];
for ( int j = 0; j < numCausal; j++ ) {
int loc_in_studyi = idx_to_snp_map[i][causal_locs[j]];
//printf("loc in study %d is %d\n", i, loc_in_studyi);
int studyi_offset = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+i, 0);
if (loc_in_studyi >= 0) { //means it exists in study i
int global_idx = studyi_offset + loc_in_studyi;
if ( global_idx >= curr_cum_num_snps ) {
curr_study += 1; //none causal in ith study, skip to next study
//curr_cum_num_snps += num_snps_all[curr_study];
break;
}
else if ( input_causal_locs[aux_idx] == global_idx ) { //we have caught up to aux_idx location
aux_idx += 1; //need to increment aux idx to next loc
causal_bool_per_study_for_config[i][j] = 1;
causal_idx_per_study[i][j] = loc_in_studyi;
while ( aux_idx < num_groups) { //reset aux idx to next causal loc
if ( input_causal_locs[aux_idx] < 0 ) {
aux_idx += 1;
} else {
break;
}
}
}
}
}
if ( aux_idx == num_groups ) { //if we have incremented aux idx past the end, then we are all done
break;
}
}
if ( aux_idx != num_groups) {
printf("This did not work as expected\n");
exit(1);
}
/*
printf("Start causal bool per study for config\n");
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
printf("%d ", causal_bool_per_study_for_config[i][j]);
}
printf("\n");
}
printf("End causal bool per study for config\n");
printf("Start causal idx per study for config\n");
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
printf("%d ", causal_idx_per_study[i][j]);
}
printf("\n");
}
printf("End idx per study for config\n");
*/
double tmp_likelihood = 0;
double just_ll = 0;
double just_prior = 0;
mat sigmaC = construct_diagC(config, numCausal, causal_idx_per_study, causal_bool_per_study_for_config);
//printf("sigma C\n");
//sigmaC.print(std::cout);
if ( haslowrank == true ) {
mycount += 1;
just_prior =
just_ll = lowrank_likelihood(config, stat, sigma_g_squared, sigmaC);
just_prior = log_prior(config, numCausal, causal_bool_per_study_for_config);
tmp_likelihood = just_ll + just_prior;
}
else {
mycount += 1;
just_ll = likelihood(config, stat, sigma_g_squared, sigmaC);
just_prior = log_prior(config, numCausal, causal_bool_per_study_for_config);
tmp_likelihood = just_ll + just_prior;
}
#pragma omp critical
sumLikelihood = addlogSpace(sumLikelihood, tmp_likelihood);
//printf("sumLikelihood is %f\n", sumLikelihood);
//printf("likelihood is %f\n", tmp_likelihood);
//printf("exp likelihood is %f\n", exp(tmp_likelihood));
for ( int w = 0; w < num_of_studies; w++ ) {
bool allZero = true;
for ( int v = 0; v < numCausal; v++ ) {
if (causal_bool_per_study_for_config[w][v] == 1) {
allZero = false;
break;
}
}
if ( allZero ) {
//printf("no causal in study %d\n", w);
noCausal[w] = addlogSpace(noCausal[w], tmp_likelihood);
}
}
for ( int g = 0; g < numCausal; g++ ) {
bool sharedCausal = true;
for ( int h = 0; h < num_of_studies; h++ ) {
if ( causal_bool_per_study_for_config[h][g] == 0 ) {
sharedCausal = false;
}
}
if ( sharedCausal ) {
//printf("shared causal\n");
sharedPips[causal_locs[g]] = addlogSpace(sharedPips[causal_locs[g]], tmp_likelihood);
sharedLL[causal_locs[g]] = addlogSpace(sharedLL[causal_locs[g]], just_ll);
//printf("shared pip = %f", sharedPips[causal_locs[g]]);
} else {
notSharedLL[causal_locs[g]] = addlogSpace(notSharedLL[causal_locs[g]], just_ll);
}
}
for(int f = 0; f < totalSnpCount; f++) {
//for(int k = 0; k < num_of_studies; k++){
#pragma omp critical
postValues[f] = addlogSpace(postValues[f], tmp_likelihood * config[f]);
//if ( nextConfigure[f] == 1 ) {
//printf("updating index %d\n", f);
//printf("added in log space %f\n", tmp_likelihood);
//printf("post value for %d is %f\n", f, postValues[f]);
//}
//}
}
//printf("post values\n");
//for ( int f = 0; f < totalSnpCount; f++ ) {
// printf("%f ", postValues[f]);
//}
//printf("\n");
for(int i = 0; i < num_of_studies; ++i) {
delete[] causal_bool_per_study_for_config[i];
delete[] causal_idx_per_study[i];
}
//Free the array of pointers
delete[] causal_bool_per_study_for_config;
delete[] causal_idx_per_study;
#pragma omp critical
if(cidx % 1000 == 0 and cidx > curr_iter){
cerr << "\r \r" << (double) (cidx) / (double) num_configs * 100.0 << "%";
curr_iter = cidx;
}
if(cidx % 1000 == 0 ){
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//std::cout << "Time to eval config = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
}
}
//cout << "\ncomputing likelihood of all configurations took " << (float)(clock()-start)/CLOCKS_PER_SEC << "seconds.\n";
printf("num total configs = %d\n", mycount);
status = munmap(configs, size_configs_file);
return(sumLikelihood);
}
double PostCal::computeTotalLikelihood(vector<double>* stat, double sigma_g_squared) {
double sumLikelihood = 0;
long int total_iteration = 0 ;
int mycount = 0;
printf("num total configs = %d\n", mycount);
int unionSnpCount = all_snp_pos.size();
for(long int i = 0; i <= maxCausalSNP; i++)
//total_iteration = total_iteration + nCr(snpCount, i);
total_iteration = total_iteration + nCr(unionSnpCount, i);
cout << "Max Causal = " << maxCausalSNP << endl;
cout << "Union Snp Count = " << unionSnpCount << endl;
//clock_t start = clock();
vector<int> configure;
int num;
int chunksize;
if(total_iteration < 1000){
if ( total_iteration < 10 ) {
chunksize = total_iteration;
} else {
chunksize = total_iteration/10;
}
}
else{
chunksize = total_iteration/1000;
}
int curr_iter = 0;
int nP = omp_get_num_procs();
printf("num procs = %d\n", nP);
printf("max num procs = %d\n", omp_get_max_threads());
printf("num threads = %d\n", omp_get_num_threads());
int*** thread_mem_idx = (int***)malloc(nP * sizeof(int**));
int*** thread_mem_bool = (int***)malloc(nP * sizeof(int**));
int*** thread_mem_bool_for_config = (int***)malloc(nP * sizeof(int**));
for ( int b = 0; b < nP; b++ ) {
thread_mem_idx[b] = (int**)malloc(num_of_studies * sizeof(int*));
thread_mem_bool[b] = (int**)malloc(num_of_studies * sizeof(int*));
thread_mem_bool_for_config[b] = (int**)malloc(num_of_studies * sizeof(int*));
for ( int q = 0; q < num_of_studies; q++ ) {
thread_mem_idx[b][q] = (int*)malloc(3 * sizeof(int));
thread_mem_bool[b][q] = (int*)malloc(3 * sizeof(int));
thread_mem_bool_for_config[b][q] = (int*)malloc(3 * sizeof(int));
}
}
omp_set_num_threads(nP);
#pragma omp parallel for schedule(static,chunksize) private(configure,num)
for(long int iter = 0; iter < total_iteration; iter++) {
if(iter%chunksize == 0){
configure = findConfig(iter);
}
else{
//num = nextBinary(configure, snpCount);
num = nextBinary(configure, unionSnpCount);
}
int numCausal = std::accumulate(configure.begin(), configure.end(), 0);
//if ((numCausal != 0) and (numCausal != maxCausalSNP)) {
// printf("%d is numCausal: ", numCausal);
// printf("%d is maxCausal: ", maxCausalSNP);
//printf("Skipping initial configure: ");
//printf("next configure to expand\n");
//printVec(configure);
// continue;
//}
//printf("Initial configure: ");
//printVec(configure);
//printf("numCausal = %d\n", numCausal);
if ( numCausal == 0 ) { //if no causal, just update sum likelihood, nothing else should change
vector<int> tempConfigure(totalSnpCount, 0);
double tmp_likelihood = 0;
if(haslowrank==true){
//mycount += 1;
mat tmpResultMatrixNN = statMatrixtTran * statMatrix;
double res = tmpResultMatrixNN(0,0);
double matDet = 1;
double lrl = (-res/2-sqrt(abs(matDet))); //lrl = low rank likelihood
tmp_likelihood = lrl + unionSnpCount * log(1-gamma);
}
else{
//mycount += 1;
mat tmpResultMatrixNM = statMatrixtTran * invSigmaMatrix;
mat tmpResultMatrixNN = tmpResultMatrixNM * statMatrix;
double res = tmpResultMatrixNN(0,0);
double matDet = sigmaDet;
double ll = (-res/2-sqrt(abs(matDet)));
tmp_likelihood = ll + unionSnpCount * log(1-gamma);
}
for ( int ss = 0; ss < num_of_studies; ss++ ) {
noCausal[ss] = addlogSpace(noCausal[ss], tmp_likelihood);
}
#pragma omp critical
sumLikelihood = addlogSpace(sumLikelihood, tmp_likelihood);
continue;
}
vector<int> causal_locs;
//printf("causal locs are: ");
for ( int i = 0; i < unionSnpCount; i++ ) {
if ( configure[i] == 1 ) {
causal_locs.push_back(i);
//printf("%d ", i);
}
}
//printf("\n");
//printf("total snp count is: %d\n", totalSnpCount);
vector<int> startConfigure(totalSnpCount, 0);
/*
int** causal_idx_per_study = new int*[num_of_studies];
int** causal_bool_per_study = new int*[num_of_studies];
int** causal_bool_per_study_for_config = new int*[num_of_studies];
for ( int i = 0; i < num_of_studies; i++ ) { //3 is hardcoded as max causal for now
causal_idx_per_study[i] = new int[3];
causal_bool_per_study[i] = new int[3];
causal_bool_per_study_for_config[i] = new int[3];
}
*/
int pid = omp_get_thread_num();
if ((pid < 0) || (pid >= nP)) {
cout << "Invalid PID\n" << endl;
exit(1); // terminate with error
}
int** causal_idx_per_study = thread_mem_idx[pid];
int** causal_bool_per_study = thread_mem_bool[pid];
for ( int i = 0; i < num_of_studies; i++ ) {
memset(causal_idx_per_study[i], 0, 3 * sizeof(int));
memset(causal_bool_per_study[i], 0, 3 * sizeof(int));
//memset(causal_bool_per_study_for_config[i], 0, 3 * sizeof(int));
}
for ( int i = 0; i < num_of_studies; i++ ) {
for ( int j = 0; j < numCausal; j++ ) {
int loc_in_studyi = idx_to_snp_map[i][causal_locs[j]];
//printf("loc in study %d is %d\n", i, loc_in_studyi);
int studyi_offset = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+i, 0);
if (loc_in_studyi >= 0) { //means it exists in study i
causal_idx_per_study[i][j] = loc_in_studyi;
causal_bool_per_study[i][j] = 1;
startConfigure[studyi_offset + loc_in_studyi] = 1;
} else {
causal_idx_per_study[i][j] = -1;
causal_bool_per_study[i][j] = 0;
}
}
}
/*
printf("causal index per study\n");
for ( int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 3; j++ ) {
printf("%d ", causal_idx_per_study[i][j]);
}
printf("\n");
}
printf("causal bool per study\n");
for ( int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 3; j++ ) {
printf("%d ", causal_bool_per_study[i][j]);
}
printf("\n");
}*/
int total_num_additional_configs = 0;
for ( int i = 0; i < numCausal; i++ ) {
for ( int j = 0; j < num_of_studies; j++ ) {
if ( causal_bool_per_study[j][i] == 1 ) {
total_num_additional_configs += 1;
}
}
}
total_num_additional_configs = (int)(pow(2, total_num_additional_configs) - 1);
// printf("num additional = %d\n", total_num_additional_configs);
for ( int i = 0; i < total_num_additional_configs; i++ ) {
//int pid = omp_get_thread_num();
//if ( ( pid < 0 ) || ( pid >= nP ) ) { printf("BAD PID\n"); }
int **causal_bool_per_study_for_config = thread_mem_bool_for_config[pid];
for ( int j = 0; j < num_of_studies; j++ ) {
memset(causal_bool_per_study_for_config[j], 0, 3 * sizeof(int));
}
/*
//int **causal_bool_per_study_for_config = new int*[num_of_studies];
int** causal_bool_per_study_for_config = (int**)malloc(num_of_studies * sizeof(int*));
if (causal_bool_per_study_for_config == nullptr) {printf("alloc error\n");}
for ( int j = 0; j < num_of_studies; j++ ) {
//causal_bool_per_study_for_config[j] = new int[3];
causal_bool_per_study_for_config[j] = (int*)malloc(3 * sizeof(int));
if (causal_bool_per_study_for_config[j] == nullptr) {printf("alloc error\n");}
}
for ( int j = 0; j < num_of_studies; j++ ) {
memset(causal_bool_per_study_for_config[j], 0, 3 * sizeof(int));
}
*/
int bmask = i+1;
vector<int> nextConfigure(totalSnpCount, 0);
for ( int j = 0; j < numCausal; j++ ) {
for ( int k = 0; k < num_of_studies; k++ ) {
int studyk_offset = std::accumulate(num_snps_all.begin(), num_snps_all.begin()+k, 0);
if ( causal_bool_per_study[k][j] == 1 ) { //if causal snp j exists in study k
int loc_in_studyk = causal_idx_per_study[k][j];
int causal_val = bmask & 0x1;
causal_bool_per_study_for_config[k][j] = causal_val;
nextConfigure[studyk_offset + loc_in_studyk] = causal_val;
bmask = bmask >> 1;
} else {
continue;
}
}
}
/*
printf("causal bool per study for config\n");
for ( int ii = 0; ii < 2; ii++ ) {
for ( int jj = 0; jj < 3; jj++ ) {
printf("%d ", causal_bool_per_study_for_config[ii][jj]);
}
printf("\n");
}*/
if (!checkOR(causal_bool_per_study_for_config, num_of_studies, numCausal)) {
continue;
}
//if (!checkAND(causal_bool_per_study_for_config, num_of_studies, numCausal)) {
// continue;
//}
//printf("next config to eval\n");
//printVec(nextConfigure);
double tmp_likelihood = 0;
double just_ll = 0;
double just_prior = 0;
mat sigmaC = construct_diagC(nextConfigure, numCausal, causal_idx_per_study, causal_bool_per_study_for_config);
//printf("sigma C\n");
//sigmaC.print(std::cout);
if ( haslowrank == true ) {
//mycount += 1;
just_ll = lowrank_likelihood(nextConfigure, stat, sigma_g_squared, sigmaC);
just_prior = log_prior(nextConfigure, numCausal, causal_bool_per_study_for_config);
tmp_likelihood = just_ll + just_prior;
}
else {
//mycount += 1;
just_ll = likelihood(nextConfigure, stat, sigma_g_squared, sigmaC);
just_prior = log_prior(nextConfigure, numCausal, causal_bool_per_study_for_config);
tmp_likelihood = just_ll + just_prior;
}
#pragma omp critical
sumLikelihood = addlogSpace(sumLikelihood, tmp_likelihood);
//printf("sumLikelihood is %f\n", sumLikelihood);
//printf("likelihood is %f\n", tmp_likelihood);
//printf("exp likelihood is %f\n", exp(tmp_likelihood));
for ( int w = 0; w < num_of_studies; w++ ) {
bool allZero = true;
for ( int v = 0; v < numCausal; v++ ) {
if (causal_bool_per_study_for_config[w][v] == 1) {
allZero = false;
break;
}
}
if ( allZero ) {
//printf("no causal in study %d\n", w);
noCausal[w] = addlogSpace(noCausal[w], tmp_likelihood);
}
}