-
Notifications
You must be signed in to change notification settings - Fork 0
/
binXdrives.R
executable file
·1741 lines (1588 loc) · 65.7 KB
/
binXdrives.R
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env R
# Description ------------------------------------------------------------------
# The script models the impact of a Bipartite Expression Drive (BED) on a
# population. The BED may be intended to spread 1) toxic proteins that sterilize
# BED-carrying females or 2) lethal seminal proteins that BED-carrying males
# transfer to females, killing them right after mating.
# Libraries --------------------------------------------------------------------
suppressPackageStartupMessages({
library("optparse")
library("stringr")
library("zeallot")
library("rlist")
library("dplyr")
library("bettermc")
library("logger")
library("ggplot2")
})
# Define logging format --------------------------------------------------------
logger <- layout_glue_generator(format = paste(
"[{crayon::italic(format(time, \"%Y-%m-%d %H:%M:%S\"))}]",
"{crayon::bold(colorize_by_log_level(level, levelr))}",
"{grayscale_by_log_level(msg, levelr)}"))
log_layout(logger)
# Auxiliary Functions ----------------------------------------------------------
validate_argument_required <- function(options, argument) {
if (is.null(options[[argument]])) {
log_error("--{argument} must be provided. Run the script with --help for ",
"more details.")
quit()
}
}
validate_argument_positive <- function(options, argument) {
if (options[[argument]] < 0) {
log_error("--{argument} must be a positive value: {options[[argument]]}. ",
"Run the script with --help for more details.")
quit()
}
}
validate_argument_in_list <- function(options, argument, items) {
if (!(options[[argument]] %in% items)) {
log_error("--{argument} doesn't match an expected value: ",
"{options[[argument]]} [{paste0(items, collapse = \", \")}]. ",
"Run the script with --help for more details.")
quit()
}
}
validate_argument_in_range <- function(options, argument, left, right) {
if (!between(options[[argument]], left, right)) {
log_error("--{argument} is out of range: {options[[argument]]} ",
"[{left}, {right}]. Run the script with --help for more details.")
quit()
}
}
validate_argument_integer_in_range <- function(options, argument, left, right) {
if (options[[argument]] %% 1 != 0) {
log_error("--{argument} must be an integer value: {options[[argument]]}. ",
"Run the script with --help for more details.")
quit()
}
if (!between(options[[argument]], left, right)) {
log_error("--{argument} is out of range: {options[[argument]]} ",
"[{left}, {right}]. Run the script with --help for more details.")
quit()
}
}
get_gamete_haplotypes <- function(value, vector, allele1, allele2, homing_efficiency, resistance_formation) {
#
# Params
# value: character
# genotype of the gametes provider
# vector: array(double)
# initial haplotypes probabilities
# allele1: character
# maternal allele
# allele2: character
# paternal allele
# homing_efficiency: double [0, 1]
# drive conversion probability
# resistance_formation: double [0, 1]
# probability of resistance allele formation
#
# Output
# Returns a probability vector which is added to the initial haplotype
# probabilities.
a <- vector[1]
b <- vector[2]
c <- vector[3]
# Create the M, m, N, n versions
l1u <- toupper(allele1)
l1l <- tolower(allele1)
l2u <- toupper(allele2)
l2l <- tolower(allele2)
# Prepare a list with all the combinations to use in the if clause
options <- list(
"Mm"=paste0(l1u, l1l),
"mM"=paste0(l1l, l1u),
"MM"=paste0(l1u, l1u),
"mm"=paste0(l1l, l1l),
"Mn"=paste0(l1u, l2l),
"nM"=paste0(l2l, l1u),
"mn"=paste0(l1l, l2l),
"nm"=paste0(l2l, l1l),
"nn"=paste0(l2l, l2l)
)
if (value %in% c(options$Mm, options$mM)) {
out <- c(
a + (1 + homing_efficiency) / 2,
b + resistance_formation / 2,
c + 1 - (1 + homing_efficiency) / 2 - resistance_formation / 2
)
} else if (value %in% c(options$MM)) {
out <- c(a + 1, b, c)
} else if (value %in% c(options$mm)) {
out <- c(a, b, c + 1)
} else if (value %in% c(options$Mn, options$nM)) {
out <- c(a + 0.5, b + 0.5, c)
} else if (value %in% c(options$mn, options$nm)) {
out <- c(a, b + 0.5, c + 0.5)
} else if (value %in% c(options$nn)) {
out <- c(a, b + 1, c)
}
return(out)
}
initialize_simulation <- function(initial_population_size = initial_population_size) {
#
# Params
# initial_population_size: integer
# initial number of virgin adults (males plus females)
# it is a constant (see --carrying capacity)
#
# Notes
# Frequencies/numbers refers to virgin adults right after emerging from pupae
# Each generation starts with the egg stage
# First raw of the output table represent the last generation before release
# We refers to this generation as generation zero, so it is the SECOND raw the
# one that reflects the first generation after release
#
# Output
# Returns a data frame to record each generation results
# each row represents a generation (total number of generation is 'gen + 1')
# initial_pop_size: initial number of virgin adults (males and females)
# mothers: number of inseminated females that will produce next generation eggs
# terminated_females: number of females killed by terminators
# freq_drive_A: drive A allelic frequency
# freq_drive_B: drive B allelic frequency
# freq_A_B_: frequency of AB carrying genotypes (A_B_)
# freq_A_resistance: A_resistance allelic frequency
# freq_B_resistance: B_resistance allelic frequency
output <-
data.frame(
generation = 0,
drive_release = TRUE,
virgin_adults = initial_population_size,
virgin_females = round(initial_population_size / 2),
unmated_females = 0,
terminated_females = 0,
mothers = round(initial_population_size / 2),
freq_A_B_ = 0,
freq_drive_A = 0,
freq_drive_B = 0,
freq_A_resistance = 0,
freq_B_resistance = 0,
genetic_load = 0
)
if (bed_design == "no") { # freq_drive_A correction for single drives
output$freq_drive_A[1] <- 1
}
return(output)
}
initialize_genotypes <- function(initial_population_size,
wt_genotype,
A_genotype,
release_A,
B_genotype,
release_B
) {
#
# Params
# initial_population_size: integer
# initial number of virgin adults (males plus females)
# it is a constant (see --carrying capacity)
# wt_genotype: string
# homozygous wt genotype
# wt_genotype is a fixed parameter ("aabb")
# A_genotype: string
# genotype of transgenic males carrying the A drive
# A_genotype is a constant ("AAbb")
# release_A: integer
# number of A-carrying males to be released in the first generation
# it is a constant
# B_genotype: string
# genotype of transgenic males carrying the B drive
# B_genotype is a constant ("aaBB")
# release_B: integer
# number of B-carrying males to be released in the first generation
# it is a constant
#
# Output
# Returns the genotypes and numbers of virgin adults in the generation zero
# (in the generation zero number of females and males are equal
# until transgenic males are added)
out <- vector("list", length = 5)
# number of virgin females
out[[1]] <- round(initial_population_size / 2)
# virgin females genotypes
out[[2]] <- rep(wt_genotype, out[[1]])
# virgin males genotypes
out[[3]] <- c(out[[2]], rep(A_genotype, release_A), rep(B_genotype, release_B))
# after-release number of virgin males
out[[4]] <- length(out[[3]])
# after-release number of adults
out[[5]] <- out[[1]] + out[[4]]
return(out)
}
make_rerelease_decision <- function(generation,
males,
estimated_freqA_B_,
estimated_freqA,
estimated_freqB,
simulation_table,
re_release_limit,
re_release_lower_limit,
critical_difference,
A_genotype,
release_A,
B_genotype,
release_B
) {
#
# Params
# estimated_freqA_B_: double (0, 1)
# frequency of A_B_ genotypes of a random sample of adults
# estimated_freqA: double (0, 1)
# A allelic frequency of a random sample of adults
# estimated_freqB: double (0, 1)
# B allelic frequency of a random sample of adults
# simulation_table: dataframe
# table that records simulation results per generation
# see initialize_simulation function
# re_release_limit: double (0, 1)
# upper limit of estimated_freqA_B_ for re-release to occur
# it is a constant
# re_release_lower_limit: double (0, 1)
# lower limit of estimated_freqA_B_ for re-release to occur
# it is a constant
# critical_difference: double (0, 1)
# lower limit of the difference between estimated allelic frequencies
# for re-release to occur
# it is a constant
# A_genotype: string
# genotype of transgenic males carrying the A drive
# A_genotype is a constant ("AAbb")
# release_A: integer
# number of A-carrying males to be released in the first generation
# it is a constant
# B_genotype: string
# genotype of transgenic males carrying the B drive
# B_genotype is a constant ("aaBB")
# release_B: integer
# number of B-carrying males to be released in the first generation
# it is a constant
#
# Output
# Returns the decision of re-release transgenic males carrying the
# less frequent drive and, if re-release occurs, it also updates
# after-re-release genotypes and numbers of adult males and females
simulation_table$drive_release[generation] <- FALSE
if (estimated_freqA_B_ > re_release_lower_limit && estimated_freqA_B_ < re_release_limit) {
if (estimated_freqA > (1 + critical_difference) * estimated_freqB) {
males <- c(males, rep(B_genotype, release_B))
simulation_table$drive_release[generation] <- TRUE
} else if ((1 + critical_difference) * estimated_freqA < estimated_freqB) {
males <- c(males, rep(A_genotype, release_A))
simulation_table$drive_release[generation] <- TRUE
}
}
out <- vector("list", length = 2)
out[[1]] <- simulation_table
out[[2]] <- males
return(out)
}
get_allee_effect <- function(population_size, critical_population_size, sensitivity) {
#
# Params
# population_size: integer
# number of virgin adults (males plus females)
# critical_population_size: double [>= 0]
# number of virgin adults (males plus females) below which Allee effect occurs
#
# Note
# This function models a mate-finding Allee effect that makes mating oportunities
# less likely
# The chances that virgin females achieve mating (and become mothers), as well
# as the number of mates per female, is modeled as a sigmoid function of the
# population size, yielding a 50% reduction at the critical population size.
#
# Output
# Returns a proportion number [0, 1] which can be interpreted as both
# 1) the probability that a virgin female find any mate and
# 2) the post-'mate-finding Allee effect' maintained fraction of lambda for
# the Poisson expected number of mates per mother.
out <- 1 - 1 / (1 + exp(sensitivity * (population_size - critical_population_size)))
return(out)
}
get_mate_finding_outcome <- function(virgin_females_number,
allee_effect,
females,
simulation_table,
generation
) {
#
# Params
# virgin_females_number: integer
# number of virgin females
# allee_effect: double (0, 1)
# see get_allee_effect function
# females
# concatenated females' genotypes
# simulation_table: dataframe
# table that records simulation results per generation
# see initialize_simulation function
#
# Note
# This function is only called when population size is sufficiently
# low and an Alee-effect occurs
#
# Output
# Returns a list of three objects after removing females that wont mate.
# [[1]] updates females object (female genotypes)
# [[2]] updates simulation object (recorded variables)
out <- vector("list", length = 2)
mate_finding <- sample(c(0, 1),
virgin_females_number,
replace = T,
prob = c(1 - allee_effect, allee_effect)
)
out[[1]] <- females[mate_finding == 1]
simulation_table$unmated_females[generation] <- virgin_females_number - length(out[[1]])
out[[2]] <- simulation_table
return(out)
}
get_mates_probability_density <- function(lambda, lower_bound, upper_bound) {
#
# Params
# lambda: double [> 0]
# Poisson parameter for the expected number of mates per female
# lower_bound: integer
# minimum admitted number of mates per female
# upper_bound: integer [> lower_bound]
# maximum admitted number of mates per female
#
# Output
# Returns a probability vector where:
# first element is the probability of having 'lower_bound' mates per female,
# element n is the probability of having 'lower_bound' plus n minus 1 mates per female, and
# last element is the probability of having 'upper_bound' mates per female.
out <- vapply(lower_bound:upper_bound, function(i) {
(lambda)^i * exp(-lambda) / factorial(i)
}, double(1))
out <- out / sum(out)
return(out)
}
get_male_mating_success <- function(unintended_reproductive_cost_A,
unintended_reproductive_cost_B,
bed_design,
males
) {
#
# Params
# unintended_reproductive_cost_A: double (0, 1)
# unintended dominant mating success cost for drive A
# if it is 0.1, A-carrying males will exhibit a 10% reduction
# unintended_reproductive_cost_B: double (0, 1)
# unintended dominant mating success cost for drive B
# if it is 0.1, B-carrying males will exhibit a 10% reduction
# bed_design: string
# "yes" (for BED designs) or "no" (for SDs)
# males: string
# a 1-dim vector containing the genotypes of the adult virgin males
#
# Note
# Unintended reproductive cost of each drive reduces male mating success
# of drive-carrying males. This cost is dominant.
#
# Output
# Returns a 1-dim vector with the mating success probability for all males
males_number <- length(males)
out <- rep(1, males_number)
for (i in 1:males_number) {
if (grepl("A", males[i]) && bed_design == "yes") {
out[i] <- out[i] - unintended_reproductive_cost_A
}
if (grepl("B", males[i])) {
out[i] <- out[i] - unintended_reproductive_cost_B
}
}
return(out)
}
get_potential_mates <- function(polyandry,
virgin_females_number,
males,
male_mating_success
) {
#
# Params
# polyandry: integer [> 0]
# maximum number of mates per mother (see get_mates_probability_density)
# virgin_females_number: integer [> 0]
# number of adult virgin females that will mate
# males: string
# a 1-dim vector containing the genotypes of the adult virgin males
# male_mating_success: double (0, 1)
# a 1-dim vector with the mating success probability for all males
#
# Output
# Returns a dataframe containing the genotypes of the males sampled
# as potential mates. Each row represents a mother and each column her
# potential mates
out <- data.frame(row.names = 1:virgin_females_number)
for (i in 1:polyandry) {
mates <- data.frame(male = sample(males,
virgin_females_number,
replace = T,
prob = male_mating_success
)
)
out <- cbind(out, mates)
}
return(out)
}
get_summary_input <- function(results_list, last_generation = 36, type = "undefined_design") {
simulations_number <- length(results_list)
generations <- last_generation + 1
for (i in 1:simulations_number) {
simulation_i <- results_list[[i]]
simulation_i$virgin_adults <-
simulation_i$virgin_adults / simulation_i$virgin_adults[1]
number_of_raws_i <- length(simulation_i[,1])
difference <- generations - number_of_raws_i
if (difference > 0) {
new_raws <- data.frame(
generation = 0,
drive_release = FALSE,
virgin_adults = 0,
virgin_females = 0,
unmated_females = 0,
terminated_females = 0,
mothers = 0,
freq_A_B_ = simulation_i$freq_A_B_[number_of_raws_i],
freq_drive_A = simulation_i$freq_drive_A[number_of_raws_i],
freq_drive_B = simulation_i$freq_drive_B[number_of_raws_i],
freq_A_resistance = simulation_i$freq_A_resistance[number_of_raws_i],
freq_B_resistance = simulation_i$freq_B_resistance[number_of_raws_i],
genetic_load = 0
)
new_raws <- do.call("rbind", replicate(difference, new_raws, simplify = FALSE))
new_raws$generation <- number_of_raws_i:last_generation
simulation_i <- rbind(simulation_i, new_raws)
} else if (difference < 0) {
simulation_i <- simulation_i[1:generations,]
}
simulation_i$simulation <- i
results_list[[i]] <- simulation_i
}
simulations_summary <- results_list[[1]]
if (simulations_number > 1) {
for (i in 2:simulations_number) {
simulations_summary <- rbind(simulations_summary, results_list[[i]])
}
}
return(simulations_summary)
}
print_crash <- function(simulation_table, generation) {
print(paste0(
"population crash! ",
"females: ",
simulation_table$virgin_females[generation],
", mothers: ",
simulation_table$mothers[generation]
)
)
}
save_crash_generation <- function(simulation_table,
generation,
stage
) {
#
# Params
# simulation_table: dataframe
# table that records simulation results per generation
# see initialize_simulation function
# generation: integer [> 0]
# generation number: generation 1 starts with eggs after first release
# stage: string
# "eggs", "larvae" or "adults"
#
# Note
# This function is called when population crashes
#
# Output
# Updates the simulation table adding the last generation values
out <- simulation_table
out[generation + 1, names(out) == "generation"] <- generation
out$drive_release[generation + 1] <- FALSE
out$virgin_adults[generation + 1] <- 0
out$virgin_females[generation + 1] <- 0
out$unmated_females[generation + 1] <- 0
out$terminated_females[generation + 1] <- 0
out$mothers[generation + 1] <- 0
out$freq_A_B_[generation + 1] <- out$freq_A_B_[generation]
out$freq_drive_A[generation + 1] <- out$freq_drive_A[generation]
out$freq_drive_B[generation + 1] <- out$freq_drive_B[generation]
out$freq_A_resistance[generation + 1] <- out$freq_A_resistance[generation]
out$freq_B_resistance[generation + 1] <- out$freq_B_resistance[generation]
if (stage != "adults") {
out$genetic_load[generation + 1] <- out$genetic_load[generation]
}
return(out)
}
save_simulation_results <- function(simulation_table,
generation,
total_adults_number,
virgin_females_number,
adults
) {
#
# Params
# simulation_table: dataframe
# table that records simulation results per generation
# see initialize_simulation function
# generation: integer [> 0]
# generation number: generation 1 starts with eggs after first release
# total_adults_number: integer [> 0]
# number of emerged adults (males plus females)
# virgin_females_number: integer [> 0]
# number of emerged females
# adults: string
# a 1-dim vector containing the genotypes of the emerged adults
# stage: string
# "eggs", "larvae" or "adults"
#
# Note
# This function is called at last generation
#
# Output
# Updates the simulation table adding the last generation values
out <- simulation_table
out[generation + 1, names(out) == "generation"] <- generation
out$virgin_adults[generation + 1] <- total_adults_number
out$virgin_females[generation + 1] <- virgin_females_number
out$freq_drive_A[generation + 1] <-
as.numeric(format(
sum(str_count(adults, "A")) / (2 * total_adults_number),
scientific = F,
digits = 9,
nsmall = 9
) )
out$freq_A_resistance[generation + 1] <-
as.numeric(format(
sum(str_count(adults, "x")) / (2 * total_adults_number),
scientific = F,
digits = 9,
nsmall = 9
) )
out$freq_drive_B[generation + 1] <-
as.numeric(format(
sum(str_count(adults, "B")) / (2 * total_adults_number),
scientific = F,
digits = 9,
nsmall = 9
) )
out$freq_B_resistance[generation + 1] <-
as.numeric(format(
sum(str_count(adults, "y")) / (2 * total_adults_number),
scientific = F,
digits = 9,
nsmall = 9
) )
out$freq_A_B_[generation + 1] <-
as.numeric(format(
sum(grepl("A", adults) & grepl("B", adults)) / total_adults_number,
scientific = F,
digits = 9,
nsmall = 9
) )
return(out)
}
# Arguments Definition ---------------------------------------------------------
option_list <- list(
# Simulation parameters
make_option(
c("--simulations", "-s"),
type = "integer",
metavar = "INTEGER",
default = 100,
help = "number of simulations/replicates/populations [default: %default]"
),
make_option(
c("--generations", "-g"),
type = "integer",
metavar = "INTEGER",
default = 30,
help = "number of generations to be simulated [default: %default]"
),
# Population parameters
make_option(
c("--carrying_capacity", "-k"),
type = "integer",
metavar = "INTEGER",
default = NULL,
help = paste("[REQUIRED] carrying capacity of adult population, which equals ",
"the initial adult population size")
),
make_option(
c("--fecundity", "-f"),
type = "double",
metavar = "DOUBLE",
default = 48,
help = "female fecundity [default: %default]"
),
make_option(
c("--larval_survival"),
type = "double",
metavar = "DOUBLE",
default = 0.5,
help = "pre-density larval to adult survival rate [default: %default]"
),
make_option(
c("--critical_population_size"),
type = "double",
metavar = "DOUBLE",
default = 250,
help = paste("mate-finding Allee effect occurs if virgin adult population ",
"size falls below this critical population size ",
"[default: %default]")
),
make_option(
c("--polyandry", "-p"),
type = "integer",
metavar = "INTEGER",
default = 3,
help = "maximum number of mates per mother [default: %default]"
),
# Intervention parameters
make_option(
c("--release_A", "-a"),
type = "integer",
metavar = "INTEGER",
default = NULL,
help = "[REQUIRED] number of released A-carrying males (AAbb)"
),
make_option(
c("--release_B", "-b"),
type = "integer",
metavar = "INTEGER",
default = NULL,
help = paste("[REQUIRED] number of released B-carrying males, ",
"homozygous aaBB for BEDs, heterozygous AABb for single drives")
),
make_option(
c("--release_critical_difference"),
type = "double",
metavar = "DOUBLE",
default = 0.3,
help = paste("next generation re-release of modified males of the less ",
"frequent drive (D1) is only allowed if ",
"(1 + release_critical_difference) * freq(D1) < freq(D2) ",
"[default: %default]")
),
make_option(
c("--re_release_limit"),
type = "double",
metavar = "DOUBLE",
default = 0.85,
help = paste("prevent next generation re-release of modified males of the ",
"less frequent drive when freq(A_B_) is higher than this ",
"limit [default: %default]")
),
# System parameters
make_option(
c("--bed_design"),
type = "character",
metavar = "STRING",
default = "yes",
help = paste("modeling a Bipartite Expression Drive (BED)? ",
"yes for BED, no for single suppression drives",
"[default: %default]")
),
make_option(
c("--conversion_efficiency", "-c"),
type = "double",
metavar = "DOUBLE",
default = 0.9,
help = paste("drive convertion efficiency, i.e. homing probability ",
"[default: %default]")
),
make_option(
c("--resistance_formation"),
type = "double",
metavar = "DOUBLE",
default = 0.01,
help = paste("formation probability of resistance alleles, i.e., allelles ",
"that prevent drive homing [default: %default]")
),
make_option(
c("--resistance_functionality"),
type = "double",
metavar = "DOUBLE",
default = 0,
help = paste("In BED designs (where target genes are essential for development) ",
"it is the probability that viable eggs homozygous for resistant ",
"alleles of any drive develop into a larva. In SD designs, where ",
"the target gene is essential for female fertility, it is the ",
"fraction of Fecundity preserved in mothers homozygous for ",
"resistant alleles. ",
"Use 1 for r1 resistance alleles that preserve target function ",
"[default: %default]")
),
make_option(
c("--dominance"),
type = "double",
metavar = "DOUBLE",
default = 0.6,
help = paste0("dominance of intended effects: [default: %default]\n ",
" 1: complete dominance (AABB effect = A_B_ effect > aabb effect = 0)\n ",
" 0 < x < 1: incomplete dominance (AaBb effect = x * AABB effect ",
" and AABb effect = AaBB effect = [x + (1 - x) / 2] * AABB effect)\n ",
" 0: no dominance, i.e., intended effects are recessive\n ",
" (no effects for non-AABB genotypes)")
),
make_option(
c("--terminator_efficiency", "-e"),
type = "double",
metavar = "DOUBLE",
default = 0.95,
help = paste("terminator efficiency, i.e. killing probability during ",
"mating [default: %default]")
),
make_option(
c("--intended_fecundity_cost"),
type = "double",
metavar = "DOUBLE",
default = 0,
help = "intended fertility/fecundity cost [default: %default]"
),
make_option(
c("--unintended_reproductive_cost_A"),
type = "double",
metavar = "DOUBLE",
default = 0.025,
help = paste("unintended dominant fertility/fecundity/mating cost for drive A ",
"[default: %default]")
),
make_option(
c("--unintended_reproductive_cost_B"),
type = "double",
metavar = "DOUBLE",
default = 0.025,
help = paste("unintended dominant fertility/fecundity/mating cost for drive B ",
"[default: %default]")
),
make_option(
c("--intended_viability_cost"),
type = "double",
metavar = "DOUBLE",
default = 0,
help = "intended egg-viability cost [default: %default]"
),
make_option(
c("--unintended_viability_cost_A"),
type = "double",
metavar = "DOUBLE",
default = 0.025,
help = paste("unintended dominant egg-viability cost of drive A ",
"[default: %default]")
),
make_option(
c("--unintended_viability_cost_B"),
type = "double",
metavar = "DOUBLE",
default = 0.025,
help = paste("unintended dominant egg-viability cost of drive B ",
"[default: %default]")
),
# Output parameters
make_option(
c("--output", "-o"),
type = "character",
metavar = "STRING",
default = "results",
help = "output directory name [default: %default]"
),
# Processing parameters
make_option(
c("--threads", "-t"),
type = "integer",
metavar = "INTEGER",
default = 1,
help = "number of threads to use [default: %default]"
),
make_option(
c("--seed"),
type = "integer",
metavar = "INTEGER",
default = 2,
help = "seed value for reproducibility [default: %default]"
)
)
opt <- parse_args(OptionParser(option_list = option_list))
# Arguments Validation ---------------------------------------------------------
# Validation of required arguments
validate_argument_required(opt, "carrying_capacity")
validate_argument_required(opt, "release_A")
validate_argument_required(opt, "release_B")
# Validation of [0,1] arguments
validate_argument_in_range(opt, "larval_survival", 0, 1)
validate_argument_in_range(opt, "re_release_limit", 0, 1)
validate_argument_in_range(opt, "conversion_efficiency", 0, 1)
validate_argument_in_range(opt, "resistance_formation", 0, 1)
validate_argument_in_range(opt, "resistance_functionality", 0, 1)
validate_argument_in_range(opt, "dominance", 0, 1)
validate_argument_in_range(opt, "terminator_efficiency", 0, 1)
validate_argument_in_range(opt, "intended_fecundity_cost", 0, 1)
validate_argument_in_range(opt, "unintended_reproductive_cost_A", 0, 1)
validate_argument_in_range(opt, "unintended_reproductive_cost_B", 0, 1)
validate_argument_in_range(opt, "intended_viability_cost", 0, 1)
validate_argument_in_range(opt, "unintended_viability_cost_A", 0, 1)
validate_argument_in_range(opt, "unintended_viability_cost_B", 0, 1)
# Validate of integer ranged arguments
validate_argument_integer_in_range(opt, "threads", 1, parallel::detectCores())
# Validation of positive arguments
validate_argument_positive(opt, "simulations")
validate_argument_positive(opt, "generations")
validate_argument_positive(opt, "carrying_capacity")
validate_argument_positive(opt, "fecundity")
validate_argument_positive(opt, "critical_population_size")
validate_argument_positive(opt, "polyandry")
validate_argument_positive(opt, "release_A")
validate_argument_positive(opt, "release_B")
validate_argument_positive(opt, "release_critical_difference")
validate_argument_positive(opt, "threads")
validate_argument_positive(opt, "seed")
# Validation against lists
validate_argument_in_list(opt, "bed_design", c("yes", "no"))
# Special validations
if (opt$conversion_efficiency + opt$resistance_formation > 1) {
log_error("--resistance_formation can not be higher than ",
"--conversion_efficiency's complement: ",
"resistance_formation < 1 - conversion_efficiency. ",
"Run the script with --help for more details.")
quit()
}
# Output directory
if (file.exists(opt$output)) {
log_error("--output points to an already existing folder: {opt$output}. ",
"Remove the folder first and try again.")
quit()
}
dir.create(opt$output)
# Constants Definition ---------------------------------------------------------
num_simulations <- opt$simulations # s
num_generations <- opt$generations # g
initial_population_size <- opt$carrying_capacity # k
fecundity <- opt$fecundity # f
larval_survival <- opt$larval_survival
critical_population_size <- opt$critical_population_size
polyandry <- opt$polyandry # p
release_A <- opt$release_A # a
release_B <- opt$release_B # b
critical_difference <- opt$release_critical_difference
re_release_limit <- opt$re_release_limit
bed_design <- opt$bed_design
homing_efficiency <- opt$conversion_efficiency # c
resistance_formation <- opt$resistance_formation
resistance_functionality <- opt$resistance_functionality
dominance <- opt$dominance
terminator <- opt$terminator_efficiency # e
intended_fecundity_cost <- opt$intended_fecundity_cost
unintended_reproductive_cost_A <- opt$unintended_reproductive_cost_A
unintended_reproductive_cost_B <- opt$unintended_reproductive_cost_B
intended_viability_cost <- opt$intended_viability_cost
unintended_viability_cost_A <- opt$unintended_viability_cost_A
unintended_viability_cost_B <- opt$unintended_viability_cost_B
output <- opt$output # o
threads <- opt$threads # t
seed <- opt$seed
# Set the seed for reproducibility
set.seed(seed)
# Fixed Parameters
wt_genotype <- "aabb" # Wild-type genotype
A_genotype <- "AAbb" # Genotype of released drive A-carrying males
B_genotype <- "aaBB" # Genotype of released drive B-carrying males
allee_sensitivity <- 0.01 # Density dependence of the Allee effect
re_release_lower_limit <- 0 # Minimum A_B_ frequency for re-release activation
two_alleles <- dominance # Proportion of intended system effect in AaBb genotype
three_alleles <-
dominance + (1 - dominance) / 2 # Proportion of intended system effect
# in AABb or AaBB genotype is intermediate
# between dominance and 1.
if (resistance_functionality < 1) {
resistance_type <- "r2"
} else {
resistance_type <- "r1"
}
costly_resistance_genotypes <- "xx|yy" # r2 non-functional genotypes for A or B target
# genes
# Some parameters need to be adjusted for SD designs
if (bed_design == "no") {
wt_genotype <- "AAbb"
B_genotype <- "AABb"
re_release_lower_limit <- 1
}
maximum_fecundity <- 2 * fecundity # Maximum admitted fecundity (upper bound of
# Poisson number of produced eggs p/mother)
# Larval population size that reduces survival rate to 50%
population_density_limit <- initial_population_size / (larval_survival - 2 / fecundity)
# Low density (or intrinsic) population growth rate
intrinsic_increase <- fecundity * larval_survival / 2
# Probability density of the number of mates per female
# [1] - probability of 1 mate
# [2] - probability of 2 mates
# [n] - probability of 'polyandry' mates