~ubuntu-branches/debian/sid/libvcflib/sid

« back to all changes in this revision

Viewing changes to src/vcfkeepinfo.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2016-09-16 15:52:29 UTC
  • Revision ID: package-import@ubuntu.com-20160916155229-24mxrntfylvsshsg
Tags: upstream-1.0.0~rc1+dfsg
ImportĀ upstreamĀ versionĀ 1.0.0~rc1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Variant.h"
 
2
#include "split.h"
 
3
#include <string>
 
4
#include <iostream>
 
5
#include <set>
 
6
 
 
7
using namespace std;
 
8
using namespace vcflib;
 
9
 
 
10
int main(int argc, char** argv) {
 
11
 
 
12
    if (argc < 3) {
 
13
        cerr << "usage: " << argv[0] << " <vcf file> [FIELD1] [FIELD2] ..." << endl
 
14
             << "outputs each record in the vcf file, removing INFO fields not listed on the command line" << endl;
 
15
        return 1;
 
16
    }
 
17
 
 
18
    string filename = argv[1];
 
19
 
 
20
    set<string> fieldsToKeep;
 
21
    for (int i = 2; i < argc; ++i) {
 
22
        fieldsToKeep.insert(argv[i]);
 
23
    }
 
24
 
 
25
    VariantCallFile variantFile;
 
26
    if (filename == "-") {
 
27
        variantFile.open(std::cin);
 
28
    } else {
 
29
        variantFile.open(filename);
 
30
    }
 
31
 
 
32
    if (!variantFile.is_open()) {
 
33
        return 1;
 
34
    }
 
35
 
 
36
    Variant var(variantFile);
 
37
 
 
38
    vector<string> fieldsToErase;
 
39
    vector<string> infoIds = variantFile.infoIds();
 
40
    for (vector<string>::iterator i = infoIds.begin(); i != infoIds.end(); ++i) {
 
41
        if (!fieldsToKeep.count(*i)) {
 
42
            fieldsToErase.push_back(*i);
 
43
            variantFile.removeInfoHeaderLine(*i);
 
44
        }
 
45
    }
 
46
 
 
47
    // write the header
 
48
    cout << variantFile.header << endl;
 
49
 
 
50
    // print the records, filtering is done via the setting of varA's output sample names
 
51
    while (variantFile.getNextVariant(var)) {
 
52
        for (map<string, vector<string> >::iterator i = var.info.begin(); i != var.info.end(); ++i) {
 
53
            if (!fieldsToKeep.count(i->first)) {
 
54
                var.info.erase(i->first);
 
55
            }
 
56
        }
 
57
        for (map<string, bool>::iterator i = var.infoFlags.begin(); i != var.infoFlags.end(); ++i) {
 
58
            if (!fieldsToKeep.count(i->first)) {
 
59
                var.infoFlags.erase(i->first);
 
60
            }
 
61
        }
 
62
        cout << var << endl;
 
63
    }
 
64
 
 
65
    return 0;
 
66
 
 
67
}
 
68