5
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("optparse"))
|
|
4
|
|
5 option_list <- list(
|
6
|
6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"),
|
|
7 make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"),
|
|
8 make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"),
|
|
9 make_option(c("--input"), action="store", dest="input", help="Temperature data for selected location"),
|
|
10 make_option(c("--insect"), action="store", dest="insect", help="Insect name"),
|
|
11 make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"),
|
|
12 make_option(c("--location"), action="store", dest="location", help="Selected location"),
|
|
13 make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
|
|
14 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
|
|
15 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
|
|
16 make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
|
|
17 make_option(c("--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
|
|
18 make_option(c("--output"), action="store", dest="output", help="Output dataset"),
|
|
19 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
|
|
20 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
|
|
21 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
|
|
22 make_option(c("--std_error_plot"), action="store", dest="std_error_plot", help="Plot Standard error"),
|
|
23 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)")
|
5
|
24 )
|
|
25
|
8
|
26 parser <- OptionParser(usage="%prog [options] file", option_list=option_list);
|
|
27 args <- parse_args(parser, positional_arguments=TRUE);
|
|
28 opt <- args$options;
|
5
|
29
|
|
30 add_daylight_length = function(temperature_data_frame, num_columns, num_rows) {
|
|
31 # Return a vector of daylight length (photoperido profile) for
|
|
32 # the number of days specified in the input temperature data
|
|
33 # (from Forsythe 1995).
|
8
|
34 p = 0.8333;
|
|
35 latitude = temperature_data_frame$LATITUDE[1];
|
|
36 daylight_length_vector = NULL;
|
5
|
37 for (i in 1:num_rows) {
|
|
38 # Get the day of the year from the current row
|
|
39 # of the temperature data for computation.
|
8
|
40 doy = temperature_data_frame$DOY[i];
|
|
41 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)));
|
|
42 phi = asin(0.39795 * cos(theta));
|
5
|
43 # Compute the length of daylight for the day of the year.
|
8
|
44 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)));
|
|
45 daylight_length_vector[i] = 24 - darkness_length;
|
5
|
46 }
|
|
47 # Append daylight_length_vector as a new column to temperature_data_frame.
|
8
|
48 temperature_data_frame[, num_columns+1] = daylight_length_vector;
|
|
49 return(temperature_data_frame);
|
5
|
50 }
|
|
51
|
6
|
52 dev.egg = function(temperature) {
|
8
|
53 dev.rate = -0.9843 * temperature + 33.438;
|
|
54 return(dev.rate);
|
6
|
55 }
|
|
56
|
|
57 dev.emerg = function(temperature) {
|
8
|
58 emerg.rate = -0.5332 * temperature + 24.147;
|
|
59 return(emerg.rate);
|
6
|
60 }
|
|
61
|
|
62 dev.old = function(temperature) {
|
8
|
63 n34 = -0.6119 * temperature + 17.602;
|
|
64 n45 = -0.4408 * temperature + 19.036;
|
|
65 dev.rate = mean(n34 + n45);
|
|
66 return(dev.rate);
|
6
|
67 }
|
|
68
|
|
69 dev.young = function(temperature) {
|
8
|
70 n12 = -0.3728 * temperature + 14.68;
|
|
71 n23 = -0.6119 * temperature + 25.249;
|
|
72 dev.rate = mean(n12 + n23);
|
|
73 return(dev.rate);
|
|
74 }
|
|
75
|
|
76
|
|
77 get_date_labels = function(temperature_data_frame, num_rows) {
|
|
78 # Keep track of the years to see if spanning years.
|
|
79 month_labels = list();
|
|
80 current_month_label = NULL;
|
|
81 for (i in 1:num_rows) {
|
|
82 # Get the year and month from the date which
|
|
83 # has the format YYYY-MM-DD.
|
|
84 date = format(temperature_data_frame$DATE[i]);
|
|
85 items = strsplit(date, "-")[[1]];
|
|
86 month = items[2];
|
|
87 month_label = month.abb[as.integer(month)];
|
|
88 if (!identical(current_month_label, month_label)) {
|
|
89 month_labels[length(month_labels)+1] = month_label;
|
|
90 current_month_label = month_label;
|
|
91 }
|
|
92 }
|
|
93 return(c(unlist(month_labels)));
|
6
|
94 }
|
|
95
|
5
|
96 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
|
8
|
97 # Base development threshold for Brown Marmorated Stink Bug
|
5
|
98 # insect phenology model.
|
8
|
99 threshold = 14.17;
|
5
|
100 # Minimum temperature for current row.
|
8
|
101 curr_min_temp = temperature_data_frame$TMIN[row];
|
5
|
102 # Maximum temperature for current row.
|
8
|
103 curr_max_temp = temperature_data_frame$TMAX[row];
|
5
|
104 # Mean temperature for current row.
|
8
|
105 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp);
|
5
|
106 # Initialize degree day accumulation
|
8
|
107 averages = 0;
|
6
|
108 if (curr_max_temp < threshold) {
|
8
|
109 averages = 0;
|
5
|
110 }
|
|
111 else {
|
|
112 # Initialize hourly temperature.
|
8
|
113 T = NULL;
|
5
|
114 # Initialize degree hour vector.
|
8
|
115 dh = NULL;
|
5
|
116 # Daylight length for current row.
|
8
|
117 y = temperature_data_frame$DAYLEN[row];
|
5
|
118 # Darkness length.
|
8
|
119 z = 24 - y;
|
5
|
120 # Lag coefficient.
|
8
|
121 a = 1.86;
|
5
|
122 # Darkness coefficient.
|
8
|
123 b = 2.20;
|
5
|
124 # Sunrise time.
|
8
|
125 risetime = 12 - y / 2;
|
5
|
126 # Sunset time.
|
8
|
127 settime = 12 + y / 2;
|
|
128 ts = (curr_max_temp - curr_min_temp) * sin(pi * (settime - 5) / (y + 2 * a)) + curr_min_temp;
|
5
|
129 for (i in 1:24) {
|
|
130 if (i > risetime && i < settime) {
|
|
131 # Number of hours after Tmin until sunset.
|
8
|
132 m = i - 5;
|
|
133 T[i] = (curr_max_temp - curr_min_temp) * sin(pi * m / (y + 2 * a)) + curr_min_temp;
|
5
|
134 if (T[i] < 8.4) {
|
8
|
135 dh[i] = 0;
|
5
|
136 }
|
|
137 else {
|
8
|
138 dh[i] = T[i] - 8.4;
|
5
|
139 }
|
|
140 }
|
6
|
141 else if (i > settime) {
|
8
|
142 n = i - settime;
|
|
143 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
5
|
144 if (T[i] < 8.4) {
|
8
|
145 dh[i] = 0;
|
5
|
146 }
|
|
147 else {
|
8
|
148 dh[i] = T[i] - 8.4;
|
5
|
149 }
|
|
150 }
|
|
151 else {
|
8
|
152 n = i + 24 - settime;
|
|
153 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
5
|
154 if (T[i] < 8.4) {
|
8
|
155 dh[i] = 0;
|
5
|
156 }
|
|
157 else {
|
8
|
158 dh[i] = T[i] - 8.4;
|
5
|
159 }
|
|
160 }
|
|
161 }
|
8
|
162 averages = sum(dh) / 24;
|
5
|
163 }
|
6
|
164 return(c(curr_mean_temp, averages))
|
5
|
165 }
|
|
166
|
6
|
167 mortality.adult = function(temperature) {
|
|
168 if (temperature < 12.7) {
|
8
|
169 mortality.probability = 0.002;
|
6
|
170 }
|
|
171 else {
|
8
|
172 mortality.probability = temperature * 0.0005 + 0.02;
|
6
|
173 }
|
|
174 return(mortality.probability)
|
5
|
175 }
|
|
176
|
|
177 mortality.egg = function(temperature) {
|
|
178 if (temperature < 12.7) {
|
8
|
179 mortality.probability = 0.8;
|
5
|
180 }
|
|
181 else {
|
8
|
182 mortality.probability = 0.8 - temperature / 40.0;
|
6
|
183 if (mortality.probability < 0) {
|
8
|
184 mortality.probability = 0.01;
|
5
|
185 }
|
|
186 }
|
6
|
187 return(mortality.probability)
|
5
|
188 }
|
|
189
|
|
190 mortality.nymph = function(temperature) {
|
|
191 if (temperature < 12.7) {
|
8
|
192 mortality.probability = 0.03;
|
5
|
193 }
|
|
194 else {
|
8
|
195 mortality.probability = temperature * 0.0008 + 0.03;
|
5
|
196 }
|
8
|
197 return(mortality.probability);
|
6
|
198 }
|
|
199
|
|
200 parse_input_data = function(input_file, num_rows) {
|
|
201 # Read in the input temperature datafile into a data frame.
|
8
|
202 temperature_data_frame = read.csv(file=input_file, header=T, strip.white=TRUE, sep=",");
|
|
203 num_columns = dim(temperature_data_frame)[2];
|
6
|
204 if (num_columns == 6) {
|
|
205 # The input data has the following 6 columns:
|
|
206 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
|
|
207 # Set the column names for access when adding daylight length..
|
8
|
208 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
|
6
|
209 # Add a column containing the daylight length for each day.
|
8
|
210 temperature_data_frame = add_daylight_length(temperature_data_frame, num_columns, num_rows);
|
6
|
211 # Reset the column names with the additional column for later access.
|
8
|
212 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN");
|
6
|
213 }
|
8
|
214 return(temperature_data_frame);
|
5
|
215 }
|
|
216
|
8
|
217
|
6
|
218 render_chart = function(chart_type, insect, location, latitude, start_date, end_date, days, maxval, plot_std_error,
|
8
|
219 group1, group2, group3, group1_std_error, group2_std_error, group3_std_error, date_labels) {
|
6
|
220 if (chart_type == "pop_size_by_life_stage") {
|
8
|
221 title = paste(insect, ": Total pop. by life stage :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
222 legend_text = c("Egg", "Nymph", "Adult");
|
|
223 columns = c(4, 2, 1);
|
6
|
224 } else if (chart_type == "pop_size_by_generation") {
|
8
|
225 title = paste(insect, ": Total pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
226 legend_text = c("P", "F1", "F2");
|
|
227 columns = c(1, 2, 4);
|
6
|
228 } else if (chart_type == "adult_pop_size_by_generation") {
|
8
|
229 title = paste(insect, ": Adult pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
230 legend_text = c("P", "F1", "F2");
|
|
231 columns = c(1, 2, 4);
|
5
|
232 }
|
8
|
233 plot(days, group1, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
234 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
235 lines(days, group2, lwd=2, lty=1, col=2);
|
|
236 lines(days, group3, lwd=2, lty=1, col=4);
|
|
237 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
|
|
238 axis(2, cex.axis=3);
|
6
|
239 if (plot_std_error==1) {
|
|
240 # Standard error for group1.
|
8
|
241 lines(days, group1+group1_std_error, lty=2);
|
|
242 lines(days, group1-group1_std_error, lty=2);
|
6
|
243 # Standard error for group2.
|
8
|
244 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
245 lines(days, group2-group2_std_error, col=2, lty=2);
|
6
|
246 # Standard error for group3.
|
8
|
247 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
248 lines(days, group3-group3_std_error, col=4, lty=2);
|
5
|
249 }
|
|
250 }
|
|
251
|
8
|
252 temperature_data_frame = parse_input_data(opt$input, opt$num_days);
|
6
|
253 # All latitude values are the same, so get the value from the first row.
|
8
|
254 latitude = temperature_data_frame$LATITUDE[1];
|
|
255 num_columns = dim(temperature_data_frame)[2];
|
|
256 date_labels = get_date_labels(temperature_data_frame, opt$num_days);
|
5
|
257
|
6
|
258 # Initialize matrices.
|
8
|
259 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
260 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
261 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
262 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
263 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
264 Diapausing.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
5
|
265
|
8
|
266 newborn.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
267 adult.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
268 death.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
6
|
269
|
8
|
270 P.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
271 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
272 F1.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
273 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
274 F2.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
275 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
6
|
276
|
8
|
277 population.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
5
|
278
|
6
|
279 # Process replications.
|
|
280 for (N.replications in 1:opt$replications) {
|
|
281 # Start with the user-defined number of insects per replication.
|
8
|
282 num_insects = opt$insects_per_replication;
|
6
|
283 # Generation, Stage, degree-days, T, Diapause.
|
8
|
284 vector.ini = c(0, 3, 0, 0, 0);
|
6
|
285 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause.
|
8
|
286 vector.matrix = rep(vector.ini, num_insects);
|
5
|
287 # Complete matrix for the population.
|
8
|
288 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
|
5
|
289 # Time series of population size.
|
8
|
290 Eggs = rep(0, opt$num_days);
|
|
291 YoungNymphs = rep(0, opt$num_days);
|
|
292 OldNymphs = rep(0, opt$num_days);
|
|
293 Previtellogenic = rep(0, opt$num_days);
|
|
294 Vitellogenic = rep(0, opt$num_days);
|
|
295 Diapausing = rep(0, opt$num_days);
|
6
|
296
|
8
|
297 N.newborn = rep(0, opt$num_days);
|
|
298 N.adult = rep(0, opt$num_days);
|
|
299 N.death = rep(0, opt$num_days);
|
6
|
300
|
8
|
301 overwintering_adult.population = rep(0, opt$num_days);
|
|
302 first_generation.population = rep(0, opt$num_days);
|
|
303 second_generation.population = rep(0, opt$num_days);
|
6
|
304
|
8
|
305 P.adult = rep(0, opt$num_days);
|
|
306 F1.adult = rep(0, opt$num_days);
|
|
307 F2.adult = rep(0, opt$num_days);
|
6
|
308
|
8
|
309 total.population = NULL;
|
6
|
310
|
8
|
311 averages.day = rep(0, opt$num_days);
|
5
|
312 # All the days included in the input temperature dataset.
|
|
313 for (row in 1:opt$num_days) {
|
|
314 # Get the integer day of the year for the current row.
|
8
|
315 doy = temperature_data_frame$DOY[row];
|
5
|
316 # Photoperiod in the day.
|
8
|
317 photoperiod = temperature_data_frame$DAYLEN[row];
|
|
318 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days);
|
|
319 mean.temp = temp.profile[1];
|
|
320 averages.temp = temp.profile[2];
|
|
321 averages.day[row] = averages.temp;
|
5
|
322 # Trash bin for death.
|
8
|
323 death.vector = NULL;
|
5
|
324 # Newborn.
|
8
|
325 birth.vector = NULL;
|
5
|
326 # All individuals.
|
6
|
327 for (i in 1:num_insects) {
|
|
328 # Individual record.
|
8
|
329 vector.individual = vector.matrix[i,];
|
6
|
330 # Adjustment for late season mortality rate (still alive?).
|
5
|
331 if (latitude < 40.0) {
|
8
|
332 post.mortality = 1;
|
|
333 day.kill = 300;
|
5
|
334 }
|
|
335 else {
|
8
|
336 post.mortality = 2;
|
|
337 day.kill = 250;
|
5
|
338 }
|
6
|
339 if (vector.individual[2] == 0) {
|
5
|
340 # Egg.
|
8
|
341 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
|
5
|
342 }
|
6
|
343 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
|
8
|
344 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
|
5
|
345 }
|
6
|
346 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
|
|
347 # Adult.
|
5
|
348 if (doy < day.kill) {
|
8
|
349 death.probability = opt$adult_mortality * mortality.adult(mean.temp);
|
5
|
350 }
|
|
351 else {
|
|
352 # Increase adult mortality after fall equinox.
|
8
|
353 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp);
|
5
|
354 }
|
|
355 }
|
6
|
356 # Dependent on temperature and life stage?
|
8
|
357 u.d = runif(1);
|
6
|
358 if (u.d < death.probability) {
|
8
|
359 death.vector = c(death.vector, i);
|
6
|
360 }
|
5
|
361 else {
|
6
|
362 # End of diapause.
|
|
363 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
|
5
|
364 # Overwintering adult (previttelogenic).
|
6
|
365 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
|
5
|
366 # Add 68C to become fully reproductively matured.
|
|
367 # Transfer to vittelogenic.
|
8
|
368 vector.individual = c(0, 4, 0, 0, 0);
|
|
369 vector.matrix[i,] = vector.individual;
|
5
|
370 }
|
|
371 else {
|
6
|
372 # Add to # Add average temperature for current day.
|
8
|
373 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
374 # Add 1 day in current stage.
|
8
|
375 vector.individual[4] = vector.individual[4] + 1;
|
|
376 vector.matrix[i,] = vector.individual;
|
5
|
377 }
|
|
378 }
|
6
|
379 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
|
5
|
380 # Not overwintering adult (previttelogenic).
|
8
|
381 current.gen = vector.individual[1];
|
6
|
382 if (vector.individual[3] > 68) {
|
5
|
383 # Add 68C to become fully reproductively matured.
|
|
384 # Transfer to vittelogenic.
|
8
|
385 vector.individual = c(current.gen, 4, 0, 0, 0);
|
|
386 vector.matrix[i,] = vector.individual;
|
5
|
387 }
|
|
388 else {
|
6
|
389 # Add average temperature for current day.
|
8
|
390 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
391 # Add 1 day in current stage.
|
8
|
392 vector.individual[4] = vector.individual[4] + 1;
|
|
393 vector.matrix[i,] = vector.individual;
|
5
|
394 }
|
|
395 }
|
6
|
396 # Oviposition -- where population dynamics comes from.
|
|
397 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
|
5
|
398 # Vittelogenic stage, overwintering generation.
|
6
|
399 if (vector.individual[4] == 0) {
|
5
|
400 # Just turned in vittelogenic stage.
|
8
|
401 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size));
|
5
|
402 }
|
|
403 else {
|
|
404 # Daily probability of birth.
|
8
|
405 p.birth = opt$oviposition * 0.01;
|
|
406 u1 = runif(1);
|
5
|
407 if (u1 < p.birth) {
|
8
|
408 num_insects.birth = round(runif(1, 2, 8));
|
5
|
409 }
|
|
410 }
|
6
|
411 # Add average temperature for current day.
|
8
|
412 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
413 # Add 1 day in current stage.
|
8
|
414 vector.individual[4] = vector.individual[4] + 1;
|
|
415 vector.matrix[i,] = vector.individual;
|
6
|
416 if (num_insects.birth > 0) {
|
5
|
417 # Add new birth -- might be in different generations.
|
8
|
418 new.gen = vector.individual[1] + 1;
|
5
|
419 # Egg profile.
|
8
|
420 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
421 new.vector = rep(new.individual, num_insects.birth);
|
5
|
422 # Update batch of egg profile.
|
8
|
423 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
424 # Group with total eggs laid in that day.
|
8
|
425 birth.vector = rbind(birth.vector, new.vector);
|
5
|
426 }
|
|
427 }
|
6
|
428 # Oviposition -- for generation 1.
|
|
429 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
5
|
430 # Vittelogenic stage, 1st generation
|
6
|
431 if (vector.individual[4] == 0) {
|
5
|
432 # Just turned in vittelogenic stage.
|
8
|
433 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size));
|
5
|
434 }
|
|
435 else {
|
|
436 # Daily probability of birth.
|
8
|
437 p.birth = opt$oviposition * 0.01;
|
|
438 u1 = runif(1);
|
5
|
439 if (u1 < p.birth) {
|
8
|
440 num_insects.birth = round(runif(1, 2, 8));
|
5
|
441 }
|
|
442 }
|
6
|
443 # Add average temperature for current day.
|
8
|
444 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
445 # Add 1 day in current stage.
|
8
|
446 vector.individual[4] = vector.individual[4] + 1;
|
|
447 vector.matrix[i,] = vector.individual;
|
6
|
448 if (num_insects.birth > 0) {
|
5
|
449 # Add new birth -- might be in different generations.
|
8
|
450 new.gen = vector.individual[1] + 1;
|
5
|
451 # Egg profile.
|
8
|
452 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
453 new.vector = rep(new.individual, num_insects.birth);
|
5
|
454 # Update batch of egg profile.
|
8
|
455 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
456 # Group with total eggs laid in that day.
|
8
|
457 birth.vector = rbind(birth.vector, new.vector);
|
5
|
458 }
|
|
459 }
|
6
|
460 # Egg to young nymph.
|
|
461 if (vector.individual[2] == 0) {
|
|
462 # Add average temperature for current day.
|
8
|
463 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
464 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
|
|
465 # From egg to young nymph, degree-days requirement met.
|
8
|
466 current.gen = vector.individual[1];
|
5
|
467 # Transfer to young nymph stage.
|
8
|
468 vector.individual = c(current.gen, 1, 0, 0, 0);
|
5
|
469 }
|
|
470 else {
|
|
471 # Add 1 day in current stage.
|
8
|
472 vector.individual[4] = vector.individual[4] + 1;
|
5
|
473 }
|
8
|
474 vector.matrix[i,] = vector.individual;
|
5
|
475 }
|
6
|
476 # Young nymph to old nymph.
|
|
477 if (vector.individual[2] == 1) {
|
|
478 # Add average temperature for current day.
|
8
|
479 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
480 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
|
|
481 # From young to old nymph, degree_days requirement met.
|
8
|
482 current.gen = vector.individual[1];
|
5
|
483 # Transfer to old nym stage.
|
8
|
484 vector.individual = c(current.gen, 2, 0, 0, 0);
|
5
|
485 if (photoperiod < opt$photoperiod && doy > 180) {
|
8
|
486 vector.individual[5] = 1;
|
5
|
487 } # Prepare for diapausing.
|
|
488 }
|
|
489 else {
|
|
490 # Add 1 day in current stage.
|
8
|
491 vector.individual[4] = vector.individual[4] + 1;
|
5
|
492 }
|
8
|
493 vector.matrix[i,] = vector.individual;
|
6
|
494 }
|
|
495 # Old nymph to adult: previttelogenic or diapausing?
|
|
496 if (vector.individual[2] == 2) {
|
|
497 # Add average temperature for current day.
|
8
|
498 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
499 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
|
|
500 # From old to adult, degree_days requirement met.
|
8
|
501 current.gen = vector.individual[1];
|
6
|
502 if (vector.individual[5] == 0) {
|
|
503 # Previttelogenic.
|
8
|
504 vector.individual = c(current.gen, 3, 0, 0, 0);
|
5
|
505 }
|
|
506 else {
|
|
507 # Diapausing.
|
8
|
508 vector.individual = c(current.gen, 5, 0, 0, 1);
|
5
|
509 }
|
|
510 }
|
|
511 else {
|
|
512 # Add 1 day in current stage.
|
8
|
513 vector.individual[4] = vector.individual[4] + 1;
|
5
|
514 }
|
8
|
515 vector.matrix[i,] = vector.individual;
|
5
|
516 }
|
6
|
517 # Growing of diapausing adult (unimportant, but still necessary).
|
|
518 if (vector.individual[2] == 5) {
|
8
|
519 vector.individual[3] = vector.individual[3] + averages.temp;
|
|
520 vector.individual[4] = vector.individual[4] + 1;
|
|
521 vector.matrix[i,] = vector.individual;
|
5
|
522 }
|
|
523 } # Else if it is still alive.
|
|
524 } # End of the individual bug loop.
|
6
|
525
|
|
526 # Number of deaths.
|
8
|
527 num_insects.death = length(death.vector);
|
6
|
528 if (num_insects.death > 0) {
|
|
529 # Remove record of dead.
|
8
|
530 vector.matrix = vector.matrix[-death.vector,];
|
5
|
531 }
|
6
|
532 # Number of births.
|
8
|
533 num_insects.newborn = length(birth.vector[,1]);
|
|
534 vector.matrix = rbind(vector.matrix, birth.vector);
|
5
|
535 # Update population size for the next day.
|
8
|
536 num_insects = num_insects - num_insects.death + num_insects.newborn;
|
5
|
537
|
|
538 # Aggregate results by day.
|
6
|
539 # Egg population size.
|
8
|
540 Eggs[row] = sum(vector.matrix[,2]==0);
|
6
|
541 # Young nymph population size.
|
8
|
542 YoungNymphs[row] = sum(vector.matrix[,2]==1);
|
6
|
543 # Old nymph population size.
|
8
|
544 OldNymphs[row] = sum(vector.matrix[,2]==2);
|
6
|
545 # Previtellogenic population size.
|
8
|
546 Previtellogenic[row] = sum(vector.matrix[,2]==3);
|
6
|
547 # Vitellogenic population size.
|
8
|
548 Vitellogenic[row] = sum(vector.matrix[,2]==4);
|
6
|
549 # Diapausing population size.
|
8
|
550 Diapausing[row] = sum(vector.matrix[,2]==5);
|
5
|
551
|
6
|
552 # Newborn population size.
|
8
|
553 N.newborn[row] = num_insects.newborn;
|
6
|
554 # Adult population size.
|
8
|
555 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5);
|
6
|
556 # Dead population size.
|
8
|
557 N.death[row] = num_insects.death;
|
6
|
558
|
8
|
559 total.population = c(total.population, num_insects);
|
6
|
560
|
|
561 # Overwintering adult population size.
|
8
|
562 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
|
6
|
563 # First generation population size.
|
8
|
564 first_generation.population[row] = sum(vector.matrix[,1]==1);
|
6
|
565 # Second generation population size.
|
8
|
566 second_generation.population[row] = sum(vector.matrix[,1]==2);
|
5
|
567
|
6
|
568 # P adult population size.
|
8
|
569 P.adult[row] = sum(vector.matrix[,1]==0);
|
6
|
570 # F1 adult population size.
|
8
|
571 F1.adult[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==3) | (vector.matrix[,1]==1 & vector.matrix[,2]==4) | (vector.matrix[,1]==1 & vector.matrix[,2]==5));
|
6
|
572 # F2 adult population size
|
8
|
573 F2.adult[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==3) | (vector.matrix[,1]==2 & vector.matrix[,2]==4) | (vector.matrix[,1]==2 & vector.matrix[,2]==5));
|
6
|
574 } # End of days specified in the input temperature data.
|
5
|
575
|
8
|
576 averages.cum = cumsum(averages.day);
|
5
|
577
|
6
|
578 # Define the output values.
|
8
|
579 Eggs.replications[,N.replications] = Eggs;
|
|
580 YoungNymphs.replications[,N.replications] = YoungNymphs;
|
|
581 OldNymphs.replications[,N.replications] = OldNymphs;
|
|
582 Previtellogenic.replications[,N.replications] = Previtellogenic;
|
|
583 Vitellogenic.replications[,N.replications] = Vitellogenic;
|
|
584 Diapausing.replications[,N.replications] = Diapausing;
|
6
|
585
|
8
|
586 newborn.replications[,N.replications] = N.newborn;
|
|
587 adult.replications[,N.replications] = N.adult;
|
|
588 death.replications[,N.replications] = N.death;
|
6
|
589
|
8
|
590 P.replications[,N.replications] = overwintering_adult.population;
|
|
591 P_adults.replications[,N.replications] = P.adult;
|
|
592 F1.replications[,N.replications] = first_generation.population;
|
|
593 F1_adults.replications[,N.replications] = F1.adult;
|
|
594 F2.replications[,N.replications] = second_generation.population;
|
|
595 F2_adults.replications[,N.replications] = F2.adult;
|
6
|
596
|
8
|
597 population.replications[,N.replications] = total.population;
|
5
|
598 }
|
|
599
|
6
|
600 # Mean value for eggs.
|
8
|
601 eggs = apply(Eggs.replications, 1, mean);
|
6
|
602 # Standard error for eggs.
|
8
|
603 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
|
6
|
604
|
|
605 # Mean value for nymphs.
|
8
|
606 nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
|
6
|
607 # Standard error for nymphs.
|
8
|
608 nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
|
5
|
609
|
6
|
610 # Mean value for adults.
|
8
|
611 adults = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, mean);
|
6
|
612 # Standard error for adults.
|
8
|
613 adults.std_error = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
|
6
|
614
|
|
615 # Mean value for P.
|
8
|
616 P = apply(P.replications, 1, mean);
|
6
|
617 # Standard error for P.
|
8
|
618 P.std_error = apply(P.replications, 1, sd) / sqrt(opt$replications);
|
5
|
619
|
6
|
620 # Mean value for P adults.
|
8
|
621 P_adults = apply(P_adults.replications, 1, mean);
|
6
|
622 # Standard error for P_adult.
|
8
|
623 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications);
|
6
|
624
|
|
625 # Mean value for F1.
|
8
|
626 F1 = apply(F1.replications, 1, mean);
|
6
|
627 # Standard error for F1.
|
8
|
628 F1.std_error = apply(F1.replications, 1, sd) / sqrt(opt$replications);
|
5
|
629
|
6
|
630 # Mean value for F1 adults.
|
8
|
631 F1_adults = apply(F1_adults.replications, 1, mean);
|
6
|
632 # Standard error for F1 adult.
|
8
|
633 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications);
|
6
|
634
|
|
635 # Mean value for F2.
|
8
|
636 F2 = apply(F2.replications, 1, mean);
|
6
|
637 # Standard error for F2.
|
8
|
638 F2.std_error = apply(F2.replications, 1, sd) / sqrt(opt$replications);
|
6
|
639
|
|
640 # Mean value for F2 adults.
|
8
|
641 F2_adults = apply(F2_adults.replications, 1, mean);
|
6
|
642 # Standard error for F2 adult.
|
8
|
643 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications);
|
6
|
644
|
|
645 # Display the total number of days in the Galaxy history item blurb.
|
8
|
646 cat("Number of days: ", opt$num_days, "\n");
|
5
|
647
|
8
|
648 dev.new(width=20, height=30);
|
5
|
649
|
|
650 # Start PDF device driver to save charts to output.
|
8
|
651 pdf(file=opt$output, width=20, height=30, bg="white");
|
|
652 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
5
|
653
|
6
|
654 # Data analysis and visualization plots only within a single calendar year.
|
8
|
655 days = c(1:opt$num_days);
|
|
656 start_date = temperature_data_frame$DATE[1];
|
|
657 end_date = temperature_data_frame$DATE[opt$num_days];
|
5
|
658
|
6
|
659 # Subfigure 1: population size by life stage.
|
8
|
660 maxval = max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error);
|
6
|
661 render_chart("pop_size_by_life_stage", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
8
|
662 opt$std_error_plot, adults, nymphs, eggs, adults.std_error, nymphs.std_error, eggs.std_error, date_labels);
|
6
|
663 # Subfigure 2: population size by generation.
|
8
|
664 maxval = max(F2);
|
6
|
665 render_chart("pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
8
|
666 opt$std_error_plot, P, F1, F2, P.std_error, F1.std_error, F2.std_error, date_labels);
|
6
|
667 # Subfigure 3: adult population size by generation.
|
8
|
668 maxval = max(F2_adults) + 100;
|
6
|
669 render_chart("adult_pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
8
|
670 opt$std_error_plot, P_adults, F1_adults, F2_adults, P_adults.std_error, F1_adults.std_error, F2_adults.std_error,
|
|
671 date_labels);
|
5
|
672
|
|
673 # Turn off device driver to flush output.
|
8
|
674 dev.off();
|