-
Notifications
You must be signed in to change notification settings - Fork 0
/
Updated_SOS_gendivcode.R
413 lines (294 loc) · 13.4 KB
/
Updated_SOS_gendivcode.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
###############################################
############### Libraries #####################
###############################################
library(adegenet)
library(poppr)
library(ggplot2)
library(poppr)
library(hierfstat)
library(car)
library(PMCMR)
library(Demerelate)
library(diveRsity)
###############################################
############## Working Directory ##############
###############################################
setwd("G:\\Shared drives\\Emily_Schumacher\\masters")
##load file
master_gendiv <- read.csv("master_gendiv.csv")
gendiv_species <- read.csv("gendiv_byspecies.csv")
gendiv_species <- gendiv_species[-21,-9]
##Stacked barplot
#pdf("stack_bp.pdf")
#ggplot(gendiv_species, aes(x = Ne)) +
# geom_histogram(binwidth = 1, aes(fill = Species)) +
#xlab("Effective Number of Alleles") + xlim(0,6) +
# scale_color_brewer(palette="Blues") +
# scale_fill_brewer(palette="Blues")
#dev.off()
###
na_lm <- lm(master_gendiv$Na ~ master_gendiv$Species*master_gendiv$Accession)
####
setwd("G:\\Shared drives\\Emily_Schumacher\\masters\\masters_gen")
##genind list
sos_genind_list <- list.files(pattern = ".gen$")
##create a list to store genind files
sos_genind <- list()
##poppr list
sos_poppr <- list()
##all rich list
sos_allrich_list <- list()
##make a list for hexp
sos_hexp_list <- list()
##now read in a genepop files
for(i in 1:length(sos_genind_list)){
sos_genind[[i]] <- read.genepop(sos_genind_list[[i]], ncode = 3)
sos_poppr[[i]] <- poppr(sos_genind[[i]])
sos_allrich_list[[i]] <- colMeans(allelic.richness(sos_genind[[i]])$Ar)
sos_hexp_list[[i]] <- sos_poppr[[i]]$Hexp
}
##calculate allelic richness
##species name list
species_names <- unique(master_gendiv$Species)
#trim matrix
master_gendiv <- master_gendiv[,-c(7,10)]
##
##matrix
pvalue_matrix_accessions <- matrix(nrow = length(species_names), ncol = length(master_gendiv[,5:9]))
rownames(pvalue_matrix_accessions) <- species_names
colnames(pvalue_matrix_accessions) <- names(master_gendiv[,5:9])
##tests
##set up p value test
for(p in 1:length(species_names)){
pvalue_matrix_accessions[p,1] <- kruskal.test(master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,5]~master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,2])[[3]]
pvalue_matrix_accessions[p,2] <- kruskal.test(master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,6]~master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,2])[[3]]
pvalue_matrix_accessions[p,3] <- kruskal.test(master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,7]~master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,2])[[3]]
pvalue_matrix_accessions[p,4] <- kruskal.test(master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,8]~master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,2])[[3]]
pvalue_matrix_accessions[p,5] <- kruskal.test(master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,9]~master_gendiv[master_gendiv$Species == paste0(species_names[[p]]),][,2])[[3]]
}
write.csv(pvalue_matrix_accessions,"pvalue_matrix_accessions.csv")
##now create a matrix for by species
pvalue_matrix_species <- matrix(nrow = 1, ncol = length(master_gendiv[,5:9]))
rownames(pvalue_matrix_species) <- "pvalues"
colnames(pvalue_matrix_species) <- c(names(master_gendiv[,5:9]))
pvalue_matrix_species[,1] <- kruskal.test(master_gendiv[,5]~master_gendiv[,1])[[3]]
pvalue_matrix_species[,2] <- kruskal.test(master_gendiv[,6]~master_gendiv[,1])[[3]]
pvalue_matrix_species[,3] <- kruskal.test(master_gendiv[,7]~master_gendiv[,1])[[3]]
pvalue_matrix_species[,4] <- kruskal.test(master_gendiv[,8]~master_gendiv[,1])[[3]]
pvalue_matrix_species[,5] <- kruskal.test(master_gendiv[,9]~master_gendiv[,1])[[3]]
##do multiple comparison tests
posthoc.kruskal.nemenyi.test(master_gendiv[,5]~as.factor(master_gendiv[,1]), data = master_gendiv)
posthoc.kruskal.nemenyi.test(master_gendiv[,6]~as.factor(master_gendiv[,1]), data = master_gendiv)
posthoc.kruskal.nemenyi.test(master_gendiv[,7]~as.factor(master_gendiv[,1]), data = master_gendiv)
posthoc.kruskal.nemenyi.test(master_gendiv[,8]~as.factor(master_gendiv[,1]), data = master_gendiv)
posthoc.kruskal.nemenyi.test(master_gendiv[,9]~as.factor(master_gendiv[,1]), data = master_gendiv)
write.csv(pvalue_matrix_species, "pvalue_byspecies.csv")
##calculate NA by species
na_list <- list()
ne_list <- list()
for(s in 1:length(species_names)){
na_list[[s]] <- mean(master_gendiv[master_gendiv$Species == paste0(species_names[[s]]),][,5])
ne_list[[s]] <- mean(master_gendiv[master_gendiv$Species == paste0(species_names[[s]]),][,6])
}
###############################################
############### Loading Files #################
###############################################
setwd("G:\\My Drive\\Masters")
master_gendiv <- read.csv("master_gendiv.csv")
##set as factor
master_gendiv$Accession <- as.factor(master_gendiv$Accession)
#na_comparisons <- list(c("ACMI", "ASCA"), c("ERUM", "ASCA"),c("GRSQ", "ASCA"))
#ne_comparisons <- list(c("ASCA", "ACMI"), c("ASCA", "CHVI"), c("ERUM", "ASCA"),
# c("GRSQ", "ASCA"))
#ho_comparisons <- list(c("ACMI", "ASCA"), c("ACMI", "CHVI"), c("ACMI", "GRSQ"),
# c("ERUM", "ASCA"), c("CHVI","ERUM"), c("ERNA", "ERUM"),
# c("ERUM","GRSQ"))
#he_comparisons <- list(c("ACMI", "ASCA"), c("ACMI", "CHVI"), c("ERUM", "ASCA"),
# c("GRSQ", "ASCA"))
#f_comparisons <- list(c("ACMI", "CHVI"), c("ACMI","GRSQ"), c("ASCA", "GRSQ"),
# c("CHVI", "ERUM"), c("CHVI", "ERUM"), c("ERUM", "GRSQ"))
####By Species comparisons
##NA
pdf("na_species.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Na)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Actual Number of Alleles") + ggtitle("Actual Number of Alleles Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77"))
##Ne
pdf("ne_species.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ne)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Effective Number of Alleles") + ggtitle("Effective Number of Alleles Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(comparisons = ne_comparisons)
dev.off()
##Ho
pdf("ho_species.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ho)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Observed Heterozygosity") + ggtitle("Observed Heterozygosity") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(comparisons = ho_comparisons)
dev.off()
##He
pdf("he_species.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ho)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(comparisons = he_comparisons)
dev.off()
##F
pdf("f_species.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ho)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Inbreeding Coefficient") + ggtitle("Inbreeding by Species") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(-1,1)
dev.off()
#########By accession
##NA
pdf("na_accession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Na)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Actual Number of Alleles") + ggtitle("Actual Number of Alleles Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77"))
dev.off()
###NE
pdf("ne_accession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ne, fill = Accession)) +
geom_boxplot() + xlab("Species") +
ylab("Effective Number of Alleles") + ggtitle("Effective Number of Alleles Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(aes(group = Accession), label = "p.signif") +
ylim(c(0,20))
dev.off()
####HO
pdf("ho_accession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=Ho)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Observed Heterozygosity") + ggtitle("Observed Heterozygosity Between Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(aes(group = Accession), label = "p.signif")
dev.off()
####He
pdf("he_accession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=He)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Between Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(aes(group = Accession), label = "p.signif")
dev.off()
##F statistic
pdf("f_accession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=F)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Inbreeding Coefficient") + ggtitle("Inbreeding Coefficient Between Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
stat_compare_means(aes(group = Accession), label = "p.signif")
dev.off()
##HEXP
pdf("boxplot_hexp_allspecies.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=He)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") + ylim(0,1) +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Among Species")
dev.off()
##plot within species
pdf("hexp_byaccession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=He)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(c(0,1))
dev.off()
##plot within species
pdf("hexp_byspecies.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=He)) +
geom_boxplot() + xlab("Species") +
ylab("Expected Heterozygosity") + ggtitle("Expected Heterozygosity Among Species") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(c(0,1))
dev.off()
###Inb
pdf("inb_byaccession.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=F)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Inbreeding Coefficient (F)") + ggtitle("Inbreeding Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(c(-1,1))
dev.off()
#by species
pdf("inb_byspecies.pdf", width = 8, height = 6)
ggplot(data = master_gendiv, aes(x=Species, y=F)) +
geom_boxplot() + xlab("Species") +
ylab("Inbreeding Coefficient (F)") + ggtitle("Inbreeding Among Species") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(c(-1,1))
dev.off()
##NA graph
ggplot(data = master_gendiv, aes(x=Species, y=Na)) +
geom_boxplot(aes(fill=Accession)) + xlab("Species") +
ylab("Na") + ggtitle("Na Among Accessions") +
scale_fill_manual(values = c("gray33", "gray48", "gray77"))
##load in diversity simulations
diversity_simulations_path <- "G:\\My Drive\\Masters\\Diversity_Simulations"
setwd(diversity_simulations_path)
##
eff_all <- read.csv("eff_num_all_gendiv.csv")
plot(eff_all[,2]~eff_all[,1])
plot(eff_all[,4]~eff_all[,1], add = TRUE, pch = 1)
#################################################
############# Allelic Richness ##################
#################################################
##load in gen documents
setwd("G:\\My Drive\\Masters\\masters_gen")
##load in genind files
sos_list <- list.files(pattern = ".gen$")
##genind list
sos_genind_list <- list()
species_list <- c("ACMI", "ARTR", "ASCA", "CHVI", "ERNA",
"ERUM", "GRSQ")
##write loop to load in
for(g in 1:length(sos_list)){
sos_genind_list[[g]] <- read.genepop(sos_list[[g]], ncode = 3)
}
##allelic richness
##write out new documents with allelic richness
for(a in 1:length(sos_list)){
temp_ar <- allelic.richness(sos_genind_list[[a]])$Ar
write.csv(temp_ar, paste0(species_list[[a]], "_ar.csv"))
}
##calculate allelic richness
for(a in 1:length(allrich_list)){
temp_ar <- colSums(allelic.richness(sos_genind_list[[a]])$Ar)/length(sos_genind_list[[a]]@loc.n.all)
write.csv(temp_ar, paste0(species_list[[a]], "_allrich_sum.csv"))
}
#by species
pdf("inb_byspecies.pdf", width = 8, height = 6)
ggplot(data = allrich_df, aes(x=Species, y=AllRich)) +
xlab("Species") + geom_point(aes(Accession))
ylab("Allelic Richness") + ggtitle("Allelic Richness By Species") +
scale_fill_manual(values = c("gray33", "gray48", "gray77")) +
ylim(c(-1,1))
dev.off()
##load arp file
setwd("G:\\My Drive\\Masters\\masters_gen")
##ERNA load in
erna_arp <- arp2gen("erna.arp")
##write out all rich
erna_genind <- read.genepop("erna.gen", ncode = 3)
##calculate allrich
erna_ar <- allelic.richness(erna_genind)$Ar
##write ar
write.csv(erna_ar, "erna_ar.csv")
##UPLOAD
setwd("G:\\My Drive\\Masters")
##upload allelic richness
allrich_species <- read.csv("all_species_ar.csv")
##
pdf("allrich_by_accession.pdf", width = 8, height = 6)
ggplot(data = allrich_species, aes(x=Species, y=AllRich)) +
xlab("Species") + geom_boxplot(aes(fill = Accession)) +
ylab("Allelic Richness") + ggtitle("Allelic Richness By Accession") +
scale_fill_manual(values = c("gray33", "gray48", "gray77"))
dev.off()