-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathPhage.h
More file actions
435 lines (385 loc) · 12.3 KB
/
Copy pathPhage.h
File metadata and controls
435 lines (385 loc) · 12.3 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
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
#ifndef PHAGE_H
#define PHAGE_H
#include "../default_mode/Symbiont.h"
#include "LysisWorld.h"
#include <optional>
class Phage: public Symbiont {
protected:
/**
* Purpose: Represents the time until lysis will be triggered.
*
*/
double burst_timer = 0;
/**
*
* Purpose: Represents if lysogeny is on.
*
*/
bool lysogeny = false;
/**
*
* Purpose: Represents the compatibility of the prophage to it's placement within the host's genome.
*
*/
double incorporation_val = 0.0;
/**
*
* Purpose: Represents the chance of lysis
*
*/
double chance_of_lysis = 1;
/**
*
* Purpose: Represents the chance of a prophage inducing to the lytic process
*
*/
double induction_chance = 1;
/**
*
* Purpose: Represents the world that the phage are living in.
*
*/
emp::Ptr<LysisWorld> my_world = NULL;
/**
*
* Purpose: Holds all configuration settings and points to same configuration
* object as my_config from superclass, but with the correct subtype.
*
*/
emp::Ptr<SymConfigLysis> lysis_config = NULL;
public:
/**
* The constructor for phage
*/
Phage(emp::Ptr<emp::Random> _random, emp::Ptr<LysisWorld> _world, emp::Ptr<SymConfigLysis> _config, double _intval=0.0, double _points = 0.0) : Symbiont(_random, _world, _config, _intval, _points) {
lysis_config = _config;
chance_of_lysis = lysis_config->LYSIS_CHANCE();
induction_chance = lysis_config->CHANCE_OF_INDUCTION();
incorporation_val = lysis_config->PHAGE_INC_VAL();
if (chance_of_lysis == -1) {
chance_of_lysis = random->GetDouble(0.0, 1.0);
}
if (induction_chance == -1) {
induction_chance = random->GetDouble(0.0, 1.0);
}
if (incorporation_val == -1) {
incorporation_val = random->GetDouble(0.0, 1.0);
}
my_world = _world;
}
/**
* Input: None
*
* Output: None
*
* Purpose: To force a copy constructor to be generated by the compiler.
*/
Phage(const Phage &) = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To force a move constructor to be generated by the compiler
*/
Phage(Phage &&) = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To tell the compiler to use its default generated variants of the constructor
*/
Phage() = default;
/**
* Input: None
*
* Output: Name of class as string, Phage
*
* Purpose: To know which subclass the object is
*/
std::string const GetName() const {
return "Phage";
}
/**Input: None
*
* Output: The double representing the phage's burst timer.
*
* Purpose: To get a phage's burst timer.
*/
double GetBurstTimer() const { return burst_timer; }
/**
* Input: None
*
* Output: None
*
* Purpose: To increment a phage's burst timer.
*/
void IncBurstTimer() {burst_timer += random->GetNormal(1.0, 1.0);}
/**
* Input: The double to be set as the phage's burst timer
*
* Output: None
*
* Purpose: To set a phage's burst timer.
*/
void SetBurstTimer(double _in) {burst_timer = _in;}
/**
* Input: None
*
* Output: The double representing a phage's change of lysis.
*
* Purpose: To determine a phage's chance of lysis.
*/
double GetLysisChance() const { return chance_of_lysis; }
/**
* Input: The double to be set as the phage's chance of lysis.
*
* Output: None
*
* Purpose: To set a phage's chance of lysis
*/
void SetLysisChance(double _in) {chance_of_lysis = _in;}
/**
* Input: None
*
* Output: The double representing a phage's incorporation value.
*
* Purpose: To determine a phage's incorporation value.
*/
double GetIncVal() const { return incorporation_val; }
/**
* Input: The double to be set as the phage's incorporation value.
*
* Output: None
*
* Purpose: To set a phage's incorporation value.
*/
void SetIncVal(double _in) {incorporation_val = _in;}
/**
* Input: None
*
* Output: The double representing a prophage's chance of induction.
*
* Purpose: To determine a lysogenic phage's chance of inducing
*/
double GetInductionChance() const { return induction_chance; }
/**
* Input: The double to be set as the phage's chance of induction
*
* Output:None
*
* Purpose: To set a phage's chance of inducing
*/
void SetInductionChance(double _in) {induction_chance = _in;}
/**
* Input: None
*
* Output: The bool representing if a phage will do lysogeny.
*
* Purpose: To determine if a phage is capable of lysogeny
*/
bool GetLysogeny() {return lysogeny;}
/**
* Input: None
*
* Output: The bool representing if an organism is a phage, always true.
*
* Purpose: To determine if an organism is a phage.
*/
bool IsPhage() const { return true; }
/**
* Input: None
*
* Output: None
*
* Purpose: To determine if a phage will choose lysis or lysogeny. If a phage chooses
* to be lytic, their interaction value will be -1 to represent them being antagonistic.
* If a phage chooses to be lysogenic, their interaction value will be 0 to represent
* them being neutral.
*/
void UponInjection() {
double rand_chance = random->GetDouble(0.0, 1.0);
if (rand_chance <= chance_of_lysis) {
lysogeny = false;
} else {
lysogeny = true;
}
}
/**
* Input: None
*
* Output: None
*
* Purpose: To mutate a phage's chance of lysis. The mutation will be based on a
* value chosen from a normal distribution centered at 0, with a standard
* deviation that is equal to the mutation size. Phage mutation can be
* on or off.
*/
void Mutate() {
Symbiont::Mutate();
double local_rate = lysis_config->MUTATION_RATE();
double local_size = lysis_config->MUTATION_SIZE();
if (random->GetDouble(0.0, 1.0) <= local_rate) {
//mutate chance of lysis/lysogeny, if enabled
if (lysis_config->MUTATE_LYSIS_CHANCE()) {
chance_of_lysis += random->GetNormal(0.0, local_size);
if (chance_of_lysis < 0) chance_of_lysis = 0;
else if (chance_of_lysis > 1) chance_of_lysis = 1;
}
if (lysis_config->MUTATE_INDUCTION_CHANCE()) {
induction_chance += random->GetNormal(0.0, local_size);
if (induction_chance < 0) induction_chance = 0;
else if (induction_chance > 1) induction_chance = 1;
}
if (lysis_config->MUTATE_INC_VAL()) {
incorporation_val += random->GetNormal(0.0, local_size);
if (incorporation_val < 0) incorporation_val = 0;
else if (incorporation_val > 1) incorporation_val = 1;
}
}
}
/**
* Input: None
*
* Output: The pointer to the newly created organism
*
* Purpose: To produce a new symbiont, identical to the original
*/
emp::Ptr<Organism> MakeNew() {
emp::Ptr<Phage> sym_baby = emp::NewPtr<Phage>(random, my_world, lysis_config, GetIntVal());
// pass down parent's genome
sym_baby->SetIncVal(GetIncVal());
sym_baby->SetLysisChance(GetLysisChance());
sym_baby->SetInductionChance(GetInductionChance());
sym_baby->SetInfectionChance(GetInfectionChance());
return sym_baby;
}
/**
* Input: location of the phage attempting to horizontally transmit
*
* Output: None
*
* Purpose: To burst host and release offspring
*/
void LysisBurst(emp::WorldPosition location) {
emp::vector<emp::Ptr<Organism>>& repro_syms = my_host->GetReproSymbionts();
//Record the burst size and count
emp::DataMonitor<double>& data_node_burst_size = my_world->GetBurstSizeDataNode();
data_node_burst_size.AddDatum(repro_syms.size());
emp::DataMonitor<int>& data_node_burst_count = my_world->GetBurstCountDataNode();
data_node_burst_count.AddDatum(1);
emp::DataMonitor<double, emp::data::Histogram>& data_node_attempts_horiztrans = my_world->GetHorizontalTransmissionAttemptCount();
emp::DataMonitor<double, emp::data::Histogram>& data_node_successes_horiztrans = my_world->GetHorizontalTransmissionSuccessCount();
for (size_t r=0; r<repro_syms.size(); r++) {
emp::WorldPosition new_pos = my_world->SymDoBirth(repro_syms[r], this, location);
//horizontal transmission data nodes
data_node_attempts_horiztrans.AddDatum(GetIntVal());
if (new_pos.IsValid()) {
data_node_successes_horiztrans.AddDatum(GetIntVal());
}
}
my_host->ClearReproSyms();
my_host->SetDead();
return;
}
/**
* Input: None
*
* Output: None
*
* Purpose: To allow lytic phage to produce offspring and increment the burst timer
*/
void LysisStep() {
IncBurstTimer();
if (lysis_config->SYM_LYSIS_RES() == 0) {
std::cout << "Lysis with a sym_lysis_res of 0 leads to an \
infinite loop, please change" << std::endl;
std::exit(1);
}
while(GetPoints() >= lysis_config->SYM_LYSIS_RES()) {
emp::Ptr<Organism> sym_baby = Reproduce();
my_host->AddReproSym(sym_baby);
SetPoints(GetPoints() - lysis_config->SYM_LYSIS_RES());
}
}
/**
* Input: A pointer to the baby host to have symbionts added.
*
* Output: None
*
* Purpose: To allow for vertical transmission to occur. lysogenic
* phage have 100% chance of vertical transmission, lytic phage have
* 0% chance
*/
std::optional<emp::Ptr<Organism>> VerticalTransmission(emp::Ptr<Organism> host_baby) {
bool success = false;
emp::Ptr<Organism> phage_baby;
//lysogenic phage have 100% chance of vertical transmission, lytic phage have 0% chance
if (lysogeny) {
phage_baby = Reproduce();
success = host_baby->AddSymbiont(phage_baby) > 0;
//vertical transmission data node
emp::DataMonitor<double, emp::data::Histogram>& data_node_attempts_verttrans = my_world->GetVerticalTransmissionAttemptCount();
data_node_attempts_verttrans.AddDatum(GetIntVal());
}
return (success) ? std::optional<emp::Ptr<Organism>>{phage_baby} : std::nullopt;
}
/**
* Input: The double representing the resources to be distributed to the phage
* and (optionally) the host from whom it comes; if no host is provided, the
* phage's host variable is used.
*
* Output: The double representing the resources that are left over from what
* was distributed to the phage.
*
* Purpose: To allow a phage to steal or use donated resources from their host.
*/
double ProcessResources(double host_donation, emp::Ptr<Organism> host = nullptr) {
if (host == nullptr) {
host = my_host;
}
if (lysogeny) {
if (lysis_config->BENEFIT_TO_HOST()) {
return host->ProcessLysogenResources(incorporation_val);
} else {
return 0;
}
}
else {
return Symbiont::ProcessResources(host_donation, host); //lytic phage do steal resources
}
}
/**
* Input: The worldposition representing the location of the phage being processed.
*
* Output: None
*
* Purpose: To process a phage, meaning check for reproduction, check for lysis, and move the phage.
*/
void Process(emp::WorldPosition location) {
if (lysis_config->LYSIS() && !GetHost().IsNull()) { //lysis enabled and phage is in a host
if (!lysogeny) { //phage has chosen lysis
if (GetBurstTimer() >= lysis_config->BURST_TIME() ) { //time to lyse!
LysisBurst(location);
}
else { //not time to lyse
LysisStep();
}
}
else if (lysogeny) { //phage has chosen lysogeny
double rand_chance = random->GetDouble(0.0, 1.0);
if (rand_chance <= induction_chance) {//phage has chosen to induce and turn lytic
lysogeny = false;
}
else if (random->GetDouble(0.0, 1.0) <= lysis_config->PROPHAGE_LOSS_RATE()) { //check if the phage's host should become susceptible again
SetDead();
}
}
}
else if (GetHost().IsNull() && lysis_config->FREE_LIVING_SYMS()) { //phage is free living
my_world->MoveFreeSym(location);
}
}
};
#endif