-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulations_analysis.R
1567 lines (1381 loc) · 64.1 KB
/
simulations_analysis.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
# Set up ------------------------------------------------------------------
# !diagnostics off
# parameters of the model; used by the functions, so need to initialize here
beta=50;phi=10^-7;p=10^-5;q=10^-5;m=0.1;spacer_len=10
# A function to identify if analysis is run on the UChicago's Midway HPC. Change it to fit your HPC.
on_Midway <- function(){ifelse(Sys.getenv('USER')=='pilosofs',T,F)}
# If run with an sbatch pipeline take arguments from there. Otherwise those specified here.
if (length(commandArgs(trailingOnly=TRUE))==0) {
# args <- c('mu5e-7_initialDiffDp1_S50P15_R-13997','5*10^-7','15',F, F)
args <- c('mu1e-7_initialDiffDp1_S10P15_R-12499','1*10^-7','15',F, T)
} else {
args <- commandArgs(trailingOnly=TRUE)
}
base_name <- args[1]
mu <- eval(parse(text = args[2]))
protospacer_len <- eval(parse(text = args[3]))
make_plots <- as.logical(args[4])
if(on_Midway()){system('module load gcc/6.1')}
if(!on_Midway()){setwd(paste('data/',base_name,sep=''))}
dir.create('figures')
library(ggtree)
library(ape)
library(treeio)
library(igraph)
library(xtable)
library(tidyverse)
library(magrittr)
library(bipartite)
library(cowplot)
library(grid)
library(infomapecology)
if(check_infomap()==F){install_infomap()} # Install infomap
# Functions ---------------------------------------------------------------
returnnull <- function(x) if (is.null(x)){'none'} else {x}
notify <- function(x){
print(paste('[',Sys.time(),'] ',x,sep=''))
}
record_data <- function(x){
notify(paste('Recording ',deparse(substitute(x)),sep=''))
write_csv(x, paste(base_name,'_',deparse(substitute(x)),'.csv',sep=''), col_names = T)
}
make_name <- function(x,hr){
hr_str <- str_pad(hr, width = 4, side = 'left', pad = '0')
paste('data/',x,'_',hr_str,'.csv',sep='')
}
make_png <- function(p, method='ggsave'){
if (method=='ggsave'){
ggplot2::ggsave(paste('figures/',base_name,'_',deparse(substitute(p)),'.png',sep=''), p, device = 'png', width = 32, height = 18, units = 'cm', dpi = 200)
} else {
png(paste('figures/',base_name,'_',deparse(substitute(p)),'.png',sep=''),1920,1080,res=150)
print(p)
dev.off()
}
}
make_svg <- function(p){
svg(paste('figures/',base_name,'_',deparse(substitute(p)),'.svg',sep=''),12.8,8)
print(p)
dev.off()
}
standard_plot <- function(p){
p+scale_x_continuous(breaks=label_seq)+
geom_vline(xintercept=BDRs$start, col='#27AE60',size=1.2)+
geom_vline(xintercept=VDRs$start, col='purple',size=1.2)+
labs(x='Time')+
theme_bw()+theme(legend.position = 'none')
}
gg_color_hue <- function(n, hue_min = 10, hue_max = 280, tune1 = 62, tune2 = 100) {
hues = seq(hue_min, hue_max, length=n+1)
hcl(h=hues, l=tune1, c=tune2)[1:n]
}
list_to_matrix <- function(l, directed=F, bipartite=T){
# Only deal with first 3 columns.
g <- graph.data.frame(l,directed = directed)
if(bipartite){
V(g)$type <- V(g)$name %in% as.data.frame(l)[,1] # As.data.frame is necessary because if l is a tibble then l[,1] does not work
output_mat <- as_incidence_matrix(g, names = T, attr = 'w', sparse = F)
} else {
output_mat <- as_adjacency_matrix(g, names = T, sparse = F, attr = 'w')
}
# print(dim(output_mat))
# if(any(rowSums(output_mat)==0)){stop('One or more rows sum to 0')}
# if(any(colSums(output_mat)==0)){stop('One or more columns sum to 0')}
return(output_mat)
}
# create_networks_hr <- function(virus_data,bacteria_data,hr){
# virus_data_hr <- virus_data %>% filter(timesOfRecord==hr)
# bacteria_data_hr <- bacteria_data %>% filter(timesOfRecord==hr)
# virus_abund_hr <- virus_data_hr %>% select(label, density) %>% rename(V_ID=label) %>% mutate(V_ID=paste('V_',str_pad(V_ID, 4, 'left', '0'),sep=''))
# bacteria_abund_hr <- bacteria_data_hr %>% select(label, density) %>% rename(B_ID=label) %>% mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep=''))
#
# # Create networks in a list form
#
# ## Virus-protospacer network
# virus_ps_hr_list <- virus_data_hr %>% select(-time, -timesOfRecord, -density) %>%
# gather(key='PSidx', value='value', -label) %>%
# rename(V_ID=label, PS=value) %>%
# arrange(V_ID,PS) %>%
# mutate(w=1) %>%
# mutate(V_ID=paste('V_',str_pad(V_ID, 4, 'left', '0'),sep='')) %>%
# mutate(PS=paste('P_',str_pad(PS, 4, 'left', '0'),sep='')) %>%
# select(V_ID,PS,w,PSidx)
#
# ## Bacteria-spacer network
# bacteria_sp_hr_list <- bacteria_data_hr %>%
# select(-time, -timesOfRecord, -density) %>%
# gather(key='SPidx', value='value', -label) %>%
# filter(value!=-1) %>%
# rename(B_ID=label, SP=value) %>%
# arrange(B_ID,SP) %>%
# mutate(w=1) %>%
# # mutate(w=ifelse(SP==-1,0,1)) %>%
# mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep='')) %>%
# mutate(SP=paste('P_',str_pad(SP, 4, 'left', '0'),sep='')) %>%
# select(B_ID,SP,w,SPidx)
#
# # Bacteria without spacers: This is needed for NEUTRAL SCENARIOS in which
# # interactions are not related to spacer-protospacer acquisition. So bacteria
# # can actually survive without acquiting protospacers.
# bacteria_no_sp <- bacteria_data_hr %>%
# select(-time, -timesOfRecord, -density) %>%
# gather(key='SPidx', value='value', -label) %>%
# filter(value==-1) %>%
# group_by(label) %>%
# summarise(no_spacers=n()) %>% filter(no_spacers==spacer_len) %>%
# rename(B_ID=label) %>%
# mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep=''))
#
# # Add to the bacteria-spacer network those bacteria without spacers.
# suppressWarnings(
# bacteria_sp_hr_list <- bind_rows(bacteria_sp_hr_list,
# as_tibble(expand.grid(B_ID=bacteria_no_sp$B_ID, SP=unique(bacteria_sp_hr_list$SP), w=0)))
# )
# ## Immunity network as a list
# x <- virus_ps_hr_list %>% select(V_ID, PS)
# y <- bacteria_sp_hr_list %>%
# filter(w!=0) %>% # MUST include only the interactions using filter(w!=0), otherwise the immunity network will have false edges that should not exist.
# select(B_ID, SP)
# immunity_list <- x %>% inner_join(y, by = c('PS'='SP')) %>%
# arrange(B_ID, V_ID) %>%
# group_by(V_ID, B_ID) %>% count() %>%
# rename(w=n)
#
# # Define nodes in the system
# viruses_hr <- sort(unique(virus_abund_hr$V_ID))
# bacteria_hr <- sort(unique(bacteria_abund_hr$B_ID))
# spacers_hr <- sort(union(bacteria_sp_hr_list$SP, virus_ps_hr_list$PS))
# nodes <- tibble(nodeID=1:(length(viruses_hr)+length(bacteria_hr)+length(spacers_hr)),
# nodeName=c(viruses_hr, bacteria_hr, spacers_hr),
# type=c(rep(1,length(viruses_hr)), rep(2,length(bacteria_hr)), rep(3,length(spacers_hr)))
# )
# nodes$type <- as.integer(nodes$type)
#
# # Check networks
# if (nrow(bacteria_sp_hr_list)==0) {bacteria_sp_hr_list <- NULL}
# if (nrow(immunity_list)==0) {immunity_list <- NULL}
#
# ## Infection network
# # To get this we first need to create the immunity nertwork as a matrix
# if (!is.null(immunity_list)){
# immunity_matrix <- list_to_matrix(immunity_list) # transform to a matrix format
# } else {
# immunity_matrix <- matrix(0, nrow=length(bacteria_hr), ncol=length(viruses_hr), dimnames = list(bacteria_hr, viruses_hr))
# }
#
# # Add all the nodes which were not included in the list because they have degree of zero
# # missing_bacteria <- setdiff(unique(bacteria_sp_hr_list$B_ID),rownames(immunity_matrix))
# # immunity_matrix <- rbind(immunity_matrix, matrix(0,
# # ncol = ncol(immunity_matrix),
# # nrow = length(missing_bacteria),
# # dimnames = list(missing_bacteria, colnames(immunity_matrix))))
# #
#
# infection_matrix <- 1*(immunity_matrix==0) # Binary infection network is the "negative (photography-wise)" of the immunity network
# if (any(infection_matrix==1)){ # If there is at least one non-zero interaction in the infection network
# N_T <- sum(bacteria_abund_hr$density)
# # This will produce a matrix with values: (N_i*V_j)/N_T
# A <- crossprod(matrix(bacteria_abund_hr$density, nrow=1,
# ncol=length(bacteria_abund_hr$density),
# dimnames=list(NULL, bacteria_abund_hr$B_ID)),
# matrix(virus_abund_hr$density,
# nrow=1,ncol=length(virus_abund_hr$density) ,
# dimnames=list(NULL, virus_abund_hr$V_ID)))
# A <- A/N_T
# # This will remove all cells that are 0.
# A <- A[rownames(infection_matrix),colnames(infection_matrix)]
# infection_matrix <- infection_matrix*A
# # Transform the infection network to a list
# g <- graph.incidence(t(infection_matrix), directed = F, weighted = T) # Need to transpose so the "from" would be viruses and the "to" would be bacteria
# infection_list <- as_tibble(igraph::as_data_frame(g, what = 'edges'))
# names(infection_list) <- c('V_ID', 'B_ID', 'w')
# } else { # All interactions are 0
# infection_list <- NULL
# }
#
# # Calculate the effective mutation matrix. These lines here save a double for loop
# # and selecting bacteria and virus abundances at each step.
# # First produce a matrix with N_i*V_j values:
# A <- crossprod(matrix(bacteria_abund_hr$density, nrow=1,
# ncol=length(bacteria_abund_hr$density),
# dimnames=list(NULL, bacteria_abund_hr$B_ID)),
# matrix(virus_abund_hr$density,
# nrow=1,ncol=length(virus_abund_hr$density) ,
# dimnames=list(NULL, virus_abund_hr$V_ID)))
# A <- A[rownames(immunity_matrix),colnames(immunity_matrix)]
# M0 <- 1*(immunity_matrix==0)
# M0 <- M0*A*beta*mu*phi*(1-q) # This is from Childs et al 2012 Suppl Info, page 4. The multiplication by M0 thakes only the no matches
# M1 <- 1*(immunity_matrix!=0)
# M1 <- M1*A*beta*mu*phi*p # This is from Childs et al 2012 Suppl Info, page 4. The multiplication by M1 thakes only the matches
# mutation_matrix <- M0+M1
#
# return(list(hr=hr,
# virus_ps_list=virus_ps_hr_list,
# bacteria_sp_list=bacteria_sp_hr_list,
# immunity_list=immunity_list,
# immunity_matrix=immunity_matrix,
# infection_list=infection_list,
# infection_matrix=infection_matrix,
# mutation_matrix=mutation_matrix,
# bacteria_no_sp=bacteria_no_sp,
# virus_abund_hr=virus_abund_hr,
# bacteria_abund_hr=bacteria_abund_hr,
# nodes=nodes
# ))
# }
create_networks_hr <- function(virus_data,bacteria_data,hr){
virus_data_hr <- virus_data %>% filter(timesOfRecord==hr)
bacteria_data_hr <- bacteria_data %>% filter(timesOfRecord==hr)
virus_abund_hr <- virus_data_hr %>% select(label, density) %>% rename(V_ID=label) %>% mutate(V_ID=paste('V_',str_pad(V_ID, 4, 'left', '0'),sep=''))
bacteria_abund_hr <- bacteria_data_hr %>% select(label, density) %>% rename(B_ID=label) %>% mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep=''))
## Virus-protospacer network
virus_ps_hr_list <- virus_data_hr %>% select(-time, -timesOfRecord, -density) %>%
gather(key='PSidx', value='value', -label) %>%
rename(V_ID=label, PS=value) %>%
arrange(V_ID,PS) %>%
mutate(w=1) %>%
mutate(V_ID=paste('V_',str_pad(V_ID, 4, 'left', '0'),sep='')) %>%
mutate(PS=paste('P_',str_pad(PS, 4, 'left', '0'),sep='')) %>%
select(V_ID,PS,w,PSidx)
## Bacteria-spacer network
bacteria_sp_hr_list <- bacteria_data_hr %>%
select(-time, -timesOfRecord, -density) %>%
gather(key='SPidx', value='value', -label) %>%
filter(value!=-1) %>%
rename(B_ID=label, SP=value) %>%
arrange(B_ID,SP) %>%
mutate(w=1) %>%
# mutate(w=ifelse(SP==-1,0,1)) %>%
mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep='')) %>%
mutate(SP=paste('P_',str_pad(SP, 4, 'left', '0'),sep='')) %>%
select(B_ID,SP,w,SPidx)
# When all bacteria have no spacers. This is mostly in the first moments of the simulation
if (nrow(bacteria_sp_hr_list)==0) {
bacteria_sp_hr_list <- NULL
# If all bacteria have no spacers, all of them are susceptible top all viruses
viruses <- sort(unique(virus_ps_hr_list$V_ID))
bacteria <- bacteria_data_hr %>% mutate(B_ID=paste('B_',str_pad(label, 4, 'left', '0'),sep='')) %>%
select(B_ID)
bacteria <- sort(unique(bacteria$B_ID))
immunity_matrix <- matrix(0, nrow = length(bacteria), ncol = length(viruses), dimnames = list(bacteria, viruses))
immunity_list <- NULL
} else {
# Immunity matrix
x <- virus_ps_hr_list %>% select(V_ID, PS)
y <- bacteria_sp_hr_list %>%
filter(w!=0) %>% # MUST include only the interactions using filter(w!=0), otherwise the immunity network will have false edges that should not exist.
select(B_ID, SP)
x$w=1; x=list_to_matrix(x)
y$w=1; y=list_to_matrix(y)
# Create a virus-protospacer network with all the protospacers in the system
all_ps <- sort(union(rownames(y), rownames(x)))
ps_virus <- matrix(0, nrow=length(all_ps), ncol=ncol(x), dimnames = list(all_ps, colnames(x)))
ps_virus[rownames(x), colnames(x)] <- x
# Create a bacteria-spacer network with all the spacers in the system
ps_bact <- matrix(0, nrow=length(all_ps), ncol=ncol(y), dimnames = list(all_ps, colnames(y)))
ps_bact[rownames(y), colnames(y)] <- y
# Calculate the immunity matrix
immunity_matrix <- crossprod(ps_bact, ps_virus)
# Immunity list
g <- graph.incidence(t(immunity_matrix), weighted = T) # transposing ensures that "from" is at the columns and "to" is the rows
immunity_list <- as_tibble(igraph::as_data_frame(g, 'edges')) %>% rename(V_ID=from, B_ID=to, w=weight)
}
# Infection matrix
infection_matrix <- 1*(immunity_matrix==0) # Binary infection network is the "negative (photography-wise)" of the immunity network
if (any(infection_matrix==1)){ # If there is at least one non-zero interaction in the infection network
N_T <- sum(bacteria_abund_hr$density)
# This will produce a matrix with values: (N_i*V_j)/N_T
A <- crossprod(matrix(bacteria_abund_hr$density, nrow=1,
ncol=length(bacteria_abund_hr$density),
dimnames=list(NULL, bacteria_abund_hr$B_ID)),
matrix(virus_abund_hr$density,
nrow=1,ncol=length(virus_abund_hr$density) ,
dimnames=list(NULL, virus_abund_hr$V_ID)))
A <- A/N_T
# This will remove all cells that are 0.
A <- A[rownames(infection_matrix),colnames(infection_matrix)]
infection_matrix <- infection_matrix*A
# Transform the infection network to a list
g <- graph.incidence(t(infection_matrix), directed = F, weighted = T) # Need to transpose so the "from" would be viruses and the "to" would be bacteria
infection_list <- as_tibble(igraph::as_data_frame(g, what = 'edges'))
names(infection_list) <- c('V_ID', 'B_ID', 'w')
} else { # All interactions are 0
infection_list <- NULL
}
# Define nodes in the system
viruses_hr <- sort(unique(virus_abund_hr$V_ID))
bacteria_hr <- sort(unique(bacteria_abund_hr$B_ID))
spacers_hr <- sort(union(bacteria_sp_hr_list$SP, virus_ps_hr_list$PS))
nodes <- tibble(nodeID=1:(length(viruses_hr)+length(bacteria_hr)+length(spacers_hr)),
nodeName=c(viruses_hr, bacteria_hr, spacers_hr),
type=c(rep(1,length(viruses_hr)), rep(2,length(bacteria_hr)), rep(3,length(spacers_hr)))
)
nodes$type <- as.integer(nodes$type)
return(list(hr=hr,
virus_ps_list=virus_ps_hr_list,
bacteria_sp_list=bacteria_sp_hr_list,
immunity_list=immunity_list,
immunity_matrix=immunity_matrix,
infection_list=infection_list,
infection_matrix=infection_matrix,
virus_abund_hr=virus_abund_hr,
bacteria_abund_hr=bacteria_abund_hr,
nodes=nodes
))
}
get_regimes <- function (phage_time_series, d2_threshold=0.001, do_smoothing = T, make_plots=F) {
if (do_smoothing){print('Finding regimes with smoothing...')} else {print('Finding regimes without smoothing...')}
# Need to use the relative abundance of phages, because the total abundaces
# create huge numbers in the derivatives.
x <- phage_time_series %>%
group_by(timesOfRecord) %>%
summarise(a=sum(Pdensity)) %>%
rename(time=timesOfRecord) %>%
mutate(rel_abund=a/max(a, na.rm = T))
# Find 1st and 2nd derivatives of the time series
d1 <- diff(x$rel_abund)/diff(x$time)
d1_df <- tibble(time=1:length(d1), d1=d1)
d2 <- diff(d1_df$d1)/diff(d1_df$time)
d2_df <- tibble(time=1:length(d2), d2=d2)
# Define time series points as regimes. A point is defined in a regime if its 2nd derivative is lower than a threshold, d2_threshold
suppressMessages(
regimes <- x %>% left_join(d1_df) %>% left_join(d2_df) %>%
mutate(regime=ifelse(abs(d2)<=d2_threshold,'BDR','VDR'))
)
# Find sequences of regimes/no regimes.
regime_seq <- rle(regimes$regime)
regime_seq <- tibble(len=regime_seq$lengths, regime=regime_seq$values) # len is the length of the sequence
# This allows to see points where dynamics switch from regimes to no regimes
regime_seq$switch=cumsum(regime_seq$len) # cumsum gives the point in the time series
# Smoothing eliminates small blips where there is a few points of 'VDR'
# surrounded by many 'BDR'. This threshold is decided by the distribution of
# the lengths of no regime sequences. Sequences with length smaller than that
# threshold are defined as yes.
if (do_smoothing){
smoothing_threshold <- quantile(subset(regime_seq, regime=='VDR')$len, 0.75)
smoothing <- subset(regime_seq, len<=smoothing_threshold & regime=='VDR') %>% mutate(start=switch-len)
# smoothing$start[1] <- 1
for (i in 1:nrow(smoothing)){
# print(smoothing$start[i]:smoothing$switch[i])
regimes$regime[smoothing$start[i]:smoothing$switch[i]] <- 'BDR'
}
# After smoothing need to define regime sequences again
regime_seq <- rle(regimes$regime)
regime_seq <- tibble(len=regime_seq$lengths, regime=regime_seq$values)
regime_seq$switch=cumsum(regime_seq$len)
}
# To be a regime, a sequences has to be larger than the largest no-regime section,
# which is the virus outbreak with the largest duration.
max_virus_cycle <- max(subset(regime_seq, regime=='VDR')$len)
regime_end <- which(regime_seq$len>max_virus_cycle)
regime_start <- which(regime_seq$len>max_virus_cycle)-1
regime_end <- regime_seq[regime_end,]$switch
regime_start <- regime_seq[regime_start,]$switch
if (length(regime_start)<length(regime_end)){
regime_end <- regime_end[-1]
}
regimes_df <- rbind(
tibble(start=1, end=regime_start[1]-1, regime_type='VDR'), # Add the first no regime period
tibble(start=regime_start, end=regime_end, regime_type='BDR'),
tibble(start=regime_end+1, end=c(regime_start-1, stop_time)[-1], regime_type='VDR')
)
regimes_df$duration=regimes_df$end-regimes_df$start
regimes_df %<>% arrange(start)
# Plot some diagnostics
if (make_plots){
plt_diagnostics <-
regimes %>%
gather(key='key', value='value', -time, -regime) %>%
mutate(key=factor(key,levels = c('a','rel_abund','d1','d2'))) %>%
ggplot()+
geom_line(aes(time, value))+
geom_point(aes(time, value, color=regime), size=0.5)+
scale_color_manual(values = c('#27AE60','purple'))+
scale_x_continuous(breaks = seq(0,max(x$time),500))+
# scale_x_continuous(limits=c(800,1000))+
facet_wrap(~key, scales='free', labeller = as_labeller(c(`a` = "Virus abundance",
`d1` = "First derivative",
`d2` = "Second derivative",
`rel_abund` = "Virus relative abundance")))+
geom_vline(xintercept=regime_start, col='#27AE60')+
geom_vline(xintercept=regime_end, col='purple')+
theme(legend.position = 'none')+labs(x='Time')
} else {
plt_diagnostics <- NULL
}
return(list(regimes_df=regimes_df,
plt_diagnostics=plt_diagnostics))
}
print_pattern <- function(parent, child, parent_death, child_birth, child_death, node, i){
sprintf("(%s:%f, %s:%f)%s",
parent, parent_death-child_birth,
child, child_death-child_birth,
paste(node, i , sep = "_"))
}
nodes_dataframe_to_one_root <- function(nodes, parent, children, parent_death) {
for(i in 1:nrow(children)) {
newChildren <- nodes %>% filter(parent_id == children$id[i]) %>% arrange(desc(creation_time))
if (i == 1) {
tempParent<-parent
parent_death <- parent_death
}else{
tempParent<-out
parent_death<-children$creation_time[i-1]
}
if (nrow(newChildren) > 0) {
child <- nodes_dataframe_to_one_root(nodes, children$id[i], newChildren, children$death[i])
child_birth <- children$creation_time[i]
child_death <- newChildren$creation_time[nrow(newChildren)]
}else{
child <- children$id[i]
child_birth <- children$creation_time[i]
child_death <- children$death[i]
}
#print(list(tempParent, child, parent_death, child_birth, child_death))
out<-print_pattern(tempParent, child, parent_death, child_birth, child_death, parent, nrow(children)-i)
}
return(out)
}
nodes_dataframe_to_newick <- function(nodes) {
root <- nodes %>% filter(is.na(parent_id))
stopifnot(nrow(root) == 1)
children <- nodes %>% filter(parent_id == root$id) %>% arrange(desc(creation_time))
head(children)
out<-nodes_dataframe_to_one_root(nodes, root$id[1], children, root$death[1])
return(paste(out, ":", children$creation_time[nrow(children)] - root$creation_time[1], ";",sep = ""))
}
# Function to test for phylogenetic signal in modules
test_PD_modules<- function(tree, module_object, node_start_letter){
# Phylogenetic signal analysis
D <- ape::cophenetic.phylo(tree) # Phyloegentic distance
D <- matrix_to_list_unipartite(D, directed = T) # Use directed to make sure that the from column has all the nodes (need it for joining later)
D <- D$edge_list
# Difference between tree and matrix
nodes_in_modules <- module_object$modules %>%
filter(str_starts(node_name, node_start_letter)) %>%
distinct(node_name) %>%
mutate(node_name=str_replace_all(node_name, pattern = '\\.', ''))
nodes_in_modules <- nodes_in_modules$node_name
nodes_in_tree <- tree$tip.label
# print(setdiff(nodes_in_modules, nodes_in_tree)) # In modules but not in tree
# print(setdiff(nodes_in_tree, nodes_in_modules)) # In tree but not in modules
# Overlapping nodes:
overlapping <- intersect(nodes_in_tree, nodes_in_modules)
# Observed modules
M_obs <- module_object$modules %>%
filter(str_starts(node_name, node_start_letter)) %>%
mutate(node_name=str_replace_all(node_name, pattern = '\\.', '')) %>%
filter(node_name %in% overlapping) %>%
rename(m=module_level1) %>%
select(node_name, m)
#Mean PDistance between hosts within modules
D_obs <- M_obs %>%
inner_join(D, by=c('node_name'='from')) %>% # join PD distances
rename(d=weight) %>%
arrange(m, node_name) %>%
group_by(m) %>% # Per module
filter(to %in% node_name) %>% #Host pairs within a module
summarise(d_mean=mean(d), mod_size=n())
D_obs_mean <- mean(D_obs$d_mean)
# print('Observed network:')
# print(D_obs)
#Shuffle to create permuted modules of the same size,
#and recalculate the meand PD within modules. The shuffling permutes the ID of the strains.
D_perm <- NULL
nperm <- 500
for (i in 1:nperm){
# print(i)
D_perm %<>% bind_rows(
M_obs %>%
mutate(node_name=sample(node_name, replace = F)) %>%
inner_join(D, by=c('node_name'='from')) %>% # join PD distances
rename(d=weight) %>%
arrange(m, node_name) %>%
group_by(m) %>% # Per module
filter(to %in% node_name) %>% #Host pairs within a module
summarise(d_mean=mean(d)) %>% # Calculate mean PD within modules
mutate(run=i)
)
}
# Null hypothesis is that the permuted distance is smaller than the observed for
# each module (i.e., no signal). If we reject this hypothesis then there is
# phylogenetic signal because the observed PD beteween hosts within each module
# would be smaller than expected by chance (closely related hosts share a module).
# Plot the means
plt_across_modules <-
D_perm %>% group_by(run) %>%
summarise(D_perm_mean=mean(d_mean)) %>%
ggplot(aes(x=D_perm_mean))+geom_histogram()+geom_vline(xintercept = D_obs_mean)
result_across_moduels <-
D_perm %>% group_by(run) %>%
summarise(D_perm=mean(d_mean)) %>%
mutate(test=D_perm<D_obs_mean) %>%
summarise(pvalue=sum(test)/nperm) %>%
mutate(res=ifelse(pvalue<0.05,'Signal','No signal'))
# This can also be tested per module
plt_within_modules <-
D_perm %>%
full_join(D_obs, by='m') %>%
rename(d_perm=d_mean.x, d_obs=d_mean.y) %>%
ggplot(aes(x=d_perm))+
geom_histogram()+
facet_wrap(~m)+
geom_vline(data = D_obs, aes(xintercept = d_mean))
result_within_moduels <-
D_perm %>%
full_join(D_obs, by='m') %>%
rename(d_perm=d_mean.x, d_obs=d_mean.y) %>%
mutate(test=d_perm<d_obs) %>%
group_by(m) %>%
summarise(pvalue=sum(test)/nperm) %>%
mutate(Signif=ifelse(pvalue<0.05,'Signal','No signal'),
Signif_Bonferroni=ifelse(pvalue<0.05/nrow(D_obs),'Signal','No signal')) # Need to divide by number of modules for Bonferroni correction
out <- list(D_obs=D_obs,
D_obs_mean=D_obs_mean,
plt_across_modules=plt_across_modules,
plt_within_modules=plt_within_modules,
result_across_moduels=result_across_moduels,
result_within_moduels=result_within_moduels,
nodes_in_modules=nodes_in_modules,
nodes_in_tree=nodes_in_tree,
overlapping=overlapping)
return(out)
}
# Inititalize -------------------------------------------------------------
virus_data <- read_delim(paste(base_name,'_data-phage.txt',sep=''), delim=' ', col_names = T)
bacteria_data <- read_delim(paste(base_name,'_data-bact.txt',sep=''), delim=' ', col_names = T)
bacteria_abundance <- read_delim(paste(base_name,'_Bacteria-abundance.txt',sep=''), delim = ' ')
phage_abundance <- read_delim(paste(base_name,'_Phage-abundance.txt',sep=''), delim = ' ')
# Defined the length of the simulation
stop_time <- min(max(virus_data$timesOfRecord), max(phage_abundance$timesOfRecord))
hr_seq <- seq(1, stop_time, 1)
print(paste('-------- Working simulation:',base_name,' | stop time: ',stop_time,' | mu: ',mu,' | protospacers: ', protospacer_len,' | make plots: ',make_plots,'---------'))
virus_data %<>% filter(timesOfRecord<=stop_time)
bacteria_data %<>% filter(timesOfRecord<=stop_time)
bacteria_abundance %<>% filter(timesOfRecord<=stop_time)
phage_abundance %<>% filter(timesOfRecord<=stop_time)
regimes_df <- get_regimes(phage_time_series = phage_abundance, do_smoothing = T)$regimes_df
if (regimes_df[nrow(regimes_df),]$duration==1){regimes_df <- regimes_df[-nrow(regimes_df),]} # remove the end of simulation spurious effect
record_data(regimes_df)
BDRs <- subset(regimes_df, regime_type=='BDR')
VDRs <- subset(regimes_df, regime_type=='VDR')
# Vectors with the time points of VDRs and BDRs
VDR_hrs <- unlist(apply(VDRs, MARGIN = 1, FUN = function(x) seq(x[1],x[2])))
BDR_hrs <- unlist(apply(BDRs, MARGIN = 1, FUN = function(x) seq(x[1],x[2])))
# This is for the x axis labels when plotting
label_seq <- pretty(hr_seq, n=10)
label_seq <- subset(label_seq, label_seq<stop_time)
regimes_seq <- tibble(hr=c(VDR_hrs,BDR_hrs), regime_type=c(rep('VDR',length(VDR_hrs)),rep('BDR',length(BDR_hrs)))) %>% arrange(hr)
record_data(regimes_seq)
# bacteria / phage diversity ----------------------------------------------
notify('Generating abundance profile plots...')
dom_strains_num <- 100
cols <- c('gray50',gg_color_hue(dom_strains_num))
dominant_strains <- bacteria_abundance %>%
group_by(label) %>%
summarise(mean_abund=mean(Bdensity)) %>%
top_n(n=dom_strains_num, wt = mean_abund) %>%
arrange(label) %>%
mutate(dominant=T)
dom <- bacteria_abundance %>%
filter(label%in%dominant_strains$label) %>%
select(timesOfRecord, Bdensity, label)
non_dom <- bacteria_abundance %>%
filter(!label%in%dominant_strains$label) %>%
group_by(timesOfRecord) %>% summarise(Bdensity=sum(Bdensity)) %>%
mutate(label='0')
to_plot <- rbind(dom,non_dom)
to_plot$label <- factor(to_plot$label, levels=sort(as.numeric(unique(to_plot$label))))
plt_bact_abund <-
standard_plot(
ggplot(to_plot, aes(x=timesOfRecord,y=Bdensity/10^5, fill=label))+
geom_area(stat="identity", color='black', size=0.2)+
scale_fill_manual(values = cols)+
labs(y='Bact. abund. (*10^5)')+
theme(legend.position = 'none')+
geom_hline(yintercept = 10^5.5/10^5, linetype='dashed', size=0.4)
)
make_png(plt_bact_abund)
# Virus abundance
dominant_strains <- phage_abundance %>%
group_by(label) %>%
summarise(mean_abund=mean(Pdensity)) %>%
top_n(n=dom_strains_num, wt = mean_abund) %>%
arrange(label) %>%
mutate(dominant=T)
dom <- phage_abundance %>%
filter(label%in%dominant_strains$label) %>%
select(timesOfRecord, Pdensity, label)
non_dom <- phage_abundance %>%
filter(!label%in%dominant_strains$label) %>%
group_by(timesOfRecord) %>%
summarise(Pdensity=sum(Pdensity)) %>%
mutate(label='0')
to_plot <- rbind(dom,non_dom)
to_plot$label <- factor(to_plot$label, levels=sort(as.numeric(unique(to_plot$label))))
plt_virus_abund <-
standard_plot(
ggplot(to_plot, aes(x=timesOfRecord,y=Pdensity/10^7, fill=label))+
geom_area(stat="identity", color='black',size=0.2)+
scale_fill_manual(values = cols)+
labs(y='Virus abund. (*10^7)')+
theme(legend.position = 'none')
)
plt_abundance_profiles <- plot_grid(plt_bact_abund, plt_virus_abund, nrow=2, align = 'vh')
make_png(plt_virus_abund)
make_png(plt_abundance_profiles)
make_svg(plt_abundance_profiles)
# Generate networks -------------------------------------------------------
print('Generating networks...')
all_networks <- vector(mode = 'list', length = length(hr_seq))
for (hr in hr_seq){
nets <- create_networks_hr(virus_data, bacteria_data, hr)
print(nets$nodes %>% group_by(type) %>% count())
all_networks[[which(hr_seq==hr)]] <- nets
notify(paste('Generated networks for time ',hr,'/',stop_time,sep=''))
}
# Measures of diversity ---------------------------------------------------
# Data frame for virus density (abundance)
virus_density <- NULL
for (hr in hr_seq){
tmp <- all_networks[[which(hr_seq==hr)]]$virus_abund_hr
tmp$hr <- hr
virus_density <- rbind(virus_density, tmp)
}
total_density <- virus_density %>% group_by(hr) %>% summarise(total_density_hr=sum(density))
virus_density %<>% left_join(total_density) %>% select(hr,V_ID,density,total_density_hr)
record_data(virus_density)
# Data frame for bacteria density (abundance)
bacteria_density <- NULL
for (hr in hr_seq){
tmp <- all_networks[[which(hr_seq==hr)]]$bacteria_abund_hr
tmp$hr <- hr
bacteria_density <- rbind(bacteria_density, tmp)
}
total_density <- bacteria_density %>% group_by(hr) %>% summarise(total_density_hr=sum(density))
bacteria_density %<>% left_join(total_density) %>% select(hr,B_ID,density,total_density_hr)
record_data(bacteria_density)
# Immunity network density and size
imm_density_size <- NULL
for (hr in hr_seq){
x <- all_networks[[which(hr_seq==hr)]]$immunity_matrix
d <- sum(x!=0)/(nrow(x)*ncol(x))
imm_density_size <- rbind(imm_density_size, tibble(hr=hr, Density=d, Links=sum(x!=0), B=nrow(x), V=ncol(x), Size=nrow(x)+ncol(x)))
}
record_data(imm_density_size)
plt_immunity_network_size <-
standard_plot(
imm_density_size %>%
select(hr,Density,Links,Size) %>%
gather(key = 'variable', value = 'value', -hr) %>%
ggplot(aes(hr, value))+
geom_line()+
facet_grid(variable~., scales = 'free_y')+
labs(title='Immunity network information')
)
make_png(plt_immunity_network_size)
make_svg(plt_immunity_network_size)
# Richness
richness <- NULL
for (hr in hr_seq){
x <- all_networks[[which(hr_seq==hr)]]
x <- x$nodes %>% group_by(type) %>% count()
x$hr <- hr
richness <- rbind(richness, x)
}
richness %<>% mutate(node_type=case_when(type==1~'viruses',
type==2~'bacteria',
type==3~'spacers'))
record_data(richness)
plt_richness <-
standard_plot(
ggplot(richness, aes(hr, n, color=node_type))+
geom_line(size=1.5)+
scale_color_manual(values = c('blue','brown', 'red'))+
labs(y='Richness')
)
make_png(plt_richness)
# Phage and bacteria diversification ----------------------------------
print(' ------- Creating virus and bacteria persistence data frames -------')
# These data frames contain the extinction/mutation events and persistence of viruses/bacteria
virus_dynamics_list <- virus_data %>%
filter(timesOfRecord<=max(hr_seq)) %>%
select(timesOfRecord, label) %>%
rename(V_ID=label) %>%
mutate(V_ID=paste('V_',str_pad(V_ID, 4, 'left', '0'),sep=''), w=1) %>%
arrange(timesOfRecord, V_ID)
virus_dynamics_list %<>% group_by(V_ID) %>% summarise(birth=first(timesOfRecord), death=last(timesOfRecord)) %>%
mutate(persistence=death-birth+1)
virus_dynamics_list %<>% filter(birth>=min(hr_seq), death<=max(hr_seq))
record_data(virus_dynamics_list)
plt_virus_persistence <-
standard_plot(
ggplot(virus_dynamics_list)+
geom_rect(aes(ymin=parse_number(V_ID),
ymax=parse_number(V_ID),
xmin=birth,
xmax=death), color='red',
size=0.7)+
labs(y='Virus ID')+
theme(legend.position = 'none')
)
make_png(plt_virus_persistence)
make_svg(plt_virus_persistence)
bacteria_dynamics_list <-
bacteria_data %>%
filter(timesOfRecord<=max(hr_seq)) %>%
select(timesOfRecord, label) %>%
rename(B_ID=label) %>%
mutate(B_ID=paste('B_',str_pad(B_ID, 4, 'left', '0'),sep=''), w=1) %>%
arrange(timesOfRecord, B_ID)
bacteria_dynamics_list %<>% group_by(B_ID) %>% summarise(birth=first(timesOfRecord), death=last(timesOfRecord)) %>%
mutate(persistence=death-birth+1)
bacteria_dynamics_list %<>% filter(birth>=min(hr_seq), death<=max(hr_seq))
record_data(bacteria_dynamics_list)
plt_bacteria_persistence <-
standard_plot(
ggplot(bacteria_dynamics_list)+
geom_rect(aes(ymin=parse_number(B_ID),
ymax=parse_number(B_ID),
xmin=birth,
xmax=death), color='blue',
size=0.7)+
labs(y='Bacteria ID')+
theme(legend.position = 'none')
)
make_png(plt_bacteria_persistence)
make_svg(plt_bacteria_persistence)
# Trees -------------------------------------------------------------------
tree_data <- read_delim(paste(base_name,'_Phage-TREE.txt',sep=''), delim = '\t',col_names = c("Recordtime","id","parent_id","creation_time"))
tree_data$id <- paste('V_',str_pad(tree_data$id, 4, 'left', '0'),sep='')
tree_data$parent_id <- paste('V_',str_pad(tree_data$parent_id, 4, 'left', '0'),sep='')
tree_data %<>% left_join(virus_dynamics_list %>% select(V_ID,death), by=c('id'='V_ID'))
tree_data[1,3] <- NA
tree_data$death[is.na(tree_data$death)]<-tree_data$creation_time[is.na(tree_data$death)]+1
tree <- nodes_dataframe_to_newick(tree_data)
writeLines(tree, 'tree.nwk')
tree_viruses <- treeio::read.tree('tree.nwk')
plt_viruses_tree <-
standard_plot(
ggtree::ggtree(tree_viruses, ladderize = T) +
ggtree::theme_tree2()
)
make_png(plt_viruses_tree)
make_svg(plt_viruses_tree)
# Bacteria tree
tree_data <- read_delim(paste(base_name,'_Bacteria-TREE.txt',sep=''), delim = '\t',col_names = c("Recordtime","id","parent_id","creation_time"))
tree_data$id <- paste('B_',str_pad(tree_data$id, 4, 'left', '0'),sep='')
tree_data$parent_id <- paste('B_',str_pad(tree_data$parent_id, 4, 'left', '0'),sep='')
tree_data %<>% left_join(bacteria_dynamics_list %>% select(B_ID,death), by=c('id'='B_ID'))
tree_data[1,3] <- NA
tree_data$death[is.na(tree_data$death)]<-tree_data$creation_time[is.na(tree_data$death)]+1
tree <- nodes_dataframe_to_newick(tree_data)
writeLines(tree, 'tree_bacteria.nwk')
tree_bacteria <- treeio::read.tree('tree_bacteria.nwk')
plt_bacteria_tree <-
standard_plot(
ggtree::ggtree(tree_bacteria, ladderize = F) +
ggtree::theme_tree2()
)
make_png(plt_bacteria_tree)
make_svg(plt_bacteria_tree)
# Modularity of infection networks ----------------------------------------
modules_df_infection <- NULL
for (hr in hr_seq){
notify(paste('Infection networks (modularity) ',hr,'/',stop_time,sep=''))
edges <- all_networks[[which(hr_seq==hr)]]$infection_list
if(is.null(edges)){next}
nodes <- all_networks[[which(hr_seq==hr)]]$nodes
x <- create_monolayer_object(edges, bipartite = T)
modules <- run_infomap_monolayer(x, infomap_executable = 'Infomap', flow_model = 'undirected', silent = T,
trials = 20, two_level = T, seed = 123,
signif = F)
if (!is.null(modules)){
x <- modules$modules %>% select(node_id, node_name, m=module_level1) %>% mutate(hr=hr)
modules_df_infection <- rbind(modules_df_infection, x)
if (make_plots){
png(paste('plots/infection_modules_',str_pad(hr,4,'left','0'),'.png',sep=''),1920,1080,res=150)
print(
plot_modular_matrix(modules)+
theme(axis.text = element_text(size=6),
axis.text.x = element_text(angle=-90))+
labs(title=hr)
)
dev.off()
}
}
}
record_data(modules_df_infection)
plt_modules_infection <-
standard_plot(
modules_df_infection %>% group_by(hr) %>% summarise(num_mod=length(unique(m))) %>%
ggplot(aes(hr,num_mod))+
geom_line(size=1.2)+
labs(y='Number of modules')+
theme(
axis.text = element_text(size = 16),
axis.title = element_text(size = 16),
legend.position = 'none'
)
)
make_png(plt_modules_infection)
make_svg(plt_modules_infection)
# Significance of modularity of infection networks --------------------------------------
infection_modularity_signif <- NULL
# Test at the end of each VDR
for (i in 1:nrow(VDRs)){
hr <- VDRs$end[i]
edges <- all_networks[[which(hr_seq==hr)]]$infection_list
x <- create_monolayer_object(edges, directed = F, bipartite = T)
test <- run_infomap_monolayer(x, infomap_executable = 'Infomap', flow_model = 'undirected', silent = T, trials = 100, two_level = T, seed = 123, signif = T, shuff_method = 'r00', nsim = 1000)
infection_modularity_signif %<>% bind_rows(tibble(hr=hr,
pvalue=test$pvalue,
n_hosts=ncol(x$mat),
n_spacer=nrow(x$mat),
n_interactions=sum(x$mat>0),
n_modules=max(test$modules$module_level1)))
plot_modular_matrix(test)
}
record_data(infection_modularity_signif)
# Phylogenetic signal in infection networks --------------------------------------
phylogenetic_signal_infection <- NULL
# Test at the end of each VDR
for (i in 1:nrow(VDRs)){
hr <- VDRs$end[i]
edges <- all_networks[[which(hr_seq==hr)]]$infection_list
x <- create_monolayer_object(edges, directed = F, bipartite = T)
infection_modularity <- run_infomap_monolayer(x, infomap_executable = 'Infomap', flow_model = 'undirected', silent = T, trials = 100, two_level = T, seed = 123, signif = F)
pd_results <- test_PD_modules(tree = tree_bacteria, module_object = infection_modularity, node_start_letter = 'B')
out <- pd_results$result_within_moduels %>% left_join(pd_results$D_obs)
out$VDR <- i
phylogenetic_signal_infection <- rbind(phylogenetic_signal_infection, out)
}
record_data(phylogenetic_signal_infection)
# Create Table S1
# x=phylogenetic_signal_infection %>% select(VDR_ID=VDR, Module=m, Size=mod_size, Pvalue=pvalue, Significant=Signif)
# print(xtable::xtable(x, type = "latex"), file = "table_s1.tex", include.rownames = F)
# Significance of modularity of host-spacer networks --------------------------------------
# Agregate host-spacer networks within each VDR and test for significance of modularity
host_sp_modularity <- NULL
for (i in 1:nrow(VDRs)){
metaweb <- NULL
for (hr in VDRs$start[i]:VDRs$end[i]){
edges <- all_networks[[which(hr_seq==hr)]]$bacteria_sp_list
if(is.null(edges)){next}
metaweb %<>% bind_rows(edges) %>% distinct(B_ID, SP)
}
metaweb %<>% mutate(w=1)
x <- create_monolayer_object(metaweb, directed = F, bipartite = T)
test <- run_infomap_monolayer(x, infomap_executable = 'Infomap', flow_model = 'undirected', silent = T, trials = 100, two_level = T, seed = 123, signif = T, shuff_method = 'r00', nsim = 100)
host_sp_modularity %<>% bind_rows(tibble(hr=hr,
pvalue=test$pvalue,
n_hosts=ncol(x$mat),
n_spacer=nrow(x$mat),
n_interactions=sum(x$mat>0),
n_modules=max(test$modules$module_level1)))
}
record_data(host_sp_modularity)
# Phylogenetic signal in host-spacer modules ------------------------------
# Agregate host-spacer networks within each VDR and test for significance of modularity
phylogenetic_signal_hs <- NULL
for (i in 1:nrow(VDRs)){
metaweb <- NULL
for (hr in VDRs$start[i]:VDRs$end[i]){
edges <- all_networks[[which(hr_seq==hr)]]$bacteria_sp_list
if(is.null(edges)){next}