File: vcf_entry_setters.cpp

package info (click to toggle)
vcftools 0.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,396 kB
  • sloc: perl: 10,233; cpp: 7,950; pascal: 751; makefile: 60; php: 43; sh: 12
file content (482 lines) | stat: -rw-r--r-- 11,449 bytes parent folder | download
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * vcf_entry_setters.cpp
 *
 *  Created on: Nov 11, 2009
 *      Author: Adam Auton
 *      ($Revision: 230 $)
 */

#include "vcf_entry.h"

void vcf_entry::set_CHROM(const string &in)
{
	CHROM = in;
}

void vcf_entry::set_POS(const int in)
{
	POS = in;
}

void vcf_entry::set_ID(const string &in)
{
	ID = in;
}

void vcf_entry::set_REF(const string &in)
{
	REF = in;
}

void vcf_entry::set_ALT(const string &in)
{
	istringstream ss(in);
	string tmpstr;
	ALT.resize(0);
	while(!ss.eof())
	{
		getline(ss, tmpstr, ',');
		add_ALT_allele(tmpstr);
	}
	parsed_ALT = true;
}

void vcf_entry::add_ALT_allele(const string &in)
{
	if (in != ".")
	{
		if (find(ALT.begin(), ALT.end(),in) == ALT.end())
		{
			ALT.push_back(in);
		}
	}
	parsed_ALT = true;
}

void vcf_entry::set_QUAL(const double in)
{
	QUAL = in;
}

void vcf_entry::add_FILTER_entry(const string &in)
{
	if ((in != "PASS") && (in != "."))
		if (find(FILTER.begin(), FILTER.end(), in) == FILTER.end())
			FILTER.push_back(in);
	sort(FILTER.begin(), FILTER.end());
	parsed_FILTER = true;
}

void vcf_entry::set_FORMAT(const string &in)
{
	FORMAT.resize(0);
	FORMAT_to_idx.clear();

	if (in.size() > 0)
	{
		istringstream ss(in);
		string tmpstr;

		unsigned int pos=0;
		while(!ss.eof())
		{
			getline(ss, tmpstr, ':');
			add_FORMAT_entry(tmpstr, pos);
			pos++;
		}
	}

	GT_idx = -1;
	GQ_idx = -1;
	DP_idx = -1;
	FT_idx = -1;

	if (FORMAT_to_idx.find("GT") != FORMAT_to_idx.end())
		GT_idx = FORMAT_to_idx["GT"];
	if (FORMAT_to_idx.find("GQ") != FORMAT_to_idx.end())
		GQ_idx = FORMAT_to_idx["GQ"];
	if (FORMAT_to_idx.find("DP") != FORMAT_to_idx.end())
		DP_idx = FORMAT_to_idx["DP"];
	if (FORMAT_to_idx.find("FT") != FORMAT_to_idx.end())
		FT_idx = FORMAT_to_idx["FT"];

	parsed_FORMAT = true;
}

void vcf_entry::add_FORMAT_entry(const string &in, unsigned int pos)
{
	FORMAT.push_back(in);
	FORMAT_to_idx[in] = pos;
}

// The following function reads in a genotype from a '0/1'-like string.
// Should handle haploid types to, but NOT polyploidy.
void vcf_entry::set_indv_GENOTYPE_and_PHASE(unsigned int indv, const string &in)
{
	ploidy.resize(N_indv);
	if ((in.size() == 3) && ((in.c_str()[1] == '/') || (in.c_str()[1] == '|')))
	{	// Fast, diploid case...
		ploidy[indv] = 2;
		set_indv_PHASE(indv, in.c_str()[1]);
		set_indv_GENOTYPE_alleles(indv, in.c_str()[0], in.c_str()[2]);
	}
	else
	{	// More complex case...
		size_t pos = in.find_first_of("/|");
		if (pos != string::npos)
		{	// autosome
			ploidy[indv] = 2;
			set_indv_PHASE(indv, in[pos]);
			set_indv_GENOTYPE_alleles(indv, make_pair(in.substr(0,pos), in.substr(pos+1)));
		}
		else
		{	// Male chrX, or chrY
			ploidy[indv] = 1;
			set_indv_PHASE(indv, '|');
			set_indv_GENOTYPE_alleles(indv, make_pair(in.substr(0,pos), "."));
		}

		// Check for polypoidy
		size_t pos2 = in.find_last_of("/|");
		if (pos != pos2)
			LOG.error("Polypolidy found, and not supported by vcftools: " + CHROM + ":" + int2str(POS));
	}

	parsed_GT[indv] = true;
}

void vcf_entry::set_indv_GENOTYPE_and_PHASE(unsigned int indv, const pair<int, int> &genotype, char phase)
{
	set_indv_GENOTYPE_ids(indv, genotype);
	set_indv_PHASE(indv, phase);
	parsed_GT[indv] = true;
}

void vcf_entry::set_indv_GENOTYPE_and_PHASE(unsigned int indv, const pair<string, string> &genotype, char phase)
{
	set_indv_GENOTYPE_alleles(indv, genotype);
	set_indv_PHASE(indv, phase);
	parsed_GT[indv] = true;
}

void vcf_entry::set_indv_GENOTYPE_alleles(unsigned int indv, const pair<string, string> &in)
{
	if (GENOTYPE.size() == 0)
		GENOTYPE.resize(N_indv, make_pair(-1,-1));

	pair<int, int> a(-1,-1);

	if (in.first != ".")
		a.first = str2int(in.first);

	if (in.second != ".")
		a.second = str2int(in.second);

	GENOTYPE[indv] = a;
	parsed_GT[indv] = true;
}

void vcf_entry::set_indv_GENOTYPE_alleles(unsigned int indv, char a1, char a2)
{
	if (GENOTYPE.size() == 0)
		GENOTYPE.resize(N_indv, make_pair(-1,-1));

	pair<int, int> a(-1,-1);

	if (a1 != '.')
		a.first = a1 - '0';

	if (a2 != '.')
		a.second = a2 - '0';

	GENOTYPE[indv] = a;
	parsed_GT[indv] = true;
}


void vcf_entry::set_indv_GENOTYPE_ids(unsigned int indv, const pair<int, int> &in)
{
	if (GENOTYPE.size() == 0)
		GENOTYPE.resize(N_indv, make_pair(-1,-1));
	GENOTYPE[indv] = in;
}

void vcf_entry::set_indv_PHASE(unsigned int indv, char in)
{
	if (PHASE.size() == 0)
		PHASE.resize(N_indv, '/');

	PHASE[indv] = in;
	parsed_GT[indv] = true;
}

void vcf_entry::set_indv_GQUALITY(unsigned int indv, double in)
{
	parsed_GQ[indv] = true;
	if (in == -1)
		return;
	if (GQUALITY.size() == 0)
		GQUALITY.resize(N_indv, -1);

	if (in > 99)
		in = 99;
	GQUALITY[indv] = in;
}

void vcf_entry::set_indv_DEPTH(unsigned int indv, int in)
{
	parsed_DP[indv] = true;
	if (in == -1)
		return;
	if (DEPTH.size() == 0)
		DEPTH.resize(N_indv, -1);

	DEPTH[indv] = in;
}

void vcf_entry::add_indv_GFILTER(unsigned int indv, const string &in)
{
	if (GFILTER.size() == 0)
		GFILTER.resize(N_indv);

	if (in != ".")
		if (find(GFILTER[indv].begin(), GFILTER[indv].end(), in) == GFILTER[indv].end())
			GFILTER[indv].push_back(in);
	parsed_FT[indv] = true;
}

void vcf_entry::set_indv_GFILTER(unsigned int indv, const string &in)
{
	parsed_FT[indv] = true;
	if ((in.size() == 0) || (in == "."))
		return;

	if (GFILTER.size() == 0)
		GFILTER.resize(N_indv);

	GFILTER[indv].resize(0);

	static istringstream ss;
	static string ith_FILTER;
	ss.clear();
	ss.str(in);
	while (!ss.eof())
	{
		getline(ss, ith_FILTER, ';');

		if ((ith_FILTER.size()==0) || (ith_FILTER == "."))
			continue;	// Don't bother storing "unfiltered" state.

		GFILTER[indv].push_back(ith_FILTER);
	}
}

void vcf_entry::set_FILTER(const string &FILTER_str)
{
	FILTER.resize(0);
	passed_filters = false;
	if (FILTER_str == "PASS")
		passed_filters = true;
	else
	{
		if (FILTER_str != ".")
		{
			istringstream ss(FILTER_str);
			string ith_FILTER;
			while (!ss.eof())
			{
				getline(ss, ith_FILTER, ';');
				FILTER.push_back(ith_FILTER);
			}
		}
	}
	sort(FILTER.begin(), FILTER.end());
	parsed_FILTER = true;
}

void vcf_entry::set_INFO(const string &INFO_str)
{
	INFO.resize(0);
	if ((INFO_str.size() > 0) && (INFO_str != "."))
	{
		istringstream ss(INFO_str);
		string tmpstr;
		while(!ss.eof())
		{
			getline(ss, tmpstr, ';');

			istringstream ss2(tmpstr);
			getline(ss2, tmpstr, '=');
			pair<string, string> INFO_entry(tmpstr, ".");

			if (!ss2.eof())
			{	// If there is a value entry, read it now
				getline(ss2, tmpstr);
				INFO_entry.second = tmpstr;
			}
			else	// Otherwise, set it equal to 1
				INFO_entry.second = "1";

			INFO.push_back(INFO_entry);
		}
	}
	parsed_INFO = true;
}

void vcf_entry::add_INFO_descriptor(const string &in)
{
	size_t found=in.find("##INFO=");
	if (found!=string::npos)
	{	// Found an INFO descriptor
		size_t found_start=in.find_first_of("<");
		size_t found_end=in.find_last_of(">");
		string details = in.substr(found_start+1, found_end-found_start-1);
		Field_description I;

		vector<string> tokens;
		tokenize(details, ',', tokens);
		if (tokens.size() < 4)
			LOG.error("Expected 4 parts in INFO definition: " + in);

		vector<string> entry;
		tokenize(tokens[0], '=', entry);
		if (entry[0] == "ID") I.ID = entry[1];
		else LOG.error("Expected ID entry as first field in INFO description: " + in);

		tokenize(tokens[1], '=', entry);
		if (entry[0] == "Number")
		{	// TODO - handle 'A' and 'G' categories correctly.
			if ((entry[1] == "A") || (entry[1] == "G"))
				I.N_entries = -1;	// Currently just treat as missing.
			else
				I.N_entries =  str2int(entry[1]);
		}
		else LOG.error("Expected Number entry as second field in INFO description: " + in);

		tokenize(tokens[2], '=', entry);
		if (entry[0] == "Type")
		{
			if (entry[1] == "Integer") I.Type = Integer;
			else if ((entry[1] == "Float") || (entry[1] == "Numeric")) I.Type = Float;
			else if (entry[1] == "Character") I.Type = Character;
			else if (entry[1] == "String") I.Type = String;
			else if (entry[1] == "Flag")
			{
				I.Type = Flag;
				if (I.N_entries != 0) LOG.error("Flag Type must have 0 entries: " + in);
			}
			else LOG.error("Unknown Type in INFO meta-information: " + in);
		}
		else LOG.error("Expected Type entry as third field in INFO description: " + in);

		tokenize(tokens[3], '=', entry);
		if (entry[0] == "Description")
		{
			I.Description = entry[1];
			for (unsigned int i=4; i<tokens.size(); i++)
			{
				I.Description += "; " + tokens[i];
			}
		}
		else LOG.error("Expected Description entry as fourth field in INFO description: " + in);

		INFO_map[I.ID] = I;
	}
}

void vcf_entry::add_FILTER_descriptor(const string &in)
{
	size_t found=in.find("##FILTER=");
	if (found!=string::npos)
	{
		size_t found_start=in.find_first_of("<");
		size_t found_end=in.find_last_of(">");
		string details = in.substr(found_start+1, found_end-found_start-1);
		vector<string> tokens;
		tokenize(details, ',', tokens);

		if (tokens.size() < 2)
			LOG.error("Expected 2 parts in FILTER definition: " + in);

		string ID, Description;
		vector<string> entry;
		tokenize(tokens[0], '=', entry);
		if (entry[0] == "ID") ID = entry[1];
		else LOG.error("Expected ID as first field in FILTER description: " + in);

		tokenize(tokens[1], '=', entry);
		if (entry[0] == "Description")
		{
			Description = entry[1];
			for (unsigned int i=2; i<tokens.size(); i++)
			{
				Description += "; " + tokens[i];
			}
		}
		else
			LOG.error("Expected Description as second field in FILTER description: " + in);

		FILTER_map[ID] = Description;
	}
}

void vcf_entry::add_FORMAT_descriptor(const string &in)
{
	size_t found=in.find("##FORMAT=");
	if (found!=string::npos)
	{	// Found an FORMAT descriptor
		size_t found_start=in.find_first_of("<");
		size_t found_end=in.find_last_of(">");
		string details = in.substr(found_start+1, found_end-found_start-1);
		vector<string> tokens;
		tokenize(details, ',', tokens);
		Field_description I;

		if (tokens.size() < 4)
			LOG.error("Expected 4 parts in FORMAT definition: " + in);

		vector<string> entry;
		tokenize(tokens[0], '=', entry);
		if (entry[0] == "ID") I.ID = entry[1];
		else LOG.error("Expected ID entry as first field in FORMAT description: " + in);

		tokenize(tokens[1], '=', entry);
		if (entry[0] == "Number")
		{	// TODO - handle 'A' and 'G' categories correctly.
			if ((entry[1] == "A") || (entry[1] == "G"))
				I.N_entries = -1;	// Currently just treat as missing.
			else
				I.N_entries = str2int(entry[1]);
		}
		else LOG.error("Expected Number entry as second field in FORMAT description: " + in);

		tokenize(tokens[2], '=', entry);
		if (entry[0] == "Type")
		{
			if (entry[1] == "Integer") I.Type = Integer;
			else if ((entry[1] == "Float") || (entry[1] == "Numeric")) I.Type = Float;
			else if (entry[1] == "Character") I.Type = Character;
			else if (entry[1] == "String") I.Type = String;
			else if (entry[1] == "Flag")
			{
				I.Type = Flag;
				if (I.N_entries != 0) LOG.error("Flag Type must have 0 entries: " + in);
			}
			else LOG.error("Unknown Type in FORMAT meta-information: " + in);
		}
		else LOG.error("Expected Type entry as third field in FORMAT description: " + in);

		tokenize(tokens[3], '=', entry);
		if (entry[0] == "Description")
		{
			I.Description = entry[1];
			for (unsigned int i=4; i<tokens.size(); i++)
			{
				I.Description += "; " + tokens[i];
			}
		}
		else LOG.error("Expected Description entry as fourth field in FORMAT description: " + in);

		FORMAT_map[I.ID] = I;
	}
}