/
Person.cpp
425 lines (351 loc) · 14.9 KB
/
Person.cpp
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
// Person.cpp
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "Person.h"
#include "Community.h"
#include "Parameters.h"
using namespace dengue::standard;
int Person::_nNextID = 0;
const Parameters* Person::_par;
Person::Person() {
_nID = _nNextID++;
_nAge = -1;
_nLifespan = -1;
_nHomeID = -1;
_nWorkID = -1;
for(int i=0; i<(int) NUM_OF_TIME_PERIODS; i++) _pLocation[i] = NULL;
_bDead = false;
_bVaccinated = false;
_bNaiveVaccineProtection = false;
}
Person::~Person() {
clearInfectionHistory();
}
void Person::clearInfectionHistory() {
for (unsigned int i = 0; i < infectionHistory.size(); i++) {
delete infectionHistory[i];
}
infectionHistory.clear();
}
Infection& Person::initializeNewInfection(Serotype serotype) {
setImmunity(serotype);
Infection* infection = new Infection(serotype);
infectionHistory.push_back(infection);
return *infection;
}
Infection& Person::initializeNewInfection(Mosquito* mos, int time, Location* loc, Serotype serotype) {
Infection& infection = initializeNewInfection(serotype);
infection.infectionOwner = this;
infection.infectedBy = mos; // this is a mosquito ID
infection.infectedLoc = loc;
infection.infectedTime = time;
infection.infectiousTime = Parameters::sampler(INCUBATION_CDF, gsl_rng_uniform(RNG)) + time;
return infection;
}
// copyImmunity - copy immune status from person* p
void Person::copyImmunity(const Person* p) {
assert(p!=NULL);
_nImmunity = p->_nImmunity;
_bVaccinated = p->_bVaccinated;
vaccineHistory.clear();
vaccineHistory.assign(p->vaccineHistory.begin(), p->vaccineHistory.end());
clearInfectionHistory();
for (int i=0; i < p->getNumNaturalInfections(); i++) {
infectionHistory.push_back( new Infection(p->infectionHistory[i]) );
}
}
// resetImmunity - reset immune status (infants)
void Person::resetImmunity() {
_nImmunity.reset();
clearInfectionHistory();
_bVaccinated = false;
vaccineHistory.clear();
_bNaiveVaccineProtection = false;
_bDead = false;
}
bool Person::naturalDeath(int t) {
if (_nLifespan<=_nAge+(t/365.0)) {
_bDead = true;
return true;
}
return false;
}
void Person::kill() {
_bDead = true;
}
bool Person::isInfectable(Serotype serotype, int time) const {
return isSusceptible(serotype) and // is susceptible to this serotype (i.e., not immune to this serotype via previous infection)
!isCrossProtected(time) and // not cross-serotype protection from last infection
!isVaccineProtected(serotype, time); // not vaccine protected at this time
}
double Person::remainingEfficacy(const int time) const {
double remainingFraction = 1.0;
if (not isVaccinated()) {
remainingFraction = 0.0;
} else {
if (_par->linearlyWaningVaccine) {
// reduce by fraction of immunity duration that has waned
int time_since_vac = daysSinceVaccination(time);
if (time_since_vac > _par->vaccineImmunityDuration) {
remainingFraction = 0.0;
} else {
remainingFraction -= ((double) time_since_vac) / _par->vaccineImmunityDuration;
}
}
}
return remainingFraction;
}
double Person::vaccineProtection(const Serotype serotype, const int time) const {
double ves;
if (not isVaccinated()) {
ves = 0.0;
} else {
if (daysSinceVaccination(time) > _par->vaccineImmunityDuration) {
ves = 0.0;
} else {
if (_bNaiveVaccineProtection == true) {
ves = _par->fVESs_NAIVE[serotype];
} else {
ves = _par->fVESs[serotype];
}
ves *= remainingEfficacy(time);
}
}
return ves;
}
enum MaternalEffect { MATERNAL_PROTECTION, NO_EFFECT, MATERNAL_ENHANCEMENT };
MaternalEffect _maternal_antibody_effect(Person* p, const Parameters* _par, int time) {
MaternalEffect effect = NO_EFFECT;
if (p->getAge() == 0 and time >= 0) { // this is an infant, and we aren't reloading an infection history
Person* mom = p->getLocation(HOME_NIGHT)->findMom(); // find a cohabitating female of reproductive age
if (mom and mom->getImmunityBitset().any()) { // if there is one and she has an infection history
if (gsl_rng_uniform(RNG) < _par->infantImmuneProb) {
effect = MATERNAL_PROTECTION;
} else if (gsl_rng_uniform(RNG) < _par->infantSevereProb) {
effect = MATERNAL_ENHANCEMENT;
}
}
}
return effect;
}
// infect - infect this individual
// primary symptomatic is a scaling factor for pathogenicity of primary infections.
// if secondaryPathogenicityOddsRatio > 1, secondary infections are more often symptomatic
// returns true if infection occurs
bool Person::infect(Mosquito* mos, int time, Location* loc, Serotype serotype) {
// Bail now if this person can not become infected
// TODO - clarify this. why would a person not be infectable in this scope?
if (not isInfectable(serotype, time)) return false;
MaternalEffect maternal_effect = _maternal_antibody_effect(this, _par, time);
bool maternalAntibodyEnhancement;
switch( maternal_effect ) {
case MATERNAL_PROTECTION:
return false;
break;
case NO_EFFECT:
maternalAntibodyEnhancement = false;
break;
case MATERNAL_ENHANCEMENT:
maternalAntibodyEnhancement = true;
break;
default:
cerr << "ERROR: Unknown maternal effect: " << maternal_effect << endl;
exit(-837);
break;
}
const int numPrevInfections = getEffectiveNumInfections(); // these both need to be called
const double remaining_efficacy = remainingEfficacy(time); // before initializing new infection
// Create a new infection record
Infection& infection = initializeNewInfection(mos, time, loc, serotype);
//if (loc and getHomeLoc()->isSurveilled() and (getAge() >= 2 and getAge() <= 15)) {
// cerr << "DEBUGGING: " << getHomeLoc()->getTrialArm() << "_" << loc->getType() << " " << getID() << endl;
//}
double symptomatic_probability = _par->serotypePathogenicityRelativeRisks[(int) serotype] * _par->basePathogenicity;
double severe_given_case = 0.0;
switch (numPrevInfections) {
case 0:
if (_par->primaryPathogenicityModel == CONSTANT_PATHOGENICITY) {
symptomatic_probability *= _par->primaryRelativeRisk;
} else if (_par->primaryPathogenicityModel == ORIGINAL_LOGISTIC) {
symptomatic_probability *= SYMPTOMATIC_BY_AGE[_nAge];
} else if (_par->primaryPathogenicityModel == GEOMETRIC_PATHOGENICITY) {
symptomatic_probability *= 1.0 - pow(1.0 - _par->annualFlavivirusAttackRate, getAge());
}
severe_given_case = _par->primarySevereFraction[(int) serotype];
break;
case 1:
//symptomatic_probability -- no change
severe_given_case = _par->secondarySevereFraction[(int) serotype];
break;
case 2:
symptomatic_probability *= _par->postSecondaryRelativeRisk;
severe_given_case = _par->tertiarySevereFraction[(int) serotype];
break;
case 3:
symptomatic_probability *= _par->postSecondaryRelativeRisk;
severe_given_case = _par->quaternarySevereFraction[(int) serotype];
break;
case 4: // NEEDED IF VACCINE COUNTS AS INFECTION
symptomatic_probability *= _par->postSecondaryRelativeRisk;
severe_given_case = _par->quaternarySevereFraction[(int) serotype];
break;
default:
cerr << "ERROR: Unsupported number of previous infections: " << numPrevInfections << endl;
exit(-838);
}
if (symptomatic_probability > 1.0) symptomatic_probability = 1.0;
const double effective_VEP = isVaccinated() ? _par->fVEP*remaining_efficacy : 0.0; // reduced symptoms due to vaccine
symptomatic_probability *= (1.0 - effective_VEP);
assert(symptomatic_probability >= 0.0);
assert(symptomatic_probability <= 1.0);
infection.recoveryTime = infection.infectiousTime + INFECTIOUS_PERIOD_ASYMPTOMATIC; // may be changed below
if ((gsl_rng_uniform(RNG) < symptomatic_probability) or maternalAntibodyEnhancement) { // Is this a case?
const double severe_rand = gsl_rng_uniform(RNG);
infection.recoveryTime = infection.infectiousTime + INFECTIOUS_PERIOD_MILD; // may yet be changed below
if ( severe_rand < severe_given_case or maternalAntibodyEnhancement) { // Is this a severe case?
if (not isVaccinated() or gsl_rng_uniform(RNG) > _par->fVEH*remaining_efficacy) { // Is this person unvaccinated or vaccinated but unlucky?
infection.recoveryTime = infection.infectiousTime + INFECTIOUS_PERIOD_SEVERE;
infection.severeDisease = true;
}
}
// Determine if this person withdraws (stops going to work/school)
infection.symptomTime = infection.infectiousTime + SYMPTOMATIC_DELAY;
const int symptomatic_duration = infection.recoveryTime - infection.symptomTime;
const int symptomatic_active_period = gsl_ran_geometric(RNG, 0.5) - 1; // min generator value is 1 trial
infection.withdrawnTime = symptomatic_active_period < symptomatic_duration ?
infection.symptomTime + symptomatic_active_period :
infection.withdrawnTime;
}
// Flag locations with (non-historical) infections, so that we know to look there for human->mosquito transmission
// Negative days are historical (pre-simulation) events, and thus we don't care about modeling transmission
for (int day = std::max(infection.infectiousTime, 0); day < infection.recoveryTime; day++) {
for (int t=0; t<(int) NUM_OF_TIME_PERIODS; t++) {
Community::flagInfectedLocation(_pLocation[t], day);
}
}
// if the antibody-primed vaccine-induced immunity can be acquired retroactively, upgrade this person from naive to mature
if (_par->bRetroactiveMatureVaccine) _bNaiveVaccineProtection = false;
return true;
}
bool Person::isNewlyInfected(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time == infection->infectedTime) {
return true;
}
}
return false;
}
bool Person::isInfected(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->infectedTime and time < infection->recoveryTime) {
return true;
}
}
return false;
}
bool Person::isViremic(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->infectiousTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isSymptomatic(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
// TODO: this is a mess of conditionals. Make it not confusing.
if (infection->isSymptomatic() and time >= infection->symptomTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::hasSevereDisease(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (infection->severeDisease and time >= infection->symptomTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isWithdrawn(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->withdrawnTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isSusceptible(Serotype serotype) const {
return !_bDead && !(_nImmunity[serotype] == 1);
}
bool Person::isCrossProtected(int time) const {
return (getNumNaturalInfections() > 0) and // has any past infection
(infectionHistory.back()->infectedTime + _par->nDaysImmune > time); // prev. infection w/in crossprotection period
}
bool Person::isVaccineProtected(Serotype serotype, int time) const {
return isVaccinated() and
( !_par->bVaccineLeaky or // if the vaccine isn't leaky
(gsl_rng_uniform(RNG) < vaccineProtection(serotype, time)) ); // or it protects (i.e., doesn't leak this time)
}
bool Person::fullySusceptible() const {
bool susceptible = true;
for (int s = 0; s<(int) NUM_OF_SEROTYPES; ++s) {
if ( not isSusceptible((Serotype) s) ) { susceptible = false; }
}
return susceptible;
}
bool Person::isSeroEligible(VaccineSeroConstraint vsc, double falsePos, double falseNeg) const {
if (vsc == VACCINATE_ALL_SERO_STATUSES) return true;
assert(falsePos >= 0.0 and falsePos <= 1.0);
assert(falseNeg >= 0.0 and falseNeg <= 1.0);
assert(vsc == VACCINATE_SEROPOSITIVE_ONLY or vsc == VACCINATE_SERONEGATIVE_ONLY);
bool isSeroPos = not fullySusceptible(); // fully susceptible == seronegative == false
if ((isSeroPos and (falseNeg > gsl_rng_uniform(RNG))) // sero+ but tests negative
or (!isSeroPos and (falsePos > gsl_rng_uniform(RNG)))) { // sero- but tests positive
isSeroPos = !isSeroPos;
}
bool eligible = vsc == VACCINATE_SEROPOSITIVE_ONLY ? isSeroPos : not isSeroPos;
return eligible;
}
bool Person::vaccinate(int time) {
if (!_bDead) {
//vector<double> _fVES = _par->fVESs;
_bVaccinated = true;
vaccineHistory.push_back(time);
if ( fullySusceptible() ) {
_bNaiveVaccineProtection = true;
} else {
_bNaiveVaccineProtection = false;
}
if ( _par->bVaccineLeaky == false ) { // all-or-none VE_S protection
if ( fullySusceptible() ) { // naive against all serotypes
for (int i=0; i<NUM_OF_SEROTYPES; i++) {
if (gsl_rng_uniform(RNG)<_par->fVESs_NAIVE[i]) _nImmunity[i] = 1; // protect against serotype i
}
} else {
for (int i=0; i<NUM_OF_SEROTYPES; i++) {
if (gsl_rng_uniform(RNG)<_par->fVESs[i]) _nImmunity[i] = 1; // protect against serotype i
}
}
}
return true;
} else {
return false;
}
}