-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeciesTrait.cpp
More file actions
299 lines (286 loc) · 7.96 KB
/
SpeciesTrait.cpp
File metadata and controls
299 lines (286 loc) · 7.96 KB
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
#include "SpeciesTrait.h"
// Species trait constructor
SpeciesTrait::SpeciesTrait(
const TraitType& trType,
const sex_t& sx,
const set<int>& pos,
const ExpressionType& expr,
const set<int>& initialPositions,
const DistributionType& initDist,
const map<GenParamType, float> initParams,
const DistributionType& initDomDist,
const map<GenParamType, float> initDomParams,
bool isInherited,
const float& mutRate,
const DistributionType& mutationDist,
const map<GenParamType, float> mutationParams,
const DistributionType& dominanceDist,
const map<GenParamType, float> dominanceParams,
const int nPloidy,
const bool isOutput) :
traitType{ trType },
sex{ sx },
genePositions{ pos },
expressionType{ expr },
initPositions{ initialPositions },
initialDistribution{ initDist },
initialParameters{ initParams },
initialDomDistribution{ initDomDist },
initialDomParameters{ initDomParams },
dominanceDistribution{ dominanceDist },
dominanceParameters{ dominanceParams },
inherited{ isInherited },
mutationDistribution{ mutationDist },
mutationParameters{ mutationParams },
mutationRate{ mutRate },
ploidy{ nPloidy },
traitIsOutput{ isOutput }
{
// Check distribution parameters
// Initial allele distribution
for (auto [paramType, paramVal] : initParams) {
switch (paramType)
{
case MIN: case MAX: case MEAN:
if (!isValidTraitVal(paramVal))
throw logic_error("Invalid parameter value: initial parameter " + to_string(paramType) + " must have a valid value for trait " + to_string(traitType) + ".");
break;
case SD:
if (paramVal <= 0.0)
throw logic_error("Invalid parameter value: initial parameter " + to_string(paramType) + " must be strictly positive");
break;
default:
break;
}
}
// Initial dominance distribution
for (auto [paramType, paramVal] : initDomParams) {
switch (paramType)
{
case MIN: case MAX: case MEAN:
if (paramVal < 0.0)
throw logic_error("Invalid parameter value: initial dominance parameter " + to_string(paramType) + " must not be negative.");
break;
case SD: case SHAPE: case SCALE:
if (paramVal <= 0.0)
throw logic_error("Invalid parameter value: initial dominance parameter " + to_string(paramType) + " must be strictly positive");
break;
default:
break;
}
}
// Mutation distribution
for (auto [paramType, paramVal] : mutationParams) {
switch (paramType)
{
case MIN: case MAX: case MEAN:
if (
(trType == NEUTRAL || trType == GENETIC_LOAD || trType == GENETIC_LOAD1 ||
trType == GENETIC_LOAD2 || trType == GENETIC_LOAD3 || trType == GENETIC_LOAD4 || trType == GENETIC_LOAD5)
&& !isValidTraitVal(paramVal)
// dispersal traits are cumulative so no value is invalid
)
throw logic_error("Invalid parameter value: mutation parameter " + to_string(paramType) + " must have a valid value for trait " + to_string(traitType) + ".");
break;
case SD: case SHAPE: case SCALE:
if (paramVal <= 0.0)
throw logic_error("Invalid parameter value: mutation parameter " + to_string(paramType) + " must be strictly positive");
break;
default:
break;
}
}
// Dominance distribution
for (auto [paramType, paramVal] : dominanceParams) {
switch (paramType)
{
case MIN: case MAX: case MEAN:
if (paramVal < 0.0)
throw logic_error("Invalid parameter value: dominance parameter " + to_string(paramType) + " must not be negative.");
break;
case SD: case SHAPE: case SCALE:
if (paramVal <= 0.0)
throw logic_error("Invalid parameter value: dominance parameter " + to_string(paramType) + " must be strictly positive");
break;
default:
break;
}
}
}
bool SpeciesTrait::isValidTraitVal(const float& val) const {
switch (traitType)
{
// Neutral trait
case NEUTRAL: // only need to check for input parameters
{
return val >= 0.0 && val <= 255.0;
}
// Genetic Load
case GENETIC_LOAD: case GENETIC_LOAD1: case GENETIC_LOAD2: case GENETIC_LOAD3: case GENETIC_LOAD4: case GENETIC_LOAD5:
{
return val >= -1.0 // genetic fitness traits can be beneficial
&& val <= 1.0;
break;
}
// Dispersal traits
/// Emigration
case E_D0_F: case E_D0_M: case E_D0: {
return val >= 0.0 && val <= 1.0; // is a probability
break;
}
case E_ALPHA_F: case E_ALPHA_M: case E_ALPHA:
case E_BETA_F: case E_BETA_M: case E_BETA:
{
return true; // slope and inflexion point can be any value
break;
}
/// Settlement
case S_S0_F: case S_S0_M: case S_S0:
{
return val >= 0.0 && val <= 1.0;
break;
}
case S_ALPHA_F: case S_ALPHA_M: case S_ALPHA:
case S_BETA_F: case S_BETA_M: case S_BETA:
{
return true; // slope and inflection point can be any value
break;
}
/// Transfer - Kernels
case KERNEL_MEANDIST_1_F: case KERNEL_MEANDIST_1_M: case KERNEL_MEANDIST_1:
case KERNEL_MEANDIST_2_F: case KERNEL_MEANDIST_2_M: case KERNEL_MEANDIST_2:
{
return val >= 0.0; // is a distance
break;
}
case KERNEL_PROBABILITY_F: case KERNEL_PROBABILITY_M: case KERNEL_PROBABILITY:
{
return val >= 0.0 && val <= 1.0;
break;
}
/// Transfer - Correlated random walk
case CRW_STEPLENGTH:
{
return val >= 0.0;
break;
}
case CRW_STEPCORRELATION:
{
return val >= 0.0 && val <= 1.0;
break;
}
/// Transfer - Stochastic Movement Simulator
case SMS_DP:
{
return val >= 1.0; // according to parameter doc
break;
}
case SMS_GB:
{
return val >= 1.0; // according to parameter doc
break;
}
case SMS_ALPHADB:
{
return val > 0.0;
break;
}
case SMS_BETADB:
{
return true;
break;
}
default:
throw logic_error("Invalid trait type " + to_string(traitType) + " passed to isValidTraitVal().");
break;
}
}
#ifdef UNIT_TESTS // Testing only
// Create a default set of gene positions ranging from zero to genome size
set<int> createTestGenePositions(const int genomeSz) {
set<int> genePositions;
for (int i = 0; i < genomeSz; i++) genePositions.insert(i);
return genePositions;
}
SpeciesTrait* createTestEmigSpTrait(const set<int>& genePositions, const bool& isDiploid) {
// Create species trait
const map<GenParamType, float> distParams{
pair<GenParamType, float>{GenParamType::MIN, 0.0},
pair<GenParamType, float>{GenParamType::MAX, 1.0}
};
SpeciesTrait* spTr = new SpeciesTrait(
TraitType::E_D0,
sex_t::NA,
genePositions,
ExpressionType::ADDITIVE,
genePositions,
DistributionType::UNIFORM,
distParams,
DistributionType::NONE, // no dominance
distParams,
true, // isInherited
0.0, // mutation rate
DistributionType::UNIFORM,
distParams,
DistributionType::NONE, // no dominance
distParams,
isDiploid ? 2 : 1,
false
);
return spTr;
}
SpeciesTrait* createTestGenLoadTrait(const set<int>& genePositions, const bool& isDiploid) {
// Create species trait
const map<GenParamType, float> distParams{
pair<GenParamType, float>{GenParamType::MIN, 0.0},
pair<GenParamType, float>{GenParamType::MAX, 1.0}
};
SpeciesTrait* spTr = new SpeciesTrait(
TraitType::GENETIC_LOAD,
sex_t::NA,
genePositions,
ExpressionType::MULTIPLICATIVE,
genePositions,
DistributionType::NONE,
distParams,
DistributionType::NONE, // initialise dominance to zero
distParams,
true, // isInherited
0.0, // mutation rate
DistributionType::UNIFORM,
distParams,
DistributionType::UNIFORM,
distParams,
isDiploid ? 2 : 1,
false
);
return spTr;
}
SpeciesTrait* createTestNeutralSpTrait(const float& maxAlleleVal, const set<int>& genePositions, const bool& isDiploid) {
const map<GenParamType, float> distParams{
// Set max allele value
pair<GenParamType, float>{GenParamType::MAX, maxAlleleVal}
};
SpeciesTrait* spTr = new SpeciesTrait(
TraitType::NEUTRAL,
sex_t::NA,
genePositions,
ExpressionType::NOTEXPR,
genePositions,
// Sample initial values from uniform(0, max)
DistributionType::UNIFORM, distParams,
DistributionType::NONE, // No dominance
map<GenParamType, float>{},
true, // isInherited
0.0, // mutation rate
// Mutation sampled from a uniform(0, max)
DistributionType::KAM,
distParams,
DistributionType::NONE, // No dominance
map<GenParamType, float>{},
isDiploid ? 2 : 1,
false
);
return spTr;
}
#endif // UNIT_TESTS