-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSMR_data_p1.cpp
4814 lines (4389 loc) · 211 KB
/
SMR_data_p1.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
//
// SMR_data_p1.cpp
// SMR_CPP
//
// Created by Futao Zhang on 10/03/2016.
// Copyright (c) 2016 Futao Zhang. All rights reserved.
//
#include "SMR_data_p1.h"
namespace SMRDATA
{
void get_top_sets(eqtlInfo* eqtlinfo, vector<string> &prbIds, vector<float> &beta, vector<float> &se, vector<string> &rs, float thres)
{
if(eqtlinfo->_rowid.empty())
{
for(int i=0;i<eqtlinfo->_include.size();i++)
{
string probeId=eqtlinfo->_epi_prbID[eqtlinfo->_include[i]];
int probechr=eqtlinfo->_epi_chr[eqtlinfo->_include[i]];
double top_zsqr=0, top_beta=0, top_se=0;
string snprs="";
for (int j = 0; j<eqtlinfo->_esi_include.size(); j++)
{
if (fabs(eqtlinfo->_bxz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]] + 9) > 1e-6)
{
int snpchr=eqtlinfo->_esi_chr[eqtlinfo->_esi_include[j]];
string rstmp=eqtlinfo->_esi_rs[eqtlinfo->_esi_include[j]];
if(snpchr==probechr)
{
float bxz=eqtlinfo->_bxz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]];
float sexz=eqtlinfo->_sexz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]];
float zxz=bxz/sexz;
zxz*=zxz;
if(zxz-top_zsqr>1e-8) {
top_zsqr=zxz;
top_beta=bxz;
top_se=sexz;
snprs=rstmp;
}
}
}
}
double pval=pchisq(top_zsqr, 1);
if(pval<thres)
{
prbIds.push_back(probeId);
beta.push_back(top_beta);
se.push_back(top_se);
rs.push_back(snprs);
}
}
}
else
{
for(int i=0;i<eqtlinfo->_include.size();i++)
{
string probeId=eqtlinfo->_epi_prbID[eqtlinfo->_include[i]];
int probechr=eqtlinfo->_epi_chr[eqtlinfo->_include[i]];
double top_zsqr=0, top_beta=0, top_se=0;
string snprs="";
uint64_t beta_start=eqtlinfo->_cols[eqtlinfo->_include[i]<<1];
uint64_t se_start=eqtlinfo->_cols[1+(eqtlinfo->_include[i]<<1)];
uint64_t numsnps=se_start-beta_start;
for(int j=0;j<numsnps;j++)
{
int ge_rowid=eqtlinfo->_rowid[beta_start+j];
int snpchr=eqtlinfo->_esi_chr[ge_rowid];
string snptmp=eqtlinfo->_esi_rs[ge_rowid];
if(snpchr==probechr )
{
float bxz=eqtlinfo->_val[beta_start+j];
float sexz=eqtlinfo->_val[se_start+j];
float zxz=bxz/sexz;
zxz*=zxz;
if(zxz-top_zsqr>1e-8) {
top_zsqr=zxz;
top_beta=bxz;
top_se=sexz;
snprs=snptmp;
}
}
}
double pval=pchisq(top_zsqr, 1);
if(pval<thres)
{
prbIds.push_back(probeId);
beta.push_back(top_beta);
se.push_back(top_se);
rs.push_back(snprs);
}
}
}
}
void get_thres_sets(eqtlInfo* eqtlinfo, vector<string> &prbIds, vector<float> &beta, vector<float> &se, vector<string> &rs, float thres)
{
if(eqtlinfo->_rowid.empty())
{
for(int i=0;i<eqtlinfo->_include.size();i++)
{
string probeId=eqtlinfo->_epi_prbID[eqtlinfo->_include[i]];
int probechr=eqtlinfo->_epi_chr[eqtlinfo->_include[i]];
for (int j = 0; j<eqtlinfo->_esi_include.size(); j++)
{
if (fabs(eqtlinfo->_bxz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]] + 9) > 1e-6)
{
int snpchr=eqtlinfo->_esi_chr[eqtlinfo->_esi_include[j]];
string rstmp=eqtlinfo->_esi_rs[eqtlinfo->_esi_include[j]];
if(snpchr==probechr)
{
float bxz=eqtlinfo->_bxz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]];
float sexz=eqtlinfo->_sexz[eqtlinfo->_include[i]][eqtlinfo->_esi_include[j]];
float zxz=bxz/sexz;
zxz*=zxz;
double pval=pchisq(zxz, 1);
if(pval<thres) {
prbIds.push_back(probeId);
beta.push_back(bxz);
se.push_back(sexz);
rs.push_back(rstmp);
}
}
}
}
}
}
else
{
for(int i=0;i<eqtlinfo->_include.size();i++)
{
string probeId=eqtlinfo->_epi_prbID[eqtlinfo->_include[i]];
int probechr=eqtlinfo->_epi_chr[eqtlinfo->_include[i]];
uint64_t beta_start=eqtlinfo->_cols[eqtlinfo->_include[i]<<1];
uint64_t se_start=eqtlinfo->_cols[1+(eqtlinfo->_include[i]<<1)];
uint64_t numsnps=se_start-beta_start;
for(int j=0;j<numsnps;j++)
{
int ge_rowid=eqtlinfo->_rowid[beta_start+j];
int snpchr=eqtlinfo->_esi_chr[ge_rowid];
string snptmp=eqtlinfo->_esi_rs[ge_rowid];
if(snpchr==probechr )
{
float bxz=eqtlinfo->_val[beta_start+j];
float sexz=eqtlinfo->_val[se_start+j];
float zxz=bxz/sexz;
zxz*=zxz;
double pval=pchisq(zxz, 1);
if(pval<thres) {
prbIds.push_back(probeId);
beta.push_back(bxz);
se.push_back(sexz);
rs.push_back(snptmp);
}
}
}
}
}
}
long est_n(vector<float> &beta,vector<float> &se)
{
long n;
VectorXd Y(se.size());
MatrixXd X(beta.size(),2);
for(int i=0;i<se.size();i++)
{
Y(i)=se[i]*se[i];
X(i,0)=1.0;
X(i,1)=beta[i]*beta[i]-Y(i);
}
MatrixXd XtX_i=(X.transpose()*X).inverse();
VectorXd w_hat=XtX_i*X.transpose()*Y;
n=-1/w_hat[1];
return n;
}
void est_effect_splsize(char* eqtlsmaslstName, char* eqtlFileName, char* snplstName,char* problstName,char* snplst2exclde, char* problst2exclde,float thres)
{
vector<string> prbIds;
vector<float> beta;
vector<float> se;
vector<string> rs;
vector<string> smasNames;
if(eqtlsmaslstName!=NULL)
{
read_smaslist(smasNames, string(eqtlsmaslstName));
if(smasNames.size()==0) throw("No eqtl summary file list in [ "+ string(eqtlsmaslstName) +" ]");
for(int ii=0;ii<smasNames.size();ii++)
{
eqtlInfo eqtlinfo;
read_esifile(&eqtlinfo, smasNames[ii]+".esi");
if (snplstName != NULL) extract_eqtl_snp(&eqtlinfo, snplstName);
if(snplst2exclde != NULL) exclude_eqtl_snp(&eqtlinfo, snplst2exclde);
read_epifile(&eqtlinfo, smasNames[ii]+".epi");
if(problstName != NULL) extract_prob(&eqtlinfo, problstName);
if(problst2exclde != NULL) exclude_prob(&eqtlinfo, problst2exclde);
read_besdfile(&eqtlinfo, smasNames[ii]+".besd");
if(eqtlinfo._rowid.empty() && eqtlinfo._bxz.empty())
{
printf("No data included from %s in the analysis.\n",smasNames[ii].c_str());
exit(EXIT_FAILURE);
}
//get_top_sets(&eqtlinfo,prbIds,beta,se,rs,thres);
get_thres_sets(&eqtlinfo,prbIds,beta,se,rs,thres);
}
} else if(eqtlFileName!=NULL)
{
eqtlInfo eqtlinfo;
read_esifile(&eqtlinfo, string(eqtlFileName)+".esi");
if (snplstName != NULL) extract_eqtl_snp(&eqtlinfo, snplstName);
if(snplst2exclde != NULL) exclude_eqtl_snp(&eqtlinfo, snplst2exclde);
read_epifile(&eqtlinfo, string(eqtlFileName)+".epi");
if(problstName != NULL) extract_prob(&eqtlinfo, problstName);
if(problst2exclde != NULL) exclude_prob(&eqtlinfo, problst2exclde);
read_besdfile(&eqtlinfo, string(eqtlFileName)+".besd");
if(eqtlinfo._rowid.empty() && eqtlinfo._bxz.empty())
{
printf("No data included from %s in the analysis.\n",eqtlFileName);
exit(EXIT_FAILURE);
}
//get_top_sets(&eqtlinfo,prbIds,beta,se,rs,thres);
get_thres_sets(&eqtlinfo,prbIds,beta,se,rs,thres);
}else {
throw("Error: please input eQTL summary data files list or eQTL summary data by the flag --beqtl-summaries or --beqtl-summary.");
}
cout<<prbIds.size()<<" eQTLs are inluded to estimate the effective population size."<<endl;
///////
string tstfile = "tst.top.bse.txt";
ofstream tst(tstfile.c_str());
if (!tst) throw ("Error: can not open the fam file " + tstfile + " to save!");
tst << "Probe" <<'\t'<<"SNP"<<'\t'<< "b" <<'\t' << "se"<<'\n';
for (int i = 0;i <beta.size(); i++) {
tst<<prbIds[i]<<'\t'<<rs[i]<<'\t'<<beta[i]<<'\t'<<se[i]<< '\n';
}
tst.close();
//////
long n=est_n(beta,se);
cout<<"The estimated effective population size is: "<<n<<endl;
}
void read_esmr(eSMRrlt* erlt, string smrFileName)
{
cout << "Reading SMR result information from [" + smrFileName + "]." << endl;
ifstream flptr;
flptr.open(smrFileName.c_str());
if (!flptr) throw ("Error: can not open the file [" + smrFileName + "] to read.");
char tbuf[MAX_LINE_SIZE];
int lineNum(0);
flptr.getline(tbuf,MAX_LINE_SIZE);// the header
while(!flptr.eof())
{
string tmpStr;
flptr.getline(tbuf,MAX_LINE_SIZE);
if(tbuf[0]!='\0'){
istringstream iss(tbuf);
iss>>tmpStr;
erlt->_Expo_id.push_back(tmpStr);
iss>>tmpStr;
erlt->_Expo_chr.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
erlt->_Expo_gene.push_back(tmpStr);
iss>>tmpStr;
erlt->_Expo_bp.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
erlt->_Outco_id.push_back(tmpStr);
iss>>tmpStr;
erlt->_Outco_chr.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
erlt->_Outco_gene.push_back(tmpStr);
iss>>tmpStr;
erlt->_Outco_bp.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
erlt->_snp_rs.push_back(tmpStr);
iss>>tmpStr;
erlt->_snp_chr.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
erlt->_snp_bp.push_back(atoi(tmpStr.c_str()));
iss>>tmpStr;
to_upper(tmpStr);
erlt->_snp_a1.push_back(tmpStr.c_str());
iss>>tmpStr;
to_upper(tmpStr);
erlt->_snp_a2.push_back(tmpStr.c_str());
iss>>tmpStr;
erlt->_snp_frq.push_back(atof(tmpStr.c_str()));
iss>>tmpStr; // b_outcome
iss>>tmpStr; // se_outcome
iss>>tmpStr; // p_outcome
iss>>tmpStr; // b_exposure
iss>>tmpStr; // se_exposure
iss>>tmpStr; // p_exposure
iss>>tmpStr;
erlt->_b.push_back(atof(tmpStr.c_str()));
iss>>tmpStr;
erlt->_se.push_back(atof(tmpStr.c_str()));
iss>>tmpStr;
erlt->_p_smr.push_back(atof(tmpStr.c_str()));
iss>>tmpStr;
if(!tmpStr.compare("NA")) erlt->_p_heidi.push_back(-9);
else erlt->_p_heidi.push_back(atof(tmpStr.c_str()));
iss>>tmpStr;
if(!tmpStr.compare("NA")) erlt->_nsnp.push_back(-9);
else erlt->_nsnp.push_back(atoi(tmpStr.c_str()));
erlt->_include.push_back(lineNum);
lineNum++;
}
}
erlt->lineNum=lineNum;
flptr.close();
cout << lineNum << " SNPs summary info to be included from [" + smrFileName + "]." << endl;
}
void read_probevarfile(eqtlInfo* eqtlinfo, string vpFileName)
{
cout << "Reading variance information from [" + vpFileName + "]." << endl;
ifstream flptr;
flptr.open(vpFileName.c_str());
if (!flptr) throw ("Error: can not open the file [" + vpFileName + "] to read.");
char tbuf[MAX_LINE_SIZE];
int lineNum(0);
vector<string> tmp_prbid;
vector<double> tmp_var;
while(!flptr.eof())
{
string tmpStr;
flptr.getline(tbuf,MAX_LINE_SIZE);
if(tbuf[0]!='\0'){
istringstream iss(tbuf);
iss>>tmpStr;
tmp_prbid.push_back(tmpStr.c_str());
iss>>tmpStr;
tmp_var.push_back(atof(tmpStr.c_str()));
lineNum++;
}
}
flptr.close();
eqtlinfo->_epi_var.resize(eqtlinfo->_epi_prbID.size());
vector<int> idx;
match_only(eqtlinfo->_epi_prbID, tmp_prbid, idx);
if(idx.size()!=eqtlinfo->_epi_prbID.size())
{
cout<<"Some Probes in summary data are not in variance data!"<<endl;
exit(1);
}
for(int i=0;i<idx.size();i++)
{
eqtlinfo->_epi_var[i]=tmp_var[idx[i]];
}
cout << idx.size() << " probes variance info to be included from [" + vpFileName + "]." << endl;
}
void iternal_test(char* outFileName, char* bFileName,char* eqtlFileName, char* eqtlFileName2, double maf,char* indilstName, char* snplstName,char* problstName, char* oproblstName,char* eproblstName,bool bFlag,double p_hetero,double ld_top,int m_hetero, int opt_hetero,char* indilst2remove, char* snplst2exclde, char* problst2exclde, char* oproblst2exclde,char* eproblst2exclde,double p_smr,int cis_itvl,char* smrFileName)
{
setNbThreads(thread_num);
eqtlInfo etrait;
eqtlInfo esdata;
bInfo bdata;
double threshold= chi_val(1,p_hetero);
cis_itvl=cis_itvl*1000;
if(eqtlFileName==NULL) throw("Error: please input eQTL summary data for SMR analysis by the flag --eqtl-summary.");
if(problstName != NULL) cout<<"WARNING: --extract-probe here presumes the probe list should contain both probes of exposure dataset and probes of outcome dataset.\n If you want to only extract probes from one dataset please include these probes in the file and all the probes of the other dataset as well.\n"<<endl;
read_esifile(&etrait, string(eqtlFileName)+".esi");
if (snplstName != NULL) extract_eqtl_snp(&etrait, snplstName);
if(snplst2exclde != NULL) exclude_eqtl_snp(&etrait, snplst2exclde);
read_epifile(&etrait, string(eqtlFileName)+".epi");
if(problstName != NULL) extract_prob(&etrait, problstName);
if(problst2exclde != NULL) exclude_prob(&etrait, problst2exclde);
if(oproblstName != NULL ) extract_prob(&etrait, oproblstName);
if(oproblst2exclde != NULL) exclude_prob(&etrait, oproblst2exclde);
read_besdfile(&etrait, string(eqtlFileName)+".besd");
if(etrait._rowid.empty() && etrait._bxz.empty())
{
printf("No data included from %s in the analysis.\n",eqtlFileName);
exit(EXIT_FAILURE);
}
read_esifile(&esdata, string(eqtlFileName2)+".esi");
if (snplstName != NULL) extract_eqtl_snp(&esdata, snplstName);
if(snplst2exclde != NULL) exclude_eqtl_snp(&esdata, snplst2exclde);
read_famfile(&bdata, string(bFileName)+".fam");
if(indilstName != NULL) keep_indi(&bdata,indilstName);
if(indilst2remove != NULL) remove_indi(&bdata, indilst2remove);
read_bimfile(&bdata, string(bFileName)+".bim");
if(snplstName != NULL) extract_snp(&bdata, snplstName);
if(snplst2exclde != NULL) exclude_snp(&bdata, snplst2exclde);
allele_check(&bdata, &etrait, &esdata);
// if no snp left after check
read_bedfile(&bdata, string(bFileName)+".bed");
if (bdata._mu.empty()) calcu_mu(&bdata);
if (maf > 0)
{
filter_snp_maf(&bdata, maf);
update_geIndx(&bdata, &etrait, &esdata);
}
cout<<"Reading eQTL summary data..."<<endl;
read_epifile(&esdata, string(eqtlFileName2)+".epi");
if(problstName != NULL) extract_prob(&esdata, problstName);
if(problst2exclde != NULL) exclude_prob(&esdata, problst2exclde);
if(eproblstName != NULL ) extract_prob(&esdata, eproblstName);
if(eproblst2exclde != NULL) exclude_prob(&esdata, eproblst2exclde);
read_besdfile(&esdata, string(eqtlFileName2)+".besd");
if(esdata._rowid.empty() && esdata._bxz.empty())
{
printf("No data included from %s in the analysis.\n",eqtlFileName2);
exit(EXIT_FAILURE);
}
for(int i=0;i<etrait._include.size();i++)
etrait._probe_name_map.insert(pair<string, int>(etrait._epi_prbID[etrait._include[i]],etrait._include[i]));
for(int i=0;i<esdata._include.size();i++)
esdata._probe_name_map.insert(pair<string, int>(esdata._epi_prbID[esdata._include[i]],esdata._include[i]));
cout<<endl<<"Performing interanl test..... "<<endl;
vector<long> out_probid;
vector<string> bxy;
vector<string> sexy;
vector<string> pxy;
vector<string> bgwas;
vector<string> segwas;
vector<string> beqtl;
vector<string> seeqtl;
vector<string> pgwas;
vector<string> peqtl;
vector<string> rsid;
vector<string> rschr;
vector<string> rsbp;
vector<string> rsfreq;
vector<string> rsa1;
vector<string> rsa2;
vector<string> prb1;
vector<string> nsnp_test1;
vector<string> etrait_id;
vector<string> etrait_bp;
vector<string> etrait_gene;
vector<string> etrait_chr;
eSMRrlt erlt;
read_esmr(&erlt,smrFileName);
float progr0=0.0 , progr1;
progress_print(progr0);
for( int lid=0;lid<erlt.lineNum;lid++)
{
progr1=1.0*lid/erlt.lineNum;
if(progr1-progr0-0.05>1e-6 || lid+1==erlt.lineNum)
{
if(lid+1==erlt.lineNum) progr1=1.0;
progress_print(progr1);
progr0=progr1;
}
string traitname=erlt._Outco_id[lid];
map<string, int>::iterator iter;
iter = etrait._probe_name_map.find(traitname);
if (iter == etrait._probe_name_map.end()) throw("Could not find probe "+traitname+" in "+eqtlFileName+" dataset, please check!");
uint32_t ii = iter->second;
gwasData gdata;
gdata.allele_1.resize(etrait._esi_include.size());
gdata.allele_2.resize(etrait._esi_include.size());
gdata.byz.resize(etrait._esi_include.size());
gdata.seyz.resize(etrait._esi_include.size());
gdata.freq.resize(etrait._esi_include.size());
gdata.pvalue.resize(etrait._esi_include.size());
gdata.splSize.resize(etrait._esi_include.size());
// cout<<"\nPerforming analysis of eTrait [ "+traitname+" ]..."<<endl;
gdata._include.clear();
gdata.snpName.clear();
int count=0;
if(etrait._rowid.empty())
{
for (int j = 0; j<etrait._esi_include.size(); j++)
{
if (fabs(etrait._bxz[ii][etrait._esi_include[j]] + 9) > 1e-6)
{
gdata.byz[count]=etrait._bxz[ii][etrait._esi_include[j]];
gdata.seyz[count]=etrait._sexz[ii][etrait._esi_include[j]];
gdata.snpName.push_back(etrait._esi_rs[etrait._esi_include[j]]);
gdata.allele_1[count]=etrait._esi_allele1[etrait._esi_include[j]];
gdata.allele_2[count]=etrait._esi_allele2[etrait._esi_include[j]];
gdata._include.push_back(etrait._esi_include[j]); // row id selected
count++;
}
}
}
else
{
uint64_t beta_start=etrait._cols[ii<<1];
uint64_t se_start=etrait._cols[1+(ii<<1)];
uint64_t numsnps=se_start-beta_start;
for(int j=0;j<numsnps;j++)
{
int ge_rowid=etrait._rowid[beta_start+j];
if(binary_search(etrait._esi_include.begin(), etrait._esi_include.end(), ge_rowid))
{
gdata.byz[count]=etrait._val[beta_start+j];
gdata.seyz[count]=etrait._val[se_start+j];
gdata.snpName.push_back(etrait._esi_rs[ge_rowid]);
gdata.allele_1[count]=etrait._esi_allele1[ge_rowid];
gdata.allele_2[count]=etrait._esi_allele2[ge_rowid];
gdata._include.push_back(ge_rowid);
count++;
}
}
}
if(gdata.snpName.size()< m_hetero)
{
cout<<gdata.snpNum<<" common SNPs (less than parameter m_hetero: "+atos(m_hetero)+" ) are included from eTrait [ "+traitname+" ] summary."<<endl;
continue;
}
gdata.snpNum=gdata.snpName.size();
//cout<<gdata.snpNum<<" common SNPs are included from eTrait [ "+traitname+" ] summary."<<endl;
vector<double> bxz;
vector<double> sexz;
vector<uint32_t> curId;
vector<string> eName;
vector<int> snpchrom;
vector<string> allele1;
vector<string> allele2;
vector<uint32_t> bpsnp;
vector<double> freq;
vector<double> byz;
vector<double> seyz;
VectorXd zsxz;
vector<int> sn_ids;
VectorXd _byz;
VectorXd _seyz;
VectorXd _bxz;
VectorXd _sexz;
VectorXd _zsxz;
MatrixXd _X;
VectorXd ld_v;
MatrixXd _LD_heidi;
MatrixXd _X_heidi;
string expo_name=erlt._Expo_id[lid];
string refSNP=erlt._snp_rs[lid];
map<string, int>::iterator iter1;
iter1 = esdata._probe_name_map.find(expo_name);
if (iter1 == esdata._probe_name_map.end())
{
cout<<"Warning: Could not find probe "+expo_name+" in "+eqtlFileName2+" dataset, please check!"<<endl;
continue;
}
int i = iter1->second;
//extract info from eqtl summary and gwas summary
bxz.clear();
sexz.clear();
curId.clear(); // is the idxes of bfile._include not the values of
eName.clear();
snpchrom.clear();
byz.clear();
seyz.clear();
allele1.clear();
allele2.clear();
bpsnp.clear();
freq.clear();
long maxid =-9;
int probebp=esdata._epi_bp[i];
int probechr=esdata._epi_chr[i];
if(esdata._rowid.empty())
{
for (int j = 0; j<esdata._esi_include.size(); j++)// bdata._include.size() == esdata._esi_include.size() == etrait._esi_include.size()
{
if (fabs(esdata._bxz[i][j] + 9) > 1e-6)
{
int snpbp=esdata._esi_bp[j];
int snpchr=esdata._esi_chr[j];
int etrait_rid=etrait._esi_include[j];
long pos=find(gdata._include.begin(), gdata._include.end(), etrait_rid)-gdata._include.begin();
if(snpchr==probechr && ABS(probebp-snpbp)<=cis_itvl && pos!=gdata._include.size())
{
bxz.push_back(esdata._bxz[i][j]);
sexz.push_back(esdata._sexz[i][j]);
byz.push_back(gdata.byz[pos]);
seyz.push_back(gdata.seyz[pos]);
curId.push_back(j);
eName.push_back(esdata._esi_rs[j]);
snpchrom.push_back(esdata._esi_chr[j]);
if(esdata._esi_rs[j]==refSNP) maxid=(eName.size()-1);
freq.push_back(bdata._mu[bdata._include[j]]/2);
allele1.push_back(esdata._esi_allele1[j]);
allele2.push_back(esdata._esi_allele2[j]);
bpsnp.push_back(esdata._esi_bp[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];
int etrait_rid=etrait._esi_include[ge_rowid];
long pos=find(gdata._include.begin(), gdata._include.end(), etrait_rid)-gdata._include.begin();
if(snpchr==probechr && ABS(probebp-snpbp)<=cis_itvl && pos!=gdata._include.size())
{
bxz.push_back(esdata._val[beta_start+j]);
sexz.push_back(esdata._val[se_start+j]);
byz.push_back(gdata.byz[pos]);
seyz.push_back(gdata.seyz[pos]);
curId.push_back(ge_rowid);
eName.push_back(esdata._esi_rs[ge_rowid]);
snpchrom.push_back(esdata._esi_chr[ge_rowid]);
if(esdata._esi_rs[ge_rowid]==refSNP) maxid=(eName.size()-1);
allele1.push_back(esdata._esi_allele1[ge_rowid]);
allele2.push_back(esdata._esi_allele2[ge_rowid]);
bpsnp.push_back(esdata._esi_bp[ge_rowid]);
freq.push_back(bdata._mu[bdata._include[ge_rowid]]/2);
}
}
}
if(maxid==-9) continue; //heidi SNP is not in selected SNPs
if (bxz.size() == 0) continue;
Map<VectorXd> ei_bxz(&bxz[0],bxz.size());
Map<VectorXd> ei_sexz(&sexz[0],sexz.size());
zsxz=ei_bxz.array()/ei_sexz.array();
double pxz_val = pchisq(zsxz[maxid]*zsxz[maxid], 1);
double bxy_val = byz[maxid] / bxz[maxid];
double sexy_val = sqrt((seyz[maxid] * seyz[maxid] * bxz[maxid] * bxz[maxid] + sexz[maxid] * sexz[maxid] * byz[maxid] * byz[maxid]) / (bxz[maxid] * bxz[maxid] * bxz[maxid] * bxz[maxid]));
double chisqxy = bxy_val*bxy_val / (sexy_val*sexy_val);
double pxy_val = pchisq(chisqxy, 1); // double pxy=chi_prob(1,chisqxy); //works
double chisqyz = byz[maxid] / seyz[maxid];
double pyz_val = pchisq(chisqyz*chisqyz, 1);
out_probid.push_back(i);
bxy.push_back(dtosf(bxy_val));
sexy.push_back(dtosf(sexy_val));
pxy.push_back(dtos(pxy_val));
bgwas.push_back(dtosf(byz[maxid]));
segwas.push_back(dtosf(seyz[maxid]));
beqtl.push_back(dtosf(bxz[maxid]));
seeqtl.push_back(dtosf(sexz[maxid]));
pgwas.push_back(dtos(pyz_val));
peqtl.push_back(dtos(pxz_val));
rsid.push_back(eName[maxid]);
rschr.push_back(atos(snpchrom[maxid]));
rsbp.push_back(itos(bpsnp[maxid]));
rsfreq.push_back(dtosf(freq[maxid]));
rsa1.push_back(allele1[maxid]);
rsa2.push_back(allele2[maxid]);
etrait_id.push_back(etrait._epi_prbID[ii]);
etrait_chr.push_back(atos(etrait._epi_chr[ii]));
etrait_gene.push_back(etrait._epi_gene[ii]);
etrait_bp.push_back(atos(etrait._epi_bp[ii]));
//extract info from reference
make_XMat(&bdata,curId, _X); //_X: one row one individual, one column one SNP
ld_calc_o2m(ld_v,maxid,_X);
sn_ids.clear(); //increase order
if(fabs(ld_top-1)<1e-6) get_square_idxes(sn_ids,zsxz,threshold);
else get_square_ldpruning_idxes(sn_ids,zsxz,threshold,ld_v, maxid,ld_top);
if(sn_ids.size() < m_hetero)
{
prb1.push_back(string("NA"));
nsnp_test1.push_back( string("NA"));
continue;
}
_byz.resize(sn_ids.size());
_seyz.resize(sn_ids.size());
_bxz.resize(sn_ids.size());
_sexz.resize(sn_ids.size());
_zsxz.resize(sn_ids.size());
_X_heidi.resize(_X.rows(), sn_ids.size());
#pragma omp parallel for
for(int j=0;j<sn_ids.size();j++)
{
_byz[j]=byz[sn_ids[j]];
_seyz[j]=seyz[sn_ids[j]];
_bxz[j]=bxz[sn_ids[j]];
_sexz[j]=sexz[sn_ids[j]];
_zsxz[j]=zsxz[sn_ids[j]];
_X_heidi.col(j)=_X.col(sn_ids[j]);
}
_X.resize(0,0);
cor_calc(_LD_heidi, _X_heidi);
_X_heidi.resize(0,0);
long nsnp = sn_ids.size();
double pdev=bxy_hetero3(_byz, _bxz, _seyz, _sexz, _zsxz, _LD_heidi, &nsnp);
prb1.push_back(dtos(pdev));
if(nsnp>0) nsnp_test1.push_back(itos(nsnp));
else nsnp_test1.push_back(string("NA"));
free_gwas_data( &gdata);
}
if(bxy.size()>0)
{
string smrfile = string(outFileName)+".smr";
ofstream smr(smrfile.c_str());
if (!smr) throw ("Error: can not open the file " + smrfile + " to save!");
smr << "Expo_ID" <<'\t'<< "Expo_Chr" <<'\t' << "Expo_Gene" << '\t' << "Expo_bp" << '\t'<< "Outco_ID" <<'\t'<< "Outco_Chr" <<'\t' << "Outco_Gene" << '\t' << "Outco_bp" << '\t'<< "SNP"<< '\t' << "SNP_Chr"<< '\t' << "SNP_bp"<< '\t' << "A1"<< '\t'<< "A2"<< '\t'<<"Freq"<<'\t'<<"b_Outco"<<'\t'<<"se_Outco"<<'\t'<< "p_Outco" << '\t'<<"b_Expo"<<'\t'<<"se_Expo"<<'\t'<< "p_Expo" << '\t'<< "b_SMR" << '\t'<< "se_SMR"<< '\t' << "p_SMR" << "\t"<< "p_HEIDI"<< "\t" << "nsnp" << '\n';
for (int i = 0;i <bxy.size(); i++) {
smr<<esdata._epi_prbID[out_probid[i]]<<'\t'<<esdata._epi_chr[out_probid[i]]<<'\t'<<esdata._epi_gene[out_probid[i]]<<'\t'<<esdata._epi_bp[out_probid[i]]<<'\t'<<etrait_id[i]<<'\t'<<etrait_chr[i]<<'\t'<<etrait_gene[i]<<'\t'<<etrait_bp[i]<<'\t'<<rsid[i]<<'\t'<<rschr[i]<<'\t'<<rsbp[i]<<'\t'<<rsa1[i]<<'\t'<<rsa2[i]<<'\t'<<rsfreq[i]<<'\t'<<bgwas[i]<<'\t'<<segwas[i]<<'\t'<<pgwas[i]<<'\t'<<beqtl[i]<<'\t'<<seeqtl[i]<<'\t'<<peqtl[i]<<'\t'<<bxy[i]<<'\t'<<sexy[i]<<'\t'<<pxy[i]<<'\t'<<prb1[i]<<'\t'<<nsnp_test1[i]<<'\n';
}
cout<<"Internal Test finished.\nInternal Test results of "<<bxy.size()<<" probes have been saved in the file [" + smrfile + "]."<<endl;
smr.close();
}else cout<<"Internal Test finished.\nInternal Test results of "<<bxy.size()<<" probes have been saved."<<endl;
}
void make_cojo(char* outFileName,char* eqtlFileName, char* snplstName,char* snplst2exclde, char* problstName, char* problst2exclde, char* genelistName, bool bFlag)
{
eqtlInfo eqtlinfo;
cout<<endl<<"Reading eQTL summary data..."<<endl;
if(eqtlFileName != NULL)
{
read_esifile(&eqtlinfo, string(eqtlFileName)+".esi");
if (snplstName != NULL) extract_eqtl_snp(&eqtlinfo, snplstName);
if(snplst2exclde != NULL) exclude_eqtl_snp(&eqtlinfo, snplst2exclde);
read_epifile(&eqtlinfo, string(eqtlFileName)+".epi");
if(problstName != NULL) extract_prob(&eqtlinfo, problstName);
if(genelistName != NULL) extract_prob_by_gene(&eqtlinfo, genelistName);
if(problst2exclde != NULL) exclude_prob(&eqtlinfo, problst2exclde);
read_besdfile(&eqtlinfo, string(eqtlFileName)+".besd");
if(eqtlinfo._rowid.empty() && eqtlinfo._bxz.empty())
{
printf("No data included from %s in the analysis.\n",eqtlFileName);
exit(EXIT_FAILURE);
}
}
else throw ("Error: please input the eQTL summary information for the eQTL data files by the option --beqtl-summary.");
vector<int> out_esi_id;
vector<int> out_epi_id;
vector<float> out_beta;
vector<float> out_se;
vector<double> out_pval;
if(eqtlinfo._valNum==0)
{
for(uint32_t i=0;i<eqtlinfo._probNum;i++)
{
for(uint32_t j=0;j<eqtlinfo._snpNum;j++)
{
double beta=eqtlinfo._bxz[i][j];
double se=eqtlinfo._sexz[i][j];
if(ABS(se+9)<1e-6) continue;
double zsxz=beta/se;
double pxz=pchisq(zsxz*zsxz, 1);
out_esi_id.push_back(j);
out_epi_id.push_back(i);
out_beta.push_back(beta);
out_se.push_back(se);
out_pval.push_back(pxz);
}
}
}
else
{
if(eqtlinfo._val.size()==0)
{
throw ("Error: No data extracted from the input, please check.");
}
for(uint32_t i=0;i<eqtlinfo._probNum;i++)
{
uint64_t proid=eqtlinfo._include[i];
uint64_t pos=eqtlinfo._cols[proid<<1];
uint64_t pos1=eqtlinfo._cols[(proid<<1)+1];
uint64_t num=pos1-pos;
for(int j=0;j<num;j++)
{
double beta=eqtlinfo._val[pos+j];
double se=eqtlinfo._val[pos+j+num];
double zsxz=beta/se;
double pxz=pchisq(zsxz*zsxz, 1);
out_esi_id.push_back(eqtlinfo._rowid[pos+j]);
out_epi_id.push_back(i);
out_beta.push_back(beta);
out_se.push_back(se);
out_pval.push_back(pxz);
}
}
}
string smrfile = string(outFileName)+".raw";
ofstream smr(smrfile.c_str());
if (!smr) throw ("Error: can not open the fam file " + smrfile + " to save!");
smr << "ProbeID"<<'\t' << "SNP" <<'\t'<< "Chr" <<'\t' << "A1" << '\t'<< "A2"<< '\t' << "b"<<'\t'<< "SE" << '\t'<<"p"<<'\n';
for (int i = 0;i <out_esi_id.size(); i++) {
smr<<eqtlinfo._epi_prbID[out_epi_id[i]]<<'\t'<<eqtlinfo._esi_rs[out_esi_id[i]]<<'\t'<<eqtlinfo._esi_chr[out_esi_id[i]]<<'\t'<<eqtlinfo._esi_allele1[out_esi_id[i]]<<'\t'<<eqtlinfo._esi_allele2[out_esi_id[i]]<<'\t'<<out_beta[i]<<'\t'<<out_se[i]<<'\t'<<out_pval[i]<< '\n';
}
smr.close();
cout<<"Extracted results of "<<out_esi_id.size()<<" items have been saved in the plaint text file [" + smrfile + "]."<<endl;
}
/*
void standardization(char* outFileName, char* eqtlFileName,bool bFlag,char* freqName, char* vpFileName)
{
eqtlInfo esdata;
if(eqtlFileName==NULL) throw("Error: please input eQTL summary data for SMR analysis by the flag --eqtl-summary.");
if(freqName==NULL && vpFileName==NULL) throw("Error: please input feq data or variance data for standardisation by the flag --freq or --probe-var.");
read_esifile(&esdata, string(eqtlFileName)+".esi");
read_epifile(&esdata, string(eqtlFileName)+".epi");
read_besdfile(&esdata, string(eqtlFileName)+".besd");
if(esdata._rowid.empty() && esdata._bxz.empty())
{
printf("No data included from %s in the analysis.\n",eqtlFileName);
exit(EXIT_FAILURE);
}
if(vpFileName!=NULL)
{
read_probevarfile(&esdata, string(vpFileName));
for(int i=0;i<esdata._probNum;i++)
{
double prbvar_sqrt=sqrt(esdata._epi_var[i]);
if(esdata._rowid.empty())
{
for (int j = 0; j<esdata._esi_include.size(); j++)
{
if (fabs(esdata._bxz[i][j] + 9) > 1e-6)
{
float bxz=esdata._bxz[i][j];
float sexz=esdata._sexz[i][j];
esdata._bxz[i][j]=bxz/prbvar_sqrt;
esdata._sexz[i][j]=sexz/prbvar_sqrt;
}
}
}
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(uint64_t j=0;j<numsnps;j++)
{
float bxz=esdata._val[beta_start+j];
float sexz=esdata._val[se_start+j];
esdata._val[beta_start+j]=bxz/prbvar_sqrt;
esdata._val[se_start+j]=sexz/prbvar_sqrt;
}
}
}
}
else if(freqName!=NULL)
{
update_freq(&esdata, string(freqName));
for(int i=0;i<esdata._probNum;i++)
{
if(esdata._rowid.empty())
{
for (int j = 0; j<esdata._esi_include.size(); j++)
{
if (fabs(esdata._bxz[i][j] + 9) > 1e-6)
{
float bxz=esdata._bxz[i][j];
float sexz=esdata._sexz[i][j];
float p=esdata._esi_freq[j];
float z=bxz/sexz;
float b=z/sqrt(2*p*(1-p)*(n+z*z));
float se=1/sqrt(2*p*(1-p)*(n+z*z));
esdata._bxz[i][j]=b;
esdata._sexz[i][j]=se;
}
}
}
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(uint64_t j=0;j<numsnps;j++)
{
uint64_t ge_rowid=esdata._rowid[beta_start+j];
float bxz=esdata._val[beta_start+j];
float sexz=esdata._val[se_start+j];
float p=esdata._esi_freq[ge_rowid];
float z=bxz/sexz;
float b=z/sqrt(2*p*(1-p)*(n+z*z));
float se=1/sqrt(2*p*(1-p)*(n+z*z));
esdata._val[beta_start+j]=b;
esdata._val[se_start+j]=se;
}
}
}
}
write_besd(outFileName, &esdata);
}
*/
void lookup(char* outFileName,char* eqtlFileName, char* snplstName, char* problstName,char* genelistName, double plookup,bool bFlag, int chr, int prbchr,int snpchr, char* snprs, char* fromsnprs, char* tosnprs, char* prbname, char* fromprbname, char* toprbname,int snpWind, int prbWind,char* genename,int fromsnpkb, int tosnpkb, int fromprbkb, int toprbkb, bool snpwindFlag, bool prbwindFlag,bool cis_flag, int cis_itvl)
{
string logstr;
int flag4chr=0;