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
|
//////////////////////////////////////////////////////////////////
// //
// 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 <sstream>
#include <cmath>
#include <vector>
#include <map>
#include <cassert>
#include "plink.h"
#include "options.h"
#include "helper.h"
#include "genogroup.h"
#include "phase.h"
#include "haplowindow.h"
extern ofstream LOG;
using namespace std;
void HaploPhase::queryGenotype(int s)
{
// Map within-region SNP coding back to within-genome SNP coding
int l = S[s];
// Right now, let's elect not to handle non-autosomal SNPs
if ( par::chr_haploid[ P.locus[l]->chr ] ||
par::chr_sex[ P.locus[l]->chr ] )
error("--proxy-error not yet set up for non-autosomal SNPs");
//////////////////////////////////////////////////////
// Calculate sample genotype frequencies for test SNP
int g0 = 0, g1 = 0, g2 = 0;
for (int i = 0; i<P.n; i++)
{
bool s1 = par::SNP_major ? P.SNP[l]->one[i] : P.sample[i]->one[l];
bool s2 = par::SNP_major ? P.SNP[l]->two[i] : P.sample[i]->two[l];
if( s1 )
{
if ( s2 )
++g0;
}
else
{
if ( s2 )
++g1;
else
++g2;
}
}
vector_t geno_freq(3);
int total = g0 + g1 + g2;
geno_freq[0] = (double)g0 / (double)total;
geno_freq[1] = (double)g1 / (double)total;
geno_freq[2] = (double)g2 / (double)total;
//////////////////////////////////////////////////////
// Check each genotype of each individual over region
for (int i=0; i<P.n; i++)
{
if (include[i])
{
bool s1 = par::SNP_major ? P.SNP[l]->one[i] : P.sample[i]->one[l];
bool s2 = par::SNP_major ? P.SNP[l]->two[i] : P.sample[i]->two[l];
int g;
if ( s1 )
{
if ( s2 )
g = 0;
else
g = -9;
}
else
{
if ( s2 )
g = 1;
else
g = 2;
}
///////////////////////////////////////////////////////////
// Determine most likely genotype given flanking haplotypes
queryThisGenotype(i, s, g, geno_freq);
}
}
}
void HaploPhase::queryThisGenotype(int i, int s, int g, vector_t & geno_freq )
{
// Do not do anything if the actual genotype is missing -- i.e. the
// focus here is on correcting genotyping error rather than
// imputation
if ( g<0 )
return;
// For a given individual, for a given phased region and set of
// sample haplotype frequencies, determine a) the possible set of
// phased haplotypes consistent with region if the test SNP were in
// fact missing, b) the relative probability that the observed
// genotype is the true genotype given the new phases.
////////////////////////////////////////////////////////////////
// These are the new possible phases, if the test genotype were
// missing
vector<int> new_hap1;
vector<int> new_hap2;
vector_t newpp;
double psum = 0;
int z2 = 0;
// Consider
// AAA-C-GGT / ACA-C-GGT
// Becomes up to three possible states:
// AAA-C-GGT / ACA-C-GGT
// AAA-C-GGT / ACA-T-GGT
// AAA-T-GGT / ACA-T-GGT
for (int z=0; z<hap1[i].size(); z++)
{
vector<int> posshap1;
vector<int> posshap2;
vector<bool> h1 = hap[hap1[i][z]];
vector<bool> h2 = hap[hap2[i][z]];
vector<bool> h1_flip = hap[hap1[i][z]];
vector<bool> h2_flip = hap[hap2[i][z]];
// flip bit at SNP position s
h1_flip[s] = !h1[s];
h2_flip[s] = !h2[s];
// Do we observed these possible haplotypes in the sample as a
// whole? If not, no need to consider, as the posterior
// probability will be 0.
if( hapmapb.find(h1) != hapmapb.end() )
posshap1.push_back(hapmapb[h1]);
if( hapmapb.find(h2) != hapmapb.end() )
posshap2.push_back(hapmapb[h2]);
if( hapmapb.find(h1_flip) != hapmapb.end() )
posshap1.push_back(hapmapb[h1_flip]);
if( hapmapb.find(h2_flip) != hapmapb.end() )
posshap2.push_back(hapmapb[h2_flip]);
// get new probabilities for each possible new phasing
for( int a = 0; a < posshap1.size(); a++ )
for( int b = 0; b < posshap2.size(); b++ )
{
newpp.push_back(f[posshap1[a]] * f[posshap2[b]]);
new_hap1.push_back(posshap1[a]);
new_hap2.push_back(posshap2[b]);
// We are already considering both explicitly
// if (posshap1[a] != posshap2[b])
// newpp[z2] *= 2;
psum += newpp[z2];
z2++;
}
}
// adjust to sum to 1
for (int z=0; z<z2; z++){
newpp[z] /= psum;
}
// Display old and new here..
int l = S[s];
// Obtain the genotype of individual i at locus l (regional SNP s)
// and compute sum of haplotype probabilities for each genotype
////////////////////////////////////
// Consider each new possible phase
double gh[3];
gh[0] = gh[1] = gh[2] = 0;
for (int z = 0; z < new_hap1.size(); z++)
{
// Implied genotype given this haplotype?
// (remembering flipped allele coding)
bool s1 = ! hap[ new_hap1[z]][s];
bool s2 = ! hap[ new_hap2[z]][s];
if ( s1 )
{
if ( s2 )
gh[0] += newpp[z];
else
gh[1] += newpp[z];
}
else
{
if ( s2 )
gh[1] += newpp[z];
else
gh[2] += newpp[z];
}
}
//////////////////////////////////////////
// Compare these to the observed genotype
// Threshold is half the population frequency?
// One-tenth of population frequency?
double threshold = 0.1 * geno_freq[g];
double impute_threshold = 0.9;
if ( gh[g] < threshold )
{
HTEST << setw(par::pp_maxsnp) << P.locus[S[s]]->name << " "
<< setw(par::pp_maxfid) << P.sample[i]->fid << " "
<< setw(par::pp_maxiid) << P.sample[i]->iid << " "
<< setw(6) << genotype(P, i, l) << " "
<< setw(8) << geno_freq[g] << " "
<< setw(8) << gh[0] << " "
<< setw(8) << gh[1] << " "
<< setw(8) << gh[2] << " ";
int ng = -9;
for ( int j=0; j<=2; j++)
if ( gh[j] > impute_threshold )
ng = j;
if ( ng==0 )
HTEST << setw(6) << P.locus[S[s]]->allele1+"/"
+P.locus[S[s]]->allele1 << "\n";
else if ( ng==1 )
HTEST << setw(6) << P.locus[S[s]]->allele1+"/"
+P.locus[S[s]]->allele2 << "\n";
else if ( ng==2 )
HTEST << setw(6) << P.locus[S[s]]->allele2+"/"
+P.locus[S[s]]->allele2 << "\n";
else
HTEST << setw(6) << par::missing_genotype+"/"
+par::missing_genotype << "\n";
}
////////////////////////////////////////////////////////////
// Only report dodgy looking genotypes even in verbose mode
if ( par::proxy_full_report && gh[g] < threshold )
{
HTEST << "Individual " << P.sample[i]->fid << " "
<< P.sample[i]->iid << " ; locus "
<< P.locus[S[s]]->name << "\n";
HTEST << "Observed genotype is "
<< genotype(P, i, S[s])
<< " g = " << g << "\n";
HTEST << "Prior genotypes probabilities = "
<< geno_freq[0] << " "
<< geno_freq[1] << " "
<< geno_freq[2] << endl;
HTEST << "Posterior genotypes probabilities = "
<< gh[0] << " "
<< gh[1] << " "
<< gh[2] << endl;
HTEST << "\nOriginal phases\n";
for (int z = 0; z < hap1[i].size(); z++)
{
HTEST << setw(par::pp_maxfid) << P.sample[i]->fid<< " "
<< setw(par::pp_maxiid) << P.sample[i]->iid<< " "
<< setw(4) << z << " "
<< setw(10) << haplotypeName(hap1[i][z]) << " "
<< setw(10) << haplotypeName(hap2[i][z]) << "\t";
if (ambig[i])
{
HTEST << setw(12) << pp[i][z]<< " ";
int max_z = 0;
for (int z2=0; z2<hap1[i].size(); z2++)
max_z = pp[i][z2] > pp[i][max_z] ? z2 : max_z ;
if (max_z == z)
HTEST << setw(6) << 1<< " "<< " ";
else
HTEST << setw(6) << 0<< " "<< " ";
}
else
HTEST << setw(12) << 1<< " "<< setw(6) << 1<< " "<< " ";
// Genotypes
for (int j=0; j<ns; j++)
HTEST << genotype(P, i, S[j]) << " ";
HTEST << "\n";
HTEST << "\n";
}
HTEST << "New possible phases, if reference were missing\n";
for (int z = 0; z < new_hap1.size(); z++)
{
HTEST << setw(par::pp_maxfid) << P.sample[i]->fid<< " "
<< setw(par::pp_maxiid) << P.sample[i]->iid<< " "
<< setw(4) << z << " "
<< setw(10) << haplotypeName(new_hap1[z]) << " "
<< setw(10) << haplotypeName(new_hap2[z]) << "\t";
HTEST << setw(12) << newpp[z]<< " ";
int max_z = 0;
for (int z2=0; z2<new_hap1.size(); z2++)
max_z = newpp[z2] > newpp[max_z] ? z2 : max_z ;
if (max_z == z)
HTEST << setw(6) << 1<< " "<< " ";
else
HTEST << setw(6) << 0<< " "<< " ";
// Genotypes
for (int j=0; j<ns; j++)
{
if ( j==s )
HTEST << par::missing_genotype << "/"
<< par::missing_genotype << " ";
else
HTEST << genotype(P, i, S[j]) << " ";
}
HTEST << "\n";
HTEST << "\n";
}
HTEST << "----------------------------------------------------------------------------\n";
HTEST << endl;
}
}
|