-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfpp.cpp
74 lines (67 loc) · 2.53 KB
/
vcfpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* VCF[.gz] pretty-printing tool. This mostly just exists for demonstrating and
* testing the usage of picovcf.
*
* Usage:
* vcfpp <command> <file>
*
* where command is one of ["stats", "matrix"]
*/
#include <iostream>
#include "picovcf.hpp"
using namespace picovcf;
inline void emitAllele(VariantT alleleIndex, std::ostream& out) {
if (alleleIndex == MISSING_VALUE) {
out << "? ";
} else {
out << alleleIndex << " ";
}
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Please pass in a command and an input file (in that order)" << std::endl;
return 1;
}
const std::string command(argv[1]);
const std::string filename(argv[2]);
VCFFile vcf(filename);
if (command == "stats") {
std::cout << "Stats for " << filename << std::endl;
std::cout << " Variants: " << vcf.numVariants() << std::endl;
std::cout << " Individuals: " << vcf.numIndividuals() << std::endl;
std::cout << " Version: " << vcf.getMetaInfo(VCFFile::META_FILE_FORMAT) << std::endl;
std::cout << " Source: " << vcf.getMetaInfo("source") << std::endl;
std::cout << " Genome range: " << vcf.getGenomeRange().first << "-" << vcf.getGenomeRange().second
<< std::endl;
vcf.seekBeforeVariants();
if (vcf.hasNextVariant()) {
vcf.nextVariant();
std::cout << " Has genotype data? " << (vcf.currentVariant().hasGenotypeData() ? "yes" : "no")
<< std::endl;
}
} else if (command == "matrix") {
std::cout << "Rows: variants" << std::endl;
std::cout << "Columns: samples" << std::endl;
// This assumes/checks for phased data.
vcf.seekBeforeVariants();
while (vcf.hasNextVariant()) {
vcf.nextVariant();
VCFVariantView variant = vcf.currentVariant();
IndividualIteratorGT iterator = variant.getIndividualIterator();
while (iterator.hasNext()) {
VariantT allele1 = 0;
VariantT allele2 = 0;
bool isPhased = iterator.getAlleles(allele1, allele2);
if (!isPhased) {
std::cerr << "Cannot create a matrix for unphased data" << std::endl;
return 2;
}
emitAllele(allele1, std::cout);
if (allele2 != NOT_DIPLOID) {
emitAllele(allele2, std::cout);
}
}
std::cout << std::endl;
}
}
return 0;
}