File: VariantFile.hpp

package info (click to toggle)
dindel 1.01%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 940 kB
  • ctags: 1,130
  • sloc: cpp: 15,773; makefile: 40
file content (304 lines) | stat: -rw-r--r-- 7,402 bytes parent folder | download | duplicates (2)
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
/*    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
 * VariantFile.hpp
 *
 *  Created on: Sep 9, 2009
 *      Author: caa
 */

#ifndef VARIANTFILE_HPP_
#define VARIANTFILE_HPP_

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "Utils.hpp"
#include "Variant.hpp"
using namespace std;

/*
 * class used as input for realignment
 */

class AlignedCandidates
{
public:
	AlignedCandidates()
	{
		tid="";
	}
	AlignedCandidates(const string & _tid, const vector<AlignedVariant> & _variants, int _leftPos, int _rightPos)
	{
		tid=_tid;
		variants=_variants;
		leftPos = _leftPos;
		rightPos = _rightPos;
		computePositions();
	}
	vector<AlignedVariant> variants;
	string tid;
	int centerPos, leftPos, rightPos;
	const AlignedVariant * findVariant(int pos, int type, const string & str) const
	{
		//cout << " ******** " << endl;
		for (size_t x=0;x<variants.size();x++) {
			//cout << "pos: " << pos << " variants[x].pos: " << variants[x].getStartHap() << " " << variants[x].getString() << endl;
			if (variants[x].isEqual(pos, type, str)) return (const AlignedVariant *) &(variants[x]);
		}
	//	cout << " ******** " << endl;
		return NULL;
	}
private:
	void computePositions()
	{
		centerPos = leftPos+(rightPos-leftPos)/2;
	}
};


class VariantFile
{
public:
	class Candidates
	{
	public:
		Candidates()
		{
			tid="";
			pos=0;
			prior=-1.0;
		}
		Candidates(const string & _tid, uint32_t _pos, double _prior, const vector<Variant> & _variants, const vector<double> & _freqs)
		{
			tid=_tid;
			pos=_pos;
			prior=_prior;
			variants=_variants;
			freqs=_freqs;
		}
		vector<Variant> variants;
		vector<double> freqs;
		double prior;
		string tid;
		uint32_t pos;
	};


public:
	VariantFile(const string & fileName)
	{
		index=0;
		isOpen=false;
		open(fileName);
	}

	int open(const string & fileName)
	{
		fin.open(fileName.c_str());
		if (!fin.is_open()) throw string("Cannot open variant file ").append(fileName);
		isOpen=true;
		return 0;
	}

	bool eof() { if (isOpen) return fin.eof(); else return true; };

	Candidates getLine(bool isOneBased=false)
	{
		if (!isOpen) return empty;

		uint32_t pos;
		string tid;
		double prior=-1.0;

		string line;
		getline(fin, line);
		if (line.empty()) return empty;

		istringstream is(line);

		index++;

		if (!is.eof()) is >> tid; else return empty;
		if (!is.eof()) is >> pos; else return empty;

		// convert to zero-based coordinates
		if (isOneBased) pos--;

		// get variants from line
		vector<Variant> variants;
		vector<double> freqs;

		string col;

		try {
			while (!is.eof()) {
				is >> col;
				if ( col.size() && ( (col[0]!='-' && col[0] != '+' && col[0] != 'A' && col[0] != 'C' && col[0]!='G' && col[0]!='T' && col[0]!='R') ) ) break;
				Variant variant(col);
				if (variant.getSeq().size()!=0) variants.push_back(variant);
			}
		} catch (string err) {
			cerr <<  "Could not parse variants in line " << index << " in variants file." << endl;
			return empty;
		}

		if (col.find('#') != string::npos) return Candidates(tid, pos, prior, variants, freqs);

		prior=from_string<double>(prior, col, std::dec);

		bool error=false;
		while (!is.eof()) {
			string in;
			is >> in;

			if (in.find('#')!=string::npos) break;
			double freq;
			if (!from_string<double>(freq, in, std::dec)) { error=true; break; };
			freqs.push_back(freq);
		}

		if (error || (!error && freqs.size()>0 && freqs.size()!=variants.size())) {
			freqs.clear();
			cerr << "Could not parse all frequencies in line " << index << " in variants file." << endl;
		}

		if (variants.size()==0) {
			cerr << "Could not parse any variants in line: " << index << " SKIPPING." << endl;
			return empty;
		}

		return Candidates(tid, pos, prior, variants, freqs);
	}

	AlignedCandidates getLineVector(bool isOneBased=false)
	{
		if (!isOpen) return aligned_empty;

		uint32_t pos;
		int leftPos, rightPos;
		string tid;

		string line;
		getline(fin, line);
		if (line.empty()) return aligned_empty;

		istringstream is(line);

		index++;

		if (!is.eof()) is >> tid; else return aligned_empty;
		if (!is.eof()) {
			string str;
			is >> str;
			if (!from_string<int>(leftPos, str, std::dec)) throw string("Cannot read left boundary of region.");

		} else return aligned_empty;
		if (!is.eof()) {
			string str;
			is >> str;
			if (!from_string<int>(rightPos, str, std::dec)) throw string("Cannot read left boundary of region.");

		} else return aligned_empty;

		//cout << "leftPos: " << leftPos << " rightPos: " << rightPos << endl;

		// get variants from line
		vector<AlignedVariant> variants;
		vector<double> freqs;

		string col;

		try {
			while (!is.eof()) {
				string pvf_str;
				if (!is.eof()) is >> pvf_str;
				//cout << "pvf_str: " << pvf_str << endl;

				if (pvf_str.empty()) break;
				if (pvf_str[0]=='#' || pvf_str[0]=='%') break;

				vector<string> els;
				int lastpos=0;
				for (int x=0;x<int(pvf_str.size());x++) {
					if ((pvf_str[x]==';' || pvf_str[x]==',') && x-lastpos>0) {
						els.push_back(pvf_str.substr(lastpos,x-lastpos));
					//	cout << "els " << x << " : " << els[els.size()-1] << endl;
						lastpos=x+1;
					}
				}
				els.push_back(pvf_str.substr(lastpos, pvf_str.size()-lastpos));

				if (els.size()<2) {
					cerr << "Error reading line in variantfile!\n";
				} else {
					double freq=-1.0;
					bool addComb=false;
					if (!from_string<uint32_t>(pos, els[0], std::dec)) throw string("Cannot read position");
				// convert to zero-based coordinates
					if (isOneBased) pos--;

					string & col = els[1];
					if ( col.size()==0 || ( (col[0]!='-' && col[0] != '+' && col[0] != 'A' && col[0] != 'C' && col[0]!='G' && col[0]!='T' && col[0]!='R') ) ) throw string("Unrecognized variant");

					if (els.size()>2) {
						if (!from_string<double>(freq, els[2],std::dec)) throw string("Cannot read prior/frequency");
					}
					if (els.size()>3) {
						int addc;
						if (!from_string<int>(addc, els[3],std::dec)) throw string("Cannot add_combinatorial");
						if (addc) addComb = true;
					}

					AlignedVariant variant(col,pos, freq, addComb);
					if (variant.getSeq().size()!=0) {
						variants.push_back(variant);
					}
				}
				// split into pos, var, col


			}
		} catch (string err) {
			cerr <<  "Could not parse variants in line " << index << " in variants file." << endl;
			cerr <<  "Error: " << err << endl;
			return aligned_empty;
		}

		if (variants.size()==0) {
			cerr << "Could not parse any variants in line: " << index << " SKIPPING." << endl;
			return aligned_empty;
		}

		return AlignedCandidates(tid, variants, leftPos, rightPos);
	}

	~VariantFile()
	{
		fin.close();
	}

protected:
	ifstream fin;
	bool isOpen;
	Candidates empty;
	AlignedCandidates aligned_empty;
	int index;
};


#endif /* VARIANTFILE_HPP_ */