-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCIsbtVariant.cpp
More file actions
265 lines (240 loc) · 7.58 KB
/
CIsbtVariant.cpp
File metadata and controls
265 lines (240 loc) · 7.58 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: CIsbtVariant.cpp
* Author: mwittig
*
* Created on July 22, 2019, 8:08 AM
*/
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <libgen.h>
#include <limits>
#include <regex>
#include "CIsbtVariant.h"
#include "CBigWigReader.h"
using namespace std;
bool CIsbtVariant::verbose = false;
CIsbtVariant::CIsbtVariant()
{
m_vcf_snp = NULL;
m_isbt_name = "";
m_high_impact_variant=false;
m_chromosome = "";
m_position = -1;
m_strand = 'u';
m_lrg_position = "";
/// attention: m_lrg_reference will be revised in bool CIsbtVariant::parseIsbtVariant()
m_lrg_reference = "";
m_lrg_alternative = "";
m_are_ref_and_alt_switched_in_GRCh = false;
m_vcf_coordinate = -1;
m_vcf_reference = "";
m_vcf_alternative = "";
m_variation_type="";
m_coverage = std::numeric_limits<float>::quiet_NaN();
}
CIsbtVariant::CIsbtVariant(const string& lrg_anno, const string& refBase, const string& chrom, int pos, char strand,
int vcfCoord, const string& vcfRef, const string& vcfAlt, bool are_ref_and_alt_switched_in_GRCh, const string& variation_type)
{
m_vcf_snp = NULL;
m_isbt_name = lrg_anno;
if(m_isbt_name.size() > 1 && m_isbt_name[0] == '!')
{
m_high_impact_variant=true;
m_isbt_name = m_isbt_name.substr(1);
}
else
m_high_impact_variant=false;
m_chromosome = chrom;
m_position = pos;
m_strand = strand;
m_lrg_position = "";
/// attention: m_lrg_reference will be revised in bool CIsbtVariant::parseIsbtVariant()
m_lrg_reference = refBase;
m_lrg_alternative = "";
m_vcf_coordinate = vcfCoord;
m_vcf_reference = vcfRef;
m_vcf_alternative = vcfAlt;
m_coverage = std::numeric_limits<float>::quiet_NaN();
m_are_ref_and_alt_switched_in_GRCh = are_ref_and_alt_switched_in_GRCh;
m_variation_type=variation_type;
parseIsbtVariant();
}
CIsbtVariant::CIsbtVariant(const CIsbtVariant& orig)
{
m_isbt_name = orig.m_isbt_name;
m_high_impact_variant=orig.m_high_impact_variant;
m_lrg_position = orig.m_lrg_position;
m_lrg_reference = orig.m_lrg_reference;
m_lrg_alternative = orig.m_lrg_alternative;
m_vcf_coordinate = orig.m_vcf_coordinate;
m_vcf_reference = orig.m_vcf_reference;
m_vcf_alternative = orig.m_vcf_alternative;
m_are_ref_and_alt_switched_in_GRCh = orig.m_are_ref_and_alt_switched_in_GRCh;
m_chromosome = orig.m_chromosome;
m_position = orig.m_position;
m_strand = orig.m_strand;
m_coverage = orig.m_coverage;
m_variation_type = orig.m_variation_type;
m_vcf_snp = new CVcfSnp(orig.m_vcf_snp);
}
CIsbtVariant& CIsbtVariant::operator =(const CIsbtVariant& orig)
{
m_isbt_name = orig.m_isbt_name;
m_high_impact_variant=orig.m_high_impact_variant;
m_lrg_position = orig.m_lrg_position;
m_lrg_reference = orig.m_lrg_reference;
m_lrg_alternative = orig.m_lrg_alternative;
m_vcf_coordinate = orig.m_vcf_coordinate;
m_vcf_reference = orig.m_vcf_reference;
m_vcf_alternative = orig.m_vcf_alternative;
m_are_ref_and_alt_switched_in_GRCh = orig.m_are_ref_and_alt_switched_in_GRCh;
m_chromosome = orig.m_chromosome;
m_position = orig.m_position;
m_strand = orig.m_strand;
m_coverage = orig.m_coverage;
m_variation_type = orig.m_variation_type;
m_vcf_snp = new CVcfSnp(orig.m_vcf_snp);
return *this;
}
nlohmann::json CIsbtVariant::getSnpAsJson()const
{
nlohmann::json jRet;
jRet["variation"]=name();
jRet["chrom"]=chrom();
jRet["position"]=pos();
jRet["depth"]=getCoverage();
jRet["reference"]=reference();
jRet["alternative"]=alternative();
jRet["lrg_reference"]=lrgReference();
jRet["lrg_alternative"]=lrgAlternative();
jRet["gt_quality"]=getVcfGenotypeQuality();
jRet["high_impact"]=getImpact();
return jRet;
}
void CIsbtVariant::addVcfSnp(const CVcfSnp& snp)
{
m_vcf_snp = new CVcfSnp(snp);
}
int CIsbtVariant::getVcfGenotypeQuality()const
{
if(m_vcf_snp)
{
vector<int> q = m_vcf_snp->genotypeQualities();
if(q.empty())
return -1;
return q[0];
}
return -1;
}
bool CIsbtVariant::operator <(const CIsbtVariant& orig)const
{
int c = m_chromosome.compare(orig.m_chromosome);
if(c < 0 )
return true;
if(c > 0)
return false;
if(m_position < orig.m_position)
return true;
if(m_position > orig.m_position)
return false;
c = m_isbt_name.compare(orig.m_isbt_name);
if(c < 0 )
return true;
return false;
}
bool CIsbtVariant::operator ==(const CIsbtVariant& orig)const
{
return (
m_chromosome.compare(orig.m_chromosome) == 0 &&
m_position == orig.m_position &&
m_strand == orig.m_strand &&
m_isbt_name.compare(orig.m_isbt_name) == 0
);
}
/*
bool CIsbtVariant::operator ==(const string& orig)const
{
return m_isbt_name.compare(orig) == 0;
}
*/
std::ostream& operator<<(std::ostream& os, const CIsbtVariant& me)
{
os << me.name();
if(me.verbose)
os << " (" << (me.m_coverage == me.m_coverage ? me.m_coverage : 0 ) << "x)";
return os;
}
CIsbtVariant::~CIsbtVariant()
{
if(m_vcf_snp)
delete m_vcf_snp;
}
bool CIsbtVariant::parseIsbtVariant()
{
smatch m;
//regex e ("^[0-9_+-]{1,}");
regex e ("^[0-9_+-?*)(]{1,}");
regex_search(m_isbt_name,m,e);
for (auto x:m)
m_lrg_position = x;
string actBaseChange= m.suffix().str();
size_t pos = actBaseChange.find('>');
if(pos != string::npos)
{
m_lrg_reference = actBaseChange.substr(0,pos);
m_lrg_alternative = actBaseChange.substr(pos+1);
}
else if(actBaseChange.substr(0,6).compare("delins")==0)
{
m_lrg_alternative = actBaseChange.substr(6);
if(strand() == '-')
m_lrg_reference = CMyTools::GetComplSequence(m_lrg_reference);
}
else if(actBaseChange.substr(0,3).compare("del")==0)
{
if(!actBaseChange.substr(3).empty())
m_lrg_reference = actBaseChange.substr(3);
else if(strand() == '-')
m_lrg_reference = CMyTools::GetComplSequence(m_lrg_reference);
m_lrg_alternative = "-";
}
else if(actBaseChange.substr(0,3).compare("ins")==0)
{
m_lrg_reference = "-";
m_lrg_alternative = actBaseChange.substr(3);
}
else if(actBaseChange.substr(0,3).compare("dup")==0)
{
// in the SNP calling a duplication is reported as a left aligned insertion
// so we have to store that here accordingly
//if(strand() == '-')
// m_lrg_reference = CMyTools::GetComplSequence(m_lrg_reference);
//m_lrg_alternative = m_lrg_reference + actBaseChange.substr(3);
m_lrg_reference = "-";
m_lrg_alternative = actBaseChange.substr(3);
}
else
{
cerr << "Can not parse this variant annotation: " << m_isbt_name << " at " << m_lrg_position << ", basechange: " << actBaseChange << endl;
}
//cout << vRet.first << " o " << m_lrg_reference << " o " << m_lrg_alternative << endl;
return true;
}
bool CIsbtVariant::addCoverage(const CBigWigReader& bigWig)
{
m_coverage = bigWig.getMinCoverage(m_chromosome,m_position-1,m_position+1);
return m_coverage == m_coverage;
}
bool CIsbtVariant::isCovered(double limit)const
{
if( !(m_coverage == m_coverage) ) // is NaN
return false;
return m_coverage >= limit;
}