Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 109:8871797b1be4 draft
Uploaded
author | greg |
---|---|
date | Tue, 05 Dec 2017 15:16:14 -0500 |
parents | 2c2903e7ce68 |
children | 37ac68b6ff10 |
comparison
equal
deleted
inserted
replaced
108:9d5824e4b5d0 | 109:8871797b1be4 |
---|---|
1 #!/usr/bin/env Rscript | 1 #!/usr/bin/env Rscript |
2 | 2 |
3 suppressPackageStartupMessages(library("optparse")) | 3 suppressPackageStartupMessages(library("optparse")) |
4 | 4 |
5 option_list <- list( | 5 option_list <- list( |
6 make_option(c("-a", "--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"), | 6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"), |
7 make_option(c("-b", "--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"), | 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("-c", "--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"), | 8 make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"), |
9 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"), | 9 make_option(c("--input"), action="store", dest="input", help="Temperature data for selected location"), |
10 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"), | 10 make_option(c("--insect"), action="store", dest="insect", help="Insect name"), |
11 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"), | 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("-j", "--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"), | 12 make_option(c("--location"), action="store", dest="location", help="Selected location"), |
13 make_option(c("-k", "--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"), | 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("-n", "--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"), | 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("-o", "--output"), action="store", dest="output", help="Output dataset"), | 15 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"), |
16 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), | 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("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), | 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("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"), | 18 make_option(c("--output"), action="store", dest="output", help="Output dataset"), |
19 make_option(c("-t", "--std_error_plot"), action="store", dest="std_error_plot", help="Plot Standard error"), | 19 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), |
20 make_option(c("-v", "--input"), action="store", dest="input", help="Temperature data for selected location"), | 20 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), |
21 make_option(c("-y", "--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)"), | 21 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"), |
22 make_option(c("-x", "--insect"), action="store", dest="insect", help="Insect name") | 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)") | |
23 ) | 24 ) |
24 | 25 |
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) | 26 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) |
26 args <- parse_args(parser, positional_arguments=TRUE) | 27 args <- parse_args(parser, positional_arguments=TRUE) |
27 opt <- args$options | 28 opt <- args$options |
226 lines(days, group3-group3_std_error, col=4, lty=2) | 227 lines(days, group3-group3_std_error, col=4, lty=2) |
227 } | 228 } |
228 } | 229 } |
229 | 230 |
230 temperature_data_frame <- parse_input_data(opt$input, opt$num_days) | 231 temperature_data_frame <- parse_input_data(opt$input, opt$num_days) |
231 # All latitude values are the same, | 232 # All latitude values are the same, so get the value from the first row. |
232 # so get the value from the first row. | |
233 latitude <- temperature_data_frame$LATITUDE[1] | 233 latitude <- temperature_data_frame$LATITUDE[1] |
234 | 234 |
235 # Initialize matrices. | 235 # Initialize matrices. |
236 Eggs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) | 236 Eggs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) |
237 YoungNymphs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) | 237 YoungNymphs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) |
253 | 253 |
254 population.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) | 254 population.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) |
255 | 255 |
256 # Process replications. | 256 # Process replications. |
257 for (N.replications in 1:opt$replications) { | 257 for (N.replications in 1:opt$replications) { |
258 # During each replication start with 1000 individuals. | 258 # Start with the user-defined number of insects per replication. |
259 # TODO: user definable as well? | 259 num_insects <- opt$insects_per_replication |
260 num_insects <- 1000 | |
261 # Generation, Stage, degree-days, T, Diapause. | 260 # Generation, Stage, degree-days, T, Diapause. |
262 vector.ini <- c(0, 3, 0, 0, 0) | 261 vector.ini <- c(0, 3, 0, 0, 0) |
263 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause. | 262 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause. |
264 vector.matrix <- rep(vector.ini, num_insects) | 263 vector.matrix <- rep(vector.ini, num_insects) |
265 # Complete matrix for the population. | 264 # Complete matrix for the population. |
335 u.d <- runif(1) | 334 u.d <- runif(1) |
336 if (u.d < death.probability) { | 335 if (u.d < death.probability) { |
337 death.vector <- c(death.vector, i) | 336 death.vector <- c(death.vector, i) |
338 } | 337 } |
339 else { | 338 else { |
340 # Aggregrate index of dead bug. | |
341 # End of diapause. | 339 # End of diapause. |
342 if (vector.individual[1] == 0 && vector.individual[2] == 3) { | 340 if (vector.individual[1] == 0 && vector.individual[2] == 3) { |
343 # Overwintering adult (previttelogenic). | 341 # Overwintering adult (previttelogenic). |
344 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) { | 342 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) { |
345 # Add 68C to become fully reproductively matured. | 343 # Add 68C to become fully reproductively matured. |
346 # Transfer to vittelogenic. | 344 # Transfer to vittelogenic. |
347 vector.individual <- c(0, 4, 0, 0, 0) | 345 vector.individual <- c(0, 4, 0, 0, 0) |
348 vector.matrix[i,] <- vector.individual | 346 vector.matrix[i,] <- vector.individual |
349 } | 347 } |
350 else { | 348 else { |
351 # Add to # Add average temperature for current day.. | 349 # Add to # Add average temperature for current day. |
352 vector.individual[3] <- vector.individual[3] + averages.temp | 350 vector.individual[3] <- vector.individual[3] + averages.temp |
353 # Add 1 day in current stage. | 351 # Add 1 day in current stage. |
354 vector.individual[4] <- vector.individual[4] + 1 | 352 vector.individual[4] <- vector.individual[4] + 1 |
355 vector.matrix[i,] <- vector.individual | 353 vector.matrix[i,] <- vector.individual |
356 } | 354 } |
370 # Add 1 day in current stage. | 368 # Add 1 day in current stage. |
371 vector.individual[4] <- vector.individual[4] + 1 | 369 vector.individual[4] <- vector.individual[4] + 1 |
372 vector.matrix[i,] <- vector.individual | 370 vector.matrix[i,] <- vector.individual |
373 } | 371 } |
374 } | 372 } |
375 # Event 2 oviposition -- where population dynamics comes from. | 373 # Oviposition -- where population dynamics comes from. |
376 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) { | 374 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) { |
377 # Vittelogenic stage, overwintering generation. | 375 # Vittelogenic stage, overwintering generation. |
378 if (vector.individual[4] == 0) { | 376 if (vector.individual[4] == 0) { |
379 # Just turned in vittelogenic stage. | 377 # Just turned in vittelogenic stage. |
380 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) | 378 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) |
402 new.vector <- t(matrix(new.vector, nrow=5)) | 400 new.vector <- t(matrix(new.vector, nrow=5)) |
403 # Group with total eggs laid in that day. | 401 # Group with total eggs laid in that day. |
404 birth.vector <- rbind(birth.vector, new.vector) | 402 birth.vector <- rbind(birth.vector, new.vector) |
405 } | 403 } |
406 } | 404 } |
407 # Event 2 oviposition -- for generation 1. | 405 # Oviposition -- for generation 1. |
408 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) { | 406 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) { |
409 # Vittelogenic stage, 1st generation | 407 # Vittelogenic stage, 1st generation |
410 if (vector.individual[4] == 0) { | 408 if (vector.individual[4] == 0) { |
411 # Just turned in vittelogenic stage. | 409 # Just turned in vittelogenic stage. |
412 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size)) | 410 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size)) |
434 new.vector <- t(matrix(new.vector, nrow=5)) | 432 new.vector <- t(matrix(new.vector, nrow=5)) |
435 # Group with total eggs laid in that day. | 433 # Group with total eggs laid in that day. |
436 birth.vector <- rbind(birth.vector, new.vector) | 434 birth.vector <- rbind(birth.vector, new.vector) |
437 } | 435 } |
438 } | 436 } |
439 # Event 3 development (with diapause determination). | 437 # Egg to young nymph. |
440 # Event 3.1 egg development to young nymph (vector.individual[2]=0 -> egg). | |
441 if (vector.individual[2] == 0) { | 438 if (vector.individual[2] == 0) { |
442 # Egg stage. | |
443 # Add average temperature for current day. | 439 # Add average temperature for current day. |
444 vector.individual[3] <- vector.individual[3] + averages.temp | 440 vector.individual[3] <- vector.individual[3] + averages.temp |
445 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) { | 441 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) { |
446 # From egg to young nymph, degree-days requirement met. | 442 # From egg to young nymph, degree-days requirement met. |
447 current.gen <- vector.individual[1] | 443 current.gen <- vector.individual[1] |
452 # Add 1 day in current stage. | 448 # Add 1 day in current stage. |
453 vector.individual[4] <- vector.individual[4] + 1 | 449 vector.individual[4] <- vector.individual[4] + 1 |
454 } | 450 } |
455 vector.matrix[i,] <- vector.individual | 451 vector.matrix[i,] <- vector.individual |
456 } | 452 } |
457 # Event 3.2 young nymph to old nymph (vector.individual[2]=1 -> young nymph: determines diapause). | 453 # Young nymph to old nymph. |
458 if (vector.individual[2] == 1) { | 454 if (vector.individual[2] == 1) { |
459 # Young nymph stage. | |
460 # Add average temperature for current day. | 455 # Add average temperature for current day. |
461 vector.individual[3] <- vector.individual[3] + averages.temp | 456 vector.individual[3] <- vector.individual[3] + averages.temp |
462 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) { | 457 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) { |
463 # From young to old nymph, degree_days requirement met. | 458 # From young to old nymph, degree_days requirement met. |
464 current.gen <- vector.individual[1] | 459 current.gen <- vector.individual[1] |
472 # Add 1 day in current stage. | 467 # Add 1 day in current stage. |
473 vector.individual[4] <- vector.individual[4] + 1 | 468 vector.individual[4] <- vector.individual[4] + 1 |
474 } | 469 } |
475 vector.matrix[i,] <- vector.individual | 470 vector.matrix[i,] <- vector.individual |
476 } | 471 } |
477 # Event 3.3 old nymph to adult: previttelogenic or diapausing? | 472 # Old nymph to adult: previttelogenic or diapausing? |
478 if (vector.individual[2] == 2) { | 473 if (vector.individual[2] == 2) { |
479 # Old nymph stage. | |
480 # Add average temperature for current day. | 474 # Add average temperature for current day. |
481 vector.individual[3] <- vector.individual[3] + averages.temp | 475 vector.individual[3] <- vector.individual[3] + averages.temp |
482 if (vector.individual[3] >= (200+opt$adult_accumulation)) { | 476 if (vector.individual[3] >= (200+opt$adult_accumulation)) { |
483 # From old to adult, degree_days requirement met. | 477 # From old to adult, degree_days requirement met. |
484 current.gen <- vector.individual[1] | 478 current.gen <- vector.individual[1] |
485 if (vector.individual[5] == 0) { | 479 if (vector.individual[5] == 0) { |
486 # Non-diapausing adult -- previttelogenic. | 480 # Previttelogenic. |
487 vector.individual <- c(current.gen, 3, 0, 0, 0) | 481 vector.individual <- c(current.gen, 3, 0, 0, 0) |
488 } | 482 } |
489 else { | 483 else { |
490 # Diapausing. | 484 # Diapausing. |
491 vector.individual <- c(current.gen, 5, 0, 0, 1) | 485 vector.individual <- c(current.gen, 5, 0, 0, 1) |
495 # Add 1 day in current stage. | 489 # Add 1 day in current stage. |
496 vector.individual[4] <- vector.individual[4] + 1 | 490 vector.individual[4] <- vector.individual[4] + 1 |
497 } | 491 } |
498 vector.matrix[i,] <- vector.individual | 492 vector.matrix[i,] <- vector.individual |
499 } | 493 } |
500 # Event 4 growing of diapausing adult (unimportant, but still necessary). | 494 # Growing of diapausing adult (unimportant, but still necessary). |
501 if (vector.individual[2] == 5) { | 495 if (vector.individual[2] == 5) { |
502 vector.individual[3] <- vector.individual[3] + averages.temp | 496 vector.individual[3] <- vector.individual[3] + averages.temp |
503 vector.individual[4] <- vector.individual[4] + 1 | 497 vector.individual[4] <- vector.individual[4] + 1 |
504 vector.matrix[i,] <- vector.individual | 498 vector.matrix[i,] <- vector.individual |
505 } | 499 } |
569 newborn.replications[,N.replications] <- N.newborn | 563 newborn.replications[,N.replications] <- N.newborn |
570 adult.replications[,N.replications] <- N.adult | 564 adult.replications[,N.replications] <- N.adult |
571 death.replications[,N.replications] <- N.death | 565 death.replications[,N.replications] <- N.death |
572 | 566 |
573 P.replications[,N.replications] <- overwintering_adult.population | 567 P.replications[,N.replications] <- overwintering_adult.population |
568 P_adults.replications[,N.replications] <- P.adult | |
574 F1.replications[,N.replications] <- first_generation.population | 569 F1.replications[,N.replications] <- first_generation.population |
570 F1_adults.replications[,N.replications] <- F1.adult | |
575 F2.replications[,N.replications] <- second_generation.population | 571 F2.replications[,N.replications] <- second_generation.population |
576 P_adults.replications[,N.replications] <- P.adult | |
577 F1_adults.replications[,N.replications] <- F1.adult | |
578 F2_adults.replications[,N.replications] <- F2.adult | 572 F2_adults.replications[,N.replications] <- F2.adult |
579 | 573 |
580 population.replications[,N.replications] <- total.population | 574 population.replications[,N.replications] <- total.population |
581 } | 575 } |
582 | 576 |
626 F2_adults.std_error <- apply(F2_adults.replications, 1, sd) / sqrt(opt$replications) | 620 F2_adults.std_error <- apply(F2_adults.replications, 1, sd) / sqrt(opt$replications) |
627 | 621 |
628 # Display the total number of days in the Galaxy history item blurb. | 622 # Display the total number of days in the Galaxy history item blurb. |
629 cat("Number of days: ", opt$num_days, "\n") | 623 cat("Number of days: ", opt$num_days, "\n") |
630 | 624 |
631 dev.new(width=20, height=40) | 625 dev.new(width=20, height=30) |
632 | 626 |
633 # Start PDF device driver to save charts to output. | 627 # Start PDF device driver to save charts to output. |
634 pdf(file=opt$output, width=20, height=40, bg="white") | 628 pdf(file=opt$output, width=20, height=30, bg="white") |
635 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)) | 629 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)) |
636 | 630 |
637 # Data analysis and visualization plots | 631 # Data analysis and visualization plots only within a single calendar year. |
638 # only within a single calendar year. | |
639 days <- c(1:opt$num_days) | 632 days <- c(1:opt$num_days) |
640 start_date <- temperature_data_frame$DATE[1] | 633 start_date <- temperature_data_frame$DATE[1] |
641 end_date <- temperature_data_frame$DATE[opt$num_days] | 634 end_date <- temperature_data_frame$DATE[opt$num_days] |
642 | 635 |
643 # Subfigure 1: population size by life stage | 636 # Subfigure 1: population size by life stage. |
644 maxval <- max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error) | 637 maxval <- max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error) |
645 render_chart("pop_size_by_life_stage", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, | 638 render_chart("pop_size_by_life_stage", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, |
646 opt$std_error_plot, adults, nymphs, eggs, adults.std_error, nymphs.std_error, eggs.std_error) | 639 opt$std_error_plot, adults, nymphs, eggs, adults.std_error, nymphs.std_error, eggs.std_error) |
647 # Subfigure 2: population size by generation | 640 # Subfigure 2: population size by generation. |
648 maxval <- max(F2) | 641 maxval <- max(F2) |
649 render_chart("pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, | 642 render_chart("pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, |
650 opt$std_error_plot, P, F1, F2, P.std_error, F1.std_error, F2.std_error) | 643 opt$std_error_plot, P, F1, F2, P.std_error, F1.std_error, F2.std_error) |
651 # Subfigure 3: adult population size by generation | 644 # Subfigure 3: adult population size by generation. |
652 maxval <- max(F2_adults) + 100 | 645 maxval <- max(F2_adults) + 100 |
653 render_chart("adult_pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, | 646 render_chart("adult_pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, |
654 opt$std_error_plot, P_adults, F1_adults, F2_adults, P_adults.std_error, F1_adults.std_error, F2_adults.std_error) | 647 opt$std_error_plot, P_adults, F1_adults, F2_adults, P_adults.std_error, F1_adults.std_error, F2_adults.std_error) |
655 | 648 |
656 # Turn off device driver to flush output. | 649 # Turn off device driver to flush output. |