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

« back to all changes in this revision

Viewing changes to src/vcfgenotypes.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
 
 
6
using namespace std;
 
7
using namespace vcflib;
 
8
 
 
9
 
 
10
int main(int argc, char** argv) {
 
11
 
 
12
    if (argc != 2) {
 
13
        cerr << "usage: " << argv[0] << " <vcf file>" << endl
 
14
             << "report the genotypes for each sample, for each variant in the vcf file" << endl;
 
15
        return 1;
 
16
    }
 
17
 
 
18
    string filename = argv[1];
 
19
 
 
20
    VariantCallFile variantFile;
 
21
 
 
22
    if (filename == "-") {
 
23
        variantFile.open(std::cin);
 
24
    } else {
 
25
        variantFile.open(filename);
 
26
    }
 
27
 
 
28
    if (!variantFile.is_open()) {
 
29
        return 1;
 
30
    }
 
31
 
 
32
    Variant var(variantFile);
 
33
    while (variantFile.getNextVariant(var)) {
 
34
        map<string, map<string, vector<string> > >::iterator s     = var.samples.begin(); 
 
35
        map<string, map<string, vector<string> > >::iterator sEnd  = var.samples.end();
 
36
        
 
37
        cout << var.sequenceName << "\t"
 
38
             << var.position     << "\t"
 
39
             << var.ref          << "\t";
 
40
        var.printAlt(cout);     cout << "\t"; 
 
41
        var.printAlleles(cout); cout << "\t"; 
 
42
        
 
43
        for (; s != sEnd; ++s) {
 
44
            map<string, vector<string> >& sample = s->second;
 
45
            string& genotype = sample["GT"].front(); // XXX assumes we can only have one GT value
 
46
            vector<string> gt = split(genotype, "|/");
 
47
            
 
48
            // report the sample and it's genotype
 
49
            cout << s->first << ":";
 
50
            for (vector<string>::iterator g = gt.begin(); g != gt.end(); ++g) {
 
51
                if (g->c_str() == ".") {
 
52
                    cout << ".";
 
53
                } else {
 
54
                    int index = atoi(g->c_str());
 
55
                    cout << var.alleles[index];
 
56
                }
 
57
                if (g != (gt.end()-1)) cout << "/";
 
58
            }
 
59
            cout << "\t";
 
60
        }
 
61
        cout << endl;
 
62
    }
 
63
    return 0;
 
64
 
 
65
}
 
66