-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmh.cpp
More file actions
1315 lines (1023 loc) · 29.5 KB
/
Copy pathmh.cpp
File metadata and controls
1315 lines (1023 loc) · 29.5 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
//////////////////////////////////////////////////////////////////
// //
// 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 <cmath>
#include "plink.h"
#include "perm.h"
#include "options.h"
#include "helper.h"
#include "stats.h"
void Plink::calcMH()
{
///////////////////////////////////
// Basic 2 x 2 x K CMH test
// i.e. Disease x allele x strata
// is taken care of in assoc.cpp
// (i.e. allows for permutation, sets, etc)
if (!par::SNP_major) Ind2SNP();
//////////////////////////////////
// Any individual not assigned to a cluster,
// making missing phenotype
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( (*person)->sol < 0 )
(*person)->missing = true;
person++;
}
///////////////////////////////////
// Generalized I x J x K CMH test
// Either ordinal or normal
// i.e. test strata X SNP controlling for disease
if (par::CMH_test_2 || par::CMH_test_ORD )
{
if (par::CMH_test_ORD && !par::bt)
error("--mh-ord specified but the phenotype is only binary: use --mh");
if (nk==1)
error("No clusters defined for --mh2 test, i.e. K=1");
string f = par::output_file_name + ".cmh2";
if (par::CMH_test_ORD)
f = par::output_file_name + ".cmh.ord";
ofstream MHOUT;
MHOUT.open(f.c_str(),ios::out);
MHOUT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(10) << "CHISQ" << " "
<< setw(10) << "P" << "\n";
MHOUT.precision(4);
if (par::CMH_test_ORD)
{
printLOG("Cochran-Mantel-Haenszel IxJxK ordinal test, K = "
+ int2str(nk) + "\n");
printLOG("Testing SNP x ORDINAL DISEASE | STRATUM (option --mh-ord)\n");
}
else
{
printLOG("Cochran-Mantel-Haenszel IxJxK test, K = "
+ int2str(nk) + "\n");
printLOG("Testing SNP x STRATUM | DISEASE (option --mh2)\n");
}
printLOG("Writing results to [ " + f + " ]\n");
vector<CSNP*>::iterator s = SNP.begin();
int l=0;
while ( s != SNP.end() )
{
/////////////////////////
// Autosomal or haploid?
bool Xchr=false, haploid=false;
if (par::chr_sex[locus[l]->chr]) Xchr=true;
else if (par::chr_haploid[locus[l]->chr]) haploid=true;
if (haploid || Xchr )
error("--mh2 / --mh-ord cannot handle X/Y markers currently...");
vector<int> X(0); // SNP
vector<int> Y(0); // Cluster
vector<int> Z(0); // Phenotype
vector<Individual*>::iterator person = sample.begin();
vector<bool>::iterator i1 = (*s)->one.begin();
vector<bool>::iterator i2 = (*s)->two.begin();
while ( person != sample.end() )
{
if ((*person)->missing)
{
// Next person
person++;
i1++;
i2++;
continue;
}
// Only consider individuals who have been assigned to a cluster
if ( (*person)->sol >= 0 )
{
if ( (!(*i1)) && (!(*i2)) )
{
X.push_back(1);
X.push_back(1);
}
else if ( (!(*i1)) && *i2 )
{
X.push_back(1);
X.push_back(2);
}
else if ( *i1 && *i2 )
{
X.push_back(2);
X.push_back(2);
}
else
{
// Next person
person++;
i1++;
i2++;
continue;
}
Y.push_back((*person)->sol);
Y.push_back((*person)->sol);
if (par::CMH_test_ORD)
Z.push_back( (int)(*person)->phenotype );
else
{
if ((*person)->phenotype==2)
{
Z.push_back(2);
Z.push_back(2);
}
else {
Z.push_back(1);
Z.push_back(1);
}
}
}
// Next person
person++;
i1++;
i2++;
}
vector<double> res;
if ( par::CMH_test_ORD )
res = calcMantelHaenszel_ORD(X,Z,Y);
else
res = calcMantelHaenszel_IxJxK(X,Y,Z);
MHOUT << setw(4) << locus[l]->chr << " "
<< setw(par::pp_maxsnp) << locus[l]->name << " "
<< setw(10) << res[0] << " "
<< setw(10) << chiprobP(res[0],res[1]) << "\n";
// Next SNP
s++;
l++;
}
MHOUT.close();
}
}
vector<double> Plink::calcMantelHaenszel_2x2xK(Perm & perm, bool original)
{
// Should we perform BD test (K>1)
if (nk<2) par::breslowday = false;
ofstream MHOUT;
if ( original )
{
//////////////////////////////////
// Any individual not assigned to a cluster, making missing
// phenotype (only need to do this once, for original)
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( (*person)->sol < 0 )
(*person)->missing = true;
person++;
}
string f = par::output_file_name + ".cmh";
MHOUT.open(f.c_str(),ios::out);
MHOUT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(10) << "BP" << " "
<< setw(4) << "A1" << " "
<< setw(8) << "MAF" << " "
<< setw(4) << "A2" << " "
<< setw(10) << "CHISQ" << " "
<< setw(10) << "P" << " "
<< setw(10) << "OR" << " "
<< setw(10) << "SE" << " "
<< setw(10) << string("L"+dbl2str(par::ci_level*100)) << " "
<< setw(10) << string("U"+dbl2str(par::ci_level*100)) << " ";
if (par::breslowday)
MHOUT << setw(10) << "CHISQ_BD" << " "
<< setw(10) << "P_BD" << " ";
MHOUT << "\n";
MHOUT.precision(4);
printLOG("Cochran-Mantel-Haenszel 2x2xK test, K = " + int2str( nk) + "\n");
if (par::breslowday)
printLOG("Performing Breslow-Day test of homogeneous odds ratios\n");
printLOG("Writing results to [ " + f + " ]\n");
// Warnings,
if (par::breslowday && nk>10)
printLOG("** Warning ** Breslow-Day statistics require large N per cluster ** \n");
}
double zt = ltqnorm( 1 - (1 - par::ci_level) / 2 ) ;
// Cochran-Mantel-Haenszel 2x2xK test
vector<double> results(nl_all);
vector<CSNP*>::iterator s = SNP.begin();
int l=0;
while ( s != SNP.end() )
{
// Skip possibly
if (par::adaptive_perm && !perm.snp_test[l])
{
s++;
l++;
continue;
}
// Disease X allele X strata
// Calculate mean of 11 cell for each strata
vector<double> mean_11(nk,0);
vector<double> var_11(nk,0);
// Calculate statistic
vector<double> n_11(nk,0);
vector<double> n_12(nk,0);
vector<double> n_21(nk,0);
vector<double> n_22(nk,0);
// Disease marginals
vector<double> n_1X(nk,0); // disease
vector<double> n_2X(nk,0); // no disease
vector<double> n_X1(nk,0); // F allele
vector<double> n_X2(nk,0); // T allele
vector<double> n_TT(nk,0); // Total allele count
/////////////////////////
// Autosomal or haploid?
bool X=false, haploid=false;
if (par::chr_sex[locus[l]->chr]) X=true;
else if (par::chr_haploid[locus[l]->chr]) haploid=true;
////////////////////////
// Consider each person
vector<bool>::iterator i1 = (*s)->one.begin();
vector<bool>::iterator i2 = (*s)->two.begin();
vector<Individual*>::iterator gperson = sample.begin();
while ( gperson != sample.end() )
{
Individual * pperson = (*gperson)->pperson;
bool s1 = *i1;
bool s2 = *i2;
// Affected individuals
if ( pperson->aff && !pperson->missing )
{
// Haploid?
if ( haploid || ( X && (*gperson)->sex ) )
{
// Allelic marginal
if ( ! s1 )
{
// FF hom
n_11[ pperson->sol ] ++ ;
n_X1[ pperson->sol ] ++ ;
}
else
{
if ( ! s2 ) // FT
{
gperson++;
i1++;
i2++;
continue; // skip missing genotypes
}
else // TT
{
n_12[ pperson->sol ] ++ ;
n_X2[ pperson->sol ] ++ ;
}
}
// Disease marginal
n_1X[ pperson->sol ] ++;
n_TT[ pperson->sol ] ++;
}
else // autosomal
{
// Allelic marginal
if ( ! s1 )
{
if ( ! s2 ) // FF hom
{
n_11[ pperson->sol ] +=2 ;
n_X1[ pperson->sol ] +=2 ;
}
else
{
n_11[ pperson->sol ]++ ; // FT het
n_12[ pperson->sol ]++ ;
n_X1[ pperson->sol ]++ ;
n_X2[ pperson->sol ]++ ;
}
}
else
{
if ( ! s2 ) // FT
{
gperson++;
i1++;
i2++;
continue; // skip missing genotypes
}
else // TT
{
n_12[ pperson->sol ] +=2 ;
n_X2[ pperson->sol ] +=2 ;
}
}
// Disease marginal
n_1X[ pperson->sol ] += 2;
n_TT[ pperson->sol ] += 2;
} // end autosomal
}
else if ( ! pperson->missing ) // Unaffecteds
{
// Haploid?
if ( haploid || ( X && (*gperson)->sex ) )
{
// Allelic marginal
if ( ! s1 )
{
// FF hom
n_21[ pperson->sol ] ++ ;
n_X1[ pperson->sol ] ++ ;
}
else
{
if ( ! s2 ) // FT
{
gperson++;
i1++;
i2++;
continue; // skip missing genotypes
}
else // TT
{
n_22[ pperson->sol ] ++ ;
n_X2[ pperson->sol ] ++ ;
}
}
// Disease marginal
n_2X[ pperson->sol ] ++;
n_TT[ pperson->sol ] ++;
}
else // autosomal
{
// Allelic marginal
if ( ! s1 )
{
if ( ! s2 ) // FF
{
n_X1[ pperson->sol ] +=2 ;
n_21[ pperson->sol ] +=2 ;
}
else
{
n_X1[ pperson->sol ] ++ ;
n_X2[ pperson->sol ] ++ ;
n_21[ pperson->sol ] ++ ;
n_22[ pperson->sol ] ++ ;
}
}
else
{
if ( ! s2 ) // FT
{
gperson++;
i1++;
i2++;
continue; // skip missing genotypes
}
else // TT
{
n_X2[ pperson->sol ] +=2 ;
n_22[ pperson->sol ] +=2 ;
}
}
// disease marginal
n_2X[ pperson->sol ] += 2;
n_TT[ pperson->sol ] += 2;
} // end autosomal
} // end unaffected
gperson++;
i1++;
i2++;
} // count next individual
// Finished iterating over individuals: cluster needs at least 2
// nonmissing individuals
vector<bool> validK(nk,false);
for (int k=0; k<nk; k++)
if (n_TT[k]>=2) validK[k]=true;
for (int k=0; k<nk; k++)
{
if (validK[k])
{
mean_11[k] = ( n_X1[k] * n_1X[k] ) / n_TT[k] ;
var_11[k] = ( n_X1[k] * n_X2[k] * n_1X[k] * n_2X[k] )
/ ( n_TT[k]*n_TT[k]*(n_TT[k]-1) );
// cout << k << " "
// << n_11[k] << " "
// << n_12[k] << " "
// << n_21[k] << " "
// << n_22[k] << "\n";
}
}
double CMH = 0;
double denom = 0;
for (int k=0; k<nk; k++)
{
if (validK[k])
{
CMH += n_11[k] - mean_11[k];
denom += var_11[k];
}
}
CMH *= CMH;
CMH /= denom;
// MH Odds ratio & CI
double R = 0;
double S = 0;
vector<double> r2(nk);
vector<double> s2(nk);
for (int k=0; k<nk; k++)
{
if (validK[k])
{
r2[k] = (n_11[k]*n_22[k]) / n_TT[k];
s2[k] = (n_12[k]*n_21[k]) / n_TT[k];
R += r2[k];
S += s2[k];
}
}
double OR = R / S ;
double v1 = 0, v2 = 0, v3 = 0;
for (int k=0; k<nk; k++)
{
if (validK[k])
{
v1 += (1/n_TT[k]) * ( n_11[k] + n_22[k] ) * r2[k] ;
v2 += (1/n_TT[k]) * ( n_12[k] + n_21[k] ) * s2[k] ;
v3 += (1/n_TT[k]) * ( ( n_11[k] + n_22[k] ) * s2[k]
+ ( n_12[k] + n_21[k] ) * r2[k] );
}
}
double SE = ( 1/(2*R*R) ) * v1
+ (1/(2*S*S)) * v2
+ (1/(2*R*S)) * v3 ;
SE = sqrt(SE);
double OR_lower = exp( log(OR) - zt * SE );
double OR_upper = exp( log(OR) + zt * SE );
if ( original )
{
double pvalue = chiprobP(CMH,1);
// Skip?, if filtering p-values
if ( par::pfilter && ( pvalue > par::pfvalue || pvalue < 0 ) )
goto skip_p_cmh;
MHOUT << setw(4) << locus[l]->chr << " "
<< setw(par::pp_maxsnp) << locus[l]->name << " "
<< setw(10) << locus[l]->bp << " "
<< setw(4) << locus[l]->allele1 << " "
<< setw(8) << locus[l]->freq << " "
<< setw(4) << locus[l]->allele2 << " ";
if (realnum(CMH))
MHOUT << setw(10) << CMH << " "
<< setw(10) << chiprobP(CMH,1) << " ";
else
MHOUT << setw(10) << "NA" << " "
<< setw(10) << "NA" << " ";
if (realnum(OR))
MHOUT << setw(10) << OR << " ";
else
MHOUT << setw(10) << "NA" << " ";
if (realnum(SE))
MHOUT << setw(10) << SE << " ";
else
MHOUT << setw(10) << "NA" << " ";
if (realnum(OR_lower))
MHOUT << setw(10) << OR_lower << " ";
else
MHOUT << setw(10) << "NA" << " ";
if (realnum(OR_upper))
MHOUT << setw(10) << OR_upper << " ";
else
MHOUT << setw(10) << "NA" << " ";
// Optional Breslow-Day test of homogeneity of odds ratios
if (par::breslowday)
{
double amax;
double bb;
double determ;
double as_plus;
double as_minus;
double Astar;
double Bstar;
double Cstar;
double Dstar;
double Var;
double BDX2 = 0;
int df = 0;
for (int k=0; k<nk; k++)
{
if (validK[k])
{
df++;
amax = (n_1X[k] < n_X1[k]) ? n_1X[k] : n_X1[k];
bb = n_2X[k] + n_1X[k] * OR - n_X1[k] * (1-OR);
determ = sqrt(bb*bb + 4*(1-OR) * OR * n_1X[k] * n_X1[k]);
as_plus = ( -bb + determ ) / ( 2 - 2 * OR );
as_minus = ( -bb - determ ) / ( 2 - 2 * OR );
Astar = as_minus <= amax && as_minus >= 0 ? as_minus : as_plus ;
Bstar = n_1X[k] - Astar;
Cstar = n_X1[k] - Astar;
Dstar = n_2X[k] - n_X1[k] + Astar;
Var = 1/(1/Astar + 1/Bstar + 1/Cstar + 1/Dstar);
BDX2 += ( (n_11[k] - Astar) * ( n_11[k] - Astar ) ) / Var ;
}
}
double BDp = chiprobP( BDX2 , df-1 );
if ( BDp > -1 )
MHOUT << setw(10) << BDX2 << " "
<< setw(10) << BDp << " ";
else
MHOUT << setw(10) << "NA" << " "
<< setw(10) << "NA" << " ";
}
MHOUT << "\n";
}
skip_p_cmh:
// Store for permutation procedure, based 2x2xK CMH result
results[l] = CMH;
// Next SNP
s++;
l++;
}
if (original)
MHOUT.close();
return results;
}
vector<double> Plink::calcMantelHaenszel_IxJxK(vector<int> & X,
vector<int> & Y,
vector<int> & Z)
{
if (X.size() != Y.size() || Y.size() != Z.size() || X.size() != Z.size() )
error("Internal problem:\n problem in calcMantelHaenszel_IxJxK()...uneven input columns");
// Determine unique elements
int nx=0, ny=0, nz=0;
map<int,int> mx;
map<int,int> my;
map<int,int> mz;
for (unsigned int i=0; i<X.size(); i++)
{
if (mx.find(X[i]) == mx.end())
mx.insert(make_pair(X[i],nx++));
if (my.find(Y[i]) == my.end())
my.insert(make_pair(Y[i],ny++));
if (mz.find(Z[i]) == mz.end())
mz.insert(make_pair(Z[i],nz++));
}
// Generic function to calculate generalized IxJxK CMH
// Assumes no missing data
vector< vector<double> > N(nz); // observed counts
vector< vector<double> > U(nz); // expected
vector< vector< vector<double> > > V(nz); // variance matrix
vector<vector<double> > Tx(nz); // marginal totals
vector<vector<double> > Ty(nz); // ..
vector<double> T(nz); // totals (per K)
for (int k=0; k<nz; k++)
{
Tx[k].resize(nx);
Ty[k].resize(ny);
N[k].resize((nx-1)*(ny-1));
U[k].resize((nx-1)*(ny-1));
V[k].resize((nx-1)*(ny-1));
for (int k2=0; k2<(nx-1)*(ny-1); k2++)
{
N[k][k2] = U[k][k2] = 0;
V[k][k2].resize((nx-1)*(ny-1));
for (int k3=0; k3<(nx-1)*(ny-1); k3++)
V[k][k2][k3] = 0;
}
}
// Consider each observation
for (int i=0; i<X.size(); i++)
{
int vx = mx.find(X[i])->second;
int vy = my.find(Y[i])->second;
int vz = mz.find(Z[i])->second;
// exclude nx + ny (upper limits)
if (vx<nx-1 && vy<ny-1)
N[vz][ vx + vy*(nx-1) ]++;
Tx[vz][vx]++;
Ty[vz][vy]++;
T[vz]++;
}
// Determine valid clusters (at least 2 people)
vector<bool> validK(nk,false);
for (int k=0; k<nk; k++)
if (T[k]>=2) validK[k]=true;
// Calculate expecteds
for (int k=0; k<nz; k++)
{
if (validK[k])
{
for (int ix=0; ix<nx-1; ix++)
for (int iy=0; iy<ny-1; iy++)
{
U[k][ix+iy*(nx-1)] = ( Tx[k][ix] * Ty[k][iy] ) / T[k];
for (int ix2=0; ix2<nx-1; ix2++)
for (int iy2=0; iy2<ny-1; iy2++)
{
int dx=0;
int dy=0;
if (ix==ix2) dx=1;
if (iy==iy2) dy=1;
V[k][ix + iy*(nx-1)][ix2 + iy2*(nx-1)] = ( ( Tx[k][ix] * ( dx * T[k] - Tx[k][ix2] )
* Ty[k][iy] * ( dy *T[k] - Ty[k][iy2] ) )
/ ( T[k]*T[k]*(T[k]-1) ) );
if (ix==ix2 && iy==iy2)
V[k][ix + iy*(nx-1)][ix2 + iy2*(nx-1)]
= abs(V[k][ix + iy*(nx-1)][ix2 + iy2*(nx-1)]);
}
}
}
}
vector<vector<double> > V0((nx-1)*(ny-1));
for (int k2=0; k2<(nx-1)*(ny-1); k2++)
V0[k2].resize((nx-1)*(ny-1));
vector<double> N0((nx-1)*(ny-1));
vector<double> U0((nx-1)*(ny-1));
// Sum N, U and V over K
for (int k=0; k<nz; k++)
{
if (validK[k])
{
for (int i=0; i<(nx-1)*(ny-1); i++)
{
N0[i] += N[k][i];
U0[i] += U[k][i];
for (int i2=0; i2<(nx-1)*(ny-1); i2++)
V0[i][i2] += V[k][i][i2];
}
}
}
bool flag = true;
vector<double> tmp1((nx-1)*(ny-1),0);
vector<double> tmp2((nx-1)*(ny-1),0);
V0 = svd_inverse(V0,flag);
for (int i=0; i<(nx-1)*(ny-1); i++)
tmp1[i] = N0[i] - U0[i];
// Matrix mult -- rows by columns
for (int i=0; i<(nx-1)*(ny-1); i++)
for (int j=0; j<(nx-1)*(ny-1); j++)
tmp2[j] += tmp1[i] * V0[i][j];
vector<double> result(2);
// CMH Chi-square
result[0]=0;
for (int i=0; i<(nx-1)*(ny-1); i++)
result[0] += tmp2[i] * tmp1[i];
// DF
result[1] = (nx-1)*(ny-1);
return result;
}
void Plink::calcHomog()
{
if (!par::SNP_major) Ind2SNP();
string f = par::output_file_name + ".homog";
ofstream MHOUT;
MHOUT.open(f.c_str(),ios::out);
MHOUT.precision(4);
if (nk==0) error("No clusters (K=0)... cannot perform CMH tests");
printLOG("Homogeneity of odds ratio test, K = " + int2str(nk) + "\n");
if (nk<2)
{
printLOG("** Warning ** less then 2 clusters specified... \n");
printLOG(" cannot compute between-cluster effects ** \n");
return;
}
if (nk>10)
printLOG("** Warning ** statistics can be unreliable if strata have small N ** \n");
printLOG("Writing results to [ " + f + " ]\n");
MHOUT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(4) << "A1" << " "
<< setw(4) << "A2" << " "
<< setw(8) << "F_A" << " "
<< setw(8) << "F_U" << " "
<< setw(8) << "N_A" << " "
<< setw(8) << "N_U" << " "
<< setw(8) << "TEST" << " "
<< setw(10) << "CHISQ" << " "
<< setw(4) << "DF" << " "
<< setw(10) << "P" << " "
<< setw(10) << "OR" << "\n";
///////////////////////////////////
// Create boolean affection coding
affCoding(*this);
//////////////////////////////////
// Any individual not assigned to a cluster,
// making missing phenotype
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( (*person)->sol < 0 )
(*person)->missing = true;
person++;
}
///////////////////////////////
// Iterate over SNPs
vector<CSNP*>::iterator s = SNP.begin();
int l=0;
while ( s != SNP.end() )
{
// Uncomment this if we allow permutation for the CMH
// tests
// In adaptive mode, possibly skip this test
// if (par::adaptive_perm && (!perm.snp_test[l]))
// {
// s++;
// l++;
// continue;
// }
// Calculate statistic
vector<double> n_11(nk,0);
vector<double> n_12(nk,0);
vector<double> n_21(nk,0);
vector<double> n_22(nk,0);
vector<double> lnOR(nk,0);
vector<double> SEsq(nk,0);
/////////////////
// Autosomal or haploid?
bool X=false, haploid=false;
if (par::chr_sex[locus[l]->chr]) X=true;
else if (par::chr_haploid[locus[l]->chr]) haploid=true;
/////////////////////////////
// Iterate over individuals
vector<bool>::iterator i1 = (*s)->one.begin();
vector<bool>::iterator i2 = (*s)->two.begin();
vector<Individual*>::iterator gperson = sample.begin();
while ( gperson != sample.end() )
{
// Phenotype for this person (i.e. might be permuted)
Individual * pperson = (*gperson)->pperson;
// SNP alleles
bool s1 = *i1;
bool s2 = *i2;
int hom = 2;
if ( haploid || ( X && (*gperson)->sex ) )
hom = 1;
// Affected individuals
if ( pperson->aff && !pperson->missing )
{
// Allelic marginal
if ( !s1 )
{
if ( !s2 ) // FF hom
{
n_11[ pperson->sol ] += hom ;
}
else
{
n_11[ pperson->sol ]++ ; // FT het
n_12[ pperson->sol ]++ ;
}
}
else
{
if ( !s2 ) // FT
{
gperson++;
i1++;
i2++;
continue; // skip missing genotypes
}