-
Notifications
You must be signed in to change notification settings - Fork 26
/
bcf_variant_key.h
116 lines (105 loc) · 2.7 KB
/
bcf_variant_key.h
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
#ifndef __BCF_VARIANT_KEY_H
#define __BCF_VARIANT_KEY_H
#include <string>
#include <vector>
#include "hts_utils.h"
class variantKeyS {
public:
std::string chrom;
int32_t pos;
int32_t rlen;
std::vector<std::string> alleles;
variantKeyS(bcf_hdr_t* hdr, bcf1_t* v) : chrom(bcf_get_chrom(hdr,v)), pos(v->pos), rlen(v->rlen) {
alleles.resize(v->n_allele);
bcf_unpack(v,BCF_UN_STR);
for(int32_t i=0; i < v->n_allele; ++i) {
alleles[i].assign(v->d.allele[i]);
}
//notice("Parsed variant key is %s : %d : %s : %u", chrom.c_str(), pos, rlen, alleles[0].c_str(), alleles.size());
}
variantKeyS(const char* s) {
const char* p = s;
const char* pp = NULL;
int32_t ncolons = 0;
alleles.resize(2);
while( *p != '\0' ) {
if ( *p == ':' ) {
if ( ncolons == 0 ) {
chrom.assign(s, p-s);
pp = p;
++ncolons;
}
else if ( ncolons == 1 ) {
pos = atoi(pp+1)-1;
pp = p;
++ncolons;
}
else if ( ncolons == 2 ) {
rlen = p-pp-1;
alleles[0].assign(pp+1,rlen);
pp = p;
++ncolons;
}
else if ( ncolons == 3 ) {
break;
}
}
++p;
}
if ( ncolons == 3 ) {
const char* p1 = pp+1;
const char* pp1 = pp+1;
//alleles.resize(2);
while( *p1 != *p ) {
if ( *p1 == ',' ) {
alleles.back().assign(pp1,p1-pp1);
pp1 = p1+1;
alleles.resize(alleles.size()+1);
}
++p1;
}
alleles.back().assign(pp1, p-pp1);
}
else
error("[E:%s] Cannot parse variant key %s",__PRETTY_FUNCTION__, s);
//notice("Parsed variant key for %s is %s : %d : %d : %s : %u", s, chrom.c_str(), pos, rlen, alleles[0].c_str(), alleles.size());
}
bool operator<(const variantKeyS& rhs) const {
if ( chrom == rhs.chrom ) {
if ( pos == rhs.pos ) {
if ( rlen == rhs.rlen ) {
for(int32_t i=0; i < (int32_t)alleles.size(); ++i) {
if ( i >= (int)rhs.alleles.size() ) return false;
else if ( alleles[i] != rhs.alleles[i] ) return alleles[i] < rhs.alleles[i];
}
return false;
}
else return ( rlen < rhs.rlen );
}
else return ( pos < rhs.pos );
}
else {
int32_t ichr1 = atoi(chrom.c_str());
int32_t ichr2 = atoi(rhs.chrom.c_str());
if ( ichr1 == ichr2 ) { return chrom < rhs.chrom; }
else { return ichr1 < ichr2; }
}
}
bool operator==(const variantKeyS& rhs) const {
if ( chrom == rhs.chrom ) {
if ( pos == rhs.pos ) {
if ( rlen == rhs.rlen ) {
if ( alleles.size() != rhs.alleles.size() ) return false;
for(int32_t i=0; i < (int32_t)alleles.size(); ++i) {
if ( alleles[i] != rhs.alleles[i] ) return false;
}
return true;
}
else return false;
}
else return false;
}
else return false;
}
};
#endif