-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClustering.Rmd
829 lines (737 loc) · 39.5 KB
/
Clustering.Rmd
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
---
title: "Clustering"
author: "Jackson Pullman"
date: "2022-12-24"
output: html_notebook
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#Clustering
library(ggplotify)
#Imports Rand Index
library(mclust)
#citation("mclust")
#Get observed metric
#Louvain clustering does not work with NA edge weights, set to zero
E(strain_rate_vil_graph_1)$weight[is.na(E(strain_rate_vil_graph_1)$weight)] <- 0
E(strain_rate_vil_graph_2)$weight[is.na(E(strain_rate_vil_graph_2)$weight)] <- 0
E(strain_rate_vil_graph_3)$weight[is.na(E(strain_rate_vil_graph_3)$weight)] <- 0
E(strain_rate_vil_graph_4)$weight[is.na(E(strain_rate_vil_graph_4)$weight)] <- 0
E(strain_rate_vil_graph_5)$weight[is.na(E(strain_rate_vil_graph_5)$weight)] <- 0
E(strain_rate_vil_graph_6)$weight[is.na(E(strain_rate_vil_graph_6)$weight)] <- 0
E(strain_rate_vil_graph_7)$weight[is.na(E(strain_rate_vil_graph_7)$weight)] <- 0
E(strain_rate_vil_graph_8)$weight[is.na(E(strain_rate_vil_graph_8)$weight)] <- 0
E(strain_rate_vil_graph_9)$weight[is.na(E(strain_rate_vil_graph_9)$weight)] <- 0
E(strain_rate_vil_graph_10)$weight[is.na(E(strain_rate_vil_graph_10)$weight)] <- 0
E(strain_rate_vil_graph_11)$weight[is.na(E(strain_rate_vil_graph_11)$weight)] <- 0
E(strain_rate_vil_graph_12)$weight[is.na(E(strain_rate_vil_graph_12)$weight)] <- 0
E(strain_rate_vil_graph_13)$weight[is.na(E(strain_rate_vil_graph_13)$weight)] <- 0
E(strain_rate_vil_graph_14)$weight[is.na(E(strain_rate_vil_graph_14)$weight)] <- 0
E(strain_rate_vil_graph_15)$weight[is.na(E(strain_rate_vil_graph_15)$weight)] <- 0
E(strain_rate_vil_graph_16)$weight[is.na(E(strain_rate_vil_graph_16)$weight)] <- 0
E(strain_rate_vil_graph_17)$weight[is.na(E(strain_rate_vil_graph_17)$weight)] <- 0
E(strain_rate_vil_graph_18)$weight[is.na(E(strain_rate_vil_graph_18)$weight)] <- 0
for(i in 1:length(village_names)){
assign(paste0("strain_rate_vil_graph_scram_", i), get(paste0("strain_rate_vil_graph_", i)))
}
set.seed(1)
strain_rate_cluster_1 <- cluster_louvain(strain_rate_vil_graph_1, weights = E(strain_rate_vil_graph_1)$weight)
strain_rate_cluster_2 <- cluster_louvain(strain_rate_vil_graph_2, weights = E(strain_rate_vil_graph_2)$weight)
strain_rate_cluster_3 <- cluster_louvain(strain_rate_vil_graph_3, weights = E(strain_rate_vil_graph_3)$weight)
strain_rate_cluster_4 <- cluster_louvain(strain_rate_vil_graph_4, weights = E(strain_rate_vil_graph_4)$weight)
strain_rate_cluster_5 <- cluster_louvain(strain_rate_vil_graph_5, weights = E(strain_rate_vil_graph_5)$weight)
strain_rate_cluster_6 <- cluster_louvain(strain_rate_vil_graph_6, weights = E(strain_rate_vil_graph_6)$weight)
strain_rate_cluster_7 <- cluster_louvain(strain_rate_vil_graph_7, weights = E(strain_rate_vil_graph_7)$weight)
strain_rate_cluster_8 <- cluster_louvain(strain_rate_vil_graph_8, weights = E(strain_rate_vil_graph_8)$weight)
strain_rate_cluster_9 <- cluster_louvain(strain_rate_vil_graph_9, weights = E(strain_rate_vil_graph_9)$weight)
strain_rate_cluster_10 <- cluster_louvain(strain_rate_vil_graph_10, weights = E(strain_rate_vil_graph_10)$weight)
strain_rate_cluster_11 <- cluster_louvain(strain_rate_vil_graph_11, weights = E(strain_rate_vil_graph_11)$weight)
strain_rate_cluster_12 <- cluster_louvain(strain_rate_vil_graph_12, weights = E(strain_rate_vil_graph_12)$weight)
strain_rate_cluster_13 <- cluster_louvain(strain_rate_vil_graph_13, weights = E(strain_rate_vil_graph_13)$weight)
strain_rate_cluster_14 <- cluster_louvain(strain_rate_vil_graph_14, weights = E(strain_rate_vil_graph_14)$weight)
strain_rate_cluster_15 <- cluster_louvain(strain_rate_vil_graph_15, weights = E(strain_rate_vil_graph_15)$weight)
strain_rate_cluster_16 <- cluster_louvain(strain_rate_vil_graph_16, weights = E(strain_rate_vil_graph_16)$weight)
strain_rate_cluster_17 <- cluster_louvain(strain_rate_vil_graph_17, weights = E(strain_rate_vil_graph_17)$weight)
strain_rate_cluster_18 <- cluster_louvain(strain_rate_vil_graph_18, weights = E(strain_rate_vil_graph_18)$weight)
#Get microbiome clustering for each village based on count of shared strains
set.seed(1)
#Get social network clustering for each village
sn_cluster_1 <- cluster_louvain(sn_vil_graph_1)
sn_cluster_2 <- cluster_louvain(sn_vil_graph_2)
sn_cluster_3 <- cluster_louvain(sn_vil_graph_3)
sn_cluster_4 <- cluster_louvain(sn_vil_graph_4)
sn_cluster_5 <- cluster_louvain(sn_vil_graph_5)
sn_cluster_6 <- cluster_louvain(sn_vil_graph_6)
sn_cluster_7 <- cluster_louvain(sn_vil_graph_7)
sn_cluster_8 <- cluster_louvain(sn_vil_graph_8)
sn_cluster_9 <- cluster_louvain(sn_vil_graph_9)
sn_cluster_10 <- cluster_louvain(sn_vil_graph_10)
sn_cluster_11 <- cluster_louvain(sn_vil_graph_11)
sn_cluster_12 <- cluster_louvain(sn_vil_graph_12)
sn_cluster_13 <- cluster_louvain(sn_vil_graph_13)
sn_cluster_14 <- cluster_louvain(sn_vil_graph_14)
sn_cluster_15 <- cluster_louvain(sn_vil_graph_15)
sn_cluster_16 <- cluster_louvain(sn_vil_graph_16)
sn_cluster_17 <- cluster_louvain(sn_vil_graph_17)
sn_cluster_18 <- cluster_louvain(sn_vil_graph_18)
#Order names for both clusters
mbiome_member_1 <- membership(strain_rate_cluster_1)[match(names(membership(sn_cluster_1)),
names(membership(strain_rate_cluster_1)))]
mbiome_member_2 <- membership(strain_rate_cluster_2)[match(names(membership(sn_cluster_2)),
names(membership(strain_rate_cluster_2)))]
mbiome_member_3 <- membership(strain_rate_cluster_3)[match(names(membership(sn_cluster_3)),
names(membership(strain_rate_cluster_3)))]
mbiome_member_4 <- membership(strain_rate_cluster_4)[match(names(membership(sn_cluster_4)),
names(membership(strain_rate_cluster_4)))]
mbiome_member_5 <- membership(strain_rate_cluster_5)[match(names(membership(sn_cluster_5)),
names(membership(strain_rate_cluster_5)))]
mbiome_member_6 <- membership(strain_rate_cluster_6)[match(names(membership(sn_cluster_6)),
names(membership(strain_rate_cluster_6)))]
mbiome_member_7 <- membership(strain_rate_cluster_7)[match(names(membership(sn_cluster_7)),
names(membership(strain_rate_cluster_7)))]
mbiome_member_8 <- membership(strain_rate_cluster_8)[match(names(membership(sn_cluster_8)),
names(membership(strain_rate_cluster_8)))]
mbiome_member_9 <- membership(strain_rate_cluster_9)[match(names(membership(sn_cluster_9)),
names(membership(strain_rate_cluster_9)))]
mbiome_member_10 <- membership(strain_rate_cluster_10)[match(names(membership(sn_cluster_10)),
names(membership(strain_rate_cluster_10)))]
mbiome_member_11 <- membership(strain_rate_cluster_11)[match(names(membership(sn_cluster_11)),
names(membership(strain_rate_cluster_11)))]
mbiome_member_12 <- membership(strain_rate_cluster_12)[match(names(membership(sn_cluster_12)),
names(membership(strain_rate_cluster_12)))]
mbiome_member_13 <- membership(strain_rate_cluster_13)[match(names(membership(sn_cluster_13)),
names(membership(strain_rate_cluster_13)))]
mbiome_member_14 <- membership(strain_rate_cluster_14)[match(names(membership(sn_cluster_14)),
names(membership(strain_rate_cluster_14)))]
mbiome_member_15 <- membership(strain_rate_cluster_15)[match(names(membership(sn_cluster_15)),
names(membership(strain_rate_cluster_15)))]
mbiome_member_16 <- membership(strain_rate_cluster_16)[match(names(membership(sn_cluster_16)),
names(membership(strain_rate_cluster_16)))]
mbiome_member_17 <- membership(strain_rate_cluster_17)[match(names(membership(sn_cluster_17)),
names(membership(strain_rate_cluster_17)))]
mbiome_member_18 <- membership(strain_rate_cluster_18)[match(names(membership(sn_cluster_18)),
names(membership(strain_rate_cluster_18)))]
#adjust village cluster numbers so the are different across villages
mbiome_member_1 <- mbiome_member_1 + 100
mbiome_member_2 <- mbiome_member_2 + 200
mbiome_member_3 <- mbiome_member_3 + 300
mbiome_member_4 <- mbiome_member_4 + 400
mbiome_member_5 <- mbiome_member_5 + 500
mbiome_member_6 <- mbiome_member_6 + 600
mbiome_member_7 <- mbiome_member_7 + 700
mbiome_member_8 <- mbiome_member_8 + 800
mbiome_member_9 <- mbiome_member_9 + 900
mbiome_member_10 <- mbiome_member_10 + 1000
mbiome_member_11 <- mbiome_member_11 + 1100
mbiome_member_12 <- mbiome_member_12 + 1200
mbiome_member_13 <- mbiome_member_13 + 1300
mbiome_member_14 <- mbiome_member_14 + 1400
mbiome_member_15 <- mbiome_member_15 + 1500
mbiome_member_16 <- mbiome_member_16 + 1600
mbiome_member_17 <- mbiome_member_17 + 1700
mbiome_member_18 <- mbiome_member_18 + 1800
#Create social network membership vectors, shifted by village
sn_member_1 <- membership(sn_cluster_1) + 100
sn_member_2 <- membership(sn_cluster_2) + 200
sn_member_3 <- membership(sn_cluster_3) + 300
sn_member_4 <- membership(sn_cluster_4) + 400
sn_member_5 <- membership(sn_cluster_5) + 500
sn_member_6 <- membership(sn_cluster_6) + 600
sn_member_7 <- membership(sn_cluster_7) + 700
sn_member_8 <- membership(sn_cluster_8) + 800
sn_member_9 <- membership(sn_cluster_9) + 900
sn_member_10 <- membership(sn_cluster_10) + 1000
sn_member_11 <- membership(sn_cluster_11) + 1100
sn_member_12 <- membership(sn_cluster_12) + 1200
sn_member_13 <- membership(sn_cluster_13) + 1300
sn_member_14 <- membership(sn_cluster_14) + 1400
sn_member_15 <- membership(sn_cluster_15) + 1500
sn_member_16 <- membership(sn_cluster_16) + 1600
sn_member_17 <- membership(sn_cluster_17) + 1700
sn_member_18 <- membership(sn_cluster_18) + 1800
#Get rand index across all villages
all_mbiome_membership <-c(mbiome_member_1,
mbiome_member_2,
mbiome_member_3,
mbiome_member_4,
mbiome_member_5,
mbiome_member_6,
mbiome_member_7,
mbiome_member_8,
mbiome_member_9,
mbiome_member_10,
mbiome_member_11,
mbiome_member_12,
mbiome_member_13,
mbiome_member_14,
mbiome_member_15,
mbiome_member_16,
mbiome_member_17,
mbiome_member_18)
all_sn_membership <- c(sn_member_1,
sn_member_2,
sn_member_3,
sn_member_4,
sn_member_5,
sn_member_6,
sn_member_7,
sn_member_8,
sn_member_9,
sn_member_10,
sn_member_11,
sn_member_12,
sn_member_13,
sn_member_14,
sn_member_15,
sn_member_16,
sn_member_17,
sn_member_18)
```
#Get Statistics on clustering
```{r}
all_sn_membership_df <- data.frame(name = names(all_sn_membership),
cluster = unname(all_sn_membership))
all_mbiome_membership_df <- data.frame(name = names(all_mbiome_membership),
cluster = unname(all_mbiome_membership))
```
#Average number of people in social network clusters
```{r}
all_sn_membership_df %>%
group_by(cluster) %>%
summarize(n = n()) %>%
summarise(mean(n), median(n), sd(n))
```
#Average number of people in strain-sharing network clusters
```{r}
all_mbiome_membership_df %>%
group_by(cluster) %>%
summarize(n = n()) %>%
summarise(mean(n), median(n), sd(n))
```
```{r}
all_mbiome_membership_df %>%
group_by(cluster) %>%
summarize(n = n()) %>%
arrange(n)
```
#Average strain-sharing rate within cluster
```{r}
uniq_clusters <- unique(all_mbiome_membership_df$cluster)
cluster_sharing_all <- c()
for(i in 1:length(uniq_clusters)){
cluster_names <- all_mbiome_membership_df %>% filter(cluster == uniq_clusters[i]) %>% pull(name)
cluster_sharing <- strain_rate[rownames(strain_rate) %in% cluster_names,
colnames(strain_rate) %in% cluster_names]
cluster_sharing[lower.tri(cluster_sharing, diag = TRUE)] <- NA
#cluster_sharing_all <- c(cluster_sharing_all,
# na.omit(unlist(as.list(cluster_sharing))))
cluster_sharing_all <- c(cluster_sharing_all,
mean(unlist(as.list(cluster_sharing)), na.rm = TRUE))
}
median(cluster_sharing_all, na.rm = TRUE)
```
#Average number of ties
```{r}
uniq_clusters <- unique(all_sn_membership_df$cluster)
cluster_ties_all <- c()
SN_Graph_all <- as_adjacency_matrix(simplify(graph_from_data_frame(SN, directed = FALSE)))
for(i in 1:length(uniq_clusters)){
cluster_names <- all_sn_membership_df %>% filter(cluster == uniq_clusters[i]) %>% pull(name)
cluster_count <- SN_Graph_all[rownames(SN_Graph_all) %in% cluster_names,
colnames(SN_Graph_all) %in% cluster_names]
cluster_count[lower.tri(cluster_count, diag = TRUE)] <- NA
#cluster_sharing_all <- c(cluster_sharing_all,
# na.omit(unlist(as.list(cluster_sharing))))
cluster_ties_all <- c(cluster_ties_all,
sum(unlist(as.list(cluster_count)), na.rm = TRUE))
}
mean(cluster_ties_all)
```
```{r}
#Create null distribution metrics
cl <- parallel::makeCluster(100)
doParallel::registerDoParallel(cl)
foreach(i= c(1:10000), .combine=rbind, .packages = c('igraph')) %dopar% {
#Scramble microbiome
strain_rate_vil_graph_scram_1 <- set.vertex.attribute(strain_rate_vil_graph_scram_1, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_1, "name")))
strain_rate_vil_graph_scram_2 <- set.vertex.attribute(strain_rate_vil_graph_scram_2, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_2, "name")))
strain_rate_vil_graph_scram_3 <- set.vertex.attribute(strain_rate_vil_graph_scram_3, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_3, "name")))
strain_rate_vil_graph_scram_4 <- set.vertex.attribute(strain_rate_vil_graph_scram_4, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_4, "name")))
strain_rate_vil_graph_scram_5 <- set.vertex.attribute(strain_rate_vil_graph_scram_5, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_5, "name")))
strain_rate_vil_graph_scram_6 <- set.vertex.attribute(strain_rate_vil_graph_scram_6, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_6, "name")))
strain_rate_vil_graph_scram_7 <- set.vertex.attribute(strain_rate_vil_graph_scram_7, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_7, "name")))
strain_rate_vil_graph_scram_8 <- set.vertex.attribute(strain_rate_vil_graph_scram_8, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_8, "name")))
strain_rate_vil_graph_scram_9 <- set.vertex.attribute(strain_rate_vil_graph_scram_9, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_9, "name")))
strain_rate_vil_graph_scram_10 <- set.vertex.attribute(strain_rate_vil_graph_scram_10, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_10, "name")))
strain_rate_vil_graph_scram_11 <- set.vertex.attribute(strain_rate_vil_graph_scram_11, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_11, "name")))
strain_rate_vil_graph_scram_12 <- set.vertex.attribute(strain_rate_vil_graph_scram_12, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_12, "name")))
strain_rate_vil_graph_scram_13 <- set.vertex.attribute(strain_rate_vil_graph_scram_13, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_13, "name")))
strain_rate_vil_graph_scram_14 <- set.vertex.attribute(strain_rate_vil_graph_scram_14, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_14, "name")))
strain_rate_vil_graph_scram_15 <- set.vertex.attribute(strain_rate_vil_graph_scram_15, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_15, "name")))
strain_rate_vil_graph_scram_16 <- set.vertex.attribute(strain_rate_vil_graph_scram_16, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_16, "name")))
strain_rate_vil_graph_scram_17 <- set.vertex.attribute(strain_rate_vil_graph_scram_17, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_17, "name")))
strain_rate_vil_graph_scram_18 <- set.vertex.attribute(strain_rate_vil_graph_scram_18, "name",
value = sample(vertex_attr(strain_rate_vil_graph_scram_18, "name")))
#Get scrambled microbiome membership
strain_rate_cluster_1_scram <- cluster_louvain(strain_rate_vil_graph_scram_1, weights = E(strain_rate_vil_graph_scram_1)$weight)
strain_rate_cluster_2_scram <- cluster_louvain(strain_rate_vil_graph_scram_2, weights = E(strain_rate_vil_graph_scram_2)$weight)
strain_rate_cluster_3_scram <- cluster_louvain(strain_rate_vil_graph_scram_3, weights = E(strain_rate_vil_graph_scram_3)$weight)
strain_rate_cluster_4_scram <- cluster_louvain(strain_rate_vil_graph_scram_4, weights = E(strain_rate_vil_graph_scram_4)$weight)
strain_rate_cluster_5_scram <- cluster_louvain(strain_rate_vil_graph_scram_5, weights = E(strain_rate_vil_graph_scram_5)$weight)
strain_rate_cluster_6_scram <- cluster_louvain(strain_rate_vil_graph_scram_6, weights = E(strain_rate_vil_graph_scram_6)$weight)
strain_rate_cluster_7_scram <- cluster_louvain(strain_rate_vil_graph_scram_7, weights = E(strain_rate_vil_graph_scram_7)$weight)
strain_rate_cluster_8_scram <- cluster_louvain(strain_rate_vil_graph_scram_8, weights = E(strain_rate_vil_graph_scram_8)$weight)
strain_rate_cluster_9_scram <- cluster_louvain(strain_rate_vil_graph_scram_9, weights = E(strain_rate_vil_graph_scram_9)$weight)
strain_rate_cluster_10_scram <- cluster_louvain(strain_rate_vil_graph_scram_10, weights = E(strain_rate_vil_graph_scram_10)$weight)
strain_rate_cluster_11_scram <- cluster_louvain(strain_rate_vil_graph_scram_11, weights = E(strain_rate_vil_graph_scram_11)$weight)
strain_rate_cluster_12_scram <- cluster_louvain(strain_rate_vil_graph_scram_12, weights = E(strain_rate_vil_graph_scram_12)$weight)
strain_rate_cluster_13_scram <- cluster_louvain(strain_rate_vil_graph_scram_13, weights = E(strain_rate_vil_graph_scram_13)$weight)
strain_rate_cluster_14_scram <- cluster_louvain(strain_rate_vil_graph_scram_14, weights = E(strain_rate_vil_graph_scram_14)$weight)
strain_rate_cluster_15_scram <- cluster_louvain(strain_rate_vil_graph_scram_15, weights = E(strain_rate_vil_graph_scram_15)$weight)
strain_rate_cluster_16_scram <- cluster_louvain(strain_rate_vil_graph_scram_16, weights = E(strain_rate_vil_graph_scram_16)$weight)
strain_rate_cluster_17_scram <- cluster_louvain(strain_rate_vil_graph_scram_17, weights = E(strain_rate_vil_graph_scram_17)$weight)
strain_rate_cluster_18_scram <- cluster_louvain(strain_rate_vil_graph_scram_18, weights = E(strain_rate_vil_graph_scram_18)$weight)
#Order scrambled microbiome membership and make separate by villages
mbiome_member_1_scram <- membership(strain_rate_cluster_1_scram)[match(names(membership(sn_cluster_1)),
names(membership(strain_rate_cluster_1_scram)))] + 100
mbiome_member_2_scram <- membership(strain_rate_cluster_2_scram)[match(names(membership(sn_cluster_2)),
names(membership(strain_rate_cluster_2_scram)))] + 200
mbiome_member_3_scram <- membership(strain_rate_cluster_3_scram)[match(names(membership(sn_cluster_3)),
names(membership(strain_rate_cluster_3_scram)))] + 300
mbiome_member_4_scram <- membership(strain_rate_cluster_4_scram)[match(names(membership(sn_cluster_4)),
names(membership(strain_rate_cluster_4_scram)))] + 400
mbiome_member_5_scram <- membership(strain_rate_cluster_5_scram)[match(names(membership(sn_cluster_5)),
names(membership(strain_rate_cluster_5_scram)))] + 500
mbiome_member_6_scram <- membership(strain_rate_cluster_6_scram)[match(names(membership(sn_cluster_6)),
names(membership(strain_rate_cluster_6_scram)))] + 600
mbiome_member_7_scram <- membership(strain_rate_cluster_7_scram)[match(names(membership(sn_cluster_7)),
names(membership(strain_rate_cluster_7_scram)))] + 700
mbiome_member_8_scram <- membership(strain_rate_cluster_8_scram)[match(names(membership(sn_cluster_8)),
names(membership(strain_rate_cluster_8_scram)))] + 800
mbiome_member_9_scram <- membership(strain_rate_cluster_9_scram)[match(names(membership(sn_cluster_9)),
names(membership(strain_rate_cluster_9_scram)))] + 900
mbiome_member_10_scram <- membership(strain_rate_cluster_10_scram)[match(names(membership(sn_cluster_10)),
names(membership(strain_rate_cluster_10_scram)))] + 1000
mbiome_member_11_scram <- membership(strain_rate_cluster_11_scram)[match(names(membership(sn_cluster_11)),
names(membership(strain_rate_cluster_11_scram)))] + 1100
mbiome_member_12_scram <- membership(strain_rate_cluster_12_scram)[match(names(membership(sn_cluster_12)),
names(membership(strain_rate_cluster_12_scram)))] + 1200
mbiome_member_13_scram <- membership(strain_rate_cluster_13_scram)[match(names(membership(sn_cluster_13)),
names(membership(strain_rate_cluster_13_scram)))] + 1300
mbiome_member_14_scram <- membership(strain_rate_cluster_14_scram)[match(names(membership(sn_cluster_14)),
names(membership(strain_rate_cluster_14_scram)))] + 1400
mbiome_member_15_scram <- membership(strain_rate_cluster_15_scram)[match(names(membership(sn_cluster_15)),
names(membership(strain_rate_cluster_15_scram)))] + 1500
mbiome_member_16_scram <- membership(strain_rate_cluster_16_scram)[match(names(membership(sn_cluster_16)),
names(membership(strain_rate_cluster_16_scram)))] + 1600
mbiome_member_17_scram <- membership(strain_rate_cluster_17_scram)[match(names(membership(sn_cluster_17)),
names(membership(strain_rate_cluster_17_scram)))] + 1700
mbiome_member_18_scram <- membership(strain_rate_cluster_18_scram)[match(names(membership(sn_cluster_18)),
names(membership(strain_rate_cluster_18_scram)))] + 1800
#combine membership
all_mbiome_membership_scram <-c(mbiome_member_1_scram,
mbiome_member_2_scram,
mbiome_member_3_scram,
mbiome_member_4_scram,
mbiome_member_5_scram,
mbiome_member_6_scram,
mbiome_member_7_scram,
mbiome_member_8_scram,
mbiome_member_9_scram,
mbiome_member_10_scram,
mbiome_member_11_scram,
mbiome_member_12_scram,
mbiome_member_13_scram,
mbiome_member_14_scram,
mbiome_member_15_scram,
mbiome_member_16_scram,
mbiome_member_17_scram,
mbiome_member_18_scram)
data.frame(rand_inds = c(mclust::adjustedRandIndex(mbiome_member_1_scram, sn_member_1),
mclust::adjustedRandIndex(mbiome_member_2_scram, sn_member_2),
mclust::adjustedRandIndex(mbiome_member_3_scram, sn_member_3),
mclust::adjustedRandIndex(mbiome_member_4_scram, sn_member_4),
mclust::adjustedRandIndex(mbiome_member_5_scram, sn_member_5),
mclust::adjustedRandIndex(mbiome_member_6_scram, sn_member_6),
mclust::adjustedRandIndex(mbiome_member_7_scram, sn_member_7),
mclust::adjustedRandIndex(mbiome_member_8_scram, sn_member_8),
mclust::adjustedRandIndex(mbiome_member_9_scram, sn_member_9),
mclust::adjustedRandIndex(mbiome_member_10_scram, sn_member_10),
mclust::adjustedRandIndex(mbiome_member_11_scram, sn_member_11),
mclust::adjustedRandIndex(mbiome_member_12_scram, sn_member_12),
mclust::adjustedRandIndex(mbiome_member_13_scram, sn_member_13),
mclust::adjustedRandIndex(mbiome_member_14_scram, sn_member_14),
mclust::adjustedRandIndex(mbiome_member_15_scram, sn_member_15),
mclust::adjustedRandIndex(mbiome_member_16_scram, sn_member_16),
mclust::adjustedRandIndex(mbiome_member_17_scram, sn_member_17),
mclust::adjustedRandIndex(mbiome_member_18_scram, sn_member_18)),
vils = village_names)
} -> null_clusters
null_clusters$vils <- village_map$village_name_deid[match(null_clusters$vils, village_map$village_code)]
parallel::stopCluster(cl)
```
```{r}
village_names_temp <- village_names
observed <- data.frame(
vils = village_map$village_name_deid[match(village_names_temp, village_map$village_code)],
obs = c(
mclust::adjustedRandIndex(mbiome_member_1, sn_member_1),
mclust::adjustedRandIndex(mbiome_member_2, sn_member_2),
mclust::adjustedRandIndex(mbiome_member_3, sn_member_3),
mclust::adjustedRandIndex(mbiome_member_4, sn_member_4),
mclust::adjustedRandIndex(mbiome_member_5, sn_member_5),
mclust::adjustedRandIndex(mbiome_member_6, sn_member_6),
mclust::adjustedRandIndex(mbiome_member_7, sn_member_7),
mclust::adjustedRandIndex(mbiome_member_8, sn_member_8),
mclust::adjustedRandIndex(mbiome_member_9, sn_member_9),
mclust::adjustedRandIndex(mbiome_member_10, sn_member_10),
mclust::adjustedRandIndex(mbiome_member_11, sn_member_11),
mclust::adjustedRandIndex(mbiome_member_12, sn_member_12),
mclust::adjustedRandIndex(mbiome_member_13, sn_member_13),
mclust::adjustedRandIndex(mbiome_member_14, sn_member_14),
mclust::adjustedRandIndex(mbiome_member_15, sn_member_15),
mclust::adjustedRandIndex(mbiome_member_16, sn_member_16),
mclust::adjustedRandIndex(mbiome_member_17, sn_member_17),
mclust::adjustedRandIndex(mbiome_member_18, sn_member_18)
)
)
p_vals <- data.frame(
vils = observed$vils,
p_val = c(
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[1]] >= observed$obs[1]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[2]] >= observed$obs[2]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[3]] >= observed$obs[3]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[4]] >= observed$obs[4]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[5]] >= observed$obs[5]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[6]] >= observed$obs[6]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[7]] >= observed$obs[7]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[8]] >= observed$obs[8]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[9]] >= observed$obs[9]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[10]] >= observed$obs[10]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[11]] >= observed$obs[11]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[12]] >= observed$obs[12]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[13]] >= observed$obs[13]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[14]] >= observed$obs[14]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[15]] >= observed$obs[15]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[16]] >= observed$obs[16]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[17]] >= observed$obs[17]) /
10000,
sum(null_clusters$rand_inds[null_clusters$vils ==
observed$vils[18]] >= observed$obs[18]) /
10000
)
)
p_vals$p_val = paste0('p = ', p_vals$p_val)
```
```{r}
cluster_pvals_fig <-
ggplot(null_clusters, aes(x=rand_inds ))+
geom_histogram(color="black",fill="grey", bins = 100, show.legend = FALSE)+
#facet_grid(as.factor(vils) ~ .) +
facet_wrap(~ vils, ncol=2) +
geom_vline(data=observed,
aes(xintercept=obs, color="red"),
linetype="solid", show.legend = FALSE) +
theme_pubr() +
ylab("Frequency") +
xlab("Adjusted Rand Index") +
ggtitle("Permutation Null Distributions")+
#scale_x_break(breaks = c(.3,.4), ticklabels=c(-.1,0,.05,.1,.15,.2,.25,.3,.4,.45)) +
geom_text(data = p_vals, aes(label = p_val,
y = 2000,
x = .52)) +
theme_pubr()+
theme(
axis.text.x.top = element_blank(),
axis.ticks.x.top = element_blank(),
strip.background = element_blank(),
strip.text.x = element_text(size = 10),
plot.title = element_text(hjust = .5, face = "bold"),
axis.title = element_text(face = "bold"),
axis.text = element_text(face = "bold")
) +
coord_cartesian(ylim = c(0,4000)) +
scale_y_continuous(limits = c(0, 4000), breaks = c(0, 2000, 4000))
#Plan el peroicso and mirasol and sesimel
```
Combine with clustering coef bucket fig from centralitysharing
```{r}
ggarrange(cluster_pvals_fig, cluster_bucket_fig, nrow = 1, labels = c("", "h"))
```
Village Plots
```{r}
add.alpha <- function(col, alpha = 1) {
if (missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb) / 255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha = alpha))
}
pdf('test.pdf')
par(mar=c(0,0,0,0))
for(i in 1:length(village_names)){
set.seed(1)
SN_Village <- SN %>% filter(village_code_w3 == village_names[i])
#Filter to relationships of interest
#Get ids for that village
village_ids <- unique(c(SN_Village$ego, SN_Village$alter))
strain_vil <- strain_rate[rownames(strain_rate) %in% village_ids ,
colnames(strain_rate) %in% village_ids]
#Create social network and microbiome networks
sn_vil <- igraph::simplify(graph_from_data_frame(SN_Village, directed = FALSE))
mbiome_all <- simplify(graph_from_adjacency_matrix(strain_vil,
mode = "undirected", weighted = TRUE))
#Cluster
#sum(is.na(E(mbiome_all)$weight))
mbiome_all <- mbiome_all - E(mbiome_all)[is.na(E(mbiome_all)$weight)]
mbiome_cluster <- cluster_louvain(mbiome_all, weights = E(mbiome_all)$weight)
sn_cluster <- cluster_louvain(sn_vil)
max_cluster <- max(max(membership(mbiome_cluster)), max(membership(sn_cluster)))
colors <- pals::cols25(max_cluster)
colors <- add.alpha(colors, alpha = .5)
mbiome_cols <- colors[membership(mbiome_cluster)]
par(mfrow=c(1,3))
plot(mbiome_cluster,
mbiome_all,
col = mbiome_cols,
#mark.groups = mbiome_cols,
mark.col = colors,
mark.border=colors,
vertex.label = NA,
vertex.size =5,
edge.width=E(mbiome_all)$weight/50,
edge.color = "black",
layout = layout_with_fr,
main = paste(village_names[i], "Strain Clusters")
)
V(sn_vil)$color <- colors[membership(sn_cluster)]
set.seed(1)
sn_colors <- colors[membership(sn_cluster)]
plot(sn_vil,
vertex.label = NA,
vertex.size =5,
mark.col = colors,
mark.border=colors,
col = sn_colors,
edge.color = "black",
layout = layout_with_fr,
main = paste(village_names[i], "Social Network Clusters")
)
V(sn_vil)$color <- membership(mbiome_cluster)[match(V(sn_vil)$name, names(membership(mbiome_cluster)))]
V(sn_vil)$color <- colors[as.numeric(V(sn_vil)$color)]
set.seed(1)
plot(sn_vil,
vertex.label = NA,
vertex.size =5,
edge.color = "black",
#vertex.color = new_cols,
layout = layout_with_fr,
main = paste(village_names[i], "Microbiome Clusters Overlay")
)
}
dev.off()
```
Illustrative Visualizations
```{r}
add.alpha <- function(col, alpha = 1) {
if (missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb) / 255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha = alpha))
}
SN_Village <- SN %>% filter(village_code_w3 == 116)
#Filter to relationships of interest
#Get ids for that village
village_ids <- unique(c(SN_Village$ego, SN_Village$alter))
strain_vil <-
strain_rate[rownames(strain_rate) %in% village_ids ,
colnames(strain_rate) %in% village_ids]
#Create social network and microbiome networks
sn_vil <-
igraph::simplify(graph_from_data_frame(SN_Village, directed = FALSE))
mbiome_all <-
simplify(graph_from_adjacency_matrix(strain_vil, mode = "undirected", weighted = TRUE))
#Cluster
set.seed(450)
set.seed(769)
mbiome_cluster <- cluster_louvain(mbiome_all, weights = E(mbiome_all)$weight )
sn_cluster <- cluster_louvain(sn_vil, resolution = 1)
max_cluster <-
max(max(membership(mbiome_cluster)), max(membership(sn_cluster)))
colors <- pals::cols25(max_cluster)
temp <- membership(mbiome_cluster)
swap <- c(temp)
swap <- case_when(swap == 4 ~ 5,
swap == 3 ~ 2,
swap == 2 ~ 4,
swap == 1 ~ 3,
TRUE ~ swap)
names(swap) <- names(temp)
V(sn_vil)$color <- colors[membership(sn_cluster)]
sn_colors <- colors[membership(sn_cluster)]
set.seed(1)
plot(
#sn_cluster,
sn_vil,
vertex.label = NA,
vertex.size = 5,
mark.col = colors,
mark.border = colors,
col = sn_colors,
edge.color = "black",
layout = layout_with_fr,
main = "Social Network Clusters for illustrative_village"
)
set.seed(1)
illustrative_village_sn <- as.ggplot(expression(plot(#sn_cluster,
sn_vil,
vertex.label = NA,
vertex.size = 5,
mark.col = colors,
mark.border = colors,
col = sn_colors,
edge.color = "black",
layout = layout_with_fr,
#main = "Social Network Clusters"
),
title("Social Network Clusters",line = 0)))
V(sn_vil)$color <- swap[match(V(sn_vil)$name, names(swap))]
V(sn_vil)$color <- colors[as.numeric(V(sn_vil)$color)]
set.seed(1)
plot(
sn_vil,
vertex.label = NA,
vertex.size = 5,
edge.color = "black",
layout = layout_with_fr,
main = "Microbiome Similarity Clusters\non Social Network"
)
set.seed(1)
illustrative_village_overlay <- as.ggplot(expression(plot(
sn_vil,
vertex.label = NA,
vertex.size = 5,
edge.color = "black",
layout = layout_with_fr#,
#main = "Microbiome Similarity Clusters\non Social Network"
),
title("Microbiome Similarity Clusters\non Social Network", line = -.75)))
colors <- add.alpha(colors, alpha = .5)
mbiome_cols <- colors[swap]
mbiome_cols_rect <- colors
mbiome_cols_rect <- mbiome_cols_rect[c(3,4,2,5)]
V(mbiome_all)$color <- mbiome_cols
set.seed(15)
plot(
mbiome_cluster,
mbiome_all,
col = mbiome_cols,
mark.col = mbiome_cols_rect,
mark.border = mbiome_cols_rect,
vertex.label = NA,
vertex.size = 5,
edge.width = E(mbiome_all)$weight / 25,
edge.color = "black",
layout = layout_with_fr,
main = "Microbiome Similarity Clusters"
)
set.seed(15)
illustrative_village_mbiome <- as.ggplot(expression(plot(
mbiome_cluster,
mbiome_all,
col = mbiome_cols,
mark.col = mbiome_cols_rect,
mark.border = mbiome_cols_rect,
vertex.label = NA,
vertex.size = 5,
edge.width = E(mbiome_all)$weight / 25,
edge.color = "black",
layout = layout_with_fr#,
#main = "Microbiome Similarity Clusters"
),
title("Microbiome Similarity Clusters",line = 0)))
```
create_figure
```{r}
library(ggpubr)
ggarrange(cluster_pvals_fig, cluster_bucket_fig, nrow = 1, labels = c("g", "h"))
fig5_1 <- ggarrange(cluster_bucket_fig,
illustrative_village_mbiome,
labels = c("A", "B"),
nrow = 1)
fig5_2 <- ggarrange(illustrative_village_sn,
illustrative_village_overlay,
labels = c("C", "D"),
nrow = 1)
#Need to get this from the species_niches script
fig5_3<- ggarrange(species_da_1, species_da_2,
labels = c("F", "G"), nrow = 1)
fig5_4 <- ggarrange(fig5_1,
fig5_2,
fig5_3,
nrow = 3)
fig5 <- ggarrange(fig5_4,
ggarrange(cluster_pvals_fig, labels = "E"),
widths = c(2,1),
nrow = 1)
svglite("../FiguresNew/Figure5/fig5_full_new_2.svg",
width = 12,
height = 12)
fig5
dev.off()
```