-
Notifications
You must be signed in to change notification settings - Fork 0
/
gendiversity_visualization_statistics
543 lines (380 loc) · 15.3 KB
/
gendiversity_visualization_statistics
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
###############################################
############### Libraries #####################
###############################################
library(adegenet)
library(poppr)
library(ggplot2)
library(poppr)
library(hierfstat)
library(car)
library(PMCMR)
library(Demerelate)
###############################################
############## Working Directory ##############
###############################################
setwd("G:\\My Drive\\Masters\\masters_gen")
###############################################
############### Loading Files #################
###############################################
##load in .gen files
sos_lists <- list.files(pattern = ".gen")
##now create list
sos_gen <- list()
sos_nomd <- list()
sos_poppr <- list()
##Load in .gen files and remove greater than 25% missing data
for(i in 1:length(sos_lists)){
sos_gen[[i]] <- read.genepop(sos_lists[i], ncode = 3)
sos_nomd[[i]] <- missingno(sos_gen[[i]], type = "geno", cutoff = 0.25, quiet = FALSE, freq = FALSE)
if(length(levels(sos_nomd[[i]]@pop)) > 2){
levels(sos_nomd[[i]]@pop) <- c("Local", "Regional", "Global")
} else {
levels(sos_nomd[[i]]@pop) <- c("Local", "Global")
}
sos_poppr[[i]] <- poppr(sos_nomd[[i]])
}
##need to rename ERUM bc its local1, local2, and global
levels(sos_nomd[[6]]@pop) <- c("Local1", "Local2", "Global")
sos_poppr[[6]]$Pop <- c("Local1", "Local2", "Global", "Total")
##########################################################
################# Number of Alleles ######################
##########################################################
##first calculate number of alleles by species and population
num_all <- list()
for(i in 1:length(sos_nomd)){
num_all[[i]] <- allelic.richness(sos_nomd[[i]])$Ar
}
##then generate matrices for plotting
gendiv_matrix <- matrix(nrow = sum(loci[,1]), ncol = 4)
colnames(gendiv_matrix) <- c("Species","AllRich", "HExp", "Inbreeding")
gendiv_matrix[1:24,1] <- "ACMI"
gendiv_matrix[25:48,1] <- "ARTR"
gendiv_matrix[49:68, 1] <- "ASCA"
gendiv_matrix[69:95,1] <- "CHVI"
gendiv_matrix[96:116,1] <- "ERNA"
gendiv_matrix[117:146,1] <- "ERUM"
gendiv_matrix[147:173,1] <- "GRSQ"
##
species_names <- c("ACMI", "ARTR", "ASCA", "CHVI","ERNA","ERUM","GRSQ")
names(num_all) <- species_names
for(s in species_names){
gendiv_matrix[gendiv_matrix[,1] == s,][,2] <- num_all[[s]]
}
##now plot box plots
pdf("num_all_boxplot.pdf", width = 8, height = 6)
ggplot(data = num_all_bind, aes(x=species, y=Num_Alls)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Number of Alleles") + ggtitle("Number of Alleles Among Accessions") +
scale_fill_manual(values = c("darkgreen", "darkorchid4", "dodgerblue", "darkorchid1"))
dev.off()
##by Species table
pdf("num_all_boxplot_byspecies.pdf", width = 8, height = 6)
ggplot(data = num_all_bind, aes(x=species, y=Num_Alls)) +
geom_boxplot() +
xlab("Species") +
ylab("Number of Alleles") + ggtitle("Number of Alleles by Species")
dev.off()
##by species determine if Shapiro Wilks is failed
shapiro.test(num_all_bind[,2])
##run levene test on ARTR
leveneTest(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##now run Kruskall - Wallis
kruskal.test(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##post-hoc test
posthoc.kruskal.nemenyi.test(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##
species_names <- c("Achillea millefolium", "Artemisia tridentata",
"Astragalus canadensis", "Chrysothamnus viscidiflorus",
"Ericameria nauseosa", "Eriogonum umbellatum", "Grindelia squarrosa")
###name matrix
##
pdf("num_all_bypop.pdf", width = 8, height = 6)
barplot(num_all, beside = T, ylim = c(0, 15), main = "Number of Alleles by Species and Population", col = c("darkorchid1", "dodgerblue", "darkgreen"), cex.names = 0.4, ylab = "Average Number of Alleles")
legend('topright', legend = c("Local", "Regional", "Global"), pch = 16, col = c("darkorchid1", "dodgerblue", "darkgreen"))
dev.off()
##create hexp list
hexp_list <- list()
for(i in 1:length(masters_nomd)){
hexp_list[[i]] <- summary(seppop(masters_nomd[[1]])$Regional)$Hexp
print(shapiro.test(hexp_list[[i]]))
}
##now assemble data frames
##ACMI
##create matrix
hexp_acmi_matrix <- matrix(nrow = 24, ncol = 2)
hexp_acmi_matrix[1:8, 1] <- "Local"
hexp_acmi_matrix[9:17, 1] <- "Regional"
hexp_acmi_matrix[18:24, 1] <- "Global"
hexp_acmi_matrix[1:8, 2] <- summary(seppop(masters_nomd[[1]])$Local)$Hexp
hexp_acmi_matrix[9:16, 2] <- summary(seppop(masters_nomd[[1]])$Regional)$Hexp
hexp_acmi_matrix[17:24, 2] <- summary(seppop(masters_nomd[[1]])$Global)$Hexp
##set col names
colnames(hexp_acmi_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_acmi_matrix <- data.frame(hexp_acmi_matrix)
hexp_acmi_matrix$species <- "ACMI"
##change back to numeric
hexp_acmi_matrix[,2] <- as.numeric(as.character(hexp_acmi_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_acmi_matrix[,2] ~ as.factor(hexp_acmi_matrix[,1]))
##can run an anova but will do kruskal-wallis
kruskal.test(hexp_acmi_matrix[,2] ~ as.factor(hexp_acmi_matrix[,1]))
##ARTR
##create matrix
hexp_artr_matrix <- matrix(nrow = 24, ncol = 2)
hexp_artr_matrix[1:8, 1] <- "Local"
hexp_artr_matrix[9:17, 1] <- "Regional"
hexp_artr_matrix[18:24, 1] <- "Global"
hexp_artr_matrix[1:8, 2] <- summary(seppop(masters_nomd[[2]])$Local)$Hexp
hexp_artr_matrix[9:16, 2] <- summary(seppop(masters_nomd[[2]])$Regional)$Hexp
hexp_artr_matrix[17:24, 2] <- summary(seppop(masters_nomd[[2]])$Global)$Hexp
##set col names
colnames(hexp_artr_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_artr_matrix <- data.frame(hexp_artr_matrix)
hexp_artr_matrix$species <- "ARTR"
##change back to numeric
hexp_artr_matrix[,2] <- as.numeric(as.character(hexp_artr_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_artr_matrix[,2] ~ as.factor(hexp_artr_matrix[,1]))
##can run an anova but will do kruskal-wallis
kruskal.test(hexp_artr_matrix[,2] ~ as.factor(hexp_artr_matrix[,1]))
##ASCA
##create matrix
hexp_asca_matrix <- matrix(nrow = 20, ncol = 2)
hexp_asca_matrix[1:10, 1] <- "Local"
hexp_asca_matrix[11:20, 1] <- "Global"
hexp_asca_matrix[1:10, 2] <- summary(seppop(masters_nomd[[3]])$Local)$Hexp
hexp_asca_matrix[11:20, 2] <- summary(seppop(masters_nomd[[3]])$Regional)$Hexp
##set col names
colnames(hexp_asca_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_asca_matrix <- data.frame(hexp_asca_matrix)
hexp_asca_matrix$species <- "ASCA"
##change back to numeric
hexp_asca_matrix[,2] <- as.numeric(as.character(hexp_asca_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_asca_matrix[,2] ~ as.factor(hexp_asca_matrix[,1]))
##can run an anova but will do kruskal-wallis
wilcox.test(hexp_asca_matrix[,2] ~ as.factor(hexp_asca_matrix[,1]))
##CHVI
##create matrix
hexp_chvi_matrix <- matrix(nrow = 27, ncol = 2)
hexp_chvi_matrix[1:9, 1] <- "Local"
hexp_chvi_matrix[10:18, 1] <- "Regional"
hexp_chvi_matrix[19:27, 1] <- "Global"
hexp_chvi_matrix[1:9, 2] <- summary(seppop(masters_nomd[[4]])$Local)$Hexp
hexp_chvi_matrix[10:18, 2] <- summary(seppop(masters_nomd[[4]])$Regional)$Hexp
hexp_chvi_matrix[19:27, 2] <- summary(seppop(masters_nomd[[4]])$Global)$Hexp
##set col names
colnames(hexp_chvi_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_chvi_matrix <- data.frame(hexp_chvi_matrix)
hexp_chvi_matrix$species <- "CHVI"
##change back to numeric
hexp_chvi_matrix[,2] <- as.numeric(as.character(hexp_chvi_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_chvi_matrix[,2] ~ as.factor(hexp_chvi_matrix[,1]))
##
kruskal.test(hexp_chvi_matrix[,2] ~ as.factor(hexp_chvi_matrix[,1]))
###ERNA
hexp_erna_matrix <- matrix(nrow = 21, ncol = 2)
hexp_erna_matrix[1:7, 1] <- "Local"
hexp_erna_matrix[8:14, 1] <- "Regional"
hexp_erna_matrix[15:21, 1] <- "Global"
hexp_erna_matrix[1:7, 2] <- summary(seppop(masters_nomd[[5]])$Local)$Hexp
hexp_erna_matrix[8:14, 2] <- summary(seppop(masters_nomd[[5]])$Regional)$Hexp
hexp_erna_matrix[15:21, 2] <- summary(seppop(masters_nomd[[5]])$Global)$Hexp
##set col names
colnames(hexp_erna_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_erna_matrix <- data.frame(hexp_erna_matrix)
hexp_erna_matrix$species <- "ERNA"
##change back to numeric
hexp_erna_matrix[,2] <- as.numeric(as.character(hexp_erna_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_erna_matrix[,2] ~ as.factor(hexp_erna_matrix[,1]))
##
kruskal.test(hexp_erna_matrix[,2] ~ as.factor(hexp_erna_matrix[,1]))
##ERUM
##create matrix
hexp_erum_matrix <- matrix(nrow = 30, ncol = 2)
hexp_erum_matrix[1:10, 1] <- "Local"
hexp_erum_matrix[11:20, 1] <- "Local2"
hexp_erum_matrix[21:30, 1] <- "Global"
hexp_erum_matrix[1:10, 2] <- summary(seppop(masters_nomd[[6]])$Local)$Hexp
hexp_erum_matrix[11:20, 2] <- summary(seppop(masters_nomd[[6]])$Regional)$Hexp
hexp_erum_matrix[21:30, 2] <- summary(seppop(masters_nomd[[6]])$Global)$Hexp
##set col names
colnames(hexp_erum_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_erum_matrix <- data.frame(hexp_erum_matrix)
hexp_erum_matrix$species <- "ERUM"
##change back to numeric
hexp_erum_matrix[,2] <- as.numeric(as.character(hexp_erum_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_erum_matrix[,2] ~ as.factor(hexp_erum_matrix[,1]))
##
kruskal.test(hexp_erum_matrix[,2] ~ as.factor(hexp_erum_matrix[,1]))
##GRSQ
##create matrix
hexp_grsq_matrix <- matrix(nrow = 27, ncol = 2)
hexp_grsq_matrix[1:9, 1] <- "Local"
hexp_grsq_matrix[10:18, 1] <- "Regional"
hexp_grsq_matrix[19:27, 1] <- "Global"
hexp_grsq_matrix[1:9, 2] <- summary(seppop(masters_nomd[[7]])$Local)$Hexp
hexp_grsq_matrix[10:18, 2] <- summary(seppop(masters_nomd[[7]])$Regional)$Hexp
hexp_grsq_matrix[19:27, 2] <- summary(seppop(masters_nomd[[7]])$Global)$Hexp
##set col names
colnames(hexp_grsq_matrix) <- c("Accession", "Hexp")
##new column with species name
hexp_grsq_matrix <- data.frame(hexp_grsq_matrix)
hexp_grsq_matrix$species <- "GRSQ"
##change back to numeric
hexp_grsq_matrix[,2] <- as.numeric(as.character(hexp_grsq_matrix[,2]))
##run levene test on CHVI
leveneTest(hexp_grsq_matrix[,2] ~ as.factor(hexp_grsq_matrix[,1]))
##
kruskal.test(hexp_grsq_matrix[,2] ~ as.factor(hexp_grsq_matrix[,1]))
##plot Hexp
hexp_bind <- rbind(hexp_acmi_matrix, hexp_artr_matrix, hexp_asca_matrix,
hexp_chvi_matrix, hexp_erna_matrix, hexp_erum_matrix,
hexp_grsq_matrix)
##now plot box plots
pdf("hexp_boxplot.pdf", width = 8, height = 6)
ggplot(data = hexp_bind, aes(x=species, y=Hexp)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Among Accessions") +
scale_fill_manual(values = c("darkgreen", "darkorchid4", "dodgerblue", "darkorchid1")) +
ylim(c(0,1))
dev.off()
##load F coefficients
setwd("G:\\My Drive\\Masters")
fis_species <- read.csv("fis_allspecies.csv")
acmi_fis <- fis_species[fis_species$Species == "ACMI",]
artr_fis <- fis_species[fis_species$Species == "ARTR",]
asca_fis <- fis_species[fis_species$Species == "ASCA",]
chvi_fis <- fis_species[fis_species$Species == "CHVI",]
erna_fis <- fis_species[fis_species$Species == "ERNA",]
erum_fis <- fis_species[fis_species$Species == "ERUM",]
grsq_fis <- fis_species[fis_species$Species == "GRSQ",]
##now run tests on every species
##shapiro
shapiro.test(acmi_fis[,3])
##levene
leveneTest(acmi_fis[,3] ~ as.factor(acmi_fis[,1]))
##ACMI ANOVA
acmi_aov <- aov(acmi_fis[,3] ~ as.factor(acmi_fis[,1]))
summary(acmi_aov)
##ARTR
##shapiro
shapiro.test(artr_fis[,3])
##levene
leveneTest(artr_fis[,3] ~ as.factor(artr_fis[,1]))
##anova
artr_aov <- aov(artr_fis[,3] ~ as.factor(artr_fis[,1]))
summary(artr_aov)
##ASCA
##shapiro
shapiro.test(asca_fis[,3])
##levene
leveneTest(asca_fis[,3] ~ as.factor(asca_fis[,1]))
##anova
asca_aov <- aov(asca_fis[,3] ~ as.factor(asca_fis[,1]))
summary(asca_aov)
##CHVI
##shapiro
shapiro.test(chvi_fis[,3])
##levene
leveneTest(chvi_fis[,3] ~ as.factor(chvi_fis[,1]))
##anova
chvi_aov <- aov(chvi_fis[,3] ~ as.factor(chvi_fis[,1]))
summary(chvi_aov)
##ERNA
##shapiro
shapiro.test(erna_fis[,3])
##levene
leveneTest(erna_fis[,3] ~ as.factor(erna_fis[,1]))
##anova
erna_aov <- aov(erna_fis[,3] ~ as.factor(erna_fis[,1]))
summary(erna_aov)
##post hoc
posthoc.kruskal.nemenyi.test(erna_fis[,3] ~ as.factor(erna_fis[,1]))
##ERUM
erum_fis[erum_fis$Accession == "Regional",][,1] <- NA
is.na(erum_fis) <- "Local2"
##shapiro
shapiro.test(erum_fis[,3])
##levene
leveneTest(erum_fis[,3] ~ as.factor(erum_fis[,1]))
##anova
erum_aov <- aov(erum_fis[,3] ~ as.factor(erum_fis[,1]))
summary(erum_aov)
##GRSQ
##shapiro
shapiro.test(grsq_fis[,3])
##levene
leveneTest(grsq_fis[,3] ~ as.factor(grsq_fis[,1]))
##anova
grsq_aov <- aov(grsq_fis[,3] ~ as.factor(grsq_fis[,1]))
summary(grsq_aov)
##Compare by species
##shapiro
shapiro.test(fis_species[,3])
##levene
leveneTest(fis_species[,3] ~ as.factor(fis_species[,2]))
##anova
kruskal.test(fis_species[,3] ~ as.factor(fis_species[,2]))
##differences by species
posthoc.kruskal.nemenyi.test(fis_species[,3] ~ as.factor(fis_species[,2]))
##now plot box plots
pdf("inbreeding_boxplot.pdf", width = 8, height = 6)
ggplot(data = fis_species, aes(x=Species, y=Fis)) + geom_hline(yintercept = 0) + geom_hline(yintercept = 0.5)+
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Inbreeding Coefficients") + ggtitle("Average Inbreeding Among Accessions") +
scale_fill_manual(values = c("darkgreen", "darkorchid4", "darkorchid1", "dodgerblue")) +
ylim(c(-1,1))
dev.off()
##calculate between species avg inbreeding
shapiro.test(fis_species[,3])
leveneTest(fis_species[,3] ~ as.factor(fis_species[,2]))
kruskal.test(fis_species[,3] ~ as.factor(fis_species[,2]))
##differences by species
posthoc.kruskal.nemenyi.test(fis_species[,3] ~ as.factor(fis_species[,2]))
##Between species diversity
pdf("boxplot_inbreeding_amongspecies.pdf", width = 8, height = 6)
ggplot(data = fis_species, aes(x=Species, y=Fis)) + geom_hline(yintercept = 0) +
geom_boxplot() + xlab("Species") +
ylab("Inbreeding Coefficients") + ggtitle("Inbreeding Among Species") +
ylim(c(-1,1))
dev.off()
##Hexp between species
hexp_allspecies <- rbind(hexp_acmi_matrix, hexp_artr_matrix, hexp_asca_matrix,
hexp_chvi_matrix, hexp_erna_matrix, hexp_erum_matrix,
hexp_grsq_matrix)
##Hexp assumption tests
shapiro.test(hexp_allspecies[,2])
leveneTest(hexp_allspecies[,2] ~ as.factor(hexp_allspecies[,3]))
##Kruskal wallis time
kruskal.test(hexp_allspecies[,2] ~ as.factor(hexp_allspecies[,3]))
##differences by species
posthoc.kruskal.nemenyi.test(hexp_allspecies[,2] ~ as.factor(hexp_allspecies[,3]))
###Now plot!
pdf("boxplot_hexp_allspecies.pdf", width = 8, height = 6)
ggplot(data = hexp_allspecies, aes(x=species, y=Hexp)) +
geom_boxplot() + xlab("Species") + ylim(0,1) +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Among Species")
dev.off()
##By species number of alleles
shapiro.test(num_all_bind[,2])
leveneTest(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##Kruskal wallis time
kruskal.test(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##differences by species
posthoc.kruskal.nemenyi.test(num_all_bind[,2] ~ as.factor(num_all_bind[,3]))
##Now plot
pdf("boxplot_num_all_allspecies.pdf", width = 8, height = 6)
ggplot(data = num_all_bind, aes(x=species, y=Num_Alls)) + ylim(0,25) +
geom_boxplot() + xlab("Species") +
ylab("Number of Alleles") + ggtitle("Number of Alleles by Species")
dev.off()