-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_function.cpp
1121 lines (1039 loc) · 32.3 KB
/
conditional_function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<iostream>
#include<fstream>
#include <armadillo>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <sstream>
#include <string>
#include <vector>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <regex>
#include "gemma_param.h"
#include "gemma.h"
#include <iomanip>
#ifndef _MEQTLPOLYGMODEL_H
#define _MEQTLPOLYGMODEL_H
#include "MeQTLPolyGModel.h"
#endif
#include "conditional_function.h"
using namespace std;
using namespace arma;
//Read phe+genotype table
void read_table(string data, vector< vector<double> > &test, vector<string> &ind, vector<string> &variant)
{
// cout<<"Come into input file to read information"<<endl;
ifstream FHIN1(data.c_str(),ifstream::in);
string a;
int b=0;
while(FHIN1 &&getline(FHIN1,a))
{
b++;
if(b==1)
{
stringstream ss(a);
string word;
ss>>word;
while(ss && ss>>word)
{
variant.push_back(word);
}
} else
{
double c=0;
stringstream ss(a);
string waste;
ss>>waste;
ind.push_back(waste);
vector<double> T;
while(ss && ss>>c)
{
T.push_back(c);
}
test.push_back(T);
}
}
// cout<<"Read phenotype and genotype finished"<<endl;
FHIN1.clear();
FHIN1.close();
}
//Find the index of a specific string in a string vector
int match(vector<string> &data, string target)
{
cout<<"Come into function match"<<endl;
// cout<<"Target data is: "<<endl;
int j=-1;
vector<string>::iterator Col;
for(Col=data.begin();Col!=data.end();Col++)
{
j++;
if((*Col)==target)
{
cout<<"Good, the string is detected"<<endl;
cout<<"Index for the target string is: "<<j<<endl;
return j;
}
}
return j;
}
//Calculate the variance inflation factor to determine whether include the explored variant or not
double calculate_VIF(mat &input,vector<string> &variant,string target,vector<string> &detected)
{
cout<<"Come to function calculate_VIF"<<endl;
int res=match(variant,target);
mat X_chosen=mat(input.n_rows,detected.size(),fill::zeros);
for(int i=0;i<detected.size();i++)
{
int ind=match(variant, detected[i]);
X_chosen.col(i)=input.col(ind);
}
cout<<"Genotype get, and start to calculate VIF"<<endl;
mat stat_test=input.col(res);
mat XtX=trans(X_chosen)*X_chosen;
mat inv_XtX=pinv(XtX);
mat res_test=inv_XtX*trans(X_chosen)*stat_test;
mat resi=stat_test-X_chosen*res_test;
double phe_variance=stddev(resi.col(0));
colvec residuals = stat_test - X_chosen * res_test;
double s2=0;
double s=0;
for(int i_x=0;i_x<input.n_rows;i_x++)
{
s2+=residuals(i_x)*residuals(i_x);
s+= stat_test(i_x,0)*stat_test(i_x,0);
}
double r2=1-s2/s;
double vif=1/r2;
return vif;
}
//read GEMMA result output
void readgemma(string input, vector < vector<double> > &out_gemma, vector<string> & variant)
{
ifstream FHIN(input.c_str(),ios::in);
if(!FHIN)
{
// cout<<"There is some error for the input, please have a check"<<endl;
return ;
}
string A;
getline(FHIN,A);
while(FHIN && getline(FHIN, A))
{
stringstream ss(A);
vector<string> X;
string B;
vector<double> Y;
ss>>B;
ss>>B;
variant.push_back(B);
for(int i=3;i<8;i++)
{
ss>>B;
}
double test;
double test1;
double test2;
ss>>test;
Y.push_back(test);
ss>>test1;
ss>>test2;
ss>>test2;
Y.push_back(test2);
test=test/test1;
if(test<0)
{
test=-test;
}
// ss>>test;
// ss>>test;
// ss>>test;
Y.push_back(test);
out_gemma.push_back(Y);
}
FHIN.clear();
FHIN.close();
}
//Attached new residual phenotype, removing effect of detected SNPs, to plink fam file.
int updatefam(vector< vector<string> > &input , mat &value,string output)
{
// cout<<"Come into updatefam fucntion"<<endl;
string A;
fstream FHOU(output.c_str(),ios::out);
if(!FHOU)
{
cout<<"Error for input or output"<<endl;
return (1);
}
if(value.n_rows!=input.size())
{
// cout<<"The number of individuals in phenotype doesn't match that in fam file "<<endl;
return (1);
}
for(int i=0;i<input.size();i++)
{
FHOU<<input[i][0];
for(int j=1;j<input[i].size();j++)
{
FHOU<<" "<<input[i][j];
}
FHOU<<" "<<value(i,0)<<endl;
}
FHOU.clear();
FHOU.clear();
return (0);
}
//Make a copy of plink bim file
void copybim(string input, string output)
{
ifstream FHIN(input.c_str(),ios::in);
string A;
fstream FHOU(output.c_str(),ios::out);
if(!FHIN || !FHOU)
{
cout<<"Error for input or output"<<endl;
return;
}
while(FHIN && getline(FHIN,A))
{
FHOU<<A;
FHOU<<endl;
}
FHIN.clear();
FHOU.clear();
FHIN.close();
FHOU.close();
}
//Extract a specific set of variants, and output a new bim file
void copybim(string input, string output, vector<int> &index)
{
ifstream FHIN(input.c_str(),ios::in);
string A;
fstream FHOU(output.c_str(),ios::out);
if(!FHIN || !FHOU)
{
cout<<"Error for input or output"<<endl;
return;
}
int val=-1;
while(FHIN && getline(FHIN,A))
{
val++;
if(find(index.begin(), index.end(), val) != index.end())
{
FHOU<<A;
FHOU<<endl;
}
}
FHIN.clear();
FHOU.clear();
FHIN.close();
FHOU.close();
}
//Make a copy of fam file
void copyfam(string input, string output)
{
ifstream FHIN(input.c_str(),ios::in);
string A;
fstream FHOU(output.c_str(),ios::out);
if(!FHIN || !FHOU)
{
cout<<"Error for input or output"<<endl;
}
while(FHIN && getline(FHIN,A))
{
stringstream ss(A);
vector<string> B;
string Test;
while(ss && ss>>Test)
{
FHOU<<Test<<" ";
}
FHOU<<endl;
}
FHIN.clear();
FHIN.close();
FHOU.clear();
FHOU.close();
}
//Make a copy of bed file
void outputbed(mat & _snp_2, mat & _snp_1, string &output)
{
int i=0, pos=0, j=0;
string OutBedFile=output;
fstream OutBed(OutBedFile.c_str(), ios::out|ios::binary);
if(!OutBed) throw("Error: can not open the file ["+OutBedFile+"] to write.");
// cout<<"Writing genotypes to PLINK BED file ["+OutBedFile+"] ..."<<endl;
bitset<8> b;
char ch[1];
b.reset();
b.set(2); b.set(3); b.set(5); b.set(6);
ch[0] = (char)b.to_ulong();
OutBed.write(ch,1);
b.reset();
b.set(0); b.set(1); b.set(3); b.set(4);
ch[0] = (char)b.to_ulong();
OutBed.write(ch,1);
b.reset();
b.set(0);
ch[0] = (char)b.to_ulong();
OutBed.write(ch,1);
int n_sid=_snp_2.n_rows;
int n_ind=_snp_2.n_cols;
// cout<<"Number of individuals is: "<<n_ind<<endl;
// cout<<"Number of variants is: "<<n_sid;
// cout<<"Right now, it is OK"<<endl;
for(i=0; i<n_sid; i++){
pos=0;
b.reset();
for(j=0; j<n_ind; j++){
b[pos++]=(!_snp_2(i,j));
b[pos++]=(!_snp_1(i,j));
if(pos>7 || j==n_ind-1){
ch[0]=(char)b.to_ulong();
OutBed.write(ch,1);
pos=0;
b.reset();
}
}
}
OutBed.close();
}
//Extract a specific set of variants from a bim file
void readbim(string file,map<string, int> &order)
{
int ibuf=-1;
string A;
string cbuf="0";
double dbuf=0.0;
string str_buf;
ifstream Bim(file.c_str());
if(!Bim) throw("Error: can not open the file ["+file+"] to read.");
// cout<<"Reading PLINK BIM file from ["+file+"]."<<endl;
while(Bim && getline(Bim, A))
{
ibuf++;
stringstream ss(A);
string waste;
ss>>waste;
ss>>waste;
// cout<<"waste is: "<<waste<<", and ibuf is: "<<ibuf<<endl;
order.insert(pair<string,int>(waste,ibuf));
}
// cout<<"Read bim file is over"<<endl;
Bim.close();
}
//Read a bim file
void readbim(string file,vector<string>&variant)
{
int ibuf=-1;
string A;
string cbuf="0";
double dbuf=0.0;
string str_buf;
ifstream Bim(file.c_str());
if(!Bim) throw("Error: can not open the file ["+file+"] to read.");
while(Bim && getline(Bim, A))
{
ibuf++;
stringstream ss(A);
string waste;
ss>>waste;
ss>>waste;
variant.push_back(waste);
}
Bim.close();
}
//Read a fam file, and calcuate the individual number
int readfam(string file)
{
cout<<"Come to estimate individual number"<<endl;
ifstream Fam(file.c_str());
string A;
if(!Fam) throw("Error: can not open the file ["+file+"] to read.");
cout<<"Reading PLINK FAM file from ["+file+"]."<<endl;
int i=0;
while(Fam && getline(Fam, A))
{
i++;
}
Fam.clear();
Fam.close();
cout<<"Finish estimation"<<endl;
return i;
}
//Read a fam file and extract phenotype
int readfam(string file,vector<double> &pheno, vector< vector<string> > &Fam_infor)
{
ifstream Fam(file.c_str());
string A;
if(!Fam) throw("Error: can not open the file ["+file+"] to read.");
cout<<"Reading PLINK FAM file from ["+file+"]."<<endl;
while(Fam && getline(Fam, A))
{
stringstream ss(A);
vector<string> T;
string TT;
for(int j=0;j<5;j++)
{
ss>>TT;
T.push_back(TT);
}
Fam_infor.push_back(T);
double TTT;
ss>>TTT;
pheno.push_back(TTT);
}
Fam.clear();
Fam.close();
}
//Read a bim file, and calculate the number of variants
int readbim(string file)
{
int ibuf=0;
string A;
string cbuf="0";
double dbuf=0.0;
string str_buf;
ifstream Bim(file.c_str());
if(!Bim) throw("Error: can not open the file ["+file+"] to read.");
cout<<"Reading PLINK BIM file from ["+file+"]."<<endl;
while(Bim && getline(Bim, A))
{
ibuf++;
}
Bim.close();
return ibuf;
}
//Given a r2 cutoff, extract the variants locating in high LD with a specific varaint
void extractvariant(string input, string output, double r2_cutoff, string target)
{
int i=0, j=0, k=0;
string strfam="fam";
string strbim="bim";
string famfile=input;
string bimfile=input;
famfile.replace(famfile.end()-3,famfile.end(),strfam);
bimfile.replace(bimfile.end()-3,bimfile.end(),strbim);
string famout=output;
string bimout=output;
famout.replace(famout.end()-3,famout.end(),strfam);
bimout.replace(bimout.end()-3,bimout.end(),strbim);
int nsnp=readbim(bimfile);
int nind=readfam(famfile);
mat _snp_2 =mat(nsnp,nind, fill::zeros);
mat _snp_1 =mat(nsnp,nind, fill::zeros);
char ch[1];
bitset<8> b;
fstream BIT(input.c_str(), ios::in|ios::binary);
if(!BIT) throw("Error: can not open the file ["+input+"] to read.");
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<nsnp; j++)
{
for(i=0, indi_indx=0; i<nind;)
{
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 < nind)
{
_snp_2(snp_indx,indi_indx)=(!b[k++]);
_snp_1(snp_indx,indi_indx)=(!b[k++]);
indi_indx++;
i++;
}
}
snp_indx++;
}
cout<<"Reading PLINK BED file is over"<<endl;
mat dosage=mat(nsnp,nind, fill::zeros);
for(int i=0;i<_snp_2.n_rows;i++)
{
for(int j=0;j<_snp_2.n_cols;j++)
{
if(_snp_2(i,j)==0 && _snp_1(i,j)==1)
{
dosage(i,j)=-9;
} else
{
dosage(i,j)=_snp_2(i,j)+_snp_1(i,j);
}
}
}
for(int i=0;i<dosage.n_rows;i++)
{
double Mean=0;
int num=0;
for(int j=0;j<dosage.n_cols;j++)
{
if(dosage(i,j)!=-9)
{
Mean+=dosage(i,j);
num++;
}
}
Mean/=num;
for(int j=0;j<dosage.n_cols;j++)
{
if(dosage(i,j)==-9)
{
dosage(i,j)=Mean;
}
}
}
map<string,int> order;
readbim(bimfile,order);
map<string,int>::iterator Ite;
Ite=order.find(target);
if(Ite==order.end())
{
return ;
}
int T_index=Ite->second;
// cout<<"T_index is: "<<T_index<<endl;
vector<int> index;
vector< vector<int> > _Snp_2;
vector< vector<int> > _Snp_1;
vector <int> C;
for(int x=0;x<_snp_2.n_rows;x++)
{
mat r2=cor(dosage.row(x),dosage.row(T_index));
// cout<<"r2 is: "<<r2<<endl;
if(r2(0,0)*r2(0,0)>=r2_cutoff)
{
C.push_back(x);
vector<int> Test1;
vector<int> Test2;
for(int y=0;y<_snp_2.n_cols;y++)
{
Test1.push_back(_snp_1(x,y));
Test2.push_back(_snp_2(x,y));
}
_Snp_2.push_back(Test2);
_Snp_1.push_back(Test1);
}
}
mat _snp_2_target=mat(_Snp_2.size(),_Snp_2[0].size(),fill::zeros);
mat _snp_1_target=mat(_Snp_1.size(),_Snp_1[0].size(),fill::zeros);
for(int i=0;i<_Snp_1.size();i++)
{
for(int j=0;j<_Snp_1[0].size();j++)
{
_snp_1_target(i,j)=_Snp_1[i][j];
_snp_2_target(i,j)=_Snp_2[i][j];
}
}
cout<<"Variants locating in high LD with target variant is over"<<endl;
outputbed(_snp_2_target,_snp_1_target,output);
copyfam(famfile,famout);
copybim(bimfile,bimout,C);
BIT.clear();
BIT.close();
}
//Make a copy of plink bed file
void copybed(string input, string output)
{
cout<<"Come into copybed function"<<endl;
int i=0, j=0, k=0;
string strfam="fam";
string strbim="bim";
string famfile=input;
string bimfile=input;
famfile.replace(famfile.end()-3,famfile.end(),strfam);
bimfile.replace(bimfile.end()-3,bimfile.end(),strbim);
int nsnp=readbim(bimfile);
int nind=readfam(famfile);
cout<<"Number of variants is: "<<nsnp<<endl;
cout<<"Number of individuals is: "<<nind<<endl;
mat _snp_2 =mat(nsnp,nind, fill::zeros);
mat _snp_1 =mat(nsnp,nind, fill::zeros);
char ch[1];
bitset<8> b;
fstream BIT(input.c_str(), ios::in|ios::binary);
if(!BIT) throw("Error: can not open the file ["+input+"] to read.");
// cout<<"Reading PLINK BED file from ["+input+"] 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<nsnp; j++)
{
for(i=0, indi_indx=0; i<nind;)
{
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 < nind)
{
_snp_2(snp_indx,indi_indx)=(!b[k++]);
_snp_1(snp_indx,indi_indx)=(!b[k++]);
indi_indx++;
i++;
}
}
snp_indx++;
}
outputbed(_snp_2,_snp_1,output);
BIT.clear();
BIT.close();
}
//If the phenotype and genotype are in plink format, convert it to dosage manner
void prepare_geno_phe(string input, string X_file, string YFile)
{
cout<<"Come into function to prepare genotype and phenotype"<<endl;
int i,j,k;
string bedfile=input+".bed";
string famfile=input+".fam";
string bimfile=input+".bim";
cout<<"bedfile is: "<<bedfile<<endl;
cout<<"famfile is: "<<famfile<<endl;
cout<<"bimfile is: "<<bimfile<<endl;
int nsnp=readbim(bimfile);
int nind=readfam(famfile);
vector<string> variant;
readbim(bimfile,variant);
mat _snp_2 =mat(nsnp,nind, fill::zeros);
mat _snp_1 =mat(nsnp,nind, fill::zeros);
cout<<"nsnp is: "<<nsnp<<endl;
cout<<"nind is: "<<nind<<endl;
char ch[1];
bitset<8> b;
fstream BIT(bedfile.c_str(), ios::in|ios::binary);
if(!BIT) throw("Error: can not open the file ["+input+"] to read.");
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<nsnp; j++)
{
for(i=0, indi_indx=0; i<nind;)
{
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 < nind)
{
_snp_2(snp_indx,indi_indx)=(!b[k++]);
_snp_1(snp_indx,indi_indx)=(!b[k++]);
indi_indx++;
i++;
}
}
snp_indx++;
}
cout<<"Reading PLINK BED file is over"<<endl;
mat dosage=mat(nsnp,nind, fill::zeros);
for(int i=0;i<_snp_2.n_rows;i++)
{
for(int j=0;j<_snp_2.n_cols;j++)
{
if(_snp_2(i,j)==0 && _snp_1(i,j)==1)
{
dosage(i,j)=-9;
} else
{
dosage(i,j)=_snp_2(i,j)+_snp_1(i,j);
}
}
}
for(int i=0;i<dosage.n_rows;i++)
{
double Mean=0;
int num=0;
for(int j=0;j<dosage.n_cols;j++)
{
if(dosage(i,j)!=-9)
{
Mean+=dosage(i,j);
num++;
}
}
Mean/=num;
for(int j=0;j<dosage.n_cols;j++)
{
if(dosage(i,j)==-9)
{
dosage(i,j)=Mean;
}
}
}
ofstream outfile1(X_file.c_str(), ios::out);
outfile1<<variant[0];
for(int x=1;x<variant.size();x++)
{
outfile1<<" "<<variant[x];
}
outfile1<<endl;
for(int i=0;i<dosage.n_cols;i++)
{
outfile1<<dosage(0,i);
for(int j=1;j<dosage.n_rows;j++)
{
outfile1<<" "<<dosage(j,i);
}
outfile1<<endl;
}
outfile1.close();
vector<double> pheno;
vector < vector<string> > fam_infor;
readfam(famfile,pheno,fam_infor);
ofstream outfile2(YFile.c_str(), ios::out);
for (int i = 0; i < pheno.size(); i++)
outfile2 << pheno[i] << endl;
outfile2.close();
}
//Just for test
int conditional_analysis(string input,string gene, int totalCausalSNP,float rho, bool histFlag)
{
cout<<"test"<<endl;
}
//Perform conditional analysis to detect peak signal
int conditional_analysis(string file, string gene, int totalCausalSNP,float rho, bool histFlag, double gamma,string weight, int nthread,string covariate, vector <string> &grm_file,string outputFile,string geno_file)
{
vector< vector<double> > Data;
vector<string> ind;
vector<string> variant;
cout<<"Read input file"<<endl;
cout<<"file is: "<<file<<endl;
cout<<"explored gene is: "<<gene<<endl;
// return (0);
read_table(file,Data,ind,variant);
cout<<"Reading input file is finished"<<endl;
mat data=mat(Data.size(),Data[0].size(),fill::zeros);
string out_test="";
string fam_file_test="";
for(int i=0;i<Data.size();i++)
{
for(int j=0;j<Data[i].size();j++)
{
data(i,j)=Data[i][j];
}
}
cout<<"Convert input vector into mat finished"<<endl;
int len=data.n_cols;
int dim=data.n_rows;
string out_text="";
vector<string> col;
for(int i=0;i<variant.size();i++)
{
col.push_back(variant[i]);
}
int j=0;
for(int fea=0;fea<col.size();fea++)
{
if (regex_match (col[fea],regex("(IL)(.*)") ) || regex_match (col[fea],regex("(phe)(.*)") ) )
{
j=j+1;
}
}
double cutoff=0.00001;
cout<<"It is safe"<<endl;
cout<<"Output individuals"<<endl;
cout<<"j is: "<<j<<endl;
for(int t=0;t<j;t++)
{
cout<<"Step1"<<endl;
cout<<"I am a"<<endl;
int x=0;
int bi=0;
vector<string> left;
int num_rev=0;
vector<string> removal;
int target=0;
double p_target;
mat data1=data;
vector<string> variant1;
for(int i=0;i<variant.size();i++)
{
variant1.push_back(variant[i]);
}
string bed_file=string("probe")+to_string(t);
cout<<"Copy genotype to each probe"<<endl;
cout<<"Copy bed file"<<endl;
copybed(geno_file+".bed",gene+"/"+bed_file+".bed");
cout<<"Copy fam file"<<endl;
copyfam(geno_file+".fam",gene+"/"+bed_file+".fam");
cout<<"Copy bim file"<<endl;
copybim(geno_file+".bim",gene+"/"+bed_file+".bim");
string fam=gene+"/"+bed_file+".fam";
vector<double> pheno;
vector < vector<string> > fam_infor;
readfam(fam,pheno,fam_infor);
mat fam_file=mat(pheno.size(),1,fill::zeros);
for(int i=0;i<pheno.size();i++)
{
fam_file(i,0)=pheno[i];
}
// cout<<"fam_file is: "<<fam_file<<endl;
int BIAO=fam_file.n_rows;
// cout<<"BIAO is: "<<BIAO<<endl;
BIAO=data.n_rows;
// cout<<"BIAO is: "<<BIAO<<endl;
fam_file.col(0)=data.col(t);
string fam_file_out=fam+"_adjust";
cout<<"updatefam"<<endl;
// updatefam(fam_infor,fam_file,fam_file_out);
updatefam(fam_infor,fam_file,fam);
cout<<"I am Ok here"<<endl;
for(int k=j;k<len;k++)
{
// cout<<"Step2"<<endl;
// cout<<"I am in"<<endl;
vector<string> col1;
for(int i=0;i<variant.size();i++)
{
col1.push_back(variant1[i]);
}
int len1=col1.size();
data1.col(t)=data.col(t);
target=0;
p_target=1;
int alle_target=0;
string file_gemma_out=string("output_of_GEMMA_")+gene;
PARAM cPar;
GEMMA cGemma;
cout<<"Inititation for GEMMA parameter and GEMMA core Over"<<endl;
// cGemma.Assign(argc, argv, cPar);
string str=gene+"/"+bed_file;
cPar.file_bfile=str;
str.assign(grm_file[0]);
cPar.file_kin=str;
str.assign(file_gemma_out);
cPar.file_out=str;
cPar.a_mode=1;
cout<<"Perform parameter checking"<<endl;
cPar.CheckParam();
cout<<"Parameter check is over"<<endl;
cGemma.BatchRun(cPar);
cout<<"GEMMA running is over"<<endl;
string file_gemma=string("./output")+"/output_of_GEMMA_"+gene+".assoc.txt";
cout<<"Output of gemma is: "<<file_gemma<<endl;
vector< vector<double> > Out_gemma;
vector<string> Out_gemma_variant;
readgemma(file_gemma,Out_gemma,Out_gemma_variant);
mat out_gemma=mat(Out_gemma.size(),Out_gemma[0].size(),fill::zeros);
for(int i=0;i<Out_gemma.size();i++)
{
for(int j=0;j<Out_gemma[i].size();j++)
{
out_gemma(i,j)=Out_gemma[i][j];
}
}
// int target_gemma =index_min(out_gemma.col(1));
int target_gemma =index_max(out_gemma.col(2));
cout<<"position for detected SNP is: "<<target_gemma<<endl;
cout<<"detected SNP in GEMMA is: "<<Out_gemma_variant[target_gemma]<<endl;
target=match(col1,Out_gemma_variant[target_gemma]);
p_target=out_gemma(target_gemma,1);
double beta_target =out_gemma(target_gemma,0);
cout<<"beta value is: "<<beta_target<<endl;
for(int X=0;X<removal.size();X++)
{
cout<<"removal is: "<<removal[X]<<endl;
}
cout<<"p_target is: "<<p_target<<", target is: "<<target<<" and SNP is: "<<Out_gemma_variant[target_gemma]<<endl;
cout<<"If there are some peak signals detected, remove"<<endl;
while(removal.size()>1&& match(removal,Out_gemma_variant[target_gemma])!=-1)
{
cout<<"Please skip this SNP "<<Out_gemma_variant[target_gemma]<<endl;
out_gemma.shed_row(target_gemma);
Out_gemma_variant.erase(Out_gemma_variant.begin()+target_gemma);
cout<<"Try to find the index of minimum p value"<<endl;
target_gemma =index_min(out_gemma.col(1));
target=match(col1,Out_gemma_variant[target_gemma]);
p_target=out_gemma(target_gemma,1);
if(p_target>cutoff)
{
break;
}
beta_target =out_gemma(target_gemma,0);
}
cout<<"p_target is:"<<p_target<<", target is: "<<target<<" and SNP is: "<<Out_gemma_variant[target_gemma]<<endl;
if(p_target>cutoff)
{
cout<<"No significant variant found"<<endl;
break ;
}
if(p_target<=cutoff)
{
cout<<"bi is: "<<bi<<endl;
if(bi>0)
{
cout<<"Some peak signals are already detected"<<endl;
int y=0;
vector <string> left_test;
for(int test=0;test<left.size();test++)
{
left_test.push_back(left[test]);
}
left_test.push_back(col1[target]);
// cout<<"left_test is: "<<left_test<<endl;
vector<string>::iterator z;
for(z=left_test.begin();z!=left_test.end();z++)
{
vector<string> col_test;
left_test.erase(z);
for(int i=0;i<left_test.size();i++)
{
col_test.push_back(left_test[i]);
}
cout<<"Come to calculate VIF"<<endl;
double vif=calculate_VIF(data,variant,(*z),col_test);
cout<<"VIF value is: "<<vif<<endl;
if(vif>=10)
{
y=1;
}
}
if(y==1)
{
cout<<"Sorry, skip this SNP, as it locates in high LD with detected variants"<<endl;
num_rev++;
removal.push_back(col1[target]);
data1.shed_row(target);
col1.erase(col1.begin()+target);
int z=0;
while(y==1)
{
y=0;
int test=index_min(out_gemma.col(1));
out_gemma.shed_row(test);
int target=match(col1,Out_gemma_variant[target_gemma]);
while(target<0)
{
out_gemma.shed_row(target_gemma);
target_gemma=index_min(out_gemma.col(1));
target=match(col1,Out_gemma_variant[target_gemma]);
cout<<"Please skip this SNP, as it is in the list of removal"<<endl;
}
p_target=out_gemma(target_gemma,1);
beta_target=out_gemma(target_gemma,0);
if(p_target>cutoff)
{
z=1;
break;
}
if(p_target<=cutoff)
{
y=0;
vector<string>::iterator z;
for(z=left_test.begin();z!=left_test.end();z++)
{
vector<string> col_test;
left_test.erase(z);
for(int i=0;i<left_test.size();i++)
{
col_test.push_back(left_test[i]);