-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.cpp
205 lines (172 loc) · 7.61 KB
/
info.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
// ******************************************************
// vcfCTools (c) 2011 Alistair Ward
// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ------------------------------------------------------
// Last modified: 18 February 2011
// ------------------------------------------------------
// Tools for manipulating the information string.
// ******************************************************
#include "info.h"
using namespace std;
using namespace vcfCTools;
// Constructor
variantInfo::variantInfo(string& info) {
infoString = info;
};
// Destructor.
variantInfo::~variantInfo(void) {};
// If alleles have been removed, modify the info field so that all
// fields have the correct number of entries.
void variantInfo::modifyInfo(vector<int>& alleleIDs, vcfHeader& header) {
string modifiedInfo = "";
retrieveFields(header, true);
// Loop through each of the info fields and check for any fields that should
// contain values corresponding to the different alternate alleles.
infoIter = infoFields.begin();
for (; infoIter != infoFields.end(); infoIter++) {
if (infoIter->second.number == "A") {
vector<string>::iterator vIter = infoIter->second.values.begin();
vector<int>::iterator alleleIter = alleleIDs.begin();
// Skip the reference allele in the alleleIDs.
alleleIter++;
modifiedInfo += infoIter->first + "=";
for (; alleleIter != alleleIDs.end(); alleleIter++) {
if (*alleleIter != -1) {modifiedInfo += *vIter + ",";}
vIter++;
}
// Strip the trailing ",".
modifiedInfo = modifiedInfo.substr(0, modifiedInfo.size() - 1);
modifiedInfo += ";";
} else if (infoIter->second.number == "0") {
modifiedInfo += infoIter->first + ";";
} else {
modifiedInfo += infoIter->first + "=" + infoIter->second.values[0] + ";";
}
}
// Strip the trailing ";" and then replace the existing info string
// with the modified one.
modifiedInfo = modifiedInfo.substr(0, modifiedInfo.size() - 1);
infoString = modifiedInfo;
}
// Split the info string into its components and populate the arrays
// to store the information.
void variantInfo::retrieveFields(vcfHeader& header, bool terminate) {
string tag;
vector<string> infoArray = split(infoString, ";");
vector<string>::iterator infoIter = infoArray.begin();
for (; infoIter != infoArray.end(); infoIter++) {
// Split the entry on "=" to get the tag.
vector<string> fields = split(*infoIter, "=");
infoStruct info;
tag = fields[0];
// In order to process the info field, the header information is
// required. If this does not exist, terminate the program.
if (header.infoFields.count(tag) == 0) {
cerr << "WARNING: No header description for info tag: " << tag << endl;
if (terminate) {exit(1);}
}
info.number = header.infoFields[tag].number;
info.type = header.infoFields[tag].type;
if (fields.size() > 1) {
info.values = split(fields[1], ",");
info.numberValues = info.values.size();
} else {
info.values.push_back("");
info.numberValues = 0;
}
infoFields[tag] = info;
}
}
// Parse through the entire info string and ensure that all entries have an
// explanation in the header and that the number and entry types are
// consistent with this header information.
void variantInfo::validateInfo(vcfHeader& header, string& referenceSequence, int& position, unsigned int& noAlts, bool& error) {
int integerValue;
// Split the info field into its constituent parts.
retrieveFields(header, false);
for (infoIter = infoFields.begin(); infoIter != infoFields.end(); infoIter++) {
// If the info number = A, there should be as many values as there are
// alternate alleles. Check that this is the case.
if (infoIter->second.number == "A") {
if (infoIter->second.numberValues != noAlts) {
cerr << "ERROR: Incorrect number of entries at " << referenceSequence << ":";
cerr << position << ", in info field for: " << infoIter->first << endl;
error = true;
}
// Check the values conform to the expected types.
checkTypes(referenceSequence, position, error);
} else if (infoIter->second.number == "G") {
// If the info number = '.', any number of entries is acceptable, so do not
// check the number of values.
} else if (infoIter->second.number == ".") {
// Check the values conform to the expected types.
checkTypes(referenceSequence, position, error);
// If the info number = 0, then this info field is a flag. Check that there is
// only a single entry.
} else if (infoIter->second.number == "0") {
if (infoIter->second.numberValues != 0 || infoIter->second.type != "Flag") {
cerr << "ERROR: Incorrect number of entries or not marked as a flag at " << referenceSequence << ":";
cerr << position << ", in info field for: " << infoIter->first << "." << endl;
error = true;
}
// Check the values conform to the expected types.
checkTypes(referenceSequence, position, error);
// Finally, if a specified number of entries is given, check that this number
// is observed. Also check that the entry is itself an integer as there are no
// other allowed types.
} else {
// First check that the number is an integer.
integerValue = atoi( infoIter->second.number.c_str() );
if (integerValue == 0) {
cerr << "ERROR: Invalid type in info field: " << infoIter->first << " at " << referenceSequence;
cerr << ":" << position << "." << endl;
error = true;
}
// Now check that the correct number is observed.
if (infoIter->second.numberValues != integerValue) {
cerr << "ERROR: Incorrect number of entries at " << referenceSequence << ":";
cerr << position << ", in info field for: " << infoIter->first << endl;
error = true;
}
// Check the values conform to the expected types.
checkTypes(referenceSequence, position, error);
}
}
}
// Check that the observed values conform to the expected type.
void variantInfo::checkTypes(string& referenceSequence, int& position, bool& error) {
int integerValue;
float floatValue;
vector<string>::iterator sIter;
// Now check that the values are of the correct type.
sIter = infoIter->second.values.begin();
for (; sIter != infoIter->second.values.end(); sIter++) {
// Check that integers appear if integers are expected.
if (infoIter->second.type == "Integer") {
integerValue = atoi( sIter->c_str() );
if (integerValue == 0 && *sIter != "0") {
cerr << "ERROR: Expected integer in info field: " << infoIter->first << " at " << referenceSequence;
cerr << ":" << position << "." << endl;
error = true;
}
// If floating point values are expected, check that this is seen.
} else if (infoIter->second.type == "Float") {
floatValue = atof( sIter->c_str() );
if (floatValue == 0. && (*sIter != "0" && *sIter != "0." && *sIter != "0.0")) {
cerr << "ERROR: Expected floats in info field: " << infoIter->first << " at " << referenceSequence;
cerr << ":" << position << "." << endl;
error = true;
}
} else if (infoIter->second.type == "Flag") {
} else if (infoIter->second.type == "Character") {
// Check that the values are of length 1.
if (sIter->length() != 1) {
cerr << "ERROR: Expected a single character for info field: " << infoIter->first << "at";
cerr << referenceSequence << ":" << position << "." << endl;
error = true;
}
} else if (infoIter->second.type == "String") {
}
}
}