0
|
1 #!/usr/bin/env Rscript
|
|
2
|
11
|
3 #suppressPackageStartupMessages(library("optparse"))
|
|
4 #suppressPackageStartupMessages(library("rjson"))
|
0
|
5
|
13
|
6 options_list <- list(
|
0
|
7 make_option(c("-s", "--save_log"), action="store_true", default=FALSE, help="Save R logs"),
|
|
8 make_option(c("-m", "--output_r_log"), action="store", help="Output dataset for R logs"),
|
|
9 make_option(c("-o", "--output"), action="store", help="Output dataset")
|
|
10 )
|
|
11
|
13
|
12 parser <- OptionParser(usage="%prog [options] file", options_list)
|
|
13 args <- parse_args(parser, positional_arguments=TRUE)
|
|
14 opt <- args$options
|
|
15
|
|
16
|
0
|
17 daylength = function(L){
|
|
18 # from Forsythe 1995
|
|
19 p = 0.8333
|
|
20 dl <- NULL
|
|
21 for (i in 1:365) {
|
|
22 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186)))
|
|
23 phi <- asin(0.39795 * cos(theta))
|
|
24 dl[i] <- 24 - 24/pi * acos((sin(p * pi/180) + sin(L * pi/180) * sin(phi))/(cos(L * pi/180) * cos(phi)))
|
|
25 }
|
|
26 # return a vector of daylength in 365 days
|
|
27 dl
|
|
28 }
|
|
29
|
|
30
|
|
31 # source("daylength.R")
|
|
32 hourtemp = function(L,date){
|
|
33 # L = 37.5 specify this in main program
|
|
34 # base development threshold for BMSB
|
|
35 threshold <- 12.7
|
|
36 # threshold2 <- threshold/24 degree hour accumulation
|
|
37 #expdata <- tempdata[1:365,11:13] # Use daily max, min, mean
|
|
38 # daily minimum
|
|
39 dnp <- expdata[date,2]
|
|
40 # daily maximum
|
|
41 dxp <- expdata[date,3]
|
|
42 dmean <- 0.5 * (dnp + dxp)
|
|
43 #if (dmean>0) {
|
|
44 #dnp <- dnp - k1 * dmean
|
|
45 #dxp <- dxp + k2 * dmean
|
|
46 #} else {
|
|
47 #dnp <- dnp + k1 * dmean
|
|
48 #dxp <- dxp - k2 * dmean
|
|
49 #}
|
|
50 dd <- 0 # initialize degree day accumulation
|
|
51
|
|
52 if (dxp<threshold) {
|
|
53 dd <- 0
|
|
54 } else {
|
|
55 # extract daylength data for entire year
|
|
56 dlprofile <- daylength(L)
|
|
57 # initialize hourly temperature
|
|
58 T <- NULL
|
|
59 #initialize degree hour vector
|
|
60 dh <- NULL
|
|
61 # calculate daylength in given date
|
|
62 # date <- 200
|
|
63 y <- dlprofile[date]
|
|
64 # night length
|
|
65 z <- 24 - y
|
|
66 # lag coefficient
|
|
67 a <- 1.86
|
|
68 # night coefficient
|
|
69 b <- 2.20
|
|
70 #import raw data set
|
|
71 #tempdata <- read.csv("tempdata.csv")
|
|
72 # Should be outside function otherwise its redundant
|
|
73 # sunrise time
|
|
74 risetime <- 12 - y/2
|
|
75 # sunset time
|
|
76 settime <- 12 + y/2
|
|
77 ts <- (dxp - dnp) * sin(pi * (settime-5)/(y + 2 * a)) + dnp
|
|
78 for (i in 1:24) {
|
|
79 if (i > risetime && i < settime) {
|
|
80 # number of hours after Tmin until sunset
|
|
81 m <- i - 5
|
|
82 T[i] = (dxp - dnp) * sin(pi * m/(y + 2 * a)) + dnp
|
|
83 if (T[i]<8.4) {
|
|
84 dh[i] <- 0
|
|
85 } else {
|
|
86 dh[i] <- T[i] - 8.4
|
|
87 }
|
|
88 } else if (i>settime) {
|
|
89 n <- i - settime
|
|
90 T[i] = dnp + (ts - dnp) * exp(-b * n/z)
|
|
91 if (T[i]<8.4) {
|
|
92 dh[i] <- 0
|
|
93 } else {
|
|
94 dh[i] <- T[i] - 8.4
|
|
95 }
|
|
96 } else {
|
|
97 n <- i + 24 - settime
|
|
98 T[i] = dnp + (ts - dnp) * exp(-b * n / z)
|
|
99 if (T[i]<8.4) {
|
|
100 dh[i] <- 0
|
|
101 } else {
|
|
102 dh[i] <- T[i] - 8.4
|
|
103 }
|
|
104 }
|
|
105 }
|
|
106 dd <- sum(dh) / 24
|
|
107 }
|
|
108 return = c(dmean, dd)
|
|
109 return
|
|
110 }
|
|
111
|
|
112
|
|
113 mortality.egg = function(temperature) {
|
|
114 if (temperature < 12.7) {
|
|
115 mort.prob = 1
|
|
116 } else {
|
|
117 # 100% mortality if <12.7
|
|
118 mort.prob = 0.8 - temperature / 40
|
|
119 if (mort.prob<0) {
|
|
120 mort.prob = 0.01
|
|
121 }
|
|
122 }
|
|
123 return = mort.prob
|
|
124 return
|
|
125 }
|
|
126
|
|
127
|
|
128 mortality.nymph = function(temperature) {
|
|
129 if (temperature<12.7) {
|
|
130 mort.prob = 0.03
|
|
131 } else {
|
|
132 # at low temperature
|
|
133 mort.prob = -temperature * 0.0008 + 0.03
|
|
134 }
|
|
135 return = mort.prob
|
|
136 return
|
|
137 }
|
|
138
|
|
139
|
|
140 mortality.adult = function(temperature) {
|
|
141 if (temperature < 12.7) {
|
|
142 mort.prob = 0.002
|
|
143 } else {
|
|
144 mort.prob = -temperature * 0.0005 + 0.02
|
|
145 }
|
|
146 return = mort.prob
|
|
147 return
|
|
148 }
|
|
149
|
|
150
|
|
151 # model initialization
|
11
|
152 # TODO: add tool params for the following options.
|
0
|
153 # start with 1000 individuals
|
|
154 n <- 1000
|
|
155 # Generation, Stage, DD, T, Diapause
|
|
156 vec.ini <- c(0,3,0,0,0)
|
|
157 # overwintering, previttelogenic, DD = 0, T = 0, no-diapause
|
|
158 vec.mat <- rep(vec.ini,n)
|
|
159 # complete matrix for the population
|
|
160 vec.mat <- t(matrix(vec.mat, nrow = 5))
|
|
161 # latitude for Asheville NC
|
|
162 L <- 35.58
|
|
163 # complete photoperiod profile in a year, requires daylength function
|
|
164 ph.p <- daylength(L)
|
|
165
|
|
166 # load temperature data@location/year
|
|
167 load("asheville2014.Rdat")
|
|
168
|
|
169 # time series of population size
|
|
170 tot.pop <- NULL
|
|
171
|
|
172 # gen.0 pop size
|
|
173 gen0.pop <- rep(0, 365)
|
|
174 gen1.pop <- rep(0, 365)
|
|
175 gen2.pop <- rep(0, 365)
|
|
176
|
|
177 # aggregate
|
|
178 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365)
|
|
179 g0.adult <- g1.adult <- g2.adult <- rep(0, 365)
|
|
180
|
|
181 # birth death adults
|
|
182 N.newborn <- N.death <- N.adult <- rep(0, 365)
|
|
183
|
|
184 # degree-day accumulation
|
|
185 dd.day <- rep(0, 365)
|
|
186
|
|
187 # start tick
|
|
188 ptm <- proc.time()
|
|
189
|
|
190 for (n.sim in 1:1000) {
|
|
191 # loop through 1000 simulations
|
|
192 for (day in 1:365) {
|
|
193 # loop through 365 day/yr
|
|
194 photoperiod <- ph.p[day]
|
|
195 # photoperiod in the day
|
|
196 temp.profile <- hourtemp(L,day)
|
|
197 # temperature profile
|
|
198 mean.temp <- temp.profile[1]
|
|
199 # mean temp
|
|
200 dd.temp <- temp.profile[2]
|
|
201 # degree-day
|
|
202 dd.day[day] <- dd.temp
|
|
203 death.vec <- NULL
|
|
204 # trash bin for death
|
|
205 birth.vec <- NULL
|
|
206 # record new born
|
|
207 for (i in 1:n) {
|
|
208 # loop through all individual
|
|
209 vec.ind <- vec.mat[i,]
|
|
210 # find individual record
|
|
211 # first of all, still alive?
|
|
212 if (vec.ind[2] == 0) {
|
|
213 # egg
|
|
214 death.prob = mortality.egg(mean.temp)
|
|
215 } else if (vec.ind[2] == 1 | vec.ind[2] == 2) {
|
|
216 # nymph
|
|
217 death.prob = mortality.nymph(mean.temp)
|
|
218 } else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) {
|
|
219 # for adult
|
|
220 death.prob = mortality.adult(mean.temp)
|
|
221 }
|
|
222 u.d <- runif(1)
|
|
223 if (u.d<death.prob) {
|
|
224 death.vec <- c(death.vec,i)
|
|
225 } else {
|
|
226 # aggregrate index of dead bug
|
|
227 # event 1 end of diapause
|
|
228 if (vec.ind[1] == 0 && vec.ind[2] == 3) {
|
|
229 # overwintering adult (previttelogenic)
|
|
230 if (photoperiod>13.5 && vec.ind[3] > 68 && day < 180) {
|
|
231 # add 68C to become fully reproductively matured
|
|
232 # transfer to vittelogenic
|
|
233 vec.ind <- c(0,4,0,0,0)
|
|
234 vec.mat[i,] <- vec.ind
|
|
235 } else {
|
|
236 # add to DD
|
|
237 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
238 vec.ind[4] <- vec.ind[4] + 1 # add 1 day in current stage
|
|
239 vec.mat[i,] <- vec.ind
|
|
240 }
|
|
241 }
|
|
242 if (vec.ind[1]!=0 && vec.ind[2] == 3) {
|
|
243 # NOT overwintering adult (previttelogenic)
|
|
244 current.gen <- vec.ind[1]
|
|
245 if (vec.ind[3]>68) {
|
|
246 # add 68C to become fully reproductively matured
|
|
247 # transfer to vittelogenic
|
|
248 vec.ind <- c(current.gen,4,0,0,0)
|
|
249 vec.mat[i,] <- vec.ind
|
|
250 } else {
|
|
251 # add to DD
|
|
252 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
253 # add 1 day in current stage
|
|
254 vec.ind[4] <- vec.ind[4] + 1
|
|
255 vec.mat[i,] <- vec.ind
|
|
256 }
|
|
257 }
|
|
258 # event 2 oviposition -- where population dynamics comes from
|
|
259 # vittelogenic stage, overwintering generation
|
|
260 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp>10) {
|
|
261 if (vec.ind[4] == 0) {
|
|
262 # just turned in vittelogenic stage
|
|
263 n.birth = round(runif(1,10,20))
|
|
264 } else {
|
|
265 p.birth = 1/4/75
|
|
266 # prob of birth
|
|
267 u1 <- runif(1)
|
|
268 if (u1<p.birth) {
|
|
269 n.birth = n.birth
|
|
270 }
|
|
271 }
|
|
272 # add to DD
|
|
273 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
274 # add 1 day in current stage
|
|
275 vec.ind[4] <- vec.ind[4] + 1
|
|
276 vec.mat[i,] <- vec.ind
|
|
277 if (n.birth>0) {
|
|
278 # add new birth -- might be in different generations
|
|
279 # generation + 1
|
|
280 new.gen <- vec.ind[1] + 1
|
|
281 # egg profile
|
|
282 new.ind <- c(new.gen,0,0,0,0)
|
|
283 new.vec <- rep(new.ind,n.birth)
|
|
284 # update batch of egg profile
|
|
285 new.vec <- t(matrix(new.vec,nrow = 5))
|
|
286 # group with total eggs laid in that day
|
|
287 birth.vec <- rbind(birth.vec,new.vec)
|
|
288 }
|
|
289 }
|
|
290 # event 2 oviposition -- for gen 1.
|
|
291 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp>12.5 && day<222) {
|
|
292 # vittelogenic stage, 1st generation
|
|
293 if (vec.ind[4] == 0) {
|
|
294 # just turned in vittelogenic stage
|
|
295 n.birth = round(runif(1,10,20))
|
|
296 } else {
|
|
297 p.birth = 1/4/75
|
|
298 u1 <- runif(1)
|
|
299 if (u1<p.birth) {
|
|
300 n.birth = n.birth
|
|
301 }
|
|
302 }
|
|
303 # add to DD
|
|
304 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
305 # add 1 day in current stage
|
|
306 vec.ind[4] <- vec.ind[4] + 1
|
|
307 vec.mat[i,] <- vec.ind
|
|
308 if (n.birth>0) {
|
|
309 # add new birth -- might be in different generations
|
|
310 # generation + 1
|
|
311 new.gen <- vec.ind[1] + 1
|
|
312 # egg profile
|
|
313 new.ind <- c(new.gen,0,0,0,0)
|
|
314 new.vec <- rep(new.ind,n.birth)
|
|
315 # update batch of egg profile
|
|
316 new.vec <- t(matrix(new.vec,nrow = 5))
|
|
317 # group with total eggs laid in that day
|
|
318 birth.vec <- rbind(birth.vec,new.vec)
|
|
319 }
|
|
320 }
|
|
321 # event 3 development (with diapause determination)
|
|
322 # event 3.1 egg development to young nymph (vec.ind[2] = 0 -> egg)
|
|
323 # egg stage
|
|
324 if (vec.ind[2] == 0) {
|
|
325 # add to DD
|
|
326 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
327 # from egg to young nymph
|
|
328 if (vec.ind[3] >= 53.30 && -0.9843 * dd.temp + 33.438>0) {
|
|
329 current.gen <- vec.ind[1]
|
|
330 # transfer to young nym stage
|
|
331 vec.ind <- c(current.gen,1,0,0,0)
|
|
332 } else {
|
|
333 # add 1 day in current stage
|
|
334 vec.ind[4] <- vec.ind[4] + 1
|
|
335 }
|
|
336 vec.mat[i,] <- vec.ind
|
|
337 }
|
|
338 # event 3.2 young nymph to old nymph (vec.ind[2] = 1 -> young nymph: determines diapause)
|
|
339 # young nymph stage
|
|
340 if (vec.ind[2] == 1) {
|
|
341 # add to DD
|
|
342 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
343 # from young to old nymph
|
|
344 if (vec.ind[3] >= 537/2 && -0.45 * dd.temp + 18>0) {
|
|
345 current.gen <- vec.ind[1]
|
|
346 # transfer to old nym stage
|
|
347 vec.ind <- c(current.gen,2,0,0,0)
|
|
348 # prepare for diapausing
|
|
349 if (photoperiod<13.5 && day > 180) {
|
|
350 vec.ind[5] <- 1
|
|
351 }
|
|
352 } else {
|
|
353 # add 1 day in current stage
|
|
354 vec.ind[4] <- vec.ind[4] + 1
|
|
355 }
|
|
356 vec.mat[i,] <- vec.ind
|
|
357 }
|
|
358 # event 3.3 old nymph to adult: previttelogenic or diapausing?
|
|
359 # old nymph stage
|
|
360 if (vec.ind[2] == 2) {
|
|
361 # add to DD
|
|
362 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
363 # from old to adult
|
|
364 if (vec.ind[3] >= 537/2 && -0.50 * dd.temp + 22>0) {
|
|
365 current.gen <- vec.ind[1]
|
|
366 # non-diapausing adult -- previttelogenic
|
|
367 if (vec.ind[5] == 0) {
|
|
368 vec.ind <- c(current.gen,3,0,0,0)
|
|
369 # diapausing
|
|
370 } else {
|
|
371 vec.ind <- c(current.gen,5,0,0,1)
|
|
372 }
|
|
373 } else {
|
|
374 # add 1 day in current stage
|
|
375 vec.ind[4] <- vec.ind[4] + 1
|
|
376 }
|
|
377 vec.mat[i,] <- vec.ind
|
|
378 }
|
|
379 # event 4 growing of diapausing adult (unimportant, but still necessary)##
|
|
380 if (vec.ind[2] == 5) {
|
|
381 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
382 vec.ind[4] <- vec.ind[4] + 1
|
|
383 vec.mat[i,] <- vec.ind
|
|
384 }
|
|
385 } # else if it is still alive
|
|
386 } # end of the individual bug loop
|
|
387 # find how many died
|
|
388 n.death <- length(death.vec)
|
|
389 if (n.death>0) {
|
|
390 vec.mat <- vec.mat[-death.vec, ]
|
|
391 }
|
|
392 # remove record of dead
|
|
393 # find how many new born
|
|
394 n.newborn <- length(birth.vec[,1])
|
|
395 vec.mat <- rbind(vec.mat,birth.vec)
|
|
396 # update population size for the next day
|
|
397 n <- n-n.death + n.newborn
|
|
398
|
|
399 # aggregate results by day
|
|
400 tot.pop <- c(tot.pop,n)
|
|
401 # egg
|
|
402 s0 <- sum(vec.mat[,2] == 0)
|
|
403 # young nymph
|
|
404 s1 <- sum(vec.mat[,2] == 1)
|
|
405 # old nymph
|
|
406 s2 <- sum(vec.mat[,2] == 2)
|
|
407 # previtellogenic
|
|
408 s3 <- sum(vec.mat[,2] == 3)
|
|
409 # vitellogenic
|
|
410 s4 <- sum(vec.mat[,2] == 4)
|
|
411 # diapausing
|
|
412 s5 <- sum(vec.mat[,2] == 5)
|
|
413 # overwintering adult
|
|
414 gen0 <- sum(vec.mat[,1] == 0)
|
|
415 # first generation
|
|
416 gen1 <- sum(vec.mat[,1] == 1)
|
|
417 # second generation
|
|
418 gen2 <- sum(vec.mat[,1] == 2)
|
|
419 # sum of all adults
|
|
420 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
|
|
421 # gen.0 pop size
|
|
422 gen0.pop[day] <- gen0
|
|
423 gen1.pop[day] <- gen1
|
|
424 gen2.pop[day] <- gen2
|
|
425 S0[day] <- s0
|
|
426 S1[day] <- s1
|
|
427 S2[day] <- s2
|
|
428 S3[day] <- s3
|
|
429 S4[day] <- s4
|
|
430 S5[day] <- s5
|
|
431 g0.adult[day] <- sum(vec.mat[,1] == 0)
|
|
432 g1.adult[day] <- sum((vec.mat[,1] == 1 & vec.mat[,2] == 3) | (vec.mat[,1] == 1 & vec.mat[,2] == 4) | (vec.mat[,1] == 1 & vec.mat[,2] == 5))
|
|
433 g2.adult[day] <- sum((vec.mat[,1] == 2 & vec.mat[,2] == 3) | (vec.mat[,1] == 2 & vec.mat[,2] == 4) | (vec.mat[,1] == 2 & vec.mat[,2] == 5))
|
|
434 N.newborn[day] <- n.newborn
|
|
435 N.death[day] <- n.death
|
|
436 N.adult[day] <- n.adult
|
|
437 }
|
11
|
438 #print(n.sim)
|
0
|
439 }
|
|
440
|
|
441 proc.time() - ptm
|
|
442 dd.cum <- cumsum(dd.day)
|
11
|
443 save(dd.day, dd.cum, S0, S1, S2, S3, S4, S5, N.newborn, N.death, N.adult, tot.pop, gen0.pop, gen1.pop, gen2.pop, g0.adult, g1.adult, g2.adult, file=opt$output)
|