-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathped2cnf-constraints.hpp
340 lines (275 loc) · 8.17 KB
/
ped2cnf-constraints.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
/**
*
* 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/>.
*
**/
/**
*
* ped2cnf-constraints.cpp
*
* Classes that represent constraint handling.
*
**/
#ifndef __PED2CNF_CONSTRAINTS_HPP__
#define __PED2CNF_CONSTRAINTS_HPP__
#include "log.hpp"
#include "utility.hpp"
#include "pedcnf.hpp"
#include <vector>
#include <boost/utility.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
class constraint_handler_t
:boost::noncopyable {
public:
typedef std::vector< var_t > individual_variables_t;
typedef std::vector< individual_variables_t > individuals_variables_t;
private:
virtual void
_handle_constraints(pedcnf_t& cnf,
const individuals_variables_t& variables) const = 0;
protected:
my_logger logger;
const std::string _description;
public:
constraint_handler_t(const std::string& description="true variables")
:logger(get_my_logger("constraint-handler")), _description(description)
{};
virtual
~constraint_handler_t()
{};
void
handle_constraints(pedcnf_t& cnf,
const individuals_variables_t& variables) const;
};
class all_false_constraints_t
:public constraint_handler_t
{
private:
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
all_false_constraints_t(const std::string& description)
:constraint_handler_t(description)
{};
};
class at_most_individual_constraints_t
:public constraint_handler_t
{
private:
const double _rate;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
at_most_individual_constraints_t(const double rate,
const std::string& description="true variables")
:constraint_handler_t(description), _rate(rate)
{};
};
class at_most_global_constraints_t
:public constraint_handler_t
{
private:
const double _rate;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
at_most_global_constraints_t(const double rate,
const std::string& description="true variables")
:constraint_handler_t(description), _rate(rate)
{};
};
class at_most_individual_constraints_abs_t
:public constraint_handler_t
{
private:
const unsigned int _limit;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
at_most_individual_constraints_abs_t(const unsigned int limit,
const std::string& description="true variables")
:constraint_handler_t(description), _limit(limit)
{};
};
class at_most_global_constraints_abs_t
:public constraint_handler_t
{
private:
const unsigned int _limit;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
at_most_global_constraints_abs_t(const unsigned int limit,
const std::string& description="true variables")
:constraint_handler_t(description), _limit(limit)
{};
};
class interval_global_constraints_abs_t
:public constraint_handler_t
{
private:
const unsigned int _vmin;
const unsigned int _vmax;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
interval_global_constraints_abs_t(const unsigned int vmin, const unsigned int vmax,
const std::string& description="true variables")
:constraint_handler_t(description), _vmin(vmin), _vmax(vmax)
{};
};
class at_most_windowed_constraints_t
:public constraint_handler_t
{
private:
const unsigned int _max_true_in_window;
const unsigned int _window_length;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
at_most_windowed_constraints_t(const int max_true_in_window,
const int window_length,
const std::string& description="true variables")
:constraint_handler_t(description),
_max_true_in_window(max_true_in_window),
_window_length(window_length)
{};
};
class composite_constraints_t
:public constraint_handler_t
{
private:
boost::ptr_vector<constraint_handler_t> _handlers;
virtual void
_handle_constraints(pedcnf_t& cnf, const individuals_variables_t& variables) const;
public:
composite_constraints_t()
:constraint_handler_t("")
{};
void add(constraint_handler_t* handler) {
_handlers.push_back(handler);
}
};
class error_handler_t {
private:
typedef constraint_handler_t::individuals_variables_t individuals_variables_t;
typedef constraint_handler_t::individual_variables_t individual_variables_t;
const constraint_handler_t& _handler;
public:
error_handler_t(const constraint_handler_t& handler)
:_handler(handler)
{};
void handle_errors(pedcnf_t& cnf,
const size_t pedigree_size,
const size_t genotype_length) const {
individuals_variables_t variables;
for (size_t i= 0; i<pedigree_size; ++i) {
{
individual_variables_t ivs;
variables.push_back(ivs);
}
individual_variables_t& ivs= variables.back();
for (size_t l= 0; l<genotype_length; ++l) {
if (cnf.has_e(i, l)) {
ivs.push_back(cnf.get_e(i, l));
}
}
}
_handler.handle_constraints(cnf, variables);
};
};
class global_recombination_handler_t {
private:
typedef constraint_handler_t::individuals_variables_t individuals_variables_t;
typedef constraint_handler_t::individual_variables_t individual_variables_t;
const constraint_handler_t& _handler;
public:
global_recombination_handler_t(const constraint_handler_t& handler)
:_handler(handler)
{};
void handle_recombinations(pedcnf_t& cnf,
const size_t pedigree_size,
const size_t genotype_length) const {
individuals_variables_t variables;
// Recombinations in both haplotypes
for (size_t i= 0; i<pedigree_size; ++i) {
{
individual_variables_t ivs;
variables.push_back(ivs);
}
individual_variables_t& ivs= variables.back();
for (size_t l= 1; l<genotype_length; ++l) {
if (cnf.has_rp(i, l)) {
ivs.push_back(cnf.get_rp(i, l));
}
if (cnf.has_rm(i, l)) {
ivs.push_back(cnf.get_rm(i, l));
}
}
}
_handler.handle_constraints(cnf, variables);
};
};
class separated_recombination_handler_t {
private:
typedef constraint_handler_t::individuals_variables_t individuals_variables_t;
typedef constraint_handler_t::individual_variables_t individual_variables_t;
const constraint_handler_t& _handler;
public:
separated_recombination_handler_t(const constraint_handler_t& handler)
:_handler(handler)
{};
void handle_recombinations(pedcnf_t& cnf,
const size_t pedigree_size,
const size_t genotype_length) const {
individuals_variables_t variables;
// Recombinations in paternal haplotypes
for (size_t i= 0; i<pedigree_size; ++i) {
{
individual_variables_t ivs;
variables.push_back(ivs);
}
individual_variables_t& ivs= variables.back();
for (size_t l= 1; l<genotype_length; ++l) {
if (cnf.has_rp(i, l)) {
ivs.push_back(cnf.get_rp(i, l));
}
}
}
_handler.handle_constraints(cnf, variables);
// Recombinations in maternal haplotypes
for (size_t i= 0; i<pedigree_size; ++i) {
individual_variables_t& ivs= variables[i];
ivs.clear();
for (size_t l= 1; l<genotype_length; ++l) {
if (cnf.has_rm(i, l)) {
ivs.push_back(cnf.get_rm(i, l));
}
}
}
_handler.handle_constraints(cnf, variables);
};
};
#endif // __PED2CNF_CONSTRAINTS_HPP__