VcfRecord.cpp 3.3 KB
Newer Older
nkindlon's avatar
nkindlon committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * VcfRecord.cpp
 *
 *  Created on: May 1, 2013
 *      Author: nek3d
 */

#include "VcfRecord.h"
#include "SingleLineDelimTextFileReader.h"

bool VcfRecord::initFromFile(SingleLineDelimTextFileReader *fileReader)
{
nkindlon's avatar
nkindlon committed
13
	setFileIdx(fileReader->getFileIdx());
nkindlon's avatar
nkindlon committed
14
15
16
17
18
19
20
	fileReader->getField(0, _chrName);
	_chrId = fileReader->getCurrChromdId();
	fileReader->getField(1, _startPosStr);
	_startPos = str2chrPos(_startPosStr);
	_startPos--; // VCF is one-based. Here we intentionally don't decrement the string version,
	//because we'll still want to output the one-based number in the print methods, even though
	//internally we decrement the integer to comply with the 0-based format common to other records.
21
22
23
	fileReader->getField(4, _varAlt);
	fileReader->getField(3, _varRef);
	if (_varAlt[0] == '<') {
24
25
26
27
28
29
30
31
		// this is a structural variant. Need to parse the tags to find the endpos,
		// UNLESS it's an insertion.
		if (!(_varAlt[1] == 'I' && _varAlt[2] == 'N' && _varAlt[3] == 'S')) {
			_endPos = _startPos + fileReader->getVcfSVlen();
		} else {
			//for insertions, treat as zero-length records
			_endPos = _startPos;
		}
32
33
	} else {
		//endPos is just the startPos plus the length of the variant
34
		_endPos = _startPos + _varRef.size();
35
	}
nkindlon's avatar
nkindlon committed
36
37
38
39
	int2str(_endPos, _endPosStr);
	fileReader->getField(2, _name);
	fileReader->getField(5, _score);

40
41
	_plusFields.setNumOffsetFields(6);
	return _plusFields.initFromFile(fileReader);
nkindlon's avatar
nkindlon committed
42
43
44
45
46
}

void VcfRecord::clear()
{
	BedPlusInterval::clear();
nkindlon's avatar
nkindlon committed
47
	_varRef.release();
48
	_varAlt.release();
nkindlon's avatar
nkindlon committed
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
}

void VcfRecord::print(QuickString &outBuf) const {
	outBuf.append(_chrName);
	outBuf.append('\t');
	outBuf.append(_startPosStr);
	printOtherFields(outBuf);
}

void VcfRecord::print(QuickString &outBuf, int start, int end) const {
	outBuf.append(_chrName);
	outBuf.append('\t');
	outBuf.append(_startPosStr);
	printOtherFields(outBuf);
}

void VcfRecord::print(QuickString &outBuf, const QuickString & start, const QuickString & end) const {
	outBuf.append(_chrName);
	outBuf.append('\t');
	outBuf.append(_startPosStr);
	printOtherFields(outBuf);

}

void VcfRecord::printNull(QuickString &outBuf) const {
74
	outBuf.append(".\t-1\t.");
75
	for (int i= 2; i < _numPrintFields; i++) {
nkindlon's avatar
nkindlon committed
76
77
78
79
80
81
82
83
84
85
		outBuf.append("\t.");
	}
}

void VcfRecord::printOtherFields(QuickString &outBuf) const {
	outBuf.append('\t');
	outBuf.append(_name);
	outBuf.append('\t');
	outBuf.append(_varRef);
	outBuf.append('\t');
86
87
	outBuf.append(_varAlt);
	outBuf.append('\t');
nkindlon's avatar
nkindlon committed
88
	outBuf.append(_score);
89
	_plusFields.printFields(outBuf);
nkindlon's avatar
nkindlon committed
90
}
arq5x's avatar
arq5x committed
91
92
93
94

const QuickString &VcfRecord::getField(int fieldNum) const
{
	//a request for any of the first six fields will retrieve
95
	//chrom, start, name, varRef, varAlt, score,  in that order.
arq5x's avatar
arq5x committed
96
97
98
99
100
101
102
103
104
105
106

	switch (fieldNum) {
	case 1:
		return _chrName;
		break;
	case 2:
		return _startPosStr;
		break;
	case 3:
		return _name;
	case 4:
107
		return _varRef;
arq5x's avatar
arq5x committed
108
109
		break;
	case 5:
110
		return _varAlt;
arq5x's avatar
arq5x committed
111
112
113
114
115
116
117
118
		break;
	case 6:
		return _score;
	default:
		return BedPlusInterval::getField(fieldNum);
		break;
	}
}
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

bool VcfRecord::isNumericField(int fieldNum) {

	switch (fieldNum) {
	case 1:
		return false; //_chrName;
		break;
	case 2:
		return true; //_startPosStr;
		break;
	case 3:
		return false; //_name;
	case 4:
		return false; //_varRef;
		break;
	case 5:
		return false; //_varAlt;
		break;
	case 6:
		return true; //_score;
	default:
		return BedPlusInterval::isNumericField(fieldNum);
		break;
	}
}