-
Notifications
You must be signed in to change notification settings - Fork 2
/
4.06A_FINAL_RDA_GEA.R
328 lines (212 loc) · 8.94 KB
/
4.06A_FINAL_RDA_GEA.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
####################### VALE INSTITUTE OF TECHNOLOGY ##########################
######################## GEA-GPA TUTORIAL ##########################
######################### STEP 06: COMPARING RESULTS ##########################
### Script prepared by Carolina S. Carvalho, Jeronymo Dalapicolla, Luciana C. Resende-Moreira, Jamille C. Veiga, and Rodolfo Jaffé ###
#PRE-ANALYSIS ------------------------------------------------------------------
#AIMS:
#A. COMPARING CANDIDATES SNPS IDENTIFIED WITH RDA AND LFMM2
##LOAD THE PACKAGES
library(LEA)
library(r2vcftools)
library(vegan)
library(usdm)
library(spdep)
library(adespatial)
library(spacemakeR)
library(ade4)
library(psych)
library(seqinr)
library(robust)
library(ggplot2)
library(ggrepel)
library(VennDiagram)
##5. LOAD FUNCTIONS TO BE USED ON THIS STEP.
VCFsummary <- function(snps){
Nid <- nrow(snps@meta)
Nsnps <- length(snps@site_id)
cat(paste(Nid, "individuals and", Nsnps, "SNPs."), sep="\n")
}
#1. LOAD GENOMIC DATASET-------------------------------------------------------
#Load snps data set to take the snps and individuals ID
snps = vcfLink("vcf/pilocarpus_adap_filtered.vcf", overwriteID=T)
snps@meta
VCFsummary(snps) ## 264 individuals and 19025 SNPs.
## Read geno file
gen.imp = read.geno("vcf/pilocarpus_filtered_imputed.geno")
gen.imp[1:10,1:10]
colnames(gen.imp) = snps@site_id
rownames(gen.imp) = snps@sample_id
dim(gen.imp)
## Number of missing data
sum(is.na(gen.imp)) #0
#2. LOAD GEA RESULTS-----------------------------------------------------------
## Load RDA and LFMM filtered vcf - Steps 4 and 5:
rda_snps = vcfLink("GEA/partial_RDA/snps_candidate_gea_RDA_mahalanobis_pilocarpus_subset.vcf", overwriteID=F) # here overwrite need to be F, otherwise we will loss the snps name
lfmm_snps = vcfLink("GEA/LFMM/snps_candidate_lfmm_GEA_pilocarpus.vcf", overwriteID=F)
all_candidate = unique(c(rda_snps@site_id,lfmm_snps@site_id))
all_candidate_gen = gen.imp[,all_candidate]
dim(all_candidate_gen) #611
inter_candidate = intersect(c(rda_snps@site_id), c(lfmm_snps@site_id))
inter_candidate_gen = gen.imp[,inter_candidate]
dim(inter_candidate_gen) #11
#3. VENN DIAGRAM---------------------------------------------------------------
#select contigs names:
RDA = Chrom(rda_snps)[,1]
LFMM2 = Chrom(lfmm_snps)[,1]
## Creating a Venn Diagram and save it as pdf
pdf("GEA/VennDiagram/VennDiagram_SNPscandidates_GEA_car.pdf", onefile = T)
plot.new()
grid.draw(w1 <- venn.diagram(list(RDA=RDA, LFMM2=LFMM2),
lty = c("blank", "blank"),
fill = c("red", "blue"),
alpha = c(0.5, 0.5), cat.cex = 1.2, cex= 1.5, cat.pos = 0,
filename=NULL ))
dev.off()
#4. LOAD ENVIRONMENTAL DATASET-------------------------------------------------
#loading extracted values per variable in Step 3 - Variables following Carvalho et al 2020:
GEA_variables = read.csv("GEA/Inputs/GEA_vars_filtered.csv", row.names = 1)
head(GEA_variables)
#selecting only variables used in RDA to comparison
names(GEA_variables)
GEA_variables = GEA_variables[, c(1:6)]
#checking VIF:
vif(GEA_variables[, -c(1:2)]) #VIF <2
#Scale the variables:
pred_scale = scale(GEA_variables[,3:6])
#Renames cols and rows to graphics:
names(GEA_variables)
rownames(pred_scale) = GEA_variables[,1]
colnames(pred_scale) = c("H+AL", "SB", "ARIDITY", "BIO_02")
head(pred_scale)
class(pred_scale)
#5. POPULATION QMATRIX DATASET-------------------------------------------------
# Load files
pop = read.csv("data_raw/pop_LEA_DAPC_TESS_pilocarpus.csv", row.names = 1)
metafiles = as.data.frame(snps@meta)
# selecting the individuals:
pop_filtered = pop[pop$ind_ID %in% metafiles$ind_ID,]
#E. Check if there are some difference between files. If they are corrected, function will return "character (0)"
setdiff(as.character(pop_filtered$ind_ID), as.character(GEA_variables$id))
setdiff(as.character(GEA_variables$id), as.character(pop_filtered$ind_ID))
#F. Check if identical samples were selected in the same order. If they are corrected, function will return "TRUE"
identical(as.character(pop_filtered$ind_ID), as.character(GEA_variables$id))
#saving the Qmatrix of sNMF to applied in the RDA
pilo_qmatrix = as.matrix(pop_filtered[, 8:11])
head(pilo_qmatrix)
#6. PARTIAL RDA ANALYSES - ALL CANDIDATES--------------------------------------
## RDA with mahalanobis distance
m.rda = rda(all_candidate_gen ~ pred_scale + Condition(pilo_qmatrix)) #change for intersected candidates
m.rda
options(scipen = 999)
control_vif = as.data.frame(vif.cca(m.rda)) #at least <10 (PRED)
control_vif
#Save the VIF results
write.csv(control_vif, file="GEA/RDA2/control_vif_rda2.csv")
#Quality
RsquareAdj(m.rda)
#r.squared 0.05370897
#adj.r.squared 0.04512067
#Axis
plot(m.rda, scaling=3)
screeplot(m.rda)
summary(eigenvals(m.rda, model= "constrained"))
#for graphics:
AX1 = "RDA1 (47.29%)"
AX2 = "RDA2 (35.57%)"
AX3 = "RDA3 (11.79%)"
AX4 = "RDA4 (5.35%)"
#check our RDA model for significance using formal tests.
signif.full = anova.cca(m.rda, permutations = 1000)
signif.full
# Df Variance F Pr(>F)
# Model 4 14.437 5.791 0.000999 **
# Residual 255 158.932
signif.terms = anova.cca(m.rda, by="terms", permutations = 1000)
signif.terms
# Df Variance F Pr(>F)
# Model 4 14.437 5.791 0.000999 **
# Residual 255 158.932
## Check axis significance
axis_sig = anova.cca(m.rda, by= "axis", permutations = 1000)
axis_sig ## 4 Axes!!
# Df Variance F Pr(>F)
# RDA1 1 6.828 10.9551 0.000999 ***
# RDA2 1 5.136 8.2402 0.000999 ***
# RDA3 1 1.702 2.7303 0.000999 ***
# RDA4 1 0.772 1.2385 0.007992 **
# Residual 255 158.932
## we chose the first 2 axes, which explained 82.87% of the variation
#Save RDA results:
sum_rda = summary(m.rda)
write.csv(as.data.frame(sum_rda$species), file="GEA/RDA2/RDA2_values_SNPs.csv") #snps
write.csv(as.data.frame(sum_rda$sites), file="GEA/RDA2/RDA2_values_individuals.csv") #individuals
write.csv(as.data.frame(sum_rda$biplot), file="GEA/RDA2/RDA2_values_variables.csv") #variables
#7. PLOT RESULTS - ALL CANDIDATES ------------------------------------------------------
#Create objects graphs:
#View analysis results
sum_rda = summary(m.rda)
sp = as.data.frame(sum_rda$species[,1:2])*5 #Selecting 5 axis for RDA #Depending on the drawing result, the drawing data can be enlarged or reduced to a certain extent, as follows
head(sp) #snps
st = as.data.frame(sum_rda$sites[,1:2])
head(st) #individuals
yz = as.data.frame(sum_rda$biplot[,1:2])*5
head(yz)
row.names(yz) = c("H+AL", "SB","ARIDITY", "BIO02") #Correcting variable names
head(yz) #biplot
grp = as.data.frame(pop_filtered$PopID_snmf) #Grouping snps by variable/selecion
colnames(grp)="group" #
head(grp)
colors = c('red', 'blue', 'yellow', "pink") #
legend = c("POP1", "POP2", "POP3", "POP4") #Alphabetichal
#RDA results. It will be added as axes labels:
AX1
AX2
## Axes 1 & 2
pdf("GEA/RDA2/GEA_RDA_IND_ALL.pdf", onefile = T)
ggplot() +
geom_point(data = sp,
mapping = aes(x= RDA1, y = RDA2),
size=1.5, shape=21, fill ="grey", color="grey")+
geom_point(data = st,
mapping = aes(x= RDA1, y = RDA2, fill=factor(grp$group)),
size=2, shape=21)+
scale_fill_manual(values =colors, labels = legend)+
geom_segment(data = yz,aes(x = 0, y = 0, xend = RDA1, yend = RDA2),
arrow = arrow(angle=22.5,length = unit(0.5,"cm"),
type = "closed"),linetype=1, size=0.9,colour = "black")+
geom_label_repel(data = yz,aes(RDA1,RDA2,label=row.names(yz)))+
labs(x=AX1,
y=AX2)+
guides(fill=guide_legend(title="Populations",override.aes = list(size=5)))+
theme_bw()
dev.off()
#Checking localities of clustering individuals in RDA space:
indi = as.data.frame(sum_rda$sites[,1:2])
#RDA1 and RDA2 <-2
names1 = which(indi$RDA1 < -2)
names2 = which(indi$RDA2 < -2)
name_clusters = unique(c(names1, names2))
name_clusters
local_label = snps@meta$local_ID[name_clusters]
local_label
## Axes 1 & 2 with localities labels
pdf("GEA/RDA2/GEA_RDA_IND_ALL_labels.pdf", onefile = T)
ggplot() +
geom_point(data = sp,
mapping = aes(x= RDA1, y = RDA2),
size=1.5, shape=21, fill ="grey", color="grey")+
geom_point(data = st,
mapping = aes(x= RDA1, y = RDA2, fill=factor(grp$group)),
size=2, shape=21)+
scale_fill_manual(values =colors, labels = legend)+
geom_segment(data = yz,aes(x = 0, y = 0, xend = RDA1, yend = RDA2),
arrow = arrow(angle=22.5,length = unit(0.5,"cm"),
type = "closed"),linetype=1, size=0.9,colour = "black")+
geom_label_repel(data = yz,aes(RDA1,RDA2,label=row.names(yz)))+
geom_label_repel(data = st[name_clusters,],aes(RDA1,RDA2,label=local_label))+
labs(x=AX1,
y=AX2)+
guides(fill=guide_legend(title="Populations",override.aes = list(size=5)))+
theme_bw()
dev.off()
## END