File: vcfsitesummarize.cpp

package info (click to toggle)
libvcflib 1.0.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 49,732 kB
  • sloc: cpp: 39,194; perl: 474; python: 321; ruby: 285; sh: 247; ansic: 198; makefile: 125; javascript: 94; lisp: 55
file content (134 lines) | stat: -rw-r--r-- 3,790 bytes parent folder | download | duplicates (3)
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
/*
    vcflib C++ library for parsing and manipulating VCF files

    Copyright © 2010-2020 Erik Garrison
    Copyright © 2020      Pjotr Prins

    This software is published under the MIT License. See the LICENSE file.
*/

#include "Variant.h"

using namespace std;
using namespace vcflib;

int main(int argc, char** argv) {

  if (argc == 2) {
    string h_flag = argv[1];
    if (h_flag == "-h" || h_flag == "--help") {
      cerr << R"(
Summarize by site

Usage: vcfsitesummarize <vcf file>

Example:

vcfsitesummarize samples/sample.vcf

CHROM   POS     ID      REF     QUAL    FILTER  AA      AC      AF      AN      DP      NS      DB      H2
19      111     .       A       9.6     .                                                       0       0
19      112     .       A       10      .                                                       0       0
20      14370   rs6054257       G       29      PASS                    0.5             14      3       1 1
20      17330   .       T       3       q10                     0.017           11      3       0       0
20      1110696 rs6040355       A       67      PASS    T                               10      2       1 0
20      1230237 .       T       47      PASS    T                               13      3       0       0
20      1234567 microsat1       G       50      PASS    G                       6       9       3       0 0
20      1235237 .       T       0       .                                                       0       0
X       10      rsTest  AC      10      PASS


Type: statistics

      )";
      exit(1);
    }
  }


    VariantCallFile variantFile;

    if (argc > 1) {
        string filename = argv[1];
        variantFile.open(filename);
    } else {
        variantFile.open(std::cin);
    }

    if (!variantFile.is_open()) {
        return 1;
    }

    // obtain all possible field names
    vector<string> infofields;
    vector<string> infoflags;

    for (map<string, VariantFieldType>::iterator i = variantFile.infoTypes.begin(); i != variantFile.infoTypes.end(); ++i) {
        if (variantFile.infoCounts[i->first] != ALLELE_NUMBER) {
            if (i->second == FIELD_BOOL) {
                infoflags.push_back(i->first);
            } else {
                infofields.push_back(i->first);
            }
        }
    }

    // write header

    // defaults
    cout << "CHROM\tPOS\tID\tREF\tQUAL\tFILTER";

    // configurable info field
    for (vector<string>::iterator i = infofields.begin(); i != infofields.end(); ++i) {
        cout << "\t" << *i;
    }
    for (vector<string>::iterator i = infoflags.begin(); i != infoflags.end(); ++i) {
        cout << "\t" << *i;
    }
    cout << endl;

    Variant var(variantFile);
    while (variantFile.getNextVariant(var)) {

	cout << var.sequenceName << "\t"
	     << var.position << "\t"
	     << var.id << "\t"
	     << var.ref << "\t"
	     << var.quality << "\t"
	     << var.filter;

	for (vector<string>::iterator i = infofields.begin(); i != infofields.end(); ++i) {
	    vector<string> value;
	    string& name = *i;
	    map<string, vector<string> >::iterator f = var.info.find(name);
	    if (f != var.info.end()) {
            value = f->second;
            if (value.size() == 1) {
                cout << "\t" << value.front();
            } else {
                cout << "\t"; // null
            }
	    } else {
            cout << "\t"; // null
	    }
	}

	for (vector<string>::iterator i = infoflags.begin(); i != infoflags.end(); ++i) {
	    string value;
	    string& name = *i;
	    map<string, bool>::iterator f = var.infoFlags.find(name);
	    cout << "\t";
	    if (f != var.infoFlags.end()) {
            cout << 1;
	    } else {
            cout << 0;
	    }
	}

	cout << endl;

    }

    return 0;

}