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
|
//////////////////////////////////////////////////////////////////
// //
// 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 "crandom.h"
#define MISSING(i,l) ( SNP[l]->one[i] && ( ! SNP[l]->two[i] ) )
void Plink::filterSNPs()
{
//////////////////////////////////////////////////////
// This functions applies the following filters and
// functions:
// Counts of number of founders, nonfounders
// Per-individual genotyping rate
// Read in, or calculate, allele frequencies (and save these)
// Optionally write allele frequencies, then close
// Exclude SNPs with too many missing genotypes
// Identify/correct heterozygote haploid
// Identify SNPs with no founder genotypes
// Calculate/report/filter on HWE tests
// Calculate/report genotyping rate per SNP/per individual
// Filter on MAF
// Remove filtered-out SNPs
bool original_SNP_major = par::SNP_major;
if ( ! par::SNP_major )
Ind2SNP();
// Which SNPs to delete
vector<bool> del(locus.size(),false);
// Which individuals to delete
vector<bool> indel(sample.size(),false);
//////////////////////////////////////////
// Display number of founders/nonfounders
cnt_f=0;
vector<Individual*>::iterator person = sample.begin();
while ( person != sample.end() )
{
if ( (*person)->founder ) cnt_f++;
person++;
}
printLOG(int2str(cnt_f)+" founders and "+int2str(n-cnt_f)+
" non-founders found\n");
if (cnt_f<n) par::has_nonfounders = true;
////////////////////////////////////////////
// If we have obligatory missing genotypes:
// ensure they really are missng
if ( par::oblig_missing )
{
set<int2>::iterator p = oblig_missing.begin();
while ( p != oblig_missing.end() )
{
int l = p->p1;
int k = p->p2;
for (int i=0; i<sample.size(); i++)
{
Individual * person = sample[i];
if ( person->sol == k )
{
SNP[l]->one[i] = true;
SNP[l]->two[i] = false;
}
}
++p;
}
}
/////////////////////////////////////////////////
// Remove individuals with too many missing calls
double total_genotyping = 0;
if ( par::MAX_IND_MISSING < 1 )
{
int n_removed = 0;
int n_orig = n;
// Consider each individual
if ( ! par::oblig_missing )
{
for (int i=0;i<sample.size();i++)
{
bool female = !sample[i]->sex;
// Sum missingness over all SNPs
int m=0; // Missing SNPs
int nsnps=0; // All non-obligatory missing SNPs
for (int l=0; l<locus.size();l++)
{
// Skip female Y chromosomes
if ( female && par::chr_Y[locus[l]->chr] )
{
continue;
}
++nsnps;
if ( MISSING(i,l) ) m++;
}
// Too much missingness?
if ( (double)m/(double)nsnps > par::MAX_IND_MISSING )
{
indel[i] = true;
n_removed++;
}
} // next individual
}
else // ... allow oblig missing values
{
for (int i=0;i<sample.size();i++)
{
bool female = ! sample[i]->sex;
// Sum missingness over all SNPs
int m=0; // Missing SNPs
int nsnps=0; // All non-obligatory missing SNPs
for (int l=0; l<locus.size();l++)
{
// Skip female Y chromosomes
if ( female && par::chr_Y[locus[l]->chr] )
continue;
if ( ! obligMissing(i,l) )
{
if ( MISSING(i,l) )
++m;
++nsnps;
}
}
// Too much missingness?
if ( (double)m/(double)nsnps > par::MAX_IND_MISSING )
{
indel[i] = true;
n_removed++;
}
} // next individual
} // end if oblig-missing section
////////////////////////////////////////
// Save list of any removed individuals
if (n_removed>0)
{
string f = par::output_file_name + ".irem";
printLOG("Writing list of removed individuals to [ " + f + " ]\n");
ofstream REM;
REM.open(f.c_str(), ifstream::out);
for (int i=0;i<sample.size();i++)
if (indel[i])
REM << sample[i]->fid << "\t" << sample[i]->iid << "\n";
REM.close();
// And now remove these individuals, so that
// SNP-based statistics are calculated with
// these samples already excluded
n_removed = deleteIndividuals(indel);
}
printLOG(int2str(n_removed)+" of "+int2str(n_orig));
printLOG(" individuals removed for low genotyping ( MIND > ");
printLOG(dbl2str(par::MAX_IND_MISSING)+" )\n");
} // end of remove people conditional
/////////////////////////////////
// Calculate or read from file?
if (par::af_read)
{
checkFileExists(par::af_file);
printLOG( "Reading allele frequencies from [ " + par::af_file + " ] \n");
// Make hash of original SNP names
map<string,int> mlocus;
map<string,int>::iterator ilocus;
vector<Locus*>::iterator loc = locus.begin();
int l=0;
while ( loc != locus.end() )
{
mlocus.insert(make_pair( (*loc)->name,l));
loc++;
l++;
}
// Read allele frequencies
ifstream FRQ;
FRQ.open(par::af_file.c_str());
FRQ.clear();
string dum1, dum2, dum3, dum4, dum5, dum6;
string snpname;
double freq;
int nm;
loc = locus.begin();
while ( loc != locus.end() )
{
(*loc)->freq = -1;
(*loc)->nm = 0;
loc++;
}
// Skip header line
FRQ >> dum1 >> dum2 >> dum3 >> dum4 >> dum5 >> dum6;
while(!FRQ.eof())
{
vector<string> tokens = tokenizeLine(FRQ);
if (tokens.size() == 0)
continue;
else if (tokens.size() != 6)
{
string sline="";
for (int i=0; i<tokens.size(); i++)
sline += tokens[i] + " ";
error("Problem with allele frequency line: 6 fields required:\n"
+sline+"\n");
}
ilocus = mlocus.find(tokens[1]);
if (ilocus != mlocus.end())
{
string rareAllele = tokens[2];
string commonAllele = tokens[3];
Locus * loc = locus[ilocus->second];
if( ! from_string<double>( loc->freq, tokens[4],std::dec))
{
loc->freq = 0;
loc->nm = 0;
}
else if( ! from_string<int>(loc->nm,tokens[5],std::dec))
{
loc->freq = 0;
loc->nm = 0;
}
// But was that pointing to the correct allele?
if ( rareAllele == loc->allele2 &&
rareAllele != par::missing_genotype &&
loc->allele2 != par::missing_genotype )
loc->freq = 1 - loc->freq;
else if ( commonAllele == loc->allele1 &&
commonAllele != par::missing_genotype &&
loc->allele1 != par::missing_genotype )
loc->freq = 1 - loc->freq;
}
}
FRQ.clear();
FRQ.close();
}
/////////////////////////////////
// Calculate allele frequencies
vector<string> hetlist(0);
vector<bool>::iterator d = del.begin();
vector<Locus*>::iterator loc = locus.begin();
vector<CSNP*>::iterator s = SNP.begin();
int l = 0; // Main locus counter
int exc_maf = 0;
int exc_miss = 0;
vector<Locus*> no_founders_found_list;
while ( loc != locus.end() )
{
if (!par::af_read)
{
(*loc)->freq = 0;
// count 1 per allele, for frequency
(*loc)->nm = 0;
}
// count 1 per genotype, for missingness
int geno_nm = 0;
// count 1 per non-obligatory missing genotype
// (or set to N individuals)
int geno_real = 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();
int i = 0;
while ( person != sample.end() )
{
bool s1 = *i1;
bool s2 = *i2;
// Check female Y genotypes
if ( par::chr_Y[(*loc)->chr] && ! (*person)->sex )
{
// Set to missing, unless in a RECODE mode
if ( ! par::preserve_all_genotypes )
{
s1 = *i1 = true;
s2 = *i2 = false;
}
// But in any case, do not include this marker in
// any genotype counts: skip to next person
++person;
++i;
++i1;
++i2;
continue;
}
// For haploid heterozygosity check, also consider all individuals
if ( haploid || ( X && (*person)->sex ) )
{
if ( (!s1) && s2 )
{
hetlist.push_back( (*person)->fid + "\t" +
(*person)->iid + "\t" +
(*loc)->name );
// Set to missing, unless in a RECODE mode
if ( ! par::preserve_all_genotypes )
{
s1 = *i1 = true;
s2 = *i2 = false;
}
}
}
// For missing genotypes
if ( ! ( s1 && (!s2) ) )
geno_nm++;
// But is this a real genotype in any case?
if ( par::oblig_missing )
{
if ( ! obligMissing(i,l) )
++geno_real;
}
else
++geno_real;
// Do not recount alleles if we have read in allele frequencies
if (!par::af_read)
{
// For allele frequencies
// only consider founders
if ( par::summ_nonfounders || (*person)->founder )
{
if ( haploid || ( X && (*person)->sex ) )
{
//////////////////
// Haploid counts
// "1" allele count
if ( (!s1) && (!s2) ) // FF = hom(11)
{
(*loc)->freq++;
(*loc)->nm++;
}
else if ( s1 && s2 ) // TT = hom(22)
{
(*loc)->nm++;
}
}
else
{
//////////////////
// Autosomal count
// "1" allele count
if (!s1)
{
if (!s2) // 00 = hom(11)
{
(*loc)->freq+=2;
(*loc)->nm+=2;
}
else // 01 = het(12)
{
(*loc)->freq+=1;
(*loc)->nm+=2;
}
}
else if ( s2 ) // 11 = hom(22)
{
(*loc)->nm+=2;
}
}
}
}
// Next individual
++person;
++i;
++i1;
++i2;
}
////////////////////////////////
// Calculate allele frequencies
if (!par::af_read)
{
if ( par::af_count) // Allele counts...
{
// Use freq to store count (keep as is)
// Use "bp" to store number of allele 2
(*loc)->bp = (long int)((*loc)->nm - (*loc)->freq);
// Use "pos" to store number missing genotypes
(*loc)->pos = geno_real - geno_nm;
}
else // ... or frequencies
{
if ((*loc)->nm>0)
(*loc)->freq /= (double)(*loc)->nm;
else
{
(*loc)->freq = 1;
// If we aren't getting rid of it anyway
if ( (double)geno_nm/(double)geno_real >= (1-par::MAX_GENO_MISSING))
no_founders_found_list.push_back(*loc);
}
}
}
//////////////////////////////////////////
// Record total proportion of missingness
double snp_genotyping = n>0 ? (double)geno_nm/(double)geno_real : 0;
total_genotyping += snp_genotyping;
/////////////////////////////////////////////////
// Exclude if SNP has too many missing genotypes
if ( snp_genotyping < (1-par::MAX_GENO_MISSING) )
{
*d = true;
exc_miss++;
}
////////////////////////////////////////////////
// Make allele1 always the least common allele
if ( par::make_minor_allele && (!par::af_count) && (*loc)->freq > 0.5 )
{
// then we need to swap alleles
(*loc)->freq = 1 - (*loc)->freq;
string tmp = (*loc)->allele2;
(*loc)->allele2 = (*loc)->allele1;
(*loc)->allele1 = tmp;
vector<bool>::iterator i1 = (*s)->one.begin();
vector<bool>::iterator i2 = (*s)->two.begin();
while ( i1 != (*s)->one.end() )
{
if ( (*i1) == (*i2) )
{
*i1 = ! (*i1);
*i2 = ! (*i2);
}
i1++;
i2++;
}
}
// Next SNP
++d;
++loc;
++l;
++s;
}
/////////////////////////////////////////////////
// Save list of any heterozygous haploid alleles
if (hetlist.size()>0)
{
printLOG(int2str( hetlist.size())
+ " heterozygous haploid genotypes; set to missing\n");
string f = par::output_file_name + ".hh";
printLOG("Writing list of heterozygous haploid genotypes to [ "
+ f + " ]\n");
ofstream REM;
REM.open(f.c_str(), ifstream::out);
for (int i=0; i<hetlist.size(); i++)
REM << hetlist[i] << "\n";
REM.close();
}
hetlist.clear();
/////////////////////////////////////////////////
// Save list of SNPs with no founders observed
if (no_founders_found_list.size()>0)
{
printLOG(int2str( no_founders_found_list.size())
+ " SNPs with no founder genotypes observed\n");
printLOG("Warning, MAF set to 0 for these SNPs (see --nonfounders)\n");
string f = par::output_file_name + ".nof";
printLOG( "Writing list of these SNPs to [ " + f + " ]\n");
ofstream NOF;
NOF.open(f.c_str(), ifstream::out);
for (int i=0; i<no_founders_found_list.size(); i++)
NOF << no_founders_found_list[i]->name << "\n";
NOF.close();
}
no_founders_found_list.clear();
//////////////////////////
// Write allele freq file
if (par::af_write)
{
if (par::include_cluster_from_file)
calcStratifiedAlleleFreqs();
else
{
ofstream FRQ;
string f = par::output_file_name + ".frq";
if (par::af_count) f += ".count";
if (par::summ_nonfounders)
printLOG("Writing allele frequencies (all individuals) to [ "
+ f + " ] \n");
else
printLOG("Writing allele frequencies (founders-only) to [ "
+ f + " ] \n");
if (par::af_count)
printLOG("Display counts rather than frequencies\n");
FRQ.open(f.c_str(), ifstream::out);
FRQ.precision(4);
FRQ << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(4) << "A1" << " "
<< setw(4) << "A2" << " ";
if (par::af_count)
FRQ << setw(6) << "C1" << " "
<< setw(6) << "C2" << " "
<< setw(6) << "G0" << "\n";
else
FRQ << setw(12) << "MAF" << " "
<< setw(8) << "NCHROBS"
<< "\n";
vector<Locus*>::iterator loc = locus.begin();
while (loc != locus.end() )
{
string a1 = (*loc)->allele1;
string a2 = (*loc)->allele2;
if (a1=="") a1="0";
if (a2=="") a2="0";
FRQ << setw(4) << (*loc)->chr << " "
<< setw(par::pp_maxsnp) << (*loc)->name << " "
<< setw(4) << a1 << " "
<< setw(4) << a2 << " ";
if (par::af_count)
{
FRQ << setw(6) << int( (*loc)->freq ) << " "
<< setw(6) << int( (*loc)->bp ) << " "
<< setw(6) << int( (*loc)->pos ) << "\n";
}
else
{
if ( (*loc)->nm > 0 )
FRQ << setw(12) << (*loc)->freq << " ";
else
FRQ << setw(12) << "NA" << " ";
FRQ << setw(8) << (*loc)->nm << "\n";
}
loc++;
}
FRQ.close();
}
// Close after we've done alle freqs,
shutdown();
}
/////////////////////////
// Write HWE statistics
if (par::HWD_test || par::HWD_report)
{
ofstream HWD;
if (par::HWD_report)
{
if (par::summ_nonfounders)
printLOG("Writing Hardy-Weinberg tests (all individuals) to [ " +
par::output_file_name + ".hwe ] \n");
else
printLOG("Writing Hardy-Weinberg tests (founders-only) to [ " +
par::output_file_name + ".hwe ] \n");
string f = par::output_file_name + ".hwe";
HWD.open(f.c_str(), ifstream::out);
HWD.precision(4);
HWD << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(8) << "TEST" << " "
<< setw(4) << "A1" << " "
<< setw(4) << "A2" << " "
<< setw(20) << "GENO" << " "
<< setw(8) << "O(HET)" << " "
<< setw(8) << "E(HET)" << " "
<< setw(12) << "P" << " "
<< "\n";
}
int cnt=0, cnt_a=0, cnt_u=0;
////////////////////////
// Consider each locus
vector<bool>::iterator d = del.begin();
vector<Locus*>::iterator loc = locus.begin();
for ( int l = 0 ; l < locus.size() ; l++ )
{
// Compute p-values for HWE test in cases, controls & all
// Only consider founders
int a11, a12, a22;
int u11, u12, u22;
int b11, b12, b22;
a11=a12=a22=0;
u11=u12=u22=0;
b11=b12=b22=0;
bool X = false, haploid = false;
if (par::chr_sex[(*loc)->chr]) X=true;
else if (par::chr_haploid[(*loc)->chr]) haploid=true;
///////////////////////////////////////////////
// Iterate over each individual, founders only
for ( int i = 0 ; i < sample.size() ; i++ )
{
Individual * person = sample[i];
///////////////////////////////////////////////
// Only consider founders, & diploid genotypes
if ( par::summ_nonfounders || person->founder )
if ( ! ( haploid || ( X && person->sex ) ) )
{
bool s1 = SNP[l]->one[i];
bool s2 = SNP[l]->two[i];
// Consider everybody, irrespective of phenotype
// (QT, C/C or missing)
if (!s1)
{
if (!s2) b11++; // 00 = hom(11)
else b12++; // 01 = het(12)
}
else if ( s2 ) b22++; // 11 = hom(22)
if (par::bt) // for binary trait, separately for cases/controls
{
if (person->phenotype == 1)
{
if (!s1)
{
if (!s2) u11++; // 00 = hom(11)
else u12++; // 01 = het(12)
}
else if ( s2 ) u22++; // 11 = hom(22)
}
else if (person->phenotype == 2)
{
if (!s1)
{
if (!s2) a11++; // 00 = hom(11)
else a12++; // 01 = het(12)
}
else if ( s2 ) a22++; // 11 = hom(22)
}
}
}
// Next individual
}
// Allele frequencies
double afreq = 0, ufreq = 0, freq = 0;
bool include_cases = true;
bool include_controls = true;
if (par::qt)
freq = ( b11 + (double)b12/2.0 ) / (double)( b11+b12+b22 );
else
{
afreq = ( a11 + (double)a12/2.0 ) / (double)( a11+a12+a22 );
ufreq = ( u11 + (double)u12/2.0 ) / (double)( u11+u12+u22 );
freq = ( b11 + (double)b12/2.0 ) / (double)( b11+b12+b22 );
if ( a11+a12+a22 == 0 ) include_cases = false;
if ( u11+u12+u22 == 0 ) include_controls = false;
}
if (par::qt)
{
double p;
if (par::HWD_standard)
{
double tot = b11 + b12 + b22;
double exp_11 = freq * freq * tot;
double exp_12 = 2 * freq * (1-freq) * tot;
double exp_22 = (1-freq) * (1-freq) * tot;
double chisq = ( (b11-exp_11)*(b11-exp_11) ) / exp_11
+ ( (b12-exp_12)*(b12-exp_12) ) / exp_12
+ ( (b22-exp_22)*(b22-exp_22) ) / exp_22 ;
p = chiprobP(chisq,1);
}
else
p = SNPHWE( b12, b11, b22 );
if (par::HWD_report)
{
HWD << setw(4) << (*loc)->chr << " "
<< setw(par::pp_maxsnp) << (*loc)->name << " "
<< setw(8) << "ALL(QT)" << " "
<< setw(4) << (*loc)->allele1 << " "
<< setw(4) << (*loc)->allele2 << " "
<< setw(20) << (int2str(b11)+
"/"+int2str(b12)+
"/"+int2str(b22)) << " "
<< setw(8) << (double)b12/(double)(b11+b12+b22) << " "
<< setw(8) << 2 * freq * (1-freq) << " ";
if ( realnum(p) )
HWD << setw(12) << p << "\n";
else
HWD << setw(12) << "NA" << "\n";
}
if ( p <= par::HWD_limit && p > -1 )
{
cnt++;
*d = true;
}
}
else
{
// For case/control data
double p, p_a, p_u;
if (par::HWD_standard)
{
double exp_a11 = afreq * afreq * (a11+a12+a22);
double exp_a12 = 2 * afreq * (1-afreq) * (a11+a12+a22);
double exp_a22 = (1-afreq) * (1-afreq) * (a11+a12+a22);
double exp_u11 = ufreq * ufreq * (u11+u12+u22);
double exp_u12 = 2 * ufreq * (1-ufreq) * (u11+u12+u22);
double exp_u22 = (1-ufreq) * (1-ufreq) * (u11+u12+u22);
double exp_11 = freq * freq * (b11+b12+b22);
double exp_12 = 2 * freq * (1-freq) * (b11+b12+b22);
double exp_22 = (1-freq) * (1-freq) * (b11+b12+b22);
double chisq_a = ( (a11-exp_a11)*(a11-exp_a11) ) / exp_a11
+ ( (a12-exp_a12)*(a12-exp_a12) ) / exp_a12
+ ( (a22-exp_a22)*(a22-exp_a22) ) / exp_a22 ;
double chisq_u = ( (u11-exp_u11)*(u11-exp_u11) ) / exp_u11
+ ( (u12-exp_u12)*(u12-exp_u12) ) / exp_u12
+ ( (u22-exp_u22)*(u22-exp_u22) ) / exp_u22 ;
double chisq = ( (b11-exp_11)*(b11-exp_11) ) / exp_11
+ ( (b12-exp_12)*(b12-exp_12) ) / exp_12
+ ( (b22-exp_22)*(b22-exp_22) ) / exp_22 ;
p = chiprobP(chisq,1);
p_a = chiprobP(chisq_a,1);
p_u = chiprobP(chisq_u,1);
}
else
{
p = SNPHWE( b12, b11, b22 );
p_a = SNPHWE( a12, a11, a22 );
p_u = SNPHWE( u12, u11, u22 );
}
if (par::HWD_report)
{
HWD << setw(4) << (*loc)->chr << " "
<< setw(par::pp_maxsnp) << (*loc)->name << " "
<< setw(8) << "ALL" << " "
<< setw(4) << (*loc)->allele1 << " "
<< setw(4) << (*loc)->allele2 << " "
<< setw(20)
<< int2str(b11)+"/"+int2str(b12)+"/"+int2str(b22) << " "
<< setw(8) << (double)b12/(double)(b11+b12+b22) << " "
<< setw(8) << 2 * freq * (1-freq) << " ";
if ( p > -1 )
HWD << setw(12) << p << "\n";
else
HWD << setw(12) << "NA" << "\n";
HWD << setw(4) << (*loc)->chr << " "
<< setw(par::pp_maxsnp) << (*loc)->name << " "
<< setw(8) << "AFF" << " "
<< setw(4) << (*loc)->allele1 << " "
<< setw(4) << (*loc)->allele2 << " "
<< setw(20)
<< int2str(a11)+"/"+int2str(a12)+"/"+int2str(a22) << " "
<< setw(8) << (double)a12/(double)(a11+a12+a22) << " "
<< setw(8) << 2 * afreq * (1-afreq) << " ";
if (include_cases && p_a > -1 )
HWD << setw(12) << p_a << "\n";
else
HWD << setw(12) << "NA" << "\n";
HWD << setw(4) << (*loc)->chr << " "
<< setw(par::pp_maxsnp) << (*loc)->name << " "
<< setw(8) << "UNAFF" << " "
<< setw(4) << (*loc)->allele1 << " "
<< setw(4) << (*loc)->allele2 << " "
<< setw(20)
<< int2str(u11)+"/"+int2str(u12)+"/"+int2str(u22) << " "
<< setw(8) << (double)u12/(double)(u11+u12+u22) << " "
<< setw(8) << 2 * ufreq * (1-ufreq) << " ";
if (include_controls && p_u > -1 )
HWD << setw(12) << p_u << "\n";
else
HWD << setw(12) << "NA" << "\n";
}
// Increase counts: in cases
if ( include_cases && p_a < par::HWD_limit && p_a > -1 ) cnt_a++;
// Controls (and, if possible, exclude on this value)
if ( include_controls &&
p_u < par::HWD_limit && p_u > -1 )
{
cnt_u++;
if ( ! par::HWD_filter_on_all )
{
*d = true;
cnt++;
}
}
// In total sample, and if needed, exclude here
if ( p < par::HWD_limit && p>-1 )
{
if ( par::HWD_filter_on_all || ! include_controls )
{
*d = true;
cnt++;
}
}
}
// next locus
++loc;
++d;
}
// Finish the report...
if (par::HWD_report)
HWD.close();
// ...or finish pruning
printLOG( int2str(cnt) +
" markers to be excluded based on HWE test ( p <= " +
dbl2str(par::HWD_limit) + " )\n");
if (par::bt)
{
printLOG("\t" + int2str(cnt_a) + " markers failed HWE test in cases\n");
printLOG("\t" + int2str(cnt_u) + " markers failed HWE test in controls\n");
}
}
///////////////////////////////////////////////////
// Summary statistics for genotyping/missing rates
if (par::report_missing)
{
///////////////////////////////////////////
// Report by genotyping rate by individual
// possibly allowing for obligatory missingness
printLOG( "Writing individual missingness information to [ " +
par::output_file_name + ".imiss ] \n");
ofstream MIS;
string f = par::output_file_name + ".imiss";
MIS.open(f.c_str(), ifstream::out);
MIS.precision(4);
MIS << setw(par::pp_maxfid) << "FID" << " "
<< setw(par::pp_maxiid) << "IID" << " "
<< setw(10) << "MISS_PHENO" << " "
<< setw(8) << "N_MISS" << " ";
MIS << setw(8) << "N_GENO" << " ";
MIS << setw(8) << "F_MISS" << "\n";
for (int i=0; i<n; i++)
{
MIS << setw(par::pp_maxfid) << sample[i]->fid << " "
<< setw(par::pp_maxiid) << sample[i]->iid << " ";
if (sample[i]->missing) MIS << setw(10) << "Y" << " ";
else MIS << setw(10) << "N" << " " ;
// Sum missingness over all SNPs
int m=0; // Missing SNPs
int nsnps=0; // All non-obligatory missing SNPs
bool female = ! sample[i]->sex;
if ( ! par::oblig_missing )
{
for (int l=0; l<locus.size();l++)
{
// Skip female Y chromosomes
if ( female && par::chr_Y[locus[l]->chr] )
continue;
if ( MISSING(i,l) ) ++m;
++nsnps;
}
}
else // ... allow oblig missing values
{
for (int l=0; l<locus.size();l++)
{
// Skip female Y chromosomes
if ( female && par::chr_Y[locus[l]->chr] )
continue;
if ( ! obligMissing(i,l) )
{
if ( MISSING(i,l) )
++m;
++nsnps;
}
}
}
MIS << setw(8) << m << " ";
MIS << setw(8) << nsnps << " ";
MIS << setw(8) << (double)m/(double)nsnps << "\n";
}
MIS.close();
///////////////////////////////////////////
// Report by genotyping rate by locus
// possibly allowing for sample strata
// possibly allowing for obligatory missingness
printLOG("Writing locus missingness information to [ " +
par::output_file_name +".lmiss ] \n");
f = par::output_file_name + ".lmiss";
MIS.open(f.c_str(), ifstream::out);
MIS.clear();
MIS.precision(4);
MIS << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " ";
if (par::include_cluster_from_file)
MIS << setw(10) << "CLST" << " ";
MIS << setw(8) << "N_MISS" << " ";
MIS << setw(8) << "N_GENO" << " ";
if (par::include_cluster_from_file)
MIS << setw(8) << "N_CLST" << " ";
MIS << setw(8) << "F_MISS" << "\n";
for (int l=0; l<locus.size(); l++)
{
Locus * loc = locus[l];
bool chrY = par::chr_Y[locus[l]->chr];
// nk==1 for basic missingness (i.e. not stratified by
// cluster)
for (int k=0; k<nk; k++)
{
MIS << setw(4) << loc->chr << " "
<< setw(par::pp_maxsnp) << loc->name << " ";
if (par::include_cluster_from_file)
MIS << setw(10) << kname[k] << " ";
int m=0; // Number of missing genotypes
int c=0; // Number of people in cluster
int nsnps=0; // Number of actual genotypes in cluster
for ( int i=0; i<sample.size(); i++)
{
// Skip female Y chromosome calls
if ( chrY && ! sample[i]->sex )
continue;
if (par::include_cluster_from_file)
{
if ( sample[i]->sol == k )
{
if ( ( ! par::oblig_missing ) ||
( ! obligMissing(i,l) ) )
{
if ( MISSING(i,l) ) ++m;
++nsnps;
}
++c;
}
}
else // ... ignore cluster strata
{
if ( ( ! par::oblig_missing ) ||
( ! obligMissing(i,l) ) )
{
if ( MISSING(i,l) ) ++m;
++nsnps;
}
}
// Next individual
}
MIS << setw(8) << m << " ";
if (par::include_cluster_from_file)
MIS << setw(8) << c << " ";
MIS << setw(8) << nsnps << " ";
MIS << setw(8) << (double)m / (double)nsnps << "\n";
}
// Next SNP
}
MIS.close();
}
/////////////////////////////////
// Remove rare SNPs
loc = locus.begin();
d = del.begin();
while ( loc != locus.end() )
{
// Note epsilon correction for MAF, due to floating point
// issues: only apply to the lower MAF range
if ( (*loc)->freq < 0 ||
(*loc)->freq + par::epsilon < par::min_af ||
(*loc)->freq > par::max_af )
{
*d = true;
exc_maf++;
}
d++;
loc++;
}
/////////////////////////////////////////
// Remove SNPs based on thresholds
if ( locus.size() > 0 )
printLOG("Total genotyping rate in remaining individuals is "
+ dbl2str(total_genotyping/(double)locus.size())+"\n");
printLOG(int2str(exc_miss)+" SNPs failed missingness test ( GENO > "
+dbl2str(par::MAX_GENO_MISSING)+" )\n");
printLOG(int2str(exc_maf)+" SNPs failed frequency test ( MAF < "+dbl2str(par::min_af));
if (par::max_af < 0.5 ) printLOG(" or MAF > " + dbl2str(par::max_af));
printLOG(" )\n");
int tmp = deleteSNPs(del);
//////////////////////////////////////////
// Need to make back to individual major?
if ( ! original_SNP_major )
SNP2Ind();
return;
}
void Plink::thinSNPs()
{
if ( par::thin_param <= 0 || par::thin_param >= 1 )
error("Parameter for --thin must be 0<x<1");
printLOG("Thinning SNP list, per-SNP probability of keeping is " + dbl2str( par::thin_param ) + "\n");
int x = 0;
set<Locus*> toKeep;
for (int l=0;l<nl_all;l++)
if ( CRandom::rand() < par::thin_param )
toKeep.insert(locus[l]);
int rem = keepSNPs( toKeep );
printLOG("Removed " + int2str(rem) + " SNPs from thinning\n");
}
|