forked from wuyangf7/OPERA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMR_data.cpp
More file actions
7065 lines (6486 loc) · 317 KB
/
SMR_data.cpp
File metadata and controls
7065 lines (6486 loc) · 317 KB
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.cpp
// SRM_CPP
//
// Created by Yang Wu on 04/06/18.
// Copyright (c) 2021 Yang Wu. All rights reserved.
//
#include "SMR_data.h"
#include "stat.hpp"
#include "SMR_data_p1.h"
namespace SMRDATA
{
int file_read_check(ifstream* in_file, const char* filename)
{
in_file->open(filename);
if(!*in_file) return 0;
return 1;
}
void progress_print(float progress)
{
int barWidth = 70;
cout << "[";
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i)
{
if (i < pos) cout << "=";
else if (i == pos) cout << ">";
else cout << " ";
}
cout << "] " << int(progress * 100.0) << " %\r";
cout.flush();
}
void read_famfile(bInfo* bdata, string famfile) {
bdata->_autosome_num = 22;
ifstream Fam(famfile.c_str());
if (!Fam) throw ("Error: can not open the file [" + famfile + "] to read.");
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();
bdata->_indi_num = bdata->_fid.size();
cout << bdata->_indi_num << " individuals to be included from [" + famfile + "]." << endl;
// Initialize _keep
bdata->_keep.clear();
bdata->_keep.resize(bdata->_indi_num);
bdata->_id_map.clear();
int size = 0;
for (int i = 0; i < bdata->_indi_num; i++) {
bdata->_keep[i] = 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();
}
}
void update_bim(bInfo* bdata,vector<int> &rsnp) {
int i = 0;
//update bim information
vector<int> chr_buf, bp_buf;
vector<string> a1_buf, a2_buf, ref_A_buf, other_A_buf;
vector<string> snp_name_buf;
vector<double> genet_dst_buf, impRsq_buf;
for (i = 0; i < bdata->_snp_num; i++) {
if (!rsnp[i]) continue;
chr_buf.push_back(bdata->_chr[i]);
snp_name_buf.push_back(bdata->_snp_name[i]);
genet_dst_buf.push_back(bdata->_genet_dst[i]);
bp_buf.push_back(bdata->_bp[i]);
a1_buf.push_back(bdata->_allele1[i]);
a2_buf.push_back(bdata->_allele2[i]);
ref_A_buf.push_back(bdata->_ref_A[i]);
other_A_buf.push_back(bdata->_other_A[i]);
if(bdata->_impRsq.size()>0) impRsq_buf.push_back(bdata->_impRsq[i]);
}
bdata->_chr.clear();
bdata->_snp_name.clear();
bdata->_genet_dst.clear();
bdata->_bp.clear();
bdata->_allele1.clear();
bdata->_allele2.clear();
bdata->_ref_A.clear();
bdata->_other_A.clear();
bdata->_impRsq.clear();
bdata->_chr = chr_buf;
bdata->_snp_name = snp_name_buf;
bdata->_genet_dst = genet_dst_buf;
bdata->_bp = bp_buf;
bdata->_allele1 = a1_buf;
bdata->_allele2 = a2_buf;
bdata->_ref_A = ref_A_buf;
bdata->_other_A = other_A_buf;
bdata->_impRsq=impRsq_buf;
bdata->_snp_num = bdata->_chr.size();
bdata->_include.clear();
bdata-> _include.resize(bdata->_snp_num);
bdata->_snp_name_map.clear();
for (i = 0; i < bdata->_snp_num; i++) {
bdata->_include[i] = i;
bdata->_snp_name_map.insert(pair<string, int>(bdata->_snp_name[i], i));
}
}
void update_fam(bInfo* bdata,vector<int> &rindi) {
//update fam information
int i = 0;
vector<string> fid_buf, pid_buf, fa_id_buf, mo_id_buf;
vector<int> sex_buf;
vector<double> pheno_buf;
for (i = 0; i < bdata->_indi_num; i++) {
if (!rindi[i]) continue;
fid_buf.push_back(bdata->_fid[i]);
pid_buf.push_back(bdata->_pid[i]);
fa_id_buf.push_back(bdata->_fa_id[i]);
mo_id_buf.push_back(bdata->_mo_id[i]);
sex_buf.push_back(bdata->_sex[i]);
pheno_buf.push_back(bdata->_pheno[i]);
}
bdata->_fid.clear();
bdata->_pid.clear();
bdata->_fa_id.clear();
bdata->_mo_id.clear();
bdata->_sex.clear();
bdata->_pheno.clear();
bdata->_fid = fid_buf;
bdata->_pid = pid_buf;
bdata->_fa_id = fa_id_buf;
bdata->_mo_id = mo_id_buf;
bdata->_sex = sex_buf;
bdata->_pheno = pheno_buf;
bdata->_indi_num = bdata->_fid.size();
bdata->_keep.clear();
bdata->_keep.resize(bdata->_indi_num);
bdata->_id_map.clear();
for (i = 0; i < bdata->_indi_num; i++) {
bdata->_keep[i] = i;
bdata->_id_map.insert(pair<string, int>(bdata->_fid[i] + ":" + bdata->_pid[i], i));
}
}
void update_epi(eqtlInfo* eqtlinfo)
{
eqtlinfo->_probNum = eqtlinfo->_include.size();
vector<int> chr_buf, gd_buf,bp_buf, start, end;
vector<string> prbID_buf, gene_buf;
vector<char> orien_buf;
for (int i = 0; i < eqtlinfo->_probNum; i++)
{
chr_buf.push_back(eqtlinfo->_epi_chr[eqtlinfo->_include[i]]);
gd_buf.push_back(eqtlinfo->_epi_gd[eqtlinfo->_include[i]]);
bp_buf.push_back(eqtlinfo->_epi_bp[eqtlinfo->_include[i]]);
prbID_buf.push_back(eqtlinfo->_epi_prbID[eqtlinfo->_include[i]]);
gene_buf.push_back(eqtlinfo->_epi_gene[eqtlinfo->_include[i]]);
orien_buf.push_back(eqtlinfo->_epi_orien[eqtlinfo->_include[i]]);
if(eqtlinfo->_epi_start.size()>0) start.push_back(eqtlinfo->_epi_start[eqtlinfo->_include[i]]);
if(eqtlinfo->_epi_end.size()>0) start.push_back(eqtlinfo->_epi_end[eqtlinfo->_include[i]]);
}
eqtlinfo->_epi_chr.clear();
eqtlinfo->_epi_gd.clear();
eqtlinfo->_epi_bp.clear();
eqtlinfo->_epi_prbID.clear();
eqtlinfo->_epi_gene.clear();
eqtlinfo->_epi_orien.clear();
eqtlinfo->_epi_start.clear();
eqtlinfo->_epi_end.clear();
eqtlinfo->_epi_chr.swap(chr_buf);
eqtlinfo->_epi_gd.swap(gd_buf);
eqtlinfo->_epi_bp.swap(bp_buf);
eqtlinfo->_epi_prbID.swap(prbID_buf);
eqtlinfo->_epi_gene.swap(gene_buf);
eqtlinfo->_epi_orien.swap(orien_buf);
eqtlinfo->_epi_start.swap(start);
eqtlinfo->_epi_end.swap(end);
eqtlinfo->_include.clear();
eqtlinfo->_probe_name_map.clear();
for (int i = 0; i < eqtlinfo->_probNum; i++)
{
eqtlinfo->_include.push_back(i);
eqtlinfo->_probe_name_map.insert(pair<string,int>(eqtlinfo->_epi_prbID[i],i));
}
}
void update_esi(eqtlInfo* eqtlinfo)
{
eqtlinfo->_snpNum = eqtlinfo->_esi_include.size();
vector<int> chr_buf, gd_buf, bp_buf;
vector<string> rs_buf;
vector<string> allele1_buf, allele2_buf;
vector<float> freq_buf;
for (int i = 0; i < eqtlinfo->_snpNum; i++)
{
chr_buf.push_back(eqtlinfo->_esi_chr[eqtlinfo->_esi_include[i]]);
gd_buf.push_back(eqtlinfo->_esi_gd[eqtlinfo->_esi_include[i]]);
bp_buf.push_back(eqtlinfo->_esi_bp[eqtlinfo->_esi_include[i]]);
rs_buf.push_back(eqtlinfo->_esi_rs[eqtlinfo->_esi_include[i]]);
allele1_buf.push_back(eqtlinfo->_esi_allele1[eqtlinfo->_esi_include[i]]);
allele2_buf.push_back(eqtlinfo->_esi_allele2[eqtlinfo->_esi_include[i]]);
freq_buf.push_back(eqtlinfo->_esi_freq[eqtlinfo->_esi_include[i]]);
}
eqtlinfo->_esi_chr.clear();
eqtlinfo->_esi_gd.clear();
eqtlinfo->_esi_bp.clear();
eqtlinfo->_esi_rs.clear();
eqtlinfo->_esi_allele1.clear();
eqtlinfo->_esi_allele2.clear();
eqtlinfo->_esi_freq.clear();
eqtlinfo->_esi_chr.swap(chr_buf);
eqtlinfo->_esi_gd.swap(gd_buf);
eqtlinfo->_esi_bp.swap(bp_buf);
eqtlinfo->_esi_rs.swap(rs_buf);
eqtlinfo->_esi_allele1.swap(allele1_buf);
eqtlinfo->_esi_allele2.swap(allele2_buf);
eqtlinfo->_esi_freq.swap(freq_buf);
eqtlinfo->_esi_include.clear();
eqtlinfo->_snp_name_map.clear();
for (int i = 0; i < eqtlinfo->_snpNum; i++)
{
eqtlinfo->_esi_include.push_back(i);
eqtlinfo->_snp_name_map.insert(pair<string, int>(eqtlinfo->_esi_rs[i], i));
}
}
void read_bimfile(bInfo* bdata,string bimfile) {
// 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.");
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();
bdata->_ref_A = bdata->_allele1;
bdata->_other_A = bdata->_allele2;
cout << bdata->_snp_num << " SNPs to be included from [" + bimfile + "]." << endl;
// Initialize _include
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;
if( bdata->_snp_name_map.find(bdata->_snp_name[i]) != bdata->_snp_name_map.end()){
cout << "Warning: Duplicated SNP ID \"" + bdata->_snp_name[i] + "\" ";
stringstream ss;
ss << bdata->_snp_name[i] << "_" << i + 1;
bdata->_snp_name[i] = ss.str();
cout<<"has been changed to \"" + bdata->_snp_name[i] + "\".\n";
}
bdata->_snp_name_map.insert(pair<string, int>(bdata->_snp_name[i], i));
}
}
// some code are adopted from PLINK with modificationsm
void read_bedfile(bInfo* bdata, string bedfile)
{
int i = 0, j = 0, k = 0;
// Flag for reading individuals and SNPs
vector<int> rindi, rsnp;
//get_rindi
rindi.clear();
rindi.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[i] = 1;
else rindi[i] = 0;
}
//get_rsnp
rsnp.clear();
rsnp.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[i] = 1;
else rsnp[i] = 0;
}
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.");
// Read bed file
char ch[1];
bitset<8> b;
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());
}
fstream BIT(bedfile.c_str(), ios::in | ios::binary);
if (!BIT) throw ("Error: can not open the file [" + bedfile + "] to read.");
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, snp_indx = 0; j < bdata->_snp_num; j++) { // Read genotype in SNP-major mode, 00: homozygote AA; 11: homozygote BB; 01: hetezygote; 10: missing
if (!rsnp[j]) {
for (i = 0; i < bdata->_indi_num; i += 4) BIT.read(ch, 1);
continue;
}
for (i = 0, indi_indx = 0; i < bdata->_indi_num;) {
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 < bdata->_indi_num) { // 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++;
}
}
if (snp_indx == bdata->_include.size()) break;
snp_indx++;
}
BIT.clear();
BIT.close();
cout << "Genotype data for " << bdata->_keep.size() << " individuals and " << bdata->_include.size() << " SNPs to be included from [" + bedfile + "]." << endl;
update_fam(bdata, rindi);
update_bim(bdata, rsnp);
}
// read mbfile here
void read_gwas_data(gwasData* gdata, char* gwasFileName)
{
bool warnnullfreq=false;
ifstream gwasFile;
if(!file_read_check(&gwasFile, gwasFileName))
{
fprintf (stderr, "%s: Couldn't open file %s\n",
gwasFileName, strerror (errno));
exit (EXIT_FAILURE);
}
cout << "Reading GWAS summary data from [" + string(gwasFileName) + "]." << endl;
gdata->_include.clear();
gdata->_snp_name_map.clear();
gdata->snpName.clear();
gdata->snpBp.clear();
gdata->allele_1.clear();
gdata->allele_2.clear();
gdata->freq.clear();
gdata->byz.clear();
gdata->seyz.clear();
gdata->pvalue.clear();
gdata->splSize.clear();
char buf[MAX_LINE_SIZE];
int lineNum(0);
gwasFile.getline(buf,MAX_LINE_SIZE);// the header
if(buf[0]=='\0')
{
printf("ERROR: the first row of the file %s is empty.\n",gwasFileName);
exit(EXIT_FAILURE);
}
vector<string> vs_buf;
split_string(buf, vs_buf, ", \t\n");
to_upper(vs_buf[0]);
if(vs_buf[0]!="SNP") {
printf("ERROR: %s should have headers that start with \"snp\".\n", gwasFileName);
exit(EXIT_FAILURE);
}
while(!gwasFile.eof())
{
gwasFile.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!=8) {
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("ERROR: the SNP name is \'NA\' in row %d.\n", lineNum+2);
exit(EXIT_FAILURE);
}
if(gdata->_snp_name_map.find(vs_buf[0]) != gdata->_snp_name_map.end()){
cout << "WARNING: Duplicated SNP ID \"" + vs_buf[0] + "\" ";
stringstream ss;
ss << vs_buf[0] << "_" << lineNum + 1;
vs_buf[0] = ss.str();
cout<<"has been changed to \"" + vs_buf[0] + "\".\n";
}
gdata->_snp_name_map.insert(pair<string, int>(vs_buf[0], lineNum));
gdata->snpName.push_back(vs_buf[0]);
if(vs_buf[1]=="NA" || vs_buf[1]=="na"){
printf("ERROR: allele1 is \'NA\' in row %d.\n", lineNum+2);
exit(EXIT_FAILURE);
}
to_upper(vs_buf[1]);
gdata->allele_1.push_back(vs_buf[1]);
if(vs_buf[2]=="NA" || vs_buf[2]=="na"){
printf("ERROR: allele2 is \'NA\' in row %d.\n", lineNum+2);
exit(EXIT_FAILURE);
}
to_upper(vs_buf[2]);
gdata->allele_2.push_back(vs_buf[2]);
if(vs_buf[3]=="NA" || vs_buf[3]=="na")
{
if(!warnnullfreq){
warnnullfreq=true;
printf("WARNING: frequency is \'NA\' in one or more rows.\n");
}
gdata->freq.push_back(-9);
}
else {
gdata->freq.push_back(atof(vs_buf[3].c_str()));
}
if(vs_buf[4]=="NA" || vs_buf[4]=="na"){
printf("WARNING: effect size is \'NA\' in row %d.\n", lineNum+2);
gdata->byz.push_back(0);
} else {
gdata->byz.push_back(atof(vs_buf[4].c_str()));
}
if(vs_buf[5]=="NA" || vs_buf[5]=="na"){
printf("WARNING: standard error is \'NA\' in row %d.\n", lineNum+2);
gdata->seyz.push_back(-9);
} else {
gdata->seyz.push_back(atof(vs_buf[5].c_str()));
}
gdata->pvalue.push_back(atof(vs_buf[6].c_str()));
gdata->splSize.push_back(atof(vs_buf[7].c_str()));
gdata->_include.push_back(lineNum);
lineNum++;
}
}
gdata->snpNum=gdata->_include.size();
cout <<"GWAS summary data of "<<gdata->snpNum << " SNPs to be included from [" + string(gwasFileName) + "]." << endl;
gwasFile.close();
}
/*
void update_include_map(eqtlInfo* eqtlinfo)
{
eqtlinfo->_incld_id_map.clear();
long size=0;
for(int i=0;i<eqtlinfo->_esi_include.size();i++)
{
eqtlinfo->_incld_id_map.insert(pair<int,int>(eqtlinfo->_esi_include[i],i));
if (size == eqtlinfo->_incld_id_map.size()) throw ("Error: Duplicated SNP IDs found: \"" + eqtlinfo->_esi_rs[eqtlinfo->_esi_include[i]] + "\".");
size = eqtlinfo->_incld_id_map.size();
}
}
*/
void read_esifile(eqtlInfo* eqtlinfo, string esifile, bool prtscr)
{
ifstream esi(esifile.c_str());
if (!esi) throw ("ERROR: can not open the file [" + esifile + "] to read.");
if(prtscr) cout << "Reading xQTL SNP information from [" + esifile + "]." << endl;
eqtlinfo->_esi_chr.clear();
eqtlinfo->_esi_rs.clear();
eqtlinfo->_esi_gd.clear();
eqtlinfo->_esi_bp.clear();
eqtlinfo->_esi_allele1.clear();
eqtlinfo->_esi_allele2.clear();
eqtlinfo->_esi_include.clear();
eqtlinfo->_snp_name_map.clear();
eqtlinfo->_esi_freq.clear();
char buf[MAX_LINE_SIZE];
vector<string> vs_buf;
int lineNum(0);
bool ptrnullfrq=false;
while(!esi.eof())
{
esi.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!=6 && col_num!=7) {
printf("ERROR: the number of columns is incorrect in row %d!\n", lineNum+1);
exit(EXIT_FAILURE);
}
if(vs_buf[0]=="NA" || vs_buf[0]=="na" || vs_buf[0]=="0"){
printf("ERROR: chromosome is \"NA\" in row %d.\n",lineNum+1);
exit(EXIT_FAILURE);
}
int tmpchr;
if(vs_buf[0]=="X" || vs_buf[0]=="x") tmpchr=23;
else if(vs_buf[0]=="Y" || vs_buf[0]=="y") tmpchr=24;
else tmpchr=atoi(vs_buf[0].c_str());
eqtlinfo->_esi_chr.push_back(tmpchr);
if(vs_buf[1]=="NA" || vs_buf[1]=="na"){
printf("ERROR: the SNP name is \'NA\' in row %d.\n", lineNum+1);
exit(EXIT_FAILURE);
}
if(eqtlinfo->_snp_name_map.find(vs_buf[1]) != eqtlinfo->_snp_name_map.end()){
cout << "WARNING: Duplicated SNP ID \"" + vs_buf[1] + "\" ";
stringstream ss;
ss << vs_buf[1] << "_" << lineNum + 1;
vs_buf[1] = ss.str();
cout<<"has been changed to \"" + vs_buf[1] + "\".\n";
}
eqtlinfo->_snp_name_map.insert(pair<string, int>(vs_buf[1], lineNum));
eqtlinfo->_esi_rs.push_back(vs_buf[1]);
eqtlinfo->_esi_gd.push_back(atoi(vs_buf[2].c_str()));
if(vs_buf[3]=="NA" || vs_buf[3]=="na"){
printf("ERROR: SNP BP is \'NA\' in row %d.\n", lineNum+1);
exit(EXIT_FAILURE);
}
eqtlinfo->_esi_bp.push_back(atoi(vs_buf[3].c_str()));
if(vs_buf[4]=="NA" || vs_buf[4]=="na") printf("WARNING: allele1 is \"NA\" in row %d.\n", lineNum+1);
to_upper(vs_buf[4]);
eqtlinfo->_esi_allele1.push_back(vs_buf[4]);
if(vs_buf[5]=="NA" || vs_buf[5]=="na") printf("WARNING: allele2 is \"NA\" in row %d.\n", lineNum+1);
to_upper(vs_buf[5]);
eqtlinfo->_esi_allele2.push_back(vs_buf[5]);
if(col_num==7)
{
if(vs_buf[6]=="NA" || vs_buf[6]=="na"){
if(!ptrnullfrq){
printf("WARNING: frequency is \"NA\" in one or more rows.\n");
ptrnullfrq=true;
}
eqtlinfo->_esi_freq.push_back(-9);
} else {
eqtlinfo->_esi_freq.push_back(atof(vs_buf[6].c_str()));
}
} else {
//printf("WARNING: frequency column missing.\n");
eqtlinfo->_esi_freq.push_back(-9);
}
eqtlinfo->_esi_include.push_back(lineNum);
lineNum++;
}
}
eqtlinfo->_snpNum=lineNum;
if(prtscr) cout << eqtlinfo->_snpNum << " SNPs to be included from [" + esifile + "]." << endl;
esi.close();
}
void read_epifile(eqtlInfo* eqtlinfo, string epifile, bool prtscr)
{
ifstream epi(epifile.c_str());
if (!epi) throw ("ERROR: can not open the file [" + epifile + "] to read.");
if(prtscr) cout << "Reading xQTL probe information from [" + epifile + "]." << endl;
eqtlinfo->_epi_chr.clear();
eqtlinfo->_epi_prbID.clear();
eqtlinfo->_epi_gd.clear();
eqtlinfo->_epi_bp.clear();
eqtlinfo->_epi_gene.clear();
eqtlinfo->_epi_orien.clear();
eqtlinfo->_include.clear();
eqtlinfo->_probe_name_map.clear();
char buf[MAX_LINE_SIZE];
int lineNum(0);
while(!epi.eof())
{
epi.getline(buf,MAX_LINE_SIZE);
lineNum++;
}
if(buf[0]=='\0') lineNum--;
eqtlinfo->_probNum=lineNum;
if(prtscr) cout << eqtlinfo->_probNum << " Probes to be included from [" + epifile + "]." << endl;
eqtlinfo->_epi_chr.resize(lineNum);
eqtlinfo->_epi_prbID.resize(lineNum);
eqtlinfo->_epi_gd.resize(lineNum);
eqtlinfo->_epi_bp.resize(lineNum);
eqtlinfo->_epi_gene.resize(lineNum);
eqtlinfo->_epi_orien.resize(lineNum);
eqtlinfo->_include.resize(lineNum);
epi.clear(ios::goodbit);
epi.seekg (0, ios::beg);
for(int i=0;i<lineNum;i++)
{
string tmpStr;
epi.getline(buf,MAX_LINE_SIZE);
istringstream iss(buf);
iss>>tmpStr;
if(tmpStr=="NA" || tmpStr=="na" || tmpStr=="-9") {
printf("ERROR: chromosome is \"NA\" in row %d.\n", i+1);
exit(EXIT_FAILURE);
}
int tmpchr;
if(tmpStr=="X" || tmpStr=="x") tmpchr=23;
else if(tmpStr=="Y" || tmpStr=="y") tmpchr=24;
else tmpchr=atoi(tmpStr.c_str());
eqtlinfo->_epi_chr[i]=tmpchr;
iss>>tmpStr;
eqtlinfo->_include[i]=i;
if(eqtlinfo->_probe_name_map.find(tmpStr) != eqtlinfo->_probe_name_map.end()){
cout << "Warning: Duplicated probe ID \"" + tmpStr + "\" ";
stringstream ss;
ss << tmpStr << "_" << i + 1;
tmpStr = ss.str();
cout<<"has been changed to \"" + tmpStr + "\".\n";
}
eqtlinfo->_probe_name_map.insert(pair<string, int>(tmpStr, i));
eqtlinfo->_epi_prbID[i]=tmpStr;
iss>>tmpStr;
eqtlinfo->_epi_gd[i]=atoi(tmpStr.c_str());
iss>>tmpStr;
if(tmpStr=="NA" || tmpStr=="na" || tmpStr=="0") {
printf("ERROR: probe BP is \"NA\" in row %d.\n", i+1);
exit(EXIT_FAILURE);
}
eqtlinfo->_epi_bp[i]=atoi(tmpStr.c_str());
iss>>tmpStr;
eqtlinfo->_epi_gene[i]=tmpStr.c_str();
iss>>tmpStr;
eqtlinfo->_epi_orien[i]=tmpStr.c_str()[0];
}
epi.close();
}
int shown(string besdfile)
{
string fname=besdfile+".besd";
FILE* besd=fopen(fname.c_str(), "rb");
if(!besd)
{
printf ( "ERROR: Couldn't open file %s\n", fname.c_str());
exit (EXIT_FAILURE);
}
printf("Reading sample size from %s \n",fname.c_str());
uint32_t indicator;
int ss=-9;
if(fread(&indicator, sizeof(uint32_t),1, besd)!=1)
{
printf("ERROR: File %s read failed!\n", fname.c_str());
exit (EXIT_FAILURE);
}
if(indicator==SPARSE_FILE_TYPE_3 || indicator==DENSE_FILE_TYPE_3)
{
if(fread(&ss, sizeof(int),1, besd)!=1)
{
printf("ERROR: File %s read failed!\n", fname.c_str());
exit (EXIT_FAILURE);
}
if(ss==-9)
{
printf("The sample size is missing. You may use --add-n to add it to the BESD file.\n");
}
else {
printf("The sample size is %d\n",ss);
}
} else {
printf("This file is in the old BESD format which doesn't contain the information of sample size.\n");
}
fclose(besd);
return ss;
}
void read_besdfile(eqtlInfo* eqtlinfo, string besdfile, bool prtscr)
{
if (eqtlinfo->_include.size() == 0) throw ("Error: No probe is retained for analysis.");
if (eqtlinfo->_esi_include.size() == 0) throw ("Error: No SNP is retained for analysis.");
eqtlinfo->_cols.clear();
eqtlinfo->_rowid.clear();
eqtlinfo->_val.clear();
eqtlinfo->_valNum = 0;
eqtlinfo->_bxz.clear();
eqtlinfo->_sexz.clear();
// the fastest way is using malloc and memcpy
char SIGN[sizeof(uint64_t)+8];
ifstream besd(besdfile.c_str(), ios::in|ios::binary);
if(!besd)
{
fprintf (stderr, "%s: Couldn't open file %s\n",
besdfile.c_str(), strerror (errno));
exit (EXIT_FAILURE);
}
if(prtscr) cout << "Reading xQTL summary data from [" + besdfile + "]." << endl;
besd.read(SIGN, 4);
uint32_t gflag = *(uint32_t *)SIGN;
/*
if(gflag==4){
// clear datastruct for sparse befor read dense
eqtlinfo->_cols.clear();
eqtlinfo->_rowid.clear();
eqtlinfo->_val.clear();
eqtlinfo->_valNum = 0;
uint64_t memsize2use=eqtlinfo->_include.size()*eqtlinfo->_esi_include.size()*2*sizeof(float);
if(memsize2use>0x200000000) printf("WARNING: %llu GB should be allocated for your besd file.\n",memsize2use>>30);
eqtlinfo->_bxz.resize(eqtlinfo->_include.size());
eqtlinfo->_sexz.resize(eqtlinfo->_include.size());
for(unsigned int i=0;i<eqtlinfo->_include.size();i++)
{
eqtlinfo->_bxz[i].resize(eqtlinfo->_esi_include.size());
eqtlinfo->_sexz[i].resize(eqtlinfo->_esi_include.size());
}
char* buffer;
buffer = (char*) malloc (sizeof(char)*eqtlinfo->_snpNum<<3);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (1);}
float* ft;
float* se_ptr;
for(int j=0;j<eqtlinfo->_esi_include.size();j++)
{
unsigned long sid=eqtlinfo->_esi_include[j];
besd.seekg(((sid<<1)*eqtlinfo->_probNum+1)<<2);
memset(buffer,0,sizeof(char)*eqtlinfo->_probNum<<3);
besd.read(buffer,eqtlinfo->_probNum<<3);
ft=(float *)buffer;
for (int i = 0; i<eqtlinfo->_include.size(); i++) eqtlinfo->_bxz[i][j] = *(ft + eqtlinfo->_include[i]);
se_ptr = ft + eqtlinfo->_probNum;
for (int i = 0; i<eqtlinfo->_include.size(); i++) eqtlinfo->_sexz[i][j] = *(se_ptr + eqtlinfo->_include[i]);
}
if(prtscr) std::cout << "eQTL summary-level statistics of " << eqtlinfo->_include.size() << " Probes and " << eqtlinfo->_esi_include.size() << " SNPs to be included from [" + besdfile + "]." << endl;
if(eqtlinfo->_include.size()<eqtlinfo->_probNum ) update_epi(eqtlinfo);
if(eqtlinfo->_esi_include.size()<eqtlinfo->_snpNum) update_esi(eqtlinfo);
free(buffer);
}
*/
if(gflag == 0x40000000){
// clear datastruct for dense befor read sparse
cout<<"This is an old file format. Please use --make-besd to update the file format."<<endl;
eqtlinfo->_bxz.clear();
eqtlinfo->_sexz.clear();
uint64_t colNum=(eqtlinfo->_probNum<<1)+1;
uint64_t valNum;
uint64_t lSize;
char* buffer;
besd.seekg(0,besd.end);
lSize = besd.tellg();
besd.seekg(4); // same as besd.seekg(4, besd.beg);
besd.read(SIGN, sizeof(uint64_t));
valNum=*(uint64_t *)SIGN;
if( lSize - (sizeof(float) + sizeof(uint64_t) + (colNum+valNum)*sizeof(uint32_t) + valNum*sizeof(float)) != 0)
{
printf("The file size is %llu",lSize);
printf(" %zu + %zu + %lld + %lld + %lld \n",sizeof(float),sizeof(uint64_t), colNum*sizeof(uint32_t),valNum*sizeof(uint32_t), valNum*sizeof(float));
printf("ERROR: failed in binary file check.\n");
exit(EXIT_FAILURE);
}
buffer = (char*) malloc (sizeof(char)*(lSize));
if (buffer == NULL) {fputs ("Memory error.\n",stderr); exit (1);}
besd.read(buffer,lSize);
if (besd.gcount()+sizeof(float) + sizeof(uint64_t) != lSize) {fputs ("Reading error",stderr); exit (2);}
uint32_t* ptr;
ptr=(uint32_t *)buffer;
if(eqtlinfo->_include.size()<eqtlinfo->_probNum || eqtlinfo->_esi_include.size()<eqtlinfo->_snpNum)
{
eqtlinfo->_cols.resize((eqtlinfo->_include.size()<<1)+1);
eqtlinfo->_cols[0]=*ptr;
uint32_t* row_ptr;
row_ptr=ptr+colNum;
float* val_ptr;
val_ptr=(float*)(row_ptr+valNum);
map<int, int > _incld_id_map;
long size = 0;
for (int i = 0; i<eqtlinfo->_esi_include.size(); i++)
{
_incld_id_map.insert(pair<int, int>(eqtlinfo->_esi_include[i], i));
if (size == _incld_id_map.size()) throw ("Error: Duplicated SNP IDs found: \"" + eqtlinfo->_esi_rs[eqtlinfo->_esi_include[i]] + "\".");
size = _incld_id_map.size();
}
for(int i=0;i<eqtlinfo->_include.size();i++)
{
uint32_t pid=eqtlinfo->_include[i];
uint32_t pos=*(ptr+(pid<<1));
uint32_t pos1=*(ptr+(pid<<1)+1);
uint32_t num=pos1-pos;
uint32_t real_num=0;
for(int j=0;j<num<<1;j++)
{
uint32_t rid=*(row_ptr+pos+j);
map<int, int>::iterator iter;
iter=_incld_id_map.find(rid);
if(iter!=_incld_id_map.end())
{
int sid=iter->second;
// long sid=find(eqtlinfo->_esi_include.begin(),eqtlinfo->_esi_include.end(),rid)-eqtlinfo->_esi_include.begin(); //slow
// if(sid<eqtlinfo->_esi_include.size())
// {
eqtlinfo->_rowid.push_back(sid);
eqtlinfo->_val.push_back(*(val_ptr+pos+j));
real_num++;
}
}
eqtlinfo->_cols[(i<<1)+1]=(real_num>>1)+eqtlinfo->_cols[i<<1];
eqtlinfo->_cols[i+1<<1]=real_num+eqtlinfo->_cols[i<<1];
}
eqtlinfo->_valNum = eqtlinfo->_val.size();
if(prtscr) cout<<"xQTL summary data of "<<eqtlinfo->_include.size()<<" Probes and "<<eqtlinfo->_esi_include.size()<<" SNPs to be included from [" + besdfile + "]." <<endl;
if(eqtlinfo->_include.size()<eqtlinfo->_probNum ) update_epi(eqtlinfo);
if(eqtlinfo->_esi_include.size()<eqtlinfo->_snpNum) update_esi(eqtlinfo);
}
else
{
eqtlinfo->_cols.resize(colNum);
eqtlinfo->_rowid.resize(valNum);
eqtlinfo->_val.resize(valNum);
for(int i=0;i<colNum;i++) eqtlinfo->_cols[i]=*ptr++;
for(int i=0;i<valNum;i++) eqtlinfo->_rowid[i]=*ptr++;
float* val_ptr=(float*)ptr;
for(int i=0;i<valNum;i++) eqtlinfo->_val[i]=*val_ptr++;
eqtlinfo->_valNum = valNum;
if(prtscr) cout<<"xQTL summary-level statistics of "<<eqtlinfo->_probNum<<" Probes and "<<eqtlinfo->_snpNum<<" SNPs to be included from [" + besdfile + "]." <<endl;
}
// terminate
free (buffer);
}
else if(gflag == DENSE_FILE_TYPE_1 || gflag == DENSE_FILE_TYPE_3)
{
// clear datastruct for sparse befor read dense
if(gflag==DENSE_FILE_TYPE_3)
{
int length=(RESERVEDUNITS-1)*sizeof(int);
char* indicators=new char[length];
besd.read(indicators,length);
int* tmp=(int *)indicators;
int ss=*tmp++;
if(ss!=-9)
{
printf("The sample size is %d.\n",ss);
}
if(*tmp++!=eqtlinfo->_snpNum)
{
printf("ERROR: The SNPs in your .esi file are not in consistency with the one in .besd file %s.\n", besdfile.c_str());
exit(EXIT_FAILURE);
}
if(*tmp++!=eqtlinfo->_probNum)
{
printf("ERROR: The probes in your .epi file are not in consistency with the one in .besd file %s.\n", besdfile.c_str());
exit(EXIT_FAILURE);
}
delete[] indicators;
}
int infoLen=sizeof(uint32_t);
if(gflag==DENSE_FILE_TYPE_3) infoLen=RESERVEDUNITS*sizeof(int);
eqtlinfo->_cols.clear();
eqtlinfo->_rowid.clear();
eqtlinfo->_val.clear();
eqtlinfo->_valNum = 0;
uint64_t memsize2use=eqtlinfo->_include.size()*eqtlinfo->_esi_include.size()*2*sizeof(float);
if(memsize2use>0x200000000) printf("WARNING: %llu GB should be allocated for your besd file.\n",memsize2use>>30);
eqtlinfo->_bxz.resize(eqtlinfo->_include.size());
eqtlinfo->_sexz.resize(eqtlinfo->_include.size());
for(unsigned int i=0;i<eqtlinfo->_include.size();i++)
{
eqtlinfo->_bxz[i].resize(eqtlinfo->_esi_include.size());
eqtlinfo->_sexz[i].resize(eqtlinfo->_esi_include.size());
}
char* buffer;
buffer = (char*) malloc (sizeof(char)*eqtlinfo->_snpNum<<3);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (1);}
float* ft;
float* se_ptr;
if (eqtlinfo->_include.size()<eqtlinfo->_probNum || eqtlinfo->_esi_include.size()<eqtlinfo->_snpNum) //means with the parameter --extract-probe. This also can read all the probes, but currently I don't think it is good for too many I/Os.
{
for(int i=0;i<eqtlinfo->_include.size();i++)
{
unsigned long pid=eqtlinfo->_include[i];
besd.seekg(((pid*eqtlinfo->_snpNum)<<3)+infoLen);
memset(buffer,0,sizeof(char)*eqtlinfo->_snpNum<<3);
besd.read(buffer,eqtlinfo->_snpNum<<3);
ft=(float *)buffer;
for (int j = 0; j<eqtlinfo->_esi_include.size(); j++) eqtlinfo->_bxz[i][j] = *(ft + eqtlinfo->_esi_include[j]);
se_ptr = ft + eqtlinfo->_snpNum;
for (int j = 0; j<eqtlinfo->_esi_include.size(); j++) eqtlinfo->_sexz[i][j] = *(se_ptr + eqtlinfo->_esi_include[j]);
}
if(prtscr) std::cout << "xQTL summary-level statistics of " << eqtlinfo->_include.size() << " Probes and " << eqtlinfo->_esi_include.size() << " SNPs to be included from [" + besdfile + "]." << endl;
if(eqtlinfo->_include.size()<eqtlinfo->_probNum ) update_epi(eqtlinfo);
if(eqtlinfo->_esi_include.size()<eqtlinfo->_snpNum) update_esi(eqtlinfo);
}
else
{
//without --extract-probe, read with less I/O. and need not to update epi.
//read with static buffer. If dynamic buffer, 2GB per I/O can be more efficient.
/*
unsigned long long count=0;
while(!besd.eof())
{
besd.read(buf,MAX_LINE_NUM);
unsigned long Bread=besd.gcount();
buf[Bread]='\0';
char* ptr=buf;
//while(*ptr != '\0') //can not use this, too many 0x00 in buf
while(Bread)
{
unsigned long pid=count/eqtlinfo->_snpNum;
unsigned long sid=count++%eqtlinfo->_snpNum;
ft=(float *)ptr;
if(pid&1) eqtlinfo->_sexz[pid>>1][sid]=*ft;
else eqtlinfo->_bxz[pid>>1][sid]=*ft;
ptr+=4;
Bread-=4;
}
}
cout<<"eQTL summary-level statistics of "<<eqtlinfo->_probNum<<" Probes and "<<eqtlinfo->_snpNum<<" SNPs to be included from [" + besdfile + "]." <<endl;
*/
/*
// besd.seekg(0,besd.end);
// uint64_t lSize = besd.tellg();
// lSize-=4;
// besd.seekg(4); // same as besd.seekg(4, besd.beg);
uint64_t alread=0;
char* buff;
uint64_t buffszie=0x40000000;
buff = (char*) malloc (sizeof(char)*buffszie);
if (buff == NULL) {fputs ("Memory error",stderr); exit (1);}
memset(buff,0,sizeof(char)*buffszie);
uint64_t count=0;
while(!besd.eof())
{
besd.read(buff,buffszie);
unsigned long Bread=besd.gcount();
alread+=Bread;