forked from noelzach/FungicidePulseDisturbance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworks.Rmd
2725 lines (2321 loc) · 153 KB
/
Networks.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
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
---
title: "Networks"
author: "Zachary Noel"
date: "6/3/2020"
output: html_document
---
```{r SOURCE}
source('functions_themes.R')
```
```{r setup, include=FALSE}
#source('http://bioconductor.org/biocLite.R')
#biocLite('phyloseq')
library(phyloseq)
library(decontam)
#browseVignettes("phyloseq")
library(devtools)
install_github("zdk123/SpiecEasi")
library(SpiecEasi)
packages <- c("biomformat", "qiimer", "MASS", "ape", "ggplot2", "plyr", "indicspecies", "labdsv", "dplyr", "reshape", "vegan", "metacoder", "lme4", "lsmeans", "ggpmisc", "ggpubr", "tidyverse", "doParallel", "DT", "exactRankTests", "foreach", "Rcpp", "shiny", "coin", "igraph", "SpiecEasi", "ggsci", "RCy3", "ggrepel", "cowplot", "ggalt", "ggfortify", "emmeans", "data.table", "randomForest", "rfUtilities", "caret", "tidyr")
ipak(packages)
packageVersion("vegan")
packageVersion('phyloseq')
#install.packages("remotes")
#remotes::install_github("DanielSprockett/reltools")
library(reltools)
#install.packages("minpack.lm")
library(minpack.lm)
#Models for the whole community
#install.packages("devtools")
library(devtools)
#install_github("DanielSprockett/tyRa")
#install.packages("Hmisc")
library(Hmisc)
library(Biostrings)
library(ggforce)
library(cowplot)
library(randomForest)
library(rfUtilities) # to test model significance
library(caret) # to get leave-one-out cross-validation accuracies and also contains the nearZeroVar function
library(NetworkExtinction)
library(ggraph)
library(tidygraph)
```
## FUNGI CSS NORM and Non-normalized RDS
Save the fungi phyloseq object as an RDS file to load faster in future.
```{r FUNGI CSS NORM - RDS}
# Restore the object
fungi.no.norm <- readRDS(file = "Fungicide_Fungi_RootsAndLeaves_1000readsmin_nonorm.rds") # need this for rarefying and core membership
fungi.css.norm <- readRDS(file = "Fungicide_Fungi_RootsAndLeaves_10000seqMin_CSSNorm.rds") # need this for plotting abundance-occupancy
fungi.CSS.norm.filt <- readRDS(file = "physeq_fungi_CSS_filt_tax.rds")
head(fungi.CSS.norm.filt@tax_table)
fungi.no.norm.filt <- readRDS(file = "physeq_fungi_nonorm_filt_tax.rds")
head(fungi.no.norm.filt@tax_table)
```
## PROKARYOTE CSS NORM and Non-normalized RDS
```{r}
# Restore the object
bacteria.no.norm <- readRDS(file = "Fungicide_Bacteria_RootsAndLeaves_10000readsmin_nonorm.rds")
# Restore the object
bacteria.css.norm <- readRDS(file = "Fungicide_Bacteria_RootsAndLeaves_10000seqMin_CSSNorm.rds")
# Restore the object
bacteria.no.norm.filt <- readRDS("physeq_prok_nonorm_filt_tax.rds")
bacteria.css.norm <- readRDS(file = "physeq_prok_CSS_filt_tax.rds")
```
# NETWORKS
- The input data for these networks is the fungi.no.norm or the prok.no.norm
- These are datasets with at least 1000 reads, most soybean leaf samples passed this filter
- samples that did not sequence well enough to be inlcuded and need to be dropped for this analysis =
T2R6FBR3L in fungal database (R3)
T2R1FCR6L in prokaryote database (R6)
- Spiec Easi does its own normalization procedure so the input is raw counts
- However we will have to take out rare taxa with large variation
- I am going to make a large network and pull out only the OTUs present in samples I want to analyse
- This meta-network aproach was done in Yang et al. 2019
## META-NETWORK
### Networks based on splitting each growth stage fungicide combo, then subgraphs by management
## One big meta-network Soybean
-has to be done on the HPCC - I am doing the same filtering as the ANCOM analysis, (i.e., mean RA > 1-5 and with OTUs that had less than 5% occupancy)
```{r}
remotes::install_github("schuyler-smith/phyloschuyler")
library(phylosmith)
fungi.no.norm <- phylosmith::set_sample_order(fungi.no.norm.filt, "SampleID")
bacteria.no.norm <- phylosmith::set_sample_order(bacteria.no.norm.filt, "SampleID")
##### fungi Subset OTUs with mean RA less than 1-5 soybean ######
fungi.names.rare <- fungi.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy" & SampleID != "T2R6FBR3L") %>%
phyloseq::transform_sample_counts(function(x) {x/sum(x)} ) %>%
phyloseq::filter_taxa(function(x) mean(x) > 1e-4, TRUE) %>%
taxa_names()
# Take out the sample that is not present in the bacterial library
fungi_leaf_soy <- fungi.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy" & SampleID != "T2R6FBR3L")
# Subset Samples
fungi_leaf_soy <- prune_taxa(fungi.names.rare, fungi_leaf_soy) # remove the taxa below 10-5
saveRDS(fungi_leaf_soy, "NetworkRDS/Input/fungi_leaf_soy.RDS")
##### bacteria Subset OTUs with mean RA less than 1-5 soybean ######
prok.names.rare <- bacteria.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy" & SampleID != "T2R1FCR6L") %>%
phyloseq::transform_sample_counts(function(x) {x/sum(x)} ) %>%
phyloseq::filter_taxa(function(x) mean(x) > 1e-4, TRUE) %>%
taxa_names()
prok_leaf_soy <- bacteria.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy" & SampleID != "T2R1FCR6L")
# Subset Samples
prok_leaf_soy <- prune_taxa(prok.names.rare, prok_leaf_soy) # remove the taxa below 10-5
saveRDS(prok_leaf_soy, "NetworkRDS/Input/prok_leaf_soy.RDS")
##### fungi Subset OTUs with mean RA less than 1-5 corn ######
fungi.samples.take.out.corn <- c("Corn2017LeafObjective2Collection1T1R1CBA3", "Corn2017LeafObjective2Collection1T2R1FBE6",
"Corn2017LeafObjective2Collection1T2R2FAA7", "Corn2017LeafObjective2Collection2T2R6FCE2",
"Corn2017LeafObjective2Collection3T1R5FCB5")
fungi.names.rare <- fungi.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Corn" & SampleID %!in% fungi.samples.take.out.corn) %>%
phyloseq::transform_sample_counts(function(x) {x/sum(x)} ) %>%
phyloseq::filter_taxa(function(x) mean(x) > 1e-4, TRUE) %>%
taxa_names()
# Take out the sample that is not present in the bacterial library
fungi_leaf_corn <- fungi.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Corn" & SampleID %!in% fungi.samples.take.out.corn)
# Subset Samples
fungi_leaf_corn <- prune_taxa(fungi.names.rare, fungi_leaf_corn) # remove the taxa below 10-5
##### bacteria Subset OTUs with mean RA less than 1-5 corn ######
prok.names.rare <- bacteria.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Corn") %>%
phyloseq::transform_sample_counts(function(x) {x/sum(x)} ) %>%
phyloseq::filter_taxa(function(x) mean(x) > 1e-4, TRUE) %>%
taxa_names()
prok_leaf_corn <- bacteria.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Corn")
# Subset Samples
prok_leaf_corn <- prune_taxa(prok.names.rare, prok_leaf_corn) # remove the taxa below 10-5
all.equal(sample_names(fungi_leaf_corn), sample_names(prok_leaf_corn))
saveRDS(fungi_leaf_corn, "NetworkRDS/Input/fungi_leaf_corn.RDS")
saveRDS(prok_leaf_corn, "NetworkRDS/Input/prok_leaf_corn.RDS")
```
- This is the code that was run on the high-performance computer. I would not recommend you run this code locally...
```{r}
spiec.easi.network <- function(prok.phyloseq, fungi.phyloseq){
set.seed(12324)
spiec_output <- spiec.easi(list(prok.phyloseq, fungi.phyloseq), method='mb', lambda.min.ratio=1e-2, nlambda=100, pulsar.params=list(thresh = 0.05, seed = 10020))
stab <- getStability(spiec_output) # 0.0479 - good
soy_net_weights <- symBeta(getOptBeta(spiec_output), mode="maxabs")
weights <- Matrix::summary(t(soy_net_weights))[,3]
names_taxa_net <- c(taxa_names(prok.phyloseq), taxa_names(fungi.phyloseq))
nat.con <- pulsar::natural.connectivity(getRefit(spiec_output))
graph.disimilarity <- as.matrix(pulsar::graph.diss(getRefit(spiec_output)))
spiec_output_graph <- adj2igraph(getRefit(spiec_output),
edge.attr = list(weight=weights),
vertex.attr = list(name=names_taxa_net))
degree.distribution <- data.frame(degree(spiec_output_graph)); colnames(degree.distribution) <- "degree"
#degree.distribution$GrowthStage <- as.character(GrowthStage)
#degree.distribution$Fungicide <- as.character(Fungicide)
return.list <- list(igraph.object = spiec_output_graph, stability = stab, degree_dist = degree.distribution, natural.connect = nat.con, graph.disim = graph.disimilarity)
return(return.list)
}
#meta.net <- spiec.easi.network(prok_leaf_soy, fungi_leaf_soy)
#meta.net$stability
```
## Load in the Meta-Network objects
- Load in the meta.net object from the hpcc
```{r}
meta.net.soy <- readRDS("NetworkRDS/Output/soy_leaf_network_112920_1-4filter.rds")
meta.net.soy$igraph.object
meta.net.corn <- readRDS("NetworkRDS/Output/corn_leaf_network_0112920_1-4filter.rds")
meta.net.corn$igraph.object
```
## Loading in Metadata associated with each OTU such as, taxonomy, differential abundance, core membership, relative abundance etc.
FULL Taxonomy lists
```{r}
fungi.taxonomy <- tax_table(fungi.no.norm) %>%
as("matrix") %>%
tibble::as_tibble() %>%
dplyr::select(OTU_ID, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy)
bacteria.taxonomy <- tax_table(bacteria.no.norm) %>%
as("matrix") %>%
tibble::as_tibble() %>%
dplyr::select(OTU_ID, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy)
taxonomy.combined <- rbind.data.frame(fungi.taxonomy, bacteria.taxonomy)
```
-Core membership across time and space
Missing corn bacteria leaves and corn T1 core fungi
```{r}
# Fungi Soy Leaves
soy_T1_core_fungi_otus <- readRDS("soy_T1_core_fungal_otus.rds")
soy_T2_core_fungi_otus <- readRDS("soy_T2_core_fungal_otus.rds")
# Fungi Corn Leaves
corn_T1_core_fungi_otus <- readRDS("corn_T1_core_fungal_otus.rds")
corn_T2_core_fungi_otus <- readRDS("corn_T2_core_fungal_otus.rds")
# Bacteria Soy Leaves
soy_T1_core_bacterial_otus <- readRDS("soy_T1_core_bacterial_otus.rds")
soy_T2_core_bacterial_otus <- readRDS("soy_T2_core_bacterial_otus.rds")
# Bacteria Corn Leaves
core.T1.combined <- c(soy_T1_core_fungi_otus, soy_T1_core_bacterial_otus)
core_labels_T1 <- taxonomy.combined$BestMatch[taxonomy.combined$OTU_ID %in% core.T1.combined]
core.T2.combined <- c(soy_T2_core_fungi_otus, soy_T2_core_bacterial_otus)
core_labels_T2 <- taxonomy.combined$BestMatch[taxonomy.combined$OTU_ID %in% core.T2.combined]
taxonomy.combined %>%
subset(Label %in% core_labels_T2) %>%
write.csv("soy_T2_core_otus.csv")
taxonomy.combined %>%
subset(Label %in% core_labels_T1) %>%
write.csv("soy_T1_core_otus.csv")
```
-Relative abundance of each OTU
```{r}
#### getting the mean relative abundance of each fungal OTU ####
fungi_obj2.ra <- fungi.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy") %>%
transform_sample_counts(function(x) {x/sum(x)} ) %>% # Transform to rel. abundance
psmelt.fast() # Melt to long format
mean.rel.abund.fungi <- fungi_obj2.ra %>%
group_by(OTU) %>%
nest() %>%
mutate(Kingdom = purrr::map(data, ~.x$Kingdom[[1]])) %>%
mutate(Phylum = purrr::map(data, ~.x$Phylum[[1]])) %>%
mutate(Class = purrr::map(data, ~.x$Class[[1]])) %>%
mutate(Order = purrr::map(data, ~.x$Order[[1]])) %>%
mutate(Family = purrr::map(data, ~.x$Family[[1]])) %>%
mutate(Genus = purrr::map(data, ~.x$Genus[[1]])) %>%
mutate(BestMatch = purrr::map(data, ~.x$BestMatch[[1]])) %>%
mutate(Taxonomy = purrr::map(data, ~.x$Taxonomy[[1]])) %>%
mutate(mean.relabund = purrr::map(data,~mean(.$Abundance*100))) %>%
mutate(SE.relabund = purrr::map(data,~sd(.$Abundance*100)/sqrt(length(.$Abundance*100)))) %>%
unnest(mean.relabund, SE.relabund, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy) %>%
dplyr::select(OTU, mean.relabund, SE.relabund, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy)
#### getting the mean relative abundance of each bacterial OTU ####
prok_obj2.ra <- bacteria.no.norm %>%
subset_samples(Compartment == "Leaf" & Crop == "Soy") %>%
transform_sample_counts(function(x) {x/sum(x)} ) %>% # Transform to rel. abundance
psmelt.fast() # Melt to long format
mean.rel.abund.prok <- prok_obj2.ra %>%
group_by(OTU) %>%
nest() %>%
mutate(Kingdom = purrr::map(data, ~.x$Kingdom[[1]])) %>%
mutate(Phylum = purrr::map(data, ~.x$Phylum[[1]])) %>%
mutate(Class = purrr::map(data, ~.x$Class[[1]])) %>%
mutate(Order = purrr::map(data, ~.x$Order[[1]])) %>%
mutate(Family = purrr::map(data, ~.x$Family[[1]])) %>%
mutate(Genus = purrr::map(data, ~.x$Genus[[1]])) %>%
mutate(BestMatch = purrr::map(data, ~.x$BestMatch[[1]])) %>%
mutate(Taxonomy = purrr::map(data, ~.x$Taxonomy[[1]])) %>%
mutate(mean.relabund = purrr::map(data,~mean(.$Abundance*100))) %>%
mutate(SE.relabund = purrr::map(data,~sd(.$Abundance*100)/sqrt(length(.$Abundance*100)))) %>%
unnest(mean.relabund, SE.relabund, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy) %>%
dplyr::select(OTU, mean.relabund, SE.relabund, Kingdom, Phylum, Class, Order, Family, Genus, BestMatch, Taxonomy)
#### Join the abundances together ####
mean.rel.abund.combined <- rbind.data.frame(mean.rel.abund.prok, mean.rel.abund.fungi)
```
ANCOM - differentially abundant OTUs
-There was no evidence that fungicides changed the composition of the corn phyllosphere dependent on management, indicating that fungicides affected the abundance of similar OTUs across both treatments
```{r}
# Should change to do both T1 and T2 for corn? does that make sense?
#### CORN V8 ####
fungi_leaf_corn_V8_ANCOM <- readRDS("ANCOM/HPCC_output/fungi_leaf_corn_V8_ANCOM.rds")
colnames(fungi_leaf_corn_V8_ANCOM) <- c("OTU", "W", "detected_0.9", "detected_0.8", "detected_0.7", "detected_0.6")
fungi_leaf_corn_V8_ANCOM$GrowthStage <- "V8"
fungi_leaf_corn_V8_ANCOM$Crop <- "Corn"
fungi_leaf_corn_V8_ANCOM$Treatment <- "T1 & T2"
fungi_leaf_corn_V8_ANCOM$StructuralZero <- ifelse(fungi_leaf_corn_V8_ANCOM$W == Inf, TRUE, FALSE)
fungi_leaf_corn_V8_ANCOM$OTU[fungi_leaf_corn_V8_ANCOM$detected_0.7 == TRUE & fungi_leaf_corn_V8_ANCOM$StructuralZero == FALSE]
fungi_leaf_corn_V8_ANCOM_sig <- fungi_leaf_corn_V8_ANCOM$OTU[fungi_leaf_corn_V8_ANCOM$detected_0.7 == TRUE]
#### SOYBEAN BOTH GROWTH STAGES ####
fungi_leaf_soy_R4_T1_ANCOM <- readRDS("ANCOM/HPCC_output/fungi_leaf_soy_R4_T1_ANCOM.rds")
colnames(fungi_leaf_soy_R4_T1_ANCOM) <- c("OTU", "W", "detected_0.9", "detected_0.8", "detected_0.7", "detected_0.6")
fungi_leaf_soy_R4_T1_ANCOM$GrowthStage <- "R4"
fungi_leaf_soy_R4_T1_ANCOM$Crop <- "Soy"
fungi_leaf_soy_R4_T1_ANCOM$Treatment <- "T1"
fungi_leaf_soy_R4_T1_ANCOM$StructuralZero <- ifelse(fungi_leaf_soy_R4_T1_ANCOM$W == Inf, TRUE, FALSE)
fungi_leaf_soy_R4_T1_ANCOM$OTU[fungi_leaf_soy_R4_T1_ANCOM$detected_0.7 == TRUE & fungi_leaf_soy_R4_T1_ANCOM$StructuralZero == FALSE]
fungi_leaf_soy_R4_T1_sig <- fungi_leaf_soy_R4_T1_ANCOM$OTU[fungi_leaf_soy_R4_T1_ANCOM$detected_0.7 == TRUE]
fungi_leaf_soy_R4_T2_ANCOM <- readRDS("ANCOM/HPCC_output/fungi_leaf_soy_R4_T2_ANCOM.rds")
colnames(fungi_leaf_soy_R4_T2_ANCOM) <- c("OTU", "W", "detected_0.9", "detected_0.8", "detected_0.7", "detected_0.6")
fungi_leaf_soy_R4_T2_ANCOM$GrowthStage <- "R4"
fungi_leaf_soy_R4_T2_ANCOM$Crop <- "Soy"
fungi_leaf_soy_R4_T2_ANCOM$Treatment <- "T2"
fungi_leaf_soy_R4_T2_ANCOM$StructuralZero <- ifelse(fungi_leaf_soy_R4_T2_ANCOM$W == Inf, TRUE, FALSE)
fungi_leaf_soy_R4_T2_ANCOM$OTU[fungi_leaf_soy_R4_T2_ANCOM$detected_0.7 == TRUE & fungi_leaf_soy_R4_T2_ANCOM$StructuralZero == FALSE]
fungi_leaf_soy_R4_T2_sig <- fungi_leaf_soy_R4_T2_ANCOM$OTU[fungi_leaf_soy_R4_T2_ANCOM$detected_0.7 == TRUE]
fungi_leaf_soy_R6_T1_ANCOM <- readRDS("ANCOM/HPCC_output/fungi_leaf_soy_R6_T1_ANCOM.rds")
colnames(fungi_leaf_soy_R6_T1_ANCOM) <- c("OTU", "W", "detected_0.9", "detected_0.8", "detected_0.7", "detected_0.6")
fungi_leaf_soy_R6_T1_ANCOM$GrowthStage <- "R6"
fungi_leaf_soy_R6_T1_ANCOM$Crop <- "Soy"
fungi_leaf_soy_R6_T1_ANCOM$Treatment <- "T1"
fungi_leaf_soy_R6_T1_ANCOM$StructuralZero <- ifelse(fungi_leaf_soy_R6_T1_ANCOM$W == Inf, TRUE, FALSE)
fungi_leaf_soy_R6_T1_ANCOM$OTU[fungi_leaf_soy_R6_T1_ANCOM$detected_0.7 == TRUE & fungi_leaf_soy_R6_T1_ANCOM$StructuralZero == FALSE]
fungi_leaf_soy_R6_T1_sig <- fungi_leaf_soy_R6_T1_ANCOM$OTU[fungi_leaf_soy_R6_T1_ANCOM$detected_0.7 == TRUE]
fungi_leaf_soy_R6_T2_ANCOM <- readRDS("ANCOM/HPCC_output/fungi_leaf_soy_R6_T2_ANCOM.rds")
colnames(fungi_leaf_soy_R6_T2_ANCOM) <- c("OTU", "W", "detected_0.9", "detected_0.8", "detected_0.7", "detected_0.6")
fungi_leaf_soy_R6_T2_ANCOM$GrowthStage <- "R6"
fungi_leaf_soy_R6_T2_ANCOM$Crop <- "Soy"
fungi_leaf_soy_R6_T2_ANCOM$Treatment <- "T2"
fungi_leaf_soy_R6_T2_ANCOM$StructuralZero <- ifelse(fungi_leaf_soy_R6_T2_ANCOM$W == Inf, TRUE, FALSE)
fungi_leaf_soy_R6_T2_ANCOM$OTU[fungi_leaf_soy_R6_T2_ANCOM$detected_0.7 == TRUE & fungi_leaf_soy_R6_T2_ANCOM$StructuralZero == FALSE]
fungi_leaf_soy_R6_T2_sig <- fungi_leaf_soy_R6_T2_ANCOM$OTU[fungi_leaf_soy_R6_T2_ANCOM$detected_0.7 == TRUE]
```
### Network Complexity
Calculate network complexity and architechure based on each subgraph containing only the OTUs present in that sample.
Corn
```{r}
##### Network Complexity Stats Corn #####
# Going to loop through all the samples and get the number of edges and nodes for each sub-graph
### INPUT ###
network.object <- meta.net.corn$igraph.object
phyloseq.object <- fungi_leaf_corn # it doesn't really matter if you pick the fungal or prokaryote as long as they have the same samples
network.complexity2 <- NULL
edge.complexity <- NULL
node.stats <- NULL
sample.levels <- levels(sample_data(phyloseq.object)$SampleID)
for (i in seq_along(sample.levels)) {
fungi.soy <- fungi.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
phyloseq::filter_taxa(function(x) sum(x) > 0, TRUE) %>%
otu_table() %>%
rownames()
prok.soy <- bacteria.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
phyloseq::filter_taxa(function(x) sum(x) > 0, TRUE) %>%
otu_table() %>%
rownames()
sample.data <- fungi.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
sample_data() %>%
as.tibble() %>%
as.data.frame()
samples <- sample.data %>%
dplyr::select(SampleID, Crop, DateSampled, GrowthStage, Treatment, Rep, Sample, Fungicide)
subset.taxa <- c(fungi.soy, prok.soy)
print(paste("N taxa present in", sample.levels[[i]]))
print(length(subset.taxa))
###### Subsetting the network
network_dc_switch <- induced_subgraph(network.object, which(V(network.object)$name %in% subset.taxa))
print(network_dc_switch)
# Delete isolated nodes
#network_dc_switch <- delete.vertices(network_dc_switch, which(degree(network_dc_switch, mode = "all")== 0))
network_dc_switch_no_weight <- delete_edge_attr(network_dc_switch, "weight")
#### Node stats ####
wtc <- cluster_walktrap(network_dc_switch_no_weight)
mod <- modularity(network_dc_switch_no_weight, membership(wtc))
connect <- pulsar::natural.connectivity(as_adj(network_dc_switch_no_weight), norm = TRUE)
print(mod)
hubs <- data.frame(hub_score(network_dc_switch_no_weight)$vector); colnames(hubs) <- c("hubscore")
hubs$OTU <- V(network_dc_switch_no_weight)$name
hubs$betweeness <- betweenness(network_dc_switch_no_weight, directed = FALSE, weights = NULL)
hubs$pr <- page_rank(network_dc_switch_no_weight)$vector
hubs$degree <- degree(network_dc_switch_no_weight)
hubs$module <- wtc$membership
hubs <- merge(hubs, samples)
nodes.stats <- hubs
node.stats2 <- left_join(nodes.stats, mean.rel.abund.combined, by = "OTU")
node.stats <- rbind.data.frame(node.stats, node.stats2)
########
###### Network Complexity ######
n.nodes <- length(V(network_dc_switch)) # N nodes
n.edges <- length(E(network_dc_switch)) # N edges
Sample <- sample.levels[[i]]
############
###### Edge Stats #######
edge.network <- data.frame(ends(network_dc_switch, E(network_dc_switch)))
colnames(edge.network) <- c("from", "to")
edge.network <- left_join(edge.network, taxonomy.combined, by = c("from" = "OTU_ID"))
colnames(edge.network) <- c("from", "to", "Kingdom_from", "Phylum_from", "Class_from", "Order_from",
"Family_from", "Genus_from", "Label_from", "Species_from")
edge.network <- left_join(edge.network, taxonomy.combined, by = c("to" = "OTU_ID"))
colnames(edge.network) <- c("from", "to", "Kingdom_from", "Phylum_from", "Class_from", "Order_from",
"Family_from", "Genus_from", "Label_from", "Species_from",
"Kingdom_to", "Phylum_to", "Class_to", "Order_to",
"Family_to", "Genus_v", "Label_to", "Species_to")
edge.network$weights <- E(network_dc_switch)$weight
edge.network$direction <- ifelse(edge.network$weights > 0, "Positive", "Negative")
edge.network$inter <- ifelse(edge.network$Kingdom_from == edge.network$Kingdom_to, "Intrakingdom", "Interkingdom")
edge.network$interXdirection <- interaction(edge.network$inter, edge.network$direction)
edge.network$Kingdom_Kingdom <- ifelse(edge.network$inter == "Intrakingdom" & edge.network$Kingdom_to == "Fungi", "Fungal-Fungal", "Prokaryote-Prokaryote")
edge.network$Tremellomycete_Alphaproteobacteria <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Alphaproteobacteria" & edge.network$direction == "Positive", "Tremellomycete-Alphaproteobacteria", "Other")
edge.network$Tremellomycete_Actinobacteria <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Actinobacteria" & edge.network$direction == "Positive", "Tremellomycete-Actinobacteria", "Other")
edge.network$Tremellomycete_Cytophagia <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Cytophagia" & edge.network$direction == "Positive", "Tremellomycete-Cytophagia", "Other")
edge.network$Bull_Prok <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Genus_from %in% c("Sphingomonas", "Methylobacterium", "Hymenobacter", "Pseudomonas", "Nocardioides") & edge.network$direction == "Positive", "Bull-Prok", "Other")
### Hannaella - Bacteria positive edges ###
edge.network$Hannaella_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Hannaella_Sphingomonas", "Other")
edge.network$Hannaella_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Hannaella_Methylobacterium", "Other")
edge.network$Hannaella_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Hannaella_Hymenobacter", "Other")
edge.network$Hannaella_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Hannaella_Pseudomonas", "Other")
edge.network$Hannaella_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Hannaella_Nocardioides", "Other")
### Dioszengia - Bacteria positive edges ###
edge.network$Dioszegia_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Dioszegia_Sphingomonas", "Other")
edge.network$Dioszegia_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Dioszegia_Methylobacterium", "Other")
edge.network$Dioszegia_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Dioszegia_Hymenobacter", "Other")
edge.network$Dioszegia_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Dioszegia_Pseudomonas", "Other")
edge.network$Dioszegia_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Dioszegia_Nocardioides", "Other")
### Vishniacozyma - Bacteria positive edges ###
edge.network$Vishniacozyma_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Vishniacozyma_Sphingomonas", "Other")
edge.network$Vishniacozyma_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Vishniacozyma_Methylobacterium", "Other")
edge.network$Vishniacozyma_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Vishniacozyma_Hymenobacter", "Other")
edge.network$Vishniacozyma_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Vishniacozyma_Pseudomonas", "Other")
edge.network$Vishniacozyma_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Vishniacozyma_Nocardioides", "Other")
### Sporobolomyces - Bacteria positive edges ###
edge.network$Sporobolomyces_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Sporobolomyces_Sphingomonas", "Other")
edge.network$Sporobolomyces_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Sporobolomyces_Methylobacterium", "Other")
edge.network$Sporobolomyces_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Sporobolomyces_Hymenobacter", "Other")
edge.network$Sporobolomyces_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Sporobolomyces_Pseudomonas", "Other")
edge.network$Sporobolomyces_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Sporobolomyces_Nocardioides", "Other")
edge.network$Sporobolomyces_Roseomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Roseomonas") & edge.network$direction == "Positive", "Sporobolomyces_Roseomonas", "Other")
edge.network$Sporobolomyces_Terrimonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Terrimonas") & edge.network$direction == "Positive", "Sporobolomyces_Terrimonas", "Other")
edge.network$Sporobolomyces_Bacillus <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Bacillus") & edge.network$direction == "Positive", "Sporobolomyces_Bacillus", "Other")
n.Tremellomycete_Alphaproteobacteria <- length(edge.network$Tremellomycete_Alphaproteobacteria[edge.network$Tremellomycete_Alphaproteobacteria != "Other"])
n.Tremellomycete_Actinobacteria <- length(edge.network$Tremellomycete_Actinobacteria[edge.network$Tremellomycete_Actinobacteria != "Other"])
n.Tremellomycete_Cytophagia <- length(edge.network$Tremellomycete_Cytophagia[edge.network$Tremellomycete_Cytophagia != "Other"])
n.Bull_Prok <- length(edge.network$Bull_Prok[edge.network$Bull_Prok != "Other"])
### n Hannaella - Bacteria positive edges ###
n.Hannaella_Sphingomonas <- length(edge.network$Hannaella_Sphingomonas[edge.network$Hannaella_Sphingomonas != "Other"])
n.Hannaella_Methylobacterium <- length(edge.network$Hannaella_Methylobacterium[edge.network$Hannaella_Methylobacterium != "Other"])
n.Hannaella_Hymenobacter <- length(edge.network$Hannaella_Hymenobacter[edge.network$Hannaella_Hymenobacter != "Other"])
n.Hannaella_Pseudomonas <- length(edge.network$Hannaella_Pseudomonas[edge.network$Hannaella_Pseudomonas != "Other"])
n.Hannaella_Nocardioides <- length(edge.network$Hannaella_Nocardioides[edge.network$Hannaella_Nocardioides != "Other"])
### n Dioszegia - Bacteria positive edges ###
n.Dioszegia_Sphingomonas <- length(edge.network$Dioszegia_Sphingomonas[edge.network$Dioszegia_Sphingomonas != "Other"])
n.Dioszegia_Methylobacterium <- length(edge.network$Dioszegia_Methylobacterium[edge.network$Dioszegia_Methylobacterium != "Other"])
n.Dioszegia_Hymenobacter <- length(edge.network$Dioszegia_Hymenobacter[edge.network$Dioszegia_Hymenobacter != "Other"])
n.Dioszegia_Pseudomonas <- length(edge.network$Dioszegia_Pseudomonas[edge.network$Dioszegia_Pseudomonas != "Other"])
n.Dioszegia_Nocardioides <- length(edge.network$Dioszegia_Nocardioides[edge.network$Dioszegia_Nocardioides != "Other"])
### n Vishniacozyma - Bacteria positive edges ###
n.Vishniacozyma_Sphingomonas <- length(edge.network$Vishniacozyma_Sphingomonas[edge.network$Vishniacozyma_Sphingomonas != "Other"])
n.Vishniacozyma_Methylobacterium <- length(edge.network$Vishniacozyma_Methylobacterium[edge.network$Vishniacozyma_Methylobacterium != "Other"])
n.Vishniacozyma_Hymenobacter <- length(edge.network$Vishniacozyma_Hymenobacter[edge.network$Vishniacozyma_Hymenobacter != "Other"])
n.Vishniacozyma_Pseudomonas <- length(edge.network$Vishniacozyma_Pseudomonas[edge.network$Vishniacozyma_Pseudomonas != "Other"])
n.Vishniacozyma_Nocardioides <- length(edge.network$Vishniacozyma_Nocardioides[edge.network$Vishniacozyma_Nocardioides != "Other"])
### n Sporobolomyces - Bacteria positive edges ###
n.Sporobolomyces_Sphingomonas <- length(edge.network$Sporobolomyces_Sphingomonas[edge.network$Sporobolomyces_Sphingomonas != "Other"])
n.Sporobolomyces_Methylobacterium <- length(edge.network$Sporobolomyces_Methylobacterium[edge.network$Sporobolomyces_Methylobacterium != "Other"])
n.Sporobolomyces_Hymenobacter <- length(edge.network$Sporobolomyces_Hymenobacter[edge.network$Sporobolomyces_Hymenobacter != "Other"])
n.Sporobolomyces_Pseudomonas <- length(edge.network$Sporobolomyces_Pseudomonas[edge.network$Sporobolomyces_Pseudomonas != "Other"])
n.Sporobolomyces_Nocardioides <- length(edge.network$Sporobolomyces_Nocardioides[edge.network$Sporobolomyces_Nocardioides != "Other"])
n.Sporobolomyces_Roseomonas <- length(edge.network$Sporobolomyces_Roseomonas[edge.network$Sporobolomyces_Roseomonas != "Other"])
n.Sporobolomyces_Terrimonas <- length(edge.network$Sporobolomyces_Terrimonas[edge.network$Sporobolomyces_Terrimonas != "Other"])
n.Sporobolomyces_Bacillus <- length(edge.network$Sporobolomyces_Bacillus[edge.network$Sporobolomyces_Bacillus != "Other"])
n.Intrakingdom.Neg <- length(edge.network$interXdirection[edge.network$interXdirection == "Intrakingdom.Negative"])
n.Intrakingdom.Pos <- length(edge.network$interXdirection[edge.network$interXdirection == "Intrakingdom.Positive"])
n.Interkingdom.Pos <- length(edge.network$interXdirection[edge.network$interXdirection == "Interkingdom.Positive"])
n.Interkingdom.Neg <- length(edge.network$interXdirection[edge.network$interXdirection == "Interkingdom.Negative"])
n.Fungal.Neg <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Fungal-Fungal" & edge.network$direction == "Negative"])
n.Fungal.Pos <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Fungal-Fungal" & edge.network$direction == "Positive"])
n.Prok.Neg <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Prokaryote-Prokaryote" & edge.network$direction == "Negative"])
n.Prok.Pos <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Prokaryote-Prokaryote" & edge.network$direction == "Positive"])
edge.network_i <- merge(edge.network, samples)
edge.complexity <- rbind.data.frame(edge.network_i, edge.complexity)
######
network_complexity <- data.frame(phyloseq.object@sam_data[phyloseq.object@sam_data$SampleID == Sample,])
network_complexity_i <- cbind.data.frame(n.nodes, n.edges, network_complexity,
n.Intrakingdom.Neg, n.Intrakingdom.Pos, n.Interkingdom.Pos, n.Interkingdom.Neg,
n.Fungal.Neg, n.Fungal.Pos, n.Prok.Neg, n.Prok.Pos, mod, connect, n.Tremellomycete_Alphaproteobacteria, n.Tremellomycete_Actinobacteria, n.Tremellomycete_Cytophagia, n.Bull_Prok, n.Hannaella_Sphingomonas, n.Hannaella_Methylobacterium, n.Hannaella_Hymenobacter, n.Hannaella_Pseudomonas, n.Hannaella_Nocardioides,
n.Vishniacozyma_Sphingomonas, n.Vishniacozyma_Methylobacterium, n.Vishniacozyma_Hymenobacter, n.Vishniacozyma_Pseudomonas, n.Vishniacozyma_Nocardioides,
n.Dioszegia_Sphingomonas, n.Dioszegia_Methylobacterium, n.Dioszegia_Hymenobacter, n.Dioszegia_Pseudomonas, n.Dioszegia_Nocardioides, n.Sporobolomyces_Sphingomonas, n.Sporobolomyces_Methylobacterium, n.Sporobolomyces_Hymenobacter, n.Sporobolomyces_Pseudomonas,
n.Sporobolomyces_Nocardioides, n.Sporobolomyces_Roseomonas, n.Sporobolomyces_Terrimonas, n.Sporobolomyces_Bacillus)
network.complexity2 <- rbind.data.frame(network.complexity2, network_complexity_i)
}
### OTUPUT ###
Network.complexity.corn <- network.complexity2
Network.edges.corn <- edge.complexity
Network.nodes.corn <- node.stats
##################################
```
Soybean
```{r}
##### Network Complexity Stats Soy #####
# Going to loop through all the samples and get the number of edges and nodes for each sub-graph
### INPUT ###
network.object <- meta.net.soy$igraph.object
phyloseq.object <- fungi_leaf_soy # it doesn't really matter if you pick the fungal or prokaryote as long as they have the same samples
network.complexity2 <- NULL
edge.complexity <- NULL
node.stats <- NULL
sample.levels <- levels(sample_data(phyloseq.object)$SampleID)
for (i in seq_along(sample.levels)) {
fungi.soy <- fungi.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
phyloseq::filter_taxa(function(x) sum(x) > 0, TRUE) %>%
otu_table() %>%
rownames()
prok.soy <- bacteria.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
phyloseq::filter_taxa(function(x) sum(x) > 0, TRUE) %>%
otu_table() %>%
rownames()
sample.data <- fungi.no.norm %>%
subset_samples(SampleID %in% sample.levels[[i]]) %>%
sample_data() %>%
as.tibble() %>%
as.data.frame()
samples <- sample.data %>%
dplyr::select(SampleID, Crop, DateSampled, GrowthStage, Treatment, Rep, Sample, Fungicide)
subset.taxa <- c(fungi.soy, prok.soy)
print(paste("N taxa present in", sample.levels[[i]]))
print(length(subset.taxa))
###### Subsetting the network
network_dc_switch <- induced_subgraph(network.object, which(V(network.object)$name %in% subset.taxa))
print(network_dc_switch)
# Delete isolated nodes
#network_dc_switch <- delete.vertices(network_dc_switch, which(degree(network_dc_switch, mode = "all")== 0))
network_dc_switch_no_weight <- delete_edge_attr(network_dc_switch, "weight")
#### Node stats ####
wtc <- cluster_walktrap(network_dc_switch_no_weight)
mod <- modularity(network_dc_switch_no_weight, membership(wtc))
connect <- pulsar::natural.connectivity(as_adj(network_dc_switch_no_weight), norm = TRUE)
print(mod)
hubs <- data.frame(hub_score(network_dc_switch_no_weight)$vector); colnames(hubs) <- c("hubscore")
hubs$OTU <- V(network_dc_switch_no_weight)$name
hubs$betweeness <- betweenness(network_dc_switch_no_weight, directed = FALSE, weights = NULL)
hubs$pr <- page_rank(network_dc_switch_no_weight)$vector
hubs$degree <- degree(network_dc_switch_no_weight)
hubs$module <- wtc$membership
hubs <- merge(hubs, samples)
nodes.stats <- hubs
node.stats2 <- left_join(nodes.stats, mean.rel.abund.combined, by = "OTU")
node.stats <- rbind.data.frame(node.stats, node.stats2)
########
###### Network Complexity ######
n.nodes <- length(V(network_dc_switch)) # N nodes
n.edges <- length(E(network_dc_switch)) # N edges
Sample <- sample.levels[[i]]
############
###### Edge Stats #######
edge.network <- data.frame(ends(network_dc_switch, E(network_dc_switch)))
colnames(edge.network) <- c("from", "to")
edge.network <- left_join(edge.network, taxonomy.combined, by = c("from" = "OTU_ID"))
colnames(edge.network) <- c("from", "to", "Kingdom_from", "Phylum_from", "Class_from", "Order_from",
"Family_from", "Genus_from", "Label_from", "Species_from")
edge.network <- left_join(edge.network, taxonomy.combined, by = c("to" = "OTU_ID"))
colnames(edge.network) <- c("from", "to", "Kingdom_from", "Phylum_from", "Class_from", "Order_from",
"Family_from", "Genus_from", "Label_from", "Species_from",
"Kingdom_to", "Phylum_to", "Class_to", "Order_to",
"Family_to", "Genus_v", "Label_to", "Species_to")
edge.network$weights <- E(network_dc_switch)$weight
edge.network$direction <- ifelse(edge.network$weights > 0, "Positive", "Negative")
edge.network$inter <- ifelse(edge.network$Kingdom_from == edge.network$Kingdom_to, "Intrakingdom", "Interkingdom")
edge.network$interXdirection <- interaction(edge.network$inter, edge.network$direction)
edge.network$Kingdom_Kingdom <- ifelse(edge.network$inter == "Intrakingdom" & edge.network$Kingdom_to == "Fungi", "Fungal-Fungal", "Prokaryote-Prokaryote")
edge.network$Tremellomycete_Alphaproteobacteria <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Alphaproteobacteria" & edge.network$direction == "Positive", "Tremellomycete-Alphaproteobacteria", "Other")
edge.network$Tremellomycete_Actinobacteria <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Actinobacteria" & edge.network$direction == "Positive", "Tremellomycete-Actinobacteria", "Other")
edge.network$Tremellomycete_Cytophagia <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Class_from == "Cytophagia" & edge.network$direction == "Positive", "Tremellomycete-Cytophagia", "Other")
edge.network$Bull_Prok <- ifelse(edge.network$Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & edge.network$Genus_from %in% c("Sphingomonas", "Methylobacterium", "Hymenobacter", "Pseudomonas", "Nocardioides") & edge.network$direction == "Positive", "Bull-Prok", "Other")
### Hannaella - Bacteria positive edges ###
edge.network$Hannaella_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Hannaella_Sphingomonas", "Other")
edge.network$Hannaella_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Hannaella_Methylobacterium", "Other")
edge.network$Hannaella_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Hannaella_Hymenobacter", "Other")
edge.network$Hannaella_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Hannaella_Pseudomonas", "Other")
edge.network$Hannaella_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Hannaella") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Hannaella_Nocardioides", "Other")
### Dioszengia - Bacteria positive edges ###
edge.network$Dioszegia_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Dioszegia_Sphingomonas", "Other")
edge.network$Dioszegia_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Dioszegia_Methylobacterium", "Other")
edge.network$Dioszegia_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Dioszegia_Hymenobacter", "Other")
edge.network$Dioszegia_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Dioszegia_Pseudomonas", "Other")
edge.network$Dioszegia_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Dioszegia") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Dioszegia_Nocardioides", "Other")
### Vishniacozyma - Bacteria positive edges ###
edge.network$Vishniacozyma_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Vishniacozyma_Sphingomonas", "Other")
edge.network$Vishniacozyma_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Vishniacozyma_Methylobacterium", "Other")
edge.network$Vishniacozyma_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Vishniacozyma_Hymenobacter", "Other")
edge.network$Vishniacozyma_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Vishniacozyma_Pseudomonas", "Other")
edge.network$Vishniacozyma_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Vishniacozyma") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Vishniacozyma_Nocardioides", "Other")
### Sporobolomyces - Bacteria positive edges ###
edge.network$Sporobolomyces_Sphingomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Sphingomonas") & edge.network$direction == "Positive", "Sporobolomyces_Sphingomonas", "Other")
edge.network$Sporobolomyces_Methylobacterium <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Methylobacterium") & edge.network$direction == "Positive", "Sporobolomyces_Methylobacterium", "Other")
edge.network$Sporobolomyces_Hymenobacter <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Hymenobacter") & edge.network$direction == "Positive", "Sporobolomyces_Hymenobacter", "Other")
edge.network$Sporobolomyces_Pseudomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Pseudomonas") & edge.network$direction == "Positive", "Sporobolomyces_Pseudomonas", "Other")
edge.network$Sporobolomyces_Nocardioides <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Nocardioides") & edge.network$direction == "Positive", "Sporobolomyces_Nocardioides", "Other")
edge.network$Sporobolomyces_Roseomonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Roseomonas") & edge.network$direction == "Positive", "Sporobolomyces_Roseomonas", "Other")
edge.network$Sporobolomyces_Terrimonas <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Terrimonas") & edge.network$direction == "Positive", "Sporobolomyces_Terrimonas", "Other")
edge.network$Sporobolomyces_Bacillus <- ifelse(edge.network$Genus_v %in% c("Sporobolomyces") & edge.network$Genus_from %in% c("Bacillus") & edge.network$direction == "Positive", "Sporobolomyces_Bacillus", "Other")
n.Tremellomycete_Alphaproteobacteria <- length(edge.network$Tremellomycete_Alphaproteobacteria[edge.network$Tremellomycete_Alphaproteobacteria != "Other"])
n.Tremellomycete_Actinobacteria <- length(edge.network$Tremellomycete_Actinobacteria[edge.network$Tremellomycete_Actinobacteria != "Other"])
n.Tremellomycete_Cytophagia <- length(edge.network$Tremellomycete_Cytophagia[edge.network$Tremellomycete_Cytophagia != "Other"])
n.Bull_Prok <- length(edge.network$Bull_Prok[edge.network$Bull_Prok != "Other"])
### n Hannaella - Bacteria positive edges ###
n.Hannaella_Sphingomonas <- length(edge.network$Hannaella_Sphingomonas[edge.network$Hannaella_Sphingomonas != "Other"])
n.Hannaella_Methylobacterium <- length(edge.network$Hannaella_Methylobacterium[edge.network$Hannaella_Methylobacterium != "Other"])
n.Hannaella_Hymenobacter <- length(edge.network$Hannaella_Hymenobacter[edge.network$Hannaella_Hymenobacter != "Other"])
n.Hannaella_Pseudomonas <- length(edge.network$Hannaella_Pseudomonas[edge.network$Hannaella_Pseudomonas != "Other"])
n.Hannaella_Nocardioides <- length(edge.network$Hannaella_Nocardioides[edge.network$Hannaella_Nocardioides != "Other"])
### n Dioszegia - Bacteria positive edges ###
n.Dioszegia_Sphingomonas <- length(edge.network$Dioszegia_Sphingomonas[edge.network$Dioszegia_Sphingomonas != "Other"])
n.Dioszegia_Methylobacterium <- length(edge.network$Dioszegia_Methylobacterium[edge.network$Dioszegia_Methylobacterium != "Other"])
n.Dioszegia_Hymenobacter <- length(edge.network$Dioszegia_Hymenobacter[edge.network$Dioszegia_Hymenobacter != "Other"])
n.Dioszegia_Pseudomonas <- length(edge.network$Dioszegia_Pseudomonas[edge.network$Dioszegia_Pseudomonas != "Other"])
n.Dioszegia_Nocardioides <- length(edge.network$Dioszegia_Nocardioides[edge.network$Dioszegia_Nocardioides != "Other"])
### n Vishniacozyma - Bacteria positive edges ###
n.Vishniacozyma_Sphingomonas <- length(edge.network$Vishniacozyma_Sphingomonas[edge.network$Vishniacozyma_Sphingomonas != "Other"])
n.Vishniacozyma_Methylobacterium <- length(edge.network$Vishniacozyma_Methylobacterium[edge.network$Vishniacozyma_Methylobacterium != "Other"])
n.Vishniacozyma_Hymenobacter <- length(edge.network$Vishniacozyma_Hymenobacter[edge.network$Vishniacozyma_Hymenobacter != "Other"])
n.Vishniacozyma_Pseudomonas <- length(edge.network$Vishniacozyma_Pseudomonas[edge.network$Vishniacozyma_Pseudomonas != "Other"])
n.Vishniacozyma_Nocardioides <- length(edge.network$Vishniacozyma_Nocardioides[edge.network$Vishniacozyma_Nocardioides != "Other"])
### n Sporobolomyces - Bacteria positive edges ###
n.Sporobolomyces_Sphingomonas <- length(edge.network$Sporobolomyces_Sphingomonas[edge.network$Sporobolomyces_Sphingomonas != "Other"])
n.Sporobolomyces_Methylobacterium <- length(edge.network$Sporobolomyces_Methylobacterium[edge.network$Sporobolomyces_Methylobacterium != "Other"])
n.Sporobolomyces_Hymenobacter <- length(edge.network$Sporobolomyces_Hymenobacter[edge.network$Sporobolomyces_Hymenobacter != "Other"])
n.Sporobolomyces_Pseudomonas <- length(edge.network$Sporobolomyces_Pseudomonas[edge.network$Sporobolomyces_Pseudomonas != "Other"])
n.Sporobolomyces_Nocardioides <- length(edge.network$Sporobolomyces_Nocardioides[edge.network$Sporobolomyces_Nocardioides != "Other"])
n.Sporobolomyces_Roseomonas <- length(edge.network$Sporobolomyces_Roseomonas[edge.network$Sporobolomyces_Roseomonas != "Other"])
n.Sporobolomyces_Terrimonas <- length(edge.network$Sporobolomyces_Terrimonas[edge.network$Sporobolomyces_Terrimonas != "Other"])
n.Sporobolomyces_Bacillus <- length(edge.network$Sporobolomyces_Bacillus[edge.network$Sporobolomyces_Bacillus != "Other"])
n.Intrakingdom.Neg <- length(edge.network$interXdirection[edge.network$interXdirection == "Intrakingdom.Negative"])
n.Intrakingdom.Pos <- length(edge.network$interXdirection[edge.network$interXdirection == "Intrakingdom.Positive"])
n.Interkingdom.Pos <- length(edge.network$interXdirection[edge.network$interXdirection == "Interkingdom.Positive"])
n.Interkingdom.Neg <- length(edge.network$interXdirection[edge.network$interXdirection == "Interkingdom.Negative"])
n.Fungal.Neg <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Fungal-Fungal" & edge.network$direction == "Negative"])
n.Fungal.Pos <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Fungal-Fungal" & edge.network$direction == "Positive"])
n.Prok.Neg <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Prokaryote-Prokaryote" & edge.network$direction == "Negative"])
n.Prok.Pos <- length(edge.network$Kingdom_Kingdom[edge.network$Kingdom_Kingdom == "Prokaryote-Prokaryote" & edge.network$direction == "Positive"])
edge.network_i <- merge(edge.network, samples)
edge.complexity <- rbind.data.frame(edge.network_i, edge.complexity)
######
network_complexity <- data.frame(phyloseq.object@sam_data[phyloseq.object@sam_data$SampleID == Sample,])
network_complexity_i <- cbind.data.frame(n.nodes, n.edges, network_complexity,
n.Intrakingdom.Neg, n.Intrakingdom.Pos, n.Interkingdom.Pos, n.Interkingdom.Neg,
n.Fungal.Neg, n.Fungal.Pos, n.Prok.Neg, n.Prok.Pos, mod, connect, n.Tremellomycete_Alphaproteobacteria, n.Tremellomycete_Actinobacteria, n.Tremellomycete_Cytophagia, n.Bull_Prok, n.Hannaella_Sphingomonas, n.Hannaella_Methylobacterium, n.Hannaella_Hymenobacter, n.Hannaella_Pseudomonas, n.Hannaella_Nocardioides,
n.Vishniacozyma_Sphingomonas, n.Vishniacozyma_Methylobacterium, n.Vishniacozyma_Hymenobacter, n.Vishniacozyma_Pseudomonas, n.Vishniacozyma_Nocardioides,
n.Dioszegia_Sphingomonas, n.Dioszegia_Methylobacterium, n.Dioszegia_Hymenobacter, n.Dioszegia_Pseudomonas, n.Dioszegia_Nocardioides, n.Sporobolomyces_Sphingomonas, n.Sporobolomyces_Methylobacterium, n.Sporobolomyces_Hymenobacter, n.Sporobolomyces_Pseudomonas,
n.Sporobolomyces_Nocardioides, n.Sporobolomyces_Roseomonas, n.Sporobolomyces_Terrimonas, n.Sporobolomyces_Bacillus)
network.complexity2 <- rbind.data.frame(network.complexity2, network_complexity_i)
}
### OTUPUT ###
Network.complexity.soy <- network.complexity2
Network.edges.soy <- edge.complexity
Network.nodes.soy <- node.stats
##################################
```
Put both metanetwork objects together
```{r}
Network.complexity <- rbind.data.frame(Network.complexity.corn, Network.complexity.soy)
Network.nodes <- rbind.data.frame(Network.nodes.corn, Network.nodes.soy)
Network.edges <- rbind.data.frame(Network.edges.soy, Network.edges.corn)
```
```{r}
# Complexity = edges per node
Network.complexity$complexity <- Network.complexity$n.edges/Network.complexity$n.nodes
Network.complexity$Treatment_label <- ifelse(Network.complexity$Treatment == "T1", "Conventional", "No-till")
# Facet Labels for plots
Network.nodes$Treatment_label <- ifelse(Network.nodes$Treatment == "T1", "Conventional", "No-till")
# Changing the date format and putting the categorical dpf
Network.complexity$DateSampled <- as.Date(Network.complexity$DateSampled, "%d-%b-%y")
Network.complexity$DPF <- ifelse(Network.complexity$DateSampled == "2018-08-03", "0-dpf",
ifelse(Network.complexity$DateSampled == "2017-06-26", "0-dpf",
ifelse(Network.complexity$DateSampled == "2018-08-16", "13-dpf",
ifelse(Network.complexity$DateSampled == "2017-07-5", "9-dpf",
ifelse(Network.complexity$DateSampled == "2017-07-31", "34-dpf", "33-dpf")))))
Network.complexity$DPF <- factor(Network.complexity$DPF, levels = c("0-dpf", "9-dpf", "13-dpf", "33-dpf", "34-dpf"))
Network.nodes$DateSampled <- as.Date(Network.nodes$DateSampled, "%d-%b-%y")
Network.nodes$DPF <- ifelse(Network.nodes$DateSampled == "2018-08-03", "0-dpf",
ifelse(Network.nodes$DateSampled == "2017-06-26", "0-dpf",
ifelse(Network.nodes$DateSampled == "2018-08-16", "13-dpf",
ifelse(Network.nodes$DateSampled == "2017-07-5", "9-dpf",
ifelse(Network.nodes$DateSampled == "2017-07-31", "34-dpf", "33-dpf")))))
Network.nodes$DPF <- factor(Network.nodes$DPF, levels = c("0-dpf", "9-dpf", "13-dpf", "33-dpf", "34-dpf"))
Network.edges$DateSampled <- as.Date(Network.edges$DateSampled, "%d-%b-%y")
Network.edges$DPF <- ifelse(Network.edges$DateSampled == "2018-08-03", "0-dpf",
ifelse(Network.edges$DateSampled == "2017-06-26", "0-dpf",
ifelse(Network.edges$DateSampled == "2018-08-16", "13-dpf",
ifelse(Network.edges$DateSampled == "2017-07-5", "9-dpf",
ifelse(Network.edges$DateSampled == "2017-07-31", "34-dpf", "33-dpf")))))
Network.edges$DPF <- factor(Network.edges$DPF, levels = c("0-dpf", "9-dpf", "13-dpf", "33-dpf", "34-dpf"))
```
What many negative vs positive interactions are there.
```{r}
Network.edges %>%
subset(Kingdom_to %in% c("Fungi") & Kingdom_from %in% c("Bacteria") ) %>%
group_by(SampleID, Crop, Fungicide, direction, Kingdom_from) %>%
tally() %>% # edges per sample (subgraph)
group_by(Crop, Fungicide, direction) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.percent.edges, SE.percent.edges)) %>%
ggplot(aes(x = interaction(Crop, Fungicide), y = mean.percent.edges, fill = direction)) +
geom_bar()
ggplot(Network.edges, aes(x = interaction(Fungicide, Crop), fill = direction)) +
geom_bar() +
facet_wrap(~inter)
```
How many edges per-sample, on average, do the Bulleribacidiaceae have with bacteria, and which Phyla, and classes do they co-occur with
```{r}
##### Soybean - Phylum ####
# bulleribacidiaceae
Network.edges %>%
subset(Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & Kingdom_from == "Bacteria" & Crop == "Soy" & Fungicide == "C") %>%
group_by(SampleID, Class_from, direction) %>%
tally() %>% # counts the number of edges
ungroup() %>%
complete(Class_from, nesting(SampleID, direction), fill = list(n = 0)) %>%
group_by(Class_from, direction) %>% # group by class
nest() %>% # nest the classes for each sample
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>% # iterate through the classes to calculate the mean number of edges per sample
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges)) %>%
arrange(-mean.edges)
# Sporidobolaceae
Network.edges %>%
subset(Genus_v %in% c("Sporobolomyces") & Kingdom_from == "Bacteria" & Fungicide == "C" & Crop == "Soy") %>%
group_by(SampleID, Fungicide, Phylum_from) %>%
tally() %>% # counts the number of edges
ungroup() %>%
complete(Phylum_from, nesting(SampleID, Fungicide), fill = list(n = 0)) %>%
group_by(Phylum_from, Fungicide) %>% # group by class
nest() %>% # nest the classes for each sample
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>% # iterate through the classes to calculate the mean number of edges per sample
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges)) %>%
arrange(-mean.edges)
###############
##### Corn - Phylum ####
Network.edges %>%
subset(Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & Kingdom_from == "Bacteria" & Fungicide == "C" & Crop == "Corn") %>%
group_by(SampleID, Phylum_from) %>%
tally() %>%
mutate(n_pct = 100*n/sum(n)) %>%
group_by(Phylum_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n_pct))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n_pct)/sqrt(length(.$n_pct)))) %>%
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges, mean.percent.edges, SE.percent.edges)) %>%
arrange(-mean.percent.edges)
###############
##### Soybean - Classes ####
Network.edges %>%
subset(Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & Class_from %in% c("Proteobacteria", "Actinobacteria", "Bacteroidetes") & Crop == "Soy") %>%
group_by(SampleID, Fungicide, Class_from) %>%
tally() %>% # counts the number of edges
ungroup() %>%
complete(Class_from, nesting(SampleID, Fungicide), fill = list(n = 0)) %>%
group_by(Class_from, Fungicide) %>% # group by class
nest() %>% # nest the classes for each sample
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>% # iterate through the classes to calculate the mean number of edges per sample
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges)) %>%
arrange(-mean.edges)
# about half of the bacteria bulleribacideaceae were with alphaproteobacteria
Network.edges %>%
subset(Genus_v %in% c("Sporobolomyces") & Kingdom_from %in% c("Bacteria") & Fungicide == "C" & Crop == "Soy") %>%
group_by(SampleID, Genus_from) %>%
tally() %>%
mutate(n_pct = 100*n/sum(n)) %>%
group_by(Genus_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n_pct))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n_pct)/sqrt(length(.$n_pct)))) %>%
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges, mean.percent.edges, SE.percent.edges)) %>%
arrange(-mean.percent.edges)
###############
##### Corn - Classes #####
Network.edges %>%
subset(Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & Phylum_from %in% c("Proteobacteria", "Actinobacteria", "Bacteroidetes") & Fungicide == "C" & Crop == "Corn") %>%
group_by(SampleID, Class_from) %>%
tally() %>%
mutate(n_pct = 100*n/sum(n)) %>%
group_by(Class_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n_pct))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n_pct)/sqrt(length(.$n_pct)))) %>%
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges, mean.percent.edges, SE.percent.edges)) %>%
arrange(-mean.percent.edges)
# About 70 % cumulative with Actinobacteria, and 30 % with Alphaproteobacteria - different for corn.
###############
```
So, it seems that most of the edges (i.e., co-occurances) between the Bulleribacidiaceae yeasts and bacteria occur between either the Actinobacteria for corn and Proteobacteria for soybean. Next lets see if those interactions are mostly negative or positive.
Are these co-occurances usually positive or negative?
```{r}
##### Soybean #####
Network.edges %>%
subset(Genus_v %in% c("Hannaella", "Vishniacozyma", "Dioszegia") & Class_from %in% c("Actinobacteria", "Gammaproteobacteria", "Alphaproteobacteria", "Bacteroidia") & Fungicide == "C" & Crop == "Soy") %>%
group_by(SampleID, direction, Species_to) %>%
tally() %>% # counts the number of edges
ungroup() %>%
complete(Species_to, nesting(SampleID, direction), fill = list(n = 0)) %>%
group_by(Species_to, direction) %>% # group by class
nest() %>% # nest the classes for each sample
mutate(mean.edges = purrr::map(data,~mean(.$n))) %>% # iterate through the classes to calculate the mean number of edges per sample
mutate(SE.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.edges,SE.edges)) %>%
arrange(-mean.edges) %>%
pivot_wider(id_cols = c(Species_to), names_from = direction, values_from = mean.edges) %>%
arrange(-Positive) %>%
mutate(fold.change = Positive/Negative)
group_by(SampleID, direction, Class_from) %>%
tally() %>% # edges per sample (subgraph) at the Class level
group_by(direction, Class_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.percent.edges, SE.percent.edges)) %>%
pivot_wider(id_cols = c(Class_from), names_from = direction, values_from = mean.percent.edges) %>%
arrange(-Positive) %>%
mutate(fold.change = Positive/Negative)
# Edges among these three fungal genera with Proteobacteria were disproportionately positive with on average 2.33 times more positive than negative edges (mean negative edges = 4.46 edges per sample; mean positive edges = 10.0 edges per sample). Similarly for Actinobacteria, there were 2.16 times more positive than negative edges per sample.
Network.edges %>%
subset(Genus_v %in% c("Sporobolomyces") & Class_from %in% c("Alphaproteobacteria", "Actinobacteria", "Bacilli") & Fungicide == "C" & Crop == "Soy") %>%
group_by(SampleID, direction, Class_from) %>%
tally() %>% # edges per sample (subgraph) at the Class level
group_by(direction, Class_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.percent.edges, SE.percent.edges)) %>%
pivot_wider(id_cols = c(Class_from), names_from = direction, values_from = mean.percent.edges) %>%
arrange(-Positive) %>%
mutate(fold.change = Positive/Negative)
##### Corn #####
Network.edges %>%
subset(Kingdom_to %in% c("Fungi") & Kingdom_from %in% c("Bacteria") & Fungicide == "C" & Crop == "Corn") %>%
group_by(SampleID, direction, Kingdom_from) %>%
tally() %>% # edges per sample (subgraph) at the Class level
group_by(direction, Class_from) %>%
nest() %>%
mutate(mean.percent.edges = purrr::map(data,~mean(.$n))) %>%
mutate(SE.percent.edges = purrr::map(data,~sd(.$n)/sqrt(length(.$n)))) %>%
unnest(c(mean.percent.edges, SE.percent.edges)) %>%
pivot_wider(id_cols = c(Class_from), names_from = direction, values_from = mean.percent.edges) %>%
arrange(-Positive) %>%
mutate(fold.change = Positive/Negative)
# Edges among these three fungal genera with Alphaproteobacteria were disproportionately positive with on average 1.57 times more positive than negative edges (mean negative edges = 4.46 edges per sample; mean positive edges = 10.0 edges per sample). Similarly for Actinobacteria, there were 3.02 times more positive than negative edges per sample.
```
Lets plot these results
```{r}
#### MODULARITY ####
modularity.box <- ggplot(Network.complexity, aes(x = DPF, y = mod, color = Fungicide)) +
geom_boxplot() +
geom_point(position = position_jitterdodge()) +
ylab("Modularity") +
xlab("")+
stat_compare_means(aes(group = Fungicide), label.y = c(0.1), method = "wilcox.test") +
#scale_color_manual(values=c(cbbPalette[[2]], cbbPalette[[1]]), labels = c("Fungicide", "Control")) +
theme_classic() +
labs(color = "") +
facet_wrap(~Treatment_label*Crop, scales = "free") +
theme(strip.background = element_rect(color="white", fill="white", size=1.5, linetype="solid"),
strip.text.x = element_text(size = 12, color = "black"),
legend.position="top")
#### NUMBER OF EDGES PER NODE (i.e., Network Complexity) ####
complexity.box <- ggplot(Network.complexity, aes(x = DPF, y = complexity, color = Fungicide)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = position_jitterdodge()) +
ylab("Linkage density") +
xlab("")+
stat_compare_means(aes(group = Fungicide), label.y = c(0.1), method = "wilcox.test") +
scale_color_manual(values=c(cbbPalette[[1]], cbbPalette[[2]]), labels = c("Control", "Fungicide")) +
theme_classic() +
labs(color = "") +
facet_wrap(~Crop*Treatment_label, scales = "free") +
theme(strip.background = element_rect(color="white", fill="white", size=1.5, linetype="solid"),
strip.text.x = element_text(size = 12, color = "black"),
legend.position="top")