-
Notifications
You must be signed in to change notification settings - Fork 2
/
vcf_parser.cpp
156 lines (133 loc) · 4.97 KB
/
vcf_parser.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
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
#include "vcf_parser.hpp"
static char passfilt[] = "PASS";
vcf_parser::~vcf_parser() {
if (sw_)
bcf_sweep_destroy(sw_);
if (reference_)
fai_destroy(reference_);
}
bool vcf_parser::init(const std::string &reference_file,
const std::string &vcf_file, int k) {
assert(!reference_ && "Not initialized");
if (!(reference_ = fai_load(reference_file.c_str()))) {
fprintf(stderr, "Failed to read reference\n");
return false;
}
if (!(sw_ = bcf_sweep_init(vcf_file.c_str()))) {
fprintf(stderr, "Failed to read VCF\n");
return false;
}
k_ = k;
hdr_ = bcf_sweep_hdr(sw_);
if (!read_next_line()) {
fprintf(stderr, "Empty VCF file\n");
return false;
}
int nseq = 0;
auto **seq_names = bcf_hdr_seqnames(hdr_, &nseq);
seq_names_.assign(seq_names, seq_names + nseq);
free(seq_names);
return true;
}
void vcf_parser::print_line() {
if (!rec_)
return;
fprintf(stdout, "%s\t%d\t%d\t%d\t%d",
hdr_->id[BCF_DT_CTG][rec_->rid].key, //contig name
hdr_->id[BCF_DT_CTG][rec_->rid].val->info[0], //contig length
rec_->rid, //contig id
rec_->pos + 1, //1-based coordinate
rec_->n_allele //number of alleles
);
}
bool vcf_parser::get_seq(const std::vector<std::string> &annots,
std::vector<std::string> *annotation) {
while (rec_) {
curi++;
if (curi >= rec_->n_allele || !bcf_has_filter(hdr_, rec_, passfilt)) {
read_next_line();
continue;
}
std::string curalt;
if (rec_->d.allele[curi][0] == '<') {
//if this is of the form <CN#>, then it's a copy number variation
//otherwise, crash
if (rec_->d.allele[curi][1] != 'C' || rec_->d.allele[curi][2] != 'N') {
//TODO: HANDLE RETROTRANSPOSONS PROPERLY
fprintf(stderr, "Can't handle this type of variant, skipping: %s\n",
rec_->d.allele[curi]);
read_next_line();
continue;
//exit(1);
}
//replace <CN#> with # copies of the ref allele
//fprintf(stderr, "%s\n", rec_->d.allele[curi]);
rec_->d.allele[curi][strlen(rec_->d.allele[curi]) - 1] = 0;
//fprintf(stderr, "%s\n", rec_->d.allele[curi]);
size_t cn = atol(rec_->d.allele[curi] + 3);
while (cn--) {
curalt += rec_->d.allele[0];
}
//fprintf(stderr, "\n%u\n%s\n%s\n", cn, rec_->d.allele[0], curalt);
} else {
curalt = rec_->d.allele[curi];
}
//construct sequence
seq = kmer1_ + curalt + kmer3_;
if (!annotation)
return true;
//annotation is a bit vector indicating inclusion in the different ethnic groups
//the first bit is always 1. if not, then the file is done and no sequence was output
//TODO: check if these annots are part of the INFO, if not, check genotypes
//ngt = bcf_get_genotypes(sr->readers[0].header, line0, >_arr, &ngt_arr); //get genotypes
//annot=1;
annotation->emplace_back(seq_names_[rec_->rid]);
for (const auto &annot : annots) {
bcf_info_t *curinfo = bcf_get_info(hdr_, rec_, annot.c_str());
if (curinfo && curinfo->v1.i) {
annotation->emplace_back(annot);
}
}
if ((bcf_get_fmt(hdr_, rec_, "GT"))) {
int32_t *gt_arr = NULL, ngt_arr = 0;
int ngt = bcf_get_genotypes(hdr_, rec_, >_arr, &ngt_arr);
int nsmpl = bcf_hdr_nsamples(hdr_);
int ploidy = ngt / nsmpl;
for (int i = 0; i < nsmpl; ++i) {
int cur = 0;
for (int j = 0; j < ploidy; ++j) {
cur |= gt_arr[i * ploidy + j];
}
if ((cur & 5) == 5) {
//at least one parent has this allele
annotation->emplace_back(hdr_->samples[i]);
}
}
if (gt_arr)
free(gt_arr);
}
return true;
}
return false;
}
bool vcf_parser::read_next_line() {
if (!(rec_ = bcf_sweep_fwd(sw_)))
return false;
curi = 0;
bcf_unpack(rec_, BCF_UN_FLT);
uint32_t ref_callele_l = strlen(rec_->d.allele[0]);
int len;
auto first = faidx_fetch_seq(reference_,
hdr_->id[BCF_DT_CTG][rec_->rid].key,
rec_->pos - k_,
rec_->pos - 1, &len);
kmer1_.assign(first, len);
free(first);
auto second = faidx_fetch_seq(reference_,
hdr_->id[BCF_DT_CTG][rec_->rid].key,
rec_->pos + ref_callele_l,
rec_->pos + ref_callele_l - 1 + k_, &len);
kmer3_.assign(second, len);
free(second);
return true;
}