-
Notifications
You must be signed in to change notification settings - Fork 0
/
epiPop.cpp
217 lines (201 loc) · 7.28 KB
/
epiPop.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
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GetOpt.h>
#include "codonString.h"
const char* helpStr =
"This program simulates an evolving viral population where the epidemiology\n"
"is described by SIRS dynamics and the molecular evolution is given by a codon model.\n"
"The program outputs a fastA file containing viral DNA sequences sampled\n"
"from the population\n\n"
"Usage: epiPop -o <outputfile> [options]\n\n"
"Options:\n"
" -h \t\t\t print help message\n"
" -g <numeric > 0>\t rate of host immune decay per timestep [0.1]\n"
" -c <0<numeric<1>\t strength of immune protection [0.1]\n"
" -b <numeric > 0>\t rate of viral infection [0.5]\n"
" -d <numeric > 0>\t rate of infection recovery [0.1]\n"
" -N <integer > 0>\t initial viral population size [10]\n"
" -H <integer > 0>\t host population size [10000]\n"
" -L <integer > 0>\t length of viral codon sequence [num antigenic sites]\n"
" -l <integer>\t\t number of antigenic codons in viral sequence [2]\n"
" -u <numeric>\t\t mutation rate per codon per timestep [1e-5]\n"
" -k <numeric>\t\t transition-transversion rate of mutational model [3.0]\n"
" -a <numeric>\t\t cost of antigenic mutation [1e-2] \n"
" -s <numeric>\t\t cost of non-antigenic mutation [1e-3]\n"
" -T <numeric > 0>\t max running time of simulation [10000]\n"
" -S <numeric > 0>\t interval between sampling [1000]\n"
" -Z <integer>\t\t number of sequences sampled [10]\n"
" -t <numeric > 0>\t size of time-step [1.0]\n\n";
void seed_time (void);
int main (int argc, char **argv)
{
// Default simulation parameters
char filename[100] = "";
double Tmax = 10000; // length of simulation
double Tsample = 1000; // time of first sample (and intervals)
int sampleSize = 10; // number of seqs sampled
double dt = 1.0; // time-step
// Default epidemiological parameters
int hostPopSize = 10000;
int N0 = 10; // initial viral pop size
double gamma = 0.1; // decay of immunity
double beta = 0.5; // transmission rate
double delta = 0.1; // recovery rate
// Default sequence parameters
double kappa = 3.0;
double mut_rate = 1e-5;
int Ld = 0;
int Lb = 2;
double sb = 0.01;
double sd = 1e-3;
double sigma = 1.0;
// User-specified arguments
int opt_char;
while ((opt_char = getopt(argc, argv, "ho:T:S:Z:t:H:N:g:b:d:L:u:k:l:a:s:c:")) != -1)
{
switch (opt_char)
{
case 'h':
printf("%s\n", helpStr);
exit(0);
break;
case 'o':
strcpy(filename, optarg);
break;
case 'T':
if ((Tmax = strtod(optarg, NULL)) <= 0.0)
{
fprintf (stderr, "Invalid -T parameter: %s\n", optarg);
exit(1);
}
break;
case 'S':
if ((Tsample = strtod(optarg, NULL)) <= 0.0)
{
fprintf (stderr, "Invalid -S parameter: %s\n", optarg);
exit(1);
}
break;
case 'Z':
sampleSize = atoi(optarg);
break;
case 't':
if ((dt = strtod(optarg, NULL)) <= 0.0)
{
fprintf (stderr, "Invalid -t parameter: %s\n", optarg);
exit(1);
}
break;
case 'N':
if ((N0 = atoi(optarg)) <= 0)
{
fprintf (stderr, "Invalid -N parameter: %s\n", optarg);
exit(1);
}
break;
case 'H':
if ((hostPopSize = atoi(optarg)) <= 0)
{
fprintf (stderr, "Invalid -H parameter: %s\n", optarg);
exit(1);
}
break;
case 'g':
if ((gamma = strtod(optarg, NULL)) < 0.0)
{
fprintf (stderr, "Invalid -g parameter: %s\n", optarg);
exit(1);
}
break;
case 'b':
if ((beta = strtod(optarg, NULL)) <= 0.0)
{
fprintf (stderr, "Invalid -b parameter: %s\n", optarg);
exit(1);
}
break;
case 'd':
if ((delta = strtod(optarg, NULL)) <= 0.0)
{
fprintf (stderr, "Invalid -d parameter: %s\n", optarg);
exit(1);
}
break;
case 'L':
if ((Ld = atoi(optarg)) < 0)
{
fprintf (stderr, "Invalid -L parameter: %s\n", optarg);
exit(1);
}
break;
case 'u':
if ((mut_rate = strtod(optarg, NULL)) < 0.0)
{
fprintf (stderr, "Invalid -u parameter: %s\n", optarg);
exit(1);
}
break;
case 'k':
if ((kappa = strtod(optarg, NULL)) < 0.0)
{
fprintf (stderr, "Invalid -k parameter: %s\n", optarg);
exit(1);
}
break;
case 'l':
if ((Lb = atoi(optarg)) < 0)
{
fprintf (stderr, "Invalid -l parameter: %s\n", optarg);
exit(1);
}
break;
case 'a':
sb = strtod(optarg, NULL);
break;
case 's':
sd = strtod(optarg, NULL);
break;
case 'c':
if ((sigma = strtod(optarg, NULL)) < 0.0)
{
fprintf (stderr, "Invalid -c parameter: %s\n", optarg);
exit(1);
}
break;
case '?':
fprintf (stderr, "Unrecognized argument\n");
exit(1);
}
}
if (filename[0] == '\0')
{
fprintf (stderr, "Output filename (option -o) must be specified\n");
exit(1);
}
FILE *outfile;
if ((outfile = fopen(filename, "w")) == NULL)
{
fprintf (stderr, "Cannot open %s\n", filename);
exit(1);
}
seed_time();
codonString::init_trans_matrix (kappa);
codonString viralPop(N0, Lb, Ld, sb, sd, sigma);
double t = 0.0;
int N = 0;
while ((N = viralPop.popSize()) > 0 && t < Tmax)
{
viralPop.mutate(mut_rate * dt);
viralPop.transmit(beta * dt, hostPopSize);
viralPop.recover(rbinom(N, delta * dt));
if (gamma > 0)
viralPop.immuneDecay(gamma * dt);
if (fmod(t, Tsample) < 1e-4)
viralPop.printSample(outfile, t, sampleSize);
t += dt;
}
fclose(outfile);
}