File: genome.cpp

package info (click to toggle)
plink 1.07%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: cpp: 72,375; makefile: 123; sh: 12
file content (1352 lines) | stat: -rw-r--r-- 29,779 bytes parent folder | download | duplicates (7)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2008 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 <sstream>
#include <vector>
#include <map>
#include <cmath>
#include <iterator>
#include "plink.h"
#include "options.h"
#include "helper.h"
#include "stats.h"
#include "cfamily.h"
#include "zed.h"

void Plink::calcStratifiedAlleleFreqs()
{
  
  // w/ Modification to output counts by John Novembre

  // Assume SNP-major data

  if (!par::SNP_major) Ind2SNP();
  

  // This is called *after* any filters are applied 
  // (i.e. unlike the original --freq command)
  // This means that various things will have been fixed already, 
  // such as het haploid calls, and which allele is the minor one
  
  if (par::summ_nonfounders)
    printLOG("Writing stratified allele frequencies (all individuals) to [ " +
	     par::output_file_name + ".frq.strat ] \n");
  else
    printLOG("Writing stratified allele frequencies (founders-only) to [ " +
	     par::output_file_name + ".frq.strat ] \n");
  
  ofstream FRQ;
  string f = par::output_file_name + ".frq.strat";
  FRQ.open(f.c_str(), ifstream::out);
  FRQ.precision(4);

  FRQ << setw(4) << "CHR" << " "
      << setw(par::pp_maxsnp) << "SNP" << " "
      << setw(8) << "CLST" << " "
      << setw(4) << "A1" << " "
      << setw(4) << "A2" << " "
      << setw(8) << "MAF" << " "
      << setw(6) << "MAC" << " "
      << setw(8) << "NCHROBS"   // Modified by JN 5/24/07
      << "\n";	  
  


  //////////////////////////////////////////
  // Calculate allele frequencies, and FST
  
  vector<Locus*>::iterator loc = locus.begin();
  vector<CSNP*>::iterator s = SNP.begin();
  
  while ( loc != locus.end() ) 
    {
      
      // Track Fst

      vector_t het(nk);
      double tothet = 0;

      // Consider each cluster

      for (int k=0; k<nk; k++)
	{
	  
	  FRQ << setw(4) << (*loc)->chr << " "
	      << setw(par::pp_maxsnp) << (*loc)->name << " ";
	  
	  (*loc)->freq = 0;
	  
	  // count 1 per allele, for frequency
	  double nmfreq = 0; 
	  double totfreq = 0;
	  double count = 0;

	  // count 1 per genotype, for missingness
	  int geno_nm = 0; 
	  bool X = false;
	  bool haploid = false;
	  
	  // Determine type of SNP
	  if (par::chr_sex[(*loc)->chr]) X=true;
	  else if (par::chr_haploid[(*loc)->chr]) haploid=true;
	  
	  
	  ///////////////////////////////
	  // Iterate over each individual
	  
	  vector<bool>::iterator i1 = (*s)->one.begin();
	  vector<bool>::iterator i2 = (*s)->two.begin();
	  vector<Individual*>::iterator person = sample.begin();
	  
	  while ( person != sample.end() ) 
	    {
	      
	      bool s1 = *i1;
	      bool s2 = *i2;
	      
	      // For allele frequencies
	      // only consider founders?	
	      
	      if ( par::summ_nonfounders || (*person)->founder ) 
		{

		  if ( (*person)->sol == k ) 
		    {
		  
		      if ( haploid || ( X && (*person)->sex ) )
			{
			  
			  //////////////////
			  // Haploid counts
			  
			  // "1" allele count
			  
			  // Possible count of 1 allele
			  totfreq++;
			  
			  if ( (!s1) && (!s2) )   //  FF = hom(11)
			    {
			      (*loc)->freq++;
			      nmfreq++;
			    }	
			  else if ( s1 && s2 )   //  TT = hom(22)
			    {
			      nmfreq++;
			    }
			  
			}
		      else
			{
			  
			  //////////////////
			  // Autosomal count
			  
			  // "1" allele count
			  
			  // Possible count of 2 alleles
			  totfreq+=2;
			  
			  if (!s1)
			    { 
			      if (!s2)  //   00 = hom(11)
				{
				  (*loc)->freq+=2;
				  nmfreq+=2;
				}	
			      else                  //   01 = het(12)
				{
				  (*loc)->freq+=1;
				  nmfreq+=2;
				}
			    }
			  else if ( s2 ) // 11 = hom(22)
			    {
			      nmfreq+=2;
			    }
			}
		      
		    }
		  
		}

	      // Next individual
	      person++;
	      i1++;
	      i2++;
	      
	    }
	  
	  if (nmfreq>0){
	    count=(*loc)->freq;  // Added by JN 5/24/07	    
	    (*loc)->freq /= (double)nmfreq;
	  }

	  string a1 = (*loc)->allele1;
	  if (a1=="") a1="0";
	  FRQ << setw(8) << kname[k] << " "
	      << setw(4)  << a1  << " "
	      << setw(4)  << (*loc)->allele2  << " "
	      << setw(8) << (*loc)->freq << " "
              << setw(6) << count << " "
              << setw(8) << nmfreq << " " // Modified by JN 5/24/07
	      << "\n";
	  
	}
                
      
      // Next SNP
      loc++;
      s++;
    }

  FRQ.close();
  shutdown();
  
}




void Plink::findMissRuns(Individual * person, ofstream & RUN)
{

  int l=0; 
  int nmiss = 0;  // now means 'not missing'
  bool run = false;
  int start = 0;
  int end = 0;

  while ( l < nl_all )
    {
      
      // Outside of a run?
      if (!run)
	{
	  // A new run? (missing)
	  if (person->one[l] && (!person->two[l]))
	    {
	      start = l;
	      nmiss=1;
	      run=true;
	    }
	}
      else // if already in a run, either end or increase length?
	{
	  // found a non-missing? 00, 11, 01

	  if ( person->one[l] == person->two[l] || person->two[l])
	    {
	      nmiss++;

	      // Average non-missing rate now too high?, given we have at least 
	      // a certain number of SNPs in run
	      // (l-start) = number of SNPs in run currently
	      
	      if ((double)nmiss / (double)(l-start) >= par::miss_run_level && (l-start) >= par::miss_run_length)
		{
		  end = l-1;
		  run = false;
		}
	    }
	  else if ( locus[l]->chr != locus[start]->chr ) // different chromosome?
	    {
	      end = l-1;
	      run = false;
	    }
	  else if ( l == (nl_all -1) ) // or end of all SNPs?
	    {
	      end = l;
	      run = false;
	    }
	}


      // Check run length?
      if (!run)
	{
	  if (par::miss_run_length_kb)
	    {
	      if ( locus[end]->bp - locus[start]->bp >= par::miss_run_length * 1000 )
		RUN << setw(par::pp_maxfid) << person->fid << " "
		    << setw(par::pp_maxiid) << person->iid << " "
		    << setw(8) << person->phenotype << " "
		    << setw(4) << locus[start]->chr << " "
		    << setw(par::pp_maxsnp) << locus[start]->name << " "
		    << setw(par::pp_maxsnp) << locus[end]->name << " "
		    << setw(10) << (double)(locus[end]->bp - locus[start]->bp)/(double)1000 << " "
		    << setw(10) << end - start +1 << " "
		    << setw(10) << (double)nmiss/(double)(end - start + 1) << "\n";
	      
	    }
	  else 
	    {
	      if ( end - start +1 >= par::miss_run_length  )
		RUN << person->fid << "\t"
		    << person->iid << "\t"
		    << locus[start]->chr << "\t"
		    << locus[start]->name << "\t"
		    << locus[end]->name << "\t"
		    << (double)(locus[end]->bp - locus[start]->bp)/(double)1000 << "\t"
		    << end - start + 1 << "\t"
		    << (double)nmiss/(double)(end - start + 1) << "\n";
	    }
	  
	  
	  
	  //////////////////
	  // Clear counters
	  
	  start = end = nmiss = 0;
	  
	}

      ///////////////
      // Next locus
      
      l++;
      
    }

}


void Plink::sexCheck()
{

  // Get range of X chromosome markers

  if (par::SNP_major) 
    SNP2Ind();

  ofstream HET;
  string f = par::output_file_name + ".sexcheck";
  HET.open(f.c_str(),ios::out);
  HET.precision(4);
        
  printLOG("Writing X-chromosome sex check results to [ "+f+" ] \n");
      
  HET << setw(par::pp_maxfid) << "FID" << " "
      << setw(par::pp_maxiid) << "IID" << " "
      << setw(12) << "PEDSEX" << " " 
      << setw(12) << "SNPSEX" << " " 
      << setw(12) << "STATUS" << " " 
      << setw(12) << "F" << "\n";
            
  for (int i1=0; i1<n; i1++)
    calcInbreeding(sample[i1],0,nl_all-1,HET);
  
  HET.close();
  
  return;
}


void Plink::calcFst()
{

  ofstream FST;
  string f = par::output_file_name + ".fst";
  FST.open(f.c_str(),ios::out);
  FST.precision(4);
  
  printLOG("Writing Fst measures to [ "+f+" ] \n");
  
  FST << setw(4) << "CHR" << " "
      << setw(par::pp_maxsnp) << "SNP" << " "
      << setw(20) << "CLST" << " " 
      << setw(8) << "N" << " " 
      << setw(8) << "HET" << "\n"; 
            
  for (int l = 0 ; l < nl_all; l++)
    {

      bool skip = false;

      if ( par::chr_sex[locus[l]->chr] || 
	   par::chr_haploid[locus[l]->chr] ) skip = true;
      if ( locus[l]->nm <= 1 || locus[l]->freq < 1e-8 )
	skip = true;

      if ( skip ) 
	continue;

      
      double f = 0;
      
      vector_t hs(nk);
      double ht = 2 * locus[l]->freq * ( 1 - locus[l]->freq );

      // Calculate Fst here
      
//       double avg_hs = 0;
//       for (int k = 0; k < nk ; k++ )
// 	avg_hs += het[k];
//       avg_hs /= (double)nk;

//       double f = ( hs - ht ) / ht;


      for ( int k = 0 ; k < nk ; k++ )
	{
// 	  FST << setw(4) << locus[l]->chr  << " "
// 	      << setw(par::pp_maxsnp) << locus[l]->name << " "
// 	      << setw(20) << kname[k] << " " 
// 	      << setw(8) << kind[k] << " " 
// 	      << setw(12) << het[k] << "\n";
	}

      
      FST << setw(4) << locus[l]->chr << " "
	  << setw(par::pp_maxsnp) << locus[l]->name << " "
	  << setw(20) << "_FST_" << " " 
	  << setw(8) << locus[l]->nm << " " 
	  << setw(12) << f << "\n";
      
    }
  
  FST.close();
  
  return;
}




double Plink::calcInbreeding(Individual * p1, int m1, int m2, ofstream & HET)
{

  // P(Homo) = F + (1-F)P(Homo by chance)

  // P(Homo by chance) = p^2+q^2 for a biallelic locus.
  // For an individual with N genotyped loci, we
  //   1. count the total observed number of loci which are homozygous (O),
  //   2. calculate the total expected number of loci homozygous by chance (E)
  // Then, using the method of moments, we have
  //    O = NF + (1-F)E
  // Which rearranges to give
  //    F = (O-E)/(N-E)

  
  // Count of nonmissing loci
  double N=0;
  double O=0;
  double E=0;

  // Consider all loci
  for (int l=m1; l<=m2;l++)
    {

      ////////////////////////////////////////////////
      // Skip X and haploid chromosome markers, or not
      
      if ( par::check_sex ) // For sex-checks
	{
	  // only consider the X chromosome
	  if ( ! par::chr_sex[locus[l]->chr] ) continue; 
	}
      else // normal heterozygosity calculation
	{
	  // so skip haploid markers
	  if ( par::chr_sex[locus[l]->chr] || 
	       par::chr_haploid[locus[l]->chr] ) continue; 
	}
      
      // Skip monomorphic markers, uninformative markers
      if ( locus[l]->nm <= 1 || locus[l]->freq < 1e-8 )
	continue;

      //////////////////
      // Observed data
      
      // check not missing:
      if (!(p1->one[l] && (!p1->two[l])))
	{
	  // homozygous non-missing loci
	  if (p1->one[l] == p1->two[l]) O++;
	  // non-missing loci
	  N++;
	  

	  /////////////////////////
	  // Expected homozygousity
	  
	  // E = 2pq . 2N/(2N-1)
	  // (Using Nei's unbiased estimator)
	  
 	  E += 1
	    - ( 2 * locus[l]->freq * ( 1 - locus[l]->freq ) 
		* ( locus[l]->nm / ( locus[l]->nm - 1 ) ) );
	  

	}
           
    }

  double F = (O-E)/(N-E);

  if ( par::check_sex)
    {
      HET << setw(par::pp_maxfid) << p1->fid << " " 
	  << setw(par::pp_maxiid) << p1->iid << " "      
	  << setw(12) << p1->sexcode << " ";

      if ( F > par::sex_threshold_male ) 
	{
	  HET << setw(12) << 1 << " ";
	  if (p1->sexcode == "1")
	    HET << setw(12) << "OK" << " ";
	  else
	    {
	      HET << setw(12) << "PROBLEM" << " ";
	      if (par::impute_sex)
		{
		  p1->sexcode = "1";
		  p1->sex = true;
		}
	    }
	}
      else if ( F < par::sex_threshold_female )
	{
	  HET << setw(12) << 2 << " ";
	  if (p1->sexcode == "2")
	    HET << setw(12) << "OK" << " ";
	  else
	    {
	      HET << setw(12) << "PROBLEM" << " ";		  
	      if (par::impute_sex)
		{
		  p1->sexcode = "2";
		  p1->sex = false;
		}
	    }
	}
      else 
	{
	  HET << setw(12) << 0 << " "
	      << setw(12) << "PROBLEM" << " ";		  
	  
	  if (par::impute_sex)
	    {
	      p1->sexcode = "0";
	      p1->sex = false;
	      if (!par::ignore_missing_sex)
		p1->missing = true;	      
	    }
	  
	}


      HET << setw(12) << F << "\n";
    }
  else
    HET << setw(par::pp_maxfid) << p1->fid << " " 
	<< setw(par::pp_maxiid) << p1->iid << " "
	<< setw(12) << (int)O << " " 
	<< setw(12) << E << " "
	<< setw(12) << (int)N << " "
	<< setw(12) << F << "\n";
  
  return F;
  
}


Z Plink::calcGenomeIBS(Individual * p1, Individual * p2)
{

  // Vector of average genome-wide IBS
  Z IBSg;
  
  // Count of nonmissing loci
  int cnt=0;

  // Other metrics ( in Plink:: )
  pvIBS0 = 0;
  pvIBS2het = 0;
  int last_chr = -1;
  int last_bp = -1;
  

  // Consider all autosomal loci
  vector<bool>::iterator ia1 = p1->one.begin();
  vector<bool>::iterator ia2 = p1->two.begin();

  vector<bool>::iterator ib1 = p2->one.begin();
  vector<bool>::iterator ib2 = p2->two.begin();
  int l=0;
  
  while ( ia1 != p1->one.end() )
    {
      
      // Skip X and haploid chromosome markers
      if ( par::chr_sex[locus[l]->chr] || 
	   par::chr_haploid[locus[l]->chr] ) 
	{
	  l++;
	  ia1++;
	  ia2++;
	  ib1++;
	  ib2++;
	  continue; 
	}

      // Only count if both genotypes nonmissing
      bool a1 = *ia1;
      bool a2 = *ia2;
      if (a1 && !a2) 
	{
	  l++;
	  ia1++;
	  ia2++;
	  ib1++;
	  ib2++;
	  continue; 
	}

      bool b1 = *ib1;
      bool b2 = *ib2;
      if (b1 && !b2) 
	{
	  l++;
	  ia1++;
	  ia2++;
	  ib1++;
	  ib2++;
	  continue; 
	}

      
      // Calculate IBS from genotypes

      // 10 = missing
      // 00 = 11hom
      // 01 = 12het
      // 11 = 22hom
      
      if ( a1 == b1 && a2 == b2 ) IBSg.z2++;      // IBS 2
      else if ( a1 != b1 && a2 != b2 ) IBSg.z0++; // IBS 0
      else IBSg.z1++;                             // IBS 1
      
      cnt++;
      
      // Also calculate p-value binomial test
      if ( ! ( par::matrix || par::cluster || par::genome_output ) ) 
	{
	  ia1++;
	  ia2++;
	  ib1++;
	  ib2++;
	  l++;	  
	  continue;
	}
      
      if ( a1 != b1 && a2 != b2 ) // IBS 0 hom/hom
	{
	  // Can we count this?
	  if (locus[l]->chr != last_chr ||
	      locus[l]->bp > last_bp + par::ibstest_gap)
	    {
	      pvIBS0++;
	      last_chr = locus[l]->chr;
	      last_bp = locus[l]->bp;
	    }
	}
      else if ( a1 != a2 && b1 != b2 )  // IBS 2 het/het
	{
	  // Can we count this?
	  if (locus[l]->chr != last_chr ||
	      locus[l]->bp > last_bp + par::ibstest_gap)
	    {
	      pvIBS2het++;
	      last_chr = locus[l]->chr;
	      last_bp = locus[l]->bp;
	    }
	}
      
      // Next SNP
      ia1++;
      ia2++;
      ib1++;
      ib2++;
      l++;
      
    }
  
  

  
  if (cnt==0)
    { 
      string msg = "No nonmissing markers for individuals "
	+ p1->fid + " "
	+ p1->iid + " - " 
	+ p2->fid + " "
	+ p2->iid;
      error(msg);
    }


  // Standard genetic distance (proportion IBS 0 to 1) 
  if ( par::cluster_euclidean )
    dst = sqrt((IBSg.z1*0.5 + IBSg.z2*2)/(IBSg.z0+IBSg.z1+IBSg.z2*2));
  else 
    dst = (IBSg.z1*0.5 + IBSg.z2)/(IBSg.z0+IBSg.z1+IBSg.z2);


  // Also calculate p-value binomial test
  if ( par::cluster_missing || ! ( par::matrix || par::cluster || par::genome_output ) ) 
    return IBSg;

    
  // Calculate p-value for IBS test
  // IBS0 : IBS2(het) in 1:2 ratio 
  // n.b.  0.2222222 = 0.666*(1-0.666)
  double z = (pvIBS2het/(pvIBS0+pvIBS2het)-0.666666) 
    / (sqrt(0.2222222/(pvIBS0+pvIBS2het)));
  
  // Store p-value in Plink::pv
  pv = normdist(z);
  
  // Return counts
  return IBSg;
}


void Plink::preCalcGenomeIBD()
{
  
  // Take pairwise IBS and information on allele frequencies
  // to generate genome-wide IBD estimates
  
  // Expected IBS given IBD

  // Bias-corrected versison of estimator for expected proportion of
  // IBS SNP pairs given IBD status (cf. Nei bias-corrected
  // heterozygosity estimator).


  // All possible permutations of taking 4 alleles from 2N = 2N(2N-1)(2N-2)(2N-3)

  // x = count of allele 1
  // y = count of allele 2
  // p = x/2N, q=y/2N

  // Of these, 
  //            x(x-1)y(y-1) will be of order 11 22, 
  //            y(y-1)x(x-1) will be of order 22 11

  // Therefore the probability of IBS 0 given IBS 0 is 
  // E00 = 2 p*p*q*q * ( (x-1)/x * (y-1)/y * (2N/(2N-1)) * (2N/(2N-2)) * (2N/(2N-3)) )
  // and so on for E01, etc
  
  // E(IBD)(IBS)
  // IBS >= IBD
  
  E00=E10=E20=E01=E11=E21=E02=E12=E22=0;
  int cnt = 0;
  for (int l=0; l<nl_all; l++)
    {
      
      if ( par::chr_sex[locus[l]->chr] || 
	   par::chr_haploid[locus[l]->chr] ) continue;
      
      double p = locus[l]->freq;
      double q = 1 - p;

      double Na = locus[l]->nm; // = # alleles = 2N where N is number of individuals
      double x = p * Na;
      double y = q * Na;
      
      // Original, non bias-corrected versions
      //       E00 += 2*p*p*q*q; 
      //       E01 += 4*p*p*p*q+4*p*q*q*q;
      //       E02 += q*q*q*q + p*p*p*p + 4*p*p*q*q;
      //       E11 += 2*p*p*q + 2*p*q*q; 
      //       E12 += p*p*p + q*q*q + p*p*q + p*q*q;	
      

      double a00 = 2*p*p*q*q * ( (x-1)/x * (y-1)/y * (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) );
      
      double a01 = 4*p*p*p*q * ( (x-1)/x * (x-2)/x *  (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) )
	+ 4*p*q*q*q * ( (y-1)/y * (y-2)/y *  (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) );
      
      double a02 = q*q*q*q * ( (y-1)/y * (y-2)/y * (y-3)/y * (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) )
	+ p*p*p*p  * ( (x-1)/x * (x-2)/x * (x-3)/x * (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) )
	+ 4*p*p*q*q * ( (x-1)/x * (y-1)/y *           (Na/(Na-1)) * (Na/(Na-2)) * (Na/(Na-3)) );

      double a11 = 2*p*p*q * ( (x-1)/x *  Na/(Na-1) * Na/(Na-2) )
	+ 2*p*q*q * ( (y-1)/y *  Na/(Na-1) * Na/(Na-2) );
      
      double a12 = p*p*p * ((x-1)/x * (x-2)/x *  Na/(Na-1) * Na/(Na-2)) 
	+ q*q*q * ( (y-1)/y * (y-2)/y *  Na/(Na-1) * Na/(Na-2))
	+ p*p*q * ( (x-1)/x * Na/(Na-1) * Na/(Na-2) )
	+ p*q*q * ((y-1)/y  * Na/(Na-1) * Na/(Na-2));

      if ( realnum(a00) &&
           realnum(a01) &&
           realnum(a02) &&
           realnum(a11) &&
           realnum(a12) )
        {
          E00 += a00;
          E01 += a01;
          E02 += a02;
          E11 += a11;
          E12 += a12;
          cnt++;
        }

      
    }
  
  E00 /= cnt; E10  = 0;   E20 = 0;
  E01 /= cnt; E11 /= cnt; E21 = 0;
  E02 /= cnt; E12 /= cnt; E22 = 1;
  
  if (par::verbose) { 
    cout << "P(IBS|IBD)  -- IBS row; IBD col\n";
    cout << E00 << "\t" << E10 << "\t" << E20 << "\n";
    cout << E01 << "\t" << E11 << "\t" << E21 << "\n";
    cout << E02 << "\t" << E12 << "\t" << E22 << "\n";
    cout << "\n";
  }
  
}



Z Plink::calcGenomeIBD(Individual * p1, Individual * p2, Z IBSg)
{
  
  Z z;
  
  double S = IBSg.z0 + IBSg.z1 + IBSg.z2;

  // E_IBS[row=IBS][col=IBD]
  // E(IBD)(IBS)
  
  double e00 = E00*S; 
  double e10 = E10*S; 
  double e20 = E20*S;
  
  double e01 = E01*S; 
  double e11 = E11*S; 
  double e21 = E21*S;
  
  double e02 = E02*S; 
  double e12 = E12*S; 
  double e22 = E22*S;
  
  z.z0 =  IBSg.z0 / e00;
  z.z1 = (IBSg.z1 - z.z0*e01) / e11;
  z.z2 = (IBSg.z2 - z.z0*e02 - z.z1*e12) / e22; 

  if (par::debug)
    cout << "DEBUG\t"
	 << z.z0 << " "
	 << z.z1 << " " 
	 << z.z2 << "\n";


  // Bound IBD estimates to sum to 1 
  // and fall within 0-1 range
  if (par::bound)
  {  
  if (z.z0>1) { z.z0=1; z.z1=z.z2=0; }
  if (z.z1>1) { z.z1=1; z.z0=z.z2=0; }
  if (z.z2>1) { z.z2=1; z.z0=z.z1=0; }
  
  if (z.z0<0) { double S=z.z1+z.z2; z.z1/=S; z.z2/=S; z.z0=0; }
  if (z.z1<0) { double S=z.z0+z.z2; z.z0/=S; z.z2/=S; z.z1=0; }
  if (z.z2<0) { double S=z.z0+z.z1; z.z0/=S; z.z1/=S; z.z2=0; }
  }
  
  // Possibly constrain IBD estimates to within possible triangle
  // i.e. 0.5 0.0 0.5 is invalid
  //
  // For purposes of sample checks, etc, we do not automatically do this (--genome)
  // For PLINK analysis we do (--plink)
  //
  // Constraint : z1^2 - 4 z0 z2 >= 0
  //            : x^2 - 2 pi x + z2  = 0
  //
  //              where pi = (z1 + 2 z2) / 2
  //
  // So the constaint can also be written as
  //
  //              pi^2 >=  z2

  
  double pihat =   z.z1/2 + z.z2 ;
  double impossible = false;
  
  if ( ( pihat * pihat ) < z.z2 ) 
    {
      impossible = true;
      
      // find new value for z1 (z1*) which satisfies the equation 
      //
      //     (z1* + 2 pi^2) / 2  = pi
      //
      // this gives
      //
      //      z1* = 2pi(1-pi)
      
      // the transformed IBD probabilities would be
      // 1 - 2pi(1-pi) - pi^2, 2pi(1-pi), pi^2
      
      if (par::nudge)
	{
	  z.z0 = ( 1 - pihat) * ( 1 - pihat);
	  z.z1 = 2 * pihat * (1-pihat);
	  z.z2 = pihat * pihat;
	}
    }


  if ( par::genome_output_minimal )
    {
      ZOUTFILE << dbl2str_fixed(dst,6) << " " 
	       << dbl2str(pv) << " " 
	       << dbl2str(z.z1/2 + z.z2) << "\n";     
    }
  else if (par::genome_output)
    {
      
      if ( (!par::pihat_filter) || 
	   ( pihat >= par::MIN_PIHAT && pihat <= par::MAX_PIHAT) )
	{
	  
	  ZOUTFILE << sw( p1->fid, par::pp_maxfid)
		   << sw( p1->iid, par::pp_maxiid)
		   << sw( p2->fid, par::pp_maxfid)
		   << sw( p2->iid, par::pp_maxiid);

	  string rt = relType(p1,p2);

	  ZOUTFILE << sw(rt,3);
	  
	  if ( rt == "UN" )
	    ZOUTFILE << sw("NA", 6);
	  else
	    ZOUTFILE << sw( genrel(p1,p2) , 6 );
	  

	  if (par::show_impossible_IBD || !impossible)
	    ZOUTFILE << sw(z.z0, 4,8) 
		     << sw(z.z1, 4,8) 
		     << sw(z.z2, 4,8);
	  else
	    ZOUTFILE << sw( -z.z0, 8)
		     << sw( -z.z1, 8)
		     << sw( -z.z2, 8);
	  
	  ZOUTFILE << sw( z.z1/2 + z.z2, 4, 8);
	  
	  if (par::bt)
	    {
	      if ( (!p1->aff) && (!p2->aff) ) 
		ZOUTFILE << sw("-1", 4);
	      else if ( p1->aff && p2->aff )
		ZOUTFILE << sw("1",4);
	      else if ((!p1->aff) && p2->aff)
		ZOUTFILE << sw("0",4);
	      else if (p1->aff && !p2->aff)
		ZOUTFILE << sw("0",4);
	      else
		ZOUTFILE << sw("NA",4);
	    }
	  else
	    ZOUTFILE << sw("NA",4);
	  
	  ZOUTFILE << sw(dst,6,10);
	  ZOUTFILE << sw(pv,4,8);
	  
	  double ov = (double)pvIBS2het / (double)pvIBS0;
	  if ( realnum(ov) )
	    ZOUTFILE << sw(ov,4,8);
	  else
	    ZOUTFILE << sw("NA",8);
	  
	  if ( par::genome_output_full )
	    ZOUTFILE << sw((int)IBSg.z0, 8)
		     << sw((int)IBSg.z1, 8) 
		     << sw((int)IBSg.z2, 8) 
		     << sw(pvIBS0, 4,8) 
		     << sw(pvIBS2het, 4, 8);
	  
	  ZOUTFILE << "\n";
	}
    } 
  return z;
}


void Plink::displayGenomeWideInfo()
{

  ///////////////////////////////////////
  // This is an individual-mode analysis

  if (par::SNP_major) SNP2Ind();

  string f = par::output_file_name + ".genome";
  if ( par::genome_output_minimal ) f += ".min";
  if ( par::compress_genome ) f += ".gz";

  if ( par::genome_output_minimal )
    printLOG("Writing minimal-format IBS information to [ " + f + " ] \n");      
  else
    printLOG("Writing whole genome IBS/IBD information to [ " + f + " ] \n");

  ZOUTFILE.open( f , par::compress_genome );

  stringstream s2;
  s2 << "Filtering output to include pairs with ( " 
     << par::MIN_PIHAT  << " <= PI-HAT <= " 
     << par::MAX_PIHAT << " )\n";
  printLOG(s2.str());
  
  if ( par::genome_output_minimal )
    {
      for (int i=0; i<n; i++)
	ZOUTFILE << sample[i]->fid << " " << sample[i]->iid << "\n";
      ZOUTFILE << "__END __END\n";
    }
  else
   {
     ZOUTFILE << sw("FID1",par::pp_maxfid)
	      << sw("IID1",par::pp_maxiid)
	      << sw("FID2",par::pp_maxfid)
 	      << sw("IID2",par::pp_maxiid)
 	      << sw("RT",3)
 	      << sw("EZ",6)
 	      << sw("Z0",8)
 	      << sw("Z1",8)
 	      << sw("Z2",8)
 	      << sw("PI_HAT",8)
 	      << sw("PHE",4)
 	      << sw("DST",10)
 	      << sw("PPC",8)
 	      << sw("RATIO",8);

     if ( par::genome_output_full )
       ZOUTFILE << sw("IBS0",8)
		<< sw("IBS1",8)
		<< sw("IBS2",8)
		<< sw("HOMHOM",8)
		<< sw("HETHET",8);
     
     ZOUTFILE << "\n"; 
   }

  
  int c=0;
  int c2=0;
  for (int i1=0; i1<n-1; i1++)
    for (int i2=i1+1; i2<n; i2++)
      {
	Individual * p1 = sample[i1];
	Individual * p2 = sample[i2];
	
	// Only update message every 100 iterations
	if ( (!par::silent ) && c==c2 || c==np)
	  {
	    cout << "IBD(g) calculation: " 
		 << c++ << " of " << np 
		 << "                  \r";
	    cout.flush();
	    c2+=100;
	  }
	else
	  ++c;

	
	/////////////////////////
	// Or perhaps we can skip?
	
	if ( par::genome_2sets )
	  {	    
	    if ( ! ( (gset1.find(p1) != gset1.end() && 
		      gset2.find(p2) != gset2.end() ) ||
		     (gset1.find(p2) != gset1.end() && 
		      gset2.find(p1) != gset2.end() ) ) )
	      continue;
	  }

	
	// Are we set to only consider people in the same family
	// (i.e. confirm known relatedness)?

	if ( par::genome_only_check_rels )
	  {
	    if ( relType(p1,p2)=="UN" )
	      continue;	    
	  }



	//////////////////////////////////////////
	// Perform main calculations for this pair
	
	Z IBSg = calcGenomeIBS(p1,p2);
	Z IBDg = calcGenomeIBD(p1,p2,IBSg);

	
	if ( par::genome_test ) 
	  {
	    if ( IBDg.z1/2 + IBDg.z2 >= par::genome_test_threshold )
	      {
		int2 pair;
		pair.p1 = i1;
		pair.p2 = i2;
		related.insert(pair);
	      }
	  }
      }	
  
  if (!par::silent)
    cout << "\n";
  
  ZOUTFILE.close();
  
}




void Plink::calcGenomeIBM(Individual * p1, Individual * p2)
{

  // Individual-major mode assumed
  
  int cnt = 0;
  
  // Consider all loci

  vector<bool>::iterator ia1 = p1->one.begin();
  vector<bool>::iterator ia2 = p1->two.begin();
  
  vector<bool>::iterator ib1 = p2->one.begin();
  vector<bool>::iterator ib2 = p2->two.begin();
  
  while ( ia1 != p1->one.end() )
    {          

      // Count discordantly missing SNPs
      
      if ( *ia1 && !*ia2 )
	{
	  if ( ! ( *ib1 && !*ib2 ) ) cnt++;
	}
      else if ( *ib1 && !*ib2 ) cnt++;
	
      // Next SNP      
      ia1++;
      ia2++;
      ib1++;
      ib2++;

    } 

  // IBM similiarity metric
  dst = 1.0 - ( (double)cnt / (double)nl_all );
}



void Plink::pruneLD()
{

  if (!par::SNP_major)
    Ind2SNP();

  printLOG("Performing LD-based pruning...\n");
  
  string f = par::output_file_name + ".prune.in";
  ofstream PIN(f.c_str(),ios::out);
  printLOG("Writing pruned-in SNPs to [ " + f + " ]\n");

  f = par::output_file_name + ".prune.out";
  ofstream POUT(f.c_str(),ios::out);
  printLOG("Writing pruned-out SNPs to [ " + f + " ]\n");

  int win_start = 0;
  int win_end = win_start + par::prune_ld_win;
  
  ////////////////////////
  // Scan each chromosome
  
  vector<int> chrs;
  if (par::run_chr==0) 
    {
      vector<int> r = getChromosomeRange(*this);

      printLOG("Scanning from chromosome "+
	       chromosomeName( r[0] ) +" to "+
	       chromosomeName( r[1] ) +"\n\n");

      for (int i=r[0];i<=r[1];i++) 
	{
	  if (seeChromosome(*this,i))
	    chrs.push_back(i);
	}
    } 
  else 
    chrs.push_back(par::run_chr);
  
  // Inclusion or no?
  vector<bool> include(nl_all,true);

  // Only consider founders (set flag)
  vector<Individual*>::iterator person = sample.begin();
  while ( person != sample.end() )
    {
      if ( (*person)->founder ) 
	(*person)->flag = true;
      else
	(*person)->flag = false;
      person++;
    }
  
  // Scan each chromosome
  for (int i=0;i<chrs.size();i++)
    {

      // Skip chromosome 0 
      if ( chrs[i] == 0 ) 
	{
	  printLOG("Skippng chromosome 0\n");
	  continue;
	}

      // Set chromosome
      par::run_chr = chrs[i];
      
      // Find scan range
      setMarkerRange();
      
      // go from par::run_start to par::run_end
      int s1 = par::run_start;
      int s2 = par::run_start + par::prune_ld_win - 1;

      // MW fix
      if (s2 > par::run_end ) 
      	s2 = par::run_end;

      while ( s2 <= par::run_end )
	{
	  
	  // calc VIF and set 
	  vector<int> nSNP(0);
	  for (int l=s1; l<=s2; l++)
	    if ( include[l] )
	      {
		nSNP.push_back( l );
	      }
	  
	  // Skip if we only have a single SNP left
	  if (nSNP.size() < 2)
	    {
	      if ( s2 == par::run_end ) 
		break;
	      
	      s1 += par::prune_ld_step;
	      s2 += par::prune_ld_step;
	      
	      if (s2 > par::run_end) 
		s2 = par::run_end;
	      
	      if ( s2-s1 < 1)
		break;
	      
	      continue;
	    }

	  vector<vector<double> > variance;
	  
	  if (!par::silent)
	    {
	      cout << "Pruning SNPs " << s1-par::run_start+1
		   << " to " << s2-par::run_start+1
		   << " of " << par::run_end - par::run_start+1 
		   << "         \r";
	      cout.flush();
	    }

	  // Calculate covariance matrices
	  variance = calcSetCovarianceMatrix(nSNP);
	  
	  // Calculate VIFs
	  vector<bool> cur = vif_prune(variance,par::prune_ld_vif,nSNP);

	  // Update main list
	  int k=0;
	  for (int l=s1; l<=s2; l++)
	    {
	      // Update main list, but do not get back
	      // already excluded SNPs
	      
	      if (include[l] && !cur[k++])
		include[l] = false;
	    }
	  
	  // Advance window
	  if ( s2 == par::run_end ) 
	    break;
	  
	  s1 += par::prune_ld_step;
	  s2 += par::prune_ld_step;
	  
 	  if (s2 > par::run_end) 
 	    s2 = par::run_end;

	  if ( s2-s1 < 1)
	    break;
	  
	} // next window
      
      if (!par::silent)
	cout << "\n";

      // Record what is in, what is out
      int cnt_in = 0, cnt_out = 0;
      for (int l=par::run_start; l<=par::run_end; l++)
	{
	  if (include[l]) 
	    {
	      PIN << locus[l]->name << "\n";
	      cnt_in++;
	    }
	  else
	    {
	      POUT << locus[l]->name << "\n";
	      cnt_out++;
	    }
	}

      printLOG("For chromosome "+int2str(par::run_chr)+
	       ", "+int2str(cnt_out)+
	       " SNPs pruned out, "+int2str(cnt_in)+" remaining\n");
      
    } // next chromosome


  PIN.close();
  POUT.close();

}