-
Notifications
You must be signed in to change notification settings - Fork 37
/
variant_manip.h
274 lines (234 loc) · 7.05 KB
/
variant_manip.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* The MIT License
Copyright (c) 2013 Adrian Tan <atks@umich.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef VARIANT_MANIP_H
#define VARIANT_MANIP_H
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
#include "htslib/faidx.h"
#include "htslib/kstring.h"
#include "htslib/vcf.h"
#include "hts_utils.h"
//int BitCount(unsigned int u)
//{
// unsigned int uCount;
//
//#ifndef countbit32
//#define countbit32(x) (y = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); ((y + (y >> 3)) & 030707070707) % 63);
//
//#endif
#define VT_REF 0 //dlen==0 && diff==0
#define VT_SNP 1 //min(rlen,alen)==1 && diff==1
#define VT_MNP 2 //min(rlen,alen)==diff
#define VT_INDEL 4 //diff!=0 && (rlen==1 || alen==1)
#define VT_CLUMPED 8 //all others
/**
* Allele.
*
* Allele can be described to be a SNP or INDEL or etc. with respect to the reference
* type - give the variant type
* len - difference in length between the ref and alt with positive inferring an insertion and negative inferring a deletion
* tlen - assuming variant is VNTR, tlen is the tract length of the repeated motif of the VNTR
*/
class Allele
{
public:
int32_t type;
int32_t diff; //number of difference bases when bases are compared
int32_t alen; //length(alt)
int32_t dlen; //length(alt)-length(ref)
int32_t tlen; //tract length with respect to reference
int32_t mlen; //min shared length
int32_t ts; //no of transitions
int32_t tv; //no of tranversions (mlen-ts)
int32_t ins; //no of insertions
Allele(int32_t type, int32_t diff, int32_t alen, int32_t dlen, int32_t tlen, int32_t mlen, int32_t ts, int32_t ins)
{
this->type = type;
this->diff = diff;
this->alen = alen;
this->dlen = dlen;
this->tlen = tlen;
this->mlen = mlen;
this->ts = ts;
this->tv = mlen-ts;
this->ins = ins;
}
Allele()
{
clear();
};
~Allele() {};
void clear()
{
type = VT_REF;
diff = 0;
alen = 0;
dlen = 0;
tlen = 0;
mlen = 0;
ts = 0;
tv = 0;
ins = 0;
}
void print()
{
std::cerr << "\ttype: " << type << "\n";
std::cerr << "\tdiff: " << diff << "\n";
std::cerr << "\talen: " << alen << "\n";
std::cerr << "\tdlen: " << dlen << "\n";
std::cerr << "\ttlen: " << tlen << "\n";
};
};
/**
* Variant.
* Describes the variant in question.
*/
class Variant
{
public:
int32_t type; //aggegrated type from the alleles
int32_t rlen; //reference length
kstring_t motif; //motif
int32_t mlen; //motif length
int32_t tlen; //reference tract length
std::vector<Allele> alleles;
Variant()
{
type = VT_REF;
motif = {0,0,0};
mlen = 0;
tlen = 0;
alleles.clear();
}
~Variant()
{
if (motif.m)
{
free(motif.s);
}
}
/**
* Prints variant information.
*/
void print();
/**
* Converts VTYPE to string.
*/
std::string vtype2string(int32_t VTYPE);
void clear()
{
type = 0;
motif.l = 0;
mlen = 0;
tlen = 0;
alleles.clear();
}
};
/**
* Methods for manipulating variants
*/
class VariantManip
{
public:
faidx_t *fai;
bool reference_present;
/**
* Constructor.
*
* @ref_fasta_file reference sequence FASTA file.
*/
VariantManip(std::string ref_fasta_file);
/**
* Constructor.
*/
VariantManip();
/**
* Detects near by STRs.
*/
bool detect_str(bcf_hdr_t *h, bcf1_t *v, Variant& variant);
/**
* Detects near by STRs.
*/
bool detect_str(const char* chrom, uint32_t pos1, Variant& variant);
/**
* Converts VTYPE to string.
*/
std::string vtype2string(int32_t VTYPE);
/**
* Converts VTYPE to string.
*/
void vtype2string(int32_t vtype, kstring_t *s);
/**
* Classifies variants.
*/
int32_t classify_variant(bcf_hdr_t *h, bcf1_t *v, Variant& variant, bool in_situ_left_trimming = true);
/**
* Classifies variants.
*/
int32_t classify_variant(bcf_hdr_t *h, bcf1_t *v);
/**
* Classifies variants.
*/
int32_t classify_variant(const char* chrom, uint32_t pos1, char** allele, int32_t n_allele, Variant& variant, bool in_situ_left_trimming = true);
/**
* Classifies variants.
*/
int32_t classify_variant(const char* chrom, uint32_t pos1, char** allele, int32_t n_allele);
/**
* Left trims a variant with unnecesary nucleotides.
*/
void left_trim(std::vector<std::string>& alleles, uint32_t& pos1, uint32_t& left_trimmed) ;
/**
* Left aligns a variant.
*/
void left_align(std::vector<std::string>& alleles, uint32_t& pos1, const char* chrom, uint32_t& leftAligned, uint32_t& right_trimmed);
/**
* Generates a probing haplotype with flanks around the variant of interest.
*/
void generate_probes(const char* chrom,
int32_t pos1, uint32_t probeDiff,
std::vector<std::string>& alleles, //store alleles
std::vector<std::string>& probes, //store probes
uint32_t min_flank_length,
int32_t& preambleLength); //store preamble length
private:
/**
* Recursive helper method for generateProbes.
*/
void generate_probes(const char* chrom,
int32_t pos1,
uint32_t flankLength,
uint32_t& currentDiff,
uint32_t& length,
uint32_t gald,
std::vector<uint32_t>& diff,
std::vector<std::string>& alleles,
std::vector<std::string>& probes);
};
#endif