-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultilocus-suppression-functions.R
More file actions
1104 lines (951 loc) · 42.8 KB
/
multilocus-suppression-functions.R
File metadata and controls
1104 lines (951 loc) · 42.8 KB
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
# Load in (and install, if necessary) all necessary packages
packages = c("tidyverse", "cowplot", "viridis","latex2exp", "here", "openxlsx", "readr", "purrr", "RColorBrewer", "bit64",
"colorspace")
installed = packages %in% installed.packages()[, "Package"]
if (any(!installed)) {
install.packages(packages[!installed])
}
lapply(packages, library, character.only = TRUE)
#######################################################
### Useful helper functions ###
calculate_text_size = function(artboard_width, overleaf_scaling,desired_text_size = 10,latex_width = 500.484){
# Calculates the necessary text size for the plot
# Arguments:
# artboard_width: Adobe Illustrator artboard width
# overleaf_scaling: the scaling parameter on the figure in Overleaf
# desired_text_size: what size you want the text to be in the paper
# latex_width: Overleaf text width
# Returns:
# text_size: size of text for everything in the plot
# subplot_text_size: size of the subplot labels (A, B, C, D)
if (latex_width*overleaf_scaling > artboard_width){
p = 1
} else {
p = (latex_width*overleaf_scaling)/artboard_width
}
t = desired_text_size/p
subplot.t = (desired_text_size + 2)/p
return(list(text_size = t, subplot_text_size = subplot.t))
}
st_for_desired_load = function(desired_load, s_drive, n_targets){
# Finds the necessary fitness cost at the individual targets
# Arguments:
# desired_load: the lambda^* you're targeting
# s_drive: fitness cost at the drive sites
# n_targets: the number of targets
# Returns:
# the disrupted fitness cost, s
# desired load = 1 - (1-s_drive)(1-st)^n_targets
return(1 - ((1-desired_load)/(1-s_drive))^(1/n_targets))
}
#######################################################
### Function for heatmaps ###
heatmap_detailed = function(csv,
xindex, yindex, zindex,
x_tick_marks, y_tick_marks,z_tick_marks,
zlow, zhigh,
color_scale=c("white","blue"),
xl = "", yl = "", zl = "",title = "",
text_size = 12,
legend.pos = "bottom",
margin = F){
# Arguments:
# csv: a tibble containing the x, y, and z (color) variable
# xindex: the column index of the x-axis variable in the csv
# yindex: the column index of the y-axis variable in the csv
# zindex: the column index of the z-axis (color axis) variable in the csv
# x_tick_marks: vector of tick-mark values for the x-axis
# y_tick_marks: vector of tick-mark values for the y-axis
# z_tick_marks: vector of tick-mark values for the colorbar
# zlow: minimum color value
# zhigh: maximum color value
# color_scale: vector of colors to use for the min and max of the z-variable range
# xl: x-axis label
# yl: y-axis label
# zl: z-axis label
# title: plot title
# text_size: size of all text
# legend.pos: position of the colorbar ("bottom", "right", or "none")
# margin: whether to include padding around the plot
indices = c(xindex, yindex, zindex)
# focus on data of interest
df = as.data.frame(csv[,indices])
rng = range(c(0,1))
class(df[,1]) = "double"
class(df[,2]) = "double"
class(df[,3]) = "double"
names(df) = c('x','y','v')
df = aggregate(v~x+y, data=df, mean)
xs = sort(unique(df$x))
ys = sort(unique(df$y))
x_range = xs[2]-xs[1]
y_range = ys[2]-ys[1]
first = ggplot(df, aes(x, y)) +
geom_tile(aes(fill = v)) +
coord_fixed(ratio=1*x_range/y_range) +
labs(x=xl, y=yl,title=title, fill = zl)
if (legend.pos %in% c("bottom", "none")){
plot = first + scale_x_continuous(breaks = x_tick_marks) +
scale_y_continuous(breaks = y_tick_marks)+
scale_fill_gradientn(colours = color_scale,
breaks = z_tick_marks,
limits = c(zlow, zhigh),
guide = guide_colourbar(direction="horizontal",
label.position = "bottom",
barheight = unit(0.03, "npc"), # sets colorbar height as fraction of plot's
barwidth = unit(0.45, "npc"))) + # sets colorbar width as fraction of plot's
theme(legend.position=legend.pos,
text = element_text(size=text_size),
plot.title = element_text(size=text_size, face = "bold"),
axis.text = element_text(size=text_size),
legend.text = element_text(size=text_size))
} else if (legend.pos == "right") {
plot = first + scale_x_continuous(breaks = x_tick_marks) +
scale_y_continuous(breaks = y_tick_marks)+
scale_fill_gradientn(colours = color_scale,
breaks = z_tick_marks,
limits = c(zlow, zhigh),
guide = guide_colourbar(direction="vertical",
label.position = "left",
barwidth = unit(0.03, "npc"),
barheight = unit(0.45, "npc"))) +
theme(legend.position=legend.pos,
text = element_text(size=text_size),
plot.title = element_text(size=text_size, face = "bold"),
axis.text = element_text(color="black",size=text_size),
legend.text = element_text(size=text_size))
}
if (zl!=""){
if (legend.pos == "right"){
plot = plot + theme(legend.title = element_text(hjust = 1))
} else if (legend.pos == "bottom"){
plot = plot + theme(legend.title = element_text(vjust = 1))
}
}
if (!margin){
plot = plot + theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
}
return(plot)
}
#############################################
###### Single locus drive plotting functions ####
summarize_single_locus_directory = function(directory,all_gens=NULL, summarize = TRUE){
# Reads in all of the replicate csv files in a directory, fills them (down from the final absorbing state) to a max generation,
# and averages across generations
# Arguments:
# directory: path to the files (don't end it in /)
# note -- ensure that all of these files have the same ending state (ie all ended in fixation or all ended in lost)
# such that averaging is not misleading
# all_gens: vector of generations to ensure each replicate has (ex: 1:100)
# summarize: whether to average the trajectories
# Returns:
# if !summarize, just returns the final_states. This shows the last row in each replicate.
# else returns the summary tibble (averaged values across generations) and the final states
# --- Read all CSV files ---
files = list.files(path = directory, pattern = "\\.csv$", full.names = TRUE)
data_list <- map2(
files,
seq_along(files),
~ read_csv(.x, show_col_types = FALSE) %>%
mutate(
replicate = .y,
filename = basename(.x) # Save the file name
)
)
all_data = bind_rows(data_list)
# need a point for each
if (is.null(all_gens)){
all_gens = sort(unique(all_data$gen_num))
}
final_states = all_data %>%
group_by(replicate,filename) %>%
filter(gen_num == max(gen_num, na.rm = TRUE)) %>%
summarize(
final_genetic_load = last(genetic_load),
final_drive_rate = last(drive_rate),
final_r1_drive_rate = last(resist_rate),
.groups = "drop"
)
n = sum(final_states$final_drive_rate)
all_ended_at_fixation = near(n, nrow(final_states))
all_ended_at_loss = near(n, 0)
if (!all_ended_at_fixation & !all_ended_at_loss){
print("SEPARATE FINAL STATES")
}
if (!summarize){
return(final_states)
}
# Extend for absorbing states
filled_data = all_data %>%
group_by(replicate) %>%
arrange(gen_num) %>%
# filter out generations above max
filter(gen_num <= max(all_gens)) %>%
# extend to all_gens, filling missing gens
complete(gen_num = all_gens) %>%
# carry forward the last observed values (which should be absorbing states)
fill(genetic_load, drive_rate, resist_rate, mean_fit, .direction = "down") %>%
mutate(
across(
c(genetic_load, drive_rate, resist_rate),
~ ifelse((near(lag(.x), 0) | near(lag(.x), 1)) & is.na(.x), lag(.x), .x)
)
) %>%
ungroup()
summary = filled_data %>%
group_by(gen_num) %>%
summarise(
avg_genetic_load = mean(genetic_load, na.rm = TRUE),
min_genetic_load = min(genetic_load, na.rm = TRUE),
max_genetic_load = max(genetic_load, na.rm = TRUE),
avg_drive_rate = mean(drive_rate, na.rm = TRUE),
min_drive_rate = min(drive_rate, na.rm = TRUE),
max_drive_rate = max(drive_rate, na.rm = TRUE),
avg_r1_drive_rate = mean(resist_rate, na.rm = TRUE),
min_r1_drive_rate = min(resist_rate, na.rm = TRUE),
max_r1_drive_rate = max(resist_rate, na.rm = TRUE),
n_replicates = n()
) %>%
ungroup()
return(list(summary = summary,
final_states = final_states))
}
plot_single_locus_joint_gl_joint_res = function(resistance_rate,
max_gen = NULL,
legend.pos = "right",
text_size = 14,
linewidth = 1,
dsx_color = "mediumslateblue",
s0.2_color = "green4") {
# Makes the single locus main plots -- the averaged genetic loads for each drive and the averaged frequencies
# Arguments:
# resistance_rate: the resistance rate at the drive site (1 of 1e-5, 1e-4, or 1e-3)
# max_gen: the max x-value of the plot
# legend.pos: 'right' for legend or 'none' for no legend
# text_size: size of all text in plot
# linewidth: width of trajectory and horizontal lines
# dsx_color: the color of the s_d = 1 drive
# s0.2_color: the color of the modification drive, with lower s
# Returns:
# List of the genetic load averaged plot, resistance frequency averaged plot, and drive frequency averaged plot
# the transparency of a line reflects the % of replicates where it ended in that state
# Set up vector for reading in the replicate data
if (!is.null(max_gen)){
all_gens = 1:max_gen
} else {
all_gens = NULL
}
dsx.dir.base = here(paste0("data/single-locus/dsx/"))
s0.2.dir.base = here(paste0("data/single-locus/s_d0.2/"))
if (near(resistance_rate, 1e-3)){
# both dsx and s = 0.5 were always lost
# 2 categories: s = c(1, 0.2)
dsx.dir = paste0(dsx.dir.base, "res1e-3")
s0.2.dir = paste0(s0.2.dir.base, "res1e-3")
dsx.results = summarize_single_locus_directory(dsx.dir,all_gens)$summary %>%
mutate(s_d = "1",
alpha_val = n_replicates/20,
group = 1)
s0.2.results = summarize_single_locus_directory(s0.2.dir,all_gens)$summary %>%
mutate(s_d = "0.2",
alpha_val = n_replicates/20,
group = 2)
all_results = bind_rows(
dsx.results,
s0.2.results
)
} else if (near(resistance_rate, 1e-4)){
# dsx always lost
# s = 0.2 fixed in 12/20 replicates; lost in 8/20
# 3 categories: s = c(1, 0.2_fixed, 0.2_lost)
dsx.dir = paste0(dsx.dir.base, "res1e-4")
dsx.results = summarize_single_locus_directory(dsx.dir,all_gens)$summary %>%
mutate(s_d = "1",
alpha_val = n_replicates/20,
group = 1) # drive loss
s0.2.dir.frequent = paste0(s0.2.dir.base, "res1e-4/fixed")
s0.2.frequent.results = summarize_single_locus_directory(s0.2.dir.frequent,all_gens)$summary %>%
mutate(s_d = "0.2",
alpha_val = n_replicates/20,
group = 2)
s0.2.dir.less.frequent = paste0(s0.2.dir.base, "res1e-4/lost")
s0.2.less.frequent.results = summarize_single_locus_directory(s0.2.dir.less.frequent,all_gens)$summary %>%
mutate(s_d = "0.2",
alpha_val = n_replicates/20,
group = 3)
all_results = bind_rows(
dsx.results,
s0.2.frequent.results,
s0.2.less.frequent.results
)
} else if (near(resistance_rate, 1e-5)) {
# dsx fixed in 10/20
# dsx lost in 10/20
# s = 0.2 fixed in 18/20
# s = 0.2 lost in 2/20
# 4 categories: s = c(1_fixed, 1_lost, 0.2_fixed, 0.2_lost)
dsx.dir.frequent = paste0(dsx.dir.base, "res1e-5/lost")
dsx.frequent.results = summarize_single_locus_directory(dsx.dir.frequent, all_gens)$summary %>%
mutate(s_d = "1",
alpha_val = n_replicates/20,
group = 1)
dsx.dir.less.frequent = paste0(dsx.dir.base, "res1e-5/fixed")
dsx.less.frequent.results = summarize_single_locus_directory(dsx.dir.less.frequent,all_gens)$summary %>%
mutate(s_d = "1",
alpha_val = n_replicates/20,
group = 2)
s0.2.dir.frequent = paste0(s0.2.dir.base, "res1e-5/fixed")
s0.2.frequent.results = summarize_single_locus_directory(s0.2.dir.frequent,all_gens)$summary %>%
mutate(s_d = "0.2",
alpha_val = n_replicates/20,
group = 3)
s0.2.dir.less.frequent = paste0(s0.2.dir.base, "res1e-5/lost")
s0.2.less.frequent.results = summarize_single_locus_directory(s0.2.dir.less.frequent,all_gens)$summary %>%
mutate(s_d = "0.2",
alpha_val = n_replicates/20,
group = 4)
all_results = bind_rows(
dsx.frequent.results,
dsx.less.frequent.results,
s0.2.frequent.results,
s0.2.less.frequent.results
)
}
plt.gl = ggplot(all_results, aes(x = gen_num, y = avg_genetic_load, color = s_d, group = interaction(s_d, alpha_val, group))) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_color_manual(values = c("1" = dsx_color, "0.2" = s0.2_color)) +
scale_alpha_identity() +
geom_hline(yintercept = 1, linetype = "dashed", color = dsx_color, linewidth = linewidth*1.05)+
geom_hline(yintercept = 0.2, linetype = "dashed", color = s0.2_color, linewidth = linewidth*1.05)+
labs(y = TeX(r"(Genetic load ($\lambda$))"),
x = TeX(r"(Generation)"),
color = TeX(r"($s_d$)")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
plt.res = ggplot(all_results, aes(x = gen_num, y = avg_r1_drive_rate, color = s_d, group = interaction(s_d, alpha_val, group))) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_color_manual(values = c("1" = dsx_color, "0.2" = s0.2_color)) +
scale_alpha_identity() +
labs(y = TeX(r"(Resistance frequency)"),
x = TeX(r"(Generation)"),
color = TeX(r"($s_d$)")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
plt.dr = ggplot(all_results, aes(x = gen_num, y = avg_drive_rate, color = s_d, group = interaction(s_d, alpha_val, group))) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_color_manual(values = c("1" = dsx_color, "0.2" = s0.2_color)) +
scale_alpha_identity() +
labs(y = TeX(r"(Drive frequency)"),
x = TeX(r"(Generation)"),
color = TeX(r"($s_d$)")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
plot.list = list(gl = plt.gl, res = plt.res, drive = plt.dr)
return(plot.list)
}
#############################################
### Functions for multilocus drive plotting ###
summarize_multilocus_directory = function(directory, all_gens = NULL, summarize = T) {
# Arguments:
# directory: path to replicate data (not ending in /)
# make sure these are separated by absorbing state (ie all lost or all fixed)
# all_gens: vector of generations to summarize (1:max_gen)
# summarize: whether to create an averaged trajectory
# Returns:
# if !summarize, just returns the final states (last rows of each replicate)
# else returns the final states and the replicate-averaged summary
files = list.files(path = directory, pattern = "\\.csv$", full.names = TRUE)
data_list = map2(
files,
seq_along(files),
~ {
df = read_csv(.x, show_col_types = FALSE)
# Generation where genetic load < 20%
gen_marker_gen = df %>%
filter(gen_marker == 1) %>%
pull(gen_num) %>%
first()
# Generation where drive reaches fixation or lost
drive_absorbed_gen = df %>%
filter(near(drive_rate,0) | near(drive_rate, 1)) %>%
pull(gen_num) %>%
first()
# Generation where targets reach fixation or lost
disrupted_absorbed_gen = df %>%
filter((near(broken_rate,0) | near(broken_rate, 1)) & gen_num > 1) %>%
pull(gen_num) %>%
first()
df %>%
mutate(
replicate = .y,
filename = basename(.x),
gen_genetic_load_below20 = gen_marker_gen,
gen_drive_fixed_or_lost = drive_absorbed_gen,
gen_disrupted_fixed_or_lost = disrupted_absorbed_gen
)
}
)
all_data = bind_rows(data_list)
# need a point for each
if (is.null(all_gens)){
all_gens = sort(unique(all_data$gen_num))
}
final_states = all_data %>%
group_by(replicate,filename) %>%
filter(gen_num == max(gen_num, na.rm = TRUE)) %>%
summarize(
final_genetic_load = last(genetic_load),
final_drive_rate = last(drive_rate),
final_r1_drive_rate = last(resist_rate),
final_disrupted_rate = last(broken_rate),
final_r1_target_rate = last(func_rate),
gen_genetic_load_below20 = last(gen_genetic_load_below20),
gen_drive_fixed_or_lost = last(gen_drive_fixed_or_lost),
gen_disrupted_fixed_or_lost = last(gen_disrupted_fixed_or_lost),
.groups = "drop"
)
n = sum(final_states$final_disrupted_rate)
all_ended_at_fixation = near(n, nrow(final_states))
all_ended_at_loss = near(n, 0)
if (!all_ended_at_fixation & !all_ended_at_loss){
print("SEPARATE TARGET FINAL STATES")
print(paste0("target lost in ", n,"/20"))
}
n = sum(final_states$final_drive_rate)
all_ended_at_fixation = near(n, nrow(final_states))
all_ended_at_loss = near(n, 0)
if (!all_ended_at_fixation & !all_ended_at_loss){
print("SEPARATE DRIVE FINAL STATES")
print(paste0("drive fixed in ", n,"/20"))
}
if (!summarize){
return(final_states)
}
# Extend for absorbing states
filled_data = all_data %>%
group_by(replicate) %>%
arrange(gen_num) %>%
# filter out generations above max
filter(gen_num <= max(all_gens)) %>%
# extend to all_gens, filling missing gens
complete(gen_num = all_gens) %>%
# carry forward the last observed values (which should be absorbing states)
fill(genetic_load, drive_rate, resist_rate, mean_fit, broken_rate, func_rate,
gen_genetic_load_below20, gen_drive_fixed_or_lost, gen_disrupted_fixed_or_lost,
.direction = "down") %>%
mutate(
across(
c(genetic_load, drive_rate, resist_rate, mean_fit, broken_rate, func_rate),
~ ifelse((near(lag(.x), 0) | near(lag(.x), 1)) & is.na(.x), lag(.x), .x)
)
) %>%
ungroup()
summary = filled_data %>%
group_by(gen_num) %>%
summarise(
avg_genetic_load = mean(genetic_load, na.rm = TRUE),
min_genetic_load = min(genetic_load, na.rm = TRUE),
max_genetic_load = max(genetic_load, na.rm = TRUE),
avg_drive_rate = mean(drive_rate, na.rm = TRUE),
min_drive_rate = min(drive_rate, na.rm = TRUE),
max_drive_rate = max(drive_rate, na.rm = TRUE),
avg_r1_drive_rate = mean(resist_rate, na.rm = TRUE),
avg_disrupted_rate = mean(broken_rate, na.rm = TRUE),
min_disrupted_rate = min(broken_rate, na.rm = TRUE),
max_disrupted_rate = max(broken_rate, na.rm = TRUE),
avg_r1_target_rate = mean(func_rate, na.rm = TRUE),
n_replicates = n()
) %>%
ungroup()
return(list(final_states = final_states,
summary = summary))
}
plot_multilocus_genetic_load_summaries = function(base.directory = here("data/n-lambda/cas9-drive1-target0.8/lambda0.95/"),
max_gen = NULL,
lambda = 0.95,
col.palette = c("saddlebrown", "green4", "mediumslateblue"),
text_size = 14, legend.pos = "right", linewidth = 1,
alpha.booster = 1.1, title = "") {
# Creates the replicate-averaged plots for the (n, lambda^*) unlinked multilocus jobs
# Arguments:
# base.directory: the directory where the num_targets<t>/drive-<fixed or lost> files are
# max_gen: the max x-value of the plot
# lambba: the common max genetic load
# col.palette: the corresponding colors for 1-target, 5-targets, 10-targets
# text_size: size of all text in plot
# legend.pos: either "right" for legend or "none" for no legend
# linewidth: width of all lines
# alpha.booster: multiplier on the transparency of the "drive fixed" outcomes
# title: optional title
# Returns:
# 3 plots:
# combined genetic load trajectories
# combined drive and disrupted trajectories
# combined disrupted and target-resistant trajectories
all_gens = NULL
if (!is.null(max_gen)){
all_gens= 1:max_gen
}
# 1 target
one.target_drive.fixed = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets1/drive-fixed"), all_gens = all_gens)$summary %>%
mutate(num_targets = "1",
alpha_val = (n_replicates/20)*alpha.booster,
group = 1)
one.target_drive.lost = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets1/drive-lost"), all_gens = all_gens)$summary %>%
mutate(num_targets = "1",
alpha_val = n_replicates/20,
group = 2)
# 5 targets
five.target_drive.fixed = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets5/drive-fixed"), all_gens = all_gens)$summary %>%
mutate(num_targets = "5",
alpha_val = (n_replicates/20)*alpha.booster,
group = 3)
five.target_drive.lost = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets5/drive-lost"), all_gens = all_gens)$summary %>%
mutate(num_targets = "5",
alpha_val = n_replicates/20,
group = 4)
# 10 targets
ten.target_drive.fixed = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets10/drive-fixed"), all_gens = all_gens)$summary %>%
mutate(num_targets = "10",
alpha_val = (n_replicates/20)*alpha.booster,
group = 5)
ten.target_drive.lost = summarize_multilocus_directory(directory = paste0(base.directory, "num_targets10/drive-lost"), all_gens = all_gens)$summary %>%
mutate(num_targets = "10",
alpha_val = n_replicates/20,
group = 6)
values.palette = c("1" = col.palette[1],
"5" = col.palette[2],
"10" = col.palette[3])
all_results = bind_rows(one.target_drive.fixed,
one.target_drive.lost,
five.target_drive.fixed,
five.target_drive.lost,
ten.target_drive.fixed,
ten.target_drive.lost)
p.gl = ggplot(all_results, aes(x = gen_num, y = avg_genetic_load, color = num_targets, group = interaction(num_targets, alpha_val, group))) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_alpha_identity() +
geom_hline(yintercept = lambda, linetype = "dashed", size = linewidth) +
labs(
x = TeX(r"(Generation)"),
y = TeX(r"(Genetic load ($\lambda$))"),
color = TeX(r"($n$)"),
title = title
) +
scale_color_manual(values = values.palette,
breaks = c("1","5", "10")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, hjust = 0.5),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
# Target site alleles
targets.freq = all_results %>%
pivot_longer(cols = c(avg_r1_target_rate, avg_disrupted_rate),
names_to = "allele", values_to = "freq")
p.res = ggplot(targets.freq, aes(x = gen_num, y = freq,
color = num_targets, group = interaction(num_targets, alpha_val, group, allele),
linetype = allele)) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_alpha_identity() +
labs(
x = TeX(r"(Generation)"),
y = "Frequency",
color = TeX(r"($n$)"),
linetype = "target site\nallele"
) +
scale_linetype_manual(
values = c(avg_r1_target_rate = "longdash",
avg_disrupted_rate = "solid"),
labels = c(avg_r1_target_rate = "resistance",
avg_disrupted_rate = "disrupted")
) +
scale_color_manual(values = values.palette,
breaks = c("1","5", "10")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
guides(linetype = guide_legend(keywidth = 2.2)) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
# Drive and disrupted target alleles
allele.freq = all_results %>%
pivot_longer(cols = c(avg_drive_rate, avg_disrupted_rate),
names_to = "allele", values_to = "freq")
p.drive.target = ggplot(allele.freq, aes(x = gen_num, y = freq,
color = num_targets, group = interaction(num_targets, alpha_val, group, allele),
linetype = allele)) +
geom_line(aes(alpha = alpha_val), linewidth = linewidth) + # alpha varies by frequent/less frequent
scale_alpha_identity() +
labs(
x = TeX(r"(Generation)"),
y = "Frequency",
color = TeX(r"($n$)"),
linetype = "allele"
) +
scale_linetype_manual(
values = c(avg_disrupted_rate = "longdash",
avg_drive_rate = "solid"),
labels = c(avg_disrupted_rate = "disrupted",
avg_drive_rate = "drive")
) +
ylim(0, 1) +
scale_color_manual(values = values.palette,
breaks = c("1","5", "10")) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
guides(linetype = guide_legend(keywidth = 2.25)) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2), limits = c(0,1))
return(list(gl.plot = p.gl,
target.freq.plot = p.res,
drive.and.target.plot = p.drive.target))
}
#### Function for plotting log jam
log_jam_summary_plots = function(data,
text_size = 14,
legend.pos = "right",
rho_colors = c("#BEEBD3", "#7ED3B7", "#43BDAA", "#2F86A2", "#33679C", "#3E488E", "black"),
linewidth = 1, point_size = 1.5){
# Arguments
# data: dataset with each of these rhos and each num_targets between 1 and 10
# text_size: size of all text on plot
# legend.pos: either "right" or "none" for no legend
# rho_colors: color of each recombination rate (least to greatest)
# linewidth: width of lines
# point_size: size of points
# Returns:
# List of plots summarizing the data:
# 1. rate.gl.below.0.2: fraction of simulations where the genetic load fell below 0.2
# 2. rate.gl.above.0.2: fraction of simulations where the genetic load did NOT fall below 0.2
# 3. time.gl.below.0.2: avg number of generations until the genetic load fell below 0.2
# 4. time.gl.below.0.2.given.rate.above.10: avg number of generations until the genetic load fell below 0.2 | at least 2 replicates
# 5. rate.disrupted.lost: fraction of simulations where disrupted alleles were eventually lost (after 10,000 gens)
# 6. time.disrupted.lost: avg number of generations until disrupted alleles were lost (given that they were)
# 7. max.gl: the avg maximum genetic load attained by the drive
# 8. time.max.gl: avg number of generations until the max genetic load
# 9. ending.gl: the avg genetic load after 10,000 generations
labs = c(TeX(r"($10^{-7}$)"), TeX(r"($10^{-6}$)"), TeX(r"($10^{-5}$)"),
TeX(r"($10^{-4}$)"), TeX(r"($10^{-3}$)"), TeX(r"($10^{-2}$)"), TeX(r"($0.5$)"))
plt.below.0.2 = ggplot(data, aes(x = num_targets,
y = avg_rate_genetic_load_fell_below_marker, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($P(\lambda(t) < 0.2)$)"),
color = TeX(r"($\rho$)"),
) + ylim(0,1) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0,1, by = 0.2), limits = c(0,1))
plt.above.0.2 = ggplot(data, aes(x = num_targets,
y = 1 - avg_rate_genetic_load_fell_below_marker,
color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($P(\lambda(t = 10000) > 0.2$))"),
color = TeX(r"($\rho$)"),
) + ylim(0,1) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0,1, by = 0.2), limits = c(0,1))
plt.ending.gl = ggplot(data, aes(x = num_targets,
y = avg_ending_genetic_load, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($\lambda(t = 10000)$)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0, 0.6, by = 0.2), limits = c(0,0.6))
plt.max.gl = ggplot(data, aes(x = num_targets,
y = avg_max_load, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
geom_hline(yintercept = 0.95, linewidth = linewidth, linetype = "dashed") +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($\max(\lambda(t))$)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0,1,by = 0.2), limits = c(0,1))
plt.time.max.gl = ggplot(data, aes(x = num_targets,
y = avg_time_to_maximum_genetic_load, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"(Time to $\max(\lambda(t))$)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
)
plt.tau = ggplot(data, aes(x = num_targets, y = avg_time_to_genetic_load_below_marker, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($\tau$)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text.x = element_text(size = text_size),
axis.title.x = element_text(size = text_size),
axis.title.y = element_text(size = 1.3*text_size),
legend.text = element_text(size = text_size),
axis.text.y = element_text(size = text_size),
aspect.ratio = 1
)
plt.tau.conditional = data %>% filter(avg_rate_genetic_load_fell_below_marker >= 2/20) %>%
ggplot(aes(x = num_targets, y = avg_time_to_genetic_load_below_marker, color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"($\tau$)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text.x = element_text(size = text_size),
axis.title.x = element_text(size = text_size),
axis.title.y = element_text(size = 1.3*text_size),
axis.text.y = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
)
plt.total.time = ggplot(data, aes(x = num_targets,
y = avg_time_to_disrupted_lost,
color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"(Time until all disrupted sites lost)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
)
rate.disrupted.lost = ggplot(data, aes(x = num_targets,
y = disrupted_lost_rate,
color = as.factor(recomb_rate))) +
geom_point(size = point_size) +
geom_line(size = linewidth) +
labs(
x = TeX(r"(Number of target sites ($n$))"),
y = TeX(r"(Rate of disrupted sites loss)"),
color = TeX(r"($\rho$)"),
) +
scale_color_manual(values = rho_colors,
labels = labs) +
scale_x_continuous(breaks = seq(1, 10, by = 2)) +
theme(
legend.position = legend.pos,
text = element_text(size = text_size),
plot.title = element_text(size = text_size, face = "bold"),
axis.text = element_text(size = text_size),
legend.text = element_text(size = text_size),
aspect.ratio = 1
) +
scale_y_continuous(breaks = seq(0,1, by = 0.2), limits = c(0,1))
plot.list = list(rate.gl.below.0.2 = plt.below.0.2,
rate.gl.above.0.2 = plt.above.0.2,
time.gl.below.0.2 = plt.tau,
time.gl.below.0.2.given.rate.above.10 = plt.tau.conditional,
rate.disrupted.lost = rate.disrupted.lost,
time.disrupted.lost = plt.total.time,
max.gl = plt.max.gl,
time.max.gl = plt.time.max.gl,
ending.gl = plt.ending.gl)