forked from wuyangf7/OPERA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOPERA.cpp
12080 lines (11581 loc) · 598 KB
/
OPERA.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
// OPERA.cpp
//
// Created by Yang Wu on 04/06/18.
// Copyright (c) 2021 Yang Wu. All rights reserved.
//
#include "SMR_data.h"
#include "OPERA.h"
#include "stat.hpp"
#include "SMR_data_p1.h"
namespace SMRDATA
{
// read mbfile, modified from GCTA, Yang WU 29/11/2021
void read_multi_bimfiles(bInfo* bdata, vector<string> multi_bfiles, map<string, string> &snp_name_per_chr)
{
cout << "Reading the PLINK BIM files ..." << endl;
int i=0, nbfiles = multi_bfiles.size();
string bimfile = "";
bdata->_chr.clear();
bdata->_snp_name.clear();
bdata->_genet_dst.clear();
bdata->_bp.clear();
bdata->_allele1.clear();
bdata->_allele2.clear();
snp_name_per_chr.clear();
for( i=0; i<nbfiles; i++ ) {
bInfo bdatatmp;
bimfile = multi_bfiles[i]+".bim";
read_single_bimfile(&bdatatmp, bimfile, false);
update_include(bdata, &bdatatmp, i, snp_name_per_chr);
}
// Initialize _include
bdata->_snp_num = bdata->_snp_name.size();
bdata->_include.clear(); bdata->_include.resize(bdata->_snp_num);
bdata->_snp_name_map.clear();
for (int i = 0; i < bdata->_snp_num; i++) {
bdata->_include[i] = i;
bdata->_snp_name_map.insert(pair<string, int>(bdata->_snp_name[i], i));
}
bdata->_ref_A = bdata->_allele1; bdata->_other_A = bdata->_allele2;
printf(" %ld SNPs to be included from PLINK BIM files.\n", bdata->_snp_num);
}
void update_include(bInfo* bdata, bInfo* bdatatmp, int file_indx, map<string, string> &snp_name_per_chr)
{
for (int i = 0; i < bdatatmp->_snp_num; i++) {
if( snp_name_per_chr.find(bdatatmp->_snp_name[i]) != snp_name_per_chr.end()) {
cout << "Warning: Duplicated SNP ID \"" + bdatatmp->_snp_name[i] + "\" ";
stringstream ss;
ss << bdatatmp->_snp_name[i] << "_" << i + 1;
bdatatmp->_snp_name[i] = ss.str();
cout<<"has been changed to \"" + bdatatmp->_snp_name[i] + "\".\n";
}
snp_name_per_chr.insert(pair<string, string>(bdatatmp->_snp_name[i], to_string(file_indx)+":"+to_string(i)));
}
// Add new SNPs
bdata->_chr.insert(bdata->_chr.end(), bdatatmp->_chr.begin(), bdatatmp->_chr.end());
bdata->_snp_name.insert(bdata->_snp_name.end(), bdatatmp->_snp_name.begin(), bdatatmp->_snp_name.end());
bdata->_genet_dst.insert(bdata->_genet_dst.end(), bdatatmp->_genet_dst.begin(), bdatatmp->_genet_dst.end());
bdata->_bp.insert(bdata->_bp.end(), bdatatmp->_bp.begin(), bdatatmp->_bp.end());
bdata->_allele1.insert(bdata->_allele1.end(), bdatatmp->_allele1.begin(), bdatatmp->_allele1.end());
bdata->_allele2.insert(bdata->_allele2.end(), bdatatmp->_allele2.begin(), bdatatmp->_allele2.end());
}
void read_single_bimfile(bInfo* bdata,string bimfile, bool msg_flag)
{
// Read bim file: recombination rate is defined between SNP i and SNP i-1
int ibuf = 0;
string cbuf = "0";
double dbuf = 0.0;
string str_buf;
ifstream Bim(bimfile.c_str());
if (!Bim) throw ("Error: can not open the file [" + bimfile + "] to read.");
if(msg_flag) cout << "Reading PLINK BIM file from [" + bimfile + "]." << endl;
bdata->_chr.clear();
bdata->_snp_name.clear();
bdata->_genet_dst.clear();
bdata->_bp.clear();
bdata->_allele1.clear();
bdata->_allele2.clear();
while (Bim) {
Bim >> ibuf;
if (Bim.eof()) break;
bdata->_chr.push_back(ibuf);
Bim >> str_buf;
bdata->_snp_name.push_back(str_buf);
Bim >> dbuf;
bdata->_genet_dst.push_back(dbuf);
Bim >> ibuf;
bdata->_bp.push_back(ibuf);
Bim >> cbuf;
StrFunc::to_upper(cbuf);
bdata->_allele1.push_back(cbuf.c_str());
Bim >> cbuf;
StrFunc::to_upper(cbuf);
bdata->_allele2.push_back(cbuf.c_str());
}
Bim.close();
bdata->_snp_num = bdata->_chr.size();
if(msg_flag) cout << bdata->_snp_num << " SNPs to be included from [" + bimfile + "]." << endl;
}
void read_multi_famfiles(bInfo* bdata, vector<string> multi_bfiles)
{
cout << "Reading the PLINK BIM files ..." << endl;
int i=0, nbfiles = multi_bfiles.size();
string famfile = "";
bdata->_fid.clear();
bdata->_pid.clear();
bdata->_fa_id.clear();
bdata->_mo_id.clear();
bdata->_sex.clear();
bdata->_pheno.clear();
for( i=0; i<nbfiles; i++ ) {
bInfo bdatatmp;
famfile = multi_bfiles[i]+".fam";
read_single_famfile(&bdatatmp, famfile, false);
update_keep(bdata, &bdatatmp, famfile);
}
// Sample size
bdata->_indi_num = bdata->_fid.size();
printf(" %ld individuals have been included from the PLINK FAM files.\n", bdata->_indi_num);
}
void update_keep(bInfo* bdata, bInfo* bdatatmp, string famfile)
{
int indx = 0; string indi_str = "";
bdatatmp->_indi_num = bdatatmp->_fid.size();
// Initial sample size
if(bdata->_fid.size() == 0) {
bdata->_fid = bdatatmp->_fid;
bdata->_pid = bdatatmp->_pid;
bdata->_fa_id = bdatatmp->_fa_id;
bdata->_mo_id = bdatatmp->_mo_id;
bdata->_sex = bdatatmp->_sex;
bdata->_pheno = bdatatmp->_pheno;
// Initialize the id map per chr
int size=0;
bdata->_keep.clear(); bdata->_id_map.clear();
for(int i = 0; i < bdatatmp->_indi_num; i++) {
bdata->_keep.push_back(i);
bdata->_id_map.insert(pair<string,int>(bdata->_fid[i] + ":" + bdata->_pid[i], i));
if (size == bdata->_id_map.size()) throw ("Error: Duplicate individual ID found: \"" + bdata->_fid[i] + "\t" + bdata->_pid[i] + "\".");
size = bdata->_id_map.size();
}
} else {
// Add new individuals
for(int i = 0; i < bdatatmp->_indi_num; i++) {
// Search conflicted information of individuals
indi_str = bdatatmp->_fid[i] + ":" + bdatatmp->_pid[i];
map<string,int>::iterator iter = bdata->_id_map.find(indi_str);
if(iter!=bdata->_id_map.end()) {
// already existed
indx = iter->second;
if(bdatatmp->_fa_id[i] != bdata->_fa_id[indx]) throw("Inconsistent paternal ID found, " + bdatatmp->_fid[i] + " " + bdatatmp->_pid[i] + ", from [" + famfile + "].");
if(bdatatmp->_mo_id[i] != bdata->_mo_id[indx]) throw("Inconsistent maternal ID found, " + bdatatmp->_fid[i] + " " + bdatatmp->_pid[i] + ", from [" + famfile + "].");
if(bdatatmp->_sex[i] != bdata->_sex[indx]) throw("Inconsistent gender found, " + bdatatmp->_fid[i] + " " + bdatatmp->_pid[i] + ", from [" + famfile + "].");
if(bdatatmp->_pheno[i] != bdata->_pheno[indx]) throw("Inconsistent phenotype found, " + bdatatmp->_fid[i] + " " + bdatatmp->_pid[i] + ", from [" + famfile + "].");
if(i!=indx) throw("Inconsistent orders of individuals found from [" + famfile + "]. Please make sure that the orders of individuals are the same across the fam files.");
} else {
// not existed
throw("Unexpected individual ID found, " + bdatatmp->_fid[i] + " " + bdatatmp->_pid[i] + ", from [" + famfile + "].");
}
}
}
}
void read_single_famfile(bInfo* bdata, string famfile, bool msg_flag)
{
bdata->_autosome_num = 22;
ifstream Fam(famfile.c_str());
if (!Fam) throw ("Error: can not open the file [" + famfile + "] to read.");
if(msg_flag) cout << "Reading PLINK FAM file from [" + famfile + "]." << endl;
int i = 0;
string str_buf;
bdata->_fid.clear();
bdata->_pid.clear();
bdata->_fa_id.clear();
bdata->_mo_id.clear();
bdata->_sex.clear();
bdata->_pheno.clear();
while (Fam) {
Fam >> str_buf;
if (Fam.eof()) break;
bdata->_fid.push_back(str_buf);
Fam >> str_buf;
bdata->_pid.push_back(str_buf);
Fam >> str_buf;
bdata->_fa_id.push_back(str_buf);
Fam >> str_buf;
bdata->_mo_id.push_back(str_buf);
Fam >> str_buf;
bdata->_sex.push_back(atoi(str_buf.c_str()));
Fam >> str_buf;
bdata->_pheno.push_back(atoi(str_buf.c_str()));
}
Fam.clear();
Fam.close();
if(msg_flag) {
bdata->_indi_num = bdata->_fid.size();
cout << bdata->_indi_num << " individuals to be included from [" + famfile + "]." << endl;
}
}
void read_multi_bedfiles(bInfo* bdata, vector<string> multi_bfiles, map<string, string> &snp_name_per_chr)
{
int i=0, nbfiles = multi_bfiles.size();
string bedfile = "";
vector<vector<pair<int, int>>> rsnp;
vector<int> rindi_flag, rsnp_flag;
if (bdata->_include.size() == 0) throw ("Error: No SNP is retained for analysis.");
if (bdata->_keep.size() == 0) throw ("Error: No individual is retained for analysis.");
cout << "Reading the PLINK BIM files ..." << endl;
// Initialize the matrix
bdata->_snp_1.resize(bdata->_include.size());
bdata->_snp_2.resize(bdata->_include.size());
for (i = 0; i < bdata->_include.size(); i++) {
bdata->_snp_1[i].reserve(bdata->_keep.size());
bdata->_snp_2[i].reserve(bdata->_keep.size());
}
// Update the map to retrieve individuals and SNPs
update_id_chr_map(snp_name_per_chr, bdata->_snp_name_map);
// Flag for reading individuals and SNPs
//get_rindi_flag
rindi_flag.clear();
rindi_flag.resize(bdata->_indi_num);
for (int i = 0; i < bdata->_indi_num; i++) {
if (bdata->_id_map.find(bdata->_fid[i] + ":" + bdata->_pid[i]) != bdata->_id_map.end()) rindi_flag[i] = 1;
else rindi_flag[i] = 0;
}
//get_rsnp_flag
rsnp_flag.clear();
rsnp_flag.resize(bdata->_snp_num);
for (int i = 0; i < bdata->_snp_num; i++) {
if (bdata->_snp_name_map.find(bdata->_snp_name[i]) != bdata->_snp_name_map.end()) rsnp_flag[i] = 1;
else rsnp_flag[i] = 0;
}
update_fam(bdata, rindi_flag);
update_bim(bdata, rsnp_flag);
retrieve_snp(snp_name_per_chr, bdata->_snp_name_map, rsnp, nbfiles);
// Read the coded genotypes
for(i=0; i<nbfiles; i++) {
if(rsnp[i].size()==0) {
cout<<"Skip reading " + multi_bfiles[i] + ".bed, no SNPs retained on this chromosome."<<endl;
continue;
} else {
bedfile = multi_bfiles[i] + ".bed";
read_single_bedfile(bdata, bedfile, rsnp[i], rindi_flag, false);
}
}
cout << "Genotype data for " + to_string(bdata->_keep.size()) + " individuals and " + to_string(bdata->_include.size()) + " SNPs have been included." <<endl;
}
void update_id_chr_map(map<string, string> &chr_map, map<string, int> id_map)
{
int i = 0;
map<string, string> chr_map_buf(chr_map);
map<string, int>::iterator iter1;
map<string, string>::iterator iter2;
for(iter1=id_map.begin(); iter1!=id_map.end(); iter1++) chr_map_buf.erase(iter1->first);
for(iter2=chr_map_buf.begin(); iter2!=chr_map_buf.end(); iter2++) chr_map.erase(iter2->first);
}
void retrieve_snp(map<string,string> snp_chr_map, map<string,int> snp_id_map, vector<vector<pair<int,int>>> &rsnp, int nbfiles)
{
int i = 0, j=0, snp_indx = 0;
string snp_indx_str = "";
vector<string> vs_buf;
map<string,string>::iterator iter1;
map<string,int>::iterator iter2;
rsnp.clear(); rsnp.resize(nbfiles);
for(iter1=snp_chr_map.begin(), iter2=snp_id_map.begin(); iter1 != snp_chr_map.end(); iter1++, iter2++) {
vs_buf.clear();
snp_indx_str = iter1->second;
StrFunc::split_string(snp_indx_str, vs_buf, ":");
snp_indx = iter2->second;
rsnp[atoi(vs_buf[0].c_str())].push_back(make_pair(atoi(vs_buf[1].c_str()), snp_indx));
}
for(i=0; i<nbfiles; i++) stable_sort(rsnp[i].begin(), rsnp[i].end());
}
void read_single_bedfile(bInfo* bdata, string bedfile, vector<pair<int,int>> rsnp, vector<int> rindi, bool msg_flag)
{
int i = 0, j = 0, k = 0, t1=0, nsnp_chr = rsnp.size(), nindi_chr = rindi.size();
// Read bed file
char ch[1];
bitset<8> b;
fstream BIT(bedfile.c_str(), ios::in | ios::binary);
if (!BIT) throw ("Error: can not open the file [" + bedfile + "] to read.");
if(msg_flag) cout << "Reading PLINK BED file from [" + bedfile + "] in SNP-major format ..." << endl;
for (i = 0; i < 3; i++) BIT.read(ch, 1); // skip the first three bytes
int snp_indx = 0, indi_indx = 0;
for (j = 0, t1 = 0, snp_indx = 0; t1 < nsnp_chr; j++) { // Read genotype in SNP-major mode, 00: homozygote AA; 11: homozygote BB; 01: hetezygote; 10: missing
if (j!=rsnp[t1].first) {
for (i = 0; i < nindi_chr; i += 4) BIT.read(ch, 1);
continue;
}
snp_indx = rsnp[t1].second;
for (i = 0, indi_indx = 0; i < nindi_chr;) {
BIT.read(ch, 1);
if (!BIT) throw ("Error: problem with the BED file ... has the FAM/BIM file been changed?");
b = ch[0];
k = 0;
while (k < 7 && i < nindi_chr) { // change code: 11 for AA; 00 for BB;
if (!rindi[i]) k += 2;
else {
bdata->_snp_2[snp_indx][indi_indx] = (!b[k++]);
bdata->_snp_1[snp_indx][indi_indx] = (!b[k++]);
indi_indx++;
}
i++;
}
}
t1++;
}
BIT.clear();
BIT.close();
if(msg_flag) cout << "Genotype data for " << bdata->_keep.size() << " individuals and " << bdata->_include.size() << " SNPs to be included from [" + bedfile + "]." << endl;
}
void read_prb_cojo_snplist(string snpprblistfile, vector<string> &prblist, map<string, vector<string>> &prb_snp)
{
// Read probe file
prblist.clear();
prb_snp.clear();
FILE* rfile=fopen(snpprblistfile.c_str(),"r");
if(!rfile) {
printf("File %s open failed.\n",snpprblistfile.c_str());
exit(EXIT_FAILURE);
}
printf("Reading the independent COJO signal list for each probe from %s ...\n", snpprblistfile.c_str());
char Tbuf[MAX_LINE_SIZE];
int line_idx=0;
vector<string> strlist;
while(fgets(Tbuf, MAX_LINE_SIZE, rfile))
{
split_string(Tbuf, strlist, " \t\n");
if(strlist.size()!=2)
{
printf("ERROR: line %d doesn't have 2 items.\n", line_idx);
exit(EXIT_FAILURE);
}
string tarprb=strlist[0];
vector<string> tarsnp;
split_string(strlist[1],tarsnp);
prb_snp.insert(pair<string, vector<string>>(tarprb,tarsnp));
if(prb_snp.size()==line_idx)
{
printf("ERROR: Duplicate probe found : %s.\n", tarprb.c_str());
exit(EXIT_FAILURE);
}
prblist.push_back(tarprb);
line_idx++;
}
fclose(rfile);
printf("The independent COJO signals for %d probes have been read from %s.\n",line_idx,snpprblistfile.c_str());
}
void read_GWAS_cojo_snplist(lociData* ldata, char* lociFileName)
{
ifstream lociFile;
if(!file_read_check(&lociFile, lociFileName))
{
fprintf (stderr, "%s: Couldn't open file %s\n",
lociFileName, strerror (errno));
exit (EXIT_FAILURE);
}
cout << "Reading the independent GWAS COJO signals list from [" + string(lociFileName) + "]." << endl;
ldata->_chr.clear();
ldata->_snp_name.clear();
ldata->_bp.clear();
ldata->_include.clear();
char buf[MAX_LINE_SIZE];
int lineNum(0);
lociFile.getline(buf,MAX_LINE_SIZE);// the header
if(buf[0]=='\0')
{
printf("ERROR: the first row of the file %s is empty.\n",lociFileName);
exit(EXIT_FAILURE);
}
vector<string> vs_buf;
split_string(buf, vs_buf, ", \t\n");
to_upper(vs_buf[0]);
if(vs_buf[1]!="SNP") {
printf("ERROR: %s should have headers that start with \"snp\".\n", lociFileName);
exit(EXIT_FAILURE);
}
while(!lociFile.eof())
{
lociFile.getline(buf,MAX_LINE_SIZE);
if(buf[0]!='\0'){
vs_buf.clear();
int col_num = split_string(buf, vs_buf, ", \t\n");
if(col_num!=3) {
printf("ERROR: column number is not correct in row %d!\n", lineNum+2);
exit(EXIT_FAILURE);
}
if(vs_buf[0]=="NA" || vs_buf[0]=="na"){
printf("WARNING: Chromosome is \'NA\' in row %d.\n", lineNum+2);
ldata->_chr.push_back(0);
} else {
ldata->_chr.push_back(atof(vs_buf[0].c_str()));
}
if(vs_buf[1]=="NA" || vs_buf[1]=="na"){
printf("ERROR: the SNP name is \'NA\' in row %d.\n", lineNum+2);
exit(EXIT_FAILURE);
}
ldata->_snp_name.push_back(vs_buf[1]);
if(vs_buf[2]=="NA" || vs_buf[2]=="na"){
printf("WARNING: physical position is \'NA\' in row %d.\n", lineNum+2);
ldata->_bp.push_back(0);
} else {
ldata->_bp.push_back(atof(vs_buf[2].c_str()));
}
ldata->_include.push_back(lineNum);
lineNum++;
}
}
cout <<"There are "<<ldata->_include.size() << " independent GWAS COJO loci information to be included from [" + string(lociFileName) + "]." << endl;
lociFile.close();
}
void read_pifile(string piFilename, vector<string> &priorsplit)
{
// Read prior probabilities file
priorsplit.clear();
FILE* rfile=fopen(piFilename.c_str(),"r");
if(!rfile) {
printf("File %s open failed.\n",piFilename.c_str());
exit(EXIT_FAILURE);
}
printf("\nReading the estimated prior probabilities from %s ...\n", piFilename.c_str());
char Tbuf[MAX_LINE_SIZE];
int line_idx=0;
vector<string> strlist;
while(fgets(Tbuf, MAX_LINE_SIZE, rfile))
{
split_string(Tbuf, strlist, ", \t\n");
if(line_idx == 0 && strlist[0]!="Posteriors")
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check.\n", piFilename.c_str());
exit(EXIT_FAILURE);
}
line_idx++;
if(line_idx == 2) {
for(int i=1;i<strlist.size();i++) {
priorsplit.push_back(strlist[i]);
}
}
}
if(line_idx != 3)
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check.\n", piFilename.c_str());
exit(EXIT_FAILURE);
}
fclose(rfile);
printf("There are %d prior probabilities included in %s.\n",priorsplit.size(),piFilename.c_str());
}
void read_numfile(string numFilename, vector<long> &numsum)
{
FILE* nfile=fopen(string(numFilename).c_str(),"r");
if(!nfile) {
printf("File %s open failed.\n",string(numFilename).c_str());
exit(EXIT_FAILURE);
}
printf("\nReading the number of tests performed for different exposure combinations in stage 2 analysis from %s ......\n", string(numFilename).c_str());
char Tbuf[MAX_LINE_SIZE];
vector<string> numlist;
fgets(Tbuf, MAX_LINE_SIZE, nfile);
split_string(Tbuf, numlist, " \t\n");
int numsize = numlist.size();
int expoNum = numsize - 1;
if(numlist[0]!="expoNum")
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 2 analysis of OPERA! Please check.\n", string(numFilename).c_str());
exit(EXIT_FAILURE);
}
int line_num = 0;
while(fgets(Tbuf, MAX_LINE_SIZE, nfile))
{
split_string(Tbuf, numlist, ", \t\n");
if(numlist.size() != numsize) {
printf("ERROR: The %ld row from the input file %s is incomplete! Please check.\n", line_num, string(numFilename).c_str());
exit(EXIT_FAILURE);
}
if(numlist[0]!="testNum")
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 2 analysis of OPERA! Please check.\n", string(numFilename).c_str());
exit(EXIT_FAILURE);
}
for(int i=1;i<numlist.size();i++) {
numsum[i-1]+=stol(numlist[i]);
}
line_num++;
}
fclose(nfile);
printf("There are %d line(s) for %ld exposure combinations included in %s.\n",line_num, expoNum, string(numFilename).c_str());
}
void read_varfile(string varFilename, vector<string> &sigmasplit)
{
// Read prior variance file
sigmasplit.clear();
FILE* rfile=fopen(varFilename.c_str(),"r");
if(!rfile) {
printf("File %s open failed.\n",varFilename.c_str());
exit(EXIT_FAILURE);
}
printf("\nReading the estimated prior variance from %s ...\n", varFilename.c_str());
char Tbuf[MAX_LINE_SIZE];
int line_idx=0;
vector<string> strlist;
while(fgets(Tbuf, MAX_LINE_SIZE, rfile))
{
split_string(Tbuf, strlist, ", \t\n");
if(line_idx == 0 && strlist[0]!="Variance")
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check.\n", varFilename.c_str());
exit(EXIT_FAILURE);
}
if(line_idx == 0) {
for(int i=1;i<strlist.size();i++) {
sigmasplit.push_back(strlist[i]);
}
}
line_idx++;
}
if(line_idx != 1)
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check.\n", varFilename.c_str());
exit(EXIT_FAILURE);
}
fclose(rfile);
printf("There are %d prior variance included in %s.\n",sigmasplit.size(),varFilename.c_str());
}
void read_rhofile(string rhoFilename, MatrixXd &rho)
{
// Read the estimated residual correlation matrix
FILE* rfile=fopen(rhoFilename.c_str(),"r");
if(!rfile) {
printf("File %s open failed.\n",rhoFilename.c_str());
exit(EXIT_FAILURE);
}
int expoNum = rho.rows();
printf("\nReading the estimated residual correlations from %s ...\n", rhoFilename.c_str());
char Tbuf[MAX_LINE_SIZE];
int line_idx=0;
vector<string> strlist;
while(fgets(Tbuf, MAX_LINE_SIZE, rfile))
{
split_string(Tbuf, strlist, ", \t\n");
if(line_idx == 0 && atof(strlist[0].c_str())!=1 || strlist.size()!=expoNum)
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check either the dimension of input matrix is consistent with the number of exposures or the diagnol matrix of input matrix is 1.\n", rhoFilename.c_str());
exit(EXIT_FAILURE);
}
for(int i=0;i<strlist.size();i++) {
rho(line_idx,i) = atof(strlist[i].c_str());
}
line_idx++;
}
if(line_idx != expoNum)
{
printf("ERROR: The input file %s doesn't follow the output file format from the stage 1 analysis of OPERA! Please check.\n", rhoFilename.c_str());
exit(EXIT_FAILURE);
}
fclose(rfile);
printf("The estimated correlation matrix of %d exposures are included in %s.\n",line_idx,rhoFilename.c_str());
}
void extract_snp_by_chr(bInfo* bdata, int chr)
{
vector<string> snplist;
for(int i = 0; i < bdata->_include.size(); i++){
int j = bdata->_include[i];
if(bdata->_chr[j] == chr) snplist.push_back(bdata->_snp_name[j]);
}
if(snplist.empty()) throw ("Error: on SNP found in this region.");
update_id_map_kp(snplist, bdata->_snp_name_map, bdata->_include);
cout << bdata->_include.size() << " SNPs are extracted from chromosome "<<chr<< "."<<endl;
}
void extract_ldata_by_chr(lociData* lociData, int prbchr)
{
vector<int> newIcld;
for(int i=0;i<lociData->_include.size();i++)
{
int tmpint=lociData->_include[i];
if(lociData->_chr[tmpint]==prbchr) newIcld.push_back(tmpint);
}
lociData->_include.clear();
lociData->_include=newIcld;
cout << lociData->_include.size() << " GWAS loci are extracted from chromosome [" + atos(prbchr) + "]." << endl;
}
void extract_prob_opera(eqtlInfo* eqtlinfo,string problstName)
{
vector<string> problist;
string msg="probes";
read_msglist(problstName, problist,msg);
vector<string> raw_problist;
for(int i=0;i<eqtlinfo->_include.size();i++) raw_problist.push_back(eqtlinfo->_epi_prbID[eqtlinfo->_include[i]]);
vector<string> common_probes;
getUnique(problist);
set_intersect(problist, raw_problist, common_probes);
eqtlinfo->_include.clear();
StrFunc::match_only(common_probes, eqtlinfo->_epi_prbID, eqtlinfo->_include);
stable_sort(eqtlinfo->_include.begin(), eqtlinfo->_include.end());
cout<<eqtlinfo->_include.size()<<" probes are extracted from ["+problstName+"]."<<endl;
}
void update_ldata(lociData* ldata)
{
long ldata_snpNum = ldata->_include.size();
vector<int> chr_buf, bp_buf;
vector<string> rs_buf;
for (int i = 0; i < ldata_snpNum; i++)
{
chr_buf.push_back(ldata->_chr[ldata->_include[i]]);
bp_buf.push_back(ldata->_bp[ldata->_include[i]]);
rs_buf.push_back(ldata->_snp_name[ldata->_include[i]]);
}
ldata->_chr.clear();
ldata->_bp.clear();
ldata->_snp_name.clear();
ldata->_chr.swap(chr_buf);
ldata->_bp.swap(bp_buf);
ldata->_snp_name.swap(rs_buf);
ldata->_include.clear();
for (int i = 0; i < ldata_snpNum; i++)
{
ldata->_include.push_back(i);
}
}
void exclude_eqtl_snp_opera(eqtlInfo* eqtlinfo, string snplstName)
{
vector<string> snplist,diffsnps;
vector<string> mapstr;
vector<int> tmp, incldidx;
string msg = "SNPs";
read_msglist(snplstName, snplist, msg);
int pre_num=eqtlinfo->_esi_include.size();
mapstr.resize(pre_num);
tmp.resize(pre_num);
for(int i=0;i<pre_num;i++)
{
mapstr[i]=eqtlinfo->_esi_rs[eqtlinfo->_esi_include[i]];
tmp[i]=eqtlinfo->_esi_include[i];
}
set_difference(mapstr, snplist, diffsnps);
match_only(diffsnps, mapstr, incldidx);
eqtlinfo->_esi_include.clear();
for(int i=0;i<incldidx.size();i++) {
eqtlinfo->_esi_include.push_back(tmp[incldidx[i]]);
}
//StrFunc::set_complement(snplist, mapstr, tmp, eqtlinfo->_esi_include); //sorted
stable_sort(eqtlinfo->_esi_include.begin(), eqtlinfo->_esi_include.end());
cout << pre_num-eqtlinfo->_esi_include.size() << " SNPs are excluded from [" + snplstName + "] and there are " << eqtlinfo->_esi_include.size() << " SNPs remaining." << endl;
}
// assume esdata->_esi_include.size() == esdata->_esi_rs.size();
long fill_smr_wk_new(bInfo* bdata,gwasData* gdata,eqtlInfo* esdata,SMRWK* smrwk, const char* refSNP,int cis_itvl,bool heidioffFlag)
{
int i=smrwk->cur_prbidx;
long maxid =-9;
if(esdata->_rowid.empty())
{
for (int j = 0; j<esdata->_esi_include.size() ; j++)// bdata._include.size() == esdata._esi_include.size() == gdata._include.size()
{
if (fabs(esdata->_bxz[i][j] + 9) > 1e-6)
{
int snpbp=esdata->_esi_bp[j];
int snpchr=esdata->_esi_chr[j];
if(snpchr==esdata->_epi_chr[i] && ABS(esdata->_epi_bp[i]-snpbp)<=cis_itvl && gdata->seyz[gdata->_include[j]]+9>1e-6)
{
if(esdata->_epi_start.size()>0 && esdata->_epi_end.size()>0) //technical eQTLs should be removed
{
if(esdata->_epi_end[i]==-9 || (snpbp>esdata->_epi_end[i] && snpbp<esdata->_epi_start[i]))
{
smrwk->bxz.push_back(esdata->_bxz[i][j]);
smrwk->sexz.push_back(esdata->_sexz[i][j]);
smrwk->zxz.push_back(esdata->_bxz[i][j]/esdata->_sexz[i][j]);
smrwk->byz.push_back(gdata->byz[gdata->_include[j]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[j]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[j]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[j]]);
smrwk->curId.push_back(j);
smrwk->rs.push_back(esdata->_esi_rs[j]);
smrwk->snpchrom.push_back(esdata->_esi_chr[j]);
smrwk->allele1.push_back(esdata->_esi_allele1[j]);
smrwk->allele2.push_back(esdata->_esi_allele2[j]);
if(refSNP!=NULL && esdata->_esi_rs[j]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[j]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[j]] / 2); //for bdata, _include should be used with j, for others, fine.
else smrwk->freq.push_back(esdata->_esi_freq[j]);
} else {
printf("Shown below is the technical eQTL and will be excluded from the analysis.\n");
double z=(esdata->_bxz[i][j]/esdata->_sexz[i][j]);
double p=pchisq(z*z, 1);
string tmp=atos(esdata->_esi_rs[j])+"\t"+ atos(esdata->_esi_chr[j])+"\t"+ atos(esdata->_esi_bp[j])+"\t"+ atos(esdata->_esi_allele1[j])+"\t"+ atos(esdata->_esi_allele2[j])+"\t"+ atos(esdata->_esi_freq[j])+"\t"+ atos(esdata->_epi_prbID[i])+"\t"+ atos(esdata->_epi_chr[i])+"\t"+ atos(esdata->_epi_bp[i])+"\t" + atos(esdata->_epi_gene[i])+"\t"+ atos(esdata->_epi_orien[i])+"\t"+ atos(esdata->_bxz[i][j])+"\t"+ atos(esdata->_sexz[i][j])+"\t"+ dtos(p)+"\n";
printf("%s\n",tmp.c_str());
}
} else {
smrwk->bxz.push_back(esdata->_bxz[i][j]);
smrwk->sexz.push_back(esdata->_sexz[i][j]);
smrwk->zxz.push_back(esdata->_bxz[i][j]/esdata->_sexz[i][j]);
smrwk->byz.push_back(gdata->byz[gdata->_include[j]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[j]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[j]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[j]]);
smrwk->curId.push_back(j);
smrwk->rs.push_back(esdata->_esi_rs[j]);
smrwk->snpchrom.push_back(esdata->_esi_chr[j]);
smrwk->allele1.push_back(esdata->_esi_allele1[j]);
smrwk->allele2.push_back(esdata->_esi_allele2[j]);
if(refSNP!=NULL && esdata->_esi_rs[j]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[j]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[j]] / 2); //for bdata, _include should be used with j, for others, fine.
else smrwk->freq.push_back(esdata->_esi_freq[j]);
}
}
}
}
}
else{
uint64_t beta_start=esdata->_cols[i<<1];
uint64_t se_start=esdata->_cols[1+(i<<1)];
uint64_t numsnps=se_start-beta_start;
for(int j=0;j<numsnps;j++)
{
int ge_rowid=esdata->_rowid[beta_start+j];
int snpbp=esdata->_esi_bp[ge_rowid];
int snpchr=esdata->_esi_chr[ge_rowid];
if(snpchr==esdata->_epi_chr[i] && abs(esdata->_epi_bp[i]-snpbp)<=cis_itvl && gdata->seyz[gdata->_include[ge_rowid]]+9>1e-6)
{
if(esdata->_epi_start.size()>0 && esdata->_epi_end.size()>0)
{
if(esdata->_epi_end[i]==-9 || (snpbp>esdata->_epi_end[i] && snpbp<esdata->_epi_start[i]))
{
smrwk->bxz.push_back(esdata->_val[beta_start+j]);
smrwk->sexz.push_back(esdata->_val[se_start+j]);
smrwk->zxz.push_back(esdata->_val[beta_start+j]/esdata->_val[se_start+j]);
smrwk->byz.push_back(gdata->byz[gdata->_include[ge_rowid]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[ge_rowid]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[ge_rowid]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[ge_rowid]]);
smrwk->curId.push_back(ge_rowid); //save snp id of the raw datastruct
smrwk->rs.push_back(esdata->_esi_rs[ge_rowid]);
smrwk->snpchrom.push_back(esdata->_esi_chr[ge_rowid]);
smrwk->allele1.push_back(esdata->_esi_allele1[ge_rowid]);
smrwk->allele2.push_back(esdata->_esi_allele2[ge_rowid]);
if(refSNP!=NULL && esdata->_esi_rs[ge_rowid]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[ge_rowid]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[ge_rowid]] / 2);
else smrwk->freq.push_back(esdata->_esi_freq[ge_rowid]);
} else {
printf("Shown below is the technical eQTL and will be excluded from the analysis.\n");
double z=(esdata->_bxz[i][j]/esdata->_sexz[i][j]);
double p=pchisq(z*z, 1);
string tmp=atos(esdata->_esi_rs[j])+"\t"+ atos(esdata->_esi_chr[j])+"\t"+ atos(esdata->_esi_bp[j])+"\t"+ atos(esdata->_esi_allele1[j])+"\t"+ atos(esdata->_esi_allele2[j])+"\t"+ atos(esdata->_esi_freq[j])+"\t"+ atos(esdata->_epi_prbID[i])+"\t"+ atos(esdata->_epi_chr[i])+"\t"+ atos(esdata->_epi_bp[i])+"\t" + atos(esdata->_epi_gene[i])+"\t"+ atos(esdata->_epi_orien[i])+"\t"+ atos(esdata->_bxz[i][j])+"\t"+ atos(esdata->_sexz[i][j])+"\t"+ dtos(p)+"\n";
printf("%s\n",tmp.c_str());
}
} else {
smrwk->bxz.push_back(esdata->_val[beta_start+j]);
smrwk->sexz.push_back(esdata->_val[se_start+j]);
smrwk->zxz.push_back(esdata->_val[beta_start+j]/esdata->_val[se_start+j]);
smrwk->byz.push_back(gdata->byz[gdata->_include[ge_rowid]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[ge_rowid]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[ge_rowid]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[ge_rowid]]);
smrwk->curId.push_back(ge_rowid); //save snp id of the raw datastruct
smrwk->rs.push_back(esdata->_esi_rs[ge_rowid]);
smrwk->snpchrom.push_back(esdata->_esi_chr[ge_rowid]);
smrwk->allele1.push_back(esdata->_esi_allele1[ge_rowid]);
smrwk->allele2.push_back(esdata->_esi_allele2[ge_rowid]);
if(refSNP!=NULL && esdata->_esi_rs[ge_rowid]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[ge_rowid]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[ge_rowid]] / 2);
else smrwk->freq.push_back(esdata->_esi_freq[ge_rowid]);
}
}
}
}
return maxid;
}
// extract SNPs based on esdata->_esi_include.size();
long fill_smr_wk_include(bInfo* bdata,gwasData* gdata,eqtlInfo* esdata,SMRWK* smrwk, const char* refSNP,int cis_itvl,bool heidioffFlag)
{
int i=smrwk->cur_prbidx;
long maxid =-9;
if(esdata->_rowid.empty())
{
for (int j = 0; j<esdata->_esi_include.size(); j++)// bdata._include.size() == esdata._esi_include.size() == gdata._include.size()
{
if (fabs(esdata->_sexz[i][esdata->_esi_include[j]] + 9) > 1e-6)
{
int snpbp=esdata->_esi_bp[esdata->_esi_include[j]];
int snpchr=esdata->_esi_chr[esdata->_esi_include[j]];
if(snpchr==esdata->_epi_chr[i] && ABS(esdata->_epi_bp[i]-snpbp)<=cis_itvl && gdata->seyz[gdata->_include[j]]+9>1e-6)
{
if(esdata->_epi_start.size()>0 && esdata->_epi_end.size()>0) //technical eQTLs should be removed
{
if(esdata->_epi_end[i]==-9 || (snpbp>esdata->_epi_end[i] && snpbp<esdata->_epi_start[i]))
{
smrwk->bxz.push_back(esdata->_bxz[i][esdata->_esi_include[j]]);
smrwk->sexz.push_back(esdata->_sexz[i][esdata->_esi_include[j]]);
smrwk->zxz.push_back(esdata->_bxz[i][esdata->_esi_include[j]]/esdata->_sexz[i][esdata->_esi_include[j]]);
smrwk->byz.push_back(gdata->byz[gdata->_include[j]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[j]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[j]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[j]]);
smrwk->curId.push_back(esdata->_esi_include[j]);
smrwk->rs.push_back(esdata->_esi_rs[esdata->_esi_include[j]]);
smrwk->snpchrom.push_back(esdata->_esi_chr[esdata->_esi_include[j]]);
smrwk->allele1.push_back(esdata->_esi_allele1[esdata->_esi_include[j]]);
smrwk->allele2.push_back(esdata->_esi_allele2[esdata->_esi_include[j]]);
if(refSNP!=NULL && esdata->_esi_rs[esdata->_esi_include[j]]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[esdata->_esi_include[j]]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[j]] / 2); //for bdata, _include should be used with j, for others, fine.
else smrwk->freq.push_back(esdata->_esi_freq[esdata->_esi_include[j]]);
} else {
printf("Shown below is the technical eQTL and will be excluded from the analysis.\n");
double z=(esdata->_bxz[i][esdata->_esi_include[j]]/esdata->_sexz[i][esdata->_esi_include[j]]);
double p=pchisq(z*z, 1);
string tmp=atos(esdata->_esi_rs[esdata->_esi_include[j]])+"\t"+ atos(esdata->_esi_chr[esdata->_esi_include[j]])+"\t"+ atos(esdata->_esi_bp[esdata->_esi_include[j]])+"\t"+ atos(esdata->_esi_allele1[esdata->_esi_include[j]])+"\t"+ atos(esdata->_esi_allele2[esdata->_esi_include[j]])+"\t"+ atos(esdata->_esi_freq[esdata->_esi_include[j]])+"\t"+ atos(esdata->_epi_prbID[i])+"\t"+ atos(esdata->_epi_chr[i])+"\t"+ atos(esdata->_epi_bp[i])+"\t" + atos(esdata->_epi_gene[i])+"\t"+ atos(esdata->_epi_orien[i])+"\t"+ atos(esdata->_bxz[i][esdata->_esi_include[j]])+"\t"+ atos(esdata->_sexz[i][esdata->_esi_include[j]])+"\t"+ dtos(p)+"\n";
printf("%s\n",tmp.c_str());
}
} else {
smrwk->bxz.push_back(esdata->_bxz[i][esdata->_esi_include[j]]);
smrwk->sexz.push_back(esdata->_sexz[i][esdata->_esi_include[j]]);
smrwk->zxz.push_back(esdata->_bxz[i][esdata->_esi_include[j]]/esdata->_sexz[i][esdata->_esi_include[j]]);
smrwk->byz.push_back(gdata->byz[gdata->_include[j]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[j]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[j]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[j]]);
smrwk->curId.push_back(esdata->_esi_include[j]);
smrwk->rs.push_back(esdata->_esi_rs[esdata->_esi_include[j]]);
smrwk->snpchrom.push_back(esdata->_esi_chr[esdata->_esi_include[j]]);
smrwk->allele1.push_back(esdata->_esi_allele1[esdata->_esi_include[j]]);
smrwk->allele2.push_back(esdata->_esi_allele2[esdata->_esi_include[j]]);
if(refSNP!=NULL && esdata->_esi_rs[esdata->_esi_include[j]]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[esdata->_esi_include[j]]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[bdata->_include[j]] / 2); //for bdata, _include should be used with j, for others, fine.
else smrwk->freq.push_back(esdata->_esi_freq[esdata->_esi_include[j]]);
}
}
}
}
}
else{
uint64_t beta_start=esdata->_cols[i<<1];
uint64_t se_start=esdata->_cols[1+(i<<1)];
uint64_t numsnps=se_start-beta_start;
vector<int> ge_rowid_common, ge_rowid_tmp, val_idx;
for(int k=0; k<numsnps; k++) ge_rowid_tmp.push_back(esdata->_rowid[beta_start+k]);
set_intersect(ge_rowid_tmp,esdata->_esi_include,ge_rowid_common);
numsnps = ge_rowid_common.size();
match_only(ge_rowid_common,ge_rowid_tmp,val_idx);
for(int j=0;j<numsnps;j++)
{
int ge_rowid=ge_rowid_common[j];
int snpbp=esdata->_esi_bp[ge_rowid];
int snpchr=esdata->_esi_chr[ge_rowid];
if(snpchr==esdata->_epi_chr[i] && abs(esdata->_epi_bp[i]-snpbp)<=cis_itvl && gdata->seyz[ge_rowid]+9>1e-6)
{
if(esdata->_epi_start.size()>0 && esdata->_epi_end.size()>0)
{
if(esdata->_epi_end[i]==-9 || (snpbp>esdata->_epi_end[i] && snpbp<esdata->_epi_start[i]))
{
smrwk->bxz.push_back(esdata->_val[beta_start+val_idx[j]]);
smrwk->sexz.push_back(esdata->_val[se_start+val_idx[j]]);
smrwk->zxz.push_back(esdata->_val[beta_start+val_idx[j]]/esdata->_val[se_start+val_idx[j]]);
smrwk->byz.push_back(gdata->byz[ge_rowid]);
smrwk->seyz.push_back(gdata->seyz[ge_rowid]);
smrwk->pyz.push_back(gdata->pvalue[ge_rowid]);
smrwk->splSize.push_back(gdata->splSize[ge_rowid]);
smrwk->curId.push_back(ge_rowid); //save snp id of the raw datastruct
smrwk->rs.push_back(esdata->_esi_rs[ge_rowid]);
smrwk->snpchrom.push_back(esdata->_esi_chr[ge_rowid]);
smrwk->allele1.push_back(esdata->_esi_allele1[ge_rowid]);
smrwk->allele2.push_back(esdata->_esi_allele2[ge_rowid]);
if(refSNP!=NULL && esdata->_esi_rs[ge_rowid]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[ge_rowid]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[ge_rowid] / 2);
else smrwk->freq.push_back(esdata->_esi_freq[ge_rowid]);
} else {
printf("Shown below is the technical eQTL and will be excluded from the analysis.\n");
double z=(esdata->_bxz[i][ge_rowid]/esdata->_sexz[i][ge_rowid]);
double p=pchisq(z*z, 1);
string tmp=atos(esdata->_esi_rs[ge_rowid])+"\t"+ atos(esdata->_esi_chr[ge_rowid])+"\t"+ atos(esdata->_esi_bp[ge_rowid])+"\t"+ atos(esdata->_esi_allele1[ge_rowid])+"\t"+ atos(esdata->_esi_allele2[ge_rowid])+"\t"+ atos(esdata->_esi_freq[ge_rowid])+"\t"+ atos(esdata->_epi_prbID[i])+"\t"+ atos(esdata->_epi_chr[i])+"\t"+ atos(esdata->_epi_bp[i])+"\t" + atos(esdata->_epi_gene[i])+"\t"+ atos(esdata->_epi_orien[i])+"\t"+ atos(esdata->_bxz[i][ge_rowid])+"\t"+ atos(esdata->_sexz[i][ge_rowid])+"\t"+ dtos(p)+"\n";
printf("%s\n",tmp.c_str());
}
} else {
smrwk->bxz.push_back(esdata->_val[beta_start+val_idx[j]]);
smrwk->sexz.push_back(esdata->_val[se_start+val_idx[j]]);
smrwk->zxz.push_back(esdata->_val[beta_start+val_idx[j]]/esdata->_val[se_start+val_idx[j]]);
smrwk->byz.push_back(gdata->byz[ge_rowid]);
smrwk->seyz.push_back(gdata->seyz[ge_rowid]);
smrwk->pyz.push_back(gdata->pvalue[ge_rowid]);
smrwk->splSize.push_back(gdata->splSize[ge_rowid]);
smrwk->curId.push_back(ge_rowid); //save snp id of the raw datastruct
smrwk->rs.push_back(esdata->_esi_rs[ge_rowid]);
smrwk->snpchrom.push_back(esdata->_esi_chr[ge_rowid]);
smrwk->allele1.push_back(esdata->_esi_allele1[ge_rowid]);
smrwk->allele2.push_back(esdata->_esi_allele2[ge_rowid]);
if(refSNP!=NULL && esdata->_esi_rs[ge_rowid]==string(refSNP)) maxid=(smrwk->rs.size()-1);
smrwk->bpsnp.push_back(esdata->_esi_bp[ge_rowid]);
if(!heidioffFlag) smrwk->freq.push_back(bdata->_mu[ge_rowid] / 2);
else smrwk->freq.push_back(esdata->_esi_freq[ge_rowid]);
}
}
}
}
return maxid;
}
// allow _include; not used for multi_heidi_func
long fill_smr_wk_mlt_include(bInfo* bdata,gwasData* gdata,vector<eqtlInfo> &esdata,MTSMRWKEXP* smrwk,const char* refSNP,int cis_itvl,bool heidioffFlag)
{
int i=smrwk->cur_prbidx;
long maxid =-9;
long outcoNum=esdata.size();
// find the exposure Num with smallest number of cis-SNPs
int t_min = 0;
if(esdata[0]._rowid.empty())
{
long num_tmp = esdata[0]._bxz[i].size();
for(int t=0; t<outcoNum; t++) {
if(esdata[t]._bxz[i].size() < num_tmp) {
num_tmp = esdata[t]._bxz[i].size();
t_min = t;
}
}
for (int j = 0; j<esdata[t_min]._esi_include.size() ; j++)// bdata._include.size() == esdata._esi_include.size() == gdata._include.size()
{
if (fabs(esdata[t_min]._bxz[i][esdata[t_min]._esi_include[j]] + 9) > 1e-6)
{
int snpbp=esdata[t_min]._esi_bp[esdata[t_min]._esi_include[j]];
int snpchr=esdata[t_min]._esi_chr[esdata[t_min]._esi_include[j]];
if(snpchr==esdata[t_min]._epi_chr[i] && ABS(esdata[t_min]._epi_bp[i]-snpbp)<=cis_itvl && gdata->seyz[gdata->_include[j]]+9>1e-6)
{
if(esdata[t_min]._epi_start.size()>0 && esdata[t_min]._epi_end.size()>0) //technical eQTLs should be removed
{
if(esdata[t_min]._epi_end[i]==-9 || (snpbp>esdata[t_min]._epi_end[i] && snpbp<esdata[t_min]._epi_start[i]))
{
for( int t=0; t<outcoNum; t++)
{
smrwk->bxz[t].push_back(esdata[t]._bxz[i][esdata[t_min]._esi_include[j]]);
smrwk->sexz[t].push_back(esdata[t]._sexz[i][esdata[t_min]._esi_include[j]]);
smrwk->zxz[t].push_back(esdata[t]._bxz[i][esdata[t_min]._esi_include[j]]/esdata[t]._sexz[i][esdata[t_min]._esi_include[j]]);
if(!heidioffFlag) smrwk->freq[t].push_back(bdata->_mu[bdata->_include[j]] / 2); //for bdata, _include should be used with j, for others, fine.
else smrwk->freq[t].push_back(esdata[t]._esi_freq[esdata[t_min]._esi_include[j]]);
}
smrwk->byz.push_back(gdata->byz[gdata->_include[j]]);
smrwk->seyz.push_back(gdata->seyz[gdata->_include[j]]);
smrwk->pyz.push_back(gdata->pvalue[gdata->_include[j]]);
smrwk->splSize.push_back(gdata->splSize[gdata->_include[j]]);
smrwk->curId.push_back(esdata[t_min]._esi_include[j]);
smrwk->rs.push_back(esdata[t_min]._esi_rs[esdata[t_min]._esi_include[j]]);
smrwk->snpchrom.push_back(esdata[t_min]._esi_chr[esdata[t_min]._esi_include[j]]);