-
Notifications
You must be signed in to change notification settings - Fork 7
/
SMR_plot.cpp
2480 lines (2219 loc) · 105 KB
/
SMR_plot.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_plot.cpp
// SMR_CPP
//
// Created by Futao Zhang on 29/06/2016.
// Copyright (c) 2016 Futao Zhang. All rights reserved.
//
#include "SMR_plot.h"
namespace SMRDATA
{
void smre2e(char* bFileName,char* eqtlFileName, char* eqtlFileName2, double maf,char* indilstName, char* snplstName,double p_hetero,double ld_top,int m_hetero, char* indilst2remove, char* snplst2exclde, double p_smr, int cis_itvl,int op_wind, const char* oprobe, vector<string> &mprobe, vector<double> &bsmr, vector<double> &sesmr, vector<double> &psmr, vector<double> &pheidi, vector<int> &nsnp)
{
setNbThreads(thread_num);
string logstr;
eqtlInfo etrait;
eqtlInfo esdata;
bInfo bdata;
double threshold= chi_val(1,p_hetero);
cis_itvl=cis_itvl*1000;
if(bFileName == NULL ) throw("Error: please input Plink file for SMR analysis by the flag --bfile.");
if(eqtlFileName==NULL) throw("Error: please input eQTL summary data for SMR analysis by the flag --eqtl-summary.");
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(oprobe != NULL) extract_eqtl_single_probe(&etrait, oprobe);
read_besdfile(&etrait, string(eqtlFileName)+".besd");
if(etrait._rowid.empty() && etrait._bxz.empty())
{
printf("No data included in the analysis.\n");
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);
}
//the etrait is not updated, so from now on _esi_include should be used always.
cout<<"Reading eQTL summary data..."<<endl;
read_epifile(&esdata, string(eqtlFileName2)+".epi");
read_besdfile(&esdata, string(eqtlFileName2)+".besd");
if(esdata._rowid.empty() && esdata._bxz.empty())
{
printf("No data included in the analysis.\n");
exit(EXIT_FAILURE);
}
for( int ii=0;ii<etrait._probNum;ii++)
{
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());
string traitname=etrait._epi_prbID[ii];
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._sexz[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;
int outcome_probe_wind=op_wind*1000;
int traitbp=etrait._epi_bp[ii];
int lowerbounder=(traitbp-outcome_probe_wind)>0?(traitbp-outcome_probe_wind):0;
int upperbounder=traitbp+outcome_probe_wind;
int traitchr=etrait._epi_chr[ii];
vector<int> icld_tmp;
for(int j=0;j<esdata._include.size();j++)
{
int bptmp=esdata._epi_bp[esdata._include[j]];
if(esdata._epi_chr[esdata._include[j]]==traitchr && bptmp>=lowerbounder && bptmp<=upperbounder) icld_tmp.push_back(esdata._include[j]);
}
int outCount = -1;
unsigned int probNum = icld_tmp.size();
// unsigned int probNum = esdata._probNum;
// vectors for output
vector<long> out_probid(probNum);
vector<string> bxy(probNum); // origin is double
vector<string> sexy(probNum);// origin is double
vector<string> pxy(probNum); // origin is double
vector<string> bgwas(probNum); // origin is double
vector<string> beqtl(probNum); // origin is double
vector<string> segwas(probNum); // origin is double
vector<string> seeqtl(probNum); // origin is double
vector<string> pgwas(probNum); // origin is double
vector<string> peqtl(probNum); // origin is double
vector<string> rsid(probNum);
vector<string> rschr(probNum);
vector<string> rsa1(probNum);
vector<string> rsa2(probNum);
vector<string> rsbp(probNum); //origin is unsigned int
vector<string> rsfreq(probNum); //origin is unsigned double
vector<string> prb1(probNum); // origin is double
vector<string> nsnp_test1(probNum);
vector<string> top_match1(probNum); // origin is int
vector<string> ldrsq(probNum); // origin is double
cout<<endl<<"Performing SMR and heterogeneity analysis..... "<<endl;
float progr0=0.0 , progr1;
progress_print(progr0);
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;
MatrixXd _LD;
VectorXd ld_v;
MatrixXd _LD_heidi;
MatrixXd _X_heidi;
//for plot
vector<string> plot_paths;
for(int ti=0;ti<icld_tmp.size();ti++)
{
int i=icld_tmp[ti];
progr1=1.0*ti/probNum;
if(progr1-progr0-0.05>1e-6 || ti+1==probNum)
{
if(ti+1==probNum) progr1=1.0;
progress_print(progr1);
progr0=progr1;
}
// for plot
vector<uint32_t> plot_snpidx;
vector<uint32_t> plot_probeidx;
vector<double> plot_bxz;
vector<double> plot_sexz;
int plotdir_id=ti>>10;
string plotdir="";
//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._sexz[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]);
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]);
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();
maxid=max_abs_id(zsxz);
double pxz_val = pchisq(zsxz[maxid]*zsxz[maxid], 1);
if(pxz_val>p_smr) continue;
else outCount++;
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[outCount]= i;
bxy[outCount]=dtosf(bxy_val);
sexy[outCount]=dtosf(sexy_val);
pxy[outCount]=dtos(pxy_val);
bgwas[outCount]=dtosf(byz[maxid]);
segwas[outCount]=dtosf(seyz[maxid]);
beqtl[outCount]=dtosf(bxz[maxid]);
seeqtl[outCount]=dtosf(sexz[maxid]);
pgwas[outCount]=dtos(pyz_val);
peqtl[outCount]=dtos(pxz_val);
rsid[outCount]=eName[maxid];
rschr[outCount]=atos(snpchrom[maxid]);
rsbp[outCount]=itos(bpsnp[maxid]);
rsfreq[outCount]=dtosf(freq[maxid]);
rsa1[outCount]=allele1[maxid];
rsa2[outCount]=allele2[maxid];
//extract info from reference
make_XMat(&bdata,curId, _X); //_X: one row one individual, one column one SNP
//last vesion ref_snpData was used. row of ref_snpData is SNP, column of ref_snpData is individual
//cor_calc(_LD, _X);
ld_calc_o2m(ld_v,maxid,_X);
//test here
// for( int kk=0;kk<curId.size();kk++) if(fabs(ld_v[kk]-_LD.col(maxid)[kk])>1e-6) cout<<"wrong"<<endl;
// test end
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, maxid,ld_top);
else get_square_ldpruning_idxes(sn_ids,zsxz,threshold,ld_v, maxid,ld_top);
if(sn_ids.size() < m_hetero)
{
prb1[outCount]= string("NA");
nsnp_test1[outCount]= string("NA");
top_match1[outCount]= string("NA");
ldrsq[outCount]= 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());
// _LD_heidi.resize(sn_ids.size(),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]];
// for(int k=0;k<=j;k++)_LD_heidi(j,k)=_LD_heidi(k,j)=_LD(sn_ids[j],sn_ids[k]);
_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[outCount] = dtos(pdev);
if(nsnp>0) nsnp_test1[outCount] = itos(nsnp);
else nsnp_test1[outCount] = string("NA");
// top GWAS SNP ?= top eQTL
int indx1 = max_abs_id(_zsxz);
_byz = _byz.array() / _seyz.array();
int indx2 = max_abs_id(_byz);
if(indx1 == indx2) top_match1[outCount] =itos(1);
else top_match1[outCount] = itos(0);
double ldrsqVal = _LD_heidi(indx1, indx2) * _LD_heidi(indx1, indx2);
ldrsq[outCount]=dtosf(ldrsqVal);
}
if(outCount>=0)
{
for (int i = 0;i <=outCount; i++) {
mprobe.push_back(esdata._epi_prbID[out_probid[i]]);
bsmr.push_back(atof(bxy[i].c_str()));
sesmr.push_back(atof(sexy[i].c_str()));
psmr.push_back(atof(pxy[i].c_str()));
pheidi.push_back(atof(prb1[i].c_str()));
nsnp.push_back(atoi(nsnp_test1[i].c_str()));
}
}
free_gwas_data( &gdata);
}
}
void psudoclone(eqtlInfo* eqtlinfo, eqtlInfo* eqtlinfo2)
{
eqtlinfo2->_epi_bp=eqtlinfo->_epi_bp;
eqtlinfo2->_epi_chr=eqtlinfo->_epi_chr;
eqtlinfo2->_epi_prbID=eqtlinfo->_epi_prbID;
eqtlinfo2->_epi_gd=eqtlinfo->_epi_gd;
eqtlinfo2->_epi_gene=eqtlinfo->_epi_gene;
eqtlinfo2->_epi_orien=eqtlinfo->_epi_orien;
eqtlinfo2->_include=eqtlinfo->_include;
eqtlinfo2->_probe_name_map=eqtlinfo->_probe_name_map;
eqtlinfo2->_esi_chr=eqtlinfo->_esi_chr;
eqtlinfo2->_esi_rs=eqtlinfo->_esi_rs;
eqtlinfo2->_esi_gd=eqtlinfo->_esi_gd;
eqtlinfo2->_esi_bp=eqtlinfo->_esi_bp;
eqtlinfo2->_esi_allele1=eqtlinfo->_esi_allele1;
eqtlinfo2->_esi_allele2=eqtlinfo->_esi_allele2;
eqtlinfo2->_esi_include=eqtlinfo->_esi_include;
eqtlinfo2->_snp_name_map=eqtlinfo->_snp_name_map;
eqtlinfo2->_esi_freq=eqtlinfo->_esi_freq;
eqtlinfo2->_snpNum=eqtlinfo->_snpNum;
eqtlinfo2->_probNum=eqtlinfo->_probNum;
eqtlinfo2->_valNum=eqtlinfo->_valNum;
}
void psudoclone(gwasData* gdata, gwasData* gdata2)
{
gdata2->snpName=gdata->snpName;
gdata2->snpBp=gdata->snpBp;
gdata2->allele_1=gdata->allele_1;
gdata2->allele_2=gdata->allele_2;
gdata2->freq=gdata->freq;
gdata2->byz=gdata->byz;
gdata2->seyz=gdata->seyz;
gdata2->pvalue=gdata->pvalue;
gdata2->splSize=gdata->splSize;
gdata2->_include=gdata->_include;
gdata2->snpNum=gdata->snpNum;
}
void psudoclone(bInfo* bdata, bInfo* bdata2)
{
bdata2->_chr=bdata->_chr;
bdata2->_snp_name=bdata->_snp_name;
bdata2->_snp_name_map=bdata->_snp_name_map;
bdata2->_genet_dst=bdata->_genet_dst;
bdata2->_bp=bdata->_bp;
bdata2->_allele1=bdata->_allele1;
bdata2->_allele2=bdata->_allele2;
bdata2->_ref_A=bdata->_ref_A;
bdata2->_other_A=bdata->_other_A;
bdata2->_include=bdata->_include;
bdata2->_snp_num=bdata->_snp_num;
bdata2->_maf=bdata->_maf;
bdata2->_fid=bdata->_fid;
bdata2->_pid=bdata->_pid;
bdata2->_id_map=bdata->_id_map;
bdata2->_fa_id=bdata->_fa_id;
bdata2->_mo_id=bdata->_mo_id;
bdata2->_sex=bdata->_sex;
bdata2->_pheno=bdata->_pheno;
bdata2->_indi_num=bdata->_indi_num;
bdata2->_keep=bdata->_keep;
bdata2->_mu=bdata->_mu;
bdata2->_autosome_num=bdata->_autosome_num;
}
double heidi_test_new_plot(bInfo* bdata,SMRWK* smrwk,vector<int> &ldnperprb,vector<string> &ldrs, vector<double> &outld, long maxid,double ldr2_top, double threshold, int m_hetero,long &nsnp ,double threshpheidiest,double ld_min,int opt_hetero, bool sampleoverlap, double theta)
{
VectorXd ld_v;
MatrixXd _X;
vector<int> sn_ids;
double pthres=pchisq(threshold,1);
//printf("Filtering SNPs (%ld in total) at eQTL p-value < %e for the HEIDI test.\n",smrwk->zxz.size(), pthres);
for(int i=0;i<smrwk->zxz.size();i++)
{
if(smrwk->zxz[i]*smrwk->zxz[i]-threshold>1e-6) sn_ids.push_back(i);
}
if(sn_ids.size() < m_hetero) {
//printf("INFO: the HEIDI test is skipped because the number of SNPs (%ld) is smaller than %d.\n", sn_ids.size(), m_hetero);
return -9;
}
//printf("%ld SNPs left after filtering.\n",sn_ids.size());
update_snidx(smrwk,sn_ids,MAX_NUM_LD,"LD pruning");
SMRWK smrwk_heidi;
extract_smrwk(smrwk,sn_ids,&smrwk_heidi);
long maxid_heidi=max_abs_id(smrwk_heidi.zxz);
make_XMat(bdata,smrwk_heidi.curId, _X);
//printf("Removing SNPs with LD r-squared between top-SNP %s > %f or < %f...\n",smrwk_heidi.rs[maxid_heidi].c_str(),ldr2_top,ld_min);
ld_calc_o2m(ld_v,maxid_heidi,_X);
if(fabs(ldr2_top-1)>1e-6 || ld_min>0) {
sn_ids.clear();
for(int i=0;i<smrwk_heidi.zxz.size();i++)
{
if(i!= maxid_heidi)
{
double ldtmp=ld_v(i)*ld_v(i);
if((ldtmp-ldr2_top)<1e-6 && (ldtmp>ld_min)) sn_ids.push_back(i);
}
else{
sn_ids.push_back(i);
}
}
}
//printf("%ld SNPs are removed and %ld SNPs are retained.\n",smrwk_heidi.zxz.size()-sn_ids.size(),sn_ids.size());
if(sn_ids.size() < m_hetero) {
//printf("INFO: HEIDI test is skipped because the number of SNPs (%ld) is smaller than %d.\n", sn_ids.size(), m_hetero);
return -9;
}
update_smrwk_x(&smrwk_heidi,sn_ids,_X);
maxid_heidi=max_abs_id(smrwk_heidi.zxz);
//printf("Removing one of each pair of remaining SNPs with LD r-squared > %f...\n",ldr2_top);
int m = (int)smrwk_heidi.bxz.size();
vector<int> rm_ID1;
MatrixXd C;
cor_calc(C, _X);
double ld_top=sqrt(ldr2_top);
if (ld_top < 1) rm_cor_sbat(C, ld_top, m, rm_ID1);
//printf("%ld SNPs are removed and %ld SNPs (including the top SNP %s) are retained.\n",rm_ID1.size(),m-rm_ID1.size(),smrwk_heidi.rs[maxid_heidi].c_str());
if(m-rm_ID1.size() < m_hetero) {
//printf("INFO: HEIDI test is skipped because the number of SNPs (%ld) is smaller than %d.\n", m-rm_ID1.size(), m_hetero);
return -9;
}
//Create new index
sn_ids.clear();
int qi=0;
for (int i=0 ; i<m ; i++) {
if (rm_ID1.size() == 0) sn_ids.push_back(i);
else {
if (qi<rm_ID1.size() && rm_ID1[qi] == i) qi++; //Skip removed snp
else sn_ids.push_back(i);
}
}
update_snidx(&smrwk_heidi,sn_ids,opt_hetero,"HEIDI test");
if (sn_ids.size() < C.size()) { //Build new matrix
MatrixXd D(sn_ids.size(),sn_ids.size());
for (int i = 0 ; i < sn_ids.size() ; i++) {
for (int j = 0 ; j < sn_ids.size() ; j++) {
D(i,j) = C(sn_ids[i],sn_ids[j]);
}
}
C = D;
}
VectorXd _byz,_seyz, _bxz,_sexz,_zsxz;
_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());
#pragma omp parallel for
for(int j=0;j<sn_ids.size();j++)
{
_byz[j]=smrwk_heidi.byz[sn_ids[j]];
_seyz[j]=smrwk_heidi.seyz[sn_ids[j]];
_bxz[j]=smrwk_heidi.bxz[sn_ids[j]];
_sexz[j]=smrwk_heidi.sexz[sn_ids[j]];
_zsxz[j]=smrwk_heidi.zxz[sn_ids[j]];
}
// out ld
int id=0;
double tmpVal, cmpVal=fabs(smrwk_heidi.zxz[sn_ids[0]]);
for( int jj=1;jj<sn_ids.size();jj++)
{
tmpVal=fabs(smrwk_heidi.zxz[sn_ids[jj]]);
if( cmpVal-tmpVal < 1e-6)
{
cmpVal=tmpVal;
id=jj;
}
}
nsnp = sn_ids.size();
double pdev=-9;
if(sampleoverlap) pdev=bxy_mltheter_so(_byz, _bxz, _seyz, _sexz, _zsxz, C, &nsnp, theta);
else pdev=bxy_hetero3(_byz, _bxz, _seyz, _sexz, _zsxz, C, &nsnp);
if(pdev>=threshpheidiest)
{
// if after pairwise LD pruning, the SNP number > 20, id should be 0. otherwise id should also recalulated
ldnperprb.push_back((int)sn_ids.size()+ldnperprb[ldnperprb.size()-1]);
for(int jj=0;jj<sn_ids.size();jj++){
ldrs.push_back(smrwk_heidi.rs[sn_ids[jj]]);
outld.push_back(C(id,jj));
}
}
return pdev;
}
double heidi_test_plot(bInfo* bdata,SMRWK* smrwk, vector<int> &ldnperprb,vector<string> &ldrs, vector<double> &outld,long maxid,double ld_top, double threshold, int m_hetero,long &nsnp ,double threshpheidiest)
{
VectorXd ld_v;
MatrixXd _X;
vector<int> sn_ids;
Map<VectorXd> ei_bxz(&smrwk->bxz[0],smrwk->bxz.size());
Map<VectorXd> ei_sexz(&smrwk->sexz[0],smrwk->sexz.size());
VectorXd zsxz=ei_bxz.array()/ei_sexz.array();
make_XMat(bdata,smrwk->curId, _X);
ld_calc_o2m(ld_v,maxid,_X);
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) {
printf("INFO: HEIDI test is skipped because the number of SNPs (%ld) is less than a threshold (%d).\n", sn_ids.size(), m_hetero);
return -9;
}
VectorXd _byz,_seyz, _bxz,_sexz,_zsxz;
MatrixXd _X_heidi, _LD_heidi;
_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]=smrwk->byz[sn_ids[j]];
_seyz[j]=smrwk->seyz[sn_ids[j]];
_bxz[j]=smrwk->bxz[sn_ids[j]];
_sexz[j]=smrwk->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);
nsnp = sn_ids.size();
double pdev=bxy_hetero3(_byz, _bxz, _seyz, _sexz, _zsxz, _LD_heidi, &nsnp);
if(pdev>=threshpheidiest)
{
// out ld
long maxid_heidi=max_abs_id(smrwk->zxz);
ldnperprb.push_back((int)sn_ids.size()+ldnperprb[ldnperprb.size()-1]);
for(int jj=0;jj<sn_ids.size();jj++){
ldrs.push_back(smrwk->rs[sn_ids[jj]]);
outld.push_back(_LD_heidi(maxid_heidi,jj));
}
}
return pdev;
}
void smr_heidi_plot(vector<SMRRLT> &smrrlts, vector<int> &ldprbid, vector<string> &ldprb, vector<int> &ldnperprb, vector<string> &ldrs, vector<double> &outld, bInfo* bdata,gwasData* gdata,eqtlInfo* esdata, int cis_itvl, bool heidioffFlag, const char* refSNP,double p_hetero,double ld_top,int m_hetero , double p_smr,double threshpsmrest, double threshpheidiest, double ld_min, bool new_heidi_mth,int opt_hetero,bool sampleoverlap, double pmecs, int minCor)
{
ldnperprb.push_back(0);
uint64_t probNum = esdata->_include.size();
double thresh_heidi= chi_val(1,p_hetero),theta=0;
VectorXd _byz,_seyz,_bxz,_sexz,_zsxz,ld_v,zsxz;
MatrixXd _X,_LD,_LD_heidi,_X_heidi;
smrrlts.clear();
SMRWK smrwk;
cout<<endl<<"Performing SMR analysis (SMR and HEIDI tests)..... "<<endl;
float progr0=0.0 , progr1;
progress_print(progr0);
cis_itvl=cis_itvl*1000;
for(int ii=0;ii<probNum;ii++)
{
progr1=1.0*ii/probNum;
if(progr1-progr0-0.05>1e-6 || ii+1==probNum)
{
if(ii+1==probNum) progr1=1.0;
progress_print(progr1);
progr0=progr1;
}
int i=esdata->_include[ii];
int probebp=esdata->_epi_bp[i];
int probechr=esdata->_epi_chr[i];
string probename=esdata->_epi_prbID[i];
string probegene=esdata->_epi_gene[i];
char probeorien=esdata->_epi_orien[i];
init_smr_wk(&smrwk);
smrwk.cur_prbidx=i;
smrwk.cur_chr=probechr;
long maxid =fill_smr_wk(bdata, gdata, esdata, &smrwk, refSNP, cis_itvl,heidioffFlag);
if(refSNP!=NULL && maxid==-9)
{
//printf("WARNING: can't find target SNP %s for probe %s.\n",refSNP, probename.c_str());
continue;
}
if (smrwk.bxz.size() == 0) {
//printf("WARNING: no SNP fetched for probe %s.\n", probename.c_str());
continue;
}
Map<VectorXd> ei_bxz(&smrwk.bxz[0],smrwk.bxz.size());
Map<VectorXd> ei_sexz(&smrwk.sexz[0],smrwk.sexz.size());
zsxz=ei_bxz.array()/ei_sexz.array();
if(refSNP==NULL) maxid=max_abs_id(zsxz);
double pxz_val = pchisq(zsxz[maxid]*zsxz[maxid], 1);
if(refSNP==NULL && pxz_val>p_smr){
//printf("WARNING: no SNP passed the p-value threshold %e for the SMR analysis for probe %s.\n", p_smr, probename.c_str());
continue;
} else {
//printf("Analysing probe %s...\n", probename.c_str());
}
if(sampleoverlap)
{
//printf("Estimating the correlation ...\n");
double z2mecs=qchisq(pmecs,1);
double zmecs=sqrt(z2mecs);
vector<double> zxz, zyz;
for(int k=0;k<smrwk.bxz.size();k++)
{
double z1=smrwk.bxz[k]/smrwk.sexz[k];
double z2=smrwk.byz[k]/smrwk.seyz[k];
if(fabs(z1)<zmecs && fabs(z2)<zmecs )
{
zxz.push_back(z1);
zyz.push_back(z2);
}
}
if(zxz.size()< minCor){
//printf("WARNING: less than %d common SNPs obtained from the cis-region of probe %s at a p-value threshold %5.2e.\n ", minCor,probename.c_str(), pmecs);
//printf("probe %s is skipped.\n ", probename.c_str());
continue;
}
else
{
theta=cor(zxz,zyz);
printf("The estimated correlation is %f.\n",theta);
}
}
double byzt=smrwk.byz[maxid], bxzt=smrwk.bxz[maxid], seyzt=smrwk.seyz[maxid], sexzt=smrwk.sexz[maxid];
double bxy_val = byzt / bxzt;
double sexy_val = -9;
if(sampleoverlap)
{
sexy_val = sqrt(seyzt* seyzt/(bxzt*bxzt) + sexzt*sexzt*byzt*byzt/(bxzt*bxzt*bxzt*bxzt) - 2*theta*seyzt*sexzt*byzt/(bxzt*bxzt*bxzt));
} else {
sexy_val = sqrt((seyzt * seyzt * bxzt * bxzt + sexzt * sexzt * byzt * byzt) / (bxzt * bxzt * bxzt * bxzt));
}
double chisqxy = bxy_val*bxy_val / (sexy_val*sexy_val);
double pxy_val = pchisq(chisqxy, 1); // double pxy=chi_prob(1,chisqxy); //works
SMRRLT currlt;
currlt.ProbeID=probename;
currlt.ProbeChr=probechr;
currlt.Gene=probegene;
currlt.Probe_bp=probebp;
currlt.Orien=probeorien;
currlt.SNP=smrwk.rs[maxid];
currlt.SNP_Chr=smrwk.snpchrom[maxid];
currlt.SNP_bp=smrwk.bpsnp[maxid];
currlt.A1=smrwk.allele1[maxid];
currlt.A2=smrwk.allele2[maxid];
currlt.Freq=smrwk.freq[maxid];
currlt.b_GWAS=smrwk.byz[maxid];
currlt.se_GWAS=smrwk.seyz[maxid];
currlt.p_GWAS=smrwk.pyz[maxid];
currlt.b_eQTL=smrwk.bxz[maxid];
currlt.se_eQTL=smrwk.sexz[maxid];
currlt.p_eQTL=pxz_val;
currlt.b_SMR=bxy_val;
currlt.se_SMR=sexy_val;
currlt.p_SMR=pxy_val;
if(heidioffFlag || pxy_val>threshpsmrest)
{
printf("INFO: the HEIDI test for probe %s is skipped because HEIDI test is turned off by the --heidi-off option or p_SMR does not pass the %e threshold.\n", probename.c_str(),threshpsmrest);
currlt.p_HET=-9;
currlt.nsnp=-9;
smrrlts.push_back(currlt);
}
else
{
long nsnp=-9;
double pdev=-9;
if(!heidioffFlag) {
if(new_heidi_mth) pdev= heidi_test_new_plot(bdata,&smrwk, ldnperprb,ldrs,outld,maxid, ld_top, thresh_heidi, m_hetero, nsnp ,threshpheidiest,ld_min,opt_hetero,sampleoverlap,theta);
else pdev= heidi_test_plot(bdata,&smrwk, ldnperprb,ldrs,outld,maxid, ld_top, thresh_heidi, m_hetero, nsnp , threshpheidiest);
if(pdev>=threshpheidiest)
{
ldprbid.push_back(i);
ldprb.push_back(probename);
}
}
currlt.p_HET=pdev;
currlt.nsnp=(int)nsnp;
smrrlts.push_back(currlt);
}
}
printf("Results of %ld probes have been returned.\n",smrrlts.size());
}
void get_eTrait(gwasData* gdata, eqtlInfo* edata,int eTraitIdx)
{
gdata->allele_1.clear();
gdata->allele_2.clear();
gdata->byz.clear();
gdata->seyz.clear();
gdata->freq.clear();
gdata->pvalue.clear();
gdata->splSize.clear();
gdata->_include.clear();
gdata->snpName.clear();
gdata->snpBp.clear();
if(edata->_rowid.empty())
{
for (int j = 0; j<edata->_esi_include.size(); j++)
{
if (fabs(edata->_sexz[eTraitIdx][edata->_esi_include[j]] + 9) > 1e-6)
{
gdata->byz.push_back(edata->_bxz[eTraitIdx][edata->_esi_include[j]]);
gdata->seyz.push_back(edata->_sexz[eTraitIdx][edata->_esi_include[j]]);
double z=edata->_bxz[0][edata->_esi_include[j]]/edata->_sexz[eTraitIdx][edata->_esi_include[j]];
gdata->pvalue.push_back(pchisq(z*z,1));
gdata->snpName.push_back(edata->_esi_rs[edata->_esi_include[j]]);
gdata->allele_1.push_back(edata->_esi_allele1[edata->_esi_include[j]]);
gdata->allele_2.push_back(edata->_esi_allele2[edata->_esi_include[j]]);
gdata->freq.push_back(edata->_esi_freq[edata->_esi_include[j]]);
gdata->snpBp.push_back(edata->_esi_bp[edata->_esi_include[j]]);
gdata->_include.push_back(edata->_esi_include[j]); // row id selected
}
}
}
else
{
int ii=0;
uint64_t beta_start=edata->_cols[ii<<1];
uint64_t se_start=edata->_cols[1+(ii<<1)];
uint64_t numsnps=se_start-beta_start;
for(int j=0;j<numsnps;j++)
{
int ge_rowid=edata->_rowid[beta_start+j];
if(binary_search(edata->_esi_include.begin(), edata->_esi_include.end(), ge_rowid))
{
gdata->byz.push_back(edata->_val[beta_start+j]);
gdata->seyz.push_back(edata->_val[se_start+j]);
double z=edata->_val[beta_start+j]/edata->_val[se_start+j];
gdata->pvalue.push_back(pchisq(z*z,1));
gdata->snpName.push_back(edata->_esi_rs[ge_rowid]);
gdata->allele_1.push_back(edata->_esi_allele1[ge_rowid]);
gdata->allele_2.push_back(edata->_esi_allele2[ge_rowid]);
gdata->freq.push_back(edata->_esi_freq[ge_rowid]);
gdata->snpBp.push_back(edata->_esi_bp[ge_rowid]);
gdata->_include.push_back(ge_rowid);
}
}
}
gdata->snpNum=gdata->snpName.size();
}
void plot_triple(char* outFileName, char* bFileName,char* gwasFileName, char* eqtlFileName,char* meqtlFileName, double maf,char* indilstName, char* snplstName,double p_hetero,double ld_top,int m_hetero ,int opt_hetero ,char* indilst2remove, char* snplst2exclde, double p_smr, char* refSNP, int cis_itvl, char* prbname, int prbWind,bool prbwindFlag, int snpchr, char* snprs, char* fromsnprs, char* tosnprs,int snpWind,int fromsnpkb, int tosnpkb,bool snpwindFlag,bool cis_flag, char* geneAnnoName, double pthres_me2esmr,double threshpsmrest,bool new_het_mtd,double threshphet,bool opt, double ld_min, bool sampleoverlap, double pmecs, int minCor,char* targetsnpproblstName,double afthresh,double percenthresh)
{
setNbThreads(thread_num);
if(prbname==NULL) throw("Error: please input probe to plot by the flag --probe.");
if(!prbwindFlag) throw("Error: please input probe window by the flag --probe-wind.");
if(bFileName == NULL ) throw("Error: please input Plink file by the flag --bfile.");
if(gwasFileName==NULL) throw("Error: please input GWAS summary data by the flag --gwas-summary.");
if(eqtlFileName==NULL) throw("Error: please input eQTL summary data by the flag --eqtl-summary.");
if(meqtlFileName==NULL) throw("Error: please input eQTL summary data by the flag --eqtl-summary.");
if(geneAnnoName==NULL) throw("Error: please input gene annotation file by the flag --gene-list.");
if(ld_min>ld_top) {
printf("ERROR: --ld-min %f is larger than --ld-top %f.\n",ld_min,ld_top);
exit(EXIT_FAILURE);
}
if(targetsnpproblstName)
{
printf("ERROR: --target-snp-probe-list can't be applied for the time being. please disable it.\n");
exit(EXIT_FAILURE);
}
bool targetLstFlg=false;
map<string, string> prb_snp;
vector<int> gene_anno_chr;
vector<string> gene_anno_genename;
vector<int> gene_anno_start;
vector<int> gene_anno_end;
vector<string> strand;
read_gene_anno_strand(geneAnnoName,gene_anno_chr, gene_anno_genename,gene_anno_start,gene_anno_end,strand);
if(strand.size()==0)
{
printf("ERROR: please input the gene list file containing strand information.\n");
exit(EXIT_FAILURE);
}
map<string, int> gene_anno_map;
map<string, int>::iterator iter;
for(int i=0;i<gene_anno_genename.size();i++) gene_anno_map.insert(pair<string,int>(gene_anno_genename[i], i));
eqtlInfo edata;
eqtlInfo mdata;
bInfo bdata;
gwasData gdata;
read_epifile(&edata, string(eqtlFileName)+".epi");
extract_prob(&edata, prbname, prbWind);
long idx=-9;
for(int i=0;i<edata._include.size();i++)
if(edata._epi_prbID[edata._include[i]]== prbname) idx=edata._include[i];
int prbbp=edata._epi_bp[idx];
int curchr=edata._epi_chr[idx];
int plotfrombp=(prbbp-prbWind*1000>0)?prbbp-prbWind*1000:0;
int plottobp=prbbp+prbWind*1000;
printf("The position of probe %s is %d. the plot region [%d , %d] is set.\n",prbname,prbbp,plotfrombp,plottobp);
//get gene list in this plot region
vector<int> gidx;
for(int i=0;i<gene_anno_genename.size();i++)
if(gene_anno_chr[i]==curchr && gene_anno_start[i]>=plotfrombp && gene_anno_end[i]<=plottobp) gidx.push_back(i);
printf("%ld genes are included in the plot region.\n",gidx.size());
int plotfromkb=ceil(plotfrombp/1000.0);
int plottokb=ceil(plottobp/1000.0);
read_epifile(&mdata, string(meqtlFileName)+".epi");
extract_eqtl_prob(&mdata, curchr, plotfromkb, plottokb);
int from_esnpbp=(plotfrombp-cis_itvl*1000>0)?plotfrombp-cis_itvl*1000:0;
int end_esnpbp=plottobp+cis_itvl*1000;
printf("\nTo conduct SMR test and HEIDI test, the analysis region [%d , %d] is set.\n",from_esnpbp,end_esnpbp);
int from_esnpkb=ceil(from_esnpbp/1000.0);
int end_esnpkb=ceil(end_esnpbp/1000.0);
read_esifile(&edata, string(eqtlFileName)+".esi");
extract_eqtl_snp(&edata, curchr, from_esnpkb, end_esnpkb);