-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcf.cpp
43 lines (42 loc) · 1.76 KB
/
vcf.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
#include "vcf.hpp"
unordered_map<string, vector<vcf_variant_t>> load_vcf_file(string path) {
std::unordered_map<std::string, std::vector<vcf_variant_t>> vcf_variants ;
std::ifstream vcf_file(path) ;
std::string line ;
unordered_map<string, int> header ;
int i = 0 ;
int n = 0 ;
string chrom = "UNKNOWN" ;
while (std::getline(vcf_file, line)) {
if (line[0] == '#' and line[1] == '#') {
continue ;
} else if (line[0] == '#') {
//TODO parse header
istringstream iss(line) ;
vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}} ;
} else {
istringstream iss(line) ;
vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}} ;
if (chrom != tokens[0]) {
chrom = tokens[0] ;
}
vcf_variant_t vcf_variant ;
vcf_variant.chrom = tokens[0] ;
vcf_variant.pos = std::stoi(tokens[1]) ;
vcf_variant.ref = tokens[3] ;
int l = vcf_variants[chrom].size() ;
if (l != 0 && vcf_variants[chrom][l - 1] == vcf_variant) {
vcf_variants[chrom][l - 1].alleles[1] = tokens[4] ;
vcf_variants[chrom][l - 1].svlen = max(vcf_variants[chrom][l - 1].svlen, int(tokens[4].length() - tokens[3].length())) ;
} else {
vcf_variant.alleles[0] = tokens[4] ;
vcf_variant.alleles[1] = "$" ;
vcf_variants[chrom].push_back(vcf_variant) ;
vcf_variants[chrom][l - 1].svlen = tokens[4].length() - tokens[3].length() ;
}
n++ ;
}
}
lprint({"Loaded", to_string(n), "variants from", path});
return vcf_variants ;
}