VCFGenotypeExtractor.cpp 14.7 KB
Newer Older
zhanxw's avatar
zhanxw committed
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
#include "VCFGenotypeExtractor.h"

#include "GenotypeCounter.h"
#include "Result.h"

#include "base/Argument.h"
#include "base/Logger.h"
#include "libVcf/VCFUtil.h"
#include "libsrc/MathMatrix.h"
#include "libsrc/MathVector.h"

DECLARE_BOOL_PARAMETER(outputID);

extern Logger* logger;

VCFGenotypeExtractor::VCFGenotypeExtractor(const std::string& fn)
    : GenotypeExtractor(fn), vin(NULL), altAlleleToParse(-1) {
  this->vin = new VCFExtractor(fn.c_str());
}

VCFGenotypeExtractor::~VCFGenotypeExtractor() {
  if (this->vin) {
    delete this->vin;
    this->vin = NULL;
  }
}

int VCFGenotypeExtractor::extractMultipleGenotype(Matrix* g) {
  assert(g);
  assert(g->rows >= 0 && g->cols >= 0);
  g->Dimension(0, 0);
  int row = 0;
  this->genotype.clear();
  this->altAlleleToParse = -1;

  while (true) {
    if (this->altAlleleToParse <= 0) {
      if (!this->vin->readRecord()) {
        // reached the end
        break;
      } else {
        const char* alt = this->vin->getVCFRecord().getAlt();
        if (multiAllelicMode) {
          parseAltAllele(alt);
          this->altAlleleToParse = altAllele.size();
        } else {
          this->altAllele.resize(1);
          this->altAllele[0] = alt;
          this->altAlleleToParse = 1;
        }
      }
    }
    assert(this->altAlleleToParse > 0);
    // parse alt allele one at a time
    VCFRecord& r = this->vin->getVCFRecord();
    VCFPeople& people = r.getPeople();
    VCFIndividual* indv;

    this->sampleSize = people.size();
    row++;
    this->variantName.resize(row);
    this->counter.resize(row);
    this->counter.back().reset();
    this->hemiRegion.resize(row);

    // get GT index and cannot assume it is a constant across variants
    int genoIdx;
    const bool useDosage = (!this->dosageTag.empty());
    if (useDosage) {
      genoIdx = r.getFormatIndex(dosageTag.c_str());
    } else {
      genoIdx = r.getFormatIndex("GT");
    }
    if (useDosage && multiAllelicMode) {
      logger->error(
          "Unsupported scenario: multiple mode and use dosage as "
          "genotypes! - we will only use dosage");
      this->altAlleleToParse = 1;
    }
    const int GDidx = r.getFormatIndex("GD");
    const int GQidx = r.getFormatIndex("GQ");
    assert(this->parRegion);
    const bool isHemiRegion =
        this->parRegion->isHemiRegion(r.getChrom(), r.getPos());
    // e.g.: Loop each (selected) people in the same order as in the VCF
    double geno;
    const int altAlleleGT = this->altAllele.size() - this->altAlleleToParse + 1;
    for (int i = 0; i < sampleSize; i++) {
      indv = people[i];
      if (multiAllelicMode) {
        geno =
            getGenotypeForAltAllele(*indv, useDosage, isHemiRegion, (*sex)[i],
                                    genoIdx, GDidx, GQidx, altAlleleGT);
      } else {
        geno = getGenotype(*indv, useDosage, isHemiRegion, (*sex)[i], genoIdx,
                           GDidx, GQidx);
      }
      this->genotype.push_back(geno);
      this->counter.back().add(geno);
    }  // end for i

    // check frequency cutoffs
    const double maf = counter.back().getMAF();
    if ((this->freqMin > 0. && this->freqMin > maf) ||
        (this->freqMax > 0. && this->freqMax < maf)) {
      // undo loaded contents
      row--;
      this->variantName.resize(row);
      this->counter.resize(row);
      this->hemiRegion.resize(row);
      this->genotype.resize(this->genotype.size() - this->sampleSize);
      this->altAlleleToParse--;
      continue;
    }

    this->variantName.back() = r.getChrom();
    this->variantName.back() += ":";
    this->variantName.back() += r.getPosStr();
    if (multiAllelicMode) {
      this->variantName.back() += r.getRef();
      this->variantName.back() += "/";
      this->variantName.back() +=
          altAllele[altAllele.size() - this->altAlleleToParse];
    }
    this->hemiRegion.back() = (isHemiRegion);

    this->altAlleleToParse--;
  }  // end while (this->vin->readRecord())

  // now transpose (marker by people -> people by marker)
  if (row > 0) {
    assert((int)genotype.size() == this->sampleSize * row);
    assign(this->genotype, sampleSize, row, g);
    for (int i = 0; i < row; ++i) {
      g->SetColumnLabel(i, variantName[i].c_str());
    }
  }
  return SUCCEED;
}  // end VCFGenotypeExtractor

int VCFGenotypeExtractor::extractSingleGenotype(Matrix* g, Result* b) {
  this->genotype.clear();
  Result& buf = *b;

  if (this->altAlleleToParse <= 0) {
    if (!this->vin->readRecord()) {
      return FILE_END;
    } else {
      const char* alt = this->vin->getVCFRecord().getAlt();
      if (multiAllelicMode) {
        parseAltAllele(alt);
        this->altAlleleToParse = altAllele.size();
      } else {
        this->altAllele.resize(1);
        this->altAllele[0] = alt;
        this->altAlleleToParse = 1;
      }
    }
  }
  assert(this->altAlleleToParse >= 0);
  VCFRecord& r = this->vin->getVCFRecord();
  VCFPeople& people = r.getPeople();
  VCFIndividual* indv;

  buf.updateValue("CHROM", r.getChrom());
  buf.updateValue("POS", r.getPosStr());
  if (FLAG_outputID) {
    buf.updateValue("ID", r.getID());
  }
  buf.updateValue("REF", r.getRef());
  buf.updateValue("ALT", altAllele[altAllele.size() - this->altAlleleToParse]);

  // genotype.Dimension(people.size(), 1);
  this->sampleSize = people.size();
  this->variantName.resize(1);
  this->counter.resize(1);
  this->counter.back().reset();
  this->hemiRegion.resize(1);

  // get GT index. if you are sure the index will not change, call this
  // function only once!
  const bool useDosage = (!this->dosageTag.empty());
  int genoIdx;
  if (useDosage) {
    genoIdx = r.getFormatIndex(dosageTag.c_str());
  } else {
    genoIdx = r.getFormatIndex("GT");
  }
  const int GDidx = r.getFormatIndex("GD");
  const int GQidx = r.getFormatIndex("GQ");
  bool isHemiRegion = this->parRegion->isHemiRegion(r.getChrom(), r.getPos());
  // e.g.: Loop each (selected) people in the same order as in the VCF
  double geno;
  const int altAlleleGT = this->altAllele.size() - this->altAlleleToParse + 1;
  for (int i = 0; i < sampleSize; i++) {
    indv = people[i];
    if (multiAllelicMode) {
      geno = getGenotypeForAltAllele(*indv, useDosage, isHemiRegion, (*sex)[i],
                                     genoIdx, GDidx, GQidx, altAlleleGT);
    } else {
      geno = getGenotype(*indv, useDosage, isHemiRegion, (*sex)[i], genoIdx,
                         GDidx, GQidx);
    }
    genotype.push_back(geno);
    counter.back().add(geno);
  }

  // check frequency cutoffs
  const double maf = counter[0].getMAF();
  if ((this->freqMin > 0. && this->freqMin > maf) ||
      (this->freqMax > 0. && this->freqMax < maf)) {
    --this->altAlleleToParse;
    return FAIL_FILTER;
  }

  variantName.back() = r.getChrom();
  variantName.back() += ':';
  variantName.back() += r.getPosStr();
  hemiRegion.back() = isHemiRegion;

  assert((int)genotype.size() == sampleSize);
  assign(genotype, sampleSize, 1, g);
  g->SetColumnLabel(0, variantName.back().c_str());

  --this->altAlleleToParse;
  return SUCCEED;
}  // end extractSingleGenotype()

int loadMarkerFromVCF(const std::string& fileName, const std::string& marker,
                      std::vector<std::string>* rowLabel, Matrix* genotype) {
  if (!rowLabel || !genotype) {
    // invalid parameter
    return -1;
  }
  Matrix& m = *genotype;
  int col = 0;

  VCFInputFile vin(fileName);
  vin.setRangeList(marker);

  while (vin.readRecord()) {
    VCFRecord& r = vin.getVCFRecord();
    VCFPeople& people = r.getPeople();
    VCFIndividual* indv;

    m.Dimension(people.size(), col + 1);

    int GTidx = r.getFormatIndex("GT");
    for (int i = 0; i < (int)people.size(); i++) {
      indv = people[i];
      // get GT index. if you are sure the index will not change,
      // call this function only once!
      if (GTidx >= 0) {
        // printf("%s ", indv->justGet(0).toStr());  // [0] meaning the first
        // field of each individual
        m[i][col] = indv->justGet(GTidx).getGenotype();
      } else {
        logger->error("Cannot find GT field!");
        return -1;
      }
    }
    if (col == 0) {
      // set-up names
      rowLabel->resize(people.size());
      for (size_t i = 0; i < people.size(); ++i) {
        (*rowLabel)[i] = people[i]->getName();
      }
    }
    std::string colLabel = r.getChrom();
    colLabel += ":";
    colLabel += r.getPosStr();
    m.SetColumnLabel(col, colLabel.c_str());
    ++col;
  }

  return 0;
}

bool VCFGenotypeExtractor::setSiteFreqMin(const double f) {
  if (f < 0.0 || f > 1.0) {
    return false;
  }
  this->freqMin = f - 1e-10;  // allow rounding error
  return true;
}
bool VCFGenotypeExtractor::setSiteFreqMax(const double f) {
  if (f < 0.0 || f > 1.0) {
    return false;
  }
  this->freqMax = f + 1e-10;  // allow rounding error
  return true;
}

void VCFGenotypeExtractor::setSiteDepthMin(int d) {
  this->vin->setSiteDepthMin(d);
}
void VCFGenotypeExtractor::setSiteDepthMax(int d) {
  this->vin->setSiteDepthMax(d);
}

// @return true if GD is valid
// if GD is missing, we will take GD = 0
bool VCFGenotypeExtractor::checkGD(VCFIndividual& indv, int gdIdx) {
  if (!needGD) return true;
  int gd = indv.justGet(gdIdx).toInt();
  if (this->GDmin > 0 && gd < this->GDmin) return false;
  if (this->GDmax > 0 && gd > this->GDmax) return false;
  return true;
}
bool VCFGenotypeExtractor::checkGQ(VCFIndividual& indv, int gqIdx) {
  if (!needGQ) return true;
  int gq = indv.justGet(gqIdx).toInt();
  if (this->GQmin > 0 && gq < this->GQmin) return false;
  if (this->GQmax > 0 && gq > this->GQmax) return false;
  return true;
}
void VCFGenotypeExtractor::setGDmin(int m) {
  this->needGD = true;
  this->GDmin = m;
}
void VCFGenotypeExtractor::setGDmax(int m) {
  this->needGD = true;
  this->GDmax = m;
}
void VCFGenotypeExtractor::setGQmin(int m) {
  this->needGQ = true;
  this->GQmin = m;
}
void VCFGenotypeExtractor::setGQmax(int m) {
  this->needGQ = true;
  this->GQmax = m;
}

void VCFGenotypeExtractor::setSiteFile(const std::string& fn) {
  this->vin->setSiteFile(fn);
}

void VCFGenotypeExtractor::setSiteQualMin(int q) {
  this->vin->setSiteQualMin(q);
}
void VCFGenotypeExtractor::setSiteMACMin(int n) { this->vin->setSiteMACMin(n); }
int VCFGenotypeExtractor::setAnnoType(const std::string& s) {
  return this->vin->setAnnoType(s.c_str());
}

void VCFGenotypeExtractor::setRange(const RangeList& l) {
  this->vin->setRange(l);
}
void VCFGenotypeExtractor::setRangeList(const std::string& l) {
  this->vin->setRangeList(l);
}
void VCFGenotypeExtractor::setRangeFile(const std::string& fn) {
  this->vin->setRangeFile(fn.c_str());
}
void VCFGenotypeExtractor::includePeople(const std::string& v) {
  this->vin->includePeople(v.c_str());
}
void VCFGenotypeExtractor::includePeople(const std::vector<std::string>& v) {
  this->vin->includePeople(v);
}
void VCFGenotypeExtractor::includePeopleFromFile(const std::string& fn) {
  this->vin->includePeopleFromFile(fn.c_str());
}
void VCFGenotypeExtractor::excludePeople(const std::string& v) {
  this->vin->excludePeople(v.c_str());
}
void VCFGenotypeExtractor::excludePeople(const std::vector<std::string>& v) {
  this->vin->excludePeople(v);
}
void VCFGenotypeExtractor::excludePeopleFromFile(const std::string& fn) {
  this->vin->excludePeopleFromFile(fn.c_str());
}
void VCFGenotypeExtractor::excludePeople(const std::vector<std::string>& sample,
                                         const std::vector<int>& index) {
  for (size_t i = 0; i < index.size(); ++i) {
    this->excludePeople(sample[i]);
  }
}

void VCFGenotypeExtractor::excludeAllPeople() { this->vin->excludeAllPeople(); }
void VCFGenotypeExtractor::enableAutoMerge() { this->vin->enableAutoMerge(); }
void VCFGenotypeExtractor::getPeopleName(std::vector<std::string>* p) {
  return this->vin->getVCFHeader()->getPeopleName(p);
}

void VCFGenotypeExtractor::getIncludedPeopleName(
    std::vector<std::string>* p) const {
  this->vin->getIncludedPeopleName(p);
  return;
}

void VCFGenotypeExtractor::parseAltAllele(const char* s) {
  stringTokenize(s, ",", &altAllele);
}

double VCFGenotypeExtractor::getGenotype(VCFIndividual& indv,
                                         const bool useDosage,
                                         const bool hemiRegion, const int sex,
                                         const int genoIdx, const int GDidx,
                                         const int GQidx) {
  double ret;
  if (genoIdx >= 0) {
    if (useDosage) {
      if (!hemiRegion) {
        ret = (indv.justGet(genoIdx).toDouble());
      } else {
        // for male hemi region, imputated dosage is usually between 0 and 1
        // need to multiply by 2.0
        if (sex == PLINK_MALE) {
          ret = (indv.justGet(genoIdx).toDouble() * 2.0);
        } else {
          ret = (indv.justGet(genoIdx).toDouble());
        }
      }
    } else {  // use hard-coded genotypes
      if (!hemiRegion) {
        ret = (indv.justGet(genoIdx).getGenotype());
      } else {
        if (sex == PLINK_MALE) {
          ret = (indv.justGet(genoIdx).getMaleNonParGenotype02());
        } else if (sex == PLINK_FEMALE) {
          ret = (indv.justGet(genoIdx).getGenotype());
        } else {
          ret = (MISSING_GENOTYPE);
        }
      }
    }
    if (!checkGD(indv, GDidx) || !checkGQ(indv, GQidx)) {
      return MISSING_GENOTYPE;
    } else {
      return ret;
    }
  } else {
    logger->error("Cannot find %s field!",
                  this->dosageTag.empty() ? "GT" : dosageTag.c_str());
    return MISSING_GENOTYPE;
  }
}

double VCFGenotypeExtractor::getGenotypeForAltAllele(
    VCFIndividual& indv, const bool useDosage, const bool hemiRegion,
    const int sex, const int genoIdx, const int GDidx, const int GQidx,
    const int alt) {
  double ret;
  if (genoIdx >= 0) {
    if (useDosage) {
      if (!hemiRegion) {
        ret = (indv.justGet(genoIdx).toDouble());
      } else {
        // for male hemi region, imputated dosage is usually between 0 and 1
        // need to multiply by 2.0
        if (sex == PLINK_MALE) {
          ret = (indv.justGet(genoIdx).toDouble() * 2.0);
        } else {
          ret = (indv.justGet(genoIdx).toDouble());
        }
      }
    } else {  // use hard-coded genotypes
      if (!hemiRegion) {
        ret = indv.justGet(genoIdx).countAltAllele(alt);
      } else {
        if (sex == PLINK_MALE) {
          // ret = (indv.justGet(genoIdx).getMaleNonParGenotype02());
          ret = (indv.justGet(genoIdx).countMaleNonParAltAllele2(alt));
        } else if (sex == PLINK_FEMALE) {
          // ret = (indv.justGet(genoIdx).getGenotype());
          ret = indv.justGet(genoIdx).countAltAllele(alt);
        } else {
          ret = (MISSING_GENOTYPE);
        }
      }
    }
    if (!checkGD(indv, GDidx) || !checkGQ(indv, GQidx)) {
      return MISSING_GENOTYPE;
    } else {
      return ret;
    }
  } else {
    logger->error("Cannot find %s field!",
                  this->dosageTag.empty() ? "GT" : dosageTag.c_str());
    return MISSING_GENOTYPE;
  }
}