-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaplotypes_genotypes.hpp
468 lines (383 loc) · 10.4 KB
/
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
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
/**
*
* reHC-*
* Haplotyping with Recombinations, Errors, and Missing Genotypes
*
* Copyright (C) 2010,2011 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/>.
*
**/
/**
*
* haplotypes_genotypes.hpp
*
* Classes to represent haplotypes and genotypes.
*
**/
#ifndef __HAPLOTYPES_GENOTYPES_HPP__
#define __HAPLOTYPES_GENOTYPES_HPP__
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/static_assert.hpp>
#include "log.hpp"
#include "assertion.hpp"
#include "utility.hpp"
/**
*
*
*
* Representation of single-locus genotypes
*
*
*
**/
typedef unsigned int allele_t;
class single_multiallelic_genotype_t {
private:
allele_t _allele1;
allele_t _allele2;
public:
static const single_multiallelic_genotype_t MISS;
single_multiallelic_genotype_t()
:_allele1(0), _allele2(0)
{};
explicit single_multiallelic_genotype_t(const allele_t allele1,
const allele_t allele2)
:_allele1(std::min(allele1, allele2)),
_allele2(std::max(allele1, allele2))
{};
single_multiallelic_genotype_t(const single_multiallelic_genotype_t& g)
:_allele1(g._allele1), _allele2(g._allele2)
{};
allele_t allele1() const {
return _allele1;
};
allele_t allele2() const {
return _allele2;
};
void set_alleles(const allele_t allele1,
const allele_t allele2) {
if (((allele1 == 0)&&(allele2 != 0))||
((allele1 != 0)&&(allele2 == 0))) {
MY_FAIL;
}
_allele1= std::min(allele1, allele2);
_allele2= std::max(allele1, allele2);
};
};
bool
operator==(const single_multiallelic_genotype_t& g1,
const single_multiallelic_genotype_t& g2);
bool
operator!=(const single_multiallelic_genotype_t& g1,
const single_multiallelic_genotype_t& g2);
bool
operator<=(const single_multiallelic_genotype_t& g1,
const single_multiallelic_genotype_t& g2);
bool
operator<(const single_multiallelic_genotype_t& g1,
const single_multiallelic_genotype_t& g2);
std::ostream&
operator<<(std::ostream& out,
const single_multiallelic_genotype_t& g);
std::istream&
operator>>(std::istream& in,
single_multiallelic_genotype_t& g);
bool
is_genotyped(const single_multiallelic_genotype_t& g);
bool
is_homozygous(const single_multiallelic_genotype_t& g);
bool
is_heterozygous(const single_multiallelic_genotype_t& g);
/**
*
*
*
* Representation of single-locus haplotypes
*
*
*
**/
class single_multiallelic_haplotype_t {
private:
allele_t _allele;
public:
static const single_multiallelic_haplotype_t MISS;
single_multiallelic_haplotype_t()
:_allele(0)
{};
explicit single_multiallelic_haplotype_t(const allele_t allele)
:_allele(allele)
{};
single_multiallelic_haplotype_t(const single_multiallelic_haplotype_t& h)
:_allele(h._allele)
{};
allele_t allele() const {
return _allele;
};
static single_multiallelic_haplotype_t ALLELE(const allele_t allele) {
MY_ASSERT_DBG(allele != single_multiallelic_haplotype_t::MISS.allele());
return single_multiallelic_haplotype_t(allele);
};
};
bool
operator==(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2);
bool
operator!=(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2);
bool
operator<=(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2);
bool
operator<(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2);
std::ostream&
operator<<(std::ostream& out,
const single_multiallelic_haplotype_t& h);
std::istream&
operator>>(std::istream& in,
single_multiallelic_haplotype_t& h);
bool
is_missing(const single_multiallelic_haplotype_t& h);
const single_multiallelic_haplotype_t
homozygous_to_haplotype(const single_multiallelic_genotype_t& g);
bool
haplotype_genotype_consistent(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2,
const single_multiallelic_genotype_t& g);
bool
strict_haplotype_genotype_consistent(const single_multiallelic_haplotype_t& h1,
const single_multiallelic_haplotype_t& h2,
const single_multiallelic_genotype_t& g);
/**
*
*
*
* Representation of multi-locus genotypes and haplotype
*
*
*
**/
template <class base_t>
class default_base_writer_t {
private:
std::ostream& _out;
public:
default_base_writer_t(std::ostream& out)
: _out(out)
{}
std::ostream& operator()(const base_t& val) {
return (_out << val);
}
};
template <class base_t>
class default_base_reader_t {
private:
std::istream& _in;
public:
default_base_reader_t(std::istream& in)
: _in(in)
{}
std::istream& operator()(base_t& val) {
return (_in >> val);
}
};
template <class _base_t,
class _reader=default_base_reader_t<_base_t>,
class _writer=default_base_writer_t<_base_t> >
class generic_fixlen_vector_t {
public:
typedef _base_t base;
typedef _reader reader;
typedef _writer writer;
typedef base* iterator;
typedef const base* const_iterator;
private:
const size_t _len;
base* _v;
public:
generic_fixlen_vector_t(const size_t len)
:_len(len), _v(new base[len])
{}
generic_fixlen_vector_t(const std::vector<base> v)
:_len(v.size()), _v(new base[v.size()])
{
std::copy(v.begin(), v.end(), _v);
}
~generic_fixlen_vector_t() {
delete [] _v;
}
const base& operator[](const size_t pos) const {
MY_ASSERT_DBG(pos < _len);
return _v[pos];
}
base& operator[](const size_t pos) {
MY_ASSERT_DBG(pos < _len);
return _v[pos];
}
base const * begin() const {
return _v;
}
base* begin() {
return _v;
}
base const * end() const {
return _v+_len;
}
base * end() {
return _v+_len;
}
friend std::ostream& operator<<(std::ostream& out,
const generic_fixlen_vector_t& val) {
std::for_each(val._v, val._v+val._len, writer(out));
return out;
}
friend std::istream& operator>>(std::istream& in, generic_fixlen_vector_t& val) {
std::for_each(val._v, val._v+val._len, reader(in));
return in;
}
size_t size() const {
return _len;
};
bool is_compatible_with(const generic_fixlen_vector_t<_base_t,_reader,_writer>& v) const {
typedef generic_fixlen_vector_t<_base_t,_reader,_writer> v_t;
if (size() != v.size())
return false;
const typename v_t::base* v1it= begin();
const typename v_t::base* v2it= v.begin();
for (; v1it != end(); ++v1it, ++v2it) {
if ( (is_genotyped(*v1it)) &&
(is_genotyped(*v2it)) &&
(*v1it != *v2it) ) {
return false;
}
}
return true;
};
};
template <typename h_t, typename g_t>
bool
multilocus_haplotype_genotype_consistent(const h_t& h1,
const h_t& h2,
const g_t& g) {
MY_ASSERT_DBG(g.size() == h1.size());
MY_ASSERT_DBG(g.size() == h2.size());
const typename g_t::base* git= g.begin();
const typename h_t::base* h1it= h1.begin();
const typename h_t::base* h2it= h2.begin();
for (; git != g.end(); ++git, ++h1it, ++h2it) {
if (! haplotype_genotype_consistent(*h1it, *h2it, *git)) {
return false;
}
}
return true;
}
template <typename h_t, typename g_t>
bool
strict_multilocus_haplotype_genotype_consistent(const h_t& h1,
const h_t& h2,
const g_t& g) {
MY_ASSERT_DBG(g.size() == h1.size());
MY_ASSERT_DBG(g.size() == h2.size());
typename g_t::const_iterator git= g.begin();
typename h_t::const_iterator h1it= h1.begin();
typename h_t::const_iterator h2it= h2.begin();
for (; git != g.end(); ++git, ++h1it, ++h2it) {
if (! strict_haplotype_genotype_consistent(*h1it, *h2it, *git)) {
return false;
}
}
return true;
}
template <typename h_t>
int
strict_multilocus_mendelian_consistent(const h_t& hpf,
const h_t& hpm,
const h_t& h) {
MY_ASSERT_DBG(hpf.size() == hpm.size());
MY_ASSERT_DBG(hpf.size() == h.size());
typename h_t::const_iterator hpfit= hpf.begin();
typename h_t::const_iterator hpmit= hpm.begin();
typename h_t::const_iterator* its[]= { &hpfit, &hpmit };
typename h_t::const_iterator hit= h.begin();
int phase1= 0; int recomb1= 0;
int phase2= 1; int recomb2= 0;
int locus= 0;
for (; hit != h.end(); ++hpfit, ++hpmit, ++hit, ++locus) {
if (*hit != **its[phase1]) {
phase1= (phase1+1)%2;
if (*hit != **its[phase1]) {
return -locus;
} else {
++recomb1;
}
}
if (*hit != **its[phase2]) {
phase2= (phase2+1)%2;
if (*hit != **its[phase2]) {
return -locus;
} else {
++recomb2;
}
}
}
return std::min(recomb1, recomb2);
}
typedef generic_fixlen_vector_t<single_multiallelic_genotype_t> genotype_t;
typedef generic_fixlen_vector_t<single_multiallelic_haplotype_t> haplotype_t;
template <class _base_t,
class _reader,
class _writer>
bool operator==(const generic_fixlen_vector_t<_base_t,_reader,_writer>& v1,
const generic_fixlen_vector_t<_base_t,_reader,_writer>& v2) {
typedef generic_fixlen_vector_t<_base_t,_reader,_writer> v_t;
if (v1.size() != v2.size())
return false;
const typename v_t::base* v1it= v1.begin();
const typename v_t::base* v2it= v2.begin();
for (; v1it != v1.end(); ++v1it, ++v2it) {
if ( (*v1it) != (*v2it) ) {
return false;
}
}
return true;
};
template <class _base_t,
class _reader,
class _writer>
bool operator!=(const generic_fixlen_vector_t<_base_t,_reader,_writer>& v1,
const generic_fixlen_vector_t<_base_t,_reader,_writer>& v2) {
typedef generic_fixlen_vector_t<_base_t,_reader,_writer> v_t;
if (v1.size() != v2.size())
return true;
const typename v_t::base* v1it= v1.begin();
const typename v_t::base* v2it= v2.begin();
for (; v1it != v1.end(); ++v1it, ++v2it) {
if ( (*v1it) != (*v2it) ) {
return true;
}
}
return false;
};
#endif // __HAPLOTYPES_GENOTYPES_HPP__