-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-haplotypes_genotypes.hpp
271 lines (228 loc) · 6.63 KB
/
io-haplotypes_genotypes.hpp
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
/**
*
* reHC-*
* Haplotyping with Recombinations, Errors, and Missing Genotypes
*
* Copyright (C) 2010 Yuri Pirola <yuri.pirola(-at-)gmail.com>
*
* Distributed under the terms of the GNU General Public License (GPL)
*
*
* This file is part of reHC-* (reHCstar),
* previously known as ZRHC-* (ZRHCstar).
*
* reHC-* 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.
*
* reHC-* 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 reHC-*. If not, see <http://www.gnu.org/licenses/>.
*
**/
/**
*
* io-haplotypes_genotypes.hpp
*
* Classes to represent readers and writers of genotypes and haplotypes.
*
**/
#ifndef __IO_HAPLOTYPES_GENOTYPES_HPP__
#define __IO_HAPLOTYPES_GENOTYPES_HPP__
#include "haplotypes_genotypes.hpp"
#include "log.hpp"
#include <sstream>
#include <vector>
#include <string>
template <typename T_BASE_GENOTYPE>
class basic_genotype_reader_t
:public log_able_t< basic_genotype_reader_t< T_BASE_GENOTYPE > >
{
protected:
virtual bool decode_next(std::istream& genotype_stream,
std::vector<T_BASE_GENOTYPE>& v) = 0;
public:
std::vector<T_BASE_GENOTYPE> decode(const std::string& genotype) {
std::istringstream is(genotype);
return decode(is);
}
std::vector<T_BASE_GENOTYPE> decode(std::istream& genotype_stream) {
std::vector<T_BASE_GENOTYPE> v;
while (decode_next(genotype_stream, v)) {
// do nothing
}
return v;
}
};
class multiallelic_genotype_reader_t
:public basic_genotype_reader_t< single_multiallelic_genotype_t >
{
protected:
typedef single_multiallelic_genotype_t g_t;
virtual bool
decode_next(std::istream& genotype_stream,
std::vector<g_t>& v) {
bool ris= true;
g_t g;
genotype_stream >> g;
if (genotype_stream) {
v.push_back(g);
} else {
if (!genotype_stream.eof()) {
L_WARN("Error while decoding the genotype.");
}
ris= false;
}
return ris;
}
};
template <typename T_BASE>
class basic_vector_writer_t
:public log_able_t< basic_vector_writer_t< T_BASE > >
{
protected:
virtual void encode_next(std::ostream& vector_stream,
const T_BASE& v,
const std::string& sep= " ") const = 0;
public:
template <typename T>
std::string encode(const T& begin,
const T& end,
const std::string& sep= " ") const {
std::ostringstream os;
encode(os, begin, end, sep);
return os.str();
}
template <typename T>
void encode(std::ostream& vector_stream,
const T& begin,
const T& end,
const std::string& sep= " ") const {
for(T it= begin; it != end; ++it) {
if (it != begin) {
vector_stream << sep;
}
encode_next(vector_stream, *it, sep);
}
}
template <typename T>
std::string encode(const T& vector,
const std::string& sep= " ") const {
return encode(vector.begin(), vector.end(), sep);
}
template <typename T>
void encode(std::ostream& vector_stream,
const T& vector,
const std::string& sep= " ") const {
encode(vector_stream, vector.begin(), vector.end(), sep);
}
};
class multiallelic_genotype_writer_t
:public basic_vector_writer_t< single_multiallelic_genotype_t >
{
protected:
typedef single_multiallelic_genotype_t g_t;
virtual void encode_next(std::ostream& genotype_stream,
const g_t& g,
const std::string& sep= " ") const {
genotype_stream << g.allele1() << sep << g.allele2();
};
};
class multiallelic_haplotype_writer_t
:public basic_vector_writer_t< single_multiallelic_haplotype_t >
{
protected:
typedef single_multiallelic_haplotype_t h_t;
virtual void encode_next(std::ostream& haplotype_stream,
const h_t& h,
const std::string& sep= " ") const {
haplotype_stream << h.allele();
};
};
template <typename T_BASE>
class basic_double_vector_writer_t
:public basic_vector_writer_t< T_BASE >
{
protected:
virtual void encode_next(std::ostream& vector_stream,
const T_BASE& v1,
const T_BASE& v2,
const std::string& insep= "|") const = 0;
public:
template <typename T>
std::string encode(const T& begin1,
const T& end1,
const T& begin2,
const T& end2,
const std::string& outsep= " ",
const std::string& insep= "|") const {
std::ostringstream os;
encode(os, begin1, end1, begin2, end2, outsep, insep);
return os.str();
}
template <typename T>
void encode(std::ostream& vector_stream,
const T& begin1,
const T& end1,
const T& begin2,
const T& end2,
const std::string& outsep= " ",
const std::string& insep= "|") const {
T it1= begin1;
T it2= begin2;
for(; it1 != end1; ++it1, ++it2) {
MY_ASSERT_DBG( it2 != end2 );
if (it1 != begin1) {
MY_ASSERT_DBG( it2 != begin2 );
vector_stream << outsep;
}
encode_next(vector_stream, *it1, *it2, insep);
}
MY_ASSERT_DBG( (it1 == end1) && (it2 == end2) );
}
template <typename T>
std::string encode(const T& vector1,
const T& vector2,
const std::string& outsep= " ",
const std::string& insep= "|") const {
return encode(vector1.begin(), vector1.end(),
vector2.begin(), vector2.end(),
outsep, insep);
}
template <typename T>
void encode(std::ostream& vector_stream,
const T& vector1,
const T& vector2,
const std::string& outsep= " ",
const std::string& insep= "|") const {
encode(vector_stream,
vector1.begin(), vector1.end(),
vector2.begin(), vector2.end(),
outsep, insep);
}
};
class multiallelic_haplotype_pair_writer_t
:public basic_double_vector_writer_t< single_multiallelic_haplotype_t >
{
protected:
typedef single_multiallelic_haplotype_t h_t;
virtual void encode_next(std::ostream& haplotype_stream,
const h_t& h,
const std::string& sep= " ") const {
haplotype_stream << h.allele();
};
virtual void encode_next(std::ostream& haplotype_stream,
const h_t& h1,
const h_t& h2,
const std::string& insep= "|") const {
encode_next(haplotype_stream, h1, "");
haplotype_stream << insep;
encode_next(haplotype_stream, h2, "");
};
};
#endif // __IO_HAPLOTYPES_GENOTYPES_HPP__