-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoreVCF.cpp
277 lines (203 loc) · 6.55 KB
/
CoreVCF.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
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
275
276
277
/*
* CoreVCF
* Date: Sep-30-2013
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include "CoreVCF.h"
CoreVCF::CoreVCF(){
closeIndel=false;
haveInfoField =false;
haveFormatNames=false;
fieldIndex=0;
}
CoreVCF::CoreVCF(const vector<string> & fields){
closeIndel=false;
fieldIndex=0;
// if(fields.size() == 11){
// if( !fields[10].empty() ){
// cerr<<"CoreVCF ERROR, found # fields = ("<<fields.size()<<") for : #"<<vectorToString(fields,"\t")<<"#"<<endl;
// for(unsigned int i=0;i<fields.size();i++){
// cerr<<"fields["<<i<<"] = "<<fields[i]<<endl;
// }
// exit(1);
// }
// }else{
if(fields.size() < 9){
cerr<<"CoreVCF ERROR, not enough fields ("<<fields.size()<<") for : #"<<vectorToString(fields,"\t")<<"#"<<endl;
for(unsigned int i=0;i<fields.size();i++){
cerr<<"fields["<<i<<"] = "<<fields[i]<<endl;
}
exit(1);
}
// }
//chrName= fields[fieldIndex++];
setName( fields[fieldIndex++].c_str() );
//position=string2uint( fields[fieldIndex++]);
setPos( fields[fieldIndex++].c_str() );
//if(fields.size() != 9 )
//id = fields[fieldIndex++];
setID( fields[fieldIndex++].c_str() );
// else
// id = "NA";
//cerr<<"CoreVCF id="<<id<<" "<<fieldIndex<<endl;
// ref = fields[fieldIndex++];
// resolvedSingleBasePairREF=validOneBP(ref); //true if ref = A,C,G or T
setREF( fields[fieldIndex++].c_str() );
setALT( fields[fieldIndex++].c_str() );
setQUAL( fields[fieldIndex++].c_str() );
// int qualFieldIdx=fieldIndex++;
// //cerr<<"CoreVCF qualFieldIdx="<<qualFieldIdx<<" "<<fieldIndex<<endl;
// if(fields[qualFieldIdx] == "."){
// qual=0.0;
// }else{
// qual = destringify<float>( fields[qualFieldIdx] );
// }
//cerr<<"CoreVCF qual="<<qual<<endl;
//filter = fields[fieldIndex++];
setFILTER( fields[fieldIndex++].c_str() );
//INFO FIELD
fieldIndexINFO = fieldIndex;
setINFO( fields[fieldIndex++].c_str() );
// cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
// cerr<<"CoreVCF filter="<<filter<<endl;
//cerr<<"CoreVCF fieldIndex0 "<<fieldIndex<<endl;
//infoFieldRaw = fields[fieldIndex++] ;
// cerr<<"CoreVCF info="<<infoFieldRaw<<endl;
// cerr<<"CoreVCF fieldIndex1 "<<fieldIndex<<endl;
//infoFieldsNames = allTokens(infoFieldRaw ,':');
//cerr<<"CoreVCF fieldIndex2 "<<fieldIndex<<endl;
//increasing to skip GT FIELD
setFORMAT( fields[fieldIndex++].c_str() );
//cerr<<"CoreVCF format="<<rawFormatNames<<endl;
// fieldIndex++;
}
CoreVCF::~CoreVCF(){
if(haveInfoField)
delete infoField;
if(haveFormatNames)
delete formatNames;
}
void CoreVCF::setName( const char * p){
chrName= string(p);
}
void CoreVCF::setPos( const char * p){
position = (unsigned int)strtoul(p, NULL, 0);
//position=string2uint( fields[fieldIndex++]);
}
void CoreVCF::setID( const char * p){
id = string(p);
}
void CoreVCF::setREF( const char * p){
ref = string(p);
resolvedSingleBasePairREF= validOneBP(ref); //true if ref = A,C,G or T
}
void CoreVCF::setALT( const char * p){
alt = string(p);
// cerr<<"CoreVCF ref="<<ref<<" "<<fieldIndex<<endl;
// cerr<<"CoreVCF alt="<<alt<<" "<<fieldIndex<<endl;
// cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
//for sites with multiple alt bases
altAlleles = allTokens(alt,',');
//cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
allAltResolvedSingleBasePair=true;
//cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
for(unsigned int i=0;i<altAlleles.size();i++){
allAltResolvedSingleBasePair&=validAltBP( altAlleles[i] ); //true if ref = A,C,G,T or .
}
//cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
//boolean flags for insert
isIndel=isInsert(ref) || isInsert(alt);
//cerr<<"isIndel "<<isIndel<<endl;
//boolean flags for a single bp in ref or alt
resolvedSingleBasePairALT=validAltBP(alt); //true if ref = A,C,G,T or .
//cerr<<"CoreVCF fieldIndex "<<fieldIndex<<endl;
}
void CoreVCF::setQUAL( const char * p){
//int qualFieldIdx=fieldIndex++;
//cerr<<"CoreVCF qualFieldIdx="<<qualFieldIdx<<" "<<fieldIndex<<endl;
//if(fields[qualFieldIdx] == "."){
if(strcmp(p,".") == 0 ){
qual=0.0;
}else{
qual = atof(p); //destringify<float>( fields[qualFieldIdx] );
}
}
void CoreVCF::setFILTER(const char * p){
filter = string(p);
}
void CoreVCF::setINFO( const char * p){
infoFieldRaw = string(p);
haveInfoField=false;
//if INDEL MARKED
isIndel = isIndel || strBeginsWith(infoFieldRaw,"INDEL");
}
void CoreVCF::setFORMAT(const char * p){
rawFormatNames = string(p);
haveFormatNames = true;
formatNames = new vector<string> (allTokens(rawFormatNames ,':'));
}
const vector<string> * CoreVCF::getFormatNames(){
return formatNames;
}
void CoreVCF::parseInfoFields(){
haveInfoField=true;
infoField = info2map( infoFieldRaw );
}
bool CoreVCF::hasInfoField(string tag) {
if(!haveInfoField){ parseInfoFields(); }
return (infoField->find(tag) != infoField->end());
}
int CoreVCF::getDepthInfo() {
if(!haveInfoField){ parseInfoFields(); }
if(hasInfoField("DP")){
int toReturn = getInfoField<int>("DP");
return toReturn;
}
return -1;
}
string CoreVCF::getRef() const{
return ref;
}
string CoreVCF::getAlt() const{
return alt;
}
string CoreVCF::getID() const{
return id;
}
string CoreVCF::getChr() const{
return chrName;
}
unsigned int CoreVCF::getPosition() const{
return position;
}
string CoreVCF::getFilter() const{
return filter;
}
string CoreVCF::getInfoFieldRaw() const{
return infoFieldRaw;
}
int CoreVCF::getFieldIndexAndIncrease(){
return fieldIndex++;
}
int CoreVCF::getFieldIndexINFO(){
return fieldIndexINFO;
}
float CoreVCF::getQual() const{
return qual;
}
void CoreVCF::setCloseIndel(bool closeIndel){
this->closeIndel=closeIndel;
}
bool CoreVCF::getCloseIndel() const{
return closeIndel;
}
bool CoreVCF::containsIndel() const{
return isIndel;
}
bool CoreVCF::isResolvedSingleBasePairREF() const{
return resolvedSingleBasePairREF;
}
bool CoreVCF::isResolvedSingleBasePairALT() const{
return resolvedSingleBasePairALT;
}