forked from yaoli1haoji/G2F_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg2f_analysis_revised_publish.R
1656 lines (1218 loc) · 80.7 KB
/
g2f_analysis_revised_publish.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
install.packages(gridExtra)
install.packages(ggplot2)
install.packages(data.table)
install.packages(emmeans)
install.packages(multcomp)
install.packages(qiime2R)
install.packages(stringr)
install.packages(dplyr)
install.packages(tibble)
install.packages(tidyr)
install.packages("devtools")
install.packages(microbiome)
install.packages(car)
install.packages(lme4)
install.packages(lme4qtl)
install.packages(gvlma)
install.packages(outliers)
install.packages("lmerTest")
install.packages("Maaslin2")
#install.packages("EnvStats")
install.packages(EnvStats)
if (!requireNamespace("devtools", quietly = TRUE)){install.packages("devtools")}
devtools::install_github("jbisanz/qiime2R")
if(!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("Maaslin2")
install.packages(qiime2R)
install.packages(vegan)
install.packages("genefilter")
install.packages("NetCoMi")
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
install.packages(metagMisc)
install.packages(phyloseq)
install.packages(geodist)
library(gridExtra)
library(ggplot2)
library(data.table)
library(emmeans)
library(multcomp)
library(qiime2R)
library(stringr)
library(dplyr)
library(tibble)
library(tidyr)
library("devtools")
#library(microbiome)
#library(car)
library(lme4)
library(lme4qtl)
library(gvlma)
library(outliers)
#install.packages("EnvStats")
library(EnvStats)
library(qiime2R)
library(vegan)
library("genefilter")
library("NetCoMi")
library(ggpubr)
library(metagMisc)
library(phyloseq)
#install.packages("geodist")
library(geodist)
#set the work direcotry in where you store all the data from github
setwd("D:/G2F_Rerun/G2F_data/G2F_data-main/")
#read in the ASV table after mitochondira and chloroplast filteration
otu_table = read.table("./dada2_table-no-mitochondria-no-chloroplast.txt",row.names =1,check.names = FALSE,sep="\t",skip=1,header = TRUE)
#identify the blank taxa
blank_taxa <- otu_table[rowSums(otu_table[,c(1:10)]) > 1,]
#filter out the blank taxa
otu_table_blank_taxa_filtered <- otu_table[rowSums(otu_table[,c(1:10)]) < 1,]
#otu_table_blank_taxa_filtered_singleton_remove <- otu_table_blank_taxa_filtered[rowSums(otu_table_blank_taxa_filtered) > 10,]
#write.table(otu_table_blank_taxa_filtered,"/home/hl46161/new_G2F_dada2/exported_table/dada2_table-no-mitochondria-no-chloroplast-blank-taxa-filtered.txt",sep="\t")
#import otu table and convert to matrix
otu_table = as.matrix(otu_table_blank_taxa_filtered)
#taxonomy come from qiime2 artifact and needs to seperate in domain, phylum,order,......
taxonomy = read.csv("./taxonomy.tsv",sep="\t",row.names =1)
#revise the taxonomy so that uncultured family have previous taxonomy information
revised_taxonomy <- taxonomy
#change the column to character
revised_taxonomy$Family <- as.character(revised_taxonomy$Family)
revised_taxonomy$Order <- as.character(revised_taxonomy$Order)
revised_taxonomy$Class <- as.character(revised_taxonomy$Class)
#select family taxa that have no taxonomu information until phylumn level and paste the phylumn level information with family information
revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured" & revised_taxonomy$Class ==" c__uncultured"] <- paste(revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured" & revised_taxonomy$Class ==" c__uncultured"],revised_taxonomy$Phylum[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured" & revised_taxonomy$Class ==" c__uncultured"])
#select family taxa that have no taxonomu information until class level and paste the class level information with family information
revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured"] <- paste(revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured"],revised_taxonomy$Class[revised_taxonomy$Family ==" f__uncultured"& revised_taxonomy$Order ==" o__uncultured"])
#select family taxa that have no taxonomu information until order level and paste the order level information with family information
revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"] <- paste(revised_taxonomy$Family[revised_taxonomy$Family ==" f__uncultured"],revised_taxonomy$Order[revised_taxonomy$Family ==" f__uncultured"])
# revise factor to character to avoid problem in column operation
revised_taxonomy$Family <- as.factor(revised_taxonomy$Family)
revised_taxonomy$Order <- as.factor(revised_taxonomy$Order)
revised_taxonomy$Class <- as.factor(revised_taxonomy$Class)
#revised_taxonomy <- revised_taxonomy[rownames(revised_taxonomy) %in% rownames(otu_table_blank_taxa_filtered),]
revised_taxonomy = as.matrix(revised_taxonomy)
taxonomy <- as.matrix(revised_taxonomy)
#import phylogenetic tree table and convert to matrix
phy_tree = read_tree("./tree.nwk")
#import metadata table and convert to matrix
metadata = read.table("./G2f_2019_sub_corrected_metadata.tsv",sep ="\t",header = 1,row.names=1)
#rownames(metadata) <- metadata$SampleID
#formate the iput data
OTU = otu_table(otu_table,taxa_are_rows = TRUE)
TAX = tax_table(taxonomy)
META = sample_data(metadata)
##build phyloseq object
ps = phyloseq(OTU,TAX,phy_tree,META)
sample_names(META)
sample_names(OTU)
#rarecurve(t(otu_table(ps)), step=100, cex=0.5)
#remove NCH1-1-159 samples for m
ps <- subset_samples(ps, rownames(sample_data(ps)) != "NCH1-1-159")
ps.rarefied = rarefy_even_depth(ps,sample.size=1500, replace=F,rngseed=1)
summary(sample_data(ps.rarefied)$Corrected_pedigree)
`%!in%` <- Negate(`%in%`)
#filtered to contain only yellow stripe species since seed substitution cause some samples are wrong pedigree
ps.rarefied_ys_filtered <- subset_samples(ps.rarefied, Corrected_pedigree %in% c("PHW52/PHM49","B73/PHM49","F42/H95","F42/MO17","OH43/B37","PHW52/PHN82","B14A/OH43","B14A/H95","B14A/MO17",
"LH74/PHN82","PHG39/PHN82","B73/MO17","B73/PHN82","B37/H95","F42/OH43","B37/MO17","B37/OH43","CG119/CG108",
"CG44/CGR01","2369/LH123HT"))
#create a heatmap to visualize the distribution of pedigree across the location
correted_pedigree_heatmap <-ggplot(sample_data(ps.rarefied_ys_filtered), aes(x=location,y=Corrected_pedigree)) +
geom_tile()
correted_pedigree_heatmap
################################################################################################################################
#calculate the alpha diversity
alphaObserved = estimate_richness(ps.rarefied_ys_filtered, measures="Observed")
alphaSimpson = estimate_richness(ps.rarefied_ys_filtered, measures="Simpson")
alphaShannon = estimate_richness(ps.rarefied_ys_filtered, measures="Shannon")
#merge the alpha diversity and metadata
G2F_metadata_2019 <- cbind(alphaObserved, sample_data(ps.rarefied_ys_filtered))
G2F_metadata_2019 <- cbind(G2F_metadata_2019, alphaSimpson)
G2F_metadata_2019 <- cbind(G2F_metadata_2019,alphaShannon)
#use kruskal wallis test to test for significant difference between location and pedigree
kruskal.test(Observed~location, data = G2F_metadata_2019)
kruskal.test(Simpson~location, data = G2F_metadata_2019)
kruskal.test(Shannon~location, data = G2F_metadata_2019)
kruskal.test(Observed~Corrected_pedigree, data = G2F_metadata_2019)
kruskal.test(Simpson~Corrected_pedigree, data = G2F_metadata_2019)
kruskal.test(Shannon~Corrected_pedigree, data = G2F_metadata_2019)
###########################################################################
################ plot the alpha diversity
Shannon_location <- plot_richness(ps.rarefied_ys_filtered, x="location", measures=c("Shannon")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
Shannon_location
ggsave("./shannon_plot_location_rerun.png", height=10, width=10, device="png")
Observed_location <- plot_richness(ps.rarefied_ys_filtered, x="location", measures=c("Observed")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
Observed_location
ggsave("./Observed_plot_location_rerun.png", height=10, width=10, device="png")
Simpson_location <- plot_richness(ps.rarefied_ys_filtered, x="location", measures=c("Simpson")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
Simpson_location
Shannon_pedigree <- plot_richness(ps.rarefied_ys_filtered, x="Corrected_pedigree", measures=c("Shannon")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
Shannon_pedigree
ggsave("./shannon_plot_pedigree_rerun.png", height=10, width=10, device="png")
Observed_pedigree <- plot_richness(ps.rarefied_ys_filtered, x="Corrected_pedigree", measures=c("Observed")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
Observed_pedigree
ggsave("./Observed_plot_pedigree_rerun.png", height=10, width=10, device="png")
Simpson_pedigree <- plot_richness(ps.rarefied_ys_filtered, x="Corrected_pedigree", measures=c("Simpson")) + geom_boxplot(fill="red") +
theme(
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 20),
axis.title.x = element_text(size=20, face="bold"),
axis.title.y = element_text(size=20, face="bold"),
plot.title = element_text(size=20, face="bold"),
axis.text.y = element_text(size=20, face="bold"),
axis.text.x = element_text(size=20, face="bold"),
strip.text.x = element_text(size=20,face="bold"),
panel.background = element_blank()
)
# plot the shannon by location and by pedigree
alpha_diversity_plot_location_pedigree <- ggarrange(Shannon_pedigree,Observed_pedigree,nrow = 1,ncol = 2)
alpha_diversity_plot_location_pedigree
ggsave("./alpha_diversity_plot_location_pedigree_rerun.png", height=9, width=12, device="png")
#alpha_diversity_plot_location <- ggarrange(Shannon_location,Observed_location,nrow = 1,ncol = 2)
#alpha_diversity_plot_location
#ggsave("./alpha_diversity_plot_location_rerun.png", height=9, width=12, device="png")
alpha_diversity_plot_location <- ggarrange(Shannon_location,Observed_location,nrow = 1,ncol = 2)
alpha_diversity_plot_location
ggsave("./alpha_diversity_plot_location_rerun.png", height=9, width=12, device="png")
####################################################################################################
##########################################################################
#functoin used to keep only duplicate pedigree in every location
allDup <- function (value)
{
duplicated(value) | duplicated(value, fromLast = TRUE)
}
#create a empty dataframe
G2F_metadata_2019_duplicate_pedigree_ys_filtered <- as.data.frame(matrix(ncol = 50, nrow = 0))
#inherit colanems from metadata
colnames(G2F_metadata_2019_duplicate_pedigree_ys_filtered) <- colnames(G2F_metadata_2019)
#filter the dataset make sure each location only contain samples with peidgree that has at least two replicates
for (Location in unique(G2F_metadata_2019$location)) {
print(Location)
#subset dataset to each location
G2F_location_sample <- subset(G2F_metadata_2019,location==Location)
#find samples with duplicate pedigree
G2F_location_all_duplcate_sample <- G2F_location_sample[allDup( G2F_location_sample$Corrected_pedigree),]
#bind this location duplicate sample to big dataset
G2F_metadata_2019_duplicate_pedigree_ys_filtered <- rbind(G2F_metadata_2019_duplicate_pedigree_ys_filtered,G2F_location_all_duplcate_sample)
}
#check location and summary
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered$location)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered$Corrected_pedigree)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered$pedigree)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered$location)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered$Corrected_pedigree)
#make sample name to be rowname of the dataframe
G2F_metadata_2019_duplicate_pedigree_ys_filtered$SampleID <- rownames(G2F_metadata_2019_duplicate_pedigree_ys_filtered)
rownames(G2F_metadata_2019_duplicate_pedigree_ys_filtered) <- NULL
#keep location that has more than 10 samples
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location <- G2F_metadata_2019_duplicate_pedigree_ys_filtered %>%
group_by(location) %>% filter(n() >= 10)
##keep location has at least 3 unique pedigree
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location <- G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location %>%
group_by(Corrected_pedigree) %>% filter(length(unique(location)) >= 3)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)
##check sample name
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$SampleID
#change . in sample name to - for later operation
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$SampleID <- gsub("\\.","-",G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$SampleID)
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$SampleID
#G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location <- as.character(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
##check location and pedigree
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location <- as.factor(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree <- as.factor(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location <- as.character(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
##format to dataframe for string substitution
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location <- as.data.frame(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location)
#merge OH43/B37 and B37/OH43
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree <- gsub(".*^OH43/B37","B37/OH43",G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)
#create a heatmap to visualize the distribution of pedigree across the location
correted_pedigree_heatmap <-ggplot(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=location,y=Corrected_pedigree)) +
geom_tile()
correted_pedigree_heatmap
##################################################################################################################################
#visualize the alpha diversity
ggplot(data = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=Shannon)) + geom_histogram()
ggplot(data = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=Observed)) + geom_histogram()
ggplot(data = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=Simpson)) + geom_histogram()
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location <- as.factor(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree <- as.factor(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)
####### analyze the G,E,and GXE on alpha diversity
heribitility_calculation <- function(anova_result,alpha_name){
#record the sum of square of each term (G,E,GXE)
ssq_residue <- (anova_result$`Sum Sq`[4])
#print(ssq_residue)
ssq_location <- (anova_result$`Sum Sq`[1])
ssq_pedigree <- (anova_result$`Sum Sq`[2])
ssq_pedigree_location <- (anova_result$`Sum Sq`[3])
heritbality_location <- (ssq_location/(ssq_residue + ssq_location + ssq_pedigree +ssq_pedigree_location))
print("variation explained by Environment")
print(heritbality_location)
heritbality_pedigree <- (ssq_pedigree/(ssq_residue + ssq_location + ssq_pedigree + ssq_pedigree_location))
print("variation explained by Maize genotype")
print(heritbality_pedigree)
heritbality_pedigree_location <- (ssq_pedigree_location/(ssq_residue + ssq_location + ssq_pedigree +ssq_pedigree_location))
print("variation explained by GxE")
print(heritbality_pedigree_location)
#initialize the a list
location_heritibility_list <- list()
#record down the variance explained by each term (G,E,GXE)
location_heritibility_list[1] <- heritbality_location
location_heritibility_list[2] <- heritbality_pedigree
location_heritibility_list[3] <- heritbality_pedigree_location
#convert list to dataframe
location_heritibility_list <- as.data.frame(location_heritibility_list)
#formulate the
result_df <- as.data.frame(matrix(c(alpha_name,alpha_name,alpha_name,"Environment","Maize Genotype","GXE"),nrow = 3))
colnames(result_df) = c("alpha_diversity","test_var")
result_df
result_df$Value <- t(location_heritibility_list)
result_df
return(result_df)
}
##################################################################################################################
anova_permutation <- function(df){
df = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location
sum_of_square_df <- as.data.frame(matrix(ncol = 3, nrow = 0))
colnames(sum_of_square_df) <- c("environment","genotype","GXE")
E2_G1_GXE3_anova_results <- anova(lm(formula = Shannon ~ Corrected_pedigree + location + location:Corrected_pedigree , data = df))
G_sum_1 <- E2_G1_GXE3_anova_results$`Sum Sq`[1]
E_sum_2 <- E2_G1_GXE3_anova_results$`Sum Sq`[2]
GXE_sum <- E2_G1_GXE3_anova_results$`Sum Sq`[3]
E1_G2_GXE3_anova_results <- anova(lm(formula = Shannon ~ location + Corrected_pedigree + location:Corrected_pedigree, data = df))
E_sum_1 <- E1_G2_GXE3_anova_results$`Sum Sq`[1]
G_sum_2 <- E1_G2_GXE3_anova_results$`Sum Sq`[2]
GXE_sum <- E1_G2_GXE3_anova_results$`Sum Sq`[3]
temp_df <- data.frame(environment =mean(E_sum_1,E_sum_2), genotype=mean(G_sum_1,G_sum_2), GXE = GXE_sum)
sum_of_square_df = rbind(sum_of_square_df,temp_df)
return(sum_of_square_df)
}
########################################################################################
library(lmerTest)
#install.packages('EnvStats')
library(EnvStats)
# use test to eliminate outliers
test <- rosnerTest(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Shannon,k = 10)
test
#create G2F shannon dataset after removing outliers
G2F_metadata_2019_shannon <- G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location
#ggplot(data = G2F_metadata_2019_NO_sub_yellow_stripe_sub, aes(x=Shannon)) + geom_histogram()
ggplot(data = G2F_metadata_2019_shannon, aes(x=Shannon)) + geom_histogram()
summary(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)
###############################################################################
lm_shannon_location_pedigree <- lm(formula = Shannon ~ location + Corrected_pedigree + location:Corrected_pedigree, data = G2F_metadata_2019_shannon)
#shannon_assumption_check <- gvlma::gvlma(lm_shannon_location_pedigree)
#shannon_assumption_check
#plot(lm_shannon_location_pedigree)
#summary lm model
summary(lm_shannon_location_pedigree)
#contrast_setting <- list(location=contr.sum,Corrected_pedigree=contr.sum)
#run anova
lm_shannon_location_pedigree_anova_results <- anova(lm_shannon_location_pedigree)
lm_shannon_location_pedigree_anova_results
#formulate a list for later visualization
lm_shannon_result_list <- heribitility_calculation(lm_shannon_location_pedigree_anova_results,"shannon")
lm_shannon_result_list
##################################################################################
# use test to eliminate outliers
test <- rosnerTest(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Observed,
k = 10)
test
############
#visualize the observed feature data
ggplot(data = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=Observed)) + geom_histogram()
#remove outliers
G2F_metadata_2019_observed_features <- G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location[-c(42,298,87),]
rownames(G2F_metadata_2019_observed_features) <- NULL
##visualize the observed feature data after filtation
ggplot(data = G2F_metadata_2019_observed_features, aes(x=Observed)) + geom_histogram()
#rownames(G2F_metadata_2019_observed_features) <- NULL
#run lm model
lm_observed_features_location_pedigree <- lm(log(Observed) ~ location + Corrected_pedigree + location:Corrected_pedigree, data = G2F_metadata_2019_observed_features)
#lm_observed_features_location_pedigree <- lm(log(Observed) ~ location + Corrected_pedigree, data = G2F_metadata_2019_observed_features)
#lm_observed_features_location_pedigree <- glm(Observed ~ location + Corrected_pedigree + location:Corrected_pedigree, family="poisson", data=G2F_metadata_2019_observed_features)
#check assumption of linear regression model
#observed_features_assumption_check <- gvlma::gvlma(lm_observed_features_location_pedigree)
#observed_features_assumption_check
#plot(lm_observed_features_location_pedigree)
#summary lm model
summary(lm_observed_features_location_pedigree)
#run anova
lm_observed_features_location_pedigree_anova_results <- anova(lm_observed_features_location_pedigree)
#calculate heritibility
#heribitility_calculation_alpha(lm_observed_features_location_pedigree_anova_results,'shannon')
lm_observed_features_result_list <- heribitility_calculation(lm_observed_features_location_pedigree_anova_results,"# of taxa")
lm_observed_features_result_list
#lm_observed_features_location_pedigree_anova_results <- anova(lm(formula = log(Observed) ~ Corrected_pedigree + location:Corrected_pedigree + location, data = G2F_metadata_2019_observed_features))
##########################################################################################################
#visualize the Simpson index
ggplot(data = G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location, aes(x=Simpson)) + geom_histogram()
# use test to eliminate outliers
test <- rosnerTest(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Simpson,
k = 10)
test
#remove outliers
G2F_metadata_2019_simpson <- G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location[-c(131,19,219,217,234,342),]
##visualize the Simpson index after filtation
ggplot(data = G2F_metadata_2019_simpson, aes(x=log(Simpson))) + geom_histogram()
G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Simpson
#rownames(G2F_metadata_2019_observed_features) <- NULL
#run lm model
lm_simpson_location_pedigree <- lm(Simpson ~ location + Corrected_pedigree + location:Corrected_pedigree, data = G2F_metadata_2019_simpson)
#lm_simpson_location_pedigree <- lm(Simpson ~ location + Corrected_pedigree, data = G2F_metadata_2019_simpson)
#lm_simpson_location_pedigree <- glm(formula = Simpson ~ location + Corrected_pedigree, family = "poisson", data = G2F_metadata_2019_simpson)
#check assumption of linear regression model
#simpson_assumption_check <- gvlma::gvlma(lm_simpson_location_pedigree)
#simpson_assumption_check
#plot(lm_simpson_location_pedigree)
#summary lm model
summary(lm_simpson_location_pedigree)
#run anova
lm_simpson_location_pedigree_anova_results <- anova(lm_simpson_location_pedigree)
#calculate heritibility
lm_simpson_list <-heribitility_calculation(lm_simpson_location_pedigree_anova_results,"simpson")
lm_simpson_list
##########################################################################################################
#bind previous calculation together
alpha_result <- dplyr::bind_rows(lm_observed_features_result_list,lm_shannon_result_list)
alpha_result <- dplyr::bind_rows(alpha_result,lm_simpson_list)
alpha_result
alpha_result$category <- " Alpha Diversity"
#plot the result in a bar graph
ggplot(data=alpha_result, aes(x=alpha_diversity, y=Value, fill=test_var)) +
geom_bar(position="stack", stat="identity") + ylab("variance explained") + labs(fill = "category") +
scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8),limits = c(0,0.8)) +
theme(axis.title = element_text(size = 20, face = "bold", colour = "grey20"),
panel.background = element_blank(),legend.title = element_text(size = 30, face = "bold", colour = "grey30"),
) + theme(
legend.text = element_text(color = "black", size = 30),
axis.title.y = element_text(size=30, face="bold"),
plot.title = element_text(size=30, face="bold"),
axis.text.y = element_text(size=30, face="bold"),
axis.text.x = element_text(size=30, face="bold"),
strip.text.x = element_text(size=30,face="bold"),
legend.title = element_blank(),
axis.title.x = element_blank(),
legend.position="bottom",
)
#output the graph
ggsave("./alpha_diversity_GXE_2.png", height=10, width=12, device="png")
###############################################################################################
beta_heritibility_cal <- function(input_qza,input_metadata,enviroment_start,enviroment_end){
#extract distance matrix information
distance_matrix <- input_qza$data$Vectors
#get the sample name and first two pc
distance_matrix <- distance_matrix[,c("SampleID","PC1","PC2")]
print(distance_matrix)
#subset the distance matrix to match the metadata
distance_matrix <- subset(distance_matrix,SampleID %in% input_metadata$SampleID)
rownames(distance_matrix) <- distance_matrix$SampleID
distance_matrix$SampleID <- NULL
print(distance_matrix)
#subset the enviroment variable to check
environment= input_metadata[,enviroment_start:enviroment_end]
#run dbrda with location, pedigree and GXE
dbRDA_result <- dbrda(distance_matrix ~ location + Corrected_pedigree + location:Corrected_pedigree, environment)
print(dbRDA_result)
#run anova
anova(dbRDA_result)
#permutation to get p value
anova_result <- anova(dbRDA_result, by="terms", permu=100)
print(anova_result)
heribitility_calculation_beta(anova_result)
}
heribitility_calculation_beta <- function(anova_result){
ssq_residue <- (anova_result$SumOfSqs[4])
ssq_location <- (anova_result$SumOfSqs[1])
ssq_pedigree <- (anova_result$SumOfSqs[2])
ssq_pedigree_location <- (anova_result$SumOfSqs[3])
heritbality_location <- (ssq_location/(ssq_residue + ssq_location + ssq_pedigree + ssq_pedigree_location))
print(anova_result)
print("variation explained by location")
print(heritbality_location)
heritbality_pedigree <- (ssq_pedigree/(ssq_residue + ssq_location + ssq_pedigree + ssq_pedigree_location))
print("variation explained by pedigree")
print( heritbality_pedigree)
heritbality_pedigree <- (ssq_pedigree_location/(ssq_residue + ssq_location + ssq_pedigree + ssq_pedigree_location))
print("variation explained by G*E")
print(heritbality_pedigree)
}
###############################################################################
#analyze the beta diversity using weighted and unweighted unifrac
#read in the weighted and unweighted unifrac distance matrix generated by qiime2 since phyloseq unifrac method have bugs
weighted_unifrac <- read_qza("./weighted_unifrac_pcoa_results.qza")
unweighted_unifrac <- read_qza("./unweighted_unifrac_pcoa_results.qza")
##extract the cordniates values
weighted_unifrac_cordination <- weighted_unifrac$data$Vectors
weighted_unifrac_cordination
#choose first two pc
weighted_unifrac_cordination <- weighted_unifrac_cordination[,c("SampleID","PC1","PC2")]
#read in the metadata
G2F_Metadata_2019 = read.table("./G2f_2019_sub_corrected_metadata.tsv",sep ="\t",header = 1)
#convert the factor to character
G2F_Metadata_2019$location <- as.character(G2F_Metadata_2019$location)
G2F_metadata_2019_yellow_stripe_sub <- subset(G2F_Metadata_2019,location %in% c("INH1","MIH1","NCH1","NYH3","SCH1","IAH2","IAH4","OHH1","MNH1","MOH1","GAH2","NEH1","NEH2","DEH1"))
#create state group to enhance the visibility of weighted unifrac
G2F_Metadata_2019 <- G2F_Metadata_2019 %>% mutate(State_group = case_when(
location %in% c("MOH1","IAH2","IAH4","MNH1","NEH1","NEH2") ~ "Mid West",
location %in% c("WIH1","INH1","MIH1","OHH1") ~ "East Mississippi River",
location %in% c("GAH1","GAH2","SCH1","NCH1") ~ "South",
location %in% c("NYH2","NYH3","DEH1") ~ "North East"
))
#combine the coordinates information with sample ID
input_pcoa_cordination_metadata <- dplyr::inner_join(weighted_unifrac_cordination,G2F_Metadata_2019,by = c("SampleID"))
input_pcoa_cordination_metadata
#?inner_join()
#exctract % of explaination
pc1 <- as.character(round(weighted_unifrac$data$ProportionExplained[1,1],3)*100)
pc2 <- as.character(round(weighted_unifrac$data$ProportionExplained[1,2],3)*100)
# generate x y axis labels
pc1_label = paste0("PC1","(",pc1,"%)")
pc2_label = paste0("PC2","(",pc2, "%)")
### print out the weighted unifrac
input_pcoa_plot <- ggplot(input_pcoa_cordination_metadata,aes(x=PC1, y=PC2, color=State_group)) +
geom_point(size = 4, alpha = 0.8) + xlab(pc1_label) +
ylab(pc2_label) + scale_colour_manual(values = c("purple","red", "steelblue","darkgreen")) +
theme(axis.title = element_text(size = 10, face = "bold", colour = "grey30"),
panel.background = element_blank(), panel.border = element_rect(fill = NA, colour = "grey30"),
axis.ticks = element_blank(), legend.key = element_blank(),
legend.title = element_blank(),
legend.text = element_text(size = 20, colour = "grey30",face = "bold"),
axis.title.x = element_text(size=30, face="bold"),
axis.title.y = element_text(size=30, face="bold"),
plot.title = element_text(size=30, face="bold"),
legend.position="bottom",
axis.text = element_blank()
)
input_pcoa_plot
#save the weighted unifrac graph
ggsave("./beta_diversity_weighted_unifrac.png", height=10, width=10, device="png")
# run dbrda analysis, function need distance matric, metadata table containing explantory variables, and column range of explantory variables
beta_heritibility_cal(weighted_unifrac,G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location,1,49)
beta_heritibility_cal(unweighted_unifrac,G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location,1,49)
colnames(G2F_metadata_2019_NO_sub_yellow_stripe_sub)
#build the weighted unifrac distance matrix
weighted_unifrac_contribution_table <- as.data.frame(matrix(data=c(0.5071286,0.04521155,0.1668562),nrow=3,ncol=1),row.names = c("Environment","Maize Genotype","GXE"))
colnames(weighted_unifrac_contribution_table) <- c("Value")
weighted_unifrac_contribution_table$type <- c("Weighted")
weighted_unifrac_contribution_table$test_var<- rownames(weighted_unifrac_contribution_table)
weighted_unifrac_contribution_table
#build the unweighted unifrac distance matrix
unweighted_unifrac_contribution_table <- as.data.frame(matrix(data=c(0.439707,0.04488889,0.1985142),nrow=3,ncol=1),row.names = c("Environment","Maize Genotype","GXE"))
colnames(unweighted_unifrac_contribution_table) <- c("Value")
unweighted_unifrac_contribution_table$type <- c("Unweighted")
unweighted_unifrac_contribution_table$test_var<- rownames(unweighted_unifrac_contribution_table)
#merge the wieghted and unweighted unifrac together
unifrac_contribution_table <- dplyr::bind_rows(weighted_unifrac_contribution_table,unweighted_unifrac_contribution_table)
unifrac_contribution_table
#change the column from "alpha diversity to type for dplyr bind
colnames(alpha_result)[1] <- "type"
#add category vairable to beta diversity table
unifrac_contribution_table$category <- "Beta Diversity"
alpha_beta_diversity_table <- dplyr::bind_rows(unifrac_contribution_table,alpha_result)
#graph the contribution of G,E,and GXE on alpha and beta diversity
ggplot(data=unifrac_contribution_table, aes(x=type, y=Value, fill=test_var)) +
geom_bar(position="stack", stat="identity") + ylab("Variance explained") +
scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.8),limits = c(0,0.8)) +
theme(axis.title = element_text(size = 20, face = "bold", colour = "grey30"),
panel.background = element_blank(),legend.title = element_text(size = 30, face = "bold", colour = "grey30"),
) + theme(
legend.text = element_text(color = "black", size = 30),
axis.title.y = element_text(size=30, face="bold"),
plot.title = element_text(size=30, face="bold"),
axis.text.y = element_text(size=30, face="bold"),
axis.text.x = element_text(size=28, face="bold"),
strip.text.x = element_text(size=30,face="bold"),
axis.title.x = element_blank(),
legend.title = element_blank(),
legend.position="bottom"
)
#ggsave("./beta_diversity_weighted_unifrac.png", height=10, width=10, device="png")
#graph the contribution of G,E,and GXE on alpha and beta diversity
ggplot(data=alpha_beta_diversity_table, aes(x=type, y=Value, fill=test_var)) +
geom_bar(position="stack", stat="identity") + ylab("Variance explained") +
scale_y_continuous(breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.8),limits = c(0,0.8)) +
theme(axis.title = element_text(size = 20, face = "bold", colour = "grey30"),
panel.background = element_blank(),legend.title = element_text(size = 30, face = "bold", colour = "grey30"),
) + theme(
legend.text = element_text(color = "black", size = 30),
axis.title.y = element_text(size=30, face="bold"),
plot.title = element_text(size=30, face="bold"),
axis.text.y = element_text(size=30, face="bold"),
axis.text.x = element_text(size=28, face="bold"),
strip.text.x = element_text(size=30,face="bold"),
axis.title.x = element_blank(),
legend.title = element_blank(),
legend.position="bottom"
)
ggsave("./alpha_beta_diversity_weighted_unifrac.png", height=10, width=12, device="png")
#######################################################################
location_core_microbiome <- function(taxonomic_level,prevalence_level){
#create an empty dataframe
core_taxa_list <- as.data.frame(matrix(ncol = 2, nrow = 0))
#add colname
colnames(core_taxa_list) <- c("Taxaname","location")
#change column type so that can be used later
core_taxa_list$Taxaname <- as.character(core_taxa_list$Taxaname)
core_taxa_list$location <- as.character(core_taxa_list$location)
#iterate through each location and get phylumn level taxa that have input prevalence prevalence
for(experiment_field in unique(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$location)){
print(experiment_field)
#create read path
directory_name = paste("./core-metrics-results-dada2_table-no-mitochondria-no-chloroplast-blank-filtered-yellow-stripe-duplicate-pedigree-",experiment_field,"-1500",sep="")
print(directory_name)
path_name = paste(directory_name,"/rarefied_table_",taxonomic_level,"_level_taxonomy_",prevalence_level,"_filtered.qza",sep="")
print(path_name)
#read into qiime 2 qza file and take the data
core_taxa <- read_qza(path_name)
core_taxa <- as.data.frame(core_taxa$data)
core_taxa$Taxaname <- rownames(core_taxa)
core_taxa$location <- experiment_field
#record down the name of core taxa and bind wih
location_core_taxa <- core_taxa[,c("Taxaname","location")]
core_taxa_list <- dplyr::bind_rows(core_taxa_list,location_core_taxa)
}
length(unique(core_taxa_list$Taxaname))
length(unique(core_taxa_list$location))
return(core_taxa_list)
}
core_microbiome_list_to_matrix <- function(core_taxa_list){
#build a dataframe based on core taxa list of all location
core_taxa_matrix <- as.data.frame(matrix(ncol = length(unique(core_taxa_list$location)), nrow = length(unique(core_taxa_list$Taxaname))))
colnames(core_taxa_matrix) <- unique(core_taxa_list$location)
rownames(core_taxa_matrix) <- unique(core_taxa_list$Taxaname)
#make a matrix table of core taxa, if core taxa exist in a location, add 1
for(taxa_name in unique(core_taxa_list$Taxaname)){
print(taxa_name)
temp_location_taxa_combination <- subset(core_taxa_list,Taxaname == taxa_name)
for(location in temp_location_taxa_combination$location){
print(location)
core_taxa_matrix[taxa_name,location] <- 1
}
}
# if core taxa do not present in a location, change na to 0
core_taxa_matrix[is.na(core_taxa_matrix)] <- 0
return(core_taxa_matrix)
}
#core microbiome
##############################################################################################
location_species_core_microbiome_list <- location_core_microbiome(7,0.6)
location_genus_core_microbiome_list <- location_core_microbiome(6,0.6)
location_family_core_microbiome_list <- location_core_microbiome(5,0.6)
location_order_core_microbiome_list <- location_core_microbiome(4,0.6)
location_class_core_microbiome_list <- location_core_microbiome(3,0.6)
location_phylumn_core_microbiome_list <- location_core_microbiome(2,0.6)
unique_location_genus_core_microbiome_list <- location_genus_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() <= 1)
unique_location_family_core_microbiome_list <- location_family_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() <= 1)
unique_location_order_core_microbiome_list <- location_order_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() <= 1)
unique_location_class_core_microbiome_list <- location_class_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() <= 1)
unique_location_phylumn_core_microbiome_list <- location_phylumn_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() <= 1)
common_location_genus_core_microbiome_list <- location_genus_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() >= 10)
common_location_family_core_microbiome_list <- location_family_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() >= 10)
common_location_order_core_microbiome_list <- location_order_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() >= 10)
common_location_class_core_microbiome_list <- location_class_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() >= 10)
common_location_phylumn_core_microbiome_list <- location_phylumn_core_microbiome_list %>% group_by(Taxaname) %>% filter(n() >= 10)
location_genus_core_microbiome_matrix <- core_microbiome_list_to_matrix(location_genus_core_microbiome_list)
location_genus_core_microbiome_matrix
location_family_core_microbiome_matrix <- core_microbiome_list_to_matrix(location_family_core_microbiome_list)
location_family_core_microbiome_matrix
location_family_core_microbiome_list[c('Domain','Phylumn','Class','Order','Family')] <- str_split_fixed(location_family_core_microbiome_list$Taxaname, ';',5)
location_family_core_microbiome_heatmap <-ggplot(location_family_core_microbiome_list, aes(x=location,y=Family)) +
geom_tile() + theme(
axis.text.y = element_text(size=15, face="bold"),
axis.text.x = element_text(size=15,angle=90, face="bold"),
axis.title.x = element_text(size=15,face="bold"),
axis.title.y = element_text(size=15, face="bold")
)
location_family_core_microbiome_heatmap
ggsave("./location_family_core_microbiome_heatmap.png",plot = location_family_core_microbiome_heatmap,core_taxa_plot,height=8, width=12, device="png")
##############################################################################################
Corrected_pedigree_phylumn_60_core_taxa_list <- as.data.frame(matrix(ncol = 2, nrow = 0))
#add colname
colnames(Corrected_pedigree_phylumn_60_core_taxa_list) <- c("Taxaname","Corrected_pedigree")
#change column type so that can be used later
Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname <- as.character(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname)
Corrected_pedigree_phylumn_60_core_taxa_list$Corrected_pedigree <- as.character(Corrected_pedigree_phylumn_60_core_taxa_list$Corrected_pedigree)
#iterate through each Corrected_pedigree and get family level taxa that have 60% prevalence
for(pedigree in unique(G2F_metadata_2019_duplicate_pedigree_ys_filtered_selected_location$Corrected_pedigree)){
#print(pedigree)
pedigree = gsub("\\/","X",pedigree)
print(pedigree)
#create read path
directory_name = paste("./core-metrics-results-dada2_table-no-mitochondria-no-chloroplast-blank-filtered-yellow-stripe-duplicate-pedigree-",pedigree,"-1500",sep="")
print(directory_name)
path_name = paste(directory_name,"/rarefied_table_5_level_taxonomy_0.6_filtered.qza",sep="")
print(path_name)
#read into qiime 2 qza file and take the data
phylumn_60_filtered_core_taxa <- read_qza(path_name)
phylumn_60_filtered_core_taxa <- as.data.frame(phylumn_60_filtered_core_taxa$data)
phylumn_60_filtered_core_taxa$Taxaname <- rownames(phylumn_60_filtered_core_taxa)
phylumn_60_filtered_core_taxa$Corrected_pedigree <- pedigree
#record down the name of core taxa and bind wih
Corrected_pedigree_core_taxa <- phylumn_60_filtered_core_taxa[,c("Taxaname","Corrected_pedigree")]
Corrected_pedigree_phylumn_60_core_taxa_list <- dplyr::bind_rows(Corrected_pedigree_phylumn_60_core_taxa_list,Corrected_pedigree_core_taxa)
}
length(unique(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname))
length(unique(Corrected_pedigree_phylumn_60_core_taxa_list$Corrected_pedigree))
#build a dataframe based on core taxa list of all Corrected_pedigree
Corrected_pedigree_phylumn_60_core_taxa_matrix <- as.data.frame(matrix(ncol = length(unique(Corrected_pedigree_phylumn_60_core_taxa_list$Corrected_pedigree)), nrow = length(unique(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname))))
colnames(Corrected_pedigree_phylumn_60_core_taxa_matrix) <- unique(Corrected_pedigree_phylumn_60_core_taxa_list$Corrected_pedigree)
rownames(Corrected_pedigree_phylumn_60_core_taxa_matrix) <- unique(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname)
#Corrected_pedigree_phylumn_60_core_taxa_matrix["d__Bacteria;p__Proteobacteria","SCH1"]
#make a matrix table of core taxa, if core taxa exist in a Corrected_pedigree, add 1
for(taxa_name in unique(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname)){
print(taxa_name)
temp_Corrected_pedigree_taxa_combination <- subset(Corrected_pedigree_phylumn_60_core_taxa_list,Taxaname == taxa_name)
for(Corrected_pedigree in temp_Corrected_pedigree_taxa_combination$Corrected_pedigree){
print(Corrected_pedigree)
Corrected_pedigree_phylumn_60_core_taxa_matrix[taxa_name,Corrected_pedigree] <- 1
}
}
# if core taxa do not present in a Corrected_pedigree, change na to 0
Corrected_pedigree_phylumn_60_core_taxa_matrix[is.na(Corrected_pedigree_phylumn_60_core_taxa_matrix)] <- 0
Corrected_pedigree_phylumn_60_core_taxa_matrix
Corrected_pedigree_phylumn_60_core_taxa_list
Corrected_pedigree_phylumn_60_core_taxa_list[c('Domain','Phylumn','Class','Order','Family')] <- str_split_fixed(Corrected_pedigree_phylumn_60_core_taxa_list$Taxaname, ';',5)
Corrected_pedigree_family_core_microbiome_heatmap <-ggplot(Corrected_pedigree_phylumn_60_core_taxa_list, aes(x=Corrected_pedigree,y=Family)) +
geom_tile() + theme(
axis.text.y = element_text(size=15, face="bold"),
axis.text.x = element_text(size=15,angle=90, face="bold"),
axis.title.x = element_text(size=15,face="bold"),
axis.title.y = element_text(size=15, face="bold")
)
Corrected_pedigree_family_core_microbiome_heatmap
###########################################################################################################
################################################################