-
Notifications
You must be signed in to change notification settings - Fork 1
/
genepi.cpp
1375 lines (1035 loc) · 32.5 KB
/
genepi.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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2006 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <functional>
#include <cmath>
#include "plink.h"
#include "options.h"
#include "sets.h"
#include "helper.h"
#include "stats.h"
#include "crandom.h"
#include "linear.h"
#include "logistic.h"
typedef vector<long double> vector_tld;
// Function that implements Pillai's (1964) approximation to upper
// distribution of the largest canonical correlatio
// Function (bico) to estimate combinations choose(n,k)
double factln(int n)
{
double gammln(double xx);
static double a[101];
if (n < 0) error("Negative factorial in routine factln");
if (n <= 1) return 0.0;
if (n <= 100 ) return a[n] ? a[n] : (a[n]=gammln(n+1.0));
else return gammln(n+1.0);
}
double bico(int n, int k)
{
double factln(int n);
return floor(0.5+exp(factln(n)-factln(k)-factln(n-k)));
}
// Beta function
double betaln(double z, double w)
{
double gammln(double xx);
return gammln(z)+gammln(w)-gammln(z+w);
}
// Pillai's helper
double C(int s, double m, double n)
{
// // Components 1 and 2
// double c1,c2,temp;
// c1=c2=temp=0;
// for (int i=1; i<s+1; i++)
// {
// c1 += gammln(0.5 * (2*m + 2*n + s + i + 2));
// c2 += gammln(0.5 * (2*m + i + 1)) + gammln(0.5 * (2*n + i + 1)) + gammln(0.5 * i) ;
// }
// // ... and finally:
// double out = log(pow(3.141593,0.5*s)) + c1 - c2;
// return out;
}
// Function that will be integrated
double I(const double t, const double m, const double n)
{
return pow(1/t,-1*m) * pow(1-t,n);
//return pow(t,m) * pow(1-t,n);
//return pow(t,2) * pow(1-t,991.5);
}
// Pillai's main function
long double pillai(int N, int p, int q, double lroot)
{
// // Required input for Pillai's approximation. p must be < q; x is the
// // largest root or eingenvalue (largest eigenvalue = largest cancor ^ 2)
// int p2 = p <= q ? p : q;
// int q2 = p > q ? p : q;
// int s = p2;
// double m = 0.5 * (q2-p2-1);
// double n = 0.5 * (N-p2-q2-2) ;
// double Csmn = C(s,m,n);
// double Cs_1mn = C(s-1,m,n);
// // Calculating log( V )
// // We need 1 V for each of the 's-1' k's needed in the formula for P
// vector_tld V(s-1);
// for (int i=1; i<s; i++)
// {
// // i==1? If so, then the numerator of V = 1
// if (i == 1)
// {
// double num = log(1);
// double denom = Cs_1mn;
// V[i-1] = num - denom;
// }
// // for i>1
// else
// {
// double a=1;
// for (int j=1; j<i; j++)
// {
// a *= (2*m+s-j+1) / (2*m+2*n+2*s-j+1);
// }
// double num = log( bico(s-1,i-1) * a );
// double denom = Cs_1mn;
// V[i-1] = num - denom ;
// }
// }
// // Computing k
// vector_tld k(s,0); // with 0s on it (the first element will NOT be used later on
// for (int i=1; i<s; i++)
// {
// k[i] = ( exp( Csmn + V[i-1] ) - (m+s-i+1)*k[i-1] ) / (m+n+s-i+1);
// }
// // Computing the cdf
// double sum_v;
// //lroot=pow(0.05,2); //test
// long double cdf;
// for (int i=1; i<s; i++)
// {
// sum_v += pow(-1.0,i) * k[i] * ( pow(lroot,m+s-i) * pow(1-lroot,n+1) );
// }
// // If min(p,q) is even...
// if ( s%2 == 0 )
// {
// cdf = 1 + sum_v;
// }
// // If s is odd
// else
// {
// double num = log( qromo(I,0.0,lroot,m,n) );
// double denom = betaln(m+1,n+1);
// double last = exp(num - denom);
// if (last > 1)
// last = 1;
// cdf = last + sum_v;
// }
// return 1-cdf;
}
// Bartlet's test for all canonical correlations
long double bartlett(int N, int p, int q, vector_t eigen)
{
int p2 = p <= q ? p : q; // Number of canonical correlations
double prod_eigen=1;
for (int i=0; i<p2; i++)
{
prod_eigen *= (1-eigen[i]);
}
double chisq = -1*(N - 1 - 0.5*(p+q+1)) * log(prod_eigen);
double pvalue = chiprobP(chisq,p*q);
return pvalue;
}
// Transpose of a matrix
void transposeMatrix(matrix_t & M)
{
int rM = M.size();
int cM = M[1].size();
matrix_t tM;
sizeMatrix(tM,cM,rM);
for (int r=0; r<cM; r++)
{
for (int c=0; c<rM; c++)
{
tM[r][c] = M[c][r];
}
}
M = tM;
}
int calcGENEPIMeanVariance(vector<CSNP*> &,
int, int,
bool,
Plink *,
vector<double> &,
vector<vector<double> > &,
vector<Individual*>&,
vector<int> &,
vector<int> &);
void CCA_logit(bool perm,
vector<vector<int> > & blperm,
Set & S,
Plink & P);
void CCA_caseonly(bool perm,
vector<vector<int> > & blperm,
Set & S,
Plink & P);
void Plink::driverSCREEPI()
{
///////////////////////////////
// Gene-based epistasis
//////////////////////////////////////////
// Case-control samples only
affCoding(*this);
//////////////////////////////////////////
// SNP-major mode analysis
if (!par::SNP_major)
Ind2SNP();
//////////////////////////////////////////
// Requires that sets have been speciefied
if (par::set_test) readSet();
else error("Need to specify genes with --set {filename} when using --genepi\n");
//////////////////
// SET statistics
Set S(snpset);
//////////////////////////////////////////////
// Prune SET (0-sized sets, MAF==0 SNPs, etc)
S.pruneSets(*this);
int ns = snpset.size();
if (ns < 2)
error("Need to specify at least two fully valid sets\n");
int n = 0;
int ncase = 0;
/////////////////////////////////////////////////////////
// Prune based on VIF
string original_outfile = par::output_file_name;
// Case-control? Prune cases and controls together...
if (!par::epi_caseonly)
{
printLOG("\nConsidering cases and controls: ");
setFlags(false);
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( ! (*person)->missing )
{
(*person)->flag = true;
n++;
}
person++;
}
par::output_file_name += ".all";
S.pruneMC(*this,false,par::vif_threshold);
//S.pruneMC(*this,false,1000);
}
// Case-only? Prune cases only...
else
{
printLOG("\nConsidering cases: ");
setFlags(false);
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( (*person)->aff && ! (*person)->missing )
{
(*person)->flag = true;
ncase++;
}
person++;
n++;
}
par::output_file_name += ".case";
S.pruneMC(*this,false,par::vif_threshold);
//S.pruneMC(*this,false,1000);
}
par::output_file_name = original_outfile;
// Write finalized set
ofstream SET1, SET2;
string f = par::output_file_name + ".all.set.in";
printLOG("Writing combined pruned-in set file to [ " + f + " ]\n");
SET1.open(f.c_str(),ios::out);
f = par::output_file_name + ".all.set.out";
printLOG("Writing combined pruned-out set file to [ " + f + " ]\n");
SET2.open(f.c_str(),ios::out);
for (int s=0; s<snpset.size(); s++)
{
int nss = snpset[s].size();
SET1 << setname[s] << "\n";
SET2 << setname[s] << "\n";
for (int j=0; j<nss; j++)
{
if (S.cur[s][j])
SET1 << locus[snpset[s][j]]->name << "\n";
else
SET2 << locus[snpset[s][j]]->name << "\n";
}
SET1 << "END\n\n";
SET2 << "END\n\n";
}
SET1.close();
SET2.close();
// Prune empty sets once more:
S.pruneSets(*this);
ns = snpset.size();
if (ns < 2)
error("Need to specify at least two fully valid sets\n");
////////////////////////////////
// Set up permutation structure
// Specialized (i.e. cannot use Perm class) as this
// requires a block-locus permutation
// First block is fixed
vector<vector<int> > blperm(ns);
vector<vector<int> > blperm_case(ns);
vector<vector<int> > blperm_control(ns);
for (int i=0; i<ns; i++)
{
// A slot for each individual per locus
for (int j=0; j<n; j++)
if ( ! sample[j]->missing )
blperm[i].push_back(j);
// A slot for each individual per locus
for (int j=0; j<n; j++)
if ( ! sample[j]->missing && sample[j]->aff )
blperm_case[i].push_back(j);
// A slot for each individual per locus
for (int j=0; j<n; j++)
if ( ! sample[j]->missing && !sample[j]->aff )
blperm_control[i].push_back(j);
}
////////////////////////////////////////////
// Open file and print header for results
ofstream EPI(f.c_str(), ios::out);
EPI.open(f.c_str(), ios::out);
EPI.precision(4);
////////////////////////////////////////
// Analysis (calls genepi functions)
if (!par::epi_caseonly)
CCA_logit(false,blperm,S,*this);
else
CCA_caseonly(false,blperm_case,S,*this);
if (!par::permute)
return;
if (!par::silent)
cout << "\n";
} // End of screepi
///////////////////////////
// CCA functions
///////////////////////////////////////////////////////////
// First CCA function: use for case-control logit analysis
void CCA_logit(bool perm,
vector<vector<int> > & blperm,
Set & S,
Plink & P)
{
///////////////
// Output results
ofstream EPI;
if (!perm)
{
string f = par::output_file_name+".genepi";
P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n");
EPI.open(f.c_str(), ios::out);
EPI.precision(4);
EPI << setw(12) << "NIND" << " "
<< setw(12) << "GENE1" << " "
<< setw(12) << "GENE2" << " "
<< setw(12) << "NSNP1" << " "
<< setw(12) << "NSNP2" << " "
<< setw(12) << "P" << " "
<< "\n";
}
//////////////////////////////////
// Canonical correlation analysis
int ns = P.snpset.size();
// Consider each pair of genes
for (int s1=0; s1 < ns-1; s1++)
{
for (int s2 = s1+1; s2 < ns; s2++)
{
////////////////////////////////////////////////////////
// Step 1. Construct covariance matrix (cases and controls together)
// And partition covariance matrix:
// S_11 S_21
// S_12 S_22
int n1=0, n2=0;
vector<vector<double> > sigma(0);
vector<double> mean(0);
vector<CSNP*> pSNP(0);
/////////////////////////////
// List of SNPs for both loci
for (int l=0; l<P.snpset[s1].size(); l++)
{
if ( S.cur[s1][l] )
{
pSNP.push_back( P.SNP[ P.snpset[s1][l] ] );
n1++;
}
}
for (int l=0; l<P.snpset[s2].size(); l++)
{
if ( S.cur[s2][l] )
{
pSNP.push_back( P.SNP[ P.snpset[s2][l] ] );
n2++;
}
}
int n12 = n1 + n2;
int ne = n1 < n2 ? n1 : n2;
///////////////////////////////////
// Construct covariance matrix (cases and controls together)
P.setFlags(false);
vector<Individual*>::iterator person = P.sample.begin();
while ( person != P.sample.end() )
{
(*person)->flag = true;
person++;
}
int nind = calcGENEPIMeanVariance(pSNP,
n1,n2,
false,
&P,
mean,
sigma,
P.sample ,
blperm[s1],
blperm[s2] );
///////////////////////////
// Partition covariance matrix
vector<vector<double> > I11;
vector<vector<double> > I11b;
vector<vector<double> > I12;
vector<vector<double> > I21;
vector<vector<double> > I22;
vector<vector<double> > I22b;
sizeMatrix( I11, n1, n1);
sizeMatrix( I11b, n1, n1);
sizeMatrix( I12, n1, n2);
sizeMatrix( I21, n2, n1);
sizeMatrix( I22, n2, n2);
sizeMatrix( I22b, n2, n2); // For step 4b (eigenvectors for gene2)
for (int i=0; i<n1; i++)
for (int j=0; j<n1; j++)
{
I11[i][j] = sigma[i][j];
I11b[i][j] = sigma[i][j];
}
for (int i=0; i<n1; i++)
for (int j=0; j<n2; j++)
I12[i][j] = sigma[i][n1+j];
for (int i=0; i<n2; i++)
for (int j=0; j<n1; j++)
I21[i][j] = sigma[n1+i][j];
for (int i=0; i<n2; i++)
for (int j=0; j<n2; j++)
{
I22[i][j] = sigma[n1+i][n1+j];
I22b[i][j] = sigma[n1+i][n1+j];
}
////////////////////////////////////////////////////////
// Step 2. Calculate the p x p matrix M1 = inv(sqrt(sig11)) %*% sig12 %*% inv(sig22) %*% sig21 %*% inv(sqrt(sig11))
bool flag = true;
I11 = msqrt(I11);
I11 = svd_inverse(I11,flag);
I22 = svd_inverse(I22,flag);
I22b = msqrt(I22b);// For Step 4b
I22b = svd_inverse(I22b,flag);
I11b = svd_inverse(I11b,flag);
matrix_t tmp;
matrix_t M1;
multMatrix(I11, I12, tmp);
multMatrix(tmp, I22, M1);
multMatrix(M1, I21, tmp);
multMatrix(tmp, I11, M1);
////////////////////////////////////////////////////////
// Step 4a. Calculate the p eigenvalues and p x p eigenvectors of
// M (e). These are required to compute the coefficients used to
// build the p canonical variates a[k] for gene1 (see below)
double max_cancor = 0.90;
// Compute evalues and evectors
Eigen gene1_eigen = eigenvectors(M1);
// Sort evalues for gene 1. (the first p of these equal the first p of gene 2 (ie M2), if they are sorted)
vector<double> sorted_eigenvalues_gene1 = gene1_eigen.d;
sort(sorted_eigenvalues_gene1.begin(),sorted_eigenvalues_gene1.end(),greater<double>());
// Position of the largest canonical correlation that is <
// max_cancor in the sorted vector of eigenvalues. This will be
// needed to use the right gene1 and gene2 coefficients to build
// the appropriate canonical variates.
double cancor1=0;
int cancor1_pos;
for (int i=0; i<n1; i++)
{
if ( sqrt(sorted_eigenvalues_gene1[i]) > cancor1 && sqrt(sorted_eigenvalues_gene1[i]) < max_cancor )
{
cancor1 = sqrt(sorted_eigenvalues_gene1[i]);
cancor1_pos = i;
break;
}
}
// Display largest canonical correlation and its position
// cout << "Largest canonical correlation [position]\n"
// << cancor1 << " [" << cancor1_pos << "]" << "\n\n" ;
// Sort evectors. Rows must be ordered according to cancor value (highest first)
matrix_t sorted_eigenvectors_gene1 = gene1_eigen.z;
vector<int> order_eigenvalues_gene1(n1);
for (int i=0; i<n1; i++)
{
// Determine position of the vector associated with the ith cancor
for (int j=0; j<n1; j++)
{
if (gene1_eigen.d[j]==sorted_eigenvalues_gene1[i])
{
if (i==0)
{
order_eigenvalues_gene1[i]=j;
break;
}
else
{
if (j!=order_eigenvalues_gene1[i-1])
{
order_eigenvalues_gene1[i]=j;
break;
}
}
}
}
}
for (int i=0; i<n1; i++)
{
sorted_eigenvectors_gene1[i] = gene1_eigen.z[order_eigenvalues_gene1[i]];
}
// cout << "Eigenvector matrix - unsorted:\n";
// display(gene1_eigen.z);
//cout << "Eigenvector matrix - sorted:\n";
//display(sorted_eigenvectors_gene1);
////////////////////////////////////////////////////////
// Step 4b. Calculate the q x q eigenvectors of M2 (f). These are
// required to compute the coefficients used to build the p
// canonical variates b[k] for gene2 (see below). The first p are
// given by: f[k] = (1/sqrt(eigen[k])) * inv_sqrt_I22 %*% I21 %*%
// inv_sqrt_sig11 %*% e[k] for (k in 1:p) { e.vectors.gene2[,k] =
// (1/sqrt(e.values[k])) * inv.sqrt.sig22 %*% sig21 %*%
// inv.sqrt.sig11 %*% e.vectors.gene1[,k] }
matrix_t M2;
multMatrix(I22b, I21, tmp);
multMatrix(tmp, I11b, M2);
multMatrix(M2, I12, tmp);
multMatrix(tmp, I22b, M2);
Eigen gene2_eigen = eigenvectors(M2);
//cout << "Eigenvalues Gene 2 - unsorted:\n";
//display(gene2_eigen.d);
// Sort evalues for gene2
vector<double> sorted_eigenvalues_gene2 = gene2_eigen.d;
sort(sorted_eigenvalues_gene2.begin(),sorted_eigenvalues_gene2.end(),greater<double>());
// Sort eigenvectors for gene2
matrix_t sorted_eigenvectors_gene2 = gene2_eigen.z;
vector<int> order_eigenvalues_gene2(n2);
for (int i=0; i<n2; i++)
{
// Determine position of the vector associated with the ith cancor
for (int j=0; j<n2; j++)
{
if (gene2_eigen.d[j]==sorted_eigenvalues_gene2[i])
{
if (i==0)
{
order_eigenvalues_gene2[i]=j;
break;
}
else
{
if (j!=order_eigenvalues_gene2[i-1])
{
order_eigenvalues_gene2[i]=j;
break;
}
}
}
}
}
for (int i=0; i<n2; i++)
{
sorted_eigenvectors_gene2[i] = gene2_eigen.z[order_eigenvalues_gene2[i]];
}
//cout << "Eigenvector matrix Gene 2 - unsorted:\n";
//display(gene2_eigen.z);
//cout << "Eigenvector matrix Gene 2 - sorted:\n";
//display(sorted_eigenvectors_gene2);
//exit(0);
//////////////////////////////////////////////////////////////////////////////////
// Step 5 - Calculate the gene1 (pxp) and gene2 (pxq) coefficients
// used to create the canonical variates associated with the p
// canonical correlations
transposeMatrix(gene1_eigen.z);
transposeMatrix(gene2_eigen.z);
matrix_t coeff_gene1;
matrix_t coeff_gene2;
multMatrix(gene1_eigen.z, I11, coeff_gene1);
multMatrix(gene2_eigen.z, I22b, coeff_gene2);
//cout << "Coefficients for Gene 1:\n";
//display(coeff_gene1);
//cout << "Coefficients for Gene 2:\n";
//display(coeff_gene2);
//exit(0);
///////////////////////////////////////////////////////////////////////
// Step 6 - Compute the gene1 and gene2 canonical variates
// associated with the highest canonical correlation NOTE: the
// original variables of data need to have the mean subtracted first!
// Otherwise, the resulting correlation between variate.gene1 and
// variate.gene1 != estimated cancor.
// For each individual, eg compos.gene1 =
// evector.gene1[1]*SNP1.gene1 + evector.gene1[2]*SNP2.gene1 + ...
/////////////////////////////////
// Consider each SNP in gene1
vector<double> gene1(nind);
for (int j=0; j<n1; j++)
{
CSNP * ps = pSNP[j];
///////////////////////////
// Iterate over individuals
for (int i=0; i< P.n ; i++)
{
// Only need to look at one perm set
bool a1 = ps->one[i];
bool a2 = ps->two[i];
if ( a1 )
{
if ( a2 ) // 11 homozygote
{
gene1[i] += (1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
}
else // 12
{
gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
}
}
else
{
if ( a2 ) // 21
{
gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
}
else // 22 homozygote
{
gene1[i] += (-1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j];
}
}
} // Next individual
} // Next SNP in gene1
/////////////////////////////////
// Consider each SNP in gene2
vector<double> gene2(P.n);
int cur_snp = -1;
for (int j=n1; j<n1+n2; j++)
{
cur_snp++;
CSNP * ps = pSNP[j];
// Iterate over individuals
for (int i=0; i<P.n; i++)
{
// Only need to look at one perm set
bool a1 = ps->one[i];
bool a2 = ps->two[i];
if ( a1 )
{
if ( a2 ) // 11 homozygote
{
gene2[i] += (1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
}
else // 12
{
gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
}
}
else
{
if ( a2 ) // 21
{
gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
}
else // 22 homozygote
{
gene2[i] += (-1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp];
}
}
} // Next individual
} // Next SNP in gene2
// Store gene1.variate and gene2.variate in the multiple_covariates field of P.sample
// TO DO: NEED TO CHECK IF FIELDS ARE EMPTY FIRST!
for (int i=0; i<P.n; i++)
{
P.sample[i]->clist.resize(2);
P.sample[i]->clist[0] = gene1[i];
P.sample[i]->clist[1] = gene2[i];
}
///////////////////////////////////////////////
// STEP 7 - Logistic or linear regression epistasis test
//
Model * lm;
if (par::bt)
{
LogisticModel * m = new LogisticModel(& P);
lm = m;
}
else
{
LinearModel * m = new LinearModel(& P);
lm = m;
}
// No SNPs used
lm->hasSNPs(false);
// Set missing data
lm->setMissing();
// Main effect of GENE1 1. Assumes that the variable is in position 0 of the clist vector
lm->addCovariate(0);
lm->label.push_back("GENE1");
// Main effect of GENE 2. Assumes that the variable is in position 1 of the clist vector
lm->addCovariate(1);
lm->label.push_back("GENE2");
// Epistasis
lm->addInteraction(1,2);
lm->label.push_back("EPI");
// Build design matrix
lm->buildDesignMatrix();
// Prune out any remaining missing individuals
// No longer needed (check)
// lm->pruneY();
// Fit linear model
lm->fitLM();
// Did model fit okay?
lm->validParameters();
// Obtain estimates and statistic
lm->testParameter = 3; // interaction
vector_t b = lm->getCoefs();
double chisq = lm->getStatistic();
double logit_pvalue = chiprobP(chisq,1);
// Clean up
delete lm;
/////////////////////////////
// OUTPUT
EPI << setw(12) << nind << " "
<< setw(12) << P.setname[s1] << " "
<< setw(12) << P.setname[s2] << " "
<< setw(12) << n1 << " "
<< setw(12) << n2 << " "
<< setw(12) << logit_pvalue << " "
<< "\n";
} // End of loop over genes2
} // End of loop over genes1
EPI.close();
} // End of CCA_logit()
///////////////////////////////////////////////////////////
// Second CCA function: use for case-control only
void CCA_caseonly(bool perm,
vector<vector<int> > & blperm_case,
Set & S,
Plink & P)
{
///////////////
// Output file
ofstream EPI;
if (!perm)
{
string f = par::output_file_name+".genepi";
P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n");
EPI.open(f.c_str(), ios::out);
EPI.precision(4);
EPI << setw(12) << "NIND" << " "
<< setw(12) << "GENE1" << " "
<< setw(12) << "GENE2" << " "
<< setw(12) << "NSNP1" << " "
<< setw(12) << "NSNP2" << " "
<< setw(12) << "CC1" << " "
// << setw(12) << "PILLAI" << " "
<< setw(12) << "BART" << " "
<< "\n";
}
//////////////////////////////////
// Canonical correlation analysis
// Number of genes
int ns = P.snpset.size();