-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdfam.cpp
640 lines (453 loc) · 15.4 KB
/
dfam.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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2007 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 <map>
#include <vector>
#include <set>
#include <cmath>
#include "plink.h"
#include "options.h"
#include "helper.h"
#include "crandom.h"
#include "sets.h"
#include "perm.h"
#include "stats.h"
vector<double> Plink::testSibTDT(bool print_results,
bool permute,
Perm & perm,
vector<bool> & flipA,
vector<bool> & flipP)
{
///////////////////////////
// Vector to store results
vector<double> res(nl_all);
ofstream TDT;
if (print_results)
{
string f = par::output_file_name + ".dfam";
TDT.open(f.c_str(),ios::out);
printLOG("Writing DFAM results (asymptotic) to [ " + f + " ] \n");
TDT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(4) << "A1" << " "
<< setw(4) << "A2" << " "
<< setw(8) << "OBS" << " "
<< setw(8) << "EXP" << " "
<< setw(12) << "CHISQ" << " "
<< setw(12) << "P" << " ";
TDT << "\n";
}
///////////////////////////////////
// Verbose display of pedigrees
if ( par::dumpped && ! par::permute )
{
string str = par::output_file_name + ".pdump";
printLOG("Dumping pedigree information to [ " + str + " ]\n");
ofstream PD(str.c_str(),ios::out);
// Of course, in practice, due to missing genotypes, the exact
// family configurations may shift.
for (int f=0; f<family.size(); f++)
{
Family * fam = family[f];
// Families with parents...
if ( fam->parents && fam->TDT && par::dfam_tdt )
{
PD << "W/PARENTS\t" << fam->pat->fid << " : ";
PD << fam->pat->iid << " x " << fam->mat->iid << " -> ";
for ( int k=0; k<fam->kid.size(); k++)
PD << fam->kid[k]->iid << " ";
PD << "\n";
}
// ...and those sibling without 2 parents
else if ( fam->sibship && par::dfam_sibs && fam->kid.size() > 1 )
{
PD << "SIBSHIP \t" << fam->kid[0]->fid << " : ";
for ( int k=0; k<fam->kid.size(); k++)
PD << fam->kid[k]->iid << " ";
PD << "\n";
}
}
// And unrelated clusters
if ( par::dfam_unrelateds )
for ( int k=0; k<nk; k++)
{
for (int c=0; c<klist[k]->person.size(); c++)
{
Individual * person = klist[k]->person[c];
if ( ! ( person->family->sibship ||
( person->family->parents ) ) )
PD << "CLUSTER " << k << "\t"
<< klist[k]->person[c]->fid << " : "
<< klist[k]->person[c]->iid << "\n";
}
}
PD.close();
}
///////////////////////////////////
// Perform analysis for each locus
for (int l=0; l<nl_all; l++)
{
// Adaptive permutation, skip this SNP?
if (par::adaptive_perm && (!perm.snp_test[l]))
continue;
// Skip X/haploid markers for now
if ( par::chr_sex[ locus[l]->chr ] ||
par::chr_haploid[ locus[l]->chr ] )
{
continue;
}
// Allele T counts
double numerator = 0;
double denom = 0;
// Total counts
double totalCount = 0;
double totalExpected = 0;
int totalInformative = 0;
/////////////////////////
// Count over families
for (int f=0; f<family.size(); f++)
{
// A simple pedigree association test
// 1. Break pedigrees into nuclear families
// 2. Classify nuclear families into (a) those where both
// parents are genotyped (b) the others.
// 3. For each type (a) family, obtain the count allele A
// among the affected children. and also its expected value
// and variance (under H0) given the genotypes of the two
// parents. These are given by the binomial distribution,
// e.g. if the parental genotypes are (AA, AB) and there are
// k affected children, then the the expected count of A is
// 1.5k, and its variance is 0.25k.
// AA AB -> AA 0.5 2
// AB 0.5 1
// E = 1.5 * K
// V = 0.25 * K
// BB AB -> AB 0.5 1
// BB 0.5 0
// E = 0.5 * K
// V = 0.25 * K
// AB AB -> AA 0.25 2
// AB 0.50 1
// BB 0.25 0
// E = 0
// V = 0.5 * K
// 4. For each type (b) family, also obtain the count A
// among the affected children and its expected value and
// variance (under H0) given the genotypes of all the
// children. These are given by the hypergeometric
// distribution, e.g. if the sibship contain n A alleles m B
// alleles and there are N affected members, then the
// expected value of the count of A among the affected
// members would be 2N n/(n+m), and its variance is 2N
// nm/(n+m)^2 Note the factor of 2 is because each person
// has 2 alleles.
// n "A" alleles, m "B" alleles
// S = n | k affected siblings
// E = 2 k n/(n+m)
// V = 2 k nm/(n+m)^2
// replacement issue?
// 5. An overall test statistic is (Sum of the Counts of A -
// Sum of expected counts )^2 / (Sum of variances)
Family * fam = family[f];
bool informative = false;
bool parents = true;
// Type A: two genotyped parents and at least 1 affected individual
if ( fam->parents && fam->TDT && par::dfam_tdt )
{
Individual * pat = family[f]->pat;
Individual * mat = family[f]->mat;
bool pat1 = pat->one[l];
bool pat2 = pat->two[l];
bool mat1 = mat->one[l];
bool mat2 = mat->two[l];
// We need two genotyped parents, with
// at least one het
if ( pat1 && (!pat2) ||
mat1 && (!mat2) )
{
parents = false;
goto jump_to_sibships;
}
int heteroParents = 0;
bool homozygParent;
if ( pat1 != pat2 )
heteroParents++;
else
homozygParent = pat1;
if ( mat1 != mat2 )
heteroParents++;
else
homozygParent = mat1;
if ( heteroParents == 0 )
{
parents = false;
goto jump_to_sibships;
}
// Consider all offspring in nuclear family
double alleleCount = 0;
double childCount = 0;
for (int c=0; c<family[f]->kid.size(); c++)
{
// Only consider affected children: based on true
// (not permuted) phenotype here: permutation works
// by flipping transmissions
if ( ! family[f]->kid[c]->aff ) continue;
bool kid1 = family[f]->kid[c]->one[l];
bool kid2 = family[f]->kid[c]->two[l];
// Skip if offspring has missing genotype
if ( kid1 && !kid2 ) continue;
// We've now established: no missing genotypes
// and at least one heterozygous parent
if ( permute )
{
if ( heteroParents == 1 )
{
if ( ! homozygParent )
alleleCount++;
if ( flipA[f] )
alleleCount++;
}
else // ...two heterozygous parents
{
if ( flipA[f] )
alleleCount+=2;
}
}
else
{
// No permtutation: standard counting
if ( ! kid1 )
alleleCount++;
if ( ! kid2 )
alleleCount++;
}
childCount++;
} // next offspring in family
double expected = childCount;
if ( heteroParents == 1 )
{
if ( ! homozygParent )
expected *= 1.5;
else
expected *= 0.5;
}
double variance = heteroParents * 0.25 * childCount;
numerator += (alleleCount - expected);
denom += variance;
totalCount += alleleCount;
totalExpected += expected;
}
jump_to_sibships:
// Sibships, considering genotypes using the
// multivariate hypergeometric distribution
if ( ( fam->sibship || ( fam->parents && !parents ) )
&& par::dfam_sibs )
{
// Let the numbers of offspring with genotypes AA, AB
// and BB.
// N=NAA+NAB+NBB
// Let the numbers of affected offsrping with genotypes
// AA, AB and BB. be
// D=DAA+DAB+DBB
// Then the observed count of A is DA = 2*DAA+DAB The
// expected value of DA = 2E(DAA) + E D(AB) = 2 NAA *
// (D/N) + NAB * (D/N). The variance of DA = 4 Var (DAA)
// + Var(DAB) + 4Cov(AA,DAB)
// The variances and covariances from the multivariate
// hypergeometric distribution.
double childCount = 0;
double affectedCount = 0;
double genotype1Count = 0; // Aa
double affectedGenotype1Count = 0; // Aa
double genotype2Count = 0; // AA
double affectedGenotype2Count = 0; // AA
for (int c=0; c<family[f]->kid.size(); c++)
{
bool kid1 = family[f]->kid[c]->one[l];
bool kid2 = family[f]->kid[c]->two[l];
// Skip if offspring has missing genotype
if ( kid1 && !kid2 ) continue;
// Only consider affected children (possibly allowing
// for permutation)
if ( family[f]->kid[c]->pperson->aff )
{
if ( ! kid1 )
{
if ( ! kid2 )
affectedGenotype2Count++;
else
affectedGenotype1Count++;
}
affectedCount++;
}
if ( !kid1 )
{
if ( !kid2 )
genotype2Count++;
else
genotype1Count++;
}
childCount++;
} // next offspring in family
if ( affectedCount > 0 && affectedCount != childCount )
{
double expectedGenotype2 = affectedCount
* ( genotype2Count / childCount ) ;
double expectedGenotype1 = affectedCount
* ( genotype1Count / childCount ) ;
double varianceGenotype2 = affectedCount
* ( genotype2Count / childCount )
* ( 1 - genotype2Count / childCount )
* ( (childCount - affectedCount ) / ( childCount - 1 ) ) ;
double varianceGenotype1 = affectedCount
* ( genotype1Count / childCount )
* ( 1 - genotype1Count / childCount )
* ( (childCount - affectedCount ) / ( childCount - 1 ) ) ;
double covarianceGenotype =
- ( affectedCount *
( ( genotype2Count * genotype1Count )
/ ( childCount * childCount ) )
* ( ( (childCount - affectedCount )
/ ( childCount - 1 ) ) ) );
double affectedAlleleCount = 2 * affectedGenotype2Count
+ affectedGenotype1Count;
double expected = 2 * expectedGenotype2
+ expectedGenotype1;
double variance = 4 * varianceGenotype2
+ varianceGenotype1 + 4 * covarianceGenotype;
numerator += ( affectedAlleleCount - expected );
denom += variance;
totalCount += affectedAlleleCount;
totalExpected += expected;
// cout << "SIB "
// << childCount << " "
// << genotype1Count << " "
// << genotype2Count << " "
// << affectedCount << " VAR1,2,3= "
// << varianceGenotype2 << " "
// << varianceGenotype1 << " "
// << covarianceGenotype << " "
// << affectedAlleleCount << " [ "
// << expected << " & "
// << variance << "] \t"
// << numerator << " / "
// << denom << " , "
// << totalCount << " "
// << totalExpected << "\n";
}
}
} // Next nuclear family
///////////////////////////////////////
// Now consider clusters of unrelateds
// As sibling test, except allelic rather than genotypic
// variance estimate (i.e. univariate rather than
// multivariate hypergeometic distribution, so equivalent
// to CMH test
if ( par::dfam_unrelateds )
for ( int k=0; k<nk; k++)
{
double affectedCount = 0;
double affectedAlleleCount = 0;
double alleleCount = 0;
double childCount = 0;
bool same = true;
for (int c=0; c<klist[k]->person.size(); c++)
{
Individual * person = klist[k]->person[c];
// Skip any individuals who we've already
// analysed as a family
if ( person->family->sibship ||
person->family->parents )
continue;
bool s1 = person->one[l];
bool s2 = person->two[l];
// Skip if offspring has missing genotype
if ( s1 && !s2 ) continue;
// Are we seeing any genotypic discordance?
// Only consider families where we do (i.e.
// ignore (het, het) pairs, for example
if ( c>0 &&
( s1 != klist[k]->person[c-1]->one[l] ||
s2 != klist[k]->person[c-1]->two[l] ) )
same = false;
// Only consider affected children
if ( person->pperson->aff )
{
if ( ! s1 )
affectedAlleleCount++;
if ( ! s2 )
affectedAlleleCount++;
affectedCount++;
}
if ( ! s1 )
alleleCount++;
if ( ! s2 )
alleleCount++;
childCount++;
} // next individual in cluster
// S = n | A affected individuals
// E = 2 k n/(n+m)
// V = 2 k nm/(n+m)^2
if ( (!same) && childCount > 1 )
{
double D = alleleCount;
double N = 2 * childCount;
double A = 2 * affectedCount;
double expected = A * ( D / N ) ;
double variance = A *(D/N)*(1-D/N)*((N-A)/(N-1));
numerator += ( affectedAlleleCount - expected );
denom += variance;
totalCount += affectedAlleleCount;
totalExpected += expected;
}
} // Next cluster of unrelateds
//////////////////////////////
// Calculate DFAM statistic
double chisq = numerator*numerator;
chisq /= denom;
//////////////////////////////
// Display asymptotic results
if (print_results)
{
double pvalue = chiprobP(chisq,1);
// Skip?, if filtering p-values
if ( par::pfilter && pvalue > par::pfvalue )
continue;
TDT.precision(4);
TDT << setw(4) << locus[l]->chr << " "
<< setw(par::pp_maxsnp) << locus[l]->name << " "
<< setw(4) << locus[l]->allele1 << " "
<< setw(4) << locus[l]->allele2 << " "
<< setw(8) << totalCount << " "
<< setw(8) << totalExpected << " ";
if ( realnum(chisq) )
{
TDT << setw(12) << chisq << " "
<< setw(12) << pvalue << " ";
}
else
TDT << setw(12) << "NA" << " "
<< setw(12) << "NA" << " ";
TDT << "\n";
}
/////////////////////////////////
// Save statistic for permutation
res[l] = chisq;
} // next locus
//////////////////////////////
// Close output file, if open
if (print_results)
TDT.close();
///////////////////////////////////////////
// Return chosen statistic for permutation
return res;
}